Glenn L McGrath | ee0d93e | 2001-04-21 10:26:15 +0000 | [diff] [blame] | 1 | #include <stdlib.h> |
Glenn L McGrath | 5faef74 | 2001-04-15 12:38:12 +0000 | [diff] [blame] | 2 | #include <string.h> |
| 3 | #include "libbb.h" |
| 4 | |
| 5 | /* |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame] | 6 | * 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 McGrath | 5faef74 | 2001-04-15 12:38:12 +0000 | [diff] [blame] | 8 | */ |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame] | 9 | int read_package_field(const char *package_buffer, char **field_name, char **field_value) |
Glenn L McGrath | 5faef74 | 2001-04-15 12:38:12 +0000 | [diff] [blame] | 10 | { |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame] | 11 | 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 McGrath | 5faef74 | 2001-04-15 12:38:12 +0000 | [diff] [blame] | 20 | |
Glenn L McGrath | 481d19b | 2001-07-11 15:43:03 +0000 | [diff] [blame] | 21 | if (package_buffer == NULL) { |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame] | 22 | *field_name = NULL; |
| 23 | *field_value = NULL; |
| 24 | return(-1); |
Glenn L McGrath | 5faef74 | 2001-04-15 12:38:12 +0000 | [diff] [blame] | 25 | } |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame] | 26 | 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 McGrath | 481d19b | 2001-07-11 15:43:03 +0000 | [diff] [blame] | 56 | } |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame] | 57 | 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 McGrath | 481d19b | 2001-07-11 15:43:03 +0000 | [diff] [blame] | 78 | } |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame] | 79 | 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 McGrath | 5faef74 | 2001-04-15 12:38:12 +0000 | [diff] [blame] | 90 | } |
| 91 | |