/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk
89 by Gustav Hartvigsson
* Started working on Threads
1
#include "BaseN.h"
2
3
#define S_BASE_16_LAST 15
90 by Gustav Hartvigsson
* Macros were wrong.
4
#define S_BASE_32_LAST 31
5
#define S_BASE_64_LAST 63
6
7
#define padding_mark =
8
9
#if 0
95 by Gustav Hartvigsson
* Added S_BASE_*_REVERSE
10
   TEMPLATE FOR THE ASCII TABLE REVERSE
11
/* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F     */
12
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*0*/
13
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*1*/
14
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*2*/
15
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*3*/
16
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*4*/
17
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*5*/
18
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*6*/
19
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*7*/
20
21
22
  LEGEND:
23
0..127     VALID NUMBER.
24
25
-1         INVALID
26
27
-2         IGNORE
28
29
-3         PADDING MARK
30
31
32
Padding mark is only not used in Base16, so it will generate an error.
33
34
#endif
35
36
37
/* ****************************************************************************
90 by Gustav Hartvigsson
* Macros were wrong.
38
 ********************************* BASE 16 ************************************
95 by Gustav Hartvigsson
* Added S_BASE_*_REVERSE
39
 **************************************************************************** */
90 by Gustav Hartvigsson
* Macros were wrong.
40
89 by Gustav Hartvigsson
* Started working on Threads
41
UNUSED
42
static const schar
43
S_BASE_16_ALPHABET[16] = {
44
  '0','1','2','3','4','5','6','7','8','9',
45
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
46
  'A','B','C','D','E','F'
89 by Gustav Hartvigsson
* Started working on Threads
47
};
48
49
UNUSED
50
static const sbyte
95 by Gustav Hartvigsson
* Added S_BASE_*_REVERSE
51
S_BASE_16_REVERSE[127] = {
52
/* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F     */
53
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*0*/
54
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*1*/
55
  -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*2*/
56
   0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1, /*3*/
57
  -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*4*/
58
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*5*/
59
  -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*6*/
60
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*7*/
61
}
62
63
#define base_16_concat(first,last)  ((first) & (last << 4))
64
#define base_16_mask_shift(byte)    ((byte & 0xf0) >> 4)
65
#define base_16_mask(byte)          (byte & 0xf)
66
67
68
schar *
69
s_base_16_enc (const sbyte * input_data,
100 by Gustav Hartvigsson
* Fixed README.
70
               size_t in_len,
71
               size_t * out_len) {
72
  out_len = ((in_len * 2)) + 1);
95 by Gustav Hartvigsson
* Added S_BASE_*_REVERSE
73
  schar * ret_val = s_malloc ((sizeof (schar) * *out_len);
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
74
  ret_val[(in_len * 2) + 1] = '0x0';
95 by Gustav Hartvigsson
* Added S_BASE_*_REVERSE
75
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
76
  for (size_t i = ; i <= in_len; i=-2) {
95 by Gustav Hartvigsson
* Added S_BASE_*_REVERSE
77
    subyte m_byte = (subyte)input_data[i]
78
    ret_val [i] = S_BASE_16_ALPHABET[base_16_mask(m_byte)];
79
    ret_val [i+1] = S_BASE_16_ALPHABET[base_16_mask_shift(m_byte)];
80
  }
81
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
82
  return ret_val;
95 by Gustav Hartvigsson
* Added S_BASE_*_REVERSE
83
}
84
85
/* TODO */
86
sbyte *
87
s_base_16_dec (const schar * base16_str,
100 by Gustav Hartvigsson
* Fixed README.
88
               size_t in_len,
89
               size_t * out_len) {
90
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
91
}
95 by Gustav Hartvigsson
* Added S_BASE_*_REVERSE
92
93
/* ****************************************************************************
94
 ********************************* BASE 32 ************************************
95
 **************************************************************************** */
