]> git.draconx.ca Git - upkg.git/blob - libupkg.c
Fix early return in upkg_close.
[upkg.git] / libupkg.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 #include "upkg.h"
6 #include "pack.h"
7
8 #define MIN(a, b) ((a) < (b) ? (a) : (b))
9
10 struct upkg_name {
11         unsigned long flags;
12         char *name;
13 };
14
15 struct upkg_export {
16         long class, super, name;
17         unsigned long package, flags;
18         unsigned long size, offset;
19 };
20
21 struct upkg_private {
22         FILE *f;
23
24         struct upkg_name   *names;
25         struct upkg_export *exports;
26
27         unsigned long name_offset, export_offset, import_offset;
28         unsigned char guid[16];
29 };
30
31 /*
32  * Decode the compact index format from the upkg.  This format is fucked.
33  * Stores the result in *val and returns the number of input bytes read (or 0
34  * if the input is invalid, in which case *val is undefined).
35  */
36 static size_t decode_index(long *val, unsigned char *bytes, size_t n)
37 {
38         size_t i = 0;
39
40         *val = 0;
41         while (i < MIN(n, 5)) {
42                 /*
43                  * Least significant bytes are first, so we need to do this
44                  * nonsense.
45                  */
46                 long tmp = bytes[i] & (i == 0 ? 0x3f : 0x7f);
47
48                 if (i > 0) tmp <<= 6;
49                 if (i > 1) tmp <<= 7*(i-1);
50                 *val += tmp;
51
52                 if (!(bytes[i] & (i == 0 ? 0x40 : 0x80))) {
53                         i++;
54                         break;
55                 }
56
57                 i++;
58         }
59
60         if (i > MIN(n, 5) || n == 0)
61                 return 0;
62         if (bytes[0] & 0x80)
63                 *val = -*val;
64         return i;
65 }
66
67 static struct upkg *init_upkg(unsigned char hdr[static UPKG_HDR_SIZE])
68 {
69         struct upkg *pkg;
70
71         pkg = malloc(sizeof *pkg);
72         if (!pkg) {
73                 return NULL;
74         }
75
76         pkg->priv = malloc(sizeof *pkg->priv);
77         if (!pkg->priv) {
78                 free(pkg);
79                 return NULL;
80         }
81
82         pkg->version      = unpack_16_le(hdr+4);
83         pkg->license      = unpack_16_le(hdr+6);
84         pkg->flags        = unpack_32_le(hdr+8);
85         pkg->name_count   = unpack_32_le(hdr+12);
86         pkg->export_count = unpack_32_le(hdr+20);
87         pkg->import_count = unpack_32_le(hdr+28);
88
89         pkg->priv->name_offset   = unpack_32_le(hdr+16);
90         pkg->priv->export_offset = unpack_32_le(hdr+24);
91         pkg->priv->import_offset = unpack_32_le(hdr+32);
92
93         return pkg;
94 }
95
96 static int pkg_init_names(struct upkg *pkg)
97 {
98         size_t rc, len, nbuf = 0;
99         unsigned long index = 0;
100         char buf[512];
101
102         if (fseek(pkg->priv->f, pkg->priv->name_offset, SEEK_SET) != 0)
103                 return -1;
104
105         pkg->priv->names = malloc(pkg->name_count * sizeof *pkg->priv->names);
106         if (!pkg->priv->names)
107                 return -1;
108
109         while (index < pkg->name_count) {
110                 struct upkg_name *name = &pkg->priv->names[index];
111
112                 /* Read some data into buffer. */
113                 if (!feof(pkg->priv->f)) {
114                         rc = fread(buf+nbuf, 1, sizeof buf-nbuf, pkg->priv->f);
115                         if (rc == 0)
116                                 goto err;
117                         nbuf += rc;
118                 }
119
120                 if (pkg->version >= 64) {
121                         len = buf[0];
122                         if (nbuf <= len + 4 || buf[len])
123                                 goto err;
124                         name->name = malloc(len);
125                         if (!name->name)
126                                 goto err;
127                         memcpy(name->name, buf+1, len);
128                         name->flags = unpack_32_le(buf+len+1);
129
130                         nbuf -= len + 5;
131                         memmove(buf, buf+len+5, nbuf);
132                         index++;
133                 } else {
134                         /* TODO */
135                         abort();
136                 }
137         }
138
139         return 0;
140 err:
141         for (unsigned i = 0; i < index; i++)
142                 free(pkg->priv->names[i].name);
143         free(pkg->priv->names);
144         return -1;
145 }
146
147 static int pkg_init_exports(struct upkg *pkg)
148 {
149         size_t rc, len, nbuf = 0;
150         unsigned long index = 0;
151         char buf[512];
152
153         if (fseek(pkg->priv->f, pkg->priv->export_offset, SEEK_SET) != 0)
154                 return -1;
155
156         pkg->priv->exports = malloc(pkg->export_count * sizeof *pkg->priv->exports);
157         if (!pkg->priv->exports)
158                 return -1;
159
160         while (index < pkg->export_count) {
161                 struct upkg_export *export = &pkg->priv->exports[index];
162
163                 /* Read some data into buffer. */
164                 if (!feof(pkg->priv->f)) {
165                         rc = fread(buf+nbuf, 1, sizeof buf-nbuf, pkg->priv->f);
166                         if (rc == 0)
167                                 goto err;
168                         nbuf += rc;
169                 }
170
171                 len = 0;
172                 rc = decode_index(&export->class, buf+len, nbuf-len);
173                 if (rc == 0) goto err;
174                 len += rc;
175
176                 rc = decode_index(&export->super, buf+len, nbuf-len);
177                 if (rc == 0) goto err;
178                 len += rc;
179
180                 if (nbuf-len < 4) goto err;
181                 export->package = unpack_32_le(buf+len);
182                 len += 4;
183
184                 rc = decode_index(&export->name, buf+len, nbuf-len);
185                 if (rc == 0) goto err;
186                 len += rc;
187
188                 if (nbuf-len < 4) goto err;
189                 export->flags = unpack_32_le(buf+len);
190                 len += 4;
191
192                 rc = decode_index(&export->size, buf+len, nbuf-len);
193                 if (rc == 0) goto err;
194                 len += rc;
195
196                 if (export->size) {
197                         rc = decode_index(&export->offset, buf+len, nbuf-len);
198                         if (rc == 0) goto err;
199                         len += rc;
200                 }
201
202                 nbuf -= len;
203                 memmove(buf, buf+len, nbuf);
204                 index++;
205         }
206
207         return 0;
208 err:
209         free(pkg->priv->exports);
210         return -1;
211 }
212
213 struct upkg *upkg_fopen(const char *path)
214 {
215         unsigned char hdr_buf[UPKG_HDR_SIZE];
216         struct upkg *pkg;
217         FILE *f;
218
219         if (!(f = fopen(path, "rb"))) {
220                 return NULL;
221         }
222         if (fread(hdr_buf, sizeof hdr_buf, 1, f) != 1) {
223                 goto err1;
224         }
225         if (unpack_32_le(hdr_buf) != UPKG_HDR_MAGIC) {
226                 goto err1;
227         }
228
229         /* Initialize package structure. */
230         pkg = init_upkg(hdr_buf);
231         if (!pkg) {
232                 goto err1;
233         }
234         pkg->priv->f = f;
235
236         if (pkg_init_exports(pkg) != 0) {
237                 goto err2;
238         }
239
240         if (pkg_init_names(pkg) != 0) {
241                 goto err3;
242         }
243
244         return pkg;
245 err3:
246         free(pkg->priv->exports);
247 err2:
248         free(pkg->priv);
249         free(pkg);
250 err1:
251         fclose(f);
252         return NULL;
253 }
254
255 int upkg_close(struct upkg *pkg)
256 {
257         int rc = 0;
258
259         if (pkg->priv->f) {
260                 rc = fclose(pkg->priv->f);
261
262                 for (unsigned i = 0; i < pkg->name_count; i++) {
263                         free(pkg->priv->names[i].name);
264                 }
265         }
266
267         free(pkg->priv->exports);
268         free(pkg->priv->names);
269         free(pkg->priv);
270         free(pkg);
271
272         return rc;
273 }
274
275 const char *upkg_get_name(struct upkg *pkg, unsigned long idx)
276 {
277         if (idx >= pkg->name_count)
278                 return 0;
279         return pkg->priv->names[idx].name;
280 }