日期:2014-05-16 浏览次数:20749 次
char charset[16] = {0}; // =?UTF-8?B?5o6i57Si56We5puy6ZqQ6JeP5Zyo6IOM5ZCO55qE56eY5a+G?= bool decode_subject(char *subject) { static char *iconvob = NULL; static iconv_t cd = (iconv_t)-1; char *p1, *p2; size_t insz, outsz; if(!strstr(subject, "=?")) return true; p1 = strchr(subject + 2, '?'); memcpy(charset, subject + 2, p1 - subject - 2); charset[p1 - subject - 2] = 0; #ifdef DEBUG fprintf(stderr, "charset: %s\n", charset); #endif p1 += 3; p2 = strrchr(p1, '?'); if(cd == (iconv_t)-1) { cd = iconv_open("UTF-8", charset); if(cd == (iconv_t)-1) { perror("iconv_open()"); return false; } } if(!iconvob) { iconvob = (char *)malloc(256); if(!iconvob) { fprintf(stderr, "malloc() failed\n"); return false; } } memset(iconvob, 0, 256); insz = p2 - p1; if(iconv(cd, &p1, &insz, &iconvob, &outsz) == (size_t)-1) { iconv_close(cd); perror("iconv()"); return false; } strcpy(subject, iconvob); return true; }