added "jammod" command and "genman" module
[k8jam.git] / src / jbunpack.c
blob8e5a9d441f4e2ff8d9fc6065433450c2f4c8c877
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, either version 3 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 #include <stdint.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <unistd.h>
22 #include "jambase.h"
24 #include "libhaunp.h"
27 static const uint8_t *jbpkdata;
28 static int jbpkdatalen;
31 static int bread (void *buf, int buflen, void *udata) {
32 static int readpos = 0;
33 int res = 0;
34 uint8_t *ob = (uint8_t *)buf;
35 while (buflen > 0) {
36 int left = jbpkdatalen-readpos;
37 if (left == 0) break;
38 if (left > buflen) left = buflen;
39 memcpy(ob, jbpkdata+readpos, left);
40 res += left;
41 ob += left;
42 readpos += left;
43 buflen -= left;
45 return res;
49 void jambase_unpack (void) {
50 if (jambase == NULL) {
51 char *txt;
52 size_t len, pos;
53 int linecount = 1, linenum = 0;
54 haunp_t hup;
55 jbpkdata = (const uint8_t *)jambasepk;
56 jbpkdatalen = jbpksize();
58 uint32_t n;
59 memcpy(&n, jbpkdata, 4);
60 jbpkdata += 4;
61 len = n;
63 hup = haunp_open_io(bread, NULL);
64 txt = calloc(len+1, 1);
65 if (haunp_read(hup, txt, len) != len) {
66 fprintf(stderr, "FATAL: decompression error!\n");
67 exit(1);
69 haunp_close(hup);
70 //fprintf(stderr, "len=%u\n", len);
71 //fprintf(stderr, "%s\n", txt);
72 for (size_t f = 0; f < len; ++f) if (txt[f] == '\n') ++linecount;
73 //fprintf(stderr, "lc=%d\n", linecount);
74 jambase = (char **)calloc(linecount, sizeof(char *));
75 pos = 0;
76 while (pos < len) {
77 char *e = txt+len, ec;
78 size_t epos = len;
79 for (size_t f = pos; f < len; ++f) {
80 if (txt[f] == '\n') { e = txt+f+1; epos = f+1; break; }
82 ec = *e;
83 *e = 0;
84 //fprintf(stderr, "%d: %s", linenum, txt+pos);
85 jambase[linenum++] = strdup(txt+pos);
86 *e = ec;
87 pos = epos;
89 free(txt);
90 //fprintf(stderr, "lc=%d; ln=%d\n", linecount, linenum);
91 //fprintf(stderr, "::%s", jambase[0]);