/simpletypesystem/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/simpletypesystem/trunk

« back to all changes in this revision

Viewing changes to src/BaseN.c

  • Committer: Gustav Hartvigsson
  • Date: 2015-10-01 19:22:55 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20151001192255-hfmmqss329o5aqo0
* Added S_BASE_*_REVERSE
* Reorderd elements of file so that the S_BASE_*_ALPHABET and
  S_BASE_*_REVERSE are adjacent to their functions.

Show diffs side-by-side

added added

removed removed

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