#include "..\..\inc\swilib.h" #include "base64.h" #include "sf_mime.h" #include "decode.h" extern int strncmp_nocase(const char *s1,const char *s2,unsigned int n); #pragma inline int is_space(int c) { return (c==' '?NULL:1); } #pragma inline int toupper(int c) { if ((c>='a')&&(c<='z')) c+='A'-'a'; return(c); } #define MIN(A,B) A=len) { doit=0; break; } unsigned int ch; ch = _sf_uc_bi[*(unsigned char *)str]; if(ch < B64_UNUSED) { pr[n] = ch; n++; str++; } else if(ch == B64_UNUSED) { str++; } else { doit = 0; break; } } if(doit == 0 && n < 4) { pr[n+2] = pr[n+1] = pr[n]='\0'; } *(ou+0) = (pr[0] << 2) | (pr[1] >> 4); *(ou+1) = (pr[1] << 4) | (pr[2] >> 2); *(ou+2) = (pr[2] << 6) | (pr[3] >> 0); ou = ou+((n * 3) >> 2); } while(doit); _sf_b64_len = (ou - output); if(size) *size = _sf_b64_len; *ou = '\0'; /* NUL-terminate */ return (realloc(output,_sf_b64_len+1)); } #pragma inline int char_to_hex(int c) { if (c>='0' && c<='9') return c-'0'; if (c>='A' && c<='F') return c-'A'+10; if (c>='a' && c<='f') return c-'a'+10; return -1; } char * quoted_printable_decode(const char *str, size_t *size) { char *buf; char *s, *d; register int n; register int len; int c; if(!str) return 0; /* * Allocate sufficient space to hold decoded string. */ d=(char *)str; len=size?*size:strlen(str); s=buf=malloc(len + 1); if(!buf) /* ENOMEM? */ return 0; for(; *str && (str - d)"); for(int i = 0, j = 0; s1[i]; i++) { if(s1[i] != '\r' && s1[i] != '\n') s1[j++] = s1[i]; if(!s1[i+1]) s1[j] = 0; } while(script = strstr_nocase(s1, ""); if(!script_end) break; script_end += strlen(""); strcpy(script, script_end); } while(script = strstr_nocase(s1, ""); if(!script_end) break; script_end += strlen(""); strcpy(script, script_end); } s2 = s1; s1 = strreplace(s2, "
", "\r\n"); mfree(s2); s2 = s1; s1 = strreplace(s2, "
", "\r\n"); mfree(s2); s2 = s1; s1 = strreplace(s2, "

", "\r\n"); mfree(s2); s2 = s1; s1 = strreplace(s2, "", "/blockquote>\r\n"); mfree(s2); s2 = s1; s1 = strreplace(s2, "", " "); mfree(s2); s2 = s1; s1 = strreplace(s2, "", "\r\n"); mfree(s2); s2 = s1; s1 = strreplace(s2, ""e;", "\""); mfree(s2); s2 = s1; s1 = strreplace(s2, " ", " "); mfree(s2); s2 = s1; s1 = strreplace(s2, "<", "<"); mfree(s2); s2 = s1; s1 = strreplace(s2, ">", ">"); mfree(s2); s2 = s1; s1 = strreplace(s2, "&", "&"); mfree(s2); s2 = s1; s1 = strreplace(s2, "©", "(c)"); mfree(s2); //сюда мы будем бросать кости char *d = (char*) malloc(strlen(s)+1); //отфильтруем html - результат заведомо меньшей длины char *r = s1; d_ptr = 0; while(*r) { for(;*r && *r != '<'; r++) d[d_ptr++] = *r; for(;*r && *r != '>'; r++); if(*r) r++; } d[d_ptr] = 0; //вернём модифицированную строку в исходный буфер strcpy(s,d); mfree(d); mfree(s1); strip_special(s); } int get_charset(char *charset) { if (!strncmp_nocase(charset,"windows-1251",12)) return WIN_1251; if (!strncmp_nocase(charset,"koi8-r",6)) return KOI8_R; if (!strncmp_nocase(charset,"ISO-8859-5",10)) return ISO_8859_5; if (!strncmp_nocase(charset,"UTF-8",5)) return UTF_8; return WIN_1251; } char *unmime_header(const char *encoded_str, int default_charset) { const char *p = encoded_str; const char *eword_begin_p, *encoding_begin_p, *text_begin_p, *eword_end_p; char charset[32]; int encoding; char *conv_str; char *outbuf; char *out_str; size_t out_len; size_t decoded_len; out_len=strlen(encoded_str) * 2; outbuf = (char*) malloc(out_len); zeromem(outbuf,out_len); while (*p != '\0') { char *decoded_text = NULL; int len; eword_begin_p = strstr(p, ENCODED_WORD_BEGIN); if (!eword_begin_p) { char * curbuf = (char*) malloc(strlen(p)+1); switch(default_charset) { default: case WIN_1251: strcpy(curbuf,p); break; case KOI8_R: koi2win(curbuf,p); break; case ISO_8859_5: iso885952win(curbuf,p); break; case UTF_8: utf82win(curbuf,p); break; } strcat(outbuf, curbuf); mfree(curbuf); //strcat(outbuf, p); break; } encoding_begin_p = strchr(eword_begin_p + 2, '?'); if (!encoding_begin_p) { strcat(outbuf, p); break; } text_begin_p = strchr(encoding_begin_p + 1, '?'); if (!text_begin_p) { strcat(outbuf, p); break; } eword_end_p = strstr(text_begin_p + 1, ENCODED_WORD_END); if (!eword_end_p) { strcat(outbuf, p); break; } if (p == encoded_str) { strncat(outbuf, p, eword_begin_p - p); p = eword_begin_p; } else { /* ignore spaces between encoded words */ const char *sp; for (sp = p; sp < eword_begin_p; sp++) { if (!is_space(*sp)) { strncat(outbuf, p, eword_begin_p - p); p = eword_begin_p; break; } } } len = MIN(sizeof(charset) - 1, encoding_begin_p - (eword_begin_p + 2)); strncpy(charset, eword_begin_p + 2, len); charset[len] = '\0'; encoding = toupper(*(encoding_begin_p + 1)); if (encoding == 'B') { decoded_len=eword_end_p - (text_begin_p + 1); decoded_text = base64_decode(text_begin_p + 1,&decoded_len); } else if (encoding == 'Q') { decoded_len=eword_end_p - (text_begin_p + 1); decoded_text = quoted_printable_decode(text_begin_p + 1,&decoded_len); } else { strncat(outbuf, p, eword_end_p + 2 - p); p = eword_end_p + 2; continue; } switch(get_charset(charset)) { case WIN_1251: strcat(outbuf, decoded_text); p = eword_end_p + 2; continue; case KOI8_R: conv_str=(char*) malloc(strlen(decoded_text)+1); koi2win(conv_str,decoded_text); break; case ISO_8859_5: conv_str=(char*) malloc(strlen(decoded_text)+1); iso885952win(conv_str,decoded_text); break; case UTF_8: conv_str=(char*) malloc(strlen(decoded_text)+1); utf82win(conv_str,decoded_text); break; } strcat(outbuf, conv_str); mfree(conv_str); mfree(decoded_text); p = eword_end_p + 2; } out_str = outbuf; out_len = strlen(outbuf); return realloc(out_str, out_len + 1); }