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

Encodes variable-length integers. More...

#include "common.h"

Go to the source code of this file.

Functions

 LZMA_API (lzma_ret)
 

Detailed Description

Encodes variable-length integers.

Definition in file vli_encoder.c.

Function Documentation

◆ LZMA_API()

LZMA_API ( lzma_ret  )

Definition at line 16 of file vli_encoder.c.

20 {
21  // If we haven't been given vli_pos, work in single-call mode.
22  size_t vli_pos_internal = 0;
23  if (vli_pos == NULL) {
24  vli_pos = &vli_pos_internal;
25 
26  // In single-call mode, we expect that the caller has
27  // reserved enough output space.
28  if (*out_pos >= out_size)
29  return LZMA_PROG_ERROR;
30  } else {
31  // This never happens when we are called by liblzma, but
32  // may happen if called directly from an application.
33  if (*out_pos >= out_size)
34  return LZMA_BUF_ERROR;
35  }
36 
37  // Validate the arguments.
38  if (*vli_pos >= LZMA_VLI_BYTES_MAX || vli > LZMA_VLI_MAX)
39  return LZMA_PROG_ERROR;
40 
41  // Shift vli so that the next bits to encode are the lowest. In
42  // single-call mode this never changes vli since *vli_pos is zero.
43  vli >>= *vli_pos * 7;
44 
45  // Write the non-last bytes in a loop.
46  while (vli >= 0x80) {
47  // We don't need *vli_pos during this function call anymore,
48  // but update it here so that it is ready if we need to
49  // return before the whole integer has been decoded.
50  ++*vli_pos;
52 
53  // Write the next byte.
54  out[*out_pos] = (uint8_t)(vli) | 0x80;
55  vli >>= 7;
56 
57  if (++*out_pos == out_size)
58  return vli_pos == &vli_pos_internal
60  }
61 
62  // Write the last byte.
63  out[*out_pos] = (uint8_t)(vli);
64  ++*out_pos;
65  ++*vli_pos;
66 
67  return vli_pos == &vli_pos_internal ? LZMA_OK : LZMA_STREAM_END;
68 
69 }
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
#define NULL
Definition: cris-opc.c:27
assert(limit<=UINT32_MAX/2)
unsigned char uint8_t
Definition: sftypes.h:31
#define LZMA_VLI_BYTES_MAX
Maximum supported encoded length of variable length integers.
Definition: vli.h:44
#define LZMA_VLI_MAX
Maximum supported value of a variable-length integer.
Definition: vli.h:34
size_t * vli_pos
Definition: vli.h:115
@ LZMA_PROG_ERROR
Programming error.
Definition: base.h:218
@ LZMA_STREAM_END
End of stream was reached.
Definition: base.h:63
@ LZMA_BUF_ERROR
No progress is possible.
Definition: base.h:191
@ LZMA_OK
Operation completed successfully.
Definition: base.h:58

References assert(), LZMA_BUF_ERROR, LZMA_OK, LZMA_PROG_ERROR, LZMA_STREAM_END, LZMA_VLI_BYTES_MAX, LZMA_VLI_MAX, NULL, out, out_pos, and vli_pos.