Debian Codegraph @

libzbitmap.c

raw symbols SCIP
1 // SPDX-License-Identifier: GPL-2.0+ OR MIT
2 /*
3 * Copyright (C) 2022 Corellium LLC
4 *
5 * Author: Ernesto A. Fernández <[email protected]>
6 *
7 * Ported from libzbitmap (https://github.com/eafer/libzbitmap). Only the
8 * decompression code is included.
9 */
10
11 #include <linux/errno.h>
12 #include <linux/string.h>
13 #include "libzbitmap.h"
14
15 #define ZBM_MAGIC "ZBM\x09"
16 #define ZBM_MAGIC_SZ 4
17
18 #define ZBM_MAX_DECMP_CHUNK_SIZE 0x8000
19 #define ZBM_MAX_DECMP_CHUNK_SIZE_BITS 15
20
21 struct uint24 {
22 uint8_t low;
23 uint8_t mid;
24 uint8_t hig;
25 };
26
27 /* This header is shared by both compressed and decompressed chunks */
28 struct zbm_chunk_hdr {
29 struct uint24 len; /* Length of the chunk */
30 struct uint24 decmp_len; /* Length of the chunk after decompression */
31 };
32
33 /* The full header for compressed chunks */
34 struct zbm_cmp_chunk_hdr {
35 /* Shared with decompressed chunks */
36 struct zbm_chunk_hdr hdr;
37
38 /* Offset for each of the three metadata areas */
39 struct uint24 meta_off_1;
40 struct uint24 meta_off_2;
41 struct uint24 meta_off_3;
42 };
43
44 /* Pointer to a half-byte */
45 struct nybl_ptr {
46 uint8_t *addr; /* Address of the byte */
47 int nibble; /* Which of the two nibbles? */
48 };
49
50 /* 0-2 and 0xf are not real bitmap indexes */
51 #define ZBM_BITMAP_COUNT (16 - 1 - 3)
52 #define ZBM_BITMAP_BASE 3
53 #define ZBM_BITMAP_BYTECNT 17
54 #define ZBM_MAX_PERIOD_BYTECNT 2
55
56 struct zbm_bmap {
57 uint8_t bitmap; /* The bitmap */
58 uint8_t period_bytecnt; /* Read this many bytes to get the new period */
59 };
60
61 struct zbm_state {
62 /* Updated during a chunk read */
63 uint8_t *dest; /* Write the next byte here */
64 size_t dest_left; /* Room left in destination buffer */
65 uint32_t written; /* Bytes written so far for current chunk */
66 uint16_t period; /* Repetition period for decompression, in bytes */
67
68 /* Updated right before a chunk read */
69 const uint8_t *src_end; /* End of current chunk */
70 uint32_t len; /* Length of the chunk */
71 uint32_t decmp_len; /* Expected chunk length after decompression */
72
73 /* Updated after a chunk read */
74 const uint8_t *src; /* Start of buffer, or current chunk if any */
75 size_t src_left; /* Room left in the source buffer */
76 size_t prewritten; /* Bytes written for previous chunks */
77
78 /* Current position in data and metadata areas for this chunk */
79 const uint8_t *data;
80 const uint8_t *meta_1;
81 const uint8_t *meta_2;
82 struct nybl_ptr meta_3;
83
84 /* Array of bitmaps for the current chunk */
85 struct zbm_bmap bitmaps[ZBM_BITMAP_COUNT];
86 };
87
88 static int zbm_check_magic(struct zbm_state *state)
89 {
90 if(state->src_left < ZBM_MAGIC_SZ)
91 return -EINVAL;
92
93 if(memcmp(state->src, ZBM_MAGIC, ZBM_MAGIC_SZ))
94 return -EINVAL;
95
96 state->src += ZBM_MAGIC_SZ;
97 state->src_left -= ZBM_MAGIC_SZ;
98 return 0;
99 }
100
101 static uint32_t zbm_u24_to_u32(struct uint24 n)
102 {
103 uint32_t res;
104
105 res = n.hig;
106 res <<= 8;
107 res += n.mid;
108 res <<= 8;
109 res += n.low;
110 return res;
111 }
112
113 /* Some chunks just have regular uncompressed data, but with a header */
114 static int zbm_chunk_is_uncompressed(struct zbm_state *state)
115 {
116 return state->len == state->decmp_len + sizeof(struct zbm_chunk_hdr);
117 }
118
119 static int zbm_handle_uncompressed_chunk(struct zbm_state *state)
120 {
121 state->meta_1 = state->meta_2 = NULL;
122 state->meta_3.addr = NULL;
123 state->meta_3.nibble = 0;
124 state->data = state->src + sizeof(struct zbm_chunk_hdr);
125 memcpy(state->dest, state->data, state->decmp_len);
126
127 state->dest += state->decmp_len;
128 state->dest_left -= state->decmp_len;
129 state->written = state->decmp_len;
130 return 0;
131 }
132
133 static int zbm_read_nibble(struct nybl_ptr *nybl, const uint8_t *limit, uint8_t *result)
134 {
135 if(nybl->addr >= limit)
136 return -EINVAL;
137
138 if(nybl->nibble == 0) {
139 *result = *nybl->addr & 0xf;
140 nybl->nibble = 1;
141 } else {
142 *result = (*nybl->addr >> 4) & 0xf;
143 nybl->nibble = 0;
144 ++nybl->addr;
145 }
146 return 0;
147 }
148
149 static void zbm_rewind_nibble(struct nybl_ptr *nybl)
150 {
151 if(nybl->nibble == 0) {
152 nybl->nibble = 1;
153 --nybl->addr;
154 } else {
155 nybl->nibble = 0;
156 }
157 }
158
159 static int zbm_apply_bitmap(struct zbm_state *state, struct zbm_bmap *bitmap)
160 {
161 int i;
162
163 /* The periods are stored in the first metadata area */
164 if(bitmap->period_bytecnt) {
165 state->period = 0;
166 for(i = 0; i < bitmap->period_bytecnt; ++i) {
167 if(state->meta_1 >= state->src_end)
168 return -EINVAL;
169 state->period |= *state->meta_1 << i * 8;
170 ++state->meta_1;
171 }
172 }
173 if(state->period == 0)
174 return -EINVAL;
175
176 for(i = 0; i < 8; ++i) {
177 if(state->written == state->decmp_len)
178 break;
179 if(bitmap->bitmap & 1 << i) {
180 if(state->data >= state->src_end)
181 return -EINVAL;
182 *state->dest = *state->data;
183 ++state->data;
184 } else {
185 if(state->prewritten + state->written < state->period)
186 return -EINVAL;
187 *state->dest = *(state->dest - state->period);
188 }
189 ++state->dest;
190 --state->dest_left;
191 ++state->written;
192 }
193
194 return 0;
195 }
196
197 static int zbm_apply_bitmap_number(struct zbm_state *state, uint8_t bmp_num)
198 {
199 struct zbm_bmap next = {0};
200
201 /* Not a valid bitmap number (it signals a repetition) */
202 if(bmp_num == 0xf)
203 return -EINVAL;
204
205 /* An actual index in the bitmap array */
206 if(bmp_num > ZBM_MAX_PERIOD_BYTECNT)
207 return zbm_apply_bitmap(state, &state->bitmaps[bmp_num - ZBM_BITMAP_BASE]);
208
209 /* For < 2, use the next bitmap in the second metadata area */
210 if(state->meta_2 >= state->src_end)
211 return -EINVAL;
212 next.bitmap = *state->meta_2;
213 next.period_bytecnt = bmp_num;
214 ++state->meta_2;
215 return zbm_apply_bitmap(state, &next);
216 }
217
218 /* Find out how many times we need to repeat the current bitmap operation */
219 static int zbm_read_repetition_count(struct zbm_state *state, uint16_t *repeat)
220 {
221 uint8_t nibble;
222 uint16_t total;
223 int err;
224
225 /* Don't confuse the trailing bitmaps with a repetition count */
226 if(state->decmp_len - state->written <= 8) {
227 *repeat = 1;
228 return 0;
229 }
230
231 err = zbm_read_nibble(&state->meta_3, state->src_end, &nibble);
232 if(err)
233 return err;
234
235 if(nibble != 0xf) {
236 /* No repetition count: the previous bitmap number gets applied once */
237 zbm_rewind_nibble(&state->meta_3);
238 *repeat = 1;
239 return 0;
240 }
241
242 /*
243 * Under this scheme, repeating a bitmap number 3 times wouldn't save any
244 * space, so the repetition count starts from 4.
245 */
246 total = 4;
247 while(nibble == 0xf) {
248 err = zbm_read_nibble(&state->meta_3, state->src_end, &nibble);
249 if(err)
250 return err;
251 total += nibble;
252 if(total < nibble)
253 return -EINVAL;
254 }
255
256 *repeat = total;
257 return 0;
258 }
259
260 static int zbm_decompress_single_bitmap(struct zbm_state *state)
261 {
262 uint8_t bmp_num;
263 uint16_t repeat;
264 int i;
265 int err;
266
267 /* The current nibble is the offset of the next bitmap to apply */
268 err = zbm_read_nibble(&state->meta_3, state->src_end, &bmp_num);
269 if(err)
270 return err;
271
272 err = zbm_read_repetition_count(state, &repeat);
273 if(err)
274 return err;
275
276 for(i = 0; i < repeat; ++i) {
277 err = zbm_apply_bitmap_number(state, bmp_num);
278 if(err)
279 return err;
280 }
281 return 0;
282 }
283
284 /* Pointer to a bit */
285 struct bit_ptr {
286 uint8_t *addr; /* Address of the byte */
287 int offset; /* Bit number */
288 };
289
290 /* This function does not perform boundary checks, the caller must do it */
291 static int zbm_read_single_bit(struct bit_ptr *bit)
292 {
293 int res = *bit->addr >> bit->offset & 1;
294
295 ++bit->offset;
296 if(bit->offset != 8)
297 return res;
298 bit->offset = 0;
299 ++bit->addr;
300 return res;
301 }
302
303 static int zbm_read_single_bitmap(struct bit_ptr *bit, const uint8_t *limit, struct zbm_bmap *result)
304 {
305 int i;
306
307 result->bitmap = 0;
308 result->period_bytecnt = 0;
309
310 /* The bitmap itself */
311 for(i = 0; i < 8; ++i) {
312 if(bit->addr >= limit)
313 return -EINVAL;
314 result->bitmap |= zbm_read_single_bit(bit) << i;
315 }
316
317 /*
318 * The two trailing bits tell us how many bytes to read for the next
319 * repetition period
320 */
321 for(i = 0; i < 2; ++i) {
322 if(bit->addr >= limit)
323 return -EINVAL;
324 result->period_bytecnt |= zbm_read_single_bit(bit) << i;
325 }
326
327 return 0;
328 }
329
330 static int zbm_read_bitmaps(struct zbm_state *state)
331 {
332 struct bit_ptr bmap = {0};
333 int err, i;
334
335 if(state->len < ZBM_BITMAP_BYTECNT)
336 return -EINVAL;
337
338 bmap.addr = (uint8_t *)state->src_end - ZBM_BITMAP_BYTECNT;
339 bmap.offset = 0;
340
341 for(i = 0; i < ZBM_BITMAP_COUNT; ++i) {
342 err = zbm_read_single_bitmap(&bmap, state->src_end, &state->bitmaps[i]);
343 if(err)
344 return err;
345 if(state->bitmaps[i].period_bytecnt > ZBM_MAX_PERIOD_BYTECNT)
346 return -EINVAL;
347 }
348 return 0;
349 }
350
351 static int zbm_handle_compressed_chunk(struct zbm_state *state)
352 {
353 const struct zbm_cmp_chunk_hdr *hdr = NULL;
354 uint32_t meta_off_1, meta_off_2, meta_off_3;
355 int err;
356
357 state->written = 0;
358 state->period = 8;
359
360 if(state->len < sizeof(*hdr))
361 return -EINVAL;
362 hdr = (struct zbm_cmp_chunk_hdr *)state->src;
363 state->data = state->src + sizeof(*hdr);
364
365 meta_off_1 = zbm_u24_to_u32(hdr->meta_off_1);
366 meta_off_2 = zbm_u24_to_u32(hdr->meta_off_2);
367 meta_off_3 = zbm_u24_to_u32(hdr->meta_off_3);
368 if(meta_off_1 >= state->len || meta_off_2 >= state->len || meta_off_3 >= state->len)
369 return -EINVAL;
370 state->meta_1 = state->src + meta_off_1;
371 state->meta_2 = state->src + meta_off_2;
372 state->meta_3.addr = (uint8_t *)state->src + meta_off_3;
373 state->meta_3.nibble = 0;
374
375 err = zbm_read_bitmaps(state);
376 if(err)
377 return err;
378
379 while(state->written < state->decmp_len) {
380 err = zbm_decompress_single_bitmap(state);
381 if(err)
382 return err;
383 }
384
385 return 0;
386 }
387
388 static int zbm_handle_chunk(struct zbm_state *state)
389 {
390 const struct zbm_chunk_hdr *decmp_hdr = NULL;
391
392 if(state->src_left < sizeof(*decmp_hdr))
393 return -EINVAL;
394 decmp_hdr = (struct zbm_chunk_hdr *)state->src;
395
396 state->len = zbm_u24_to_u32(decmp_hdr->len);
397 if(state->len > state->src_left)
398 return -EINVAL;
399 state->src_end = state->src + state->len;
400
401 state->decmp_len = zbm_u24_to_u32(decmp_hdr->decmp_len);
402 if(state->decmp_len > ZBM_MAX_DECMP_CHUNK_SIZE)
403 return -EINVAL;
404 if(!state->dest) /* We just wanted the length, so we are done */
405 return 0;
406 if(state->decmp_len > state->dest_left)
407 return -ERANGE;
408
409 if(zbm_chunk_is_uncompressed(state))
410 return zbm_handle_uncompressed_chunk(state);
411
412 return zbm_handle_compressed_chunk(state);
413 }
414
415 int zbm_decompress(void *dest, size_t dest_size, const void *src, size_t src_size, size_t *out_len)
416 {
417 struct zbm_state state = {0};
418 int err;
419
420 state.src = src;
421 state.src_left = src_size;
422 state.dest = dest;
423 state.dest_left = dest_size;
424 state.prewritten = 0;
425
426 err = zbm_check_magic(&state);
427 if(err)
428 return err;
429
430 /* The final chunk has zero decompressed length */
431 do {
432 err = zbm_handle_chunk(&state);
433 if(err)
434 return err;
435 state.src += state.len;
436 state.src_left -= state.len;
437 state.prewritten += state.decmp_len;
438 } while(state.decmp_len != 0);
439
440 *out_len = state.prewritten;
441 return 0;
442 }