~ubuntu-branches/ubuntu/dapper/ruby1.9/dapper

« back to all changes in this revision

Viewing changes to pack.c

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2006-05-08 22:23:12 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060508222312-w2wqeaz030ifi59j
Tags: 1.9.0+20060423-3ubuntu1
* Resynchronized with Debian.
* Only change from Debian is the addition of
  debian/patches/903_sparc_fix_define.patch to fix illegal instructions
  at runtime on sparc. (change from 1.9.0+20050921-1ubuntu1)

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
  pack.c -
4
4
 
5
5
  $Author: ocean $
6
 
  $Date: 2005/09/14 06:32:32 $
 
6
  $Date: 2006/03/01 10:06:03 $
7
7
  created at: Thu Feb 10 15:17:05 JST 1994
8
8
 
9
9
  Copyright (C) 1993-2003 Yukihiro Matsumoto
152
152
#else
153
153
#if SIZEOF_SHORT == 4   /* SIZEOF_DOUBLE == 8 && 4 == SIZEOF_SHORT */
154
154
static double
155
 
swapd(d)
156
 
    const double d;
 
155
swapd(const double d)
157
156
{
158
157
    double dtmp = d;
159
158
    unsigned short utmp[2];
186
185
#undef htonl
187
186
#endif
188
187
static int
189
 
endian()
 
188
endian(void)
190
189
{
191
190
    static int init = 0;
192
191
    static int endian_value;
345
344
    return 0;                   /* not reached */
346
345
}
347
346
 
348
 
#if SIZEOF_LONG == SIZE32 || SIZEOF_INT == SIZE32
 
347
#if SIZEOF_LONG == SIZE32
349
348
# define EXTEND32(x) 
350
349
#else
351
350
/* invariant in modulo 1<<31 */
352
 
# define EXTEND32(x) do {if (!natint) {(x) = (I32)(((1<<31)-1-(x))^~(~0<<31))}} while(0)
 
351
# define EXTEND32(x) do { if (!natint) {(x) = (((1L<<31)-1-(x))^~(~0L<<31));}} while(0)
353
352
#endif
354
353
#if SIZEOF_SHORT == SIZE16
355
354
# define EXTEND16(x) 
356
355
#else
357
 
# define EXTEND16(x) do { if (!natint) {(x) = (short)(((1<<15)-1-(x))^~(~0<<15))}} while(0)
 
356
# define EXTEND16(x) do { if (!natint) {(x) = (short)(((1<<15)-1-(x))^~(~0<<15));}} while(0)
358
357
#endif
359
358
 
360
359
#ifdef HAVE_LONG_LONG
362
361
#else
363
362
# define QUAD_SIZE 8
364
363
#endif
365
 
static char *toofew = "too few arguments";
366
364
 
367
 
static void encodes(VALUE,char*,long,int);
 
365
static void encodes(VALUE,const char*,long,int);
368
366
static void qpencode(VALUE,VALUE,long);
369
367
 
370
368
static int uv_to_utf8(char*,unsigned long);
371
 
static unsigned long utf8_to_uv(char*,long*);
 
369
static unsigned long utf8_to_uv(const char*,long*);
372
370
 
373
371
/*
374
372
 *  call-seq:
438
436
static VALUE
439
437
pack_pack(VALUE ary, VALUE fmt)
440
438
{
441
 
    static char *nul10 = "\0\0\0\0\0\0\0\0\0\0";
442
 
    static char *spc10 = "          ";
443
 
    char *p, *pend;
 
439
    static const char nul10[] = "\0\0\0\0\0\0\0\0\0\0";
 
440
    static const char spc10[] = "          ";
 
441
    const char *p, *pend;
444
442
    VALUE res, from, associates = 0;
445
443
    char type;
446
444
    long items, len, idx, plen;
447
 
    char *ptr;
 
445
    const char *ptr;
448
446
#ifdef NATINT_PACK
449
447
    int natint;         /* native integer */
450
448
#endif
458
456
    idx = 0;
459
457
 
460
458
#define THISFROM RARRAY(ary)->ptr[idx]
461
 
#define NEXTFROM (items-- > 0 ? RARRAY(ary)->ptr[idx++] : (rb_raise(rb_eArgError, toofew),0))
 
459
#define NEXTFROM (items-- > 0 ? RARRAY(ary)->ptr[idx++] : (rb_raise(rb_eArgError, "too few arguments"),0))
462
460
 
463
461
    while (p < pend) {
464
462
        if (RSTRING(fmt)->ptr + RSTRING(fmt)->len != pend) {
999
997
    return res;
1000
998
}
1001
999
 
1002
 
static char uu_table[] =
 
1000
static const char uu_table[] =
1003
1001
"`!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
1004
 
static char b64_table[] =
 
1002
static const char b64_table[] =
1005
1003
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
1006
1004
 
1007
1005
static void
1008
 
encodes(VALUE str, char *s, long len, int type)
 
1006
encodes(VALUE str, const char *s, long len, int type)
1009
1007
{
1010
1008
    char *buff = ALLOCA_N(char, len * 4 / 3 + 6);
1011
1009
    long i = 0;
1012
 
    char *trans = type == 'u' ? uu_table : b64_table;
 
1010
    const char *trans = type == 'u' ? uu_table : b64_table;
1013
1011
    int padding;
1014
1012
 
1015
1013
    if (type == 'u') {
1043
1041
    rb_str_buf_cat(str, buff, i);
1044
1042
}
1045
1043
 
1046
 
static char hex_table[] = "0123456789ABCDEF";
 
1044
static const char hex_table[] = "0123456789ABCDEF";
1047
1045
 
1048
1046
static void
1049
1047
qpencode(VALUE str, VALUE from, long len)
1937
1935
          case 'w':
1938
1936
            {
1939
1937
                unsigned long ul = 0;
1940
 
                unsigned long ulmask = 0xfeL << ((sizeof(unsigned long) - 1) * 8);
 
1938
                unsigned long ulmask = 0xfeUL << ((sizeof(unsigned long) - 1) * 8);
1941
1939
 
1942
1940
                while (len > 0 && s < send) {
1943
1941
                    ul <<= 7;
2020
2018
    rb_raise(rb_eRangeError, "pack(U): value out of range");
2021
2019
}
2022
2020
 
2023
 
static const long utf8_limits[] = {
 
2021
static const unsigned long utf8_limits[] = {
2024
2022
    0x0,                        /* 1 */
2025
2023
    0x80,                       /* 2 */
2026
2024
    0x800,                      /* 3 */
2031
2029
};
2032
2030
 
2033
2031
static unsigned long
2034
 
utf8_to_uv(char *p, long *lenp)
 
2032
utf8_to_uv(const char *p, long *lenp)
2035
2033
{
2036
2034
    int c = *p++ & 0xff;
2037
2035
    unsigned long uv = c;
2056
2054
        rb_raise(rb_eArgError, "malformed UTF-8 character");
2057
2055
    }
2058
2056
    if (n > *lenp) {
2059
 
        rb_raise(rb_eArgError, "malformed UTF-8 character (expected %d bytes, given %d bytes)",
 
2057
        rb_raise(rb_eArgError, "malformed UTF-8 character (expected %ld bytes, given %ld bytes)",
2060
2058
                 n, *lenp);
2061
2059
    }
2062
2060
    *lenp = n--;