option.c: fixed warnings
[k8jam.git] / src / jbunpack.c
blob8208f4d856660f0f15f9ad3202f74b20da5f162a
1 /* coded by Ketmar // Vampire Avalon (psyc://ketmar.no-ip.org/~Ketmar)
2 * Understanding is not required. Only obedience.
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 #include <stdint.h>
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <unistd.h>
21 #include "jambase.h"
23 #include "libhaunp.h"
26 static const uint8_t *jbpkdata;
27 static int jbpkdatalen;
30 static int bread (void *buf, int buflen, void *udata) {
31 static int readpos = 0;
32 int res = 0;
33 uint8_t *ob = (uint8_t *)buf;
34 while (buflen > 0) {
35 int left = jbpkdatalen-readpos;
36 if (left == 0) break;
37 if (left > buflen) left = buflen;
38 memcpy(ob, jbpkdata+readpos, left);
39 res += left;
40 ob += left;
41 readpos += left;
42 buflen -= left;
44 return res;
48 void jambase_unpack (void) {
49 if (jambase == NULL) {
50 char *txt;
51 size_t len, pos;
52 int linecount = 1, linenum = 0;
53 haunp_t hup;
54 jbpkdata = (const uint8_t *)jambasepk;
55 jbpkdatalen = jbpksize();
57 uint32_t n;
58 memcpy(&n, jbpkdata, 4);
59 jbpkdata += 4;
60 len = n;
62 hup = haunp_open_io(bread, NULL);
63 txt = calloc(len+1, 1);
64 if (haunp_read(hup, txt, len) != (int)len) {
65 fprintf(stderr, "FATAL: decompression error!\n");
66 exit(1);
68 haunp_close(hup);
69 //fprintf(stderr, "len=%u\n", len);
70 //fprintf(stderr, "%s\n", txt);
71 for (size_t f = 0; f < len; ++f) if (txt[f] == '\n') ++linecount;
72 //fprintf(stderr, "lc=%d\n", linecount);
73 jambase = (char **)calloc(linecount, sizeof(char *));
74 pos = 0;
75 while (pos < len) {
76 char *e = txt+len, ec;
77 size_t epos = len;
78 for (size_t f = pos; f < len; ++f) {
79 if (txt[f] == '\n') { e = txt+f+1; epos = f+1; break; }
81 ec = *e;
82 *e = 0;
83 //fprintf(stderr, "%d: %s", linenum, txt+pos);
84 jambase[linenum++] = strdup(txt+pos);
85 *e = ec;
86 pos = epos;
88 free(txt);
89 //fprintf(stderr, "lc=%d; ln=%d\n", linecount, linenum);
90 //fprintf(stderr, "::%s", jambase[0]);