Rizin
unix-like reverse engineering framework and cli tools
base85.c File Reference
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <rz_types.h>

Go to the source code of this file.

Functions

static int getc_nospace (FILE *f)
 
static void putc_wrap (char c, int wrap, int *len)
 
static void encode_tuple (unsigned long tuple, int count, int wrap, int *plen, int y_abbr)
 
RZ_API void rz_base85_decode_tuple (unsigned long tuple, int count)
 
RZ_API void rz_base85_encode (FILE *fp, int delims, int wrap, int y_abbr)
 
RZ_API bool rz_base85_decode (FILE *fp, int delims, int ignore_garbage)
 

Function Documentation

◆ encode_tuple()

static void encode_tuple ( unsigned long  tuple,
int  count,
int  wrap,
int plen,
int  y_abbr 
)
static

Definition at line 54 of file base85.c.

54  {
55  int i, lim;
56  char out[5];
57  if (tuple == 0 && count == 4) {
58  putc_wrap('z', wrap, plen);
59  } else if (tuple == 0x20202020 && count == 4 && y_abbr) {
60  putc_wrap('y', wrap, plen);
61  } else {
62  for (i = 0; i < 5; i++) {
63  out[i] = tuple % 85 + '!';
64  tuple /= 85;
65  }
66  lim = 4 - count;
67  for (i = 4; i >= lim; i--) {
68  putc_wrap(out[i], wrap, plen);
69  }
70  }
71 }
lzma_index ** i
Definition: index.h:629
static void putc_wrap(char c, int wrap, int *len)
Definition: base85.c:45
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

References count, i, out, and putc_wrap().

Referenced by rz_base85_encode().

◆ getc_nospace()

static int getc_nospace ( FILE *  f)
static

Definition at line 37 of file base85.c.

37  {
38  int c;
39  while (isspace(c = getc(f))) {
40  ;
41  }
42  return c;
43 }
#define isspace(c)
Definition: safe-ctype.h:141
#define f(i)
Definition: sha256.c:46
#define c(i)
Definition: sha256.c:43

References c, f, and isspace.

Referenced by rz_base85_decode().

◆ putc_wrap()

static void putc_wrap ( char  c,
int  wrap,
int len 
)
static

Definition at line 45 of file base85.c.

45  {
46  if (wrap && *len >= wrap) {
47  putchar('\n');
48  *len = 0;
49  }
50  putchar(c);
51  (*len)++;
52 }
size_t len
Definition: 6502dis.c:15

References c, and len.

Referenced by encode_tuple(), and rz_base85_encode().

◆ rz_base85_decode()

RZ_API bool rz_base85_decode ( FILE *  fp,
int  delims,
int  ignore_garbage 
)

Definition at line 110 of file base85.c.

110  {
111  int c, count = 0, end = 0;
112  unsigned long tuple = 0, pows[] = { 85 * 85 * 85 * 85, 85 * 85 * 85, 85 * 85, 85, 1 };
113  while (delims) {
114  c = getc_nospace(fp);
115  if (c == '<') {
116  c = getc_nospace(fp);
117  if (c == '~') {
118  break;
119  }
120  ungetc(c, fp);
121  } else if (c == EOF) {
122  eprintf("ascii85: missing <~");
123  return false;
124  }
125  }
126  for (;;) {
127  c = getc_nospace(fp);
128  if (c == 'z' && count == 0) {
130  continue;
131  }
132  if (c == 'y' && count == 0) {
133  rz_base85_decode_tuple(0x20202020, 5);
134  continue;
135  }
136  if (c == '~' && delims) {
137  c = getc_nospace(fp);
138  if (c != '>') {
139  eprintf("ascii85: ~ without >\n");
140  return false;
141  }
142  c = EOF;
143  end = 1;
144  }
145  if (c == EOF) {
146  if (delims && !end) {
147  eprintf("ascii85: missing ~>");
148  return false;
149  }
150  if (count > 0) {
151  tuple += pows[count - 1];
153  }
154  break;
155  }
156  if (c < '!' || c > 'u') {
157  if (ignore_garbage) {
158  continue;
159  }
160  eprintf("ascii85: invalid character '%c'\n", c);
161  return false;
162  }
163  tuple += (c - '!') * pows[count++];
164  if (count == 5) {
166  tuple = 0;
167  count = 0;
168  }
169  }
170  return true;
171 }
static int getc_nospace(FILE *f)
Definition: base85.c:37
RZ_API void rz_base85_decode_tuple(unsigned long tuple, int count)
Definition: base85.c:73
#define eprintf(x, y...)
Definition: rlcc.c:7

References c, count, test_evm::end, eprintf, getc_nospace(), and rz_base85_decode_tuple().

◆ rz_base85_decode_tuple()

RZ_API void rz_base85_decode_tuple ( unsigned long  tuple,
int  count 
)

Definition at line 73 of file base85.c.

73  {
74  int i;
75  for (i = 1; i < count; i++) {
76  putchar(tuple >> ((4 - i) * 8));
77  }
78 }

References count, and i.

Referenced by rz_base85_decode().

◆ rz_base85_encode()

RZ_API void rz_base85_encode ( FILE *  fp,
int  delims,
int  wrap,
int  y_abbr 
)

Definition at line 80 of file base85.c.

80  {
81  int c, count = 0, len = 0;
82  unsigned long tuple = 0;
83  if (delims) {
84  putc_wrap('<', wrap, &len);
85  putc_wrap('~', wrap, &len);
86  }
87  for (;;) {
88  c = getc(fp);
89  if (c != EOF) {
90  tuple |= c << ((3 - count++) * 8);
91  if (count < 4) {
92  continue;
93  }
94  } else if (count == 0) {
95  break;
96  }
97  encode_tuple(tuple, count, wrap, &len, y_abbr);
98  if (c == EOF) {
99  break;
100  }
101  tuple = 0;
102  count = 0;
103  }
104  if (delims) {
105  putc_wrap('~', wrap, &len);
106  putc_wrap('>', wrap, &len);
107  }
108 }
static void encode_tuple(unsigned long tuple, int count, int wrap, int *plen, int y_abbr)
Definition: base85.c:54

References c, count, encode_tuple(), len, and putc_wrap().