]> git.draconx.ca Git - upkg.git/blob - libupkg.c
b932d861f0a52df6faccbdf7e0f6fe5800acc858
[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 struct upkg_name {
9         unsigned long index, flags;
10         char *name;
11 };
12
13 struct upkg_private {
14         FILE *f;
15
16         struct upkg_name *names;
17
18         unsigned long name_offset, export_offset, import_offset;
19         unsigned char guid[16];
20 };
21
22 static int namecmp(const void *_a, const void *_b, void *_pkg)
23 {
24         const struct upkg_name *a = _a, *b = _b;
25         if (a->index < b->index)
26                 return -1;
27         if (a->index > b->index)
28                 return -1;
29         return 0;
30 }
31
32 static long decode_index(unsigned char *bytes)
33 {
34         long val = 0;
35         size_t i = 1;
36
37         val += bytes[0] & 0x3f;
38         if (bytes[0] & 0x40) do {
39                 val += bytes[i] & 0x7f;
40         } while (bytes[i] & 0x80 && i < 5);
41
42         return bytes[0] & 0x80 ? -val : val;
43 }
44
45 static struct upkg *init_upkg(unsigned char hdr[static UPKG_HDR_SIZE])
46 {
47         struct upkg *pkg;
48
49         pkg = malloc(sizeof *pkg);
50         if (!pkg) {
51                 return NULL;
52         }
53
54         pkg->priv = malloc(sizeof *pkg->priv);
55         if (!pkg->priv) {
56                 free(pkg);
57                 return NULL;
58         }
59
60         pkg->version      = unpack_16_le(hdr+4);
61         pkg->license      = unpack_16_le(hdr+6);
62         pkg->flags        = unpack_32_le(hdr+8);
63         pkg->name_count   = unpack_32_le(hdr+12);
64         pkg->export_count = unpack_32_le(hdr+20);
65         pkg->import_count = unpack_32_le(hdr+28);
66
67         pkg->priv->name_offset   = unpack_32_le(hdr+16);
68         pkg->priv->export_offset = unpack_32_le(hdr+24);
69         pkg->priv->import_offset = unpack_32_le(hdr+32);
70
71         return pkg;
72 }
73
74 static int pkg_init_names(struct upkg *pkg)
75 {
76         size_t rc, nbuf = 0, len;
77         unsigned long index = 0;
78         char buf[512];
79
80         if (fseek(pkg->priv->f, pkg->priv->name_offset, SEEK_SET) != 0)
81                 return -1;
82
83         pkg->priv->names = malloc(pkg->name_count * sizeof *pkg->priv->names);
84         if (!pkg->priv->names)
85                 return -1;
86
87         while (index < pkg->name_count) {
88                 struct upkg_name *name = &pkg->priv->names[index];
89                 name->index = index;
90
91                 /* Read some data into buffer. */
92                 if (!feof(pkg->priv->f)) {
93                         rc = fread(buf+nbuf, 1, sizeof buf-nbuf, pkg->priv->f);
94                         if (rc == 0)
95                                 goto err;
96                         nbuf += rc;
97                 }
98
99                 if (pkg->version >= 64) {
100                         len = buf[0];
101                         if (nbuf <= len + 4 || buf[len])
102                                 goto err;
103                         name->name = malloc(len);
104                         if (!name->name)
105                                 goto err;
106                         memcpy(name->name, buf+1, len);
107                         name->flags = unpack_32_le(buf+len+1);
108
109                         nbuf -= len + 5;
110                         memmove(buf, buf+len+5, nbuf);
111                         index++;
112                 } else {
113                         abort();
114                 }
115         }
116
117         return 0;
118 err:
119         free(pkg->priv->names);
120         return -1;
121 }
122
123 struct upkg *upkg_fopen(const char *path)
124 {
125         unsigned char hdr_buf[UPKG_HDR_SIZE];
126         struct upkg *pkg;
127         FILE *f;
128
129         if (!(f = fopen(path, "rb"))) {
130                 return NULL;
131         }
132         if (fread(hdr_buf, sizeof hdr_buf, 1, f) != 1) {
133                 goto err1;
134         }
135         if (unpack_32_le(hdr_buf) != UPKG_HDR_MAGIC) {
136                 goto err1;
137         }
138
139         /* Initialize package structure. */
140         pkg = init_upkg(hdr_buf);
141         if (!pkg) {
142                 goto err1;
143         }
144         pkg->priv->f = f;
145
146         if (pkg_init_names(pkg) != 0) {
147                 goto err2;
148         }
149
150         return pkg;
151 err2:
152         free(pkg->priv);
153         free(pkg);
154 err1:
155         fclose(f);
156         return NULL;
157 }
158
159 int upkg_close(struct upkg *pkg)
160 {
161         int rc = 0;
162
163         if (pkg->priv->f) {
164                 rc = fclose(pkg->priv->f);
165
166                 for (unsigned i = 0; i < pkg->name_count; i++) {
167                         free(pkg->priv->names[i].name);
168                 }
169                 free(pkg->priv->names);
170
171                 return rc;
172         }
173
174         free(pkg->priv);
175         free(pkg);
176
177         return 0;
178 }
179
180 const char *upkg_get_name(struct upkg *pkg, unsigned long idx)
181 {
182         if (idx >= pkg->name_count)
183                 return 0;
184         return pkg->priv->names[idx].name;
185 }