krypt-core 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +20 -0
- data/ext/krypt/core/Makefile +221 -0
- data/ext/krypt/core/binyo-error.h +40 -0
- data/ext/krypt/core/binyo-io-buffer.h +54 -0
- data/ext/krypt/core/binyo-io.h +131 -0
- data/ext/krypt/core/extconf.h +8 -0
- data/ext/krypt/core/extconf.rb +80 -0
- data/ext/krypt/core/krypt-core.c +110 -0
- data/ext/krypt/core/krypt-core.h +97 -0
- data/ext/krypt/core/krypt-core.o +0 -0
- data/ext/krypt/core/krypt-provider.h +86 -0
- data/ext/krypt/core/krypt_asn1-internal.c +681 -0
- data/ext/krypt/core/krypt_asn1-internal.h +117 -0
- data/ext/krypt/core/krypt_asn1-internal.o +0 -0
- data/ext/krypt/core/krypt_asn1.c +2109 -0
- data/ext/krypt/core/krypt_asn1.h +88 -0
- data/ext/krypt/core/krypt_asn1.o +0 -0
- data/ext/krypt/core/krypt_asn1_codec.c +973 -0
- data/ext/krypt/core/krypt_asn1_codec.o +0 -0
- data/ext/krypt/core/krypt_asn1_in_adapter.c +178 -0
- data/ext/krypt/core/krypt_asn1_in_adapter.o +0 -0
- data/ext/krypt/core/krypt_asn1_in_chunked.c +292 -0
- data/ext/krypt/core/krypt_asn1_in_chunked.o +0 -0
- data/ext/krypt/core/krypt_asn1_in_definite.c +156 -0
- data/ext/krypt/core/krypt_asn1_in_definite.o +0 -0
- data/ext/krypt/core/krypt_asn1_parser.c +592 -0
- data/ext/krypt/core/krypt_asn1_parser.o +0 -0
- data/ext/krypt/core/krypt_asn1_template-internal.h +185 -0
- data/ext/krypt/core/krypt_asn1_template.c +459 -0
- data/ext/krypt/core/krypt_asn1_template.h +56 -0
- data/ext/krypt/core/krypt_asn1_template.o +0 -0
- data/ext/krypt/core/krypt_asn1_template_encoder.c +76 -0
- data/ext/krypt/core/krypt_asn1_template_encoder.o +0 -0
- data/ext/krypt/core/krypt_asn1_template_parser.c +1176 -0
- data/ext/krypt/core/krypt_asn1_template_parser.o +0 -0
- data/ext/krypt/core/krypt_b64-internal.h +38 -0
- data/ext/krypt/core/krypt_b64.c +391 -0
- data/ext/krypt/core/krypt_b64.h +41 -0
- data/ext/krypt/core/krypt_b64.o +0 -0
- data/ext/krypt/core/krypt_digest.c +391 -0
- data/ext/krypt/core/krypt_digest.h +51 -0
- data/ext/krypt/core/krypt_digest.o +0 -0
- data/ext/krypt/core/krypt_error.c +221 -0
- data/ext/krypt/core/krypt_error.h +46 -0
- data/ext/krypt/core/krypt_error.o +0 -0
- data/ext/krypt/core/krypt_hex-internal.h +36 -0
- data/ext/krypt/core/krypt_hex.c +255 -0
- data/ext/krypt/core/krypt_hex.h +41 -0
- data/ext/krypt/core/krypt_hex.o +0 -0
- data/ext/krypt/core/krypt_io.c +65 -0
- data/ext/krypt/core/krypt_io.h +56 -0
- data/ext/krypt/core/krypt_io.o +0 -0
- data/ext/krypt/core/krypt_io_in_pem.c +397 -0
- data/ext/krypt/core/krypt_io_in_pem.o +0 -0
- data/ext/krypt/core/krypt_missing.c +238 -0
- data/ext/krypt/core/krypt_missing.h +62 -0
- data/ext/krypt/core/krypt_missing.o +0 -0
- data/ext/krypt/core/krypt_pem.c +171 -0
- data/ext/krypt/core/krypt_pem.o +0 -0
- data/ext/krypt/core/krypt_provider-internal.h +40 -0
- data/ext/krypt/core/krypt_provider.c +136 -0
- data/ext/krypt/core/krypt_provider.o +0 -0
- data/ext/krypt/core/kryptcore.so +0 -0
- data/ext/krypt/core/mkmf.log +130 -0
- data/lib/krypt-core/version.rb +3 -0
- data/lib/krypt-core.rb +35 -0
- data/lib/kryptcore.so +0 -0
- data/spec/README +2 -0
- data/test/README +2 -0
- data/test/res/certificate.cer +0 -0
- data/test/resources.rb +48 -0
- data/test/scratch.rb +17 -0
- metadata +150 -0
Binary file
|
@@ -0,0 +1,38 @@
|
|
1
|
+
/*
|
2
|
+
* krypt-core API - C implementation
|
3
|
+
*
|
4
|
+
* Copyright (c) 2011-2013
|
5
|
+
* Hiroshi Nakamura <nahi@ruby-lang.org>
|
6
|
+
* Martin Bosslet <martin.bosslet@gmail.com>
|
7
|
+
* All rights reserved.
|
8
|
+
*
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
10
|
+
* a copy of this software and associated documentation files (the
|
11
|
+
* "Software"), to deal in the Software without restriction, including
|
12
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
13
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
14
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
15
|
+
* the following conditions:
|
16
|
+
*
|
17
|
+
* The above copyright notice and this permission notice shall be
|
18
|
+
* included in all copies or substantial portions of the Software.
|
19
|
+
*
|
20
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
21
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
22
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
23
|
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
24
|
+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
25
|
+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
26
|
+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
27
|
+
*/
|
28
|
+
|
29
|
+
#ifndef _KRYPT_B64_INTERNAL_H
|
30
|
+
#define _KRYPT_B64_INTERNAL_H_
|
31
|
+
|
32
|
+
int krypt_base64_encode(uint8_t *bytes, size_t len, int cols, uint8_t **out, size_t *outlen);
|
33
|
+
int krypt_base64_buffer_encode_to(binyo_outstream *out, uint8_t *bytes, size_t off, size_t len, int cols);
|
34
|
+
int krypt_base64_decode(uint8_t *bytes, size_t len, uint8_t **out, size_t *outlen);
|
35
|
+
int krypt_base64_buffer_decode_to(binyo_outstream *out, uint8_t *bytes, size_t off, size_t len);
|
36
|
+
|
37
|
+
#endif /* _KRYPT_B64_INTERNAL_H_ */
|
38
|
+
|
@@ -0,0 +1,391 @@
|
|
1
|
+
/*
|
2
|
+
* krypt-core API - C implementation
|
3
|
+
*
|
4
|
+
* Copyright (c) 2011-2013
|
5
|
+
* Hiroshi Nakamura <nahi@ruby-lang.org>
|
6
|
+
* Martin Bosslet <martin.bosslet@gmail.com>
|
7
|
+
* All rights reserved.
|
8
|
+
*
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
10
|
+
* a copy of this software and associated documentation files (the
|
11
|
+
* "Software"), to deal in the Software without restriction, including
|
12
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
13
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
14
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
15
|
+
* the following conditions:
|
16
|
+
*
|
17
|
+
* The above copyright notice and this permission notice shall be
|
18
|
+
* included in all copies or substantial portions of the Software.
|
19
|
+
*
|
20
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
21
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
22
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
23
|
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
24
|
+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
25
|
+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
26
|
+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
27
|
+
*/
|
28
|
+
|
29
|
+
#include "krypt-core.h"
|
30
|
+
|
31
|
+
VALUE mKryptBase64;
|
32
|
+
VALUE cKryptBase64Encoder;
|
33
|
+
VALUE cKryptBase64Decoder;
|
34
|
+
VALUE eKryptBase64Error;
|
35
|
+
|
36
|
+
static const char krypt_b64_table[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
37
|
+
static const char krypt_b64_table_inv[] = {
|
38
|
+
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
39
|
+
-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
|
40
|
+
62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,-1,
|
41
|
+
0,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,
|
42
|
+
-1,-1,-1,-1,-1,-1,
|
43
|
+
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};
|
44
|
+
static uint8_t krypt_b64_separator[] = { '\r', '\n' };
|
45
|
+
|
46
|
+
static uint8_t krypt_b64_out_buf[4];
|
47
|
+
static uint8_t krypt_b64_in_buf[3];
|
48
|
+
|
49
|
+
#define KRYPT_BASE64_INV_MAX 123
|
50
|
+
#define KRYPT_BASE64_DECODE 0
|
51
|
+
#define KRYPT_BASE64_ENCODE 1
|
52
|
+
|
53
|
+
#define int_compute_int(res, b, i) \
|
54
|
+
do { \
|
55
|
+
(res) = ((b)[(i)] << 16) | ((b)[(i)+1] << 8) | ((b)[(i)+2]); \
|
56
|
+
} while(0)
|
57
|
+
|
58
|
+
static inline void
|
59
|
+
int_encode_int(int n)
|
60
|
+
{
|
61
|
+
krypt_b64_out_buf[0] = krypt_b64_table[(n >> 18) & 0x3f];
|
62
|
+
krypt_b64_out_buf[1] = krypt_b64_table[(n >> 12) & 0x3f];
|
63
|
+
krypt_b64_out_buf[2] = krypt_b64_table[(n >> 6) & 0x3f];
|
64
|
+
krypt_b64_out_buf[3] = krypt_b64_table[n & 0x3f];
|
65
|
+
}
|
66
|
+
|
67
|
+
static int
|
68
|
+
int_write_int(binyo_outstream *out, int n)
|
69
|
+
{
|
70
|
+
int_encode_int(n);
|
71
|
+
if (binyo_outstream_write(out, krypt_b64_out_buf, 4) == BINYO_ERR)
|
72
|
+
return KRYPT_ERR;
|
73
|
+
return KRYPT_OK;
|
74
|
+
}
|
75
|
+
|
76
|
+
static int
|
77
|
+
int_write_update(binyo_outstream *out, uint8_t *bytes, size_t off, size_t len)
|
78
|
+
{
|
79
|
+
size_t i;
|
80
|
+
int n;
|
81
|
+
|
82
|
+
for (i=0; i < len; i+=3) {
|
83
|
+
int_compute_int(n, bytes, off + i);
|
84
|
+
if (int_write_int(out, n) == KRYPT_ERR) return KRYPT_ERR;
|
85
|
+
}
|
86
|
+
return KRYPT_OK;
|
87
|
+
}
|
88
|
+
|
89
|
+
static int
|
90
|
+
int_write_update_cols(binyo_outstream *out, uint8_t *bytes, size_t off, size_t len, int cols)
|
91
|
+
{
|
92
|
+
size_t i;
|
93
|
+
int n, linepos = 0;
|
94
|
+
|
95
|
+
for (i=0; i < len; i+=3) {
|
96
|
+
int_compute_int(n, bytes, off + i);
|
97
|
+
if (int_write_int(out, n) == KRYPT_ERR) return KRYPT_ERR;
|
98
|
+
linepos += 4;
|
99
|
+
if (linepos >= cols) {
|
100
|
+
if (binyo_outstream_write(out, krypt_b64_separator, 2) == BINYO_ERR)
|
101
|
+
return KRYPT_ERR;
|
102
|
+
linepos = 0;
|
103
|
+
}
|
104
|
+
}
|
105
|
+
return KRYPT_OK;
|
106
|
+
}
|
107
|
+
|
108
|
+
static inline void
|
109
|
+
int_encode_final(uint8_t *bytes, int remainder)
|
110
|
+
{
|
111
|
+
int n;
|
112
|
+
|
113
|
+
n = (bytes[0] << 16) | (remainder == 2 ? bytes[1] << 8 : 0);
|
114
|
+
krypt_b64_out_buf[0] = krypt_b64_table[(n >> 18) & 0x3f];
|
115
|
+
krypt_b64_out_buf[1] = krypt_b64_table[(n >> 12) & 0x3f];
|
116
|
+
krypt_b64_out_buf[2] = remainder == 2 ? krypt_b64_table[(n >> 6) & 0x3f] : '=';
|
117
|
+
krypt_b64_out_buf[3] = '=';
|
118
|
+
}
|
119
|
+
|
120
|
+
static int
|
121
|
+
int_write_final(binyo_outstream *out, uint8_t *bytes, int remainder, int crlf)
|
122
|
+
{
|
123
|
+
if (remainder) {
|
124
|
+
int_encode_final(bytes, remainder);
|
125
|
+
if (binyo_outstream_write(out, krypt_b64_out_buf, 4) == BINYO_ERR)
|
126
|
+
return KRYPT_ERR;
|
127
|
+
}
|
128
|
+
if (crlf) {
|
129
|
+
if (binyo_outstream_write(out, krypt_b64_separator, 2) == BINYO_ERR)
|
130
|
+
return KRYPT_ERR;
|
131
|
+
}
|
132
|
+
return 1;
|
133
|
+
}
|
134
|
+
|
135
|
+
int
|
136
|
+
krypt_base64_buffer_encode_to(binyo_outstream *out, uint8_t *bytes, size_t off, size_t len, int cols)
|
137
|
+
{
|
138
|
+
int remainder;
|
139
|
+
|
140
|
+
if (!bytes || !out) return KRYPT_ERR;
|
141
|
+
|
142
|
+
remainder = len % 3;
|
143
|
+
if (cols < 0) {
|
144
|
+
if (int_write_update(out, bytes, off, len - remainder) == KRYPT_ERR) return KRYPT_ERR;
|
145
|
+
} else {
|
146
|
+
if (int_write_update_cols(out, bytes, off, len - remainder, cols) == KRYPT_ERR) return KRYPT_ERR;
|
147
|
+
}
|
148
|
+
|
149
|
+
if (int_write_final(out, bytes + len - remainder, remainder, cols > 0) == KRYPT_ERR) return KRYPT_ERR;
|
150
|
+
return KRYPT_OK;
|
151
|
+
}
|
152
|
+
|
153
|
+
int
|
154
|
+
krypt_base64_encode(uint8_t *bytes, size_t len, int cols, uint8_t **out, size_t *outlen)
|
155
|
+
{
|
156
|
+
size_t retlen;
|
157
|
+
binyo_outstream *outstream;
|
158
|
+
|
159
|
+
if (!bytes) return KRYPT_ERR;
|
160
|
+
if ( (len / 3 + 1) > (SIZE_MAX / 4) ) {
|
161
|
+
krypt_error_add("Buffer too large: %ld", len);
|
162
|
+
return KRYPT_ERR;
|
163
|
+
}
|
164
|
+
|
165
|
+
/* this is the maximum value, no exactness needed, we'll resize anyway */
|
166
|
+
retlen = 4 * (len / 3 + 1);
|
167
|
+
|
168
|
+
/* Add the number of new line characters */
|
169
|
+
if (cols > 0) {
|
170
|
+
if ( (len / cols * 2) > SIZE_MAX - retlen ) {
|
171
|
+
krypt_error_add("Buffer too large: %ld", len);
|
172
|
+
return KRYPT_ERR;
|
173
|
+
}
|
174
|
+
retlen += len / cols * 2;
|
175
|
+
}
|
176
|
+
|
177
|
+
outstream = binyo_outstream_new_bytes_size(retlen);
|
178
|
+
if (krypt_base64_buffer_encode_to(outstream, bytes, 0, len, cols) == KRYPT_ERR) {
|
179
|
+
binyo_outstream_free(outstream);
|
180
|
+
return KRYPT_ERR;
|
181
|
+
}
|
182
|
+
retlen = binyo_outstream_bytes_get_bytes_free(outstream, out);
|
183
|
+
|
184
|
+
if (retlen > SIZE_MAX) {
|
185
|
+
krypt_error_add("Return value too large");
|
186
|
+
xfree(*out);
|
187
|
+
*out = NULL;
|
188
|
+
return KRYPT_ERR;
|
189
|
+
}
|
190
|
+
*outlen = retlen;
|
191
|
+
return KRYPT_OK;
|
192
|
+
}
|
193
|
+
|
194
|
+
static inline void
|
195
|
+
int_decode_int(int n)
|
196
|
+
{
|
197
|
+
krypt_b64_in_buf[0] = (n >> 16) & 0xff;
|
198
|
+
krypt_b64_in_buf[1] = (n >> 8) & 0xff;
|
199
|
+
krypt_b64_in_buf[2] = n & 0xff;
|
200
|
+
}
|
201
|
+
|
202
|
+
static int
|
203
|
+
int_read_int(binyo_outstream *out, int n)
|
204
|
+
{
|
205
|
+
int_decode_int(n);
|
206
|
+
if (binyo_outstream_write(out, krypt_b64_in_buf, 3) == BINYO_ERR)
|
207
|
+
return KRYPT_ERR;
|
208
|
+
return KRYPT_OK;
|
209
|
+
}
|
210
|
+
|
211
|
+
static inline void
|
212
|
+
int_decode_final(int n, int remainder)
|
213
|
+
{
|
214
|
+
switch (remainder) {
|
215
|
+
/* 2 of 4 bytes are to be discarded.
|
216
|
+
* 2 bytes represent 12 bits of meaningful data -> 1 byte plus 4 bits to be dropped */
|
217
|
+
case 2:
|
218
|
+
krypt_b64_in_buf[0] = (n >> 4) & 0xff;
|
219
|
+
break;
|
220
|
+
/* 1 of 4 bytes are to be discarded.
|
221
|
+
* 3 bytes represent 18 bits of meaningful data -> 2 bytes plus 2 bits to be dropped */
|
222
|
+
case 3:
|
223
|
+
n >>= 2;
|
224
|
+
krypt_b64_in_buf[0] = (n >> 8) & 0xff;
|
225
|
+
krypt_b64_in_buf[1] = n & 0xff;
|
226
|
+
break;
|
227
|
+
}
|
228
|
+
}
|
229
|
+
|
230
|
+
static int
|
231
|
+
int_read_final(binyo_outstream *out, int n, int remainder)
|
232
|
+
{
|
233
|
+
int_decode_final(n, remainder);
|
234
|
+
if (remainder > 1) {
|
235
|
+
if (binyo_outstream_write(out, krypt_b64_in_buf, remainder - 1) == BINYO_ERR) return KRYPT_ERR;
|
236
|
+
}
|
237
|
+
return KRYPT_OK;
|
238
|
+
}
|
239
|
+
|
240
|
+
int
|
241
|
+
krypt_base64_buffer_decode_to(binyo_outstream *out, uint8_t *bytes, size_t off, size_t len)
|
242
|
+
{
|
243
|
+
size_t i;
|
244
|
+
int n = 0;
|
245
|
+
int remainder = 0;
|
246
|
+
char inv;
|
247
|
+
|
248
|
+
if (len > SIZE_MAX - off) {
|
249
|
+
krypt_error_add("Buffer too large: %ld", len);
|
250
|
+
return KRYPT_ERR;
|
251
|
+
}
|
252
|
+
|
253
|
+
for (i=0; i < len; i++) {
|
254
|
+
uint8_t b = bytes[off + i];
|
255
|
+
if (b == '=')
|
256
|
+
break;
|
257
|
+
if (b > KRYPT_BASE64_INV_MAX)
|
258
|
+
continue;
|
259
|
+
inv = krypt_b64_table_inv[b];
|
260
|
+
if (inv < 0)
|
261
|
+
continue;
|
262
|
+
n = (n << 6) | inv;
|
263
|
+
remainder = (remainder + 1) % 4;
|
264
|
+
if (remainder == 0) {
|
265
|
+
if (int_read_int(out, n) == KRYPT_ERR) return KRYPT_ERR;
|
266
|
+
}
|
267
|
+
}
|
268
|
+
|
269
|
+
if (remainder && (int_read_final(out, n, remainder) == KRYPT_ERR)) return KRYPT_ERR;
|
270
|
+
return KRYPT_OK;
|
271
|
+
}
|
272
|
+
|
273
|
+
int
|
274
|
+
krypt_base64_decode(uint8_t *bytes, size_t len, uint8_t **out, size_t *outlen)
|
275
|
+
{
|
276
|
+
size_t retlen;
|
277
|
+
binyo_outstream *outstream;
|
278
|
+
|
279
|
+
if (!bytes) return KRYPT_ERR;
|
280
|
+
|
281
|
+
/* Approximate, will resize anyway */
|
282
|
+
retlen = len / 4 * 3;
|
283
|
+
|
284
|
+
outstream = binyo_outstream_new_bytes_size(retlen);
|
285
|
+
if (krypt_base64_buffer_decode_to(outstream, bytes, 0, len) == KRYPT_ERR) {
|
286
|
+
binyo_outstream_free(outstream);
|
287
|
+
return KRYPT_ERR;
|
288
|
+
}
|
289
|
+
retlen = binyo_outstream_bytes_get_bytes_free(outstream, out);
|
290
|
+
if (retlen > SSIZE_MAX) {
|
291
|
+
xfree(*out);
|
292
|
+
*out = NULL;
|
293
|
+
return KRYPT_ERR;
|
294
|
+
}
|
295
|
+
*outlen = retlen;
|
296
|
+
return KRYPT_OK;
|
297
|
+
}
|
298
|
+
|
299
|
+
/* Krypt::Base64 */
|
300
|
+
|
301
|
+
/**
|
302
|
+
* call-seq:
|
303
|
+
* Krypt::Base64.decode(data) -> String
|
304
|
+
*
|
305
|
+
* Decodes a Base64-encoded string of +data+, which need not necessarily be
|
306
|
+
* a String, but must allow a conversion with to_str.
|
307
|
+
*/
|
308
|
+
static VALUE
|
309
|
+
krypt_base64_module_decode(VALUE self, VALUE data)
|
310
|
+
{
|
311
|
+
VALUE ret;
|
312
|
+
size_t len;
|
313
|
+
size_t result_len;
|
314
|
+
uint8_t *bytes;
|
315
|
+
uint8_t *result = NULL;
|
316
|
+
|
317
|
+
StringValue(data);
|
318
|
+
len = (size_t) RSTRING_LEN(data);
|
319
|
+
bytes = (uint8_t *) RSTRING_PTR(data);
|
320
|
+
|
321
|
+
if (krypt_base64_decode(bytes, len, &result, &result_len) == KRYPT_ERR)
|
322
|
+
krypt_error_raise(eKryptBase64Error, "Processing the value failed.");
|
323
|
+
|
324
|
+
ret = rb_str_new((const char *) result, result_len);
|
325
|
+
xfree(result);
|
326
|
+
return ret;
|
327
|
+
}
|
328
|
+
|
329
|
+
/**
|
330
|
+
* call-seq:
|
331
|
+
* Krypt::Base64.encode(data, [cols=nil]) -> String
|
332
|
+
*
|
333
|
+
* Encodes a String, or an object allowing conversion with to_str, in Base64
|
334
|
+
* encoding. The optional +cols+ is an Integer parameter that may be used to
|
335
|
+
* specify the line length of the resulting Base64 string. As the result is
|
336
|
+
* being constructed in chunks of 4 characters at a time, a value of +cols+
|
337
|
+
* that is not a multiple of 4 will result in line feeds after the next higher
|
338
|
+
* multiple of 4 - for example, if +cols+ is specified as 22, then the result
|
339
|
+
* will have line feeds after every 24 characters of output.
|
340
|
+
*/
|
341
|
+
static VALUE
|
342
|
+
krypt_base64_module_encode(int argc, VALUE *argv, VALUE self)
|
343
|
+
{
|
344
|
+
VALUE data;
|
345
|
+
VALUE cols = Qnil;
|
346
|
+
VALUE ret;
|
347
|
+
int c;
|
348
|
+
size_t len;
|
349
|
+
size_t result_len;
|
350
|
+
uint8_t *bytes;
|
351
|
+
uint8_t *result = NULL;
|
352
|
+
|
353
|
+
rb_scan_args(argc, argv, "11", &data, &cols);
|
354
|
+
|
355
|
+
if (NIL_P(data))
|
356
|
+
rb_raise(eKryptBase64Error, "Data must not be nil");
|
357
|
+
if (NIL_P(cols))
|
358
|
+
c = -1;
|
359
|
+
else
|
360
|
+
c = NUM2INT(cols);
|
361
|
+
|
362
|
+
StringValue(data);
|
363
|
+
len = (size_t) RSTRING_LEN(data);
|
364
|
+
bytes = (uint8_t *) RSTRING_PTR(data);
|
365
|
+
|
366
|
+
if (krypt_base64_encode(bytes, len, c, &result, &result_len) == KRYPT_ERR)
|
367
|
+
krypt_error_raise(eKryptBase64Error, "Processing the value failed.");
|
368
|
+
|
369
|
+
ret = rb_str_new((const char *) result, result_len);
|
370
|
+
rb_enc_associate(ret, rb_usascii_encoding());
|
371
|
+
xfree(result);
|
372
|
+
return ret;
|
373
|
+
}
|
374
|
+
|
375
|
+
/* End Krypt::Base64 */
|
376
|
+
|
377
|
+
void
|
378
|
+
Init_krypt_base64(void)
|
379
|
+
{
|
380
|
+
#if 0
|
381
|
+
mKrypt = rb_define_module("Krypt"); /* Let RDoc know */
|
382
|
+
#endif
|
383
|
+
|
384
|
+
mKryptBase64 = rb_define_module_under(mKrypt, "Base64");
|
385
|
+
|
386
|
+
eKryptBase64Error = rb_define_class_under(mKryptBase64, "Base64Error", eKryptError);
|
387
|
+
|
388
|
+
rb_define_module_function(mKryptBase64, "decode", krypt_base64_module_decode, 1);
|
389
|
+
rb_define_module_function(mKryptBase64, "encode", krypt_base64_module_encode, -1);
|
390
|
+
}
|
391
|
+
|
@@ -0,0 +1,41 @@
|
|
1
|
+
/*
|
2
|
+
* krypt-core API - C implementation
|
3
|
+
*
|
4
|
+
* Copyright (c) 2011-2013
|
5
|
+
* Hiroshi Nakamura <nahi@ruby-lang.org>
|
6
|
+
* Martin Bosslet <martin.bosslet@gmail.com>
|
7
|
+
* All rights reserved.
|
8
|
+
*
|
9
|
+
* Permission is hereby granted, free of charge, to any person obtaining
|
10
|
+
* a copy of this software and associated documentation files (the
|
11
|
+
* "Software"), to deal in the Software without restriction, including
|
12
|
+
* without limitation the rights to use, copy, modify, merge, publish,
|
13
|
+
* distribute, sublicense, and/or sell copies of the Software, and to
|
14
|
+
* permit persons to whom the Software is furnished to do so, subject to
|
15
|
+
* the following conditions:
|
16
|
+
*
|
17
|
+
* The above copyright notice and this permission notice shall be
|
18
|
+
* included in all copies or substantial portions of the Software.
|
19
|
+
*
|
20
|
+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
21
|
+
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
22
|
+
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
23
|
+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
24
|
+
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
25
|
+
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
26
|
+
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
27
|
+
*/
|
28
|
+
|
29
|
+
#ifndef _KRYPT_B64_H
|
30
|
+
#define _KRYPT_B64_H_
|
31
|
+
|
32
|
+
extern VALUE mKryptBase64;
|
33
|
+
extern VALUE cKryptBase64Encoder;
|
34
|
+
extern VALUE cKryptBase64Decoder;
|
35
|
+
|
36
|
+
extern VALUE eKryptBase64Error;
|
37
|
+
|
38
|
+
void Init_krypt_base64(void);
|
39
|
+
|
40
|
+
#endif /* _KRYPT_B64_H_ */
|
41
|
+
|
Binary file
|