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

Encoder for LZMA_Alone files. More...

#include "common.h"
#include "lzma_encoder.h"

Go to the source code of this file.

Classes

struct  lzma_alone_coder
 

Macros

#define ALONE_HEADER_SIZE   (1 + 4 + 8)
 

Functions

static lzma_ret alone_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 void alone_encoder_end (void *coder_ptr, const lzma_allocator *allocator)
 
static lzma_ret alone_encoder_init (lzma_next_coder *next, const lzma_allocator *allocator, const lzma_options_lzma *options)
 
 LZMA_API (lzma_ret)
 

Detailed Description

Encoder for LZMA_Alone files.

Definition in file alone_encoder.c.

Macro Definition Documentation

◆ ALONE_HEADER_SIZE

#define ALONE_HEADER_SIZE   (1 + 4 + 8)

Definition at line 17 of file alone_encoder.c.

Function Documentation

◆ alone_encode()

static lzma_ret alone_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 34 of file alone_encoder.c.

39 {
40  lzma_alone_coder *coder = coder_ptr;
41 
42  while (*out_pos < out_size)
43  switch (coder->sequence) {
44  case SEQ_HEADER:
45  lzma_bufcpy(coder->header, &coder->header_pos,
47  out, out_pos, out_size);
48  if (coder->header_pos < ALONE_HEADER_SIZE)
49  return LZMA_OK;
50 
51  coder->sequence = SEQ_CODE;
52  break;
53 
54  case SEQ_CODE:
55  return coder->next.code(coder->next.coder,
57  out, out_pos, out_size, action);
58 
59  default:
60  assert(0);
61  return LZMA_PROG_ERROR;
62  }
63 
64  return LZMA_OK;
65 }
#define ALONE_HEADER_SIZE
Definition: alone_encoder.c:17
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
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
assert(limit<=UINT32_MAX/2)
enum lzma_alone_coder::@640 sequence
lzma_next_coder next
Definition: alone_decoder.c:19
uint8_t header[ALONE_HEADER_SIZE]
Definition: alone_encoder.c:29
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
@ LZMA_PROG_ERROR
Programming error.
Definition: base.h:218
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58
size_t lzma_bufcpy(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)
Definition: common.c:94

References test-lz4-speed::action, allocator, ALONE_HEADER_SIZE, assert(), lzma_next_coder_s::code, lzma_next_coder_s::coder, lzma_alone_coder::header, lzma_alone_coder::header_pos, in, in_pos, in_size, lzma_bufcpy(), LZMA_OK, LZMA_PROG_ERROR, lzma_alone_coder::next, out, out_pos, and lzma_alone_coder::sequence.

Referenced by alone_encoder_init().

◆ alone_encoder_end()

static void alone_encoder_end ( void *  coder_ptr,
const lzma_allocator allocator 
)
static

Definition at line 69 of file alone_encoder.c.

70 {
71  lzma_alone_coder *coder = coder_ptr;
72  lzma_next_end(&coder->next, allocator);
73  lzma_free(coder, allocator);
74  return;
75 }
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

References allocator, lzma_free(), lzma_next_end(), and lzma_alone_coder::next.

Referenced by alone_encoder_init().

◆ alone_encoder_init()

static lzma_ret alone_encoder_init ( lzma_next_coder next,
const lzma_allocator allocator,
const lzma_options_lzma options 
)
static

Definition at line 80 of file alone_encoder.c.

82 {
84 
85  lzma_alone_coder *coder = next->coder;
86 
87  if (coder == NULL) {
88  coder = lzma_alloc(sizeof(lzma_alone_coder), allocator);
89  if (coder == NULL)
90  return LZMA_MEM_ERROR;
91 
92  next->coder = coder;
93  next->code = &alone_encode;
94  next->end = &alone_encoder_end;
95  coder->next = LZMA_NEXT_CODER_INIT;
96  }
97 
98  // Basic initializations
99  coder->sequence = SEQ_HEADER;
100  coder->header_pos = 0;
101 
102  // Encode the header:
103  // - Properties (1 byte)
105  return LZMA_OPTIONS_ERROR;
106 
107  // - Dictionary size (4 bytes)
108  if (options->dict_size < LZMA_DICT_SIZE_MIN)
109  return LZMA_OPTIONS_ERROR;
110 
111  // Round up to the next 2^n or 2^n + 2^(n - 1) depending on which
112  // one is the next unless it is UINT32_MAX. While the header would
113  // allow any 32-bit integer, we do this to keep the decoder of liblzma
114  // accepting the resulting files.
115  uint32_t d = options->dict_size - 1;
116  d |= d >> 2;
117  d |= d >> 3;
118  d |= d >> 4;
119  d |= d >> 8;
120  d |= d >> 16;
121  if (d != UINT32_MAX)
122  ++d;
123 
124  write32le(coder->header + 1, d);
125 
126  // - Uncompressed size (always unknown and using EOPM)
127  memset(coder->header + 1 + 4, 0xFF, 8);
128 
129  // Initialize the LZMA encoder.
130  const lzma_filter_info filters[2] = {
131  {
132  .init = &lzma_lzma_encoder_init,
133  .options = (void *)(options),
134  }, {
135  .init = NULL,
136  }
137  };
138 
139  return lzma_next_filter_init(&coder->next, allocator, filters);
140 }
static lzma_ret alone_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: alone_encoder.c:34
static lzma_ret alone_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator, const lzma_options_lzma *options)
Definition: alone_encoder.c:80
static void alone_encoder_end(void *coder_ptr, const lzma_allocator *allocator)
Definition: alone_encoder.c:69
const lzma_filter * filters
Definition: container.h:315
#define NULL
Definition: cris-opc.c:27
return memset(p, 0, total)
static const char struct stat static buf struct stat static buf static vhangup int options
Definition: sflib.h:145
#define LZMA_DICT_SIZE_MIN
Definition: lzma12.h:218
lzma_ret lzma_lzma_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator, const lzma_filter_info *filters)
Definition: lzma_encoder.c:619
bool lzma_lzma_lclppb_encode(const lzma_options_lzma *options, uint8_t *byte)
Encodes lc/lp/pb into one byte. Returns false on success and true on error.
Definition: lzma_encoder.c:645
unsigned int uint32_t
Definition: sftypes.h:29
#define d(i)
Definition: sha256.c:44
#define UINT32_MAX
lzma_end_function end
Definition: common.h:155
#define LZMA_NEXT_CODER_INIT
Macro to initialize lzma_next_coder structure.
Definition: common.h:180
#define lzma_next_coder_init(func, next, allocator)
Definition: common.h:291
void * lzma_alloc(size_t size, const lzma_allocator *allocator) lzma_attribute((__malloc__)) lzma_attr_alloc_size(1)
Allocates memory.
#define write32le(buf, num)
@ 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

References allocator, alone_encode(), alone_encoder_end(), lzma_next_coder_s::code, lzma_next_coder_s::coder, d, lzma_next_coder_s::end, filters, lzma_alone_coder::header, lzma_alone_coder::header_pos, lzma_alloc(), LZMA_DICT_SIZE_MIN, lzma_lzma_encoder_init(), lzma_lzma_lclppb_encode(), LZMA_MEM_ERROR, LZMA_NEXT_CODER_INIT, lzma_next_coder_init, lzma_next_filter_init(), LZMA_OPTIONS_ERROR, memset(), lzma_alone_coder::next, NULL, options, lzma_alone_coder::sequence, UINT32_MAX, and write32le.

Referenced by LZMA_API().

◆ LZMA_API()

LZMA_API ( lzma_ret  )

Definition at line 153 of file alone_encoder.c.

155 {
157 
160 
161  return LZMA_OK;
162 }
static lzma_stream strm
Definition: full_flush.c:20
bool supported_actions[LZMA_ACTION_MAX+1]
Indicates which lzma_action values are allowed by next.code.
Definition: common.h:220
lzma_internal * internal
Definition: base.h:505
#define lzma_next_strm_init(func, strm,...)
Definition: common.h:303
@ LZMA_FINISH
Finish the coding operation.
Definition: base.h:328
@ LZMA_RUN
Continue coding.
Definition: base.h:251

References alone_encoder_init(), lzma_stream::internal, LZMA_FINISH, lzma_next_strm_init, LZMA_OK, LZMA_RUN, options, strm, and lzma_internal_s::supported_actions.