-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathprinter.c
More file actions
215 lines (194 loc) · 5.89 KB
/
printer.c
File metadata and controls
215 lines (194 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <string.h>
#include <limits.h>
#include "os_error.h"
#include "rfc3339.h"
#include "sol/printer.h"
#include "util.h"
// max amount is max uint64 scaled down: "18446744073.709551615"
#define AMOUNT_MAX_SIZE 22
int print_token_amount(uint64_t amount,
const char *const asset,
uint8_t decimals,
char *out,
const size_t out_length) {
BAIL_IF(out_length > INT_MAX);
uint64_t dVal = amount;
const int outlen = (int) out_length;
int i = 0;
int min_chars = decimals + 1;
if (i < (outlen - 1)) {
do {
if (i == decimals) {
out[i] = '.';
i += 1;
}
out[i] = (dVal % 10) + '0';
dVal /= 10;
i += 1;
} while ((dVal > 0 || i < min_chars) && i < outlen);
}
BAIL_IF(i >= outlen);
// Reverse order
int j, k;
for (j = 0, k = i - 1; j < k; j++, k--) {
char tmp = out[j];
out[j] = out[k];
out[k] = tmp;
}
// Strip trailing 0s
for (i -= 1; i > 0; i--) {
if (out[i] != '0') break;
}
i += 1;
// Strip trailing .
if (out[i - 1] == '.') i -= 1;
if (asset) {
const int asset_length = strlen(asset);
// Check buffer has space
BAIL_IF((i + 1 + asset_length + 1) > outlen);
// Qualify amount
out[i++] = ' ';
strncpy(out + i, asset, asset_length + 1);
} else {
out[i] = '\0';
}
return 0;
}
#define SOL_DECIMALS 9
int print_amount(uint64_t amount, char *out, size_t out_length) {
return print_token_amount(amount, "SOL", SOL_DECIMALS, out, out_length);
}
int print_sized_string(const SizedString *string, char *out, size_t out_length) {
size_t len = MIN(out_length, string->length);
strncpy(out, string->string, len);
if (string->length < out_length) {
out[string->length] = '\0';
return 0;
} else {
out[--out_length] = '\0';
if (out_length != 0) {
/* signal truncation */
out[out_length - 1] = '~';
}
return 1;
}
}
int print_string(const char *in, char *out, size_t out_length) {
strncpy(out, in, out_length);
int rc = (out[--out_length] != '\0');
if (rc) {
/* ensure the output is NUL terminated */
out[out_length] = '\0';
if (out_length != 0) {
/* signal truncation */
out[out_length - 1] = '~';
}
}
return rc;
}
int print_summary(const char *in,
char *out,
size_t out_length,
size_t left_length,
size_t right_length) {
BAIL_IF(out_length <= (left_length + right_length + 2));
size_t in_length = strlen(in);
if ((in_length + 1) > out_length) {
memcpy(out, in, left_length);
out[left_length] = '.';
out[left_length + 1] = '.';
memcpy(out + left_length + 2, in + in_length - right_length, right_length);
out[left_length + right_length + 2] = '\0';
} else {
print_string(in, out, out_length);
}
return 0;
}
static const char BASE58_ALPHABET[] = {'1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C',
'D', 'E', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q',
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c',
'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'm', 'n', 'o', 'p',
'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
int encode_base58(const void *in, size_t length, char *out, size_t maxoutlen) {
uint8_t tmp[64];
uint8_t buffer[64];
uint8_t j;
size_t start_at;
size_t zero_count = 0;
if (length > sizeof(tmp)) {
return INVALID_PARAMETER;
}
memmove(tmp, in, length);
while ((zero_count < length) && (tmp[zero_count] == 0)) {
++zero_count;
}
j = 2 * length;
start_at = zero_count;
while (start_at < length) {
uint16_t remainder = 0;
size_t div_loop;
for (div_loop = start_at; div_loop < length; div_loop++) {
uint16_t digit256 = (uint16_t) (tmp[div_loop] & 0xff);
uint16_t tmp_div = remainder * 256 + digit256;
tmp[div_loop] = (uint8_t) (tmp_div / 58);
remainder = (tmp_div % 58);
}
if (tmp[start_at] == 0) {
++start_at;
}
buffer[--j] = (uint8_t) BASE58_ALPHABET[remainder];
}
while ((j < (2 * length)) && (buffer[j] == BASE58_ALPHABET[0])) {
++j;
}
while (zero_count-- > 0) {
buffer[--j] = BASE58_ALPHABET[0];
}
length = 2 * length - j;
if (maxoutlen < length + 1) {
return EXCEPTION_OVERFLOW;
}
memmove(out, (buffer + j), length);
out[length] = '\0';
return 0;
}
int print_i64(int64_t i64, char *out, size_t out_length) {
BAIL_IF(out_length < 1);
uint64_t u64 = (uint64_t) i64;
if (i64 < 0) {
out[0] = '-';
out++;
out_length--;
u64 = (u64 ^ 0xffffffffffffffff) + 1;
}
return print_u64(u64, out, out_length);
}
int print_u64(uint64_t u64, char *out, size_t out_length) {
BAIL_IF(out_length > INT_MAX);
uint64_t dVal = u64;
int outlen = (int) out_length;
int i = 0;
int j = 0;
if (i < (outlen - 1)) {
do {
if (dVal > 0) {
out[i] = (dVal % 10) + '0';
dVal /= 10;
} else {
out[i] = '0';
}
i++;
} while (dVal > 0 && i < outlen);
}
BAIL_IF(i >= outlen);
out[i--] = '\0';
for (; j < i; j++, i--) {
int tmp = out[j];
out[j] = out[i];
out[i] = tmp;
}
return 0;
}
int print_timestamp(int64_t timestamp, char *out, size_t out_length) {
return rfc3339_format(out, out_length, timestamp);
}