summaryrefslogtreecommitdiff
path: root/util/cbfstool/lzma/lzma.c
blob: 914d8b7da924e0d9db63654b9701c7f926db6b29 (plain)
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "../common.h"
#include "C/LzmaDec.h"
#include "C/LzmaEnc.h"

/* Endianness / unaligned memory access handling */

#if defined(__x86_64__) || defined(__i386__)
#define LITTLE_ENDIAN_AND_UNALIGNED_ACCESS_OK
#else
#undef LITTLE_ENDIAN_AND_UNALIGNED_ACCESS_OK
#endif

#define L (uint64_t)

static inline uint64_t get_64(const void *p)
{
#ifdef LITTLE_ENDIAN_AND_UNALIGNED_ACCESS_OK
	return *(const uint64_t *)p;
#else
	const unsigned char *data = (const unsigned char *)p;
	return (L data[0]) | (L data[1] << 8) | (L data[2] << 16) |
		(L data[3] << 24) | (L data [4] << 32) | (L data[5] << 40) |
		(L data[6] << 48) | (L data[7] << 56);
#endif
}

static void put_64(void *p, uint64_t value)
{
#ifdef LITTLE_ENDIAN_AND_UNALIGNED_ACCESS_OK
	*(uint64_t *) p = value;
#else
	unsigned char *data = (unsigned char *)p;
	data[0] = value & 0xff;
	data[1] = (value >> 8) & 0xff;
	data[2] = (value >> 16) & 0xff;
	data[3] = (value >> 24) & 0xff;
	data[4] = (value >> 32) & 0xff;
	data[5] = (value >> 40) & 0xff;
	data[6] = (value >> 48) & 0xff;
	data[7] = (value >> 56) & 0xff;
#endif
}

/* Memory Allocation API */

static void *SzAlloc(void *unused, size_t size)
{
	return malloc(size);
}

static void SzFree(void *unused, void *address)
{
	free(address);
}

static ISzAlloc LZMAalloc = { SzAlloc, SzFree };

/* Streaming API */

typedef struct vector {
	char *p;
	size_t pos;
	size_t size;
} vector_t;

static vector_t instream, outstream;

static SRes Read(void *unused, void *buf, size_t *size)
{
	if ((instream.size - instream.pos) < *size)
		*size = instream.size - instream.pos;
	memcpy(buf, instream.p + instream.pos, *size);
	instream.pos += *size;
	return SZ_OK;
}

static size_t Write(void *unused, const void *buf, size_t size)
{
	if(outstream.size - outstream.pos < size)
		size = outstream.size - outstream.pos;
	memcpy(outstream.p + outstream.pos, buf, size);
	outstream.pos += size;
	return size;
}

static ISeqInStream is = { Read };
static ISeqOutStream os = { Write };

/**
 * Compress a buffer with lzma
 * Don't copy the result back if it is too large.
 * @param in a pointer to the buffer
 * @param in_len the length in bytes
 * @param out a pointer to a buffer of at least size in_len
 * @param out_len a pointer to the compressed length of in
 */

void do_lzma_compress(char *in, int in_len, char *out, int *out_len)
{
	if (in_len == 0) {
		ERROR("LZMA: Input length is zero.\n");
		return;
	}

	CLzmaEncProps props;
	LzmaEncProps_Init(&props);
	props.dictSize = in_len;
	props.pb = 0; /* PosStateBits, default: 2, range: 0..4 */
	props.lp = 0; /* LiteralPosStateBits, default: 0, range: 0..4 */
	props.lc = 1; /* LiteralContextBits, default: 3, range: 0..8 */
	props.fb = 273; /* NumFastBytes */
	props.mc = 0; /* MatchFinderCycles, default: 0 */
	props.algo = 1; /* AlgorithmNo, apparently, 0 and 1 are valid values. 0 = fast mode */
	props.numThreads = 1;

	switch (props.algo) {
	case 0:	// quick: HC4
		props.btMode = 0;
		props.level = 1;
		break;
	case 1:	// full: BT4
	default:
		props.level = 9;
		props.btMode = 1;
		props.numHashBytes = 4;
		break;
	}

	CLzmaEncHandle p = LzmaEnc_Create(&LZMAalloc);

	int res = LzmaEnc_SetProps(p, &props);
	if (res != SZ_OK) {
		ERROR("LZMA: LzmaEnc_SetProps failed.\n");
		return;
	}

	unsigned char propsEncoded[LZMA_PROPS_SIZE + 8];
	size_t propsSize = sizeof propsEncoded;
	res = LzmaEnc_WriteProperties(p, propsEncoded, &propsSize);
	if (res != SZ_OK) {
		ERROR("LZMA: LzmaEnc_WriteProperties failed.\n");
		return;
	}

	instream.p = in;
	instream.size = in_len;

	outstream.p = out;
	outstream.size = in_len;

	put_64(propsEncoded + LZMA_PROPS_SIZE, in_len);
	Write(&os, propsEncoded, LZMA_PROPS_SIZE+8);

	res = LzmaEnc_Encode(p, &os, &is, 0, &LZMAalloc, &LZMAalloc);
	if (res != SZ_OK) {
		ERROR("LZMA: LzmaEnc_Encode failed %d.\n", res);
		return;
	}

	*out_len = outstream.pos;
}

void do_lzma_uncompress(char *dst, int dst_len, char *src, int src_len)
{
	if (src_len <= LZMA_PROPS_SIZE + 8) {
		ERROR("LZMA: Input length is too small.\n");
		return;
	}

	uint64_t out_sizemax = get_64(&src[LZMA_PROPS_SIZE]);

	if (out_sizemax > (size_t) dst_len) {
		ERROR("Not copying %d bytes to %d-byte buffer!\n",
			(unsigned int)out_sizemax, dst_len);
		return;
	}

	ELzmaStatus status;

	size_t destlen = out_sizemax;
	size_t srclen = src_len - (LZMA_PROPS_SIZE + 8);

	int res = LzmaDecode((Byte *) dst, &destlen,
			     (Byte *) &src[LZMA_PROPS_SIZE + 8], &srclen,
			     (Byte *) &src[0], LZMA_PROPS_SIZE,
			     LZMA_FINISH_END,
			     &status,
			     &LZMAalloc);

	if (res != SZ_OK) {
		ERROR("Error while decompressing.\n");
		return;
	}
}