Rizin
unix-like reverse engineering framework and cli tools
delta_common.c
Go to the documentation of this file.
1 //
5 //
6 // Author: Lasse Collin
7 //
8 // This file has been put into the public domain.
9 // You can do whatever you want with this file.
10 //
12 
13 #include "delta_common.h"
14 #include "delta_private.h"
15 
16 
17 static void
18 delta_coder_end(void *coder_ptr, const lzma_allocator *allocator)
19 {
20  lzma_delta_coder *coder = coder_ptr;
21  lzma_next_end(&coder->next, allocator);
22  lzma_free(coder, allocator);
23  return;
24 }
25 
26 
27 extern lzma_ret
30 {
31  // Allocate memory for the decoder if needed.
32  lzma_delta_coder *coder = next->coder;
33  if (coder == NULL) {
34  coder = lzma_alloc(sizeof(lzma_delta_coder), allocator);
35  if (coder == NULL)
36  return LZMA_MEM_ERROR;
37 
38  next->coder = coder;
39 
40  // End function is the same for encoder and decoder.
41  next->end = &delta_coder_end;
42  coder->next = LZMA_NEXT_CODER_INIT;
43  }
44 
45  // Validate the options.
47  return LZMA_OPTIONS_ERROR;
48 
49  // Set the delta distance.
50  const lzma_options_delta *opt = filters[0].options;
51  coder->distance = opt->dist;
52 
53  // Initialize the rest of the variables.
54  coder->pos = 0;
56 
57  // Initialize the next decoder in the chain, if any.
58  return lzma_next_filter_init(&coder->next, allocator, filters + 1);
59 }
60 
61 
62 extern uint64_t
64 {
65  const lzma_options_delta *opt = options;
66 
67  if (opt == NULL || opt->type != LZMA_DELTA_TYPE_BYTE
68  || opt->dist < LZMA_DELTA_DIST_MIN
69  || opt->dist > LZMA_DELTA_DIST_MAX)
70  return UINT64_MAX;
71 
72  return sizeof(lzma_delta_coder);
73 }
const lzma_allocator * allocator
Definition: block.h:377
const lzma_filter * filters
Definition: container.h:315
#define NULL
Definition: cris-opc.c:27
@ LZMA_DELTA_TYPE_BYTE
Definition: delta.h:36
#define LZMA_DELTA_DIST_MIN
Definition: delta.h:60
#define LZMA_DELTA_DIST_MAX
Definition: delta.h:61
static void delta_coder_end(void *coder_ptr, const lzma_allocator *allocator)
Definition: delta_common.c:18
uint64_t lzma_delta_coder_memusage(const void *options)
Definition: delta_common.c:63
lzma_ret lzma_delta_coder_init(lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter_info *filters)
Definition: delta_common.c:28
Common stuff for Delta encoder and decoder.
Private common stuff for Delta encoder and decoder.
static const char struct stat static buf struct stat static buf static vhangup int options
Definition: sflib.h:145
unsigned long uint64_t
Definition: sftypes.h:28
#define UINT64_MAX
Custom functions for memory handling.
Definition: base.h:372
lzma_next_coder next
Next coder in the chain.
Definition: delta_private.h:20
uint8_t pos
Position in history[].
Definition: delta_private.h:26
uint8_t history[LZMA_DELTA_DIST_MAX]
Buffer to hold history of the original data.
Definition: delta_private.h:29
size_t distance
Delta distance.
Definition: delta_private.h:23
void * options
Pointer to filter-specific options structure.
Definition: filter.h:63
Hold data and function pointers of the next filter in the chain.
Definition: common.h:135
void * coder
Pointer to coder-specific data.
Definition: common.h:137
lzma_end_function end
Definition: common.h:155
Options for the Delta filter.
Definition: delta.h:45
uint32_t dist
Delta distance.
Definition: delta.h:59
lzma_delta_type type
Definition: delta.h:47
#define LZMA_NEXT_CODER_INIT
Macro to initialize lzma_next_coder structure.
Definition: common.h:180
void * lzma_alloc(size_t size, const lzma_allocator *allocator) lzma_attribute((__malloc__)) lzma_attr_alloc_size(1)
Allocates memory.
#define memzero(s, n)
Definition: sysdefs.h:180
lzma_ret
Return values used by several functions in liblzma.
Definition: base.h:57
@ LZMA_MEM_ERROR
Cannot allocate memory.
Definition: base.h:128
@ LZMA_OPTIONS_ERROR
Invalid or unsupported options.
Definition: base.h:160
lzma_ret lzma_next_filter_init(lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter_info *filters)
Definition: common.c:116
void lzma_free(void *ptr, const lzma_allocator *allocator)
Frees memory.
Definition: common.c:78
void lzma_next_end(lzma_next_coder *next, const lzma_allocator *allocator)
Definition: common.c:145