unarchive: fix possible segmentation fault in deb_extract()
[oweals/opkg-lede.git] / libopkg / pkg_parse.c
1 /* pkg_parse.c - the opkg package management system
2
3    Copyright (C) 2009 Ubiq Technologies <graham.gower@gmail.com>
4
5    Steven M. Ayer
6    Copyright (C) 2002 Compaq Computer Corporation
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17 */
18
19 #include <stdio.h>
20 #include <ctype.h>
21 #include <unistd.h>
22
23 #include "pkg.h"
24 #include "opkg_utils.h"
25 #include "pkg_parse.h"
26 #include "pkg_depends.h"
27 #include "libbb/libbb.h"
28
29 #include "file_util.h"
30 #include "parse_util.h"
31
32 static void parse_status(pkg_t * pkg, const char *sstr)
33 {
34         char sw_str[64], sf_str[64], ss_str[64];
35
36         if (sscanf(sstr, "Status: %63s %63s %63s", sw_str, sf_str, ss_str) != 3) {
37                 opkg_msg(ERROR, "Failed to parse Status line for %s\n",
38                          pkg->name);
39                 return;
40         }
41
42         pkg->state_want = pkg_state_want_from_str(sw_str);
43         pkg->state_flag |= pkg_state_flag_from_str(sf_str);
44         pkg->state_status = pkg_state_status_from_str(ss_str);
45 }
46
47 static void parse_conffiles(pkg_t * pkg, const char *cstr)
48 {
49         conffile_list_t *cl;
50         char file_name[1024], md5sum[85];
51
52         if (sscanf(cstr, "%1023s %84s", file_name, md5sum) != 2) {
53                 opkg_msg(ERROR, "Failed to parse Conffiles line for %s\n",
54                          pkg->name);
55                 return;
56         }
57
58         cl = pkg_get_ptr(pkg, PKG_CONFFILES);
59
60         if (cl)
61                 conffile_list_append(cl, file_name, md5sum);
62 }
63
64 int parse_version(pkg_t * pkg, const char *vstr)
65 {
66         char *colon, *dup, *rev;
67
68         if (strncmp(vstr, "Version:", 8) == 0)
69                 vstr += 8;
70
71         while (*vstr && isspace(*vstr))
72                 vstr++;
73
74         colon = strchr(vstr, ':');
75         if (colon) {
76                 errno = 0;
77                 pkg_set_int(pkg, PKG_EPOCH, strtoul(vstr, NULL, 10));
78                 if (errno) {
79                         opkg_perror(ERROR, "%s: invalid epoch", pkg->name);
80                 }
81                 vstr = ++colon;
82         }
83
84
85         dup = xstrdup(vstr);
86         rev = strrchr(dup, '-');
87
88         if (rev) {
89                 *rev++ = '\0';
90                 pkg_set_string(pkg, PKG_REVISION, rev);
91         }
92
93         pkg_set_string(pkg, PKG_VERSION, dup);
94         free(dup);
95
96         return 0;
97 }
98
99 static char *parse_architecture(pkg_t *pkg, const char *str)
100 {
101         const char *s = str;
102         const char *e;
103
104         while (isspace(*s))
105                 s++;
106
107         e = s + strlen(s);
108
109         while (e > s && isspace(*e))
110                 e--;
111
112         return pkg_set_architecture(pkg, s, e - s);
113 }
114
115 int pkg_parse_line(void *ptr, const char *line, uint mask)
116 {
117         pkg_t *pkg = (pkg_t *) ptr;
118         abstract_pkg_t *ab_pkg = NULL;
119         conffile_list_t *cl;
120
121         /* these flags are a bit hackish... */
122         static int reading_conffiles = 0, reading_description = 0;
123         static char *description = NULL;
124         char *s;
125         int ret = 0;
126
127         /* Exclude globally masked fields. */
128         mask |= conf->pfm;
129
130         /* Flip the semantics of the mask. */
131         mask ^= PFM_ALL;
132
133         switch (*line) {
134         case 'A':
135                 if ((mask & PFM_ARCHITECTURE) && is_field("Architecture", line))
136                         parse_architecture(pkg, line + strlen("Architecture") + 1);
137                 else if ((mask & PFM_AUTO_INSTALLED)
138                            && is_field("Auto-Installed", line)) {
139                         char *tmp = parse_simple("Auto-Installed", line);
140                         if (strcmp(tmp, "yes") == 0)
141                                 pkg->auto_installed = 1;
142                         free(tmp);
143                 }
144                 break;
145
146         case 'C':
147                 if ((mask & PFM_CONFFILES) && is_field("Conffiles", line)) {
148                         reading_conffiles = 1;
149                         reading_description = 0;
150
151                         cl = xcalloc(1, sizeof(*cl));
152                         conffile_list_init(cl);
153                         pkg_set_ptr(pkg, PKG_CONFFILES, cl);
154
155                         goto dont_reset_flags;
156                 } else if ((mask & PFM_CONFLICTS)
157                            && is_field("Conflicts", line))
158                         parse_deplist(pkg, CONFLICTS, line + strlen("Conflicts") + 1);
159                 break;
160
161         case 'D':
162                 if ((mask & PFM_DESCRIPTION) && is_field("Description", line)) {
163                         description = parse_simple("Description", line);
164                         reading_conffiles = 0;
165                         reading_description = 1;
166                         goto dont_reset_flags;
167                 } else if ((mask & PFM_DEPENDS) && is_field("Depends", line))
168                         parse_deplist(pkg, DEPEND, line + strlen("Depends") + 1);
169                 break;
170
171         case 'E':
172                 if ((mask & PFM_ESSENTIAL) && is_field("Essential", line)) {
173                         char *tmp = parse_simple("Essential", line);
174                         if (strcmp(tmp, "yes") == 0)
175                                 pkg->essential = 1;
176                         free(tmp);
177                 }
178                 break;
179
180         case 'F':
181                 if ((mask & PFM_FILENAME) && is_field("Filename", line))
182                         pkg_set_string(pkg, PKG_FILENAME, line + strlen("Filename") + 1);
183                 break;
184
185         case 'I':
186                 if ((mask & PFM_INSTALLED_SIZE)
187                     && is_field("Installed-Size", line)) {
188                         pkg_set_int(pkg, PKG_INSTALLED_SIZE, strtoul(line + strlen("Installed-Size") + 1, NULL, 0));
189                 } else if ((mask & PFM_INSTALLED_TIME)
190                            && is_field("Installed-Time", line)) {
191                         pkg_set_int(pkg, PKG_INSTALLED_TIME, strtoul(line + strlen("Installed-Time") + 1, NULL, 0));
192                 }
193                 break;
194
195         case 'M':
196                 if ((mask & PFM_MD5SUM) && (is_field("MD5sum:", line) || is_field("MD5Sum:", line)))
197                         pkg_set_md5(pkg, line + strlen("MD5sum") + 1);
198                 else if ((mask & PFM_MAINTAINER)
199                          && is_field("Maintainer", line))
200                         pkg_set_string(pkg, PKG_MAINTAINER, line + strlen("Maintainer") + 1);
201                 break;
202
203         case 'P':
204                 if ((mask & PFM_PACKAGE) && is_field("Package", line)) {
205                         pkg->name = parse_simple("Package", line);
206                         ab_pkg = abstract_pkg_fetch_by_name(pkg->name);
207
208                         if (ab_pkg && (ab_pkg->state_flag & SF_NEED_DETAIL)) {
209                                 if (!(pkg->state_flag & SF_NEED_DETAIL)) {
210                                         opkg_msg(DEPEND, "propagating abpkg flag to pkg %s\n", pkg->name);
211                                         pkg->state_flag |= SF_NEED_DETAIL;
212                                 }
213                         }
214                 }
215                 else if ((mask & PFM_PRIORITY) && is_field("Priority", line))
216                         pkg_set_string(pkg, PKG_PRIORITY, line + strlen("Priority") + 1);
217                 else if ((mask & PFM_PROVIDES) && is_field("Provides", line))
218                         parse_providelist(pkg, line + strlen("Provides") + 1);
219                 else if ((mask & PFM_PRE_DEPENDS)
220                          && is_field("Pre-Depends", line))
221                         parse_deplist(pkg, PREDEPEND, line + strlen("Pre-Depends") + 1);
222                 break;
223
224         case 'R':
225                 if ((mask & PFM_RECOMMENDS) && is_field("Recommends", line))
226                         parse_deplist(pkg, RECOMMEND, line + strlen("Recommends") + 1);
227                 else if ((mask & PFM_REPLACES) && is_field("Replaces", line))
228                         parse_replacelist(pkg, line + strlen("Replaces") + 1);
229                 break;
230
231         case 'S':
232                 if ((mask & PFM_SECTION) && is_field("Section", line))
233                         pkg_set_string(pkg, PKG_SECTION, line + strlen("Section") + 1);
234                 else if ((mask & PFM_SHA256SUM) && is_field("SHA256sum", line))
235                         pkg_set_sha256(pkg, line + strlen("SHA256sum") + 1);
236                 else if ((mask & PFM_SIZE) && is_field("Size", line)) {
237                         pkg_set_int(pkg, PKG_SIZE, strtoul(line + strlen("Size") + 1, NULL, 0));
238                 } else if ((mask & PFM_SOURCE) && is_field("Source", line))
239                         pkg_set_string(pkg, PKG_SOURCE, line + strlen("Source") + 1);
240                 else if ((mask & PFM_STATUS) && is_field("Status", line))
241                         parse_status(pkg, line);
242                 else if ((mask & PFM_SUGGESTS) && is_field("Suggests", line))
243                         parse_deplist(pkg, SUGGEST, line + strlen("Suggests") + 1);
244                 break;
245
246         case 'T':
247                 if ((mask & PFM_TAGS) && is_field("Tags", line))
248                         pkg_set_string(pkg, PKG_TAGS, line + strlen("Tags") + 1);
249                 break;
250
251         case 'V':
252                 if ((mask & PFM_VERSION) && is_field("Version", line))
253                         parse_version(pkg, line);
254                 break;
255
256         case ' ':
257                 if ((mask & PFM_DESCRIPTION) && reading_description) {
258                         if (isatty(1)) {
259                                 description = xrealloc(description,
260                                                             strlen(description)
261                                                             + 1 + strlen(line) +
262                                                             1);
263                                 strcat(description, "\n");
264                         } else {
265                                 description = xrealloc(description,
266                                                             strlen(description)
267                                                             + 1 + strlen(line));
268                         }
269                         strcat(description, (line));
270                         goto dont_reset_flags;
271                 } else if ((mask & PFM_CONFFILES) && reading_conffiles) {
272                         parse_conffiles(pkg, line);
273                         goto dont_reset_flags;
274                 }
275
276                 /* FALLTHROUGH */
277         default:
278                 /* For package lists, signifies end of package. */
279                 if (line_is_blank(line)) {
280                         ret = 1;
281                         break;
282                 }
283         }
284
285         if (reading_description && description) {
286                 pkg_set_string(pkg, PKG_DESCRIPTION, description);
287                 free(description);
288                 reading_description = 0;
289                 description = NULL;
290         }
291
292         reading_conffiles = 0;
293
294 dont_reset_flags:
295
296         return ret;
297 }
298
299 int pkg_parse_from_stream(pkg_t * pkg, FILE * fp, uint mask)
300 {
301         int ret;
302         char *buf;
303         const size_t len = 4096;
304
305         buf = xmalloc(len);
306         ret =
307             parse_from_stream_nomalloc(pkg_parse_line, pkg, fp, mask, &buf,
308                                        len);
309         free(buf);
310
311         if (pkg->name == NULL) {
312                 /* probably just a blank line */
313                 ret = 1;
314         }
315
316         return ret;
317 }