1#include <stdlib.h> 2#include <string.h> 3#include "libbb.h" 4 5/* 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 8 */ 9int read_package_field(const char *package_buffer, char **field_name, char **field_value) 10{ 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; 20 21 if (package_buffer == NULL) { 22 *field_name = NULL; 23 *field_value = NULL; 24 return(-1); 25 } 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; 56 } 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++; 78 } 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); 90} 91 92