-
Notifications
You must be signed in to change notification settings - Fork 0
/
pack.c
1930 lines (1739 loc) · 39 KB
/
pack.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
pack.c -
$Author$
$Date$
created at: Thu Feb 10 15:17:05 JST 1994
Copyright (C) 1993-2003 Yukihiro Matsumoto
**********************************************************************/
#include "ruby.h"
#include <sys/types.h>
#include <ctype.h>
#define SIZE16 2
#define SIZE32 4
#if SIZEOF_SHORT != 2 || SIZEOF_LONG != 4
# define NATINT_PACK
#endif
#ifdef NATINT_PACK
# define OFF16B(p) ((char*)(p) + (natint?0:(sizeof(short) - SIZE16)))
# define OFF32B(p) ((char*)(p) + (natint?0:(sizeof(long) - SIZE32)))
# define NATINT_I32(x) (natint?NUM2LONG(x):(NUM2I32(x)))
# define NATINT_U32(x) (natint?NUM2ULONG(x):(NUM2U32(x)))
# define NATINT_LEN(type,len) (natint?sizeof(type):(len))
# ifdef WORDS_BIGENDIAN
# define OFF16(p) OFF16B(p)
# define OFF32(p) OFF32B(p)
# endif
#else
# define NATINT_I32(x) NUM2I32(x)
# define NATINT_U32(x) NUM2U32(x)
# define NATINT_LEN(type,len) sizeof(type)
#endif
#ifndef OFF16
# define OFF16(p) (char*)(p)
# define OFF32(p) (char*)(p)
#endif
#ifndef OFF16B
# define OFF16B(p) (char*)(p)
# define OFF32B(p) (char*)(p)
#endif
#define define_swapx(x, xtype) \
static xtype \
TOKEN_PASTE(swap,x)(z) \
xtype z; \
{ \
xtype r; \
xtype *zp; \
unsigned char *s, *t; \
int i; \
\
zp = malloc(sizeof(xtype)); \
*zp = z; \
s = (unsigned char*)zp; \
t = malloc(sizeof(xtype)); \
for (i=0; i<sizeof(xtype); i++) { \
t[sizeof(xtype)-i-1] = s[i]; \
} \
r = *(xtype *)t; \
free(t); \
free(zp); \
return r; \
}
#if SIZEOF_SHORT == 2
#define swaps(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
#else
#if SIZEOF_SHORT == 4
#define swaps(x) ((((x)&0xFF)<<24) \
|(((x)>>24)&0xFF) \
|(((x)&0x0000FF00)<<8) \
|(((x)&0x00FF0000)>>8) )
#else
define_swapx(s,short);
#endif
#endif
#if SIZEOF_LONG == 4
#define swapl(x) ((((x)&0xFF)<<24) \
|(((x)>>24)&0xFF) \
|(((x)&0x0000FF00)<<8) \
|(((x)&0x00FF0000)>>8) )
#else
#if SIZEOF_LONG == 8
#define swapl(x) ((((x)&0x00000000000000FF)<<56) \
|(((x)&0xFF00000000000000)>>56) \
|(((x)&0x000000000000FF00)<<40) \
|(((x)&0x00FF000000000000)>>40) \
|(((x)&0x0000000000FF0000)<<24) \
|(((x)&0x0000FF0000000000)>>24) \
|(((x)&0x00000000FF000000)<<8) \
|(((x)&0x000000FF00000000)>>8))
#else
define_swapx(l,long);
#endif
#endif
#if SIZEOF_FLOAT == 4
#if SIZEOF_LONG == 4 /* SIZEOF_FLOAT == 4 == SIZEOF_LONG */
#define swapf(x) swapl(x)
#define FLOAT_SWAPPER unsigned long
#else
#if SIZEOF_SHORT == 4 /* SIZEOF_FLOAT == 4 == SIZEOF_SHORT */
#define swapf(x) swaps(x)
#define FLOAT_SWAPPER unsigned short
#else /* SIZEOF_FLOAT == 4 but undivide by known size of int */
define_swapx(f,float);
#endif /* #if SIZEOF_SHORT == 4 */
#endif /* #if SIZEOF_LONG == 4 */
#else /* SIZEOF_FLOAT != 4 */
define_swapx(f,float);
#endif /* #if SIZEOF_FLOAT == 4 */
#if SIZEOF_DOUBLE == 8
#if SIZEOF_LONG == 8 /* SIZEOF_DOUBLE == 8 == SIZEOF_LONG */
#define swapd(x) swapl(x)
#define DOUBLE_SWAPPER unsigned long
#else
#if SIZEOF_LONG == 4 /* SIZEOF_DOUBLE == 8 && 4 == SIZEOF_LONG */
static double
swapd(d)
const double d;
{
double dtmp = d;
unsigned long utmp[2];
unsigned long utmp0;
utmp[0] = 0; utmp[1] = 0;
memcpy(utmp,&dtmp,sizeof(double));
utmp0 = utmp[0];
utmp[0] = swapl(utmp[1]);
utmp[1] = swapl(utmp0);
memcpy(&dtmp,utmp,sizeof(double));
return dtmp;
}
#else
#if SIZEOF_SHORT == 4 /* SIZEOF_DOUBLE == 8 && 4 == SIZEOF_SHORT */
static double
swapd(d)
const double d;
{
double dtmp = d;
unsigned short utmp[2];
unsigned short utmp0;
utmp[0] = 0; utmp[1] = 0;
memcpy(utmp,&dtmp,sizeof(double));
utmp0 = utmp[0];
utmp[0] = swaps(utmp[1]);
utmp[1] = swaps(utmp0);
memcpy(&dtmp,utmp,sizeof(double));
return dtmp;
}
#else /* SIZEOF_DOUBLE == 8 but undivied by known size of int */
define_swapx(d, double);
#endif /* #if SIZEOF_SHORT == 4 */
#endif /* #if SIZEOF_LONG == 4 */
#endif /* #if SIZEOF_LONG == 8 */
#else /* SIZEOF_DOUBLE != 8 */
define_swapx(d, double);
#endif /* #if SIZEOF_DPOUBLE == 8 */
#undef define_swapx
#ifdef DYNAMIC_ENDIAN
#ifdef ntohs
#undef ntohs
#undef ntohl
#undef htons
#undef htonl
#endif
static int
endian()
{
static int init = 0;
static int endian_value;
char *p;
if (init) return endian_value;
init = 1;
p = (char*)&init;
return endian_value = p[0]?0:1;
}
#define ntohs(x) (endian()?(x):swaps(x))
#define ntohl(x) (endian()?(x):swapl(x))
#define ntohf(x) (endian()?(x):swapf(x))
#define ntohd(x) (endian()?(x):swapd(x))
#define htons(x) (endian()?(x):swaps(x))
#define htonl(x) (endian()?(x):swapl(x))
#define htonf(x) (endian()?(x):swapf(x))
#define htond(x) (endian()?(x):swapd(x))
#define htovs(x) (endian()?swaps(x):(x))
#define htovl(x) (endian()?swapl(x):(x))
#define htovf(x) (endian()?swapf(x):(x))
#define htovd(x) (endian()?swapd(x):(x))
#define vtohs(x) (endian()?swaps(x):(x))
#define vtohl(x) (endian()?swapl(x):(x))
#define vtohf(x) (endian()?swapf(x):(x))
#define vtohd(x) (endian()?swapd(x):(x))
#else
#ifdef WORDS_BIGENDIAN
#ifndef ntohs
#define ntohs(x) (x)
#define ntohl(x) (x)
#define htons(x) (x)
#define htonl(x) (x)
#endif
#define ntohf(x) (x)
#define ntohd(x) (x)
#define htonf(x) (x)
#define htond(x) (x)
#define htovs(x) swaps(x)
#define htovl(x) swapl(x)
#define htovf(x) swapf(x)
#define htovd(x) swapd(x)
#define vtohs(x) swaps(x)
#define vtohl(x) swapl(x)
#define vtohf(x) swapf(x)
#define vtohd(x) swapd(x)
#else /* LITTLE ENDIAN */
#ifndef ntohs
#undef ntohs
#undef ntohl
#undef htons
#undef htonl
#define ntohs(x) swaps(x)
#define ntohl(x) swapl(x)
#define htons(x) swaps(x)
#define htonl(x) swapl(x)
#endif
#define ntohf(x) swapf(x)
#define ntohd(x) swapd(x)
#define htonf(x) swapf(x)
#define htond(x) swapd(x)
#define htovs(x) (x)
#define htovl(x) (x)
#define htovf(x) (x)
#define htovd(x) (x)
#define vtohs(x) (x)
#define vtohl(x) (x)
#define vtohf(x) (x)
#define vtohd(x) (x)
#endif
#endif
#ifdef FLOAT_SWAPPER
#define FLOAT_CONVWITH(y) FLOAT_SWAPPER y;
#define HTONF(x,y) (memcpy(&y,&x,sizeof(float)), \
y = htonf((FLOAT_SWAPPER)y), \
memcpy(&x,&y,sizeof(float)), \
x)
#define HTOVF(x,y) (memcpy(&y,&x,sizeof(float)), \
y = htovf((FLOAT_SWAPPER)y), \
memcpy(&x,&y,sizeof(float)), \
x)
#define NTOHF(x,y) (memcpy(&y,&x,sizeof(float)), \
y = ntohf((FLOAT_SWAPPER)y), \
memcpy(&x,&y,sizeof(float)), \
x)
#define VTOHF(x,y) (memcpy(&y,&x,sizeof(float)), \
y = vtohf((FLOAT_SWAPPER)y), \
memcpy(&x,&y,sizeof(float)), \
x)
#else
#define FLOAT_CONVWITH(y)
#define HTONF(x,y) htonf(x)
#define HTOVF(x,y) htovf(x)
#define NTOHF(x,y) ntohf(x)
#define VTOHF(x,y) vtohf(x)
#endif
#ifdef DOUBLE_SWAPPER
#define DOUBLE_CONVWITH(y) DOUBLE_SWAPPER y;
#define HTOND(x,y) (memcpy(&y,&x,sizeof(double)), \
y = htond((DOUBLE_SWAPPER)y), \
memcpy(&x,&y,sizeof(double)), \
x)
#define HTOVD(x,y) (memcpy(&y,&x,sizeof(double)), \
y = htovd((DOUBLE_SWAPPER)y), \
memcpy(&x,&y,sizeof(double)), \
x)
#define NTOHD(x,y) (memcpy(&y,&x,sizeof(double)), \
y = ntohd((DOUBLE_SWAPPER)y), \
memcpy(&x,&y,sizeof(double)), \
x)
#define VTOHD(x,y) (memcpy(&y,&x,sizeof(double)), \
y = vtohd((DOUBLE_SWAPPER)y), \
memcpy(&x,&y,sizeof(double)), \
x)
#else
#define DOUBLE_CONVWITH(y)
#define HTOND(x,y) htond(x)
#define HTOVD(x,y) htovd(x)
#define NTOHD(x,y) ntohd(x)
#define VTOHD(x,y) vtohd(x)
#endif
#if SIZEOF_LONG == SIZE32
typedef long I32;
typedef unsigned long U32;
#define NUM2I32(x) NUM2LONG(x)
#define NUM2U32(x) NUM2ULONG(x)
#else
typedef int I32;
typedef unsigned int U32;
# if SIZEOF_INT == SIZE32
# define NUM2I32(x) NUM2INT(x)
# define NUM2U32(x) NUM2UINT(x)
# else
#define I32_MAX 2147483647
#define I32_MIN (-I32_MAX-1)
static I32
num2i32(x)
VALUE x;
{
long num = NUM2LONG(x);
if (num < I32_MIN || I32_MAX < num) {
rb_raise(rb_eRangeError, "integer %ld too big to convert to `I32'", num);
}
return (I32)num;
}
#define U32_MAX 4294967295
static U32
num2u32(x)
VALUE x;
{
unsigned long num = NUM2ULONG(x);
if (U32_MAX < num) {
rb_raise(rb_eRangeError, "integer %ld too big to convert to `U32'", num);
}
return (U32)num;
}
# define NUM2I32(x) num2i32(x)
# define NUM2U32(x) num2u32(x)
# endif
#endif
#if SIZEOF_LONG == SIZE32 || SIZEOF_INT == SIZE32
# define EXTEND32(x) ((I32)(x))
#else
/* invariant in modulo 1<<31 */
# define EXTEND32(x) (I32)(((1<<31)-1-(x))^~(~0<<31))
#endif
#if SIZEOF_SHORT == SIZE16
# define EXTEND16(x) (short)(x)
#else
# define EXTEND16(x) (short)(((1<<15)-1-(x))^~(~0<<15))
#endif
#ifdef HAVE_LONG_LONG
# define QUAD_SIZE sizeof(LONG_LONG)
#else
# define QUAD_SIZE 8
#endif
static char *toofew = "too few arguments";
static void encodes _((VALUE,char*,long,int));
static void qpencode _((VALUE,VALUE,long));
static int uv_to_utf8 _((char*,unsigned long));
static unsigned long utf8_to_uv _((char*,long*));
static VALUE
pack_pack(ary, fmt)
VALUE ary, fmt;
{
static char *nul10 = "\0\0\0\0\0\0\0\0\0\0";
static char *spc10 = " ";
char *p, *pend;
VALUE res, from, associates = 0;
char type;
long items, len, idx, plen;
char *ptr;
#ifdef NATINT_PACK
int natint; /* native integer */
#endif
StringValue(fmt);
p = RSTRING(fmt)->ptr;
pend = p + RSTRING(fmt)->len;
res = rb_str_buf_new(0);
items = RARRAY(ary)->len;
idx = 0;
#define THISFROM RARRAY(ary)->ptr[idx]
#define NEXTFROM (items-- > 0 ? RARRAY(ary)->ptr[idx++] : (rb_raise(rb_eArgError, toofew),0))
while (p < pend) {
type = *p++; /* get data type */
#ifdef NATINT_PACK
natint = 0;
#endif
if (ISSPACE(type)) continue;
if (type == '#') {
while ((p < pend) && (*p != '\n')) {
p++;
}
continue;
}
if (*p == '_' || *p == '!') {
char *natstr = "sSiIlL";
if (strchr(natstr, type)) {
#ifdef NATINT_PACK
natint = 1;
#endif
p++;
}
else {
rb_raise(rb_eArgError, "'%c' allowed only after types %s", *p, natstr);
}
}
if (*p == '*') { /* set data length */
len = strchr("@Xxu", type) ? 0 : items;
p++;
}
else if (ISDIGIT(*p)) {
len = strtoul(p, (char**)&p, 10);
}
else {
len = 1;
}
switch (type) {
case 'A': case 'a': case 'Z':
case 'B': case 'b':
case 'H': case 'h':
from = NEXTFROM;
if (NIL_P(from)) {
ptr = "";
plen = 0;
}
else {
StringValue(from);
ptr = RSTRING(from)->ptr;
plen = RSTRING(from)->len;
OBJ_INFECT(res, from);
}
if (p[-1] == '*')
len = plen;
switch (type) {
case 'a':
case 'A':
case 'Z':
if (plen >= len)
rb_str_buf_cat(res, ptr, len);
else {
rb_str_buf_cat(res, ptr, plen);
len -= plen;
while (len >= 10) {
rb_str_buf_cat(res, (type == 'A')?spc10:nul10, 10);
len -= 10;
}
rb_str_buf_cat(res, (type == 'A')?spc10:nul10, len);
}
break;
case 'b':
{
int byte = 0;
long i, j = 0;
if (len > plen) {
j = (len - plen + 1)/2;
len = plen;
}
for (i=0; i++ < len; ptr++) {
if (*ptr & 1)
byte |= 128;
if (i & 7)
byte >>= 1;
else {
char c = byte & 0xff;
rb_str_buf_cat(res, &c, 1);
byte = 0;
}
}
if (len & 7) {
char c;
byte >>= 7 - (len & 7);
c = byte & 0xff;
rb_str_buf_cat(res, &c, 1);
}
len = j;
goto grow;
}
break;
case 'B':
{
int byte = 0;
long i, j = 0;
if (len > plen) {
j = (len - plen + 1)/2;
len = plen;
}
for (i=0; i++ < len; ptr++) {
byte |= *ptr & 1;
if (i & 7)
byte <<= 1;
else {
char c = byte & 0xff;
rb_str_buf_cat(res, &c, 1);
byte = 0;
}
}
if (len & 7) {
char c;
byte <<= 7 - (len & 7);
c = byte & 0xff;
rb_str_buf_cat(res, &c, 1);
}
len = j;
goto grow;
}
break;
case 'h':
{
int byte = 0;
long i, j = 0;
if (len > plen) {
j = (len - plen + 1)/2;
len = plen;
}
for (i=0; i++ < len; ptr++) {
if (ISALPHA(*ptr))
byte |= (((*ptr & 15) + 9) & 15) << 4;
else
byte |= (*ptr & 15) << 4;
if (i & 1)
byte >>= 4;
else {
char c = byte & 0xff;
rb_str_buf_cat(res, &c, 1);
byte = 0;
}
}
if (len & 1) {
char c = byte & 0xff;
rb_str_buf_cat(res, &c, 1);
}
len = j;
goto grow;
}
break;
case 'H':
{
int byte = 0;
long i, j = 0;
if (len > plen) {
j = (len - plen + 1)/2;
len = plen;
}
for (i=0; i++ < len; ptr++) {
if (ISALPHA(*ptr))
byte |= ((*ptr & 15) + 9) & 15;
else
byte |= *ptr & 15;
if (i & 1)
byte <<= 4;
else {
char c = byte & 0xff;
rb_str_buf_cat(res, &c, 1);
byte = 0;
}
}
if (len & 1) {
char c = byte & 0xff;
rb_str_buf_cat(res, &c, 1);
}
len = j;
goto grow;
}
break;
}
break;
case 'c':
case 'C':
while (len-- > 0) {
char c;
from = NEXTFROM;
if (NIL_P(from)) c = 0;
else {
c = NUM2INT(from);
}
rb_str_buf_cat(res, &c, sizeof(char));
}
break;
case 's':
case 'S':
while (len-- > 0) {
short s;
from = NEXTFROM;
if (NIL_P(from)) s = 0;
else {
s = NUM2INT(from);
}
rb_str_buf_cat(res, OFF16(&s), NATINT_LEN(short,2));
}
break;
case 'i':
case 'I':
while (len-- > 0) {
int i;
from = NEXTFROM;
if (NIL_P(from)) i = 0;
else {
i = NUM2UINT(from);
}
rb_str_buf_cat(res, (char*)&i, sizeof(int));
}
break;
case 'l':
case 'L':
while (len-- > 0) {
long l;
from = NEXTFROM;
if (NIL_P(from)) l = 0;
else {
l = NATINT_U32(from);
}
rb_str_buf_cat(res, OFF32(&l), NATINT_LEN(long,4));
}
break;
case 'q':
case 'Q':
while (len-- > 0) {
char tmp[QUAD_SIZE];
from = NEXTFROM;
if (NIL_P(from)) from = INT2FIX(0);
rb_quad_pack(tmp, from);
rb_str_buf_cat(res, (char*)&tmp, QUAD_SIZE);
}
break;
case 'n':
while (len-- > 0) {
unsigned short s;
from = NEXTFROM;
if (NIL_P(from)) s = 0;
else {
s = NUM2INT(from);
}
s = htons(s);
rb_str_buf_cat(res, OFF16B(&s), NATINT_LEN(short,2));
}
break;
case 'N':
while (len-- > 0) {
unsigned long l;
from = NEXTFROM;
if (NIL_P(from)) l = 0;
else {
l = NATINT_U32(from);
}
l = htonl(l);
rb_str_buf_cat(res, OFF32B(&l), NATINT_LEN(long,4));
}
break;
case 'v':
while (len-- > 0) {
unsigned short s;
from = NEXTFROM;
if (NIL_P(from)) s = 0;
else {
s = NUM2INT(from);
}
s = htovs(s);
rb_str_buf_cat(res, OFF16(&s), NATINT_LEN(short,2));
}
break;
case 'V':
while (len-- > 0) {
unsigned long l;
from = NEXTFROM;
if (NIL_P(from)) l = 0;
else {
l = NATINT_U32(from);
}
l = htovl(l);
rb_str_buf_cat(res, OFF32(&l), NATINT_LEN(long,4));
}
break;
case 'f':
case 'F':
while (len-- > 0) {
float f;
from = NEXTFROM;
f = RFLOAT(rb_Float(from))->value;
rb_str_buf_cat(res, (char*)&f, sizeof(float));
}
break;
case 'e':
while (len-- > 0) {
float f;
FLOAT_CONVWITH(ftmp);
from = NEXTFROM;
f = RFLOAT(rb_Float(from))->value;
f = HTOVF(f,ftmp);
rb_str_buf_cat(res, (char*)&f, sizeof(float));
}
break;
case 'E':
while (len-- > 0) {
double d;
DOUBLE_CONVWITH(dtmp);
from = NEXTFROM;
d = RFLOAT(rb_Float(from))->value;
d = HTOVD(d,dtmp);
rb_str_buf_cat(res, (char*)&d, sizeof(double));
}
break;
case 'd':
case 'D':
while (len-- > 0) {
double d;
from = NEXTFROM;
d = RFLOAT(rb_Float(from))->value;
rb_str_buf_cat(res, (char*)&d, sizeof(double));
}
break;
case 'g':
while (len-- > 0) {
float f;
FLOAT_CONVWITH(ftmp);
from = NEXTFROM;
f = RFLOAT(rb_Float(from))->value;
f = HTONF(f,ftmp);
rb_str_buf_cat(res, (char*)&f, sizeof(float));
}
break;
case 'G':
while (len-- > 0) {
double d;
DOUBLE_CONVWITH(dtmp);
from = NEXTFROM;
d = RFLOAT(rb_Float(from))->value;
d = HTOND(d,dtmp);
rb_str_buf_cat(res, (char*)&d, sizeof(double));
}
break;
case 'x':
grow:
while (len >= 10) {
rb_str_buf_cat(res, nul10, 10);
len -= 10;
}
rb_str_buf_cat(res, nul10, len);
break;
case 'X':
shrink:
plen = RSTRING(res)->len;
if (plen < len)
rb_raise(rb_eArgError, "X outside of string");
RSTRING(res)->len = plen - len;
RSTRING(res)->ptr[plen - len] = '\0';
break;
case '@':
len -= RSTRING(res)->len;
if (len > 0) goto grow;
len = -len;
if (len > 0) goto shrink;
break;
case '%':
rb_raise(rb_eArgError, "%% is not supported");
break;
case 'U':
while (len-- > 0) {
unsigned long l;
char buf[8];
int le;
from = NEXTFROM;
if (NIL_P(from)) l = 0;
else {
l = NUM2ULONG(from);
}
le = uv_to_utf8(buf, l);
rb_str_buf_cat(res, (char*)buf, le);
}
break;
case 'u':
case 'm':
from = NEXTFROM;
StringValue(from);
ptr = RSTRING(from)->ptr;
plen = RSTRING(from)->len;
if (len <= 2)
len = 45;
else
len = len / 3 * 3;
while (plen > 0) {
long todo;
if (plen > len)
todo = len;
else
todo = plen;
encodes(res, ptr, todo, type);
plen -= todo;
ptr += todo;
}
break;
case 'M':
from = rb_obj_as_string(NEXTFROM);
if (len <= 1)
len = 72;
qpencode(res, from, len);
break;
case 'P':
from = THISFROM;
if (!NIL_P(from)) {
StringValue(from);
if (RSTRING(from)->len < len) {
rb_raise(rb_eArgError, "too short buffer for P(%ld for %ld)",
RSTRING(from)->len, len);
}
}
len = 1;
/* FALL THROUGH */
case 'p':
while (len-- > 0) {
char *t;
from = NEXTFROM;
if (NIL_P(from)) {
t = 0;
}
else {
t = StringValuePtr(from);
}
if (!associates) {
associates = rb_ary_new();
}
rb_ary_push(associates, from);
rb_str_buf_cat(res, (char*)&t, sizeof(char*));
}
break;
case 'w':
while (len-- > 0) {
unsigned long ul;
VALUE buf = rb_str_new(0, 0);
char c, *bufs, *bufe;
from = NEXTFROM;
if (TYPE(from) == T_BIGNUM) {
VALUE big128 = rb_uint2big(128);
while (TYPE(from) == T_BIGNUM) {
from = rb_big_divmod(from, big128);
c = NUM2INT(RARRAY(from)->ptr[1]) | 0x80; /* mod */
rb_str_buf_cat(buf, &c, sizeof(char));
from = RARRAY(from)->ptr[0]; /* div */
}
}
if (NIL_P(from)) ul = 0;
else {
ul = NUM2ULONG(from);
}
while (ul) {
c = ((ul & 0x7f) | 0x80);
rb_str_buf_cat(buf, &c, sizeof(char));
ul >>= 7;
}
if (RSTRING(buf)->len) {
bufs = RSTRING(buf)->ptr;
bufe = bufs + RSTRING(buf)->len - 1;
*bufs &= 0x7f; /* clear continue bit */
while (bufs < bufe) { /* reverse */
c = *bufs;
*bufs++ = *bufe;
*bufe-- = c;
}
rb_str_buf_cat(res, RSTRING(buf)->ptr, RSTRING(buf)->len);
}
else {
c = 0;
rb_str_buf_cat(res, &c, sizeof(char));
}
}
break;
default:
break;
}
}
if (associates) {
rb_str_associate(res, associates);
}
return res;
}
static char uu_table[] =
"`!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
static char b64_table[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static void
encodes(str, s, len, type)
VALUE str;
char *s;
long len;
int type;
{
char *buff = ALLOCA_N(char, len * 4 / 3 + 6);
long i = 0;
char *trans = type == 'u' ? uu_table : b64_table;
int padding;
if (type == 'u') {
buff[i++] = len + ' ';
padding = '`';
}
else {
padding = '=';
}
while (len >= 3) {
buff[i++] = trans[077 & (*s >> 2)];
buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
buff[i++] = trans[077 & (((s[1] << 2) & 074) | ((s[2] >> 6) & 03))];
buff[i++] = trans[077 & s[2]];
s += 3;
len -= 3;
}
if (len == 2) {
buff[i++] = trans[077 & (*s >> 2)];
buff[i++] = trans[077 & (((*s << 4) & 060) | ((s[1] >> 4) & 017))];
buff[i++] = trans[077 & (((s[1] << 2) & 074) | (('\0' >> 6) & 03))];
buff[i++] = padding;
}
else if (len == 1) {
buff[i++] = trans[077 & (*s >> 2)];
buff[i++] = trans[077 & (((*s << 4) & 060) | (('\0' >> 4) & 017))];
buff[i++] = padding;