Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) many different people. |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 6 | * If you wrote this, please acknowledge your work. |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 7 | * |
"Robert P. J. Day" | 5d8843e | 2006-07-10 11:41:19 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | bdfd0d7 | 2001-10-24 05:00:29 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Glenn L McGrath | ee0d93e | 2001-04-21 10:26:15 +0000 | [diff] [blame] | 11 | #include <stdlib.h> |
Glenn L McGrath | 5faef74 | 2001-04-15 12:38:12 +0000 | [diff] [blame] | 12 | #include <string.h> |
| 13 | #include "libbb.h" |
| 14 | |
| 15 | /* |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame] | 16 | * Gets the next package field from package_buffer, seperated into the field name |
| 17 | * 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] | 18 | */ |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame] | 19 | 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] | 20 | { |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame] | 21 | int offset_name_start = 0; |
| 22 | int offset_name_end = 0; |
| 23 | int offset_value_start = 0; |
| 24 | int offset_value_end = 0; |
| 25 | int offset = 0; |
| 26 | int next_offset; |
| 27 | int name_length; |
| 28 | int value_length; |
| 29 | int exit_flag = FALSE; |
Glenn L McGrath | 5faef74 | 2001-04-15 12:38:12 +0000 | [diff] [blame] | 30 | |
Glenn L McGrath | 481d19b | 2001-07-11 15:43:03 +0000 | [diff] [blame] | 31 | if (package_buffer == NULL) { |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame] | 32 | *field_name = NULL; |
| 33 | *field_value = NULL; |
| 34 | return(-1); |
Glenn L McGrath | 5faef74 | 2001-04-15 12:38:12 +0000 | [diff] [blame] | 35 | } |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame] | 36 | while (1) { |
| 37 | next_offset = offset + 1; |
| 38 | switch (package_buffer[offset]) { |
| 39 | case('\0'): |
| 40 | exit_flag = TRUE; |
| 41 | break; |
| 42 | case(':'): |
| 43 | if (offset_name_end == 0) { |
| 44 | offset_name_end = offset; |
| 45 | offset_value_start = next_offset; |
| 46 | } |
| 47 | /* TODO: Name might still have trailing spaces if ':' isnt |
| 48 | * immediately after name */ |
| 49 | break; |
| 50 | case('\n'): |
| 51 | /* TODO: The char next_offset may be out of bounds */ |
| 52 | if (package_buffer[next_offset] != ' ') { |
| 53 | exit_flag = TRUE; |
| 54 | break; |
| 55 | } |
| 56 | case('\t'): |
| 57 | case(' '): |
| 58 | /* increment the value start point if its a just filler */ |
| 59 | if (offset_name_start == offset) { |
| 60 | offset_name_start++; |
| 61 | } |
| 62 | if (offset_value_start == offset) { |
| 63 | offset_value_start++; |
| 64 | } |
| 65 | break; |
Glenn L McGrath | 481d19b | 2001-07-11 15:43:03 +0000 | [diff] [blame] | 66 | } |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 67 | if (exit_flag) { |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame] | 68 | /* Check that the names are valid */ |
| 69 | offset_value_end = offset; |
| 70 | name_length = offset_name_end - offset_name_start; |
| 71 | value_length = offset_value_end - offset_value_start; |
| 72 | if (name_length == 0) { |
| 73 | break; |
| 74 | } |
| 75 | if ((name_length > 0) && (value_length > 0)) { |
| 76 | break; |
| 77 | } |
| 78 | |
| 79 | /* If not valid, start fresh with next field */ |
| 80 | exit_flag = FALSE; |
| 81 | offset_name_start = offset + 1; |
| 82 | offset_name_end = 0; |
| 83 | offset_value_start = offset + 1; |
| 84 | offset_value_end = offset + 1; |
| 85 | offset++; |
| 86 | } |
| 87 | offset++; |
Glenn L McGrath | 481d19b | 2001-07-11 15:43:03 +0000 | [diff] [blame] | 88 | } |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame] | 89 | if (name_length == 0) { |
| 90 | *field_name = NULL; |
| 91 | } else { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 92 | *field_name = bb_xstrndup(&package_buffer[offset_name_start], name_length); |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame] | 93 | } |
| 94 | if (value_length > 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 95 | *field_value = bb_xstrndup(&package_buffer[offset_value_start], value_length); |
Glenn L McGrath | c3fbec7 | 2001-07-18 15:47:21 +0000 | [diff] [blame] | 96 | } else { |
| 97 | *field_value = NULL; |
| 98 | } |
| 99 | return(next_offset); |
Glenn L McGrath | 5faef74 | 2001-04-15 12:38:12 +0000 | [diff] [blame] | 100 | } |
| 101 | |