]> git.draconx.ca Git - upkg.git/blob - libupkg.c
Remove redundant index field from upkg_name and fix some types.
[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                         abort();
103                 }
104         }
105
106         return 0;
107 err:
108         free(pkg->priv->names);
109         return -1;
110 }
111
112 struct upkg *upkg_fopen(const char *path)
113 {
114         unsigned char hdr_buf[UPKG_HDR_SIZE];
115         struct upkg *pkg;
116         FILE *f;
117
118         if (!(f = fopen(path, "rb"))) {
119                 return NULL;
120         }
121         if (fread(hdr_buf, sizeof hdr_buf, 1, f) != 1) {
122                 goto err1;
123         }
124         if (unpack_32_le(hdr_buf) != UPKG_HDR_MAGIC) {
125                 goto err1;
126         }
127
128         /* Initialize package structure. */
129         pkg = init_upkg(hdr_buf);
130         if (!pkg) {
131                 goto err1;
132         }
133         pkg->priv->f = f;
134
135         if (pkg_init_names(pkg) != 0) {
136                 goto err2;
137         }
138
139         return pkg;
140 err2:
141         free(pkg->priv);
142         free(pkg);
143 err1:
144         fclose(f);
145         return NULL;
146 }
147
148 int upkg_close(struct upkg *pkg)
149 {
150         int rc = 0;
151
152         if (pkg->priv->f) {
153                 rc = fclose(pkg->priv->f);
154
155                 for (unsigned i = 0; i < pkg->name_count; i++) {
156                         free(pkg->priv->names[i].name);
157                 }
158                 free(pkg->priv->names);
159
160                 return rc;
161         }
162
163         free(pkg->priv);
164         free(pkg);
165
166         return 0;
167 }
168
169 const char *upkg_get_name(struct upkg *pkg, unsigned long idx)
170 {
171         if (idx >= pkg->name_count)
172                 return 0;
173         return pkg->priv->names[idx].name;
174 }