pike.git
/
src
/
builtin.cmod
version
»
Context lines:
10
20
40
80
file
none
3
pike.git/src/builtin.cmod:892:
*! @[hex2string()] */ static const char hexchar[] = { '0','1','2','3','4','5','6','7','8','9', 'a','b','c','d','e','f' }; static const unsigned char hexdecode[256] = {
-
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
-
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
+
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
+
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
/* '0' - '9' */ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
-
0
,
0
,
0
,
0
,
0
,
0
,
0
,
+
16
,
16
,
16
,
16
,
16
,
16
,
16
,
/* 'A' - 'F' */ 10, 11, 12, 13, 14, 15,
-
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
+
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
+
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
16
,
+
/* 'a' - 'f' */ 10, 11, 12, 13, 14, 15,
-
+
+
16,16,16,16,16,16,16,16,16,
+
16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
+
16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
+
16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
+
16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
+
16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
+
16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
+
16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
+
16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
+
16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,16,
}; PMOD_EXPORT PIKEFUN string(0..255) string2hex(string s) optflags OPT_TRY_OPTIMIZE; { struct pike_string *hex; unsigned char *p,*st = (unsigned char *)s->str; int i, l;
pike.git/src/builtin.cmod:932:
for (i=0; i<l; i++) { *p++ = hexchar[*st>>4]; *p++ = hexchar[*st&15]; st++; } RETURN end_shared_string(hex); }
-
/*! @decl string hex2string(string hex)
+
/*! @decl string
(8bit)
hex2string(string
(8bit)
hex)
*! @appears String.hex2string *!
-
*! Convert a string of hexadecimal digits to binary data.
+
*! Convert a string of hexadecimal digits to binary
+
*!
data.
Non-hexadecimal characters will be ignored when between
+
*! tuples. Eg. "00 00" is ok, but "0 000" isn't.
*! *! @seealso *! @[string2hex()] */ PMOD_EXPORT PIKEFUN string(0..255) hex2string(string hex) optflags OPT_TRY_OPTIMIZE; { struct pike_string *s; int tmp, i; unsigned char *p, *q = (unsigned char *)hex->str;
-
int l = hex->len
>>1
;
-
if(hex->size_shift) Pike_error("
Only
hex
digits
allowed.\n");
-
if(hex->len&1) Pike_error("Can't have odd number of digits.\n");
+
int l =
i =
hex->len;
+
if(hex->size_shift) Pike_error("
Wide
strings
are
not
allowed.\n");
-
s = begin_shared_string(l);
+
s = begin_shared_string(l
>>1
);
p = (unsigned char *)s->str;
-
for
(
i=0;
i<
l
;
i++
)
+
while
( l
> 1
)
{ tmp = hexdecode[*q++];
-
*
p++
=
(
tmp<<4
)
|
hexdecode[*q++];
+
if( tmp==16 ) { l--; i--; continue; }
+
*
p
= tmp<<4
;
+
tmp =
hexdecode[*q++];
+
if( tmp==16 ) Pike_error("Illegal hex format.\n");
+
*p++ |= tmp;
+
l -= 2;
}
-
+
if( l && hexdecode[*q]!=16 )
+
Pike_error("Illegal hex format.\n");
+
+
if( hex->len>>1 == i>>1 )
RETURN end_shared_string(s);
-
+
else
+
RETURN end_and_resize_shared_string(s, i>>1);
} /*! @decl array(int) range(string s) *! @appears String.range *! *! Returns the character range of a string in an array of two *! elements. The first element contains the lower bound and the *! second the upper. The precision is only 8 bits, so for wide *! strings only character blocks are known. */