2000-05-01 13:42:38 +04:00
|
|
|
/**********************************************************************
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
pack.c -
|
|
|
|
|
|
|
|
$Author$
|
|
|
|
created at: Thu Feb 10 15:17:05 JST 1994
|
|
|
|
|
* encoding.c: provide basic features for M17N.
* parse.y: encoding aware parsing.
* parse.y (pragma_encoding): encoding specification pragma.
* parse.y (rb_intern3): encoding specified symbols.
* string.c (rb_str_length): length based on characters.
for older behavior, bytesize method added.
* string.c (rb_str_index_m): index based on characters. rindex as
well.
* string.c (succ_char): encoding aware succeeding string.
* string.c (rb_str_reverse): reverse based on characters.
* string.c (rb_str_inspect): encoding aware string description.
* string.c (rb_str_upcase_bang): encoding aware case conversion.
downcase, capitalize, swapcase as well.
* string.c (rb_str_tr_bang): tr based on characters. delete,
squeeze, tr_s, count as well.
* string.c (rb_str_split_m): split based on characters.
* string.c (rb_str_each_line): encoding aware each_line.
* string.c (rb_str_each_char): added. iteration based on
characters.
* string.c (rb_str_strip_bang): encoding aware whitespace
stripping. lstrip, rstrip as well.
* string.c (rb_str_justify): encoding aware justifying (ljust,
rjust, center).
* string.c (str_encoding): get encoding attribute from a string.
* re.c (rb_reg_initialize): encoding aware regular expression
* sprintf.c (rb_str_format): formatting (i.e. length count) based
on characters.
* io.c (rb_io_getc): getc to return one-character string.
for older behavior, getbyte method added.
* ext/stringio/stringio.c (strio_getc): ditto.
* io.c (rb_io_ungetc): allow pushing arbitrary string at the
current reading point.
* ext/stringio/stringio.c (strio_ungetc): ditto.
* ext/strscan/strscan.c: encoding support.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@13261 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-08-25 07:29:39 +04:00
|
|
|
Copyright (C) 1993-2007 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
|
|
|
|
2020-05-08 12:31:09 +03:00
|
|
|
#include "ruby/internal/config.h"
|
2019-12-04 11:16:30 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
#include <ctype.h>
|
2008-05-17 21:56:41 +04:00
|
|
|
#include <errno.h>
|
2018-11-16 12:04:34 +03:00
|
|
|
#include <float.h>
|
2019-12-04 11:16:30 +03:00
|
|
|
#include <sys/types.h>
|
|
|
|
|
|
|
|
#include "internal.h"
|
2020-08-14 08:45:23 +03:00
|
|
|
#include "internal/array.h"
|
2019-12-04 11:16:30 +03:00
|
|
|
#include "internal/bits.h"
|
|
|
|
#include "internal/string.h"
|
|
|
|
#include "internal/symbol.h"
|
|
|
|
#include "internal/variable.h"
|
2021-07-28 10:40:02 +03:00
|
|
|
#include "ruby/util.h"
|
2019-12-04 11:16:30 +03:00
|
|
|
|
2019-11-08 05:54:39 +03:00
|
|
|
#include "builtin.h"
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2013-04-02 15:28:57 +04:00
|
|
|
/*
|
2013-04-05 15:44:56 +04:00
|
|
|
* It is intentional that the condition for natstr is HAVE_TRUE_LONG_LONG
|
|
|
|
* instead of HAVE_LONG_LONG or LONG_LONG.
|
2013-04-02 15:28:57 +04:00
|
|
|
* This means q! and Q! means always the standard long long type and
|
|
|
|
* causes ArgumentError for platforms which has no long long type,
|
|
|
|
* even if the platform has an implementation specific 64bit type.
|
|
|
|
* This behavior is consistent with the document of pack/unpack.
|
|
|
|
*/
|
2013-04-05 15:44:56 +04:00
|
|
|
#ifdef HAVE_TRUE_LONG_LONG
|
2015-06-12 15:22:36 +03:00
|
|
|
static const char natstr[] = "sSiIlLqQjJ";
|
2013-04-02 15:28:57 +04:00
|
|
|
#else
|
2015-06-12 15:22:36 +03:00
|
|
|
static const char natstr[] = "sSiIlLjJ";
|
2013-04-02 15:28:57 +04:00
|
|
|
#endif
|
2015-06-12 15:22:36 +03:00
|
|
|
static const char endstr[] = "sSiIlLqQjJ";
|
2013-04-02 15:28:57 +04:00
|
|
|
|
2013-04-06 03:00:59 +04:00
|
|
|
#ifdef HAVE_TRUE_LONG_LONG
|
|
|
|
/* It is intentional to use long long instead of LONG_LONG. */
|
|
|
|
# define NATINT_LEN_Q NATINT_LEN(long long, 8)
|
|
|
|
#else
|
|
|
|
# define NATINT_LEN_Q 8
|
|
|
|
#endif
|
|
|
|
|
2013-04-05 15:44:56 +04:00
|
|
|
#if SIZEOF_SHORT != 2 || SIZEOF_LONG != 4 || (defined(HAVE_TRUE_LONG_LONG) && SIZEOF_LONG_LONG != 8)
|
1999-08-13 09:45:20 +04:00
|
|
|
# define NATINT_PACK
|
|
|
|
#endif
|
|
|
|
|
2010-02-25 17:00:48 +03:00
|
|
|
#ifdef DYNAMIC_ENDIAN
|
2018-11-16 03:25:53 +03:00
|
|
|
/* for universal binary of NEXTSTEP and MacOS X */
|
|
|
|
/* useless since autoconf 2.63? */
|
|
|
|
static int
|
|
|
|
is_bigendian(void)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2010-02-25 17:00:48 +03:00
|
|
|
# define BIGENDIAN_P() (is_bigendian())
|
|
|
|
#elif defined(WORDS_BIGENDIAN)
|
|
|
|
# define BIGENDIAN_P() 1
|
2010-02-24 18:32:06 +03:00
|
|
|
#else
|
2010-02-25 17:00:48 +03:00
|
|
|
# define BIGENDIAN_P() 0
|
2010-02-24 18:32:06 +03:00
|
|
|
#endif
|
|
|
|
|
2013-04-06 04:54:23 +04:00
|
|
|
#ifdef NATINT_PACK
|
|
|
|
# define NATINT_LEN(type,len) (natint?(int)sizeof(type):(int)(len))
|
|
|
|
#else
|
|
|
|
# define NATINT_LEN(type,len) ((int)sizeof(type))
|
|
|
|
#endif
|
|
|
|
|
2016-07-05 05:08:55 +03:00
|
|
|
typedef union {
|
|
|
|
float f;
|
|
|
|
uint32_t u;
|
|
|
|
char buf[4];
|
|
|
|
} FLOAT_SWAPPER;
|
|
|
|
typedef union {
|
|
|
|
double d;
|
|
|
|
uint64_t u;
|
|
|
|
char buf[8];
|
|
|
|
} DOUBLE_SWAPPER;
|
|
|
|
#define swapf(x) swap32(x)
|
|
|
|
#define swapd(x) swap64(x)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2010-02-25 17:00:48 +03:00
|
|
|
#define rb_ntohf(x) (BIGENDIAN_P()?(x):swapf(x))
|
|
|
|
#define rb_ntohd(x) (BIGENDIAN_P()?(x):swapd(x))
|
|
|
|
#define rb_htonf(x) (BIGENDIAN_P()?(x):swapf(x))
|
|
|
|
#define rb_htond(x) (BIGENDIAN_P()?(x):swapd(x))
|
|
|
|
#define rb_htovf(x) (BIGENDIAN_P()?swapf(x):(x))
|
|
|
|
#define rb_htovd(x) (BIGENDIAN_P()?swapd(x):(x))
|
|
|
|
#define rb_vtohf(x) (BIGENDIAN_P()?swapf(x):(x))
|
|
|
|
#define rb_vtohd(x) (BIGENDIAN_P()?swapd(x):(x))
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2016-07-05 05:08:55 +03:00
|
|
|
#define FLOAT_CONVWITH(x) FLOAT_SWAPPER x;
|
|
|
|
#define HTONF(x) ((x).u = rb_htonf((x).u))
|
|
|
|
#define HTOVF(x) ((x).u = rb_htovf((x).u))
|
|
|
|
#define NTOHF(x) ((x).u = rb_ntohf((x).u))
|
|
|
|
#define VTOHF(x) ((x).u = rb_vtohf((x).u))
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2016-07-05 05:08:55 +03:00
|
|
|
#define DOUBLE_CONVWITH(x) DOUBLE_SWAPPER x;
|
|
|
|
#define HTOND(x) ((x).u = rb_htond((x).u))
|
|
|
|
#define HTOVD(x) ((x).u = rb_htovd((x).u))
|
|
|
|
#define NTOHD(x) ((x).u = rb_ntohd((x).u))
|
|
|
|
#define VTOHD(x) ((x).u = rb_vtohd((x).u))
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2010-02-28 05:32:09 +03:00
|
|
|
#define MAX_INTEGER_PACK_SIZE 8
|
|
|
|
|
2006-08-04 08:58:25 +04:00
|
|
|
static const char toofew[] = "too few arguments";
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2008-09-25 16:24:54 +04:00
|
|
|
static void encodes(VALUE,const char*,long,int,int);
|
* bignum.c: changed `foo _((boo))' to `foo(boo)`. [ruby-dev:27056]
* defines.h, dir.c, dln.h, enumerator.c, env.h, error.c, eval.c, file.c,
gc.c, hash.c, inits.c, intern.h, io.c, lex.c, marshal.c, missing.h,
node.h, numeric.c, pack.c, process.c, re.h, ruby.c, ruby.h, rubyio.h,
rubysig.h, signal.c, sprintf.c, st.h, string.c, struct.c, time.c,
util.c, util.h, variable.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9155 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-14 10:32:32 +04:00
|
|
|
static void qpencode(VALUE,VALUE,long);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
2006-03-01 13:06:03 +03:00
|
|
|
static unsigned long utf8_to_uv(const char*,long*);
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2014-02-04 09:07:21 +04:00
|
|
|
static ID id_associated;
|
|
|
|
|
|
|
|
static void
|
|
|
|
str_associate(VALUE str, VALUE add)
|
|
|
|
{
|
2016-11-12 09:12:13 +03:00
|
|
|
/* assert(NIL_P(rb_attr_get(str, id_associated))); */
|
|
|
|
rb_ivar_set(str, id_associated, add);
|
2014-02-04 09:07:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
str_associated(VALUE str)
|
|
|
|
{
|
2021-10-22 19:38:45 +03:00
|
|
|
VALUE associates = rb_ivar_lookup(str, id_associated, Qfalse);
|
|
|
|
if (!associates)
|
|
|
|
rb_raise(rb_eArgError, "no associated pointer");
|
|
|
|
return associates;
|
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
|
|
|
associated_pointer(VALUE associates, const char *t)
|
|
|
|
{
|
|
|
|
const VALUE *p = RARRAY_CONST_PTR(associates);
|
|
|
|
const VALUE *pend = p + RARRAY_LEN(associates);
|
|
|
|
for (; p < pend; p++) {
|
|
|
|
VALUE tmp = *p;
|
|
|
|
if (RB_TYPE_P(tmp, T_STRING) && RSTRING_PTR(tmp) == t) return tmp;
|
|
|
|
}
|
|
|
|
rb_raise(rb_eArgError, "non associated pointer");
|
|
|
|
UNREACHABLE_RETURN(Qnil);
|
2014-02-04 09:07:21 +04:00
|
|
|
}
|
|
|
|
|
2018-11-16 03:25:54 +03:00
|
|
|
static void
|
|
|
|
unknown_directive(const char *mode, char type, VALUE fmt)
|
|
|
|
{
|
|
|
|
char unknown[5];
|
|
|
|
|
|
|
|
if (ISPRINT(type)) {
|
|
|
|
unknown[0] = type;
|
|
|
|
unknown[1] = '\0';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
snprintf(unknown, sizeof(unknown), "\\x%.2x", type & 0xff);
|
|
|
|
}
|
2021-07-24 08:24:18 +03:00
|
|
|
fmt = rb_str_quote_unprintable(fmt);
|
2018-11-16 03:25:54 +03:00
|
|
|
rb_warning("unknown %s directive '%s' in '%"PRIsVALUE"'",
|
|
|
|
mode, unknown, fmt);
|
|
|
|
}
|
|
|
|
|
2018-11-16 12:04:34 +03:00
|
|
|
static float
|
|
|
|
VALUE_to_float(VALUE obj)
|
|
|
|
{
|
|
|
|
VALUE v = rb_to_float(obj);
|
|
|
|
double d = RFLOAT_VALUE(v);
|
|
|
|
|
|
|
|
if (isnan(d)) {
|
|
|
|
return NAN;
|
|
|
|
}
|
|
|
|
else if (d < -FLT_MAX) {
|
|
|
|
return -INFINITY;
|
|
|
|
}
|
|
|
|
else if (d <= FLT_MAX) {
|
|
|
|
return d;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return INFINITY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2019-11-08 05:54:39 +03:00
|
|
|
pack_pack(rb_execution_context_t *ec, VALUE ary, VALUE fmt, VALUE buffer)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2006-03-01 13:06:03 +03:00
|
|
|
static const char nul10[] = "\0\0\0\0\0\0\0\0\0\0";
|
|
|
|
static const char spc10[] = " ";
|
|
|
|
const char *p, *pend;
|
2019-11-08 05:54:39 +03:00
|
|
|
VALUE res, from, associates = 0;
|
1998-01-16 15:13:05 +03:00
|
|
|
char type;
|
2015-12-13 12:33:40 +03:00
|
|
|
long len, idx, plen;
|
2006-03-01 13:06:03 +03:00
|
|
|
const char *ptr;
|
2008-12-23 18:30:05 +03:00
|
|
|
int enc_info = 1; /* 0 - BINARY, 1 - US-ASCII, 2 - UTF-8 */
|
1999-08-13 09:45:20 +04:00
|
|
|
#ifdef NATINT_PACK
|
|
|
|
int natint; /* native integer */
|
|
|
|
#endif
|
2011-12-05 13:50:12 +04:00
|
|
|
int integer_size, bigendian_p;
|
2001-05-02 08:22:21 +04:00
|
|
|
|
|
|
|
StringValue(fmt);
|
2006-08-31 14:47:44 +04:00
|
|
|
p = RSTRING_PTR(fmt);
|
|
|
|
pend = p + RSTRING_LEN(fmt);
|
2016-12-01 16:08:20 +03:00
|
|
|
|
2019-11-08 05:54:39 +03:00
|
|
|
if (NIL_P(buffer)) {
|
|
|
|
res = rb_str_buf_new(0);
|
2016-12-01 16:08:20 +03:00
|
|
|
}
|
2019-11-08 05:54:39 +03:00
|
|
|
else {
|
|
|
|
if (!RB_TYPE_P(buffer, T_STRING))
|
|
|
|
rb_raise(rb_eTypeError, "buffer must be String, not %s", rb_obj_classname(buffer));
|
2016-12-01 16:08:20 +03:00
|
|
|
res = buffer;
|
2019-11-08 05:54:39 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
idx = 0;
|
|
|
|
|
2006-08-04 08:58:25 +04:00
|
|
|
#define TOO_FEW (rb_raise(rb_eArgError, toofew), 0)
|
2015-12-13 12:33:40 +03:00
|
|
|
#define MORE_ITEM (idx < RARRAY_LEN(ary))
|
|
|
|
#define THISFROM (MORE_ITEM ? RARRAY_AREF(ary, idx) : TOO_FEW)
|
|
|
|
#define NEXTFROM (MORE_ITEM ? RARRAY_AREF(ary, idx++) : TOO_FEW)
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
while (p < pend) {
|
2010-10-15 11:42:21 +04:00
|
|
|
int explicit_endian = 0;
|
2006-08-31 14:47:44 +04:00
|
|
|
if (RSTRING_PTR(fmt) + RSTRING_LEN(fmt) != pend) {
|
2004-10-08 07:36:54 +04:00
|
|
|
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
|
|
|
}
|
2010-10-14 17:12:56 +04:00
|
|
|
|
|
|
|
{
|
|
|
|
modifiers:
|
|
|
|
switch (*p) {
|
|
|
|
case '_':
|
|
|
|
case '!':
|
|
|
|
if (strchr(natstr, type)) {
|
1999-08-13 09:45:20 +04:00
|
|
|
#ifdef NATINT_PACK
|
2010-10-14 17:12:56 +04:00
|
|
|
natint = 1;
|
1999-08-13 09:45:20 +04:00
|
|
|
#endif
|
2010-10-14 17:12:56 +04:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eArgError, "'%c' allowed only after types %s", *p, natstr);
|
|
|
|
}
|
|
|
|
goto modifiers;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2010-10-14 17:12:56 +04:00
|
|
|
case '<':
|
|
|
|
case '>':
|
|
|
|
if (!strchr(endstr, type)) {
|
|
|
|
rb_raise(rb_eArgError, "'%c' allowed only after types %s", *p, endstr);
|
|
|
|
}
|
|
|
|
if (explicit_endian) {
|
|
|
|
rb_raise(rb_eRangeError, "Can't use both '<' and '>'");
|
|
|
|
}
|
|
|
|
explicit_endian = *p++;
|
|
|
|
goto modifiers;
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
}
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
if (*p == '*') { /* set data length */
|
2008-12-08 17:41:44 +03:00
|
|
|
len = strchr("@Xxu", type) ? 0
|
|
|
|
: strchr("PMm", type) ? 1
|
2015-12-13 12:33:40 +03:00
|
|
|
: RARRAY_LEN(ary) - idx;
|
2006-08-04 08:58:25 +04:00
|
|
|
p++;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-01-20 07:59:39 +03:00
|
|
|
else if (ISDIGIT(*p)) {
|
2008-05-17 21:56:41 +04:00
|
|
|
errno = 0;
|
2008-01-02 09:24:27 +03:00
|
|
|
len = STRTOUL(p, (char**)&p, 10);
|
2008-05-17 21:56:41 +04:00
|
|
|
if (errno) {
|
|
|
|
rb_raise(rb_eRangeError, "pack length too big");
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
len = 1;
|
|
|
|
}
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2008-12-23 18:30:05 +03:00
|
|
|
switch (type) {
|
|
|
|
case 'U':
|
|
|
|
/* if encoding is US-ASCII, upgrade to UTF-8 */
|
|
|
|
if (enc_info == 1) enc_info = 2;
|
|
|
|
break;
|
|
|
|
case 'm': case 'M': case 'u':
|
|
|
|
/* keep US-ASCII (do nothing) */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* fall back to BINARY */
|
|
|
|
enc_info = 0;
|
|
|
|
break;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
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);
|
2006-08-31 14:47:44 +04:00
|
|
|
ptr = RSTRING_PTR(from);
|
|
|
|
plen = RSTRING_LEN(from);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
if (p[-1] == '*')
|
|
|
|
len = plen;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
switch (type) {
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'a': /* arbitrary binary string (null padded) */
|
2007-12-11 17:05:34 +03:00
|
|
|
case 'A': /* arbitrary binary string (ASCII space padded) */
|
|
|
|
case 'Z': /* null terminated 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;
|
|
|
|
|
2012-07-18 09:29:24 +04:00
|
|
|
#define castchar(from) (char)((from) & 0xff)
|
|
|
|
|
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;
|
2022-07-21 19:23:58 +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 {
|
2012-07-18 09:29:24 +04:00
|
|
|
char c = castchar(byte);
|
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);
|
2012-07-18 09:29:24 +04:00
|
|
|
c = castchar(byte);
|
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;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
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;
|
2022-07-21 19:23:58 +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 {
|
2012-07-18 09:29:24 +04:00
|
|
|
char c = castchar(byte);
|
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);
|
2012-07-18 09:29:24 +04:00
|
|
|
c = castchar(byte);
|
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;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
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;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
if (len > plen) {
|
2008-12-08 12:31:44 +03:00
|
|
|
j = (len + 1) / 2 - (plen + 1) / 2;
|
1999-08-13 09:45:20 +04:00
|
|
|
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 {
|
2012-07-18 09:29:24 +04:00
|
|
|
char c = castchar(byte);
|
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) {
|
2012-07-18 09:29:24 +04:00
|
|
|
char c = castchar(byte);
|
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;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
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;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
if (len > plen) {
|
2008-12-08 12:31:44 +03:00
|
|
|
j = (len + 1) / 2 - (plen + 1) / 2;
|
1999-08-13 09:45:20 +04:00
|
|
|
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 {
|
2012-07-18 09:29:24 +04:00
|
|
|
char c = castchar(byte);
|
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) {
|
2012-07-18 09:29:24 +04:00
|
|
|
char c = castchar(byte);
|
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;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'c': /* signed char */
|
|
|
|
case 'C': /* unsigned char */
|
2013-06-21 14:25:40 +04:00
|
|
|
integer_size = 1;
|
|
|
|
bigendian_p = BIGENDIAN_P(); /* not effective */
|
|
|
|
goto pack_integer;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2013-04-05 16:06:34 +04:00
|
|
|
case 's': /* s for int16_t, s! for signed short */
|
2010-02-25 17:00:48 +03:00
|
|
|
integer_size = NATINT_LEN(short, 2);
|
|
|
|
bigendian_p = BIGENDIAN_P();
|
|
|
|
goto pack_integer;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2013-04-05 16:06:34 +04:00
|
|
|
case 'S': /* S for uint16_t, S! for unsigned short */
|
2010-02-25 17:00:48 +03:00
|
|
|
integer_size = NATINT_LEN(short, 2);
|
|
|
|
bigendian_p = BIGENDIAN_P();
|
|
|
|
goto pack_integer;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2013-04-05 16:06:34 +04:00
|
|
|
case 'i': /* i and i! for signed int */
|
2010-02-26 08:17:13 +03:00
|
|
|
integer_size = (int)sizeof(int);
|
2010-02-25 17:00:48 +03:00
|
|
|
bigendian_p = BIGENDIAN_P();
|
|
|
|
goto pack_integer;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2013-04-05 16:06:34 +04:00
|
|
|
case 'I': /* I and I! for unsigned int */
|
2010-02-26 08:17:13 +03:00
|
|
|
integer_size = (int)sizeof(int);
|
2010-02-25 17:00:48 +03:00
|
|
|
bigendian_p = BIGENDIAN_P();
|
|
|
|
goto pack_integer;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2013-04-05 16:06:34 +04:00
|
|
|
case 'l': /* l for int32_t, l! for signed long */
|
2010-02-25 17:00:48 +03:00
|
|
|
integer_size = NATINT_LEN(long, 4);
|
|
|
|
bigendian_p = BIGENDIAN_P();
|
|
|
|
goto pack_integer;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2013-04-05 16:06:34 +04:00
|
|
|
case 'L': /* L for uint32_t, L! for unsigned long */
|
2010-02-25 17:00:48 +03:00
|
|
|
integer_size = NATINT_LEN(long, 4);
|
|
|
|
bigendian_p = BIGENDIAN_P();
|
|
|
|
goto pack_integer;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2013-04-05 16:06:34 +04:00
|
|
|
case 'q': /* q for int64_t, q! for signed long long */
|
2013-04-02 15:28:57 +04:00
|
|
|
integer_size = NATINT_LEN_Q;
|
2010-02-25 17:00:48 +03:00
|
|
|
bigendian_p = BIGENDIAN_P();
|
|
|
|
goto pack_integer;
|
2002-02-13 12:01:11 +03:00
|
|
|
|
2013-04-05 16:06:34 +04:00
|
|
|
case 'Q': /* Q for uint64_t, Q! for unsigned long long */
|
2013-04-02 15:28:57 +04:00
|
|
|
integer_size = NATINT_LEN_Q;
|
2010-02-25 17:00:48 +03:00
|
|
|
bigendian_p = BIGENDIAN_P();
|
|
|
|
goto pack_integer;
|
2002-02-13 12:01:11 +03:00
|
|
|
|
2015-06-12 15:22:36 +03:00
|
|
|
case 'j': /* j for intptr_t */
|
|
|
|
integer_size = sizeof(intptr_t);
|
|
|
|
bigendian_p = BIGENDIAN_P();
|
|
|
|
goto pack_integer;
|
|
|
|
|
|
|
|
case 'J': /* J for uintptr_t */
|
|
|
|
integer_size = sizeof(uintptr_t);
|
|
|
|
bigendian_p = BIGENDIAN_P();
|
|
|
|
goto pack_integer;
|
|
|
|
|
2013-04-05 16:06:34 +04:00
|
|
|
case 'n': /* 16 bit (2 bytes) integer (network byte-order) */
|
2010-02-25 17:00:48 +03:00
|
|
|
integer_size = 2;
|
|
|
|
bigendian_p = 1;
|
|
|
|
goto pack_integer;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2013-04-05 16:06:34 +04:00
|
|
|
case 'N': /* 32 bit (4 bytes) integer (network byte-order) */
|
2010-02-25 17:00:48 +03:00
|
|
|
integer_size = 4;
|
|
|
|
bigendian_p = 1;
|
|
|
|
goto pack_integer;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2013-04-05 16:06:34 +04:00
|
|
|
case 'v': /* 16 bit (2 bytes) integer (VAX byte-order) */
|
2010-02-25 17:00:48 +03:00
|
|
|
integer_size = 2;
|
|
|
|
bigendian_p = 0;
|
|
|
|
goto pack_integer;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2013-04-05 16:06:34 +04:00
|
|
|
case 'V': /* 32 bit (4 bytes) integer (VAX byte-order) */
|
2010-02-25 17:00:48 +03:00
|
|
|
integer_size = 4;
|
|
|
|
bigendian_p = 0;
|
|
|
|
goto pack_integer;
|
|
|
|
|
|
|
|
pack_integer:
|
2010-10-14 17:12:56 +04:00
|
|
|
if (explicit_endian) {
|
2010-10-15 12:28:18 +04:00
|
|
|
bigendian_p = explicit_endian == '>';
|
2010-10-14 17:12:56 +04:00
|
|
|
}
|
2013-06-22 05:39:15 +04:00
|
|
|
if (integer_size > MAX_INTEGER_PACK_SIZE)
|
|
|
|
rb_bug("unexpected intger size for pack: %d", integer_size);
|
|
|
|
while (len-- > 0) {
|
|
|
|
char intbuf[MAX_INTEGER_PACK_SIZE];
|
|
|
|
|
|
|
|
from = NEXTFROM;
|
|
|
|
rb_integer_pack(from, intbuf, integer_size, 1, 0,
|
|
|
|
INTEGER_PACK_2COMP |
|
|
|
|
(bigendian_p ? INTEGER_PACK_BIG_ENDIAN : INTEGER_PACK_LITTLE_ENDIAN));
|
|
|
|
rb_str_buf_cat(res, intbuf, integer_size);
|
|
|
|
}
|
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;
|
2018-11-16 12:04:34 +03:00
|
|
|
f = VALUE_to_float(from);
|
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) {
|
2016-07-05 05:08:55 +03:00
|
|
|
FLOAT_CONVWITH(tmp);
|
1999-08-13 09:45:20 +04:00
|
|
|
|
|
|
|
from = NEXTFROM;
|
2018-11-16 12:04:34 +03:00
|
|
|
tmp.f = VALUE_to_float(from);
|
2016-07-05 05:08:55 +03:00
|
|
|
HTOVF(tmp);
|
|
|
|
rb_str_buf_cat(res, tmp.buf, sizeof(float));
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
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) {
|
2016-07-05 05:08:55 +03:00
|
|
|
DOUBLE_CONVWITH(tmp);
|
1999-08-13 09:45:20 +04:00
|
|
|
from = NEXTFROM;
|
2016-07-05 05:08:55 +03:00
|
|
|
tmp.d = RFLOAT_VALUE(rb_to_float(from));
|
|
|
|
HTOVD(tmp);
|
|
|
|
rb_str_buf_cat(res, tmp.buf, sizeof(double));
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
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;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
from = NEXTFROM;
|
2008-12-30 19:10:23 +03:00
|
|
|
d = RFLOAT_VALUE(rb_to_float(from));
|
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;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
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) {
|
2016-07-05 05:08:55 +03:00
|
|
|
FLOAT_CONVWITH(tmp);
|
1999-08-13 09:45:20 +04:00
|
|
|
from = NEXTFROM;
|
2018-11-16 12:04:34 +03:00
|
|
|
tmp.f = VALUE_to_float(from);
|
2016-07-05 05:08:55 +03:00
|
|
|
HTONF(tmp);
|
|
|
|
rb_str_buf_cat(res, tmp.buf, sizeof(float));
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
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) {
|
2016-07-05 05:08:55 +03:00
|
|
|
DOUBLE_CONVWITH(tmp);
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
from = NEXTFROM;
|
2016-07-05 05:08:55 +03:00
|
|
|
tmp.d = RFLOAT_VALUE(rb_to_float(from));
|
|
|
|
HTOND(tmp);
|
|
|
|
rb_str_buf_cat(res, tmp.buf, sizeof(double));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
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;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'X': /* back up byte */
|
1998-01-16 15:13:05 +03:00
|
|
|
shrink:
|
2006-08-31 14:47:44 +04:00
|
|
|
plen = RSTRING_LEN(res);
|
2001-05-30 13:12:34 +04:00
|
|
|
if (plen < len)
|
1999-01-20 07:59:39 +03:00
|
|
|
rb_raise(rb_eArgError, "X outside of string");
|
2006-08-31 14:47:44 +04:00
|
|
|
rb_str_set_len(res, plen - len);
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case '@': /* null fill to absolute position */
|
2006-08-31 14:47:44 +04:00
|
|
|
len -= RSTRING_LEN(res);
|
1998-01-16 15:13:05 +03:00
|
|
|
if (len > 0) goto grow;
|
|
|
|
len = -len;
|
|
|
|
if (len > 0) goto shrink;
|
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
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;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'U': /* Unicode character */
|
1999-08-13 09:45:20 +04:00
|
|
|
while (len-- > 0) {
|
2007-12-24 21:12:24 +03:00
|
|
|
SIGNED_VALUE l;
|
1999-08-13 09:45:20 +04:00
|
|
|
char buf[8];
|
|
|
|
int le;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
from = NEXTFROM;
|
2004-03-31 06:59:57 +04:00
|
|
|
from = rb_to_int(from);
|
2007-12-24 21:12:24 +03:00
|
|
|
l = NUM2LONG(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");
|
|
|
|
}
|
2007-12-01 19:56:19 +03:00
|
|
|
le = rb_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;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
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);
|
2006-08-31 14:47:44 +04:00
|
|
|
ptr = RSTRING_PTR(from);
|
|
|
|
plen = RSTRING_LEN(from);
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2008-12-06 11:55:57 +03:00
|
|
|
if (len == 0 && type == 'm') {
|
2008-09-25 16:24:54 +04:00
|
|
|
encodes(res, ptr, plen, type, 0);
|
|
|
|
ptr += plen;
|
|
|
|
break;
|
|
|
|
}
|
2001-01-05 18:24:13 +03:00
|
|
|
if (len <= 2)
|
1998-01-16 15:13:05 +03:00
|
|
|
len = 45;
|
2012-07-18 18:57:40 +04:00
|
|
|
else if (len > 63 && type == 'u')
|
|
|
|
len = 63;
|
1998-01-16 15:13:05 +03:00
|
|
|
else
|
|
|
|
len = len / 3 * 3;
|
|
|
|
while (plen > 0) {
|
2002-08-28 12:05:23 +04:00
|
|
|
long todo;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
if (plen > len)
|
|
|
|
todo = len;
|
|
|
|
else
|
|
|
|
todo = plen;
|
2008-09-25 16:24:54 +04:00
|
|
|
encodes(res, ptr, todo, type, 1);
|
1998-01-16 15:13:05 +03:00
|
|
|
plen -= todo;
|
|
|
|
ptr += todo;
|
|
|
|
}
|
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
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;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
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);
|
2006-08-31 14:47:44 +04:00
|
|
|
if (RSTRING_LEN(from) < len) {
|
2002-05-28 22:11:07 +04:00
|
|
|
rb_raise(rb_eArgError, "too short buffer for P(%ld for %ld)",
|
2006-08-31 14:47:44 +04:00
|
|
|
RSTRING_LEN(from), len);
|
2001-05-02 08:22:21 +04:00
|
|
|
}
|
|
|
|
}
|
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;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2003-12-01 11:42:53 +03:00
|
|
|
case 'w': /* BER compressed integer */
|
2000-05-12 13:07:57 +04:00
|
|
|
while (len-- > 0) {
|
|
|
|
VALUE buf = rb_str_new(0, 0);
|
2013-06-06 15:57:35 +04:00
|
|
|
size_t numbytes;
|
|
|
|
int sign;
|
|
|
|
char *cp;
|
2000-05-12 13:07:57 +04:00
|
|
|
|
|
|
|
from = NEXTFROM;
|
2013-06-06 15:57:35 +04:00
|
|
|
from = rb_to_int(from);
|
2013-06-09 11:53:20 +04:00
|
|
|
numbytes = rb_absint_numwords(from, 7, NULL);
|
2013-06-06 15:57:35 +04:00
|
|
|
if (numbytes == 0)
|
|
|
|
numbytes = 1;
|
|
|
|
buf = rb_str_new(NULL, numbytes);
|
|
|
|
|
2013-06-10 14:37:39 +04:00
|
|
|
sign = rb_integer_pack(from, RSTRING_PTR(buf), RSTRING_LEN(buf), 1, 1, INTEGER_PACK_BIG_ENDIAN);
|
2013-06-06 15:57:35 +04:00
|
|
|
|
|
|
|
if (sign < 0)
|
|
|
|
rb_raise(rb_eArgError, "can't compress negative numbers");
|
|
|
|
if (sign == 2)
|
|
|
|
rb_bug("buffer size problem?");
|
|
|
|
|
|
|
|
cp = RSTRING_PTR(buf);
|
|
|
|
while (1 < numbytes) {
|
2018-11-16 03:25:53 +03:00
|
|
|
*cp |= 0x80;
|
|
|
|
cp++;
|
|
|
|
numbytes--;
|
2013-06-06 15:57:35 +04:00
|
|
|
}
|
2000-05-12 13:07:57 +04:00
|
|
|
|
2013-06-06 15:57:35 +04:00
|
|
|
rb_str_buf_cat(res, RSTRING_PTR(buf), RSTRING_LEN(buf));
|
2000-05-12 13:07:57 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-11-06 17:58:43 +03:00
|
|
|
default: {
|
2018-11-16 03:25:54 +03:00
|
|
|
unknown_directive("pack", type, fmt);
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
2014-11-06 17:58:43 +03:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
}
|
2001-08-06 19:05:50 +04:00
|
|
|
|
|
|
|
if (associates) {
|
2014-02-04 09:07:21 +04:00
|
|
|
str_associate(res, associates);
|
2001-08-06 19:05:50 +04:00
|
|
|
}
|
2008-12-23 18:30:05 +03:00
|
|
|
switch (enc_info) {
|
|
|
|
case 1:
|
|
|
|
ENCODING_CODERANGE_SET(res, rb_usascii_encindex(), ENC_CODERANGE_7BIT);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
rb_enc_set_index(res, rb_utf8_encindex());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* do nothing, keep ASCII-8BIT */
|
|
|
|
break;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2006-03-01 13:06:03 +03:00
|
|
|
static const char uu_table[] =
|
1998-01-16 15:19:22 +03:00
|
|
|
"`!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_";
|
2006-03-01 13:06:03 +03:00
|
|
|
static const char b64_table[] =
|
1998-01-16 15:19:22 +03:00
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static void
|
2014-10-17 12:50:01 +04:00
|
|
|
encodes(VALUE str, const char *s0, long len, int type, int tail_lf)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2014-08-04 17:27:01 +04:00
|
|
|
enum {buff_size = 4096, encoded_unit = 4, input_unit = 3};
|
2014-07-11 05:09:05 +04:00
|
|
|
char buff[buff_size + 1]; /* +1 for tail_lf */
|
2002-08-28 12:05:23 +04:00
|
|
|
long i = 0;
|
2014-08-04 17:27:01 +04:00
|
|
|
const char *const trans = type == 'u' ? uu_table : b64_table;
|
2012-07-18 09:29:24 +04:00
|
|
|
char padding;
|
2014-10-17 12:50:01 +04:00
|
|
|
const unsigned char *s = (const unsigned char *)s0;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
if (type == 'u') {
|
* array.c, bignum.c, dln.c, error.c, gc.c, io.c, marshal.c,
numeric.c, pack.c, strftime.c, string.c, thread.c, transcode.c,
transcode_data.h, util.c, variable.c, vm_dump.c,
include/ruby/encoding.h, missing/crypt.c, missing/vsnprintf.c:
suppress VC type warnings. [ruby-core:22726]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22914 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-03-12 12:16:15 +03:00
|
|
|
buff[i++] = (char)len + ' ';
|
1998-01-16 15:19:22 +03:00
|
|
|
padding = '`';
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
padding = '=';
|
|
|
|
}
|
2014-08-04 17:27:01 +04:00
|
|
|
while (len >= input_unit) {
|
|
|
|
while (len >= input_unit && buff_size-i >= encoded_unit) {
|
2008-09-04 19:06:34 +04:00
|
|
|
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]];
|
2014-08-04 17:27:01 +04:00
|
|
|
s += input_unit;
|
|
|
|
len -= input_unit;
|
2008-09-04 19:06:34 +04:00
|
|
|
}
|
2014-07-11 05:09:05 +04:00
|
|
|
if (buff_size-i < encoded_unit) {
|
2008-09-04 19:06:34 +04:00
|
|
|
rb_str_buf_cat(str, buff, i);
|
|
|
|
i = 0;
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2008-09-04 19:06:34 +04:00
|
|
|
|
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;
|
|
|
|
}
|
2008-09-25 16:24:54 +04:00
|
|
|
if (tail_lf) buff[i++] = '\n';
|
2001-05-30 13:12:34 +04:00
|
|
|
rb_str_buf_cat(str, buff, i);
|
2014-07-11 05:09:05 +04:00
|
|
|
if ((size_t)i > sizeof(buff)) rb_bug("encodes() buffer overrun");
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
|
2006-03-01 13:06:03 +03:00
|
|
|
static const char hex_table[] = "0123456789ABCDEF";
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
static void
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
qpencode(VALUE str, VALUE from, 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;
|
2006-08-31 14:47:44 +04:00
|
|
|
unsigned char *s = (unsigned char*)RSTRING_PTR(from);
|
|
|
|
unsigned char *send = s + RSTRING_LEN(from);
|
1999-01-20 07:59:39 +03:00
|
|
|
|
|
|
|
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
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
hex2num(char c)
|
1999-01-20 07:59:39 +03:00
|
|
|
{
|
2013-07-03 17:32:14 +04:00
|
|
|
int n;
|
|
|
|
n = ruby_digit36_to_number_table[(unsigned char)c];
|
|
|
|
if (16 <= n)
|
|
|
|
n = -1;
|
|
|
|
return n;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
|
2002-02-13 12:01:11 +03:00
|
|
|
#define PACK_LENGTH_ADJUST_SIZE(sz) do { \
|
2010-02-26 08:17:13 +03:00
|
|
|
tmp_len = 0; \
|
2010-12-21 01:39:55 +03:00
|
|
|
if (len > (long)((send-s)/(sz))) { \
|
1999-08-13 09:45:20 +04:00
|
|
|
if (!star) { \
|
2018-11-16 03:25:53 +03:00
|
|
|
tmp_len = len-(send-s)/(sz); \
|
1999-08-13 09:45:20 +04:00
|
|
|
} \
|
2010-12-21 01:39:55 +03:00
|
|
|
len = (send-s)/(sz); \
|
1999-08-13 09:45:20 +04:00
|
|
|
} \
|
|
|
|
} while (0)
|
2002-02-13 12:01:11 +03:00
|
|
|
|
2010-02-26 08:17:13 +03:00
|
|
|
#define PACK_ITEM_ADJUST() do { \
|
2016-12-01 17:18:32 +03:00
|
|
|
if (tmp_len > 0 && mode == UNPACK_ARRAY) \
|
2010-02-26 08:17:13 +03:00
|
|
|
rb_ary_store(ary, RARRAY_LEN(ary)+tmp_len-1, Qnil); \
|
|
|
|
} while (0)
|
1999-08-13 09:45:20 +04:00
|
|
|
|
2017-12-19 19:55:08 +03:00
|
|
|
/* Workaround for Oracle Developer Studio (Oracle Solaris Studio)
|
|
|
|
* 12.4/12.5/12.6 C compiler optimization bug
|
2015-11-13 20:06:43 +03:00
|
|
|
* with "-xO4" optimization option.
|
|
|
|
*/
|
2017-12-19 19:55:08 +03:00
|
|
|
#if defined(__SUNPRO_C) && 0x5130 <= __SUNPRO_C && __SUNPRO_C <= 0x5150
|
2015-11-13 20:06:43 +03:00
|
|
|
# define AVOID_CC_BUG volatile
|
|
|
|
#else
|
|
|
|
# define AVOID_CC_BUG
|
|
|
|
#endif
|
|
|
|
|
2016-12-01 17:18:32 +03:00
|
|
|
/* unpack mode */
|
|
|
|
#define UNPACK_ARRAY 0
|
|
|
|
#define UNPACK_BLOCK 1
|
|
|
|
#define UNPACK_1 2
|
2003-12-19 00:08:25 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
static VALUE
|
2021-10-18 17:23:54 +03:00
|
|
|
pack_unpack_internal(VALUE str, VALUE fmt, int mode, long offset)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2015-02-13 10:07:39 +03:00
|
|
|
#define hexdigits ruby_hexdigits
|
1999-01-20 07:59:39 +03:00
|
|
|
char *s, *send;
|
|
|
|
char *p, *pend;
|
2021-10-22 19:38:45 +03:00
|
|
|
VALUE ary, associates = Qfalse;
|
1998-01-16 15:13:05 +03:00
|
|
|
char type;
|
2015-11-13 20:06:43 +03:00
|
|
|
long len;
|
|
|
|
AVOID_CC_BUG long tmp_len;
|
2010-02-26 08:17:13 +03:00
|
|
|
int star;
|
1999-08-13 09:45:20 +04:00
|
|
|
#ifdef NATINT_PACK
|
|
|
|
int natint; /* native integer */
|
|
|
|
#endif
|
2010-02-24 18:32:06 +03:00
|
|
|
int signed_p, integer_size, bigendian_p;
|
2006-10-16 02:57:37 +04:00
|
|
|
#define UNPACK_PUSH(item) do {\
|
|
|
|
VALUE item_val = (item);\
|
2016-12-01 17:18:32 +03:00
|
|
|
if ((mode) == UNPACK_BLOCK) {\
|
2006-10-16 02:57:37 +04:00
|
|
|
rb_yield(item_val);\
|
|
|
|
}\
|
2016-12-01 17:18:32 +03:00
|
|
|
else if ((mode) == UNPACK_ARRAY) {\
|
2006-10-16 02:57:37 +04:00
|
|
|
rb_ary_push(ary, item_val);\
|
|
|
|
}\
|
2016-12-01 17:18:32 +03:00
|
|
|
else /* if ((mode) == UNPACK_1) { */ {\
|
|
|
|
return item_val; \
|
|
|
|
}\
|
2006-10-16 02:57:37 +04:00
|
|
|
} while (0)
|
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);
|
2021-10-18 17:23:54 +03:00
|
|
|
|
|
|
|
if (offset < 0) rb_raise(rb_eArgError, "offset can't be negative");
|
|
|
|
len = RSTRING_LEN(str);
|
|
|
|
if (offset > len) rb_raise(rb_eArgError, "offset outside of string");
|
|
|
|
|
2006-08-31 14:47:44 +04:00
|
|
|
s = RSTRING_PTR(str);
|
2021-10-18 17:23:54 +03:00
|
|
|
send = s + len;
|
|
|
|
s += offset;
|
|
|
|
|
2006-08-31 14:47:44 +04:00
|
|
|
p = RSTRING_PTR(fmt);
|
|
|
|
pend = p + RSTRING_LEN(fmt);
|
1998-01-16 15:13:05 +03:00
|
|
|
|
2021-10-21 15:52:17 +03:00
|
|
|
#define UNPACK_FETCH(var, type) (memcpy((var), s, sizeof(type)), s += sizeof(type))
|
|
|
|
|
2016-12-01 17:18:32 +03:00
|
|
|
ary = mode == UNPACK_ARRAY ? rb_ary_new() : Qnil;
|
1998-01-16 15:13:05 +03:00
|
|
|
while (p < pend) {
|
2010-10-15 11:42:21 +04:00
|
|
|
int explicit_endian = 0;
|
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
|
|
|
}
|
2010-10-14 17:12:56 +04:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
star = 0;
|
2010-10-14 17:12:56 +04:00
|
|
|
{
|
|
|
|
modifiers:
|
|
|
|
switch (*p) {
|
|
|
|
case '_':
|
|
|
|
case '!':
|
|
|
|
|
|
|
|
if (strchr(natstr, type)) {
|
1999-08-13 09:45:20 +04:00
|
|
|
#ifdef NATINT_PACK
|
2010-10-14 17:12:56 +04:00
|
|
|
natint = 1;
|
1999-08-13 09:45:20 +04:00
|
|
|
#endif
|
2010-10-14 17:12:56 +04:00
|
|
|
p++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_raise(rb_eArgError, "'%c' allowed only after types %s", *p, natstr);
|
|
|
|
}
|
|
|
|
goto modifiers;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2010-10-14 17:12:56 +04:00
|
|
|
case '<':
|
|
|
|
case '>':
|
|
|
|
if (!strchr(endstr, type)) {
|
|
|
|
rb_raise(rb_eArgError, "'%c' allowed only after types %s", *p, endstr);
|
|
|
|
}
|
|
|
|
if (explicit_endian) {
|
|
|
|
rb_raise(rb_eRangeError, "Can't use both '<' and '>'");
|
|
|
|
}
|
|
|
|
explicit_endian = *p++;
|
|
|
|
goto modifiers;
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
}
|
2022-07-21 19:23:58 +03:00
|
|
|
|
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)) {
|
2008-05-17 21:56:41 +04:00
|
|
|
errno = 0;
|
2008-01-02 09:24:27 +03:00
|
|
|
len = STRTOUL(p, (char**)&p, 10);
|
2018-03-28 13:12:17 +03:00
|
|
|
if (len < 0 || errno) {
|
2008-05-17 21:56:41 +04:00
|
|
|
rb_raise(rb_eRangeError, "pack length too big");
|
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
len = (type != '@');
|
|
|
|
}
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
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;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
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;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
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--;
|
|
|
|
}
|
2019-09-25 06:59:12 +03:00
|
|
|
UNPACK_PUSH(rb_str_new(s, len));
|
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++;
|
2019-09-25 06:59:12 +03:00
|
|
|
UNPACK_PUSH(rb_str_new(s, t-s));
|
2004-05-13 06:04:20 +04:00
|
|
|
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;
|
2019-09-25 06:59:12 +03:00
|
|
|
UNPACK_PUSH(rb_str_new(s, len));
|
1998-01-16 15:13:05 +03:00
|
|
|
s += len;
|
|
|
|
break;
|
2022-07-21 19:23:58 +03: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;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
if (p[-1] == '*' || len > (send - s) * 8)
|
|
|
|
len = (send - s) * 8;
|
|
|
|
bits = 0;
|
2016-12-26 09:32:00 +03:00
|
|
|
bitstr = rb_usascii_str_new(0, len);
|
2006-08-31 14:47:44 +04:00
|
|
|
t = RSTRING_PTR(bitstr);
|
1998-01-16 15:13:05 +03:00
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
if (i & 7) bits >>= 1;
|
2014-10-17 16:52:26 +04:00
|
|
|
else bits = (unsigned char)*s++;
|
1998-01-16 15:13:05 +03:00
|
|
|
*t++ = (bits & 1) ? '1' : '0';
|
|
|
|
}
|
2016-12-26 09:32:00 +03:00
|
|
|
UNPACK_PUSH(bitstr);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
break;
|
2022-07-21 19:23:58 +03: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;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
if (p[-1] == '*' || len > (send - s) * 8)
|
|
|
|
len = (send - s) * 8;
|
|
|
|
bits = 0;
|
2016-12-26 09:32:00 +03:00
|
|
|
bitstr = rb_usascii_str_new(0, len);
|
2006-08-31 14:47:44 +04:00
|
|
|
t = RSTRING_PTR(bitstr);
|
1998-01-16 15:13:05 +03:00
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
if (i & 7) bits <<= 1;
|
2014-10-17 12:50:01 +04:00
|
|
|
else bits = (unsigned char)*s++;
|
1998-01-16 15:13:05 +03:00
|
|
|
*t++ = (bits & 128) ? '1' : '0';
|
|
|
|
}
|
2016-12-26 09:32:00 +03:00
|
|
|
UNPACK_PUSH(bitstr);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
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;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
if (p[-1] == '*' || len > (send - s) * 2)
|
|
|
|
len = (send - s) * 2;
|
|
|
|
bits = 0;
|
2016-12-26 09:32:00 +03:00
|
|
|
bitstr = rb_usascii_str_new(0, len);
|
2006-08-31 14:47:44 +04:00
|
|
|
t = RSTRING_PTR(bitstr);
|
1998-01-16 15:13:05 +03:00
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
if (i & 1)
|
|
|
|
bits >>= 4;
|
|
|
|
else
|
2014-10-17 16:52:26 +04:00
|
|
|
bits = (unsigned char)*s++;
|
1998-01-16 15:13:05 +03:00
|
|
|
*t++ = hexdigits[bits & 15];
|
|
|
|
}
|
2016-12-26 09:32:00 +03:00
|
|
|
UNPACK_PUSH(bitstr);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
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;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
if (p[-1] == '*' || len > (send - s) * 2)
|
|
|
|
len = (send - s) * 2;
|
|
|
|
bits = 0;
|
2016-12-26 09:32:00 +03:00
|
|
|
bitstr = rb_usascii_str_new(0, len);
|
2006-08-31 14:47:44 +04:00
|
|
|
t = RSTRING_PTR(bitstr);
|
1998-01-16 15:13:05 +03:00
|
|
|
for (i=0; i<len; i++) {
|
|
|
|
if (i & 1)
|
|
|
|
bits <<= 4;
|
|
|
|
else
|
2014-10-17 12:50:01 +04:00
|
|
|
bits = (unsigned char)*s++;
|
1998-01-16 15:13:05 +03:00
|
|
|
*t++ = hexdigits[(bits >> 4) & 15];
|
|
|
|
}
|
2016-12-26 09:32:00 +03:00
|
|
|
UNPACK_PUSH(bitstr);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
case 'c':
|
2013-06-22 16:05:31 +04:00
|
|
|
signed_p = 1;
|
|
|
|
integer_size = 1;
|
|
|
|
bigendian_p = BIGENDIAN_P(); /* not effective */
|
|
|
|
goto unpack_integer;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
case 'C':
|
2013-06-22 16:05:31 +04:00
|
|
|
signed_p = 0;
|
|
|
|
integer_size = 1;
|
|
|
|
bigendian_p = BIGENDIAN_P(); /* not effective */
|
|
|
|
goto unpack_integer;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
case 's':
|
2010-02-24 18:32:06 +03:00
|
|
|
signed_p = 1;
|
|
|
|
integer_size = NATINT_LEN(short, 2);
|
2010-02-25 17:00:48 +03:00
|
|
|
bigendian_p = BIGENDIAN_P();
|
2010-02-24 18:32:06 +03:00
|
|
|
goto unpack_integer;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
case 'S':
|
2010-02-24 18:32:06 +03:00
|
|
|
signed_p = 0;
|
|
|
|
integer_size = NATINT_LEN(short, 2);
|
2010-02-25 17:00:48 +03:00
|
|
|
bigendian_p = BIGENDIAN_P();
|
2010-02-24 18:32:06 +03:00
|
|
|
goto unpack_integer;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
case 'i':
|
2010-02-24 18:32:06 +03:00
|
|
|
signed_p = 1;
|
2010-02-26 08:17:13 +03:00
|
|
|
integer_size = (int)sizeof(int);
|
2010-02-25 17:00:48 +03:00
|
|
|
bigendian_p = BIGENDIAN_P();
|
2010-02-24 18:32:06 +03:00
|
|
|
goto unpack_integer;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
case 'I':
|
2010-02-24 18:32:06 +03:00
|
|
|
signed_p = 0;
|
2010-02-26 08:17:13 +03:00
|
|
|
integer_size = (int)sizeof(int);
|
2010-02-25 17:00:48 +03:00
|
|
|
bigendian_p = BIGENDIAN_P();
|
2010-02-24 18:32:06 +03:00
|
|
|
goto unpack_integer;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
case 'l':
|
2010-02-24 18:32:06 +03:00
|
|
|
signed_p = 1;
|
|
|
|
integer_size = NATINT_LEN(long, 4);
|
2010-02-25 17:00:48 +03:00
|
|
|
bigendian_p = BIGENDIAN_P();
|
2010-02-24 18:32:06 +03:00
|
|
|
goto unpack_integer;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
case 'L':
|
2010-02-24 18:32:06 +03:00
|
|
|
signed_p = 0;
|
|
|
|
integer_size = NATINT_LEN(long, 4);
|
2010-02-25 17:00:48 +03:00
|
|
|
bigendian_p = BIGENDIAN_P();
|
2010-02-24 18:32:06 +03:00
|
|
|
goto unpack_integer;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2002-02-13 12:01:11 +03:00
|
|
|
case 'q':
|
2010-02-24 18:32:06 +03:00
|
|
|
signed_p = 1;
|
2013-04-02 15:28:57 +04:00
|
|
|
integer_size = NATINT_LEN_Q;
|
2010-02-25 17:00:48 +03:00
|
|
|
bigendian_p = BIGENDIAN_P();
|
2010-02-24 18:32:06 +03:00
|
|
|
goto unpack_integer;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
* compile.c, dir.c, eval.c, eval_jump.h, eval_method.h, numeric.c,
pack.c, parse.y, re.c, thread.c, vm.c, vm_dump.c, call_cfunc.ci,
thread_pthread.ci, thread_win32.ci: fixed indentation.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@12431 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2007-06-05 08:25:10 +04:00
|
|
|
case 'Q':
|
2010-02-24 18:32:06 +03:00
|
|
|
signed_p = 0;
|
2013-04-02 15:28:57 +04:00
|
|
|
integer_size = NATINT_LEN_Q;
|
2010-02-25 17:00:48 +03:00
|
|
|
bigendian_p = BIGENDIAN_P();
|
2010-02-24 18:32:06 +03:00
|
|
|
goto unpack_integer;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2015-06-12 15:22:36 +03:00
|
|
|
case 'j':
|
|
|
|
signed_p = 1;
|
|
|
|
integer_size = sizeof(intptr_t);
|
|
|
|
bigendian_p = BIGENDIAN_P();
|
|
|
|
goto unpack_integer;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2015-06-12 15:22:36 +03:00
|
|
|
case 'J':
|
|
|
|
signed_p = 0;
|
|
|
|
integer_size = sizeof(uintptr_t);
|
|
|
|
bigendian_p = BIGENDIAN_P();
|
|
|
|
goto unpack_integer;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
case 'n':
|
2010-02-24 18:32:06 +03:00
|
|
|
signed_p = 0;
|
|
|
|
integer_size = 2;
|
|
|
|
bigendian_p = 1;
|
|
|
|
goto unpack_integer;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
case 'N':
|
2010-02-24 18:32:06 +03:00
|
|
|
signed_p = 0;
|
|
|
|
integer_size = 4;
|
|
|
|
bigendian_p = 1;
|
|
|
|
goto unpack_integer;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
case 'v':
|
2010-02-24 18:32:06 +03:00
|
|
|
signed_p = 0;
|
|
|
|
integer_size = 2;
|
|
|
|
bigendian_p = 0;
|
|
|
|
goto unpack_integer;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
case 'V':
|
2010-02-24 18:32:06 +03:00
|
|
|
signed_p = 0;
|
|
|
|
integer_size = 4;
|
|
|
|
bigendian_p = 0;
|
|
|
|
goto unpack_integer;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2010-02-24 18:32:06 +03:00
|
|
|
unpack_integer:
|
2010-10-14 17:12:56 +04:00
|
|
|
if (explicit_endian) {
|
2010-10-15 12:28:18 +04:00
|
|
|
bigendian_p = explicit_endian == '>';
|
2010-10-14 17:12:56 +04:00
|
|
|
}
|
2013-06-22 16:05:31 +04:00
|
|
|
PACK_LENGTH_ADJUST_SIZE(integer_size);
|
|
|
|
while (len-- > 0) {
|
|
|
|
int flags = bigendian_p ? INTEGER_PACK_BIG_ENDIAN : INTEGER_PACK_LITTLE_ENDIAN;
|
|
|
|
VALUE val;
|
|
|
|
if (signed_p)
|
|
|
|
flags |= INTEGER_PACK_2COMP;
|
|
|
|
val = rb_integer_unpack(s, integer_size, 1, 0, flags);
|
|
|
|
UNPACK_PUSH(val);
|
|
|
|
s += integer_size;
|
|
|
|
}
|
|
|
|
PACK_ITEM_ADJUST();
|
2010-05-10 18:52:16 +04:00
|
|
|
break;
|
1998-01-16 15:13:05 +03:00
|
|
|
|
|
|
|
case 'f':
|
|
|
|
case 'F':
|
2010-02-25 17:00:48 +03:00
|
|
|
PACK_LENGTH_ADJUST_SIZE(sizeof(float));
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
|
|
|
float tmp;
|
2021-10-21 15:52:17 +03:00
|
|
|
UNPACK_FETCH(&tmp, float);
|
2008-09-05 22:24:21 +04:00
|
|
|
UNPACK_PUSH(DBL2NUM((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;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
case 'e':
|
2010-02-25 17:00:48 +03:00
|
|
|
PACK_LENGTH_ADJUST_SIZE(sizeof(float));
|
1999-08-13 09:45:20 +04:00
|
|
|
while (len-- > 0) {
|
2016-07-05 05:08:55 +03:00
|
|
|
FLOAT_CONVWITH(tmp);
|
2021-10-21 15:52:17 +03:00
|
|
|
UNPACK_FETCH(tmp.buf, float);
|
2016-07-05 05:08:55 +03:00
|
|
|
VTOHF(tmp);
|
|
|
|
UNPACK_PUSH(DBL2NUM(tmp.f));
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
PACK_ITEM_ADJUST();
|
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
case 'E':
|
2010-02-25 17:00:48 +03:00
|
|
|
PACK_LENGTH_ADJUST_SIZE(sizeof(double));
|
1999-08-13 09:45:20 +04:00
|
|
|
while (len-- > 0) {
|
2016-07-05 05:08:55 +03:00
|
|
|
DOUBLE_CONVWITH(tmp);
|
2021-10-21 15:52:17 +03:00
|
|
|
UNPACK_FETCH(tmp.buf, double);
|
2016-07-05 05:08:55 +03:00
|
|
|
VTOHD(tmp);
|
|
|
|
UNPACK_PUSH(DBL2NUM(tmp.d));
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
PACK_ITEM_ADJUST();
|
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
case 'D':
|
|
|
|
case 'd':
|
2010-02-25 17:00:48 +03:00
|
|
|
PACK_LENGTH_ADJUST_SIZE(sizeof(double));
|
1999-08-13 09:45:20 +04:00
|
|
|
while (len-- > 0) {
|
|
|
|
double tmp;
|
2021-10-21 15:52:17 +03:00
|
|
|
UNPACK_FETCH(&tmp, double);
|
2008-09-05 22:24:21 +04:00
|
|
|
UNPACK_PUSH(DBL2NUM(tmp));
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
PACK_ITEM_ADJUST();
|
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
case 'g':
|
2010-02-25 17:00:48 +03:00
|
|
|
PACK_LENGTH_ADJUST_SIZE(sizeof(float));
|
1999-08-13 09:45:20 +04:00
|
|
|
while (len-- > 0) {
|
2016-07-05 05:08:55 +03:00
|
|
|
FLOAT_CONVWITH(tmp);
|
2021-10-21 15:52:17 +03:00
|
|
|
UNPACK_FETCH(tmp.buf, float);
|
2016-07-05 05:08:55 +03:00
|
|
|
NTOHF(tmp);
|
|
|
|
UNPACK_PUSH(DBL2NUM(tmp.f));
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
PACK_ITEM_ADJUST();
|
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
case 'G':
|
2010-02-25 17:00:48 +03:00
|
|
|
PACK_LENGTH_ADJUST_SIZE(sizeof(double));
|
1998-01-16 15:13:05 +03:00
|
|
|
while (len-- > 0) {
|
2016-07-05 05:08:55 +03:00
|
|
|
DOUBLE_CONVWITH(tmp);
|
2021-10-21 15:52:17 +03:00
|
|
|
UNPACK_FETCH(tmp.buf, double);
|
2016-07-05 05:08:55 +03:00
|
|
|
NTOHD(tmp);
|
|
|
|
UNPACK_PUSH(DBL2NUM(tmp.d));
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
1999-08-13 09:45:20 +04:00
|
|
|
PACK_ITEM_ADJUST();
|
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
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;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1999-08-13 09:45:20 +04:00
|
|
|
l = utf8_to_uv(s, &alen);
|
2002-04-15 11:48:47 +04:00
|
|
|
s += alen; len--;
|
2006-10-16 02:57:37 +04:00
|
|
|
UNPACK_PUSH(ULONG2NUM(l));
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
case 'u':
|
|
|
|
{
|
2019-09-25 06:59:12 +03:00
|
|
|
VALUE buf = rb_str_new(0, (send - s)*3/4);
|
2006-08-31 14:47:44 +04:00
|
|
|
char *ptr = RSTRING_PTR(buf);
|
2002-08-21 19:47:54 +04:00
|
|
|
long total = 0;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2014-10-17 18:28:34 +04:00
|
|
|
while (s < send && (unsigned char)*s > ' ' && (unsigned char)*s < 'a') {
|
1998-01-16 15:13:05 +03:00
|
|
|
long a,b,c,d;
|
2014-10-17 18:28:34 +04:00
|
|
|
char hunk[3];
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2014-10-17 18:28:34 +04:00
|
|
|
len = ((unsigned char)*s++ - ' ') & 077;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
total += len;
|
2006-08-31 14:47:44 +04:00
|
|
|
if (total > RSTRING_LEN(buf)) {
|
|
|
|
len -= total - RSTRING_LEN(buf);
|
|
|
|
total = RSTRING_LEN(buf);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2022-07-21 19:23:58 +03:00
|
|
|
|
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;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2014-10-17 18:28:34 +04:00
|
|
|
if (s < send && (unsigned char)*s >= ' ' && (unsigned char)*s < 'a')
|
|
|
|
a = ((unsigned char)*s++ - ' ') & 077;
|
1998-01-16 15:13:05 +03:00
|
|
|
else
|
|
|
|
a = 0;
|
2014-10-17 18:28:34 +04:00
|
|
|
if (s < send && (unsigned char)*s >= ' ' && (unsigned char)*s < 'a')
|
|
|
|
b = ((unsigned char)*s++ - ' ') & 077;
|
1998-01-16 15:13:05 +03:00
|
|
|
else
|
|
|
|
b = 0;
|
2014-10-17 18:28:34 +04:00
|
|
|
if (s < send && (unsigned char)*s >= ' ' && (unsigned char)*s < 'a')
|
|
|
|
c = ((unsigned char)*s++ - ' ') & 077;
|
1998-01-16 15:13:05 +03:00
|
|
|
else
|
|
|
|
c = 0;
|
2014-10-17 18:28:34 +04:00
|
|
|
if (s < send && (unsigned char)*s >= ' ' && (unsigned char)*s < 'a')
|
|
|
|
d = ((unsigned char)*s++ - ' ') & 077;
|
1998-01-16 15:13:05 +03:00
|
|
|
else
|
|
|
|
d = 0;
|
* array.c, bignum.c, dln.c, error.c, gc.c, io.c, marshal.c,
numeric.c, pack.c, strftime.c, string.c, thread.c, transcode.c,
transcode_data.h, util.c, variable.c, vm_dump.c,
include/ruby/encoding.h, missing/crypt.c, missing/vsnprintf.c:
suppress VC type warnings. [ruby-core:22726]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22914 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-03-12 12:16:15 +03:00
|
|
|
hunk[0] = (char)(a << 2 | b >> 4);
|
|
|
|
hunk[1] = (char)(b << 4 | c >> 2);
|
|
|
|
hunk[2] = (char)(c << 6 | d);
|
1998-01-16 15:13:05 +03:00
|
|
|
memcpy(ptr, hunk, mlen);
|
|
|
|
ptr += mlen;
|
|
|
|
len -= mlen;
|
|
|
|
}
|
2014-10-17 18:28:34 +04:00
|
|
|
if (s < send && (unsigned char)*s != '\r' && *s != '\n')
|
|
|
|
s++; /* possible checksum byte */
|
|
|
|
if (s < send && *s == '\r') s++;
|
|
|
|
if (s < send && *s == '\n') s++;
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2006-08-31 14:47:44 +04:00
|
|
|
rb_str_set_len(buf, total);
|
2006-10-16 02:57:37 +04:00
|
|
|
UNPACK_PUSH(buf);
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
case 'm':
|
|
|
|
{
|
2019-09-25 06:59:12 +03:00
|
|
|
VALUE buf = rb_str_new(0, (send - s + 3)*3/4); /* +3 is for skipping paddings */
|
2006-08-31 14:47:44 +04:00
|
|
|
char *ptr = RSTRING_PTR(buf);
|
2008-09-25 16:24:54 +04:00
|
|
|
int a = -1,b = -1,c = 0,d = 0;
|
2008-07-30 19:58:36 +04:00
|
|
|
static signed char b64_xtable[256];
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2008-07-30 19:58:36 +04:00
|
|
|
if (b64_xtable['/'] <= 0) {
|
1998-01-16 15:19:22 +03:00
|
|
|
int i;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:19:22 +03:00
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
b64_xtable[i] = -1;
|
|
|
|
}
|
|
|
|
for (i = 0; i < 64; i++) {
|
2012-07-18 09:29:24 +04:00
|
|
|
b64_xtable[(unsigned char)b64_table[i]] = (char)i;
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
|
|
|
}
|
2008-09-25 16:24:54 +04:00
|
|
|
if (len == 0) {
|
|
|
|
while (s < send) {
|
|
|
|
a = b = c = d = -1;
|
|
|
|
a = b64_xtable[(unsigned char)*s++];
|
|
|
|
if (s >= send || a == -1) rb_raise(rb_eArgError, "invalid base64");
|
|
|
|
b = b64_xtable[(unsigned char)*s++];
|
|
|
|
if (s >= send || b == -1) rb_raise(rb_eArgError, "invalid base64");
|
|
|
|
if (*s == '=') {
|
|
|
|
if (s + 2 == send && *(s + 1) == '=') break;
|
|
|
|
rb_raise(rb_eArgError, "invalid base64");
|
|
|
|
}
|
|
|
|
c = b64_xtable[(unsigned char)*s++];
|
|
|
|
if (s >= send || c == -1) rb_raise(rb_eArgError, "invalid base64");
|
|
|
|
if (s + 1 == send && *s == '=') break;
|
|
|
|
d = b64_xtable[(unsigned char)*s++];
|
|
|
|
if (d == -1) rb_raise(rb_eArgError, "invalid base64");
|
2012-07-18 09:29:24 +04:00
|
|
|
*ptr++ = castchar(a << 2 | b >> 4);
|
|
|
|
*ptr++ = castchar(b << 4 | c >> 2);
|
|
|
|
*ptr++ = castchar(c << 6 | d);
|
2008-09-25 16:24:54 +04:00
|
|
|
}
|
|
|
|
if (c == -1) {
|
2012-07-18 09:29:24 +04:00
|
|
|
*ptr++ = castchar(a << 2 | b >> 4);
|
2008-09-25 16:24:54 +04:00
|
|
|
if (b & 0xf) rb_raise(rb_eArgError, "invalid base64");
|
|
|
|
}
|
|
|
|
else if (d == -1) {
|
2012-07-18 09:29:24 +04:00
|
|
|
*ptr++ = castchar(a << 2 | b >> 4);
|
|
|
|
*ptr++ = castchar(b << 4 | c >> 2);
|
2008-09-25 16:24:54 +04:00
|
|
|
if (c & 0x3) rb_raise(rb_eArgError, "invalid base64");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
while (s < send) {
|
|
|
|
a = b = c = d = -1;
|
|
|
|
while ((a = b64_xtable[(unsigned char)*s]) == -1 && s < send) {s++;}
|
|
|
|
if (s >= send) break;
|
|
|
|
s++;
|
|
|
|
while ((b = b64_xtable[(unsigned char)*s]) == -1 && s < send) {s++;}
|
|
|
|
if (s >= send) break;
|
|
|
|
s++;
|
|
|
|
while ((c = b64_xtable[(unsigned char)*s]) == -1 && s < send) {if (*s == '=') break; s++;}
|
|
|
|
if (*s == '=' || s >= send) break;
|
|
|
|
s++;
|
|
|
|
while ((d = b64_xtable[(unsigned char)*s]) == -1 && s < send) {if (*s == '=') break; s++;}
|
|
|
|
if (*s == '=' || s >= send) break;
|
|
|
|
s++;
|
2012-07-18 09:29:24 +04:00
|
|
|
*ptr++ = castchar(a << 2 | b >> 4);
|
|
|
|
*ptr++ = castchar(b << 4 | c >> 2);
|
|
|
|
*ptr++ = castchar(c << 6 | d);
|
2013-04-18 11:20:25 +04:00
|
|
|
a = -1;
|
2008-09-25 16:24:54 +04:00
|
|
|
}
|
|
|
|
if (a != -1 && b != -1) {
|
2013-04-18 09:03:00 +04:00
|
|
|
if (c == -1)
|
2012-07-18 09:29:24 +04:00
|
|
|
*ptr++ = castchar(a << 2 | b >> 4);
|
2013-04-18 09:03:00 +04:00
|
|
|
else {
|
2012-07-18 09:29:24 +04:00
|
|
|
*ptr++ = castchar(a << 2 | b >> 4);
|
|
|
|
*ptr++ = castchar(b << 4 | c >> 2);
|
2008-09-25 16:24:54 +04:00
|
|
|
}
|
2003-06-23 10:52:39 +04:00
|
|
|
}
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
2006-08-31 14:47:44 +04:00
|
|
|
rb_str_set_len(buf, ptr - RSTRING_PTR(buf));
|
2006-10-16 02:57:37 +04:00
|
|
|
UNPACK_PUSH(buf);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
case 'M':
|
|
|
|
{
|
2019-09-25 06:59:12 +03:00
|
|
|
VALUE buf = rb_str_new(0, send - s);
|
2012-03-11 18:59:42 +04:00
|
|
|
char *ptr = RSTRING_PTR(buf), *ss = s;
|
2017-09-29 11:00:47 +03:00
|
|
|
int csum = 0;
|
1999-01-20 07:59:39 +03:00
|
|
|
int c1, c2;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
while (s < send) {
|
|
|
|
if (*s == '=') {
|
|
|
|
if (++s == send) break;
|
2012-07-24 08:24:20 +04:00
|
|
|
if (s+1 < send && *s == '\r' && *(s+1) == '\n')
|
|
|
|
s++;
|
1999-01-20 07:59:39 +03:00
|
|
|
if (*s != '\n') {
|
|
|
|
if ((c1 = hex2num(*s)) == -1) break;
|
|
|
|
if (++s == send) break;
|
|
|
|
if ((c2 = hex2num(*s)) == -1) break;
|
2017-09-29 11:00:47 +03:00
|
|
|
csum |= *ptr++ = castchar(c1 << 4 | c2);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2017-09-29 11:00:47 +03:00
|
|
|
csum |= *ptr++ = *s;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
s++;
|
2012-03-11 18:59:42 +04:00
|
|
|
ss = s;
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
2006-08-31 14:47:44 +04:00
|
|
|
rb_str_set_len(buf, ptr - RSTRING_PTR(buf));
|
2012-03-11 18:59:42 +04:00
|
|
|
rb_str_buf_cat(buf, ss, send-ss);
|
2017-09-29 11:00:47 +03:00
|
|
|
csum = ISASCII(csum) ? ENC_CODERANGE_7BIT : ENC_CODERANGE_VALID;
|
|
|
|
ENCODING_CODERANGE_SET(buf, rb_ascii8bit_encindex(), csum);
|
2006-10-16 02:57:37 +04:00
|
|
|
UNPACK_PUSH(buf);
|
1998-01-16 15:19:22 +03:00
|
|
|
}
|
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
case '@':
|
2006-08-31 14:47:44 +04:00
|
|
|
if (len > RSTRING_LEN(str))
|
2003-06-23 10:52:39 +04:00
|
|
|
rb_raise(rb_eArgError, "@ outside of string");
|
2006-08-31 14:47:44 +04:00
|
|
|
s = RSTRING_PTR(str) + len;
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
case 'X':
|
2006-08-31 14:47:44 +04:00
|
|
|
if (len > s - RSTRING_PTR(str))
|
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;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
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;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
case 'P':
|
2010-02-26 08:17:13 +03:00
|
|
|
if (sizeof(char *) <= (size_t)(send - s)) {
|
2008-04-22 17:49:43 +04:00
|
|
|
VALUE tmp = Qnil;
|
1999-01-20 07:59:39 +03:00
|
|
|
char *t;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2021-10-21 15:52:17 +03:00
|
|
|
UNPACK_FETCH(&t, char *);
|
2001-01-15 10:01:00 +03:00
|
|
|
if (t) {
|
2021-10-22 19:38:45 +03:00
|
|
|
if (!associates) associates = str_associated(str);
|
|
|
|
tmp = associated_pointer(associates, t);
|
|
|
|
if (len < RSTRING_LEN(tmp)) {
|
|
|
|
tmp = rb_str_new(t, len);
|
|
|
|
str_associate(tmp, associates);
|
2001-01-15 10:01:00 +03:00
|
|
|
}
|
|
|
|
}
|
2006-10-16 02:57:37 +04:00
|
|
|
UNPACK_PUSH(tmp);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
1999-01-20 07:59:39 +03:00
|
|
|
case 'p':
|
* array.c, bignum.c, dln.c, error.c, gc.c, io.c, marshal.c,
numeric.c, pack.c, strftime.c, string.c, thread.c, transcode.c,
transcode_data.h, util.c, variable.c, vm_dump.c,
include/ruby/encoding.h, missing/crypt.c, missing/vsnprintf.c:
suppress VC type warnings. [ruby-core:22726]
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@22914 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2009-03-12 12:16:15 +03:00
|
|
|
if (len > (long)((send - s) / sizeof(char *)))
|
1999-01-20 07:59:39 +03:00
|
|
|
len = (send - s) / sizeof(char *);
|
|
|
|
while (len-- > 0) {
|
2010-02-26 08:17:13 +03:00
|
|
|
if ((size_t)(send - s) < sizeof(char *))
|
1999-01-20 07:59:39 +03:00
|
|
|
break;
|
|
|
|
else {
|
2008-04-22 17:49:43 +04:00
|
|
|
VALUE tmp = Qnil;
|
1999-01-20 07:59:39 +03:00
|
|
|
char *t;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2021-10-21 15:52:17 +03:00
|
|
|
UNPACK_FETCH(&t, char *);
|
2000-04-10 09:48:43 +04:00
|
|
|
if (t) {
|
2021-10-22 19:38:45 +03:00
|
|
|
if (!associates) associates = str_associated(str);
|
|
|
|
tmp = associated_pointer(associates, t);
|
2000-04-10 09:48:43 +04:00
|
|
|
}
|
2006-10-16 02:57:37 +04:00
|
|
|
UNPACK_PUSH(tmp);
|
1999-01-20 07:59:39 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2022-07-21 19:23:58 +03:00
|
|
|
|
2000-05-12 13:07:57 +04:00
|
|
|
case 'w':
|
|
|
|
{
|
2013-06-07 01:20:04 +04:00
|
|
|
char *s0 = s;
|
|
|
|
while (len > 0 && s < send) {
|
|
|
|
if (*s & 0x80) {
|
|
|
|
s++;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
s++;
|
2013-06-12 01:39:55 +04:00
|
|
|
UNPACK_PUSH(rb_integer_unpack(s0, s-s0, 1, 1, INTEGER_PACK_BIG_ENDIAN));
|
2013-06-07 01:20:04 +04:00
|
|
|
len--;
|
|
|
|
s0 = s;
|
|
|
|
}
|
|
|
|
}
|
2000-05-12 13:07:57 +04:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
default:
|
2018-11-16 03:25:54 +03:00
|
|
|
unknown_directive("unpack", type, fmt);
|
1998-01-16 15:13:05 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ary;
|
|
|
|
}
|
|
|
|
|
2016-12-01 17:18:32 +03:00
|
|
|
static VALUE
|
2021-10-18 17:23:54 +03:00
|
|
|
pack_unpack(rb_execution_context_t *ec, VALUE str, VALUE fmt, VALUE offset)
|
2016-12-01 17:18:32 +03:00
|
|
|
{
|
|
|
|
int mode = rb_block_given_p() ? UNPACK_BLOCK : UNPACK_ARRAY;
|
2021-10-18 17:23:54 +03:00
|
|
|
return pack_unpack_internal(str, fmt, mode, RB_NUM2LONG(offset));
|
2016-12-01 17:18:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static VALUE
|
2021-10-18 17:23:54 +03:00
|
|
|
pack_unpack1(rb_execution_context_t *ec, VALUE str, VALUE fmt, VALUE offset)
|
2016-12-01 17:18:32 +03:00
|
|
|
{
|
2021-10-18 17:23:54 +03:00
|
|
|
return pack_unpack_internal(str, fmt, UNPACK_1, RB_NUM2LONG(offset));
|
2016-12-01 17:18:32 +03:00
|
|
|
}
|
|
|
|
|
2007-12-01 19:56:19 +03:00
|
|
|
int
|
|
|
|
rb_uv_to_utf8(char buf[6], unsigned long uv)
|
1999-08-13 09:45:20 +04:00
|
|
|
{
|
|
|
|
if (uv <= 0x7f) {
|
|
|
|
buf[0] = (char)uv;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (uv <= 0x7ff) {
|
2012-07-18 09:29:24 +04:00
|
|
|
buf[0] = castchar(((uv>>6)&0xff)|0xc0);
|
|
|
|
buf[1] = castchar((uv&0x3f)|0x80);
|
1999-08-13 09:45:20 +04:00
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
if (uv <= 0xffff) {
|
2012-07-18 09:29:24 +04:00
|
|
|
buf[0] = castchar(((uv>>12)&0xff)|0xe0);
|
|
|
|
buf[1] = castchar(((uv>>6)&0x3f)|0x80);
|
|
|
|
buf[2] = castchar((uv&0x3f)|0x80);
|
1999-08-13 09:45:20 +04:00
|
|
|
return 3;
|
|
|
|
}
|
|
|
|
if (uv <= 0x1fffff) {
|
2012-07-18 09:29:24 +04:00
|
|
|
buf[0] = castchar(((uv>>18)&0xff)|0xf0);
|
|
|
|
buf[1] = castchar(((uv>>12)&0x3f)|0x80);
|
|
|
|
buf[2] = castchar(((uv>>6)&0x3f)|0x80);
|
|
|
|
buf[3] = castchar((uv&0x3f)|0x80);
|
1999-08-13 09:45:20 +04:00
|
|
|
return 4;
|
|
|
|
}
|
|
|
|
if (uv <= 0x3ffffff) {
|
2012-07-18 09:29:24 +04:00
|
|
|
buf[0] = castchar(((uv>>24)&0xff)|0xf8);
|
|
|
|
buf[1] = castchar(((uv>>18)&0x3f)|0x80);
|
|
|
|
buf[2] = castchar(((uv>>12)&0x3f)|0x80);
|
|
|
|
buf[3] = castchar(((uv>>6)&0x3f)|0x80);
|
|
|
|
buf[4] = castchar((uv&0x3f)|0x80);
|
1999-08-13 09:45:20 +04:00
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
if (uv <= 0x7fffffff) {
|
2012-07-18 09:29:24 +04:00
|
|
|
buf[0] = castchar(((uv>>30)&0xff)|0xfc);
|
|
|
|
buf[1] = castchar(((uv>>24)&0x3f)|0x80);
|
|
|
|
buf[2] = castchar(((uv>>18)&0x3f)|0x80);
|
|
|
|
buf[3] = castchar(((uv>>12)&0x3f)|0x80);
|
|
|
|
buf[4] = castchar(((uv>>6)&0x3f)|0x80);
|
|
|
|
buf[5] = castchar((uv&0x3f)|0x80);
|
1999-08-13 09:45:20 +04:00
|
|
|
return 6;
|
|
|
|
}
|
2004-03-31 06:59:57 +04:00
|
|
|
rb_raise(rb_eRangeError, "pack(U): value out of range");
|
2012-04-14 03:45:37 +04:00
|
|
|
|
2018-07-24 08:38:07 +03:00
|
|
|
UNREACHABLE_RETURN(Qnil);
|
1999-08-13 09:45:20 +04:00
|
|
|
}
|
|
|
|
|
2005-10-13 18:30:54 +04:00
|
|
|
static const unsigned long utf8_limits[] = {
|
2002-12-04 10:39:32 +03:00
|
|
|
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
|
2006-03-01 13:06:03 +03:00
|
|
|
utf8_to_uv(const char *p, 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) {
|
2005-09-24 04:17:43 +04:00
|
|
|
rb_raise(rb_eArgError, "malformed UTF-8 character (expected %ld bytes, given %ld bytes)",
|
2002-12-10 09:23:44 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-11-08 05:54:39 +03:00
|
|
|
#include "pack.rbinc"
|
|
|
|
|
1998-01-16 15:13:05 +03:00
|
|
|
void
|
* array.c: moved to ANSI function style from K&R function style.
(used protoize on windows, so still K&R remains on #ifdef part of
other platforms. And `foo _((boo))' stuff is still there)
[ruby-dev:26975]
* bignum.c, class.c, compar.c, dir.c, dln.c, dmyext.c, enum.c,
enumerator.c, error.c, eval.c, file.c, gc.c, hash.c, inits.c,
io.c, main.c, marshal.c, math.c, numeric.c, object.c, pack.c,
prec.c, process.c, random.c, range.c, re.c, regcomp.c, regenc.c,
regerror.c, regexec.c, regparse.c, regparse.h, ruby.c, signal.c,
sprintf.c, st.c, string.c, struct.c, time.c, util.h, variable.c,
version.c: ditto.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@9126 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2005-09-12 14:44:21 +04:00
|
|
|
Init_pack(void)
|
1998-01-16 15:13:05 +03:00
|
|
|
{
|
2014-02-05 15:56:35 +04:00
|
|
|
id_associated = rb_make_internal_id();
|
1998-01-16 15:13:05 +03:00
|
|
|
}
|