/
emb.c
139 lines (106 loc) · 2.71 KB
/
emb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
* Copyright (c) 2005, 2006 by KoanLogic s.r.l. <http://www.koanlogic.com>
* All rights reserved.
*
* This file is part of KLone, and as such it is subject to the license stated
* in the LICENSE file which you have received as part of this distribution.
*
* $Id: emb.c,v 1.19 2008/10/27 21:28:04 tat Exp $
*/
#include <klone/emb.h>
#include <klone/io.h>
#include <klone/codecs.h>
#include <u/libu.h>
#include <u/toolbox/hmap.h>
/* these are klone-site autogen functions */
void register_pages(void);
void unregister_pages(void);
static u_hmap_t *embmap = NULL; /* hashmap of embedded resources */
static int init = 0;
int emb_init(void)
{
if(init++ == 0)
{
dbg_err_if (u_hmap_new(NULL, &embmap));
/* call autogen external function (cannot be called more then once!) */
dbg("registering embedded resources");
register_pages();
}
return 0;
err:
return ~0;
}
int emb_term(void)
{
dbg_err_if(init == 0);
unregister_pages();
if (embmap) {
u_hmap_free(embmap);
embmap = NULL;
}
return 0;
err:
return ~0;
}
int emb_register(embres_t *res)
{
u_hmap_o_t *obj = NULL;
dbg_err_if(init == 0 || res == NULL);
if(res->type == ET_FILE)
dbg("registering %s (%s)", res->filename,
((embfile_t*)res)->comp ? "compressed" : "uncompressed");
else
dbg("registering %s", res->filename);
obj = u_hmap_o_new((void *) res->filename, res);
dbg_err_if (obj == NULL);
dbg_err_if (u_hmap_put(embmap, obj, NULL));
return 0;
err:
return ~0;
}
int emb_unregister(embres_t *res)
{
dbg_err_if(init == 0 || res == NULL);
dbg_err_if (u_hmap_del(embmap, (void *) res->filename, NULL));
return 0;
err:
return ~0;
}
int emb_lookup(const char *filename, embres_t **pr)
{
embres_t *res;
u_hmap_o_t *obj = NULL;
dbg_err_if (init == 0);
dbg_err_if (filename == NULL || filename[0] == 0);
dbg_err_if (pr == NULL);
nop_err_if (u_hmap_get(embmap, (void *) filename, &obj));
*pr = obj->val;
return 0;
err:
/* not found */
return ~0;
}
int emb_open(const char *file, io_t **pio)
{
embfile_t *e = NULL;
codec_t *gzip = NULL;
io_t *io;
dbg_return_if (pio == NULL, ~0);
dbg_return_if (file == NULL, ~0);
dbg_err_if(emb_lookup(file, (embres_t**)&e) || e->res.type != ET_FILE);
dbg_err_if(io_mem_create((char*)e->data, e->size, 0, &io));
#ifdef HAVE_LIBZ
if(e->comp)
{
dbg_err_if(codec_gzip_create(GZIP_UNCOMPRESS, &gzip));
dbg_err_if(io_codec_add_tail(io, (codec_t*)gzip));
gzip = NULL;
}
#endif
*pio = io;
return 0;
err:
if(gzip)
codec_free(gzip);
return ~0;
}