diff options
author | Glenn L McGrath <bug1@ihug.co.nz> | 2001-07-18 15:47:21 +0000 |
---|---|---|
committer | Glenn L McGrath <bug1@ihug.co.nz> | 2001-07-18 15:47:21 +0000 |
commit | c3fbec73fb45997918bef927cea519866e1e1c9d (patch) | |
tree | 5c1fd9758c4c9894bb050d9c152e1735bc007536 /libbb | |
parent | 8d3b0497a4d8866f0cafd873f241971c2871d580 (diff) | |
download | busybox-w32-c3fbec73fb45997918bef927cea519866e1e1c9d.tar.gz busybox-w32-c3fbec73fb45997918bef927cea519866e1e1c9d.tar.bz2 busybox-w32-c3fbec73fb45997918bef927cea519866e1e1c9d.zip |
Change read_package_field interface, and rewrite using low level functions
Fixes for a few bugs that have crept into dpkg in the last few days
Diffstat (limited to 'libbb')
-rw-r--r-- | libbb/libbb.h | 2 | ||||
-rw-r--r-- | libbb/read_package_field.c | 90 |
2 files changed, 78 insertions, 14 deletions
diff --git a/libbb/libbb.h b/libbb/libbb.h index a9c1a870f..bf5f0c1c5 100644 --- a/libbb/libbb.h +++ b/libbb/libbb.h | |||
@@ -245,7 +245,7 @@ char *unarchive(FILE *src_stream, FILE *out_stream, file_header_t *(*get_header) | |||
245 | const int extract_function, const char *prefix, char **extract_names); | 245 | const int extract_function, const char *prefix, char **extract_names); |
246 | char *deb_extract(const char *package_filename, FILE *out_stream, const int extract_function, | 246 | char *deb_extract(const char *package_filename, FILE *out_stream, const int extract_function, |
247 | const char *prefix, const char *filename); | 247 | const char *prefix, const char *filename); |
248 | char *read_package_field(const char *package_buffer); | 248 | int read_package_field(const char *package_buffer, char **field_name, char **field_value); |
249 | char *fgets_str(FILE *file, const char *terminating_string); | 249 | char *fgets_str(FILE *file, const char *terminating_string); |
250 | 250 | ||
251 | extern int unzip(FILE *l_in_file, FILE *l_out_file); | 251 | extern int unzip(FILE *l_in_file, FILE *l_out_file); |
diff --git a/libbb/read_package_field.c b/libbb/read_package_field.c index 3715230fe..f561df831 100644 --- a/libbb/read_package_field.c +++ b/libbb/read_package_field.c | |||
@@ -3,25 +3,89 @@ | |||
3 | #include "libbb.h" | 3 | #include "libbb.h" |
4 | 4 | ||
5 | /* | 5 | /* |
6 | * Returns a [multi-line] package field | 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 | ||
7 | */ | 8 | */ |
8 | extern char *read_package_field(const char *package_buffer) | 9 | int read_package_field(const char *package_buffer, char **field_name, char **field_value) |
9 | { | 10 | { |
10 | int field_length = 0; | 11 | int offset_name_start = 0; |
11 | int buffer_length = 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; | ||
12 | 20 | ||
13 | if (package_buffer == NULL) { | 21 | if (package_buffer == NULL) { |
14 | return(NULL); | 22 | *field_name = NULL; |
23 | *field_value = NULL; | ||
24 | return(-1); | ||
15 | } | 25 | } |
16 | buffer_length = strlen(package_buffer); | 26 | while (1) { |
17 | field_length = strcspn(package_buffer, "\n"); | 27 | next_offset = offset + 1; |
18 | while (field_length < buffer_length) { | 28 | switch (package_buffer[offset]) { |
19 | if (package_buffer[field_length + 1] != ' ') { | 29 | case('\0'): |
20 | return(xstrndup(package_buffer, field_length)); | 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; | ||
21 | } | 56 | } |
22 | field_length++; | 57 | if (exit_flag == TRUE) { |
23 | field_length += strcspn(&package_buffer[field_length], "\n"); | 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; | ||
24 | } | 88 | } |
25 | return(xstrdup(package_buffer)); | 89 | return(next_offset); |
26 | } | 90 | } |
27 | 91 | ||