pike.git
/
src
/
modules
/
MIME
/
mime.c
version
»
Context lines:
10
20
40
80
file
none
3
pike.git/src/modules/MIME/mime.c:172:
} /* Restore and exit module */ PIKE_MODULE_EXIT { } static void decode_base64( INT32 args, const char *name, const char *tab) {
-
if(args != 1)
-
Pike_error( "Wrong number of arguments to MIME.%s()\n",name );
-
if (TYPEOF(sp[-1]) != T_STRING)
-
Pike_error( "Wrong type of argument to MIME.%s()\n",name );
-
if (sp[-1].u.string->size_shift != 0)
-
Pike_error( "Char out of range for MIME.%s()\n",name );
-
+
/* Decode the string in sp[-1].u.string. Any whitespace etc must be ignored, so the size of the result can't be exactly calculated from the input size. We'll use a string builder instead. */ struct string_builder buf; SIGNED char *src; ptrdiff_t cnt; INT32 d = 1; int pads = 3;
-
+
if(args != 1)
+
Pike_error( "Wrong number of arguments to MIME.%s()\n",name );
+
if (TYPEOF(sp[-1]) != T_STRING)
+
Pike_error( "Wrong type of argument to MIME.%s()\n",name );
+
if (sp[-1].u.string->size_shift != 0)
+
Pike_error( "Char out of range for MIME.%s()\n",name );
+
init_string_builder( &buf, 0 ); for (src = (SIGNED char *)sp[-1].u.string->str, cnt = sp[-1].u.string->len; cnt--; src++) if(*src>=' ' && tab[*src-' ']>=0) { /* 6 more bits to put into d */ if((d=(d<<6)|tab[*src-' '])>=0x1000000) { /* d now contains 24 valid bits. Put them in the buffer */ string_builder_putchar( &buf, (d>>16)&0xff ); string_builder_putchar( &buf, (d>>8)&0xff );
pike.git/src/modules/MIME/mime.c:301:
/* Update pointers */ *srcp = src; *destp = dest; return g; } static void encode_base64( INT32 args, const char *name, const char *tab, int pad ) {
+
ptrdiff_t groups;
+
ptrdiff_t last;
+
int insert_crlf;
+
ptrdiff_t length;
+
struct pike_string *str;
+
unsigned char *src;
+
char *dest;
+
if(args != 1 && args != 2) Pike_error( "Wrong number of arguments to MIME.%s()\n",name ); if(TYPEOF(sp[-args]) != T_STRING) Pike_error( "Wrong type of argument to MIME.%s()\n",name ); if (sp[-args].u.string->size_shift != 0) Pike_error( "Char out of range for MIME.%s()\n",name ); /* Encode the string in sp[-args].u.string. First, we need to know the number of 24 bit groups in the input, and the number of bytes actually present in the last group. */
-
ptrdiff_t
groups = (sp[-args].u.string->len+2)/3;
-
ptrdiff_t
last = (sp[-args].u.string->len-1)%3+1;
+
groups = (sp[-args].u.string->len+2)/3;
+
last = (sp[-args].u.string->len-1)%3+1;
-
int
insert_crlf = !(args == 2 && TYPEOF(sp[-1]) == T_INT &&
+
insert_crlf = !(args == 2 && TYPEOF(sp[-1]) == T_INT &&
sp[-1].u.integer != 0); /* We need 4 bytes for each 24 bit group, and 2 bytes for each linebreak */
-
ptrdiff_t
length = groups*4+(insert_crlf? (groups/19)*2 : 0);
-
struct pike_string *
str = begin_shared_string( length );
+
length = groups*4+(insert_crlf? (groups/19)*2 : 0);
+
str = begin_shared_string( length );
-
unsigned char *
src = (unsigned char *)sp[-args].u.string->str;
-
char *
dest = str->str;
+
src = (unsigned char *)sp[-args].u.string->str;
+
dest = str->str;
if (groups) { /* Temporary storage for the last group, as we may have to read an extra byte or two and don't want to get any page-faults. */ unsigned char tmp[3], *tmpp = tmp; int i; if (do_b64_encode( groups-1, &src, &dest, insert_crlf, tab ) == 18) /* Skip the final linebreak if it's not to be followed by anything */ str->len -= 2;