aboutsummaryrefslogtreecommitdiffstats
path: root/package/system/opkg/patches/020-avoid_getline.patch
blob: 8a1a8f6272cc79f1be8a50b6a2ed60d809141e72 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
--- a/libopkg/parse_util.c
+++ b/libopkg/parse_util.c
@@ -22,6 +22,7 @@
 #include "libbb/libbb.h"
 
 #include "parse_util.h"
+#include "pkg_parse.h"
 
 int
 is_field(const char *type, const char *line)
@@ -86,3 +87,84 @@ parse_list(const char *raw, unsigned int
 	*count = line_count;
 	return depends;
 }
+
+int
+parse_from_stream_nomalloc(parse_line_t parse_line, void *item, FILE *fp, uint mask,
+						char **buf0, size_t buf0len)
+{
+	int ret, lineno;
+	char *buf, *nl;
+	size_t buflen;
+
+	lineno = 1;
+	ret = 0;
+
+	buflen = buf0len;
+	buf = *buf0;
+	buf[0] = '\0';
+
+	while (1) {
+		if (fgets(buf, (int)buflen, fp) == NULL) {
+			if (ferror(fp)) {
+				opkg_perror(ERROR, "fgets");
+				ret = -1;
+			} else if (strlen(*buf0) == buf0len-1) {
+				opkg_msg(ERROR, "Missing new line character"
+						" at end of file!\n");
+				parse_line(item, *buf0, mask);
+			}
+			break;
+		}
+
+		nl = strchr(buf, '\n');
+		if (nl == NULL) {
+			if (strlen(buf) < buflen-1) {
+				/*
+				 * Line could be exactly buflen-1 long and
+				 * missing a newline, but we won't know until
+				 * fgets fails to read more data.
+				 */
+				opkg_msg(ERROR, "Missing new line character"
+						" at end of file!\n");
+				parse_line(item, *buf0, mask);
+				break;
+			}
+			if (buf0len >= EXCESSIVE_LINE_LEN) {
+				opkg_msg(ERROR, "Excessively long line at "
+					"%d. Corrupt file?\n",
+					lineno);
+				ret = -1;
+				break;
+			}
+
+			/*
+			 * Realloc and point buf past the data already read,
+			 * at the NULL terminator inserted by fgets.
+			 * |<--------------- buf0len ----------------->|
+			 * |                     |<------- buflen ---->|
+			 * |---------------------|---------------------|
+			 * buf0                   buf
+			 */
+			buflen = buf0len +1;
+			buf0len *= 2;
+			*buf0 = xrealloc(*buf0, buf0len);
+			buf = *buf0 + buflen -2;
+
+			continue;
+		}
+
+		*nl = '\0';
+
+		lineno++;
+
+		if (parse_line(item, *buf0, mask))
+			break;
+
+		buf = *buf0;
+		buflen = buf0len;
+		buf[0] = '\0';
+	}
+
+	return ret;
+}
+
--- a/libopkg/parse_util.h
+++ b/libopkg/parse_util.h
@@ -22,4 +22,8 @@ int is_field(const char *type, const cha
 char *parse_simple(const char *type, const char *line);
 char **parse_list(const char *raw, unsigned int *count, const char sep, int skip_field);
 
+typedef int (*parse_line_t)(void *, const char *, uint);
+int parse_from_stream_nomalloc(parse_line_t parse_line, void *item, FILE *fp, uint mask,
+						char **buf0, size_t buf0len);
+
 #endif
--- a/libopkg/pkg_hash.c
+++ b/libopkg/pkg_hash.c
@@ -23,6 +23,7 @@
 #include "opkg_message.h"
 #include "pkg_vec.h"
 #include "pkg_hash.h"
+#include "parse_util.h"
 #include "pkg_parse.h"
 #include "opkg_utils.h"
 #include "sprintf_alloc.h"
@@ -119,8 +120,14 @@ pkg_hash_add_from_file(const char *file_
 		pkg->src = src;
 		pkg->dest = dest;
 
-		ret = pkg_parse_from_stream_nomalloc(pkg, fp, 0,
+		ret = parse_from_stream_nomalloc(pkg_parse_line, pkg, fp, 0,
 				&buf, len);
+
+		if (pkg->name == NULL) {
+			/* probably just a blank line */
+			ret = 1;
+		}
+
 		if (ret) {
 			pkg_deinit (pkg);
 			free(pkg);
--- a/libopkg/pkg_parse.c
+++ b/libopkg/pkg_parse.c
@@ -104,9 +104,11 @@ get_arch_priority(const char *arch)
 	return 0;
 }
 
-static int
-pkg_parse_line(pkg_t *pkg, const char *line, uint mask)
+int
+pkg_parse_line(void *ptr, const char *line, uint mask)
 {
+	pkg_t *pkg = (pkg_t *) ptr;
+
 	/* these flags are a bit hackish... */
 	static int reading_conffiles = 0, reading_description = 0;
 	int ret = 0;
@@ -266,91 +268,6 @@ dont_reset_flags:
 }
 
 int
-pkg_parse_from_stream_nomalloc(pkg_t *pkg, FILE *fp, uint mask,
-						char **buf0, size_t buf0len)
-{
-	int ret, lineno;
-	char *buf, *nl;
-	size_t buflen;
-
-	lineno = 1;
-	ret = 0;
-
-	buflen = buf0len;
-	buf = *buf0;
-	buf[0] = '\0';
-
-	while (1) {
-		if (fgets(buf, (int)buflen, fp) == NULL) {
-			if (ferror(fp)) {
-				opkg_perror(ERROR, "fgets");
-				ret = -1;
-			} else if (strlen(*buf0) == buf0len-1) {
-				opkg_msg(ERROR, "Missing new line character"
-						" at end of file!\n");
-				pkg_parse_line(pkg, *buf0, mask);
-			}
-			break;
-		}
-
-		nl = strchr(buf, '\n');
-		if (nl == NULL) {
-			if (strlen(buf) < buflen-1) {
-				/*
-				 * Line could be exactly buflen-1 long and
-				 * missing a newline, but we won't know until
-				 * fgets fails to read more data.
-				 */
-				opkg_msg(ERROR, "Missing new line character"
-						" at end of file!\n");
-				pkg_parse_line(pkg, *buf0, mask);
-				break;
-			}
-			if (buf0len >= EXCESSIVE_LINE_LEN) {
-				opkg_msg(ERROR, "Excessively long line at "
-					"%d. Corrupt file?\n",
-					lineno);
-				ret = -1;
-				break;
-			}
-
-			/*
-			 * Realloc and point buf past the data already read,
-			 * at the NULL terminator inserted by fgets.
-			 * |<--------------- buf0len ----------------->|
-			 * |                     |<------- buflen ---->|
-			 * |---------------------|---------------------|
-			 * buf0                   buf
-			 */
-			buflen = buf0len +1;
-			buf0len *= 2;
-			*buf0 = xrealloc(*buf0, buf0len);
-			buf = *buf0 + buflen -2;
-
-			continue;
-		}
-
-		*nl = '\0';
-
-		lineno++;
-
-		if (pkg_parse_line(pkg, *buf0, mask))
-			break;
-
-		buf = *buf0;
-		buflen = buf0len;
-		buf[0] = '\0';
-	}
-
-	if (pkg->name == NULL) {
-		/* probably just a blank line */
-		ret = 1;
-	}
-
-	return ret;
-}
-
-int
 pkg_parse_from_stream(pkg_t *pkg, FILE *fp, uint mask)
 {
 	int ret;
@@ -358,8 +275,13 @@ pkg_parse_from_stream(pkg_t *pkg, FILE *
 	const size_t len = 4096;
 
 	buf = xmalloc(len);
-	ret = pkg_parse_from_stream_nomalloc(pkg, fp, mask, &buf, len);
+	ret = parse_from_stream_nomalloc(pkg_parse_line, pkg, fp, mask, &buf, len);
 	free(buf);
 
+	if (pkg->name == NULL) {
+		/* probably just a blank line */
+		ret = 1;
+	}
+
 	return ret;
 }
--- a/libopkg/pkg_parse.h
+++ b/libopkg/pkg_parse.h
@@ -18,10 +18,11 @@
 #ifndef PKG_PARSE_H
 #define PKG_PARSE_H
 
+#include "pkg.h"
+
 int parse_version(pkg_t *pkg, const char *raw);
 int pkg_parse_from_stream(pkg_t *pkg, FILE *fp, uint mask);
-int pkg_parse_from_stream_nomalloc(pkg_t *pkg, FILE *fp, uint mask,
-						char **buf0, size_t buf0len);
+int pkg_parse_line(void *ptr, const char *line, uint mask);
 
 #define EXCESSIVE_LINE_LEN	(4096 << 8)
 
--- a/libopkg/release_parse.c
+++ b/libopkg/release_parse.c
@@ -23,8 +23,10 @@
 #include "parse_util.h"
 
 static int
-release_parse_line(release_t *release, const char *line)
+release_parse_line(void *ptr, const char *line, uint mask)
 {
+	release_t *release = (release_t *) ptr;
+
 	int ret = 0;
 	unsigned int count = 0;
 	char **list = 0;
@@ -111,25 +113,14 @@ dont_reset_flags:
 int
 release_parse_from_stream(release_t *release, FILE *fp)
 {
-	int ret = 0;
-	char *buf = NULL;
-	size_t buflen, nread;
-
-	nread = getline(&buf, &buflen, fp);
-	while ( nread != -1 ) {
-		if (buf[nread-1] == '\n') buf[nread-1] = '\0';
-		if (release_parse_line(release, buf))
-                        opkg_msg(DEBUG, "Failed to parse release line for %s:\n\t%s\n",
-					release->name, buf);
-		nread = getline(&buf, &buflen, fp);
-	}
-
-	if (!feof(fp)) {
-		opkg_perror(ERROR, "Problems reading Release file for %sd\n", release->name);
-		ret = -1;
-	}
+	int ret;
+	char *buf;
+	const size_t len = 4096;
 
+	buf = xmalloc(len);
+	ret = parse_from_stream_nomalloc(release_parse_line, release, fp, 0, &buf, len);
 	free(buf);
+
 	return ret;
 }