2000-05-01 13:42:38 +04:00
|
|
|
/**********************************************************************
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
pack.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
$Date$
|
|
|
|
created at: Thu Feb 10 15:17:05 JST 1994
|
|
|
|
|
2003-01-16 10:34:03 +03:00
|
|
|
Copyright (C) 1993-2003 Yukihiro Matsumoto
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2000-05-01 13:42:38 +04:00
|
|
|
**********************************************************************/
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
#include "ruby.h"
|
|
|
|
#include <sys/types.h>
|
1999-01-20 07:59:39 +03:00
|
|
|
#include <ctype.h>
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
#define SIZE16 2
|
|
|
|
#define SIZE32 4
|
|
|
|
|
|
|
|
#if SIZEOF_SHORT != 2 || SIZEOF_LONG != 4
|
|
|
|
# define NATINT_PACK
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef NATINT_PACK
|
2004-01-05 13:01:54 +03:00
|
|
|
# define OFF16B(p) ((char*)(p) + (natint?0:(sizeof(short) - SIZE16)))
|
|
|
|
# define OFF32B(p) ((char*)(p) + (natint?0:(sizeof(long) - SIZE32)))
|
1999-08-13 09:45:20 +04:00
|
|
|
# define NATINT_LEN(type,len) (natint?sizeof(type):(len))
|
2000-05-24 08:34:26 +04:00
|
|
|
# ifdef WORDS_BIGENDIAN
|
2004-01-05 13:01:54 +03:00
|
|
|
# define OFF16(p) OFF16B(p)
|
|
|
|
# define OFF32(p) OFF32B(p)
|
1999-10-04 08:51:08 +04:00
|
|
|
# endif
|
2003-12-01 11:42:53 +03:00
|
|
|
# define NATINT_HTOVS(x) (natint?htovs(x):htov16(x))
|
|
|
|
# define NATINT_HTOVL(x) (natint?htovl(x):htov32(x))
|
|
|
|
# define NATINT_HTONS(x) (natint?htons(x):hton16(x))
|
|
|
|
# define NATINT_HTONL(x) (natint?htonl(x):hton32(x))
|
1999-08-13 09:45:20 +04:00
|
|
|
#else
|
|
|
|
# define NATINT_LEN(type,len) sizeof(type)
|
2003-12-01 11:42:53 +03:00
|
|
|
# define NATINT_HTOVS(x) htovs(x)
|
|
|
|
# define NATINT_HTOVL(x) htovl(x)
|
|
|
|
# define NATINT_HTONS(x) htons(x)
|
|
|
|
# define NATINT_HTONL(x) htonl(x)
|
1999-08-13 09:45:20 +04:00
|
|
|
#endif
|
|
|
|
|
1999-10-04 08:51:08 +04:00
|
|
|
#ifndef OFF16
|
|
|
|
# define OFF16(p) (char*)(p)
|
|
|
|
# define OFF32(p) (char*)(p)
|
|
|
|
#endif
|
2004-01-05 13:01:54 +03:00
|
|
|
#ifndef OFF16B
|
|
|
|
# define OFF16B(p) (char*)(p)
|
|
|
|
# define OFF32B(p) (char*)(p)
|
|
|
|
#endif
|
1999-10-04 08:51:08 +04:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
#define define_swapx(x, xtype) \
|
|
|
|
static xtype \
|
|
|
|
TOKEN_PASTE(swap,x)(z) \
|
|
|
|
xtype z; \
|
|
|
|
{ \
|
|
|
|
xtype r; \
|
|
|
|
xtype *zp; \
|
|
|
|
unsigned char *s, *t; \
|
|
|
|
int i; \
|
|
|
|
\
|
2003-01-14 10:45:19 +03:00
|
|
|
zp = malloc(sizeof(xtype)); \
|
1999-08-13 09:45:20 +04:00
|
|
|
*zp = z; \
|
2003-01-14 10:45:19 +03:00
|
|
|
s = (unsigned char*)zp; \
|
|
|
|
t = malloc(sizeof(xtype)); \
|
1999-08-13 09:45:20 +04:00
|
|
|
for (i=0; i<sizeof(xtype); i++) { \
|
|
|
|
t[sizeof(xtype)-i-1] = s[i]; \
|
|
|
|
} \
|
|
|
|
r = *(xtype *)t; \
|
|
|
|
free(t); \
|
|
|
|
free(zp); \
|
|
|
|
return r; \
|
|
|
|
}
|
|
|
|
|
2003-12-08 06:47:04 +03:00
|
|
|
#ifndef swap16
|
2003-12-01 11:42:53 +03:00
|
|
|
#define swap16(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
|
2003-12-08 06:47:04 +03:00
|
|
|
#endif
|
1999-08-13 09:45:20 +04:00
|
|
|
#if SIZEOF_SHORT == 2
|
2003-12-01 11:42:53 +03:00
|
|
|
#define swaps(x) swap16(x)
|
1999-08-13 09:45:20 +04:00
|
|
|
#else
|
|
|
|
#if SIZEOF_SHORT == 4
|
|
|
|
#define swaps(x) ((((x)&0xFF)<<24) \
|
2000-11-20 10:31:55 +03:00
|
|
|
|(((x)>>24)&0xFF) \
|
|
|
|
|(((x)&0x0000FF00)<<8) \
|
|
|
|
|(((x)&0x00FF0000)>>8) )
|
1999-08-13 09:45:20 +04:00
|
|
|
#else
|
2003-12-23 13:02:17 +03:00
|
|
|
define_swapx(s,short)
|
1999-08-13 09:45:20 +04:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2003-12-08 06:47:04 +03:00
|
|
|
#ifndef swap32
|
2003-12-01 11:42:53 +03:00
|
|
|
#define swap32(x) ((((x)&0xFF)<<24) \
|
2000-11-20 10:31:55 +03:00
|
|
|
|(((x)>>24)&0xFF) \
|
|
|
|
|(((x)&0x0000FF00)<<8) \
|
|
|
|
|(((x)&0x00FF0000)>>8) )
|
2003-12-08 06:47:04 +03:00
|
|
|
#endif
|
2003-12-01 11:42:53 +03:00
|
|
|
#if SIZEOF_LONG == 4
|
|
|
|
#define swapl(x) swap32(x)
|
1999-08-13 09:45:20 +04:00
|
|
|
#else
|
|
|
|
#if SIZEOF_LONG == 8
|
|
|
|
#define swapl(x) ((((x)&0x00000000000000FF)<<56) \
|
2000-11-20 10:31:55 +03:00
|
|
|
|(((x)&0xFF00000000000000)>>56) \
|
|
|
|
|(((x)&0x000000000000FF00)<<40) \
|
|
|
|
|(((x)&0x00FF000000000000)>>40) \
|
|
|
|
|(((x)&0x0000000000FF0000)<<24) \
|
|
|
|
|(((x)&0x0000FF0000000000)>>24) \
|
|
|
|
|(((x)&0x00000000FF000000)<<8) \
|
|
|
|
|(((x)&0x000000FF00000000)>>8))
|
1999-08-13 09:45:20 +04:00
|
|
|
#else
|
2003-12-23 13:02:17 +03:00
|
|
|
define_swapx(l,long)
|
1999-08-13 09:45:20 +04:00
|
|
|
#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 */
|
2003-12-23 13:02:17 +03:00
|
|
|
define_swapx(f,float)
|
1999-08-13 09:45:20 +04:00
|
|
|
#endif /* #if SIZEOF_SHORT == 4 */
|
|
|
|
#endif /* #if SIZEOF_LONG == 4 */
|
|
|
|
#else /* SIZEOF_FLOAT != 4 */
|
2003-12-23 13:02:17 +03:00
|
|
|
define_swapx(f,float)
|
1999-08-13 09:45:20 +04:00
|
|
|
#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 */
|
2003-12-23 13:02:17 +03:00
|
|
|
define_swapx(d, double)
|
1999-08-13 09:45:20 +04:00
|
|
|
#endif /* #if SIZEOF_SHORT == 4 */
|
|
|
|
#endif /* #if SIZEOF_LONG == 4 */
|
|
|
|
#endif /* #if SIZEOF_LONG == 8 */
|
|
|
|
#else /* SIZEOF_DOUBLE != 8 */
|
2003-12-23 13:02:17 +03:00
|
|
|
define_swapx(d, double)
|
2004-11-10 10:17:53 +03:00
|
|
|
#endif /* #if SIZEOF_DOUBLE == 8 */
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
#undef define_swapx
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
#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))
|
1999-08-13 09:45:20 +04:00
|
|
|
#define ntohf(x) (endian()?(x):swapf(x))
|
|
|
|
#define ntohd(x) (endian()?(x):swapd(x))
|
1998-01-16 15:13:05 +03:00
|
|
|
#define htons(x) (endian()?(x):swaps(x))
|
|
|
|
#define htonl(x) (endian()?(x):swapl(x))
|
1999-08-13 09:45:20 +04:00
|
|
|
#define htonf(x) (endian()?(x):swapf(x))
|
|
|
|
#define htond(x) (endian()?(x):swapd(x))
|
1998-01-16 15:13:05 +03:00
|
|
|
#define htovs(x) (endian()?swaps(x):(x))
|
|
|
|
#define htovl(x) (endian()?swapl(x):(x))
|
1999-08-13 09:45:20 +04:00
|
|
|
#define htovf(x) (endian()?swapf(x):(x))
|
|
|
|
#define htovd(x) (endian()?swapd(x):(x))
|
1998-01-16 15:13:05 +03:00
|
|
|
#define vtohs(x) (endian()?swaps(x):(x))
|
|
|
|
#define vtohl(x) (endian()?swapl(x):(x))
|
1999-08-13 09:45:20 +04:00
|
|
|
#define vtohf(x) (endian()?swapf(x):(x))
|
|
|
|
#define vtohd(x) (endian()?swapd(x):(x))
|
2003-12-01 11:42:53 +03:00
|
|
|
# ifdef NATINT_PACK
|
|
|
|
#define htov16(x) (endian()?swap16(x):(x))
|
|
|
|
#define htov32(x) (endian()?swap32(x):(x))
|
|
|
|
#define hton16(x) (endian()?(x):swap16(x))
|
|
|
|
#define hton32(x) (endian()?(x):swap32(x))
|
|
|
|
# endif
|
1998-01-16 15:13:05 +03:00
|
|
|
#else
|
|
|
|
#ifdef WORDS_BIGENDIAN
|
|
|
|
#ifndef ntohs
|
|
|
|
#define ntohs(x) (x)
|
|
|
|
#define ntohl(x) (x)
|
|
|
|
#define htons(x) (x)
|
|
|
|
#define htonl(x) (x)
|
|
|
|
#endif
|
1999-08-13 09:45:20 +04:00
|
|
|
#define ntohf(x) (x)
|
|
|
|
#define ntohd(x) (x)
|
|
|
|
#define htonf(x) (x)
|
|
|
|
#define htond(x) (x)
|
1998-01-16 15:13:05 +03:00
|
|
|
#define htovs(x) swaps(x)
|
|
|
|
#define htovl(x) swapl(x)
|
1999-08-13 09:45:20 +04:00
|
|
|
#define htovf(x) swapf(x)
|
|
|
|
#define htovd(x) swapd(x)
|
1998-01-16 15:13:05 +03:00
|
|
|
#define vtohs(x) swaps(x)
|
|
|
|
#define vtohl(x) swapl(x)
|
1999-08-13 09:45:20 +04:00
|
|
|
#define vtohf(x) swapf(x)
|
|
|
|
#define vtohd(x) swapd(x)
|
2003-12-01 11:42:53 +03:00
|
|
|
# ifdef NATINT_PACK
|
|
|
|
#define htov16(x) swap16(x)
|
|
|
|
#define htov32(x) swap32(x)
|
|
|
|
#define hton16(x) (x)
|
|
|
|
#define hton32(x) (x)
|
|
|
|
# endif
|
1998-01-16 15:13:05 +03:00
|
|
|
#else /* LITTLE ENDIAN */
|
2004-11-10 10:17:53 +03:00
|
|
|
#ifdef ntohs
|
1999-08-13 09:45:20 +04:00
|
|
|
#undef ntohs
|
|
|
|
#undef ntohl
|
|
|
|
#undef htons
|
|
|
|
#undef htonl
|
2004-11-14 18:41:40 +03:00
|
|
|
#endif
|
1998-01-16 15:13:05 +03:00
|
|
|
#define ntohs(x) swaps(x)
|
|
|
|
#define ntohl(x) swapl(x)
|
|
|
|
#define htons(x) swaps(x)
|
|
|
|
#define htonl(x) swapl(x)
|
1999-08-13 09:45:20 +04:00
|
|
|
#define ntohf(x) swapf(x)
|
|
|
|
#define ntohd(x) swapd(x)
|
|
|
|
#define htonf(x) swapf(x)
|
|
|
|
#define htond(x) swapd(x)
|
1998-01-16 15:13:05 +03:00
|
|
|
#define htovs(x) (x)
|
|
|
|
#define htovl(x) (x)
|
1999-08-13 09:45:20 +04:00
|
|
|
#define htovf(x) (x)
|
|
|
|
#define htovd(x) (x)
|
1998-01-16 15:13:05 +03:00
|
|
|
#define vtohs(x) (x)
|
|
|
|
#define vtohl(x) (x)
|
1999-08-13 09:45:20 +04:00
|
|
|
#define vtohf(x) (x)
|
|
|
|
#define vtohd(x) (x)
|
2003-12-01 11:42:53 +03:00
|
|
|
# ifdef NATINT_PACK
|
|
|
|
#define htov16(x) (x)
|
|
|
|
#define htov32(x) (x)
|
|
|
|
#define hton16(x) swap16(x)
|
|
|
|
#define hton32(x) swap32(x)
|
|
|
|
# endif
|
1999-08-13 09:45:20 +04:00
|
|
|
#endif
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
#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)
|
1998-01-16 15:13:05 +03:00
|
|
|
#endif
|
|
|
|
|
2004-01-22 21:06:38 +03:00
|
|
|
unsigned long rb_big2ulong_pack _((VALUE x));
|
2002-11-22 17:30:33 +03:00
|
|
|
|
2004-01-22 21:06:38 +03:00
|
|
|
static unsigned long
|
2002-11-22 17:30:33 +03:00
|
|
|
num2i32(x)
|
|
|
|
VALUE x;
|
|
|
|
{
|
2004-01-22 21:06:38 +03:00
|
|
|
x = rb_to_int(x); /* is nil OK? (should not) */
|
2002-11-22 17:30:33 +03:00
|
|
|
|
2004-01-22 21:06:38 +03:00
|
|
|
if (FIXNUM_P(x)) return FIX2LONG(x);
|
|
|
|
if (TYPE(x) == T_BIGNUM) {
|
|
|
|
return rb_big2ulong_pack(x);
|
2002-11-22 17:30:33 +03:00
|
|
|
}
|
* dln.c, io.c, pack.c, lib/benchmark.rb, lib/cgi.rb, lib/csv.rb,
lib/date.rb, lib/ftools.rb, lib/getoptlong.rb, lib/logger.rb,
lib/matrix.rb, lib/monitor.rb, lib/set.rb, lib/thwait.rb,
lib/timeout.rb, lib/yaml.rb, lib/drb/drb.rb, lib/irb/workspace.rb,
lib/net/ftp.rb, lib/net/http.rb, lib/net/imap.rb, lib/net/pop.rb,
lib/net/telnet.rb, lib/racc/parser.rb, lib/rinda/rinda.rb,
lib/rinda/tuplespace.rb, lib/shell/command-processor.rb,
lib/soap/rpc/soaplet.rb, lib/test/unit/testcase.rb,
lib/test/unit/testsuite.rb: typo fix.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@6178 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2004-04-19 03:19:47 +04:00
|
|
|
rb_raise(rb_eTypeError, "cannot convert %s to `integer'", rb_obj_classname(x));
|
2004-01-22 21:06:38 +03:00
|
|
|
return 0; /* not reached */
|
2002-11-22 17:30:33 +03:00
|
|
|
}
|
2000-10-16 13:13:20 +04:00
|
|
|
|
2003-05-23 10:16:20 +04:00
|
|
|
#if SIZEOF_LONG == SIZE32 || SIZEOF_INT == SIZE32
|
2003-12-26 18:34:33 +03:00
|
|
|
# define EXTEND32(x)
|
2003-05-23 10:16:20 +04:00
|
|
|
#else
|
|
|
|
/* invariant in modulo 1<<31 */
|
2003-12-26 18:34:33 +03:00
|
|
|
# define EXTEND32(x) do {if (!natint) {(x) = (I32)(((1<<31)-1-(x))^~(~0<<31))}} while(0)
|
2003-05-23 10:16:20 +04:00
|
|
|
#endif
|
|
|
|
#if SIZEOF_SHORT == SIZE16
|
2003-12-26 18:34:33 +03:00
|
|
|
# define EXTEND16(x)
|
2003-05-23 10:16:20 +04:00
|
|
|
#else
|
2003-12-26 18:34:33 +03:00
|
|
|
# define EXTEND16(x) do { if (!natint) {(x) = (short)(((1<<15)-1-(x))^~(~0<<15))}} while(0)
|
2003-05-23 10:16:20 +04:00
|
|
|
#endif
|
|
|
|
|
2002-02-13 12:01:11 +03:00
|
|
|
#ifdef HAVE_LONG_LONG
|
|
|
|
# define QUAD_SIZE sizeof(LONG_LONG)
|
|
|
|
#else
|
|
|
|
# define QUAD_SIZE 8
|
|
|
|
#endif
|
1998-01-16 15:13:05 +03:00
|
|
|
static char *toofew = "too few arguments";
|
|
|
|
|
2002-08-28 12:05:23 +04:00
|
|
|
static void encodes _((VALUE,char*,long,int));
|
|
|
|
static void qpencode _((VALUE,VALUE,long));
|
1999-01-20 07:59:39 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static int uv_to_utf8 _((char*,unsigned long));
|
2002-08-28 12:05:23 +04:00
|
|
|
static unsigned long utf8_to_uv _((char*,long*));
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2003-12-19 00:08:25 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* arr.pack ( aTemplateString ) -> aBinaryString
|
|
|
|
*
|
|
|
|
* Packs the contents of <i>arr</i> into a binary sequence according to
|
|
|
|
* the directives in <i>aTemplateString</i> (see the table below)
|
|
|
|
* Directives ``A,'' ``a,'' and ``Z'' may be followed by a count,
|
|
|
|
* which gives the width of the resulting field. The remaining
|
|
|
|
* directives also may take a count, indicating the number of array
|
|
|
|
* elements to convert. If the count is an asterisk
|
|
|
|
* (``<code>*</code>''), all remaining array elements will be
|
|
|
|
* converted. Any of the directives ``<code>sSiIlL</code>'' may be
|
|
|
|
* followed by an underscore (``<code>_</code>'') to use the underlying
|
|
|
|
* platform's native size for the specified type; otherwise, they use a
|
|
|
|
* platform-independent size. Spaces are ignored in the template
|
|
|
|
* string. See also <code>String#unpack</code>.
|
|
|
|
*
|
|
|
|
* a = [ "a", "b", "c" ]
|
|
|
|
* n = [ 65, 66, 67 ]
|
|
|
|
* a.pack("A3A3A3") #=> "a b c "
|
|
|
|
* a.pack("a3a3a3") #=> "a\000\000b\000\000c\000\000"
|
|
|
|
* n.pack("ccc") #=> "ABC"
|
|
|
|
*
|
2004-07-09 17:38:34 +04:00
|
|
|
* Directives for +pack+.
|
2003-12-19 00:08:25 +03:00
|
|
|
*
|
|
|
|
* Directive Meaning
|
|
|
|
* ---------------------------------------------------------------
|
|
|
|
* @ | Moves to absolute position
|
|
|
|
* A | ASCII string (space padded, count is width)
|
|
|
|
* a | ASCII string (null padded, count is width)
|
|
|
|
* B | Bit string (descending bit order)
|
|
|
|
* b | Bit string (ascending bit order)
|
|
|
|
* C | Unsigned char
|
|
|
|
* c | Char
|
|
|
|
* D, d | Double-precision float, native format
|
|
|
|
* E | Double-precision float, little-endian byte order
|
|
|
|
* e | Single-precision float, little-endian byte order
|
|
|
|
* F, f | Single-precision float, native format
|
|
|
|
* G | Double-precision float, network (big-endian) byte order
|
|
|
|
* g | Single-precision float, network (big-endian) byte order
|
|
|
|
* H | Hex string (high nibble first)
|
|
|
|
* h | Hex string (low nibble first)
|
|
|
|
* I | Unsigned integer
|
|
|
|
* i | Integer
|
|
|
|
* L | Unsigned long
|
|
|
|
* l | Long
|
|
|
|
* M | Quoted printable, MIME encoding (see RFC2045)
|
|
|
|
* m | Base64 encoded string
|
|
|
|
* N | Long, network (big-endian) byte order
|
|
|
|
* n | Short, network (big-endian) byte-order
|
|
|
|
* P | Pointer to a structure (fixed-length string)
|
|
|
|
* p | Pointer to a null-terminated string
|
|
|
|
* Q, q | 64-bit number
|
|
|
|
* S | Unsigned short
|
|
|
|
* s | Short
|
|
|
|
* U | UTF-8
|
|
|
|
* u | UU-encoded string
|
|
|
|
* V | Long, little-endian byte order
|
|
|
|
* v | Short, little-endian byte order
|
|
|
|
* w | BER-compressed integer\fnm
|
|
|
|
* X | Back up a byte
|
|
|
|
* x | Null byte
|
2004-05-13 06:04:20 +04:00
|
|
|
* Z | Same as ``a'', except that null is added with *
|
2003-12-19 00:08:25 +03:00
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
|
|
|
pack_pack(ary, fmt)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE ary, fmt;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
static char *nul10 = "\0\0\0\0\0\0\0\0\0\0";
|
|
|
|
static char *spc10 = " ";
|
1999-01-20 07:59:39 +03:00
|
|
|
char *p, *pend;
|
2001-08-06 19:05:50 +04:00
|
|
|
VALUE res, from, associates = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
char type;
|
2002-08-21 19:47:54 +04:00
|
|
|
long items, len, idx, plen;
|
1999-01-20 07:59:39 +03:00
|
|
|
char *ptr;
|
1999-08-13 09:45:20 +04:00
|
|
|
#ifdef NATINT_PACK
|
|
|
|
int natint; /* native integer */
|
|
|
|
#endif
|
2001-05-02 08:22:21 +04:00
|
|
|
|
|
|
|
StringValue(fmt);
|
|
|
|
p = RSTRING(fmt)->ptr;
|
|
|
|
pend = p + RSTRING(fmt)->len;
|
2001-05-30 13:12:34 +04:00
|
|
|
res = rb_str_buf_new(0);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
items = RARRAY(ary)->len;
|
1998-01-16 15:13:05 +03:00
|
|
|
idx = 0;
|
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
#define THISFROM RARRAY(ary)->ptr[idx]
|
1999-01-20 07:59:39 +03:00
|
|
|
#define NEXTFROM (items-- > 0 ? RARRAY(ary)->ptr[idx++] : (rb_raise(rb_eArgError, toofew),0))
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
while (p < pend) {
|
2004-10-08 07:36:54 +04:00
|
|
|
if (RSTRING(fmt)->ptr + RSTRING(fmt)->len != pend) {
|
|
|
|
rb_raise(rb_eRuntimeError, "format string modified");
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
type = *p++; /* get data type */
|
1999-08-13 09:45:20 +04:00
|
|
|
#ifdef NATINT_PACK
|
|
|
|
natint = 0;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (ISSPACE(type)) continue;
|
2002-02-18 12:52:48 +03:00
|
|
|
if (type == '#') {
|
2002-02-18 14:51:10 +03:00
|
|
|
while ((p < pend) && (*p != '\n')) {
|
2002-02-18 12:52:48 +03:00
|
|
|
p++;
|
|
|
|
}
|
2002-02-21 10:15:06 +03:00
|
|
|
continue;
|
2002-02-18 12:52:48 +03:00
|
|
|
}
|
2000-07-27 13:49:34 +04:00
|
|
|
if (*p == '_' || *p == '!') {
|
2004-01-22 21:06:38 +03:00
|
|
|
const char *natstr = "sSiIlL";
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
if (strchr(natstr, type)) {
|
|
|
|
#ifdef NATINT_PACK
|
|
|
|
natint = 1;
|
|
|
|
#endif
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
else {
|
2000-07-27 13:49:34 +04:00
|
|
|
rb_raise(rb_eArgError, "'%c' allowed only after types %s", *p, natstr);
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
if (*p == '*') { /* set data length */
|
|
|
|
len = strchr("@Xxu", type) ? 0 : items;
|
|
|
|
p++;
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
else if (ISDIGIT(*p)) {
|
1998-01-16 15:13:05 +03:00
|
|
|
len = strtoul(p, (char**)&p, 10);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
len = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
1999-08-13 09:45:20 +04:00
|
|
|
case 'A': case 'a': case 'Z':
|
1998-01-16 15:13:05 +03:00
|
|
|
case 'B': case 'b':
|
|
|
|
case 'H': case 'h':
|
|
|
|
from = NEXTFROM;
|
|
|
|
if (NIL_P(from)) {
|
1999-01-20 07:59:39 +03:00
|
|
|
ptr = "";
|
1998-01-16 15:13:05 +03:00
|
|
|
plen = 0;
|
|
|
|
}
|
|
|
|
else {
|
2001-05-02 08:22:21 +04:00
|
|
|
StringValue(from);
|
|
|
|
ptr = RSTRING(from)->ptr;
|
|
|
|
plen = RSTRING(from)->len;
|
2002-05-21 09:39:19 +04:00
|
|
|
OBJ_INFECT(res, from);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (p[-1] == '*')
|
|
|
|
len = plen;
|
|
|
|
|
|
|
|
switch (type) {
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'a': /* arbitrary binary string (null padded) */
|
|
|
|
case 'A': /* ASCII string (space padded) */
|
|
|
|
case 'Z': /* null terminated ASCII string */
|
2004-05-13 06:04:20 +04:00
|
|
|
if (plen >= len) {
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, ptr, len);
|
2004-05-13 06:04:20 +04:00
|
|
|
if (p[-1] == '*' && type == 'Z')
|
|
|
|
rb_str_buf_cat(res, nul10, 1);
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
else {
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, ptr, plen);
|
1998-01-16 15:13:05 +03:00
|
|
|
len -= plen;
|
|
|
|
while (len >= 10) {
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, (type == 'A')?spc10:nul10, 10);
|
1998-01-16 15:13:05 +03:00
|
|
|
len -= 10;
|
|
|
|
}
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, (type == 'A')?spc10:nul10, len);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'b': /* bit string (ascending) */
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
int byte = 0;
|
2002-08-28 12:05:23 +04:00
|
|
|
long i, j = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
if (len > plen) {
|
|
|
|
j = (len - plen + 1)/2;
|
|
|
|
len = plen;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
for (i=0; i++ < len; ptr++) {
|
|
|
|
if (*ptr & 1)
|
|
|
|
byte |= 128;
|
|
|
|
if (i & 7)
|
|
|
|
byte >>= 1;
|
|
|
|
else {
|
|
|
|
char c = byte & 0xff;
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, &c, 1);
|
1998-01-16 15:13:05 +03:00
|
|
|
byte = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (len & 7) {
|
|
|
|
char c;
|
|
|
|
byte >>= 7 - (len & 7);
|
|
|
|
c = byte & 0xff;
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, &c, 1);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2003-03-29 14:17:55 +03:00
|
|
|
len = j;
|
|
|
|
goto grow;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'B': /* bit string (descending) */
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
int byte = 0;
|
2002-08-28 12:05:23 +04:00
|
|
|
long i, j = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
if (len > plen) {
|
|
|
|
j = (len - plen + 1)/2;
|
|
|
|
len = plen;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
for (i=0; i++ < len; ptr++) {
|
|
|
|
byte |= *ptr & 1;
|
|
|
|
if (i & 7)
|
|
|
|
byte <<= 1;
|
|
|
|
else {
|
|
|
|
char c = byte & 0xff;
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, &c, 1);
|
1998-01-16 15:13:05 +03:00
|
|
|
byte = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (len & 7) {
|
|
|
|
char c;
|
|
|
|
byte <<= 7 - (len & 7);
|
|
|
|
c = byte & 0xff;
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, &c, 1);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2003-03-29 14:17:55 +03:00
|
|
|
len = j;
|
|
|
|
goto grow;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'h': /* hex string (low nibble first) */
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
int byte = 0;
|
2002-08-28 12:05:23 +04:00
|
|
|
long i, j = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
if (len > plen) {
|
|
|
|
j = (len - plen + 1)/2;
|
|
|
|
len = plen;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
for (i=0; i++ < len; ptr++) {
|
1999-08-13 09:45:20 +04:00
|
|
|
if (ISALPHA(*ptr))
|
|
|
|
byte |= (((*ptr & 15) + 9) & 15) << 4;
|
|
|
|
else
|
|
|
|
byte |= (*ptr & 15) << 4;
|
|
|
|
if (i & 1)
|
|
|
|
byte >>= 4;
|
|
|
|
else {
|
|
|
|
char c = byte & 0xff;
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, &c, 1);
|
1999-08-13 09:45:20 +04:00
|
|
|
byte = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (len & 1) {
|
|
|
|
char c = byte & 0xff;
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, &c, 1);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2003-04-07 10:58:31 +04:00
|
|
|
len = j;
|
2003-03-29 14:17:55 +03:00
|
|
|
goto grow;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'H': /* hex string (high nibble first) */
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
int byte = 0;
|
2002-08-28 12:05:23 +04:00
|
|
|
long i, j = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
if (len > plen) {
|
|
|
|
j = (len - plen + 1)/2;
|
|
|
|
len = plen;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
for (i=0; i++ < len; ptr++) {
|
1999-08-13 09:45:20 +04:00
|
|
|
if (ISALPHA(*ptr))
|
|
|
|
byte |= ((*ptr & 15) + 9) & 15;
|
|
|
|
else
|
|
|
|
byte |= *ptr & 15;
|
|
|
|
if (i & 1)
|
|
|
|
byte <<= 4;
|
|
|
|
else {
|
|
|
|
char c = byte & 0xff;
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, &c, 1);
|
1999-08-13 09:45:20 +04:00
|
|
|
byte = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (len & 1) {
|
|
|
|
char c = byte & 0xff;
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, &c, 1);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2003-03-29 14:17:55 +03:00
|
|
|
len = j;
|
|
|
|
goto grow;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'c': /* signed char */
|
|
|
|
case 'C': /* unsigned char */
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
|
|
|
char c;
|
|
|
|
|
|
|
|
from = NEXTFROM;
|
2004-01-22 21:06:38 +03:00
|
|
|
c = num2i32(from);
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, &c, sizeof(char));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 's': /* signed short */
|
|
|
|
case 'S': /* unsigned short */
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
|
|
|
short s;
|
|
|
|
|
|
|
|
from = NEXTFROM;
|
2004-01-22 21:06:38 +03:00
|
|
|
s = num2i32(from);
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, OFF16(&s), NATINT_LEN(short,2));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'i': /* signed int */
|
|
|
|
case 'I': /* unsigned int */
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
2003-12-01 11:42:53 +03:00
|
|
|
long i;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
from = NEXTFROM;
|
2004-01-22 21:06:38 +03:00
|
|
|
i = num2i32(from);
|
2003-12-01 11:42:53 +03:00
|
|
|
rb_str_buf_cat(res, OFF32(&i), NATINT_LEN(int,4));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'l': /* signed long */
|
|
|
|
case 'L': /* unsigned long */
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
|
|
|
long l;
|
|
|
|
|
|
|
|
from = NEXTFROM;
|
2004-01-22 21:06:38 +03:00
|
|
|
l = num2i32(from);
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, OFF32(&l), NATINT_LEN(long,4));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'q': /* signed quad (64bit) int */
|
|
|
|
case 'Q': /* unsigned quad (64bit) int */
|
2002-02-13 12:01:11 +03:00
|
|
|
while (len-- > 0) {
|
|
|
|
char tmp[QUAD_SIZE];
|
|
|
|
|
|
|
|
from = NEXTFROM;
|
|
|
|
rb_quad_pack(tmp, from);
|
|
|
|
rb_str_buf_cat(res, (char*)&tmp, QUAD_SIZE);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'n': /* unsigned short (network byte-order) */
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
|
|
|
unsigned short s;
|
|
|
|
|
|
|
|
from = NEXTFROM;
|
2004-01-22 21:06:38 +03:00
|
|
|
s = num2i32(from);
|
2003-12-01 11:42:53 +03:00
|
|
|
s = NATINT_HTONS(s);
|
2003-12-23 22:53:45 +03:00
|
|
|
rb_str_buf_cat(res, OFF16(&s), NATINT_LEN(short,2));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'N': /* unsigned long (network byte-order) */
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
|
|
|
unsigned long l;
|
|
|
|
|
|
|
|
from = NEXTFROM;
|
2004-01-22 21:06:38 +03:00
|
|
|
l = num2i32(from);
|
2003-12-01 11:42:53 +03:00
|
|
|
l = NATINT_HTONL(l);
|
2003-12-23 22:53:45 +03:00
|
|
|
rb_str_buf_cat(res, OFF32(&l), NATINT_LEN(long,4));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'v': /* unsigned short (VAX byte-order) */
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
|
|
|
unsigned short s;
|
|
|
|
|
|
|
|
from = NEXTFROM;
|
2004-01-22 21:06:38 +03:00
|
|
|
s = num2i32(from);
|
2003-12-01 11:42:53 +03:00
|
|
|
s = NATINT_HTOVS(s);
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, OFF16(&s), NATINT_LEN(short,2));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'V': /* unsigned long (VAX byte-order) */
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
|
|
|
unsigned long l;
|
|
|
|
|
|
|
|
from = NEXTFROM;
|
2004-01-22 21:06:38 +03:00
|
|
|
l = num2i32(from);
|
2003-12-01 11:42:53 +03:00
|
|
|
l = NATINT_HTOVL(l);
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, OFF32(&l), NATINT_LEN(long,4));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'f': /* single precision float in native format */
|
|
|
|
case 'F': /* ditto */
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
|
|
|
float f;
|
|
|
|
|
|
|
|
from = NEXTFROM;
|
2001-09-03 09:37:42 +04:00
|
|
|
f = RFLOAT(rb_Float(from))->value;
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, (char*)&f, sizeof(float));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'e': /* single precision float in VAX byte-order */
|
1999-08-13 09:45:20 +04:00
|
|
|
while (len-- > 0) {
|
|
|
|
float f;
|
|
|
|
FLOAT_CONVWITH(ftmp);
|
|
|
|
|
|
|
|
from = NEXTFROM;
|
2001-09-03 09:37:42 +04:00
|
|
|
f = RFLOAT(rb_Float(from))->value;
|
1999-08-13 09:45:20 +04:00
|
|
|
f = HTOVF(f,ftmp);
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, (char*)&f, sizeof(float));
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'E': /* double precision float in VAX byte-order */
|
1999-08-13 09:45:20 +04:00
|
|
|
while (len-- > 0) {
|
|
|
|
double d;
|
|
|
|
DOUBLE_CONVWITH(dtmp);
|
|
|
|
|
|
|
|
from = NEXTFROM;
|
2001-09-03 09:37:42 +04:00
|
|
|
d = RFLOAT(rb_Float(from))->value;
|
1999-08-13 09:45:20 +04:00
|
|
|
d = HTOVD(d,dtmp);
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, (char*)&d, sizeof(double));
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'd': /* double precision float in native format */
|
|
|
|
case 'D': /* ditto */
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
|
|
|
double d;
|
|
|
|
|
|
|
|
from = NEXTFROM;
|
2001-09-03 09:37:42 +04:00
|
|
|
d = RFLOAT(rb_Float(from))->value;
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, (char*)&d, sizeof(double));
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'g': /* single precision float in network byte-order */
|
1999-08-13 09:45:20 +04:00
|
|
|
while (len-- > 0) {
|
|
|
|
float f;
|
|
|
|
FLOAT_CONVWITH(ftmp);
|
|
|
|
|
|
|
|
from = NEXTFROM;
|
2001-09-03 09:37:42 +04:00
|
|
|
f = RFLOAT(rb_Float(from))->value;
|
1999-08-13 09:45:20 +04:00
|
|
|
f = HTONF(f,ftmp);
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, (char*)&f, sizeof(float));
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'G': /* double precision float in network byte-order */
|
1999-08-13 09:45:20 +04:00
|
|
|
while (len-- > 0) {
|
|
|
|
double d;
|
|
|
|
DOUBLE_CONVWITH(dtmp);
|
|
|
|
|
|
|
|
from = NEXTFROM;
|
2001-09-03 09:37:42 +04:00
|
|
|
d = RFLOAT(rb_Float(from))->value;
|
1999-08-13 09:45:20 +04:00
|
|
|
d = HTOND(d,dtmp);
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, (char*)&d, sizeof(double));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'x': /* null byte */
|
1998-01-16 15:13:05 +03:00
|
|
|
grow:
|
|
|
|
while (len >= 10) {
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, nul10, 10);
|
1998-01-16 15:13:05 +03:00
|
|
|
len -= 10;
|
|
|
|
}
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, nul10, len);
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'X': /* back up byte */
|
1998-01-16 15:13:05 +03:00
|
|
|
shrink:
|
2001-05-30 13:12:34 +04:00
|
|
|
plen = RSTRING(res)->len;
|
|
|
|
if (plen < len)
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eArgError, "X outside of string");
|
2001-05-30 13:12:34 +04:00
|
|
|
RSTRING(res)->len = plen - len;
|
|
|
|
RSTRING(res)->ptr[plen - len] = '\0';
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case '@': /* null fill to absolute position */
|
1998-01-16 15:13:05 +03:00
|
|
|
len -= RSTRING(res)->len;
|
|
|
|
if (len > 0) goto grow;
|
|
|
|
len = -len;
|
|
|
|
if (len > 0) goto shrink;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case '%':
|
1999-10-04 08:51:08 +04:00
|
|
|
rb_raise(rb_eArgError, "%% is not supported");
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'U': /* Unicode character */
|
1999-08-13 09:45:20 +04:00
|
|
|
while (len-- > 0) {
|
2003-10-09 21:45:53 +04:00
|
|
|
long l;
|
1999-08-13 09:45:20 +04:00
|
|
|
char buf[8];
|
|
|
|
int le;
|
|
|
|
|
|
|
|
from = NEXTFROM;
|
2004-03-31 06:59:57 +04:00
|
|
|
from = rb_to_int(from);
|
2004-04-07 10:30:15 +04:00
|
|
|
l = NUM2INT(from);
|
2004-04-07 06:51:05 +04:00
|
|
|
if (l < 0) {
|
2004-03-31 06:59:57 +04:00
|
|
|
rb_raise(rb_eRangeError, "pack(U): value out of range");
|
|
|
|
}
|
2004-04-07 06:51:05 +04:00
|
|
|
le = uv_to_utf8(buf, l);
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, (char*)buf, le);
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'u': /* uuencoded string */
|
|
|
|
case 'm': /* base64 encoded string */
|
2001-05-02 08:22:21 +04:00
|
|
|
from = NEXTFROM;
|
|
|
|
StringValue(from);
|
|
|
|
ptr = RSTRING(from)->ptr;
|
|
|
|
plen = RSTRING(from)->len;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-01-05 18:24:13 +03:00
|
|
|
if (len <= 2)
|
1998-01-16 15:13:05 +03:00
|
|
|
len = 45;
|
|
|
|
else
|
|
|
|
len = len / 3 * 3;
|
|
|
|
while (plen > 0) {
|
2002-08-28 12:05:23 +04:00
|
|
|
long todo;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (plen > len)
|
|
|
|
todo = len;
|
|
|
|
else
|
|
|
|
todo = plen;
|
1998-01-16 15:19:22 +03:00
|
|
|
encodes(res, ptr, todo, type);
|
1998-01-16 15:13:05 +03:00
|
|
|
plen -= todo;
|
|
|
|
ptr += todo;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'M': /* quoted-printable encoded string */
|
1999-01-20 07:59:39 +03:00
|
|
|
from = rb_obj_as_string(NEXTFROM);
|
|
|
|
if (len <= 1)
|
|
|
|
len = 72;
|
|
|
|
qpencode(res, from, len);
|
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'P': /* pointer to packed byte string */
|
2001-05-02 08:22:21 +04:00
|
|
|
from = THISFROM;
|
|
|
|
if (!NIL_P(from)) {
|
|
|
|
StringValue(from);
|
|
|
|
if (RSTRING(from)->len < len) {
|
2002-05-28 22:11:07 +04:00
|
|
|
rb_raise(rb_eArgError, "too short buffer for P(%ld for %ld)",
|
2001-05-02 08:22:21 +04:00
|
|
|
RSTRING(from)->len, len);
|
|
|
|
}
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
len = 1;
|
|
|
|
/* FALL THROUGH */
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'p': /* pointer to string */
|
1999-01-20 07:59:39 +03:00
|
|
|
while (len-- > 0) {
|
|
|
|
char *t;
|
|
|
|
from = NEXTFROM;
|
2001-01-15 10:01:00 +03:00
|
|
|
if (NIL_P(from)) {
|
2001-05-02 08:22:21 +04:00
|
|
|
t = 0;
|
|
|
|
}
|
|
|
|
else {
|
2003-06-23 10:52:39 +04:00
|
|
|
t = StringValuePtr(from);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2001-08-06 19:05:50 +04:00
|
|
|
if (!associates) {
|
|
|
|
associates = rb_ary_new();
|
|
|
|
}
|
|
|
|
rb_ary_push(associates, from);
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, (char*)&t, sizeof(char*));
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'w': /* BER compressed integer */
|
2000-05-12 13:07:57 +04:00
|
|
|
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 */
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(buf, &c, sizeof(char));
|
2000-05-12 13:07:57 +04:00
|
|
|
from = RARRAY(from)->ptr[0]; /* div */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-01-22 21:06:38 +03:00
|
|
|
{
|
2003-10-09 21:45:53 +04:00
|
|
|
long l = NUM2LONG(from);
|
|
|
|
if (l < 0) {
|
|
|
|
rb_raise(rb_eArgError, "cannot compress negative numbers");
|
|
|
|
}
|
|
|
|
ul = l;
|
2000-05-12 13:07:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
while (ul) {
|
|
|
|
c = ((ul & 0x7f) | 0x80);
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(buf, &c, sizeof(char));
|
2000-05-12 13:07:57 +04:00
|
|
|
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;
|
|
|
|
}
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, RSTRING(buf)->ptr, RSTRING(buf)->len);
|
2000-05-12 13:07:57 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
c = 0;
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(res, &c, sizeof(char));
|
2000-05-12 13:07:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2001-08-06 19:05:50 +04:00
|
|
|
|
|
|
|
if (associates) {
|
|
|
|
rb_str_associate(res, associates);
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
static char uu_table[] =
|
|
|
|
"`!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
|
|
|
|
static char b64_table[] =
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static void
|
1998-01-16 15:19:22 +03:00
|
|
|
encodes(str, s, len, type)
|
|
|
|
VALUE str;
|
1999-01-20 07:59:39 +03:00
|
|
|
char *s;
|
2002-08-28 12:05:23 +04:00
|
|
|
long len;
|
1998-01-16 15:19:22 +03:00
|
|
|
int type;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
char *buff = ALLOCA_N(char, len * 4 / 3 + 6);
|
2002-08-28 12:05:23 +04:00
|
|
|
long i = 0;
|
1998-01-16 15:19:22 +03:00
|
|
|
char *trans = type == 'u' ? uu_table : b64_table;
|
|
|
|
int padding;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
if (type == 'u') {
|
1999-01-20 07:59:39 +03:00
|
|
|
buff[i++] = len + ' ';
|
1998-01-16 15:19:22 +03:00
|
|
|
padding = '`';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
padding = '=';
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
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]];
|
1998-01-16 15:13:05 +03:00
|
|
|
s += 3;
|
|
|
|
len -= 3;
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
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;
|
|
|
|
buff[i++] = padding;
|
|
|
|
}
|
|
|
|
buff[i++] = '\n';
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(str, buff, i);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static char hex_table[] = "0123456789ABCDEF";
|
|
|
|
|
|
|
|
static void
|
|
|
|
qpencode(str, from, len)
|
|
|
|
VALUE str, from;
|
2002-08-28 12:05:23 +04:00
|
|
|
long len;
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
|
|
|
char buff[1024];
|
2002-08-28 12:05:23 +04:00
|
|
|
long i = 0, n = 0, prev = EOF;
|
1999-08-13 09:45:20 +04:00
|
|
|
unsigned char *s = (unsigned char*)RSTRING(from)->ptr;
|
1999-01-20 07:59:39 +03:00
|
|
|
unsigned char *send = s + RSTRING(from)->len;
|
|
|
|
|
|
|
|
while (s < send) {
|
|
|
|
if ((*s > 126) ||
|
|
|
|
(*s < 32 && *s != '\n' && *s != '\t') ||
|
|
|
|
(*s == '=')) {
|
|
|
|
buff[i++] = '=';
|
|
|
|
buff[i++] = hex_table[*s >> 4];
|
|
|
|
buff[i++] = hex_table[*s & 0x0f];
|
|
|
|
n += 3;
|
|
|
|
prev = EOF;
|
|
|
|
}
|
|
|
|
else if (*s == '\n') {
|
|
|
|
if (prev == ' ' || prev == '\t') {
|
|
|
|
buff[i++] = '=';
|
|
|
|
buff[i++] = *s;
|
|
|
|
}
|
|
|
|
buff[i++] = *s;
|
|
|
|
n = 0;
|
|
|
|
prev = *s;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
buff[i++] = *s;
|
|
|
|
n++;
|
|
|
|
prev = *s;
|
|
|
|
}
|
|
|
|
if (n > len) {
|
|
|
|
buff[i++] = '=';
|
|
|
|
buff[i++] = '\n';
|
|
|
|
n = 0;
|
|
|
|
prev = '\n';
|
|
|
|
}
|
|
|
|
if (i > 1024 - 5) {
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(str, buff, i);
|
1999-01-20 07:59:39 +03:00
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
if (n > 0) {
|
|
|
|
buff[i++] = '=';
|
|
|
|
buff[i++] = '\n';
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
if (i > 0) {
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(str, buff, i);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-10 13:07:31 +03:00
|
|
|
static inline int
|
1999-01-20 07:59:39 +03:00
|
|
|
hex2num(c)
|
|
|
|
char c;
|
|
|
|
{
|
|
|
|
switch (c) {
|
|
|
|
case '0': case '1': case '2': case '3': case '4':
|
|
|
|
case '5': case '6': case '7': case '8': case '9':
|
|
|
|
return c - '0';
|
|
|
|
case 'a': case 'b': case 'c':
|
|
|
|
case 'd': case 'e': case 'f':
|
|
|
|
return c - 'a' + 10;
|
|
|
|
case 'A': case 'B': case 'C':
|
|
|
|
case 'D': case 'E': case 'F':
|
|
|
|
return c - 'A' + 10;
|
|
|
|
default:
|
|
|
|
return -1;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-02-13 12:01:11 +03:00
|
|
|
#define PACK_LENGTH_ADJUST_SIZE(sz) do { \
|
1999-08-13 09:45:20 +04:00
|
|
|
tmp = 0; \
|
2002-02-13 12:01:11 +03:00
|
|
|
if (len > (send-s)/sz) { \
|
1999-08-13 09:45:20 +04:00
|
|
|
if (!star) { \
|
2002-02-13 12:01:11 +03:00
|
|
|
tmp = len-(send-s)/sz; \
|
1999-08-13 09:45:20 +04:00
|
|
|
} \
|
2002-02-13 12:01:11 +03:00
|
|
|
len = (send-s)/sz; \
|
1999-08-13 09:45:20 +04:00
|
|
|
} \
|
|
|
|
} while (0)
|
2002-02-13 12:01:11 +03:00
|
|
|
|
|
|
|
#ifdef NATINT_PACK
|
|
|
|
#define PACK_LENGTH_ADJUST(type,sz) do { \
|
|
|
|
int t__len = NATINT_LEN(type,(sz)); \
|
|
|
|
PACK_LENGTH_ADJUST_SIZE(t__len); \
|
|
|
|
} while (0)
|
1999-08-13 09:45:20 +04:00
|
|
|
#else
|
2002-04-25 17:57:01 +04:00
|
|
|
#define PACK_LENGTH_ADJUST(type,sz) \
|
|
|
|
PACK_LENGTH_ADJUST_SIZE(sizeof(type))
|
1999-08-13 09:45:20 +04:00
|
|
|
#endif
|
|
|
|
|
2002-04-25 17:57:01 +04:00
|
|
|
#define PACK_ITEM_ADJUST() while (tmp--) rb_ary_push(ary, Qnil)
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2002-05-21 09:39:19 +04:00
|
|
|
static VALUE
|
|
|
|
infected_str_new(ptr, len, str)
|
|
|
|
const char *ptr;
|
|
|
|
long len;
|
|
|
|
VALUE str;
|
|
|
|
{
|
|
|
|
VALUE s = rb_str_new(ptr, len);
|
|
|
|
|
|
|
|
OBJ_INFECT(s, str);
|
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
2003-12-19 00:08:25 +03:00
|
|
|
/*
|
|
|
|
* call-seq:
|
|
|
|
* str.unpack(format) => anArray
|
|
|
|
*
|
|
|
|
* Decodes <i>str</i> (which may contain binary data) according to the
|
|
|
|
* format string, returning an array of each value extracted. The
|
|
|
|
* format string consists of a sequence of single-character directives,
|
|
|
|
* summarized in the table at the end of this entry.
|
|
|
|
* Each directive may be followed
|
|
|
|
* by a number, indicating the number of times to repeat with this
|
|
|
|
* directive. An asterisk (``<code>*</code>'') will use up all
|
|
|
|
* remaining elements. The directives <code>sSiIlL</code> may each be
|
|
|
|
* followed by an underscore (``<code>_</code>'') to use the underlying
|
|
|
|
* platform's native size for the specified type; otherwise, it uses a
|
|
|
|
* platform-independent consistent size. Spaces are ignored in the
|
|
|
|
* format string. See also <code>Array#pack</code>.
|
|
|
|
*
|
|
|
|
* "abc \0\0abc \0\0".unpack('A6Z6') #=> ["abc", "abc "]
|
|
|
|
* "abc \0\0".unpack('a3a3') #=> ["abc", " \000\000"]
|
2004-05-13 06:04:20 +04:00
|
|
|
* "abc \0abc \0".unpack('Z*Z*') #=> ["abc ", "abc "]
|
2003-12-19 00:08:25 +03:00
|
|
|
* "aa".unpack('b8B8') #=> ["10000110", "01100001"]
|
|
|
|
* "aaa".unpack('h2H2c') #=> ["16", "61", 97]
|
|
|
|
* "\xfe\xff\xfe\xff".unpack('sS') #=> [-2, 65534]
|
|
|
|
* "now=20is".unpack('M*') #=> ["now is"]
|
|
|
|
* "whole".unpack('xax2aX2aX1aX2a') #=> ["h", "e", "l", "l", "o"]
|
|
|
|
*
|
|
|
|
* This table summarizes the various formats and the Ruby classes
|
|
|
|
* returned by each.
|
|
|
|
*
|
|
|
|
* Format | Returns | Function
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* A | String | with trailing nulls and spaces removed
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* a | String | string
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* B | String | extract bits from each character (msb first)
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* b | String | extract bits from each character (lsb first)
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* C | Fixnum | extract a character as an unsigned integer
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* c | Fixnum | extract a character as an integer
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* d,D | Float | treat sizeof(double) characters as
|
|
|
|
* | | a native double
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* E | Float | treat sizeof(double) characters as
|
|
|
|
* | | a double in little-endian byte order
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* e | Float | treat sizeof(float) characters as
|
|
|
|
* | | a float in little-endian byte order
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* f,F | Float | treat sizeof(float) characters as
|
|
|
|
* | | a native float
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* G | Float | treat sizeof(double) characters as
|
|
|
|
* | | a double in network byte order
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* g | Float | treat sizeof(float) characters as a
|
|
|
|
* | | float in network byte order
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* H | String | extract hex nibbles from each character
|
|
|
|
* | | (most significant first)
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* h | String | extract hex nibbles from each character
|
|
|
|
* | | (least significant first)
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* I | Integer | treat sizeof(int) (modified by _)
|
|
|
|
* | | successive characters as an unsigned
|
|
|
|
* | | native integer
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* i | Integer | treat sizeof(int) (modified by _)
|
|
|
|
* | | successive characters as a signed
|
|
|
|
* | | native integer
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* L | Integer | treat four (modified by _) successive
|
|
|
|
* | | characters as an unsigned native
|
|
|
|
* | | long integer
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* l | Integer | treat four (modified by _) successive
|
|
|
|
* | | characters as a signed native
|
|
|
|
* | | long integer
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* M | String | quoted-printable
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* m | String | base64-encoded
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* N | Integer | treat four characters as an unsigned
|
|
|
|
* | | long in network byte order
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* n | Fixnum | treat two characters as an unsigned
|
|
|
|
* | | short in network byte order
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* P | String | treat sizeof(char *) characters as a
|
|
|
|
* | | pointer, and return \emph{len} characters
|
|
|
|
* | | from the referenced location
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* p | String | treat sizeof(char *) characters as a
|
|
|
|
* | | pointer to a null-terminated string
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* Q | Integer | treat 8 characters as an unsigned
|
|
|
|
* | | quad word (64 bits)
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* q | Integer | treat 8 characters as a signed
|
|
|
|
* | | quad word (64 bits)
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* S | Fixnum | treat two (different if _ used)
|
|
|
|
* | | successive characters as an unsigned
|
|
|
|
* | | short in native byte order
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* s | Fixnum | Treat two (different if _ used)
|
|
|
|
* | | successive characters as a signed short
|
|
|
|
* | | in native byte order
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* U | Integer | UTF-8 characters as unsigned integers
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* u | String | UU-encoded
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* V | Fixnum | treat four characters as an unsigned
|
|
|
|
* | | long in little-endian byte order
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* v | Fixnum | treat two characters as an unsigned
|
|
|
|
* | | short in little-endian byte order
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* w | Integer | BER-compressed integer (see Array.pack)
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* X | --- | skip backward one character
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* x | --- | skip forward one character
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* Z | String | with trailing nulls removed
|
2004-05-13 06:04:20 +04:00
|
|
|
* | | upto first null with *
|
2003-12-19 00:08:25 +03:00
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
* @ | --- | skip to the offset given by the
|
|
|
|
* | | length argument
|
|
|
|
* -------+---------+-----------------------------------------
|
|
|
|
*/
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
|
|
|
pack_unpack(str, fmt)
|
1998-01-16 15:19:22 +03:00
|
|
|
VALUE str, fmt;
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
|
|
|
static char *hexdigits = "0123456789abcdef0123456789ABCDEFx";
|
1999-01-20 07:59:39 +03:00
|
|
|
char *s, *send;
|
|
|
|
char *p, *pend;
|
1998-01-16 15:13:05 +03:00
|
|
|
VALUE ary;
|
|
|
|
char type;
|
2002-08-21 19:47:54 +04:00
|
|
|
long len;
|
|
|
|
int tmp, star;
|
1999-08-13 09:45:20 +04:00
|
|
|
#ifdef NATINT_PACK
|
|
|
|
int natint; /* native integer */
|
|
|
|
#endif
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
StringValue(str);
|
2004-10-07 08:06:41 +04:00
|
|
|
StringValue(fmt);
|
2001-05-02 08:22:21 +04:00
|
|
|
s = RSTRING(str)->ptr;
|
|
|
|
send = s + RSTRING(str)->len;
|
|
|
|
p = RSTRING(fmt)->ptr;
|
|
|
|
pend = p + RSTRING(fmt)->len;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
ary = rb_ary_new();
|
1998-01-16 15:13:05 +03:00
|
|
|
while (p < pend) {
|
2002-02-18 12:52:48 +03:00
|
|
|
type = *p++;
|
1999-08-13 09:45:20 +04:00
|
|
|
#ifdef NATINT_PACK
|
|
|
|
natint = 0;
|
|
|
|
#endif
|
2002-02-18 12:52:48 +03:00
|
|
|
|
|
|
|
if (ISSPACE(type)) continue;
|
|
|
|
if (type == '#') {
|
2002-02-18 14:51:10 +03:00
|
|
|
while ((p < pend) && (*p != '\n')) {
|
2002-02-18 12:52:48 +03:00
|
|
|
p++;
|
|
|
|
}
|
2002-02-21 10:15:06 +03:00
|
|
|
continue;
|
2002-02-18 12:52:48 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
star = 0;
|
2000-07-27 13:49:34 +04:00
|
|
|
if (*p == '_' || *p == '!') {
|
1999-08-13 09:45:20 +04:00
|
|
|
char *natstr = "sSiIlL";
|
|
|
|
|
|
|
|
if (strchr(natstr, type)) {
|
|
|
|
#ifdef NATINT_PACK
|
|
|
|
natint = 1;
|
|
|
|
#endif
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
else {
|
2000-07-27 13:49:34 +04:00
|
|
|
rb_raise(rb_eArgError, "'%c' allowed only after types %s", *p, natstr);
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (p >= pend)
|
|
|
|
len = 1;
|
|
|
|
else if (*p == '*') {
|
|
|
|
star = 1;
|
1998-01-16 15:13:05 +03:00
|
|
|
len = send - s;
|
|
|
|
p++;
|
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
else if (ISDIGIT(*p)) {
|
1998-01-16 15:13:05 +03:00
|
|
|
len = strtoul(p, (char**)&p, 10);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
len = (type != '@');
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case '%':
|
1999-10-04 08:51:08 +04:00
|
|
|
rb_raise(rb_eArgError, "%% is not supported");
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'A':
|
|
|
|
if (len > send - s) len = send - s;
|
|
|
|
{
|
2002-08-28 12:05:23 +04:00
|
|
|
long end = len;
|
1999-01-20 07:59:39 +03:00
|
|
|
char *t = s + len - 1;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
while (t >= s) {
|
|
|
|
if (*t != ' ' && *t != '\0') break;
|
1999-08-13 09:45:20 +04:00
|
|
|
t--; len--;
|
|
|
|
}
|
2002-05-21 09:39:19 +04:00
|
|
|
rb_ary_push(ary, infected_str_new(s, len, str));
|
1999-08-13 09:45:20 +04:00
|
|
|
s += end;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'Z':
|
|
|
|
{
|
2004-05-13 06:04:20 +04:00
|
|
|
char *t = s;
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2004-05-13 06:04:20 +04:00
|
|
|
if (len > send-s) len = send-s;
|
|
|
|
while (t < s+len && *t) t++;
|
|
|
|
rb_ary_push(ary, infected_str_new(s, t-s, str));
|
|
|
|
if (t < send) t++;
|
|
|
|
s = star ? t : s+len;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'a':
|
|
|
|
if (len > send - s) len = send - s;
|
2002-05-21 09:39:19 +04:00
|
|
|
rb_ary_push(ary, infected_str_new(s, len, str));
|
1998-01-16 15:13:05 +03:00
|
|
|
s += len;
|
|
|
|
break;
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
case 'b':
|
|
|
|
{
|
|
|
|
VALUE bitstr;
|
1999-01-20 07:59:39 +03:00
|
|
|
char *t;
|
2002-08-28 12:05:23 +04:00
|
|
|
int bits;
|
|
|
|
long i;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (p[-1] == '*' || len > (send - s) * 8)
|
|
|
|
len = (send - s) * 8;
|
|
|
|
bits = 0;
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(ary, bitstr = rb_str_new(0, len));
|
1998-01-16 15:13:05 +03:00
|
|
|
t = RSTRING(bitstr)->ptr;
|
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
if (i & 7) bits >>= 1;
|
|
|
|
else bits = *s++;
|
|
|
|
*t++ = (bits & 1) ? '1' : '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'B':
|
|
|
|
{
|
|
|
|
VALUE bitstr;
|
1999-01-20 07:59:39 +03:00
|
|
|
char *t;
|
2002-08-28 12:05:23 +04:00
|
|
|
int bits;
|
|
|
|
long i;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (p[-1] == '*' || len > (send - s) * 8)
|
|
|
|
len = (send - s) * 8;
|
|
|
|
bits = 0;
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(ary, bitstr = rb_str_new(0, len));
|
1998-01-16 15:13:05 +03:00
|
|
|
t = RSTRING(bitstr)->ptr;
|
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
if (i & 7) bits <<= 1;
|
|
|
|
else bits = *s++;
|
|
|
|
*t++ = (bits & 128) ? '1' : '0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'h':
|
|
|
|
{
|
|
|
|
VALUE bitstr;
|
1999-01-20 07:59:39 +03:00
|
|
|
char *t;
|
2002-08-28 12:05:23 +04:00
|
|
|
int bits;
|
|
|
|
long i;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (p[-1] == '*' || len > (send - s) * 2)
|
|
|
|
len = (send - s) * 2;
|
|
|
|
bits = 0;
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(ary, bitstr = rb_str_new(0, len));
|
1998-01-16 15:13:05 +03:00
|
|
|
t = RSTRING(bitstr)->ptr;
|
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
if (i & 1)
|
|
|
|
bits >>= 4;
|
|
|
|
else
|
|
|
|
bits = *s++;
|
|
|
|
*t++ = hexdigits[bits & 15];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'H':
|
|
|
|
{
|
|
|
|
VALUE bitstr;
|
1999-01-20 07:59:39 +03:00
|
|
|
char *t;
|
2002-08-28 12:05:23 +04:00
|
|
|
int bits;
|
|
|
|
long i;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (p[-1] == '*' || len > (send - s) * 2)
|
|
|
|
len = (send - s) * 2;
|
|
|
|
bits = 0;
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(ary, bitstr = rb_str_new(0, len));
|
1998-01-16 15:13:05 +03:00
|
|
|
t = RSTRING(bitstr)->ptr;
|
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
if (i & 1)
|
|
|
|
bits <<= 4;
|
|
|
|
else
|
|
|
|
bits = *s++;
|
|
|
|
*t++ = hexdigits[(bits >> 4) & 15];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'c':
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_LENGTH_ADJUST(char,sizeof(char));
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
|
|
|
int c = *s++;
|
|
|
|
if (c > (char)127) c-=256;
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(ary, INT2FIX(c));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_ITEM_ADJUST();
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'C':
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_LENGTH_ADJUST(unsigned char,sizeof(unsigned char));
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
1999-01-20 07:59:39 +03:00
|
|
|
unsigned char c = *s++;
|
|
|
|
rb_ary_push(ary, INT2FIX(c));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_ITEM_ADJUST();
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 's':
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_LENGTH_ADJUST(short,2);
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
1999-10-04 08:51:08 +04:00
|
|
|
short tmp = 0;
|
|
|
|
memcpy(OFF16(&tmp), s, NATINT_LEN(short,2));
|
2003-12-26 18:34:33 +03:00
|
|
|
EXTEND16(tmp);
|
1999-08-13 09:45:20 +04:00
|
|
|
s += NATINT_LEN(short,2);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(ary, INT2FIX(tmp));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_ITEM_ADJUST();
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'S':
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_LENGTH_ADJUST(unsigned short,2);
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
1999-10-04 08:51:08 +04:00
|
|
|
unsigned short tmp = 0;
|
|
|
|
memcpy(OFF16(&tmp), s, NATINT_LEN(unsigned short,2));
|
1999-08-13 09:45:20 +04:00
|
|
|
s += NATINT_LEN(unsigned short,2);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(ary, INT2FIX(tmp));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_ITEM_ADJUST();
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'i':
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_LENGTH_ADJUST(int,sizeof(int));
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
|
|
|
int tmp;
|
|
|
|
memcpy(&tmp, s, sizeof(int));
|
|
|
|
s += sizeof(int);
|
2002-08-28 12:05:23 +04:00
|
|
|
rb_ary_push(ary, INT2NUM(tmp));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_ITEM_ADJUST();
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'I':
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_LENGTH_ADJUST(unsigned int,sizeof(unsigned int));
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
|
|
|
unsigned int tmp;
|
1999-08-13 09:45:20 +04:00
|
|
|
memcpy(&tmp, s, sizeof(unsigned int));
|
|
|
|
s += sizeof(unsigned int);
|
2002-08-28 12:05:23 +04:00
|
|
|
rb_ary_push(ary, UINT2NUM(tmp));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_ITEM_ADJUST();
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'l':
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_LENGTH_ADJUST(long,4);
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
1999-10-04 08:51:08 +04:00
|
|
|
long tmp = 0;
|
|
|
|
memcpy(OFF32(&tmp), s, NATINT_LEN(long,4));
|
2003-12-26 18:34:33 +03:00
|
|
|
EXTEND32(tmp);
|
1999-08-13 09:45:20 +04:00
|
|
|
s += NATINT_LEN(long,4);
|
2002-08-28 12:05:23 +04:00
|
|
|
rb_ary_push(ary, LONG2NUM(tmp));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_ITEM_ADJUST();
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
case 'L':
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_LENGTH_ADJUST(unsigned long,4);
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
1999-10-04 08:51:08 +04:00
|
|
|
unsigned long tmp = 0;
|
|
|
|
memcpy(OFF32(&tmp), s, NATINT_LEN(unsigned long,4));
|
1999-08-13 09:45:20 +04:00
|
|
|
s += NATINT_LEN(unsigned long,4);
|
2002-08-28 12:05:23 +04:00
|
|
|
rb_ary_push(ary, ULONG2NUM(tmp));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_ITEM_ADJUST();
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
2002-02-13 12:01:11 +03:00
|
|
|
case 'q':
|
|
|
|
PACK_LENGTH_ADJUST_SIZE(QUAD_SIZE);
|
|
|
|
while (len-- > 0) {
|
|
|
|
char *tmp = (char*)s;
|
|
|
|
s += QUAD_SIZE;
|
|
|
|
rb_ary_push(ary, rb_quad_unpack(tmp, 1));
|
|
|
|
}
|
|
|
|
PACK_ITEM_ADJUST();
|
|
|
|
break;
|
|
|
|
case 'Q':
|
|
|
|
PACK_LENGTH_ADJUST_SIZE(QUAD_SIZE);
|
|
|
|
while (len-- > 0) {
|
|
|
|
char *tmp = (char*)s;
|
|
|
|
s += QUAD_SIZE;
|
|
|
|
rb_ary_push(ary, rb_quad_unpack(tmp, 0));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
case 'n':
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_LENGTH_ADJUST(unsigned short,2);
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
1999-10-04 08:51:08 +04:00
|
|
|
unsigned short tmp = 0;
|
2004-01-05 13:01:54 +03:00
|
|
|
memcpy(OFF16B(&tmp), s, NATINT_LEN(unsigned short,2));
|
1999-08-13 09:45:20 +04:00
|
|
|
s += NATINT_LEN(unsigned short,2);
|
2002-08-28 12:05:23 +04:00
|
|
|
rb_ary_push(ary, UINT2NUM(ntohs(tmp)));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_ITEM_ADJUST();
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'N':
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_LENGTH_ADJUST(unsigned long,4);
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
1999-10-04 08:51:08 +04:00
|
|
|
unsigned long tmp = 0;
|
2004-01-05 13:01:54 +03:00
|
|
|
memcpy(OFF32B(&tmp), s, NATINT_LEN(unsigned long,4));
|
1999-08-13 09:45:20 +04:00
|
|
|
s += NATINT_LEN(unsigned long,4);
|
2002-08-28 12:05:23 +04:00
|
|
|
rb_ary_push(ary, ULONG2NUM(ntohl(tmp)));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_ITEM_ADJUST();
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'v':
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_LENGTH_ADJUST(unsigned short,2);
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
1999-10-04 08:51:08 +04:00
|
|
|
unsigned short tmp = 0;
|
|
|
|
memcpy(OFF16(&tmp), s, NATINT_LEN(unsigned short,2));
|
1999-08-13 09:45:20 +04:00
|
|
|
s += NATINT_LEN(unsigned short,2);
|
2002-08-28 12:05:23 +04:00
|
|
|
rb_ary_push(ary, UINT2NUM(vtohs(tmp)));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_ITEM_ADJUST();
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'V':
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_LENGTH_ADJUST(unsigned long,4);
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
1999-10-04 08:51:08 +04:00
|
|
|
unsigned long tmp = 0;
|
|
|
|
memcpy(OFF32(&tmp), s, NATINT_LEN(long,4));
|
1999-08-13 09:45:20 +04:00
|
|
|
s += NATINT_LEN(long,4);
|
2002-08-28 12:05:23 +04:00
|
|
|
rb_ary_push(ary, ULONG2NUM(vtohl(tmp)));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_ITEM_ADJUST();
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'f':
|
|
|
|
case 'F':
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_LENGTH_ADJUST(float,sizeof(float));
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
|
|
|
float tmp;
|
|
|
|
memcpy(&tmp, s, sizeof(float));
|
|
|
|
s += sizeof(float);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(ary, rb_float_new((double)tmp));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_ITEM_ADJUST();
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
case 'e':
|
|
|
|
PACK_LENGTH_ADJUST(float,sizeof(float));
|
|
|
|
while (len-- > 0) {
|
|
|
|
float tmp;
|
|
|
|
FLOAT_CONVWITH(ftmp);
|
|
|
|
|
|
|
|
memcpy(&tmp, s, sizeof(float));
|
|
|
|
s += sizeof(float);
|
|
|
|
tmp = VTOHF(tmp,ftmp);
|
|
|
|
rb_ary_push(ary, rb_float_new((double)tmp));
|
|
|
|
}
|
|
|
|
PACK_ITEM_ADJUST();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'E':
|
|
|
|
PACK_LENGTH_ADJUST(double,sizeof(double));
|
|
|
|
while (len-- > 0) {
|
|
|
|
double tmp;
|
|
|
|
DOUBLE_CONVWITH(dtmp);
|
|
|
|
|
|
|
|
memcpy(&tmp, s, sizeof(double));
|
|
|
|
s += sizeof(double);
|
|
|
|
tmp = VTOHD(tmp,dtmp);
|
|
|
|
rb_ary_push(ary, rb_float_new(tmp));
|
|
|
|
}
|
|
|
|
PACK_ITEM_ADJUST();
|
|
|
|
break;
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
case 'D':
|
|
|
|
case 'd':
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_LENGTH_ADJUST(double,sizeof(double));
|
|
|
|
while (len-- > 0) {
|
|
|
|
double tmp;
|
|
|
|
memcpy(&tmp, s, sizeof(double));
|
|
|
|
s += sizeof(double);
|
|
|
|
rb_ary_push(ary, rb_float_new(tmp));
|
|
|
|
}
|
|
|
|
PACK_ITEM_ADJUST();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'g':
|
|
|
|
PACK_LENGTH_ADJUST(float,sizeof(float));
|
|
|
|
while (len-- > 0) {
|
|
|
|
float tmp;
|
|
|
|
FLOAT_CONVWITH(ftmp;)
|
|
|
|
|
|
|
|
memcpy(&tmp, s, sizeof(float));
|
|
|
|
s += sizeof(float);
|
|
|
|
tmp = NTOHF(tmp,ftmp);
|
|
|
|
rb_ary_push(ary, rb_float_new((double)tmp));
|
|
|
|
}
|
|
|
|
PACK_ITEM_ADJUST();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'G':
|
|
|
|
PACK_LENGTH_ADJUST(double,sizeof(double));
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
|
|
|
double tmp;
|
1999-08-13 09:45:20 +04:00
|
|
|
DOUBLE_CONVWITH(dtmp);
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
memcpy(&tmp, s, sizeof(double));
|
|
|
|
s += sizeof(double);
|
1999-08-13 09:45:20 +04:00
|
|
|
tmp = NTOHD(tmp,dtmp);
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_ary_push(ary, rb_float_new(tmp));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_ITEM_ADJUST();
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'U':
|
|
|
|
if (len > send - s) len = send - s;
|
2001-11-19 08:03:03 +03:00
|
|
|
while (len > 0 && s < send) {
|
2002-08-28 12:05:23 +04:00
|
|
|
long alen = send - s;
|
1999-08-13 09:45:20 +04:00
|
|
|
unsigned long l;
|
|
|
|
|
|
|
|
l = utf8_to_uv(s, &alen);
|
2002-04-15 11:48:47 +04:00
|
|
|
s += alen; len--;
|
2002-08-28 12:05:23 +04:00
|
|
|
rb_ary_push(ary, ULONG2NUM(l));
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'u':
|
|
|
|
{
|
2002-06-28 18:53:03 +04:00
|
|
|
VALUE buf = infected_str_new(0, (send - s)*3/4, str);
|
|
|
|
char *ptr = RSTRING(buf)->ptr;
|
2002-08-21 19:47:54 +04:00
|
|
|
long total = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
while (s < send && *s > ' ' && *s < 'a') {
|
|
|
|
long a,b,c,d;
|
|
|
|
char hunk[4];
|
|
|
|
|
|
|
|
hunk[3] = '\0';
|
|
|
|
len = (*s++ - ' ') & 077;
|
|
|
|
total += len;
|
2002-06-28 18:53:03 +04:00
|
|
|
if (total > RSTRING(buf)->len) {
|
|
|
|
len -= total - RSTRING(buf)->len;
|
|
|
|
total = RSTRING(buf)->len;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
while (len > 0) {
|
2002-08-28 12:05:23 +04:00
|
|
|
long mlen = len > 3 ? 3 : len;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
if (s < send && *s >= ' ')
|
|
|
|
a = (*s++ - ' ') & 077;
|
|
|
|
else
|
|
|
|
a = 0;
|
|
|
|
if (s < send && *s >= ' ')
|
|
|
|
b = (*s++ - ' ') & 077;
|
|
|
|
else
|
|
|
|
b = 0;
|
|
|
|
if (s < send && *s >= ' ')
|
|
|
|
c = (*s++ - ' ') & 077;
|
|
|
|
else
|
|
|
|
c = 0;
|
|
|
|
if (s < send && *s >= ' ')
|
|
|
|
d = (*s++ - ' ') & 077;
|
|
|
|
else
|
|
|
|
d = 0;
|
|
|
|
hunk[0] = a << 2 | b >> 4;
|
|
|
|
hunk[1] = b << 4 | c >> 2;
|
|
|
|
hunk[2] = c << 6 | d;
|
|
|
|
memcpy(ptr, hunk, mlen);
|
|
|
|
ptr += mlen;
|
|
|
|
len -= mlen;
|
|
|
|
}
|
|
|
|
if (*s == '\r') s++;
|
|
|
|
if (*s == '\n') s++;
|
|
|
|
else if (s < send && (s+1 == send || s[1] == '\n'))
|
|
|
|
s += 2; /* possible checksum byte */
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2002-06-28 18:53:03 +04:00
|
|
|
RSTRING(buf)->ptr[total] = '\0';
|
|
|
|
RSTRING(buf)->len = total;
|
|
|
|
rb_ary_push(ary, buf);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
case 'm':
|
|
|
|
{
|
2002-06-28 18:53:03 +04:00
|
|
|
VALUE buf = infected_str_new(0, (send - s)*3/4, str);
|
|
|
|
char *ptr = RSTRING(buf)->ptr;
|
2003-06-23 10:52:39 +04:00
|
|
|
int a = -1,b = -1,c = 0,d;
|
1998-01-16 15:19:22 +03:00
|
|
|
static int first = 1;
|
|
|
|
static int b64_xtable[256];
|
|
|
|
|
|
|
|
if (first) {
|
|
|
|
int i;
|
|
|
|
first = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
b64_xtable[i] = -1;
|
|
|
|
}
|
|
|
|
for (i = 0; i < 64; i++) {
|
1999-08-13 09:45:20 +04:00
|
|
|
b64_xtable[(int)b64_table[i]] = i;
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
|
|
|
}
|
2003-06-23 10:52:39 +04:00
|
|
|
while (s < send) {
|
1998-01-16 15:19:22 +03:00
|
|
|
while (s[0] == '\r' || s[0] == '\n') { s++; }
|
1999-08-13 09:45:20 +04:00
|
|
|
if ((a = b64_xtable[(int)s[0]]) == -1) break;
|
|
|
|
if ((b = b64_xtable[(int)s[1]]) == -1) break;
|
|
|
|
if ((c = b64_xtable[(int)s[2]]) == -1) break;
|
|
|
|
if ((d = b64_xtable[(int)s[3]]) == -1) break;
|
1998-01-16 15:19:22 +03:00
|
|
|
*ptr++ = a << 2 | b >> 4;
|
|
|
|
*ptr++ = b << 4 | c >> 2;
|
|
|
|
*ptr++ = c << 6 | d;
|
|
|
|
s += 4;
|
|
|
|
}
|
2003-06-23 10:52:39 +04:00
|
|
|
if (a != -1 && b != -1) {
|
|
|
|
if (s + 2 < send && s[2] == '=')
|
|
|
|
*ptr++ = a << 2 | b >> 4;
|
|
|
|
if (c != -1 && s + 3 < send && s[3] == '=') {
|
|
|
|
*ptr++ = a << 2 | b >> 4;
|
|
|
|
*ptr++ = b << 4 | c >> 2;
|
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
*ptr = '\0';
|
2002-06-28 18:53:03 +04:00
|
|
|
RSTRING(buf)->len = ptr - RSTRING(buf)->ptr;
|
|
|
|
rb_ary_push(ary, buf);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'M':
|
|
|
|
{
|
2002-06-28 18:53:03 +04:00
|
|
|
VALUE buf = infected_str_new(0, send - s, str);
|
|
|
|
char *ptr = RSTRING(buf)->ptr;
|
1999-01-20 07:59:39 +03:00
|
|
|
int c1, c2;
|
|
|
|
|
|
|
|
while (s < send) {
|
|
|
|
if (*s == '=') {
|
|
|
|
if (++s == send) break;
|
|
|
|
if (*s != '\n') {
|
|
|
|
if ((c1 = hex2num(*s)) == -1) break;
|
|
|
|
if (++s == send) break;
|
|
|
|
if ((c2 = hex2num(*s)) == -1) break;
|
|
|
|
*ptr++ = c1 << 4 | c2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
*ptr++ = *s;
|
|
|
|
}
|
|
|
|
s++;
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
*ptr = '\0';
|
2002-06-28 18:53:03 +04:00
|
|
|
RSTRING(buf)->len = ptr - RSTRING(buf)->ptr;
|
|
|
|
rb_ary_push(ary, buf);
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
case '@':
|
2003-06-23 10:52:39 +04:00
|
|
|
if (len > RSTRING(str)->len)
|
|
|
|
rb_raise(rb_eArgError, "@ outside of string");
|
1998-01-16 15:19:22 +03:00
|
|
|
s = RSTRING(str)->ptr + len;
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
|
|
|
|
case 'X':
|
1998-01-16 15:19:22 +03:00
|
|
|
if (len > s - RSTRING(str)->ptr)
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eArgError, "X outside of string");
|
1998-01-16 15:13:05 +03:00
|
|
|
s -= len;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'x':
|
|
|
|
if (len > send - s)
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eArgError, "x outside of string");
|
1998-01-16 15:13:05 +03:00
|
|
|
s += len;
|
|
|
|
break;
|
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
case 'P':
|
|
|
|
if (sizeof(char *) <= send - s) {
|
|
|
|
char *t;
|
2001-05-02 08:22:21 +04:00
|
|
|
VALUE tmp;
|
2001-01-15 10:01:00 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
memcpy(&t, s, sizeof(char *));
|
|
|
|
s += sizeof(char *);
|
2001-01-15 10:01:00 +03:00
|
|
|
|
|
|
|
if (t) {
|
2001-05-02 08:22:21 +04:00
|
|
|
VALUE a, *p, *pend;
|
2001-01-15 10:01:00 +03:00
|
|
|
|
2001-05-02 08:22:21 +04:00
|
|
|
if (!(a = rb_str_associated(str))) {
|
|
|
|
rb_raise(rb_eArgError, "no associated pointer");
|
|
|
|
}
|
2001-01-15 10:01:00 +03:00
|
|
|
p = RARRAY(a)->ptr;
|
|
|
|
pend = p + RARRAY(a)->len;
|
|
|
|
while (p < pend) {
|
2001-01-18 11:43:14 +03:00
|
|
|
if (TYPE(*p) == T_STRING && RSTRING(*p)->ptr == t) {
|
|
|
|
if (len > RSTRING(*p)->len) {
|
|
|
|
len = RSTRING(*p)->len;
|
|
|
|
}
|
2001-01-15 10:01:00 +03:00
|
|
|
break;
|
2001-01-18 11:43:14 +03:00
|
|
|
}
|
2001-01-15 10:01:00 +03:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if (p == pend) {
|
|
|
|
rb_raise(rb_eArgError, "non associated pointer");
|
|
|
|
}
|
2001-03-26 12:57:16 +04:00
|
|
|
tmp = rb_tainted_str_new(t, len);
|
2001-01-15 10:01:00 +03:00
|
|
|
}
|
|
|
|
else {
|
2001-05-02 08:22:21 +04:00
|
|
|
tmp = Qnil;
|
2001-01-15 10:01:00 +03:00
|
|
|
}
|
|
|
|
rb_ary_push(ary, tmp);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'p':
|
|
|
|
if (len > (send - s) / sizeof(char *))
|
|
|
|
len = (send - s) / sizeof(char *);
|
|
|
|
while (len-- > 0) {
|
|
|
|
if (send - s < sizeof(char *))
|
|
|
|
break;
|
|
|
|
else {
|
2001-05-02 08:22:21 +04:00
|
|
|
VALUE tmp;
|
1999-01-20 07:59:39 +03:00
|
|
|
char *t;
|
2001-01-18 11:43:14 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
memcpy(&t, s, sizeof(char *));
|
|
|
|
s += sizeof(char *);
|
2001-01-18 11:43:14 +03:00
|
|
|
|
2000-04-10 09:48:43 +04:00
|
|
|
if (t) {
|
2001-08-06 07:05:23 +04:00
|
|
|
VALUE a, *p, *pend;
|
2001-05-02 08:22:21 +04:00
|
|
|
|
|
|
|
if (!(a = rb_str_associated(str))) {
|
|
|
|
rb_raise(rb_eArgError, "no associated pointer");
|
|
|
|
}
|
2001-08-06 07:05:23 +04:00
|
|
|
p = RARRAY(a)->ptr;
|
2001-01-18 11:43:14 +03:00
|
|
|
pend = p + RARRAY(a)->len;
|
|
|
|
while (p < pend) {
|
|
|
|
if (TYPE(*p) == T_STRING && RSTRING(*p)->ptr == t) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
if (p == pend) {
|
|
|
|
rb_raise(rb_eArgError, "non associated pointer");
|
|
|
|
}
|
|
|
|
tmp = rb_str_new2(t);
|
2002-05-21 09:39:19 +04:00
|
|
|
OBJ_INFECT(tmp, str);
|
2000-04-10 09:48:43 +04:00
|
|
|
}
|
2001-05-02 08:22:21 +04:00
|
|
|
else {
|
|
|
|
tmp = Qnil;
|
|
|
|
}
|
2001-01-18 11:43:14 +03:00
|
|
|
rb_ary_push(ary, tmp);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2000-05-12 13:07:57 +04:00
|
|
|
case 'w':
|
|
|
|
{
|
|
|
|
unsigned long ul = 0;
|
2000-05-15 10:38:46 +04:00
|
|
|
unsigned long ulmask = 0xfeL << ((sizeof(unsigned long) - 1) * 8);
|
2000-05-12 13:07:57 +04:00
|
|
|
|
|
|
|
while (len > 0 && s < send) {
|
|
|
|
ul <<= 7;
|
|
|
|
ul |= (*s & 0x7f);
|
|
|
|
if (!(*s++ & 0x80)) {
|
2002-08-28 12:05:23 +04:00
|
|
|
rb_ary_push(ary, ULONG2NUM(ul));
|
2000-05-12 13:07:57 +04:00
|
|
|
len--;
|
|
|
|
ul = 0;
|
|
|
|
}
|
|
|
|
else if (ul & ulmask) {
|
|
|
|
VALUE big = rb_uint2big(ul);
|
|
|
|
VALUE big128 = rb_uint2big(128);
|
|
|
|
while (s < send) {
|
|
|
|
big = rb_big_mul(big, big128);
|
|
|
|
big = rb_big_plus(big, rb_uint2big(*s & 0x7f));
|
|
|
|
if (!(*s++ & 0x80)) {
|
|
|
|
rb_ary_push(ary, big);
|
|
|
|
len--;
|
|
|
|
ul = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
#define BYTEWIDTH 8
|
|
|
|
|
|
|
|
static int
|
|
|
|
uv_to_utf8(buf, uv)
|
|
|
|
char *buf;
|
|
|
|
unsigned long uv;
|
|
|
|
{
|
|
|
|
if (uv <= 0x7f) {
|
|
|
|
buf[0] = (char)uv;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (uv <= 0x7ff) {
|
|
|
|
buf[0] = ((uv>>6)&0xff)|0xc0;
|
|
|
|
buf[1] = (uv&0x3f)|0x80;
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if (uv <= 0xffff) {
|
|
|
|
buf[0] = ((uv>>12)&0xff)|0xe0;
|
|
|
|
buf[1] = ((uv>>6)&0x3f)|0x80;
|
|
|
|
buf[2] = (uv&0x3f)|0x80;
|
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
if (uv <= 0x1fffff) {
|
|
|
|
buf[0] = ((uv>>18)&0xff)|0xf0;
|
|
|
|
buf[1] = ((uv>>12)&0x3f)|0x80;
|
|
|
|
buf[2] = ((uv>>6)&0x3f)|0x80;
|
|
|
|
buf[3] = (uv&0x3f)|0x80;
|
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
if (uv <= 0x3ffffff) {
|
|
|
|
buf[0] = ((uv>>24)&0xff)|0xf8;
|
|
|
|
buf[1] = ((uv>>18)&0x3f)|0x80;
|
|
|
|
buf[2] = ((uv>>12)&0x3f)|0x80;
|
|
|
|
buf[3] = ((uv>>6)&0x3f)|0x80;
|
|
|
|
buf[4] = (uv&0x3f)|0x80;
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
if (uv <= 0x7fffffff) {
|
|
|
|
buf[0] = ((uv>>30)&0xff)|0xfc;
|
|
|
|
buf[1] = ((uv>>24)&0x3f)|0x80;
|
|
|
|
buf[2] = ((uv>>18)&0x3f)|0x80;
|
|
|
|
buf[3] = ((uv>>12)&0x3f)|0x80;
|
|
|
|
buf[4] = ((uv>>6)&0x3f)|0x80;
|
|
|
|
buf[5] = (uv&0x3f)|0x80;
|
|
|
|
return 6;
|
|
|
|
}
|
2004-03-31 06:59:57 +04:00
|
|
|
rb_raise(rb_eRangeError, "pack(U): value out of range");
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
|
2002-12-04 10:39:32 +03:00
|
|
|
static const long utf8_limits[] = {
|
|
|
|
0x0, /* 1 */
|
|
|
|
0x80, /* 2 */
|
|
|
|
0x800, /* 3 */
|
2002-12-10 11:32:54 +03:00
|
|
|
0x10000, /* 4 */
|
2002-12-04 10:39:32 +03:00
|
|
|
0x200000, /* 5 */
|
|
|
|
0x4000000, /* 6 */
|
|
|
|
0x80000000, /* 7 */
|
|
|
|
};
|
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
static unsigned long
|
|
|
|
utf8_to_uv(p, lenp)
|
|
|
|
char *p;
|
2002-08-28 12:05:23 +04:00
|
|
|
long *lenp;
|
1999-08-13 09:45:20 +04:00
|
|
|
{
|
2002-12-02 10:13:56 +03:00
|
|
|
int c = *p++ & 0xff;
|
2003-07-10 02:28:42 +04:00
|
|
|
unsigned long uv = c;
|
2002-12-02 10:13:56 +03:00
|
|
|
long n;
|
|
|
|
|
|
|
|
if (!(uv & 0x80)) {
|
|
|
|
*lenp = 1;
|
|
|
|
return uv;
|
|
|
|
}
|
|
|
|
if (!(uv & 0x40)) {
|
|
|
|
*lenp = 1;
|
2002-12-10 09:23:44 +03:00
|
|
|
rb_raise(rb_eArgError, "malformed UTF-8 character");
|
2002-12-02 10:13:56 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!(uv & 0x20)) { n = 2; uv &= 0x1f; }
|
|
|
|
else if (!(uv & 0x10)) { n = 3; uv &= 0x0f; }
|
|
|
|
else if (!(uv & 0x08)) { n = 4; uv &= 0x07; }
|
|
|
|
else if (!(uv & 0x04)) { n = 5; uv &= 0x03; }
|
|
|
|
else if (!(uv & 0x02)) { n = 6; uv &= 0x01; }
|
2002-12-10 09:23:44 +03:00
|
|
|
else {
|
|
|
|
*lenp = 1;
|
|
|
|
rb_raise(rb_eArgError, "malformed UTF-8 character");
|
|
|
|
}
|
2002-12-02 10:13:56 +03:00
|
|
|
if (n > *lenp) {
|
2002-12-10 09:23:44 +03:00
|
|
|
rb_raise(rb_eArgError, "malformed UTF-8 character (expected %d bytes, given %d bytes)",
|
|
|
|
n, *lenp);
|
2002-12-02 10:13:56 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
*lenp = n--;
|
|
|
|
if (n != 0) {
|
|
|
|
while (n--) {
|
2002-12-02 10:13:56 +03:00
|
|
|
c = *p++ & 0xff;
|
|
|
|
if ((c & 0xc0) != 0x80) {
|
|
|
|
*lenp -= n + 1;
|
2002-12-10 09:23:44 +03:00
|
|
|
rb_raise(rb_eArgError, "malformed UTF-8 character");
|
2002-12-02 10:13:56 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
c &= 0x3f;
|
|
|
|
uv = uv << 6 | c;
|
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
}
|
2002-12-04 10:39:32 +03:00
|
|
|
n = *lenp - 1;
|
2002-12-10 09:23:44 +03:00
|
|
|
if (uv < utf8_limits[n]) {
|
|
|
|
rb_raise(rb_eArgError, "redundant UTF-8 sequence");
|
2002-12-04 10:39:32 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
return uv;
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
void
|
|
|
|
Init_pack()
|
|
|
|
{
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_define_method(rb_cArray, "pack", pack_pack, 1);
|
|
|
|
rb_define_method(rb_cString, "unpack", pack_unpack, 1);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|