📄 parse_~2.cpp
字号:
}//copy remaining string else { new_buff=temp_buff; strcat(new_buff,start); start=strchr(new_buff,'\n'); if(start != NULL) *start=' '; } } } } while(status == st_pending); parse_rfc2047(&new_buff); return(new_buff); }//////////////////////////////////////////////////////////////////////// This will check a header line to see if it is in the ASCII encoded// string format specified by RFC2047//// Format of string is:// =?charset?encoding?encoded_text?=//// encoding is Q for quoted printable, B for base64 (case insensitive)// no linear whitespace characters are allowed in the token// limited to 76 characters per token// multiple tokens can appear in a header if folding occurs// int parse_rfc2047(char** buff){ char* find=NULL; char* start=NULL; char* end=NULL; char encoding=0; if(buff == NULL || *buff == NULL) return(-1); find=*buff; while(*find != '\0') { encoding=0; start=NULL; end=NULL; if(*find == '=' && *(find+1) == '?') { encoding=parse_rfc2047_token(find,&start,&end); if(encoding == 'Q' || encoding == 'q') unencode_rfc2047_quoted_printable(find,start,end); else if(encoding == 'B' || encoding == 'b') unencode_rfc2047_base64(find,start,end); } find++; } return(0);}//////////////////////////////////////////////////////////////////////char parse_rfc2047_token(char* buff, char** text, char** end){ char* next=NULL; char* ptr_to_charset=NULL; char* ptr_to_encoding=NULL; char* ptr_to_text=NULL; char* ptr_to_end=NULL; if(buff == NULL || text == NULL || end == NULL) return(0); next=buff; while(ptr_to_end == NULL && *next != '\0' && *next != ' ' && *next != '\t' && *next != '\n') { if(*next == '?') { if(ptr_to_charset == NULL) ptr_to_charset=next; else if(ptr_to_encoding == NULL) ptr_to_encoding=next; else if(ptr_to_text == NULL) ptr_to_text=next; else if(ptr_to_end == NULL) ptr_to_end=next; } next++; } /* check to make sure we found all 4 '?' */ if( ptr_to_charset == NULL || ptr_to_encoding == NULL || ptr_to_text == NULL || ptr_to_end == NULL) return(0); /* check to make sure the first '?' is prepended with a '=' */ if(ptr_to_charset > buff) if( *(ptr_to_charset-1) != '=') return(0); /* check to make sure the last '?' is followed with a '=' */ if( *(ptr_to_end+1) != '=') return(0); /* make sure charset has at least one character */ if( ptr_to_charset+1 >= ptr_to_encoding) return(0); /* make sure encoding is valid character */ if( *(ptr_to_encoding+1) != 'Q' && *(ptr_to_encoding+1) != 'q' && *(ptr_to_encoding+1) != 'B' && *(ptr_to_encoding+1) != 'b') return(0); //passed all checks, looks valid *text=ptr_to_text+1; *end=ptr_to_end-1; return( *(ptr_to_encoding+1) );}//////////////////////////////////////////////////////////////////////// This will unencode a quoted printable string. The encoded text starts// at encoding and ends at end. The start pointer is where the// unencoded message and the remaining string will be copied to (over).int unencode_rfc2047_quoted_printable(char* start, char* encoding, char* end){ char* next_enc=NULL; char* next_save=NULL; int ch=0; if(start == NULL || encoding == NULL) return(-1); next_save=start; next_enc=encoding; while(next_enc <= end && *next_enc != '\0') { if( *next_enc == '=' ) //if encoded char, decode it { ch=quoted_unencode(next_enc+1); if(ch >= 0) //if encoding okay { *(next_save++)=ch; next_enc+=3; } else //bad encoding, copy it to be tolerant *(next_save++)=*(next_enc++); } else //not encoded, just copy it *(next_save++)=*(next_enc++); } if(*next_enc == '?') //should be, unless '\0' next_enc++; if(*next_enc == '=') //should be, unless '\0' next_enc++; while(*next_enc != '\0') //copy remaining string *(next_save++)=*(next_enc++); *(next_save++)=*(next_enc++);//copy terminating NULL return(0);}//////////////////////////////////////////////////////////////////////// This will convert the two ascii hex characters pointed to by text into// binary form. Will return -1 if not valid ascii hex charactersint quoted_unencode(char* text){ int a=0; int b=0; int c=0; if(text == NULL) return(-1); if(*text == '\0' || *(text+1) == '\0') return(-1); a=conv_from_ascii_hex(*text); b=conv_from_ascii_hex(*(text+1)); if(a < 0 || b < 0) return(-1); c=(a & 0x0f) << 4 | (b & 0x0f); c&=0xff; return(c); }//////////////////////////////////////////////////////////////////////// This will convert the ascii hex character c into its pure binary form.// Will return -1 on errorint conv_from_ascii_hex(char 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);}//////////////////////////////////////////////////////////////////////int unencode_rfc2047_base64(char* start, char* encoding, char* end){ int in[4]; int out[3]; int x=0; int err=0; char* ptr_next=NULL; char* ptr_save=NULL; if(start == NULL || encoding == NULL || end == NULL) return(-1); if(start >= encoding || encoding >= end) return(-1); for(ptr_save=start; ptr_save <= end; ptr_save++) { if(*ptr_save == '\0') return(-1); } ptr_next=encoding; ptr_save=start; while(ptr_next+3 <= end) { for(x=0; x<4; x++) in[x]=*(ptr_next+x); for(x=0; x<3; x++) out[x]=0; err=conv_from_base64(in,out); if(err == 0) { for(x=0; x<3; x++) if(out[x] >= 0) *(ptr_save++)=out[x]; ptr_next+=4; } else ptr_next=end; } if(*(end+1) != '\0' && *(end+2) != '\0') ptr_next=end+3; else return(-1); while(*ptr_next != '\0') *(ptr_save++)=*(ptr_next++); //copy remaining string *(ptr_save++)=*(ptr_next++); //copy terminating null return(0);}////////////////////////////////////////////////////////////////////////This will free all mail_header structures used during inbox display//// pass pointer to FIRST mail_header structure and it will free // all subsequent mail_header structuresvoid free_mail_headers(struct mail_header* mh_data){ mail_header* current_header=NULL; mail_header* next_header=NULL; next_header=mh_data; while(next_header != NULL) { current_header=next_header; next_header=current_header->next_header; free_mh_data(current_header); }}////////////////////////////////////////////////////////////////////////This will free the data contained within a mail_header structurevoid free_mh_data(struct mail_header* mh_data){ if(mh_data != NULL) { if(mh_data->from != NULL) destroy_addr_arr(&(mh_data->from)); if(mh_data->to != NULL) destroy_addr_arr(&(mh_data->to)); if(mh_data->cc != NULL) destroy_addr_arr(&(mh_data->cc)); if(mh_data->reply_to != NULL) destroy_addr_arr(&(mh_data->reply_to)); if(mh_data->subject != NULL) free(mh_data->subject); if(mh_data->cont_type_string != NULL) free(mh_data->cont_type_string); if(mh_data->cont_boundary != NULL) free(mh_data->cont_boundary); if(mh_data->filename != NULL) free(mh_data->filename); free(mh_data); }}////////////////////////////////////////////////////////////////////////This will convert a date time string into a time_t value//// pass string that points to first value of date time (no LWSP)time_t compute_time(char* time_string){ struct tm time_struct; time_t conv_time=0; char* months[]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep", "Oct","Nov","Dec"}; char* charptr=NULL; int x; int err; char t_month[4]; for(x=0;x<4;x++) t_month[x]=0; if(time_string == NULL) return(0); charptr=time_string;//rfc822 states an optional day of week as first field. If first field is not// day of month, must be day of week. // Skip Day of week (not needed for calculation) if(*charptr < '0' || *charptr > '9') { charptr=strchr(time_string,' '); if(charptr != NULL) charptr++; } if(charptr == NULL) return(0); err=sscanf(charptr,"%d %3c %d %d:%d:%d", &(time_struct.tm_mday), t_month, &(time_struct.tm_year), &(time_struct.tm_hour), &(time_struct.tm_min), &(time_struct.tm_sec) ); if(err != 6) return(0);//Fix for year time_struct.tm_year-=1900;//Compute month x=0; time_struct.tm_mon=-1; while( time_struct.tm_mon == -1 && x < 12) { if( strcmp(t_month,months[x]) == 0) time_struct.tm_mon=x; x++; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -