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

Encodes the Index field. More...

#include "index_encoder.h"
#include "index.h"
#include "check.h"

Go to the source code of this file.

Classes

struct  lzma_index_coder
 

Functions

static lzma_ret index_encode (void *coder_ptr, const lzma_allocator *allocator lzma_attribute((__unused__)), const uint8_t *restrict in lzma_attribute((__unused__)), size_t *restrict in_pos lzma_attribute((__unused__)), size_t in_size lzma_attribute((__unused__)), uint8_t *restrict out, size_t *restrict out_pos, size_t out_size, lzma_action action lzma_attribute((__unused__)))
 
static void index_encoder_end (void *coder, const lzma_allocator *allocator)
 
static void index_encoder_reset (lzma_index_coder *coder, const lzma_index *i)
 
lzma_ret lzma_index_encoder_init (lzma_next_coder *next, const lzma_allocator *allocator, const lzma_index *i)
 
 LZMA_API (lzma_ret)
 

Detailed Description

Encodes the Index field.

Definition in file index_encoder.c.

Function Documentation

◆ index_encode()

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

Definition at line 44 of file index_encoder.c.

52 {
53  lzma_index_coder *coder = coder_ptr;
54 
55  // Position where to start calculating CRC32. The idea is that we
56  // need to call lzma_crc32() only once per call to index_encode().
57  const size_t out_start = *out_pos;
58 
59  // Return value to use if we return at the end of this function.
60  // We use "goto out" to jump out of the while-switch construct
61  // instead of returning directly, because that way we don't need
62  // to copypaste the lzma_crc32() call to many places.
63  lzma_ret ret = LZMA_OK;
64 
65  while (*out_pos < out_size)
66  switch (coder->sequence) {
67  case SEQ_INDICATOR:
68  out[*out_pos] = 0x00;
69  ++*out_pos;
70  coder->sequence = SEQ_COUNT;
71  break;
72 
73  case SEQ_COUNT: {
74  const lzma_vli count = lzma_index_block_count(coder->index);
75  ret = lzma_vli_encode(count, &coder->pos,
76  out, out_pos, out_size);
77  if (ret != LZMA_STREAM_END)
78  goto out;
79 
80  ret = LZMA_OK;
81  coder->pos = 0;
82  coder->sequence = SEQ_NEXT;
83  break;
84  }
85 
86  case SEQ_NEXT:
87  if (lzma_index_iter_next(
88  &coder->iter, LZMA_INDEX_ITER_BLOCK)) {
89  // Get the size of the Index Padding field.
90  coder->pos = lzma_index_padding_size(coder->index);
91  assert(coder->pos <= 3);
92  coder->sequence = SEQ_PADDING;
93  break;
94  }
95 
96  coder->sequence = SEQ_UNPADDED;
97 
98  // Fall through
99 
100  case SEQ_UNPADDED:
101  case SEQ_UNCOMPRESSED: {
102  const lzma_vli size = coder->sequence == SEQ_UNPADDED
103  ? coder->iter.block.unpadded_size
104  : coder->iter.block.uncompressed_size;
105 
106  ret = lzma_vli_encode(size, &coder->pos,
107  out, out_pos, out_size);
108  if (ret != LZMA_STREAM_END)
109  goto out;
110 
111  ret = LZMA_OK;
112  coder->pos = 0;
113 
114  // Advance to SEQ_UNCOMPRESSED or SEQ_NEXT.
115  ++coder->sequence;
116  break;
117  }
118 
119  case SEQ_PADDING:
120  if (coder->pos > 0) {
121  --coder->pos;
122  out[(*out_pos)++] = 0x00;
123  break;
124  }
125 
126  // Finish the CRC32 calculation.
127  coder->crc32 = lzma_crc32(out + out_start,
128  *out_pos - out_start, coder->crc32);
129 
130  coder->sequence = SEQ_CRC32;
131 
132  // Fall through
133 
134  case SEQ_CRC32:
135  // We don't use the main loop, because we don't want
136  // coder->crc32 to be touched anymore.
137  do {
138  if (*out_pos == out_size)
139  return LZMA_OK;
140 
141  out[*out_pos] = (coder->crc32 >> (coder->pos * 8))
142  & 0xFF;
143  ++*out_pos;
144 
145  } while (++coder->pos < 4);
146 
147  return LZMA_STREAM_END;
148 
149  default:
150  assert(0);
151  return LZMA_PROG_ERROR;
152  }
153 
154 out:
155  // Update the CRC32.
156  coder->crc32 = lzma_crc32(out + out_start,
157  *out_pos - out_start, coder->crc32);
158 
159  return ret;
160 }
@ LZMA_INDEX_ITER_BLOCK
Get the next Block.
Definition: index.h:249
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 uint8_t * out
Definition: block.h:528
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void count
Definition: sflib.h:98
uint32_t lzma_index_padding_size(const lzma_index *i)
Definition: index.c:593
voidpf void uLong size
Definition: ioapi.h:138
assert(limit<=UINT32_MAX/2)
lzma_index_iter iter
Iterator for the Index being encoded.
Definition: index_encoder.c:33
lzma_index * index
Target Index.
Definition: index_decoder.c:33
enum lzma_index_coder::@649 sequence
size_t pos
Position in integers.
Definition: index_decoder.c:49
uint32_t crc32
CRC32 of the List of Records field.
Definition: index_decoder.c:52
struct lzma_index_iter::@636 block
lzma_vli unpadded_size
Unpadded size of this Block.
Definition: index.h:188
lzma_vli uncompressed_size
Uncompressed size of this Stream.
Definition: index.h:99
uint64_t lzma_vli
Variable-length integer type.
Definition: vli.h:63
lzma_ret
Return values used by several functions in liblzma.
Definition: base.h:57
@ LZMA_PROG_ERROR
Programming error.
Definition: base.h:218
@ LZMA_STREAM_END
End of stream was reached.
Definition: base.h:63
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58

References assert(), lzma_index_iter::block, count, lzma_index_coder::crc32, lzma_index_coder::index, lzma_index_coder::iter, LZMA_INDEX_ITER_BLOCK, lzma_index_padding_size(), LZMA_OK, LZMA_PROG_ERROR, LZMA_STREAM_END, out, out_pos, lzma_index_coder::pos, lzma_index_coder::sequence, lzma_index_iter::uncompressed_size, and lzma_index_iter::unpadded_size.

Referenced by lzma_index_encoder_init().

◆ index_encoder_end()

static void index_encoder_end ( void *  coder,
const lzma_allocator allocator 
)
static

Definition at line 164 of file index_encoder.c.

165 {
166  lzma_free(coder, allocator);
167  return;
168 }
const lzma_allocator * allocator
Definition: block.h:377
void lzma_free(void *ptr, const lzma_allocator *allocator)
Frees memory.
Definition: common.c:78

References allocator, and lzma_free().

Referenced by lzma_index_encoder_init().

◆ index_encoder_reset()

static void index_encoder_reset ( lzma_index_coder coder,
const lzma_index i 
)
static

Definition at line 172 of file index_encoder.c.

173 {
174  lzma_index_iter_init(&coder->iter, i);
175 
176  coder->sequence = SEQ_INDICATOR;
177  coder->index = i;
178  coder->pos = 0;
179  coder->crc32 = 0;
180 
181  return;
182 }
lzma_index ** i
Definition: index.h:629

References lzma_index_coder::crc32, i, lzma_index_coder::index, lzma_index_coder::iter, lzma_index_coder::pos, and lzma_index_coder::sequence.

Referenced by lzma_index_encoder_init().

◆ LZMA_API()

LZMA_API ( lzma_ret  )

Definition at line 209 of file index_encoder.c.

211 {
213 
216 
217  return LZMA_OK;
218 }
static lzma_stream strm
Definition: full_flush.c:20
lzma_ret lzma_index_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator, const lzma_index *i)
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 i, lzma_stream::internal, LZMA_FINISH, lzma_index_encoder_init(), lzma_next_strm_init, LZMA_OK, LZMA_RUN, strm, and lzma_internal_s::supported_actions.

◆ lzma_index_encoder_init()

lzma_ret lzma_index_encoder_init ( lzma_next_coder next,
const lzma_allocator allocator,
const lzma_index i 
)

Definition at line 186 of file index_encoder.c.

188 {
190 
191  if (i == NULL)
192  return LZMA_PROG_ERROR;
193 
194  if (next->coder == NULL) {
195  next->coder = lzma_alloc(sizeof(lzma_index_coder), allocator);
196  if (next->coder == NULL)
197  return LZMA_MEM_ERROR;
198 
199  next->code = &index_encode;
200  next->end = &index_encoder_end;
201  }
202 
203  index_encoder_reset(next->coder, i);
204 
205  return LZMA_OK;
206 }
#define NULL
Definition: cris-opc.c:27
static lzma_ret index_encode(void *coder_ptr, const lzma_allocator *allocator lzma_attribute((__unused__)), const uint8_t *restrict in lzma_attribute((__unused__)), size_t *restrict in_pos lzma_attribute((__unused__)), size_t in_size lzma_attribute((__unused__)), uint8_t *restrict out, size_t *restrict out_pos, size_t out_size, lzma_action action lzma_attribute((__unused__)))
Definition: index_encoder.c:44
static void index_encoder_reset(lzma_index_coder *coder, const lzma_index *i)
static void index_encoder_end(void *coder, const lzma_allocator *allocator)
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_end_function end
Definition: common.h:155
#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.
@ LZMA_MEM_ERROR
Cannot allocate memory.
Definition: base.h:128

References allocator, lzma_next_coder_s::code, lzma_next_coder_s::coder, lzma_next_coder_s::end, i, index_encode(), index_encoder_end(), index_encoder_reset(), lzma_alloc(), LZMA_MEM_ERROR, lzma_next_coder_init, LZMA_OK, LZMA_PROG_ERROR, and NULL.

Referenced by LZMA_API(), stream_encode(), and stream_encode_mt().