cli: implement --force-signature
[oweals/opkg-lede.git] / libopkg / release_parse.c
1 /* release_parse.c - the opkg package management system
2
3    Copyright (C) 2010,2011 Javier Palacios
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2, or (at
8    your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14 */
15
16 #include "config.h"
17
18 #include <stdio.h>
19
20 #include "release.h"
21 #include "release_parse.h"
22 #include "libbb/libbb.h"
23 #include "parse_util.h"
24
25 static int
26 release_parse_line(void *ptr, const char *line, uint mask)
27 {
28         release_t *release = (release_t *) ptr;
29
30         int ret = 0;
31         unsigned int count = 0;
32         char **list = 0;
33         static int reading_md5sums = 0;
34 #ifdef HAVE_SHA256
35         static int reading_sha256sums = 0;
36 #endif
37
38         switch (*line) {
39         case 'A':
40                 if (is_field("Architectures", line)) {
41                         release->architectures = parse_list(line, &release->architectures_count, ' ', 0);
42                 }
43                 break;
44
45         case 'C':
46                 if (is_field("Codename", line)) {
47                         release->name = parse_simple("Codename", line);
48                 }
49                 else if (is_field("Components", line)) {
50                         release->components = parse_list(line, &release->components_count, ' ', 0);
51                 }
52                 break;
53
54         case 'D':
55                 if (is_field("Date", line)) {
56                         release->datestring = parse_simple("Date", line);
57                 }
58                 break;
59
60         case 'M':
61                 if (is_field("MD5sum", line)) {
62                         reading_md5sums = 1;
63                         if (release->md5sums == NULL) {
64                              release->md5sums = xcalloc(1, sizeof(cksum_list_t));
65                              cksum_list_init(release->md5sums);
66                         }
67                         goto dont_reset_flags;
68                 }
69                 break;
70
71 #ifdef HAVE_SHA256
72         case 'S':
73                 if (is_field("SHA256", line)) {
74                         reading_sha256sums = 1;
75                         if (release->sha256sums == NULL) {
76                              release->sha256sums = xcalloc(1, sizeof(cksum_list_t));
77                              cksum_list_init(release->sha256sums);
78                         }
79                         goto dont_reset_flags;
80                 }
81                 break;
82 #endif
83
84         case ' ':
85                 if (reading_md5sums) {
86                         list = parse_list(line, &count, ' ', 1);
87                         cksum_list_append(release->md5sums, list);
88                         goto dont_reset_flags;
89                 }
90 #ifdef HAVE_SHA256
91                 else if (reading_sha256sums) {
92                         list = parse_list(line, &count, ' ', 1);
93                         cksum_list_append(release->sha256sums, list);
94                         goto dont_reset_flags;
95                 }
96 #endif
97                 break;
98
99         default:
100                 ret = 1;
101         }
102
103         reading_md5sums = 0;
104 #ifdef HAVE_SHA256
105         reading_sha256sums = 0;
106 #endif
107
108 dont_reset_flags:
109
110         return ret;
111 }
112
113 int
114 release_parse_from_stream(release_t *release, FILE *fp)
115 {
116         int ret;
117         char *buf;
118         const size_t len = 4096;
119
120         buf = xmalloc(len);
121         ret = parse_from_stream_nomalloc(release_parse_line, release, fp, 0, &buf, len);
122         free(buf);
123
124         return ret;
125 }
126