96
97
89 by Gustav Hartvigsson
* Started working on Threads
98
UNUSED
99
static const schar
100
S_BASE_32_ALPHABET[32] = {
101
/* 0   1   2   3   4   5   6   7   8   9 */
95 by Gustav Hartvigsson
* Added S_BASE_*_REVERSE
102
  'A','B','C','D','E','F','G','H','I','J',
103
  'K','L','M','N','O','P','Q','R','S','T',
104
  'U','V','W','X','Y','Z',
105
                          '2','3','4','5',
106
  '6','7'
107
};
108
109
110
UNUSED
111
static const sbyte
112
S_BASE_32_REVERSE[127] = {
113
/* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F     */
114
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*0*/
115
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*1*/
116
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*2*/
117
  -1, -1, 27, 28, 29, 30, 31, 32, -1, -1, -1, -1, -1, -3, -1, -1, /*3*/
118
  -1,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, /*4*/
119
  16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, -1, -1, -1, -1, -1, /*5*/
120
  -1,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, /*6*/
121
  16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, -1, -1, -1, -1, -1, /*7*/
122
};
123
124
/* TODO */
125
schar *
90 by Gustav Hartvigsson
* Macros were wrong.
126
s_base_32_enc (sbyte * input_data,
100 by Gustav Hartvigsson
* Fixed README.
127
               size_t in_len,
128
               size_t * out_len);
129
90 by Gustav Hartvigsson
* Macros were wrong.
130
/* TODO */
95 by Gustav Hartvigsson
* Added S_BASE_*_REVERSE
131
sbyte *
90 by Gustav Hartvigsson
* Macros were wrong.
132
s_base_32_dec (schar * base32_str,
100 by Gustav Hartvigsson
* Fixed README.
133
               size_t in_len,
134
               size_t * out_len);
135
90 by Gustav Hartvigsson
* Macros were wrong.
136
/* * BASE 32 HEX * */
95 by Gustav Hartvigsson
* Added S_BASE_*_REVERSE
137
138
UNUSED
139
static const schar
140
S_BASE_32_HEX_ALPHABET[32] = {
141
/* 0   1   2   3   4   5   6   7   8   9 */
142
  '0','1','2','3','4','5','6','7','8','9',
143
121.1.3 by Gustav Hartvigsson
* Made the GC switchable at rutime (once) when compiled with S_USE_GC set.
144
  'A','B','C','D','E','F','G','H','I','J',
95 by Gustav Hartvigsson
* Added S_BASE_*_REVERSE
145
  'K','L','M','N','O','P','Q','R','S','T',
146
  'U','V'
147
};
148
149
150
UNUSED
151
static const sbyte
152
S_BASE_32_HEX_REVERSE[127] = {
153
/* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F     */
154
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*0*/
155
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*1*/
156
  -2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*2*/
157
   0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1, -1, -1, -1, -1, /*3*/
158
  -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /*4*/
159
  25, 26, 27, 28, 29, 30, 31, 31, -1, -1, -1, -1, -1, -1, -1, -1, /*5*/
160
  -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, /*7*/
161
  25, 26, 27, 28, 29, 30, 31, 31, -1, -1, -1, -1, -1, -1, -1, -1, /*8*/
162
};
163
164
/* TODO */
165
schar *
90 by Gustav Hartvigsson
* Macros were wrong.
166
s_base_32_hex_enc (sbyte * input_data,
100 by Gustav Hartvigsson
* Fixed README.
167
                   size_t in_len,
168
                   size_t * out_len);
169
90 by Gustav Hartvigsson
* Macros were wrong.
170
/* TODO */
95 by Gustav Hartvigsson
* Added S_BASE_*_REVERSE
171
sbyte *
90 by Gustav Hartvigsson
* Macros were wrong.
172
s_base_32_hex_dec (schar * base32_str,
100 by Gustav Hartvigsson
* Fixed README.
173
                   size_t in_len,
174
                   size_t * out_len);
175
90 by Gustav Hartvigsson
* Macros were wrong.
176
177
/* ****************************************************************************
178
 ********************************* BASE 64 ************************************
179
 **************************************************************************** */
