-
Notifications
You must be signed in to change notification settings - Fork 2
/
baseutils.c
203 lines (180 loc) · 6.49 KB
/
baseutils.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdbool.h>
#include "baseutils.h"
// const char _94[] = {
// ' ', '!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/',
// '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
// ':', ';', '<', '=', '>', '?', '@',
// 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'X', 'Y', 'Z',
// '[', '\\', ']', '^', '_', '`',
// 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'x', 'y', 'z',
// '{', '|', '}', '~'
// };
const char _62[] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
};
uint64_t uconvb36tob10 (const char* str) {
uint64_t dec = 0;
uint8_t len = strlen(str);
while (*str) {
((*str - 0) > 64) ?
(dec += (9 + ((*str++ - 0) - 64)) * pow(36, --len)) :
(dec += ((*str++ - 0) - 48) * pow(36, --len));
}
return dec;
}
int64_t convb36tob10 (const char* str) {
int64_t dec = 0;
uint8_t len = strlen(str);
bool neg;
(*str++ == '-') ? (neg = true, len--) : (neg = false);
while (*str) {
((*str - 0) > 64) ?
(dec += (9 + ((*str++ - 0) - 64)) * pow(36, --len)) :
(dec += ((*str++ - 0) - 48) * pow(36, --len));
}
return (neg) ? (dec - dec - dec) : (dec);
}
uint64_t uconvb62tob10 (const char* str) {
uint64_t dec = 0;
uint8_t len = strlen(str);
while (*str) {
((*str - 0) > 96) ?
(dec += (9 + ((*str++ - 0) - (96 - 26))) * pow(62, --len)) :
( ((*str - 0) > 64) ? (dec += (9 + ((*str++ - 0) - 64)) * pow(62, --len)) : (dec += ((*str++ - 0) - 48) * pow(62, --len)) );
}
return dec;
}
int64_t convb62tob10 (const char* str) {
int64_t dec = 0;
uint8_t len = strlen(str);
bool neg;
(*str++ == '-') ? (neg = true, len--) : (neg = false);
while (*str) {
((*str - 0) > 96) ?
(dec += (9 + ((*str++ - 0) - (96 - 26))) * pow(62, --len)) :
( ((*str - 0) > 64) ? (dec += (9 + ((*str++ - 0) - 64)) * pow(62, --len)) : (dec += ((*str++ - 0) - 48) * pow(62, --len)) );
}
return (neg) ? (dec - dec - dec) : (dec);
}
uint64_t uatoi (const char* str) {
uint64_t val = 0;
while (*str) {
val = val * 10 + (*str++ - '0');
}
return val;
}
static inline uint_fast16_t locatevalue_ (const char* charset_, uint8_t char_) {
uint_fast16_t ind_ ;
while ( *charset_ ) {
if (*charset_ == char_) {
return ind_;
} else {
*charset_++;
ind_++;
};
}
return -1;
}
char* basetobase (const char* str, uint8_t a, uint8_t b) {
char* arr = malloc(sizeof(char) * 50); // 50 to ensure a secure margin in case of huge representations
char* tmp = arr;
uint64_t aux = 0;
uint8_t len = strlen(str);
uint8_t lvl = 0;
bool neg;
(*str == '-') ? (neg = true, *str++) : (neg = false);
// Before enconding to the new requested base, the numeric representation is pre-encoded in decimal
while ( *str ) {
((*str - 0) > 96) ?
(aux += (9 + ((*str++ - 0) - (96 - 26))) * pow(a, --len)) :
( ((*str - 0) > 64) ? (aux += (9 + ((*str++ - 0) - 64)) * pow(a, --len)) : (aux += ((*str++ - 0) - 48) * pow(a, --len)) );
}
// Successive divisions to find the new numeric representation
while (aux >= b) {
++lvl;
// *arr++ = aux % b;
// C = A – B * (A / B)
*arr++ = aux - b * (aux / b);
aux = aux / b;
}
*arr = aux;
lvl++;
// Creates a new specialized array where the new representation will be stored and checks if the number will be negative
// The +1 is for adding the '\0' character at the end of the function to avoid trash characters
char* rep = malloc(sizeof(char) * (lvl + 1));
char* res = rep;
if (neg) {
(*rep++ = '-');
lvl++;
}
// Reverses the array, replacing each decimal value by the corresponding value given by a "charset lookup table"
while (lvl > 0) {
*rep++ = _62[tmp[--lvl]];
}
*rep = '\0';
// Cleans the space used to store the support variable array
if (tmp != NULL) {
free(tmp);
tmp = NULL;
}
return res;
}
char* basetobasecustom (const char* str, uint8_t a, uint8_t b, const char* charset) {
char* arr = malloc(sizeof(char) * 50); // 50 to ensure a secure margin in case of huge representations
char* tmp = arr;
uint64_t aux = 0;
uint8_t len = strlen(str);
uint8_t lvl = 0;
bool neg;
(*str == '-') ? (neg = true, *str++) : (neg = false);
// Before enconding to the new requested base, the numeric representation is pre-encoded in decimal
uint_fast16_t ind;
while ( *str ) {
ind = locatevalue_(charset, *str++);
if (ind > -1) {
aux += ind * pow(a, --len);
*str++;
} else {
if (arr != NULL) {
free(arr);
arr = NULL;
}
return arr;
}
}
// Successive divisions to find the new numeric representation
while (aux >= b) {
++lvl;
// *arr++ = aux % b;
// C = A – B * (A / B)
*arr++ = aux - b * (aux / b);
aux = aux / b;
}
*arr = aux;
lvl++;
// Creates a new specialized array where the new representation will be stored and checks if the number will be negative
// The +1 is for adding the '\0' character at the end of the function to avoid trash characters
char* rep = malloc(sizeof(char) * (lvl + 1));
char* res = rep;
if (neg) {
(*rep++ = '-');
lvl++;
}
// Reverses the array, replacing each decimal value by the corresponding value given by a "charset lookup table"
while (lvl > 0) {
*rep++ = charset[tmp[--lvl]];
}
*rep = '\0';
// Cleans the space used to store the support variable array
if (tmp != NULL) {
free(tmp);
tmp = NULL;
}
return res;
}