1/*
2 * squashfs with lzma compression
3 *
4 * Copyright (C) 2010, Broadcom Corporation. All Rights Reserved.
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
13 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
15 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
16 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 *
18 * $Id: sqlzma.c,v 1.3 2009/03/10 08:42:14 Exp $
19 */
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23#include <errno.h>
24#include "LzmaDec.h"
25#include "LzmaEnc.h"
26#include "sqlzma.h"
27
28static void *SzAlloc(void *p, size_t size) { p = p; return malloc(size); }
29static void SzFree(void *p, void *address) { p = p; free(address); }
30static ISzAlloc g_Alloc = { SzAlloc, SzFree };
31
32int LzmaUncompress(char *dst, unsigned long * dstlen, char *src, int srclen)
33{
34	int res;
35	SizeT inSizePure;
36	ELzmaStatus status;
37
38	if (srclen < LZMA_PROPS_SIZE)
39	{
40		memcpy(dst, src, srclen);
41		return srclen;
42	}
43	inSizePure = srclen - LZMA_PROPS_SIZE;
44	res = LzmaDecode(dst, dstlen, src + LZMA_PROPS_SIZE, &inSizePure,
45	                 src, LZMA_PROPS_SIZE, LZMA_FINISH_ANY, &status, &g_Alloc);
46	srclen = inSizePure ;
47
48	if ((res == SZ_OK) ||
49		((res == SZ_ERROR_INPUT_EOF) && (srclen == inSizePure)))
50		res = 0;
51	if (res != SZ_OK)
52		printf("LzmaUncompress: error (%d)\n", res);
53	return res;
54}
55
56
57int LzmaCompress(char *in_data, int in_size, char *out_data, int out_size, unsigned long *total_out)
58{
59	CLzmaEncProps props;
60	size_t headerSize = LZMA_PROPS_SIZE;
61	int ret;
62	SizeT outProcess;
63
64	LzmaEncProps_Init(&props);
65	props.algo = 1;
66	outProcess = out_size - LZMA_PROPS_SIZE;
67	ret = LzmaEncode(out_data+LZMA_PROPS_SIZE, &outProcess, in_data, in_size, &props, out_data,
68						&headerSize, 0, NULL, &g_Alloc, &g_Alloc);
69	*total_out = outProcess + LZMA_PROPS_SIZE;
70	return ret;
71}
72