Rizin
unix-like reverse engineering framework and cli tools
ubase64.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <rz_util.h>

Go to the source code of this file.

Macros

#define SZ   1024
 

Functions

static void local_b64_encode (const ut8 in[3], char out[4], int len)
 
static int local_b64_decode (const char in[4], ut8 out[3])
 
RZ_API int rz_base64_decode (ut8 *bout, const char *bin, int len)
 
RZ_API ut8rz_base64_decode_dyn (const char *in, int len)
 
RZ_API size_t rz_base64_encode (char *bout, const ut8 *bin, size_t sz)
 
RZ_API char * rz_base64_encode_dyn (const ut8 *bin, size_t sz)
 

Variables

static const char cb64 [] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
 
static const char cd64 [] = "|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq"
 

Macro Definition Documentation

◆ SZ

#define SZ   1024

Definition at line 14 of file ubase64.c.

Function Documentation

◆ local_b64_decode()

static int local_b64_decode ( const char  in[4],
ut8  out[3] 
)
static

Definition at line 28 of file ubase64.c.

28  {
29  int len = 3;
30  ut8 i, v[4] = { 0 };
31  for (i = 0; i < 4; i++) {
32  if (in[i] < 43 || in[i] > 122) {
33  return -1;
34  }
35  v[i] = cd64[in[i] - 43];
36  if (v[i] == '$') {
37  len = i ? i - 1 : -1;
38  break;
39  }
40  v[i] -= 62;
41  }
42  out[0] = v[0] << 2 | v[1] >> 4;
43  out[1] = v[1] << 4 | v[2] >> 2;
44  out[2] = ((v[2] << 6) & 0xc0) | v[3];
45  return len;
46 }
size_t len
Definition: 6502dis.c:15
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
const char * v
Definition: dsignal.c:12
uint8_t ut8
Definition: lh5801.h:11
static const char cd64[]
Definition: ubase64.c:16

References cd64, i, in, len, out, and v.

Referenced by rz_base64_decode().

◆ local_b64_encode()

static void local_b64_encode ( const ut8  in[3],
char  out[4],
int  len 
)
static

Definition at line 18 of file ubase64.c.

18  {
19  if (len < 1) {
20  return;
21  }
22  out[0] = cb64[in[0] >> 2];
23  out[1] = cb64[((in[0] & 0x03) << 4) | ((len > 1) ? ((in[1] & 0xf0) >> 4) : 0)];
24  out[2] = (len > 1 ? cb64[((in[1] & 0x0f) << 2) | (len > 2 ? ((in[2] & 0xc0) >> 6) : 0)] : '=');
25  out[3] = (len > 2 ? cb64[in[2] & 0x3f] : '=');
26 }
static const char cb64[]
Definition: ubase64.c:15

References cb64, in, len, and out.

Referenced by rz_base64_encode().

◆ rz_base64_decode()

RZ_API int rz_base64_decode ( ut8 bout,
const char *  bin,
int  len 
)

Definition at line 48 of file ubase64.c.

48  {
49  int in, out;
50  if (len < 0) {
51  len = strlen(bin);
52  }
53  for (in = out = 0; in + 3 < len; in += 4) {
54  int ret = local_b64_decode(bin + in, bout + out);
55  if (ret < 1) {
56  return -1;
57  }
58  out += ret;
59  }
60  bout[out] = 0;
61  /* XXX this makes no sense, just return out? */
62  return (in != out) ? out : -1;
63 }
Definition: malloc.c:26
static int local_b64_decode(const char in[4], ut8 out[3])
Definition: ubase64.c:28

References in, len, local_b64_decode(), and out.

Referenced by block_load_cb(), function_load_cb(), rax(), rz_base64_decode_dyn(), rz_cf_value_data_new(), rz_cmd_help(), rz_core_write_base64d_at(), rz_hex_from_js(), rz_socket_http_accept(), sdb_decode_raw(), and update().

◆ rz_base64_decode_dyn()

RZ_API ut8* rz_base64_decode_dyn ( const char *  in,
int  len 
)

Definition at line 65 of file ubase64.c.

65  {
66  ut8 *bout;
67  if (!in) {
68  return NULL;
69  }
70  if (len < 0) {
71  len = strlen(in) + 1;
72  }
73  bout = calloc(4, len + 1);
74  if (rz_base64_decode(bout, in, len) == -1) {
75  free(bout);
76  return NULL;
77  }
78  return bout;
79 }
#define NULL
Definition: cris-opc.c:27
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
void * calloc(size_t number, size_t size)
Definition: malloc.c:102
RZ_API int rz_base64_decode(ut8 *bout, const char *bin, int len)
Definition: ubase64.c:48

References calloc(), free(), in, len, NULL, and rz_base64_decode().

Referenced by flag_set_comment(), rz_bin_string_decode_base64(), rz_cmd_alias_set(), rz_cmd_base64_decode_handler(), rz_core_agraph_add_node(), and strings_print().

◆ rz_base64_encode()

RZ_API size_t rz_base64_encode ( char *  bout,
const ut8 bin,
size_t  sz 
)

Definition at line 81 of file ubase64.c.

81  {
83  size_t in, out;
84  for (in = out = 0; in < sz; in += 3, out += 4) {
85  local_b64_encode(bin + in, (char *)bout + out,
86  (sz - in) > 3 ? 3 : sz - in);
87  }
88  bout[out] = 0;
89  return out;
90 }
#define rz_return_val_if_fail(expr, val)
Definition: rz_assert.h:108
static void local_b64_encode(const ut8 in[3], char out[4], int len)
Definition: ubase64.c:18

References in, local_b64_encode(), out, and rz_return_val_if_fail.

Referenced by lang_byte_array_nodejs(), pcache_save_cb(), rax(), rz_base64_encode_dyn(), rz_cmd_help(), rz_core_write_base64_at(), sdb_encode_raw(), and update().

◆ rz_base64_encode_dyn()

RZ_API char* rz_base64_encode_dyn ( const ut8 bin,
size_t  sz 
)

Definition at line 92 of file ubase64.c.

92  {
94  if (sz > (SIZE_MAX - 2) / 4) {
95  return NULL;
96  }
97  const size_t osz = (sz * 4) + 2;
98  char *bout = malloc(osz);
99  if (!bout) {
100  return NULL;
101  }
102  rz_base64_encode(bout, bin, sz);
103  return bout;
104 }
void * malloc(size_t size)
Definition: malloc.c:123
#define SIZE_MAX
RZ_API size_t rz_base64_encode(char *bout, const ut8 *bin, size_t sz)
Definition: ubase64.c:81

References malloc(), NULL, rz_base64_encode(), rz_return_val_if_fail, and SIZE_MAX.

Referenced by agraph_print_node(), block_store(), core_analysis_graph_construct_nodes(), foreach_offset_annotation(), function_store(), print_flag_rizin(), print_graph_agg(), rz_cmd_alias(), rz_cmd_base64_encode_handler(), rz_core_meta_print(), and rz_core_print_bb_custom().

Variable Documentation

◆ cb64

const char cb64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
static

Definition at line 15 of file ubase64.c.

Referenced by local_b64_encode().

◆ cd64

const char cd64[] = "|$$$}rstuvwxyz{$$$$$$$>?@ABCDEFGHIJKLMNOPQRSTUVW$$$$$$XYZ[\\]^_`abcdefghijklmnopq"
static

Definition at line 16 of file ubase64.c.

Referenced by local_b64_decode().