Ruby  2.0.0p645(2015-04-13revision50299)
pack.c
Go to the documentation of this file.
1 /**********************************************************************
2 
3  pack.c -
4 
5  $Author: usa $
6  created at: Thu Feb 10 15:17:05 JST 1994
7 
8  Copyright (C) 1993-2007 Yukihiro Matsumoto
9 
10 **********************************************************************/
11 
12 #include "ruby/ruby.h"
13 #include "ruby/encoding.h"
14 #include <sys/types.h>
15 #include <ctype.h>
16 #include <errno.h>
17 
18 #define GCC_VERSION_SINCE(major, minor, patchlevel) \
19  (defined(__GNUC__) && !defined(__INTEL_COMPILER) && \
20  ((__GNUC__ > (major)) || \
21  (__GNUC__ == (major) && __GNUC_MINOR__ > (minor)) || \
22  (__GNUC__ == (major) && __GNUC_MINOR__ == (minor) && __GNUC_PATCHLEVEL__ >= (patchlevel))))
23 #if SIZEOF_SHORT != 2 || SIZEOF_LONG != 4
24 # define NATINT_PACK
25 #endif
26 
27 #ifdef DYNAMIC_ENDIAN
28  /* for universal binary of NEXTSTEP and MacOS X */
29  /* useless since autoconf 2.63? */
30  static int
31  is_bigendian(void)
32  {
33  static int init = 0;
34  static int endian_value;
35  char *p;
36 
37  if (init) return endian_value;
38  init = 1;
39  p = (char*)&init;
40  return endian_value = p[0]?0:1;
41  }
42 # define BIGENDIAN_P() (is_bigendian())
43 #elif defined(WORDS_BIGENDIAN)
44 # define BIGENDIAN_P() 1
45 #else
46 # define BIGENDIAN_P() 0
47 #endif
48 
49 #ifdef NATINT_PACK
50 # define NATINT_LEN(type,len) (natint?(int)sizeof(type):(int)(len))
51 #else
52 # define NATINT_LEN(type,len) ((int)sizeof(type))
53 #endif
54 
55 #if SIZEOF_LONG == 8
56 # define INT64toNUM(x) LONG2NUM(x)
57 # define UINT64toNUM(x) ULONG2NUM(x)
58 #elif defined(HAVE_LONG_LONG) && SIZEOF_LONG_LONG == 8
59 # define INT64toNUM(x) LL2NUM(x)
60 # define UINT64toNUM(x) ULL2NUM(x)
61 #endif
62 
63 #define define_swapx(x, xtype) \
64 static xtype \
65 TOKEN_PASTE(swap,x)(xtype z) \
66 { \
67  xtype r; \
68  xtype *zp; \
69  unsigned char *s, *t; \
70  int i; \
71  \
72  zp = xmalloc(sizeof(xtype)); \
73  *zp = z; \
74  s = (unsigned char*)zp; \
75  t = xmalloc(sizeof(xtype)); \
76  for (i=0; i<sizeof(xtype); i++) { \
77  t[sizeof(xtype)-i-1] = s[i]; \
78  } \
79  r = *(xtype *)t; \
80  xfree(t); \
81  xfree(zp); \
82  return r; \
83 }
84 
85 #if GCC_VERSION_SINCE(4,3,0)
86 # define swap32(x) __builtin_bswap32(x)
87 # define swap64(x) __builtin_bswap64(x)
88 #endif
89 
90 #ifndef swap16
91 # define swap16(x) ((uint16_t)((((x)&0xFF)<<8) | (((x)>>8)&0xFF)))
92 #endif
93 
94 #ifndef swap32
95 # define swap32(x) ((uint32_t)((((x)&0xFF)<<24) \
96  |(((x)>>24)&0xFF) \
97  |(((x)&0x0000FF00)<<8) \
98  |(((x)&0x00FF0000)>>8) ))
99 #endif
100 
101 #ifndef swap64
102 # ifdef HAVE_INT64_T
103 # define byte_in_64bit(n) ((uint64_t)0xff << (n))
104 # define swap64(x) ((uint64_t)((((x)&byte_in_64bit(0))<<56) \
105  |(((x)>>56)&0xFF) \
106  |(((x)&byte_in_64bit(8))<<40) \
107  |(((x)&byte_in_64bit(48))>>40) \
108  |(((x)&byte_in_64bit(16))<<24) \
109  |(((x)&byte_in_64bit(40))>>24) \
110  |(((x)&byte_in_64bit(24))<<8) \
111  |(((x)&byte_in_64bit(32))>>8)))
112 # endif
113 #endif
114 
115 #if SIZEOF_SHORT == 2
116 # define swaps(x) swap16(x)
117 #elif SIZEOF_SHORT == 4
118 # define swaps(x) swap32(x)
119 #else
120  define_swapx(s,short)
121 #endif
122 
123 #if SIZEOF_INT == 2
124 # define swapi(x) swap16(x)
125 #elif SIZEOF_INT == 4
126 # define swapi(x) swap32(x)
127 #else
128  define_swapx(i,int)
129 #endif
130 
131 #if SIZEOF_LONG == 4
132 # define swapl(x) swap32(x)
133 #elif SIZEOF_LONG == 8
134 # define swapl(x) swap64(x)
135 #else
136  define_swapx(l,long)
137 #endif
138 
139 #ifdef HAVE_LONG_LONG
140 # if SIZEOF_LONG_LONG == 8
141 # define swapll(x) swap64(x)
142 # else
143  define_swapx(ll,LONG_LONG)
144 # endif
145 #endif
146 
147 #if SIZEOF_FLOAT == 4 && defined(HAVE_INT32_T)
148 # define swapf(x) swap32(x)
149 # define FLOAT_SWAPPER uint32_t
150 #else
151  define_swapx(f,float)
152 #endif
153 
154 #if SIZEOF_DOUBLE == 8 && defined(HAVE_INT64_T)
155 # define swapd(x) swap64(x)
156 # define DOUBLE_SWAPPER uint64_t
157 #elif SIZEOF_DOUBLE == 8 && defined(HAVE_INT32_T)
158  static double
159  swapd(const double d)
160  {
161  double dtmp = d;
162  uint32_t utmp[2];
163  uint32_t utmp0;
164 
165  utmp[0] = 0; utmp[1] = 0;
166  memcpy(utmp,&dtmp,sizeof(double));
167  utmp0 = utmp[0];
168  utmp[0] = swap32(utmp[1]);
169  utmp[1] = swap32(utmp0);
170  memcpy(&dtmp,utmp,sizeof(double));
171  return dtmp;
172  }
173 #else
174  define_swapx(d, double)
175 #endif
176 
177 #undef define_swapx
178 
179 #define rb_ntohf(x) (BIGENDIAN_P()?(x):swapf(x))
180 #define rb_ntohd(x) (BIGENDIAN_P()?(x):swapd(x))
181 #define rb_htonf(x) (BIGENDIAN_P()?(x):swapf(x))
182 #define rb_htond(x) (BIGENDIAN_P()?(x):swapd(x))
183 #define rb_htovf(x) (BIGENDIAN_P()?swapf(x):(x))
184 #define rb_htovd(x) (BIGENDIAN_P()?swapd(x):(x))
185 #define rb_vtohf(x) (BIGENDIAN_P()?swapf(x):(x))
186 #define rb_vtohd(x) (BIGENDIAN_P()?swapd(x):(x))
187 
188 #ifdef FLOAT_SWAPPER
189 # define FLOAT_CONVWITH(y) FLOAT_SWAPPER y;
190 # define HTONF(x,y) (memcpy(&(y),&(x),sizeof(float)), \
191  (y) = rb_htonf((FLOAT_SWAPPER)(y)), \
192  memcpy(&(x),&(y),sizeof(float)), \
193  (x))
194 # define HTOVF(x,y) (memcpy(&(y),&(x),sizeof(float)), \
195  (y) = rb_htovf((FLOAT_SWAPPER)(y)), \
196  memcpy(&(x),&(y),sizeof(float)), \
197  (x))
198 # define NTOHF(x,y) (memcpy(&(y),&(x),sizeof(float)), \
199  (y) = rb_ntohf((FLOAT_SWAPPER)(y)), \
200  memcpy(&(x),&(y),sizeof(float)), \
201  (x))
202 # define VTOHF(x,y) (memcpy(&(y),&(x),sizeof(float)), \
203  (y) = rb_vtohf((FLOAT_SWAPPER)(y)), \
204  memcpy(&(x),&(y),sizeof(float)), \
205  (x))
206 #else
207 # define FLOAT_CONVWITH(y)
208 # define HTONF(x,y) rb_htonf(x)
209 # define HTOVF(x,y) rb_htovf(x)
210 # define NTOHF(x,y) rb_ntohf(x)
211 # define VTOHF(x,y) rb_vtohf(x)
212 #endif
213 
214 #ifdef DOUBLE_SWAPPER
215 # define DOUBLE_CONVWITH(y) DOUBLE_SWAPPER y;
216 # define HTOND(x,y) (memcpy(&(y),&(x),sizeof(double)), \
217  (y) = rb_htond((DOUBLE_SWAPPER)(y)), \
218  memcpy(&(x),&(y),sizeof(double)), \
219  (x))
220 # define HTOVD(x,y) (memcpy(&(y),&(x),sizeof(double)), \
221  (y) = rb_htovd((DOUBLE_SWAPPER)(y)), \
222  memcpy(&(x),&(y),sizeof(double)), \
223  (x))
224 # define NTOHD(x,y) (memcpy(&(y),&(x),sizeof(double)), \
225  (y) = rb_ntohd((DOUBLE_SWAPPER)(y)), \
226  memcpy(&(x),&(y),sizeof(double)), \
227  (x))
228 # define VTOHD(x,y) (memcpy(&(y),&(x),sizeof(double)), \
229  (y) = rb_vtohd((DOUBLE_SWAPPER)(y)), \
230  memcpy(&(x),&(y),sizeof(double)), \
231  (x))
232 #else
233 # define DOUBLE_CONVWITH(y)
234 # define HTOND(x,y) rb_htond(x)
235 # define HTOVD(x,y) rb_htovd(x)
236 # define NTOHD(x,y) rb_ntohd(x)
237 # define VTOHD(x,y) rb_vtohd(x)
238 #endif
239 
240 static unsigned long
241 num2i32(VALUE x)
242 {
243  x = rb_to_int(x); /* is nil OK? (should not) */
244 
245  if (FIXNUM_P(x)) return FIX2LONG(x);
246  if (RB_TYPE_P(x, T_BIGNUM)) {
247  return rb_big2ulong_pack(x);
248  }
249  rb_raise(rb_eTypeError, "can't convert %s to `integer'", rb_obj_classname(x));
250 
251  UNREACHABLE;
252 }
253 
254 #define MAX_INTEGER_PACK_SIZE 8
255 /* #define FORCE_BIG_PACK */
256 
257 static const char toofew[] = "too few arguments";
258 
259 static void encodes(VALUE,const char*,long,int,int);
260 static void qpencode(VALUE,VALUE,long);
261 
262 static unsigned long utf8_to_uv(const char*,long*);
263 
265 
266 static void
268 {
269  VALUE assoc;
270 
271  assoc = rb_attr_get(str, id_associated);
272  if (RB_TYPE_P(assoc, T_ARRAY)) {
273  /* already associated */
274  rb_ary_concat(assoc, add);
275  }
276  else {
277  rb_ivar_set(str, id_associated, add);
278  }
279 }
280 
281 static VALUE
283 {
284  VALUE assoc = rb_attr_get(str, id_associated);
285  if (NIL_P(assoc)) assoc = Qfalse;
286  return assoc;
287 }
288 
289 /*
290  * call-seq:
291  * arr.pack ( aTemplateString ) -> aBinaryString
292  *
293  * Packs the contents of <i>arr</i> into a binary sequence according to
294  * the directives in <i>aTemplateString</i> (see the table below)
295  * Directives ``A,'' ``a,'' and ``Z'' may be followed by a count,
296  * which gives the width of the resulting field. The remaining
297  * directives also may take a count, indicating the number of array
298  * elements to convert. If the count is an asterisk
299  * (``<code>*</code>''), all remaining array elements will be
300  * converted. Any of the directives ``<code>sSiIlL</code>'' may be
301  * followed by an underscore (``<code>_</code>'') or
302  * exclamation mark (``<code>!</code>'') to use the underlying
303  * platform's native size for the specified type; otherwise, they use a
304  * platform-independent size. Spaces are ignored in the template
305  * string. See also <code>String#unpack</code>.
306  *
307  * a = [ "a", "b", "c" ]
308  * n = [ 65, 66, 67 ]
309  * a.pack("A3A3A3") #=> "a b c "
310  * a.pack("a3a3a3") #=> "a\000\000b\000\000c\000\000"
311  * n.pack("ccc") #=> "ABC"
312  *
313  * Directives for +pack+.
314  *
315  * Integer | Array |
316  * Directive | Element | Meaning
317  * ---------------------------------------------------------------------------
318  * C | Integer | 8-bit unsigned (unsigned char)
319  * S | Integer | 16-bit unsigned, native endian (uint16_t)
320  * L | Integer | 32-bit unsigned, native endian (uint32_t)
321  * Q | Integer | 64-bit unsigned, native endian (uint64_t)
322  * | |
323  * c | Integer | 8-bit signed (signed char)
324  * s | Integer | 16-bit signed, native endian (int16_t)
325  * l | Integer | 32-bit signed, native endian (int32_t)
326  * q | Integer | 64-bit signed, native endian (int64_t)
327  * | |
328  * S_, S! | Integer | unsigned short, native endian
329  * I, I_, I! | Integer | unsigned int, native endian
330  * L_, L! | Integer | unsigned long, native endian
331  * | |
332  * s_, s! | Integer | signed short, native endian
333  * i, i_, i! | Integer | signed int, native endian
334  * l_, l! | Integer | signed long, native endian
335  * | |
336  * S> L> Q> | Integer | same as the directives without ">" except
337  * s> l> q> | | big endian
338  * S!> I!> | | (available since Ruby 1.9.3)
339  * L!> | | "S>" is same as "n"
340  * s!> i!> | | "L>" is same as "N"
341  * l!> | |
342  * | |
343  * S< L< Q< | Integer | same as the directives without "<" except
344  * s< l< q< | | little endian
345  * S!< I!< | | (available since Ruby 1.9.3)
346  * L!< | | "S<" is same as "v"
347  * s!< i!< | | "L<" is same as "V"
348  * l!< | |
349  * | |
350  * n | Integer | 16-bit unsigned, network (big-endian) byte order
351  * N | Integer | 32-bit unsigned, network (big-endian) byte order
352  * v | Integer | 16-bit unsigned, VAX (little-endian) byte order
353  * V | Integer | 32-bit unsigned, VAX (little-endian) byte order
354  * | |
355  * U | Integer | UTF-8 character
356  * w | Integer | BER-compressed integer
357  *
358  * Float | |
359  * Directive | | Meaning
360  * ---------------------------------------------------------------------------
361  * D, d | Float | double-precision, native format
362  * F, f | Float | single-precision, native format
363  * E | Float | double-precision, little-endian byte order
364  * e | Float | single-precision, little-endian byte order
365  * G | Float | double-precision, network (big-endian) byte order
366  * g | Float | single-precision, network (big-endian) byte order
367  *
368  * String | |
369  * Directive | | Meaning
370  * ---------------------------------------------------------------------------
371  * A | String | arbitrary binary string (space padded, count is width)
372  * a | String | arbitrary binary string (null padded, count is width)
373  * Z | String | same as ``a'', except that null is added with *
374  * B | String | bit string (MSB first)
375  * b | String | bit string (LSB first)
376  * H | String | hex string (high nibble first)
377  * h | String | hex string (low nibble first)
378  * u | String | UU-encoded string
379  * M | String | quoted printable, MIME encoding (see RFC2045)
380  * m | String | base64 encoded string (see RFC 2045, count is width)
381  * | | (if count is 0, no line feed are added, see RFC 4648)
382  * P | String | pointer to a structure (fixed-length string)
383  * p | String | pointer to a null-terminated string
384  *
385  * Misc. | |
386  * Directive | | Meaning
387  * ---------------------------------------------------------------------------
388  * @ | --- | moves to absolute position
389  * X | --- | back up a byte
390  * x | --- | null byte
391  */
392 
393 static VALUE
395 {
396  static const char nul10[] = "\0\0\0\0\0\0\0\0\0\0";
397  static const char spc10[] = " ";
398  const char *p, *pend;
399  VALUE res, from, associates = 0;
400  char type;
401  long items, len, idx, plen;
402  const char *ptr;
403  int enc_info = 1; /* 0 - BINARY, 1 - US-ASCII, 2 - UTF-8 */
404 #ifdef NATINT_PACK
405  int natint; /* native integer */
406 #endif
407  int integer_size, bigendian_p;
408 
409  StringValue(fmt);
410  p = RSTRING_PTR(fmt);
411  pend = p + RSTRING_LEN(fmt);
412  res = rb_str_buf_new(0);
413 
414  items = RARRAY_LEN(ary);
415  idx = 0;
416 
417 #define TOO_FEW (rb_raise(rb_eArgError, toofew), 0)
418 #define THISFROM (items > 0 ? RARRAY_PTR(ary)[idx] : TOO_FEW)
419 #define NEXTFROM (items-- > 0 ? RARRAY_PTR(ary)[idx++] : TOO_FEW)
420 
421  while (p < pend) {
422  int explicit_endian = 0;
423  if (RSTRING_PTR(fmt) + RSTRING_LEN(fmt) != pend) {
424  rb_raise(rb_eRuntimeError, "format string modified");
425  }
426  type = *p++; /* get data type */
427 #ifdef NATINT_PACK
428  natint = 0;
429 #endif
430 
431  if (ISSPACE(type)) continue;
432  if (type == '#') {
433  while ((p < pend) && (*p != '\n')) {
434  p++;
435  }
436  continue;
437  }
438 
439  {
440  static const char natstr[] = "sSiIlL";
441  static const char endstr[] = "sSiIlLqQ";
442 
443  modifiers:
444  switch (*p) {
445  case '_':
446  case '!':
447  if (strchr(natstr, type)) {
448 #ifdef NATINT_PACK
449  natint = 1;
450 #endif
451  p++;
452  }
453  else {
454  rb_raise(rb_eArgError, "'%c' allowed only after types %s", *p, natstr);
455  }
456  goto modifiers;
457 
458  case '<':
459  case '>':
460  if (!strchr(endstr, type)) {
461  rb_raise(rb_eArgError, "'%c' allowed only after types %s", *p, endstr);
462  }
463  if (explicit_endian) {
464  rb_raise(rb_eRangeError, "Can't use both '<' and '>'");
465  }
466  explicit_endian = *p++;
467  goto modifiers;
468  }
469  }
470 
471  if (*p == '*') { /* set data length */
472  len = strchr("@Xxu", type) ? 0
473  : strchr("PMm", type) ? 1
474  : items;
475  p++;
476  }
477  else if (ISDIGIT(*p)) {
478  errno = 0;
479  len = STRTOUL(p, (char**)&p, 10);
480  if (errno) {
481  rb_raise(rb_eRangeError, "pack length too big");
482  }
483  }
484  else {
485  len = 1;
486  }
487 
488  switch (type) {
489  case 'U':
490  /* if encoding is US-ASCII, upgrade to UTF-8 */
491  if (enc_info == 1) enc_info = 2;
492  break;
493  case 'm': case 'M': case 'u':
494  /* keep US-ASCII (do nothing) */
495  break;
496  default:
497  /* fall back to BINARY */
498  enc_info = 0;
499  break;
500  }
501  switch (type) {
502  case 'A': case 'a': case 'Z':
503  case 'B': case 'b':
504  case 'H': case 'h':
505  from = NEXTFROM;
506  if (NIL_P(from)) {
507  ptr = "";
508  plen = 0;
509  }
510  else {
511  StringValue(from);
512  ptr = RSTRING_PTR(from);
513  plen = RSTRING_LEN(from);
514  OBJ_INFECT(res, from);
515  }
516 
517  if (p[-1] == '*')
518  len = plen;
519 
520  switch (type) {
521  case 'a': /* arbitrary binary string (null padded) */
522  case 'A': /* arbitrary binary string (ASCII space padded) */
523  case 'Z': /* null terminated string */
524  if (plen >= len) {
525  rb_str_buf_cat(res, ptr, len);
526  if (p[-1] == '*' && type == 'Z')
527  rb_str_buf_cat(res, nul10, 1);
528  }
529  else {
530  rb_str_buf_cat(res, ptr, plen);
531  len -= plen;
532  while (len >= 10) {
533  rb_str_buf_cat(res, (type == 'A')?spc10:nul10, 10);
534  len -= 10;
535  }
536  rb_str_buf_cat(res, (type == 'A')?spc10:nul10, len);
537  }
538  break;
539 
540 #define castchar(from) (char)((from) & 0xff)
541 
542  case 'b': /* bit string (ascending) */
543  {
544  int byte = 0;
545  long i, j = 0;
546 
547  if (len > plen) {
548  j = (len - plen + 1)/2;
549  len = plen;
550  }
551  for (i=0; i++ < len; ptr++) {
552  if (*ptr & 1)
553  byte |= 128;
554  if (i & 7)
555  byte >>= 1;
556  else {
557  char c = castchar(byte);
558  rb_str_buf_cat(res, &c, 1);
559  byte = 0;
560  }
561  }
562  if (len & 7) {
563  char c;
564  byte >>= 7 - (len & 7);
565  c = castchar(byte);
566  rb_str_buf_cat(res, &c, 1);
567  }
568  len = j;
569  goto grow;
570  }
571  break;
572 
573  case 'B': /* bit string (descending) */
574  {
575  int byte = 0;
576  long i, j = 0;
577 
578  if (len > plen) {
579  j = (len - plen + 1)/2;
580  len = plen;
581  }
582  for (i=0; i++ < len; ptr++) {
583  byte |= *ptr & 1;
584  if (i & 7)
585  byte <<= 1;
586  else {
587  char c = castchar(byte);
588  rb_str_buf_cat(res, &c, 1);
589  byte = 0;
590  }
591  }
592  if (len & 7) {
593  char c;
594  byte <<= 7 - (len & 7);
595  c = castchar(byte);
596  rb_str_buf_cat(res, &c, 1);
597  }
598  len = j;
599  goto grow;
600  }
601  break;
602 
603  case 'h': /* hex string (low nibble first) */
604  {
605  int byte = 0;
606  long i, j = 0;
607 
608  if (len > plen) {
609  j = (len + 1) / 2 - (plen + 1) / 2;
610  len = plen;
611  }
612  for (i=0; i++ < len; ptr++) {
613  if (ISALPHA(*ptr))
614  byte |= (((*ptr & 15) + 9) & 15) << 4;
615  else
616  byte |= (*ptr & 15) << 4;
617  if (i & 1)
618  byte >>= 4;
619  else {
620  char c = castchar(byte);
621  rb_str_buf_cat(res, &c, 1);
622  byte = 0;
623  }
624  }
625  if (len & 1) {
626  char c = castchar(byte);
627  rb_str_buf_cat(res, &c, 1);
628  }
629  len = j;
630  goto grow;
631  }
632  break;
633 
634  case 'H': /* hex string (high nibble first) */
635  {
636  int byte = 0;
637  long i, j = 0;
638 
639  if (len > plen) {
640  j = (len + 1) / 2 - (plen + 1) / 2;
641  len = plen;
642  }
643  for (i=0; i++ < len; ptr++) {
644  if (ISALPHA(*ptr))
645  byte |= ((*ptr & 15) + 9) & 15;
646  else
647  byte |= *ptr & 15;
648  if (i & 1)
649  byte <<= 4;
650  else {
651  char c = castchar(byte);
652  rb_str_buf_cat(res, &c, 1);
653  byte = 0;
654  }
655  }
656  if (len & 1) {
657  char c = castchar(byte);
658  rb_str_buf_cat(res, &c, 1);
659  }
660  len = j;
661  goto grow;
662  }
663  break;
664  }
665  break;
666 
667  case 'c': /* signed char */
668  case 'C': /* unsigned char */
669  while (len-- > 0) {
670  char c;
671 
672  from = NEXTFROM;
673  c = (char)num2i32(from);
674  rb_str_buf_cat(res, &c, sizeof(char));
675  }
676  break;
677 
678  case 's': /* signed short */
679  integer_size = NATINT_LEN(short, 2);
680  bigendian_p = BIGENDIAN_P();
681  goto pack_integer;
682 
683  case 'S': /* unsigned short */
684  integer_size = NATINT_LEN(short, 2);
685  bigendian_p = BIGENDIAN_P();
686  goto pack_integer;
687 
688  case 'i': /* signed int */
689  integer_size = (int)sizeof(int);
690  bigendian_p = BIGENDIAN_P();
691  goto pack_integer;
692 
693  case 'I': /* unsigned int */
694  integer_size = (int)sizeof(int);
695  bigendian_p = BIGENDIAN_P();
696  goto pack_integer;
697 
698  case 'l': /* signed long */
699  integer_size = NATINT_LEN(long, 4);
700  bigendian_p = BIGENDIAN_P();
701  goto pack_integer;
702 
703  case 'L': /* unsigned long */
704  integer_size = NATINT_LEN(long, 4);
705  bigendian_p = BIGENDIAN_P();
706  goto pack_integer;
707 
708  case 'q': /* signed quad (64bit) int */
709  integer_size = 8;
710  bigendian_p = BIGENDIAN_P();
711  goto pack_integer;
712 
713  case 'Q': /* unsigned quad (64bit) int */
714  integer_size = 8;
715  bigendian_p = BIGENDIAN_P();
716  goto pack_integer;
717 
718  case 'n': /* unsigned short (network byte-order) */
719  integer_size = 2;
720  bigendian_p = 1;
721  goto pack_integer;
722 
723  case 'N': /* unsigned long (network byte-order) */
724  integer_size = 4;
725  bigendian_p = 1;
726  goto pack_integer;
727 
728  case 'v': /* unsigned short (VAX byte-order) */
729  integer_size = 2;
730  bigendian_p = 0;
731  goto pack_integer;
732 
733  case 'V': /* unsigned long (VAX byte-order) */
734  integer_size = 4;
735  bigendian_p = 0;
736  goto pack_integer;
737 
738  pack_integer:
739  if (explicit_endian) {
740  bigendian_p = explicit_endian == '>';
741  }
742 
743  switch (integer_size) {
744 #if defined(HAVE_INT16_T) && !defined(FORCE_BIG_PACK)
745  case SIZEOF_INT16_T:
746  while (len-- > 0) {
747  union {
748  int16_t i;
749  char a[sizeof(int16_t)];
750  } v;
751 
752  from = NEXTFROM;
753  v.i = (int16_t)num2i32(from);
754  if (bigendian_p != BIGENDIAN_P()) v.i = swap16(v.i);
755  rb_str_buf_cat(res, v.a, sizeof(int16_t));
756  }
757  break;
758 #endif
759 
760 #if defined(HAVE_INT32_T) && !defined(FORCE_BIG_PACK)
761  case SIZEOF_INT32_T:
762  while (len-- > 0) {
763  union {
764  int32_t i;
765  char a[sizeof(int32_t)];
766  } v;
767 
768  from = NEXTFROM;
769  v.i = (int32_t)num2i32(from);
770  if (bigendian_p != BIGENDIAN_P()) v.i = swap32(v.i);
771  rb_str_buf_cat(res, v.a, sizeof(int32_t));
772  }
773  break;
774 #endif
775 
776 #if defined(HAVE_INT64_T) && SIZEOF_LONG == SIZEOF_INT64_T && !defined(FORCE_BIG_PACK)
777  case SIZEOF_INT64_T:
778  while (len-- > 0) {
779  union {
780  int64_t i;
781  char a[sizeof(int64_t)];
782  } v;
783 
784  from = NEXTFROM;
785  v.i = num2i32(from); /* can return 64bit value if SIZEOF_LONG == SIZEOF_INT64_T */
786  if (bigendian_p != BIGENDIAN_P()) v.i = swap64(v.i);
787  rb_str_buf_cat(res, v.a, sizeof(int64_t));
788  }
789  break;
790 #endif
791 
792  default:
793  if (integer_size > MAX_INTEGER_PACK_SIZE)
794  rb_bug("unexpected intger size for pack: %d", integer_size);
795  while (len-- > 0) {
796  union {
797  unsigned long i[(MAX_INTEGER_PACK_SIZE+SIZEOF_LONG-1)/SIZEOF_LONG];
798  char a[(MAX_INTEGER_PACK_SIZE+SIZEOF_LONG-1)/SIZEOF_LONG*SIZEOF_LONG];
799  } v;
800  int num_longs = (integer_size+SIZEOF_LONG-1)/SIZEOF_LONG;
801  int i;
802 
803  from = NEXTFROM;
804  rb_big_pack(from, v.i, num_longs);
805  if (bigendian_p) {
806  for (i = 0; i < num_longs/2; i++) {
807  unsigned long t = v.i[i];
808  v.i[i] = v.i[num_longs-1-i];
809  v.i[num_longs-1-i] = t;
810  }
811  }
812  if (bigendian_p != BIGENDIAN_P()) {
813  for (i = 0; i < num_longs; i++)
814  v.i[i] = swapl(v.i[i]);
815  }
816  rb_str_buf_cat(res,
817  bigendian_p ?
818  v.a + sizeof(long)*num_longs - integer_size :
819  v.a,
820  integer_size);
821  }
822  break;
823  }
824  break;
825 
826  case 'f': /* single precision float in native format */
827  case 'F': /* ditto */
828  while (len-- > 0) {
829  float f;
830 
831  from = NEXTFROM;
832  f = (float)RFLOAT_VALUE(rb_to_float(from));
833  rb_str_buf_cat(res, (char*)&f, sizeof(float));
834  }
835  break;
836 
837  case 'e': /* single precision float in VAX byte-order */
838  while (len-- > 0) {
839  float f;
840  FLOAT_CONVWITH(ftmp);
841 
842  from = NEXTFROM;
843  f = (float)RFLOAT_VALUE(rb_to_float(from));
844  f = HTOVF(f,ftmp);
845  rb_str_buf_cat(res, (char*)&f, sizeof(float));
846  }
847  break;
848 
849  case 'E': /* double precision float in VAX byte-order */
850  while (len-- > 0) {
851  double d;
852  DOUBLE_CONVWITH(dtmp);
853 
854  from = NEXTFROM;
855  d = RFLOAT_VALUE(rb_to_float(from));
856  d = HTOVD(d,dtmp);
857  rb_str_buf_cat(res, (char*)&d, sizeof(double));
858  }
859  break;
860 
861  case 'd': /* double precision float in native format */
862  case 'D': /* ditto */
863  while (len-- > 0) {
864  double d;
865 
866  from = NEXTFROM;
867  d = RFLOAT_VALUE(rb_to_float(from));
868  rb_str_buf_cat(res, (char*)&d, sizeof(double));
869  }
870  break;
871 
872  case 'g': /* single precision float in network byte-order */
873  while (len-- > 0) {
874  float f;
875  FLOAT_CONVWITH(ftmp);
876 
877  from = NEXTFROM;
878  f = (float)RFLOAT_VALUE(rb_to_float(from));
879  f = HTONF(f,ftmp);
880  rb_str_buf_cat(res, (char*)&f, sizeof(float));
881  }
882  break;
883 
884  case 'G': /* double precision float in network byte-order */
885  while (len-- > 0) {
886  double d;
887  DOUBLE_CONVWITH(dtmp);
888 
889  from = NEXTFROM;
890  d = RFLOAT_VALUE(rb_to_float(from));
891  d = HTOND(d,dtmp);
892  rb_str_buf_cat(res, (char*)&d, sizeof(double));
893  }
894  break;
895 
896  case 'x': /* null byte */
897  grow:
898  while (len >= 10) {
899  rb_str_buf_cat(res, nul10, 10);
900  len -= 10;
901  }
902  rb_str_buf_cat(res, nul10, len);
903  break;
904 
905  case 'X': /* back up byte */
906  shrink:
907  plen = RSTRING_LEN(res);
908  if (plen < len)
909  rb_raise(rb_eArgError, "X outside of string");
910  rb_str_set_len(res, plen - len);
911  break;
912 
913  case '@': /* null fill to absolute position */
914  len -= RSTRING_LEN(res);
915  if (len > 0) goto grow;
916  len = -len;
917  if (len > 0) goto shrink;
918  break;
919 
920  case '%':
921  rb_raise(rb_eArgError, "%% is not supported");
922  break;
923 
924  case 'U': /* Unicode character */
925  while (len-- > 0) {
926  SIGNED_VALUE l;
927  char buf[8];
928  int le;
929 
930  from = NEXTFROM;
931  from = rb_to_int(from);
932  l = NUM2LONG(from);
933  if (l < 0) {
934  rb_raise(rb_eRangeError, "pack(U): value out of range");
935  }
936  le = rb_uv_to_utf8(buf, l);
937  rb_str_buf_cat(res, (char*)buf, le);
938  }
939  break;
940 
941  case 'u': /* uuencoded string */
942  case 'm': /* base64 encoded string */
943  from = NEXTFROM;
944  StringValue(from);
945  ptr = RSTRING_PTR(from);
946  plen = RSTRING_LEN(from);
947 
948  if (len == 0 && type == 'm') {
949  encodes(res, ptr, plen, type, 0);
950  ptr += plen;
951  break;
952  }
953  if (len <= 2)
954  len = 45;
955  else if (len > 63 && type == 'u')
956  len = 63;
957  else
958  len = len / 3 * 3;
959  while (plen > 0) {
960  long todo;
961 
962  if (plen > len)
963  todo = len;
964  else
965  todo = plen;
966  encodes(res, ptr, todo, type, 1);
967  plen -= todo;
968  ptr += todo;
969  }
970  break;
971 
972  case 'M': /* quoted-printable encoded string */
973  from = rb_obj_as_string(NEXTFROM);
974  if (len <= 1)
975  len = 72;
976  qpencode(res, from, len);
977  break;
978 
979  case 'P': /* pointer to packed byte string */
980  from = THISFROM;
981  if (!NIL_P(from)) {
982  StringValue(from);
983  if (RSTRING_LEN(from) < len) {
984  rb_raise(rb_eArgError, "too short buffer for P(%ld for %ld)",
985  RSTRING_LEN(from), len);
986  }
987  }
988  len = 1;
989  /* FALL THROUGH */
990  case 'p': /* pointer to string */
991  while (len-- > 0) {
992  char *t;
993  from = NEXTFROM;
994  if (NIL_P(from)) {
995  t = 0;
996  }
997  else {
998  t = StringValuePtr(from);
999  }
1000  if (!associates) {
1001  associates = rb_ary_new();
1002  }
1003  rb_ary_push(associates, from);
1004  rb_obj_taint(from);
1005  rb_str_buf_cat(res, (char*)&t, sizeof(char*));
1006  }
1007  break;
1008 
1009  case 'w': /* BER compressed integer */
1010  while (len-- > 0) {
1011  unsigned long ul;
1012  VALUE buf = rb_str_new(0, 0);
1013  char c, *bufs, *bufe;
1014 
1015  from = NEXTFROM;
1016  if (RB_TYPE_P(from, T_BIGNUM)) {
1017  VALUE big128 = rb_uint2big(128);
1018  while (RB_TYPE_P(from, T_BIGNUM)) {
1019  from = rb_big_divmod(from, big128);
1020  c = castchar(NUM2INT(RARRAY_PTR(from)[1]) | 0x80); /* mod */
1021  rb_str_buf_cat(buf, &c, sizeof(char));
1022  from = RARRAY_PTR(from)[0]; /* div */
1023  }
1024  }
1025 
1026  {
1027  long l = NUM2LONG(from);
1028  if (l < 0) {
1029  rb_raise(rb_eArgError, "can't compress negative numbers");
1030  }
1031  ul = l;
1032  }
1033 
1034  while (ul) {
1035  c = castchar((ul & 0x7f) | 0x80);
1036  rb_str_buf_cat(buf, &c, sizeof(char));
1037  ul >>= 7;
1038  }
1039 
1040  if (RSTRING_LEN(buf)) {
1041  bufs = RSTRING_PTR(buf);
1042  bufe = bufs + RSTRING_LEN(buf) - 1;
1043  *bufs &= 0x7f; /* clear continue bit */
1044  while (bufs < bufe) { /* reverse */
1045  c = *bufs;
1046  *bufs++ = *bufe;
1047  *bufe-- = c;
1048  }
1049  rb_str_buf_cat(res, RSTRING_PTR(buf), RSTRING_LEN(buf));
1050  }
1051  else {
1052  c = 0;
1053  rb_str_buf_cat(res, &c, sizeof(char));
1054  }
1055  }
1056  break;
1057 
1058  default:
1059  rb_warning("unknown pack directive '%c' in '%s'",
1060  type, RSTRING_PTR(fmt));
1061  break;
1062  }
1063  }
1064 
1065  if (associates) {
1066  str_associate(res, associates);
1067  }
1068  OBJ_INFECT(res, fmt);
1069  switch (enc_info) {
1070  case 1:
1072  break;
1073  case 2:
1075  break;
1076  default:
1077  /* do nothing, keep ASCII-8BIT */
1078  break;
1079  }
1080  return res;
1081 }
1082 
1083 static const char uu_table[] =
1084 "`!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
1085 static const char b64_table[] =
1086 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1087 
1088 static void
1089 encodes(VALUE str, const char *s, long len, int type, int tail_lf)
1090 {
1091  enum {buff_size = 4096, encoded_unit = 4};
1092  char buff[buff_size + 1]; /* +1 for tail_lf */
1093  long i = 0;
1094  const char *trans = type == 'u' ? uu_table : b64_table;
1095  char padding;
1096 
1097  if (type == 'u') {
1098  buff[i++] = (char)len + ' ';
1099  padding = '`';
1100  }
1101  else {
1102  padding = '=';
1103  }
1104  while (len >= 3) {
1105  while (len >= 3 && buff_size-i >= encoded_unit) {
1106  buff[i++] = trans[077 & (*s >> 2)];
1107  buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
1108  buff[i++] = trans[077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03))];
1109  buff[i++] = trans[077 & s[2]];
1110  s += 3;
1111  len -= 3;
1112  }
1113  if (buff_size-i < encoded_unit) {
1114  rb_str_buf_cat(str, buff, i);
1115  i = 0;
1116  }
1117  }
1118 
1119  if (len == 2) {
1120  buff[i++] = trans[077 & (*s >> 2)];
1121  buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
1122  buff[i++] = trans[077 & (((s[1] << 2) & 074) | (('\0' >> 6) & 03))];
1123  buff[i++] = padding;
1124  }
1125  else if (len == 1) {
1126  buff[i++] = trans[077 & (*s >> 2)];
1127  buff[i++] = trans[077 & (((*s << 4) & 060) | (('\0' >> 4) & 017))];
1128  buff[i++] = padding;
1129  buff[i++] = padding;
1130  }
1131  if (tail_lf) buff[i++] = '\n';
1132  rb_str_buf_cat(str, buff, i);
1133  if ((size_t)i > sizeof(buff)) rb_bug("encodes() buffer overrun");
1134 }
1135 
1136 static const char hex_table[] = "0123456789ABCDEF";
1137 
1138 static void
1139 qpencode(VALUE str, VALUE from, long len)
1140 {
1141  char buff[1024];
1142  long i = 0, n = 0, prev = EOF;
1143  unsigned char *s = (unsigned char*)RSTRING_PTR(from);
1144  unsigned char *send = s + RSTRING_LEN(from);
1145 
1146  while (s < send) {
1147  if ((*s > 126) ||
1148  (*s < 32 && *s != '\n' && *s != '\t') ||
1149  (*s == '=')) {
1150  buff[i++] = '=';
1151  buff[i++] = hex_table[*s >> 4];
1152  buff[i++] = hex_table[*s & 0x0f];
1153  n += 3;
1154  prev = EOF;
1155  }
1156  else if (*s == '\n') {
1157  if (prev == ' ' || prev == '\t') {
1158  buff[i++] = '=';
1159  buff[i++] = *s;
1160  }
1161  buff[i++] = *s;
1162  n = 0;
1163  prev = *s;
1164  }
1165  else {
1166  buff[i++] = *s;
1167  n++;
1168  prev = *s;
1169  }
1170  if (n > len) {
1171  buff[i++] = '=';
1172  buff[i++] = '\n';
1173  n = 0;
1174  prev = '\n';
1175  }
1176  if (i > 1024 - 5) {
1177  rb_str_buf_cat(str, buff, i);
1178  i = 0;
1179  }
1180  s++;
1181  }
1182  if (n > 0) {
1183  buff[i++] = '=';
1184  buff[i++] = '\n';
1185  }
1186  if (i > 0) {
1187  rb_str_buf_cat(str, buff, i);
1188  }
1189 }
1190 
1191 static inline int
1192 hex2num(char c)
1193 {
1194  switch (c) {
1195  case '0': case '1': case '2': case '3': case '4':
1196  case '5': case '6': case '7': case '8': case '9':
1197  return c - '0';
1198  case 'a': case 'b': case 'c':
1199  case 'd': case 'e': case 'f':
1200  return c - 'a' + 10;
1201  case 'A': case 'B': case 'C':
1202  case 'D': case 'E': case 'F':
1203  return c - 'A' + 10;
1204  default:
1205  return -1;
1206  }
1207 }
1208 
1209 #define PACK_LENGTH_ADJUST_SIZE(sz) do { \
1210  tmp_len = 0; \
1211  if (len > (long)((send-s)/(sz))) { \
1212  if (!star) { \
1213  tmp_len = len-(send-s)/(sz); \
1214  } \
1215  len = (send-s)/(sz); \
1216  } \
1217 } while (0)
1218 
1219 #define PACK_ITEM_ADJUST() do { \
1220  if (tmp_len > 0 && !block_p) \
1221  rb_ary_store(ary, RARRAY_LEN(ary)+tmp_len-1, Qnil); \
1222 } while (0)
1223 
1224 static VALUE
1225 infected_str_new(const char *ptr, long len, VALUE str)
1226 {
1227  VALUE s = rb_str_new(ptr, len);
1228 
1229  OBJ_INFECT(s, str);
1230  return s;
1231 }
1232 
1233 /*
1234  * call-seq:
1235  * str.unpack(format) -> anArray
1236  *
1237  * Decodes <i>str</i> (which may contain binary data) according to the
1238  * format string, returning an array of each value extracted. The
1239  * format string consists of a sequence of single-character directives,
1240  * summarized in the table at the end of this entry.
1241  * Each directive may be followed
1242  * by a number, indicating the number of times to repeat with this
1243  * directive. An asterisk (``<code>*</code>'') will use up all
1244  * remaining elements. The directives <code>sSiIlL</code> may each be
1245  * followed by an underscore (``<code>_</code>'') or
1246  * exclamation mark (``<code>!</code>'') to use the underlying
1247  * platform's native size for the specified type; otherwise, it uses a
1248  * platform-independent consistent size. Spaces are ignored in the
1249  * format string. See also <code>Array#pack</code>.
1250  *
1251  * "abc \0\0abc \0\0".unpack('A6Z6') #=> ["abc", "abc "]
1252  * "abc \0\0".unpack('a3a3') #=> ["abc", " \000\000"]
1253  * "abc \0abc \0".unpack('Z*Z*') #=> ["abc ", "abc "]
1254  * "aa".unpack('b8B8') #=> ["10000110", "01100001"]
1255  * "aaa".unpack('h2H2c') #=> ["16", "61", 97]
1256  * "\xfe\xff\xfe\xff".unpack('sS') #=> [-2, 65534]
1257  * "now=20is".unpack('M*') #=> ["now is"]
1258  * "whole".unpack('xax2aX2aX1aX2a') #=> ["h", "e", "l", "l", "o"]
1259  *
1260  * This table summarizes the various formats and the Ruby classes
1261  * returned by each.
1262  *
1263  * Integer | |
1264  * Directive | Returns | Meaning
1265  * -----------------------------------------------------------------
1266  * C | Integer | 8-bit unsigned (unsigned char)
1267  * S | Integer | 16-bit unsigned, native endian (uint16_t)
1268  * L | Integer | 32-bit unsigned, native endian (uint32_t)
1269  * Q | Integer | 64-bit unsigned, native endian (uint64_t)
1270  * | |
1271  * c | Integer | 8-bit signed (signed char)
1272  * s | Integer | 16-bit signed, native endian (int16_t)
1273  * l | Integer | 32-bit signed, native endian (int32_t)
1274  * q | Integer | 64-bit signed, native endian (int64_t)
1275  * | |
1276  * S_, S! | Integer | unsigned short, native endian
1277  * I, I_, I! | Integer | unsigned int, native endian
1278  * L_, L! | Integer | unsigned long, native endian
1279  * | |
1280  * s_, s! | Integer | signed short, native endian
1281  * i, i_, i! | Integer | signed int, native endian
1282  * l_, l! | Integer | signed long, native endian
1283  * | |
1284  * S> L> Q> | Integer | same as the directives without ">" except
1285  * s> l> q> | | big endian
1286  * S!> I!> | | (available since Ruby 1.9.3)
1287  * L!> Q!> | | "S>" is same as "n"
1288  * s!> i!> | | "L>" is same as "N"
1289  * l!> q!> | |
1290  * | |
1291  * S< L< Q< | Integer | same as the directives without "<" except
1292  * s< l< q< | | little endian
1293  * S!< I!< | | (available since Ruby 1.9.3)
1294  * L!< Q!< | | "S<" is same as "v"
1295  * s!< i!< | | "L<" is same as "V"
1296  * l!< q!< | |
1297  * | |
1298  * n | Integer | 16-bit unsigned, network (big-endian) byte order
1299  * N | Integer | 32-bit unsigned, network (big-endian) byte order
1300  * v | Integer | 16-bit unsigned, VAX (little-endian) byte order
1301  * V | Integer | 32-bit unsigned, VAX (little-endian) byte order
1302  * | |
1303  * U | Integer | UTF-8 character
1304  * w | Integer | BER-compressed integer (see Array.pack)
1305  *
1306  * Float | |
1307  * Directive | Returns | Meaning
1308  * -----------------------------------------------------------------
1309  * D, d | Float | double-precision, native format
1310  * F, f | Float | single-precision, native format
1311  * E | Float | double-precision, little-endian byte order
1312  * e | Float | single-precision, little-endian byte order
1313  * G | Float | double-precision, network (big-endian) byte order
1314  * g | Float | single-precision, network (big-endian) byte order
1315  *
1316  * String | |
1317  * Directive | Returns | Meaning
1318  * -----------------------------------------------------------------
1319  * A | String | arbitrary binary string (remove trailing nulls and ASCII spaces)
1320  * a | String | arbitrary binary string
1321  * Z | String | null-terminated string
1322  * B | String | bit string (MSB first)
1323  * b | String | bit string (LSB first)
1324  * H | String | hex string (high nibble first)
1325  * h | String | hex string (low nibble first)
1326  * u | String | UU-encoded string
1327  * M | String | quoted-printable, MIME encoding (see RFC2045)
1328  * m | String | base64 encoded string (RFC 2045) (default)
1329  * | | base64 encoded string (RFC 4648) if followed by 0
1330  * P | String | pointer to a structure (fixed-length string)
1331  * p | String | pointer to a null-terminated string
1332  *
1333  * Misc. | |
1334  * Directive | Returns | Meaning
1335  * -----------------------------------------------------------------
1336  * @ | --- | skip to the offset given by the length argument
1337  * X | --- | skip backward one byte
1338  * x | --- | skip forward one byte
1339  */
1340 
1341 static VALUE
1343 {
1344  static const char hexdigits[] = "0123456789abcdef";
1345  char *s, *send;
1346  char *p, *pend;
1347  VALUE ary;
1348  char type;
1349  long len, tmp_len;
1350  int star;
1351 #ifdef NATINT_PACK
1352  int natint; /* native integer */
1353 #endif
1354  int block_p = rb_block_given_p();
1355  int signed_p, integer_size, bigendian_p;
1356 #define UNPACK_PUSH(item) do {\
1357  VALUE item_val = (item);\
1358  if (block_p) {\
1359  rb_yield(item_val);\
1360  }\
1361  else {\
1362  rb_ary_push(ary, item_val);\
1363  }\
1364  } while (0)
1365 
1366  StringValue(str);
1367  StringValue(fmt);
1368  s = RSTRING_PTR(str);
1369  send = s + RSTRING_LEN(str);
1370  p = RSTRING_PTR(fmt);
1371  pend = p + RSTRING_LEN(fmt);
1372 
1373  ary = block_p ? Qnil : rb_ary_new();
1374  while (p < pend) {
1375  int explicit_endian = 0;
1376  type = *p++;
1377 #ifdef NATINT_PACK
1378  natint = 0;
1379 #endif
1380 
1381  if (ISSPACE(type)) continue;
1382  if (type == '#') {
1383  while ((p < pend) && (*p != '\n')) {
1384  p++;
1385  }
1386  continue;
1387  }
1388 
1389  star = 0;
1390  {
1391  static const char natstr[] = "sSiIlL";
1392  static const char endstr[] = "sSiIlLqQ";
1393 
1394  modifiers:
1395  switch (*p) {
1396  case '_':
1397  case '!':
1398 
1399  if (strchr(natstr, type)) {
1400 #ifdef NATINT_PACK
1401  natint = 1;
1402 #endif
1403  p++;
1404  }
1405  else {
1406  rb_raise(rb_eArgError, "'%c' allowed only after types %s", *p, natstr);
1407  }
1408  goto modifiers;
1409 
1410  case '<':
1411  case '>':
1412  if (!strchr(endstr, type)) {
1413  rb_raise(rb_eArgError, "'%c' allowed only after types %s", *p, endstr);
1414  }
1415  if (explicit_endian) {
1416  rb_raise(rb_eRangeError, "Can't use both '<' and '>'");
1417  }
1418  explicit_endian = *p++;
1419  goto modifiers;
1420  }
1421  }
1422 
1423  if (p >= pend)
1424  len = 1;
1425  else if (*p == '*') {
1426  star = 1;
1427  len = send - s;
1428  p++;
1429  }
1430  else if (ISDIGIT(*p)) {
1431  errno = 0;
1432  len = STRTOUL(p, (char**)&p, 10);
1433  if (errno) {
1434  rb_raise(rb_eRangeError, "pack length too big");
1435  }
1436  }
1437  else {
1438  len = (type != '@');
1439  }
1440 
1441  switch (type) {
1442  case '%':
1443  rb_raise(rb_eArgError, "%% is not supported");
1444  break;
1445 
1446  case 'A':
1447  if (len > send - s) len = send - s;
1448  {
1449  long end = len;
1450  char *t = s + len - 1;
1451 
1452  while (t >= s) {
1453  if (*t != ' ' && *t != '\0') break;
1454  t--; len--;
1455  }
1456  UNPACK_PUSH(infected_str_new(s, len, str));
1457  s += end;
1458  }
1459  break;
1460 
1461  case 'Z':
1462  {
1463  char *t = s;
1464 
1465  if (len > send-s) len = send-s;
1466  while (t < s+len && *t) t++;
1467  UNPACK_PUSH(infected_str_new(s, t-s, str));
1468  if (t < send) t++;
1469  s = star ? t : s+len;
1470  }
1471  break;
1472 
1473  case 'a':
1474  if (len > send - s) len = send - s;
1475  UNPACK_PUSH(infected_str_new(s, len, str));
1476  s += len;
1477  break;
1478 
1479  case 'b':
1480  {
1481  VALUE bitstr;
1482  char *t;
1483  int bits;
1484  long i;
1485 
1486  if (p[-1] == '*' || len > (send - s) * 8)
1487  len = (send - s) * 8;
1488  bits = 0;
1489  UNPACK_PUSH(bitstr = rb_usascii_str_new(0, len));
1490  t = RSTRING_PTR(bitstr);
1491  for (i=0; i<len; i++) {
1492  if (i & 7) bits >>= 1;
1493  else bits = *s++;
1494  *t++ = (bits & 1) ? '1' : '0';
1495  }
1496  }
1497  break;
1498 
1499  case 'B':
1500  {
1501  VALUE bitstr;
1502  char *t;
1503  int bits;
1504  long i;
1505 
1506  if (p[-1] == '*' || len > (send - s) * 8)
1507  len = (send - s) * 8;
1508  bits = 0;
1509  UNPACK_PUSH(bitstr = rb_usascii_str_new(0, len));
1510  t = RSTRING_PTR(bitstr);
1511  for (i=0; i<len; i++) {
1512  if (i & 7) bits <<= 1;
1513  else bits = *s++;
1514  *t++ = (bits & 128) ? '1' : '0';
1515  }
1516  }
1517  break;
1518 
1519  case 'h':
1520  {
1521  VALUE bitstr;
1522  char *t;
1523  int bits;
1524  long i;
1525 
1526  if (p[-1] == '*' || len > (send - s) * 2)
1527  len = (send - s) * 2;
1528  bits = 0;
1529  UNPACK_PUSH(bitstr = rb_usascii_str_new(0, len));
1530  t = RSTRING_PTR(bitstr);
1531  for (i=0; i<len; i++) {
1532  if (i & 1)
1533  bits >>= 4;
1534  else
1535  bits = *s++;
1536  *t++ = hexdigits[bits & 15];
1537  }
1538  }
1539  break;
1540 
1541  case 'H':
1542  {
1543  VALUE bitstr;
1544  char *t;
1545  int bits;
1546  long i;
1547 
1548  if (p[-1] == '*' || len > (send - s) * 2)
1549  len = (send - s) * 2;
1550  bits = 0;
1551  UNPACK_PUSH(bitstr = rb_usascii_str_new(0, len));
1552  t = RSTRING_PTR(bitstr);
1553  for (i=0; i<len; i++) {
1554  if (i & 1)
1555  bits <<= 4;
1556  else
1557  bits = *s++;
1558  *t++ = hexdigits[(bits >> 4) & 15];
1559  }
1560  }
1561  break;
1562 
1563  case 'c':
1564  PACK_LENGTH_ADJUST_SIZE(sizeof(char));
1565  while (len-- > 0) {
1566  int c = *s++;
1567  if (c > (char)127) c-=256;
1568  UNPACK_PUSH(INT2FIX(c));
1569  }
1570  PACK_ITEM_ADJUST();
1571  break;
1572 
1573  case 'C':
1574  PACK_LENGTH_ADJUST_SIZE(sizeof(unsigned char));
1575  while (len-- > 0) {
1576  unsigned char c = *s++;
1577  UNPACK_PUSH(INT2FIX(c));
1578  }
1579  PACK_ITEM_ADJUST();
1580  break;
1581 
1582  case 's':
1583  signed_p = 1;
1584  integer_size = NATINT_LEN(short, 2);
1585  bigendian_p = BIGENDIAN_P();
1586  goto unpack_integer;
1587 
1588  case 'S':
1589  signed_p = 0;
1590  integer_size = NATINT_LEN(short, 2);
1591  bigendian_p = BIGENDIAN_P();
1592  goto unpack_integer;
1593 
1594  case 'i':
1595  signed_p = 1;
1596  integer_size = (int)sizeof(int);
1597  bigendian_p = BIGENDIAN_P();
1598  goto unpack_integer;
1599 
1600  case 'I':
1601  signed_p = 0;
1602  integer_size = (int)sizeof(int);
1603  bigendian_p = BIGENDIAN_P();
1604  goto unpack_integer;
1605 
1606  case 'l':
1607  signed_p = 1;
1608  integer_size = NATINT_LEN(long, 4);
1609  bigendian_p = BIGENDIAN_P();
1610  goto unpack_integer;
1611 
1612  case 'L':
1613  signed_p = 0;
1614  integer_size = NATINT_LEN(long, 4);
1615  bigendian_p = BIGENDIAN_P();
1616  goto unpack_integer;
1617 
1618  case 'q':
1619  signed_p = 1;
1620  integer_size = 8;
1621  bigendian_p = BIGENDIAN_P();
1622  goto unpack_integer;
1623 
1624  case 'Q':
1625  signed_p = 0;
1626  integer_size = 8;
1627  bigendian_p = BIGENDIAN_P();
1628  goto unpack_integer;
1629 
1630  case 'n':
1631  signed_p = 0;
1632  integer_size = 2;
1633  bigendian_p = 1;
1634  goto unpack_integer;
1635 
1636  case 'N':
1637  signed_p = 0;
1638  integer_size = 4;
1639  bigendian_p = 1;
1640  goto unpack_integer;
1641 
1642  case 'v':
1643  signed_p = 0;
1644  integer_size = 2;
1645  bigendian_p = 0;
1646  goto unpack_integer;
1647 
1648  case 'V':
1649  signed_p = 0;
1650  integer_size = 4;
1651  bigendian_p = 0;
1652  goto unpack_integer;
1653 
1654  unpack_integer:
1655  if (explicit_endian) {
1656  bigendian_p = explicit_endian == '>';
1657  }
1658 
1659  switch (integer_size) {
1660 #if defined(HAVE_INT16_T) && !defined(FORCE_BIG_PACK)
1661  case SIZEOF_INT16_T:
1662  if (signed_p) {
1663  PACK_LENGTH_ADJUST_SIZE(sizeof(int16_t));
1664  while (len-- > 0) {
1665  union {
1666  int16_t i;
1667  char a[sizeof(int16_t)];
1668  } v;
1669  memcpy(v.a, s, sizeof(int16_t));
1670  if (bigendian_p != BIGENDIAN_P()) v.i = swap16(v.i);
1671  s += sizeof(int16_t);
1672  UNPACK_PUSH(INT2FIX(v.i));
1673  }
1674  PACK_ITEM_ADJUST();
1675  }
1676  else {
1677  PACK_LENGTH_ADJUST_SIZE(sizeof(uint16_t));
1678  while (len-- > 0) {
1679  union {
1680  uint16_t i;
1681  char a[sizeof(uint16_t)];
1682  } v;
1683  memcpy(v.a, s, sizeof(uint16_t));
1684  if (bigendian_p != BIGENDIAN_P()) v.i = swap16(v.i);
1685  s += sizeof(uint16_t);
1686  UNPACK_PUSH(INT2FIX(v.i));
1687  }
1688  PACK_ITEM_ADJUST();
1689  }
1690  break;
1691 #endif
1692 
1693 #if defined(HAVE_INT32_T) && !defined(FORCE_BIG_PACK)
1694  case SIZEOF_INT32_T:
1695  if (signed_p) {
1696  PACK_LENGTH_ADJUST_SIZE(sizeof(int32_t));
1697  while (len-- > 0) {
1698  union {
1699  int32_t i;
1700  char a[sizeof(int32_t)];
1701  } v;
1702  memcpy(v.a, s, sizeof(int32_t));
1703  if (bigendian_p != BIGENDIAN_P()) v.i = swap32(v.i);
1704  s += sizeof(int32_t);
1705  UNPACK_PUSH(INT2NUM(v.i));
1706  }
1707  PACK_ITEM_ADJUST();
1708  }
1709  else {
1711  while (len-- > 0) {
1712  union {
1713  uint32_t i;
1714  char a[sizeof(uint32_t)];
1715  } v;
1716  memcpy(v.a, s, sizeof(uint32_t));
1717  if (bigendian_p != BIGENDIAN_P()) v.i = swap32(v.i);
1718  s += sizeof(uint32_t);
1719  UNPACK_PUSH(UINT2NUM(v.i));
1720  }
1721  PACK_ITEM_ADJUST();
1722  }
1723  break;
1724 #endif
1725 
1726 #if defined(HAVE_INT64_T) && !defined(FORCE_BIG_PACK)
1727  case SIZEOF_INT64_T:
1728  if (signed_p) {
1729  PACK_LENGTH_ADJUST_SIZE(sizeof(int64_t));
1730  while (len-- > 0) {
1731  union {
1732  int64_t i;
1733  char a[sizeof(int64_t)];
1734  } v;
1735  memcpy(v.a, s, sizeof(int64_t));
1736  if (bigendian_p != BIGENDIAN_P()) v.i = swap64(v.i);
1737  s += sizeof(int64_t);
1738  UNPACK_PUSH(INT64toNUM(v.i));
1739  }
1740  PACK_ITEM_ADJUST();
1741  }
1742  else {
1744  while (len-- > 0) {
1745  union {
1746  uint64_t i;
1747  char a[sizeof(uint64_t)];
1748  } v;
1749  memcpy(v.a, s, sizeof(uint64_t));
1750  if (bigendian_p != BIGENDIAN_P()) v.i = swap64(v.i);
1751  s += sizeof(uint64_t);
1752  UNPACK_PUSH(UINT64toNUM(v.i));
1753  }
1754  PACK_ITEM_ADJUST();
1755  }
1756  break;
1757 #endif
1758 
1759  default:
1760  if (integer_size > MAX_INTEGER_PACK_SIZE)
1761  rb_bug("unexpected integer size for pack: %d", integer_size);
1762  PACK_LENGTH_ADJUST_SIZE(integer_size);
1763  while (len-- > 0) {
1764  union {
1765  unsigned long i[(MAX_INTEGER_PACK_SIZE+SIZEOF_LONG)/SIZEOF_LONG];
1766  char a[(MAX_INTEGER_PACK_SIZE+SIZEOF_LONG)/SIZEOF_LONG*SIZEOF_LONG];
1767  } v;
1768  int num_longs = (integer_size+SIZEOF_LONG)/SIZEOF_LONG;
1769  int i;
1770 
1771  if (signed_p && (signed char)s[bigendian_p ? 0 : (integer_size-1)] < 0)
1772  memset(v.a, 0xff, sizeof(long)*num_longs);
1773  else
1774  memset(v.a, 0, sizeof(long)*num_longs);
1775  if (bigendian_p)
1776  memcpy(v.a + sizeof(long)*num_longs - integer_size, s, integer_size);
1777  else
1778  memcpy(v.a, s, integer_size);
1779  if (bigendian_p) {
1780  for (i = 0; i < num_longs/2; i++) {
1781  unsigned long t = v.i[i];
1782  v.i[i] = v.i[num_longs-1-i];
1783  v.i[num_longs-1-i] = t;
1784  }
1785  }
1786  if (bigendian_p != BIGENDIAN_P()) {
1787  for (i = 0; i < num_longs; i++)
1788  v.i[i] = swapl(v.i[i]);
1789  }
1790  s += integer_size;
1791  UNPACK_PUSH(rb_big_unpack(v.i, num_longs));
1792  }
1793  PACK_ITEM_ADJUST();
1794  break;
1795  }
1796  break;
1797 
1798  case 'f':
1799  case 'F':
1800  PACK_LENGTH_ADJUST_SIZE(sizeof(float));
1801  while (len-- > 0) {
1802  float tmp;
1803  memcpy(&tmp, s, sizeof(float));
1804  s += sizeof(float);
1805  UNPACK_PUSH(DBL2NUM((double)tmp));
1806  }
1807  PACK_ITEM_ADJUST();
1808  break;
1809 
1810  case 'e':
1811  PACK_LENGTH_ADJUST_SIZE(sizeof(float));
1812  while (len-- > 0) {
1813  float tmp;
1814  FLOAT_CONVWITH(ftmp);
1815 
1816  memcpy(&tmp, s, sizeof(float));
1817  s += sizeof(float);
1818  tmp = VTOHF(tmp,ftmp);
1819  UNPACK_PUSH(DBL2NUM((double)tmp));
1820  }
1821  PACK_ITEM_ADJUST();
1822  break;
1823 
1824  case 'E':
1825  PACK_LENGTH_ADJUST_SIZE(sizeof(double));
1826  while (len-- > 0) {
1827  double tmp;
1828  DOUBLE_CONVWITH(dtmp);
1829 
1830  memcpy(&tmp, s, sizeof(double));
1831  s += sizeof(double);
1832  tmp = VTOHD(tmp,dtmp);
1833  UNPACK_PUSH(DBL2NUM(tmp));
1834  }
1835  PACK_ITEM_ADJUST();
1836  break;
1837 
1838  case 'D':
1839  case 'd':
1840  PACK_LENGTH_ADJUST_SIZE(sizeof(double));
1841  while (len-- > 0) {
1842  double tmp;
1843  memcpy(&tmp, s, sizeof(double));
1844  s += sizeof(double);
1845  UNPACK_PUSH(DBL2NUM(tmp));
1846  }
1847  PACK_ITEM_ADJUST();
1848  break;
1849 
1850  case 'g':
1851  PACK_LENGTH_ADJUST_SIZE(sizeof(float));
1852  while (len-- > 0) {
1853  float tmp;
1854  FLOAT_CONVWITH(ftmp);
1855 
1856  memcpy(&tmp, s, sizeof(float));
1857  s += sizeof(float);
1858  tmp = NTOHF(tmp,ftmp);
1859  UNPACK_PUSH(DBL2NUM((double)tmp));
1860  }
1861  PACK_ITEM_ADJUST();
1862  break;
1863 
1864  case 'G':
1865  PACK_LENGTH_ADJUST_SIZE(sizeof(double));
1866  while (len-- > 0) {
1867  double tmp;
1868  DOUBLE_CONVWITH(dtmp);
1869 
1870  memcpy(&tmp, s, sizeof(double));
1871  s += sizeof(double);
1872  tmp = NTOHD(tmp,dtmp);
1873  UNPACK_PUSH(DBL2NUM(tmp));
1874  }
1875  PACK_ITEM_ADJUST();
1876  break;
1877 
1878  case 'U':
1879  if (len > send - s) len = send - s;
1880  while (len > 0 && s < send) {
1881  long alen = send - s;
1882  unsigned long l;
1883 
1884  l = utf8_to_uv(s, &alen);
1885  s += alen; len--;
1886  UNPACK_PUSH(ULONG2NUM(l));
1887  }
1888  break;
1889 
1890  case 'u':
1891  {
1892  VALUE buf = infected_str_new(0, (send - s)*3/4, str);
1893  char *ptr = RSTRING_PTR(buf);
1894  long total = 0;
1895 
1896  while (s < send && *s > ' ' && *s < 'a') {
1897  long a,b,c,d;
1898  char hunk[4];
1899 
1900  hunk[3] = '\0';
1901  len = (*s++ - ' ') & 077;
1902  total += len;
1903  if (total > RSTRING_LEN(buf)) {
1904  len -= total - RSTRING_LEN(buf);
1905  total = RSTRING_LEN(buf);
1906  }
1907 
1908  while (len > 0) {
1909  long mlen = len > 3 ? 3 : len;
1910 
1911  if (s < send && *s >= ' ')
1912  a = (*s++ - ' ') & 077;
1913  else
1914  a = 0;
1915  if (s < send && *s >= ' ')
1916  b = (*s++ - ' ') & 077;
1917  else
1918  b = 0;
1919  if (s < send && *s >= ' ')
1920  c = (*s++ - ' ') & 077;
1921  else
1922  c = 0;
1923  if (s < send && *s >= ' ')
1924  d = (*s++ - ' ') & 077;
1925  else
1926  d = 0;
1927  hunk[0] = (char)(a << 2 | b >> 4);
1928  hunk[1] = (char)(b << 4 | c >> 2);
1929  hunk[2] = (char)(c << 6 | d);
1930  memcpy(ptr, hunk, mlen);
1931  ptr += mlen;
1932  len -= mlen;
1933  }
1934  if (*s == '\r') s++;
1935  if (*s == '\n') s++;
1936  else if (s < send && (s+1 == send || s[1] == '\n'))
1937  s += 2; /* possible checksum byte */
1938  }
1939 
1940  rb_str_set_len(buf, total);
1941  UNPACK_PUSH(buf);
1942  }
1943  break;
1944 
1945  case 'm':
1946  {
1947  VALUE buf = infected_str_new(0, (send - s)*3/4, str);
1948  char *ptr = RSTRING_PTR(buf);
1949  int a = -1,b = -1,c = 0,d = 0;
1950  static signed char b64_xtable[256];
1951 
1952  if (b64_xtable['/'] <= 0) {
1953  int i;
1954 
1955  for (i = 0; i < 256; i++) {
1956  b64_xtable[i] = -1;
1957  }
1958  for (i = 0; i < 64; i++) {
1959  b64_xtable[(unsigned char)b64_table[i]] = (char)i;
1960  }
1961  }
1962  if (len == 0) {
1963  while (s < send) {
1964  a = b = c = d = -1;
1965  a = b64_xtable[(unsigned char)*s++];
1966  if (s >= send || a == -1) rb_raise(rb_eArgError, "invalid base64");
1967  b = b64_xtable[(unsigned char)*s++];
1968  if (s >= send || b == -1) rb_raise(rb_eArgError, "invalid base64");
1969  if (*s == '=') {
1970  if (s + 2 == send && *(s + 1) == '=') break;
1971  rb_raise(rb_eArgError, "invalid base64");
1972  }
1973  c = b64_xtable[(unsigned char)*s++];
1974  if (s >= send || c == -1) rb_raise(rb_eArgError, "invalid base64");
1975  if (s + 1 == send && *s == '=') break;
1976  d = b64_xtable[(unsigned char)*s++];
1977  if (d == -1) rb_raise(rb_eArgError, "invalid base64");
1978  *ptr++ = castchar(a << 2 | b >> 4);
1979  *ptr++ = castchar(b << 4 | c >> 2);
1980  *ptr++ = castchar(c << 6 | d);
1981  }
1982  if (c == -1) {
1983  *ptr++ = castchar(a << 2 | b >> 4);
1984  if (b & 0xf) rb_raise(rb_eArgError, "invalid base64");
1985  }
1986  else if (d == -1) {
1987  *ptr++ = castchar(a << 2 | b >> 4);
1988  *ptr++ = castchar(b << 4 | c >> 2);
1989  if (c & 0x3) rb_raise(rb_eArgError, "invalid base64");
1990  }
1991  }
1992  else {
1993  while (s < send) {
1994  a = b = c = d = -1;
1995  while ((a = b64_xtable[(unsigned char)*s]) == -1 && s < send) {s++;}
1996  if (s >= send) break;
1997  s++;
1998  while ((b = b64_xtable[(unsigned char)*s]) == -1 && s < send) {s++;}
1999  if (s >= send) break;
2000  s++;
2001  while ((c = b64_xtable[(unsigned char)*s]) == -1 && s < send) {if (*s == '=') break; s++;}
2002  if (*s == '=' || s >= send) break;
2003  s++;
2004  while ((d = b64_xtable[(unsigned char)*s]) == -1 && s < send) {if (*s == '=') break; s++;}
2005  if (*s == '=' || s >= send) break;
2006  s++;
2007  *ptr++ = castchar(a << 2 | b >> 4);
2008  *ptr++ = castchar(b << 4 | c >> 2);
2009  *ptr++ = castchar(c << 6 | d);
2010  }
2011  if (a != -1 && b != -1) {
2012  if (c == -1 && *s == '=')
2013  *ptr++ = castchar(a << 2 | b >> 4);
2014  else if (c != -1 && *s == '=') {
2015  *ptr++ = castchar(a << 2 | b >> 4);
2016  *ptr++ = castchar(b << 4 | c >> 2);
2017  }
2018  }
2019  }
2020  rb_str_set_len(buf, ptr - RSTRING_PTR(buf));
2021  UNPACK_PUSH(buf);
2022  }
2023  break;
2024 
2025  case 'M':
2026  {
2027  VALUE buf = infected_str_new(0, send - s, str);
2028  char *ptr = RSTRING_PTR(buf), *ss = s;
2029  int c1, c2;
2030 
2031  while (s < send) {
2032  if (*s == '=') {
2033  if (++s == send) break;
2034  if (s+1 < send && *s == '\r' && *(s+1) == '\n')
2035  s++;
2036  if (*s != '\n') {
2037  if ((c1 = hex2num(*s)) == -1) break;
2038  if (++s == send) break;
2039  if ((c2 = hex2num(*s)) == -1) break;
2040  *ptr++ = castchar(c1 << 4 | c2);
2041  }
2042  }
2043  else {
2044  *ptr++ = *s;
2045  }
2046  s++;
2047  ss = s;
2048  }
2049  rb_str_set_len(buf, ptr - RSTRING_PTR(buf));
2050  rb_str_buf_cat(buf, ss, send-ss);
2052  UNPACK_PUSH(buf);
2053  }
2054  break;
2055 
2056  case '@':
2057  if (len > RSTRING_LEN(str))
2058  rb_raise(rb_eArgError, "@ outside of string");
2059  s = RSTRING_PTR(str) + len;
2060  break;
2061 
2062  case 'X':
2063  if (len > s - RSTRING_PTR(str))
2064  rb_raise(rb_eArgError, "X outside of string");
2065  s -= len;
2066  break;
2067 
2068  case 'x':
2069  if (len > send - s)
2070  rb_raise(rb_eArgError, "x outside of string");
2071  s += len;
2072  break;
2073 
2074  case 'P':
2075  if (sizeof(char *) <= (size_t)(send - s)) {
2076  VALUE tmp = Qnil;
2077  char *t;
2078 
2079  memcpy(&t, s, sizeof(char *));
2080  s += sizeof(char *);
2081 
2082  if (t) {
2083  VALUE a, *p, *pend;
2084 
2085  if (!(a = str_associated(str))) {
2086  rb_raise(rb_eArgError, "no associated pointer");
2087  }
2088  p = RARRAY_PTR(a);
2089  pend = p + RARRAY_LEN(a);
2090  while (p < pend) {
2091  if (RB_TYPE_P(*p, T_STRING) && RSTRING_PTR(*p) == t) {
2092  if (len < RSTRING_LEN(*p)) {
2093  tmp = rb_tainted_str_new(t, len);
2094  str_associate(tmp, a);
2095  }
2096  else {
2097  tmp = *p;
2098  }
2099  break;
2100  }
2101  p++;
2102  }
2103  if (p == pend) {
2104  rb_raise(rb_eArgError, "non associated pointer");
2105  }
2106  }
2107  UNPACK_PUSH(tmp);
2108  }
2109  break;
2110 
2111  case 'p':
2112  if (len > (long)((send - s) / sizeof(char *)))
2113  len = (send - s) / sizeof(char *);
2114  while (len-- > 0) {
2115  if ((size_t)(send - s) < sizeof(char *))
2116  break;
2117  else {
2118  VALUE tmp = Qnil;
2119  char *t;
2120 
2121  memcpy(&t, s, sizeof(char *));
2122  s += sizeof(char *);
2123 
2124  if (t) {
2125  VALUE a, *p, *pend;
2126 
2127  if (!(a = str_associated(str))) {
2128  rb_raise(rb_eArgError, "no associated pointer");
2129  }
2130  p = RARRAY_PTR(a);
2131  pend = p + RARRAY_LEN(a);
2132  while (p < pend) {
2133  if (RB_TYPE_P(*p, T_STRING) && RSTRING_PTR(*p) == t) {
2134  tmp = *p;
2135  break;
2136  }
2137  p++;
2138  }
2139  if (p == pend) {
2140  rb_raise(rb_eArgError, "non associated pointer");
2141  }
2142  }
2143  UNPACK_PUSH(tmp);
2144  }
2145  }
2146  break;
2147 
2148  case 'w':
2149  {
2150  unsigned long ul = 0;
2151  unsigned long ulmask = 0xfeUL << ((sizeof(unsigned long) - 1) * 8);
2152 
2153  while (len > 0 && s < send) {
2154  ul <<= 7;
2155  ul |= (*s & 0x7f);
2156  if (!(*s++ & 0x80)) {
2157  UNPACK_PUSH(ULONG2NUM(ul));
2158  len--;
2159  ul = 0;
2160  }
2161  else if (ul & ulmask) {
2162  VALUE big = rb_uint2big(ul);
2163  VALUE big128 = rb_uint2big(128);
2164  while (s < send) {
2165  big = rb_big_mul(big, big128);
2166  big = rb_big_plus(big, rb_uint2big(*s & 0x7f));
2167  if (!(*s++ & 0x80)) {
2168  UNPACK_PUSH(big);
2169  len--;
2170  ul = 0;
2171  break;
2172  }
2173  }
2174  }
2175  }
2176  }
2177  break;
2178 
2179  default:
2180  rb_warning("unknown unpack directive '%c' in '%s'",
2181  type, RSTRING_PTR(fmt));
2182  break;
2183  }
2184  }
2185 
2186  return ary;
2187 }
2188 
2189 #define BYTEWIDTH 8
2190 
2191 int
2192 rb_uv_to_utf8(char buf[6], unsigned long uv)
2193 {
2194  if (uv <= 0x7f) {
2195  buf[0] = (char)uv;
2196  return 1;
2197  }
2198  if (uv <= 0x7ff) {
2199  buf[0] = castchar(((uv>>6)&0xff)|0xc0);
2200  buf[1] = castchar((uv&0x3f)|0x80);
2201  return 2;
2202  }
2203  if (uv <= 0xffff) {
2204  buf[0] = castchar(((uv>>12)&0xff)|0xe0);
2205  buf[1] = castchar(((uv>>6)&0x3f)|0x80);
2206  buf[2] = castchar((uv&0x3f)|0x80);
2207  return 3;
2208  }
2209  if (uv <= 0x1fffff) {
2210  buf[0] = castchar(((uv>>18)&0xff)|0xf0);
2211  buf[1] = castchar(((uv>>12)&0x3f)|0x80);
2212  buf[2] = castchar(((uv>>6)&0x3f)|0x80);
2213  buf[3] = castchar((uv&0x3f)|0x80);
2214  return 4;
2215  }
2216  if (uv <= 0x3ffffff) {
2217  buf[0] = castchar(((uv>>24)&0xff)|0xf8);
2218  buf[1] = castchar(((uv>>18)&0x3f)|0x80);
2219  buf[2] = castchar(((uv>>12)&0x3f)|0x80);
2220  buf[3] = castchar(((uv>>6)&0x3f)|0x80);
2221  buf[4] = castchar((uv&0x3f)|0x80);
2222  return 5;
2223  }
2224  if (uv <= 0x7fffffff) {
2225  buf[0] = castchar(((uv>>30)&0xff)|0xfc);
2226  buf[1] = castchar(((uv>>24)&0x3f)|0x80);
2227  buf[2] = castchar(((uv>>18)&0x3f)|0x80);
2228  buf[3] = castchar(((uv>>12)&0x3f)|0x80);
2229  buf[4] = castchar(((uv>>6)&0x3f)|0x80);
2230  buf[5] = castchar((uv&0x3f)|0x80);
2231  return 6;
2232  }
2233  rb_raise(rb_eRangeError, "pack(U): value out of range");
2234 
2235  UNREACHABLE;
2236 }
2237 
2238 static const unsigned long utf8_limits[] = {
2239  0x0, /* 1 */
2240  0x80, /* 2 */
2241  0x800, /* 3 */
2242  0x10000, /* 4 */
2243  0x200000, /* 5 */
2244  0x4000000, /* 6 */
2245  0x80000000, /* 7 */
2246 };
2247 
2248 static unsigned long
2249 utf8_to_uv(const char *p, long *lenp)
2250 {
2251  int c = *p++ & 0xff;
2252  unsigned long uv = c;
2253  long n;
2254 
2255  if (!(uv & 0x80)) {
2256  *lenp = 1;
2257  return uv;
2258  }
2259  if (!(uv & 0x40)) {
2260  *lenp = 1;
2261  rb_raise(rb_eArgError, "malformed UTF-8 character");
2262  }
2263 
2264  if (!(uv & 0x20)) { n = 2; uv &= 0x1f; }
2265  else if (!(uv & 0x10)) { n = 3; uv &= 0x0f; }
2266  else if (!(uv & 0x08)) { n = 4; uv &= 0x07; }
2267  else if (!(uv & 0x04)) { n = 5; uv &= 0x03; }
2268  else if (!(uv & 0x02)) { n = 6; uv &= 0x01; }
2269  else {
2270  *lenp = 1;
2271  rb_raise(rb_eArgError, "malformed UTF-8 character");
2272  }
2273  if (n > *lenp) {
2274  rb_raise(rb_eArgError, "malformed UTF-8 character (expected %ld bytes, given %ld bytes)",
2275  n, *lenp);
2276  }
2277  *lenp = n--;
2278  if (n != 0) {
2279  while (n--) {
2280  c = *p++ & 0xff;
2281  if ((c & 0xc0) != 0x80) {
2282  *lenp -= n + 1;
2283  rb_raise(rb_eArgError, "malformed UTF-8 character");
2284  }
2285  else {
2286  c &= 0x3f;
2287  uv = uv << 6 | c;
2288  }
2289  }
2290  }
2291  n = *lenp - 1;
2292  if (uv < utf8_limits[n]) {
2293  rb_raise(rb_eArgError, "redundant UTF-8 sequence");
2294  }
2295  return uv;
2296 }
2297 
2298 void
2300 {
2301  rb_define_method(rb_cArray, "pack", pack_pack, 1);
2302  rb_define_method(rb_cString, "unpack", pack_unpack, 1);
2303 
2304  id_associated = rb_intern_const("__pack_associated__");
2305 }
RUBY_EXTERN VALUE rb_cString
Definition: ruby.h:1456
#define MAX_INTEGER_PACK_SIZE
Definition: pack.c:254
#define RARRAY_LEN(a)
Definition: ruby.h:899
void rb_bug(const char *fmt,...)
Definition: error.c:295
VALUE rb_uint2big(VALUE n)
Definition: bignum.c:288
#define swap32(x)
Definition: pack.c:95
#define INT2NUM(x)
Definition: ruby.h:1178
#define BIGENDIAN_P()
Definition: pack.c:46
int i
Definition: win32ole.c:784
#define ENCODING_CODERANGE_SET(obj, encindex, cr)
Definition: encoding.h:73
#define NATINT_LEN(type, len)
Definition: pack.c:50
#define NUM2INT(x)
Definition: ruby.h:622
#define HTONF(x, y)
#define THISFROM
void rb_big_pack(VALUE val, unsigned long *buf, long num_longs)
Definition: bignum.c:369
#define PACK_ITEM_ADJUST()
Definition: pack.c:1219
VALUE rb_big_plus(VALUE x, VALUE y)
Definition: bignum.c:2031
#define HTOND(x, y)
VALUE rb_eTypeError
Definition: error.c:516
#define UNREACHABLE
Definition: ruby.h:40
#define castchar(from)
#define ULONG2NUM(x)
Definition: ruby.h:1209
VALUE rb_ary_push(VALUE ary, VALUE item)
Definition: array.c:822
int rb_usascii_encindex(void)
Definition: encoding.c:1190
void rb_str_set_len(VALUE, long)
Definition: string.c:1838
static void str_associate(VALUE str, VALUE add)
Definition: pack.c:267
VALUE rb_to_int(VALUE)
Definition: object.c:2482
void rb_raise(VALUE exc, const char *fmt,...)
Definition: error.c:1788
#define VTOHD(x, y)
static const char uu_table[]
Definition: pack.c:1083
static const char toofew[]
Definition: pack.c:257
#define DOUBLE_CONVWITH(y)
#define T_ARRAY
Definition: ruby.h:492
#define NEXTFROM
#define ISDIGIT(c)
VALUE rb_big_unpack(unsigned long *buf, long num_longs)
Definition: bignum.c:411
#define FIXNUM_P(f)
Definition: ruby.h:355
#define ENC_CODERANGE_7BIT
Definition: encoding.h:58
VALUE rb_eRangeError
Definition: error.c:520
const char * rb_obj_classname(VALUE)
Definition: variable.c:396
VALUE rb_str_buf_cat(VALUE, const char *, long)
Definition: string.c:1951
Win32OLEIDispatch * p
Definition: win32ole.c:786
#define HTOVD(x, y)
static ID id_associated
Definition: pack.c:264
#define ISALPHA(c)
Definition: ruby.h:1636
#define NTOHF(x, y)
static VALUE pack_unpack(VALUE str, VALUE fmt)
Definition: pack.c:1342
#define RB_TYPE_P(obj, type)
Definition: ruby.h:1537
VALUE rb_big2ulong_pack(VALUE x)
Definition: bignum.c:1215
VALUE rb_big_divmod(VALUE x, VALUE y)
Definition: bignum.c:3010
#define le(x, y)
Definition: time.c:69
unsigned long long uint64_t
Definition: sha2.h:102
static void encodes(VALUE, const char *, long, int, int)
Definition: pack.c:1089
int rb_block_given_p(void)
Definition: eval.c:672
VALUE rb_obj_taint(VALUE)
Definition: object.c:901
VALUE rb_eRuntimeError
Definition: error.c:515
VALUE rb_obj_as_string(VALUE)
Definition: string.c:895
static VALUE infected_str_new(const char *ptr, long len, VALUE str)
Definition: pack.c:1225
VALUE rb_ary_new(void)
Definition: array.c:424
int rb_ascii8bit_encindex(void)
Definition: encoding.c:1160
#define UINT2NUM(x)
Definition: ruby.h:1188
#define STRTOUL(str, endptr, base)
Definition: ruby.h:1649
#define NIL_P(v)
Definition: ruby.h:446
#define define_swapx(x, xtype)
Definition: pack.c:63
#define add(x, y)
Definition: date_strftime.c:23
static void qpencode(VALUE, VALUE, long)
Definition: pack.c:1139
void rb_enc_set_index(VALUE obj, int idx)
Definition: encoding.c:741
static VALUE str_associated(VALUE str)
Definition: pack.c:282
#define Qfalse
Definition: ruby.h:433
#define T_BIGNUM
Definition: ruby.h:495
#define HTOVF(x, y)
#define ENC_CODERANGE_VALID
Definition: encoding.h:59
static const char hex_table[]
Definition: pack.c:1136
#define EOF
Definition: vsnprintf.c:207
#define NTOHD(x, y)
#define RSTRING_LEN(str)
Definition: ruby.h:862
int errno
#define VTOHF(x, y)
VALUE rb_ivar_set(VALUE, ID, VALUE)
Definition: variable.c:1128
unsigned char buf[MIME_BUF_SIZE]
Definition: nkf.c:4308
unsigned long ID
Definition: ruby.h:105
#define Qnil
Definition: ruby.h:435
int type
Definition: tcltklib.c:111
static int hex2num(char c)
Definition: pack.c:1192
unsigned long VALUE
Definition: ruby.h:104
VALUE rb_big_mul(VALUE x, VALUE y)
Definition: bignum.c:2660
char * strchr(char *, char)
int rb_utf8_encindex(void)
Definition: encoding.c:1175
#define FLOAT_CONVWITH(y)
unsigned int uint32_t
Definition: sha2.h:101
static VALUE pack_pack(VALUE ary, VALUE fmt)
Definition: pack.c:394
#define RSTRING_PTR(str)
Definition: ruby.h:866
static unsigned long utf8_to_uv(const char *, long *)
Definition: pack.c:2249
static const unsigned long utf8_limits[]
Definition: pack.c:2238
#define RFLOAT_VALUE(v)
Definition: ruby.h:836
#define f
#define INT2FIX(i)
Definition: ruby.h:241
#define RARRAY_PTR(a)
Definition: ruby.h:904
#define T_STRING
Definition: ruby.h:490
#define OBJ_INFECT(x, s)
Definition: ruby.h:1157
v
Definition: win32ole.c:798
VALUE rb_cArray
Definition: array.c:29
VALUE rb_ary_concat(VALUE x, VALUE y)
Definition: array.c:3370
int rb_uv_to_utf8(char buf[6], unsigned long uv)
Definition: pack.c:2192
#define PACK_LENGTH_ADJUST_SIZE(sz)
Definition: pack.c:1209
#define swap16(x)
Definition: pack.c:91
#define StringValuePtr(v)
Definition: ruby.h:547
static const char b64_table[]
Definition: pack.c:1085
void Init_pack(void)
Definition: pack.c:2299
void rb_warning(const char *fmt,...)
Definition: error.c:234
#define rb_intern_const(str)
Definition: ruby.h:1332
VALUE rb_tainted_str_new(const char *, long)
VALUE rb_str_buf_new(long)
Definition: string.c:777
VALUE rb_usascii_str_new(const char *, long)
Definition: string.c:431
#define FIX2LONG(x)
Definition: ruby.h:353
void rb_define_method(VALUE klass, const char *name, VALUE(*func)(ANYARGS), int argc)
Definition: class.c:1344
#define UNPACK_PUSH(item)
VALUE rb_eArgError
Definition: error.c:517
#define NUM2LONG(x)
Definition: ruby.h:592
VALUE rb_attr_get(VALUE, ID)
Definition: variable.c:1122
#define DBL2NUM(dbl)
Definition: ruby.h:837
#define ISSPACE(c)
Definition: ruby.h:1632
#define StringValue(v)
Definition: ruby.h:546
VALUE rb_to_float(VALUE)
Definition: object.c:2745
VALUE rb_str_new(const char *, long)
Definition: string.c:425
#define SIGNED_VALUE
Definition: ruby.h:106