1 --- a/libopkg/parse_util.c
2 +++ b/libopkg/parse_util.c
4 #include "libbb/libbb.h"
6 #include "parse_util.h"
7 +#include "pkg_parse.h"
10 is_field(const char *type, const char *line)
11 @@ -86,3 +87,84 @@ parse_list(const char *raw, unsigned int
17 +parse_from_stream_nomalloc(parse_line_t parse_line, void *item, FILE *fp, uint mask,
18 + char **buf0, size_t buf0len)
32 + if (fgets(buf, (int)buflen, fp) == NULL) {
34 + opkg_perror(ERROR, "fgets");
36 + } else if (strlen(*buf0) == buf0len-1) {
37 + opkg_msg(ERROR, "Missing new line character"
38 + " at end of file!\n");
39 + parse_line(item, *buf0, mask);
44 + nl = strchr(buf, '\n');
46 + if (strlen(buf) < buflen-1) {
48 + * Line could be exactly buflen-1 long and
49 + * missing a newline, but we won't know until
50 + * fgets fails to read more data.
52 + opkg_msg(ERROR, "Missing new line character"
53 + " at end of file!\n");
54 + parse_line(item, *buf0, mask);
57 + if (buf0len >= EXCESSIVE_LINE_LEN) {
58 + opkg_msg(ERROR, "Excessively long line at "
59 + "%d. Corrupt file?\n",
66 + * Realloc and point buf past the data already read,
67 + * at the NULL terminator inserted by fgets.
68 + * |<--------------- buf0len ----------------->|
69 + * | |<------- buflen ---->|
70 + * |---------------------|---------------------|
73 + buflen = buf0len +1;
75 + *buf0 = xrealloc(*buf0, buf0len);
76 + buf = *buf0 + buflen -2;
85 + if (parse_line(item, *buf0, mask))
96 --- a/libopkg/parse_util.h
97 +++ b/libopkg/parse_util.h
98 @@ -22,4 +22,8 @@ int is_field(const char *type, const cha
99 char *parse_simple(const char *type, const char *line);
100 char **parse_list(const char *raw, unsigned int *count, const char sep, int skip_field);
102 +typedef int (*parse_line_t)(void *, const char *, uint);
103 +int parse_from_stream_nomalloc(parse_line_t parse_line, void *item, FILE *fp, uint mask,
104 + char **buf0, size_t buf0len);
107 --- a/libopkg/pkg_hash.c
108 +++ b/libopkg/pkg_hash.c
110 #include "opkg_message.h"
112 #include "pkg_hash.h"
113 +#include "parse_util.h"
114 #include "pkg_parse.h"
115 #include "opkg_utils.h"
116 #include "sprintf_alloc.h"
117 @@ -119,8 +120,14 @@ pkg_hash_add_from_file(const char *file_
121 - ret = pkg_parse_from_stream_nomalloc(pkg, fp, 0,
122 + ret = parse_from_stream_nomalloc(pkg_parse_line, pkg, fp, 0,
125 + if (pkg->name == NULL) {
126 + /* probably just a blank line */
133 --- a/libopkg/pkg_parse.c
134 +++ b/libopkg/pkg_parse.c
135 @@ -104,9 +104,11 @@ get_arch_priority(const char *arch)
140 -pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
142 +pkg_parse_line(void *ptr, const char *line, uint mask)
144 + pkg_t *pkg = (pkg_t *) ptr;
146 /* these flags are a bit hackish... */
147 static int reading_conffiles = 0, reading_description = 0;
149 @@ -266,91 +268,6 @@ dont_reset_flags:
153 -pkg_parse_from_stream_nomalloc(pkg_t *pkg, FILE *fp, uint mask,
154 - char **buf0, size_t buf0len)
168 - if (fgets(buf, (int)buflen, fp) == NULL) {
170 - opkg_perror(ERROR, "fgets");
172 - } else if (strlen(*buf0) == buf0len-1) {
173 - opkg_msg(ERROR, "Missing new line character"
174 - " at end of file!\n");
175 - pkg_parse_line(pkg, *buf0, mask);
180 - nl = strchr(buf, '\n');
182 - if (strlen(buf) < buflen-1) {
184 - * Line could be exactly buflen-1 long and
185 - * missing a newline, but we won't know until
186 - * fgets fails to read more data.
188 - opkg_msg(ERROR, "Missing new line character"
189 - " at end of file!\n");
190 - pkg_parse_line(pkg, *buf0, mask);
193 - if (buf0len >= EXCESSIVE_LINE_LEN) {
194 - opkg_msg(ERROR, "Excessively long line at "
195 - "%d. Corrupt file?\n",
202 - * Realloc and point buf past the data already read,
203 - * at the NULL terminator inserted by fgets.
204 - * |<--------------- buf0len ----------------->|
205 - * | |<------- buflen ---->|
206 - * |---------------------|---------------------|
209 - buflen = buf0len +1;
211 - *buf0 = xrealloc(*buf0, buf0len);
212 - buf = *buf0 + buflen -2;
221 - if (pkg_parse_line(pkg, *buf0, mask))
229 - if (pkg->name == NULL) {
230 - /* probably just a blank line */
238 pkg_parse_from_stream(pkg_t *pkg, FILE *fp, uint mask)
241 @@ -358,8 +275,13 @@ pkg_parse_from_stream(pkg_t *pkg, FILE *
242 const size_t len = 4096;
245 - ret = pkg_parse_from_stream_nomalloc(pkg, fp, mask, &buf, len);
246 + ret = parse_from_stream_nomalloc(pkg_parse_line, pkg, fp, mask, &buf, len);
249 + if (pkg->name == NULL) {
250 + /* probably just a blank line */
256 --- a/libopkg/pkg_parse.h
257 +++ b/libopkg/pkg_parse.h
264 int parse_version(pkg_t *pkg, const char *raw);
265 int pkg_parse_from_stream(pkg_t *pkg, FILE *fp, uint mask);
266 -int pkg_parse_from_stream_nomalloc(pkg_t *pkg, FILE *fp, uint mask,
267 - char **buf0, size_t buf0len);
268 +int pkg_parse_line(void *ptr, const char *line, uint mask);
270 #define EXCESSIVE_LINE_LEN (4096 << 8)
272 --- a/libopkg/release_parse.c
273 +++ b/libopkg/release_parse.c
275 #include "parse_util.h"
278 -release_parse_line(release_t *release, const char *line)
279 +release_parse_line(void *ptr, const char *line, uint mask)
281 + release_t *release = (release_t *) ptr;
284 unsigned int count = 0;
286 @@ -111,25 +113,14 @@ dont_reset_flags:
288 release_parse_from_stream(release_t *release, FILE *fp)
292 - size_t buflen, nread;
294 - nread = getline(&buf, &buflen, fp);
295 - while ( nread != -1 ) {
296 - if (buf[nread-1] == '\n') buf[nread-1] = '\0';
297 - if (release_parse_line(release, buf))
298 - opkg_msg(DEBUG, "Failed to parse release line for %s:\n\t%s\n",
299 - release->name, buf);
300 - nread = getline(&buf, &buflen, fp);
304 - opkg_perror(ERROR, "Problems reading Release file for %sd\n", release->name);
309 + const size_t len = 4096;
311 + buf = xmalloc(len);
312 + ret = parse_from_stream_nomalloc(release_parse_line, release, fp, 0, &buf, len);