Rizin
unix-like reverse engineering framework and cli tools
base85.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2011 Remy Oukaour
2 // SPDX-FileCopyrightText: 2017 pancake
3 // SPDX-License-Identifier: MIT
4 
5 /*
6  * ascii85 - Ascii85 encode/decode data and print to standard output
7  *
8  * Copyright (C) 2011 Remy Oukaour
9  * Updated by pancake in 2017
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a copy
12  * of this software and associated documentation files (the "Software"), to deal
13  * in the Software without restriction, including without limitation the rights
14  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
15  * copies of the Software, and to permit persons to whom the Software is
16  * furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included in
19  * all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
24  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
26  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27  * THE SOFTWARE.
28  */
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <errno.h>
34 #include <ctype.h>
35 #include <rz_types.h>
36 
37 static int getc_nospace(FILE *f) {
38  int c;
39  while (isspace(c = getc(f))) {
40  ;
41  }
42  return c;
43 }
44 
45 static void putc_wrap(char c, int wrap, int *len) {
46  if (wrap && *len >= wrap) {
47  putchar('\n');
48  *len = 0;
49  }
50  putchar(c);
51  (*len)++;
52 }
53 
54 static void encode_tuple(unsigned long tuple, int count, int wrap, int *plen, int y_abbr) {
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 }
72 
73 RZ_API void rz_base85_decode_tuple(unsigned long tuple, int count) {
74  int i;
75  for (i = 1; i < count; i++) {
76  putchar(tuple >> ((4 - i) * 8));
77  }
78 }
79 
80 RZ_API void rz_base85_encode(FILE *fp, int delims, int wrap, int y_abbr) {
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 }
109 
110 RZ_API bool rz_base85_decode(FILE *fp, int delims, int ignore_garbage) {
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 }
size_t len
Definition: 6502dis.c:15
lzma_index ** i
Definition: index.h:629
static void encode_tuple(unsigned long tuple, int count, int wrap, int *plen, int y_abbr)
Definition: base85.c:54
static void putc_wrap(char c, int wrap, int *len)
Definition: base85.c:45
RZ_API bool rz_base85_decode(FILE *fp, int delims, int ignore_garbage)
Definition: base85.c:110
static int getc_nospace(FILE *f)
Definition: base85.c:37
RZ_API void rz_base85_encode(FILE *fp, int delims, int wrap, int y_abbr)
Definition: base85.c:80
RZ_API void rz_base85_decode_tuple(unsigned long tuple, int count)
Definition: base85.c:73
const lzma_allocator const uint8_t size_t uint8_t * out
Definition: block.h:528
#define RZ_API
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
string FILE
Definition: benchmark.py:21
#define eprintf(x, y...)
Definition: rlcc.c:7
#define isspace(c)
Definition: safe-ctype.h:141
#define f(i)
Definition: sha256.c:46
#define c(i)
Definition: sha256.c:43