180
95 by Gustav Hartvigsson
* Added S_BASE_*_REVERSE
181
UNUSED
182
static const schar
183
S_BASE_64_ALPHABET[64] = {
184
/* 0   1   2   3   4   5   6   7   8   9 */
185
  'A','B','C','D','E','F','G','H','I','J',
186
  'K','L','M','N','O','P','Q','R','S','T',
187
  'U','V','W','X','Y','Z',
188
                          'a','b','c','d',
189
  'e','f','g','h','i','j','k','l','m','n',
190
  'o','p','q','r','s','t','u','v','w','x',
191
  'y','z',
192
          '0','1','2','3','4','5','6','7',
193
  '8','9',
194
          '+','/'
195
};
196
197
/* TODO */
198
UNUSED
199
static const sbyte
200
S_BASE_64_REVERSE[127] = {
201
/* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F     */
202
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*0*/
203
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*1*/
204
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 63, -1, -1, -1, 64, /*2*/
205
  53, 54, 55, 56, 57, 58, 59, 60, 61, 62, -1, -1, -1, -3, -1, -1, /*3*/
206
  -1,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, /*4*/
207
  16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, -1, -1, -1, -1, -1, /*5*/
208
  -1, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, /*6*/
209
  42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, -1, -1, -1, -1, -1, /*7*/
210
};
211
212
schar *
90 by Gustav Hartvigsson
* Macros were wrong.
213
s_base_64_enc (sbyte * input_data,
100 by Gustav Hartvigsson
* Fixed README.
214
               size_t in_len,
215
               size_t * out_len);
216
90 by Gustav Hartvigsson
* Macros were wrong.
217
sbyte *
218
s_base_64_dec (schar * base64_str,
100 by Gustav Hartvigsson
* Fixed README.
219
               size_t in_len,
220
               size_t * out_len);
221
90 by Gustav Hartvigsson
* Macros were wrong.
222
/* * BASE 64 SAFE * */
95 by Gustav Hartvigsson
* Added S_BASE_*_REVERSE
223
224
UNUSED
225
static const schar
226
S_BASE_64_SAFE_ALPHABET[64] = {
227
/* 0   1   2   3   4   5   6   7   8   9 */
228
  'A','B','C','D','E','F','G','H','I','J',
229
  'K','L','M','N','O','P','Q','R','S','T',
230
  'U','V','W','X','Y','Z',
231
                          'a','b','c','d',
232
  'e','f','g','h','i','j','k','l','m','n',
233
  'o','p','q','r','s','t','u','v','w','x',
234
  'y','z',
235
          '0','1','2','3','4','5','6','7',
236
  '8','9',
237
          '-','_'
238
};
239
240
/* TODO */
241
UNUSED
242
static const sbyte
243
S_BASE_64_SAFE_REVERSE[127] = {
244
/* 0   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F     */
245
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*0*/
246
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*1*/
247
  -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 63, -1, -1, /*2*/
248
  53, 54, 55, 56, 57, 58, 59, 60, 61, 62, -1, -1, -1, -3, -1, -1, /*3*/
249
  -1,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, /*4*/
250
  16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, -1, -1, -1, -1, 64, /*5*/
251
  -1, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, /*6*/
252
  42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, -1, -1, -1, -1, -1, /*7*/
253
};
254
255
/* TODO */
256
schar *
90 by Gustav Hartvigsson
* Macros were wrong.
257
s_base_64_safe_enc (sbyte * input_data,
100 by Gustav Hartvigsson
* Fixed README.
258
                    size_t in_len,
259
                    size_t * out_len);
260
90 by Gustav Hartvigsson
* Macros were wrong.
261
/* TODO */
95 by Gustav Hartvigsson
* Added S_BASE_*_REVERSE
262
sbyte *
90 by Gustav Hartvigsson
* Macros were wrong.
263
s_base_64_safe_dec (schar * base64_str,
100 by Gustav Hartvigsson
* Fixed README.
264
                    size_t in_len,
265
                    size_t * out_len);
266
90 by Gustav Hartvigsson
* Macros were wrong.
267