GUI: Fix Tomato RAF theme for all builds. Compilation typo.
[tomato.git] / release / src-rt-6.x.4708 / cfe / cfe / main / cfe_lzmafs.c
blob32a81fc0fda1de1d362f82768a4680211ef31d7a
1 /* *********************************************************************
2 * Broadcom Common Firmware Environment (CFE)
3 *
4 * "LZMA compression" file system File: cfe_lzmafs.c
5 *
6 * This is more of a filesystem "hook" than an actual file system.
7 * You can stick it on the front of the chain of file systems
8 * that CFE calls and it will route data read from the
9 * underlying filesystem through LZMA before passing it up to the
10 * user.
12 * Author:
14 *********************************************************************
16 * Copyright 2000,2001,2002,2003
17 * Broadcom Corporation. All rights reserved.
19 * This software is furnished under license and may be used and
20 * copied only in accordance with the following terms and
21 * conditions. Subject to these conditions, you may download,
22 * copy, install, use, modify and distribute modified or unmodified
23 * copies of this software in source and/or binary form. No title
24 * or ownership is transferred hereby.
26 * 1) Any source code used, modified or distributed must reproduce
27 * and retain this copyright notice and list of conditions
28 * as they appear in the source file.
30 * 2) No right is granted to use any trade name, trademark, or
31 * logo of Broadcom Corporation. The "Broadcom Corporation"
32 * name may not be used to endorse or promote products derived
33 * from this software without the prior written permission of
34 * Broadcom Corporation.
36 * 3) THIS SOFTWARE IS PROVIDED "AS-IS" AND ANY EXPRESS OR
37 * IMPLIED WARRANTIES, INCLUDING BUT NOT LIMITED TO, ANY IMPLIED
38 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
39 * PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED. IN NO EVENT
40 * SHALL BROADCOM BE LIABLE FOR ANY DAMAGES WHATSOEVER, AND IN
41 * PARTICULAR, BROADCOM SHALL NOT BE LIABLE FOR DIRECT, INDIRECT,
42 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
43 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
44 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
45 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
46 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
47 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE), EVEN IF ADVISED OF
48 * THE POSSIBILITY OF SUCH DAMAGE.
49 ********************************************************************* */
51 #if CFG_LZMA
53 #include "lib_types.h"
54 #include "lib_string.h"
55 #include "lib_queue.h"
56 #include "lib_malloc.h"
57 #include "lib_printf.h"
58 #include "lib_arena.h"
60 #include "cfe_error.h"
62 #include "cfe.h"
63 #include "cfe_mem.h"
64 #include "initdata.h"
65 #include "addrspace.h" /* for macros dealing with addresses */
66 #include "cfe_fileops.h"
67 #include "cfe_iocb.h"
68 #include "cfe_devfuncs.h"
69 #include "cfe_console.h"
71 #include "cfe.h"
72 #include "LzmaDec.h"
74 /* *********************************************************************
75 * LZMAFS context
76 ********************************************************************* */
79 * File system context - describes overall file system info,
80 * such as the handle to the underlying device.
83 typedef struct lzmafs_fsctx_s {
84 void *lzmafsctx_subfsctx;
85 const fileio_dispatch_t *lzmafsctx_subops;
86 int lzmafsctx_refcnt;
87 } lzmafs_fsctx_t;
90 * File context - describes an open file on the file system.
91 * For raw devices, this is pretty meaningless, but we do
92 * keep track of where we are.
95 #define LZMAFS_INBUFSIZE 1024
96 #define LZMAFS_OUTBUFSIZE 16*1024
97 typedef struct lzmafs_file_s {
98 lzmafs_fsctx_t *lzmafs_fsctx;
99 int lzmafs_fileoffset;
100 void *lzmafs_subfile;
101 uint8_t *lzmafs_inbuf;
102 uint8_t *lzmafs_outbuf;
103 int lzmafs_inlen;
104 int lzmafs_outlen;
105 int lzmafs_unpackSize;
106 uint8_t *lzmafs_inptr;
107 uint8_t *lzmafs_outptr;
108 int lzmafs_eofseen;
109 CLzmaDec *lzmafs_state;
110 }lzmafs_file_t;
112 /* *********************************************************************
113 * Prototypes
114 ********************************************************************* */
115 static int lzmafs_fileop_init(void **fsctx,void *ctx);
116 static int lzmafs_fileop_open(void **ref,void *fsctx,char *filename,int mode);
117 static int lzmafs_fileop_read(void *ref,uint8_t *buf,int len);
118 static int lzmafs_fileop_write(void *ref,uint8_t *buf,int len);
119 static int lzmafs_fileop_seek(void *ref,int offset,int how);
120 static void lzmafs_fileop_close(void *ref);
121 static void lzmafs_fileop_uninit(void *fsctx);
123 static void *SzAlloc(void *p, size_t size) { p = p; return KMALLOC(size, 0); }
124 static void SzFree(void *p, void *address) { p = p; KFREE(address); }
125 static ISzAlloc g_Alloc = { SzAlloc, SzFree };
127 /* *********************************************************************
128 * LZMA fileio dispatch table
129 ********************************************************************* */
131 const fileio_dispatch_t lzmafs_fileops = {
132 "lzma",
134 lzmafs_fileop_init,
135 lzmafs_fileop_open,
136 lzmafs_fileop_read,
137 lzmafs_fileop_write,
138 lzmafs_fileop_seek,
139 lzmafs_fileop_close,
140 lzmafs_fileop_uninit
143 static int lzmafs_fileop_init(void **newfsctx,void *curfsvoid)
145 lzmafs_fsctx_t *fsctx;
146 fileio_ctx_t *curfsctx = (fileio_ctx_t *) curfsvoid;
148 *newfsctx = NULL;
150 fsctx = KMALLOC(sizeof(lzmafs_fsctx_t),0);
151 if (!fsctx) {
152 return CFE_ERR_NOMEM;
155 fsctx->lzmafsctx_refcnt = 0;
156 fsctx->lzmafsctx_subops = curfsctx->ops;
157 fsctx->lzmafsctx_subfsctx = curfsctx->fsctx;
158 *newfsctx = fsctx;
159 return 0;
162 static int lzmafs_fileop_open(void **ref,void *fsctx_arg,char *filename,int mode)
164 lzmafs_fsctx_t *fsctx;
165 lzmafs_file_t *file;
166 int err = 0, i;
167 /* header: 5 bytes of LZMA properties and 8 bytes of uncompressed size */
168 unsigned char header[LZMA_PROPS_SIZE + 8];
170 if (mode != FILE_MODE_READ)
171 return CFE_ERR_UNSUPPORTED;
173 fsctx = (lzmafs_fsctx_t *) fsctx_arg;
175 file = KMALLOC(sizeof(lzmafs_file_t), 0);
176 if (!file) {
177 return CFE_ERR_NOMEM;
180 file->lzmafs_fileoffset = 0;
181 file->lzmafs_fsctx = fsctx;
182 file->lzmafs_inlen = 0;
183 file->lzmafs_eofseen = 0;
184 file->lzmafs_state = KMALLOC(sizeof(CLzmaDec), 0);
185 if (!file->lzmafs_state)
186 goto error2;
188 file->lzmafs_outlen = 0;
190 /* Open the raw file system */
191 err = BDOPEN(fsctx->lzmafsctx_subops, &(file->lzmafs_subfile),
192 fsctx->lzmafsctx_subfsctx, filename);
193 if (err != 0)
194 goto error2;
196 err = BDREAD(file->lzmafs_fsctx->lzmafsctx_subops,
197 file->lzmafs_subfile,
198 header,
199 sizeof(header));
200 if (err < 0)
201 goto error;
203 file->lzmafs_unpackSize = 0;
204 /* uncompressed size */
205 for (i = 0; i < 8; i++)
206 file->lzmafs_unpackSize += header[LZMA_PROPS_SIZE + i] << (i * 8);
208 LzmaDec_Construct(file->lzmafs_state);
209 RINOK(LzmaDec_Allocate(file->lzmafs_state, header, LZMA_PROPS_SIZE, &g_Alloc));
211 file->lzmafs_inbuf = KMALLOC(LZMAFS_INBUFSIZE, 0);
212 file->lzmafs_outbuf = KMALLOC(LZMAFS_OUTBUFSIZE, 0);
213 if (!file->lzmafs_inbuf || !file->lzmafs_outbuf) {
214 err = CFE_ERR_NOMEM;
215 goto error;
218 file->lzmafs_outptr = file->lzmafs_outbuf;
219 LzmaDec_Init(file->lzmafs_state);
220 fsctx->lzmafsctx_refcnt++;
221 *ref = file;
222 return 0;
224 error:
225 BDCLOSE(file->lzmafs_fsctx->lzmafsctx_subops, file->lzmafs_subfile);
226 error2:
227 if (file->lzmafs_inbuf)
228 KFREE(file->lzmafs_inbuf);
229 if (file->lzmafs_outbuf)
230 KFREE(file->lzmafs_outbuf);
231 if (file->lzmafs_state) {
232 LzmaDec_Free(file->lzmafs_state, &g_Alloc);
233 KFREE(file->lzmafs_state);
235 if (file)
236 KFREE(file);
237 return err;
240 static int
241 lzmafs_fileop_read(void *ref, uint8_t *buf, int len)
243 lzmafs_file_t *file = (lzmafs_file_t *) ref;
244 int res = 0;
245 int err;
246 int amtcopy;
247 int ttlcopy = 0;
248 unsigned int in_processed;
249 ELzmaFinishMode finishMode = LZMA_FINISH_ANY;
250 ELzmaStatus status;
252 if (len == 0) return 0;
254 while (len) {
256 /* Figure the amount to copy. This is the min of what we
257 have left to do and what is available. */
258 amtcopy = len;
259 if (amtcopy > file->lzmafs_outlen) {
260 amtcopy = file->lzmafs_outlen;
263 /* Copy the data. */
264 if (buf) {
265 memcpy(buf, file->lzmafs_outptr, amtcopy);
266 buf += amtcopy;
269 /* Update the pointers. */
270 file->lzmafs_outptr += amtcopy;
271 file->lzmafs_outlen -= amtcopy;
272 len -= amtcopy;
273 ttlcopy += amtcopy;
274 file->lzmafs_unpackSize -= amtcopy;
276 if (file->lzmafs_unpackSize == 0) {
277 file->lzmafs_eofseen = 1;
279 /* If we've eaten all of the output, reset and call decode
280 again. */
282 if (file->lzmafs_outlen == 0) {
283 /* If no input data to decompress, get some more if we can. */
284 if (file->lzmafs_eofseen)
285 break;
286 if (file->lzmafs_inlen == 0) {
287 file->lzmafs_inlen = BDREAD(file->lzmafs_fsctx->lzmafsctx_subops,
288 file->lzmafs_subfile,
289 file->lzmafs_inbuf,
290 LZMAFS_INBUFSIZE);
291 /* If at EOF or error, get out. */
292 if (file->lzmafs_inlen <= 0)
293 break;
294 file->lzmafs_inptr = file->lzmafs_inbuf;
296 in_processed = file->lzmafs_inlen;
297 file->lzmafs_outlen = LZMAFS_OUTBUFSIZE;
298 file->lzmafs_outptr = file->lzmafs_outbuf;
300 if (file->lzmafs_outlen > file->lzmafs_unpackSize) {
301 file->lzmafs_outlen = (SizeT)file->lzmafs_unpackSize;
302 finishMode = LZMA_FINISH_END;
304 /* decompress the input data. */
305 err = LzmaDec_DecodeToBuf(file->lzmafs_state, file->lzmafs_outbuf, (SizeT *)&file->lzmafs_outlen,
306 file->lzmafs_inptr, &in_processed, finishMode, &status);
307 file->lzmafs_inptr += in_processed;
308 file->lzmafs_inlen -= in_processed;
309 if (err != SZ_OK) {
310 res = CFE_ERR;
311 break;
316 file->lzmafs_fileoffset += ttlcopy;
317 return (res < 0) ? res : ttlcopy;
320 static int lzmafs_fileop_write(void *ref,uint8_t *buf,int len)
322 return CFE_ERR_UNSUPPORTED;
325 static int lzmafs_fileop_seek(void *ref,int offset,int how)
327 return CFE_ERR_UNSUPPORTED;
331 static void lzmafs_fileop_close(void *ref)
333 lzmafs_file_t *file = (lzmafs_file_t *) ref;
335 file->lzmafs_fsctx->lzmafsctx_refcnt--;
337 BDCLOSE(file->lzmafs_fsctx->lzmafsctx_subops,file->lzmafs_subfile);
338 if (file->lzmafs_inbuf)
339 KFREE(file->lzmafs_inbuf);
340 if (file->lzmafs_outbuf)
341 KFREE(file->lzmafs_outbuf);
342 if (file->lzmafs_state) {
343 LzmaDec_Free(file->lzmafs_state, &g_Alloc);
344 KFREE(file->lzmafs_state);
346 if (file)
347 KFREE(file);
350 static void lzmafs_fileop_uninit(void *fsctx_arg)
352 lzmafs_fsctx_t *fsctx = (lzmafs_fsctx_t *) fsctx_arg;
354 if (fsctx->lzmafsctx_refcnt) {
355 xprintf("lzmafs_fileop_uninit: warning: refcnt not zero\n");
358 BDUNINIT(fsctx->lzmafsctx_subops,fsctx->lzmafsctx_subfsctx);
360 KFREE(fsctx);
362 #endif