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