blob: 4292689ca9d9681ae861ebaadae10048dba98aed [file] [log] [blame]
Eric Andersenbdfd0d72001-10-24 05:00:29 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) many different people.
Eric Andersencb81e642003-07-14 21:21:08 +00006 * If you wrote this, please acknowledge your work.
Eric Andersenbdfd0d72001-10-24 05:00:29 +00007 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21 * USA
22 */
23
Glenn L McGrathee0d93e2001-04-21 10:26:15 +000024#include <stdlib.h>
Glenn L McGrath5faef742001-04-15 12:38:12 +000025#include <string.h>
26#include "libbb.h"
27
28/*
Glenn L McGrathc3fbec72001-07-18 15:47:21 +000029 * Gets the next package field from package_buffer, seperated into the field name
30 * and field value, it returns the int offset to the first character of the next field
Glenn L McGrath5faef742001-04-15 12:38:12 +000031 */
Glenn L McGrathc3fbec72001-07-18 15:47:21 +000032int read_package_field(const char *package_buffer, char **field_name, char **field_value)
Glenn L McGrath5faef742001-04-15 12:38:12 +000033{
Glenn L McGrathc3fbec72001-07-18 15:47:21 +000034 int offset_name_start = 0;
35 int offset_name_end = 0;
36 int offset_value_start = 0;
37 int offset_value_end = 0;
38 int offset = 0;
39 int next_offset;
40 int name_length;
41 int value_length;
42 int exit_flag = FALSE;
Glenn L McGrath5faef742001-04-15 12:38:12 +000043
Glenn L McGrath481d19b2001-07-11 15:43:03 +000044 if (package_buffer == NULL) {
Glenn L McGrathc3fbec72001-07-18 15:47:21 +000045 *field_name = NULL;
46 *field_value = NULL;
47 return(-1);
Glenn L McGrath5faef742001-04-15 12:38:12 +000048 }
Glenn L McGrathc3fbec72001-07-18 15:47:21 +000049 while (1) {
50 next_offset = offset + 1;
51 switch (package_buffer[offset]) {
52 case('\0'):
53 exit_flag = TRUE;
54 break;
55 case(':'):
56 if (offset_name_end == 0) {
57 offset_name_end = offset;
58 offset_value_start = next_offset;
59 }
60 /* TODO: Name might still have trailing spaces if ':' isnt
61 * immediately after name */
62 break;
63 case('\n'):
64 /* TODO: The char next_offset may be out of bounds */
65 if (package_buffer[next_offset] != ' ') {
66 exit_flag = TRUE;
67 break;
68 }
69 case('\t'):
70 case(' '):
71 /* increment the value start point if its a just filler */
72 if (offset_name_start == offset) {
73 offset_name_start++;
74 }
75 if (offset_value_start == offset) {
76 offset_value_start++;
77 }
78 break;
Glenn L McGrath481d19b2001-07-11 15:43:03 +000079 }
Matt Kraai1f0c4362001-12-20 23:13:26 +000080 if (exit_flag) {
Glenn L McGrathc3fbec72001-07-18 15:47:21 +000081 /* Check that the names are valid */
82 offset_value_end = offset;
83 name_length = offset_name_end - offset_name_start;
84 value_length = offset_value_end - offset_value_start;
85 if (name_length == 0) {
86 break;
87 }
88 if ((name_length > 0) && (value_length > 0)) {
89 break;
90 }
91
92 /* If not valid, start fresh with next field */
93 exit_flag = FALSE;
94 offset_name_start = offset + 1;
95 offset_name_end = 0;
96 offset_value_start = offset + 1;
97 offset_value_end = offset + 1;
98 offset++;
99 }
100 offset++;
Glenn L McGrath481d19b2001-07-11 15:43:03 +0000101 }
Glenn L McGrathc3fbec72001-07-18 15:47:21 +0000102 if (name_length == 0) {
103 *field_name = NULL;
104 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000105 *field_name = bb_xstrndup(&package_buffer[offset_name_start], name_length);
Glenn L McGrathc3fbec72001-07-18 15:47:21 +0000106 }
107 if (value_length > 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000108 *field_value = bb_xstrndup(&package_buffer[offset_value_start], value_length);
Glenn L McGrathc3fbec72001-07-18 15:47:21 +0000109 } else {
110 *field_value = NULL;
111 }
112 return(next_offset);
Glenn L McGrath5faef742001-04-15 12:38:12 +0000113}
114