blob: f561df8311b10c600478654cf506bcbb2aeb1a02 [file] [log] [blame]
Glenn L McGrathee0d93e2001-04-21 10:26:15 +00001#include <stdlib.h>
Glenn L McGrath5faef742001-04-15 12:38:12 +00002#include <string.h>
3#include "libbb.h"
4
5/*
Glenn L McGrathc3fbec72001-07-18 15:47:21 +00006 * Gets the next package field from package_buffer, seperated into the field name
7 * and field value, it returns the int offset to the first character of the next field
Glenn L McGrath5faef742001-04-15 12:38:12 +00008 */
Glenn L McGrathc3fbec72001-07-18 15:47:21 +00009int read_package_field(const char *package_buffer, char **field_name, char **field_value)
Glenn L McGrath5faef742001-04-15 12:38:12 +000010{
Glenn L McGrathc3fbec72001-07-18 15:47:21 +000011 int offset_name_start = 0;
12 int offset_name_end = 0;
13 int offset_value_start = 0;
14 int offset_value_end = 0;
15 int offset = 0;
16 int next_offset;
17 int name_length;
18 int value_length;
19 int exit_flag = FALSE;
Glenn L McGrath5faef742001-04-15 12:38:12 +000020
Glenn L McGrath481d19b2001-07-11 15:43:03 +000021 if (package_buffer == NULL) {
Glenn L McGrathc3fbec72001-07-18 15:47:21 +000022 *field_name = NULL;
23 *field_value = NULL;
24 return(-1);
Glenn L McGrath5faef742001-04-15 12:38:12 +000025 }
Glenn L McGrathc3fbec72001-07-18 15:47:21 +000026 while (1) {
27 next_offset = offset + 1;
28 switch (package_buffer[offset]) {
29 case('\0'):
30 exit_flag = TRUE;
31 break;
32 case(':'):
33 if (offset_name_end == 0) {
34 offset_name_end = offset;
35 offset_value_start = next_offset;
36 }
37 /* TODO: Name might still have trailing spaces if ':' isnt
38 * immediately after name */
39 break;
40 case('\n'):
41 /* TODO: The char next_offset may be out of bounds */
42 if (package_buffer[next_offset] != ' ') {
43 exit_flag = TRUE;
44 break;
45 }
46 case('\t'):
47 case(' '):
48 /* increment the value start point if its a just filler */
49 if (offset_name_start == offset) {
50 offset_name_start++;
51 }
52 if (offset_value_start == offset) {
53 offset_value_start++;
54 }
55 break;
Glenn L McGrath481d19b2001-07-11 15:43:03 +000056 }
Glenn L McGrathc3fbec72001-07-18 15:47:21 +000057 if (exit_flag == TRUE) {
58 /* Check that the names are valid */
59 offset_value_end = offset;
60 name_length = offset_name_end - offset_name_start;
61 value_length = offset_value_end - offset_value_start;
62 if (name_length == 0) {
63 break;
64 }
65 if ((name_length > 0) && (value_length > 0)) {
66 break;
67 }
68
69 /* If not valid, start fresh with next field */
70 exit_flag = FALSE;
71 offset_name_start = offset + 1;
72 offset_name_end = 0;
73 offset_value_start = offset + 1;
74 offset_value_end = offset + 1;
75 offset++;
76 }
77 offset++;
Glenn L McGrath481d19b2001-07-11 15:43:03 +000078 }
Glenn L McGrathc3fbec72001-07-18 15:47:21 +000079 if (name_length == 0) {
80 *field_name = NULL;
81 } else {
82 *field_name = xstrndup(&package_buffer[offset_name_start], name_length);
83 }
84 if (value_length > 0) {
85 *field_value = xstrndup(&package_buffer[offset_value_start], value_length);
86 } else {
87 *field_value = NULL;
88 }
89 return(next_offset);
Glenn L McGrath5faef742001-04-15 12:38:12 +000090}
91