/* * decoder.c, SJ */ #include #include #include #include #include "decoder.h" #include "htmlentities.h" #include "config.h" static int b64[] = { 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 62, 255, 255, 255, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 255, 255, 255, 0, 255, 255, 255, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 255, 255, 255, 255, 255, 255, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255 }; static char hex_table[] = { 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, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 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, 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, 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, 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, 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 }; static int compmi(const void *m1, const void *m2){ struct mi *mi1 = (struct mi *) m1; struct mi *mi2 = (struct mi *) m2; return strcmp(mi1->entity, mi2->entity); } void sanitiseBase64(char *s){ char *p1; if(s == NULL) return; for(; *s; s++){ if(b64[(unsigned int)(*s & 0xFF)] == 255){ for(p1 = s; p1[0] != '\0'; p1++) p1[0] = p1[1]; } } } inline void pack_4_into_3(char *s, char *s2){ int j, n[4], k1, k2; memset(s2, 0, 3); if(strlen(s) != 4) return; for(j=0; j<4; j++){ k1 = s[j]; n[j] = b64[k1]; } k1 = n[0]; k1 = k1 << 2; k2 = n[1]; k2 = k2 >> 4; s2[0] = k1 | k2; k1 = (n[1] & 0x0F) << 4; k2 = n[2]; k2 = k2 >> 2; s2[1] = k1 | k2; k1 = n[2] << 6; k2 = n[3] >> 0; s2[2] = k1 | k2; } int decodeBase64(char *p){ int i, len=0; char s[5], s2[3], puf[MAXBUFSIZE]; if(strlen(p) < 4 || strlen(p) > MAXBUFSIZE/2) return 0; for(i=0; i sizeof(puf)-1) break; if(strlen(s) == 4){ pack_4_into_3(s, s2); memcpy(puf+len, s2, 3); len += 3; } } *(puf+len) = '\0'; snprintf(p, MAXBUFSIZE-1, "%s", puf); return len; } int decode_base64_to_buffer(char *p, int plen, unsigned char *b, int blen){ int i, len=0; char s[5], s2[3]; if(plen < 4 || plen > blen) return 0; for(i=0; i blen-1) break; if(strlen(s) == 4){ pack_4_into_3(s, s2); memcpy(b+len, s2, 3); len += 3; } } return len; } void decodeQP(char *p){ int i, k=0, a, b; char c; if(p == NULL) return; for(i=0; ival <= 255){ utf8_encode_char(res->val, &__u[0], sizeof(__u), &len); memcpy(&buf[count], &__u[0], len); count += len; } else { buf[count] = 'q'; count++; } } s = q; } } else { buf[count] = *s; count++; } } buf[count] = '\0'; count++; memcpy(p, buf, count); } void decodeURL(char *p){ int i, c, k=0, a, b; if(p == NULL) return; for(i=0; i> 6) ); count++; *(buf+count) = ( 0x80 | (c & 0x3F) ); count++; } else if (c <= 0xFFFF){ *(buf+count) = ( 0xE0 | (c >> 12) ); count++; *(buf+count) = ( 0x80 | ((c >> 6) & 0x3F) ); count++; *(buf+count) = ( 0x80 | (c & 0x3F) ); count++; } *len = count; } void utf8_encode(unsigned char *p){ int count=0, len; unsigned char *u, *s, utf8[MAXBUFSIZE], __u[8]; if(p == NULL || strlen((char *)p) == 0) return; memset(utf8, 0, MAXBUFSIZE); u = &utf8[0]; s = p; for(; *s; s++){ utf8_encode_char(*s, &__u[0], sizeof(__u), &len); /* * this condition should never happen, as according to the RFCs: * * "Each line of characters MUST be no more than 998 characters, and * SHOULD be no more than 78 characters, excluding the CRLF." * */ if(count+len > sizeof(utf8)-1) break; //printf("%s", __u); memcpy(u+count, &__u[0], len); count += len; } *(u+count) = '\0'; count++; memcpy(p, u, count); }