Rizin
unix-like reverse engineering framework and cli tools
delta_encoder.c File Reference

Delta filter encoder. More...

#include "delta_encoder.h"
#include "delta_private.h"

Go to the source code of this file.

Functions

static void copy_and_encode (lzma_delta_coder *coder, const uint8_t *restrict in, uint8_t *restrict out, size_t size)
 
static void encode_in_place (lzma_delta_coder *coder, uint8_t *buffer, size_t size)
 
static lzma_ret delta_encode (void *coder_ptr, const lzma_allocator *allocator, const uint8_t *restrict in, size_t *restrict in_pos, size_t in_size, uint8_t *restrict out, size_t *restrict out_pos, size_t out_size, lzma_action action)
 
static lzma_ret delta_encoder_update (void *coder_ptr, const lzma_allocator *allocator, const lzma_filter *filters_null lzma_attribute((__unused__)), const lzma_filter *reversed_filters)
 
lzma_ret lzma_delta_encoder_init (lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter_info *filters)
 
lzma_ret lzma_delta_props_encode (const void *options, uint8_t *out)
 

Detailed Description

Delta filter encoder.

Definition in file delta_encoder.c.

Function Documentation

◆ copy_and_encode()

static void copy_and_encode ( lzma_delta_coder coder,
const uint8_t *restrict  in,
uint8_t *restrict  out,
size_t  size 
)
static

Copies and encodes the data at the same time. This is used when Delta is the first filter in the chain (and thus the last filter in the encoder's filter stack).

Definition at line 21 of file delta_encoder.c.

23 {
24  const size_t distance = coder->distance;
25 
26  for (size_t i = 0; i < size; ++i) {
27  const uint8_t tmp = coder->history[
28  (distance + coder->pos) & 0xFF];
29  coder->history[coder->pos-- & 0xFF] = in[i];
30  out[i] = in[i] - tmp;
31  }
32 }
lzma_index ** i
Definition: index.h:629
const lzma_allocator const uint8_t * in
Definition: block.h:527
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
voidpf void uLong size
Definition: ioapi.h:138
unsigned char uint8_t
Definition: sftypes.h:31
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

References lzma_delta_coder::distance, lzma_delta_coder::history, i, in, out, lzma_delta_coder::pos, and autogen_x86imm::tmp.

Referenced by delta_encode().

◆ delta_encode()

static lzma_ret delta_encode ( void *  coder_ptr,
const lzma_allocator allocator,
const uint8_t *restrict  in,
size_t *restrict  in_pos,
size_t  in_size,
uint8_t *restrict  out,
size_t *restrict  out_pos,
size_t  out_size,
lzma_action  action 
)
static

Definition at line 52 of file delta_encoder.c.

56 {
57  lzma_delta_coder *coder = coder_ptr;
58 
59  lzma_ret ret;
60 
61  if (coder->next.code == NULL) {
62  const size_t in_avail = in_size - *in_pos;
63  const size_t out_avail = out_size - *out_pos;
64  const size_t size = my_min(in_avail, out_avail);
65 
66  copy_and_encode(coder, in + *in_pos, out + *out_pos, size);
67 
68  *in_pos += size;
69  *out_pos += size;
70 
71  ret = action != LZMA_RUN && *in_pos == in_size
73 
74  } else {
75  const size_t out_start = *out_pos;
76 
77  ret = coder->next.code(coder->next.coder, allocator,
78  in, in_pos, in_size, out, out_pos, out_size,
79  action);
80 
81  encode_in_place(coder, out + out_start, *out_pos - out_start);
82  }
83 
84  return ret;
85 }
const lzma_allocator const uint8_t size_t uint8_t size_t * out_pos
Definition: block.h:528
const lzma_allocator const uint8_t size_t * in_pos
Definition: block.h:579
const lzma_allocator const uint8_t size_t in_size
Definition: block.h:527
const lzma_allocator * allocator
Definition: block.h:377
#define NULL
Definition: cris-opc.c:27
static void encode_in_place(lzma_delta_coder *coder, uint8_t *buffer, size_t size)
Definition: delta_encoder.c:38
static void copy_and_encode(lzma_delta_coder *coder, const uint8_t *restrict in, uint8_t *restrict out, size_t size)
Definition: delta_encoder.c:21
lzma_next_coder next
Next coder in the chain.
Definition: delta_private.h:20
lzma_code_function code
Pointer to function to do the actual coding.
Definition: common.h:150
void * coder
Pointer to coder-specific data.
Definition: common.h:137
#define my_min(x, y)
Definition: sysdefs.h:185
lzma_ret
Return values used by several functions in liblzma.
Definition: base.h:57
@ LZMA_STREAM_END
End of stream was reached.
Definition: base.h:63
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58
@ LZMA_RUN
Continue coding.
Definition: base.h:251

References test-lz4-speed::action, allocator, lzma_next_coder_s::code, lzma_next_coder_s::coder, copy_and_encode(), encode_in_place(), in, in_pos, in_size, LZMA_OK, LZMA_RUN, LZMA_STREAM_END, my_min, lzma_delta_coder::next, NULL, out, and out_pos.

Referenced by lzma_delta_encoder_init().

◆ delta_encoder_update()

static lzma_ret delta_encoder_update ( void *  coder_ptr,
const lzma_allocator allocator,
const lzma_filter *filters_null   lzma_attribute(__unused__),
const lzma_filter reversed_filters 
)
static

Definition at line 89 of file delta_encoder.c.

92 {
93  lzma_delta_coder *coder = coder_ptr;
94 
95  // Delta doesn't and will never support changing the options in
96  // the middle of encoding. If the app tries to change them, we
97  // simply ignore them.
99  &coder->next, allocator, reversed_filters + 1);
100 }
lzma_ret lzma_next_filter_update(lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter *reversed_filters)
Definition: common.c:127

