blob: 9e559034738d4cc86fbdb92fbe1001d2893d5e04 [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 *
"Robert P. J. Day"5d8843e2006-07-10 11:41:19 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Eric Andersenbdfd0d72001-10-24 05:00:29 +00009 */
10
Glenn L McGrathee0d93e2001-04-21 10:26:15 +000011#include <stdlib.h>
Glenn L McGrath5faef742001-04-15 12:38:12 +000012#include <string.h>
13#include "libbb.h"
14
15/*
Glenn L McGrathc3fbec72001-07-18 15:47:21 +000016 * 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 McGrath5faef742001-04-15 12:38:12 +000018 */
Glenn L McGrathc3fbec72001-07-18 15:47:21 +000019int read_package_field(const char *package_buffer, char **field_name, char **field_value)
Glenn L McGrath5faef742001-04-15 12:38:12 +000020{
Glenn L McGrathc3fbec72001-07-18 15:47:21 +000021 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 McGrath5faef742001-04-15 12:38:12 +000030
Glenn L McGrath481d19b2001-07-11 15:43:03 +000031 if (package_buffer == NULL) {
Glenn L McGrathc3fbec72001-07-18 15:47:21 +000032 *field_name = NULL;
33 *field_value = NULL;
34 return(-1);
Glenn L McGrath5faef742001-04-15 12:38:12 +000035 }
Glenn L McGrathc3fbec72001-07-18 15:47:21 +000036 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 McGrath481d19b2001-07-11 15:43:03 +000066 }
Matt Kraai1f0c4362001-12-20 23:13:26 +000067 if (exit_flag) {
Glenn L McGrathc3fbec72001-07-18 15:47:21 +000068 /* 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 McGrath481d19b2001-07-11 15:43:03 +000088 }
Glenn L McGrathc3fbec72001-07-18 15:47:21 +000089 if (name_length == 0) {
90 *field_name = NULL;
91 } else {
Manuel Novoa III cad53642003-03-19 09:13:01 +000092 *field_name = bb_xstrndup(&package_buffer[offset_name_start], name_length);
Glenn L McGrathc3fbec72001-07-18 15:47:21 +000093 }
94 if (value_length > 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000095 *field_value = bb_xstrndup(&package_buffer[offset_value_start], value_length);
Glenn L McGrathc3fbec72001-07-18 15:47:21 +000096 } else {
97 *field_value = NULL;
98 }
99 return(next_offset);
Glenn L McGrath5faef742001-04-15 12:38:12 +0000100}
101