zscan 2.0.6 → 2.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ext/extconf.rb +9 -1
- data/ext/pack/builtin-27.h +79 -0
- data/ext/pack/builtin.h +79 -0
- data/ext/pack/internal-27.h +96 -0
- data/ext/pack/internal-27/array.h +103 -0
- data/ext/pack/internal-27/bignum.h +246 -0
- data/ext/pack/internal-27/bits.h +566 -0
- data/ext/pack/internal-27/class.h +162 -0
- data/ext/pack/internal-27/compar.h +50 -0
- data/ext/pack/internal-27/compile.h +32 -0
- data/ext/pack/internal-27/compilers.h +108 -0
- data/ext/pack/internal-27/complex.h +29 -0
- data/ext/pack/internal-27/cont.h +23 -0
- data/ext/pack/internal-27/dir.h +17 -0
- data/ext/pack/internal-27/enc.h +20 -0
- data/ext/pack/internal-27/encoding.h +28 -0
- data/ext/pack/internal-27/enum.h +19 -0
- data/ext/pack/internal-27/enumerator.h +22 -0
- data/ext/pack/internal-27/error.h +129 -0
- data/ext/pack/internal-27/eval.h +33 -0
- data/ext/pack/internal-27/file.h +39 -0
- data/ext/pack/internal-27/fixnum.h +185 -0
- data/ext/pack/internal-27/gc.h +153 -0
- data/ext/pack/internal-27/hash.h +234 -0
- data/ext/pack/internal-27/imemo.h +242 -0
- data/ext/pack/internal-27/inits.h +51 -0
- data/ext/pack/internal-27/io.h +35 -0
- data/ext/pack/internal-27/load.h +19 -0
- data/ext/pack/internal-27/loadpath.h +17 -0
- data/ext/pack/internal-27/math.h +24 -0
- data/ext/pack/internal-27/missing.h +19 -0
- data/ext/pack/internal-27/mjit.h +29 -0
- data/ext/pack/internal-27/numeric.h +249 -0
- data/ext/pack/internal-27/object.h +60 -0
- data/ext/pack/internal-27/parse.h +23 -0
- data/ext/pack/internal-27/proc.h +32 -0
- data/ext/pack/internal-27/process.h +130 -0
- data/ext/pack/internal-27/random.h +17 -0
- data/ext/pack/internal-27/range.h +37 -0
- data/ext/pack/internal-27/rational.h +68 -0
- data/ext/pack/internal-27/re.h +29 -0
- data/ext/pack/internal-27/sanitizers.h +191 -0
- data/ext/pack/internal-27/serial.h +24 -0
- data/ext/pack/internal-27/signal.h +22 -0
- data/ext/pack/internal-27/static_assert.h +17 -0
- data/ext/pack/internal-27/string.h +135 -0
- data/ext/pack/internal-27/struct.h +154 -0
- data/ext/pack/internal-27/symbol.h +41 -0
- data/ext/pack/internal-27/thread.h +52 -0
- data/ext/pack/internal-27/time.h +35 -0
- data/ext/pack/internal-27/transcode.h +21 -0
- data/ext/pack/internal-27/util.h +31 -0
- data/ext/pack/internal-27/variable.h +92 -0
- data/ext/pack/internal-27/vm.h +127 -0
- data/ext/pack/internal-27/warnings.h +17 -0
- data/ext/pack/internal.h +57 -2102
- data/ext/pack/internal/array.h +103 -0
- data/ext/pack/internal/bignum.h +246 -0
- data/ext/pack/internal/bits.h +566 -0
- data/ext/pack/internal/class.h +162 -0
- data/ext/pack/internal/compar.h +50 -0
- data/ext/pack/internal/compile.h +32 -0
- data/ext/pack/internal/compilers.h +108 -0
- data/ext/pack/internal/complex.h +29 -0
- data/ext/pack/internal/cont.h +23 -0
- data/ext/pack/internal/dir.h +17 -0
- data/ext/pack/internal/enc.h +20 -0
- data/ext/pack/internal/encoding.h +28 -0
- data/ext/pack/internal/enum.h +19 -0
- data/ext/pack/internal/enumerator.h +22 -0
- data/ext/pack/internal/error.h +129 -0
- data/ext/pack/internal/eval.h +33 -0
- data/ext/pack/internal/file.h +39 -0
- data/ext/pack/internal/fixnum.h +185 -0
- data/ext/pack/internal/gc.h +153 -0
- data/ext/pack/internal/hash.h +234 -0
- data/ext/pack/internal/imemo.h +242 -0
- data/ext/pack/internal/inits.h +51 -0
- data/ext/pack/internal/io.h +35 -0
- data/ext/pack/internal/load.h +19 -0
- data/ext/pack/internal/loadpath.h +17 -0
- data/ext/pack/internal/math.h +24 -0
- data/ext/pack/internal/missing.h +19 -0
- data/ext/pack/internal/mjit.h +29 -0
- data/ext/pack/internal/numeric.h +249 -0
- data/ext/pack/internal/object.h +60 -0
- data/ext/pack/internal/parse.h +23 -0
- data/ext/pack/internal/proc.h +32 -0
- data/ext/pack/internal/process.h +130 -0
- data/ext/pack/internal/random.h +17 -0
- data/ext/pack/internal/range.h +37 -0
- data/ext/pack/internal/rational.h +68 -0
- data/ext/pack/internal/re.h +29 -0
- data/ext/pack/internal/sanitizers.h +191 -0
- data/ext/pack/internal/serial.h +24 -0
- data/ext/pack/internal/signal.h +22 -0
- data/ext/pack/internal/static_assert.h +17 -0
- data/ext/pack/internal/string.h +135 -0
- data/ext/pack/internal/struct.h +154 -0
- data/ext/pack/internal/symbol.h +41 -0
- data/ext/pack/internal/thread.h +52 -0
- data/ext/pack/internal/time.h +35 -0
- data/ext/pack/internal/transcode.h +21 -0
- data/ext/pack/internal/util.h +31 -0
- data/ext/pack/internal/variable.h +92 -0
- data/ext/pack/internal/vm.h +127 -0
- data/ext/pack/internal/warnings.h +17 -0
- data/ext/pack/pack-26.c +2 -109
- data/ext/pack/pack-27.c +1022 -0
- data/ext/pack/pack.c +57 -1047
- data/lib/zscan.rb +1 -1
- data/zscan.gemspec +1 -1
- metadata +109 -3
data/ext/pack/pack.c
CHANGED
@@ -2,18 +2,30 @@
|
|
2
2
|
|
3
3
|
pack.c -
|
4
4
|
|
5
|
-
$Author
|
5
|
+
$Author$
|
6
6
|
created at: Thu Feb 10 15:17:05 JST 1994
|
7
7
|
|
8
8
|
Copyright (C) 1993-2007 Yukihiro Matsumoto
|
9
9
|
|
10
10
|
**********************************************************************/
|
11
11
|
|
12
|
-
#include "ruby/
|
13
|
-
|
14
|
-
#include <sys/types.h>
|
12
|
+
#include "ruby/internal/config.h"
|
13
|
+
|
15
14
|
#include <ctype.h>
|
16
15
|
#include <errno.h>
|
16
|
+
#include <float.h>
|
17
|
+
#include <sys/types.h>
|
18
|
+
|
19
|
+
#include "internal.h"
|
20
|
+
#include "internal/bits.h"
|
21
|
+
#include "internal/string.h"
|
22
|
+
#include "internal/symbol.h"
|
23
|
+
#include "internal/util.h"
|
24
|
+
|
25
|
+
// #include "internal/variable.h"
|
26
|
+
VALUE rb_ivar_lookup(VALUE obj, ID id, VALUE undef);
|
27
|
+
|
28
|
+
#include "builtin.h"
|
17
29
|
|
18
30
|
/*
|
19
31
|
* It is intentional that the condition for natstr is HAVE_TRUE_LONG_LONG
|
@@ -42,20 +54,20 @@ static const char endstr[] = "sSiIlLqQjJ";
|
|
42
54
|
#endif
|
43
55
|
|
44
56
|
#ifdef DYNAMIC_ENDIAN
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
57
|
+
/* for universal binary of NEXTSTEP and MacOS X */
|
58
|
+
/* useless since autoconf 2.63? */
|
59
|
+
static int
|
60
|
+
is_bigendian(void)
|
61
|
+
{
|
62
|
+
static int init = 0;
|
63
|
+
static int endian_value;
|
64
|
+
char *p;
|
65
|
+
|
66
|
+
if (init) return endian_value;
|
67
|
+
init = 1;
|
68
|
+
p = (char*)&init;
|
69
|
+
return endian_value = p[0]?0:1;
|
70
|
+
}
|
59
71
|
# define BIGENDIAN_P() (is_bigendian())
|
60
72
|
#elif defined(WORDS_BIGENDIAN)
|
61
73
|
# define BIGENDIAN_P() 1
|
@@ -105,11 +117,6 @@ typedef union {
|
|
105
117
|
|
106
118
|
#define MAX_INTEGER_PACK_SIZE 8
|
107
119
|
|
108
|
-
static const char toofew[] = "too few arguments";
|
109
|
-
|
110
|
-
static void encodes(VALUE,const char*,long,int,int);
|
111
|
-
static void qpencode(VALUE,VALUE,long);
|
112
|
-
|
113
120
|
static unsigned long utf8_to_uv(const char*,long*);
|
114
121
|
|
115
122
|
static ID id_associated;
|
@@ -127,867 +134,30 @@ str_associated(VALUE str)
|
|
127
134
|
return rb_ivar_lookup(str, id_associated, Qfalse);
|
128
135
|
}
|
129
136
|
|
130
|
-
|
131
|
-
*
|
132
|
-
* arr.pack( aTemplateString ) -> aBinaryString
|
133
|
-
* arr.pack( aTemplateString, buffer: aBufferString ) -> aBufferString
|
134
|
-
*
|
135
|
-
* Packs the contents of <i>arr</i> into a binary sequence according to
|
136
|
-
* the directives in <i>aTemplateString</i> (see the table below)
|
137
|
-
* Directives ``A,'' ``a,'' and ``Z'' may be followed by a count,
|
138
|
-
* which gives the width of the resulting field. The remaining
|
139
|
-
* directives also may take a count, indicating the number of array
|
140
|
-
* elements to convert. If the count is an asterisk
|
141
|
-
* (``<code>*</code>''), all remaining array elements will be
|
142
|
-
* converted. Any of the directives ``<code>sSiIlL</code>'' may be
|
143
|
-
* followed by an underscore (``<code>_</code>'') or
|
144
|
-
* exclamation mark (``<code>!</code>'') to use the underlying
|
145
|
-
* platform's native size for the specified type; otherwise, they use a
|
146
|
-
* platform-independent size. Spaces are ignored in the template
|
147
|
-
* string. See also <code>String#unpack</code>.
|
148
|
-
*
|
149
|
-
* a = [ "a", "b", "c" ]
|
150
|
-
* n = [ 65, 66, 67 ]
|
151
|
-
* a.pack("A3A3A3") #=> "a b c "
|
152
|
-
* a.pack("a3a3a3") #=> "a\000\000b\000\000c\000\000"
|
153
|
-
* n.pack("ccc") #=> "ABC"
|
154
|
-
*
|
155
|
-
* If <i>aBufferString</i> is specified and its capacity is enough,
|
156
|
-
* +pack+ uses it as the buffer and returns it.
|
157
|
-
* When the offset is specified by the beginning of <i>aTemplateString</i>,
|
158
|
-
* the result is filled after the offset.
|
159
|
-
* If original contents of <i>aBufferString</i> exists and it's longer than
|
160
|
-
* the offset, the rest of <i>offsetOfBuffer</i> are overwritten by the result.
|
161
|
-
* If it's shorter, the gap is filled with ``<code>\0</code>''.
|
162
|
-
*
|
163
|
-
* Note that ``buffer:'' option does not guarantee not to allocate memory
|
164
|
-
* in +pack+. If the capacity of <i>aBufferString</i> is not enough,
|
165
|
-
* +pack+ allocates memory.
|
166
|
-
*
|
167
|
-
* Directives for +pack+.
|
168
|
-
*
|
169
|
-
* Integer | Array |
|
170
|
-
* Directive | Element | Meaning
|
171
|
-
* ----------------------------------------------------------------------------
|
172
|
-
* C | Integer | 8-bit unsigned (unsigned char)
|
173
|
-
* S | Integer | 16-bit unsigned, native endian (uint16_t)
|
174
|
-
* L | Integer | 32-bit unsigned, native endian (uint32_t)
|
175
|
-
* Q | Integer | 64-bit unsigned, native endian (uint64_t)
|
176
|
-
* J | Integer | pointer width unsigned, native endian (uintptr_t)
|
177
|
-
* | | (J is available since Ruby 2.3.)
|
178
|
-
* | |
|
179
|
-
* c | Integer | 8-bit signed (signed char)
|
180
|
-
* s | Integer | 16-bit signed, native endian (int16_t)
|
181
|
-
* l | Integer | 32-bit signed, native endian (int32_t)
|
182
|
-
* q | Integer | 64-bit signed, native endian (int64_t)
|
183
|
-
* j | Integer | pointer width signed, native endian (intptr_t)
|
184
|
-
* | | (j is available since Ruby 2.3.)
|
185
|
-
* | |
|
186
|
-
* S_ S! | Integer | unsigned short, native endian
|
187
|
-
* I I_ I! | Integer | unsigned int, native endian
|
188
|
-
* L_ L! | Integer | unsigned long, native endian
|
189
|
-
* Q_ Q! | Integer | unsigned long long, native endian (ArgumentError
|
190
|
-
* | | if the platform has no long long type.)
|
191
|
-
* | | (Q_ and Q! is available since Ruby 2.1.)
|
192
|
-
* J! | Integer | uintptr_t, native endian (same with J)
|
193
|
-
* | | (J! is available since Ruby 2.3.)
|
194
|
-
* | |
|
195
|
-
* s_ s! | Integer | signed short, native endian
|
196
|
-
* i i_ i! | Integer | signed int, native endian
|
197
|
-
* l_ l! | Integer | signed long, native endian
|
198
|
-
* q_ q! | Integer | signed long long, native endian (ArgumentError
|
199
|
-
* | | if the platform has no long long type.)
|
200
|
-
* | | (q_ and q! is available since Ruby 2.1.)
|
201
|
-
* j! | Integer | intptr_t, native endian (same with j)
|
202
|
-
* | | (j! is available since Ruby 2.3.)
|
203
|
-
* | |
|
204
|
-
* S> s> S!> s!> | Integer | same as the directives without ">" except
|
205
|
-
* L> l> L!> l!> | | big endian
|
206
|
-
* I!> i!> | | (available since Ruby 1.9.3)
|
207
|
-
* Q> q> Q!> q!> | | "S>" is same as "n"
|
208
|
-
* J> j> J!> j!> | | "L>" is same as "N"
|
209
|
-
* | |
|
210
|
-
* S< s< S!< s!< | Integer | same as the directives without "<" except
|
211
|
-
* L< l< L!< l!< | | little endian
|
212
|
-
* I!< i!< | | (available since Ruby 1.9.3)
|
213
|
-
* Q< q< Q!< q!< | | "S<" is same as "v"
|
214
|
-
* J< j< J!< j!< | | "L<" is same as "V"
|
215
|
-
* | |
|
216
|
-
* n | Integer | 16-bit unsigned, network (big-endian) byte order
|
217
|
-
* N | Integer | 32-bit unsigned, network (big-endian) byte order
|
218
|
-
* v | Integer | 16-bit unsigned, VAX (little-endian) byte order
|
219
|
-
* V | Integer | 32-bit unsigned, VAX (little-endian) byte order
|
220
|
-
* | |
|
221
|
-
* U | Integer | UTF-8 character
|
222
|
-
* w | Integer | BER-compressed integer
|
223
|
-
*
|
224
|
-
* Float | Array |
|
225
|
-
* Directive | Element | Meaning
|
226
|
-
* ---------------------------------------------------------------------------
|
227
|
-
* D d | Float | double-precision, native format
|
228
|
-
* F f | Float | single-precision, native format
|
229
|
-
* E | Float | double-precision, little-endian byte order
|
230
|
-
* e | Float | single-precision, little-endian byte order
|
231
|
-
* G | Float | double-precision, network (big-endian) byte order
|
232
|
-
* g | Float | single-precision, network (big-endian) byte order
|
233
|
-
*
|
234
|
-
* String | Array |
|
235
|
-
* Directive | Element | Meaning
|
236
|
-
* ---------------------------------------------------------------------------
|
237
|
-
* A | String | arbitrary binary string (space padded, count is width)
|
238
|
-
* a | String | arbitrary binary string (null padded, count is width)
|
239
|
-
* Z | String | same as ``a'', except that null is added with *
|
240
|
-
* B | String | bit string (MSB first)
|
241
|
-
* b | String | bit string (LSB first)
|
242
|
-
* H | String | hex string (high nibble first)
|
243
|
-
* h | String | hex string (low nibble first)
|
244
|
-
* u | String | UU-encoded string
|
245
|
-
* M | String | quoted printable, MIME encoding (see also RFC2045)
|
246
|
-
* | | (text mode but input must use LF and output LF)
|
247
|
-
* m | String | base64 encoded string (see RFC 2045, count is width)
|
248
|
-
* | | (if count is 0, no line feed are added, see RFC 4648)
|
249
|
-
* P | String | pointer to a structure (fixed-length string)
|
250
|
-
* p | String | pointer to a null-terminated string
|
251
|
-
*
|
252
|
-
* Misc. | Array |
|
253
|
-
* Directive | Element | Meaning
|
254
|
-
* ---------------------------------------------------------------------------
|
255
|
-
* @ | --- | moves to absolute position
|
256
|
-
* X | --- | back up a byte
|
257
|
-
* x | --- | null byte
|
258
|
-
*/
|
259
|
-
|
260
|
-
static VALUE
|
261
|
-
pack_pack(int argc, VALUE *argv, VALUE ary)
|
137
|
+
static void
|
138
|
+
unknown_directive(const char *mode, char type, VALUE fmt)
|
262
139
|
{
|
263
|
-
|
264
|
-
|
265
|
-
const char *p, *pend;
|
266
|
-
VALUE fmt, opt = Qnil, res, from, associates = 0, buffer = 0;
|
267
|
-
char type;
|
268
|
-
long len, idx, plen;
|
269
|
-
const char *ptr;
|
270
|
-
int enc_info = 1; /* 0 - BINARY, 1 - US-ASCII, 2 - UTF-8 */
|
271
|
-
#ifdef NATINT_PACK
|
272
|
-
int natint; /* native integer */
|
273
|
-
#endif
|
274
|
-
int integer_size, bigendian_p;
|
275
|
-
|
276
|
-
rb_scan_args(argc, argv, "10:", &fmt, &opt);
|
277
|
-
|
278
|
-
StringValue(fmt);
|
279
|
-
p = RSTRING_PTR(fmt);
|
280
|
-
pend = p + RSTRING_LEN(fmt);
|
281
|
-
if (!NIL_P(opt)) {
|
282
|
-
static ID keyword_ids[1];
|
283
|
-
if (!keyword_ids[0])
|
284
|
-
CONST_ID(keyword_ids[0], "buffer");
|
285
|
-
|
286
|
-
rb_get_kwargs(opt, keyword_ids, 0, 1, &buffer);
|
287
|
-
|
288
|
-
if (buffer != Qundef && !RB_TYPE_P(buffer, T_STRING))
|
289
|
-
rb_raise(rb_eTypeError, "buffer must be String, not %s", rb_obj_classname(buffer));
|
290
|
-
}
|
291
|
-
if (buffer)
|
292
|
-
res = buffer;
|
293
|
-
else
|
294
|
-
res = rb_str_buf_new(0);
|
295
|
-
|
296
|
-
idx = 0;
|
297
|
-
|
298
|
-
#define TOO_FEW (rb_raise(rb_eArgError, toofew), 0)
|
299
|
-
#define MORE_ITEM (idx < RARRAY_LEN(ary))
|
300
|
-
#define THISFROM (MORE_ITEM ? RARRAY_AREF(ary, idx) : TOO_FEW)
|
301
|
-
#define NEXTFROM (MORE_ITEM ? RARRAY_AREF(ary, idx++) : TOO_FEW)
|
302
|
-
|
303
|
-
while (p < pend) {
|
304
|
-
int explicit_endian = 0;
|
305
|
-
if (RSTRING_PTR(fmt) + RSTRING_LEN(fmt) != pend) {
|
306
|
-
rb_raise(rb_eRuntimeError, "format string modified");
|
307
|
-
}
|
308
|
-
type = *p++; /* get data type */
|
309
|
-
#ifdef NATINT_PACK
|
310
|
-
natint = 0;
|
311
|
-
#endif
|
312
|
-
|
313
|
-
if (ISSPACE(type)) continue;
|
314
|
-
if (type == '#') {
|
315
|
-
while ((p < pend) && (*p != '\n')) {
|
316
|
-
p++;
|
317
|
-
}
|
318
|
-
continue;
|
319
|
-
}
|
320
|
-
|
321
|
-
{
|
322
|
-
modifiers:
|
323
|
-
switch (*p) {
|
324
|
-
case '_':
|
325
|
-
case '!':
|
326
|
-
if (strchr(natstr, type)) {
|
327
|
-
#ifdef NATINT_PACK
|
328
|
-
natint = 1;
|
329
|
-
#endif
|
330
|
-
p++;
|
331
|
-
}
|
332
|
-
else {
|
333
|
-
rb_raise(rb_eArgError, "'%c' allowed only after types %s", *p, natstr);
|
334
|
-
}
|
335
|
-
goto modifiers;
|
336
|
-
|
337
|
-
case '<':
|
338
|
-
case '>':
|
339
|
-
if (!strchr(endstr, type)) {
|
340
|
-
rb_raise(rb_eArgError, "'%c' allowed only after types %s", *p, endstr);
|
341
|
-
}
|
342
|
-
if (explicit_endian) {
|
343
|
-
rb_raise(rb_eRangeError, "Can't use both '<' and '>'");
|
344
|
-
}
|
345
|
-
explicit_endian = *p++;
|
346
|
-
goto modifiers;
|
347
|
-
}
|
348
|
-
}
|
349
|
-
|
350
|
-
if (*p == '*') { /* set data length */
|
351
|
-
len = strchr("@Xxu", type) ? 0
|
352
|
-
: strchr("PMm", type) ? 1
|
353
|
-
: RARRAY_LEN(ary) - idx;
|
354
|
-
p++;
|
355
|
-
}
|
356
|
-
else if (ISDIGIT(*p)) {
|
357
|
-
errno = 0;
|
358
|
-
len = STRTOUL(p, (char**)&p, 10);
|
359
|
-
if (errno) {
|
360
|
-
rb_raise(rb_eRangeError, "pack length too big");
|
361
|
-
}
|
362
|
-
}
|
363
|
-
else {
|
364
|
-
len = 1;
|
365
|
-
}
|
366
|
-
|
367
|
-
switch (type) {
|
368
|
-
case 'U':
|
369
|
-
/* if encoding is US-ASCII, upgrade to UTF-8 */
|
370
|
-
if (enc_info == 1) enc_info = 2;
|
371
|
-
break;
|
372
|
-
case 'm': case 'M': case 'u':
|
373
|
-
/* keep US-ASCII (do nothing) */
|
374
|
-
break;
|
375
|
-
default:
|
376
|
-
/* fall back to BINARY */
|
377
|
-
enc_info = 0;
|
378
|
-
break;
|
379
|
-
}
|
380
|
-
switch (type) {
|
381
|
-
case 'A': case 'a': case 'Z':
|
382
|
-
case 'B': case 'b':
|
383
|
-
case 'H': case 'h':
|
384
|
-
from = NEXTFROM;
|
385
|
-
if (NIL_P(from)) {
|
386
|
-
ptr = "";
|
387
|
-
plen = 0;
|
388
|
-
}
|
389
|
-
else {
|
390
|
-
StringValue(from);
|
391
|
-
ptr = RSTRING_PTR(from);
|
392
|
-
plen = RSTRING_LEN(from);
|
393
|
-
OBJ_INFECT(res, from);
|
394
|
-
}
|
395
|
-
|
396
|
-
if (p[-1] == '*')
|
397
|
-
len = plen;
|
398
|
-
|
399
|
-
switch (type) {
|
400
|
-
case 'a': /* arbitrary binary string (null padded) */
|
401
|
-
case 'A': /* arbitrary binary string (ASCII space padded) */
|
402
|
-
case 'Z': /* null terminated string */
|
403
|
-
if (plen >= len) {
|
404
|
-
rb_str_buf_cat(res, ptr, len);
|
405
|
-
if (p[-1] == '*' && type == 'Z')
|
406
|
-
rb_str_buf_cat(res, nul10, 1);
|
407
|
-
}
|
408
|
-
else {
|
409
|
-
rb_str_buf_cat(res, ptr, plen);
|
410
|
-
len -= plen;
|
411
|
-
while (len >= 10) {
|
412
|
-
rb_str_buf_cat(res, (type == 'A')?spc10:nul10, 10);
|
413
|
-
len -= 10;
|
414
|
-
}
|
415
|
-
rb_str_buf_cat(res, (type == 'A')?spc10:nul10, len);
|
416
|
-
}
|
417
|
-
break;
|
418
|
-
|
419
|
-
#define castchar(from) (char)((from) & 0xff)
|
420
|
-
|
421
|
-
case 'b': /* bit string (ascending) */
|
422
|
-
{
|
423
|
-
int byte = 0;
|
424
|
-
long i, j = 0;
|
425
|
-
|
426
|
-
if (len > plen) {
|
427
|
-
j = (len - plen + 1)/2;
|
428
|
-
len = plen;
|
429
|
-
}
|
430
|
-
for (i=0; i++ < len; ptr++) {
|
431
|
-
if (*ptr & 1)
|
432
|
-
byte |= 128;
|
433
|
-
if (i & 7)
|
434
|
-
byte >>= 1;
|
435
|
-
else {
|
436
|
-
char c = castchar(byte);
|
437
|
-
rb_str_buf_cat(res, &c, 1);
|
438
|
-
byte = 0;
|
439
|
-
}
|
440
|
-
}
|
441
|
-
if (len & 7) {
|
442
|
-
char c;
|
443
|
-
byte >>= 7 - (len & 7);
|
444
|
-
c = castchar(byte);
|
445
|
-
rb_str_buf_cat(res, &c, 1);
|
446
|
-
}
|
447
|
-
len = j;
|
448
|
-
goto grow;
|
449
|
-
}
|
450
|
-
break;
|
451
|
-
|
452
|
-
case 'B': /* bit string (descending) */
|
453
|
-
{
|
454
|
-
int byte = 0;
|
455
|
-
long i, j = 0;
|
456
|
-
|
457
|
-
if (len > plen) {
|
458
|
-
j = (len - plen + 1)/2;
|
459
|
-
len = plen;
|
460
|
-
}
|
461
|
-
for (i=0; i++ < len; ptr++) {
|
462
|
-
byte |= *ptr & 1;
|
463
|
-
if (i & 7)
|
464
|
-
byte <<= 1;
|
465
|
-
else {
|
466
|
-
char c = castchar(byte);
|
467
|
-
rb_str_buf_cat(res, &c, 1);
|
468
|
-
byte = 0;
|
469
|
-
}
|
470
|
-
}
|
471
|
-
if (len & 7) {
|
472
|
-
char c;
|
473
|
-
byte <<= 7 - (len & 7);
|
474
|
-
c = castchar(byte);
|
475
|
-
rb_str_buf_cat(res, &c, 1);
|
476
|
-
}
|
477
|
-
len = j;
|
478
|
-
goto grow;
|
479
|
-
}
|
480
|
-
break;
|
481
|
-
|
482
|
-
case 'h': /* hex string (low nibble first) */
|
483
|
-
{
|
484
|
-
int byte = 0;
|
485
|
-
long i, j = 0;
|
486
|
-
|
487
|
-
if (len > plen) {
|
488
|
-
j = (len + 1) / 2 - (plen + 1) / 2;
|
489
|
-
len = plen;
|
490
|
-
}
|
491
|
-
for (i=0; i++ < len; ptr++) {
|
492
|
-
if (ISALPHA(*ptr))
|
493
|
-
byte |= (((*ptr & 15) + 9) & 15) << 4;
|
494
|
-
else
|
495
|
-
byte |= (*ptr & 15) << 4;
|
496
|
-
if (i & 1)
|
497
|
-
byte >>= 4;
|
498
|
-
else {
|
499
|
-
char c = castchar(byte);
|
500
|
-
rb_str_buf_cat(res, &c, 1);
|
501
|
-
byte = 0;
|
502
|
-
}
|
503
|
-
}
|
504
|
-
if (len & 1) {
|
505
|
-
char c = castchar(byte);
|
506
|
-
rb_str_buf_cat(res, &c, 1);
|
507
|
-
}
|
508
|
-
len = j;
|
509
|
-
goto grow;
|
510
|
-
}
|
511
|
-
break;
|
512
|
-
|
513
|
-
case 'H': /* hex string (high nibble first) */
|
514
|
-
{
|
515
|
-
int byte = 0;
|
516
|
-
long i, j = 0;
|
517
|
-
|
518
|
-
if (len > plen) {
|
519
|
-
j = (len + 1) / 2 - (plen + 1) / 2;
|
520
|
-
len = plen;
|
521
|
-
}
|
522
|
-
for (i=0; i++ < len; ptr++) {
|
523
|
-
if (ISALPHA(*ptr))
|
524
|
-
byte |= ((*ptr & 15) + 9) & 15;
|
525
|
-
else
|
526
|
-
byte |= *ptr & 15;
|
527
|
-
if (i & 1)
|
528
|
-
byte <<= 4;
|
529
|
-
else {
|
530
|
-
char c = castchar(byte);
|
531
|
-
rb_str_buf_cat(res, &c, 1);
|
532
|
-
byte = 0;
|
533
|
-
}
|
534
|
-
}
|
535
|
-
if (len & 1) {
|
536
|
-
char c = castchar(byte);
|
537
|
-
rb_str_buf_cat(res, &c, 1);
|
538
|
-
}
|
539
|
-
len = j;
|
540
|
-
goto grow;
|
541
|
-
}
|
542
|
-
break;
|
543
|
-
}
|
544
|
-
break;
|
545
|
-
|
546
|
-
case 'c': /* signed char */
|
547
|
-
case 'C': /* unsigned char */
|
548
|
-
integer_size = 1;
|
549
|
-
bigendian_p = BIGENDIAN_P(); /* not effective */
|
550
|
-
goto pack_integer;
|
551
|
-
|
552
|
-
case 's': /* s for int16_t, s! for signed short */
|
553
|
-
integer_size = NATINT_LEN(short, 2);
|
554
|
-
bigendian_p = BIGENDIAN_P();
|
555
|
-
goto pack_integer;
|
556
|
-
|
557
|
-
case 'S': /* S for uint16_t, S! for unsigned short */
|
558
|
-
integer_size = NATINT_LEN(short, 2);
|
559
|
-
bigendian_p = BIGENDIAN_P();
|
560
|
-
goto pack_integer;
|
561
|
-
|
562
|
-
case 'i': /* i and i! for signed int */
|
563
|
-
integer_size = (int)sizeof(int);
|
564
|
-
bigendian_p = BIGENDIAN_P();
|
565
|
-
goto pack_integer;
|
566
|
-
|
567
|
-
case 'I': /* I and I! for unsigned int */
|
568
|
-
integer_size = (int)sizeof(int);
|
569
|
-
bigendian_p = BIGENDIAN_P();
|
570
|
-
goto pack_integer;
|
571
|
-
|
572
|
-
case 'l': /* l for int32_t, l! for signed long */
|
573
|
-
integer_size = NATINT_LEN(long, 4);
|
574
|
-
bigendian_p = BIGENDIAN_P();
|
575
|
-
goto pack_integer;
|
576
|
-
|
577
|
-
case 'L': /* L for uint32_t, L! for unsigned long */
|
578
|
-
integer_size = NATINT_LEN(long, 4);
|
579
|
-
bigendian_p = BIGENDIAN_P();
|
580
|
-
goto pack_integer;
|
581
|
-
|
582
|
-
case 'q': /* q for int64_t, q! for signed long long */
|
583
|
-
integer_size = NATINT_LEN_Q;
|
584
|
-
bigendian_p = BIGENDIAN_P();
|
585
|
-
goto pack_integer;
|
586
|
-
|
587
|
-
case 'Q': /* Q for uint64_t, Q! for unsigned long long */
|
588
|
-
integer_size = NATINT_LEN_Q;
|
589
|
-
bigendian_p = BIGENDIAN_P();
|
590
|
-
goto pack_integer;
|
140
|
+
VALUE f;
|
141
|
+
char unknown[5];
|
591
142
|
|
592
|
-
|
593
|
-
|
594
|
-
|
595
|
-
goto pack_integer;
|
596
|
-
|
597
|
-
case 'J': /* J for uintptr_t */
|
598
|
-
integer_size = sizeof(uintptr_t);
|
599
|
-
bigendian_p = BIGENDIAN_P();
|
600
|
-
goto pack_integer;
|
601
|
-
|
602
|
-
case 'n': /* 16 bit (2 bytes) integer (network byte-order) */
|
603
|
-
integer_size = 2;
|
604
|
-
bigendian_p = 1;
|
605
|
-
goto pack_integer;
|
606
|
-
|
607
|
-
case 'N': /* 32 bit (4 bytes) integer (network byte-order) */
|
608
|
-
integer_size = 4;
|
609
|
-
bigendian_p = 1;
|
610
|
-
goto pack_integer;
|
611
|
-
|
612
|
-
case 'v': /* 16 bit (2 bytes) integer (VAX byte-order) */
|
613
|
-
integer_size = 2;
|
614
|
-
bigendian_p = 0;
|
615
|
-
goto pack_integer;
|
616
|
-
|
617
|
-
case 'V': /* 32 bit (4 bytes) integer (VAX byte-order) */
|
618
|
-
integer_size = 4;
|
619
|
-
bigendian_p = 0;
|
620
|
-
goto pack_integer;
|
621
|
-
|
622
|
-
pack_integer:
|
623
|
-
if (explicit_endian) {
|
624
|
-
bigendian_p = explicit_endian == '>';
|
625
|
-
}
|
626
|
-
if (integer_size > MAX_INTEGER_PACK_SIZE)
|
627
|
-
rb_bug("unexpected intger size for pack: %d", integer_size);
|
628
|
-
while (len-- > 0) {
|
629
|
-
char intbuf[MAX_INTEGER_PACK_SIZE];
|
630
|
-
|
631
|
-
from = NEXTFROM;
|
632
|
-
rb_integer_pack(from, intbuf, integer_size, 1, 0,
|
633
|
-
INTEGER_PACK_2COMP |
|
634
|
-
(bigendian_p ? INTEGER_PACK_BIG_ENDIAN : INTEGER_PACK_LITTLE_ENDIAN));
|
635
|
-
rb_str_buf_cat(res, intbuf, integer_size);
|
636
|
-
}
|
637
|
-
break;
|
638
|
-
|
639
|
-
case 'f': /* single precision float in native format */
|
640
|
-
case 'F': /* ditto */
|
641
|
-
while (len-- > 0) {
|
642
|
-
float f;
|
643
|
-
|
644
|
-
from = NEXTFROM;
|
645
|
-
f = (float)RFLOAT_VALUE(rb_to_float(from));
|
646
|
-
rb_str_buf_cat(res, (char*)&f, sizeof(float));
|
647
|
-
}
|
648
|
-
break;
|
649
|
-
|
650
|
-
case 'e': /* single precision float in VAX byte-order */
|
651
|
-
while (len-- > 0) {
|
652
|
-
FLOAT_CONVWITH(tmp);
|
653
|
-
|
654
|
-
from = NEXTFROM;
|
655
|
-
tmp.f = (float)RFLOAT_VALUE(rb_to_float(from));
|
656
|
-
HTOVF(tmp);
|
657
|
-
rb_str_buf_cat(res, tmp.buf, sizeof(float));
|
658
|
-
}
|
659
|
-
break;
|
660
|
-
|
661
|
-
case 'E': /* double precision float in VAX byte-order */
|
662
|
-
while (len-- > 0) {
|
663
|
-
DOUBLE_CONVWITH(tmp);
|
664
|
-
from = NEXTFROM;
|
665
|
-
tmp.d = RFLOAT_VALUE(rb_to_float(from));
|
666
|
-
HTOVD(tmp);
|
667
|
-
rb_str_buf_cat(res, tmp.buf, sizeof(double));
|
668
|
-
}
|
669
|
-
break;
|
670
|
-
|
671
|
-
case 'd': /* double precision float in native format */
|
672
|
-
case 'D': /* ditto */
|
673
|
-
while (len-- > 0) {
|
674
|
-
double d;
|
675
|
-
|
676
|
-
from = NEXTFROM;
|
677
|
-
d = RFLOAT_VALUE(rb_to_float(from));
|
678
|
-
rb_str_buf_cat(res, (char*)&d, sizeof(double));
|
679
|
-
}
|
680
|
-
break;
|
681
|
-
|
682
|
-
case 'g': /* single precision float in network byte-order */
|
683
|
-
while (len-- > 0) {
|
684
|
-
FLOAT_CONVWITH(tmp);
|
685
|
-
from = NEXTFROM;
|
686
|
-
tmp.f = (float)RFLOAT_VALUE(rb_to_float(from));
|
687
|
-
HTONF(tmp);
|
688
|
-
rb_str_buf_cat(res, tmp.buf, sizeof(float));
|
689
|
-
}
|
690
|
-
break;
|
691
|
-
|
692
|
-
case 'G': /* double precision float in network byte-order */
|
693
|
-
while (len-- > 0) {
|
694
|
-
DOUBLE_CONVWITH(tmp);
|
695
|
-
|
696
|
-
from = NEXTFROM;
|
697
|
-
tmp.d = RFLOAT_VALUE(rb_to_float(from));
|
698
|
-
HTOND(tmp);
|
699
|
-
rb_str_buf_cat(res, tmp.buf, sizeof(double));
|
700
|
-
}
|
701
|
-
break;
|
702
|
-
|
703
|
-
case 'x': /* null byte */
|
704
|
-
grow:
|
705
|
-
while (len >= 10) {
|
706
|
-
rb_str_buf_cat(res, nul10, 10);
|
707
|
-
len -= 10;
|
708
|
-
}
|
709
|
-
rb_str_buf_cat(res, nul10, len);
|
710
|
-
break;
|
711
|
-
|
712
|
-
case 'X': /* back up byte */
|
713
|
-
shrink:
|
714
|
-
plen = RSTRING_LEN(res);
|
715
|
-
if (plen < len)
|
716
|
-
rb_raise(rb_eArgError, "X outside of string");
|
717
|
-
rb_str_set_len(res, plen - len);
|
718
|
-
break;
|
719
|
-
|
720
|
-
case '@': /* null fill to absolute position */
|
721
|
-
len -= RSTRING_LEN(res);
|
722
|
-
if (len > 0) goto grow;
|
723
|
-
len = -len;
|
724
|
-
if (len > 0) goto shrink;
|
725
|
-
break;
|
726
|
-
|
727
|
-
case '%':
|
728
|
-
rb_raise(rb_eArgError, "%% is not supported");
|
729
|
-
break;
|
730
|
-
|
731
|
-
case 'U': /* Unicode character */
|
732
|
-
while (len-- > 0) {
|
733
|
-
SIGNED_VALUE l;
|
734
|
-
char buf[8];
|
735
|
-
int le;
|
736
|
-
|
737
|
-
from = NEXTFROM;
|
738
|
-
from = rb_to_int(from);
|
739
|
-
l = NUM2LONG(from);
|
740
|
-
if (l < 0) {
|
741
|
-
rb_raise(rb_eRangeError, "pack(U): value out of range");
|
742
|
-
}
|
743
|
-
le = rb_uv_to_utf8(buf, l);
|
744
|
-
rb_str_buf_cat(res, (char*)buf, le);
|
745
|
-
}
|
746
|
-
break;
|
747
|
-
|
748
|
-
case 'u': /* uuencoded string */
|
749
|
-
case 'm': /* base64 encoded string */
|
750
|
-
from = NEXTFROM;
|
751
|
-
StringValue(from);
|
752
|
-
ptr = RSTRING_PTR(from);
|
753
|
-
plen = RSTRING_LEN(from);
|
754
|
-
|
755
|
-
if (len == 0 && type == 'm') {
|
756
|
-
encodes(res, ptr, plen, type, 0);
|
757
|
-
ptr += plen;
|
758
|
-
break;
|
759
|
-
}
|
760
|
-
if (len <= 2)
|
761
|
-
len = 45;
|
762
|
-
else if (len > 63 && type == 'u')
|
763
|
-
len = 63;
|
764
|
-
else
|
765
|
-
len = len / 3 * 3;
|
766
|
-
while (plen > 0) {
|
767
|
-
long todo;
|
768
|
-
|
769
|
-
if (plen > len)
|
770
|
-
todo = len;
|
771
|
-
else
|
772
|
-
todo = plen;
|
773
|
-
encodes(res, ptr, todo, type, 1);
|
774
|
-
plen -= todo;
|
775
|
-
ptr += todo;
|
776
|
-
}
|
777
|
-
break;
|
778
|
-
|
779
|
-
case 'M': /* quoted-printable encoded string */
|
780
|
-
from = rb_obj_as_string(NEXTFROM);
|
781
|
-
if (len <= 1)
|
782
|
-
len = 72;
|
783
|
-
qpencode(res, from, len);
|
784
|
-
break;
|
785
|
-
|
786
|
-
case 'P': /* pointer to packed byte string */
|
787
|
-
from = THISFROM;
|
788
|
-
if (!NIL_P(from)) {
|
789
|
-
StringValue(from);
|
790
|
-
if (RSTRING_LEN(from) < len) {
|
791
|
-
rb_raise(rb_eArgError, "too short buffer for P(%ld for %ld)",
|
792
|
-
RSTRING_LEN(from), len);
|
793
|
-
}
|
794
|
-
}
|
795
|
-
len = 1;
|
796
|
-
/* FALL THROUGH */
|
797
|
-
case 'p': /* pointer to string */
|
798
|
-
while (len-- > 0) {
|
799
|
-
char *t;
|
800
|
-
from = NEXTFROM;
|
801
|
-
if (NIL_P(from)) {
|
802
|
-
t = 0;
|
803
|
-
}
|
804
|
-
else {
|
805
|
-
t = StringValuePtr(from);
|
806
|
-
rb_obj_taint(from);
|
807
|
-
}
|
808
|
-
if (!associates) {
|
809
|
-
associates = rb_ary_new();
|
810
|
-
}
|
811
|
-
rb_ary_push(associates, from);
|
812
|
-
rb_str_buf_cat(res, (char*)&t, sizeof(char*));
|
813
|
-
}
|
814
|
-
break;
|
815
|
-
|
816
|
-
case 'w': /* BER compressed integer */
|
817
|
-
while (len-- > 0) {
|
818
|
-
VALUE buf = rb_str_new(0, 0);
|
819
|
-
size_t numbytes;
|
820
|
-
int sign;
|
821
|
-
char *cp;
|
822
|
-
|
823
|
-
from = NEXTFROM;
|
824
|
-
from = rb_to_int(from);
|
825
|
-
numbytes = rb_absint_numwords(from, 7, NULL);
|
826
|
-
if (numbytes == 0)
|
827
|
-
numbytes = 1;
|
828
|
-
buf = rb_str_new(NULL, numbytes);
|
829
|
-
|
830
|
-
sign = rb_integer_pack(from, RSTRING_PTR(buf), RSTRING_LEN(buf), 1, 1, INTEGER_PACK_BIG_ENDIAN);
|
831
|
-
|
832
|
-
if (sign < 0)
|
833
|
-
rb_raise(rb_eArgError, "can't compress negative numbers");
|
834
|
-
if (sign == 2)
|
835
|
-
rb_bug("buffer size problem?");
|
836
|
-
|
837
|
-
cp = RSTRING_PTR(buf);
|
838
|
-
while (1 < numbytes) {
|
839
|
-
*cp |= 0x80;
|
840
|
-
cp++;
|
841
|
-
numbytes--;
|
842
|
-
}
|
843
|
-
|
844
|
-
rb_str_buf_cat(res, RSTRING_PTR(buf), RSTRING_LEN(buf));
|
845
|
-
}
|
846
|
-
break;
|
847
|
-
|
848
|
-
default: {
|
849
|
-
char unknown[5];
|
850
|
-
if (ISPRINT(type)) {
|
851
|
-
unknown[0] = type;
|
852
|
-
unknown[1] = '\0';
|
853
|
-
}
|
854
|
-
else {
|
855
|
-
snprintf(unknown, sizeof(unknown), "\\x%.2x", type & 0xff);
|
856
|
-
}
|
857
|
-
rb_warning("unknown pack directive '%s' in '% "PRIsVALUE"'",
|
858
|
-
unknown, fmt);
|
859
|
-
break;
|
860
|
-
}
|
861
|
-
}
|
143
|
+
if (ISPRINT(type)) {
|
144
|
+
unknown[0] = type;
|
145
|
+
unknown[1] = '\0';
|
862
146
|
}
|
863
|
-
|
864
|
-
|
865
|
-
str_associate(res, associates);
|
147
|
+
else {
|
148
|
+
snprintf(unknown, sizeof(unknown), "\\x%.2x", type & 0xff);
|
866
149
|
}
|
867
|
-
|
868
|
-
|
869
|
-
|
870
|
-
ENCODING_CODERANGE_SET(res, rb_usascii_encindex(), ENC_CODERANGE_7BIT);
|
871
|
-
break;
|
872
|
-
case 2:
|
873
|
-
rb_enc_set_index(res, rb_utf8_encindex());
|
874
|
-
break;
|
875
|
-
default:
|
876
|
-
/* do nothing, keep ASCII-8BIT */
|
877
|
-
break;
|
150
|
+
f = rb_str_quote_unprintable(fmt);
|
151
|
+
if (f != fmt) {
|
152
|
+
fmt = rb_str_subseq(f, 1, RSTRING_LEN(f) - 2);
|
878
153
|
}
|
879
|
-
|
154
|
+
rb_warning("unknown %s directive '%s' in '%"PRIsVALUE"'",
|
155
|
+
mode, unknown, fmt);
|
880
156
|
}
|
881
157
|
|
882
|
-
static const char uu_table[] =
|
883
|
-
"`!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
|
884
158
|
static const char b64_table[] =
|
885
159
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
886
160
|
|
887
|
-
static void
|
888
|
-
encodes(VALUE str, const char *s0, long len, int type, int tail_lf)
|
889
|
-
{
|
890
|
-
enum {buff_size = 4096, encoded_unit = 4, input_unit = 3};
|
891
|
-
char buff[buff_size + 1]; /* +1 for tail_lf */
|
892
|
-
long i = 0;
|
893
|
-
const char *const trans = type == 'u' ? uu_table : b64_table;
|
894
|
-
char padding;
|
895
|
-
const unsigned char *s = (const unsigned char *)s0;
|
896
|
-
|
897
|
-
if (type == 'u') {
|
898
|
-
buff[i++] = (char)len + ' ';
|
899
|
-
padding = '`';
|
900
|
-
}
|
901
|
-
else {
|
902
|
-
padding = '=';
|
903
|
-
}
|
904
|
-
while (len >= input_unit) {
|
905
|
-
while (len >= input_unit && buff_size-i >= encoded_unit) {
|
906
|
-
buff[i++] = trans[077 & (*s >> 2)];
|
907
|
-
buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
|
908
|
-
buff[i++] = trans[077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03))];
|
909
|
-
buff[i++] = trans[077 & s[2]];
|
910
|
-
s += input_unit;
|
911
|
-
len -= input_unit;
|
912
|
-
}
|
913
|
-
if (buff_size-i < encoded_unit) {
|
914
|
-
rb_str_buf_cat(str, buff, i);
|
915
|
-
i = 0;
|
916
|
-
}
|
917
|
-
}
|
918
|
-
|
919
|
-
if (len == 2) {
|
920
|
-
buff[i++] = trans[077 & (*s >> 2)];
|
921
|
-
buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
|
922
|
-
buff[i++] = trans[077 & (((s[1] << 2) & 074) | (('\0' >> 6) & 03))];
|
923
|
-
buff[i++] = padding;
|
924
|
-
}
|
925
|
-
else if (len == 1) {
|
926
|
-
buff[i++] = trans[077 & (*s >> 2)];
|
927
|
-
buff[i++] = trans[077 & (((*s << 4) & 060) | (('\0' >> 4) & 017))];
|
928
|
-
buff[i++] = padding;
|
929
|
-
buff[i++] = padding;
|
930
|
-
}
|
931
|
-
if (tail_lf) buff[i++] = '\n';
|
932
|
-
rb_str_buf_cat(str, buff, i);
|
933
|
-
if ((size_t)i > sizeof(buff)) rb_bug("encodes() buffer overrun");
|
934
|
-
}
|
935
|
-
|
936
|
-
static const char hex_table[] = "0123456789ABCDEF";
|
937
|
-
|
938
|
-
static void
|
939
|
-
qpencode(VALUE str, VALUE from, long len)
|
940
|
-
{
|
941
|
-
char buff[1024];
|
942
|
-
long i = 0, n = 0, prev = EOF;
|
943
|
-
unsigned char *s = (unsigned char*)RSTRING_PTR(from);
|
944
|
-
unsigned char *send = s + RSTRING_LEN(from);
|
945
|
-
|
946
|
-
while (s < send) {
|
947
|
-
if ((*s > 126) ||
|
948
|
-
(*s < 32 && *s != '\n' && *s != '\t') ||
|
949
|
-
(*s == '=')) {
|
950
|
-
buff[i++] = '=';
|
951
|
-
buff[i++] = hex_table[*s >> 4];
|
952
|
-
buff[i++] = hex_table[*s & 0x0f];
|
953
|
-
n += 3;
|
954
|
-
prev = EOF;
|
955
|
-
}
|
956
|
-
else if (*s == '\n') {
|
957
|
-
if (prev == ' ' || prev == '\t') {
|
958
|
-
buff[i++] = '=';
|
959
|
-
buff[i++] = *s;
|
960
|
-
}
|
961
|
-
buff[i++] = *s;
|
962
|
-
n = 0;
|
963
|
-
prev = *s;
|
964
|
-
}
|
965
|
-
else {
|
966
|
-
buff[i++] = *s;
|
967
|
-
n++;
|
968
|
-
prev = *s;
|
969
|
-
}
|
970
|
-
if (n > len) {
|
971
|
-
buff[i++] = '=';
|
972
|
-
buff[i++] = '\n';
|
973
|
-
n = 0;
|
974
|
-
prev = '\n';
|
975
|
-
}
|
976
|
-
if (i > 1024 - 5) {
|
977
|
-
rb_str_buf_cat(str, buff, i);
|
978
|
-
i = 0;
|
979
|
-
}
|
980
|
-
s++;
|
981
|
-
}
|
982
|
-
if (n > 0) {
|
983
|
-
buff[i++] = '=';
|
984
|
-
buff[i++] = '\n';
|
985
|
-
}
|
986
|
-
if (i > 0) {
|
987
|
-
rb_str_buf_cat(str, buff, i);
|
988
|
-
}
|
989
|
-
}
|
990
|
-
|
991
161
|
static inline int
|
992
162
|
hex2num(char c)
|
993
163
|
{
|
@@ -1002,7 +172,7 @@ hex2num(char c)
|
|
1002
172
|
tmp_len = 0; \
|
1003
173
|
if (len > (long)((send-s)/(sz))) { \
|
1004
174
|
if (!star) { \
|
1005
|
-
tmp_len = len-(send-s)/(sz);
|
175
|
+
tmp_len = len-(send-s)/(sz); \
|
1006
176
|
} \
|
1007
177
|
len = (send-s)/(sz); \
|
1008
178
|
} \
|
@@ -1023,20 +193,13 @@ hex2num(char c)
|
|
1023
193
|
# define AVOID_CC_BUG
|
1024
194
|
#endif
|
1025
195
|
|
1026
|
-
static VALUE
|
1027
|
-
infected_str_new(const char *ptr, long len, VALUE str)
|
1028
|
-
{
|
1029
|
-
VALUE s = rb_str_new(ptr, len);
|
1030
|
-
|
1031
|
-
OBJ_INFECT(s, str);
|
1032
|
-
return s;
|
1033
|
-
}
|
1034
|
-
|
1035
196
|
/* unpack mode */
|
1036
197
|
#define UNPACK_ARRAY 0
|
1037
198
|
#define UNPACK_BLOCK 1
|
1038
199
|
#define UNPACK_1 2
|
1039
200
|
|
201
|
+
#define castchar(from) (char)((from) & 0xff)
|
202
|
+
|
1040
203
|
VALUE zscan_internal_unpack(VALUE str, VALUE fmt, long* parsed_len)
|
1041
204
|
{
|
1042
205
|
char* init_s;
|
@@ -1153,7 +316,7 @@ VALUE zscan_internal_unpack(VALUE str, VALUE fmt, long* parsed_len)
|
|
1153
316
|
if (*t != ' ' && *t != '\0') break;
|
1154
317
|
t--; len--;
|
1155
318
|
}
|
1156
|
-
|
319
|
+
UNPACK_PUSH(rb_str_new(s, len));
|
1157
320
|
s += end;
|
1158
321
|
}
|
1159
322
|
break;
|
@@ -1164,7 +327,7 @@ VALUE zscan_internal_unpack(VALUE str, VALUE fmt, long* parsed_len)
|
|
1164
327
|
|
1165
328
|
if (len > send-s) len = send-s;
|
1166
329
|
while (t < s+len && *t) t++;
|
1167
|
-
|
330
|
+
UNPACK_PUSH(rb_str_new(s, t-s));
|
1168
331
|
if (t < send) t++;
|
1169
332
|
s = star ? t : s+len;
|
1170
333
|
}
|
@@ -1172,7 +335,7 @@ VALUE zscan_internal_unpack(VALUE str, VALUE fmt, long* parsed_len)
|
|
1172
335
|
|
1173
336
|
case 'a':
|
1174
337
|
if (len > send - s) len = send - s;
|
1175
|
-
|
338
|
+
UNPACK_PUSH(rb_str_new(s, len));
|
1176
339
|
s += len;
|
1177
340
|
break;
|
1178
341
|
|
@@ -1463,7 +626,7 @@ VALUE zscan_internal_unpack(VALUE str, VALUE fmt, long* parsed_len)
|
|
1463
626
|
|
1464
627
|
case 'u':
|
1465
628
|
{
|
1466
|
-
|
629
|
+
VALUE buf = rb_str_new(0, (send - s)*3/4);
|
1467
630
|
char *ptr = RSTRING_PTR(buf);
|
1468
631
|
long total = 0;
|
1469
632
|
|
@@ -1518,7 +681,7 @@ VALUE zscan_internal_unpack(VALUE str, VALUE fmt, long* parsed_len)
|
|
1518
681
|
|
1519
682
|
case 'm':
|
1520
683
|
{
|
1521
|
-
|
684
|
+
VALUE buf = rb_str_new(0, (send - s + 3)*3/4); /* +3 is for skipping paddings */
|
1522
685
|
char *ptr = RSTRING_PTR(buf);
|
1523
686
|
int a = -1,b = -1,c = 0,d = 0;
|
1524
687
|
static signed char b64_xtable[256];
|
@@ -1599,7 +762,7 @@ VALUE zscan_internal_unpack(VALUE str, VALUE fmt, long* parsed_len)
|
|
1599
762
|
|
1600
763
|
case 'M':
|
1601
764
|
{
|
1602
|
-
|
765
|
+
VALUE buf = rb_str_new(0, send - s);
|
1603
766
|
char *ptr = RSTRING_PTR(buf), *ss = s;
|
1604
767
|
int csum = 0;
|
1605
768
|
int c1, c2;
|
@@ -1668,7 +831,7 @@ VALUE zscan_internal_unpack(VALUE str, VALUE fmt, long* parsed_len)
|
|
1668
831
|
while (p < pend) {
|
1669
832
|
if (RB_TYPE_P(*p, T_STRING) && RSTRING_PTR(*p) == t) {
|
1670
833
|
if (len < RSTRING_LEN(*p)) {
|
1671
|
-
|
834
|
+
tmp = rb_str_new(t, len);
|
1672
835
|
str_associate(tmp, a);
|
1673
836
|
}
|
1674
837
|
else {
|
@@ -1742,8 +905,7 @@ VALUE zscan_internal_unpack(VALUE str, VALUE fmt, long* parsed_len)
|
|
1742
905
|
break;
|
1743
906
|
|
1744
907
|
default:
|
1745
|
-
|
1746
|
-
type, RSTRING_PTR(fmt));
|
908
|
+
unknown_directive("unpack", type, fmt);
|
1747
909
|
break;
|
1748
910
|
}
|
1749
911
|
}
|
@@ -1752,150 +914,8 @@ VALUE zscan_internal_unpack(VALUE str, VALUE fmt, long* parsed_len)
|
|
1752
914
|
return ary;
|
1753
915
|
}
|
1754
916
|
|
1755
|
-
/*
|
1756
|
-
* call-seq:
|
1757
|
-
* str.unpack(format) -> anArray
|
1758
|
-
*
|
1759
|
-
* Decodes <i>str</i> (which may contain binary data) according to the
|
1760
|
-
* format string, returning an array of each value extracted. The
|
1761
|
-
* format string consists of a sequence of single-character directives,
|
1762
|
-
* summarized in the table at the end of this entry.
|
1763
|
-
* Each directive may be followed
|
1764
|
-
* by a number, indicating the number of times to repeat with this
|
1765
|
-
* directive. An asterisk (``<code>*</code>'') will use up all
|
1766
|
-
* remaining elements. The directives <code>sSiIlL</code> may each be
|
1767
|
-
* followed by an underscore (``<code>_</code>'') or
|
1768
|
-
* exclamation mark (``<code>!</code>'') to use the underlying
|
1769
|
-
* platform's native size for the specified type; otherwise, it uses a
|
1770
|
-
* platform-independent consistent size. Spaces are ignored in the
|
1771
|
-
* format string. See also <code>String#unpack1</code>, <code>Array#pack</code>.
|
1772
|
-
*
|
1773
|
-
* "abc \0\0abc \0\0".unpack('A6Z6') #=> ["abc", "abc "]
|
1774
|
-
* "abc \0\0".unpack('a3a3') #=> ["abc", " \000\000"]
|
1775
|
-
* "abc \0abc \0".unpack('Z*Z*') #=> ["abc ", "abc "]
|
1776
|
-
* "aa".unpack('b8B8') #=> ["10000110", "01100001"]
|
1777
|
-
* "aaa".unpack('h2H2c') #=> ["16", "61", 97]
|
1778
|
-
* "\xfe\xff\xfe\xff".unpack('sS') #=> [-2, 65534]
|
1779
|
-
* "now=20is".unpack('M*') #=> ["now is"]
|
1780
|
-
* "whole".unpack('xax2aX2aX1aX2a') #=> ["h", "e", "l", "l", "o"]
|
1781
|
-
*
|
1782
|
-
* This table summarizes the various formats and the Ruby classes
|
1783
|
-
* returned by each.
|
1784
|
-
*
|
1785
|
-
* Integer | |
|
1786
|
-
* Directive | Returns | Meaning
|
1787
|
-
* ------------------------------------------------------------------
|
1788
|
-
* C | Integer | 8-bit unsigned (unsigned char)
|
1789
|
-
* S | Integer | 16-bit unsigned, native endian (uint16_t)
|
1790
|
-
* L | Integer | 32-bit unsigned, native endian (uint32_t)
|
1791
|
-
* Q | Integer | 64-bit unsigned, native endian (uint64_t)
|
1792
|
-
* J | Integer | pointer width unsigned, native endian (uintptr_t)
|
1793
|
-
* | |
|
1794
|
-
* c | Integer | 8-bit signed (signed char)
|
1795
|
-
* s | Integer | 16-bit signed, native endian (int16_t)
|
1796
|
-
* l | Integer | 32-bit signed, native endian (int32_t)
|
1797
|
-
* q | Integer | 64-bit signed, native endian (int64_t)
|
1798
|
-
* j | Integer | pointer width signed, native endian (intptr_t)
|
1799
|
-
* | |
|
1800
|
-
* S_ S! | Integer | unsigned short, native endian
|
1801
|
-
* I I_ I! | Integer | unsigned int, native endian
|
1802
|
-
* L_ L! | Integer | unsigned long, native endian
|
1803
|
-
* Q_ Q! | Integer | unsigned long long, native endian (ArgumentError
|
1804
|
-
* | | if the platform has no long long type.)
|
1805
|
-
* J! | Integer | uintptr_t, native endian (same with J)
|
1806
|
-
* | |
|
1807
|
-
* s_ s! | Integer | signed short, native endian
|
1808
|
-
* i i_ i! | Integer | signed int, native endian
|
1809
|
-
* l_ l! | Integer | signed long, native endian
|
1810
|
-
* q_ q! | Integer | signed long long, native endian (ArgumentError
|
1811
|
-
* | | if the platform has no long long type.)
|
1812
|
-
* j! | Integer | intptr_t, native endian (same with j)
|
1813
|
-
* | |
|
1814
|
-
* S> s> S!> s!> | Integer | same as the directives without ">" except
|
1815
|
-
* L> l> L!> l!> | | big endian
|
1816
|
-
* I!> i!> | |
|
1817
|
-
* Q> q> Q!> q!> | | "S>" is same as "n"
|
1818
|
-
* J> j> J!> j!> | | "L>" is same as "N"
|
1819
|
-
* | |
|
1820
|
-
* S< s< S!< s!< | Integer | same as the directives without "<" except
|
1821
|
-
* L< l< L!< l!< | | little endian
|
1822
|
-
* I!< i!< | |
|
1823
|
-
* Q< q< Q!< q!< | | "S<" is same as "v"
|
1824
|
-
* J< j< J!< j!< | | "L<" is same as "V"
|
1825
|
-
* | |
|
1826
|
-
* n | Integer | 16-bit unsigned, network (big-endian) byte order
|
1827
|
-
* N | Integer | 32-bit unsigned, network (big-endian) byte order
|
1828
|
-
* v | Integer | 16-bit unsigned, VAX (little-endian) byte order
|
1829
|
-
* V | Integer | 32-bit unsigned, VAX (little-endian) byte order
|
1830
|
-
* | |
|
1831
|
-
* U | Integer | UTF-8 character
|
1832
|
-
* w | Integer | BER-compressed integer (see Array.pack)
|
1833
|
-
*
|
1834
|
-
* Float | |
|
1835
|
-
* Directive | Returns | Meaning
|
1836
|
-
* -----------------------------------------------------------------
|
1837
|
-
* D d | Float | double-precision, native format
|
1838
|
-
* F f | Float | single-precision, native format
|
1839
|
-
* E | Float | double-precision, little-endian byte order
|
1840
|
-
* e | Float | single-precision, little-endian byte order
|
1841
|
-
* G | Float | double-precision, network (big-endian) byte order
|
1842
|
-
* g | Float | single-precision, network (big-endian) byte order
|
1843
|
-
*
|
1844
|
-
* String | |
|
1845
|
-
* Directive | Returns | Meaning
|
1846
|
-
* -----------------------------------------------------------------
|
1847
|
-
* A | String | arbitrary binary string (remove trailing nulls and ASCII spaces)
|
1848
|
-
* a | String | arbitrary binary string
|
1849
|
-
* Z | String | null-terminated string
|
1850
|
-
* B | String | bit string (MSB first)
|
1851
|
-
* b | String | bit string (LSB first)
|
1852
|
-
* H | String | hex string (high nibble first)
|
1853
|
-
* h | String | hex string (low nibble first)
|
1854
|
-
* u | String | UU-encoded string
|
1855
|
-
* M | String | quoted-printable, MIME encoding (see RFC2045)
|
1856
|
-
* m | String | base64 encoded string (RFC 2045) (default)
|
1857
|
-
* | | base64 encoded string (RFC 4648) if followed by 0
|
1858
|
-
* P | String | pointer to a structure (fixed-length string)
|
1859
|
-
* p | String | pointer to a null-terminated string
|
1860
|
-
*
|
1861
|
-
* Misc. | |
|
1862
|
-
* Directive | Returns | Meaning
|
1863
|
-
* -----------------------------------------------------------------
|
1864
|
-
* @ | --- | skip to the offset given by the length argument
|
1865
|
-
* X | --- | skip backward one byte
|
1866
|
-
* x | --- | skip forward one byte
|
1867
|
-
*
|
1868
|
-
* HISTORY
|
1869
|
-
*
|
1870
|
-
* * J, J! j, and j! are available since Ruby 2.3.
|
1871
|
-
* * Q_, Q!, q_, and q! are available since Ruby 2.1.
|
1872
|
-
* * I!<, i!<, I!>, and i!> are available since Ruby 1.9.3.
|
1873
|
-
*/
|
1874
|
-
|
1875
|
-
static VALUE
|
1876
|
-
pack_unpack(VALUE str, VALUE fmt)
|
1877
|
-
{
|
1878
|
-
int mode = rb_block_given_p() ? UNPACK_BLOCK : UNPACK_ARRAY;
|
1879
|
-
return pack_unpack_internal(str, fmt, mode);
|
1880
|
-
}
|
1881
|
-
|
1882
|
-
/*
|
1883
|
-
* call-seq:
|
1884
|
-
* str.unpack1(format) -> obj
|
1885
|
-
*
|
1886
|
-
* Decodes <i>str</i> (which may contain binary data) according to the
|
1887
|
-
* format string, returning the first value extracted.
|
1888
|
-
* See also <code>String#unpack</code>, <code>Array#pack</code>.
|
1889
|
-
*/
|
1890
|
-
|
1891
|
-
static VALUE
|
1892
|
-
pack_unpack1(VALUE str, VALUE fmt)
|
1893
|
-
{
|
1894
|
-
return pack_unpack_internal(str, fmt, UNPACK_1);
|
1895
|
-
}
|
1896
|
-
|
1897
917
|
int
|
1898
|
-
|
918
|
+
srb_uv_to_utf8(char buf[6], unsigned long uv)
|
1899
919
|
{
|
1900
920
|
if (uv <= 0x7f) {
|
1901
921
|
buf[0] = (char)uv;
|
@@ -1938,7 +958,7 @@ rb_uv_to_utf8(char buf[6], unsigned long uv)
|
|
1938
958
|
}
|
1939
959
|
rb_raise(rb_eRangeError, "pack(U): value out of range");
|
1940
960
|
|
1941
|
-
|
961
|
+
UNREACHABLE_RETURN(Qnil);
|
1942
962
|
}
|
1943
963
|
|
1944
964
|
static const unsigned long utf8_limits[] = {
|
@@ -2000,13 +1020,3 @@ utf8_to_uv(const char *p, long *lenp)
|
|
2000
1020
|
}
|
2001
1021
|
return uv;
|
2002
1022
|
}
|
2003
|
-
|
2004
|
-
void
|
2005
|
-
Init_pack(void)
|
2006
|
-
{
|
2007
|
-
rb_define_method(rb_cArray, "pack", pack_pack, -1);
|
2008
|
-
rb_define_method(rb_cString, "unpack", pack_unpack, 1);
|
2009
|
-
rb_define_method(rb_cString, "unpack1", pack_unpack1, 1);
|
2010
|
-
|
2011
|
-
id_associated = rb_make_internal_id();
|
2012
|
-
}
|