References allocator, lzma_next_filter_update(), and lzma_delta_coder::next.

Referenced by lzma_delta_encoder_init().

◆ encode_in_place()

static void encode_in_place ( lzma_delta_coder coder,
uint8_t buffer,
size_t  size 
)
static

Encodes the data in place. This is used when we are the last filter in the chain (and thus non-last filter in the encoder's filter stack).

Definition at line 38 of file delta_encoder.c.

39 {
40  const size_t distance = coder->distance;
41 
42  for (size_t i = 0; i < size; ++i) {
43  const uint8_t tmp = coder->history[
44  (distance + coder->pos) & 0xFF];
45  coder->history[coder->pos-- & 0xFF] = buffer[i];
46  buffer[i] -= tmp;
47  }
48 }
Definition: buffer.h:15

References lzma_delta_coder::distance, lzma_delta_coder::history, i, lzma_delta_coder::pos, and autogen_x86imm::tmp.

Referenced by delta_encode().

◆ lzma_delta_encoder_init()

lzma_ret lzma_delta_encoder_init ( lzma_next_coder next,
const lzma_allocator allocator,
const lzma_filter_info filters 
)

Definition at line 104 of file delta_encoder.c.

106 {
107  next->code = &delta_encode;
108  next->update = &delta_encoder_update;
109  return lzma_delta_coder_init(next, allocator, filters);
110 }
const lzma_filter * filters
Definition: container.h:315
lzma_ret lzma_delta_coder_init(lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter_info *filters)
Definition: delta_common.c:28
static lzma_ret delta_encode(void *coder_ptr, const lzma_allocator *allocator, const uint8_t *restrict in, size_t *restrict in_pos, size_t in_size, uint8_t *restrict out, size_t *restrict out_pos, size_t out_size, lzma_action action)
Definition: delta_encoder.c:52
static lzma_ret delta_encoder_update(void *coder_ptr, const lzma_allocator *allocator, const lzma_filter *filters_null lzma_attribute((__unused__)), const lzma_filter *reversed_filters)
Definition: delta_encoder.c:89
lzma_ret(* update)(void *coder, const lzma_allocator *allocator, const lzma_filter *filters, const lzma_filter *reversed_filters)
Definition: common.h:173

References allocator, lzma_next_coder_s::code, delta_encode(), delta_encoder_update(), filters, lzma_delta_coder_init(), and lzma_next_coder_s::update.

◆ lzma_delta_props_encode()

lzma_ret lzma_delta_props_encode ( const void *  options,
uint8_t out 
)

Definition at line 114 of file delta_encoder.c.

115 {
116  // The caller must have already validated the options, so it's
117  // LZMA_PROG_ERROR if they are invalid.
119  return LZMA_PROG_ERROR;
120 
121  const lzma_options_delta *opt = options;
122  out[0] = opt->dist - LZMA_DELTA_DIST_MIN;
123 
124  return LZMA_OK;
125 }
#define LZMA_DELTA_DIST_MIN
Definition: delta.h:60
uint64_t lzma_delta_coder_memusage(const void *options)
Definition: delta_common.c:63
static const char struct stat static buf struct stat static buf static vhangup int options
Definition: sflib.h:145
#define UINT64_MAX
Options for the Delta filter.
Definition: delta.h:45
uint32_t dist
Delta distance.
Definition: delta.h:59
@ LZMA_PROG_ERROR
Programming error.
Definition: base.h:218

References lzma_options_delta::dist, lzma_delta_coder_memusage(), LZMA_DELTA_DIST_MIN, LZMA_OK, LZMA_PROG_ERROR, options, out, and UINT64_MAX.