From 19a19f1dd6776e57166c3c8dd462817cca56e41c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Mon, 21 Apr 2025 19:54:20 +0300 Subject: [PATCH 01/80] Implement listing for extacted mime files with corresponding `-l` option --- mime.c | 266 ++++++++++++++++++++++++++++++------------------------ mime.h | 26 ++++-- ripmime.c | 37 +++++--- 3 files changed, 193 insertions(+), 136 deletions(-) diff --git a/mime.c b/mime.c index 8ea9233..d5e8bba 100644 --- a/mime.c +++ b/mime.c @@ -60,12 +60,12 @@ #include "MIME_headers.h" -int MIME_unpack_stage2( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info *hinfo, int current_recursion_level, struct SS_object *ss ); -int MIME_unpack_single( char *unpackdir, char *mpname, int current_recursion_level, struct SS_object *ss ); -int MIME_unpack_single_fp( char *unpackdir, FILE *fi, int current_recursion_level, struct SS_object *ss ); -int MIME_unpack_mailbox( char *unpackdir, char *mpname, int current_recursion_level, struct SS_object *ss ); -int MIME_handle_multipart( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ); -int MIME_handle_rfc822( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ); +int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int current_recursion_level, struct SS_object *ss ); +int MIME_unpack_single( RIPMIME_output *unpack_metadata, char *mpname, int current_recursion_level, struct SS_object *ss ); +int MIME_unpack_single_fp( RIPMIME_output *unpack_metadata, FILE *fi, int current_recursion_level, struct SS_object *ss ); +int MIME_unpack_mailbox( RIPMIME_output *unpack_metadata, char *mpname, int current_recursion_level, struct SS_object *ss ); +int MIME_handle_multipart( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ); +int MIME_handle_rfc822( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ); // Predefined filenames #define MIME_BLANKZONE_FILENAME_DEFAULT "_blankzone_" @@ -180,6 +180,8 @@ struct MIME_globals { // wise, any consequent parsing of sub-message bodies // will result in the clobbering of the hinfo struct char subject[_MIME_STRLEN_MAX]; + + int mime_count; }; static struct MIME_globals glb; @@ -942,6 +944,18 @@ int get_random_value(void) { return randval; } +/*------------------------------------------------------------------------ +Procedure: MIME_fprintf_decoded +Purpose: Print meatadata of decoded fragment. +Input: +Output: +Errors: +------------------------------------------------------------------------*/ +void MIME_fprintf_decoded( RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo) +{ + fprintf (stdout, "%d|%d|%d|%d|%s|%s\n", glb.mime_count, glb.attachment_count, glb.filecount, hinfo->current_recursion_level, hinfo->content_type_string, hinfo->filename); +} + /*------------------------------------------------------------------------ Procedure: MIME_test_uniquename ID:1 Purpose: Checks to see that the filename specified is unique. If it's not @@ -1162,13 +1176,13 @@ Purpose: Decodes TNEF encoded attachments Output: Errors: ------------------------------------------------------------------------*/ -int MIME_decode_TNEF( char *unpackdir, struct MIMEH_header_info *hinfo, int keep ) +int MIME_decode_TNEF( RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int keep ) { int result=0; char fullpath[1024]; - snprintf(fullpath,sizeof(fullpath),"%s/%s",unpackdir,hinfo->filename); - TNEF_set_path(unpackdir); + snprintf(fullpath,sizeof(fullpath),"%s/%s",unpack_metadata->dir,hinfo->filename); + TNEF_set_path(unpack_metadata->dir); result = TNEF_main( fullpath ); if (result >= 0) @@ -1193,7 +1207,7 @@ int MIME_report_filename_decoded_RIPOLE(char *filename) Function Name : MIME_decode_OLE Returns Type : int ----Parameter List - 1. char *unpackdir, + 1. RIPMIME_output *unpack_metadata, 2. struct MIMEH_header_info *hinfo, 3. int keep , ------------------ @@ -1206,13 +1220,13 @@ int MIME_report_filename_decoded_RIPOLE(char *filename) Changes: \------------------------------------------------------------------*/ -int MIME_decode_OLE( char *unpackdir, struct MIMEH_header_info *hinfo, int keep ) +int MIME_decode_OLE( RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int keep ) { struct OLE_object ole; char fullpath[1024]; int result; - snprintf(fullpath,sizeof(fullpath),"%s/%s",unpackdir,hinfo->filename); + snprintf(fullpath,sizeof(fullpath),"%s/%s",unpack_metadata->dir,hinfo->filename); OLE_init(&ole); OLE_set_quiet(&ole,glb.quiet); @@ -1222,7 +1236,7 @@ int MIME_decode_OLE( char *unpackdir, struct MIMEH_header_info *hinfo, int keep OLE_set_filename_report_fn(&ole, MIME_report_filename_decoded_RIPOLE ); if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_OLE:DEBUG: Starting OLE Decode",FL); - result = OLE_decode_file(&ole, fullpath, unpackdir ); + result = OLE_decode_file(&ole, fullpath, unpack_metadata->dir ); if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_OLE:DEBUG: Decode done, cleaning up.",FL); OLE_decode_file_done(&ole); @@ -1238,7 +1252,7 @@ Purpose: Decodes a binary type attachment, ie, no encoding, just raw data. Output: Errors: ------------------------------------------------------------------------*/ -int MIME_decode_raw( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info *hinfo, int keep ) +int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int keep ) { int result = 0; char fullpath[1024]; @@ -1258,7 +1272,13 @@ int MIME_decode_raw( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info *h if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Start\n",FL); - snprintf(fullpath,sizeof(fullpath),"%s/%s",unpackdir,hinfo->filename); + glb.mime_count++; + if (unpack_metadata->unpack_mode == RIPMIME_UNPACK_MODE_LIST_FILES) + { + MIME_fprintf_decoded(unpack_metadata, hinfo); + } + + snprintf(fullpath,sizeof(fullpath),"%s/%s",unpack_metadata->dir,hinfo->filename); fo = open(fullpath, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR); if (fo == -1) @@ -1300,12 +1320,12 @@ int MIME_decode_raw( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info *h if (file_has_uuencode) { char full_decode_path[512]; - snprintf(full_decode_path,sizeof(full_decode_path),"%s/%s",unpackdir,hinfo->filename); + snprintf(full_decode_path,sizeof(full_decode_path),"%s/%s",unpack_metadata->dir,hinfo->filename); if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Decoding UUencoded data\n",FL); if ( hinfo->content_transfer_encoding == _CTRANS_ENCODING_UUENCODE ) decode_entire_file = 0; - //result = UUENCODE_decode_uu(NULL, unpackdir, hinfo->filename, hinfo->uudec_name, sizeof(hinfo->uudec_name), decode_entire_file, keep ); - result = UUENCODE_decode_uu(NULL, unpackdir, full_decode_path, hinfo->uudec_name, sizeof(hinfo->uudec_name), decode_entire_file, keep ); + //result = UUENCODE_decode_uu(NULL, unpack_metadata->dir, hinfo->filename, hinfo->uudec_name, sizeof(hinfo->uudec_name), decode_entire_file, keep ); + result = UUENCODE_decode_uu(NULL, unpack_metadata->dir, full_decode_path, hinfo->uudec_name, sizeof(hinfo->uudec_name), decode_entire_file, keep ); if (result == -1) { switch (uuencode_error) { @@ -1334,7 +1354,7 @@ int MIME_decode_raw( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info *h { if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Decoding TNEF format\n",FL); snprintf(hinfo->filename, 128, "%s", hinfo->uudec_name); - MIME_decode_TNEF( unpackdir, hinfo, keep); + MIME_decode_TNEF( unpack_metadata, hinfo, keep); } else LOGGER_log("%s:%d:MIME_decode_raw:WARNING: hinfo has been clobbered.\n",FL); } @@ -1353,7 +1373,7 @@ keep : if set, retain the file Output: Errors: ------------------------------------------------------------------------*/ -int MIME_decode_text( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info *hinfo, int keep ) +int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int keep ) { FILE *of; // output file @@ -1366,7 +1386,13 @@ int MIME_decode_text( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info * int result = 0; int decodesize=0; - snprintf(fullfilename,sizeof(fullfilename),"%s/%s",unpackdir,hinfo->filename); + glb.mime_count++; + if (unpack_metadata->unpack_mode == RIPMIME_UNPACK_MODE_LIST_FILES) + { + MIME_fprintf_decoded(unpack_metadata, hinfo); + } + + snprintf(fullfilename,sizeof(fullfilename),"%s/%s",unpack_metadata->dir,hinfo->filename); if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Decoding TEXT [encoding=%d] to %s\n",FL, hinfo->content_transfer_encoding, fullfilename); if (!f) @@ -1457,7 +1483,7 @@ int MIME_decode_text( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info * if (file_has_uuencode) { char ffname[256]; - snprintf(ffname,256,"%s/%s", unpackdir, hinfo->filename); + snprintf(ffname,256,"%s/%s", unpack_metadata->dir, hinfo->filename); // PLD-20040627-1212 // Make sure uudec_name is blank too // @@ -1473,7 +1499,7 @@ int MIME_decode_text( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info * // propergate this value unintentionally to parent functions (ie, if you were thinking it was // an error-status return value - result = UUENCODE_decode_uu( NULL, unpackdir, ffname, hinfo->uudec_name, sizeof(hinfo->uudec_name), 1, keep ); + result = UUENCODE_decode_uu( NULL, unpack_metadata->dir, ffname, hinfo->uudec_name, sizeof(hinfo->uudec_name), 1, keep ); if (result == -1) { switch (uuencode_error) { @@ -1499,7 +1525,7 @@ int MIME_decode_text( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info * { if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Decoding TNEF format\n",FL); snprintf(hinfo->filename, 128, "%s", hinfo->uudec_name); - MIME_decode_TNEF( unpackdir, hinfo, keep ); + MIME_decode_TNEF( unpack_metadata, hinfo, keep ); } if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Completed decoding UUencoded data.\n",FL); } @@ -1518,12 +1544,12 @@ now have to detect the start character of the "boundary" marker I may consider testing the 1st n' chars of the boundary marker just incase it's not always a hypen '-'. Input: FGET_FILE *f: stream we're reading from -char *unpackdir: directory we have to write the file to +RIPMIME_output *unpack_metadata: directory we have to write the file to struct MIMEH_header_info *hinfo: Auxillairy information such as the destination filename Output: Errors: ------------------------------------------------------------------------*/ -int MIME_decode_64( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info *hinfo ) +int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ) { int i; int cr_total = 0; @@ -1550,9 +1576,15 @@ int MIME_decode_64( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info *hi int of; /* output file pointer */ if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_64:DEBUG: attempting to decode '%s'", FL, hinfo->filename); + glb.mime_count++; + if (unpack_metadata->unpack_mode == RIPMIME_UNPACK_MODE_LIST_FILES) + { + MIME_fprintf_decoded(unpack_metadata, hinfo); + } + /* generate the MIME_filename, and open it up... */ - if (glb.unique_names) MIME_test_uniquename( unpackdir, hinfo->filename, glb.rename_method ); - snprintf(fullMIME_filename,_MIME_STRLEN_MAX,"%s/%s",unpackdir,hinfo->filename); + if (glb.unique_names) MIME_test_uniquename( unpack_metadata->dir, hinfo->filename, glb.rename_method ); + snprintf(fullMIME_filename,_MIME_STRLEN_MAX,"%s/%s",unpack_metadata->dir,hinfo->filename); //of = fopen(fullMIME_filename,"wb"); of = open(fullMIME_filename, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR); /* if we were unable to open the output file, then we better log an error and drop out */ @@ -1836,7 +1868,7 @@ int MIME_decode_64( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info *hi Returns Type : int ----Parameter List 1. FFGET_FILE *f, - 2. char *unpackdir, + 2. RIPMIME_output *unpack_metadata, 3. struct MIMEH_header_info *hinfo, ------------------ Exit Codes : @@ -1848,7 +1880,7 @@ int MIME_decode_64( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info *hi Changes: \------------------------------------------------------------------*/ -int MIME_decode_64_cleanup( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info *hinfo) +int MIME_decode_64_cleanup( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo) { int result = 0; char buffer[128]; @@ -1867,13 +1899,13 @@ Purpose: Decodes a text sequence as detected in the processing of the MIME This is a specialised call, not really a normal part of MIME decoding, but is required in order to deal with decyphering MS Outlook visable defects. Input: char *filename: Name of the encoded file we need to decode -char *unpackdir: Directory we need to unpack the file to +RIPMIME_output *unpack_metadata: Directory we need to unpack the file to struct MIMEH_header_info *hinfo: Header information already gleaned from the headers int current_recursion_level: How many nest levels we are deep Output: Errors: ------------------------------------------------------------------------*/ -int MIME_doubleCR_decode( char *filename, char *unpackdir, struct MIMEH_header_info *hinfo, int current_recursion_level ) +int MIME_doubleCR_decode( char *filename, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int current_recursion_level ) { int result = 0; struct MIMEH_header_info h; @@ -1885,7 +1917,7 @@ int MIME_doubleCR_decode( char *filename, char *unpackdir, struct MIMEH_header_i p = filename; // * Initialise the header fields h.uudec_name[0] = '\0'; - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_doubleCR_decode:DEBUG: filename=%s, path=%s, recursion=%d", FL, filename, unpackdir, current_recursion_level ); + if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_doubleCR_decode:DEBUG: filename=%s, path=%s, recursion=%d", FL, filename, unpack_metadata->dir, current_recursion_level ); memcpy(&h, hinfo, sizeof(h)); // Works for ripMIME snprintf(h.filename, sizeof(h.filename), "%s/%s", unpackdir, p); snprintf(h.filename, sizeof(h.filename), "%s", p); /// Works for Xamime @@ -1893,13 +1925,13 @@ int MIME_doubleCR_decode( char *filename, char *unpackdir, struct MIMEH_header_i if (MIME_is_file_RFC822(filename)) { if (MIME_VERBOSE) LOGGER_log("Attempting to decode Double-CR delimeted MIME attachment '%s'\n",filename); - result = MIME_unpack( unpackdir, filename, current_recursion_level ); // 20040305-1303:PLD - Capture the result of the unpack and propagate up + result = MIME_unpack( unpack_metadata, filename, current_recursion_level ); // 20040305-1303:PLD - Capture the result of the unpack and propagate up } else if (UUENCODE_is_file_uuencoded(h.filename)) { if (MIME_VERBOSE) LOGGER_log("Attempting to decode UUENCODED attachment from Double-CR delimeted attachment '%s'\n",filename); UUENCODE_set_doubleCR_mode(1); - result = UUENCODE_decode_uu(NULL, unpackdir, filename, h.uudec_name, _MIMEH_FILENAMELEN_MAX , 1, 1 ); + result = UUENCODE_decode_uu(NULL, unpack_metadata->dir, filename, h.uudec_name, _MIMEH_FILENAMELEN_MAX , 1, 1 ); UUENCODE_set_doubleCR_mode(0); glb.attachment_count += result; result = 0; @@ -2111,6 +2143,8 @@ int MIME_init( void ) glb.blankfileprefix_expliticly_set = 0; glb.subject[0]='\0'; + + glb.mime_count = 0; return 0; } @@ -2135,7 +2169,7 @@ hardlinks to replicate this in our output. Changes: \------------------------------------------------------------------*/ -int MIME_generate_multiple_hardlink_filenames(struct MIMEH_header_info *hinfo, char *unpackdir) +int MIME_generate_multiple_hardlink_filenames(struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata) { char *name; char oldname[1024]; @@ -2143,7 +2177,7 @@ int MIME_generate_multiple_hardlink_filenames(struct MIMEH_header_info *hinfo, c if (glb.multiple_filenames == 0) return 0; //LOGGER_log("%s:%d:MIME_generate_multiple_hardlink_filenames:DEBUG: Generating hardlinks for %s",FL, hinfo->filename); - snprintf(oldname,sizeof(oldname),"%s/%s",unpackdir, hinfo->filename); + snprintf(oldname,sizeof(oldname),"%s/%s",unpack_metadata->dir, hinfo->filename); if (SS_count(&(hinfo->ss_names)) > 1){ do { @@ -2159,7 +2193,7 @@ int MIME_generate_multiple_hardlink_filenames(struct MIMEH_header_info *hinfo, c np = strrchr(name, '/'); if (np) np++; else np = name; - snprintf(newname,sizeof(newname),"%s/%s",unpackdir, np); + snprintf(newname,sizeof(newname),"%s/%s",unpack_metadata->dir, np); //LOGGER_log("%s:%d:MIME_generate_multiple_hardlink_filenames:DEBUG: Linking %s->%s",FL,newname, oldname); rv = link(oldname, newname); if (rv == -1) @@ -2189,7 +2223,7 @@ int MIME_generate_multiple_hardlink_filenames(struct MIMEH_header_info *hinfo, c char newname[1024]; int rv; - snprintf(newname,sizeof(newname),"%s/%s",unpackdir, name); + snprintf(newname,sizeof(newname),"%s/%s",unpack_metadata->dir, name); //LOGGER_log("%s:%d:MIME_generate_multiple_hardlink_filenames:DEBUG: Linking %s->%s",FL,newname, oldname); rv = link(oldname, newname); if (rv == -1) @@ -2223,7 +2257,7 @@ which is contained within the MIME structure Output: Errors: ------------------------------------------------------------------------*/ -int MIME_decode_encoding( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info *hinfo, struct SS_object *ss ) +int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, struct SS_object *ss ) { int keep = 1; int result = -1; @@ -2281,7 +2315,7 @@ int MIME_decode_encoding( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_in // its tests here if ((glb.unique_names)&&(keep)) { - MIME_test_uniquename( unpackdir, hinfo->filename, glb.rename_method ); + MIME_test_uniquename( unpack_metadata->dir, hinfo->filename, glb.rename_method ); } // If the calling program requested verbosity, then indicate that we're decoding // the file here @@ -2337,7 +2371,7 @@ int MIME_decode_encoding( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_in { case _CTRANS_ENCODING_B64: if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding BASE64 format\n",FL); - result = MIME_decode_64(f, unpackdir, hinfo); + result = MIME_decode_64(f, unpack_metadata, hinfo); switch (result) { case MIME_ERROR_B64_INPUT_STREAM_EOF: break; @@ -2345,7 +2379,7 @@ int MIME_decode_encoding( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_in result = 0; break; case 0: - result = MIME_decode_64_cleanup(f, unpackdir, hinfo); + result = MIME_decode_64_cleanup(f, unpack_metadata, hinfo); break; default: break; @@ -2353,26 +2387,26 @@ int MIME_decode_encoding( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_in break; case _CTRANS_ENCODING_7BIT: if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding 7BIT format\n",FL); - result = MIME_decode_text(f, unpackdir, hinfo, keep); + result = MIME_decode_text(f, unpack_metadata, hinfo, keep); break; case _CTRANS_ENCODING_8BIT: if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding 8BIT format\n",FL); - result = MIME_decode_text(f, unpackdir, hinfo, keep); + result = MIME_decode_text(f, unpack_metadata, hinfo, keep); break; case _CTRANS_ENCODING_BINARY: case _CTRANS_ENCODING_RAW: if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding RAW format\n",FL); - result = MIME_decode_raw(f, unpackdir, hinfo, keep); + result = MIME_decode_raw(f, unpack_metadata, hinfo, keep); break; case _CTRANS_ENCODING_QP: if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding Quoted-Printable format\n",FL); - result = MIME_decode_text(f, unpackdir, hinfo, keep); + result = MIME_decode_text(f, unpack_metadata, hinfo, keep); break; case _CTRANS_ENCODING_UUENCODE: if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding UUENCODED format\n",FL); // Added as a test - remove if we can get this to work in a better way snprintf(hinfo->uudec_name,sizeof(hinfo->uudec_name),"%s",hinfo->filename); - result = UUENCODE_decode_uu(f, unpackdir, hinfo->filename, hinfo->uudec_name, sizeof(hinfo->uudec_name), 0, keep ); + result = UUENCODE_decode_uu(f, unpack_metadata->dir, hinfo->filename, hinfo->uudec_name, sizeof(hinfo->uudec_name), 0, keep ); glb.attachment_count += result; // Because this is a file-count, it's not really an 'error result' as such, so, set the // return code back to 0! @@ -2382,17 +2416,17 @@ int MIME_decode_encoding( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_in switch (hinfo->content_disposition) { case _CDISPOSITION_FORMDATA: if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding UNKNOWN format of FORMDATA disposition\n",FL); - result = MIME_decode_raw(f, unpackdir, hinfo, keep); + result = MIME_decode_raw(f, unpack_metadata, hinfo, keep); break; default: if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding UNKNOWN format\n",FL); - result = MIME_decode_text(f, unpackdir, hinfo, keep); + result = MIME_decode_text(f, unpack_metadata, hinfo, keep); } if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: UNKNOWN Decode completed, result = %d\n",FL,result); break; case _CTRANS_ENCODING_UNSPECIFIED: if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding UNSPECIFIED format\n",FL); - result = MIME_decode_text(f, unpackdir, hinfo, keep); + result = MIME_decode_text(f, unpack_metadata, hinfo, keep); if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding result for UNSPECIFIED format = %d\n",FL, result); // 20040114-1236:PLD: Added nested mail checking // @@ -2407,18 +2441,18 @@ int MIME_decode_encoding( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_in // Original sample mailpack was sent by Farit - thanks. if (1) { - snprintf(hinfo->scratch,sizeof(hinfo->scratch),"%s/%s",unpackdir,hinfo->filename); + snprintf(hinfo->scratch,sizeof(hinfo->scratch),"%s/%s",unpack_metadata->dir,hinfo->filename); LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG:REMOVEME: Testing for RFC822 headers in file %s",FL,hinfo->scratch); if (MIME_is_file_RFC822(hinfo->scratch) > 0 ) { // 20040305-1304:PLD: unpack the file, propagate result upwards - result = MIME_unpack_single( unpackdir, hinfo->scratch, (hinfo->current_recursion_level+1),ss ); + result = MIME_unpack_single( unpack_metadata, hinfo->scratch, (hinfo->current_recursion_level+1),ss ); } } break; default: if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding format is not defined (%d)\n",FL, hinfo->content_transfer_encoding); - result = MIME_decode_raw(f, unpackdir, hinfo, keep); + result = MIME_decode_raw(f, unpack_metadata, hinfo, keep); break; } // Analyze our results @@ -2448,7 +2482,7 @@ int MIME_decode_encoding( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_in // performance of the ripMIME decoding engine if (glb.decode_ole > 0) { - MIME_decode_OLE( unpackdir, hinfo, 0 ); + MIME_decode_OLE( unpack_metadata, hinfo, 0 ); } #endif @@ -2462,7 +2496,7 @@ int MIME_decode_encoding( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_in { if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding TNEF format\n",FL); glb.attachment_count++; - MIME_decode_TNEF( unpackdir, hinfo, 0 ); + MIME_decode_TNEF( unpack_metadata, hinfo, 0 ); } // Decode TNEF // Look for Microsoft MHT files... and try decode them. @@ -2476,16 +2510,16 @@ int MIME_decode_encoding( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_in { // Patched 26-01-03: supplied by Chris Hine if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Microsoft MHT format email filename='%s'\n",FL, hinfo->filename); - snprintf(hinfo->scratch,sizeof(hinfo->scratch),"%s/%s",unpackdir,hinfo->filename); + snprintf(hinfo->scratch,sizeof(hinfo->scratch),"%s/%s",unpack_metadata->dir,hinfo->filename); // 20040305-1304:PLD: unpack the file, propagate result upwards - result = MIME_unpack_single( unpackdir, hinfo->scratch, (hinfo->current_recursion_level+1),ss ); + result = MIME_unpack_single( unpack_metadata, hinfo->scratch, (hinfo->current_recursion_level+1),ss ); } } // Decode MHT files } // If result != -1 // End. - MIME_generate_multiple_hardlink_filenames(hinfo,unpackdir); + MIME_generate_multiple_hardlink_filenames(hinfo,unpack_metadata); if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Done for filename = '%s'",FL,hinfo->filename); return result; } @@ -2493,11 +2527,11 @@ int MIME_decode_encoding( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_in /*------------------------------------------------------------------------ Procedure: MIME_postdecode_cleanup ID:1 Purpose: Performs any cleanup operations required after the immediate completion of the mailpack decoding. -Input: char *unpackdir - directory where the mailpack was unpacked to +Input: RIPMIME_output *unpack_metadata - directory where the mailpack was unpacked to Output: none Errors: ------------------------------------------------------------------------*/ -int MIME_postdecode_cleanup( char *unpackdir, struct SS_object *ss ) +int MIME_postdecode_cleanup( RIPMIME_output *unpack_metadata, struct SS_object *ss ) { char fullpath[256]; int result; @@ -2510,7 +2544,7 @@ int MIME_postdecode_cleanup( char *unpackdir, struct SS_object *ss ) if (MIME_DNORMAL) LOGGER_log("%s:%d: Popped file '%s'",FL, filename); if ( strncmp( glb.blankfileprefix, filename, strlen( glb.blankfileprefix ) ) == 0 ) { - snprintf( fullpath, sizeof(fullpath), "%s/%s", unpackdir, filename ); + snprintf( fullpath, sizeof(fullpath), "%s/%s", unpack_metadata->dir, filename ); result = unlink( fullpath ); if (MIME_VERBOSE) { @@ -2527,7 +2561,7 @@ int MIME_postdecode_cleanup( char *unpackdir, struct SS_object *ss ) Returns Type : int ----Parameter List 1. FFGET_FILE *f, - 2. char *unpackdir, + 2. RIPMIME_output *unpack_metadata, 3. struct MIMEH_header_info *hinfo, 4. int current_recursion_level, 5. struct SS_object *ss , @@ -2541,7 +2575,7 @@ int MIME_postdecode_cleanup( char *unpackdir, struct SS_object *ss ) Changes: \------------------------------------------------------------------*/ -int MIME_handle_multipart( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ) +int MIME_handle_multipart( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ) { int result = 0; if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_handle_multipart:DEBUG: Decoding multipart/embedded \n",FL); @@ -2561,23 +2595,23 @@ int MIME_handle_multipart( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_i // headers and decodes. if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_handle_multipart:DEBUG: Non base64 encoding AND no filename, embedded message\n",FL); h->boundary_located = 0; - result = MIME_unpack_stage2(f, unpackdir, h, current_recursion_level , ss); + result = MIME_unpack_stage2(f, unpack_metadata, h, current_recursion_level , ss); p = BS_top(); if (p) PLD_strncpy(h->boundary, p,sizeof(h->boundary)); } else { if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_handle_multipart:DEBUG: Embedded message has a filename, decoding to file %s",FL,h->filename); - result = MIME_decode_encoding( f, unpackdir, h, ss ); + result = MIME_decode_encoding( f, unpack_metadata, h, ss ); if (result == 0) { // Because we're calling MIME_unpack_single again [ie, recursively calling it // we need to now adjust the input-filename so that it correctly is prefixed // with the directory we unpacked to. - snprintf(scratch,sizeof(scratch),"%s/%s",unpackdir, h->filename); + snprintf(scratch,sizeof(scratch),"%s/%s",unpack_metadata->dir, h->filename); snprintf(h->filename,sizeof(h->filename),"%s",scratch); //result = MIME_unpack_single( unpackdir, h->filename, current_recursion_level +1, ss); - result = MIME_unpack_single( unpackdir, h->filename, current_recursion_level, ss ); + result = MIME_unpack_single( unpack_metadata, h->filename, current_recursion_level, ss ); } } // else-if transfer-encoding != B64 && filename was empty @@ -2590,7 +2624,7 @@ int MIME_handle_multipart( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_i Returns Type : int ----Parameter List 1. FFGET_FILE *f, Input stream - 2. char *unpackdir, Directory to write files to + 2. RIPMIME_output *unpack_metadata, Directory to write files to 3. struct MIMEH_header_info *hinfo, Header information structure 4. int current_recursion_level, Current recursion level 5. struct SS_object *ss , String stack containing already decoded file names @@ -2604,7 +2638,7 @@ int MIME_handle_multipart( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_i Changes: \------------------------------------------------------------------*/ -int MIME_handle_rfc822( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ) +int MIME_handle_rfc822( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ) { /** Decode a RFC822 encoded stream of data from *f **/ int result = 0; @@ -2629,13 +2663,13 @@ int MIME_handle_rfc822( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info // headers and decodes. DMIME LOGGER_log("%s:%d:MIME_handle_rfc822:DEBUG: Non base64 encoding AND no filename, embedded message\n",FL); h->boundary_located = 0; - result = MIME_unpack_stage2(f, unpackdir, h, current_recursion_level , ss); + result = MIME_unpack_stage2(f, unpack_metadata, h, current_recursion_level , ss); p = BS_top(); if (p) PLD_strncpy(h->boundary, p,sizeof(h->boundary)); } else { /** ...else... if the section has a filename or B64 type encoding, we need to put it through extra decoding **/ if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_handle_rfc822:DEBUG: Embedded message has a filename, decoding to file %s",FL,h->filename); - result = MIME_decode_encoding( f, unpackdir, h, ss ); + result = MIME_decode_encoding( f, unpack_metadata, h, ss ); if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_handle_rfc822:DEBUG: Result of extracting %s is %d",FL,h->filename, result); if (result == 0) { /** Because we're calling MIME_unpack_single again [ie, recursively calling it @@ -2643,11 +2677,11 @@ int MIME_handle_rfc822( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info with the directory we unpacked to. **/ if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_handle_rfc822:DEBUG: Now attempting to extract contents of '%s'",FL,h->filename); - snprintf(scratch,sizeof(scratch),"%s/%s",unpackdir, h->filename); + snprintf(scratch,sizeof(scratch),"%s/%s",unpack_metadata->dir, h->filename); snprintf(h->filename,sizeof(h->filename),"%s",scratch); if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_handle_rfc822:DEBUG: Now attempting to extract contents of '%s'",FL,scratch); - result = MIME_unpack_single( unpackdir, scratch, current_recursion_level, ss ); + result = MIME_unpack_single( unpack_metadata, scratch, current_recursion_level, ss ); result = 0; } } /** else-if transfer-encoding != B64 && filename was empty **/ @@ -2660,7 +2694,7 @@ int MIME_handle_rfc822( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info Returns Type : int ----Parameter List 1. FFGET_FILE *f, - 2. char *unpackdir, + 2. RIPMIME_output *unpack_metadata, 3. struct MIMEH_header_info *h, 4. int current_recursion_level, 5. struct SS_object *ss , @@ -2674,21 +2708,21 @@ int MIME_handle_rfc822( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info Changes: \------------------------------------------------------------------*/ -int MIME_handle_plain( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ) +int MIME_handle_plain( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ) { /** Handle a plain text encoded data stream from *f **/ int result = 0; if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_handle_plain:DEBUG: Handling plain email",FL); - result = MIME_decode_encoding( f, unpackdir, h, ss ); + result = MIME_decode_encoding( f, unpack_metadata, h, ss ); if ((result == MIME_ERROR_FFGET_EMPTY)||(result == 0)) { /** Test for RFC822 content... if so, go decode it **/ - snprintf(h->scratch,sizeof(h->scratch),"%s/%s",unpackdir,h->filename); + snprintf(h->scratch,sizeof(h->scratch),"%s/%s",unpack_metadata->dir,h->filename); if (MIME_is_file_RFC822(h->scratch)==1) { /** If the file is RFC822, then decode it using MIME_unpack_single() **/ if (glb.header_longsearch != 0) MIMEH_set_header_longsearch(glb.header_longsearch); - result = MIME_unpack_single( unpackdir, h->scratch, current_recursion_level, ss ); + result = MIME_unpack_single( unpack_metadata, h->scratch, current_recursion_level, ss ); if (glb.header_longsearch != 0) MIMEH_set_header_longsearch(0); } } @@ -2703,7 +2737,7 @@ as required by the MIME structure of the file. Output: Errors: ------------------------------------------------------------------------*/ -int MIME_unpack_stage2( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info *hinfo, int current_recursion_level, struct SS_object *ss ) +int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int current_recursion_level, struct SS_object *ss ) { int result = 0; struct MIMEH_header_info *h; @@ -2754,7 +2788,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info // flag. if (MIMEH_get_doubleCR() != 0) { - MIME_doubleCR_decode( MIMEH_get_doubleCR_name(), unpackdir, h, current_recursion_level); + MIME_doubleCR_decode( MIMEH_get_doubleCR_name(), unpack_metadata, h, current_recursion_level); MIMEH_set_doubleCR( 0 ); FFGET_SDL_MODE = 0; } @@ -2787,14 +2821,14 @@ int MIME_unpack_stage2( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info { // Pass off to the RFC822 handler if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Decoding with RFC822 decoder\n",FL); - result = MIME_handle_rfc822(f,unpackdir,h,current_recursion_level,ss); + result = MIME_handle_rfc822(f,unpack_metadata,h,current_recursion_level,ss); } else if (MIMEH_is_contenttype(_CTYPE_MULTIPART, h->content_type)) { // Pass off to the multipart handler if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Decoding with Multipart decoder\n",FL); - result = MIME_handle_multipart(f,unpackdir,h,current_recursion_level,ss); + result = MIME_handle_multipart(f,unpack_metadata,h,current_recursion_level,ss); } else { if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Decoding boundaryless file (%s)...\n",FL,h->filename); - result = MIME_handle_plain( f, unpackdir,h,current_recursion_level,ss); + result = MIME_handle_plain( f, unpack_metadata,h,current_recursion_level,ss); } // else-if content was RFC822 or multi-part return result; } // End of the boundary-LESS mode ( processing the mail which has no boundaries in the primary headers ) @@ -2807,7 +2841,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info // Decode the data in the current MIME segment // based on the header information retrieved from // the start of this function. - result = MIME_decode_encoding(f, unpackdir, h, ss); + result = MIME_decode_encoding(f, unpack_metadata, h, ss); if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Done decoding, result = %d",FL,result); if (result == 0) { @@ -2873,7 +2907,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info // however, it is a rather robust/reliable way. if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Chose Content-type == RFC822 clause",FL); if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Calling MIME_decode_encoding()",FL); - result = MIME_handle_rfc822(f,unpackdir,h,current_recursion_level,ss); + result = MIME_handle_rfc822(f,unpack_metadata,h,current_recursion_level,ss); // First up - extract the RFC822 body out of the parent mailpack // XX result = MIME_decode_encoding( f, unpackdir, h, ss ); @@ -2900,7 +2934,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info // they are. Shame on me for not remembering, in future I must comment more. if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: NON-BASE64 DECODE\n",FL); h->boundary_located = 0; - result = MIME_unpack_stage2(f, unpackdir, h, current_recursion_level , ss); + result = MIME_unpack_stage2(f, unpack_metadata, h, current_recursion_level , ss); // When we've exited from decoding the sub-mailpack, we need to restore the original // boundary @@ -2917,11 +2951,11 @@ int MIME_unpack_stage2( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info // layout of other nested emails if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Handle Appledouble explicitly",FL); - result = MIME_unpack_stage2(f, unpackdir, h, current_recursion_level , ss); + result = MIME_unpack_stage2(f, unpack_metadata, h, current_recursion_level , ss); } else { if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: RFC822 Message to be decoded...\n",FL); - result = MIME_decode_encoding( f, unpackdir, h, ss ); + result = MIME_decode_encoding( f, unpack_metadata, h, ss ); if (result != 0) return result; // 20040305-1313:PLD else { @@ -2931,10 +2965,10 @@ int MIME_unpack_stage2( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info // we need to now adjust the input-filename so that it correctly is prefixed // with the directory we unpacked to. - snprintf(scratch,sizeof(scratch),"%s/%s",unpackdir, h->filename); + snprintf(scratch,sizeof(scratch),"%s/%s",unpack_metadata->dir, h->filename); snprintf(h->filename,sizeof(h->filename),"%s",scratch); //result = MIME_unpack_single( unpackdir, h->filename, current_recursion_level +1, ss); - result = MIME_unpack_single( unpackdir, h->filename, current_recursion_level,ss ); + result = MIME_unpack_single( unpack_metadata, h->filename, current_recursion_level,ss ); } } // else-if transfer-encoding wasn't B64 and filename was blank @@ -2943,7 +2977,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info // multipart or RFC822 embedded email, we can then simply use // the normal decoding function to interpret its data. if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Decoding a normal attachment \n",FL); - result = MIME_decode_encoding( f, unpackdir, h, ss ); + result = MIME_decode_encoding( f, unpack_metadata, h, ss ); if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Decoding a normal attachment '%s' done. \n",FL, h->filename); // See if we have an attachment output which is actually another @@ -2957,14 +2991,14 @@ int MIME_unpack_stage2( FFGET_FILE *f, char *unpackdir, struct MIMEH_header_info { char *mime_fname; - mime_fname = PLD_dprintf("%s/%s", unpackdir, h->filename); + mime_fname = PLD_dprintf("%s/%s", unpack_metadata->dir, h->filename); if(mime_fname != NULL) { if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Testing '%s' for email type",FL,mime_fname); if (MIME_is_file_RFC822(mime_fname)) { //MIME_unpack_single( unpackdir, mime_fname, (hinfo->current_recursion_level+1), ss); - MIME_unpack_single( unpackdir, mime_fname, current_recursion_level+1,ss); + MIME_unpack_single( unpack_metadata, mime_fname, current_recursion_level+1,ss); } free(mime_fname); } @@ -2994,7 +3028,7 @@ Purpose: Decodes mailbox formatted email files Output: Errors: ------------------------------------------------------------------------*/ -int MIME_unpack_mailbox( char *unpackdir, char *mpname, int current_recursion_level, struct SS_object *ss ) +int MIME_unpack_mailbox( RIPMIME_output *unpack_metadata, char *mpname, int current_recursion_level, struct SS_object *ss ) { FFGET_FILE f; FILE *fi; @@ -3006,7 +3040,7 @@ int MIME_unpack_mailbox( char *unpackdir, char *mpname, int current_recursion_le int result; int input_is_stdin=0; - snprintf(fname,sizeof(fname),"%s/tmp.email000.mailpack",unpackdir); + snprintf(fname,sizeof(fname),"%s/tmp.email000.mailpack",unpack_metadata->dir); if ((mpname[0] == '-')&&(mpname[1] == '\0')) { fi = stdin; @@ -3042,7 +3076,7 @@ int MIME_unpack_mailbox( char *unpackdir, char *mpname, int current_recursion_le // Now, decode the mailpack //MIME_unpack_single(unpackdir, fname, current_recursion_level, ss); // 20040317-2358:PLD - MIME_unpack_single(unpackdir, fname, current_recursion_level ,ss ); + MIME_unpack_single(unpack_metadata, fname, current_recursion_level ,ss ); // Remove the now unpacked mailpack result = remove(fname); if (result == -1) @@ -3050,7 +3084,7 @@ int MIME_unpack_mailbox( char *unpackdir, char *mpname, int current_recursion_le LOGGER_log("%s:%d:MIME_unpack_mailbox:ERROR: removing temporary mailpack '%s' (%s)",FL, fname,strerror(errno)); } // Create a new mailpack filename, and keep on going... - snprintf(fname,sizeof(fname),"%s/tmp.email%03d.mailpack",unpackdir,++mcount); + snprintf(fname,sizeof(fname),"%s/tmp.email%03d.mailpack",unpack_metadata->dir,++mcount); fo = fopen(fname,"w"); } else @@ -3087,7 +3121,7 @@ int MIME_unpack_mailbox( char *unpackdir, char *mpname, int current_recursion_le // Now, decode the mailpack //MIME_unpack_single(unpackdir, fname, current_recursion_level, ss); // 20040317-2358:PLD - MIME_unpack_single(unpackdir, fname, current_recursion_level , ss ); + MIME_unpack_single(unpack_metadata, fname, current_recursion_level , ss ); // Remove the now unpacked mailpack result = remove(fname); if (result == -1) @@ -3101,7 +3135,7 @@ int MIME_unpack_mailbox( char *unpackdir, char *mpname, int current_recursion_le Function Name : MIME_unpack_single Returns Type : int ----Parameter List - 1. char *unpackdir, + 1. RIPMIME_output *unpack_metadata, 2. char *mpname, 3. int current_recursion_level , ------------------ @@ -3114,7 +3148,7 @@ int MIME_unpack_mailbox( char *unpackdir, char *mpname, int current_recursion_le Changes: \------------------------------------------------------------------*/ -int MIME_unpack_single( char *unpackdir, char *mpname, int current_recursion_level, struct SS_object *ss ) +int MIME_unpack_single( RIPMIME_output *unpack_metadata, char *mpname, int current_recursion_level, struct SS_object *ss ) { FILE *fi; /* Pointer for the MIME file we're going to be going through */ int result = 0; @@ -3125,7 +3159,7 @@ int MIME_unpack_single( char *unpackdir, char *mpname, int current_recursion_lev return MIME_ERROR_RECURSION_LIMIT_REACHED; } - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_single:DEBUG: dir=%s packname=%s level=%d (max = %d)\n",FL, unpackdir, mpname, current_recursion_level, glb.max_recursion_level); + if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_single:DEBUG: dir=%s packname=%s level=%d (max = %d)\n",FL, unpack_metadata->dir, mpname, current_recursion_level, glb.max_recursion_level); /* if we're reading in from STDIN */ if( mpname[0] == '-' && mpname[1] == '\0' ) { @@ -3150,7 +3184,7 @@ int MIME_unpack_single( char *unpackdir, char *mpname, int current_recursion_lev return -1; } // 20040317-2359:PLD - result = MIME_unpack_single_fp(unpackdir,fi,current_recursion_level , ss); + result = MIME_unpack_single_fp(unpack_metadata,fi,current_recursion_level , ss); if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_single:DEBUG: result = %d, recursion = %d, filename = '%s'", FL, result, current_recursion_level, mpname ); if ((current_recursion_level > 1)&&(result == 241)) result = 0; fclose(fi); @@ -3161,13 +3195,13 @@ int MIME_unpack_single( char *unpackdir, char *mpname, int current_recursion_lev Procedure: MIME_unpack_single ID:1 Purpose: Decodes a single mailpack file (as apposed to mailbox format) into its possible attachments and text bodies -Input: char *unpackdir: Directory to unpack the attachments to +Input: RIPMIME_output *unpack_metadata: Directory to unpack the attachments to char *mpname: Name of the mailpack we have to decode int current_recusion_level: Level of recursion we're currently at. Output: Errors: ------------------------------------------------------------------------*/ -int MIME_unpack_single_fp( char *unpackdir, FILE *fi, int current_recursion_level, struct SS_object *ss ) +int MIME_unpack_single_fp( RIPMIME_output *unpack_metadata, FILE *fi, int current_recursion_level, struct SS_object *ss ) { struct MIMEH_header_info h; int result = 0; @@ -3178,7 +3212,7 @@ int MIME_unpack_single_fp( char *unpackdir, FILE *fi, int current_recursion_leve // Because this MIME module gets used in both CLI and daemon modes // we should check to see that we can report to stderr // - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_single_fp:DEBUG: dir=%s level=%d (max = %d)\n",FL, unpackdir, current_recursion_level, glb.max_recursion_level); + if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_single_fp:DEBUG: dir=%s level=%d (max = %d)\n",FL, unpack_metadata->dir, current_recursion_level, glb.max_recursion_level); if (current_recursion_level > glb.max_recursion_level) { LOGGER_log("%s:%d:MIME_unpack_single_fp:WARNING: Current recursion level of %d is greater than permitted %d",FL, current_recursion_level, glb.max_recursion_level); @@ -3193,7 +3227,7 @@ int MIME_unpack_single_fp( char *unpackdir, FILE *fi, int current_recursion_leve if ((!hf)&&(glb.save_headers)&&(MIMEH_get_headers_save()==0)) { // Prepend the unpackdir path to the headers file name - snprintf(scratch,sizeof(scratch),"%s/%s",unpackdir, glb.headersname); + snprintf(scratch,sizeof(scratch),"%s/%s",unpack_metadata->dir, glb.headersname); hf = fopen(scratch,"w"); if (!hf) { @@ -3231,8 +3265,8 @@ int MIME_unpack_single_fp( char *unpackdir, FILE *fi, int current_recursion_leve if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_single_fp:DEBUG: preparing to decode, calling stage2...\n",FL); // 20040318-0001:PLD - result = MIME_unpack_stage2(&f, unpackdir, &h, current_recursion_level +1, ss); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_single_fp:DEBUG: done decoding ( in stage2 ) result=%d, to %s\n",FL, result, unpackdir); + result = MIME_unpack_stage2(&f, unpack_metadata, &h, current_recursion_level +1, ss); + if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_single_fp:DEBUG: done decoding ( in stage2 ) result=%d, to %s\n",FL, result, unpack_metadata->dir); // fclose(fi); 20040208-1726:PLD if ( headers_save_set_here > 0 ) { @@ -3259,30 +3293,30 @@ which one to execute based on the mailbox setting Output: Errors: ------------------------------------------------------------------------*/ -int MIME_unpack( char *unpackdir, char *mpname, int current_recursion_level ) +int MIME_unpack( RIPMIME_output *unpack_metadata, char *mpname, int current_recursion_level ) { int result = 0; struct SS_object ss; // Stores the filenames that are created in the unpack operation if (current_recursion_level > glb.max_recursion_level) return MIME_ERROR_RECURSION_LIMIT_REACHED; - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack: Unpacking %s to %s, recursion level is %d",FL,mpname,unpackdir,current_recursion_level); - MIMEH_set_outputdir(unpackdir); + if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack: Unpacking %s to %s, recursion level is %d",FL,mpname,unpack_metadata->dir,current_recursion_level); + MIMEH_set_outputdir(unpack_metadata->dir); if (MIME_DNORMAL) SS_set_debug(&ss,1); SS_init(&ss); if (glb.mailbox_format > 0) { if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack: Unpacking using mailbox format",FL); - result = MIME_unpack_mailbox( unpackdir, mpname, (current_recursion_level), &ss ); + result = MIME_unpack_mailbox( unpack_metadata, mpname, (current_recursion_level), &ss ); } else { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack: Unpacking standard mailpack",FL,mpname,unpackdir,current_recursion_level); - result = MIME_unpack_single( unpackdir, mpname, (current_recursion_level +1), &ss ); + if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack: Unpacking standard mailpack",FL,mpname,unpack_metadata->dir,current_recursion_level); + result = MIME_unpack_single( unpack_metadata, mpname, (current_recursion_level +1), &ss ); } if (glb.no_nameless) { - MIME_postdecode_cleanup( unpackdir, &ss ); + MIME_postdecode_cleanup( unpack_metadata, &ss ); } if (MIME_DNORMAL) diff --git a/mime.h b/mime.h index a3e8e85..1cc70ed 100644 --- a/mime.h +++ b/mime.h @@ -56,13 +56,27 @@ /* status return codes */ #define MIME_STATUS_ZERO_FILE 100 +/* unpack modes */ +#define RIPMIME_UNPACK_MODE_TO_DIRECTORY 0 +#define RIPMIME_UNPACK_MODE_COUNT_FILES 1 +#define RIPMIME_UNPACK_MODE_LIST_FILES 2 + +struct mime_output +{ + char *dir; + int unpack_mode; + // int fragment_number; will be used later +}; + +typedef struct mime_output RIPMIME_output; + int MIME_version( void ); size_t MIME_read_raw( char *src_mpname, char *dest_mpname, size_t rw_buffer_size ); int MIME_read( char *mpname ); /* returns filesize in KB */ -int MIME_unpack( char *unpackdir, char *mpname, int current_recusion_level ); -//int MIME_unpack_single( char *unpackdir, char *mpname, int current_recusion_level ); -//int MIME_unpack_single_fp( char *unpackdir, FILE *fi, int current_recusion_level ); -//int MIME_unpack_mailbox( char *unpackdir, char *mpname, int current_recursion_level ); +int MIME_unpack( RIPMIME_output *unpack_metadata, char *mpname, int current_recusion_level ); +//int MIME_unpack_single( RIPMIME_output *unpack_metadata, char *mpname, int current_recusion_level ); +//int MIME_unpack_single_fp( RIPMIME_output *unpack_metadata, FILE *fi, int current_recusion_level ); +//int MIME_unpack_mailbox( RIPMIME_output *unpack_metadata, char *mpname, int current_recursion_level ); int MIME_insert_Xheader( char *fname, char *xheader ); int MIME_set_blankfileprefix( char *prefix ); int MIME_set_recursion_level(int level); @@ -115,8 +129,8 @@ char *MIME_get_subject( void ); int MIME_init( void ); int MIME_close( void ); int MIME_set_tmpdir( char *tmpdir ); -//int MIME_postdecode_cleanup( char *unpackdir, struct SS_object *ss ); +//int MIME_postdecode_cleanup( RIPMIME_output *unpack_metadata, struct SS_object *ss ); -//int MIME_decode_TNEF( FILE *f, char *unpackdir, struct _header_info *hinfo, int keep ); +//int MIME_decode_TNEF( FILE *f, RIPMIME_output *unpack_metadata, struct _header_info *hinfo, int keep ); #endif diff --git a/ripmime.c b/ripmime.c index 8241ffe..0994b98 100644 --- a/ripmime.c +++ b/ripmime.c @@ -37,7 +37,7 @@ struct RIPMIME_globals { char *inputfile; - char *dir; + RIPMIME_output *output; int use_return_codes; int timeout; int quiet; @@ -64,6 +64,8 @@ char help[] = "ripMIME -i -d " "-e [headers file name] : Dump headers from mailpack (default '_headers_')\n" "-v : Turn on verbosity\n" "-q : Run quietly, do no report non-fatal errors\n" + "-l : list included mime fragments metadata to STDOUT delimited by '|' sign. Contains :\n" + " internal id, attachment count, file count, recursion level, mime content type, file name\n" "\n" "--verbose-contenttype : Turn on verbosity of file content type\n" "--verbose-oldstyle : Uses the v1.2.x style or filename reporting\n" @@ -201,14 +203,14 @@ int RIPMIME_parse_parameters (struct RIPMIME_globals *glb, int argc, char **argv case 'd': if (argv[i][2] != '\0') { - glb->dir = &(argv[i][2]); + glb->output->dir = &(argv[i][2]); } else { i++; if (i < argc) { - glb->dir = argv[i]; + glb->output->dir = argv[i]; } else { @@ -263,6 +265,10 @@ int RIPMIME_parse_parameters (struct RIPMIME_globals *glb, int argc, char **argv break; // blankzone storage option #endif + case 'l': + glb->output->unpack_mode = RIPMIME_UNPACK_MODE_LIST_FILES; + break; + case 'v': MIME_set_verbosity (1); glb->verbose = 1; @@ -526,9 +532,11 @@ Side Effects : Changes: \------------------------------------------------------------------*/ -int RIPMIME_init (struct RIPMIME_globals *glb) +int RIPMIME_init (struct RIPMIME_globals *glb, RIPMIME_output *o) { - glb->dir = defaultdir; + glb->output = o; + glb->output->dir = defaultdir; + glb->output->unpack_mode = RIPMIME_UNPACK_MODE_TO_DIRECTORY; glb->inputfile = NULL; glb->use_return_codes = 0; glb->timeout = 0; @@ -556,7 +564,7 @@ Side Effects : \------------------------------------------------------------------*/ void RIPMIME_signal_alarm( int sig ) { - if (ripmime_globals->quiet == 0) LOGGER_log("%s:%d:RIPMIME_signal_alarm: ripMIME took too long to complete. Mailpack is \"%s\", output dir is \"%s\"",FL, ripmime_globals->inputfile, ripmime_globals->dir ); + if (ripmime_globals->quiet == 0) LOGGER_log("%s:%d:RIPMIME_signal_alarm: ripMIME took too long to complete. Mailpack is \"%s\", output dir is \"%s\"",FL, ripmime_globals->inputfile, ripmime_globals->output->dir ); exit(RIPMIME_ERROR_TIMEOUT); } @@ -587,8 +595,8 @@ int RIPMIME_unpack_single( struct RIPMIME_globals *glb, char *fname ) alarm(glb->timeout); } - MIMEH_set_outputdir (glb->dir); - result = MIME_unpack (glb->dir, fname, 0); + MIMEH_set_outputdir (glb->output->dir); + result = MIME_unpack (glb->output, fname, 0); // do any last minute things @@ -686,6 +694,7 @@ Side Effects : int main (int argc, char **argv) { struct RIPMIME_globals glb; + RIPMIME_output mime_output; int result = 0; /* if the user has just typed in "ripmime" and nothing else, then we had better give them @@ -711,7 +720,7 @@ int main (int argc, char **argv) // Perform system initialisations MIME_init (); - RIPMIME_init (&glb); + RIPMIME_init (&glb, &mime_output); // Setup our default behaviours */ @@ -737,16 +746,16 @@ int main (int argc, char **argv) // clean up the output directory name if required (remove any trailing /'s, as suggested by James Cownie 03/02/2001 - if (glb.dir[strlen (glb.dir) - 1] == '/') + if (glb.output->dir[strlen (glb.output->dir) - 1] == '/') { - glb.dir[strlen (glb.dir) - 1] = '\0'; + glb.output->dir[strlen (glb.output->dir) - 1] = '\0'; } // Create the output directory required as specified by the -d parameter - if (glb.dir != defaultdir) + if (glb.output->dir != defaultdir) { - result = mkdir (glb.dir, S_IRWXU); + result = mkdir (glb.output->dir, S_IRWXU); // if we had a problem creating a directory, and it wasn't just // due to the directory already existing, then we have a bit of @@ -756,7 +765,7 @@ int main (int argc, char **argv) if ((result == -1) && (errno != EEXIST)) { LOGGER_log("ripMIME: Cannot create directory '%s' (%s)\n", - glb.dir, strerror (errno)); + glb.output->dir, strerror (errno)); return RIPMIME_ERROR_CANT_CREATE_OUTPUT_DIR; } From a7abf8f111667c02b34fcda15fa52206a9bb824d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Tue, 22 Apr 2025 07:36:47 +0300 Subject: [PATCH 02/80] Separate `MIME_is_diskfile_RFC822` and in-mem `MIME_is_file_RFC822` --- mime.c | 93 ++++++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 64 insertions(+), 29 deletions(-) diff --git a/mime.c b/mime.c index d5e8bba..b32deff 100644 --- a/mime.c +++ b/mime.c @@ -1072,43 +1072,45 @@ int MIME_test_uniquename( char *path, char *fname, int method ) } /*------------------------------------------------------------------------ -Procedure: MIME_is_file_mime ID:1 +Procedure: MIME_is_RFC Purpose: Determines if the file handed to it is a MIME type email file. -Input: file name to analyze -Output: Returns 0 for NO, 1 for YES, -1 for "Things di +Input: FILE object to analyze +Output: 0 if not RFC, + 1 if the file represents RFC content, + -1 oherwise Errors: ------------------------------------------------------------------------*/ -int MIME_is_file_RFC822( char *fname ) +int MIME_is_file_RFC822( FILE *f ) { char conditions[16][16] = { - "Received: ", "From: ", "Subject: ", "Date: ", "Content-", "content-", "from: ", "subject: ", "date: ", "boundary=", "Boundary=", "MIME-Version" }; + "Received: ", + "From: ", + "Subject: ", + "Date: ", + "Content-", + "content-", + "from: ", + "subject: ", + "date: ", + "boundary=", + "Boundary=", + "MIME-Version" }; int result = 0; int flag_mime_version = 0; int hitcount = 0; int linecount = 100; // We should only need to read the first 10 lines of any file. char *line; - FILE *f; - - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_is_file_RFC822:DEBUG: Testing %s for RFC822 headers",FL,fname); - - f = fopen(fname,"r"); - if (!f) - { - if (glb.quiet == 0) - { - LOGGER_log("%s:%d:MIME_is_file_mime:ERROR: cannot open file '%s' for reading (%s)", FL, fname,strerror(errno)); - } - return 0; - } line = malloc(sizeof(char) *1025); if (!line) { LOGGER_log("%s:%d:MIME_is_file_mime:ERROR: cannot allocate memory for read buffer", FL); - return 0; + return -1; } + fseek(f, 0, SEEK_SET); + while (((!flag_mime_version)||(hitcount < 2))&&(fgets(line,1024,f))&&(linecount--)) { /** test every line for possible headers, until we get a blank line **/ @@ -1133,14 +1135,47 @@ int MIME_is_file_RFC822( char *fname ) } } - fclose(f); - if (hitcount >= 2 && flag_mime_version) result = 1; - else result = 0; - if (line) free(line); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_is_file_RFC822:DEBUG: Hit count = %d, result = %d",FL,hitcount,result); + if (hitcount >= 2 && flag_mime_version) + result = 1; + else + result = 0; + if (line) + free(line); + if (MIME_DNORMAL) + LOGGER_log("%s:%d:MIME_is_file_RFC822:DEBUG: Hit count = %d, result = %d",FL,hitcount,result); return result; } +/*------------------------------------------------------------------------ +Procedure: MIME_is_diskfile_RFC822 +Purpose: Determines if the file handed to it is a MIME type email file. + +Input: file name to analyze +Output: Returns 0 for NO, 1 for YES, -1 for "Things di +Errors: +------------------------------------------------------------------------*/ +int MIME_is_diskfile_RFC822( char *fname ) +{ + int result = 0; + FILE *f; + + if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_is_diskfile_RFC822:DEBUG: Testing %s for RFC822 headers",FL,fname); + + f = fopen(fname,"r"); + if (!f) + { + if (glb.quiet == 0) + { + LOGGER_log("%s:%d:MIME_is_diskfile_RFC822:ERROR: cannot open file '%s' for reading (%s)", FL, fname,strerror(errno)); + } + return 0; + } + + result = MIME_is_file_RFC822(f); + fclose(f); + return result == 1; +} + /*------------------------------------------------------------------------ Procedure: MIME_getchar_start ID:1 Purpose: This function is used on a once-off basis. It's purpose is to locate a @@ -1379,7 +1414,7 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM FILE *of; // output file int linecount = 0; // The number of lines int file_has_uuencode = 0; // Flag to indicate this text has UUENCODE in it - char fullfilename[1024]=""; // Filename of the output file + char fullfilename[1024]=""; // Filename of the output file char line[1024]; // The input lines from the file we're decoding char *get_result = &line[0]; int lastlinewasboundary = 0; @@ -1922,7 +1957,7 @@ int MIME_doubleCR_decode( char *filename, RIPMIME_output *unpack_metadata, struc // Works for ripMIME snprintf(h.filename, sizeof(h.filename), "%s/%s", unpackdir, p); snprintf(h.filename, sizeof(h.filename), "%s", p); /// Works for Xamime if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_doubleCR_decode:DEBUG: header.filename = %s", FL, h.filename ); - if (MIME_is_file_RFC822(filename)) + if (MIME_is_diskfile_RFC822(filename)) { if (MIME_VERBOSE) LOGGER_log("Attempting to decode Double-CR delimeted MIME attachment '%s'\n",filename); result = MIME_unpack( unpack_metadata, filename, current_recursion_level ); // 20040305-1303:PLD - Capture the result of the unpack and propagate up @@ -2443,7 +2478,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct { snprintf(hinfo->scratch,sizeof(hinfo->scratch),"%s/%s",unpack_metadata->dir,hinfo->filename); LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG:REMOVEME: Testing for RFC822 headers in file %s",FL,hinfo->scratch); - if (MIME_is_file_RFC822(hinfo->scratch) > 0 ) + if (MIME_is_diskfile_RFC822(hinfo->scratch) > 0 ) { // 20040305-1304:PLD: unpack the file, propagate result upwards result = MIME_unpack_single( unpack_metadata, hinfo->scratch, (hinfo->current_recursion_level+1),ss ); @@ -2718,7 +2753,7 @@ int MIME_handle_plain( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MI { /** Test for RFC822 content... if so, go decode it **/ snprintf(h->scratch,sizeof(h->scratch),"%s/%s",unpack_metadata->dir,h->filename); - if (MIME_is_file_RFC822(h->scratch)==1) + if (MIME_is_diskfile_RFC822(h->scratch)==1) { /** If the file is RFC822, then decode it using MIME_unpack_single() **/ if (glb.header_longsearch != 0) MIMEH_set_header_longsearch(glb.header_longsearch); @@ -2995,7 +3030,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M if(mime_fname != NULL) { if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Testing '%s' for email type",FL,mime_fname); - if (MIME_is_file_RFC822(mime_fname)) + if (MIME_is_diskfile_RFC822(mime_fname)) { //MIME_unpack_single( unpackdir, mime_fname, (hinfo->current_recursion_level+1), ss); MIME_unpack_single( unpack_metadata, mime_fname, current_recursion_level+1,ss); From 291a80d9fa41aa4ec6e58c8e1ae4573e6e1328c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Wed, 23 Apr 2025 06:39:00 +0300 Subject: [PATCH 03/80] Clean `MIME_headers.c` by gcc warnings --- MIME_headers.c | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/MIME_headers.c b/MIME_headers.c index 57812d3..8ce6431 100644 --- a/MIME_headers.c +++ b/MIME_headers.c @@ -1072,12 +1072,10 @@ int MIMEH_read_headers( struct MIMEH_header_info *hinfo, FFGET_FILE *f ) int linesize=0; int totalsize_original=0; int result = 0; - int firstline = 1; int search_count=0; char *tmp; char *tmp_original; char *fget_result = NULL; - char *headerline_end; char *p; char *linestart; char *lineend; @@ -1171,7 +1169,6 @@ int MIMEH_read_headers( struct MIMEH_header_info *hinfo, FFGET_FILE *f ) glb.headerline = tmp; totalsize = linesize; PLD_strncpy(glb.headerline, linestart, (linesize +1)); - headerline_end = glb.headerline +totalsize; } // If the global headerline is currently NULL else { @@ -1249,11 +1246,8 @@ int MIMEH_read_headers( struct MIMEH_header_info *hinfo, FFGET_FILE *f ) FFGET_doubleCR = 0; FFGET_SDL_MODE = 0; } // FFGET_doubleCR test - - firstline = 0; } // While reading more headers from the source file. - // If FFGET ran out of data whilst processing the headers, then acknowledge this // by returning a -1. // @@ -2745,13 +2739,13 @@ int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ) { /** scan through our headers string looking for information that is ** valid **/ - char *safeh, *h, *safehl; + char *h, *safehl; char *current_header_position; int headerlength; if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: Start [hinfo=%p]\n",FL, hinfo); - safeh = h = headers; + h = headers; /** Duplicate the headers for processing - this way we don't 'taint' the ** original headers during our searching / altering. **/ From d24d65f465f357636170fe294a021f2d791e04f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Wed, 23 Apr 2025 06:46:52 +0300 Subject: [PATCH 04/80] Separate `UUENCODE_is_diskfile_uuencoded` and in-mem `UUENCODE_is_diskfile_uuencoded` --- uuencode.c | 72 ++++++++++++++++-------------------------------------- uuencode.h | 2 +- 2 files changed, 22 insertions(+), 52 deletions(-) diff --git a/uuencode.c b/uuencode.c index 2209b2a..3f1d036 100644 --- a/uuencode.c +++ b/uuencode.c @@ -283,14 +283,28 @@ int UUENCODE_is_uuencode_header( char *line ) } } - return result; } +int UUENCODE_is_file_uuencoded( FILE *f ) +{ + int result = 0; + int linecount = 0; + int limit=20; + char line[ UUENCODE_STRLEN_MAX ]; - - - + while ((linecount < limit)&&(fgets(line, sizeof(line), f))) + { + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_is_file_uuencoded:DEBUG: Testing line '%s'\n", FL, line); + if (UUENCODE_is_uuencode_header( line )) + { + result = 1; + break; + } + linecount++; + } + return result; +} /*------------------------------------------------------------------------ Procedure: UUENCODE_is_file_uuenc ID:1 @@ -304,35 +318,21 @@ Output: 0 = not uuencoded 1 = _probably_ uuencoded. Errors: ------------------------------------------------------------------------*/ -int UUENCODE_is_file_uuencoded( char *fname ) +int UUENCODE_is_diskfile_uuencoded( char *fname ) { int result = 0; - int linecount = 0; - int limit=20; - char line[ UUENCODE_STRLEN_MAX ]; FILE *f; f = fopen(fname,"r"); if (!f) { - LOGGER_log("%s:%d:UUENCODE_is_file_uuencoded:ERROR: cannot open file '%s' for reading (%s)", FL, fname,strerror(errno)); + LOGGER_log("%s:%d:UUENCODE_is_diskfile_uuencoded:ERROR: cannot open file '%s' for reading (%s)", FL, fname,strerror(errno)); uuencode_error = UUENCODE_STATUS_CANNOT_OPEN_FILE; return -1; } - while ((linecount < limit)&&(fgets(line, sizeof(line), f))) - { - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_is_file_uuencoded:DEBUG: Testing line '%s'\n", FL, line); - if (UUENCODE_is_uuencode_header( line )) - { - result = 1; - break; - } - linecount++; - } - + result = UUENCODE_is_file_uuencoded(f); fclose(f); - return result; } @@ -441,18 +441,10 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *unpackdir, char *input_filename, ch while (!FFGET_feof(finf)) { filename_found = 0; - - // First lets locate the BEGIN line of this UUDECODE file - - // if (output_filename_supplied == 0) - - if (1) /** 20041105-23H00:PLD: Stepan Kasal - UUbegin patch **/ { - while (FFGET_fgets(buf, sizeof(buf), finf)) { - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: BUFFER: \n%s\n", FL, buf); // Check for the presence of 'BEGIN', but make sure it's not followed by a @@ -494,7 +486,6 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *unpackdir, char *input_filename, ch filename_found = 1; break; } // If line starts with BEGIN - } // While more lines in the INPUT file. } @@ -508,9 +499,6 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *unpackdir, char *input_filename, ch if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Output filename set to '%s'",FL, bp); } - - - // If we have a filename, and we have our bp as NON-null, then we shall commence // to decode the UUencoded data from the stream. @@ -524,7 +512,6 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *unpackdir, char *input_filename, ch // If our filename wasn't supplied via the params, then copy it over here if (output_filename_supplied == 0) snprintf( out_filename, out_filename_size, "%s", bp ); - // Create the new output full path snprintf(fullpath, sizeof(fullpath), "%s/%s", unpackdir, bp ); @@ -550,7 +537,6 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *unpackdir, char *input_filename, ch while (outf) { - // for each input line FFGET_fgets(buf, sizeof(buf), finf); if (UUENCODE_DPEDANTIC) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Read line:\n%s",FL,buf); @@ -576,10 +562,8 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *unpackdir, char *input_filename, ch } // The first char of the line indicates how many bytes are to be expected - n = uudec[(int)*buf]; - // If the line is a -blank- then break out. if ((start_found == 0)&&((*buf == '\n')||(*buf == '\r'))) continue; @@ -649,10 +633,8 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *unpackdir, char *input_filename, ch if (bc != wbcount) { LOGGER_log("%s:%d:ERROR: Attempted to write %ld bytes, only wrote %ld\n", FL, wbcount, bc); } - } - if (outfo) fclose(outf); // Call our reporting function, else, if no function is defined, use the default // standard call @@ -668,7 +650,6 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *unpackdir, char *input_filename, ch } filecount++; - } // If valid filename was found for UUdecode else @@ -682,26 +663,15 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *unpackdir, char *input_filename, ch // If this file was a result of the x-uuencode content encoding, then we need to exit out // as we're reading in the -stream-, and we dont want to carry on reading because we'll // end up just absorbing email data which we weren't supposed to. - if ((f)&&( !decode_whole_file )) break; - - } // While !feof(inf) - - if (writebuffer) free(writebuffer); if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Completed\n",FL); if (inf) fclose(inf); - return filecount; } - - - - - diff --git a/uuencode.h b/uuencode.h index 07c4f16..feb35bc 100644 --- a/uuencode.h +++ b/uuencode.h @@ -18,7 +18,7 @@ int UUENCODE_set_doubleCR_mode( int level ); int UUENCODE_set_filename_report_fn( int (*ptr_to_fn)(char *, char *) ); int UUENCODE_is_uuencode_header( char *line ); -int UUENCODE_is_file_uuencoded( char *fname ); +int UUENCODE_is_diskfile_uuencoded( char *fname ); int UUENCODE_decode_uu( FFGET_FILE *f, char *unpackdir, char *input_filename, char *out_filename, int out_filename_size, int decode_whole_file, int keep ); From 77a4dd237f453e27c4d8396b27c5d21bf7db0963 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Wed, 23 Apr 2025 06:48:06 +0300 Subject: [PATCH 05/80] Use `UUENCODE_is_diskfile_uuencoded` --- mime.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mime.c b/mime.c index b32deff..48d322b 100644 --- a/mime.c +++ b/mime.c @@ -1962,7 +1962,7 @@ int MIME_doubleCR_decode( char *filename, RIPMIME_output *unpack_metadata, struc if (MIME_VERBOSE) LOGGER_log("Attempting to decode Double-CR delimeted MIME attachment '%s'\n",filename); result = MIME_unpack( unpack_metadata, filename, current_recursion_level ); // 20040305-1303:PLD - Capture the result of the unpack and propagate up } - else if (UUENCODE_is_file_uuencoded(h.filename)) + else if (UUENCODE_is_diskfile_uuencoded(h.filename)) { if (MIME_VERBOSE) LOGGER_log("Attempting to decode UUENCODED attachment from Double-CR delimeted attachment '%s'\n",filename); UUENCODE_set_doubleCR_mode(1); From 55090252da8a17df98edf35fa5c8f25500d338e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Wed, 23 Apr 2025 07:09:16 +0300 Subject: [PATCH 06/80] Simplify `if..else..else` to `switch` --- mime.c | 47 ++++++++++++++++++----------------------------- 1 file changed, 18 insertions(+), 29 deletions(-) diff --git a/mime.c b/mime.c index 48d322b..dfd6e59 100644 --- a/mime.c +++ b/mime.c @@ -1026,36 +1026,25 @@ int MIME_test_uniquename( char *path, char *fname, int method ) } else { - if (method == _MIME_RENAME_METHOD_PREFIX) - { - snprintf(newname,_MIME_STRLEN_MAX,"%s/%d_%s",path,count,fname); + switch (method) { + case _MIME_RENAME_METHOD_PREFIX: + snprintf(newname,_MIME_STRLEN_MAX,"%s/%d_%s",path,count,fname); + break; + case _MIME_RENAME_METHOD_INFIX: + snprintf(newname,_MIME_STRLEN_MAX,"%s/%s_%d.%s",path,frontname,count,extention); + break; + case _MIME_RENAME_METHOD_POSTFIX: + snprintf(newname,_MIME_STRLEN_MAX,"%s/%s_%d",path,fname,count); + break; + case _MIME_RENAME_METHOD_RANDPREFIX: + snprintf(newname,_MIME_STRLEN_MAX,"%s/%d_%d_%s",path,count,randval,fname); + break; + case _MIME_RENAME_METHOD_RANDINFIX: + snprintf(newname,_MIME_STRLEN_MAX,"%s/%s_%d_%d.%s",path,frontname,count,randval,extention); + break; + case _MIME_RENAME_METHOD_RANDPOSTFIX: + snprintf(newname,_MIME_STRLEN_MAX,"%s/%s_%d_%d",path,fname,count,randval); } - else - if (method == _MIME_RENAME_METHOD_INFIX) - { - snprintf(newname,_MIME_STRLEN_MAX,"%s/%s_%d.%s",path,frontname,count,extention); - } - else - if (method == _MIME_RENAME_METHOD_POSTFIX) - { - snprintf(newname,_MIME_STRLEN_MAX,"%s/%s_%d",path,fname,count); - } - else - /* Handle randome methots*/ - if (method == _MIME_RENAME_METHOD_RANDPREFIX) - { - snprintf(newname,_MIME_STRLEN_MAX,"%s/%d_%d_%s",path,count,randval,fname); - } - else - if (method == _MIME_RENAME_METHOD_RANDINFIX) - { - snprintf(newname,_MIME_STRLEN_MAX,"%s/%s_%d_%d.%s",path,frontname,count,randval,extention); - } - else - if (method == _MIME_RENAME_METHOD_RANDPOSTFIX) - { - snprintf(newname,_MIME_STRLEN_MAX,"%s/%s_%d_%d",path,fname,count,randval); - } count++; } } From f1dd647c4f837f297cafc698584d8093ceb6bb2b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Wed, 23 Apr 2025 07:47:58 +0300 Subject: [PATCH 07/80] Remove trailing spaces and tabulations --- README.md | 2 +- boundary-stack.c | 146 +++++++++++++++++++++++------------------------ ffget.c | 20 +++---- logger.c | 2 +- mime.c | 12 ++-- strstack.c | 102 ++++++++++++++++----------------- uuencode.c | 56 +++++++++--------- 7 files changed, 170 insertions(+), 170 deletions(-) diff --git a/README.md b/README.md index 68ffb52..15e3c46 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ ### Features -- Extracts all attachments even from multiple MUA personalities +- Extracts all attachments even from multiple MUA personalities ### TODO diff --git a/boundary-stack.c b/boundary-stack.c index 4f6b5c5..8866214 100644 --- a/boundary-stack.c +++ b/boundary-stack.c @@ -49,16 +49,16 @@ static struct BS_globals glb; Function Name : BS_init Returns Type : int ----Parameter List - 1. void , + 1. void , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: - + -------------------------------------------------------------------- Changes: - + \------------------------------------------------------------------*/ int BS_init( void ) { @@ -82,14 +82,14 @@ int BS_init( void ) ----Parameter List 1. int limit , how many boundary strings to hold ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: - + -------------------------------------------------------------------- Changes: - + \------------------------------------------------------------------*/ int BS_set_hold_limit( int limit ) { @@ -102,16 +102,16 @@ int BS_set_hold_limit( int limit ) Function Name : BS_set_verbose Returns Type : int ----Parameter List - 1. int level , + 1. int level , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: - + -------------------------------------------------------------------- Changes: - + \------------------------------------------------------------------*/ int BS_set_verbose( int level ) { @@ -124,16 +124,16 @@ int BS_set_verbose( int level ) Function Name : BS_set_debug Returns Type : int ----Parameter List - 1. int level , + 1. int level , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: - + -------------------------------------------------------------------- Changes: - + \------------------------------------------------------------------*/ int BS_set_debug( int level ) { @@ -146,16 +146,16 @@ int BS_set_debug( int level ) Function Name : BS_set_boundary_detect_limit Returns Type : int ----Parameter List - 1. int limit , + 1. int limit , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: - + -------------------------------------------------------------------- Changes: - + \------------------------------------------------------------------*/ int BS_set_boundary_detect_limit( int limit ) { @@ -172,16 +172,16 @@ int BS_set_boundary_detect_limit( int limit ) Function Name : BS_clear Returns Type : int ----Parameter List - 1. void , + 1. void , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: - + -------------------------------------------------------------------- Changes: - + \------------------------------------------------------------------*/ int BS_clear( void ) { @@ -209,16 +209,16 @@ int BS_clear( void ) Function Name : BS_non_hyphen_length Returns Type : int ----Parameter List - 1. char *boundary , + 1. char *boundary , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: - + -------------------------------------------------------------------- Changes: - + \------------------------------------------------------------------*/ int BS_non_hyphen_length( char *boundary ) { @@ -234,28 +234,28 @@ int BS_non_hyphen_length( char *boundary ) Function Name : BS_push Returns Type : int ----Parameter List - 1. char *boundary , + 1. char *boundary , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: - + -------------------------------------------------------------------- Changes: - + \------------------------------------------------------------------*/ int BS_push( char *boundary ) { struct BS_node *node; - if ((glb.hold_limit > 0)&&(glb.count >= glb.hold_limit)) + if ((glb.hold_limit > 0)&&(glb.count >= glb.hold_limit)) { DBS LOGGER_log("%s:%d:BS_push:DEBUG: Number of boundaries to hold is at limit (limit=%d)",FL,glb.hold_limit); return 0; } - + node = malloc(sizeof(struct BS_node)); DBS LOGGER_log("%s:%d:BS_push:DEBUG: head = %p, nn = %p boundary = '%s'",FL, glb.boundarystack, node, boundary); @@ -289,16 +289,16 @@ int BS_push( char *boundary ) Function Name : *BS_pop Returns Type : char ----Parameter List - 1. void , + 1. void , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: - + -------------------------------------------------------------------- Changes: - + \------------------------------------------------------------------*/ char *BS_pop( void ) { @@ -321,16 +321,16 @@ char *BS_pop( void ) Function Name : *BS_top Returns Type : char ----Parameter List - 1. void , + 1. void , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: - + -------------------------------------------------------------------- Changes: - + \------------------------------------------------------------------*/ char *BS_top( void ) { @@ -346,16 +346,16 @@ char *BS_top( void ) Function Name : BS_count Returns Type : int ----Parameter List - 1. void , + 1. void , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: - + -------------------------------------------------------------------- Changes: - + \------------------------------------------------------------------*/ int BS_count( void ) { @@ -367,16 +367,16 @@ int BS_count( void ) Function Name : BS_is_long_enough Returns Type : int ----Parameter List - 1. int blen , + 1. int blen , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: - + -------------------------------------------------------------------- Changes: - + \------------------------------------------------------------------*/ int BS_is_long_enough( int blen ) { @@ -390,17 +390,17 @@ int BS_is_long_enough( int blen ) Function Name : BS_boundary_detect Returns Type : int ----Parameter List - 1. char *needle, - 2. char *haystack , + 1. char *needle, + 2. char *haystack , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: - + -------------------------------------------------------------------- Changes: - + \------------------------------------------------------------------*/ int BS_boundary_detect( char *haystack, char *needle, int needle_length ) { @@ -434,8 +434,8 @@ int BS_boundary_detect( char *haystack, char *needle, int needle_length ) if (strncmp( needle, haystack_start, needle_length )==0) { DBS LOGGER_log("%s:%d:BS_boundary_detect:DEBUG: Hit on compare",FL); - result = 0; - break; + result = 0; + break; } haystack_start++; } @@ -454,13 +454,13 @@ int BS_boundary_detect( char *haystack, char *needle, int needle_length ) 2. int len , the length of the boundary ------------------ Exit Codes : 1 == boundary found, 0 == no boundary found - Side Effects : + Side Effects : -------------------------------------------------------------------- Comments: - + -------------------------------------------------------------------- Changes: - + \------------------------------------------------------------------*/ int BS_cmp( char *boundary, int len ) { @@ -504,7 +504,7 @@ int BS_cmp( char *boundary, int len ) { // if (node->boundary_length <= len) if (node->boundary_nhl == nhl) - { + { DBS LOGGER_log("%s:%d:BS_cmp:DEBUG: Comparing '%s' to '%s'", FL, boundary, node->boundary); // * 20040903-08H57:PLD: Set boundary length comparison from > 0 to >= 0 if ((node->boundary != NULL)&&(node->boundary_length >= 0)) diff --git a/ffget.c b/ffget.c index cdd0360..8c9c9b2 100644 --- a/ffget.c +++ b/ffget.c @@ -58,10 +58,10 @@ int FFGET_set_watch_SDL( int level ) Function Name : FFGET_set_allow_nul Returns Type : int ----Parameter List - 1. int level , + 1. int level , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: This tells the FFGET_raw() if it needs to remove \0's or not @@ -270,13 +270,13 @@ int FFGET_feof( FFGET_FILE *f ) Function Name : FFGET_seek Returns Type : int ----Parameter List - 1. FFGET_FILE *f, - 2. size_t offset , + 1. FFGET_FILE *f, + 2. size_t offset , ------------------ - Exit Codes : + Exit Codes : -1 = error, check logs for reason of failure. - Side Effects : + Side Effects : -------------------------------------------------------------------- Comments: Seeks to 'offset' bytes from the first byte of the file. @@ -309,10 +309,10 @@ int FFGET_seek( FFGET_FILE *f, long offset, int whence ) Function Name : FFGET_tell Returns Type : size_t ----Parameter List - 1. FFGET_FILE *f , + 1. FFGET_FILE *f , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: Returns the position in the file that the current "file cursor" diff --git a/logger.c b/logger.c index d0cc5dc..ed049a6 100644 --- a/logger.c +++ b/logger.c @@ -196,7 +196,7 @@ int LOGGER_clean_output( char *string, char **buffer ) int maxsize = slen *2; // First up, allocate maxsize bytes for a temporary new string. - newstr = malloc(slen *2 +1); + newstr = malloc(slen *2 +1); if ( newstr == NULL ) { // FIXME - Report an error here ... to -somewhere- diff --git a/mime.c b/mime.c index dfd6e59..721661e 100644 --- a/mime.c +++ b/mime.c @@ -180,7 +180,7 @@ struct MIME_globals { // wise, any consequent parsing of sub-message bodies // will result in the clobbering of the hinfo struct char subject[_MIME_STRLEN_MAX]; - + int mime_count; }; @@ -1126,7 +1126,7 @@ int MIME_is_file_RFC822( FILE *f ) if (hitcount >= 2 && flag_mime_version) result = 1; - else + else result = 0; if (line) free(line); @@ -1600,11 +1600,11 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH int of; /* output file pointer */ if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_64:DEBUG: attempting to decode '%s'", FL, hinfo->filename); - glb.mime_count++; + glb.mime_count++; if (unpack_metadata->unpack_mode == RIPMIME_UNPACK_MODE_LIST_FILES) { MIME_fprintf_decoded(unpack_metadata, hinfo); - } + } /* generate the MIME_filename, and open it up... */ if (glb.unique_names) MIME_test_uniquename( unpack_metadata->dir, hinfo->filename, glb.rename_method ); @@ -2167,7 +2167,7 @@ int MIME_init( void ) glb.blankfileprefix_expliticly_set = 0; glb.subject[0]='\0'; - + glb.mime_count = 0; return 0; @@ -2492,7 +2492,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct case MIME_ERROR_RECURSION_LIMIT_REACHED: return result; break; - default: + default: return result; } diff --git a/strstack.c b/strstack.c index 65c4635..9baeaa0 100644 --- a/strstack.c +++ b/strstack.c @@ -19,16 +19,16 @@ Function Name : SS_init Returns Type : int ----Parameter List - 1. void , + 1. void , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: - + -------------------------------------------------------------------- Changes: - + \------------------------------------------------------------------*/ int SS_init( struct SS_object *ss ) { @@ -44,16 +44,16 @@ int SS_init( struct SS_object *ss ) Function Name : SS_set_verbose Returns Type : int ----Parameter List - 1. int level , + 1. int level , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: - + -------------------------------------------------------------------- Changes: - + \------------------------------------------------------------------*/ int SS_set_verbose( struct SS_object *ss, int level ) { @@ -66,16 +66,16 @@ int SS_set_verbose( struct SS_object *ss, int level ) Function Name : SS_set_debug Returns Type : int ----Parameter List - 1. int level , + 1. int level , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: - + -------------------------------------------------------------------- Changes: - + \------------------------------------------------------------------*/ int SS_set_debug( struct SS_object *ss, int level ) { @@ -88,16 +88,16 @@ int SS_set_debug( struct SS_object *ss, int level ) Function Name : SS_done Returns Type : int ----Parameter List - 1. void , + 1. void , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: - + -------------------------------------------------------------------- Changes: - + \------------------------------------------------------------------*/ int SS_done( struct SS_object *ss ) { @@ -125,16 +125,16 @@ int SS_done( struct SS_object *ss ) Function Name : SS_dump Returns Type : int ----Parameter List - 1. struct SS_object *ss , + 1. struct SS_object *ss , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: - + -------------------------------------------------------------------- Changes: - + \------------------------------------------------------------------*/ int SS_dump( struct SS_object *ss ) { @@ -153,16 +153,16 @@ int SS_dump( struct SS_object *ss ) Function Name : SS_push Returns Type : int ----Parameter List - 1. char *string , + 1. char *string , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: - + -------------------------------------------------------------------- Changes: - + \------------------------------------------------------------------*/ int SS_push( struct SS_object *ss, char *data, size_t data_length ) { @@ -192,16 +192,16 @@ int SS_push( struct SS_object *ss, char *data, size_t data_length ) Function Name : *SS_pop Returns Type : char ----Parameter List - 1. void , + 1. void , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: - + -------------------------------------------------------------------- Changes: - + \------------------------------------------------------------------*/ char *SS_pop( struct SS_object *ss ) { @@ -224,16 +224,16 @@ char *SS_pop( struct SS_object *ss ) Function Name : *SS_top Returns Type : char ----Parameter List - 1. void , + 1. void , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: - + -------------------------------------------------------------------- Changes: - + \------------------------------------------------------------------*/ char *SS_top( struct SS_object *ss ) { @@ -249,16 +249,16 @@ char *SS_top( struct SS_object *ss ) Function Name : SS_count Returns Type : int ----Parameter List - 1. void , + 1. void , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: - + -------------------------------------------------------------------- Changes: - + \------------------------------------------------------------------*/ int SS_count( struct SS_object *ss ) { @@ -269,17 +269,17 @@ int SS_count( struct SS_object *ss ) Function Name : *SS_cmp Returns Type : char ----Parameter List - 1. struct SS_object *ss, - 2. char *find_me , + 1. struct SS_object *ss, + 2. char *find_me , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: - + -------------------------------------------------------------------- Changes: - + \------------------------------------------------------------------*/ char *SS_cmp( struct SS_object *ss, char *find_me, size_t find_me_len ) { diff --git a/uuencode.c b/uuencode.c index 3f1d036..229c120 100644 --- a/uuencode.c +++ b/uuencode.c @@ -79,10 +79,10 @@ int uuencode_error; // this contains the error code for parents to check if th Function Name : UUENCODE_init Returns Type : int ----Parameter List - 1. void , + 1. void , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: @@ -106,10 +106,10 @@ int UUENCODE_init( void ) Function Name : UUENCODE_set_debug Returns Type : int ----Parameter List - 1. int level , + 1. int level , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: @@ -127,10 +127,10 @@ int UUENCODE_set_debug( int level ) Function Name : UUENCODE_set_verbosity Returns Type : int ----Parameter List - 1. int level , + 1. int level , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: @@ -148,10 +148,10 @@ int UUENCODE_set_verbosity( int level ) Function Name : UUENCODE_set_verbosity_contenttype Returns Type : int ----Parameter List - 1. int level , + 1. int level , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: @@ -169,10 +169,10 @@ int UUENCODE_set_verbosity_contenttype( int level ) Function Name : UUENCODE_set_decode Returns Type : int ----Parameter List - 1. int level , + 1. int level , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: @@ -190,10 +190,10 @@ int UUENCODE_set_decode( int level ) Function Name : UUENCODE_set_doubleCR_mode Returns Type : int ----Parameter List - 1. int level , + 1. int level , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: @@ -211,11 +211,11 @@ int UUENCODE_set_doubleCR_mode( int level ) Function Name : UUENCODE_set_filename_report_fn Returns Type : int ----Parameter List - 1. int (*ptr_to_fn)(char *, - 2. char *) , + 1. int (*ptr_to_fn)(char *, + 2. char *) , ------------------ - Exit Codes : - Side Effects : + Exit Codes : + Side Effects : -------------------------------------------------------------------- Comments: @@ -258,7 +258,7 @@ int UUENCODE_is_uuencode_header( char *line ) // colon after the BEGIN // // 2003-08-12:PLD:Added *(bp+6) test as recommended by Bernard Fischer to ensure there's more - // data after the begin + // data after the begin if ((bp)&&(strncasecmp(bp,"begin",5)==0)&&(*(bp+5)!=':')&&(isspace((int)*(bp+5)))&&(*(bp+6))) { @@ -346,11 +346,11 @@ int UUENCODE_is_diskfile_uuencoded( char *fname ) 3. char *input_filename, The fully pathed input filename, containing UU data 4. char *out_filename, Pointer to a buffer where we will write the filename of the UU data 5. int out_filename_size, out_filename buffers size - 6. int decode_whole_file, 0 == only first segment, >0 == all - 7. int keep , Keep the files we create, don't delete + 6. int decode_whole_file, 0 == only first segment, >0 == all + 7. int keep , Keep the files we create, don't delete ------------------ Exit Codes : Returns the number of attachments decoded in the data - Side Effects : + Side Effects : -------------------------------------------------------------------- Comments: @@ -418,7 +418,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *unpackdir, char *input_filename, ch if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Creation done. [FFGET-FILE=%p, FILE=%p]\n", FL, finf, inf); } else { - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: File handle already exists to read from, using",FL); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: File handle already exists to read from, using",FL); finf = f; } @@ -447,7 +447,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *unpackdir, char *input_filename, ch { if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: BUFFER: \n%s\n", FL, buf); - // Check for the presence of 'BEGIN', but make sure it's not followed by a + // Check for the presence of 'BEGIN', but make sure it's not followed by a // colon ( which indicates a VCARD instead of UUENCODE if ((strncasecmp(buf,"begin",5)==0)&&(buf[5] !=':')&&(isspace((int)buf[5]))) From 07bc355e74c65e86b46759a647c920e07fbca040 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Wed, 23 Apr 2025 08:08:21 +0300 Subject: [PATCH 08/80] Remove unused by gcc warnings --- mime.c | 17 ++++++----------- tnef/tnef.c | 6 +----- 2 files changed, 7 insertions(+), 16 deletions(-) diff --git a/mime.c b/mime.c index 721661e..38d0814 100644 --- a/mime.c +++ b/mime.c @@ -1329,9 +1329,8 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Boundary located - breaking out.\n",FL); break; } else { - size_t bc; if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: writing: %s\n",FL, buffer); - bc = write( fo, buffer, readcount); + write( fo, buffer, readcount); } } @@ -1458,10 +1457,9 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM { if (hinfo->content_transfer_encoding == _CTRANS_ENCODING_QP) { - size_t bc; if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_DNORMAL:DEBUG: Hit a boundary on the line",FL); decodesize = MDECODE_decode_qp_text(line); - bc = fwrite(line, 1, decodesize, of); + fwrite(line, 1, decodesize, of); } else { fprintf(of,"%s",line); @@ -1646,7 +1644,6 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH /* do an endless loop, as we're -breaking- out later */ while (1) { - int lastchar_was_linebreak=0; /* Initialise the decode buffer */ input[0] = input[1] = input[2] = input[3] = 0; // was '0' - Stepan Kasal patch /* snatch 4 characters from the input */ @@ -1663,7 +1660,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH //----------INLINE Version of FFGET_getchar() // do { - if ((c == '\n')||(c == '\r')) lastchar_was_linebreak = 1; else lastchar_was_linebreak = 0; + // lastchar_was_linebreak = ((c == '\n')||(c == '\r')); if (f->ungetcset) { f->ungetcset = 0; @@ -1690,7 +1687,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH // //-------END OF INLINE--------------------------------------------- if ((ignore_crcount == 1)&&(c == '-')) - //&&(lastchar_was_linebreak == 1)) + //&&(lastchar_was_linebreak)) { int hit = 0; char *p; @@ -1763,11 +1760,10 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH /* if we get an EOF char, then we know something went wrong */ if ( c == EOF ) { - size_t bc; if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_64:DEBUG: input stream broken for base64 decoding for file %s. %ld bytes of data in buffer to be written out\n",FL,hinfo->filename,wbcount); status = MIME_ERROR_B64_INPUT_STREAM_EOF; //fwrite(writebuffer, 1, wbcount, of); - bc = write( of, writebuffer, wbcount); + write( of, writebuffer, wbcount); close(of); if (writebuffer) free(writebuffer); return status; @@ -1834,8 +1830,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH // interrupt costs. if ( wbcount > _MIME_WRITE_BUFFER_LIMIT ) { - size_t bc; - bc = write ( of, writebuffer, wbcount ); + write ( of, writebuffer, wbcount ); // fwrite(writebuffer, 1, wbcount, of); wbpos = writebuffer; wbcount = 0; diff --git a/tnef/tnef.c b/tnef/tnef.c index 06e1ade..8202dd0 100644 --- a/tnef/tnef.c +++ b/tnef/tnef.c @@ -330,7 +330,6 @@ int save_attach_data(char *title, uint8 *tsp, uint32 size) { FILE *out; char filename[1024]; - size_t bc; snprintf(filename, sizeof(filename),"%s/%s", TNEF_glb.path, title ); @@ -341,7 +340,7 @@ int save_attach_data(char *title, uint8 *tsp, uint32 size) return -1; } - bc = fwrite(tsp, sizeof(uint8), size, out); + fwrite(tsp, sizeof(uint8), size, out); fclose(out); return 0; } @@ -488,7 +487,6 @@ int read_attribute(uint8 *tsp) int bytes = 0, header = 0; int rv = 0; uint32 attribute; - uint8 component = 0; uint32 size = 0; uint16 checksum = 0; static char attach_title[256] = { @@ -497,8 +495,6 @@ int read_attribute(uint8 *tsp) //static uint32 attach_loc = 0; // 2003-02-22-1231-PLD static uint8 *attach_loc = 0; - component = *tsp; - bytes += sizeof(uint8); // Read the attributes of this component From a4d4d4270437495b62f435df894558fffa37bf6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Wed, 23 Apr 2025 19:07:08 +0300 Subject: [PATCH 09/80] Refactor to `FILE` instead of `int` file descriptors, unify naming --- mime.c | 78 +++++++++++++++++++++++++--------------------------------- 1 file changed, 33 insertions(+), 45 deletions(-) diff --git a/mime.c b/mime.c index 38d0814..6f42dd0 100644 --- a/mime.c +++ b/mime.c @@ -1285,7 +1285,7 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME size_t readcount; int file_has_uuencode = 0; int decode_entire_file = 0; - int fo; + FILE* result_f = NULL; /* Decoding / reading a binary attachment is a real interesting situation, as we * still use the fgets() call, but we do so repeatedly until it returns a line with a @@ -1303,9 +1303,9 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME } snprintf(fullpath,sizeof(fullpath),"%s/%s",unpack_metadata->dir,hinfo->filename); - fo = open(fullpath, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR); + result_f = fopen(fullpath, "w"); - if (fo == -1) + if (result_f == -1) { LOGGER_log("%s:%d:MIME_decode_raw:ERROR: cannot open file %s for writing. (%s)\n\n",FL,fullpath,strerror(errno)); return -1; @@ -1330,13 +1330,13 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME break; } else { if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: writing: %s\n",FL, buffer); - write( fo, buffer, readcount); + fwrite( buffer, readcount, 1, result_f); } } if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Completed reading RAW data\n",FL); free(buffer); - close(fo); + fclose(result_f); if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Closed file and free'd buffer\n",FL); // If there was UUEncoded portions [potentially] in the email, the // try to extract them using the MIME_decode_uu() @@ -1398,11 +1398,10 @@ keep : if set, retain the file ------------------------------------------------------------------------*/ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int keep ) { - - FILE *of; // output file + FILE *result_f; // output file int linecount = 0; // The number of lines int file_has_uuencode = 0; // Flag to indicate this text has UUENCODE in it - char fullfilename[1024]=""; // Filename of the output file + char fullpath[1024]=""; // Filename of the output file char line[1024]; // The input lines from the file we're decoding char *get_result = &line[0]; int lastlinewasboundary = 0; @@ -1415,8 +1414,8 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM MIME_fprintf_decoded(unpack_metadata, hinfo); } - snprintf(fullfilename,sizeof(fullfilename),"%s/%s",unpack_metadata->dir,hinfo->filename); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Decoding TEXT [encoding=%d] to %s\n",FL, hinfo->content_transfer_encoding, fullfilename); + snprintf(fullpath,sizeof(fullpath),"%s/%s",unpack_metadata->dir,hinfo->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Decoding TEXT [encoding=%d] to %s\n",FL, hinfo->content_transfer_encoding, fullpath); if (!f) { @@ -1427,14 +1426,14 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM if (f) { /** If we were able to open the input file, try opening the output file and process the data **/ - of = fopen(fullfilename,"w"); - if (!of) + result_f = fopen(fullpath,"w"); + if (!result_f) { /** If we were unable to open the output file, report the error and return -1 **/ - LOGGER_log("%s:%d:MIME_decode_text:ERROR: cannot open %s for writing",FL,fullfilename); + LOGGER_log("%s:%d:MIME_decode_text:ERROR: cannot open %s for writing",FL,fullpath); return _EXITERR_MIMEREAD_CANNOT_WRITE_OUTPUT; } - while ((get_result = FFGET_fgets(line,1023,f))&&(of)) + while ((get_result = FFGET_fgets(line,1023,f))&&(result_f)) { int line_len = strlen(line); linecount++; @@ -1459,10 +1458,10 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM { if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_DNORMAL:DEBUG: Hit a boundary on the line",FL); decodesize = MDECODE_decode_qp_text(line); - fwrite(line, 1, decodesize, of); + fwrite(line, 1, decodesize, result_f); } else { - fprintf(of,"%s",line); + fprintf(result_f,"%s",line); } if ((!file_has_uuencode)&&( UUENCODE_is_uuencode_header( line ))) @@ -1474,11 +1473,11 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM // linecount++; if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_DNORMAL:DEBUG: End processing line.",FL); } // while - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Done writing output file '%s'...now attempting to close.",FL, fullfilename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Done writing output file '%s'...now attempting to close.",FL, fullpath); // if the file is still safely open - if (of) + if (result_f) { - fclose(of); + fclose(result_f); } // if file still safely open if (linecount == 0) @@ -1594,8 +1593,8 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH unsigned char *wbpos; int wbcount = 0; int loop; + FILE * result_f = NULL; - int of; /* output file pointer */ if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_64:DEBUG: attempting to decode '%s'", FL, hinfo->filename); glb.mime_count++; @@ -1607,10 +1606,9 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH /* generate the MIME_filename, and open it up... */ if (glb.unique_names) MIME_test_uniquename( unpack_metadata->dir, hinfo->filename, glb.rename_method ); snprintf(fullMIME_filename,_MIME_STRLEN_MAX,"%s/%s",unpack_metadata->dir,hinfo->filename); - //of = fopen(fullMIME_filename,"wb"); - of = open(fullMIME_filename, O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR); + result_f = fopen(fullMIME_filename,"wb"); /* if we were unable to open the output file, then we better log an error and drop out */ - if (of < 0) + if (result_f < 0) { LOGGER_log("%s:%d:MIME_decode_64:ERROR: Cannot open output file %s for BASE64 decoding. (%s)",FL,fullMIME_filename, strerror(errno)); // exit(_EXITERR_BASE64_OUTPUT_NOT_OPEN); @@ -1762,9 +1760,8 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH { if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_64:DEBUG: input stream broken for base64 decoding for file %s. %ld bytes of data in buffer to be written out\n",FL,hinfo->filename,wbcount); status = MIME_ERROR_B64_INPUT_STREAM_EOF; - //fwrite(writebuffer, 1, wbcount, of); - write( of, writebuffer, wbcount); - close(of); + fwrite(writebuffer, 1, wbcount, result_f); + fclose(result_f); if (writebuffer) free(writebuffer); return status; break; @@ -1830,8 +1827,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH // interrupt costs. if ( wbcount > _MIME_WRITE_BUFFER_LIMIT ) { - write ( of, writebuffer, wbcount ); - // fwrite(writebuffer, 1, wbcount, of); + fwrite(writebuffer, 1, wbcount, result_f); wbpos = writebuffer; wbcount = 0; } @@ -1854,12 +1850,10 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH // we'll end up with truncated files. if (wbcount > 0) { - size_t bc; - //fwrite(writebuffer, 1, wbcount, of); - bc = write( of, writebuffer, wbcount); + fwrite(writebuffer, 1, wbcount, result_f); } /* close the output file, we're done writing to it */ - close(of); + fclose(result_f); /* if we didn't really write anything, then trash the file */ if (bytecount == 0) { @@ -1978,7 +1972,7 @@ size_t MIME_read_raw( char *src_mpname, char *dest_mpname, size_t rw_buffer_size size_t fsize=-1; char *rw_buffer; int fin; - int fout; + FILE* result_f = NULL; rw_buffer = NULL; @@ -2000,8 +1994,8 @@ size_t MIME_read_raw( char *src_mpname, char *dest_mpname, size_t rw_buffer_size } /* open up our input file */ - fout = open(dest_mpname,O_WRONLY|O_CREAT, S_IRUSR|S_IWUSR); - if (fout == -1) { + result_f = fopen(dest_mpname, "w"); + if (result_f == -1) { LOGGER_log("%s:%d:MIME_read_raw:ERROR: Cannot open '%s' for writing. (%s)",FL, dest_mpname, strerror(errno)); return -1; } @@ -2011,7 +2005,7 @@ size_t MIME_read_raw( char *src_mpname, char *dest_mpname, size_t rw_buffer_size do { readcount = read( fin, rw_buffer, rw_buffer_size ); if (readcount > 0) { - writecount = write( fout, rw_buffer, readcount ); + writecount = fwrite(rw_buffer, readcount, 1, result_f ); if (writecount == -1) { LOGGER_log("%s:%d:MIME_read_raw:ERROR: While attempting to write data to '%s' (%s)", FL, dest_mpname, strerror(errno)); return -1; @@ -2031,7 +2025,7 @@ size_t MIME_read_raw( char *src_mpname, char *dest_mpname, size_t rw_buffer_size LOGGER_log("%s:%d:MIME_read_raw:ERROR: read() '%s'",FL, strerror(errno)); return -1; } - close(fout); + fclose(result_f); if ( rw_buffer != NULL ) free( rw_buffer ); return (size_t)(fsize); } @@ -2103,9 +2097,6 @@ int MIME_read( char *mpname ) return (int)(fsize /1024); } - - - /*------------------------------------------------------------------------ Procedure: MIME_init ID:1 Purpose: Initialise various required parameters to ensure a clean starting of @@ -2116,15 +2107,13 @@ MIME decoding. ------------------------------------------------------------------------*/ int MIME_init( void ) { - BS_init(); // Boundary-stack initialisations MIMEH_init(); // Initialise MIME header routines. UUENCODE_init(); // uuen:coding decoding initialisations FNFILTER_init(); // Filename filtering - MDECODE_init(); // ISO filename decoding initialisation + MDECODE_init(); // ISO filename decoding initialisation TNEF_init(); // TNEF decoder - glb.header_defect_count = 0; glb.filecount = 0; glb.attachment_count = 0; @@ -3065,8 +3054,7 @@ int MIME_unpack_mailbox( RIPMIME_output *unpack_metadata, char *mpname, int curr fi = stdin; input_is_stdin=1; } else { - // fi = fopen(mpname,"r"); - if (strcmp(mpname,"-")==0) fi = stdin; else fi = fopen(mpname,"r"); // 20040208-1715:PLD + fi = (strcmp(mpname,"-")==0) ? stdin : fopen(mpname,"r"); if (!fi) { LOGGER_log("%s:%d:MIME_unpack_mailbox:ERROR: Cannot open '%s' for reading (%s)",FL, mpname,strerror(errno)); From 0e2c8d2adead36b4f6ab8aafed0887dc74110d6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Wed, 23 Apr 2025 19:10:21 +0300 Subject: [PATCH 10/80] Fix `result_f` unsuccessfully result condition --- mime.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mime.c b/mime.c index 6f42dd0..0c5235b 100644 --- a/mime.c +++ b/mime.c @@ -1305,7 +1305,7 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME snprintf(fullpath,sizeof(fullpath),"%s/%s",unpack_metadata->dir,hinfo->filename); result_f = fopen(fullpath, "w"); - if (result_f == -1) + if (result_f == NULL) { LOGGER_log("%s:%d:MIME_decode_raw:ERROR: cannot open file %s for writing. (%s)\n\n",FL,fullpath,strerror(errno)); return -1; @@ -1995,7 +1995,7 @@ size_t MIME_read_raw( char *src_mpname, char *dest_mpname, size_t rw_buffer_size /* open up our input file */ result_f = fopen(dest_mpname, "w"); - if (result_f == -1) { + if (result_f == NULL) { LOGGER_log("%s:%d:MIME_read_raw:ERROR: Cannot open '%s' for writing. (%s)",FL, dest_mpname, strerror(errno)); return -1; } From 0184c0ec61ecb487953e8cc808ac209922bc6fb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Wed, 23 Apr 2025 19:32:03 +0300 Subject: [PATCH 11/80] Unify naming, exclude file descriptors, use C stdlib only --- mime.c | 32 +++++++++++--------------------- 1 file changed, 11 insertions(+), 21 deletions(-) diff --git a/mime.c b/mime.c index 0c5235b..ccf8c73 100644 --- a/mime.c +++ b/mime.c @@ -35,7 +35,6 @@ #include #include #include -#include #include #include #include @@ -126,11 +125,7 @@ static unsigned char b64[256]={ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128 \ }; - - - struct MIME_globals { - int header_defect_count; int filecount; char blankfileprefix[_MIME_STRLEN_MAX]; @@ -185,10 +180,7 @@ struct MIME_globals { }; static struct MIME_globals glb; - -//char OK[]="OKAY"; static char scratch[1024]; - /* File pointer for the headers output */ FILE *headers; @@ -245,7 +237,6 @@ the different types of content-types. int MIME_set_name_by_type( int level ) { glb.name_by_type = level; - return glb.name_by_type; } @@ -1585,7 +1576,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH long int bytecount=0; /* The total file decoded size */ char output[3]; /* The 4->3 byte output array */ char input[4]; /* The 4->3 byte input array */ - char fullMIME_filename[_MIME_STRLEN_MAX]=""; /* Full Filename of output file */ + char fullpath[_MIME_STRLEN_MAX]=""; /* Full Filename of output file */ // Write Buffer routine @@ -1605,12 +1596,12 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH /* generate the MIME_filename, and open it up... */ if (glb.unique_names) MIME_test_uniquename( unpack_metadata->dir, hinfo->filename, glb.rename_method ); - snprintf(fullMIME_filename,_MIME_STRLEN_MAX,"%s/%s",unpack_metadata->dir,hinfo->filename); - result_f = fopen(fullMIME_filename,"wb"); + snprintf(fullpath,_MIME_STRLEN_MAX,"%s/%s",unpack_metadata->dir,hinfo->filename); + result_f = fopen(fullpath,"wb"); /* if we were unable to open the output file, then we better log an error and drop out */ if (result_f < 0) { - LOGGER_log("%s:%d:MIME_decode_64:ERROR: Cannot open output file %s for BASE64 decoding. (%s)",FL,fullMIME_filename, strerror(errno)); + LOGGER_log("%s:%d:MIME_decode_64:ERROR: Cannot open output file %s for BASE64 decoding. (%s)",FL,fullpath, strerror(errno)); // exit(_EXITERR_BASE64_OUTPUT_NOT_OPEN); return -1; } @@ -1857,13 +1848,12 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH /* if we didn't really write anything, then trash the file */ if (bytecount == 0) { - // unlink(fullMIME_filename); + // unlink(fullpath); status = MIME_BASE64_STATUS_ZERO_FILE; } if (boundary_crash) { // Absorb to end of line - // status = MIME_BASE64_STATUS_HIT_BOUNDARY; // was _BOUNDARY_CRASH } if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_64:DEBUG: File size = %ld bytes, Exit Status = %d, Boundary Crash = %d\n",FL, bytecount, status, boundary_crash); @@ -1971,16 +1961,16 @@ size_t MIME_read_raw( char *src_mpname, char *dest_mpname, size_t rw_buffer_size size_t readcount, writecount; size_t fsize=-1; char *rw_buffer; - int fin; + FILE* input_f = NULL; FILE* result_f = NULL; rw_buffer = NULL; if (*src_mpname == '\0') { - fin = STDIN_FILENO; + input_f = STDIN_FILENO; } else { - fin = open(src_mpname, O_RDONLY); - if (fin == -1) { + input_f = fopen(src_mpname, "r"); + if (input_f == NULL) { LOGGER_log("%s:%d:MIME_read_raw:ERROR: Cannot open '%s' for reading (%s)", FL, src_mpname, strerror(errno)); return -1; } @@ -2003,7 +1993,7 @@ size_t MIME_read_raw( char *src_mpname, char *dest_mpname, size_t rw_buffer_size fsize=0; /* while there is more data, consume it */ do { - readcount = read( fin, rw_buffer, rw_buffer_size ); + readcount = fread( rw_buffer, rw_buffer_size, 1, input_f ); if (readcount > 0) { writecount = fwrite(rw_buffer, readcount, 1, result_f ); if (writecount == -1) { @@ -2019,7 +2009,7 @@ size_t MIME_read_raw( char *src_mpname, char *dest_mpname, size_t rw_buffer_size } while (readcount > 0); if ((*src_mpname != '\0')&&(readcount >= 0)) { - close(fin); + fclose(input_f); } if (readcount == -1) { LOGGER_log("%s:%d:MIME_read_raw:ERROR: read() '%s'",FL, strerror(errno)); From 142a9c4019464a23649337d605e2e6e321d46159 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Wed, 23 Apr 2025 19:39:16 +0300 Subject: [PATCH 12/80] Fix UUEcode code style --- uuencode.c | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/uuencode.c b/uuencode.c index 229c120..8720e08 100644 --- a/uuencode.c +++ b/uuencode.c @@ -22,8 +22,6 @@ The biggest issue is that the interfaces to the decoding functions are too speci #include "uuencode.h" - - #ifndef FL #define FL __FILE__,__LINE__ #endif @@ -41,9 +39,8 @@ The biggest issue is that the interfaces to the decoding functions are too speci #define UUENCODE_WRITE_BUFFER_SIZE 4096 #define UUENCODE_WRITE_BUFFER_LIMIT 4000 - static unsigned char uudec[256]={ - 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,\ + 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,\ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,\ 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, 26, 27, 28, 29, 30, 31,\ @@ -336,7 +333,6 @@ int UUENCODE_is_diskfile_uuencoded( char *fname ) return result; } - /*-----------------------------------------------------------------\ Function Name : UUENCODE_decode_uu Returns Type : int @@ -394,7 +390,6 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *unpackdir, char *input_filename, ch // If no FFGET_FILE param is passed to us directly, then we must create out own. - if (!f) { if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: NULL FFGET source stream given to us, create our own.\n",FL); @@ -434,10 +429,8 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *unpackdir, char *input_filename, ch wbcount = 0; } - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Beginning.(%s)\n",FL,fullpath); - while (!FFGET_feof(finf)) { filename_found = 0; From 285d5fd152c72568dd40214afe4e49269362f3bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Wed, 23 Apr 2025 20:22:22 +0300 Subject: [PATCH 13/80] Exclude unnecessary header --- ripmime.c | 1 - 1 file changed, 1 deletion(-) diff --git a/ripmime.c b/ripmime.c index 0994b98..500a2b0 100644 --- a/ripmime.c +++ b/ripmime.c @@ -13,7 +13,6 @@ #include #include #include -#include #include #include #include From f0fd3a5325afbc8dae76fa2615b4c3eb120e392f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Wed, 23 Apr 2025 20:23:22 +0300 Subject: [PATCH 14/80] Exclude unnecessary header --- ripmime-api.c | 1 - 1 file changed, 1 deletion(-) diff --git a/ripmime-api.c b/ripmime-api.c index d036616..7ed977d 100644 --- a/ripmime-api.c +++ b/ripmime-api.c @@ -13,7 +13,6 @@ #include #include #include -#include #include #include #include From 14959736c1ee63b5de8721189f7c4d6327dbf6f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Thu, 24 Apr 2025 09:11:21 +0300 Subject: [PATCH 15/80] Unify MIME element creating to `MIME_element_add` and other simple code cleanup --- mime.c | 136 ++++++++++++++++++++++---------------------------- mime.h | 4 +- ripmime-api.c | 6 +-- 3 files changed, 63 insertions(+), 83 deletions(-) diff --git a/mime.c b/mime.c index ccf8c73..0a0c120 100644 --- a/mime.c +++ b/mime.c @@ -107,7 +107,7 @@ int MIME_handle_rfc822( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M /* our base 64 decoder table */ static unsigned char b64[256]={ - 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\ + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 62, 128, 128, 128, 63,\ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 128, 128, 128, 0, 128, 128,\ @@ -125,6 +125,14 @@ static unsigned char b64[256]={ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128 \ }; +typedef struct { + struct MIMEH_header_info *hinfo; + int id; + char* fullpath; + FILE* f; + int result; +} MIME_element; + struct MIME_globals { int header_defect_count; int filecount; @@ -177,12 +185,11 @@ struct MIME_globals { char subject[_MIME_STRLEN_MAX]; int mime_count; + MIME_element *mime_arr; }; static struct MIME_globals glb; static char scratch[1024]; -/* File pointer for the headers output */ -FILE *headers; /*-----------------------------------------------------------------\ Function Name : MIME_version @@ -1260,6 +1267,32 @@ int MIME_decode_OLE( RIPMIME_output *unpack_metadata, struct MIMEH_header_info * } #endif +MIME_element* MIME_element_add (RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo) +{ + MIME_element * cur = malloc(sizeof(MIME_element)); + int fullpath_len = strlen(unpack_metadata->dir) + strlen(hinfo->filename) + 3 * sizeof(char); + + if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_element_add:start\n",FL); + + cur->hinfo = hinfo; + cur->id = glb.mime_count++; + cur->fullpath = (char*)malloc(fullpath_len); + snprintf(cur->fullpath,fullpath_len,"%s/%s",unpack_metadata->dir,hinfo->filename); + + cur->f = fopen(cur->fullpath,"wb"); + if (cur->f == NULL) { + /** If we were unable to open the output file, report the error and return -1 **/ + LOGGER_log("%s:%d:MIME_decode_text:ERROR: cannot open %s for writing",FL,cur->fullpath); + } + + if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_element_add:DEBUG: Decoding [encoding=%d] to %s\n",FL, hinfo->content_transfer_encoding, cur->fullpath); + + if (unpack_metadata->unpack_mode == RIPMIME_UNPACK_MODE_LIST_FILES && cur->f != NULL) { + fprintf (stdout, "%d|%d|%d|%d|%s|%s\n", glb.mime_count, glb.attachment_count, glb.filecount, hinfo->current_recursion_level, hinfo->content_type_string, hinfo->filename); + } + return cur; +} + /*------------------------------------------------------------------------ Procedure: MIME_decode_raw ID:1 Purpose: Decodes a binary type attachment, ie, no encoding, just raw data. @@ -1270,13 +1303,12 @@ Purpose: Decodes a binary type attachment, ie, no encoding, just raw data. int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int keep ) { int result = 0; - char fullpath[1024]; int bufsize=1024; char *buffer = malloc((bufsize +1)*sizeof(char)); size_t readcount; int file_has_uuencode = 0; int decode_entire_file = 0; - FILE* result_f = NULL; + MIME_element* cur_mime = NULL; /* Decoding / reading a binary attachment is a real interesting situation, as we * still use the fgets() call, but we do so repeatedly until it returns a line with a @@ -1287,20 +1319,7 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Start\n",FL); - glb.mime_count++; - if (unpack_metadata->unpack_mode == RIPMIME_UNPACK_MODE_LIST_FILES) - { - MIME_fprintf_decoded(unpack_metadata, hinfo); - } - - snprintf(fullpath,sizeof(fullpath),"%s/%s",unpack_metadata->dir,hinfo->filename); - result_f = fopen(fullpath, "w"); - - if (result_f == NULL) - { - LOGGER_log("%s:%d:MIME_decode_raw:ERROR: cannot open file %s for writing. (%s)\n\n",FL,fullpath,strerror(errno)); - return -1; - } + cur_mime = MIME_element_add (unpack_metadata, hinfo); while ((readcount=FFGET_raw(f, (unsigned char *) buffer,bufsize)) > 0) { @@ -1321,13 +1340,13 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME break; } else { if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: writing: %s\n",FL, buffer); - fwrite( buffer, readcount, 1, result_f); + fwrite( buffer, readcount, 1, cur_mime->f); } } if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Completed reading RAW data\n",FL); free(buffer); - fclose(result_f); + fclose(cur_mime->f); if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Closed file and free'd buffer\n",FL); // If there was UUEncoded portions [potentially] in the email, the // try to extract them using the MIME_decode_uu() @@ -1389,24 +1408,18 @@ keep : if set, retain the file ------------------------------------------------------------------------*/ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int keep ) { - FILE *result_f; // output file int linecount = 0; // The number of lines int file_has_uuencode = 0; // Flag to indicate this text has UUENCODE in it - char fullpath[1024]=""; // Filename of the output file char line[1024]; // The input lines from the file we're decoding char *get_result = &line[0]; int lastlinewasboundary = 0; int result = 0; int decodesize=0; + MIME_element* cur_mime = NULL; - glb.mime_count++; - if (unpack_metadata->unpack_mode == RIPMIME_UNPACK_MODE_LIST_FILES) - { - MIME_fprintf_decoded(unpack_metadata, hinfo); - } + if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Decoding TEXT [encoding=%d] to %s\n",FL, hinfo->content_transfer_encoding, hinfo->filename); - snprintf(fullpath,sizeof(fullpath),"%s/%s",unpack_metadata->dir,hinfo->filename); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Decoding TEXT [encoding=%d] to %s\n",FL, hinfo->content_transfer_encoding, fullpath); + cur_mime = MIME_element_add (unpack_metadata, hinfo); if (!f) { @@ -1416,15 +1429,7 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM } if (f) { - /** If we were able to open the input file, try opening the output file and process the data **/ - result_f = fopen(fullpath,"w"); - if (!result_f) - { - /** If we were unable to open the output file, report the error and return -1 **/ - LOGGER_log("%s:%d:MIME_decode_text:ERROR: cannot open %s for writing",FL,fullpath); - return _EXITERR_MIMEREAD_CANNOT_WRITE_OUTPUT; - } - while ((get_result = FFGET_fgets(line,1023,f))&&(result_f)) + while ((get_result = FFGET_fgets(line,1023,f))&&(cur_mime->f)) { int line_len = strlen(line); linecount++; @@ -1449,10 +1454,10 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM { if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_DNORMAL:DEBUG: Hit a boundary on the line",FL); decodesize = MDECODE_decode_qp_text(line); - fwrite(line, 1, decodesize, result_f); + fwrite(line, 1, decodesize, cur_mime->f); } else { - fprintf(result_f,"%s",line); + fprintf(cur_mime->f,"%s",line); } if ((!file_has_uuencode)&&( UUENCODE_is_uuencode_header( line ))) @@ -1464,11 +1469,11 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM // linecount++; if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_DNORMAL:DEBUG: End processing line.",FL); } // while - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Done writing output file '%s'...now attempting to close.",FL, fullpath); + if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Done writing output file '%s'...now attempting to close.",FL, cur_mime->fullpath); // if the file is still safely open - if (result_f) + if (cur_mime->f) { - fclose(result_f); + fclose(cur_mime->f); } // if file still safely open if (linecount == 0) @@ -1576,7 +1581,6 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH long int bytecount=0; /* The total file decoded size */ char output[3]; /* The 4->3 byte output array */ char input[4]; /* The 4->3 byte input array */ - char fullpath[_MIME_STRLEN_MAX]=""; /* Full Filename of output file */ // Write Buffer routine @@ -1584,25 +1588,13 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH unsigned char *wbpos; int wbcount = 0; int loop; - FILE * result_f = NULL; + MIME_element* cur_mime = NULL; if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_64:DEBUG: attempting to decode '%s'", FL, hinfo->filename); - glb.mime_count++; - if (unpack_metadata->unpack_mode == RIPMIME_UNPACK_MODE_LIST_FILES) + cur_mime = MIME_element_add (unpack_metadata, hinfo); + if (cur_mime->f == NULL) { - MIME_fprintf_decoded(unpack_metadata, hinfo); - } - - /* generate the MIME_filename, and open it up... */ - if (glb.unique_names) MIME_test_uniquename( unpack_metadata->dir, hinfo->filename, glb.rename_method ); - snprintf(fullpath,_MIME_STRLEN_MAX,"%s/%s",unpack_metadata->dir,hinfo->filename); - result_f = fopen(fullpath,"wb"); - /* if we were unable to open the output file, then we better log an error and drop out */ - if (result_f < 0) - { - LOGGER_log("%s:%d:MIME_decode_64:ERROR: Cannot open output file %s for BASE64 decoding. (%s)",FL,fullpath, strerror(errno)); - // exit(_EXITERR_BASE64_OUTPUT_NOT_OPEN); return -1; } @@ -1751,8 +1743,8 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH { if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_64:DEBUG: input stream broken for base64 decoding for file %s. %ld bytes of data in buffer to be written out\n",FL,hinfo->filename,wbcount); status = MIME_ERROR_B64_INPUT_STREAM_EOF; - fwrite(writebuffer, 1, wbcount, result_f); - fclose(result_f); + fwrite(writebuffer, 1, wbcount, cur_mime->f); + fclose(cur_mime->f); if (writebuffer) free(writebuffer); return status; break; @@ -1818,7 +1810,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH // interrupt costs. if ( wbcount > _MIME_WRITE_BUFFER_LIMIT ) { - fwrite(writebuffer, 1, wbcount, result_f); + fwrite(writebuffer, 1, wbcount, cur_mime->f); wbpos = writebuffer; wbcount = 0; } @@ -1841,10 +1833,10 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH // we'll end up with truncated files. if (wbcount > 0) { - fwrite(writebuffer, 1, wbcount, result_f); + fwrite(writebuffer, 1, wbcount, cur_mime->f); } /* close the output file, we're done writing to it */ - fclose(result_f); + fclose(cur_mime->f); /* if we didn't really write anything, then trash the file */ if (bytecount == 0) { @@ -2095,7 +2087,7 @@ MIME decoding. Output: Errors: ------------------------------------------------------------------------*/ -int MIME_init( void ) +void MIME_init( void ) { BS_init(); // Boundary-stack initialisations MIMEH_init(); // Initialise MIME header routines. @@ -2143,12 +2135,8 @@ int MIME_init( void ) glb.subject[0]='\0'; glb.mime_count = 0; - - return 0; } - - /*-----------------------------------------------------------------\ Function Name : MIME_generate_multiple_hardlink_filenames Returns Type : int @@ -3355,12 +3343,8 @@ int MIME_unpack( RIPMIME_output *unpack_metadata, char *mpname, int current_recu * MIME_close * * Closes the files used in MIME_unpack, such as headers etc */ -int MIME_close( void ) +void MIME_close( void ) { - if (headers) - { - fclose(headers); - } - return 0; + if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_close: start.",FL); } /*----------END OF MIME.c------------*/ diff --git a/mime.h b/mime.h index 1cc70ed..2d8bb8e 100644 --- a/mime.h +++ b/mime.h @@ -126,8 +126,8 @@ int MIME_is_file_uuenc( char *fname ); char *MIME_get_blankfileprefix( void ); char *MIME_get_headersname( void ); char *MIME_get_subject( void ); -int MIME_init( void ); -int MIME_close( void ); +void MIME_init( void ); +void MIME_close( void ); int MIME_set_tmpdir( char *tmpdir ); //int MIME_postdecode_cleanup( RIPMIME_output *unpack_metadata, struct SS_object *ss ); diff --git a/ripmime-api.c b/ripmime-api.c index 7ed977d..3caf7e7 100644 --- a/ripmime-api.c +++ b/ripmime-api.c @@ -43,7 +43,7 @@ char version[] = "v1.4.0.1 - 30/08/2004 (C) PLDaniels http://www.pldaniels.com/r Changes: \------------------------------------------------------------------*/ -int RIPMIME_init (struct RIPMIME_object *rm) +void RIPMIME_init (struct RIPMIME_object *rm) { rm->outputdir = defaultdir; rm->mailpack = NULL; @@ -54,9 +54,6 @@ int RIPMIME_init (struct RIPMIME_object *rm) MIME_set_paranoid(0); MIME_set_renamemethod(_MIME_RENAME_METHOD_INFIX); MIME_set_verbosity(0); - - - return 0; } @@ -137,7 +134,6 @@ int RIPMIME_decode( struct RIPMIME_object *rm, char *mailpack, char *outputdir ) MIME_close (); return 0; - } /*-END-----------------------------------------------------------*/ From e169c11eef4fca3bb3007921704e9cacca85ea9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Thu, 24 Apr 2025 09:14:35 +0300 Subject: [PATCH 16/80] Fix cannot open style --- mime.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mime.c b/mime.c index 0a0c120..eb9d363 100644 --- a/mime.c +++ b/mime.c @@ -1281,8 +1281,8 @@ MIME_element* MIME_element_add (RIPMIME_output *unpack_metadata, struct MIMEH_he cur->f = fopen(cur->fullpath,"wb"); if (cur->f == NULL) { - /** If we were unable to open the output file, report the error and return -1 **/ - LOGGER_log("%s:%d:MIME_decode_text:ERROR: cannot open %s for writing",FL,cur->fullpath); + LOGGER_log("%s:%d:MIME_element_add:ERROR: cannot open %s for writing",FL,cur->fullpath); + return cur; } if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_element_add:DEBUG: Decoding [encoding=%d] to %s\n",FL, hinfo->content_transfer_encoding, cur->fullpath); From 6bb4a33ce45e8d099640e64ae650172a88a8e508 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Thu, 24 Apr 2025 09:24:20 +0300 Subject: [PATCH 17/80] Unify to `MIME_element_remove` --- mime.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/mime.c b/mime.c index eb9d363..6c91c94 100644 --- a/mime.c +++ b/mime.c @@ -1293,6 +1293,17 @@ MIME_element* MIME_element_add (RIPMIME_output *unpack_metadata, struct MIMEH_he return cur; } +void MIME_element_remove (MIME_element* cur) +{ + if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_element_remove:start\n",FL); + + if (cur->f != NULL) { + fclose(cur->f); + } + free(cur->fullpath); + free(cur); +} + /*------------------------------------------------------------------------ Procedure: MIME_decode_raw ID:1 Purpose: Decodes a binary type attachment, ie, no encoding, just raw data. @@ -1346,7 +1357,7 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Completed reading RAW data\n",FL); free(buffer); - fclose(cur_mime->f); + MIME_element_remove(cur_mime); if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Closed file and free'd buffer\n",FL); // If there was UUEncoded portions [potentially] in the email, the // try to extract them using the MIME_decode_uu() @@ -1470,11 +1481,8 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_DNORMAL:DEBUG: End processing line.",FL); } // while if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Done writing output file '%s'...now attempting to close.",FL, cur_mime->fullpath); - // if the file is still safely open - if (cur_mime->f) - { - fclose(cur_mime->f); - } // if file still safely open + + MIME_element_remove(cur_mime); if (linecount == 0) { @@ -1744,7 +1752,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_64:DEBUG: input stream broken for base64 decoding for file %s. %ld bytes of data in buffer to be written out\n",FL,hinfo->filename,wbcount); status = MIME_ERROR_B64_INPUT_STREAM_EOF; fwrite(writebuffer, 1, wbcount, cur_mime->f); - fclose(cur_mime->f); + MIME_element_remove(cur_mime); if (writebuffer) free(writebuffer); return status; break; @@ -1836,7 +1844,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH fwrite(writebuffer, 1, wbcount, cur_mime->f); } /* close the output file, we're done writing to it */ - fclose(cur_mime->f); + MIME_element_remove(cur_mime); /* if we didn't really write anything, then trash the file */ if (bytecount == 0) { From 324fd336793002c54beafedb832b4ef73d8e3d60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Thu, 24 Apr 2025 09:46:11 +0300 Subject: [PATCH 18/80] Add a dynamic array for MIME elements --- mime.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 120 insertions(+), 2 deletions(-) diff --git a/mime.c b/mime.c index 6c91c94..28a442c 100644 --- a/mime.c +++ b/mime.c @@ -133,6 +133,12 @@ typedef struct { int result; } MIME_element; +typedef struct { + size_t size; + size_t capacity; + MIME_element** array; +} dynamic_array; + struct MIME_globals { int header_defect_count; int filecount; @@ -185,12 +191,27 @@ struct MIME_globals { char subject[_MIME_STRLEN_MAX]; int mime_count; - MIME_element *mime_arr; + dynamic_array* mime_arr; }; static struct MIME_globals glb; static char scratch[1024]; +/* Dynamic array support*/ +#define INITIAL_SIZE 8 + +// function prototypes +// array container functions +void arrayInit(dynamic_array** arr_ptr); +void freeArray(dynamic_array* container); + +// Basic Operation functions +void insertItem(dynamic_array* container, MIME_element* item); +void updateItem(dynamic_array* container, int i, MIME_element* item); +int getItem(dynamic_array* container, int i); +void deleteItem(dynamic_array* container, int i); +void printArray(dynamic_array* container); + /*-----------------------------------------------------------------\ Function Name : MIME_version Returns Type : int @@ -1269,11 +1290,12 @@ int MIME_decode_OLE( RIPMIME_output *unpack_metadata, struct MIMEH_header_info * MIME_element* MIME_element_add (RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo) { - MIME_element * cur = malloc(sizeof(MIME_element)); + MIME_element *cur = malloc(sizeof(MIME_element)); int fullpath_len = strlen(unpack_metadata->dir) + strlen(hinfo->filename) + 3 * sizeof(char); if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_element_add:start\n",FL); + insertItem(glb.mime_arr, cur); cur->hinfo = hinfo; cur->id = glb.mime_count++; cur->fullpath = (char*)malloc(fullpath_len); @@ -2143,6 +2165,7 @@ void MIME_init( void ) glb.subject[0]='\0'; glb.mime_count = 0; + arrayInit(&(glb.mime_arr)); } /*-----------------------------------------------------------------\ @@ -3354,5 +3377,100 @@ int MIME_unpack( RIPMIME_output *unpack_metadata, char *mpname, int current_recu void MIME_close( void ) { if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_close: start.",FL); + printArray(glb.mime_arr); + + freeArray(glb.mime_arr); +} + +//------Dynamic array function definitions------ +// Array initialization +void arrayInit(dynamic_array** arr_ptr) +{ + dynamic_array *container; + container = (dynamic_array*)malloc(sizeof(dynamic_array)); + if(!container) { + printf("Memory Allocation Failed\n"); + exit(0); + } + + container->size = 0; + container->capacity = INITIAL_SIZE; + container->array = (MIME_element **)malloc(INITIAL_SIZE * sizeof(MIME_element*)); + if (!container->array){ + printf("Memory Allocation Failed\n"); + exit(0); + } + + *arr_ptr = container; +} + +// Insertion Operation +void insertItem(dynamic_array* container, MIME_element* item) +{ + if (container->size == container->capacity) { + MIME_element **temp = container->array; + container->capacity <<= 1; + container->array = realloc(container->array, container->capacity * sizeof(MIME_element*)); + if(!container->array) { + printf("Out of Memory\n"); + container->array = temp; + return; + } + } + container->array[container->size++] = item; +} + +// Retrieve Item at Particular Index +int getItem(dynamic_array* container, int index) +{ + if(index >= container->size) { + printf("Index Out of Bounds\n"); + return -1; + } + return container->array[index]; +} + +// Update Operation +void updateItem(dynamic_array* container, int index, MIME_element* item) +{ + if (index >= container->size) { + printf("Index Out of Bounds\n"); + return; + } + container->array[index] = item; +} + +// Delete Item from Particular Index +void deleteItem(dynamic_array* container, int index) +{ + if(index >= container->size) { + printf("Index Out of Bounds\n"); + return; + } + + for (int i = index; i < container->size; i++) { + container->array[i] = container->array[i + 1]; + } + container->size--; +} + +// Array Traversal +void printArray(dynamic_array* container) +{ + printf("Array elements: "); + for (int i = 0; i < container->size; i++) { + printf("%p ", container->array[i]); + } + printf("\nSize: "); + printf("%lu", container->size); + printf("\nCapacity: "); + printf("%lu\n", container->capacity); +} + +// Freeing the memory allocated to the array +void freeArray(dynamic_array* container) +{ + free(container->array); + free(container); } /*----------END OF MIME.c------------*/ From 6c4b961912fd1841c389e2e226084a79b4c6aae6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Thu, 24 Apr 2025 10:30:34 +0300 Subject: [PATCH 19/80] Refactor TNEF path, add `MIME_element_add_with_path` --- mime.c | 33 ++++++++++++++------------ tnef/tnef.c | 61 +++++++++++++------------------------------------ tnef/tnef_api.h | 5 ++-- 3 files changed, 36 insertions(+), 63 deletions(-) diff --git a/mime.c b/mime.c index 28a442c..252fef9 100644 --- a/mime.c +++ b/mime.c @@ -1221,19 +1221,14 @@ Purpose: Decodes TNEF encoded attachments ------------------------------------------------------------------------*/ int MIME_decode_TNEF( RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int keep ) { - int result=0; - char fullpath[1024]; - - snprintf(fullpath,sizeof(fullpath),"%s/%s",unpack_metadata->dir,hinfo->filename); - TNEF_set_path(unpack_metadata->dir); - result = TNEF_main( fullpath ); + int result = TNEF_main( hinfo->filename, unpack_metadata->dir ); if (result >= 0) { // result = remove( fullpath ); if (result == -1) { - if (MIME_VERBOSE) LOGGER_log("%s:%d:MIME_decode_TNEF: Removing %s failed (%s)",FL,fullpath,strerror(errno)); + if (MIME_VERBOSE) LOGGER_log("%s:%d:MIME_decode_TNEF: Removing %s/%s failed (%s)",FL,hinfo->filename, unpack_metadata->dir,strerror(errno)); } } return result; @@ -1288,18 +1283,15 @@ int MIME_decode_OLE( RIPMIME_output *unpack_metadata, struct MIMEH_header_info * } #endif -MIME_element* MIME_element_add (RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo) +MIME_element* MIME_element_add_with_path (char* fullpath, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo) { MIME_element *cur = malloc(sizeof(MIME_element)); - int fullpath_len = strlen(unpack_metadata->dir) + strlen(hinfo->filename) + 3 * sizeof(char); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_element_add:start\n",FL); insertItem(glb.mime_arr, cur); cur->hinfo = hinfo; cur->id = glb.mime_count++; - cur->fullpath = (char*)malloc(fullpath_len); - snprintf(cur->fullpath,fullpath_len,"%s/%s",unpack_metadata->dir,hinfo->filename); + cur->fullpath = fullpath; cur->f = fopen(cur->fullpath,"wb"); if (cur->f == NULL) { @@ -1309,12 +1301,21 @@ MIME_element* MIME_element_add (RIPMIME_output *unpack_metadata, struct MIMEH_he if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_element_add:DEBUG: Decoding [encoding=%d] to %s\n",FL, hinfo->content_transfer_encoding, cur->fullpath); - if (unpack_metadata->unpack_mode == RIPMIME_UNPACK_MODE_LIST_FILES && cur->f != NULL) { + if (unpack_metadata != NULL && unpack_metadata->unpack_mode == RIPMIME_UNPACK_MODE_LIST_FILES && cur->f != NULL) { fprintf (stdout, "%d|%d|%d|%d|%s|%s\n", glb.mime_count, glb.attachment_count, glb.filecount, hinfo->current_recursion_level, hinfo->content_type_string, hinfo->filename); } return cur; } +MIME_element* MIME_element_add (RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo) +{ + int fullpath_len = strlen(unpack_metadata->dir) + strlen(hinfo->filename) + 3 * sizeof(char); + char *fullpath = (char*)malloc(fullpath_len); + + snprintf(fullpath,fullpath_len,"%s/%s",unpack_metadata->dir,hinfo->filename); + return MIME_element_add_with_path(fullpath, unpack_metadata, hinfo); +} + void MIME_element_remove (MIME_element* cur) { if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_element_remove:start\n",FL); @@ -3376,8 +3377,10 @@ int MIME_unpack( RIPMIME_output *unpack_metadata, char *mpname, int current_recu * Closes the files used in MIME_unpack, such as headers etc */ void MIME_close( void ) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_close: start.",FL); - printArray(glb.mime_arr); + if (MIME_DNORMAL) { + LOGGER_log("%s:%d:MIME_close: start.",FL); + printArray(glb.mime_arr); + } freeArray(glb.mime_arr); } diff --git a/tnef/tnef.c b/tnef/tnef.c index 8202dd0..6eef4c3 100644 --- a/tnef/tnef.c +++ b/tnef/tnef.c @@ -29,6 +29,9 @@ * 1.3 Version (7/22/97) * Ok, take out the DTR over the stream, now uses read_16. * + * 1.5 Version (4/24/24) + * Rewritten from file utility to part of ripMIME + * * NOTE: THIS SOFTWARE IS FOR YOUR PERSONAL GRATIFICATION ONLY. I DON'T * IMPLY IN ANY LEGAL SENSE THAT THIS SOFTWARE DOES ANYTHING OR THAT IT WILL * BE USEFULL IN ANY WAY. But, you can send me fixes to it, I don't mind. @@ -60,14 +63,11 @@ /** 20041207-1246:PLD: Added RT32 macro to allow for large numbers of read-tests **/ #define RT32( num_addr, offset ) if (read_32(num_addr, offset)==-1) return -1 -#define TNEF_PATH_SIZE 1024 - struct TNEF_globals { int file_num; int verbose; int verbosity_contenttype; int debug; - char path[ TNEF_PATH_SIZE +1]; int TNEF_Verbose; int savedata; @@ -103,7 +103,7 @@ static struct TNEF_globals TNEF_glb; Changes: \------------------------------------------------------------------*/ -int TNEF_init( void ) +void TNEF_init( void ) { TNEF_glb.file_num = 0; TNEF_glb.verbose = 0; @@ -112,12 +112,8 @@ int TNEF_init( void ) TNEF_glb.savedata = 1; TNEF_glb.TNEF_Verbose = 0; TNEF_glb.filename_decoded_report = NULL; - TNEF_glb.path[0] = '\0'; - - return 0; } - /*-----------------------------------------------------------------\ Function Name : TNEF_set_decode Returns Type : int @@ -140,22 +136,6 @@ int TNEF_set_decode( int level ) return TNEF_glb.savedata; } - -/*------------------------------------------------------------------------ -Procedure: TNEF_set_path ID:1 -Purpose: -Input: -Output: -Errors: -------------------------------------------------------------------------*/ -int TNEF_set_path( char *path ) -{ - snprintf( TNEF_glb.path, TNEF_PATH_SIZE , "%s", path); - - return 0; -} - - /*------------------------------------------------------------------------ Procedure: TNEF_set_verbosity ID:1 Purpose: @@ -326,12 +306,12 @@ Procedure: save_attach_data ID:1 Output: Errors: ------------------------------------------------------------------------*/ -int save_attach_data(char *title, uint8 *tsp, uint32 size) +int save_attach_data(char *title, uint8 *tsp, uint32 size, char * file_dir) { FILE *out; char filename[1024]; - snprintf(filename, sizeof(filename),"%s/%s", TNEF_glb.path, title ); + snprintf(filename, sizeof(filename),"%s/%s", title ); out = fopen(filename, "w"); if (!out) @@ -354,7 +334,7 @@ Procedure: handle_props ID:1 Output: Errors: ------------------------------------------------------------------------*/ -int handle_props(uint8 *tsp) +int handle_props(uint8 *tsp, char * file_dir) { int bytes = 0; uint32 num_props = 0; @@ -384,7 +364,7 @@ int handle_props(uint8 *tsp) { sprintf (filename, "XAM_%d.rtf", TNEF_glb.file_num); TNEF_glb.file_num++; - save_attach_data(filename, tsp+bytes, num); + save_attach_data(filename, tsp+bytes, num, file_dir); } /* num + PAD */ bytes += num + ((num % 4) ? (4 - num%4) : 0); @@ -481,7 +461,7 @@ Procedure: read_attribute ID:1 Output: Errors: ------------------------------------------------------------------------*/ -int read_attribute(uint8 *tsp) +int read_attribute(uint8 *tsp, char *file_dir) { int bytes = 0, header = 0; @@ -574,7 +554,7 @@ int read_attribute(uint8 *tsp) // attach_loc =(int)tsp+header; // 2003-02-22-1232-PLD attach_loc =(uint8 *)tsp+header; if (TNEF_glb.savedata && strlen(attach_title)>0 && attach_size > 0) { - if (!save_attach_data(attach_title, (uint8 *)attach_loc,attach_size)) + if (!save_attach_data(attach_title, (uint8 *)attach_loc,attach_size,file_dir)) { if (TNEF_VERBOSE) { if (TNEF_glb.filename_decoded_report == NULL) @@ -595,7 +575,7 @@ int read_attribute(uint8 *tsp) case attAttachTitle: strncpy(attach_title, make_string(tsp+header,size),255); if (TNEF_glb.savedata && strlen(attach_title)>0 && attach_size > 0) { - if (!save_attach_data(attach_title, (uint8 *)attach_loc,attach_size)) + if (!save_attach_data(attach_title, (uint8 *)attach_loc,attach_size, file_dir)) { if (TNEF_VERBOSE) { if (TNEF_glb.filename_decoded_report == NULL) @@ -632,7 +612,7 @@ int read_attribute(uint8 *tsp) default_handler(attribute, tsp+header, size); break; case attMAPIProps: - if (handle_props(tsp+header)==-1) return -1; + if (handle_props(tsp+header, file_dir)==-1) return -1; break; case attRecipTable: default_handler(attribute, tsp+header, size); @@ -689,7 +669,7 @@ Procedure: decode_tnef ID:1 Output: Errors: ------------------------------------------------------------------------*/ -int TNEF_decode_tnef(uint8 *tnef_stream, int size) +int TNEF_decode_tnef(uint8 *tnef_stream, int size, char* file_dir) { int ra_response; @@ -733,7 +713,7 @@ int TNEF_decode_tnef(uint8 *tnef_stream, int size) while ((tsp - tnef_stream) < size) { if (TNEF_DEBUG) LOGGER_log("%s:%d:TNEF_decode_tnef:DEBUG: Offset = %d\n", FL,tsp -TNEF_glb.tnef_home); - ra_response = read_attribute(tsp); + ra_response = read_attribute(tsp, file_dir); if ( ra_response > 0 ) { tsp += ra_response; @@ -752,11 +732,6 @@ int TNEF_decode_tnef(uint8 *tnef_stream, int size) return 0; } - - - - - /*------------------------------------------------------------------------ Procedure: TNEF_main ID:1 Purpose: Decodes a given TNEF encoded file @@ -764,7 +739,7 @@ Purpose: Decodes a given TNEF encoded file Output: Errors: ------------------------------------------------------------------------*/ -int TNEF_main( char *filename ) +int TNEF_main( char *filename, char *file_dir ) { FILE *fp; struct stat sb; @@ -840,17 +815,13 @@ int TNEF_main( char *filename ) // Proceed to decode the file // - TNEF_decode_tnef(tnef_stream,size); + TNEF_decode_tnef(tnef_stream,size, file_dir); if (TNEF_glb.tnef_home) free(TNEF_glb.tnef_home); - if (TNEF_DEBUG) LOGGER_log("%s:%d:TNEF_main:DEBUG: finished decoding.\n",FL); return 0; } - //--------------------------END. - - diff --git a/tnef/tnef_api.h b/tnef/tnef_api.h index 9dd3b24..3ecefbe 100644 --- a/tnef/tnef_api.h +++ b/tnef/tnef_api.h @@ -3,12 +3,11 @@ #ifndef __TNEF_API__ #define __TNEF_API__ -int TNEF_init( void ); -int TNEF_main( char *filename ); +void TNEF_init( void ); +int TNEF_main( char *filename, char* file_dir ); int TNEF_set_filename_report_fn( int (*ptr_to_fn)(char *, char *)); int TNEF_set_verbosity( int level ); int TNEF_set_verbosity_contenttype( int level ); int TNEF_set_debug( int level ); -int TNEF_set_path( char *path ); int TNEF_set_decode( int level ); #endif From c7fef0af46c0573f828bf726513f9946303ca85c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Thu, 24 Apr 2025 10:35:46 +0300 Subject: [PATCH 20/80] Remove TNEF savedata, because there will be in-memory `FILE` object. --- tnef/tnef.c | 42 ++---------------------------------------- 1 file changed, 2 insertions(+), 40 deletions(-) diff --git a/tnef/tnef.c b/tnef/tnef.c index 6eef4c3..c25a662 100644 --- a/tnef/tnef.c +++ b/tnef/tnef.c @@ -70,7 +70,6 @@ struct TNEF_globals { int debug; int TNEF_Verbose; - int savedata; uint8 *tnef_home; uint8 *tnef_limit; @@ -81,13 +80,6 @@ struct TNEF_globals { static struct TNEF_globals TNEF_glb; -// The variables below have been pushed into the TNEF globals -//int Verbose = FALSE; -//int SaveData = FALSE; -//uint8 *tnef_home; -//uint8 *tnef_limit; - - /*-----------------------------------------------------------------\ Function Name : TNEF_init Returns Type : int @@ -109,33 +101,10 @@ void TNEF_init( void ) TNEF_glb.verbose = 0; TNEF_glb.verbosity_contenttype = 0; TNEF_glb.debug = 0; - TNEF_glb.savedata = 1; TNEF_glb.TNEF_Verbose = 0; TNEF_glb.filename_decoded_report = NULL; } -/*-----------------------------------------------------------------\ - Function Name : TNEF_set_decode - Returns Type : int - ----Parameter List - 1. int level , - ------------------ - Exit Codes : - Side Effects : - -------------------------------------------------------------------- -Comments: - --------------------------------------------------------------------- -Changes: - -\------------------------------------------------------------------*/ -int TNEF_set_decode( int level ) -{ - TNEF_glb.savedata = level; - - return TNEF_glb.savedata; -} - /*------------------------------------------------------------------------ Procedure: TNEF_set_verbosity ID:1 Purpose: @@ -553,7 +522,7 @@ int read_attribute(uint8 *tsp, char *file_dir) attach_size=size; // attach_loc =(int)tsp+header; // 2003-02-22-1232-PLD attach_loc =(uint8 *)tsp+header; - if (TNEF_glb.savedata && strlen(attach_title)>0 && attach_size > 0) { + if (strlen(attach_title)>0 && attach_size > 0) { if (!save_attach_data(attach_title, (uint8 *)attach_loc,attach_size,file_dir)) { if (TNEF_VERBOSE) { @@ -574,7 +543,7 @@ int read_attribute(uint8 *tsp, char *file_dir) break; case attAttachTitle: strncpy(attach_title, make_string(tsp+header,size),255); - if (TNEF_glb.savedata && strlen(attach_title)>0 && attach_size > 0) { + if (strlen(attach_title)>0 && attach_size > 0) { if (!save_attach_data(attach_title, (uint8 *)attach_loc,attach_size, file_dir)) { if (TNEF_VERBOSE) { @@ -748,13 +717,6 @@ int TNEF_main( char *filename, char *file_dir ) if (TNEF_DEBUG) LOGGER_log("%s:%d:TNEF_main:DEBUG: Start, decoding %s\n",FL, filename); - if (TNEF_glb.savedata == 0 ) - { - if (TNEF_DEBUG) LOGGER_log("%s:%d:TNEF_name:DEBUG: decode_tnef is set to 0, not decoding file.",FL); - return 0; - } - - // Test to see if the file actually exists // if (stat(filename,&sb) == -1) From 869bfb9bb2ee06150b9f5c3d33a5ee72de75153c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Thu, 24 Apr 2025 10:42:48 +0300 Subject: [PATCH 21/80] Unify file name with other, exclude TNEF decode flague --- Makefile | 2 +- mime.c | 24 +----------------------- MIME_headers.c => mime_headers.c | 2 +- MIME_headers.h => mime_headers.h | 0 ripmime.c | 9 ++------- 5 files changed, 5 insertions(+), 32 deletions(-) rename MIME_headers.c => mime_headers.c (99%) rename MIME_headers.h => mime_headers.h (100%) diff --git a/Makefile b/Makefile index 9299dfd..fc3f637 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ LIBS= OBJ=ripmime RIPOLE_OBJS= ripOLE/ole.o ripOLE/olestream-unwrap.o ripOLE/bytedecoders.o ripOLE/bt-int.o #RIPOLE_OBJS= -OFILES= strstack.o mime.o ffget.o MIME_headers.o tnef/tnef.o rawget.o pldstr.o logger.o libmime-decoders.o boundary-stack.o uuencode.o filename-filters.o $(RIPOLE_OBJS) +OFILES= strstack.o mime.o ffget.o mime_headers.o tnef/tnef.o rawget.o pldstr.o logger.o libmime-decoders.o boundary-stack.o uuencode.o filename-filters.o $(RIPOLE_OBJS) default: tnef/tnef.o ripmime ripOLE/ole.o diff --git a/mime.c b/mime.c index 252fef9..1fd97e3 100644 --- a/mime.c +++ b/mime.c @@ -57,7 +57,7 @@ #include "logger.h" #include "strstack.h" -#include "MIME_headers.h" +#include "mime_headers.h" int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int current_recursion_level, struct SS_object *ss ); int MIME_unpack_single( RIPMIME_output *unpack_metadata, char *mpname, int current_recursion_level, struct SS_object *ss ); @@ -333,28 +333,6 @@ int MIME_set_recursion_level( int level ) return glb.max_recursion_level; } -/*-----------------------------------------------------------------\ - Function Name : MIME_set_decode_tnef - Returns Type : int - ----Parameter List - 1. int level , - ------------------ - Exit Codes : - Side Effects : - -------------------------------------------------------------------- -Comments: - --------------------------------------------------------------------- -Changes: - -\------------------------------------------------------------------*/ -int MIME_set_decode_tnef( int level ) -{ - glb.decode_tnef = level; - TNEF_set_decode( level ); - - return level; -} /*-----------------------------------------------------------------\ Function Name : MIME_set_decode_ole Returns Type : int diff --git a/MIME_headers.c b/mime_headers.c similarity index 99% rename from MIME_headers.c rename to mime_headers.c index 8ce6431..d10896a 100644 --- a/MIME_headers.c +++ b/mime_headers.c @@ -35,7 +35,7 @@ #include "strstack.h" #include "boundary-stack.h" #include "filename-filters.h" -#include "MIME_headers.h" +#include "mime_headers.h" #ifndef FL #define FL __FILE__, __LINE__ diff --git a/MIME_headers.h b/mime_headers.h similarity index 100% rename from MIME_headers.h rename to mime_headers.h diff --git a/ripmime.c b/ripmime.c index 500a2b0..acb4422 100644 --- a/ripmime.c +++ b/ripmime.c @@ -25,7 +25,7 @@ #include "ffget.h" #include "mime.h" #include "strstack.h" -#include "MIME_headers.h" +#include "mime_headers.h" #define RIPMIME_ERROR_CANT_CREATE_OUTPUT_DIR 1 #define RIPMIME_ERROR_CANT_OPEN_INPUT_FILE 2 @@ -54,7 +54,7 @@ char help[] = "ripMIME -i -d " "[-p prefix] [-e [header file]] [-vVh] [--version]" "[--no_nameless] [--unique_names [--prefix|--postfix|--infix|--randprefix|--randpostfix|--randinfix]]" "[--paranoid] [--mailbox] [--formdata] [--debug]" - "[--no-tnef] [--no-quotedprintable] [--no-uudecode]\n" + "[--no-quotedprintable] [--no-uudecode]\n" "Options available :\n" "-i : Input MIME encoded file (use '-' to input from STDIN)\n" "\tIf is a directory, it will be recursed\n" @@ -90,7 +90,6 @@ char help[] = "ripMIME -i -d " "--mailbox : Process mailbox file\n" "--formdata : Process as form data (from HTML form etc). Inhibits conversion of NUL/zero-bytes to spaces\n" "\n" - "--no-tnef : Turn off TNEF/winmail.dat decoding\n" "--no-ole : Turn off OLE decoding\n" "--no-uudecode : Turns off the facility of detecting UUencoded attachments in emails\n" "--no-quotedprintable : Turns off the facility of decoding QuotedPrintable data\n" @@ -409,10 +408,6 @@ int RIPMIME_parse_parameters (struct RIPMIME_globals *glb, int argc, char **argv { MIME_set_decode_uudecode(0); } - else if (strncmp (&(argv[i][2]), "no-tnef", 7) == 0) - { - MIME_set_decode_tnef (0); - } else if (strncmp (&(argv[i][2]), "no-ole", 6) == 0) { MIME_set_decode_ole(0); From 8cd22451293f6781aec1df51fb30be348b58ad23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Thu, 24 Apr 2025 11:06:11 +0300 Subject: [PATCH 22/80] Add `MIME_element_add_with_path` to interface --- mime.c | 11 ----------- mime.h | 14 ++++++++++++-- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/mime.c b/mime.c index 1fd97e3..7f815b1 100644 --- a/mime.c +++ b/mime.c @@ -55,9 +55,6 @@ #include "uuencode.h" #include "filename-filters.h" #include "logger.h" -#include "strstack.h" - -#include "mime_headers.h" int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int current_recursion_level, struct SS_object *ss ); int MIME_unpack_single( RIPMIME_output *unpack_metadata, char *mpname, int current_recursion_level, struct SS_object *ss ); @@ -125,14 +122,6 @@ static unsigned char b64[256]={ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128 \ }; -typedef struct { - struct MIMEH_header_info *hinfo; - int id; - char* fullpath; - FILE* f; - int result; -} MIME_element; - typedef struct { size_t size; size_t capacity; diff --git a/mime.h b/mime.h index 2d8bb8e..0a2b260 100644 --- a/mime.h +++ b/mime.h @@ -129,8 +129,18 @@ char *MIME_get_subject( void ); void MIME_init( void ); void MIME_close( void ); int MIME_set_tmpdir( char *tmpdir ); -//int MIME_postdecode_cleanup( RIPMIME_output *unpack_metadata, struct SS_object *ss ); -//int MIME_decode_TNEF( FILE *f, RIPMIME_output *unpack_metadata, struct _header_info *hinfo, int keep ); +#include "strstack.h" +#include "mime_headers.h" + +typedef struct { + struct MIMEH_header_info *hinfo; + int id; + char* fullpath; + FILE* f; + int result; +} MIME_element; + +MIME_element* MIME_element_add_with_path (char* fullpath, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo); #endif From bb18f776abeae4bfd530ba3e9d7b4b7639a0b064 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Thu, 24 Apr 2025 11:13:29 +0300 Subject: [PATCH 23/80] Add fromal parametres for future `UUENCODE_decode_uu` refactoring --- mime.c | 8 ++++---- uuencode.c | 2 +- uuencode.h | 4 +++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/mime.c b/mime.c index 7f815b1..fa9799b 100644 --- a/mime.c +++ b/mime.c @@ -1359,7 +1359,7 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME if ( hinfo->content_transfer_encoding == _CTRANS_ENCODING_UUENCODE ) decode_entire_file = 0; //result = UUENCODE_decode_uu(NULL, unpack_metadata->dir, hinfo->filename, hinfo->uudec_name, sizeof(hinfo->uudec_name), decode_entire_file, keep ); - result = UUENCODE_decode_uu(NULL, unpack_metadata->dir, full_decode_path, hinfo->uudec_name, sizeof(hinfo->uudec_name), decode_entire_file, keep ); + result = UUENCODE_decode_uu(NULL, unpack_metadata->dir, full_decode_path, hinfo->uudec_name, sizeof(hinfo->uudec_name), decode_entire_file, keep, unpack_metadata, hinfo ); if (result == -1) { switch (uuencode_error) { @@ -1514,7 +1514,7 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM // propergate this value unintentionally to parent functions (ie, if you were thinking it was // an error-status return value - result = UUENCODE_decode_uu( NULL, unpack_metadata->dir, ffname, hinfo->uudec_name, sizeof(hinfo->uudec_name), 1, keep ); + result = UUENCODE_decode_uu( NULL, unpack_metadata->dir, ffname, hinfo->uudec_name, sizeof(hinfo->uudec_name), 1, keep, unpack_metadata, hinfo ); if (result == -1) { switch (uuencode_error) { @@ -1924,7 +1924,7 @@ int MIME_doubleCR_decode( char *filename, RIPMIME_output *unpack_metadata, struc { if (MIME_VERBOSE) LOGGER_log("Attempting to decode UUENCODED attachment from Double-CR delimeted attachment '%s'\n",filename); UUENCODE_set_doubleCR_mode(1); - result = UUENCODE_decode_uu(NULL, unpack_metadata->dir, filename, h.uudec_name, _MIMEH_FILENAMELEN_MAX , 1, 1 ); + result = UUENCODE_decode_uu(NULL, unpack_metadata->dir, filename, h.uudec_name, _MIMEH_FILENAMELEN_MAX , 1, 1, unpack_metadata, hinfo ); UUENCODE_set_doubleCR_mode(0); glb.attachment_count += result; result = 0; @@ -2391,7 +2391,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding UUENCODED format\n",FL); // Added as a test - remove if we can get this to work in a better way snprintf(hinfo->uudec_name,sizeof(hinfo->uudec_name),"%s",hinfo->filename); - result = UUENCODE_decode_uu(f, unpack_metadata->dir, hinfo->filename, hinfo->uudec_name, sizeof(hinfo->uudec_name), 0, keep ); + result = UUENCODE_decode_uu(f, unpack_metadata->dir, hinfo->filename, hinfo->uudec_name, sizeof(hinfo->uudec_name), 0, keep, unpack_metadata, hinfo ); glb.attachment_count += result; // Because this is a file-count, it's not really an 'error result' as such, so, set the // return code back to 0! diff --git a/uuencode.c b/uuencode.c index 8720e08..7f51222 100644 --- a/uuencode.c +++ b/uuencode.c @@ -354,7 +354,7 @@ int UUENCODE_is_diskfile_uuencoded( char *fname ) Changes: \------------------------------------------------------------------*/ -int UUENCODE_decode_uu( FFGET_FILE *f, char *unpackdir, char *input_filename, char *out_filename, int out_filename_size, int decode_whole_file, int keep ) +int UUENCODE_decode_uu( FFGET_FILE *f, char *unpackdir, char *input_filename, char *out_filename, int out_filename_size, int decode_whole_file, int keep, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ) { int filename_found = 0; char buf[ UUENCODE_STRLEN_MAX ]; diff --git a/uuencode.h b/uuencode.h index feb35bc..1c0d882 100644 --- a/uuencode.h +++ b/uuencode.h @@ -4,6 +4,8 @@ #define UUENCODE_STATUS_CANNOT_FIND_FILENAME 103 #define UUENCODE_STATUS_OK 0 +#include "mime.h" + extern int uuencode_error; int UUENCODE_init( void ); @@ -20,5 +22,5 @@ int UUENCODE_set_filename_report_fn( int (*ptr_to_fn)(char *, char *) ); int UUENCODE_is_uuencode_header( char *line ); int UUENCODE_is_diskfile_uuencoded( char *fname ); -int UUENCODE_decode_uu( FFGET_FILE *f, char *unpackdir, char *input_filename, char *out_filename, int out_filename_size, int decode_whole_file, int keep ); +int UUENCODE_decode_uu( FFGET_FILE *f, char *unpackdir, char *input_filename, char *out_filename, int out_filename_size, int decode_whole_file, int keep, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ); From 2e10bd259a6fd80d6b1575c0d9a6d4299d3c49fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Thu, 24 Apr 2025 11:17:55 +0300 Subject: [PATCH 24/80] Remove separate `unpackdir`, change to `unpack_metadata` field --- mime.c | 12 ++++++------ uuencode.c | 17 +++++++++-------- uuencode.h | 2 +- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/mime.c b/mime.c index fa9799b..d29958e 100644 --- a/mime.c +++ b/mime.c @@ -1358,8 +1358,8 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Decoding UUencoded data\n",FL); if ( hinfo->content_transfer_encoding == _CTRANS_ENCODING_UUENCODE ) decode_entire_file = 0; - //result = UUENCODE_decode_uu(NULL, unpack_metadata->dir, hinfo->filename, hinfo->uudec_name, sizeof(hinfo->uudec_name), decode_entire_file, keep ); - result = UUENCODE_decode_uu(NULL, unpack_metadata->dir, full_decode_path, hinfo->uudec_name, sizeof(hinfo->uudec_name), decode_entire_file, keep, unpack_metadata, hinfo ); + //result = UUENCODE_decode_uu(NULL, hinfo->filename, hinfo->uudec_name, sizeof(hinfo->uudec_name), decode_entire_file, keep ); + result = UUENCODE_decode_uu(NULL, full_decode_path, hinfo->uudec_name, sizeof(hinfo->uudec_name), decode_entire_file, keep, unpack_metadata, hinfo ); if (result == -1) { switch (uuencode_error) { @@ -1504,7 +1504,7 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM // hinfo->uudec_name[0] = '\0'; if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Decoding UUencoded data in file '%s'\n",FL,hinfo->filename); - //result = UUENCODE_decode_uu( NULL, unpackdir, hinfo->filename, hinfo->uudec_name, sizeof(hinfo->uudec_name), 1, keep ); + //result = UUENCODE_decode_uu( NULL, info->filename, hinfo->uudec_name, sizeof(hinfo->uudec_name), 1, keep ); // Attempt to decode the UUENCODED data in the file, // NOTE - hinfo->uudec_name is a blank buffer which will be filled by the UUENCODE_decode_uu // function once it has located a filename in the UUENCODED data. A bit of a problem here @@ -1514,7 +1514,7 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM // propergate this value unintentionally to parent functions (ie, if you were thinking it was // an error-status return value - result = UUENCODE_decode_uu( NULL, unpack_metadata->dir, ffname, hinfo->uudec_name, sizeof(hinfo->uudec_name), 1, keep, unpack_metadata, hinfo ); + result = UUENCODE_decode_uu( NULL, ffname, hinfo->uudec_name, sizeof(hinfo->uudec_name), 1, keep, unpack_metadata, hinfo ); if (result == -1) { switch (uuencode_error) { @@ -1924,7 +1924,7 @@ int MIME_doubleCR_decode( char *filename, RIPMIME_output *unpack_metadata, struc { if (MIME_VERBOSE) LOGGER_log("Attempting to decode UUENCODED attachment from Double-CR delimeted attachment '%s'\n",filename); UUENCODE_set_doubleCR_mode(1); - result = UUENCODE_decode_uu(NULL, unpack_metadata->dir, filename, h.uudec_name, _MIMEH_FILENAMELEN_MAX , 1, 1, unpack_metadata, hinfo ); + result = UUENCODE_decode_uu(NULL, filename, h.uudec_name, _MIMEH_FILENAMELEN_MAX , 1, 1, unpack_metadata, hinfo ); UUENCODE_set_doubleCR_mode(0); glb.attachment_count += result; result = 0; @@ -2391,7 +2391,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding UUENCODED format\n",FL); // Added as a test - remove if we can get this to work in a better way snprintf(hinfo->uudec_name,sizeof(hinfo->uudec_name),"%s",hinfo->filename); - result = UUENCODE_decode_uu(f, unpack_metadata->dir, hinfo->filename, hinfo->uudec_name, sizeof(hinfo->uudec_name), 0, keep, unpack_metadata, hinfo ); + result = UUENCODE_decode_uu(f, hinfo->filename, hinfo->uudec_name, sizeof(hinfo->uudec_name), 0, keep, unpack_metadata, hinfo ); glb.attachment_count += result; // Because this is a file-count, it's not really an 'error result' as such, so, set the // return code back to 0! diff --git a/uuencode.c b/uuencode.c index 7f51222..698afb7 100644 --- a/uuencode.c +++ b/uuencode.c @@ -338,12 +338,13 @@ int UUENCODE_is_diskfile_uuencoded( char *fname ) Returns Type : int ----Parameter List 1. FFGET_FILE *f, Source Data Stream - 2. char *unpackdir, Directory to prefix to our write output - 3. char *input_filename, The fully pathed input filename, containing UU data - 4. char *out_filename, Pointer to a buffer where we will write the filename of the UU data - 5. int out_filename_size, out_filename buffers size - 6. int decode_whole_file, 0 == only first segment, >0 == all - 7. int keep , Keep the files we create, don't delete + 2. char *input_filename, The fully pathed input filename, containing UU data + 3. char *out_filename, Pointer to a buffer where we will write the filename of the UU data + 4. int out_filename_size, out_filename buffers size + 5. int decode_whole_file, 0 == only first segment, >0 == all + 6. int keep , Keep the files we create, don't delete + 7. unpack file metadata + 8. related MIME headers ------------------ Exit Codes : Returns the number of attachments decoded in the data Side Effects : @@ -354,7 +355,7 @@ int UUENCODE_is_diskfile_uuencoded( char *fname ) Changes: \------------------------------------------------------------------*/ -int UUENCODE_decode_uu( FFGET_FILE *f, char *unpackdir, char *input_filename, char *out_filename, int out_filename_size, int decode_whole_file, int keep, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ) +int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, int out_filename_size, int decode_whole_file, int keep, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ) { int filename_found = 0; char buf[ UUENCODE_STRLEN_MAX ]; @@ -507,7 +508,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *unpackdir, char *input_filename, ch // Create the new output full path - snprintf(fullpath, sizeof(fullpath), "%s/%s", unpackdir, bp ); + snprintf(fullpath, sizeof(fullpath), "%s/%s", unpack_metadata->dir, bp ); if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Filename = (%s)\n", FL, fullpath); outf = fopen(fullpath, "wb"); diff --git a/uuencode.h b/uuencode.h index 1c0d882..1ff2b4b 100644 --- a/uuencode.h +++ b/uuencode.h @@ -22,5 +22,5 @@ int UUENCODE_set_filename_report_fn( int (*ptr_to_fn)(char *, char *) ); int UUENCODE_is_uuencode_header( char *line ); int UUENCODE_is_diskfile_uuencoded( char *fname ); -int UUENCODE_decode_uu( FFGET_FILE *f, char *unpackdir, char *input_filename, char *out_filename, int out_filename_size, int decode_whole_file, int keep, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ); +int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, int out_filename_size, int decode_whole_file, int keep, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ); From 9bfb3915dc9c84a880f8f5ce0212a7d52e6900f7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Thu, 24 Apr 2025 11:28:40 +0300 Subject: [PATCH 25/80] Remove `out_filename_size` --- mime.c | 12 ++++++------ uuencode.c | 26 +++++++++----------------- uuencode.h | 2 +- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/mime.c b/mime.c index d29958e..3e7974f 100644 --- a/mime.c +++ b/mime.c @@ -1358,8 +1358,8 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Decoding UUencoded data\n",FL); if ( hinfo->content_transfer_encoding == _CTRANS_ENCODING_UUENCODE ) decode_entire_file = 0; - //result = UUENCODE_decode_uu(NULL, hinfo->filename, hinfo->uudec_name, sizeof(hinfo->uudec_name), decode_entire_file, keep ); - result = UUENCODE_decode_uu(NULL, full_decode_path, hinfo->uudec_name, sizeof(hinfo->uudec_name), decode_entire_file, keep, unpack_metadata, hinfo ); + //result = UUENCODE_decode_uu(NULL, hinfo->filename, hinfo->uudec_name, decode_entire_file, keep ); + result = UUENCODE_decode_uu(NULL, full_decode_path, hinfo->uudec_name, decode_entire_file, keep, unpack_metadata, hinfo ); if (result == -1) { switch (uuencode_error) { @@ -1504,7 +1504,7 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM // hinfo->uudec_name[0] = '\0'; if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Decoding UUencoded data in file '%s'\n",FL,hinfo->filename); - //result = UUENCODE_decode_uu( NULL, info->filename, hinfo->uudec_name, sizeof(hinfo->uudec_name), 1, keep ); + //result = UUENCODE_decode_uu( NULL, info->filename, hinfo->uudec_name, 1, keep ); // Attempt to decode the UUENCODED data in the file, // NOTE - hinfo->uudec_name is a blank buffer which will be filled by the UUENCODE_decode_uu // function once it has located a filename in the UUENCODED data. A bit of a problem here @@ -1514,7 +1514,7 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM // propergate this value unintentionally to parent functions (ie, if you were thinking it was // an error-status return value - result = UUENCODE_decode_uu( NULL, ffname, hinfo->uudec_name, sizeof(hinfo->uudec_name), 1, keep, unpack_metadata, hinfo ); + result = UUENCODE_decode_uu( NULL, ffname, hinfo->uudec_name, 1, keep, unpack_metadata, hinfo ); if (result == -1) { switch (uuencode_error) { @@ -1924,7 +1924,7 @@ int MIME_doubleCR_decode( char *filename, RIPMIME_output *unpack_metadata, struc { if (MIME_VERBOSE) LOGGER_log("Attempting to decode UUENCODED attachment from Double-CR delimeted attachment '%s'\n",filename); UUENCODE_set_doubleCR_mode(1); - result = UUENCODE_decode_uu(NULL, filename, h.uudec_name, _MIMEH_FILENAMELEN_MAX , 1, 1, unpack_metadata, hinfo ); + result = UUENCODE_decode_uu(NULL, filename, h.uudec_name, 1, 1, unpack_metadata, hinfo ); UUENCODE_set_doubleCR_mode(0); glb.attachment_count += result; result = 0; @@ -2391,7 +2391,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding UUENCODED format\n",FL); // Added as a test - remove if we can get this to work in a better way snprintf(hinfo->uudec_name,sizeof(hinfo->uudec_name),"%s",hinfo->filename); - result = UUENCODE_decode_uu(f, hinfo->filename, hinfo->uudec_name, sizeof(hinfo->uudec_name), 0, keep, unpack_metadata, hinfo ); + result = UUENCODE_decode_uu(f, hinfo->filename, hinfo->uudec_name, 0, keep, unpack_metadata, hinfo ); glb.attachment_count += result; // Because this is a file-count, it's not really an 'error result' as such, so, set the // return code back to 0! diff --git a/uuencode.c b/uuencode.c index 698afb7..c4e3b5b 100644 --- a/uuencode.c +++ b/uuencode.c @@ -340,11 +340,10 @@ int UUENCODE_is_diskfile_uuencoded( char *fname ) 1. FFGET_FILE *f, Source Data Stream 2. char *input_filename, The fully pathed input filename, containing UU data 3. char *out_filename, Pointer to a buffer where we will write the filename of the UU data - 4. int out_filename_size, out_filename buffers size - 5. int decode_whole_file, 0 == only first segment, >0 == all - 6. int keep , Keep the files we create, don't delete - 7. unpack file metadata - 8. related MIME headers + 4. int decode_whole_file, 0 == only first segment, >0 == all + 5. int keep , Keep the files we create, don't delete + 6. unpack file metadata + 7. related MIME headers ------------------ Exit Codes : Returns the number of attachments decoded in the data Side Effects : @@ -355,7 +354,7 @@ int UUENCODE_is_diskfile_uuencoded( char *fname ) Changes: \------------------------------------------------------------------*/ -int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, int out_filename_size, int decode_whole_file, int keep, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ) +int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, int decode_whole_file, int keep, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ) { int filename_found = 0; char buf[ UUENCODE_STRLEN_MAX ]; @@ -374,17 +373,9 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, FILE *outf; int outfo =0; // set if outfile was opened. FILE *inf = NULL; - int output_filename_supplied = 0; + int output_filename_supplied = (out_filename != NULL) && (out_filename[0] != '\0'); int start_found = 0; - if ((out_filename != NULL)) - { - if ((out_filename[0] != '\0')) - { - output_filename_supplied = 1; - } - } - bp = buf; if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Starting.(input=%s,output=%s)\n", FL, input_filename,out_filename ); @@ -501,10 +492,11 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Located filename (%s), now decoding.\n", FL, bp); // Clean up the file name - FNFILTER_filter( bp, out_filename_size ); + FNFILTER_filter( bp, 255 ); /* the longest for most of filesystems */ // If our filename wasn't supplied via the params, then copy it over here - if (output_filename_supplied == 0) snprintf( out_filename, out_filename_size, "%s", bp ); + if (output_filename_supplied == 0) + out_filename = strdup(bp); // Create the new output full path diff --git a/uuencode.h b/uuencode.h index 1ff2b4b..3854f86 100644 --- a/uuencode.h +++ b/uuencode.h @@ -22,5 +22,5 @@ int UUENCODE_set_filename_report_fn( int (*ptr_to_fn)(char *, char *) ); int UUENCODE_is_uuencode_header( char *line ); int UUENCODE_is_diskfile_uuencoded( char *fname ); -int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, int out_filename_size, int decode_whole_file, int keep, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ); +int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, int decode_whole_file, int keep, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ); From 67d43c6f222c778641963ec201e51f1bdb8232d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Thu, 24 Apr 2025 11:32:53 +0300 Subject: [PATCH 26/80] Unify output file naming --- uuencode.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/uuencode.c b/uuencode.c index c4e3b5b..57bb02d 100644 --- a/uuencode.c +++ b/uuencode.c @@ -370,8 +370,8 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, int filecount = 0; FFGET_FILE ffinf; // Local static FFGET struct used if *f is NULL FFGET_FILE *finf; // Points to either *f or &ffinf - FILE *outf; - int outfo =0; // set if outfile was opened. + FILE *result_f; + int result_fo =0; // set if outfile was opened. FILE *inf = NULL; int output_filename_supplied = (out_filename != NULL) && (out_filename[0] != '\0'); int start_found = 0; @@ -503,15 +503,15 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, snprintf(fullpath, sizeof(fullpath), "%s/%s", unpack_metadata->dir, bp ); if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Filename = (%s)\n", FL, fullpath); - outf = fopen(fullpath, "wb"); - if (!outf) + result_f = fopen(fullpath, "wb"); + if (!result_f) { LOGGER_log("%s:%d:UUENCODE_decode_uu:ERROR: Cannot open file '%s' (%s)", FL, fullpath,strerror(errno)); if (writebuffer) free(writebuffer); uuencode_error = UUENCODE_STATUS_CANNOT_OPEN_FILE; return -1; } - else outfo = 1; + else result_fo = 1; // Allocate the write buffer. By using the write buffer we gain an additional 10% in performance // due to the lack of function call (fwrite) overheads @@ -521,7 +521,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, wbcount = 0; wbpos = writebuffer; - while (outf) + while (result_f) { // for each input line FFGET_fgets(buf, sizeof(buf), finf); @@ -571,7 +571,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, // In order to reduce function call overheads, we've bought the UUDecoding // bit shifting routines into the UUDecode main decoding routines. This should // save us about 250,000 function calls per Mb. - // UUENCODE_outdec(bp, outf, n); + // UUENCODE_outdec(bp, result_f, n); char c[3]; int m = n; @@ -585,7 +585,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, if ( wbcount >= UUENCODE_WRITE_BUFFER_LIMIT ) { size_t bc; - bc = fwrite(writebuffer, 1, wbcount, outf); + bc = fwrite(writebuffer, 1, wbcount, result_f); if (bc != wbcount) { LOGGER_log("%s:%d:ERROR: Attempted to write %ld bytes, only wrote %ld\n", FL, wbcount, bc); } @@ -612,16 +612,16 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, } // While (1) - if ((outfo)&&(wbcount > 0)) + if ((result_fo)&&(wbcount > 0)) { size_t bc; - bc = fwrite(writebuffer, 1, wbcount, outf); + bc = fwrite(writebuffer, 1, wbcount, result_f); if (bc != wbcount) { LOGGER_log("%s:%d:ERROR: Attempted to write %ld bytes, only wrote %ld\n", FL, wbcount, bc); } } - if (outfo) fclose(outf); + if (result_fo) fclose(result_f); // Call our reporting function, else, if no function is defined, use the default // standard call From 3f7c8db1fc02ca841821ee2dd4e66eebbe01ccbe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Thu, 24 Apr 2025 11:40:49 +0300 Subject: [PATCH 27/80] Refactor UUE decode to `MIME_element` usage --- uuencode.c | 25 ++++++++----------------- 1 file changed, 8 insertions(+), 17 deletions(-) diff --git a/uuencode.c b/uuencode.c index 57bb02d..0e67578 100644 --- a/uuencode.c +++ b/uuencode.c @@ -370,8 +370,6 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, int filecount = 0; FFGET_FILE ffinf; // Local static FFGET struct used if *f is NULL FFGET_FILE *finf; // Points to either *f or &ffinf - FILE *result_f; - int result_fo =0; // set if outfile was opened. FILE *inf = NULL; int output_filename_supplied = (out_filename != NULL) && (out_filename[0] != '\0'); int start_found = 0; @@ -489,6 +487,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, if ((filename_found != 0)&&(bp)) { + MIME_element* mime_el = NULL; if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Located filename (%s), now decoding.\n", FL, bp); // Clean up the file name @@ -503,15 +502,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, snprintf(fullpath, sizeof(fullpath), "%s/%s", unpack_metadata->dir, bp ); if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Filename = (%s)\n", FL, fullpath); - result_f = fopen(fullpath, "wb"); - if (!result_f) - { - LOGGER_log("%s:%d:UUENCODE_decode_uu:ERROR: Cannot open file '%s' (%s)", FL, fullpath,strerror(errno)); - if (writebuffer) free(writebuffer); - uuencode_error = UUENCODE_STATUS_CANNOT_OPEN_FILE; - return -1; - } - else result_fo = 1; + mime_el = MIME_element_add_with_path (fullpath, unpack_metadata, hinfo); // Allocate the write buffer. By using the write buffer we gain an additional 10% in performance // due to the lack of function call (fwrite) overheads @@ -521,7 +512,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, wbcount = 0; wbpos = writebuffer; - while (result_f) + while (mime_el->f) { // for each input line FFGET_fgets(buf, sizeof(buf), finf); @@ -571,7 +562,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, // In order to reduce function call overheads, we've bought the UUDecoding // bit shifting routines into the UUDecode main decoding routines. This should // save us about 250,000 function calls per Mb. - // UUENCODE_outdec(bp, result_f, n); + // UUENCODE_outdec(bp, mime_el->f, n); char c[3]; int m = n; @@ -585,7 +576,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, if ( wbcount >= UUENCODE_WRITE_BUFFER_LIMIT ) { size_t bc; - bc = fwrite(writebuffer, 1, wbcount, result_f); + bc = fwrite(writebuffer, 1, wbcount, mime_el->f); if (bc != wbcount) { LOGGER_log("%s:%d:ERROR: Attempted to write %ld bytes, only wrote %ld\n", FL, wbcount, bc); } @@ -612,16 +603,16 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, } // While (1) - if ((result_fo)&&(wbcount > 0)) + if ((mime_el->f != NULL)&&(wbcount > 0)) { size_t bc; - bc = fwrite(writebuffer, 1, wbcount, result_f); + bc = fwrite(writebuffer, 1, wbcount, mime_el->f); if (bc != wbcount) { LOGGER_log("%s:%d:ERROR: Attempted to write %ld bytes, only wrote %ld\n", FL, wbcount, bc); } } - if (result_fo) fclose(result_f); + MIME_element_remove(mime_el); // Call our reporting function, else, if no function is defined, use the default // standard call From 5822462f285543297438cc0699dcdb23563d3428 Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Fri, 25 Apr 2025 09:59:45 +0300 Subject: [PATCH 28/80] Fix function decalaration --- mime.h | 1 + 1 file changed, 1 insertion(+) diff --git a/mime.h b/mime.h index 0a2b260..649e108 100644 --- a/mime.h +++ b/mime.h @@ -142,5 +142,6 @@ typedef struct { } MIME_element; MIME_element* MIME_element_add_with_path (char* fullpath, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo); +void MIME_element_remove (MIME_element* cur); #endif From 97505e7eb655f62b659cffd6f6065442cbec4f5f Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Fri, 25 Apr 2025 10:18:19 +0300 Subject: [PATCH 29/80] Exclude stat file size from OLE module --- ripOLE/ole.c | 78 ++++++++-------------------------------------------- 1 file changed, 12 insertions(+), 66 deletions(-) diff --git a/ripOLE/ole.c b/ripOLE/ole.c index 05ede33..7d10370 100644 --- a/ripOLE/ole.c +++ b/ripOLE/ole.c @@ -14,10 +14,10 @@ #include "ole.h" /** Sector ID values (predefined) **/ -#define OLE_SECTORID_FREE -1 /** Unallocated sector **/ -#define OLE_SECTORID_ENDOFCHAIN -2 /** Sector marks the end of the a sector-ID chain **/ -#define OLE_SECTORID_SAT -3 /** Sector used by sector allocation Table **/ -#define OLE_SECTORID_MSAT -4 /** Sector used by master sector allocation Table **/ +#define OLE_SECTORID_FREE -1 /** Unallocated sector **/ +#define OLE_SECTORID_ENDOFCHAIN -2 /** Sector marks the end of the a sector-ID chain **/ +#define OLE_SECTORID_SAT -3 /** Sector used by sector allocation Table **/ +#define OLE_SECTORID_MSAT -4 /** Sector used by master sector allocation Table **/ // Main header accessors #define header_id(x) ((x) +0) @@ -1395,25 +1395,13 @@ unsigned char *OLE_load_chain( struct OLE_object *ole, int FAT_sector_start ) can use this in the sanity checking to see if the requested sectors are outside of the possible valid filesize range. - +20220425-mkgrgis: rewrite fili size to seek, exclude FS operations \------------------------------------------------------------------*/ int OLE_open_file( struct OLE_object *ole, char *fullpath ) { struct stat st; int stat_result; FILE *f; - - stat_result = stat(fullpath, &st); - if (stat_result != 0) { - DOLE LOGGER_log("%s:%d:OLE_open_file:ERROR: Cannot locate file '%s' for opening (%s)",FL, fullpath, strerror(errno)); - return OLEER_BAD_INPUT_FILE; - } - - DOLE LOGGER_log("%s:%d:OLE_open_file:DEBUG: File size of %s = %ld",FL, fullpath, st.st_size); - if ((stat_result == 0) && (st.st_size < 512)) return OLEER_NOT_OLE_FILE; - - ole->file_size = st.st_size; - f = fopen(fullpath,"r"); if (f == NULL) { @@ -1425,13 +1413,18 @@ int OLE_open_file( struct OLE_object *ole, char *fullpath ) return -1; } else { ole->f = f; - ole->file_size = st.st_size; + fseek(ole->f, 0L, SEEK_END); + ole->file_size = ftell(ole->f); + fseek(ole->f, 0L, SEEK_SET); + if (ole->file_size < 512) { + fclose(ole->f); + return OLEER_NOT_OLE_FILE; + } ole->last_sector = -1; } return OLE_OK; } - /*-----------------------------------------------------------------\ Function Name : OLE_open_directory Returns Type : int @@ -1596,53 +1589,6 @@ int OLE_terminate_and_return( struct OLE_object *ole, int result ) return result; } - -#ifdef RIPOLE_WALK_TREE -int OLE_walk_tree( struct OLE_object *ole, char *fname, char *decode_path, int depth ) -{ - - /** Sanity check **/ - - if (depth > 100) return 0; - if (ole->total_file_count > 10000) return 0; - if (element_type < 0) return 0; - - switch (element_type) { - - case STGTY_ROOT: - /** ROOT DIRECTORY ENTRY **/ - /** ROOT DIRECTORY ENTRY **/ - /** ROOT DIRECTORY ENTRY **/ - DOLE LOGGER_log("%s:%d:OLE_walk_tree:DEBUG: Loading ministream/SmallBlockArray",FL); - ole->ministream = OLE_load_chain( ole, adir->start_sector ); - if (ole->ministream == NULL) return OLEER_MINISTREAM_READ_FAIL; - DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: ministream done",FL); - } - - - - - - - -} else if (adir->element_type == STGTY_STORAGE) { - /** STORAGE ELEMENT **/ - /** STORAGE ELEMENT **/ - /** STORAGE ELEMENT **/ - DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Item is directory, start child is at index %d\n",FL,i); - ole->ministream = OLE_load_chain( ole, adir->start_sector ); - if (ole->ministream == NULL) return OLEER_MINISTREAM_READ_FAIL; - DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: DIRECTORY ministream done",FL); - - -} -#endif - - - - - - /*-----------------------------------------------------------------\ Date Code: : 20081101-020137 Function Name : OLE_decode_stream From 1684661afbaa760db080289a1c10001e69c39207 Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Fri, 25 Apr 2025 11:04:32 +0300 Subject: [PATCH 30/80] Unify and refactor OLE fragment saving --- Makefile | 3 +- ripOLE/Makefile | 1 - ripOLE/ole.c | 180 ++++++++++++++++++++++-------------------------- ripOLE/ole.h | 34 ++++----- 4 files changed, 98 insertions(+), 120 deletions(-) diff --git a/Makefile b/Makefile index fc3f637..f75bcd4 100644 --- a/Makefile +++ b/Makefile @@ -32,11 +32,10 @@ LIBS= # DEBUGGING Related Flags OBJ=ripmime -RIPOLE_OBJS= ripOLE/ole.o ripOLE/olestream-unwrap.o ripOLE/bytedecoders.o ripOLE/bt-int.o +RIPOLE_OBJS= ripOLE/ole.o ripOLE/olestream-unwrap.o ripOLE/bytedecoders.o ripOLE/bt-int.o #RIPOLE_OBJS= OFILES= strstack.o mime.o ffget.o mime_headers.o tnef/tnef.o rawget.o pldstr.o logger.o libmime-decoders.o boundary-stack.o uuencode.o filename-filters.o $(RIPOLE_OBJS) - default: tnef/tnef.o ripmime ripOLE/ole.o debug: CFLAGS=-Wall -ggdb -DDEBUG -I. -O0 diff --git a/ripOLE/Makefile b/ripOLE/Makefile index 6276e04..7a26f44 100644 --- a/ripOLE/Makefile +++ b/ripOLE/Makefile @@ -2,7 +2,6 @@ OBJS= ole.o olestream-unwrap.o bytedecoders.o logger.o pldstr.o bt-int.o CFLAGS=-Wall -g -O2 -I. - .c.o: $(CC) $(CFLAGS) $(DEFINES) -c $*.c diff --git a/ripOLE/ole.c b/ripOLE/ole.c index 7d10370..1f393dd 100644 --- a/ripOLE/ole.c +++ b/ripOLE/ole.c @@ -13,6 +13,11 @@ #include "olestream-unwrap.h" #include "ole.h" +#include "../ffget.h" +#include "../strstack.h" +#include "../mime_headers.h" +#include "../mime.h" + /** Sector ID values (predefined) **/ #define OLE_SECTORID_FREE -1 /** Unallocated sector **/ #define OLE_SECTORID_ENDOFCHAIN -2 /** Sector marks the end of the a sector-ID chain **/ @@ -1377,6 +1382,33 @@ unsigned char *OLE_load_chain( struct OLE_object *ole, int FAT_sector_start ) return buffer; } +/*-----------------------------------------------------------------\ + Function Name : OLE_input_file_data_ini + Returns Type : int + ----Parameter List + 1. struct OLE_object *ole, + ------------------ + Exit Codes : + Side Effects : + -------------------------------------------------------------------- +Comments: + +-------------------------------------------------------------------- +Changes: +\------------------------------------------------------------------*/ +int OLE_input_file_data_ini( struct OLE_object *ole ) +{ + fseek(ole->f, 0L, SEEK_END); + ole->file_size = ftell(ole->f); + fseek(ole->f, 0L, SEEK_SET); + if (ole->file_size < OLE_HEADER_BLOCK_SIZE) { + fclose(ole->f); + return OLEER_NOT_OLE_FILE; + } + ole->last_sector = -1; + return OLE_OK; +} + /*-----------------------------------------------------------------\ Function Name : OLE_open_file Returns Type : int @@ -1395,34 +1427,21 @@ unsigned char *OLE_load_chain( struct OLE_object *ole, int FAT_sector_start ) can use this in the sanity checking to see if the requested sectors are outside of the possible valid filesize range. -20220425-mkgrgis: rewrite fili size to seek, exclude FS operations +20220425-mkgrgis: rewrite file size to seek, exclude FS operations \------------------------------------------------------------------*/ int OLE_open_file( struct OLE_object *ole, char *fullpath ) { - struct stat st; - int stat_result; - FILE *f; - f = fopen(fullpath,"r"); - if (f == NULL) + ole->f = fopen(fullpath,"r"); + if (ole->f == NULL) { - ole->f = NULL; if (ole->quiet == 0) { LOGGER_log("%s:%d:OLE_open_file:ERROR:Cannot open %s for reading (%s)\n",FL,fullpath, strerror(errno)); } return -1; - } else { - ole->f = f; - fseek(ole->f, 0L, SEEK_END); - ole->file_size = ftell(ole->f); - fseek(ole->f, 0L, SEEK_SET); - if (ole->file_size < 512) { - fclose(ole->f); - return OLEER_NOT_OLE_FILE; - } - ole->last_sector = -1; } - return OLE_OK; + else + return OLE_input_file_data_ini(ole); } /*-----------------------------------------------------------------\ @@ -1508,29 +1527,18 @@ int OLE_store_stream( struct OLE_object *ole, char *stream_name, char *directory LOGGER_log("%s:%d:OLE_store_stream:ERROR: Cannot compose full filename string from '%s' and '%s'", FL, directory, stream_name); return -1; } else { - FILE *f; - - f = fopen(full_path,"w"); - if (f == NULL) + MIME_element* mime_el = MIME_element_add_with_path (full_path, NULL, NULL); + size_t written_bytes = fwrite( stream, 1, stream_size, mime_el->f ); + if (written_bytes != stream_size) { - LOGGER_log("%s:%d:OLE_store_stream:ERROR: Cannot open %s for writing (%s)",FL, full_path, strerror(errno)); - if (full_path) free(full_path); - return -1; - } else { - size_t written_bytes; - - written_bytes = fwrite( stream, 1, stream_size, f ); - if (written_bytes != stream_size) - { - LOGGER_log("%s:%d:OLE_store_stream:WARNING: Only wrote %d of %d bytes to file %s",FL,written_bytes,stream_size,full_path); - } - fclose(f); + LOGGER_log("%s:%d:OLE_store_stream:WARNING: Only wrote %d of %d bytes to file %s",FL,written_bytes,stream_size,full_path); + } + MIME_element_remove (mime_el); - if ((OLE_VNORMAL(ole->verbose))&&(ole->filename_report_fn != NULL)) - { - ole->filename_report_fn( stream_name ); - } - } // if file is valid + if ((OLE_VNORMAL(ole->verbose))&&(ole->filename_report_fn != NULL)) + { + ole->filename_report_fn( stream_name ); + } } // if full_path is valid if (full_path) free(full_path); @@ -1686,39 +1694,12 @@ int OLE_decode_stream( struct OLE_object *ole, struct OLE_directory_entry *adir return result; } -/*-----------------------------------------------------------------\ - Function Name : OLE_decode_file - Returns Type : int - ----Parameter List - 1. char *fname, - 2. char *decode_path , - ------------------ - Exit Codes : - Side Effects : - -------------------------------------------------------------------- -Comments: - --------------------------------------------------------------------- -Changes: - -\------------------------------------------------------------------*/ -int OLE_decode_file( struct OLE_object *ole, char *fname, char *decode_path ) +int OLE_decode( struct OLE_object *ole, char *decode_path ) { unsigned char *current_property, *property_limit; int result = 0; int i; - // Reject any bad paramters. - if (ole == NULL) return OLEER_DECODE_NULL_OBJECT; - if (fname == NULL) return OLEER_DECODE_NULL_FILENAME; - if (decode_path == NULL) return OLEER_DECODE_NULL_PATH; - - // We need to gain access to the OLE2 data file, without - // this pretty much everything is pointless. - DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: opening %s", FL, fname ); - result = OLE_open_file( ole, fname ); - if (result != 0) return result; - // Try create the output directory which we're using // to write the decoded files out to. DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: opening output directory %s", FL, decode_path); @@ -1785,49 +1766,27 @@ int OLE_decode_file( struct OLE_object *ole, char *fname, char *decode_path ) { DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: breaking out due to element type %d",FL, adir->element_type); break; - } else if (adir->element_type == STGTY_ROOT){ - /** ROOT DIRECTORY ENTRY **/ - /** ROOT DIRECTORY ENTRY **/ /** ROOT DIRECTORY ENTRY **/ DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Loading ministream/SmallBlockArray",FL); ole->ministream = OLE_load_chain( ole, adir->start_sector ); if (ole->ministream == NULL) return OLEER_MINISTREAM_READ_FAIL; DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: ministream done",FL); - - - - - - - } else if (adir->element_type == STGTY_STORAGE) { - /** STORAGE ELEMENT **/ - /** STORAGE ELEMENT **/ /** STORAGE ELEMENT **/ DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Item is directory, start child is at index %d\n",FL,i); ole->ministream = OLE_load_chain( ole, adir->start_sector ); if (ole->ministream == NULL) return OLEER_MINISTREAM_READ_FAIL; DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: DIRECTORY ministream done",FL); - - - - } else if (adir->element_type == STGTY_STREAM) { - /** STREAM ELEMENT **/ - /** STREAM ELEMENT **/ /** STREAM ELEMENT **/ OLE_decode_stream( ole, adir, decode_path ); - - - } else { /** If the element isn't of the above types then it's possibly ** an empty element or just one used for the MSAT/SAT ** either way we just step over it and carry on **/ DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Element type %d does not need to be handled",FL,adir->element_type); } - // Jump to the next property record, which // is always 128 bytes ahead. current_property += 128; @@ -1837,14 +1796,41 @@ int OLE_decode_file( struct OLE_object *ole, char *fname, char *decode_path ) DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Finished",FL); - /* - //if (ole->f) fclose(ole->f); - fclose(ole->f); - if (ole->FAT) free(ole->FAT); - if (ole->miniFAT) free(ole->miniFAT); - if (ole->ministream) free(ole->ministream); - if (ole->properties) free(ole->properties); + /* OLE_decode_file_done(ole); */ - return OLE_OK; } + +/*-----------------------------------------------------------------\ + Function Name : OLE_decode_file + Returns Type : int + ----Parameter List + 1. char *fname, + 2. char *decode_path , + ------------------ + Exit Codes : + Side Effects : + -------------------------------------------------------------------- +Comments: + +-------------------------------------------------------------------- +Changes: + +\------------------------------------------------------------------*/ +int OLE_decode_file( struct OLE_object *ole, char *fname, char *decode_path ) +{ + int result = 0; + + // Reject any bad paramters. + if (ole == NULL) return OLEER_DECODE_NULL_OBJECT; + if (fname == NULL) return OLEER_DECODE_NULL_FILENAME; + if (decode_path == NULL) return OLEER_DECODE_NULL_PATH; + + // We need to gain access to the OLE2 data file, without + // this pretty much everything is pointless. + DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: opening %s", FL, fname ); + result = OLE_open_file( ole, fname ); + if (result != 0) return result; + + return OLE_decode( ole, decode_path ); +} diff --git a/ripOLE/ole.h b/ripOLE/ole.h index f4d1cca..ae14894 100644 --- a/ripOLE/ole.h +++ b/ripOLE/ole.h @@ -10,26 +10,26 @@ #define OLEER_NOT_OLE_FILE 102 #define OLEER_INSANE_OLE_FILE 103 -#define OLEER_DECODE_NULL_OBJECT 10 +#define OLEER_DECODE_NULL_OBJECT 10 #define OLEER_DECODE_NULL_FILENAME 11 #define OLEER_DECODE_NULL_PATH 12 #define OLEER_MINIFAT_READ_FAIL 30 #define OLEER_PROPERTIES_READ_FAIL 31 #define OLEER_MINISTREAM_READ_FAIL 32 -#define OLEER_MINISTREAM_STREAM_READ_FAIL 33 -#define OLEER_NORMALSTREAM_STREAM_READ_FAIL 34 +#define OLEER_MINISTREAM_STREAM_READ_FAIL 33 +#define OLEER_NORMALSTREAM_STREAM_READ_FAIL 34 -#define OLEER_GET_BLOCK_SEEK 41 -#define OLEER_GET_BLOCK_READ 42 +#define OLEER_GET_BLOCK_SEEK 41 +#define OLEER_GET_BLOCK_READ 42 -#define OLEER_MEMORY_OVERFLOW 50 +#define OLEER_MEMORY_OVERFLOW 50 -#define OLE_VERBOSE_NORMAL 1 -#define OLE_VERBOSE_FATREAD 2 -#define OLE_VERBOSE_DIRREAD 4 -#define OLE_VERBOSE_STREAMREAD 8 -#define OLE_VERBOSE_STREAMDECODE 16 +#define OLE_VERBOSE_NORMAL 1 +#define OLE_VERBOSE_FATREAD 2 +#define OLE_VERBOSE_DIRREAD 4 +#define OLE_VERBOSE_STREAMREAD 8 +#define OLE_VERBOSE_STREAMDECODE 16 #define OLE_VNORMAL(x) ((x) && OLE_VERBOSE_NORMAL == OLE_VERBOSE_NORMAL ) @@ -39,7 +39,6 @@ #define OLE_DNORMAL(x) ((x) && OLE_DEBUG_NORMAL == OLE_DEBUG_NORMAL) #define OLE_DPEDANTIC(x) ((x) && OLE_DEBUG_PEDANTIC == OLE_DEBUG_PEDANTIC) - #define OLE_HEADER_FAT_SECTOR_COUNT_LIMIT 109 struct OLE_header { unsigned int minor_version; @@ -59,9 +58,9 @@ struct OLE_header { unsigned int FAT[OLE_HEADER_FAT_SECTOR_COUNT_LIMIT]; }; -#define OLE_DIRECTORY_ELEMENT_NAME_SIZE 64 -#define OLE_DIRECTORY_CLASS_SIZE 16 -#define OLE_DIRECTORY_TIMESTAMPS_SIZE 16 +#define OLE_DIRECTORY_ELEMENT_NAME_SIZE 64 +#define OLE_DIRECTORY_CLASS_SIZE 16 +#define OLE_DIRECTORY_TIMESTAMPS_SIZE 16 struct OLE_directory_entry { char element_name[OLE_DIRECTORY_ELEMENT_NAME_SIZE]; int element_name_byte_count; @@ -80,11 +79,9 @@ struct OLE_directory_entry { unsigned int stream_size; }; - #define OLE_HEADER_BLOCK_SIZE 512 struct OLE_object { - int error; size_t file_size; int last_sector; @@ -116,11 +113,8 @@ struct OLE_object { int decode_normal_streams; int (*filename_report_fn)(char *); - }; - - // Prototypes int OLE_version( void ); From b5394ef7d01494007be264740501f3bcb2f138cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Sat, 26 Apr 2025 07:10:36 +0300 Subject: [PATCH 31/80] Code cleanup, refactor debug in mime.c to C99 --- libmime-decoders.c | 2 +- mime.c | 362 ++++++++++++++++++++++----------------------- 2 files changed, 174 insertions(+), 190 deletions(-) diff --git a/libmime-decoders.c b/libmime-decoders.c index 8b51f4e..87b7d2a 100644 --- a/libmime-decoders.c +++ b/libmime-decoders.c @@ -29,7 +29,7 @@ /* our base 64 decoder table */ static unsigned char b64[256]={ - 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\ + 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 62, 128, 128, 128, 63,\ 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 128, 128, 128, 0, 128, 128,\ diff --git a/mime.c b/mime.c index 3e7974f..f768343 100644 --- a/mime.c +++ b/mime.c @@ -68,7 +68,7 @@ int MIME_handle_rfc822( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M #define MIME_HEADERS_FILENAME "_headers_" #ifndef FL -#define FL __FILE__, __LINE__ +#define FL __FILE__, __LINE__, __func__ #endif #define _ENC_UNKNOWN 0 @@ -91,8 +91,6 @@ int MIME_handle_rfc822( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M #define _MIME_WRITE_BUFFER_SIZE (8 *1024) #define _MIME_WRITE_BUFFER_LIMIT (_MIME_WRITE_BUFFER_SIZE -4) - - // Debug precodes #define MIME_DPEDANTIC ((glb.debug >= _MIME_DEBUG_PEDANTIC)) #define MIME_DNORMAL ((glb.debug >= _MIME_DEBUG_NORMAL )) @@ -100,8 +98,6 @@ int MIME_handle_rfc822( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M #define MIME_VERBOSE_12 ((glb.verbosity_12x_style > 0 )) #define DMIME if (glb.debug >= _MIME_DEBUG_NORMAL) -#define FL __FILE__,__LINE__ - /* our base 64 decoder table */ static unsigned char b64[256]={ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\ @@ -760,7 +756,7 @@ int MIME_set_blankzone_save_option( int option ) break; default: - LOGGER_log("%s:%d:MIME_set_blankzone_save_option:WARNING: Unknown option for saving method (%d). Setting to '%s'",FL, option, MIME_BLANKZONE_FILENAME_DEFAULT ); + LOGGER_log("%s:%d:%s:WARNING: Unknown option for saving method (%d). Setting to '%s'",FL, option, MIME_BLANKZONE_FILENAME_DEFAULT ); glb.blankzone_save_option = MIME_BLANKZONE_SAVE_FILENAME; snprintf( glb.blankzone_filename, _MIME_STRLEN_MAX, "%s", MIME_BLANKZONE_FILENAME_DEFAULT ); } @@ -881,7 +877,7 @@ int MIME_set_renamemethod( int method ) } else { - LOGGER_log("%s:%d:MIME_set_renamemethod:ERROR: selected method not within %d > x > %d range", FL, _MIME_RENAME_METHOD_INFIX, _MIME_RENAME_METHOD_POSTFIX ); + LOGGER_log("%s:%d:%s:ERROR: selected method not within %d > x > %d range", FL, _MIME_RENAME_METHOD_INFIX, _MIME_RENAME_METHOD_POSTFIX ); return -1; } @@ -930,18 +926,6 @@ int get_random_value(void) { return randval; } -/*------------------------------------------------------------------------ -Procedure: MIME_fprintf_decoded -Purpose: Print meatadata of decoded fragment. -Input: -Output: -Errors: -------------------------------------------------------------------------*/ -void MIME_fprintf_decoded( RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo) -{ - fprintf (stdout, "%d|%d|%d|%d|%s|%s\n", glb.mime_count, glb.attachment_count, glb.filecount, hinfo->current_recursion_level, hinfo->content_type_string, hinfo->filename); -} - /*------------------------------------------------------------------------ Procedure: MIME_test_uniquename ID:1 Purpose: Checks to see that the filename specified is unique. If it's not @@ -965,7 +949,7 @@ int MIME_test_uniquename( char *path, char *fname, int method ) int randval = get_random_value(); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_test_uniquename:DEBUG: Start (%s)",FL,fname); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Start (%s)",FL,fname); frontname = extention = NULL; // shuts the compiler up @@ -1042,7 +1026,7 @@ int MIME_test_uniquename( char *path, char *fname, int method ) PLD_strncpy(fname, frontname, _MIMEH_FILENAMELEN_MAX); //FIXME - this assumes that the buffer space is at least MIME_STRLEN_MAX sized. } - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_test_uniquename:DEBUG: Done (%s)",FL,fname); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done (%s)",FL,fname); return 0; } @@ -1080,7 +1064,7 @@ int MIME_is_file_RFC822( FILE *f ) line = malloc(sizeof(char) *1025); if (!line) { - LOGGER_log("%s:%d:MIME_is_file_mime:ERROR: cannot allocate memory for read buffer", FL); + LOGGER_log("%s:%d:%s:ERROR: cannot allocate memory for read buffer", FL); return -1; } @@ -1095,16 +1079,16 @@ int MIME_is_file_RFC822( FILE *f ) for (result = 0; result < 12; result++) { /** Test for every possible MIME header prefix, ie, From: Subject: etc **/ - if (MIME_DPEDANTIC) LOGGER_log("%s:%d:MIME_is_file_mime:DEBUG: Testing for '%s' in '%s'", FL, line, conditions[result]); + if (MIME_DPEDANTIC) LOGGER_log("%s:%d:%s:DEBUG: Testing for '%s' in '%s'", FL, line, conditions[result]); if (strncasecmp(line,conditions[result],strlen(conditions[result]))==0) { /** If we get a match, then increment the hit counter **/ hitcount++; - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_is_file_RFC822:DEBUG: Hit on %s",FL,conditions[result]); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Hit on %s",FL,conditions[result]); if (result == 11) { flag_mime_version = 1; - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_is_file_RFC822:DEBUG: Find 'MIME Version' field",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Find 'MIME Version' field",FL); } } } @@ -1117,7 +1101,7 @@ int MIME_is_file_RFC822( FILE *f ) if (line) free(line); if (MIME_DNORMAL) - LOGGER_log("%s:%d:MIME_is_file_RFC822:DEBUG: Hit count = %d, result = %d",FL,hitcount,result); + LOGGER_log("%s:%d:%s:DEBUG: Hit count = %d, result = %d",FL,hitcount,result); return result; } @@ -1134,14 +1118,14 @@ int MIME_is_diskfile_RFC822( char *fname ) int result = 0; FILE *f; - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_is_diskfile_RFC822:DEBUG: Testing %s for RFC822 headers",FL,fname); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Testing %s for RFC822 headers",FL,fname); f = fopen(fname,"r"); if (!f) { if (glb.quiet == 0) { - LOGGER_log("%s:%d:MIME_is_diskfile_RFC822:ERROR: cannot open file '%s' for reading (%s)", FL, fname,strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: cannot open file '%s' for reading (%s)", FL, fname,strerror(errno)); } return 0; } @@ -1195,7 +1179,7 @@ int MIME_decode_TNEF( RIPMIME_output *unpack_metadata, struct MIMEH_header_info // result = remove( fullpath ); if (result == -1) { - if (MIME_VERBOSE) LOGGER_log("%s:%d:MIME_decode_TNEF: Removing %s/%s failed (%s)",FL,hinfo->filename, unpack_metadata->dir,strerror(errno)); + if (MIME_VERBOSE) LOGGER_log("%s:%d:%s: Removing %s/%s failed (%s)",FL,hinfo->filename, unpack_metadata->dir,strerror(errno)); } } return result; @@ -1240,12 +1224,12 @@ int MIME_decode_OLE( RIPMIME_output *unpack_metadata, struct MIMEH_header_info * OLE_set_save_unknown_streams(&ole,0); OLE_set_filename_report_fn(&ole, MIME_report_filename_decoded_RIPOLE ); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_OLE:DEBUG: Starting OLE Decode",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Starting OLE Decode",FL); result = OLE_decode_file(&ole, fullpath, unpack_metadata->dir ); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_OLE:DEBUG: Decode done, cleaning up.",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decode done, cleaning up.",FL); OLE_decode_file_done(&ole); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_OLE:DEBUG: Decode returned with code = %d",FL,result); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decode returned with code = %d",FL,result); return result; } #endif @@ -1253,7 +1237,7 @@ int MIME_decode_OLE( RIPMIME_output *unpack_metadata, struct MIMEH_header_info * MIME_element* MIME_element_add_with_path (char* fullpath, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo) { MIME_element *cur = malloc(sizeof(MIME_element)); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_element_add:start\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:start\n",FL); insertItem(glb.mime_arr, cur); cur->hinfo = hinfo; @@ -1262,11 +1246,11 @@ MIME_element* MIME_element_add_with_path (char* fullpath, RIPMIME_output *unpack cur->f = fopen(cur->fullpath,"wb"); if (cur->f == NULL) { - LOGGER_log("%s:%d:MIME_element_add:ERROR: cannot open %s for writing",FL,cur->fullpath); + LOGGER_log("%s:%d:%s:ERROR: cannot open %s for writing",FL,cur->fullpath); return cur; } - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_element_add:DEBUG: Decoding [encoding=%d] to %s\n",FL, hinfo->content_transfer_encoding, cur->fullpath); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding [encoding=%d] to %s\n",FL, hinfo->content_transfer_encoding, cur->fullpath); if (unpack_metadata != NULL && unpack_metadata->unpack_mode == RIPMIME_UNPACK_MODE_LIST_FILES && cur->f != NULL) { fprintf (stdout, "%d|%d|%d|%d|%s|%s\n", glb.mime_count, glb.attachment_count, glb.filecount, hinfo->current_recursion_level, hinfo->content_type_string, hinfo->filename); @@ -1285,7 +1269,7 @@ MIME_element* MIME_element_add (RIPMIME_output *unpack_metadata, struct MIMEH_he void MIME_element_remove (MIME_element* cur) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_element_remove:start\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:start\n",FL); if (cur->f != NULL) { fclose(cur->f); @@ -1318,44 +1302,44 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME * --binary flag */ - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Start\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Start\n",FL); cur_mime = MIME_element_add (unpack_metadata, hinfo); while ((readcount=FFGET_raw(f, (unsigned char *) buffer,bufsize)) > 0) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: BUFFER[%p]= '%s'\n",FL,buffer, buffer); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: BUFFER[%p]= '%s'\n",FL,buffer, buffer); if ((!file_has_uuencode)&&(UUENCODE_is_uuencode_header( buffer ))) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: UUENCODED is YES (buffer=[%p]\n",FL,buffer); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: UUENCODED is YES (buffer=[%p]\n",FL,buffer); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: File contains UUENCODED data(%s)\n",FL,buffer); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: File contains UUENCODED data(%s)\n",FL,buffer); file_has_uuencode = 1; } if (BS_cmp(buffer, readcount)) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Boundary located - breaking out.\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Boundary located - breaking out.\n",FL); break; } else { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: writing: %s\n",FL, buffer); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: writing: %s\n",FL, buffer); fwrite( buffer, readcount, 1, cur_mime->f); } } - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Completed reading RAW data\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Completed reading RAW data\n",FL); free(buffer); MIME_element_remove(cur_mime); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Closed file and free'd buffer\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Closed file and free'd buffer\n",FL); // If there was UUEncoded portions [potentially] in the email, the // try to extract them using the MIME_decode_uu() if (file_has_uuencode) { char full_decode_path[512]; snprintf(full_decode_path,sizeof(full_decode_path),"%s/%s",unpack_metadata->dir,hinfo->filename); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Decoding UUencoded data\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UUencoded data\n",FL); if ( hinfo->content_transfer_encoding == _CTRANS_ENCODING_UUENCODE ) decode_entire_file = 0; //result = UUENCODE_decode_uu(NULL, hinfo->filename, hinfo->uudec_name, decode_entire_file, keep ); @@ -1366,17 +1350,17 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME case UUENCODE_STATUS_SHORT_FILE: case UUENCODE_STATUS_CANNOT_OPEN_FILE: case UUENCODE_STATUS_CANNOT_FIND_FILENAME: - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Nullifying uuencode_error result %d",FL, uuencode_error); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Nullifying uuencode_error result %d",FL, uuencode_error); result = 0; break; case UUENCODE_STATUS_CANNOT_ALLOCATE_MEMORY: - LOGGER_log("%s:%d:MIME_decode_raw:ERROR: Failure to allocate memory for UUdecode process",FL); + LOGGER_log("%s:%d:%s:ERROR: Failure to allocate memory for UUdecode process",FL); return -1; break; default: - LOGGER_log("%s:%d:MIME_decode_raw:ERROR: Unknown return code from UUDecode process [%d]",FL,uuencode_error); + LOGGER_log("%s:%d:%s:ERROR: Unknown return code from UUDecode process [%d]",FL,uuencode_error); return -1; } } @@ -1386,15 +1370,15 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME { if (strcasecmp(hinfo->uudec_name,"winmail.dat")==0) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Decoding TNEF format\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding TNEF format\n",FL); snprintf(hinfo->filename, 128, "%s", hinfo->uudec_name); MIME_decode_TNEF( unpack_metadata, hinfo, keep); } - else LOGGER_log("%s:%d:MIME_decode_raw:WARNING: hinfo has been clobbered.\n",FL); + else LOGGER_log("%s:%d:%s:WARNING: hinfo has been clobbered.\n",FL); } } - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: End[result = %d]\n",FL,result); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: End[result = %d]\n",FL,result); return result; } @@ -1418,14 +1402,14 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM int decodesize=0; MIME_element* cur_mime = NULL; - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Decoding TEXT [encoding=%d] to %s\n",FL, hinfo->content_transfer_encoding, hinfo->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding TEXT [encoding=%d] to %s\n",FL, hinfo->content_transfer_encoding, hinfo->filename); cur_mime = MIME_element_add (unpack_metadata, hinfo); if (!f) { /** If we cannot open the file for reading, leave an error and return -1 **/ - LOGGER_log("%s:%d:MIME_decode_text:ERROR: print-quotable input stream broken.",FL); + LOGGER_log("%s:%d:%s:ERROR: print-quotable input stream broken.",FL); return _EXITERR_MIMEREAD_CANNOT_OPEN_INPUT; } if (f) @@ -1434,8 +1418,8 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM { int line_len = strlen(line); linecount++; - // if (MIME_DPEDANTIC) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: line=%s",FL,line); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: line[len=%d]=%s",FL,line_len,line); + // if (MIME_DPEDANTIC) LOGGER_log("%s:%d:%s:DEBUG: line=%s",FL,line); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: line[len=%d]=%s",FL,line_len,line); //20041217-1529:PLD: if (line[0] == '-') { @@ -1463,14 +1447,14 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM if ((!file_has_uuencode)&&( UUENCODE_is_uuencode_header( line ))) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: UUENCODED data located in file.\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: UUENCODED data located in file.\n",FL); file_has_uuencode = 1; } } // linecount++; if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_DNORMAL:DEBUG: End processing line.",FL); } // while - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Done writing output file '%s'...now attempting to close.",FL, cur_mime->fullpath); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done writing output file '%s'...now attempting to close.",FL, cur_mime->fullpath); MIME_element_remove(cur_mime); @@ -1479,7 +1463,7 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM result = MIME_STATUS_ZERO_FILE; return result; } - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Closed.",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Closed.",FL); } // if main input file stream was open // If our input from the file was invalid due to EOF or other @@ -1487,7 +1471,7 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM // occured. // if (!get_result) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: FFGET module ran out of file data while attempting to decode",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: FFGET module ran out of file data while attempting to decode",FL); // result = -1; result = MIME_ERROR_FFGET_EMPTY; // 20040305-1323:PLD } @@ -1503,7 +1487,7 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM // Make sure uudec_name is blank too // hinfo->uudec_name[0] = '\0'; - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Decoding UUencoded data in file '%s'\n",FL,hinfo->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UUencoded data in file '%s'\n",FL,hinfo->filename); //result = UUENCODE_decode_uu( NULL, info->filename, hinfo->uudec_name, 1, keep ); // Attempt to decode the UUENCODED data in the file, // NOTE - hinfo->uudec_name is a blank buffer which will be filled by the UUENCODE_decode_uu @@ -1521,30 +1505,30 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM case UUENCODE_STATUS_SHORT_FILE: case UUENCODE_STATUS_CANNOT_OPEN_FILE: case UUENCODE_STATUS_CANNOT_FIND_FILENAME: - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_raw:DEBUG: Nullifying uuencode_error result %d",FL, uuencode_error); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Nullifying uuencode_error result %d",FL, uuencode_error); result = 0; break; case UUENCODE_STATUS_CANNOT_ALLOCATE_MEMORY: - LOGGER_log("%s:%d:MIME_decode_raw:ERROR: Failure to allocate memory for UUdecode process",FL); + LOGGER_log("%s:%d:%s:ERROR: Failure to allocate memory for UUdecode process",FL); return -1; break; default: - LOGGER_log("%s:%d:MIME_decode_raw:ERROR: Unknown return code from UUDecode process [%d]",FL,uuencode_error); + LOGGER_log("%s:%d:%s:ERROR: Unknown return code from UUDecode process [%d]",FL,uuencode_error); return -1; } } if ( result > 0 ) { glb.attachment_count += result; result = 0; } - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: hinfo = %p\n",FL,hinfo); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Done. [ UUName = '%s' ]\n",FL,hinfo->uudec_name); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: hinfo = %p\n",FL,hinfo); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done. [ UUName = '%s' ]\n",FL,hinfo->uudec_name); if (strncasecmp(hinfo->uudec_name,"winmail.dat",11)==0) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Decoding TNEF format\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding TNEF format\n",FL); snprintf(hinfo->filename, 128, "%s", hinfo->uudec_name); MIME_decode_TNEF( unpack_metadata, hinfo, keep ); } - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: Completed decoding UUencoded data.\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Completed decoding UUencoded data.\n",FL); } - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_text:DEBUG: result=%d ----------------Done\n",FL,result); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: result=%d ----------------Done\n",FL,result); return result; } @@ -1588,7 +1572,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH int loop; MIME_element* cur_mime = NULL; - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_64:DEBUG: attempting to decode '%s'", FL, hinfo->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: attempting to decode '%s'", FL, hinfo->filename); cur_mime = MIME_element_add (unpack_metadata, hinfo); if (cur_mime->f == NULL) @@ -1601,7 +1585,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH writebuffer = malloc( _MIME_WRITE_BUFFER_SIZE *sizeof(unsigned char)); if (!writebuffer) { - LOGGER_log("%s:%d:MIME_decode_64:ERROR: cannot allocate %dbytes of memory for the write buffer",FL, _MIME_WRITE_BUFFER_SIZE); + LOGGER_log("%s:%d:%s:ERROR: cannot allocate %dbytes of memory for the write buffer",FL, _MIME_WRITE_BUFFER_SIZE); return -1; } else { @@ -1611,7 +1595,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH /* Set our ignore_crcount flag */ if (BS_count() > 0) { - //LOGGER_log("%s:%d:MIME_decode_64:DEBUG: Ignore CR set to 1",FL); + //LOGGER_log("%s:%d:%s:DEBUG: Ignore CR set to 1",FL); ignore_crcount = 1; } /* collect prefixing trash (if any, such as spaces, CR's etc)*/ @@ -1670,7 +1654,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH { int hit = 0; char *p; - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_64:DEBUG: leader '-' found at %50s",FL,(f->startpoint -1)); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: leader '-' found at %50s",FL,(f->startpoint -1)); p = strchr((f->startpoint -1), '\n'); if (p == NULL) { /* The boundary test was failing because sometimes the full line @@ -1679,16 +1663,16 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH */ char scratch[1024]; FFGET_fgets(scratch,sizeof(scratch), f); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_64:DEBUG: Scratch = '%s'", FL, scratch); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Scratch = '%s'", FL, scratch); hit = BS_cmp(scratch,strlen(scratch) +1); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_64:DEBUG: Boundary hit = %d", FL, hit); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Boundary hit = %d", FL, hit); } else { *p = '\0'; hit = BS_cmp((f->startpoint -1),strlen(f->startpoint) +1); *p = '\n'; } if (hit > 0) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_64:DEBUG: Boundary detected and breaking out ",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Boundary detected and breaking out ",FL); // FFGET_fgets(scratch,sizeof(scratch),f); // eom_reached = 1; boundary_crash = 1; @@ -1723,7 +1707,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH // CR's ). if (cr_count > 2) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_64:DEBUG: EOF Reached due to two consecutive CR's on line %d\n",FL,cr_total); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: EOF Reached due to two consecutive CR's on line %d\n",FL,cr_total); eom_reached++; break; } @@ -1739,7 +1723,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH /* if we get an EOF char, then we know something went wrong */ if ( c == EOF ) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_64:DEBUG: input stream broken for base64 decoding for file %s. %ld bytes of data in buffer to be written out\n",FL,hinfo->filename,wbcount); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: input stream broken for base64 decoding for file %s. %ld bytes of data in buffer to be written out\n",FL,hinfo->filename,wbcount); status = MIME_ERROR_B64_INPUT_STREAM_EOF; fwrite(writebuffer, 1, wbcount, cur_mime->f); MIME_element_remove(cur_mime); @@ -1768,7 +1752,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH // So, now we -absorb- till the end of the line using FFGET_fgets() stopcount = 4 -i; FFGET_fgets(scratch,sizeof(scratch),f); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_64:DEBUG: Stop char detected pos=%d...StopCount = %d\n",FL,i,stopcount); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Stop char detected pos=%d...StopCount = %d\n",FL,i,stopcount); i = 4; break; // out of FOR. } @@ -1822,7 +1806,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH // tally up our total byte conversion count bytecount+=(3 -stopcount); } - else if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_64:DEBUG: could not attain 4 bytes input\n",FL); + else if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: could not attain 4 bytes input\n",FL); // if we wrote less than 3 chars, it means we were at the end of the encoded file thus we exit if ((eom_reached)||(stopcount > 0)||(boundary_crash)||(i!=4)) @@ -1846,7 +1830,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH // Absorb to end of line status = MIME_BASE64_STATUS_HIT_BOUNDARY; // was _BOUNDARY_CRASH } - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_64:DEBUG: File size = %ld bytes, Exit Status = %d, Boundary Crash = %d\n",FL, bytecount, status, boundary_crash); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: File size = %ld bytes, Exit Status = %d, Boundary Crash = %d\n",FL, bytecount, status, boundary_crash); if (writebuffer) free(writebuffer); return status; } // if End-of-MIME or Stopchars appeared @@ -1910,11 +1894,11 @@ int MIME_doubleCR_decode( char *filename, RIPMIME_output *unpack_metadata, struc p = filename; // * Initialise the header fields h.uudec_name[0] = '\0'; - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_doubleCR_decode:DEBUG: filename=%s, path=%s, recursion=%d", FL, filename, unpack_metadata->dir, current_recursion_level ); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: filename=%s, path=%s, recursion=%d", FL, filename, unpack_metadata->dir, current_recursion_level ); memcpy(&h, hinfo, sizeof(h)); // Works for ripMIME snprintf(h.filename, sizeof(h.filename), "%s/%s", unpackdir, p); snprintf(h.filename, sizeof(h.filename), "%s", p); /// Works for Xamime - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_doubleCR_decode:DEBUG: header.filename = %s", FL, h.filename ); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: header.filename = %s", FL, h.filename ); if (MIME_is_diskfile_RFC822(filename)) { if (MIME_VERBOSE) LOGGER_log("Attempting to decode Double-CR delimeted MIME attachment '%s'\n",filename); @@ -1961,7 +1945,7 @@ size_t MIME_read_raw( char *src_mpname, char *dest_mpname, size_t rw_buffer_size } else { input_f = fopen(src_mpname, "r"); if (input_f == NULL) { - LOGGER_log("%s:%d:MIME_read_raw:ERROR: Cannot open '%s' for reading (%s)", FL, src_mpname, strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: Cannot open '%s' for reading (%s)", FL, src_mpname, strerror(errno)); return -1; } } @@ -1969,14 +1953,14 @@ size_t MIME_read_raw( char *src_mpname, char *dest_mpname, size_t rw_buffer_size rw_buffer = malloc( (rw_buffer_size +1) *sizeof(char) ); if ( !rw_buffer ) { - LOGGER_log("%s:%d:MIME_read:ERROR: could not allocate %d of memory for file read buffer\n",FL, rw_buffer_size ); + LOGGER_log("%s:%d:%s:ERROR: could not allocate %d of memory for file read buffer\n",FL, rw_buffer_size ); return -1; } /* open up our input file */ result_f = fopen(dest_mpname, "w"); if (result_f == NULL) { - LOGGER_log("%s:%d:MIME_read_raw:ERROR: Cannot open '%s' for writing. (%s)",FL, dest_mpname, strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: Cannot open '%s' for writing. (%s)",FL, dest_mpname, strerror(errno)); return -1; } @@ -1987,12 +1971,12 @@ size_t MIME_read_raw( char *src_mpname, char *dest_mpname, size_t rw_buffer_size if (readcount > 0) { writecount = fwrite(rw_buffer, readcount, 1, result_f ); if (writecount == -1) { - LOGGER_log("%s:%d:MIME_read_raw:ERROR: While attempting to write data to '%s' (%s)", FL, dest_mpname, strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: While attempting to write data to '%s' (%s)", FL, dest_mpname, strerror(errno)); return -1; } if ( readcount != writecount ) { - LOGGER_log("%s:%d:MIME_read_raw:ERROR: Attempted to write %d bytes, but only managed %d to file '%s'",FL, readcount, writecount, dest_mpname ); + LOGGER_log("%s:%d:%s:ERROR: Attempted to write %d bytes, but only managed %d to file '%s'",FL, readcount, writecount, dest_mpname ); } fsize += writecount; } @@ -2002,7 +1986,7 @@ size_t MIME_read_raw( char *src_mpname, char *dest_mpname, size_t rw_buffer_size fclose(input_f); } if (readcount == -1) { - LOGGER_log("%s:%d:MIME_read_raw:ERROR: read() '%s'",FL, strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: read() '%s'",FL, strerror(errno)); return -1; } fclose(result_f); @@ -2033,7 +2017,7 @@ int MIME_read( char *mpname ) buffer = malloc( MIME_MIME_READ_BUFFER_SIZE *sizeof(char) ); if ( !buffer ) { - LOGGER_log("%s:%d:MIME_read:ERROR: could not allocate 4K of memory for file read buffer\n",FL ); + LOGGER_log("%s:%d:%s:ERROR: could not allocate 4K of memory for file read buffer\n",FL ); return -1; } /* open up our input file */ @@ -2041,7 +2025,7 @@ int MIME_read( char *mpname ) /* check that out file opened up okay */ if (!fout) { - LOGGER_log("%s:%d:MIME_read:ERROR: Cannot open file %s for writing... check permissions perhaps?",FL,mpname); + LOGGER_log("%s:%d:%s:ERROR: Cannot open file %s for writing... check permissions perhaps?",FL,mpname); //exit(_EXITERR_MIMEREAD_CANNOT_OPEN_OUTPUT); return -1; } @@ -2060,7 +2044,7 @@ int MIME_read( char *mpname ) if ( readcount != writecount ) { - LOGGER_log("%s:%d:MIME_read:ERROR: Attempted to write %d bytes, but only managed %d to file '%s'",FL, readcount, writecount, mpname ); + LOGGER_log("%s:%d:%s:ERROR: Attempted to write %d bytes, but only managed %d to file '%s'",FL, readcount, writecount, mpname ); } } else { @@ -2185,7 +2169,7 @@ int MIME_generate_multiple_hardlink_filenames(struct MIMEH_header_info *hinfo, R { if (errno != EEXIST) { - LOGGER_log("%s:%d:MIME_generate_multiple_hardlink_filenames:WARNING: While trying to create '%s' link to '%s' (%s)",FL, newname, oldname,strerror(errno)); + LOGGER_log("%s:%d:%s:WARNING: While trying to create '%s' link to '%s' (%s)",FL, newname, oldname,strerror(errno)); } } else { @@ -2215,7 +2199,7 @@ int MIME_generate_multiple_hardlink_filenames(struct MIMEH_header_info *hinfo, R { if (errno != EEXIST) { - LOGGER_log("%s:%d:MIME_generate_multiple_hardlink_filenames:WARNING: While trying to create '%s' link to '%s' (%s)",FL, newname, oldname,strerror(errno)); + LOGGER_log("%s:%d:%s:WARNING: While trying to create '%s' link to '%s' (%s)",FL, newname, oldname,strerror(errno)); } } else { @@ -2247,13 +2231,13 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct int keep = 1; int result = -1; - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Start:DEBUG: (%s)\n",FL, hinfo->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Start:DEBUG: (%s)\n",FL, hinfo->filename); // If we have a valid filename, then put it through the process of // cleaning and filtering if (isprint((int)hinfo->filename[0])) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Filename is valid, cleaning\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Filename is valid, cleaning\n",FL); FNFILTER_filter(hinfo->filename, _MIMEH_FILENAMELEN_MAX); /* check out thefilename for ISO filenames */ } @@ -2284,7 +2268,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct // then we'll simply use the blankfileprefix. snprintf( hinfo->filename, _MIMEH_FILENAMELEN_MAX, "%s%d", glb.blankfileprefix, glb.filecount ); } - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Filename is empty, setting to default...(%s)\n",FL, hinfo->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Filename is empty, setting to default...(%s)\n",FL, hinfo->filename); if (glb.no_nameless) keep = 0; glb.filecount++; } @@ -2329,7 +2313,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct // The format of the evaluation is: // // (logic-test?true-expression:false-expression) - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: About to execute callback [0x%p]",FL,glb.filename_decoded_reporter); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: About to execute callback [0x%p]",FL,glb.filename_decoded_reporter); if (glb.filename_decoded_reporter != NULL) { glb.filename_decoded_reporter( hinfo->filename, (MIMEH_get_verbosity_contenttype()?hinfo->content_type_string:NULL)); @@ -2343,7 +2327,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct /** Find the start of the filename. **/ fp = strrchr(hinfo->filename, '/'); if (fp) fp++; else fp = hinfo->filename; - //LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Pushing filename %s to the stack",FL,fp); + //LOGGER_log("%s:%d:%s:DEBUG: Pushing filename %s to the stack",FL,fp); // 20040305-1419:PLD // Store the filename we're going to use to save the file to in the filename stack SS_push(ss, fp, strlen(fp)); @@ -2351,11 +2335,11 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct // Select the decoding method based on the content transfer encoding // method which we read from the headers - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: ENCODING = %d\n",FL, hinfo->content_transfer_encoding); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: ENCODING = %d\n",FL, hinfo->content_transfer_encoding); switch (hinfo->content_transfer_encoding) { case _CTRANS_ENCODING_B64: - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding BASE64 format\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding BASE64 format\n",FL); result = MIME_decode_64(f, unpack_metadata, hinfo); switch (result) { case MIME_ERROR_B64_INPUT_STREAM_EOF: @@ -2371,24 +2355,24 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct } break; case _CTRANS_ENCODING_7BIT: - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding 7BIT format\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding 7BIT format\n",FL); result = MIME_decode_text(f, unpack_metadata, hinfo, keep); break; case _CTRANS_ENCODING_8BIT: - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding 8BIT format\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding 8BIT format\n",FL); result = MIME_decode_text(f, unpack_metadata, hinfo, keep); break; case _CTRANS_ENCODING_BINARY: case _CTRANS_ENCODING_RAW: - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding RAW format\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding RAW format\n",FL); result = MIME_decode_raw(f, unpack_metadata, hinfo, keep); break; case _CTRANS_ENCODING_QP: - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding Quoted-Printable format\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding Quoted-Printable format\n",FL); result = MIME_decode_text(f, unpack_metadata, hinfo, keep); break; case _CTRANS_ENCODING_UUENCODE: - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding UUENCODED format\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UUENCODED format\n",FL); // Added as a test - remove if we can get this to work in a better way snprintf(hinfo->uudec_name,sizeof(hinfo->uudec_name),"%s",hinfo->filename); result = UUENCODE_decode_uu(f, hinfo->filename, hinfo->uudec_name, 0, keep, unpack_metadata, hinfo ); @@ -2400,19 +2384,19 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct case _CTRANS_ENCODING_UNKNOWN: switch (hinfo->content_disposition) { case _CDISPOSITION_FORMDATA: - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding UNKNOWN format of FORMDATA disposition\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UNKNOWN format of FORMDATA disposition\n",FL); result = MIME_decode_raw(f, unpack_metadata, hinfo, keep); break; default: - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding UNKNOWN format\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UNKNOWN format\n",FL); result = MIME_decode_text(f, unpack_metadata, hinfo, keep); } - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: UNKNOWN Decode completed, result = %d\n",FL,result); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: UNKNOWN Decode completed, result = %d\n",FL,result); break; case _CTRANS_ENCODING_UNSPECIFIED: - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding UNSPECIFIED format\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UNSPECIFIED format\n",FL); result = MIME_decode_text(f, unpack_metadata, hinfo, keep); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding result for UNSPECIFIED format = %d\n",FL, result); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding result for UNSPECIFIED format = %d\n",FL, result); // 20040114-1236:PLD: Added nested mail checking // // Sometimes mailpacks have false headers at the start, resulting @@ -2427,7 +2411,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct if (1) { snprintf(hinfo->scratch,sizeof(hinfo->scratch),"%s/%s",unpack_metadata->dir,hinfo->filename); - LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG:REMOVEME: Testing for RFC822 headers in file %s",FL,hinfo->scratch); + LOGGER_log("%s:%d:%s:DEBUG:REMOVEME: Testing for RFC822 headers in file %s",FL,hinfo->scratch); if (MIME_is_diskfile_RFC822(hinfo->scratch) > 0 ) { // 20040305-1304:PLD: unpack the file, propagate result upwards @@ -2436,7 +2420,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct } break; default: - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding format is not defined (%d)\n",FL, hinfo->content_transfer_encoding); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding format is not defined (%d)\n",FL, hinfo->content_transfer_encoding); result = MIME_decode_raw(f, unpack_metadata, hinfo, keep); break; } @@ -2479,7 +2463,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct // side-effects if (hinfo->content_type == _CTYPE_TNEF) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Decoding TNEF format\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding TNEF format\n",FL); glb.attachment_count++; MIME_decode_TNEF( unpack_metadata, hinfo, 0 ); } // Decode TNEF @@ -2494,7 +2478,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct if (glb.decode_mht != 0) { // Patched 26-01-03: supplied by Chris Hine - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Microsoft MHT format email filename='%s'\n",FL, hinfo->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Microsoft MHT format email filename='%s'\n",FL, hinfo->filename); snprintf(hinfo->scratch,sizeof(hinfo->scratch),"%s/%s",unpack_metadata->dir,hinfo->filename); // 20040305-1304:PLD: unpack the file, propagate result upwards @@ -2505,7 +2489,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct // End. MIME_generate_multiple_hardlink_filenames(hinfo,unpack_metadata); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_decode_encoding:DEBUG: Done for filename = '%s'",FL,hinfo->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done for filename = '%s'",FL,hinfo->filename); return result; } @@ -2523,10 +2507,10 @@ int MIME_postdecode_cleanup( RIPMIME_output *unpack_metadata, struct SS_object * result = 0; do { char *filename; - if (MIME_DNORMAL) LOGGER_log("%s:%d: Popping file...",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s Popping file...",FL); filename = SS_pop(ss); if (filename == NULL) break; - if (MIME_DNORMAL) LOGGER_log("%s:%d: Popped file '%s'",FL, filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s Popped file '%s'",FL, filename); if ( strncmp( glb.blankfileprefix, filename, strlen( glb.blankfileprefix ) ) == 0 ) { snprintf( fullpath, sizeof(fullpath), "%s/%s", unpack_metadata->dir, filename ); @@ -2563,7 +2547,7 @@ int MIME_postdecode_cleanup( RIPMIME_output *unpack_metadata, struct SS_object * int MIME_handle_multipart( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ) { int result = 0; - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_handle_multipart:DEBUG: Decoding multipart/embedded \n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding multipart/embedded \n",FL); // If there is no filename, then we have a "standard" // embedded message, which can be just read off as a // continuous stream (simply with new boundaries @@ -2578,13 +2562,13 @@ int MIME_handle_multipart( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struc // layer of headers and get into the core of the message. This is why we // call unpack_stage2() because this function just reads the next set of // headers and decodes. - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_handle_multipart:DEBUG: Non base64 encoding AND no filename, embedded message\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Non base64 encoding AND no filename, embedded message\n",FL); h->boundary_located = 0; result = MIME_unpack_stage2(f, unpack_metadata, h, current_recursion_level , ss); p = BS_top(); if (p) PLD_strncpy(h->boundary, p,sizeof(h->boundary)); } else { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_handle_multipart:DEBUG: Embedded message has a filename, decoding to file %s",FL,h->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Embedded message has a filename, decoding to file %s",FL,h->filename); result = MIME_decode_encoding( f, unpack_metadata, h, ss ); if (result == 0) { @@ -2600,7 +2584,7 @@ int MIME_handle_multipart( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struc } } // else-if transfer-encoding != B64 && filename was empty - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_handle_multipart:DEBUG: done handling '%s' result = %d",FL,h->filename, result); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: done handling '%s' result = %d",FL,h->filename, result); return result; } @@ -2627,12 +2611,12 @@ int MIME_handle_rfc822( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M { /** Decode a RFC822 encoded stream of data from *f **/ int result = 0; - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_handle_rfc822:DEBUG: Decoding RFC822 message\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding RFC822 message\n",FL); /** If there is no filename, then we have a "standard" ** embedded message, which can be just read off as a ** continuous stream (simply with new boundaries **/ - DMIME LOGGER_log("%s:%d:MIME_handle_rfc822:DEBUG: Filename='%s', encoding = %d",FL, h->filename, h->content_transfer_encoding); + DMIME LOGGER_log("%s:%d:%s:DEBUG: Filename='%s', encoding = %d",FL, h->filename, h->content_transfer_encoding); // if ((0)&&( h->content_transfer_encoding != _CTRANS_ENCODING_B64)&&( h->filename[0] == '0' )) 20041217-1635:PLD: if (( h->content_transfer_encoding != _CTRANS_ENCODING_B64)&&( h->filename[0] == '\0' )) @@ -2646,31 +2630,31 @@ int MIME_handle_rfc822( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M // layer of headers and get into the core of the message. This is why we // call unpack_stage2() because this function just reads the next set of // headers and decodes. - DMIME LOGGER_log("%s:%d:MIME_handle_rfc822:DEBUG: Non base64 encoding AND no filename, embedded message\n",FL); + DMIME LOGGER_log("%s:%d:%s:DEBUG: Non base64 encoding AND no filename, embedded message\n",FL); h->boundary_located = 0; result = MIME_unpack_stage2(f, unpack_metadata, h, current_recursion_level , ss); p = BS_top(); if (p) PLD_strncpy(h->boundary, p,sizeof(h->boundary)); } else { /** ...else... if the section has a filename or B64 type encoding, we need to put it through extra decoding **/ - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_handle_rfc822:DEBUG: Embedded message has a filename, decoding to file %s",FL,h->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Embedded message has a filename, decoding to file %s",FL,h->filename); result = MIME_decode_encoding( f, unpack_metadata, h, ss ); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_handle_rfc822:DEBUG: Result of extracting %s is %d",FL,h->filename, result); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Result of extracting %s is %d",FL,h->filename, result); if (result == 0) { /** Because we're calling MIME_unpack_single again [ie, recursively calling it we need to now adjust the input-filename so that it correctly is prefixed with the directory we unpacked to. **/ - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_handle_rfc822:DEBUG: Now attempting to extract contents of '%s'",FL,h->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Now attempting to extract contents of '%s'",FL,h->filename); snprintf(scratch,sizeof(scratch),"%s/%s",unpack_metadata->dir, h->filename); snprintf(h->filename,sizeof(h->filename),"%s",scratch); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_handle_rfc822:DEBUG: Now attempting to extract contents of '%s'",FL,scratch); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Now attempting to extract contents of '%s'",FL,scratch); result = MIME_unpack_single( unpack_metadata, scratch, current_recursion_level, ss ); result = 0; } } /** else-if transfer-encoding != B64 && filename was empty **/ - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_handle_rfc822:DEBUG: done handling '%s' result = %d",FL,h->filename, result); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: done handling '%s' result = %d",FL,h->filename, result); return result; } @@ -2697,7 +2681,7 @@ int MIME_handle_plain( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MI { /** Handle a plain text encoded data stream from *f **/ int result = 0; - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_handle_plain:DEBUG: Handling plain email",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Handling plain email",FL); result = MIME_decode_encoding( f, unpack_metadata, h, ss ); if ((result == MIME_ERROR_FFGET_EMPTY)||(result == 0)) { @@ -2728,27 +2712,27 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M struct MIMEH_header_info *h; char *p; - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Start, recursion %d\n",FL, current_recursion_level); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Start, recursion %d\n",FL, current_recursion_level); if (current_recursion_level > glb.max_recursion_level) { /** Test for recursion limits **/ - if (MIME_VERBOSE) LOGGER_log("%s:%d:MIME_unpack_stage2:WARNING: Current recursion level of %d is greater than permitted %d"\ + if (MIME_VERBOSE) LOGGER_log("%s:%d:%s:WARNING: Current recursion level of %d is greater than permitted %d"\ ,FL, current_recursion_level, glb.max_recursion_level); return MIME_ERROR_RECURSION_LIMIT_REACHED; // 20040306-1301:PLD } h = hinfo; // Get our headers and determin what we have... // - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Parsing headers (initial)\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Parsing headers (initial)\n",FL); // Parse the headers, extracting what information we need /** 20041216-1102:PLD: Keep attempting to read headers until we get a sane set **/ do { /** Read next set of headers, repeat until a sane set of headers are found **/ result = MIMEH_parse_headers(f,h); - DMIME LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Parsing of headers done, sanity = %d, result = %d",FL,h->sanity, result); + DMIME LOGGER_log("%s:%d:%s:DEBUG: Parsing of headers done, sanity = %d, result = %d",FL,h->sanity, result); } while ((h->sanity == 0)&&(result != -1)); - DMIME LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Repeat loop of header-reading done, sanity = %d, result = %d",FL,h->sanity, result); + DMIME LOGGER_log("%s:%d:%s:DEBUG: Repeat loop of header-reading done, sanity = %d, result = %d",FL,h->sanity, result); glb.header_defect_count += MIMEH_get_defect_count(h); // Test the result output switch (result) { @@ -2764,7 +2748,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M { snprintf(glb.subject, sizeof(glb.subject), "%s", h->subject ); } - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Headers parsed, Result = %d, Boundary located = %d\n"\ + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Headers parsed, Result = %d, Boundary located = %d\n"\ ,FL,result, hinfo->boundary_located); // Test to see if we encountered any DoubleCR situations while we @@ -2786,7 +2770,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M char *lbc; char *fbc; - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Boundary located, pushing to stack (%s)\n",FL,h->boundary); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Boundary located, pushing to stack (%s)\n",FL,h->boundary); BS_push(h->boundary); // Get the first and last character positions of the boundary @@ -2801,18 +2785,18 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M if (*lbc == '"') { *lbc = '\0'; BS_push(h->boundary); *lbc = '"'; MIMEH_set_defect( hinfo, MIMEH_DEFECT_UNBALANCED_BOUNDARY_QUOTE); } h->boundary_located = 0; } else { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Decoding in BOUNDARY-LESS mode\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding in BOUNDARY-LESS mode\n",FL); if (h->content_type == _CTYPE_RFC822) { // Pass off to the RFC822 handler - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Decoding with RFC822 decoder\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding with RFC822 decoder\n",FL); result = MIME_handle_rfc822(f,unpack_metadata,h,current_recursion_level,ss); } else if (MIMEH_is_contenttype(_CTYPE_MULTIPART, h->content_type)) { // Pass off to the multipart handler - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Decoding with Multipart decoder\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding with Multipart decoder\n",FL); result = MIME_handle_multipart(f,unpack_metadata,h,current_recursion_level,ss); } else { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Decoding boundaryless file (%s)...\n",FL,h->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding boundaryless file (%s)...\n",FL,h->filename); result = MIME_handle_plain( f, unpack_metadata,h,current_recursion_level,ss); } // else-if content was RFC822 or multi-part return result; @@ -2821,13 +2805,13 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M if ((BS_top()!=NULL)&&(result == 0)) { // Commence decoding WITH boundary awareness. - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Decoding with boundaries (filename = %s)\n",FL,h->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding with boundaries (filename = %s)\n",FL,h->filename); // Decode the data in the current MIME segment // based on the header information retrieved from // the start of this function. result = MIME_decode_encoding(f, unpack_metadata, h, ss); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Done decoding, result = %d",FL,result); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done decoding, result = %d",FL,result); if (result == 0) { if (BS_top()!=NULL) @@ -2843,13 +2827,13 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M h->content_transfer_encoding = -1; h->content_disposition = -1; - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Decoding headers...\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding headers...\n",FL); do { result = MIMEH_parse_headers(f,h); } while ((h->sanity == 0)&&(result != -1)); glb.header_defect_count += MIMEH_get_defect_count(h); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Mime header parsing result = %d\n",FL, result); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Mime header parsing result = %d\n",FL, result); // 20040305-1331:PLD if (result == -1) { @@ -2859,7 +2843,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M if (h->boundary_located) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Pushing boundary %s\n",FL, h->boundary); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Pushing boundary %s\n",FL, h->boundary); BS_push(h->boundary); h->boundary_located = 0; } @@ -2878,7 +2862,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M || (MIMEH_is_contenttype(_CTYPE_MULTIPART, h->content_type))\ ) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Multipart/RFC822 mail headers found\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Multipart/RFC822 mail headers found\n",FL); /* If there is no filename, then we have a "standard" * embedded message, which can be just read off as a * continuous stream (simply with new boundaries */ @@ -2890,8 +2874,8 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M // the file into ripMIME. Certainly, this is not // the most efficent way of dealing with nested emails // however, it is a rather robust/reliable way. - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Chose Content-type == RFC822 clause",FL); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Calling MIME_decode_encoding()",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Chose Content-type == RFC822 clause",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Calling MIME_decode_encoding()",FL); result = MIME_handle_rfc822(f,unpack_metadata,h,current_recursion_level,ss); // First up - extract the RFC822 body out of the parent mailpack // XX result = MIME_decode_encoding( f, unpackdir, h, ss ); @@ -2903,10 +2887,10 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M { snprintf(scratch,sizeof(scratch),"%s/%s",unpackdir, h->filename); snprintf(h->filename,sizeof(h->filename),"%s",scratch); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Now calling MIME_unpack_single() on the file '%s' for our RFC822 decode operation.",FL, scratch); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Now calling MIME_unpack_single() on the file '%s' for our RFC822 decode operation.",FL, scratch); //result = MIME_unpack_single( unpackdir, h->filename, current_recursion_level +1, ss); result = MIME_unpack( unpackdir, h->filename, current_recursion_level +1 ); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Unpack result = %d", FL, result); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Unpack result = %d", FL, result); result = 0; } else return result; // 20040305-1312:PLD */ @@ -2917,7 +2901,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M // To be honest, i've forgotten what this section of test is supposed to pick out // in terms of files - certainly it does pick out some, but I'm not sure what format // they are. Shame on me for not remembering, in future I must comment more. - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: NON-BASE64 DECODE\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: NON-BASE64 DECODE\n",FL); h->boundary_located = 0; result = MIME_unpack_stage2(f, unpack_metadata, h, current_recursion_level , ss); @@ -2935,16 +2919,16 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M // and embedded format, it does not have the normal headers->[headers->data] // layout of other nested emails - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Handle Appledouble explicitly",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Handle Appledouble explicitly",FL); result = MIME_unpack_stage2(f, unpack_metadata, h, current_recursion_level , ss); } else { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: RFC822 Message to be decoded...\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: RFC822 Message to be decoded...\n",FL); result = MIME_decode_encoding( f, unpack_metadata, h, ss ); if (result != 0) return result; // 20040305-1313:PLD else { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Now running ripMIME over decoded RFC822 message...\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Now running ripMIME over decoded RFC822 message...\n",FL); // Because we're calling MIME_unpack_single again [ie, recursively calling it // we need to now adjust the input-filename so that it correctly is prefixed @@ -2961,10 +2945,10 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M // If the attachment included in this MIME segment is NOT a // multipart or RFC822 embedded email, we can then simply use // the normal decoding function to interpret its data. - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Decoding a normal attachment \n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding a normal attachment \n",FL); result = MIME_decode_encoding( f, unpack_metadata, h, ss ); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Decoding a normal attachment '%s' done. \n",FL, h->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding a normal attachment '%s' done. \n",FL, h->filename); // See if we have an attachment output which is actually another // email. // @@ -2979,7 +2963,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M mime_fname = PLD_dprintf("%s/%s", unpack_metadata->dir, h->filename); if(mime_fname != NULL) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Testing '%s' for email type",FL,mime_fname); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Testing '%s' for email type",FL,mime_fname); if (MIME_is_diskfile_RFC822(mime_fname)) { //MIME_unpack_single( unpackdir, mime_fname, (hinfo->current_recursion_level+1), ss); @@ -3002,7 +2986,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M } // if MIME_BS_top() } // if result == 0 } // if (result) - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_stage2:DEBUG: Exiting with result=%d recursion=%d\n",FL,result, current_recursion_level); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Exiting with result=%d recursion=%d\n",FL,result, current_recursion_level); return result; } @@ -3034,7 +3018,7 @@ int MIME_unpack_mailbox( RIPMIME_output *unpack_metadata, char *mpname, int curr fi = (strcmp(mpname,"-")==0) ? stdin : fopen(mpname,"r"); if (!fi) { - LOGGER_log("%s:%d:MIME_unpack_mailbox:ERROR: Cannot open '%s' for reading (%s)",FL, mpname,strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: Cannot open '%s' for reading (%s)",FL, mpname,strerror(errno)); return -1; } } @@ -3042,7 +3026,7 @@ int MIME_unpack_mailbox( RIPMIME_output *unpack_metadata, char *mpname, int curr fo = fopen(fname,"w"); if (!fo) { - LOGGER_log("%s:%d:MIME_unpack_mailbox:ERROR: Cannot open '%s' for writing (%s)",FL, fname,strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: Cannot open '%s' for writing (%s)",FL, fname,strerror(errno)); return -1; } @@ -3065,7 +3049,7 @@ int MIME_unpack_mailbox( RIPMIME_output *unpack_metadata, char *mpname, int curr result = remove(fname); if (result == -1) { - LOGGER_log("%s:%d:MIME_unpack_mailbox:ERROR: removing temporary mailpack '%s' (%s)",FL, fname,strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: removing temporary mailpack '%s' (%s)",FL, fname,strerror(errno)); } // Create a new mailpack filename, and keep on going... snprintf(fname,sizeof(fname),"%s/tmp.email%03d.mailpack",unpack_metadata->dir,++mcount); @@ -3110,7 +3094,7 @@ int MIME_unpack_mailbox( RIPMIME_output *unpack_metadata, char *mpname, int curr result = remove(fname); if (result == -1) { - LOGGER_log("%s:%d:MIME_unpack_mailbox:ERROR: removing temporary mailpack '%s' (%s)",FL, fname,strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: removing temporary mailpack '%s' (%s)",FL, fname,strerror(errno)); } return 0; } @@ -3139,15 +3123,15 @@ int MIME_unpack_single( RIPMIME_output *unpack_metadata, char *mpname, int curre if (current_recursion_level > glb.max_recursion_level) { - LOGGER_log("%s:%d:MIME_unpack_single:WARNING: Current recursion level of %d is greater than permitted %d",FL, current_recursion_level, glb.max_recursion_level); + LOGGER_log("%s:%d:%s:WARNING: Current recursion level of %d is greater than permitted %d",FL, current_recursion_level, glb.max_recursion_level); return MIME_ERROR_RECURSION_LIMIT_REACHED; } - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_single:DEBUG: dir=%s packname=%s level=%d (max = %d)\n",FL, unpack_metadata->dir, mpname, current_recursion_level, glb.max_recursion_level); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: dir=%s packname=%s level=%d (max = %d)\n",FL, unpack_metadata->dir, mpname, current_recursion_level, glb.max_recursion_level); /* if we're reading in from STDIN */ if( mpname[0] == '-' && mpname[1] == '\0' ) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_single:DEBUG: STDIN opened...\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: STDIN opened...\n",FL); fi = stdin; } else @@ -3155,21 +3139,21 @@ int MIME_unpack_single( RIPMIME_output *unpack_metadata, char *mpname, int curre fi = fopen(mpname,"r"); if (!fi) { - LOGGER_log("%s:%d:MIME_unpack_single:ERROR: Cannot open file '%s' for reading.\n",FL, mpname); + LOGGER_log("%s:%d:%s:ERROR: Cannot open file '%s' for reading.\n",FL, mpname); return -1; } - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_single:DEBUG: Input file (%s) opened...\n",FL, mpname); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Input file (%s) opened...\n",FL, mpname); } - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_single:DEBUG: Checking input streams...\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Checking input streams...\n",FL); /* check to see if we had problems opening the file */ if (fi == NULL) { - LOGGER_log("%s:%d:MIME_unpack_single:ERROR: Could not open mailpack file '%s' (%s)",FL, mpname, strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: Could not open mailpack file '%s' (%s)",FL, mpname, strerror(errno)); return -1; } // 20040317-2359:PLD result = MIME_unpack_single_fp(unpack_metadata,fi,current_recursion_level , ss); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_single:DEBUG: result = %d, recursion = %d, filename = '%s'", FL, result, current_recursion_level, mpname ); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: result = %d, recursion = %d, filename = '%s'", FL, result, current_recursion_level, mpname ); if ((current_recursion_level > 1)&&(result == 241)) result = 0; fclose(fi); return result; @@ -3196,18 +3180,18 @@ int MIME_unpack_single_fp( RIPMIME_output *unpack_metadata, FILE *fi, int curren // Because this MIME module gets used in both CLI and daemon modes // we should check to see that we can report to stderr // - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_single_fp:DEBUG: dir=%s level=%d (max = %d)\n",FL, unpack_metadata->dir, current_recursion_level, glb.max_recursion_level); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: dir=%s level=%d (max = %d)\n",FL, unpack_metadata->dir, current_recursion_level, glb.max_recursion_level); if (current_recursion_level > glb.max_recursion_level) { - LOGGER_log("%s:%d:MIME_unpack_single_fp:WARNING: Current recursion level of %d is greater than permitted %d",FL, current_recursion_level, glb.max_recursion_level); + LOGGER_log("%s:%d:%s:WARNING: Current recursion level of %d is greater than permitted %d",FL, current_recursion_level, glb.max_recursion_level); // return -1; return MIME_ERROR_RECURSION_LIMIT_REACHED; // 20040305-1302:PLD //return 0; // 20040208-1723:PLD } else h.current_recursion_level = current_recursion_level; glb.current_line = 0; - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_single_fp:DEBUG: recursion level checked...%d\n",FL, current_recursion_level); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_single_fp:DEBUG: DumpHeaders = %d\n",FL, glb.save_headers); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: recursion level checked...%d\n",FL, current_recursion_level); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: DumpHeaders = %d\n",FL, glb.save_headers); if ((!hf)&&(glb.save_headers)&&(MIMEH_get_headers_save()==0)) { // Prepend the unpackdir path to the headers file name @@ -3216,7 +3200,7 @@ int MIME_unpack_single_fp( RIPMIME_output *unpack_metadata, FILE *fi, int curren if (!hf) { glb.save_headers = 0; - LOGGER_log("%s:%d:MIME_unpack_single_fp:ERROR: Cannot open '%s' for writing (%s)",FL, glb.headersname,strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: Cannot open '%s' for writing (%s)",FL, glb.headersname,strerror(errno)); } else { @@ -3225,7 +3209,7 @@ int MIME_unpack_single_fp( RIPMIME_output *unpack_metadata, FILE *fi, int curren } } - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_single_fp:DEBUG: Setting up streams to decode\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Setting up streams to decode\n",FL); FFGET_setstream(&f, fi); /** Initialize the header record **/ h.boundary[0] = '\0'; @@ -3247,14 +3231,14 @@ int MIME_unpack_single_fp( RIPMIME_output *unpack_metadata, FILE *fi, int curren } } - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_single_fp:DEBUG: preparing to decode, calling stage2...\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: preparing to decode, calling stage2...\n",FL); // 20040318-0001:PLD result = MIME_unpack_stage2(&f, unpack_metadata, &h, current_recursion_level +1, ss); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_single_fp:DEBUG: done decoding ( in stage2 ) result=%d, to %s\n",FL, result, unpack_metadata->dir); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: done decoding ( in stage2 ) result=%d, to %s\n",FL, result, unpack_metadata->dir); // fclose(fi); 20040208-1726:PLD if ( headers_save_set_here > 0 ) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_single_fp:DEBUG: Closing header file.\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Closing header file.\n",FL); fflush(stdout); MIMEH_set_headers_nosave(); fclose(hf); @@ -3264,7 +3248,7 @@ int MIME_unpack_single_fp( RIPMIME_output *unpack_metadata, FILE *fi, int curren /** Flush out the string stacks **/ SS_done(&(h.ss_filenames)); SS_done(&(h.ss_names)); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack_single_fp:DEBUG: Done. Result=%d Recursion=%d\n",FL, result, current_recursion_level); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done. Result=%d Recursion=%d\n",FL, result, current_recursion_level); return result; // return status; // 20040305-1318:PLD } @@ -3283,18 +3267,18 @@ int MIME_unpack( RIPMIME_output *unpack_metadata, char *mpname, int current_recu struct SS_object ss; // Stores the filenames that are created in the unpack operation if (current_recursion_level > glb.max_recursion_level) return MIME_ERROR_RECURSION_LIMIT_REACHED; - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack: Unpacking %s to %s, recursion level is %d",FL,mpname,unpack_metadata->dir,current_recursion_level); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s: Unpacking %s to %s, recursion level is %d",FL,mpname,unpack_metadata->dir,current_recursion_level); MIMEH_set_outputdir(unpack_metadata->dir); if (MIME_DNORMAL) SS_set_debug(&ss,1); SS_init(&ss); if (glb.mailbox_format > 0) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack: Unpacking using mailbox format",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s: Unpacking using mailbox format",FL); result = MIME_unpack_mailbox( unpack_metadata, mpname, (current_recursion_level), &ss ); } else { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack: Unpacking standard mailpack",FL,mpname,unpack_metadata->dir,current_recursion_level); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s: Unpacking standard mailpack",FL,mpname,unpack_metadata->dir,current_recursion_level); result = MIME_unpack_single( unpack_metadata, mpname, (current_recursion_level +1), &ss ); } @@ -3305,7 +3289,7 @@ int MIME_unpack( RIPMIME_output *unpack_metadata, char *mpname, int current_recu if (MIME_DNORMAL) { - LOGGER_log("%s:%d:MIME_unpack: Files unpacked from '%s' (recursion=%d);",FL,mpname,current_recursion_level); + LOGGER_log("%s:%d:%s: Files unpacked from '%s' (recursion=%d);",FL,mpname,current_recursion_level); SS_dump(&ss); } @@ -3330,11 +3314,11 @@ int MIME_unpack( RIPMIME_output *unpack_metadata, char *mpname, int current_recu if (current_recursion_level == 0) { - //LOGGER_log("%s:%d:MIME_unpack:DEBUG: Clearing boundary stack",FL); + //LOGGER_log("%s:%d:%s:DEBUG: Clearing boundary stack",FL); BS_clear(); } - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack: Unpacking of %s is done.",FL,mpname); - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_unpack: -----------------------------------",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s: Unpacking of %s is done.",FL,mpname); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s: -----------------------------------",FL); return result; } @@ -3345,7 +3329,7 @@ int MIME_unpack( RIPMIME_output *unpack_metadata, char *mpname, int current_recu void MIME_close( void ) { if (MIME_DNORMAL) { - LOGGER_log("%s:%d:MIME_close: start.",FL); + LOGGER_log("%s:%d:%s: start.",FL); printArray(glb.mime_arr); } From 9ea9732becc717ba651420ea0f51fc76309d1ce1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Sat, 26 Apr 2025 08:54:10 +0300 Subject: [PATCH 32/80] Separate `mime_element` module from `mime`, some C99 cleanup --- Makefile | 2 +- mime.c | 174 +++---------------------------------------------- mime.h | 28 -------- mime_element.c | 172 ++++++++++++++++++++++++++++++++++++++++++++++++ mime_element.h | 49 ++++++++++++++ mime_headers.h | 3 - ripOLE/ole.c | 4 +- ripmime.c | 1 + strstack.c | 26 ++++---- uuencode.c | 7 +- 10 files changed, 250 insertions(+), 216 deletions(-) create mode 100644 mime_element.c create mode 100644 mime_element.h diff --git a/Makefile b/Makefile index f75bcd4..11f07b8 100644 --- a/Makefile +++ b/Makefile @@ -34,7 +34,7 @@ LIBS= OBJ=ripmime RIPOLE_OBJS= ripOLE/ole.o ripOLE/olestream-unwrap.o ripOLE/bytedecoders.o ripOLE/bt-int.o #RIPOLE_OBJS= -OFILES= strstack.o mime.o ffget.o mime_headers.o tnef/tnef.o rawget.o pldstr.o logger.o libmime-decoders.o boundary-stack.o uuencode.o filename-filters.o $(RIPOLE_OBJS) +OFILES= strstack.o mime.o ffget.o mime_headers.o tnef/tnef.o rawget.o pldstr.o logger.o libmime-decoders.o boundary-stack.o uuencode.o filename-filters.o mime_element.o $(RIPOLE_OBJS) default: tnef/tnef.o ripmime ripOLE/ole.o diff --git a/mime.c b/mime.c index f768343..b516120 100644 --- a/mime.c +++ b/mime.c @@ -48,6 +48,8 @@ #include "pldstr.h" #include "boundary-stack.h" #include "ffget.h" +#include "strstack.h" +#include "mime_element.h" #include "mime.h" #include "tnef/tnef_api.h" #include "ripOLE/ole.h" @@ -56,6 +58,7 @@ #include "filename-filters.h" #include "logger.h" + int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int current_recursion_level, struct SS_object *ss ); int MIME_unpack_single( RIPMIME_output *unpack_metadata, char *mpname, int current_recursion_level, struct SS_object *ss ); int MIME_unpack_single_fp( RIPMIME_output *unpack_metadata, FILE *fi, int current_recursion_level, struct SS_object *ss ); @@ -118,12 +121,6 @@ static unsigned char b64[256]={ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128 \ }; -typedef struct { - size_t size; - size_t capacity; - MIME_element** array; -} dynamic_array; - struct MIME_globals { int header_defect_count; int filecount; @@ -174,29 +171,11 @@ struct MIME_globals { // wise, any consequent parsing of sub-message bodies // will result in the clobbering of the hinfo struct char subject[_MIME_STRLEN_MAX]; - - int mime_count; - dynamic_array* mime_arr; }; static struct MIME_globals glb; static char scratch[1024]; -/* Dynamic array support*/ -#define INITIAL_SIZE 8 - -// function prototypes -// array container functions -void arrayInit(dynamic_array** arr_ptr); -void freeArray(dynamic_array* container); - -// Basic Operation functions -void insertItem(dynamic_array* container, MIME_element* item); -void updateItem(dynamic_array* container, int i, MIME_element* item); -int getItem(dynamic_array* container, int i); -void deleteItem(dynamic_array* container, int i); -void printArray(dynamic_array* container); - /*-----------------------------------------------------------------\ Function Name : MIME_version Returns Type : int @@ -1234,49 +1213,7 @@ int MIME_decode_OLE( RIPMIME_output *unpack_metadata, struct MIMEH_header_info * } #endif -MIME_element* MIME_element_add_with_path (char* fullpath, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo) -{ - MIME_element *cur = malloc(sizeof(MIME_element)); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:start\n",FL); - - insertItem(glb.mime_arr, cur); - cur->hinfo = hinfo; - cur->id = glb.mime_count++; - cur->fullpath = fullpath; - - cur->f = fopen(cur->fullpath,"wb"); - if (cur->f == NULL) { - LOGGER_log("%s:%d:%s:ERROR: cannot open %s for writing",FL,cur->fullpath); - return cur; - } - - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding [encoding=%d] to %s\n",FL, hinfo->content_transfer_encoding, cur->fullpath); - if (unpack_metadata != NULL && unpack_metadata->unpack_mode == RIPMIME_UNPACK_MODE_LIST_FILES && cur->f != NULL) { - fprintf (stdout, "%d|%d|%d|%d|%s|%s\n", glb.mime_count, glb.attachment_count, glb.filecount, hinfo->current_recursion_level, hinfo->content_type_string, hinfo->filename); - } - return cur; -} - -MIME_element* MIME_element_add (RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo) -{ - int fullpath_len = strlen(unpack_metadata->dir) + strlen(hinfo->filename) + 3 * sizeof(char); - char *fullpath = (char*)malloc(fullpath_len); - - snprintf(fullpath,fullpath_len,"%s/%s",unpack_metadata->dir,hinfo->filename); - return MIME_element_add_with_path(fullpath, unpack_metadata, hinfo); -} - -void MIME_element_remove (MIME_element* cur) -{ - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:start\n",FL); - - if (cur->f != NULL) { - fclose(cur->f); - } - free(cur->fullpath); - free(cur); -} /*------------------------------------------------------------------------ Procedure: MIME_decode_raw ID:1 @@ -1304,7 +1241,7 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Start\n",FL); - cur_mime = MIME_element_add (unpack_metadata, hinfo); + cur_mime = MIME_element_add (unpack_metadata, hinfo, glb.attachment_count, glb.filecount); while ((readcount=FFGET_raw(f, (unsigned char *) buffer,bufsize)) > 0) { @@ -1404,7 +1341,7 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding TEXT [encoding=%d] to %s\n",FL, hinfo->content_transfer_encoding, hinfo->filename); - cur_mime = MIME_element_add (unpack_metadata, hinfo); + cur_mime = MIME_element_add (unpack_metadata, hinfo, glb.attachment_count, glb.filecount); if (!f) { @@ -1574,7 +1511,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: attempting to decode '%s'", FL, hinfo->filename); - cur_mime = MIME_element_add (unpack_metadata, hinfo); + cur_mime = MIME_element_add (unpack_metadata, hinfo, glb.attachment_count, glb.filecount); if (cur_mime->f == NULL) { return -1; @@ -2116,8 +2053,7 @@ void MIME_init( void ) glb.subject[0]='\0'; - glb.mime_count = 0; - arrayInit(&(glb.mime_arr)); + all_MIME_elements_init(); } /*-----------------------------------------------------------------\ @@ -3330,101 +3266,9 @@ void MIME_close( void ) { if (MIME_DNORMAL) { LOGGER_log("%s:%d:%s: start.",FL); - printArray(glb.mime_arr); - } - - freeArray(glb.mime_arr); -} - -//------Dynamic array function definitions------ -// Array initialization -void arrayInit(dynamic_array** arr_ptr) -{ - dynamic_array *container; - container = (dynamic_array*)malloc(sizeof(dynamic_array)); - if(!container) { - printf("Memory Allocation Failed\n"); - exit(0); - } - - container->size = 0; - container->capacity = INITIAL_SIZE; - container->array = (MIME_element **)malloc(INITIAL_SIZE * sizeof(MIME_element*)); - if (!container->array){ - printf("Memory Allocation Failed\n"); - exit(0); + printArray(all_MIME_elements.mime_arr); } - *arr_ptr = container; + freeArray(all_MIME_elements.mime_arr); } -// Insertion Operation -void insertItem(dynamic_array* container, MIME_element* item) -{ - if (container->size == container->capacity) { - MIME_element **temp = container->array; - container->capacity <<= 1; - container->array = realloc(container->array, container->capacity * sizeof(MIME_element*)); - if(!container->array) { - printf("Out of Memory\n"); - container->array = temp; - return; - } - } - container->array[container->size++] = item; -} - -// Retrieve Item at Particular Index -int getItem(dynamic_array* container, int index) -{ - if(index >= container->size) { - printf("Index Out of Bounds\n"); - return -1; - } - return container->array[index]; -} - -// Update Operation -void updateItem(dynamic_array* container, int index, MIME_element* item) -{ - if (index >= container->size) { - printf("Index Out of Bounds\n"); - return; - } - container->array[index] = item; -} - -// Delete Item from Particular Index -void deleteItem(dynamic_array* container, int index) -{ - if(index >= container->size) { - printf("Index Out of Bounds\n"); - return; - } - - for (int i = index; i < container->size; i++) { - container->array[i] = container->array[i + 1]; - } - container->size--; -} - -// Array Traversal -void printArray(dynamic_array* container) -{ - printf("Array elements: "); - for (int i = 0; i < container->size; i++) { - printf("%p ", container->array[i]); - } - printf("\nSize: "); - printf("%lu", container->size); - printf("\nCapacity: "); - printf("%lu\n", container->capacity); -} - -// Freeing the memory allocated to the array -void freeArray(dynamic_array* container) -{ - free(container->array); - free(container); -} -/*----------END OF MIME.c------------*/ diff --git a/mime.h b/mime.h index 649e108..c2211f3 100644 --- a/mime.h +++ b/mime.h @@ -56,20 +56,6 @@ /* status return codes */ #define MIME_STATUS_ZERO_FILE 100 -/* unpack modes */ -#define RIPMIME_UNPACK_MODE_TO_DIRECTORY 0 -#define RIPMIME_UNPACK_MODE_COUNT_FILES 1 -#define RIPMIME_UNPACK_MODE_LIST_FILES 2 - -struct mime_output -{ - char *dir; - int unpack_mode; - // int fragment_number; will be used later -}; - -typedef struct mime_output RIPMIME_output; - int MIME_version( void ); size_t MIME_read_raw( char *src_mpname, char *dest_mpname, size_t rw_buffer_size ); int MIME_read( char *mpname ); /* returns filesize in KB */ @@ -130,18 +116,4 @@ void MIME_init( void ); void MIME_close( void ); int MIME_set_tmpdir( char *tmpdir ); -#include "strstack.h" -#include "mime_headers.h" - -typedef struct { - struct MIMEH_header_info *hinfo; - int id; - char* fullpath; - FILE* f; - int result; -} MIME_element; - -MIME_element* MIME_element_add_with_path (char* fullpath, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo); -void MIME_element_remove (MIME_element* cur); - #endif diff --git a/mime_element.c b/mime_element.c new file mode 100644 index 0000000..b81ea23 --- /dev/null +++ b/mime_element.c @@ -0,0 +1,172 @@ + +#include +#include +#include + +#include "ffget.h" +#include "mime_element.h" +#include "logger.h" + +#ifndef FL +#define FL __FILE__, __LINE__, __func__ +#endif + +/* Dynamic array support*/ +#define INITIAL_SIZE 8 + +// function prototypes +// array container functions +void arrayInit(dynamic_array** arr_ptr); +void freeArray(dynamic_array* container); +// Basic Operation functions +void insertItem(dynamic_array* container, MIME_element* item); +void updateItem(dynamic_array* container, int i, MIME_element* item); +MIME_element* getItem(dynamic_array* container, int i); +void deleteItem(dynamic_array* container, int i); + +void all_MIME_elements_init (void) +{ + all_MIME_elements.mime_count = 0; + arrayInit(&(all_MIME_elements.mime_arr)); +} + +MIME_element* MIME_element_add_with_path (char* fullpath, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int attachment_count, int filecount) +{ + MIME_element *cur = malloc(sizeof(MIME_element)); + char* fn = strrchr(fullpath, '/'); + if (!fn) + fn = fullpath; + + LOGGER_log("%s:%d:%s:start\n",FL); + + insertItem(all_MIME_elements.mime_arr, cur); + cur->hinfo = hinfo; + cur->id = all_MIME_elements.mime_count++; + cur->fullpath = fullpath; + + cur->f = fopen(cur->fullpath,"wb"); + if (cur->f == NULL) { + LOGGER_log("%s:%d:%s:ERROR: cannot open %s for writing",FL,cur->fullpath); + return cur; + } + + LOGGER_log("%s:%d:%s:DEBUG: Decoding [encoding=%d] to %s\n",FL, hinfo->content_transfer_encoding, cur->fullpath); + + if (unpack_metadata != NULL && unpack_metadata->unpack_mode == RIPMIME_UNPACK_MODE_LIST_FILES && cur->f != NULL) { + fprintf (stdout, "%d|%d|%d|%d|%s|%s\n", all_MIME_elements.mime_count, attachment_count, filecount, (hinfo != NULL) ? hinfo->current_recursion_level : "?", (hinfo != NULL) ? hinfo->content_type_string : "", (hinfo != NULL) ? hinfo->filename : fn); + } + return cur; +} + +MIME_element* MIME_element_add (RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int attachment_count, int filecount) +{ + int fullpath_len = strlen(unpack_metadata->dir) + strlen(hinfo->filename) + 3 * sizeof(char); + char *fullpath = (char*)malloc(fullpath_len); + + snprintf(fullpath,fullpath_len,"%s/%s",unpack_metadata->dir,hinfo->filename); + return MIME_element_add_with_path(fullpath, unpack_metadata, hinfo, attachment_count, filecount); +} + +void MIME_element_remove (MIME_element* cur) +{ + LOGGER_log("%s:%d:%s:start\n",FL); + + if (cur->f != NULL) { + fclose(cur->f); + } + free(cur->fullpath); + free(cur); +} + +//------Dynamic array function definitions------ +// Array initialization +void arrayInit(dynamic_array** arr_ptr) +{ + dynamic_array *container; + container = (dynamic_array*)malloc(sizeof(dynamic_array)); + if(!container) { + printf("Memory Allocation Failed\n"); + exit(0); + } + + container->size = 0; + container->capacity = INITIAL_SIZE; + container->array = (MIME_element **)malloc(INITIAL_SIZE * sizeof(MIME_element*)); + if (!container->array){ + printf("Memory Allocation Failed\n"); + exit(0); + } + + *arr_ptr = container; +} + +// Insertion Operation +void insertItem(dynamic_array* container, MIME_element* item) +{ + if (container->size == container->capacity) { + MIME_element **temp = container->array; + container->capacity <<= 1; + container->array = realloc(container->array, container->capacity * sizeof(MIME_element*)); + if(!container->array) { + printf("Out of Memory\n"); + container->array = temp; + return; + } + } + container->array[container->size++] = item; +} + +// Retrieve Item at Particular Index +MIME_element* getItem(dynamic_array* container, int index) +{ + if(index >= container->size) { + printf("Index Out of Bounds\n"); + return NULL; + } + return container->array[index]; +} + +// Update Operation +void updateItem(dynamic_array* container, int index, MIME_element* item) +{ + if (index >= container->size) { + printf("Index Out of Bounds\n"); + return; + } + container->array[index] = item; +} + +// Delete Item from Particular Index +void deleteItem(dynamic_array* container, int index) +{ + if(index >= container->size) { + printf("Index Out of Bounds\n"); + return; + } + + for (int i = index; i < container->size; i++) { + container->array[i] = container->array[i + 1]; + } + container->size--; +} + +// Array Traversal +void printArray(dynamic_array* container) +{ + printf("Array elements: "); + for (int i = 0; i < container->size; i++) { + printf("%p ", container->array[i]); + } + printf("\nSize: "); + printf("%lu", container->size); + printf("\nCapacity: "); + printf("%lu\n", container->capacity); +} + +// Freeing the memory allocated to the array +void freeArray(dynamic_array* container) +{ + free(container->array); + free(container); +} +/*----------END OF MIME.c------------*/ diff --git a/mime_element.h b/mime_element.h new file mode 100644 index 0000000..422e17f --- /dev/null +++ b/mime_element.h @@ -0,0 +1,49 @@ + +#ifndef MIME_ELEMENT +#define MIME_ELEMENT +/* mime_element.h */ + +/* unpack modes */ +#define RIPMIME_UNPACK_MODE_TO_DIRECTORY 0 +#define RIPMIME_UNPACK_MODE_COUNT_FILES 1 +#define RIPMIME_UNPACK_MODE_LIST_FILES 2 + +struct mime_output +{ + char *dir; + int unpack_mode; + // int fragment_number; will be used later +}; +typedef struct mime_output RIPMIME_output; + +#include "strstack.h" +#include "mime_headers.h" + +typedef struct { + struct MIMEH_header_info *hinfo; + int id; + char* fullpath; + FILE* f; + int result; +} MIME_element; + +typedef struct { + size_t size; + size_t capacity; + MIME_element** array; +} dynamic_array; + +typedef struct { + int mime_count; + dynamic_array* mime_arr; +} all_MIME_elements_s; + +all_MIME_elements_s all_MIME_elements; + +void all_MIME_elements_init (void); +MIME_element* MIME_element_add_with_path (char* fullpath, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int attachment_count, int filecount); +MIME_element* MIME_element_add (RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int attachment_count, int filecount); +void MIME_element_remove (MIME_element* cur); +void printArray(dynamic_array* container); + +#endif diff --git a/mime_headers.h b/mime_headers.h index 1ca00c8..87d6436 100644 --- a/mime_headers.h +++ b/mime_headers.h @@ -167,9 +167,6 @@ struct MIMEH_email_info { #endif - - - int MIMEH_version(void); int MIMEH_init( void ); diff --git a/ripOLE/ole.c b/ripOLE/ole.c index 1f393dd..6229159 100644 --- a/ripOLE/ole.c +++ b/ripOLE/ole.c @@ -16,7 +16,7 @@ #include "../ffget.h" #include "../strstack.h" #include "../mime_headers.h" -#include "../mime.h" +#include "../mime_element.h" /** Sector ID values (predefined) **/ #define OLE_SECTORID_FREE -1 /** Unallocated sector **/ @@ -1527,7 +1527,7 @@ int OLE_store_stream( struct OLE_object *ole, char *stream_name, char *directory LOGGER_log("%s:%d:OLE_store_stream:ERROR: Cannot compose full filename string from '%s' and '%s'", FL, directory, stream_name); return -1; } else { - MIME_element* mime_el = MIME_element_add_with_path (full_path, NULL, NULL); + MIME_element* mime_el = MIME_element_add_with_path (full_path, NULL, NULL, 1, 1); size_t written_bytes = fwrite( stream, 1, stream_size, mime_el->f ); if (written_bytes != stream_size) { diff --git a/ripmime.c b/ripmime.c index acb4422..d45c400 100644 --- a/ripmime.c +++ b/ripmime.c @@ -23,6 +23,7 @@ #include "buildcodes.h" #include "logger.h" #include "ffget.h" +#include "mime_element.h" #include "mime.h" #include "strstack.h" #include "mime_headers.h" diff --git a/strstack.c b/strstack.c index 9baeaa0..629877f 100644 --- a/strstack.c +++ b/strstack.c @@ -1,3 +1,5 @@ +/* STRING STACK IMPLEMEMNTATION */ + #include #include #include @@ -18,9 +20,9 @@ /*-----------------------------------------------------------------\ Function Name : SS_init Returns Type : int - ----Parameter List + ----Parameter List 1. void , - ------------------ + ------------------ Exit Codes : Side Effects : -------------------------------------------------------------------- @@ -43,9 +45,9 @@ int SS_init( struct SS_object *ss ) /*-----------------------------------------------------------------\ Function Name : SS_set_verbose Returns Type : int - ----Parameter List + ----Parameter List 1. int level , - ------------------ + ------------------ Exit Codes : Side Effects : -------------------------------------------------------------------- @@ -166,10 +168,8 @@ int SS_dump( struct SS_object *ss ) \------------------------------------------------------------------*/ int SS_push( struct SS_object *ss, char *data, size_t data_length ) { - struct SS_node *node = malloc(sizeof(struct SS_node)); - if (node) { DSS LOGGER_log("%s:%d:SS_push: Pushing %s to %p, stack count = %d",FL,data, ss->stringstack, ss->count); @@ -191,9 +191,9 @@ int SS_push( struct SS_object *ss, char *data, size_t data_length ) /*-----------------------------------------------------------------\ Function Name : *SS_pop Returns Type : char - ----Parameter List + ----Parameter List 1. void , - ------------------ + ------------------ Exit Codes : Side Effects : -------------------------------------------------------------------- @@ -205,7 +205,6 @@ int SS_push( struct SS_object *ss, char *data, size_t data_length ) \------------------------------------------------------------------*/ char *SS_pop( struct SS_object *ss ) { - struct SS_node *node = ss->stringstack; if ((ss->stringstack)&&(ss->count > 0)) @@ -223,9 +222,9 @@ char *SS_pop( struct SS_object *ss ) /*-----------------------------------------------------------------\ Function Name : *SS_top Returns Type : char - ----Parameter List + ----Parameter List 1. void , - ------------------ + ------------------ Exit Codes : Side Effects : -------------------------------------------------------------------- @@ -237,7 +236,6 @@ char *SS_pop( struct SS_object *ss ) \------------------------------------------------------------------*/ char *SS_top( struct SS_object *ss ) { - if (ss->stringstack) { return ss->stringstack->data; @@ -248,9 +246,9 @@ char *SS_top( struct SS_object *ss ) /*-----------------------------------------------------------------\ Function Name : SS_count Returns Type : int - ----Parameter List + ----Parameter List 1. void , - ------------------ + ------------------ Exit Codes : Side Effects : -------------------------------------------------------------------- diff --git a/uuencode.c b/uuencode.c index 0e67578..d14e8df 100644 --- a/uuencode.c +++ b/uuencode.c @@ -19,6 +19,7 @@ The biggest issue is that the interfaces to the decoding functions are too speci #include "pldstr.h" #include "ffget.h" #include "filename-filters.h" +#include "mime_element.h" #include "uuencode.h" @@ -502,7 +503,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, snprintf(fullpath, sizeof(fullpath), "%s/%s", unpack_metadata->dir, bp ); if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Filename = (%s)\n", FL, fullpath); - mime_el = MIME_element_add_with_path (fullpath, unpack_metadata, hinfo); + mime_el = MIME_element_add_with_path (fullpath, unpack_metadata, hinfo, 1, 1); // Allocate the write buffer. By using the write buffer we gain an additional 10% in performance // due to the lack of function call (fwrite) overheads @@ -605,8 +606,8 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, if ((mime_el->f != NULL)&&(wbcount > 0)) { - size_t bc; - bc = fwrite(writebuffer, 1, wbcount, mime_el->f); + size_t bc = fwrite(writebuffer, 1, wbcount, mime_el->f); + if (bc != wbcount) { LOGGER_log("%s:%d:ERROR: Attempted to write %ld bytes, only wrote %ld\n", FL, wbcount, bc); } From 0a2d997a15213dc2b5aa5188b2608546b5a6985f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Sat, 26 Apr 2025 09:34:33 +0300 Subject: [PATCH 33/80] Refactor some warnrd by gcc `snprintf` members to dynamic `malloc` style --- mime.c | 54 +++++++++++++++++++++++++++++++------------------- mime_element.c | 4 ++-- mime_element.h | 1 + tnef/tnef.c | 15 +++++++------- uuencode.c | 33 +++++++++++++++--------------- 5 files changed, 62 insertions(+), 45 deletions(-) diff --git a/mime.c b/mime.c index b516120..82e8ec0 100644 --- a/mime.c +++ b/mime.c @@ -174,7 +174,6 @@ struct MIME_globals { }; static struct MIME_globals glb; -static char scratch[1024]; /*-----------------------------------------------------------------\ Function Name : MIME_version @@ -1670,6 +1669,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH } /* if c was the EOF */ else if (c == '=') { + char line_buf[1025]; // Once we've found a stop char, we can actually just "pad" in the rest // of the stop chars because we know we're at the end. Some MTA's dont // put in enough stopchars... at least it seems X-MIMEOLE: Produced By Microsoft MimeOLE V5.50.4133.2400 @@ -1688,7 +1688,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH // are now only retrieving data byte at a time. // So, now we -absorb- till the end of the line using FFGET_fgets() stopcount = 4 -i; - FFGET_fgets(scratch,sizeof(scratch),f); + FFGET_fgets(line_buf,1024,f); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Stop char detected pos=%d...StopCount = %d\n",FL,i,stopcount); i = 4; break; // out of FOR. @@ -2508,15 +2508,18 @@ int MIME_handle_multipart( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struc result = MIME_decode_encoding( f, unpack_metadata, h, ss ); if (result == 0) { + char * fn; + int fn_l = strlen(unpack_metadata->dir) + strlen(h->filename) + sizeof(char) * 2; + + fn = malloc(fn_l); + snprintf(fn,fn_l,"%s/%s",unpack_metadata->dir,h->filename); // Because we're calling MIME_unpack_single again [ie, recursively calling it // we need to now adjust the input-filename so that it correctly is prefixed // with the directory we unpacked to. - snprintf(scratch,sizeof(scratch),"%s/%s",unpack_metadata->dir, h->filename); - snprintf(h->filename,sizeof(h->filename),"%s",scratch); - - //result = MIME_unpack_single( unpackdir, h->filename, current_recursion_level +1, ss); - result = MIME_unpack_single( unpack_metadata, h->filename, current_recursion_level, ss ); + //result = MIME_unpack_single( unpackdir, fn, current_recursion_level +1, ss); + result = MIME_unpack_single( unpack_metadata, fn, current_recursion_level, ss ); + free(fn); } } // else-if transfer-encoding != B64 && filename was empty @@ -2577,16 +2580,19 @@ int MIME_handle_rfc822( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M result = MIME_decode_encoding( f, unpack_metadata, h, ss ); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Result of extracting %s is %d",FL,h->filename, result); if (result == 0) { + char * fn; + int fn_l = strlen(unpack_metadata->dir) + strlen(h->filename) + sizeof(char) * 2; + + fn = malloc(fn_l); + snprintf(fn,fn_l,"%s/%s",unpack_metadata->dir,h->filename); + /** Because we're calling MIME_unpack_single again [ie, recursively calling it we need to now adjust the input-filename so that it correctly is prefixed with the directory we unpacked to. **/ - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Now attempting to extract contents of '%s'",FL,h->filename); - snprintf(scratch,sizeof(scratch),"%s/%s",unpack_metadata->dir, h->filename); - snprintf(h->filename,sizeof(h->filename),"%s",scratch); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Now attempting to extract contents of '%s'",FL,scratch); - result = MIME_unpack_single( unpack_metadata, scratch, current_recursion_level, ss ); + result = MIME_unpack_single( unpack_metadata, fn, current_recursion_level, ss ); + free(fn); result = 0; } } /** else-if transfer-encoding != B64 && filename was empty **/ @@ -2864,16 +2870,18 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M if (result != 0) return result; // 20040305-1313:PLD else { + char * fn; + int fn_l = strlen(unpack_metadata->dir) + strlen(h->filename) + sizeof(char) * 2; if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Now running ripMIME over decoded RFC822 message...\n",FL); // Because we're calling MIME_unpack_single again [ie, recursively calling it // we need to now adjust the input-filename so that it correctly is prefixed // with the directory we unpacked to. - - snprintf(scratch,sizeof(scratch),"%s/%s",unpack_metadata->dir, h->filename); - snprintf(h->filename,sizeof(h->filename),"%s",scratch); - //result = MIME_unpack_single( unpackdir, h->filename, current_recursion_level +1, ss); - result = MIME_unpack_single( unpack_metadata, h->filename, current_recursion_level,ss ); + fn = malloc(fn_l); + snprintf(fn,fn_l,"%s/%s",unpack_metadata->dir,h->filename); + //result = MIME_unpack_single( unpackdir, fn, current_recursion_level +1, ss); + result = MIME_unpack_single( unpack_metadata, fn, current_recursion_level,ss ); + free(fn); } } // else-if transfer-encoding wasn't B64 and filename was blank @@ -3130,19 +3138,25 @@ int MIME_unpack_single_fp( RIPMIME_output *unpack_metadata, FILE *fi, int curren if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: DumpHeaders = %d\n",FL, glb.save_headers); if ((!hf)&&(glb.save_headers)&&(MIMEH_get_headers_save()==0)) { + char * fn; + int fn_l = strlen(unpack_metadata->dir) + strlen(glb.headersname) + sizeof(char) * 2; + + fn = malloc(fn_l); + snprintf(fn,fn_l,"%s/%s",unpack_metadata->dir,glb.headersname); + // Prepend the unpackdir path to the headers file name - snprintf(scratch,sizeof(scratch),"%s/%s",unpack_metadata->dir, glb.headersname); - hf = fopen(scratch,"w"); + hf = fopen(fn,"w"); if (!hf) { glb.save_headers = 0; - LOGGER_log("%s:%d:%s:ERROR: Cannot open '%s' for writing (%s)",FL, glb.headersname,strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: Cannot open '%s' for writing (%s)", FL, fn, strerror(errno)); } else { headers_save_set_here = 1; MIMEH_set_headers_save(hf); } + free(fn); } if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Setting up streams to decode\n",FL); diff --git a/mime_element.c b/mime_element.c index b81ea23..aaf5b12 100644 --- a/mime_element.c +++ b/mime_element.c @@ -17,7 +17,7 @@ // function prototypes // array container functions void arrayInit(dynamic_array** arr_ptr); -void freeArray(dynamic_array* container); + // Basic Operation functions void insertItem(dynamic_array* container, MIME_element* item); void updateItem(dynamic_array* container, int i, MIME_element* item); @@ -53,7 +53,7 @@ MIME_element* MIME_element_add_with_path (char* fullpath, RIPMIME_output *unpack LOGGER_log("%s:%d:%s:DEBUG: Decoding [encoding=%d] to %s\n",FL, hinfo->content_transfer_encoding, cur->fullpath); if (unpack_metadata != NULL && unpack_metadata->unpack_mode == RIPMIME_UNPACK_MODE_LIST_FILES && cur->f != NULL) { - fprintf (stdout, "%d|%d|%d|%d|%s|%s\n", all_MIME_elements.mime_count, attachment_count, filecount, (hinfo != NULL) ? hinfo->current_recursion_level : "?", (hinfo != NULL) ? hinfo->content_type_string : "", (hinfo != NULL) ? hinfo->filename : fn); + fprintf (stdout, "%d|%d|%d|%d|%s|%s\n", all_MIME_elements.mime_count, attachment_count, filecount, (hinfo != NULL) ? hinfo->current_recursion_level : 0, (hinfo != NULL) ? hinfo->content_type_string : "", (hinfo != NULL) ? hinfo->filename : fn); } return cur; } diff --git a/mime_element.h b/mime_element.h index 422e17f..a5d248b 100644 --- a/mime_element.h +++ b/mime_element.h @@ -45,5 +45,6 @@ MIME_element* MIME_element_add_with_path (char* fullpath, RIPMIME_output *unpack MIME_element* MIME_element_add (RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int attachment_count, int filecount); void MIME_element_remove (MIME_element* cur); void printArray(dynamic_array* container); +void freeArray(dynamic_array* container); #endif diff --git a/tnef/tnef.c b/tnef/tnef.c index c25a662..667b6b7 100644 --- a/tnef/tnef.c +++ b/tnef/tnef.c @@ -278,24 +278,25 @@ Procedure: save_attach_data ID:1 int save_attach_data(char *title, uint8 *tsp, uint32 size, char * file_dir) { FILE *out; - char filename[1024]; + char * fn; + int fn_l = strlen(file_dir) + strlen(title) + sizeof(char) * 2; - snprintf(filename, sizeof(filename),"%s/%s", title ); - - out = fopen(filename, "w"); + fn = malloc(fn_l); + snprintf(fn,fn_l,"%s/%s",file_dir,title); + out = fopen(fn, "w"); if (!out) { - LOGGER_log("%s:%d:TNEF_save_attach_data:ERROR: Failed opening file %s for writing (%s)\n", FL, filename, strerror(errno)); + LOGGER_log("%s:%d:TNEF_save_attach_data:ERROR: Failed opening file %s for writing (%s)\n", FL, fn, strerror(errno)); + free(fn); return -1; } + free(fn); fwrite(tsp, sizeof(uint8), size, out); fclose(out); return 0; } - - /*------------------------------------------------------------------------ Procedure: handle_props ID:1 Purpose: diff --git a/uuencode.c b/uuencode.c index d14e8df..f496abc 100644 --- a/uuencode.c +++ b/uuencode.c @@ -361,7 +361,6 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, char buf[ UUENCODE_STRLEN_MAX ]; char *bp = buf, *fn, *fp; int n, i, expected; - char fullpath[ UUENCODE_STRLEN_MAX ]=""; struct PLD_strtok tx; unsigned char *writebuffer = NULL; unsigned char *wbpos; @@ -385,15 +384,12 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, { if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: NULL FFGET source stream given to us, create our own.\n",FL); - // Setup the full source-data file path. - snprintf(fullpath, sizeof(fullpath),"%s", input_filename ); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Full INPUT file path set as '%s'\n", FL, input_filename); - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Full INPUT file path set as '%s'\n", FL, fullpath); - - inf = fopen(fullpath,"r"); + inf = fopen(input_filename,"r"); if (!inf) { - LOGGER_log("%s:%d:UUENCODE_decode_uu:ERROR: Cannot open file '%s' for reading (%s)", FL, fullpath, strerror(errno)); + LOGGER_log("%s:%d:UUENCODE_decode_uu:ERROR: Cannot open file '%s' for reading (%s)", FL, input_filename, strerror(errno)); uuencode_error = UUENCODE_STATUS_CANNOT_OPEN_FILE; return -1; } @@ -420,7 +416,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, wbcount = 0; } - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Beginning.(%s)\n",FL,fullpath); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Beginning.(%s)\n",FL,input_filename); while (!FFGET_feof(finf)) { @@ -489,6 +485,13 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, if ((filename_found != 0)&&(bp)) { MIME_element* mime_el = NULL; + char * fn; + int fn_l = strlen(unpack_metadata->dir) + strlen(bp) + sizeof(char) * 2; + + fn = malloc(fn_l); + snprintf(fn,fn_l,"%s/%s",unpack_metadata->dir,bp); + + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Located filename (%s), now decoding.\n", FL, bp); // Clean up the file name @@ -498,12 +501,10 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, if (output_filename_supplied == 0) out_filename = strdup(bp); - // Create the new output full path - - snprintf(fullpath, sizeof(fullpath), "%s/%s", unpack_metadata->dir, bp ); - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Filename = (%s)\n", FL, fullpath); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Filename = (%s)\n", FL, fn); - mime_el = MIME_element_add_with_path (fullpath, unpack_metadata, hinfo, 1, 1); + mime_el = MIME_element_add_with_path (fn, unpack_metadata, hinfo, 1, filecount); + free(fn); // Allocate the write buffer. By using the write buffer we gain an additional 10% in performance // due to the lack of function call (fwrite) overheads @@ -520,7 +521,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, if (UUENCODE_DPEDANTIC) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Read line:\n%s",FL,buf); if (FFGET_feof(finf) != 0) { - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:WARNING: Short file (%s)\n",FL, fullpath); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:WARNING: Short file (%s)\n",FL, input_filename); if (writebuffer != NULL) free(writebuffer); uuencode_error = UUENCODE_STATUS_SHORT_FILE; return -1; @@ -621,9 +622,9 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, { if (glb.filename_decoded_report == NULL) { - LOGGER_log("Decoded: %s\n", fullpath); + LOGGER_log("Decoded: %s\n", input_filename); } else { - glb.filename_decoded_report( fullpath, (glb.verbosity_contenttype>0?"uuencoded":NULL) ); + glb.filename_decoded_report( input_filename, (glb.verbosity_contenttype>0?"uuencoded":NULL) ); } } From df0a8ee80fc0610c5523260f2a3bd50f752ed6e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Sat, 26 Apr 2025 09:38:34 +0300 Subject: [PATCH 34/80] TNEF code clenup to C99 --- tnef/tnef.c | 32 -------------------------------- 1 file changed, 32 deletions(-) diff --git a/tnef/tnef.c b/tnef/tnef.c index 667b6b7..64f1980 100644 --- a/tnef/tnef.c +++ b/tnef/tnef.c @@ -75,7 +75,6 @@ struct TNEF_globals { uint8 *tnef_limit; int (*filename_decoded_report)(char *, char *); // Pointer to our filename reporting function - }; static struct TNEF_globals TNEF_glb; @@ -118,7 +117,6 @@ int TNEF_set_verbosity( int level ) return TNEF_glb.verbose; } - /*-----------------------------------------------------------------\ Function Name : TNEF_set_verbosity_contenttype Returns Type : int @@ -159,12 +157,9 @@ int TNEF_set_verbosity_contenttype( int level ) int TNEF_set_filename_report_fn( int (*ptr_to_fn)(char *, char *) ) { TNEF_glb.filename_decoded_report = ptr_to_fn; - return 0; } - - /*------------------------------------------------------------------------ Procedure: TNEF_set_debug ID:1 Purpose: @@ -179,10 +174,6 @@ int TNEF_set_debug( int level ) return TNEF_glb.debug; } - - - - /* Some systems don't like to read unaligned data */ /*------------------------------------------------------------------------ Procedure: read_32 ID:1 @@ -241,8 +232,6 @@ int read_16( uint16 *value, uint8 *tsp) return 0; } - - /*------------------------------------------------------------------------ Procedure: make_string ID:1 Purpose: @@ -255,19 +244,9 @@ char *make_string(uint8 *tsp, int size) static char s[256] = ""; snprintf(s,sizeof(s),"%s",tsp); - - /** 20041106-0929:PLD: Remove this ugly old code and use snprintf() instead - int len = (size>sizeof(s)-1) ? sizeof(s)-1 : size; - - strncpy(s,tsp, len); - s[len] = '\0'; - **/ - return s; } - - /*------------------------------------------------------------------------ Procedure: save_attach_data ID:1 Purpose: @@ -378,13 +357,9 @@ int handle_props(uint8 *tsp, char * file_dir) } x++; } - return 0; } - - - /*------------------------------------------------------------------------ Procedure: default_handler ID:1 Purpose: @@ -418,12 +393,8 @@ int default_handler(uint32 attribute, uint8 *tsp, uint32 size) break; } return 0; - } - - - /*------------------------------------------------------------------------ Procedure: read_attribute ID:1 Purpose: @@ -629,9 +600,6 @@ int read_attribute(uint8 *tsp, char *file_dir) } - - - /*------------------------------------------------------------------------ Procedure: decode_tnef ID:1 Purpose: From 1e95defffc6cb5acc2d3b5dfe58e2167a7d9e6b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Sat, 26 Apr 2025 10:01:59 +0300 Subject: [PATCH 35/80] TNEF debug refactoring to C99 --- tnef/tnef.c | 77 +++++++++++++++++++++-------------------------------- 1 file changed, 31 insertions(+), 46 deletions(-) diff --git a/tnef/tnef.c b/tnef/tnef.c index 64f1980..4d55b22 100644 --- a/tnef/tnef.c +++ b/tnef/tnef.c @@ -38,7 +38,6 @@ ***************************************************************************/ #include -#include #include #include #include @@ -54,7 +53,7 @@ #define VERSION "pldtnef/0.0.2" #ifndef FL -#define FL __FILE__,__LINE__ +#define FL __FILE__, __LINE__, __func__ #endif #define TNEF_VERBOSE ((TNEF_glb.verbose > 0)) @@ -188,7 +187,7 @@ int read_32( uint32 *value, uint8 *tsp) if ((tsp +4) > TNEF_glb.tnef_limit) { - if ((TNEF_VERBOSE)||(TNEF_DEBUG)) LOGGER_log("%s:%d:TNEF_read_32:ERROR: Attempting to read beyond end of memory block",FL); + if ((TNEF_VERBOSE)||(TNEF_DEBUG)) LOGGER_log("%s:%d:%s:ERROR: Attempting to read beyond end of memory block",FL); return -1; } @@ -218,7 +217,7 @@ int read_16( uint16 *value, uint8 *tsp) //PLD-20070707-17H20, how do we even compare tsp to a negative!? ||(tsp == -1) ) { - if ((TNEF_VERBOSE)||(TNEF_DEBUG)) LOGGER_log("%s:%d:TNEF_read_16:ERROR: Attempting to read past end\n",FL); + if ((TNEF_VERBOSE)||(TNEF_DEBUG)) LOGGER_log("%s:%d:%s:ERROR: Attempting to read past end\n",FL); return -1; } @@ -265,7 +264,7 @@ int save_attach_data(char *title, uint8 *tsp, uint32 size, char * file_dir) out = fopen(fn, "w"); if (!out) { - LOGGER_log("%s:%d:TNEF_save_attach_data:ERROR: Failed opening file %s for writing (%s)\n", FL, fn, strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: Failed opening file %s for writing (%s)\n", FL, fn, strerror(errno)); free(fn); return -1; } @@ -420,14 +419,14 @@ int read_attribute(uint8 *tsp, char *file_dir) // Read the attributes of this component - if (TNEF_DEBUG) LOGGER_log("%s:%d:TNEF_read_attribute:DEBUG: Reading Attribute...\n",FL); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Reading Attribute...\n",FL); rv = read_32(&attribute, tsp+bytes); if (rv == -1) return -1; bytes += sizeof(attribute); // Read the size of the information we have to read - if (TNEF_DEBUG) LOGGER_log("%s:%d:TNEF_read_attribute: Reading Size...\n",FL); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s: Reading Size...\n",FL); rv = read_32(&size, tsp+bytes); if (rv == -1) return -1; bytes += sizeof(size); @@ -455,12 +454,12 @@ int read_attribute(uint8 *tsp, char *file_dir) // --END of ammendment. - if (TNEF_DEBUG) LOGGER_log("%s:%d:TNEF_read_attribute:DEBUG: Reading Checksum...(offset %d, bytes=%d)\n", FL, tsp -TNEF_glb.tnef_home, bytes); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Reading Checksum...(offset %d, bytes=%d)\n", FL, tsp -TNEF_glb.tnef_home, bytes); if (read_16(&checksum, tsp+bytes) == -1) return -1; bytes += sizeof(checksum); - if (TNEF_DEBUG) LOGGER_log("%s:%d:TNEF_read_attribute:DEBUG: Decoding attribute %d\n", FL, attribute); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Decoding attribute %d\n", FL, attribute); switch (attribute) { case attNull: @@ -509,7 +508,7 @@ int read_attribute(uint8 *tsp, char *file_dir) } else { - LOGGER_log("%s:%d:TNEF_read_attribute:ERROR: While saving attachment '%s'\n", FL, attach_title); + LOGGER_log("%s:%d:%s:ERROR: While saving attachment '%s'\n", FL, attach_title); } } break; @@ -530,7 +529,7 @@ int read_attribute(uint8 *tsp, char *file_dir) } else { - LOGGER_log("%s:%d:TNEF_read_attribute:ERROR: While saving attachment '%s'\n", FL, attach_title); + LOGGER_log("%s:%d:%s:ERROR: While saving attachment '%s'\n", FL, attach_title); } } break; @@ -615,7 +614,7 @@ int TNEF_decode_tnef(uint8 *tnef_stream, int size, char* file_dir) uint16 tnef_attachkey; uint8 *tsp; - if (TNEF_DEBUG) LOGGER_log("%s:%d:TNEF_decode_tnef:DEBUG: Start. Size = %d\n", FL,size); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Start. Size = %d\n", FL,size); // TSP == TNEF Stream Pointer (well memory block actually!) // @@ -626,9 +625,9 @@ int TNEF_decode_tnef(uint8 *tnef_stream, int size, char* file_dir) ra_response = read_32(&tnefs, tsp); if ((ra_response != -1)&&(TNEF_SIGNATURE == tnefs)) { - if (TNEF_DEBUG) LOGGER_log("%s:%d:TNEF_decode_tnef:DEBUG: TNEF signature is good\n",FL); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: TNEF signature is good\n",FL); } else { - if (TNEF_VERBOSE) LOGGER_log("%s:%d:TNEF_decode_tnef:WARNING: Bad TNEF signature, expecting %lx got %lx\n",FL,TNEF_SIGNATURE,tnefs); + if (TNEF_VERBOSE) LOGGER_log("%s:%d:%s:WARNING: Bad TNEF signature, expecting %lx got %lx\n",FL,TNEF_SIGNATURE,tnefs); } // Move tsp pointer along @@ -637,7 +636,7 @@ int TNEF_decode_tnef(uint8 *tnef_stream, int size, char* file_dir) /** Read the TNEF Attach key **/ if (read_16(&tnef_attachkey, tsp) == -1) return -1; - if (TNEF_DEBUG) LOGGER_log("%s:%d:TNEF_decode_tnef:DEBUG: TNEF Attach Key: %x\n",FL,tnef_attachkey); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: TNEF Attach Key: %x\n",FL,tnef_attachkey); // Move tsp pointer along // @@ -647,10 +646,10 @@ int TNEF_decode_tnef(uint8 *tnef_stream, int size, char* file_dir) // go through entire memory block and extract // all the required attributes and files // - if (TNEF_DEBUG) LOGGER_log("%s:%d:TNEF_decode_tnef:DEBUG: TNEF - Commence reading attributes\n",FL); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: TNEF - Commence reading attributes\n",FL); while ((tsp - tnef_stream) < size) { - if (TNEF_DEBUG) LOGGER_log("%s:%d:TNEF_decode_tnef:DEBUG: Offset = %d\n", FL,tsp -TNEF_glb.tnef_home); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Offset = %d\n", FL,tsp -TNEF_glb.tnef_home); ra_response = read_attribute(tsp, file_dir); if ( ra_response > 0 ) { @@ -660,12 +659,12 @@ int TNEF_decode_tnef(uint8 *tnef_stream, int size, char* file_dir) // Must find out /WHY/ this happens, and, how to rectify the issue. tsp++; - if (TNEF_DEBUG) LOGGER_log("%s:%d:TNEF_decode_tnef:WARNING: TNEF - Attempting to read attribute at %d resulted in a sub-zero response, ending decoding to be safe\n",FL,tsp); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:WARNING: TNEF - Attempting to read attribute at %d resulted in a sub-zero response, ending decoding to be safe\n",FL,tsp); break; } } - if (TNEF_DEBUG) LOGGER_log("%s:%d:TNEF_decode_tnef:DEBUG: Done.\n",FL); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Done.\n",FL); return 0; } @@ -680,77 +679,63 @@ Purpose: Decodes a given TNEF encoded file int TNEF_main( char *filename, char *file_dir ) { FILE *fp; - struct stat sb; uint8 *tnef_stream; int size, nread; - if (TNEF_DEBUG) LOGGER_log("%s:%d:TNEF_main:DEBUG: Start, decoding %s\n",FL, filename); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Start, decoding %s\n",FL, filename); - // Test to see if the file actually exists - // - if (stat(filename,&sb) == -1) + // Attempt to open up the TNEF encoded file... if it fails + // then report the failed condition to syslog + if ((fp = fopen(filename,"r")) == NULL) { - LOGGER_log("%s:%d:TNEF_main:ERROR: while attempting to get details on file %s (%s)\n", FL, filename,strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: opening file %s for reading (%s)\n", FL, filename,strerror(errno)); + if (TNEF_glb.tnef_home) free(TNEF_glb.tnef_home); return -1; } // Get the filesize - // - size = sb.st_size; + fseek(fp, 0L, SEEK_END); + size = ftell(fp); + fseek(fp, 0L, SEEK_SET); // Allocate enough memory to read in the ENTIRE file // FIXME - This could be a real consumer if multiple // instances of TNEF decoding is going on - // TNEF_glb.tnef_home = tnef_stream = (uint8 *)malloc(size); TNEF_glb.tnef_limit = TNEF_glb.tnef_home +size; // If we were unable to allocate enough memory, then we // should report this - // if (tnef_stream == NULL) { - LOGGER_log("%s:%d:TNEF_main:ERROR: When allocating %d bytes for loading file (%s)\n", FL, size,strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: When allocating %d bytes for loading file (%s)\n", FL, size,strerror(errno)); if (TNEF_glb.tnef_home) free(TNEF_glb.tnef_home); return -1; } - // Attempt to open up the TNEF encoded file... if it fails - // then report the failed condition to syslog - // - if ((fp = fopen(filename,"r")) == NULL) - { - LOGGER_log("%s:%d:TNEF_main:ERROR: opening file %s for reading (%s)\n", FL, filename,strerror(errno)); - if (TNEF_glb.tnef_home) free(TNEF_glb.tnef_home); - return -1; - } // Attempt to read in the entire file - // nread = fread(tnef_stream, sizeof(uint8), size, fp); - if (TNEF_DEBUG) LOGGER_log("%s:%d:TNEF_main:DEBUG: Read %d bytes\n", FL, nread); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Read %d bytes\n", FL, nread); // If we did not read in all the bytes, then let syslogs know! - // if (nread < size) { - LOGGER_log("%s:%d:TNEF_main:ERROR: while reading stream from file %s (%s)\n", FL, filename,strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: while reading stream from file %s (%s)\n", FL, filename,strerror(errno)); if (TNEF_glb.tnef_home) free(TNEF_glb.tnef_home); return -1; } // Close the file - // fclose(fp); // Proceed to decode the file - // TNEF_decode_tnef(tnef_stream,size, file_dir); if (TNEF_glb.tnef_home) free(TNEF_glb.tnef_home); - if (TNEF_DEBUG) LOGGER_log("%s:%d:TNEF_main:DEBUG: finished decoding.\n",FL); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: finished decoding.\n",FL); return 0; } From f63c6d3411269c0754165abd6a279c6efd9f841d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Sat, 26 Apr 2025 11:02:31 +0300 Subject: [PATCH 36/80] Refactor MIME headers file output --- mime.c | 4 +- mime_headers.c | 139 +++++++++++-------------------------------------- mime_headers.h | 45 ++++++++-------- 3 files changed, 54 insertions(+), 134 deletions(-) diff --git a/mime.c b/mime.c index 82e8ec0..e53b7e0 100644 --- a/mime.c +++ b/mime.c @@ -2670,7 +2670,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M /** 20041216-1102:PLD: Keep attempting to read headers until we get a sane set **/ do { /** Read next set of headers, repeat until a sane set of headers are found **/ - result = MIMEH_parse_headers(f,h); + result = MIMEH_parse_headers(f,h,unpack_metadata->dir); DMIME LOGGER_log("%s:%d:%s:DEBUG: Parsing of headers done, sanity = %d, result = %d",FL,h->sanity, result); } while ((h->sanity == 0)&&(result != -1)); @@ -2771,7 +2771,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding headers...\n",FL); do { - result = MIMEH_parse_headers(f,h); + result = MIMEH_parse_headers(f,h,unpack_metadata->dir); } while ((h->sanity == 0)&&(result != -1)); glb.header_defect_count += MIMEH_get_defect_count(h); diff --git a/mime_headers.c b/mime_headers.c index d10896a..d7ff5e9 100644 --- a/mime_headers.c +++ b/mime_headers.c @@ -47,6 +47,16 @@ #define DMIMEH if ((glb.debug >= _MIMEH_DEBUG_NORMAL)) +#define max(a,b) \ + ({ __typeof__ (a) _a = (a); \ + __typeof__ (b) _b = (b); \ + _a > _b ? _a : _b; }) + +#define min(a,b) \ + ({ __typeof__ (a) _a = (a); \ + __typeof__ (b) _b = (b); \ + _a < _b ? _a : _b; }) + char *MIMEH_defect_description_array[_MIMEH_DEFECT_ARRAY_SIZE]; struct MIMEH_globals { @@ -74,7 +84,6 @@ struct MIMEH_globals { int header_longsearch; // keep searching until valid headers are found - this is used to filter out qmail bounced emails - breaks RFC's but people are wanting it :-( int longsearch_limit; // how many segments do we attempt to look ahead... - char output_dir[_MIMEH_STRLEN_MAX +1]; FILE *header_file; FILE *original_header_file; int original_header_save_to_file; @@ -118,7 +127,7 @@ int MIMEH_version(void) Changes: \------------------------------------------------------------------*/ -int MIMEH_init( void ) +void MIMEH_init( void ) { glb.doubleCR = 0; glb.headerline = NULL; @@ -137,13 +146,10 @@ int MIMEH_init( void ) glb.header_fix = 1; glb.verbose = 0; glb.verbose_contenttype = 0; - glb.output_dir[0]='\0'; glb.doubleCRname[0]='\0'; glb.appledouble_filename[0]='\0'; glb.header_longsearch=0; glb.longsearch_limit=1; - - return 0; } /*-----------------------------------------------------------------\ @@ -289,26 +295,6 @@ int MIMEH_set_debug( int level ) return glb.debug; } -/*-----------------------------------------------------------------\ - Function Name : MIMEH_set_outputdir - Returns Type : int - ----Parameter List - 1. char *dir , - ------------------ - Exit Codes : - Side Effects : - -------------------------------------------------------------------- -Comments: - --------------------------------------------------------------------- -Changes: -\------------------------------------------------------------------*/ -int MIMEH_set_outputdir( char *dir ) -{ - if (dir) snprintf(glb.output_dir,_MIMEH_STRLEN_MAX,"%s",dir); - return 0; -} - /*-----------------------------------------------------------------\ Function Name : MIMEH_set_webform Returns Type : int @@ -742,7 +728,7 @@ int MIMEH_are_headers_RFC822( char *headers ) Changes: \------------------------------------------------------------------*/ -int MIMEH_save_doubleCR( FFGET_FILE *f ) +int MIMEH_save_doubleCR( FFGET_FILE *f, char * output_dir ) { //char c; int c; @@ -752,7 +738,7 @@ int MIMEH_save_doubleCR( FFGET_FILE *f ) // Determine a file name we can use. do { glb.doubleCR_count++; - snprintf(glb.doubleCRname,_MIMEH_STRLEN_MAX,"%s/doubleCR.%d",glb.output_dir,glb.doubleCR_count); + snprintf(glb.doubleCRname,_MIMEH_STRLEN_MAX,"%s/doubleCR.%d",output_dir,glb.doubleCR_count); } while (stat(glb.doubleCRname, &st) == 0); @@ -1065,7 +1051,7 @@ Purpose: Reads from the stream F until it detects a From line, or a blank Output: Errors: ------------------------------------------------------------------------*/ -int MIMEH_read_headers( struct MIMEH_header_info *hinfo, FFGET_FILE *f ) +int MIMEH_read_headers( struct MIMEH_header_info *hinfo, FFGET_FILE *f, char* output_dir ) { char buffer[_MIMEH_STRLEN_MAX+1]; int totalsize=0; @@ -1240,7 +1226,7 @@ int MIMEH_read_headers( struct MIMEH_header_info *hinfo, FFGET_FILE *f ) { if (glb.doubleCR_save != 0) { - MIMEH_save_doubleCR(f); + MIMEH_save_doubleCR(f, output_dir); glb.doubleCR = 1; } FFGET_doubleCR = 0; @@ -1953,8 +1939,6 @@ int MIMEH_parse_contenttype( char *header_name, char *header_value, struct MIMEH char *p, *q; char *hv = strdup( header_value ); - // CONTENT TYPE ------------------------------- - // CONTENT TYPE ------------------------------- // CONTENT TYPE ------------------------------- DMIMEH LOGGER_log("%s:%d:MIMEH_parse_contenttype:DEBUG: Start",FL); @@ -1993,11 +1977,12 @@ int MIMEH_parse_contenttype( char *header_name, char *header_value, struct MIMEH hinfo->content_type = _CTYPE_APPLICATION_APPLEFILE; if ( hinfo->filename[0] == '\0' ) { - if (strlen(glb.appledouble_filename)>0) + int l = strlen(glb.appledouble_filename); + if (l>0) { - snprintf(hinfo->filename, sizeof(hinfo->filename), "%s.applemeta", glb.appledouble_filename ); + snprintf(hinfo->filename, _MIMEH_FILENAMELEN_MAX, "%s.applemeta", glb.appledouble_filename ); } else { - snprintf(hinfo->filename, sizeof(hinfo->filename), "applefile"); + snprintf(hinfo->filename, _MIMEH_FILENAMELEN_MAX, "applefile"); } } } @@ -2018,7 +2003,6 @@ int MIMEH_parse_contenttype( char *header_name, char *header_value, struct MIMEH FNFILTER_set_mac(hinfo->x_mac); } - // Copy the string to our content-type string storage field p = header_value; if (p != NULL) @@ -2077,11 +2061,8 @@ int MIMEH_parse_contenttype( char *header_name, char *header_value, struct MIMEH if (*param == '\0') break; /** - ** Look for name or filename specifications in the headers - ** Look for name or filename specifications in the headers ** Look for name or filename specifications in the headers **/ - return_value = MIMEH_parse_header_parameter( hinfo, param, "name", hinfo->name, sizeof(hinfo->name), &data_end_point); /** Update param to point where data_end_point is ** this is so when we come around here again due @@ -2119,12 +2100,7 @@ int MIMEH_parse_contenttype( char *header_name, char *header_value, struct MIMEH } /* If a filename was located in the headers */ - - - /** - ** Look for the MIME Boundary specification in the headers - ** Look for the MIME Boundary specification in the headers ** Look for the MIME Boundary specification in the headers **/ return_value = MIMEH_parse_header_parameter(hinfo, param, "boundary", hinfo->boundary, sizeof(hinfo->boundary), &data_end_point); @@ -2169,18 +2145,8 @@ int MIMEH_parse_contenttype( char *header_name, char *header_value, struct MIMEH if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_contenttype:DEBUG: end.",FL); return 0; - } - - - - - - - - - /*-----------------------------------------------------------------\ Function Name : MIMEH_parse_contentlocation Returns Type : int @@ -2203,9 +2169,6 @@ int MIMEH_parse_contentlocation( char *header_name, char *header_value, struct M char *p, *q; // CONTENT LOCATION ------------------------------- - // CONTENT LOCATION ------------------------------- - // CONTENT LOCATION ------------------------------- - PLD_strlower( header_name ); p = strstr(header_name,"content-location"); if (p) @@ -2233,24 +2196,9 @@ int MIMEH_parse_contentlocation( char *header_name, char *header_value, struct M } } - return 0; - } - - - - - - - - - - - - - /*-----------------------------------------------------------------\ Function Name : MIMEH_parse_contenttransferencoding Returns Type : int @@ -2274,10 +2222,6 @@ int MIMEH_parse_contenttransferencoding( char *header_name, char *header_value, char c = '\n'; // CONTENT TRANSFER ENCODING --------------------- - // CONTENT TRANSFER ENCODING --------------------- - // CONTENT TRANSFER ENCODING --------------------- - - p = strstr(header_name,"content-transfer-encoding"); if (p) { @@ -2357,9 +2301,7 @@ int MIMEH_parse_contenttransferencoding( char *header_name, char *header_value, // which use the data. if (q != NULL) *q = c; } - return 0; - } /*-----------------------------------------------------------------\ @@ -2385,9 +2327,6 @@ int MIMEH_parse_contentdisposition( char *header_name, char *header_value, struc char *hv = strdup(header_value); // CONTENT DISPOSITION ------------------------------ - // CONTENT DISPOSITION ------------------------------ - // CONTENT DISPOSITION ------------------------------ - //LOGGER_log("%s:%d:DEBUG: Headers='%s'",FL,header_value); p = strstr(header_name,"content-disposition"); if (p != NULL) @@ -2498,9 +2437,6 @@ int MIMEH_parse_contentdisposition( char *header_name, char *header_value, struc return 0; } - - - /*-----------------------------------------------------------------\ Function Name : MIMEH_parse_subject Returns Type : int @@ -2547,9 +2483,7 @@ int MIMEH_parse_generic( char *header_name, char *header_value, struct MIMEH_hea hinfo->sanity++; break; } - } - return 0; } @@ -2579,9 +2513,6 @@ int MIMEH_parse_subject( char *header_name, char *header_value, struct MIMEH_hea return result; } - - - /*-----------------------------------------------------------------\ Function Name : MIMEH_parse_date Returns Type : int @@ -2604,8 +2535,6 @@ int MIMEH_parse_date( char *header_name, char *header_value, struct MIMEH_header return MIMEH_parse_generic( header_name, header_value, hinfo, "date", hinfo->date, sizeof(hinfo->date) ); } - - /*-----------------------------------------------------------------\ Function Name : MIMEH_parse_from Returns Type : int @@ -2870,7 +2799,6 @@ int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ) MIMEH_parse_received( header_name, header_value, hinfo ); MIMEH_parse_charset( header_name, header_value, hinfo ); - if (hinfo->filename[0] == '\0') { MIMEH_parse_contentlocation( header_name, header_value, hinfo ); @@ -2880,18 +2808,17 @@ int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ) if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_headerss:DEBUG: Header value end position is NULL",FL); } } - - - } // while // Final analysis on our headers: if ( hinfo->content_type == _CTYPE_MULTIPART_APPLEDOUBLE ) { - char tmp[128]; - snprintf( tmp, sizeof(tmp), "mac-%s", hinfo->filename ); - snprintf( hinfo->filename, sizeof(hinfo->filename), "%s", tmp ); - snprintf( hinfo->name, sizeof(hinfo->name), "%s", tmp ); + int len = strlen(hinfo->filename); + char * tmp = malloc(len + 4); + snprintf( tmp, min(_MIMEH_FILENAMELEN_MAX, len + 4), "mac-%s", hinfo->filename ); + strncat(hinfo->filename, tmp, min(_MIMEH_FILENAMELEN_MAX, len)); + strncat(hinfo->name, tmp, min(_MIMEH_STRLEN_MAX, len)); + free(tmp); } // PLD:20031205 @@ -2911,7 +2838,6 @@ int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ) } } - if (safehl) { free(safehl); @@ -2923,7 +2849,6 @@ int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ) return 0; } - /*-----------------------------------------------------------------\ Date Code: : 20081124-184934 Function Name : MIMEH_headers_clearcount @@ -2965,7 +2890,7 @@ int MIMEH_headers_clearcount( struct MIMEH_header_info *hinfo ) { Changes: \------------------------------------------------------------------*/ -int MIMEH_headers_get( struct MIMEH_header_info *hinfo, FFGET_FILE *f ) +int MIMEH_headers_get( struct MIMEH_header_info *hinfo, FFGET_FILE *f, char* output_dir ) { int result = 0; @@ -2997,8 +2922,6 @@ int MIMEH_headers_get( struct MIMEH_header_info *hinfo, FFGET_FILE *f ) if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_headers_get:DEBUG: ffget value of %d offered us no guide for the delimeter", FL, f->linebreak); } - - // Initialise header defects array. hinfo->header_defect_count = 0; memset(hinfo->defects, 0, sizeof(int) *_MIMEH_DEFECT_ARRAY_SIZE); @@ -3008,7 +2931,7 @@ int MIMEH_headers_get( struct MIMEH_header_info *hinfo, FFGET_FILE *f ) // Read from the file, the headers we need FFGET_set_watch_SDL(1); - result = MIMEH_read_headers(hinfo, f); + result = MIMEH_read_headers(hinfo, f, output_dir); FFGET_set_watch_SDL(0); if (hinfo->lf_count > hinfo->crlf_count) { @@ -3084,7 +3007,7 @@ int MIMEH_headers_cleanup( void ) Changes: \------------------------------------------------------------------*/ -int MIMEH_parse_headers( FFGET_FILE *f, struct MIMEH_header_info *hinfo ) +int MIMEH_parse_headers( FFGET_FILE *f, struct MIMEH_header_info *hinfo, char *output_dir ) { int result = 0; DMIMEH LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: Start [F=%p, hinfo=%p]\n", FL, f, hinfo); @@ -3094,7 +3017,7 @@ int MIMEH_parse_headers( FFGET_FILE *f, struct MIMEH_header_info *hinfo ) /** Proceed to read, process and finish headers **/ DMIMEH LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: Getting headers",FL); - if ( result == 0 ) result = MIMEH_headers_get( hinfo, f ); + if ( result == 0 ) result = MIMEH_headers_get( hinfo, f, output_dir ); DMIMEH LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: Processing headers",FL); if ( result == 0 ) result = MIMEH_headers_process( hinfo, glb.headerline ); DMIMEH LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: cleanup of headers",FL); @@ -3186,7 +3109,7 @@ int MIMEH_get_defect_count( struct MIMEH_header_info *hinfo ) Changes: \------------------------------------------------------------------*/ -int MIMEH_read_primary_headers( char *fname, struct MIMEH_header_info *hinfo ) +int MIMEH_read_primary_headers( char *fname, struct MIMEH_header_info *hinfo, char* output_dir ) { FFGET_FILE F; FILE *f; @@ -3198,7 +3121,7 @@ int MIMEH_read_primary_headers( char *fname, struct MIMEH_header_info *hinfo ) } FFGET_setstream(&F,f); - MIMEH_parse_headers(&F, hinfo); + MIMEH_parse_headers(&F, hinfo, output_dir); fclose(f); return 0; diff --git a/mime_headers.h b/mime_headers.h index 87d6436..e9c87d1 100644 --- a/mime_headers.h +++ b/mime_headers.h @@ -98,22 +98,22 @@ struct MIMEH_header_info { - char scratch[_MIMEH_STRLEN_MAX +1]; + char scratch[_MIMEH_STRLEN_MAX + 1]; int content_type; - char content_type_string[ _MIMEH_CONTENT_TYPE_MAX +1 ]; - char content_description_string[ _MIMEH_CONTENT_DESCRIPTION_MAX +1 ]; - char boundary[_MIMEH_STRLEN_MAX +1]; + char content_type_string[ _MIMEH_CONTENT_TYPE_MAX + 1 ]; + char content_description_string[ _MIMEH_CONTENT_DESCRIPTION_MAX + 1 ]; + char boundary[_MIMEH_STRLEN_MAX + 1]; int boundary_located; - char subject[_MIMEH_SUBJECTLEN_MAX +1]; - char filename[_MIMEH_FILENAMELEN_MAX +1]; - char name[_MIMEH_STRLEN_MAX +1]; + char subject[_MIMEH_SUBJECTLEN_MAX + 1]; + char filename[_MIMEH_FILENAMELEN_MAX + 1]; + char name[_MIMEH_STRLEN_MAX + 1]; /** 20041217-1601:PLD: New header fields to keep **/ - char from[_MIMEH_STRLEN_MAX +1]; - char date[_MIMEH_STRLEN_MAX +1]; - char to[_MIMEH_STRLEN_MAX +1]; - char messageid[_MIMEH_STRLEN_MAX +1]; - char received[_MIMEH_STRLEN_MAX +1]; + char from[_MIMEH_STRLEN_MAX + 1]; + char date[_MIMEH_STRLEN_MAX + 1]; + char to[_MIMEH_STRLEN_MAX + 1]; + char messageid[_MIMEH_STRLEN_MAX + 1]; + char received[_MIMEH_STRLEN_MAX + 1]; /** end of new fields **/ // Store multiple filenames @@ -122,14 +122,14 @@ struct MIMEH_header_info struct SS_object ss_names; int content_transfer_encoding; - char content_transfer_encoding_string[ _MIMEH_CONTENT_TRANSFER_ENCODING_MAX +1 ]; + char content_transfer_encoding_string[ _MIMEH_CONTENT_TRANSFER_ENCODING_MAX + 1 ]; int content_disposition; - char content_disposition_string[ _MIMEH_CONTENT_DISPOSITION_MAX +1 ]; + char content_disposition_string[ _MIMEH_CONTENT_DISPOSITION_MAX + 1 ]; //int charset; - char charset[ _MIMEH_CHARSET_MAX +1 ]; + char charset[ _MIMEH_CHARSET_MAX + 1 ]; int format; int file_has_uuencode; - char uudec_name[_MIMEH_FILENAMELEN_MAX +1]; // UUDecode name. This is a post-decode information field. + char uudec_name[_MIMEH_FILENAMELEN_MAX + 1]; // UUDecode name. This is a post-decode information field. int current_recursion_level; // Malformed email reporting @@ -149,17 +149,14 @@ struct MIMEH_header_info int crlf_count; // 200811151149:PLD: Tally's the number of CRLF lines int crcr_count; // 200811151149:PLD: Tally's the number of CRLF lines int lf_count; // 200811151149:PLD: Tally's the number of LF only lines - }; - #ifdef RIPMIME_V2XX struct MIMEH_header_node { struct MIMEH_header_info *header_list; struct MIMEH_header_node *next; }; - struct MIMEH_email_info { char mailpack_name[1024]; struct MIMEH_header_node *headers; @@ -169,7 +166,7 @@ struct MIMEH_email_info { int MIMEH_version(void); -int MIMEH_init( void ); +void MIMEH_init( void ); int MIMEH_set_debug( int level ); int MIMEH_set_verbosity( int level ); int MIMEH_set_verbosity_contenttype( int level ); @@ -199,12 +196,12 @@ char *MIMEH_get_doubleCR_name( void ); int MIMEH_set_header_longsearch( int level ); int MIMEH_headers_clearcount( struct MIMEH_header_info *hinfo ); -int MIMEH_read_headers( struct MIMEH_header_info *hinfo, FFGET_FILE *f ); +int MIMEH_read_headers( struct MIMEH_header_info *hinfo, FFGET_FILE *f, char* output_dir ); -int MIMEH_headers_get( struct MIMEH_header_info *hinfo, FFGET_FILE *f ); +int MIMEH_headers_get( struct MIMEH_header_info *hinfo, FFGET_FILE *f, char* output_dir); int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ); int MIMEH_headers_cleanup(); -int MIMEH_parse_headers( FFGET_FILE *f, struct MIMEH_header_info *hinfo ); +int MIMEH_parse_headers( FFGET_FILE *f, struct MIMEH_header_info *hinfo, char* output_dir ); int MIMEH_display_info( struct MIMEH_header_info *hinfo ); @@ -217,6 +214,6 @@ int MIMEH_get_defect_count( struct MIMEH_header_info *hinfo ); int MIMEH_set_report_MIME( int level ); -int MIMEH_read_primary_headers( char *fname, struct MIMEH_header_info *hinfo ); +int MIMEH_read_primary_headers( char *fname, struct MIMEH_header_info *hinfo, char* output_dir ); #endif From 8db0bc68fe2a7e0f18f35b2fe968bc7b39385da9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Sat, 26 Apr 2025 19:45:14 +0300 Subject: [PATCH 37/80] Big `MIME_element` and file saving refactoring also debug refactoring to C99 `__func__` across many files and some simple code cleanup --- mime.c | 368 +++++++++++++++++++------------------- mime_element.c | 206 +++++++++++---------- mime_element.h | 44 ++--- mime_headers.c | 3 +- mime_headers.h | 1 - ripOLE/Makefile | 2 +- ripOLE/logger.h | 2 +- ripOLE/mime_element.c | 1 + ripOLE/mime_element.h | 1 + ripOLE/ole.c | 303 +++++++++++++++---------------- ripOLE/ole.h | 5 +- ripOLE/olestream-unwrap.c | 102 +++++------ ripOLE/olestream-unwrap.h | 6 +- ripOLE/ripole.c | 7 +- ripmime.c | 4 - tnef/tnef.c | 48 ++--- uuencode.c | 30 ++-- 17 files changed, 547 insertions(+), 586 deletions(-) create mode 120000 ripOLE/mime_element.c create mode 120000 ripOLE/mime_element.h diff --git a/mime.c b/mime.c index e53b7e0..ed7c962 100644 --- a/mime.c +++ b/mime.c @@ -49,6 +49,7 @@ #include "boundary-stack.h" #include "ffget.h" #include "strstack.h" +#include "mime_headers.h" #include "mime_element.h" #include "mime.h" #include "tnef/tnef_api.h" @@ -71,7 +72,7 @@ int MIME_handle_rfc822( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M #define MIME_HEADERS_FILENAME "_headers_" #ifndef FL -#define FL __FILE__, __LINE__, __func__ +#define FL __FILE__,__LINE__ #endif #define _ENC_UNKNOWN 0 @@ -734,7 +735,7 @@ int MIME_set_blankzone_save_option( int option ) break; default: - LOGGER_log("%s:%d:%s:WARNING: Unknown option for saving method (%d). Setting to '%s'",FL, option, MIME_BLANKZONE_FILENAME_DEFAULT ); + LOGGER_log("%s:%d:%s:WARNING: Unknown option for saving method (%d). Setting to '%s'",FL,__func__, option, MIME_BLANKZONE_FILENAME_DEFAULT ); glb.blankzone_save_option = MIME_BLANKZONE_SAVE_FILENAME; snprintf( glb.blankzone_filename, _MIME_STRLEN_MAX, "%s", MIME_BLANKZONE_FILENAME_DEFAULT ); } @@ -855,7 +856,7 @@ int MIME_set_renamemethod( int method ) } else { - LOGGER_log("%s:%d:%s:ERROR: selected method not within %d > x > %d range", FL, _MIME_RENAME_METHOD_INFIX, _MIME_RENAME_METHOD_POSTFIX ); + LOGGER_log("%s:%d:%s:ERROR: selected method not within %d > x > %d range", FL,__func__, _MIME_RENAME_METHOD_INFIX, _MIME_RENAME_METHOD_POSTFIX ); return -1; } @@ -927,7 +928,7 @@ int MIME_test_uniquename( char *path, char *fname, int method ) int randval = get_random_value(); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Start (%s)",FL,fname); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Start (%s)",FL,__func__,fname); frontname = extention = NULL; // shuts the compiler up @@ -1004,7 +1005,7 @@ int MIME_test_uniquename( char *path, char *fname, int method ) PLD_strncpy(fname, frontname, _MIMEH_FILENAMELEN_MAX); //FIXME - this assumes that the buffer space is at least MIME_STRLEN_MAX sized. } - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done (%s)",FL,fname); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done (%s)",FL,__func__,fname); return 0; } @@ -1042,7 +1043,7 @@ int MIME_is_file_RFC822( FILE *f ) line = malloc(sizeof(char) *1025); if (!line) { - LOGGER_log("%s:%d:%s:ERROR: cannot allocate memory for read buffer", FL); + LOGGER_log("%s:%d:%s:ERROR: cannot allocate memory for read buffer", FL,__func__); return -1; } @@ -1057,16 +1058,16 @@ int MIME_is_file_RFC822( FILE *f ) for (result = 0; result < 12; result++) { /** Test for every possible MIME header prefix, ie, From: Subject: etc **/ - if (MIME_DPEDANTIC) LOGGER_log("%s:%d:%s:DEBUG: Testing for '%s' in '%s'", FL, line, conditions[result]); + if (MIME_DPEDANTIC) LOGGER_log("%s:%d:%s:DEBUG: Testing for '%s' in '%s'", FL,__func__, line, conditions[result]); if (strncasecmp(line,conditions[result],strlen(conditions[result]))==0) { /** If we get a match, then increment the hit counter **/ hitcount++; - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Hit on %s",FL,conditions[result]); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Hit on %s",FL,__func__,conditions[result]); if (result == 11) { flag_mime_version = 1; - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Find 'MIME Version' field",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Find 'MIME Version' field",FL,__func__); } } } @@ -1079,7 +1080,7 @@ int MIME_is_file_RFC822( FILE *f ) if (line) free(line); if (MIME_DNORMAL) - LOGGER_log("%s:%d:%s:DEBUG: Hit count = %d, result = %d",FL,hitcount,result); + LOGGER_log("%s:%d:%s:DEBUG: Hit count = %d, result = %d",FL,__func__,hitcount,result); return result; } @@ -1096,14 +1097,14 @@ int MIME_is_diskfile_RFC822( char *fname ) int result = 0; FILE *f; - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Testing %s for RFC822 headers",FL,fname); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Testing %s for RFC822 headers",FL,__func__,fname); f = fopen(fname,"r"); if (!f) { if (glb.quiet == 0) { - LOGGER_log("%s:%d:%s:ERROR: cannot open file '%s' for reading (%s)", FL, fname,strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: cannot open file '%s' for reading (%s)", FL,__func__, fname,strerror(errno)); } return 0; } @@ -1157,7 +1158,7 @@ int MIME_decode_TNEF( RIPMIME_output *unpack_metadata, struct MIMEH_header_info // result = remove( fullpath ); if (result == -1) { - if (MIME_VERBOSE) LOGGER_log("%s:%d:%s: Removing %s/%s failed (%s)",FL,hinfo->filename, unpack_metadata->dir,strerror(errno)); + if (MIME_VERBOSE) LOGGER_log("%s:%d:%s: Removing %s/%s failed (%s)",FL,__func__,hinfo->filename, unpack_metadata->dir,strerror(errno)); } } return result; @@ -1202,12 +1203,12 @@ int MIME_decode_OLE( RIPMIME_output *unpack_metadata, struct MIMEH_header_info * OLE_set_save_unknown_streams(&ole,0); OLE_set_filename_report_fn(&ole, MIME_report_filename_decoded_RIPOLE ); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Starting OLE Decode",FL); - result = OLE_decode_file(&ole, fullpath, unpack_metadata->dir ); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decode done, cleaning up.",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Starting OLE Decode",FL,__func__); + result = OLE_decode_file(&ole, fullpath, unpack_metadata ); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decode done, cleaning up.",FL,__func__); OLE_decode_file_done(&ole); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decode returned with code = %d",FL,result); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decode returned with code = %d",FL,__func__,result); return result; } #endif @@ -1238,44 +1239,44 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME * --binary flag */ - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Start\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Start\n",FL,__func__); - cur_mime = MIME_element_add (unpack_metadata, hinfo, glb.attachment_count, glb.filecount); + cur_mime = MIME_element_add (NULL, unpack_metadata, hinfo->filename, hinfo->content_type_string, hinfo->content_transfer_encoding_string, hinfo->name, hinfo->current_recursion_level, glb.attachment_count, glb.filecount); while ((readcount=FFGET_raw(f, (unsigned char *) buffer,bufsize)) > 0) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: BUFFER[%p]= '%s'\n",FL,buffer, buffer); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: BUFFER[%p]= '%s'\n",FL,__func__,buffer, buffer); if ((!file_has_uuencode)&&(UUENCODE_is_uuencode_header( buffer ))) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: UUENCODED is YES (buffer=[%p]\n",FL,buffer); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: UUENCODED is YES (buffer=[%p]\n",FL,__func__,buffer); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: File contains UUENCODED data(%s)\n",FL,buffer); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: File contains UUENCODED data(%s)\n",FL,__func__,buffer); file_has_uuencode = 1; } if (BS_cmp(buffer, readcount)) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Boundary located - breaking out.\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Boundary located - breaking out.\n",FL,__func__); break; } else { - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: writing: %s\n",FL, buffer); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: writing: %s\n",FL,__func__, buffer); fwrite( buffer, readcount, 1, cur_mime->f); } } - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Completed reading RAW data\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Completed reading RAW data\n",FL,__func__); free(buffer); MIME_element_remove(cur_mime); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Closed file and free'd buffer\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Closed file and free'd buffer\n",FL,__func__); // If there was UUEncoded portions [potentially] in the email, the // try to extract them using the MIME_decode_uu() if (file_has_uuencode) { char full_decode_path[512]; snprintf(full_decode_path,sizeof(full_decode_path),"%s/%s",unpack_metadata->dir,hinfo->filename); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UUencoded data\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UUencoded data\n",FL,__func__); if ( hinfo->content_transfer_encoding == _CTRANS_ENCODING_UUENCODE ) decode_entire_file = 0; //result = UUENCODE_decode_uu(NULL, hinfo->filename, hinfo->uudec_name, decode_entire_file, keep ); @@ -1286,17 +1287,17 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME case UUENCODE_STATUS_SHORT_FILE: case UUENCODE_STATUS_CANNOT_OPEN_FILE: case UUENCODE_STATUS_CANNOT_FIND_FILENAME: - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Nullifying uuencode_error result %d",FL, uuencode_error); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Nullifying uuencode_error result %d",FL,__func__, uuencode_error); result = 0; break; case UUENCODE_STATUS_CANNOT_ALLOCATE_MEMORY: - LOGGER_log("%s:%d:%s:ERROR: Failure to allocate memory for UUdecode process",FL); + LOGGER_log("%s:%d:%s:ERROR: Failure to allocate memory for UUdecode process",FL,__func__); return -1; break; default: - LOGGER_log("%s:%d:%s:ERROR: Unknown return code from UUDecode process [%d]",FL,uuencode_error); + LOGGER_log("%s:%d:%s:ERROR: Unknown return code from UUDecode process [%d]",FL,__func__,uuencode_error); return -1; } } @@ -1306,15 +1307,15 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME { if (strcasecmp(hinfo->uudec_name,"winmail.dat")==0) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding TNEF format\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding TNEF format\n",FL,__func__); snprintf(hinfo->filename, 128, "%s", hinfo->uudec_name); MIME_decode_TNEF( unpack_metadata, hinfo, keep); } - else LOGGER_log("%s:%d:%s:WARNING: hinfo has been clobbered.\n",FL); + else LOGGER_log("%s:%d:%s:WARNING: hinfo has been clobbered.\n",FL,__func__); } } - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: End[result = %d]\n",FL,result); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: End[result = %d]\n",FL,__func__,result); return result; } @@ -1338,14 +1339,13 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM int decodesize=0; MIME_element* cur_mime = NULL; - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding TEXT [encoding=%d] to %s\n",FL, hinfo->content_transfer_encoding, hinfo->filename); - - cur_mime = MIME_element_add (unpack_metadata, hinfo, glb.attachment_count, glb.filecount); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding TEXT [encoding=%d] to %s\n",FL,__func__, hinfo->content_transfer_encoding, hinfo->filename); + cur_mime = MIME_element_add (NULL, unpack_metadata, hinfo->filename, hinfo->content_type_string, hinfo->content_transfer_encoding_string, hinfo->name, hinfo->current_recursion_level, glb.attachment_count, glb.filecount); if (!f) { /** If we cannot open the file for reading, leave an error and return -1 **/ - LOGGER_log("%s:%d:%s:ERROR: print-quotable input stream broken.",FL); + LOGGER_log("%s:%d:%s:ERROR: print-quotable input stream broken.",FL,__func__); return _EXITERR_MIMEREAD_CANNOT_OPEN_INPUT; } if (f) @@ -1354,15 +1354,15 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM { int line_len = strlen(line); linecount++; - // if (MIME_DPEDANTIC) LOGGER_log("%s:%d:%s:DEBUG: line=%s",FL,line); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: line[len=%d]=%s",FL,line_len,line); + // if (MIME_DPEDANTIC) LOGGER_log("%s:%d:%s:DEBUG: line=%s",FL,__func__,line); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: line[len=%d]=%s",FL,__func__,line_len,line); //20041217-1529:PLD: if (line[0] == '-') { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_DNORMAL:DEBUG: Testing boundary",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_DNORMAL:DEBUG: Testing boundary",FL,__func__); if ((BS_count() > 0)&&(BS_cmp(line,line_len))) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_DNORMAL:DEBUG: Hit a boundary on the line",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_DNORMAL:DEBUG: Hit a boundary on the line",FL,__func__); lastlinewasboundary = 1; result = 0; break; @@ -1373,7 +1373,7 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM { if (hinfo->content_transfer_encoding == _CTRANS_ENCODING_QP) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_DNORMAL:DEBUG: Hit a boundary on the line",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_DNORMAL:DEBUG: Hit a boundary on the line",FL,__func__); decodesize = MDECODE_decode_qp_text(line); fwrite(line, 1, decodesize, cur_mime->f); @@ -1383,14 +1383,14 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM if ((!file_has_uuencode)&&( UUENCODE_is_uuencode_header( line ))) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: UUENCODED data located in file.\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: UUENCODED data located in file.\n",FL,__func__); file_has_uuencode = 1; } } // linecount++; - if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_DNORMAL:DEBUG: End processing line.",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:MIME_DNORMAL:DEBUG: End processing line.",FL,__func__); } // while - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done writing output file '%s'...now attempting to close.",FL, cur_mime->fullpath); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done writing output file '%s'...now attempting to close.",FL,__func__, cur_mime->fullpath); MIME_element_remove(cur_mime); @@ -1399,7 +1399,7 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM result = MIME_STATUS_ZERO_FILE; return result; } - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Closed.",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Closed.",FL,__func__); } // if main input file stream was open // If our input from the file was invalid due to EOF or other @@ -1407,7 +1407,7 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM // occured. // if (!get_result) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: FFGET module ran out of file data while attempting to decode",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: FFGET module ran out of file data while attempting to decode",FL,__func__); // result = -1; result = MIME_ERROR_FFGET_EMPTY; // 20040305-1323:PLD } @@ -1423,7 +1423,7 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM // Make sure uudec_name is blank too // hinfo->uudec_name[0] = '\0'; - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UUencoded data in file '%s'\n",FL,hinfo->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UUencoded data in file '%s'\n",FL,__func__,hinfo->filename); //result = UUENCODE_decode_uu( NULL, info->filename, hinfo->uudec_name, 1, keep ); // Attempt to decode the UUENCODED data in the file, // NOTE - hinfo->uudec_name is a blank buffer which will be filled by the UUENCODE_decode_uu @@ -1441,30 +1441,30 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM case UUENCODE_STATUS_SHORT_FILE: case UUENCODE_STATUS_CANNOT_OPEN_FILE: case UUENCODE_STATUS_CANNOT_FIND_FILENAME: - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Nullifying uuencode_error result %d",FL, uuencode_error); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Nullifying uuencode_error result %d",FL,__func__, uuencode_error); result = 0; break; case UUENCODE_STATUS_CANNOT_ALLOCATE_MEMORY: - LOGGER_log("%s:%d:%s:ERROR: Failure to allocate memory for UUdecode process",FL); + LOGGER_log("%s:%d:%s:ERROR: Failure to allocate memory for UUdecode process",FL,__func__); return -1; break; default: - LOGGER_log("%s:%d:%s:ERROR: Unknown return code from UUDecode process [%d]",FL,uuencode_error); + LOGGER_log("%s:%d:%s:ERROR: Unknown return code from UUDecode process [%d]",FL,__func__,uuencode_error); return -1; } } if ( result > 0 ) { glb.attachment_count += result; result = 0; } - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: hinfo = %p\n",FL,hinfo); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done. [ UUName = '%s' ]\n",FL,hinfo->uudec_name); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: hinfo = %p\n",FL,__func__,hinfo); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done. [ UUName = '%s' ]\n",FL,__func__,hinfo->uudec_name); if (strncasecmp(hinfo->uudec_name,"winmail.dat",11)==0) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding TNEF format\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding TNEF format\n",FL,__func__); snprintf(hinfo->filename, 128, "%s", hinfo->uudec_name); MIME_decode_TNEF( unpack_metadata, hinfo, keep ); } - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Completed decoding UUencoded data.\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Completed decoding UUencoded data.\n",FL,__func__); } - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: result=%d ----------------Done\n",FL,result); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: result=%d ----------------Done\n",FL,__func__,result); return result; } @@ -1508,9 +1508,9 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH int loop; MIME_element* cur_mime = NULL; - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: attempting to decode '%s'", FL, hinfo->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: attempting to decode '%s'", FL,__func__, hinfo->filename); - cur_mime = MIME_element_add (unpack_metadata, hinfo, glb.attachment_count, glb.filecount); + cur_mime = MIME_element_add (NULL, unpack_metadata, hinfo->filename, hinfo->content_type_string, hinfo->content_transfer_encoding_string, hinfo->name, hinfo->current_recursion_level, glb.attachment_count, glb.filecount); if (cur_mime->f == NULL) { return -1; @@ -1521,7 +1521,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH writebuffer = malloc( _MIME_WRITE_BUFFER_SIZE *sizeof(unsigned char)); if (!writebuffer) { - LOGGER_log("%s:%d:%s:ERROR: cannot allocate %dbytes of memory for the write buffer",FL, _MIME_WRITE_BUFFER_SIZE); + LOGGER_log("%s:%d:%s:ERROR: cannot allocate %dbytes of memory for the write buffer",FL,__func__, _MIME_WRITE_BUFFER_SIZE); return -1; } else { @@ -1531,7 +1531,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH /* Set our ignore_crcount flag */ if (BS_count() > 0) { - //LOGGER_log("%s:%d:%s:DEBUG: Ignore CR set to 1",FL); + //LOGGER_log("%s:%d:%s:DEBUG: Ignore CR set to 1",FL,__func__); ignore_crcount = 1; } /* collect prefixing trash (if any, such as spaces, CR's etc)*/ @@ -1590,7 +1590,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH { int hit = 0; char *p; - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: leader '-' found at %50s",FL,(f->startpoint -1)); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: leader '-' found at %50s",FL,__func__,(f->startpoint -1)); p = strchr((f->startpoint -1), '\n'); if (p == NULL) { /* The boundary test was failing because sometimes the full line @@ -1599,16 +1599,16 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH */ char scratch[1024]; FFGET_fgets(scratch,sizeof(scratch), f); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Scratch = '%s'", FL, scratch); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Scratch = '%s'", FL,__func__, scratch); hit = BS_cmp(scratch,strlen(scratch) +1); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Boundary hit = %d", FL, hit); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Boundary hit = %d", FL,__func__, hit); } else { *p = '\0'; hit = BS_cmp((f->startpoint -1),strlen(f->startpoint) +1); *p = '\n'; } if (hit > 0) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Boundary detected and breaking out ",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Boundary detected and breaking out ",FL,__func__); // FFGET_fgets(scratch,sizeof(scratch),f); // eom_reached = 1; boundary_crash = 1; @@ -1643,7 +1643,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH // CR's ). if (cr_count > 2) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: EOF Reached due to two consecutive CR's on line %d\n",FL,cr_total); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: EOF Reached due to two consecutive CR's on line %d\n",FL,__func__,cr_total); eom_reached++; break; } @@ -1659,7 +1659,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH /* if we get an EOF char, then we know something went wrong */ if ( c == EOF ) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: input stream broken for base64 decoding for file %s. %ld bytes of data in buffer to be written out\n",FL,hinfo->filename,wbcount); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: input stream broken for base64 decoding for file %s. %ld bytes of data in buffer to be written out\n",FL,__func__,hinfo->filename,wbcount); status = MIME_ERROR_B64_INPUT_STREAM_EOF; fwrite(writebuffer, 1, wbcount, cur_mime->f); MIME_element_remove(cur_mime); @@ -1689,7 +1689,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH // So, now we -absorb- till the end of the line using FFGET_fgets() stopcount = 4 -i; FFGET_fgets(line_buf,1024,f); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Stop char detected pos=%d...StopCount = %d\n",FL,i,stopcount); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Stop char detected pos=%d...StopCount = %d\n",FL,__func__,i,stopcount); i = 4; break; // out of FOR. } @@ -1743,7 +1743,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH // tally up our total byte conversion count bytecount+=(3 -stopcount); } - else if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: could not attain 4 bytes input\n",FL); + else if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: could not attain 4 bytes input\n",FL,__func__); // if we wrote less than 3 chars, it means we were at the end of the encoded file thus we exit if ((eom_reached)||(stopcount > 0)||(boundary_crash)||(i!=4)) @@ -1767,7 +1767,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH // Absorb to end of line status = MIME_BASE64_STATUS_HIT_BOUNDARY; // was _BOUNDARY_CRASH } - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: File size = %ld bytes, Exit Status = %d, Boundary Crash = %d\n",FL, bytecount, status, boundary_crash); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: File size = %ld bytes, Exit Status = %d, Boundary Crash = %d\n",FL,__func__, bytecount, status, boundary_crash); if (writebuffer) free(writebuffer); return status; } // if End-of-MIME or Stopchars appeared @@ -1776,7 +1776,6 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH return status; } - /*-----------------------------------------------------------------\ Function Name : MIME_decode_64_cleanup Returns Type : int @@ -1831,11 +1830,11 @@ int MIME_doubleCR_decode( char *filename, RIPMIME_output *unpack_metadata, struc p = filename; // * Initialise the header fields h.uudec_name[0] = '\0'; - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: filename=%s, path=%s, recursion=%d", FL, filename, unpack_metadata->dir, current_recursion_level ); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: filename=%s, path=%s, recursion=%d", FL,__func__, filename, unpack_metadata->dir, current_recursion_level ); memcpy(&h, hinfo, sizeof(h)); // Works for ripMIME snprintf(h.filename, sizeof(h.filename), "%s/%s", unpackdir, p); snprintf(h.filename, sizeof(h.filename), "%s", p); /// Works for Xamime - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: header.filename = %s", FL, h.filename ); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: header.filename = %s", FL,__func__, h.filename ); if (MIME_is_diskfile_RFC822(filename)) { if (MIME_VERBOSE) LOGGER_log("Attempting to decode Double-CR delimeted MIME attachment '%s'\n",filename); @@ -1882,7 +1881,7 @@ size_t MIME_read_raw( char *src_mpname, char *dest_mpname, size_t rw_buffer_size } else { input_f = fopen(src_mpname, "r"); if (input_f == NULL) { - LOGGER_log("%s:%d:%s:ERROR: Cannot open '%s' for reading (%s)", FL, src_mpname, strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: Cannot open '%s' for reading (%s)", FL,__func__, src_mpname, strerror(errno)); return -1; } } @@ -1890,14 +1889,14 @@ size_t MIME_read_raw( char *src_mpname, char *dest_mpname, size_t rw_buffer_size rw_buffer = malloc( (rw_buffer_size +1) *sizeof(char) ); if ( !rw_buffer ) { - LOGGER_log("%s:%d:%s:ERROR: could not allocate %d of memory for file read buffer\n",FL, rw_buffer_size ); + LOGGER_log("%s:%d:%s:ERROR: could not allocate %d of memory for file read buffer\n",FL,__func__, rw_buffer_size ); return -1; } /* open up our input file */ result_f = fopen(dest_mpname, "w"); if (result_f == NULL) { - LOGGER_log("%s:%d:%s:ERROR: Cannot open '%s' for writing. (%s)",FL, dest_mpname, strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: Cannot open '%s' for writing. (%s)",FL,__func__, dest_mpname, strerror(errno)); return -1; } @@ -1908,12 +1907,12 @@ size_t MIME_read_raw( char *src_mpname, char *dest_mpname, size_t rw_buffer_size if (readcount > 0) { writecount = fwrite(rw_buffer, readcount, 1, result_f ); if (writecount == -1) { - LOGGER_log("%s:%d:%s:ERROR: While attempting to write data to '%s' (%s)", FL, dest_mpname, strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: While attempting to write data to '%s' (%s)", FL,__func__, dest_mpname, strerror(errno)); return -1; } if ( readcount != writecount ) { - LOGGER_log("%s:%d:%s:ERROR: Attempted to write %d bytes, but only managed %d to file '%s'",FL, readcount, writecount, dest_mpname ); + LOGGER_log("%s:%d:%s:ERROR: Attempted to write %d bytes, but only managed %d to file '%s'",FL,__func__, readcount, writecount, dest_mpname ); } fsize += writecount; } @@ -1923,7 +1922,7 @@ size_t MIME_read_raw( char *src_mpname, char *dest_mpname, size_t rw_buffer_size fclose(input_f); } if (readcount == -1) { - LOGGER_log("%s:%d:%s:ERROR: read() '%s'",FL, strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: read() '%s'",FL,__func__, strerror(errno)); return -1; } fclose(result_f); @@ -1954,7 +1953,7 @@ int MIME_read( char *mpname ) buffer = malloc( MIME_MIME_READ_BUFFER_SIZE *sizeof(char) ); if ( !buffer ) { - LOGGER_log("%s:%d:%s:ERROR: could not allocate 4K of memory for file read buffer\n",FL ); + LOGGER_log("%s:%d:%s:ERROR: could not allocate 4K of memory for file read buffer\n",FL,__func__ ); return -1; } /* open up our input file */ @@ -1962,7 +1961,7 @@ int MIME_read( char *mpname ) /* check that out file opened up okay */ if (!fout) { - LOGGER_log("%s:%d:%s:ERROR: Cannot open file %s for writing... check permissions perhaps?",FL,mpname); + LOGGER_log("%s:%d:%s:ERROR: Cannot open file %s for writing... check permissions perhaps?",FL,__func__,mpname); //exit(_EXITERR_MIMEREAD_CANNOT_OPEN_OUTPUT); return -1; } @@ -1981,7 +1980,7 @@ int MIME_read( char *mpname ) if ( readcount != writecount ) { - LOGGER_log("%s:%d:%s:ERROR: Attempted to write %d bytes, but only managed %d to file '%s'",FL, readcount, writecount, mpname ); + LOGGER_log("%s:%d:%s:ERROR: Attempted to write %d bytes, but only managed %d to file '%s'",FL,__func__, readcount, writecount, mpname ); } } else { @@ -2081,7 +2080,7 @@ int MIME_generate_multiple_hardlink_filenames(struct MIMEH_header_info *hinfo, R if (glb.multiple_filenames == 0) return 0; - //LOGGER_log("%s:%d:MIME_generate_multiple_hardlink_filenames:DEBUG: Generating hardlinks for %s",FL, hinfo->filename); + //LOGGER_log("%s:%d:MIME_generate_multiple_hardlink_filenames:DEBUG: Generating hardlinks for %s",FL,__func__, hinfo->filename); snprintf(oldname,sizeof(oldname),"%s/%s",unpack_metadata->dir, hinfo->filename); if (SS_count(&(hinfo->ss_names)) > 1){ @@ -2099,13 +2098,13 @@ int MIME_generate_multiple_hardlink_filenames(struct MIMEH_header_info *hinfo, R if (np) np++; else np = name; snprintf(newname,sizeof(newname),"%s/%s",unpack_metadata->dir, np); - //LOGGER_log("%s:%d:MIME_generate_multiple_hardlink_filenames:DEBUG: Linking %s->%s",FL,newname, oldname); + //LOGGER_log("%s:%d:MIME_generate_multiple_hardlink_filenames:DEBUG: Linking %s->%s",FL,__func__,newname, oldname); rv = link(oldname, newname); if (rv == -1) { if (errno != EEXIST) { - LOGGER_log("%s:%d:%s:WARNING: While trying to create '%s' link to '%s' (%s)",FL, newname, oldname,strerror(errno)); + LOGGER_log("%s:%d:%s:WARNING: While trying to create '%s' link to '%s' (%s)",FL,__func__, newname, oldname,strerror(errno)); } } else { @@ -2129,13 +2128,13 @@ int MIME_generate_multiple_hardlink_filenames(struct MIMEH_header_info *hinfo, R int rv; snprintf(newname,sizeof(newname),"%s/%s",unpack_metadata->dir, name); - //LOGGER_log("%s:%d:MIME_generate_multiple_hardlink_filenames:DEBUG: Linking %s->%s",FL,newname, oldname); + //LOGGER_log("%s:%d:MIME_generate_multiple_hardlink_filenames:DEBUG: Linking %s->%s",FL,__func__,newname, oldname); rv = link(oldname, newname); if (rv == -1) { if (errno != EEXIST) { - LOGGER_log("%s:%d:%s:WARNING: While trying to create '%s' link to '%s' (%s)",FL, newname, oldname,strerror(errno)); + LOGGER_log("%s:%d:%s:WARNING: While trying to create '%s' link to '%s' (%s)",FL,__func__, newname, oldname,strerror(errno)); } } else { @@ -2167,13 +2166,13 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct int keep = 1; int result = -1; - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Start:DEBUG: (%s)\n",FL, hinfo->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Start:DEBUG: (%s)\n",FL,__func__, hinfo->filename); // If we have a valid filename, then put it through the process of // cleaning and filtering if (isprint((int)hinfo->filename[0])) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Filename is valid, cleaning\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Filename is valid, cleaning\n",FL,__func__); FNFILTER_filter(hinfo->filename, _MIMEH_FILENAMELEN_MAX); /* check out thefilename for ISO filenames */ } @@ -2204,7 +2203,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct // then we'll simply use the blankfileprefix. snprintf( hinfo->filename, _MIMEH_FILENAMELEN_MAX, "%s%d", glb.blankfileprefix, glb.filecount ); } - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Filename is empty, setting to default...(%s)\n",FL, hinfo->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Filename is empty, setting to default...(%s)\n",FL,__func__, hinfo->filename); if (glb.no_nameless) keep = 0; glb.filecount++; } @@ -2249,7 +2248,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct // The format of the evaluation is: // // (logic-test?true-expression:false-expression) - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: About to execute callback [0x%p]",FL,glb.filename_decoded_reporter); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: About to execute callback [0x%p]",FL,__func__,glb.filename_decoded_reporter); if (glb.filename_decoded_reporter != NULL) { glb.filename_decoded_reporter( hinfo->filename, (MIMEH_get_verbosity_contenttype()?hinfo->content_type_string:NULL)); @@ -2263,7 +2262,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct /** Find the start of the filename. **/ fp = strrchr(hinfo->filename, '/'); if (fp) fp++; else fp = hinfo->filename; - //LOGGER_log("%s:%d:%s:DEBUG: Pushing filename %s to the stack",FL,fp); + //LOGGER_log("%s:%d:%s:DEBUG: Pushing filename %s to the stack",FL,__func__,fp); // 20040305-1419:PLD // Store the filename we're going to use to save the file to in the filename stack SS_push(ss, fp, strlen(fp)); @@ -2271,11 +2270,11 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct // Select the decoding method based on the content transfer encoding // method which we read from the headers - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: ENCODING = %d\n",FL, hinfo->content_transfer_encoding); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: ENCODING = %d\n",FL,__func__, hinfo->content_transfer_encoding); switch (hinfo->content_transfer_encoding) { case _CTRANS_ENCODING_B64: - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding BASE64 format\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding BASE64 format\n",FL,__func__); result = MIME_decode_64(f, unpack_metadata, hinfo); switch (result) { case MIME_ERROR_B64_INPUT_STREAM_EOF: @@ -2291,24 +2290,24 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct } break; case _CTRANS_ENCODING_7BIT: - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding 7BIT format\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding 7BIT format\n",FL,__func__); result = MIME_decode_text(f, unpack_metadata, hinfo, keep); break; case _CTRANS_ENCODING_8BIT: - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding 8BIT format\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding 8BIT format\n",FL,__func__); result = MIME_decode_text(f, unpack_metadata, hinfo, keep); break; case _CTRANS_ENCODING_BINARY: case _CTRANS_ENCODING_RAW: - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding RAW format\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding RAW format\n",FL,__func__); result = MIME_decode_raw(f, unpack_metadata, hinfo, keep); break; case _CTRANS_ENCODING_QP: - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding Quoted-Printable format\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding Quoted-Printable format\n",FL,__func__); result = MIME_decode_text(f, unpack_metadata, hinfo, keep); break; case _CTRANS_ENCODING_UUENCODE: - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UUENCODED format\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UUENCODED format\n",FL,__func__); // Added as a test - remove if we can get this to work in a better way snprintf(hinfo->uudec_name,sizeof(hinfo->uudec_name),"%s",hinfo->filename); result = UUENCODE_decode_uu(f, hinfo->filename, hinfo->uudec_name, 0, keep, unpack_metadata, hinfo ); @@ -2320,19 +2319,19 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct case _CTRANS_ENCODING_UNKNOWN: switch (hinfo->content_disposition) { case _CDISPOSITION_FORMDATA: - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UNKNOWN format of FORMDATA disposition\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UNKNOWN format of FORMDATA disposition\n",FL,__func__); result = MIME_decode_raw(f, unpack_metadata, hinfo, keep); break; default: - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UNKNOWN format\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UNKNOWN format\n",FL,__func__); result = MIME_decode_text(f, unpack_metadata, hinfo, keep); } - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: UNKNOWN Decode completed, result = %d\n",FL,result); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: UNKNOWN Decode completed, result = %d\n",FL,__func__,result); break; case _CTRANS_ENCODING_UNSPECIFIED: - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UNSPECIFIED format\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UNSPECIFIED format\n",FL,__func__); result = MIME_decode_text(f, unpack_metadata, hinfo, keep); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding result for UNSPECIFIED format = %d\n",FL, result); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding result for UNSPECIFIED format = %d\n",FL,__func__, result); // 20040114-1236:PLD: Added nested mail checking // // Sometimes mailpacks have false headers at the start, resulting @@ -2347,7 +2346,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct if (1) { snprintf(hinfo->scratch,sizeof(hinfo->scratch),"%s/%s",unpack_metadata->dir,hinfo->filename); - LOGGER_log("%s:%d:%s:DEBUG:REMOVEME: Testing for RFC822 headers in file %s",FL,hinfo->scratch); + LOGGER_log("%s:%d:%s:DEBUG:REMOVEME: Testing for RFC822 headers in file %s",FL,__func__,hinfo->scratch); if (MIME_is_diskfile_RFC822(hinfo->scratch) > 0 ) { // 20040305-1304:PLD: unpack the file, propagate result upwards @@ -2356,7 +2355,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct } break; default: - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding format is not defined (%d)\n",FL, hinfo->content_transfer_encoding); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding format is not defined (%d)\n",FL,__func__, hinfo->content_transfer_encoding); result = MIME_decode_raw(f, unpack_metadata, hinfo, keep); break; } @@ -2399,7 +2398,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct // side-effects if (hinfo->content_type == _CTYPE_TNEF) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding TNEF format\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding TNEF format\n",FL,__func__); glb.attachment_count++; MIME_decode_TNEF( unpack_metadata, hinfo, 0 ); } // Decode TNEF @@ -2414,7 +2413,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct if (glb.decode_mht != 0) { // Patched 26-01-03: supplied by Chris Hine - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Microsoft MHT format email filename='%s'\n",FL, hinfo->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Microsoft MHT format email filename='%s'\n",FL,__func__, hinfo->filename); snprintf(hinfo->scratch,sizeof(hinfo->scratch),"%s/%s",unpack_metadata->dir,hinfo->filename); // 20040305-1304:PLD: unpack the file, propagate result upwards @@ -2425,7 +2424,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct // End. MIME_generate_multiple_hardlink_filenames(hinfo,unpack_metadata); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done for filename = '%s'",FL,hinfo->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done for filename = '%s'",FL,__func__,hinfo->filename); return result; } @@ -2443,10 +2442,10 @@ int MIME_postdecode_cleanup( RIPMIME_output *unpack_metadata, struct SS_object * result = 0; do { char *filename; - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s Popping file...",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s Popping file...",FL,__func__); filename = SS_pop(ss); if (filename == NULL) break; - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s Popped file '%s'",FL, filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s Popped file '%s'",FL,__func__, filename); if ( strncmp( glb.blankfileprefix, filename, strlen( glb.blankfileprefix ) ) == 0 ) { snprintf( fullpath, sizeof(fullpath), "%s/%s", unpack_metadata->dir, filename ); @@ -2483,7 +2482,7 @@ int MIME_postdecode_cleanup( RIPMIME_output *unpack_metadata, struct SS_object * int MIME_handle_multipart( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ) { int result = 0; - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding multipart/embedded \n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding multipart/embedded \n",FL,__func__); // If there is no filename, then we have a "standard" // embedded message, which can be just read off as a // continuous stream (simply with new boundaries @@ -2498,13 +2497,13 @@ int MIME_handle_multipart( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struc // layer of headers and get into the core of the message. This is why we // call unpack_stage2() because this function just reads the next set of // headers and decodes. - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Non base64 encoding AND no filename, embedded message\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Non base64 encoding AND no filename, embedded message\n",FL,__func__); h->boundary_located = 0; result = MIME_unpack_stage2(f, unpack_metadata, h, current_recursion_level , ss); p = BS_top(); if (p) PLD_strncpy(h->boundary, p,sizeof(h->boundary)); } else { - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Embedded message has a filename, decoding to file %s",FL,h->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Embedded message has a filename, decoding to file %s",FL,__func__,h->filename); result = MIME_decode_encoding( f, unpack_metadata, h, ss ); if (result == 0) { @@ -2512,7 +2511,7 @@ int MIME_handle_multipart( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struc int fn_l = strlen(unpack_metadata->dir) + strlen(h->filename) + sizeof(char) * 2; fn = malloc(fn_l); - snprintf(fn,fn_l,"%s/%s",unpack_metadata->dir,h->filename); + snprintf(fn,fn_l,"%s/%s",unpack_metadata->dir,h->filename); // Because we're calling MIME_unpack_single again [ie, recursively calling it // we need to now adjust the input-filename so that it correctly is prefixed // with the directory we unpacked to. @@ -2523,7 +2522,7 @@ int MIME_handle_multipart( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struc } } // else-if transfer-encoding != B64 && filename was empty - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: done handling '%s' result = %d",FL,h->filename, result); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: done handling '%s' result = %d",FL,__func__,h->filename, result); return result; } @@ -2550,12 +2549,12 @@ int MIME_handle_rfc822( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M { /** Decode a RFC822 encoded stream of data from *f **/ int result = 0; - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding RFC822 message\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding RFC822 message\n",FL,__func__); /** If there is no filename, then we have a "standard" ** embedded message, which can be just read off as a ** continuous stream (simply with new boundaries **/ - DMIME LOGGER_log("%s:%d:%s:DEBUG: Filename='%s', encoding = %d",FL, h->filename, h->content_transfer_encoding); + DMIME LOGGER_log("%s:%d:%s:DEBUG: Filename='%s', encoding = %d",FL,__func__, h->filename, h->content_transfer_encoding); // if ((0)&&( h->content_transfer_encoding != _CTRANS_ENCODING_B64)&&( h->filename[0] == '0' )) 20041217-1635:PLD: if (( h->content_transfer_encoding != _CTRANS_ENCODING_B64)&&( h->filename[0] == '\0' )) @@ -2569,16 +2568,16 @@ int MIME_handle_rfc822( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M // layer of headers and get into the core of the message. This is why we // call unpack_stage2() because this function just reads the next set of // headers and decodes. - DMIME LOGGER_log("%s:%d:%s:DEBUG: Non base64 encoding AND no filename, embedded message\n",FL); + DMIME LOGGER_log("%s:%d:%s:DEBUG: Non base64 encoding AND no filename, embedded message\n",FL,__func__); h->boundary_located = 0; result = MIME_unpack_stage2(f, unpack_metadata, h, current_recursion_level , ss); p = BS_top(); if (p) PLD_strncpy(h->boundary, p,sizeof(h->boundary)); } else { /** ...else... if the section has a filename or B64 type encoding, we need to put it through extra decoding **/ - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Embedded message has a filename, decoding to file %s",FL,h->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Embedded message has a filename, decoding to file %s",FL,__func__,h->filename); result = MIME_decode_encoding( f, unpack_metadata, h, ss ); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Result of extracting %s is %d",FL,h->filename, result); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Result of extracting %s is %d",FL,__func__,h->filename, result); if (result == 0) { char * fn; int fn_l = strlen(unpack_metadata->dir) + strlen(h->filename) + sizeof(char) * 2; @@ -2589,14 +2588,14 @@ int MIME_handle_rfc822( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M /** Because we're calling MIME_unpack_single again [ie, recursively calling it we need to now adjust the input-filename so that it correctly is prefixed with the directory we unpacked to. **/ - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Now attempting to extract contents of '%s'",FL,h->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Now attempting to extract contents of '%s'",FL,__func__,h->filename); result = MIME_unpack_single( unpack_metadata, fn, current_recursion_level, ss ); free(fn); result = 0; } } /** else-if transfer-encoding != B64 && filename was empty **/ - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: done handling '%s' result = %d",FL,h->filename, result); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: done handling '%s' result = %d",FL,__func__,h->filename, result); return result; } @@ -2623,7 +2622,7 @@ int MIME_handle_plain( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MI { /** Handle a plain text encoded data stream from *f **/ int result = 0; - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Handling plain email",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Handling plain email",FL,__func__); result = MIME_decode_encoding( f, unpack_metadata, h, ss ); if ((result == MIME_ERROR_FFGET_EMPTY)||(result == 0)) { @@ -2654,27 +2653,27 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M struct MIMEH_header_info *h; char *p; - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Start, recursion %d\n",FL, current_recursion_level); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Start, recursion %d\n",FL,__func__, current_recursion_level); if (current_recursion_level > glb.max_recursion_level) { /** Test for recursion limits **/ if (MIME_VERBOSE) LOGGER_log("%s:%d:%s:WARNING: Current recursion level of %d is greater than permitted %d"\ - ,FL, current_recursion_level, glb.max_recursion_level); + ,FL,__func__, current_recursion_level, glb.max_recursion_level); return MIME_ERROR_RECURSION_LIMIT_REACHED; // 20040306-1301:PLD } h = hinfo; // Get our headers and determin what we have... // - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Parsing headers (initial)\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Parsing headers (initial)\n",FL,__func__); // Parse the headers, extracting what information we need /** 20041216-1102:PLD: Keep attempting to read headers until we get a sane set **/ do { /** Read next set of headers, repeat until a sane set of headers are found **/ result = MIMEH_parse_headers(f,h,unpack_metadata->dir); - DMIME LOGGER_log("%s:%d:%s:DEBUG: Parsing of headers done, sanity = %d, result = %d",FL,h->sanity, result); + DMIME LOGGER_log("%s:%d:%s:DEBUG: Parsing of headers done, sanity = %d, result = %d",FL,__func__,h->sanity, result); } while ((h->sanity == 0)&&(result != -1)); - DMIME LOGGER_log("%s:%d:%s:DEBUG: Repeat loop of header-reading done, sanity = %d, result = %d",FL,h->sanity, result); + DMIME LOGGER_log("%s:%d:%s:DEBUG: Repeat loop of header-reading done, sanity = %d, result = %d",FL,__func__,h->sanity, result); glb.header_defect_count += MIMEH_get_defect_count(h); // Test the result output switch (result) { @@ -2686,12 +2685,12 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M // this will give us the 'main' subject of the entire email and prevent // and subsequent subjects from clobbering it. //if (glb.subject[0] == '\0') snprintf(glb.subject, _MIME_STRLEN_MAX, "%s", h->subject ); - if ((strlen(glb.subject) < 1)&&(h->subject != NULL)&&(strlen(h->subject) > 0)) + if ((strlen(glb.subject) < 1) && (strlen(h->subject) > 0)) { snprintf(glb.subject, sizeof(glb.subject), "%s", h->subject ); } if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Headers parsed, Result = %d, Boundary located = %d\n"\ - ,FL,result, hinfo->boundary_located); + ,FL,__func__,result, hinfo->boundary_located); // Test to see if we encountered any DoubleCR situations while we // were processing the headers. If we did encounter a doubleCR @@ -2712,7 +2711,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M char *lbc; char *fbc; - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Boundary located, pushing to stack (%s)\n",FL,h->boundary); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Boundary located, pushing to stack (%s)\n",FL,__func__,h->boundary); BS_push(h->boundary); // Get the first and last character positions of the boundary @@ -2727,18 +2726,18 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M if (*lbc == '"') { *lbc = '\0'; BS_push(h->boundary); *lbc = '"'; MIMEH_set_defect( hinfo, MIMEH_DEFECT_UNBALANCED_BOUNDARY_QUOTE); } h->boundary_located = 0; } else { - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding in BOUNDARY-LESS mode\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding in BOUNDARY-LESS mode\n",FL,__func__); if (h->content_type == _CTYPE_RFC822) { // Pass off to the RFC822 handler - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding with RFC822 decoder\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding with RFC822 decoder\n",FL,__func__); result = MIME_handle_rfc822(f,unpack_metadata,h,current_recursion_level,ss); } else if (MIMEH_is_contenttype(_CTYPE_MULTIPART, h->content_type)) { // Pass off to the multipart handler - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding with Multipart decoder\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding with Multipart decoder\n",FL,__func__); result = MIME_handle_multipart(f,unpack_metadata,h,current_recursion_level,ss); } else { - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding boundaryless file (%s)...\n",FL,h->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding boundaryless file (%s)...\n",FL,__func__,h->filename); result = MIME_handle_plain( f, unpack_metadata,h,current_recursion_level,ss); } // else-if content was RFC822 or multi-part return result; @@ -2747,13 +2746,13 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M if ((BS_top()!=NULL)&&(result == 0)) { // Commence decoding WITH boundary awareness. - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding with boundaries (filename = %s)\n",FL,h->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding with boundaries (filename = %s)\n",FL,__func__,h->filename); // Decode the data in the current MIME segment // based on the header information retrieved from // the start of this function. result = MIME_decode_encoding(f, unpack_metadata, h, ss); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done decoding, result = %d",FL,result); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done decoding, result = %d",FL,__func__,result); if (result == 0) { if (BS_top()!=NULL) @@ -2769,13 +2768,13 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M h->content_transfer_encoding = -1; h->content_disposition = -1; - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding headers...\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding headers...\n",FL,__func__); do { result = MIMEH_parse_headers(f,h,unpack_metadata->dir); } while ((h->sanity == 0)&&(result != -1)); glb.header_defect_count += MIMEH_get_defect_count(h); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Mime header parsing result = %d\n",FL, result); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Mime header parsing result = %d\n",FL,__func__, result); // 20040305-1331:PLD if (result == -1) { @@ -2785,7 +2784,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M if (h->boundary_located) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Pushing boundary %s\n",FL, h->boundary); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Pushing boundary %s\n",FL,__func__, h->boundary); BS_push(h->boundary); h->boundary_located = 0; } @@ -2804,7 +2803,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M || (MIMEH_is_contenttype(_CTYPE_MULTIPART, h->content_type))\ ) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Multipart/RFC822 mail headers found\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Multipart/RFC822 mail headers found\n",FL,__func__); /* If there is no filename, then we have a "standard" * embedded message, which can be just read off as a * continuous stream (simply with new boundaries */ @@ -2816,8 +2815,8 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M // the file into ripMIME. Certainly, this is not // the most efficent way of dealing with nested emails // however, it is a rather robust/reliable way. - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Chose Content-type == RFC822 clause",FL); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Calling MIME_decode_encoding()",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Chose Content-type == RFC822 clause",FL,__func__); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Calling MIME_decode_encoding()",FL,__func__); result = MIME_handle_rfc822(f,unpack_metadata,h,current_recursion_level,ss); // First up - extract the RFC822 body out of the parent mailpack // XX result = MIME_decode_encoding( f, unpackdir, h, ss ); @@ -2829,10 +2828,10 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M { snprintf(scratch,sizeof(scratch),"%s/%s",unpackdir, h->filename); snprintf(h->filename,sizeof(h->filename),"%s",scratch); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Now calling MIME_unpack_single() on the file '%s' for our RFC822 decode operation.",FL, scratch); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Now calling MIME_unpack_single() on the file '%s' for our RFC822 decode operation.",FL,__func__, scratch); //result = MIME_unpack_single( unpackdir, h->filename, current_recursion_level +1, ss); result = MIME_unpack( unpackdir, h->filename, current_recursion_level +1 ); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Unpack result = %d", FL, result); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Unpack result = %d", FL,__func__, result); result = 0; } else return result; // 20040305-1312:PLD */ @@ -2843,7 +2842,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M // To be honest, i've forgotten what this section of test is supposed to pick out // in terms of files - certainly it does pick out some, but I'm not sure what format // they are. Shame on me for not remembering, in future I must comment more. - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: NON-BASE64 DECODE\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: NON-BASE64 DECODE\n",FL,__func__); h->boundary_located = 0; result = MIME_unpack_stage2(f, unpack_metadata, h, current_recursion_level , ss); @@ -2861,18 +2860,18 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M // and embedded format, it does not have the normal headers->[headers->data] // layout of other nested emails - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Handle Appledouble explicitly",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Handle Appledouble explicitly",FL,__func__); result = MIME_unpack_stage2(f, unpack_metadata, h, current_recursion_level , ss); } else { - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: RFC822 Message to be decoded...\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: RFC822 Message to be decoded...\n",FL,__func__); result = MIME_decode_encoding( f, unpack_metadata, h, ss ); if (result != 0) return result; // 20040305-1313:PLD else { char * fn; int fn_l = strlen(unpack_metadata->dir) + strlen(h->filename) + sizeof(char) * 2; - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Now running ripMIME over decoded RFC822 message...\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Now running ripMIME over decoded RFC822 message...\n",FL,__func__); // Because we're calling MIME_unpack_single again [ie, recursively calling it // we need to now adjust the input-filename so that it correctly is prefixed @@ -2889,10 +2888,10 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M // If the attachment included in this MIME segment is NOT a // multipart or RFC822 embedded email, we can then simply use // the normal decoding function to interpret its data. - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding a normal attachment \n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding a normal attachment \n",FL,__func__); result = MIME_decode_encoding( f, unpack_metadata, h, ss ); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding a normal attachment '%s' done. \n",FL, h->filename); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding a normal attachment '%s' done. \n",FL,__func__, h->filename); // See if we have an attachment output which is actually another // email. // @@ -2907,7 +2906,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M mime_fname = PLD_dprintf("%s/%s", unpack_metadata->dir, h->filename); if(mime_fname != NULL) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Testing '%s' for email type",FL,mime_fname); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Testing '%s' for email type",FL,__func__,mime_fname); if (MIME_is_diskfile_RFC822(mime_fname)) { //MIME_unpack_single( unpackdir, mime_fname, (hinfo->current_recursion_level+1), ss); @@ -2930,7 +2929,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M } // if MIME_BS_top() } // if result == 0 } // if (result) - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Exiting with result=%d recursion=%d\n",FL,result, current_recursion_level); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Exiting with result=%d recursion=%d\n",FL,__func__,result, current_recursion_level); return result; } @@ -2962,7 +2961,7 @@ int MIME_unpack_mailbox( RIPMIME_output *unpack_metadata, char *mpname, int curr fi = (strcmp(mpname,"-")==0) ? stdin : fopen(mpname,"r"); if (!fi) { - LOGGER_log("%s:%d:%s:ERROR: Cannot open '%s' for reading (%s)",FL, mpname,strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: Cannot open '%s' for reading (%s)",FL,__func__, mpname,strerror(errno)); return -1; } } @@ -2970,7 +2969,7 @@ int MIME_unpack_mailbox( RIPMIME_output *unpack_metadata, char *mpname, int curr fo = fopen(fname,"w"); if (!fo) { - LOGGER_log("%s:%d:%s:ERROR: Cannot open '%s' for writing (%s)",FL, fname,strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: Cannot open '%s' for writing (%s)",FL,__func__, fname,strerror(errno)); return -1; } @@ -2993,7 +2992,7 @@ int MIME_unpack_mailbox( RIPMIME_output *unpack_metadata, char *mpname, int curr result = remove(fname); if (result == -1) { - LOGGER_log("%s:%d:%s:ERROR: removing temporary mailpack '%s' (%s)",FL, fname,strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: removing temporary mailpack '%s' (%s)",FL,__func__, fname,strerror(errno)); } // Create a new mailpack filename, and keep on going... snprintf(fname,sizeof(fname),"%s/tmp.email%03d.mailpack",unpack_metadata->dir,++mcount); @@ -3038,7 +3037,7 @@ int MIME_unpack_mailbox( RIPMIME_output *unpack_metadata, char *mpname, int curr result = remove(fname); if (result == -1) { - LOGGER_log("%s:%d:%s:ERROR: removing temporary mailpack '%s' (%s)",FL, fname,strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: removing temporary mailpack '%s' (%s)",FL,__func__, fname,strerror(errno)); } return 0; } @@ -3067,15 +3066,15 @@ int MIME_unpack_single( RIPMIME_output *unpack_metadata, char *mpname, int curre if (current_recursion_level > glb.max_recursion_level) { - LOGGER_log("%s:%d:%s:WARNING: Current recursion level of %d is greater than permitted %d",FL, current_recursion_level, glb.max_recursion_level); + LOGGER_log("%s:%d:%s:WARNING: Current recursion level of %d is greater than permitted %d",FL,__func__, current_recursion_level, glb.max_recursion_level); return MIME_ERROR_RECURSION_LIMIT_REACHED; } - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: dir=%s packname=%s level=%d (max = %d)\n",FL, unpack_metadata->dir, mpname, current_recursion_level, glb.max_recursion_level); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: dir=%s packname=%s level=%d (max = %d)\n",FL,__func__, unpack_metadata->dir, mpname, current_recursion_level, glb.max_recursion_level); /* if we're reading in from STDIN */ if( mpname[0] == '-' && mpname[1] == '\0' ) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: STDIN opened...\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: STDIN opened...\n",FL,__func__); fi = stdin; } else @@ -3083,21 +3082,21 @@ int MIME_unpack_single( RIPMIME_output *unpack_metadata, char *mpname, int curre fi = fopen(mpname,"r"); if (!fi) { - LOGGER_log("%s:%d:%s:ERROR: Cannot open file '%s' for reading.\n",FL, mpname); + LOGGER_log("%s:%d:%s:ERROR: Cannot open file '%s' for reading.\n",FL,__func__, mpname); return -1; } - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Input file (%s) opened...\n",FL, mpname); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Input file (%s) opened...\n",FL,__func__, mpname); } - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Checking input streams...\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Checking input streams...\n",FL,__func__); /* check to see if we had problems opening the file */ if (fi == NULL) { - LOGGER_log("%s:%d:%s:ERROR: Could not open mailpack file '%s' (%s)",FL, mpname, strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: Could not open mailpack file '%s' (%s)",FL,__func__, mpname, strerror(errno)); return -1; } // 20040317-2359:PLD result = MIME_unpack_single_fp(unpack_metadata,fi,current_recursion_level , ss); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: result = %d, recursion = %d, filename = '%s'", FL, result, current_recursion_level, mpname ); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: result = %d, recursion = %d, filename = '%s'", FL,__func__, result, current_recursion_level, mpname ); if ((current_recursion_level > 1)&&(result == 241)) result = 0; fclose(fi); return result; @@ -3124,18 +3123,18 @@ int MIME_unpack_single_fp( RIPMIME_output *unpack_metadata, FILE *fi, int curren // Because this MIME module gets used in both CLI and daemon modes // we should check to see that we can report to stderr // - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: dir=%s level=%d (max = %d)\n",FL, unpack_metadata->dir, current_recursion_level, glb.max_recursion_level); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: dir=%s level=%d (max = %d)\n",FL,__func__, unpack_metadata->dir, current_recursion_level, glb.max_recursion_level); if (current_recursion_level > glb.max_recursion_level) { - LOGGER_log("%s:%d:%s:WARNING: Current recursion level of %d is greater than permitted %d",FL, current_recursion_level, glb.max_recursion_level); + LOGGER_log("%s:%d:%s:WARNING: Current recursion level of %d is greater than permitted %d",FL,__func__, current_recursion_level, glb.max_recursion_level); // return -1; return MIME_ERROR_RECURSION_LIMIT_REACHED; // 20040305-1302:PLD //return 0; // 20040208-1723:PLD } else h.current_recursion_level = current_recursion_level; glb.current_line = 0; - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: recursion level checked...%d\n",FL, current_recursion_level); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: DumpHeaders = %d\n",FL, glb.save_headers); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: recursion level checked...%d\n",FL,__func__, current_recursion_level); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: DumpHeaders = %d\n",FL,__func__, glb.save_headers); if ((!hf)&&(glb.save_headers)&&(MIMEH_get_headers_save()==0)) { char * fn; @@ -3143,13 +3142,13 @@ int MIME_unpack_single_fp( RIPMIME_output *unpack_metadata, FILE *fi, int curren fn = malloc(fn_l); snprintf(fn,fn_l,"%s/%s",unpack_metadata->dir,glb.headersname); - + // Prepend the unpackdir path to the headers file name hf = fopen(fn,"w"); if (!hf) { glb.save_headers = 0; - LOGGER_log("%s:%d:%s:ERROR: Cannot open '%s' for writing (%s)", FL, fn, strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: Cannot open '%s' for writing (%s)", FL,__func__, fn, strerror(errno)); } else { @@ -3159,7 +3158,7 @@ int MIME_unpack_single_fp( RIPMIME_output *unpack_metadata, FILE *fi, int curren free(fn); } - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Setting up streams to decode\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Setting up streams to decode\n",FL,__func__); FFGET_setstream(&f, fi); /** Initialize the header record **/ h.boundary[0] = '\0'; @@ -3181,14 +3180,14 @@ int MIME_unpack_single_fp( RIPMIME_output *unpack_metadata, FILE *fi, int curren } } - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: preparing to decode, calling stage2...\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: preparing to decode, calling stage2...\n",FL,__func__); // 20040318-0001:PLD result = MIME_unpack_stage2(&f, unpack_metadata, &h, current_recursion_level +1, ss); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: done decoding ( in stage2 ) result=%d, to %s\n",FL, result, unpack_metadata->dir); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: done decoding ( in stage2 ) result=%d, to %s\n",FL,__func__, result, unpack_metadata->dir); // fclose(fi); 20040208-1726:PLD if ( headers_save_set_here > 0 ) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Closing header file.\n",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Closing header file.\n",FL,__func__); fflush(stdout); MIMEH_set_headers_nosave(); fclose(hf); @@ -3198,7 +3197,7 @@ int MIME_unpack_single_fp( RIPMIME_output *unpack_metadata, FILE *fi, int curren /** Flush out the string stacks **/ SS_done(&(h.ss_filenames)); SS_done(&(h.ss_names)); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done. Result=%d Recursion=%d\n",FL, result, current_recursion_level); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done. Result=%d Recursion=%d\n",FL,__func__, result, current_recursion_level); return result; // return status; // 20040305-1318:PLD } @@ -3217,18 +3216,17 @@ int MIME_unpack( RIPMIME_output *unpack_metadata, char *mpname, int current_recu struct SS_object ss; // Stores the filenames that are created in the unpack operation if (current_recursion_level > glb.max_recursion_level) return MIME_ERROR_RECURSION_LIMIT_REACHED; - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s: Unpacking %s to %s, recursion level is %d",FL,mpname,unpack_metadata->dir,current_recursion_level); - MIMEH_set_outputdir(unpack_metadata->dir); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s: Unpacking %s to %s, recursion level is %d",FL,__func__,mpname,unpack_metadata->dir,current_recursion_level); if (MIME_DNORMAL) SS_set_debug(&ss,1); SS_init(&ss); if (glb.mailbox_format > 0) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s: Unpacking using mailbox format",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s: Unpacking using mailbox format",FL,__func__); result = MIME_unpack_mailbox( unpack_metadata, mpname, (current_recursion_level), &ss ); } else { - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s: Unpacking standard mailpack",FL,mpname,unpack_metadata->dir,current_recursion_level); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s: Unpacking standard mailpack",FL,__func__,mpname,unpack_metadata->dir,current_recursion_level); result = MIME_unpack_single( unpack_metadata, mpname, (current_recursion_level +1), &ss ); } @@ -3239,7 +3237,7 @@ int MIME_unpack( RIPMIME_output *unpack_metadata, char *mpname, int current_recu if (MIME_DNORMAL) { - LOGGER_log("%s:%d:%s: Files unpacked from '%s' (recursion=%d);",FL,mpname,current_recursion_level); + LOGGER_log("%s:%d:%s: Files unpacked from '%s' (recursion=%d);",FL,__func__,mpname,current_recursion_level); SS_dump(&ss); } @@ -3264,11 +3262,11 @@ int MIME_unpack( RIPMIME_output *unpack_metadata, char *mpname, int current_recu if (current_recursion_level == 0) { - //LOGGER_log("%s:%d:%s:DEBUG: Clearing boundary stack",FL); + //LOGGER_log("%s:%d:%s:DEBUG: Clearing boundary stack",FL,__func__); BS_clear(); } - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s: Unpacking of %s is done.",FL,mpname); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s: -----------------------------------",FL); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s: Unpacking of %s is done.",FL,__func__,mpname); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s: -----------------------------------",FL,__func__); return result; } @@ -3279,7 +3277,7 @@ int MIME_unpack( RIPMIME_output *unpack_metadata, char *mpname, int current_recu void MIME_close( void ) { if (MIME_DNORMAL) { - LOGGER_log("%s:%d:%s: start.",FL); + LOGGER_log("%s:%d:%s: start.",FL,__func__); printArray(all_MIME_elements.mime_arr); } diff --git a/mime_element.c b/mime_element.c index aaf5b12..807a770 100644 --- a/mime_element.c +++ b/mime_element.c @@ -3,12 +3,11 @@ #include #include -#include "ffget.h" #include "mime_element.h" #include "logger.h" #ifndef FL -#define FL __FILE__, __LINE__, __func__ +#define FL __FILE__, __LINE__ #endif /* Dynamic array support*/ @@ -26,147 +25,160 @@ void deleteItem(dynamic_array* container, int i); void all_MIME_elements_init (void) { - all_MIME_elements.mime_count = 0; - arrayInit(&(all_MIME_elements.mime_arr)); + all_MIME_elements.mime_count = 0; + arrayInit(&(all_MIME_elements.mime_arr)); } -MIME_element* MIME_element_add_with_path (char* fullpath, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int attachment_count, int filecount) +all_MIME_elements_s all_MIME_elements; + +static char * dup_ini (char* s) { - MIME_element *cur = malloc(sizeof(MIME_element)); - char* fn = strrchr(fullpath, '/'); - if (!fn) - fn = fullpath; - - LOGGER_log("%s:%d:%s:start\n",FL); - - insertItem(all_MIME_elements.mime_arr, cur); - cur->hinfo = hinfo; - cur->id = all_MIME_elements.mime_count++; - cur->fullpath = fullpath; - - cur->f = fopen(cur->fullpath,"wb"); - if (cur->f == NULL) { - LOGGER_log("%s:%d:%s:ERROR: cannot open %s for writing",FL,cur->fullpath); - return cur; - } - - LOGGER_log("%s:%d:%s:DEBUG: Decoding [encoding=%d] to %s\n",FL, hinfo->content_transfer_encoding, cur->fullpath); - - if (unpack_metadata != NULL && unpack_metadata->unpack_mode == RIPMIME_UNPACK_MODE_LIST_FILES && cur->f != NULL) { - fprintf (stdout, "%d|%d|%d|%d|%s|%s\n", all_MIME_elements.mime_count, attachment_count, filecount, (hinfo != NULL) ? hinfo->current_recursion_level : 0, (hinfo != NULL) ? hinfo->content_type_string : "", (hinfo != NULL) ? hinfo->filename : fn); - } - return cur; + return (s != NULL) ? strdup(s) : "\0"; } -MIME_element* MIME_element_add (RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int attachment_count, int filecount) +MIME_element* MIME_element_add (void* parent, RIPMIME_output *unpack_metadata, char* filename, char* content_type_string, char* content_transfer_encoding, char* name, int current_recursion_level, int attachment_count, int filecount) { - int fullpath_len = strlen(unpack_metadata->dir) + strlen(hinfo->filename) + 3 * sizeof(char); - char *fullpath = (char*)malloc(fullpath_len); + MIME_element *cur = malloc(sizeof(MIME_element)); + int fullpath_len = 0; + + // LOGGER_log("%s:%d:%s:start\n",FL,__func__); + + fullpath_len = strlen(unpack_metadata->dir) + strlen(filename) + 3 * sizeof(char); + insertItem(all_MIME_elements.mime_arr, cur); + cur->parent = parent; + cur->id = all_MIME_elements.mime_count++; + cur->filename = dup_ini(filename); + cur->content_type_string = dup_ini(content_type_string); + cur->content_transfer_encoding = dup_ini(content_transfer_encoding); + cur->name = dup_ini(name); + + cur->fullpath = (char*)malloc(fullpath_len); + snprintf(cur->fullpath,fullpath_len,"%s/%s",unpack_metadata->dir,filename); + cur->f = fopen(cur->fullpath,"wb"); + if (cur->f == NULL) { + LOGGER_log("%s:%d:%s:ERROR: cannot open %s for writing",FL,__func__,cur->fullpath); + return cur; + } + + // LOGGER_log("%s:%d:%s:DEBUG: Decoding [encoding=%d] to %s\n",FL,__func__, content_transfer_encoding, cur->fullpath); + + if (unpack_metadata != NULL && unpack_metadata->unpack_mode == RIPMIME_UNPACK_MODE_LIST_FILES && cur->f != NULL) { + fprintf (stdout, "%d|%d|%d|%d|%s|%s\n", all_MIME_elements.mime_count, attachment_count, filecount, current_recursion_level, cur->content_type_string, cur->filename); + } + return cur; +} - snprintf(fullpath,fullpath_len,"%s/%s",unpack_metadata->dir,hinfo->filename); - return MIME_element_add_with_path(fullpath, unpack_metadata, hinfo, attachment_count, filecount); +static void dup_free(char *s) +{ + if ((s != NULL) && s[0]) + free(s); } void MIME_element_remove (MIME_element* cur) { - LOGGER_log("%s:%d:%s:start\n",FL); - - if (cur->f != NULL) { - fclose(cur->f); - } - free(cur->fullpath); - free(cur); + // LOGGER_log("%s:%d:%s:start\n",FL,__func__); + + if (cur->f != NULL) { + fclose(cur->f); + } + dup_free(cur->fullpath); + dup_free(cur->filename); + dup_free(cur->content_type_string); + dup_free(cur->content_transfer_encoding); + dup_free(cur->name); + + free(cur); } //------Dynamic array function definitions------ // Array initialization void arrayInit(dynamic_array** arr_ptr) { - dynamic_array *container; - container = (dynamic_array*)malloc(sizeof(dynamic_array)); - if(!container) { - printf("Memory Allocation Failed\n"); - exit(0); - } - - container->size = 0; - container->capacity = INITIAL_SIZE; - container->array = (MIME_element **)malloc(INITIAL_SIZE * sizeof(MIME_element*)); - if (!container->array){ - printf("Memory Allocation Failed\n"); - exit(0); - } - - *arr_ptr = container; + dynamic_array *container; + container = (dynamic_array*)malloc(sizeof(dynamic_array)); + if(!container) { + printf("Memory Allocation Failed\n"); + exit(0); + } + + container->size = 0; + container->capacity = INITIAL_SIZE; + container->array = (MIME_element **)malloc(INITIAL_SIZE * sizeof(MIME_element*)); + if (!container->array){ + printf("Memory Allocation Failed\n"); + exit(0); + } + + *arr_ptr = container; } // Insertion Operation void insertItem(dynamic_array* container, MIME_element* item) { - if (container->size == container->capacity) { - MIME_element **temp = container->array; - container->capacity <<= 1; - container->array = realloc(container->array, container->capacity * sizeof(MIME_element*)); - if(!container->array) { - printf("Out of Memory\n"); - container->array = temp; - return; - } - } - container->array[container->size++] = item; + if (container->size == container->capacity) { + MIME_element **temp = container->array; + container->capacity <<= 1; + container->array = realloc(container->array, container->capacity * sizeof(MIME_element*)); + if(!container->array) { + printf("Out of Memory\n"); + container->array = temp; + return; + } + } + container->array[container->size++] = item; } // Retrieve Item at Particular Index MIME_element* getItem(dynamic_array* container, int index) { - if(index >= container->size) { - printf("Index Out of Bounds\n"); - return NULL; - } - return container->array[index]; + if(index >= container->size) { + printf("Index Out of Bounds\n"); + return NULL; + } + return container->array[index]; } // Update Operation void updateItem(dynamic_array* container, int index, MIME_element* item) { - if (index >= container->size) { - printf("Index Out of Bounds\n"); - return; - } - container->array[index] = item; + if (index >= container->size) { + printf("Index Out of Bounds\n"); + return; + } + container->array[index] = item; } // Delete Item from Particular Index void deleteItem(dynamic_array* container, int index) { - if(index >= container->size) { - printf("Index Out of Bounds\n"); - return; - } - - for (int i = index; i < container->size; i++) { - container->array[i] = container->array[i + 1]; - } - container->size--; + if(index >= container->size) { + printf("Index Out of Bounds\n"); + return; + } + + for (int i = index; i < container->size; i++) { + container->array[i] = container->array[i + 1]; + } + container->size--; } // Array Traversal void printArray(dynamic_array* container) { - printf("Array elements: "); - for (int i = 0; i < container->size; i++) { - printf("%p ", container->array[i]); - } - printf("\nSize: "); - printf("%lu", container->size); - printf("\nCapacity: "); - printf("%lu\n", container->capacity); + printf("Array elements: "); + for (int i = 0; i < container->size; i++) { + printf("%p ", container->array[i]); + } + printf("\nSize: "); + printf("%lu", container->size); + printf("\nCapacity: "); + printf("%lu\n", container->capacity); } // Freeing the memory allocated to the array void freeArray(dynamic_array* container) { - free(container->array); - free(container); + free(container->array); + free(container); } /*----------END OF MIME.c------------*/ diff --git a/mime_element.h b/mime_element.h index a5d248b..cfd27f7 100644 --- a/mime_element.h +++ b/mime_element.h @@ -4,45 +4,45 @@ /* mime_element.h */ /* unpack modes */ -#define RIPMIME_UNPACK_MODE_TO_DIRECTORY 0 -#define RIPMIME_UNPACK_MODE_COUNT_FILES 1 -#define RIPMIME_UNPACK_MODE_LIST_FILES 2 +#define RIPMIME_UNPACK_MODE_TO_DIRECTORY 0 +#define RIPMIME_UNPACK_MODE_COUNT_FILES 1 +#define RIPMIME_UNPACK_MODE_LIST_FILES 2 struct mime_output { - char *dir; - int unpack_mode; - // int fragment_number; will be used later + char *dir; + int unpack_mode; + // int fragment_number; will be used later }; typedef struct mime_output RIPMIME_output; -#include "strstack.h" -#include "mime_headers.h" - typedef struct { - struct MIMEH_header_info *hinfo; - int id; - char* fullpath; - FILE* f; - int result; + void* parent; + int id; + char* directory; + char* filename; + char* fullpath; + FILE* f; + char* content_type_string; + char* content_transfer_encoding; + char* name; } MIME_element; typedef struct { - size_t size; - size_t capacity; - MIME_element** array; + size_t size; + size_t capacity; + MIME_element** array; } dynamic_array; typedef struct { - int mime_count; - dynamic_array* mime_arr; + int mime_count; + dynamic_array* mime_arr; } all_MIME_elements_s; -all_MIME_elements_s all_MIME_elements; +extern all_MIME_elements_s all_MIME_elements; void all_MIME_elements_init (void); -MIME_element* MIME_element_add_with_path (char* fullpath, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int attachment_count, int filecount); -MIME_element* MIME_element_add (RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int attachment_count, int filecount); +MIME_element* MIME_element_add (void* parent, RIPMIME_output *unpack_metadata, char* filename, char* content_type_string, char* content_transfer_encoding, char* name, int current_recursion_level, int attachment_count, int filecount); void MIME_element_remove (MIME_element* cur); void printArray(dynamic_array* container); void freeArray(dynamic_array* container); diff --git a/mime_headers.c b/mime_headers.c index d7ff5e9..0b4688c 100644 --- a/mime_headers.c +++ b/mime_headers.c @@ -24,8 +24,8 @@ #include #include #include -#include #include +#include #include #include "ffget.h" @@ -60,7 +60,6 @@ char *MIMEH_defect_description_array[_MIMEH_DEFECT_ARRAY_SIZE]; struct MIMEH_globals { - int doubleCR; int doubleCR_save; char doubleCRname[_MIMEH_STRLEN_MAX +1]; diff --git a/mime_headers.h b/mime_headers.h index e9c87d1..904b0b4 100644 --- a/mime_headers.h +++ b/mime_headers.h @@ -95,7 +95,6 @@ #define MIMEH_DEFECT_MULTIPLE_NAMES 9 #define MIMEH_DEFECT_MULTIPLE_FILENAMES 10 - struct MIMEH_header_info { char scratch[_MIMEH_STRLEN_MAX + 1]; diff --git a/ripOLE/Makefile b/ripOLE/Makefile index 7a26f44..ff13ef0 100644 --- a/ripOLE/Makefile +++ b/ripOLE/Makefile @@ -1,5 +1,5 @@ -OBJS= ole.o olestream-unwrap.o bytedecoders.o logger.o pldstr.o bt-int.o +OBJS= ole.o olestream-unwrap.o bytedecoders.o logger.o pldstr.o bt-int.o mime_element.o CFLAGS=-Wall -g -O2 -I. .c.o: diff --git a/ripOLE/logger.h b/ripOLE/logger.h index 703ce6f..94e3fe4 100644 --- a/ripOLE/logger.h +++ b/ripOLE/logger.h @@ -13,7 +13,7 @@ #define _LOGGER_NULL 5 #ifndef FL -#define FL __FILE__,__LINE__ +#define FL __FILE__,__LINE__,__func__ #endif int LOGGER_log( char *format, ...); diff --git a/ripOLE/mime_element.c b/ripOLE/mime_element.c new file mode 120000 index 0000000..058ccc0 --- /dev/null +++ b/ripOLE/mime_element.c @@ -0,0 +1 @@ +../mime_element.c \ No newline at end of file diff --git a/ripOLE/mime_element.h b/ripOLE/mime_element.h new file mode 120000 index 0000000..e1310b6 --- /dev/null +++ b/ripOLE/mime_element.h @@ -0,0 +1 @@ +../mime_element.h \ No newline at end of file diff --git a/ripOLE/ole.c b/ripOLE/ole.c index 6229159..f7a83e3 100644 --- a/ripOLE/ole.c +++ b/ripOLE/ole.c @@ -10,14 +10,10 @@ #include "pldstr.h" #include "bt-int.h" #include "bytedecoders.h" +#include "mime_element.h" #include "olestream-unwrap.h" #include "ole.h" -#include "../ffget.h" -#include "../strstack.h" -#include "../mime_headers.h" -#include "../mime_element.h" - /** Sector ID values (predefined) **/ #define OLE_SECTORID_FREE -1 /** Unallocated sector **/ #define OLE_SECTORID_ENDOFCHAIN -2 /** Sector marks the end of the a sector-ID chain **/ @@ -25,21 +21,21 @@ #define OLE_SECTORID_MSAT -4 /** Sector used by master sector allocation Table **/ // Main header accessors -#define header_id(x) ((x) +0) -#define header_clid(x) ((x) +0x08) -#define header_minor_version(x) ((x) +0x18) -#define header_dll_version(x) ((x) +0x1a) -#define header_byte_order(x) ((x) +0x1c) -#define header_sector_shift(x) ((x) +0x1e) -#define header_mini_sector_shift(x) ((x) +0x20) -#define header_fat_sector_count(x) ((x) +0x2c) +#define header_id(x) ((x) +0) +#define header_clid(x) ((x) +0x08) +#define header_minor_version(x) ((x) +0x18) +#define header_dll_version(x) ((x) +0x1a) +#define header_byte_order(x) ((x) +0x1c) +#define header_sector_shift(x) ((x) +0x1e) +#define header_mini_sector_shift(x) ((x) +0x20) +#define header_fat_sector_count(x) ((x) +0x2c) #define header_directory_stream_start_sector(x) ((x) +0x30) -#define header_mini_cutoff_size(x) ((x) +0x38) -#define header_mini_fat_start(x) ((x) +0x3c) -#define header_mini_fat_sector_count(x) ((x) +0x40) -#define header_dif_start_sector(x) ((x) +0x44) -#define header_dif_sector_count(x) ((x) +0x48) -#define header_fat_sectors(x) ((x) +0x4c) +#define header_mini_cutoff_size(x) ((x) +0x38) +#define header_mini_fat_start(x) ((x) +0x3c) +#define header_mini_fat_sector_count(x) ((x) +0x40) +#define header_dif_start_sector(x) ((x) +0x44) +#define header_dif_sector_count(x) ((x) +0x48) +#define header_fat_sectors(x) ((x) +0x4c) //Property Storage accessor macros #define pps_rawname(x) ((x) +0) @@ -47,35 +43,34 @@ #define pps_type(x) ((x) +0x42) #define pps_previouspps(x) ((x) +0x44) #define pps_nextpps(x) ((x) +0x48) -#define pps_directorypps(x) ((x) +0x4c) -#define pps_time1seconds(x) ((x) +0x64) +#define pps_directorypps(x) ((x) +0x4c) +#define pps_time1seconds(x) ((x) +0x64) #define pps_time1days(x) ((x) +0x68) -#define pps_time2seconds(x) ((x) +0x6c) +#define pps_time2seconds(x) ((x) +0x6c) #define pps_time2days(x) ((x) +0x70) #define pps_propertystart(x) ((x) +0x74) #define pps_sizeofproperty(x) ((x) +0x78) // Type lenghts #define LEN_BYTE 1 -#define LEN_USHORT 2 +#define LEN_USHORT 2 #define LEN_ULONG 4 // Directory types -#define STGTY_INVALID 0 -#define STGTY_STORAGE 1 -#define STGTY_STREAM 2 -#define STGTY_LOCKBYTES 3 -#define STGTY_PROPERTY 4 -#define STGTY_ROOT 5 +#define STGTY_INVALID 0 +#define STGTY_STORAGE 1 +#define STGTY_STREAM 2 +#define STGTY_LOCKBYTES 3 +#define STGTY_PROPERTY 4 +#define STGTY_ROOT 5 // Directory tag colours -#define DE_RED 0 -#define DE_BLACK 1 +#define DE_RED 0 +#define DE_BLACK 1 #define DOLE if (OLE_DNORMAL(ole->debug)) #define VOLE if (ole->verbose) - unsigned char OLE_id_v2[]={ 0xd0, 0xcf, 0x11, 0xe0, 0xa1, 0xb1, 0x1a, 0xe1 }; unsigned char OLE_id_v1[]={ 0x0e, 0x11, 0xfc, 0x0d, 0xd0, 0xcf, 0x11, 0xe0 }; @@ -101,7 +96,6 @@ int OLE_version( void ) return 0; } - /*-----------------------------------------------------------------\ Function Name : OLE_init Returns Type : int @@ -246,7 +240,7 @@ int OLE_set_quiet( struct OLE_object *ole, int level ) int OLE_set_debug( struct OLE_object *ole, int level ) { ole->debug = level; - if (ole->debug > 0) LOGGER_log("%s:%d:OLE_set_debug: Debug level set to %d",FL, ole->debug); + if (ole->debug > 0) LOGGER_log("%s:%d:%s: Debug level set to %d",FL,__func__, ole->debug); return OLE_OK; } @@ -328,7 +322,7 @@ int OLE_get_block( struct OLE_object *ole, int block_index, unsigned char *block { if (block_buffer == NULL) { - LOGGER_log("%s:%d:OLE_get_block:ERROR: Block buffer is NULL",FL); + LOGGER_log("%s:%d:%s:ERROR: Block buffer is NULL",FL,__func__); return -1; } @@ -342,52 +336,52 @@ int OLE_get_block( struct OLE_object *ole, int block_index, unsigned char *block bb = malloc(sizeof(unsigned char) *ole->header.sector_size); if (bb == NULL) { - LOGGER_log("%s:%d:OLE_get_block:ERROR: Cannot allocate %d bytes for OLE block",FL, ole->header.sector_size); + LOGGER_log("%s:%d:%s:ERROR: Cannot allocate %d bytes for OLE block",FL,__func__, ole->header.sector_size); return -1; } - DOLE LOGGER_log("%s:%d:OLE_get_block:DEBUG: BlockIndex=%d, Buffer=0x%x",FL, block_index, block_buffer); + DOLE LOGGER_log("%s:%d:%s:DEBUG: BlockIndex=%d, Buffer=0x%x",FL,__func__, block_index, block_buffer); //20051211-2343:PLD: offset = (block_index +1) << ole->header.sector_shift; offset = OLE_sectorpos(ole, block_index); - DOLE LOGGER_log("%s:%d:OLE_get_block:DEBUG: Read offset in file = 0x%x size to read= 0x%x",FL,offset,ole->header.sector_size); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Read offset in file = 0x%x size to read= 0x%x",FL,__func__,offset,ole->header.sector_size); fseek_result = fseek(ole->f, offset, SEEK_SET); if (fseek_result != 0) { if (bb != NULL) { free(bb); bb = NULL; } - LOGGER_log("%s:%d:OLE_get_block:ERROR: Seek failure (block=%d:%d)",FL, block_index,offset, strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: Seek failure (block=%d:%d)",FL,__func__, block_index,offset, strerror(errno)); return OLEER_GET_BLOCK_SEEK; } //read_count = fread(block_buffer, sizeof(unsigned char), ole->header.sector_size, ole->f); read_count = fread(bb, sizeof(unsigned char), ole->header.sector_size, ole->f); - DOLE LOGGER_log("%s:%d:OLE_get_block:DEBUG: Read %d byte of data",FL,read_count); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Read %d byte of data",FL,__func__,read_count); if (read_count != (int)ole->header.sector_size) { if (bb != NULL){ free(bb); bb = NULL; } - VOLE LOGGER_log("%s:%d:Mismatch in bytes read. Requested %d, got %d\n", FL, ole->header.sector_size, read_count); + VOLE LOGGER_log("%s:%d:Mismatch in bytes read. Requested %d, got %d\n", FL,__func__, ole->header.sector_size, read_count); return OLEER_GET_BLOCK_READ; } - DOLE LOGGER_log("%s:%d:OLE_get_block:DEBUG: Copying over memory read from file",FL); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Copying over memory read from file",FL,__func__); memcpy(block_buffer, bb, ole->header.sector_size); - DOLE LOGGER_log("%s:%d:OLE_get_block:DEBUG: memory block copied to block_buffer",FL); + DOLE LOGGER_log("%s:%d:%s:DEBUG: memory block copied to block_buffer",FL,__func__); /* We're now done with BB, dispose of it */ if (bb) { free(bb); bb = NULL; } - DOLE LOGGER_log("%s:%d:OLE_get_block:DEBUG: Disposed of temporary bb block",FL); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Disposed of temporary bb block",FL,__func__); } else { - LOGGER_log("%s:%d:OLE_get_block:ERROR: OLE file is closed\n",FL); + LOGGER_log("%s:%d:%s:ERROR: OLE file is closed\n",FL,__func__); return -1; } - DOLE LOGGER_log("%s:%d:OLE_get_block:DEBUG: Done",FL); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Done",FL,__func__); return OLE_OK; } @@ -832,7 +826,7 @@ int OLE_convert_directory( struct OLE_object *ole, unsigned char *buf, struct OL /** Size of this stream **/ DOLE LOGGER_log("%s:%d:OLE_directory_entry:DEBUG: stream size = 0x%x %x %x %x" - ,FL + ,FL,__func__ ,*(buf +0x78) ,*(buf +0x79) ,*(buf +0x7A) @@ -909,8 +903,8 @@ int OLE_load_FAT( struct OLE_object *ole ) unsigned int FAT_size; FAT_size = ole->header.fat_sector_count << ole->header.sector_shift; - DOLE LOGGER_log("%s:%d:OLE_load_FAT:DEBUG:Allocating for %d sectors (%d bytes) \n" - ,FL,ole->header.fat_sector_count, FAT_size); + DOLE LOGGER_log("%s:%d:%s:DEBUG:Allocating for %d sectors (%d bytes) \n" + ,FL,__func__,ole->header.fat_sector_count, FAT_size); ole->FAT = malloc( FAT_size *sizeof(unsigned char)); ole->FAT_limit = ole->FAT +FAT_size; @@ -923,7 +917,7 @@ int OLE_load_FAT( struct OLE_object *ole ) if (sector_count > OLE_HEADER_FAT_SECTOR_COUNT_LIMIT) { sector_count = OLE_HEADER_FAT_SECTOR_COUNT_LIMIT; - DOLE LOGGER_log("%s:%d:OLE_load_FAT:DEBUG: sector count greater than limit; set to %d",FL, sector_count); + DOLE LOGGER_log("%s:%d:%s:DEBUG: sector count greater than limit; set to %d",FL,__func__, sector_count); } // Load in all our primary-FAT sectors from the OLE file @@ -931,7 +925,7 @@ int OLE_load_FAT( struct OLE_object *ole ) { int getblock_result = 0; - DOLE LOGGER_log("%s:%d:OLE_load_FAT:DEBUG: Loading sector %d",FL, i); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Loading sector %d",FL,__func__, i); getblock_result = OLE_get_block(ole, ole->header.FAT[i], fat_position); if (getblock_result != 0) { @@ -950,7 +944,7 @@ int OLE_load_FAT( struct OLE_object *ole ) fat_position += ole->header.sector_size; if (fat_position > ole->FAT_limit) { - LOGGER_log("%s:%d:OLE_load_FAT:DEBUG: FAT boundary limit exceeded %p > %p", FL, fat_position, ole->FAT_limit); + LOGGER_log("%s:%d:%s:DEBUG: FAT boundary limit exceeded %p > %p", FL,__func__, fat_position, ole->FAT_limit); return -1; } } @@ -967,13 +961,13 @@ int OLE_load_FAT( struct OLE_object *ole ) unsigned char *fat_block_end; unsigned int current_sector = ole->header.dif_start_sector; - DOLE LOGGER_log("%s:%d:OLE_load_FAT:DEBUG: Allocating %d bytes to fat_block\n",FL, ole->header.sector_size); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Allocating %d bytes to fat_block\n",FL,__func__, ole->header.sector_size); fat_block = malloc( ole->header.sector_size ); if (fat_block == NULL) { - LOGGER_log("%s:%d:OLE_load_FAT:ERROR: Unable to allocate %d bytes\n", FL, ole->header.sector_size); + LOGGER_log("%s:%d:%s:ERROR: Unable to allocate %d bytes\n", FL,__func__, ole->header.sector_size); return -1; // exit(1); } @@ -991,7 +985,7 @@ int OLE_load_FAT( struct OLE_object *ole ) // contain the actual FAT data we're after (this is the double // dereference bit that twists your brain ) - DOLE LOGGER_log("%s:%d:OLE_load_FAT:DEBUG: Loading DIF sectors (count = %d)",FL,ole->header.dif_sector_count); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Loading DIF sectors (count = %d)",FL,__func__,ole->header.dif_sector_count); for (i = 0; i < ole->header.dif_sector_count; i++) { @@ -1000,7 +994,7 @@ int OLE_load_FAT( struct OLE_object *ole ) int tick = 0; int getblock_result; - DOLE LOGGER_log("%s:%d:OLE_load_FAT:DEBUG: Reading DIF/XBAT index-data[%d] from sector 0x%x",FL,i,current_sector); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Reading DIF/XBAT index-data[%d] from sector 0x%x",FL,__func__,i,current_sector); getblock_result = OLE_get_block(ole, current_sector, fat_block); if (getblock_result != OLE_OK) { @@ -1018,23 +1012,23 @@ int OLE_load_FAT( struct OLE_object *ole ) do { import_sector = get_int32( (char *) DIF ); - DOLE LOGGER_log("%s:%d:OLE_load_FAT:DEBUG: import sector = 0x%x",FL,import_sector); + DOLE LOGGER_log("%s:%d:%s:DEBUG: import sector = 0x%x",FL,__func__,import_sector); if (import_sector >= 0) { if (fat_position +ole->header.sector_size <= ole->FAT_limit) { - DOLE LOGGER_log("%s:%d:OLE_load_FAT:DEBUG: Reading DIF/XBAT-data[%d] from sector 0x%x",FL,tick,import_sector); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Reading DIF/XBAT-data[%d] from sector 0x%x",FL,__func__,tick,import_sector); getblock_result = OLE_get_block(ole, import_sector, fat_position); if (getblock_result != OLE_OK) { - LOGGER_log("%s:%d:OLE_load_FAT:ERROR: Not able to load block, import sector = 0x%x, fat position = 0x%x",FL, import_sector, fat_position); + LOGGER_log("%s:%d:%s:ERROR: Not able to load block, import sector = 0x%x, fat position = 0x%x",FL,__func__, import_sector, fat_position); if (fat_block) free(fat_block); return getblock_result; } fat_position += ole->header.sector_size; - DOLE LOGGER_log("%s:%d:OLE_load_FAT:DEBUG: FAT position = 0x%x (start = 0x%x, end = 0x%x)" - ,FL + DOLE LOGGER_log("%s:%d:%s:DEBUG: FAT position = 0x%x (start = 0x%x, end = 0x%x)" + ,FL,__func__ ,fat_position ,fat_block ,ole->FAT_limit @@ -1043,22 +1037,22 @@ int OLE_load_FAT( struct OLE_object *ole ) //if (fat_position +ole->header.sector_size > ole->FAT_limit) if (fat_position > ole->FAT_limit) { - DOLE LOGGER_log("%s:%d:OLE_load_FAT:ERROR: FAT memory boundary limit exceeded %p >= %p",FL,fat_position,ole->FAT_limit); + DOLE LOGGER_log("%s:%d:%s:ERROR: FAT memory boundary limit exceeded %p >= %p",FL,__func__,fat_position,ole->FAT_limit); if (fat_block) free(fat_block); return OLEER_MEMORY_OVERFLOW; } tick++; DIF += LEN_ULONG; } else { - LOGGER_log("%s:%d:OLE_load_FAT:ERROR: FAT memory boundary limit exceeded %p >= %p",FL,fat_position,ole->FAT_limit); + LOGGER_log("%s:%d:%s:ERROR: FAT memory boundary limit exceeded %p >= %p",FL,__func__,fat_position,ole->FAT_limit); if (fat_block) free(fat_block); return OLEER_MEMORY_OVERFLOW; } } else { - VOLE LOGGER_log("%s:%d:OLE_load_FAT:ERROR: sector request was negative (%d)",FL, import_sector); + VOLE LOGGER_log("%s:%d:%s:ERROR: sector request was negative (%d)",FL,__func__, import_sector); } - DOLE LOGGER_log("%s:%d:OLE_load_FAT:DEBUG: DIF = 0x%x",FL,DIF); + DOLE LOGGER_log("%s:%d:%s:DEBUG: DIF = 0x%x",FL,__func__,DIF); } while ((import_sector >= 0)&&(DIF < fat_block_end)); // Get the next sector of DIF/XBAT data ... @@ -1071,7 +1065,7 @@ int OLE_load_FAT( struct OLE_object *ole ) if ( i < ole->header.dif_sector_count -1 ) { current_sector = get_uint32((char *) fat_block_end ); - DOLE LOGGER_log("%s:%d:OLE_load_FAT:DEBUG: Next DIF/XBAT index sector located at 0x%x",FL,current_sector); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Next DIF/XBAT index sector located at 0x%x",FL,__func__,current_sector); if (current_sector < 0) break; } } // For every DIF/XBAT sector we're supposed to read @@ -1084,8 +1078,6 @@ int OLE_load_FAT( struct OLE_object *ole ) return OLE_OK; } - - /*-----------------------------------------------------------------\ Function Name : OLE_follow_chain Returns Type : int @@ -1114,7 +1106,7 @@ int OLE_follow_chain( struct OLE_object *ole, int FAT_sector_start ) if (FAT_sector_start < 0) return 0; - DOLE LOGGER_log("%s:%d:OLE_follow_chain:DEBUG: Starting chain follow at sector %d",FL, FAT_sector_start ); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Starting chain follow at sector %d",FL,__func__, FAT_sector_start ); do { unsigned int next_sector; @@ -1122,7 +1114,7 @@ int OLE_follow_chain( struct OLE_object *ole, int FAT_sector_start ) next_sector_location = ole->FAT +(LEN_ULONG *current_sector); if (next_sector_location > (ole->FAT_limit -4)) { - DOLE LOGGER_log("%s:%d:OLE_follow_chain:DEBUG: ERROR: Next sector was outside of the limits of this file (%ld > %ld)",FL, next_sector_location, ole->FAT_limit); + DOLE LOGGER_log("%s:%d:%s:DEBUG: ERROR: Next sector was outside of the limits of this file (%ld > %ld)",FL,__func__, next_sector_location, ole->FAT_limit); break; } @@ -1131,13 +1123,13 @@ int OLE_follow_chain( struct OLE_object *ole, int FAT_sector_start ) if (BTI_add(&n, next_sector) != 0) { - DOLE LOGGER_log("%s:%d:OLE_follow_chain:DEBUG: Sector collision, terminating chain traversal",FL); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Sector collision, terminating chain traversal",FL,__func__); chain_length=-1; break; } - DOLE LOGGER_log("%s:%d:OLE_follow_chain:DEBUG: 0x%0X:%d)->(0x%0X:%d)\n",FL, current_sector, current_sector, next_sector, next_sector); + DOLE LOGGER_log("%s:%d:%s:DEBUG: 0x%0X:%d)->(0x%0X:%d)\n",FL,__func__, current_sector, current_sector, next_sector, next_sector); // 20040729-10H37 Added this to prevent endless loop which sometimes occurs at sector 0 @@ -1191,22 +1183,22 @@ int OLE_follow_minichain( struct OLE_object *ole, int miniFAT_sector_start ) int chain_length=0; int break_out = 0; - DOLE LOGGER_log("%s:%d:OLE_follow_minichain:DEBUG: Starting at sector %d",FL, miniFAT_sector_start); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Starting at sector %d",FL,__func__, miniFAT_sector_start); if (miniFAT_sector_start < 0) return 0; do { unsigned int next_sector; - DOLE LOGGER_log("%s:%d:OLE_follow_minichain:DEBUG: Requesting 4-byte value at '%d'",FL, ole->miniFAT +(LEN_ULONG *current_sector)); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Requesting 4-byte value at '%d'",FL,__func__, ole->miniFAT +(LEN_ULONG *current_sector)); if (ole->miniFAT +(LEN_ULONG *current_sector) > ole->miniFAT_limit) { - DOLE LOGGER_log("%s:%d:OLE_follow_minichain:DEBUG: Requested location is out of bounds\n",FL); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Requested location is out of bounds\n",FL,__func__); return 0; } next_sector = get_uint32((char*) ole->miniFAT +(LEN_ULONG *current_sector)); - DOLE LOGGER_log("%s:%d:OLE_follow_minichain:DEBUG: Current Msector(0x%0X:%d)->next(0x%0X:%d)\n", FL, current_sector, current_sector, next_sector, next_sector); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Current Msector(0x%0X:%d)->next(0x%0X:%d)\n", FL,__func__, current_sector, current_sector, next_sector, next_sector); /** Check for conditions that indicate we should stop traversing this chain **/ @@ -1228,10 +1220,10 @@ int OLE_follow_minichain( struct OLE_object *ole, int miniFAT_sector_start ) break_out=0; }; - DOLE LOGGER_log("%s:%d:OLE_follow_minichain:DEBUG: current sector = %d",FL,current_sector); + DOLE LOGGER_log("%s:%d:%s:DEBUG: current sector = %d",FL,__func__,current_sector); } while ((break_out==0)&&(current_sector <= ole->last_sector )); - DOLE LOGGER_log("%s:%d:OLE_follow_minichain:DEBUG: Done. Chainlength=%d",FL, chain_length); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Done. Chainlength=%d",FL,__func__, chain_length); return chain_length; } @@ -1263,13 +1255,13 @@ unsigned char *OLE_load_minichain( struct OLE_object *ole, int miniFAT_sector_st unsigned char *buffer; unsigned char *bp; - DOLE LOGGER_log("%s:%d:OLE_load_minichain:DEBUG: Loading minichain starting at %d",FL, miniFAT_sector_start); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Loading minichain starting at %d",FL,__func__, miniFAT_sector_start); // Added this sanity checking 2003 Aug 28 if (miniFAT_sector_start < 0) return NULL; chain_length = OLE_follow_minichain( ole, miniFAT_sector_start ); - DOLE LOGGER_log("%s:%d:OLE_load_minichain:DEBUG: Found %d mini-sectors to load (%d bytes)\n",FL, chain_length, chain_length *ole->header.mini_sector_size); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Found %d mini-sectors to load (%d bytes)\n",FL,__func__, chain_length, chain_length *ole->header.mini_sector_size); // 20040911-21H59 // If our chain is 0 length, then there's nothing to return @@ -1281,7 +1273,7 @@ unsigned char *OLE_load_minichain( struct OLE_object *ole, int miniFAT_sector_st do { unsigned int next_sector; - DOLE LOGGER_log("%s:%d:OLE_load_minichain:DEBUG: Loading sector %d",FL, current_sector); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Loading sector %d",FL,__func__, current_sector); OLE_get_miniblock( ole, current_sector, bp ); bp += ole->header.mini_sector_size; @@ -1289,10 +1281,10 @@ unsigned char *OLE_load_minichain( struct OLE_object *ole, int miniFAT_sector_st current_sector = next_sector; } while ((current_sector != OLE_SECTORID_ENDOFCHAIN)&&(current_sector >= 0)&&(current_sector <= ole->last_sector)); } else { - LOGGER_log("%s:%d:OLE_get_miniblock:ERROR: Failed to allocate enough memory for miniChain",FL); + LOGGER_log("%s:%d:OLE_get_miniblock:ERROR: Failed to allocate enough memory for miniChain",FL,__func__); } - DOLE LOGGER_log("%s:%d:OLE_load_minichain:DEBUG: Done. buffer=%p",FL, buffer); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Done. buffer=%p",FL,__func__, buffer); return buffer; } @@ -1329,10 +1321,10 @@ unsigned char *OLE_load_chain( struct OLE_object *ole, int FAT_sector_start ) if (FAT_sector_start < 0) return NULL; - DOLE LOGGER_log("%s:%d:OLE_load_chain:DEBUG: Loading chain, starting at sector %d",FL,FAT_sector_start); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Loading chain, starting at sector %d",FL,__func__,FAT_sector_start); chain_length = OLE_follow_chain( ole, FAT_sector_start ); - DOLE LOGGER_log("%s:%d:OLE_load_chain:DEBUG: %d sectors need to be loaded",FL,chain_length); + DOLE LOGGER_log("%s:%d:%s:DEBUG: %d sectors need to be loaded",FL,__func__,chain_length); if (chain_length > 0) { @@ -1342,7 +1334,7 @@ unsigned char *OLE_load_chain( struct OLE_object *ole, int FAT_sector_start ) bp = buffer = malloc( offset *sizeof(unsigned char)); if (buffer == NULL) { - LOGGER_log("%s:%d:OLE_load_chain:ERROR: Cannot allocate %d bytes for OLE chain",FL,offset); + LOGGER_log("%s:%d:%s:ERROR: Cannot allocate %d bytes for OLE chain",FL,__func__,offset); return NULL; } @@ -1355,7 +1347,7 @@ unsigned char *OLE_load_chain( struct OLE_object *ole, int FAT_sector_start ) do { int next_sector; - DOLE LOGGER_log("%s:%d:OLE_load_chain:DEBUG: Loading sector[%d] %d",FL, tick, current_sector ); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Loading sector[%d] %d",FL,__func__, tick, current_sector ); ole->error = OLE_get_block( ole, current_sector, bp ); if (ole->error != OLE_OK) @@ -1367,7 +1359,7 @@ unsigned char *OLE_load_chain( struct OLE_object *ole, int FAT_sector_start ) bp += ole->header.sector_size; if (bp > bp_limit) { if (buffer != NULL) { free(buffer); bp = buffer = NULL; } - VOLE LOGGER_log("%s:%d:OLE_load_chain:ERROR: Load-chain went over memory boundary",FL); + VOLE LOGGER_log("%s:%d:%s:ERROR: Load-chain went over memory boundary",FL,__func__); return NULL; }; @@ -1377,7 +1369,7 @@ unsigned char *OLE_load_chain( struct OLE_object *ole, int FAT_sector_start ) } while ((current_sector >= 0)&&(current_sector <= ole->last_sector)); } } - DOLE LOGGER_log("%s:%d:OLE_load_chain:DEBUG: Done loading chain",FL); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Done loading chain",FL,__func__); return buffer; } @@ -1403,6 +1395,7 @@ int OLE_input_file_data_ini( struct OLE_object *ole ) fseek(ole->f, 0L, SEEK_SET); if (ole->file_size < OLE_HEADER_BLOCK_SIZE) { fclose(ole->f); + ole->f = NULL; return OLEER_NOT_OLE_FILE; } ole->last_sector = -1; @@ -1436,7 +1429,7 @@ int OLE_open_file( struct OLE_object *ole, char *fullpath ) { if (ole->quiet == 0) { - LOGGER_log("%s:%d:OLE_open_file:ERROR:Cannot open %s for reading (%s)\n",FL,fullpath, strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR:Cannot open %s for reading (%s)\n",FL,__func__,fullpath, strerror(errno)); } return -1; } @@ -1460,14 +1453,14 @@ int OLE_open_file( struct OLE_object *ole, char *fullpath ) Changes: \------------------------------------------------------------------*/ -int OLE_open_directory( struct OLE_object *ole, char *directory ) +int OLE_open_directory( struct OLE_object *ole, RIPMIME_output *unpack_metadata ) { int result=0; - result = mkdir( directory, S_IRWXU ); + result = mkdir( unpack_metadata->dir, S_IRWXU ); if ((result != 0)&&(errno != EEXIST)) { - LOGGER_log("%s:%d:OLE_open_directory:ERROR: %s",FL,strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: %s",FL,__func__,strerror(errno)); } else result = OLE_OK; return result; @@ -1517,36 +1510,24 @@ int OLE_set_filename_report_fn( struct OLE_object *ole, int (*ptr_to_fn)(char *) Changes: \------------------------------------------------------------------*/ -int OLE_store_stream( struct OLE_object *ole, char *stream_name, char *directory, char *stream, size_t stream_size ) +int OLE_store_stream( struct OLE_object *ole, char *stream_name, RIPMIME_output *unpack_metadata, char *stream, size_t stream_size ) { - char *full_path = NULL; + MIME_element* cur_mime = MIME_element_add (NULL, unpack_metadata, stream_name, "OLE", "OLE", "OLE", 0, 1, 0); - full_path = PLD_dprintf("%s/%s", directory, stream_name); - if (full_path == NULL) + size_t written_bytes = fwrite( stream, 1, stream_size, cur_mime->f ); + if (written_bytes != stream_size) { - LOGGER_log("%s:%d:OLE_store_stream:ERROR: Cannot compose full filename string from '%s' and '%s'", FL, directory, stream_name); - return -1; - } else { - MIME_element* mime_el = MIME_element_add_with_path (full_path, NULL, NULL, 1, 1); - size_t written_bytes = fwrite( stream, 1, stream_size, mime_el->f ); - if (written_bytes != stream_size) - { - LOGGER_log("%s:%d:OLE_store_stream:WARNING: Only wrote %d of %d bytes to file %s",FL,written_bytes,stream_size,full_path); - } - MIME_element_remove (mime_el); - - if ((OLE_VNORMAL(ole->verbose))&&(ole->filename_report_fn != NULL)) - { - ole->filename_report_fn( stream_name ); - } - } // if full_path is valid - - if (full_path) free(full_path); + LOGGER_log("%s:%d:%s:WARNING: Only wrote %d of %d bytes to file %s",FL,__func__,written_bytes,stream_size,cur_mime->fullpath); + } + MIME_element_remove (cur_mime); + if ((OLE_VNORMAL(ole->verbose))&&(ole->filename_report_fn != NULL)) + { + ole->filename_report_fn( stream_name ); + } return OLE_OK; } - /*-----------------------------------------------------------------\ Function Name : OLE_decode_file_done Returns Type : int @@ -1562,16 +1543,17 @@ int OLE_store_stream( struct OLE_object *ole, char *stream_name, char *directory Changes: \------------------------------------------------------------------*/ -int OLE_decode_file_done( struct OLE_object *ole ) +void OLE_decode_file_done( struct OLE_object *ole ) { + DOLE LOGGER_log("%s:%d:%s:DEBUG: ole->f close",FL,__func__); if (ole->f) fclose(ole->f); /** Why weren't these active? (they were commented out ) **/ + DOLE LOGGER_log("%s:%d:%s:DEBUG: OLE FAT",FL,__func__); if (ole->FAT) free(ole->FAT); if (ole->miniFAT) free(ole->miniFAT); + DOLE LOGGER_log("%s:%d:%s:DEBUG: OLE streams",FL,__func__); if (ole->ministream) free(ole->ministream); if (ole->properties) free(ole->properties); - - return 0; } @@ -1615,7 +1597,7 @@ Side Effects : Changes: \------------------------------------------------------------------*/ -int OLE_decode_stream( struct OLE_object *ole, struct OLE_directory_entry *adir, char *decode_path ) +int OLE_decode_stream( struct OLE_object *ole, struct OLE_directory_entry *adir, RIPMIME_output *unpack_metadata ) { unsigned char *stream_data; struct OLEUNWRAP_object oleuw; @@ -1626,52 +1608,49 @@ int OLE_decode_stream( struct OLE_object *ole, struct OLE_directory_entry *adir memset(element_name, '\0', 64); OLE_dbstosbs( adir->element_name, adir->element_name_byte_count, element_name, 64 ); - DOLE LOGGER_log("%s:%d:OLE_decode_stream:DEBUG: Decoding stream '%s'",FL, element_name); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Decoding stream '%s'",FL,__func__, element_name); - DOLE LOGGER_log("%s:%d:OLE_decode_stream:DEBUG: Initializing stream unwrapper",FL); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Initializing stream unwrapper",FL,__func__); OLEUNWRAP_init(&oleuw); OLEUNWRAP_set_debug(&oleuw,ole->debug); OLEUNWRAP_set_verbose(&oleuw,ole->verbose); OLEUNWRAP_set_filename_report_fn(&oleuw, ole->filename_report_fn); OLEUNWRAP_set_save_unknown_streams(&oleuw, ole->save_unknown_streams); - DOLE LOGGER_log("%s:%d:OLE_decode_stream:DEBUG: Unwrap engine set.",FL); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Unwrap engine set.",FL,__func__); if (adir->stream_size >= ole->header.mini_cutoff_size) { /** Standard size sector stored stream **/ /** Standard size sector stored stream **/ /** Standard size sector stored stream **/ - DOLE LOGGER_log("%s:%d:OLE_decode_stream:DEBUG: Loading normal sized chain starting at sector %d",FL, adir->start_sector); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Loading normal sized chain starting at sector %d",FL,__func__, adir->start_sector); stream_data = OLE_load_chain( ole, (int)adir->start_sector ); if (stream_data == NULL) { - DOLE LOGGER_log("%s:%d:OLE_decode_stream:DEBUG: Terminating from stream data being NULL ",FL); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Terminating from stream data being NULL ",FL,__func__); //OLE_decode_file_done(ole); return OLEER_MINISTREAM_STREAM_READ_FAIL; } - DOLE LOGGER_log("%s:%d:OLE_decode_stream:DEBUG: Normal decode START. element name ='%s' stream size = '%ld'",FL, element_name, adir->stream_size); - decode_result = OLEUNWRAP_decodestream( &oleuw, element_name, (char *)stream_data, adir->stream_size, decode_path ); - DOLE LOGGER_log("%s:%d:OLE_decode_stream:DEBUG: Normal decode done.",FL); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Normal decode START. element name ='%s' stream size = '%ld'",FL,__func__, element_name, adir->stream_size); + decode_result = OLEUNWRAP_decodestream( &oleuw, element_name, (char *)stream_data, adir->stream_size, unpack_metadata ); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Normal decode done.",FL,__func__); } else { - - /** Minichain/Minisector stored stream **/ - /** Minichain/Minisector stored stream **/ /** Minichain/Minisector stored stream **/ - DOLE LOGGER_log("%s:%d:OLE_decode_stream:DEBUG: Minichain loader, starting at sector %d" - ,FL + DOLE LOGGER_log("%s:%d:%s:DEBUG: Minichain loader, starting at sector %d" + ,FL,__func__ ,adir->start_sector ); stream_data = OLE_load_minichain( ole, adir->start_sector ); if (stream_data == NULL) { - DOLE LOGGER_log("%s:%d:OLE_decode_stream:DEBUG: Ministream was non-existant, terminating",FL); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Ministream was non-existant, terminating",FL,__func__); //OLE_decode_file_done(ole); return OLEER_NORMALSTREAM_STREAM_READ_FAIL; } - DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Mini decode START.",FL); - decode_result = OLEUNWRAP_decodestream( &oleuw, element_name, (char *)stream_data, adir->stream_size, decode_path ); - DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Mini decode done.",FL); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Mini decode START.",FL,__func__); + decode_result = OLEUNWRAP_decodestream( &oleuw, element_name, (char *)stream_data, adir->stream_size, unpack_metadata ); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Mini decode done.",FL,__func__); } if ((stream_data != NULL)&&(decode_result == OLEUW_STREAM_NOT_DECODED)&&(ole->save_unknown_streams)) @@ -1681,8 +1660,8 @@ int OLE_decode_stream( struct OLE_object *ole, struct OLE_directory_entry *adir lfname = PLD_dprintf("ole-stream.%d",adir->start_sector); if (lfname != NULL) { - DOLE LOGGER_log("%s:%d:OLE_decode_stream:DEBUG: Saving stream to %s",FL,lfname); - OLE_store_stream( ole, lfname, decode_path, (char *) stream_data, adir->stream_size ); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Saving stream to %s",FL,__func__,lfname); + OLE_store_stream( ole, lfname, unpack_metadata, (char *) stream_data, adir->stream_size ); free(lfname); } } // If we needed to save an unknown stream @@ -1694,7 +1673,7 @@ int OLE_decode_stream( struct OLE_object *ole, struct OLE_directory_entry *adir return result; } -int OLE_decode( struct OLE_object *ole, char *decode_path ) +int OLE_decode( struct OLE_object *ole, RIPMIME_output *unpack_metadata ) { unsigned char *current_property, *property_limit; int result = 0; @@ -1702,18 +1681,18 @@ int OLE_decode( struct OLE_object *ole, char *decode_path ) // Try create the output directory which we're using // to write the decoded files out to. - DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: opening output directory %s", FL, decode_path); - result = OLE_open_directory( ole, decode_path ); + DOLE LOGGER_log("%s:%d:%s:DEBUG: opening output directory %s", FL,__func__, unpack_metadata->dir); + result = OLE_open_directory( ole, unpack_metadata); if (result != 0) return result; // In order to successfully decode an OLE2 stream, we have to read // and understand the first 512 bytes of the file, this is the // OLE2 header. - DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Getting main header", FL); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Getting main header", FL,__func__); result = OLE_get_header( ole ); if (result != 0) return result; - DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Converting main header", FL); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Converting main header", FL,__func__); result = OLE_convert_header( ole ); if (result != 0) return result; @@ -1722,15 +1701,15 @@ int OLE_decode( struct OLE_object *ole, char *decode_path ) DOLE OLE_print_header( ole ); - DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Loading FAT", FL); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Loading FAT", FL,__func__); result = OLE_load_FAT( ole ); if (result != 0) return result; - DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Loading miniFAT chain", FL); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Loading miniFAT chain", FL,__func__); ole->miniFAT = OLE_load_chain( ole, ole->header.mini_fat_start ); if (ole->miniFAT == NULL) return OLEER_MINIFAT_READ_FAIL; - DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Loading Directory stream chain", FL); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Loading Directory stream chain", FL,__func__); ole->properties = OLE_load_chain( ole, ole->header.directory_stream_start_sector ); if (ole->properties == NULL) return OLEER_PROPERTIES_READ_FAIL; @@ -1750,42 +1729,42 @@ int OLE_decode( struct OLE_object *ole, char *decode_path ) property_value = get_uint8((char *)current_property); if (property_value < 1) break; - DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG:--------- DIRECTORY INDEX: %d",FL,i); + DOLE LOGGER_log("%s:%d:%s:DEBUG:--------- DIRECTORY INDEX: %d",FL,__func__,i); OLE_convert_directory( ole, current_property, adir ); DOLE { - LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Printing directory details...",FL); + LOGGER_log("%s:%d:%s:DEBUG: Printing directory details...",FL,__func__); OLE_print_directory( ole, adir); - LOGGER_log("%s:%d:OLE_decode_file:DEBUG: End of directory details",FL); + LOGGER_log("%s:%d:%s:DEBUG: End of directory details",FL,__func__); } if (adir->element_colour > 1) break; if ((adir->element_type == STGTY_INVALID)||(adir->element_type > STGTY_ROOT)) { - DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: breaking out due to element type %d",FL, adir->element_type); + DOLE LOGGER_log("%s:%d:%s:DEBUG: breaking out due to element type %d",FL,__func__, adir->element_type); break; } else if (adir->element_type == STGTY_ROOT){ /** ROOT DIRECTORY ENTRY **/ - DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Loading ministream/SmallBlockArray",FL); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Loading ministream/SmallBlockArray",FL,__func__); ole->ministream = OLE_load_chain( ole, adir->start_sector ); if (ole->ministream == NULL) return OLEER_MINISTREAM_READ_FAIL; - DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: ministream done",FL); + DOLE LOGGER_log("%s:%d:%s:DEBUG: ministream done",FL,__func__); } else if (adir->element_type == STGTY_STORAGE) { /** STORAGE ELEMENT **/ - DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Item is directory, start child is at index %d\n",FL,i); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Item is directory, start child is at index %d\n",FL,__func__,i); ole->ministream = OLE_load_chain( ole, adir->start_sector ); if (ole->ministream == NULL) return OLEER_MINISTREAM_READ_FAIL; - DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: DIRECTORY ministream done",FL); + DOLE LOGGER_log("%s:%d:%s:DEBUG: DIRECTORY ministream done",FL,__func__); } else if (adir->element_type == STGTY_STREAM) { /** STREAM ELEMENT **/ - OLE_decode_stream( ole, adir, decode_path ); + OLE_decode_stream( ole, adir, unpack_metadata ); } else { /** If the element isn't of the above types then it's possibly ** an empty element or just one used for the MSAT/SAT ** either way we just step over it and carry on **/ - DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Element type %d does not need to be handled",FL,adir->element_type); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Element type %d does not need to be handled",FL,__func__,adir->element_type); } // Jump to the next property record, which // is always 128 bytes ahead. @@ -1794,7 +1773,7 @@ int OLE_decode( struct OLE_object *ole, char *decode_path ) } // While there are still more directory entries to read in. - DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: Finished",FL); + DOLE LOGGER_log("%s:%d:%s:DEBUG: Finished",FL,__func__); /* OLE_decode_file_done(ole); */ @@ -1817,20 +1796,20 @@ int OLE_decode( struct OLE_object *ole, char *decode_path ) Changes: \------------------------------------------------------------------*/ -int OLE_decode_file( struct OLE_object *ole, char *fname, char *decode_path ) +int OLE_decode_file( struct OLE_object *ole, char *fname, RIPMIME_output *unpack_metadata ) { int result = 0; // Reject any bad paramters. if (ole == NULL) return OLEER_DECODE_NULL_OBJECT; if (fname == NULL) return OLEER_DECODE_NULL_FILENAME; - if (decode_path == NULL) return OLEER_DECODE_NULL_PATH; + if (unpack_metadata == NULL || unpack_metadata->dir == NULL) return OLEER_DECODE_NULL_PATH; // We need to gain access to the OLE2 data file, without // this pretty much everything is pointless. - DOLE LOGGER_log("%s:%d:OLE_decode_file:DEBUG: opening %s", FL, fname ); + DOLE LOGGER_log("%s:%d:%s:DEBUG: opening %s", FL,__func__, fname ); result = OLE_open_file( ole, fname ); if (result != 0) return result; - return OLE_decode( ole, decode_path ); + return OLE_decode( ole, unpack_metadata ); } diff --git a/ripOLE/ole.h b/ripOLE/ole.h index ae14894..17d162f 100644 --- a/ripOLE/ole.h +++ b/ripOLE/ole.h @@ -141,9 +141,8 @@ int OLE_follow_minichain( struct OLE_object *ole, int miniFAT_sector_start ); unsigned char *OLE_load_minichain( struct OLE_object *ole, int miniFAT_sector_start ); unsigned char *OLE_load_chain( struct OLE_object *ole, int FAT_sector_start ); int OLE_open_file( struct OLE_object *ole, char *fullpath ); -int OLE_decode_file( struct OLE_object *ole, char *fname, char *decode_path ); -int OLE_decode_file_done( struct OLE_object *ole ); - +int OLE_decode_file( struct OLE_object *ole, char *fname, RIPMIME_output *unpack_metadata ); +void OLE_decode_file_done( struct OLE_object *ole ); // Our callbacks. int OLE_set_filename_report_fn( struct OLE_object *ole, int (*ptr_to_fn)(char *) ); diff --git a/ripOLE/olestream-unwrap.c b/ripOLE/olestream-unwrap.c index 0b43663..851cdd6 100644 --- a/ripOLE/olestream-unwrap.c +++ b/ripOLE/olestream-unwrap.c @@ -8,6 +8,7 @@ #include "pldstr.h" #include "bytedecoders.h" +#include "mime_element.h" #include "olestream-unwrap.h" #define DUW if (oleuw->debug) @@ -145,48 +146,29 @@ int OLEUNWRAP_set_save_unknown_streams( struct OLEUNWRAP_object *oleuw, int leve Changes: \------------------------------------------------------------------*/ -int OLEUNWRAP_save_stream( struct OLEUNWRAP_object *oleuw, char *fname, char *decode_path, char *stream, size_t bytes ) +int OLEUNWRAP_save_stream( struct OLEUNWRAP_object *oleuw, char *fname, RIPMIME_output *unpack_metadata, char *stream, size_t bytes ) { - char *full_name; - FILE *f; int result = 0; + MIME_element* cur_mime = NULL; + size_t write_count; - DUW LOGGER_log("%s:%d:OLEUNWRAP_save_stream:DEBUG: fname=%s, decodepath=%s, size=%ld" + DUW LOGGER_log("%s:%d:%s:DEBUG: fname=%s, decodepath=%s, size=%ld" ,FL ,fname - ,decode_path + ,unpack_metadata->dir ,bytes ); - full_name = PLD_dprintf("%s/%s", decode_path, fname ); - if (full_name == NULL) - { - LOGGER_log("%s:%d:OLEUNWRAP_save_stream:ERROR: Unable to create filename string from '%s' and '%s'",FL,fname,decode_path); - return -1; - } + cur_mime = MIME_element_add (NULL, unpack_metadata, fname, "OLE", "OLE", "OLE", 0, 1, 0); - f = fopen(full_name,"w"); - if (f != NULL) + write_count = fwrite( stream, 1, bytes, cur_mime->f ); + if (write_count != bytes) { - size_t write_count; - - write_count = fwrite( stream, 1, bytes, f ); - if (write_count != bytes) - { - LOGGER_log("%s:%d:OLEUNWRAP_save_stream:WARNING: Only wrote %d of %d bytes to file %s\n",FL, write_count, bytes, full_name ); - } - - fclose(f); - - - } else { - LOGGER_log("%s:%d:OLEUNWRAP_save_stream:ERROR: Unable to open %s for writing (%s)\n",FL,full_name, strerror(errno)); - result = -1; + LOGGER_log("%s:%d:%s:WARNING: Only wrote %d of %d bytes to file %s\n",FL, write_count, bytes, cur_mime->fullpath ); } - if (full_name) free(full_name); - - DUW LOGGER_log("%s:%d:OLEUNWRAP_save_stream:DEBUG: Done saving '%s'",FL, fname); + MIME_element_remove (cur_mime); + DUW LOGGER_log("%s:%d:%s:DEBUG: Done saving '%s'",FL, fname); return result; } @@ -268,7 +250,7 @@ int OLEUNWRAP_seach_for_file_sig( struct OLEUNWRAP_object *oleuw, char *block, s p = tsp->sequence; /** set p to point to the start of the image signature sequence **/ cmpresult = memcmp(bp, p, 3); if (cmpresult == 0) { - DUW LOGGER_log("%s:%d:OLEUNWRAP_seach_for_file_sig:DEBUG: Hit at offset %d for signature %d",FL,(bp-block),(tsp -sigs)); + DUW LOGGER_log("%s:%d:%s:DEBUG: Hit at offset %d for signature %d",FL,(bp-block),(tsp -sigs)); hit = 1; break; } /** If we had a match in the signatures **/ @@ -290,8 +272,6 @@ int OLEUNWRAP_seach_for_file_sig( struct OLEUNWRAP_object *oleuw, char *block, s return result; } - - /** Look for PNG signature **/ /*-----------------------------------------------------------------\ Function Name : OLEUNWRAP_decode_attachment @@ -308,7 +288,7 @@ int OLEUNWRAP_seach_for_file_sig( struct OLEUNWRAP_object *oleuw, char *block, s Changes: \------------------------------------------------------------------*/ -int OLEUNWRAP_decode_attachment( struct OLEUNWRAP_object *oleuw, char *stream, size_t stream_size, char *decode_path ) +int OLEUNWRAP_decode_attachment( struct OLEUNWRAP_object *oleuw, char *stream, size_t stream_size, RIPMIME_output *unpack_metadata ) { struct OLE10_header oh; char *sp = stream; @@ -319,7 +299,7 @@ int OLEUNWRAP_decode_attachment( struct OLEUNWRAP_object *oleuw, char *stream, s oh.attach_size_1 = (size_t)get_int32( sp ); sp += 4; - DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: attachsize = %d [ 0x%x ], stream length = %d [ 0x%x] \n", FL, oh.attach_size_1, oh.attach_size_1, stream_size, stream_size ); + DUW LOGGER_log("%s:%d:%s:DEBUG: attachsize = %d [ 0x%x ], stream length = %d [ 0x%x] \n", FL, oh.attach_size_1, oh.attach_size_1, stream_size, stream_size ); oh.attach_start_offset = (stream_size -oh.attach_size_1); data_start_point = stream +oh.attach_start_offset; @@ -336,31 +316,31 @@ int OLEUNWRAP_decode_attachment( struct OLEUNWRAP_object *oleuw, char *stream, s // check next 4 bytes. cbheader = get_uint16( sp ); - DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: cbHeader = %d [ 0x%x ]", FL, cbheader, cbheader); + DUW LOGGER_log("%s:%d:%s:DEBUG: cbHeader = %d [ 0x%x ]", FL, cbheader, cbheader); mfpmm = get_uint16( sp +2 ); - DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: mfp.mm = %d [ 0x%x ]", FL, mfpmm, mfpmm); + DUW LOGGER_log("%s:%d:%s:DEBUG: mfp.mm = %d [ 0x%x ]", FL, mfpmm, mfpmm); mfpxext = get_uint16( sp +4 ); - DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: mfp.xext = %d [ 0x%x ]", FL, mfpxext, mfpxext); + DUW LOGGER_log("%s:%d:%s:DEBUG: mfp.xext = %d [ 0x%x ]", FL, mfpxext, mfpxext); mfpyext = get_uint16( sp +8 ); - DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: mfp.yext = %d [ 0x%x ]", FL, mfpyext, mfpyext); + DUW LOGGER_log("%s:%d:%s:DEBUG: mfp.yext = %d [ 0x%x ]", FL, mfpyext, mfpyext); mfphmf = get_uint16( sp +10 ); - DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: mfp.hmf = %d [ 0x%x ]", FL, mfphmf, mfphmf); + DUW LOGGER_log("%s:%d:%s:DEBUG: mfp.hmf = %d [ 0x%x ]", FL, mfphmf, mfphmf); // If we only had the stream byte-lenght in our header // then we know we don't have a complex header. DUW { switch (mfpmm) { case 100: - LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Image is Escher format",FL); + LOGGER_log("%s:%d:%s:DEBUG: Image is Escher format",FL); break; case 99: - LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Image is Bitmapped",FL); + LOGGER_log("%s:%d:%s:DEBUG: Image is Bitmapped",FL); break; case 98: - LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Image is TIFF",FL); + LOGGER_log("%s:%d:%s:DEBUG: Image is TIFF",FL); break; default: - LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Unknown image type for code '%d'",FL, mfpmm); + LOGGER_log("%s:%d:%s:DEBUG: Unknown image type for code '%d'",FL, mfpmm); } } @@ -371,25 +351,25 @@ int OLEUNWRAP_decode_attachment( struct OLEUNWRAP_object *oleuw, char *stream, s int imageoffset = 0; int search_size = 500; - DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: searcing for image signatures",FL); + DUW LOGGER_log("%s:%d:%s:DEBUG: searcing for image signatures",FL); if (stream_size < (search_size +68)) search_size = (stream_size -69); /** just make sure we don't over-search the stream **/ imageoffset = OLEUNWRAP_seach_for_file_sig(oleuw, data_start_point, search_size); if (imageoffset >= 0) { - DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Image data found at offset %d",FL,imageoffset); + DUW LOGGER_log("%s:%d:%s:DEBUG: Image data found at offset %d",FL,imageoffset); data_start_point += imageoffset; } else { - DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Could not detect image signature, dumping whole stream",FL); + DUW LOGGER_log("%s:%d:%s:DEBUG: Could not detect image signature, dumping whole stream",FL); } } oh.attach_name = PLD_dprintf("image-%ld",oh.attach_size_1); oh.attach_size = oh.attach_size_1; oh.fname_1 = oh.fname_2 = NULL; - DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Setting attachment name to '%s', size = %d",FL,oh.attach_name, oh.attach_size); + DUW LOGGER_log("%s:%d:%s:DEBUG: Setting attachment name to '%s', size = %d",FL,oh.attach_name, oh.attach_size); } else { - DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Decoding file information header",FL); + DUW LOGGER_log("%s:%d:%s:DEBUG: Decoding file information header",FL); // Unknown memory segment memcpy( oh.data, sp, 2 ); sp += 2; @@ -418,7 +398,7 @@ int OLEUNWRAP_decode_attachment( struct OLEUNWRAP_object *oleuw, char *stream, s data_start_point = sp; } - DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Attachment %s:%s:%s size = %d\n",FL, oh.attach_name, oh.fname_1, oh.fname_2, oh.attach_size ); + DUW LOGGER_log("%s:%d:%s:DEBUG: Attachment %s:%s:%s size = %d\n",FL, oh.attach_name, oh.fname_1, oh.fname_2, oh.attach_size ); /** 20050119:2053:PLD - Added to sanitize 8-bit filenames **/ @@ -427,12 +407,12 @@ int OLEUNWRAP_decode_attachment( struct OLEUNWRAP_object *oleuw, char *stream, s OLEUNWRAP_sanitize_filename(oh.fname_1); OLEUNWRAP_sanitize_filename(oh.fname_2); - DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Sanitized attachment filenames",FL); + DUW LOGGER_log("%s:%d:%s:DEBUG: Sanitized attachment filenames",FL); - result = OLEUNWRAP_save_stream( oleuw, oh.attach_name, decode_path, data_start_point, oh.attach_size ); + result = OLEUNWRAP_save_stream( oleuw, oh.attach_name, unpack_metadata, data_start_point, oh.attach_size ); if (result == OLEUW_OK) { - DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Calling reporter for the filename",FL); + DUW LOGGER_log("%s:%d:%s:DEBUG: Calling reporter for the filename",FL); if ((oleuw->verbose > 0)&&(oleuw->filename_report_fn != NULL)) { oleuw->filename_report_fn(oh.attach_name); @@ -440,13 +420,13 @@ int OLEUNWRAP_decode_attachment( struct OLEUNWRAP_object *oleuw, char *stream, s // Do call back to reporting function } - DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Cleaning up",FL); + DUW LOGGER_log("%s:%d:%s:DEBUG: Cleaning up",FL); // Clean up our previously allocated data if (oh.fname_1 != NULL) free(oh.fname_1); if (oh.attach_name != NULL) free(oh.attach_name); if (oh.fname_2 != NULL) free(oh.fname_2); - DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: done.",FL); + DUW LOGGER_log("%s:%d:%s:DEBUG: done.",FL); return OLEUW_OK; } @@ -466,23 +446,23 @@ int OLEUNWRAP_decode_attachment( struct OLEUNWRAP_object *oleuw, char *stream, s Changes: \------------------------------------------------------------------*/ -int OLEUNWRAP_decodestream( struct OLEUNWRAP_object *oleuw, char *element_string, char *stream, size_t stream_size, char *decode_path ) +int OLEUNWRAP_decodestream( struct OLEUNWRAP_object *oleuw, char *element_string, char *stream, size_t stream_size, RIPMIME_output *unpack_metadata ) { int result = OLEUW_OK; if (strstr(element_string, OLEUW_ELEMENT_10NATIVE_STRING) != NULL) { - DUW LOGGER_log("%s:%d:OLEUNWRAP_decodestream:DEBUG: Debugging element '%s'",FL, element_string); - OLEUNWRAP_decode_attachment( oleuw, stream, stream_size, decode_path ); + DUW LOGGER_log("%s:%d:%s:DEBUG: Debugging element '%s'",FL, element_string); + OLEUNWRAP_decode_attachment( oleuw, stream, stream_size, unpack_metadata ); } else if (strstr(element_string, OLEUW_ELEMENT_DATA) != NULL) { - DUW LOGGER_log("%s:%d:OLEUNWRAP_decodestream:DEBUG: Debugging element '%s'",FL, element_string); - OLEUNWRAP_decode_attachment( oleuw, stream, stream_size, decode_path ); + DUW LOGGER_log("%s:%d:%s:DEBUG: Debugging element '%s'",FL, element_string); + OLEUNWRAP_decode_attachment( oleuw, stream, stream_size, unpack_metadata ); } else { - DUW LOGGER_log("%s:%d:OLEUNWRAP_decode_attachment:DEBUG: Unable to decode stream with element string '%s'\n", FL, element_string); + DUW LOGGER_log("%s:%d:%s:DEBUG: Unable to decode stream with element string '%s'\n", FL, element_string); result = OLEUW_STREAM_NOT_DECODED; } diff --git a/ripOLE/olestream-unwrap.h b/ripOLE/olestream-unwrap.h index 82f6e90..df2c7df 100644 --- a/ripOLE/olestream-unwrap.h +++ b/ripOLE/olestream-unwrap.h @@ -19,9 +19,9 @@ int OLEUNWRAP_set_debug( struct OLEUNWRAP_object *oleuw, int level ); int OLEUNWRAP_set_verbose( struct OLEUNWRAP_object *oleuw, int level ); int OLEUNWRAP_set_save_unknown_streams( struct OLEUNWRAP_object *oleuw, int level ); -int OLEUNWRAP_save_stream( struct OLEUNWRAP_object *oleuw, char *fname, char *decode_path, char *stream, size_t bytes ); -int OLEUNWRAP_decode_attachment( struct OLEUNWRAP_object *oleuw, char *stream, size_t stream_size, char *decode_path ); -int OLEUNWRAP_decodestream( struct OLEUNWRAP_object *oleuw, char *element_string, char *stream, size_t stream_size, char *decode_path ); +int OLEUNWRAP_save_stream( struct OLEUNWRAP_object *oleuw, char *fname, RIPMIME_output *unpack_metadata, char *stream, size_t bytes ); +int OLEUNWRAP_decode_attachment( struct OLEUNWRAP_object *oleuw, char *stream, size_t stream_size, RIPMIME_output *unpack_metadata ); +int OLEUNWRAP_decodestream( struct OLEUNWRAP_object *oleuw, char *element_string, char *stream, size_t stream_size, RIPMIME_output *unpack_metadata ); int OLEUNWRAP_set_filename_report_fn( struct OLEUNWRAP_object *oleuw, int (*ptr_to_fn)(char *) ); diff --git a/ripOLE/ripole.c b/ripOLE/ripole.c index 387d382..998b3c3 100644 --- a/ripOLE/ripole.c +++ b/ripOLE/ripole.c @@ -8,6 +8,7 @@ #include "logger.h" #include "pldstr.h" +#include "mime_element.h" #include "ole.h" struct ripOLE_object { @@ -309,6 +310,7 @@ int main( int argc, char **argv ) { struct ripOLE_object role; struct OLE_object *ole = NULL; + RIPMIME_output o; int result = 0; if (argc == 1) { fprintf (stdout, "%s\n", help); exit(1); } @@ -320,7 +322,6 @@ int main( int argc, char **argv ) return 1; } - LOGGER_set_output_mode(_LOGGER_STDOUT); OLE_init(ole); @@ -335,7 +336,9 @@ int main( int argc, char **argv ) OLE_set_filename_report_fn(ole, ROLE_report_filename_decoded ); - result = OLE_decode_file( ole, role.inputfile, role.outputdir ); + o.dir = role.outputdir; + o.unpack_mode = RIPMIME_UNPACK_MODE_TO_DIRECTORY; + result = OLE_decode_file( ole, role.inputfile, &o ); OLE_decode_file_done(ole); if ((result != 0)) { diff --git a/ripmime.c b/ripmime.c index d45c400..f55a44f 100644 --- a/ripmime.c +++ b/ripmime.c @@ -590,7 +590,6 @@ int RIPMIME_unpack_single( struct RIPMIME_globals *glb, char *fname ) alarm(glb->timeout); } - MIMEH_set_outputdir (glb->output->dir); result = MIME_unpack (glb->output, fname, 0); // do any last minute things @@ -724,10 +723,8 @@ int main (int argc, char **argv) MIME_set_header_longsearch(1); // 20040310-0117:PLD - Added by default as it seems stable, use --disable-qmail-bounce to turn off MIME_set_renamemethod (_MIME_RENAME_METHOD_INFIX); - RIPMIME_parse_parameters (&glb, argc, argv); - // if our input filename wasn't specified, then we better let the user know! if (!glb.inputfile) { @@ -736,7 +733,6 @@ int main (int argc, char **argv) } // Fire up the randomizer - srand (time (NULL)); // clean up the output directory name if required (remove any trailing /'s, as suggested by James Cownie 03/02/2001 diff --git a/tnef/tnef.c b/tnef/tnef.c index 4d55b22..e94a377 100644 --- a/tnef/tnef.c +++ b/tnef/tnef.c @@ -53,7 +53,7 @@ #define VERSION "pldtnef/0.0.2" #ifndef FL -#define FL __FILE__, __LINE__, __func__ +#define FL __FILE__, __LINE__ #endif #define TNEF_VERBOSE ((TNEF_glb.verbose > 0)) @@ -187,7 +187,7 @@ int read_32( uint32 *value, uint8 *tsp) if ((tsp +4) > TNEF_glb.tnef_limit) { - if ((TNEF_VERBOSE)||(TNEF_DEBUG)) LOGGER_log("%s:%d:%s:ERROR: Attempting to read beyond end of memory block",FL); + if ((TNEF_VERBOSE)||(TNEF_DEBUG)) LOGGER_log("%s:%d:%s:ERROR: Attempting to read beyond end of memory block",FL,__func__); return -1; } @@ -217,7 +217,7 @@ int read_16( uint16 *value, uint8 *tsp) //PLD-20070707-17H20, how do we even compare tsp to a negative!? ||(tsp == -1) ) { - if ((TNEF_VERBOSE)||(TNEF_DEBUG)) LOGGER_log("%s:%d:%s:ERROR: Attempting to read past end\n",FL); + if ((TNEF_VERBOSE)||(TNEF_DEBUG)) LOGGER_log("%s:%d:%s:ERROR: Attempting to read past end\n",FL,__func__); return -1; } @@ -264,7 +264,7 @@ int save_attach_data(char *title, uint8 *tsp, uint32 size, char * file_dir) out = fopen(fn, "w"); if (!out) { - LOGGER_log("%s:%d:%s:ERROR: Failed opening file %s for writing (%s)\n", FL, fn, strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: Failed opening file %s for writing (%s)\n", FL,__func__, fn, strerror(errno)); free(fn); return -1; } @@ -419,14 +419,14 @@ int read_attribute(uint8 *tsp, char *file_dir) // Read the attributes of this component - if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Reading Attribute...\n",FL); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Reading Attribute...\n",FL,__func__); rv = read_32(&attribute, tsp+bytes); if (rv == -1) return -1; bytes += sizeof(attribute); // Read the size of the information we have to read - if (TNEF_DEBUG) LOGGER_log("%s:%d:%s: Reading Size...\n",FL); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s: Reading Size...\n",FL,__func__); rv = read_32(&size, tsp+bytes); if (rv == -1) return -1; bytes += sizeof(size); @@ -454,12 +454,12 @@ int read_attribute(uint8 *tsp, char *file_dir) // --END of ammendment. - if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Reading Checksum...(offset %d, bytes=%d)\n", FL, tsp -TNEF_glb.tnef_home, bytes); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Reading Checksum...(offset %d, bytes=%d)\n", FL,__func__, tsp -TNEF_glb.tnef_home, bytes); if (read_16(&checksum, tsp+bytes) == -1) return -1; bytes += sizeof(checksum); - if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Decoding attribute %d\n", FL, attribute); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Decoding attribute %d\n", FL,__func__, attribute); switch (attribute) { case attNull: @@ -508,7 +508,7 @@ int read_attribute(uint8 *tsp, char *file_dir) } else { - LOGGER_log("%s:%d:%s:ERROR: While saving attachment '%s'\n", FL, attach_title); + LOGGER_log("%s:%d:%s:ERROR: While saving attachment '%s'\n", FL,__func__, attach_title); } } break; @@ -529,7 +529,7 @@ int read_attribute(uint8 *tsp, char *file_dir) } else { - LOGGER_log("%s:%d:%s:ERROR: While saving attachment '%s'\n", FL, attach_title); + LOGGER_log("%s:%d:%s:ERROR: While saving attachment '%s'\n", FL,__func__, attach_title); } } break; @@ -614,7 +614,7 @@ int TNEF_decode_tnef(uint8 *tnef_stream, int size, char* file_dir) uint16 tnef_attachkey; uint8 *tsp; - if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Start. Size = %d\n", FL,size); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Start. Size = %d\n", FL,__func__,size); // TSP == TNEF Stream Pointer (well memory block actually!) // @@ -625,9 +625,9 @@ int TNEF_decode_tnef(uint8 *tnef_stream, int size, char* file_dir) ra_response = read_32(&tnefs, tsp); if ((ra_response != -1)&&(TNEF_SIGNATURE == tnefs)) { - if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: TNEF signature is good\n",FL); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: TNEF signature is good\n",FL,__func__); } else { - if (TNEF_VERBOSE) LOGGER_log("%s:%d:%s:WARNING: Bad TNEF signature, expecting %lx got %lx\n",FL,TNEF_SIGNATURE,tnefs); + if (TNEF_VERBOSE) LOGGER_log("%s:%d:%s:WARNING: Bad TNEF signature, expecting %lx got %lx\n",FL,__func__,TNEF_SIGNATURE,tnefs); } // Move tsp pointer along @@ -636,7 +636,7 @@ int TNEF_decode_tnef(uint8 *tnef_stream, int size, char* file_dir) /** Read the TNEF Attach key **/ if (read_16(&tnef_attachkey, tsp) == -1) return -1; - if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: TNEF Attach Key: %x\n",FL,tnef_attachkey); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: TNEF Attach Key: %x\n",FL,__func__,tnef_attachkey); // Move tsp pointer along // @@ -646,10 +646,10 @@ int TNEF_decode_tnef(uint8 *tnef_stream, int size, char* file_dir) // go through entire memory block and extract // all the required attributes and files // - if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: TNEF - Commence reading attributes\n",FL); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: TNEF - Commence reading attributes\n",FL,__func__); while ((tsp - tnef_stream) < size) { - if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Offset = %d\n", FL,tsp -TNEF_glb.tnef_home); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Offset = %d\n", FL,__func__,tsp -TNEF_glb.tnef_home); ra_response = read_attribute(tsp, file_dir); if ( ra_response > 0 ) { @@ -659,12 +659,12 @@ int TNEF_decode_tnef(uint8 *tnef_stream, int size, char* file_dir) // Must find out /WHY/ this happens, and, how to rectify the issue. tsp++; - if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:WARNING: TNEF - Attempting to read attribute at %d resulted in a sub-zero response, ending decoding to be safe\n",FL,tsp); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:WARNING: TNEF - Attempting to read attribute at %d resulted in a sub-zero response, ending decoding to be safe\n",FL,__func__,tsp); break; } } - if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Done.\n",FL); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Done.\n",FL,__func__); return 0; } @@ -682,13 +682,13 @@ int TNEF_main( char *filename, char *file_dir ) uint8 *tnef_stream; int size, nread; - if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Start, decoding %s\n",FL, filename); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Start, decoding %s\n",FL,__func__, filename); // Attempt to open up the TNEF encoded file... if it fails // then report the failed condition to syslog if ((fp = fopen(filename,"r")) == NULL) { - LOGGER_log("%s:%d:%s:ERROR: opening file %s for reading (%s)\n", FL, filename,strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: opening file %s for reading (%s)\n", FL,__func__, filename,strerror(errno)); if (TNEF_glb.tnef_home) free(TNEF_glb.tnef_home); return -1; } @@ -708,7 +708,7 @@ int TNEF_main( char *filename, char *file_dir ) // should report this if (tnef_stream == NULL) { - LOGGER_log("%s:%d:%s:ERROR: When allocating %d bytes for loading file (%s)\n", FL, size,strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: When allocating %d bytes for loading file (%s)\n", FL,__func__, size,strerror(errno)); if (TNEF_glb.tnef_home) free(TNEF_glb.tnef_home); return -1; } @@ -717,12 +717,12 @@ int TNEF_main( char *filename, char *file_dir ) // Attempt to read in the entire file nread = fread(tnef_stream, sizeof(uint8), size, fp); - if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Read %d bytes\n", FL, nread); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Read %d bytes\n", FL,__func__, nread); // If we did not read in all the bytes, then let syslogs know! if (nread < size) { - LOGGER_log("%s:%d:%s:ERROR: while reading stream from file %s (%s)\n", FL, filename,strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: while reading stream from file %s (%s)\n", FL,__func__, filename,strerror(errno)); if (TNEF_glb.tnef_home) free(TNEF_glb.tnef_home); return -1; } @@ -735,7 +735,7 @@ int TNEF_main( char *filename, char *file_dir ) if (TNEF_glb.tnef_home) free(TNEF_glb.tnef_home); - if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: finished decoding.\n",FL); + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: finished decoding.\n",FL,__func__); return 0; } diff --git a/uuencode.c b/uuencode.c index f496abc..881afc1 100644 --- a/uuencode.c +++ b/uuencode.c @@ -19,6 +19,8 @@ The biggest issue is that the interfaces to the decoding functions are too speci #include "pldstr.h" #include "ffget.h" #include "filename-filters.h" +#include "strstack.h" +#include "mime_headers.h" #include "mime_element.h" #include "uuencode.h" @@ -359,7 +361,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, { int filename_found = 0; char buf[ UUENCODE_STRLEN_MAX ]; - char *bp = buf, *fn, *fp; + char *bp = buf, *fn = NULL, *fp = NULL; int n, i, expected; struct PLD_strtok tx; unsigned char *writebuffer = NULL; @@ -484,14 +486,8 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, if ((filename_found != 0)&&(bp)) { - MIME_element* mime_el = NULL; - char * fn; - int fn_l = strlen(unpack_metadata->dir) + strlen(bp) + sizeof(char) * 2; - - fn = malloc(fn_l); - snprintf(fn,fn_l,"%s/%s",unpack_metadata->dir,bp); - - + MIME_element* cur_mime = NULL; + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Located filename (%s), now decoding.\n", FL, bp); // Clean up the file name @@ -503,18 +499,16 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Filename = (%s)\n", FL, fn); - mime_el = MIME_element_add_with_path (fn, unpack_metadata, hinfo, 1, filecount); - free(fn); + cur_mime = MIME_element_add (NULL, unpack_metadata, fn, hinfo->content_type_string, hinfo->content_transfer_encoding_string, hinfo->name, hinfo->current_recursion_level, 1, filecount); // Allocate the write buffer. By using the write buffer we gain an additional 10% in performance // due to the lack of function call (fwrite) overheads // Okay, now we have the UUDECODE data to decode... - wbcount = 0; wbpos = writebuffer; - while (mime_el->f) + while (cur_mime->f) { // for each input line FFGET_fgets(buf, sizeof(buf), finf); @@ -564,7 +558,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, // In order to reduce function call overheads, we've bought the UUDecoding // bit shifting routines into the UUDecode main decoding routines. This should // save us about 250,000 function calls per Mb. - // UUENCODE_outdec(bp, mime_el->f, n); + // UUENCODE_outdec(bp, cur_mime->f, n); char c[3]; int m = n; @@ -578,7 +572,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, if ( wbcount >= UUENCODE_WRITE_BUFFER_LIMIT ) { size_t bc; - bc = fwrite(writebuffer, 1, wbcount, mime_el->f); + bc = fwrite(writebuffer, 1, wbcount, cur_mime->f); if (bc != wbcount) { LOGGER_log("%s:%d:ERROR: Attempted to write %ld bytes, only wrote %ld\n", FL, wbcount, bc); } @@ -605,16 +599,16 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, } // While (1) - if ((mime_el->f != NULL)&&(wbcount > 0)) + if ((cur_mime->f != NULL)&&(wbcount > 0)) { - size_t bc = fwrite(writebuffer, 1, wbcount, mime_el->f); + size_t bc = fwrite(writebuffer, 1, wbcount, cur_mime->f); if (bc != wbcount) { LOGGER_log("%s:%d:ERROR: Attempted to write %ld bytes, only wrote %ld\n", FL, wbcount, bc); } } - MIME_element_remove(mime_el); + MIME_element_remove(cur_mime); // Call our reporting function, else, if no function is defined, use the default // standard call From a28caa43ed3a5cd527a79d71677e788f14f6634c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Sat, 26 Apr 2025 20:05:39 +0300 Subject: [PATCH 38/80] Add debug refactoring to C99 --- ripOLE/olestream-unwrap.c | 54 +++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/ripOLE/olestream-unwrap.c b/ripOLE/olestream-unwrap.c index 851cdd6..fccf709 100644 --- a/ripOLE/olestream-unwrap.c +++ b/ripOLE/olestream-unwrap.c @@ -153,7 +153,7 @@ int OLEUNWRAP_save_stream( struct OLEUNWRAP_object *oleuw, char *fname, RIPMIME_ size_t write_count; DUW LOGGER_log("%s:%d:%s:DEBUG: fname=%s, decodepath=%s, size=%ld" - ,FL + ,FL,__func__ ,fname ,unpack_metadata->dir ,bytes @@ -164,11 +164,11 @@ int OLEUNWRAP_save_stream( struct OLEUNWRAP_object *oleuw, char *fname, RIPMIME_ write_count = fwrite( stream, 1, bytes, cur_mime->f ); if (write_count != bytes) { - LOGGER_log("%s:%d:%s:WARNING: Only wrote %d of %d bytes to file %s\n",FL, write_count, bytes, cur_mime->fullpath ); + LOGGER_log("%s:%d:%s:WARNING: Only wrote %d of %d bytes to file %s\n",FL,__func__, write_count, bytes, cur_mime->fullpath ); } MIME_element_remove (cur_mime); - DUW LOGGER_log("%s:%d:%s:DEBUG: Done saving '%s'",FL, fname); + DUW LOGGER_log("%s:%d:%s:DEBUG: Done saving '%s'",FL,__func__, fname); return result; } @@ -250,7 +250,7 @@ int OLEUNWRAP_seach_for_file_sig( struct OLEUNWRAP_object *oleuw, char *block, s p = tsp->sequence; /** set p to point to the start of the image signature sequence **/ cmpresult = memcmp(bp, p, 3); if (cmpresult == 0) { - DUW LOGGER_log("%s:%d:%s:DEBUG: Hit at offset %d for signature %d",FL,(bp-block),(tsp -sigs)); + DUW LOGGER_log("%s:%d:%s:DEBUG: Hit at offset %d for signature %d",FL,__func__,(bp-block),(tsp -sigs)); hit = 1; break; } /** If we had a match in the signatures **/ @@ -299,7 +299,7 @@ int OLEUNWRAP_decode_attachment( struct OLEUNWRAP_object *oleuw, char *stream, s oh.attach_size_1 = (size_t)get_int32( sp ); sp += 4; - DUW LOGGER_log("%s:%d:%s:DEBUG: attachsize = %d [ 0x%x ], stream length = %d [ 0x%x] \n", FL, oh.attach_size_1, oh.attach_size_1, stream_size, stream_size ); + DUW LOGGER_log("%s:%d:%s:DEBUG: attachsize = %d [ 0x%x ], stream length = %d [ 0x%x] \n", FL,__func__, oh.attach_size_1, oh.attach_size_1, stream_size, stream_size ); oh.attach_start_offset = (stream_size -oh.attach_size_1); data_start_point = stream +oh.attach_start_offset; @@ -316,31 +316,31 @@ int OLEUNWRAP_decode_attachment( struct OLEUNWRAP_object *oleuw, char *stream, s // check next 4 bytes. cbheader = get_uint16( sp ); - DUW LOGGER_log("%s:%d:%s:DEBUG: cbHeader = %d [ 0x%x ]", FL, cbheader, cbheader); + DUW LOGGER_log("%s:%d:%s:DEBUG: cbHeader = %d [ 0x%x ]", FL,__func__, cbheader, cbheader); mfpmm = get_uint16( sp +2 ); - DUW LOGGER_log("%s:%d:%s:DEBUG: mfp.mm = %d [ 0x%x ]", FL, mfpmm, mfpmm); + DUW LOGGER_log("%s:%d:%s:DEBUG: mfp.mm = %d [ 0x%x ]", FL,__func__, mfpmm, mfpmm); mfpxext = get_uint16( sp +4 ); - DUW LOGGER_log("%s:%d:%s:DEBUG: mfp.xext = %d [ 0x%x ]", FL, mfpxext, mfpxext); + DUW LOGGER_log("%s:%d:%s:DEBUG: mfp.xext = %d [ 0x%x ]", FL,__func__, mfpxext, mfpxext); mfpyext = get_uint16( sp +8 ); - DUW LOGGER_log("%s:%d:%s:DEBUG: mfp.yext = %d [ 0x%x ]", FL, mfpyext, mfpyext); + DUW LOGGER_log("%s:%d:%s:DEBUG: mfp.yext = %d [ 0x%x ]", FL,__func__, mfpyext, mfpyext); mfphmf = get_uint16( sp +10 ); - DUW LOGGER_log("%s:%d:%s:DEBUG: mfp.hmf = %d [ 0x%x ]", FL, mfphmf, mfphmf); + DUW LOGGER_log("%s:%d:%s:DEBUG: mfp.hmf = %d [ 0x%x ]", FL,__func__, mfphmf, mfphmf); // If we only had the stream byte-lenght in our header // then we know we don't have a complex header. DUW { switch (mfpmm) { case 100: - LOGGER_log("%s:%d:%s:DEBUG: Image is Escher format",FL); + LOGGER_log("%s:%d:%s:DEBUG: Image is Escher format",FL,__func__); break; case 99: - LOGGER_log("%s:%d:%s:DEBUG: Image is Bitmapped",FL); + LOGGER_log("%s:%d:%s:DEBUG: Image is Bitmapped",FL,__func__); break; case 98: - LOGGER_log("%s:%d:%s:DEBUG: Image is TIFF",FL); + LOGGER_log("%s:%d:%s:DEBUG: Image is TIFF",FL,__func__); break; default: - LOGGER_log("%s:%d:%s:DEBUG: Unknown image type for code '%d'",FL, mfpmm); + LOGGER_log("%s:%d:%s:DEBUG: Unknown image type for code '%d'",FL,__func__, mfpmm); } } @@ -351,25 +351,25 @@ int OLEUNWRAP_decode_attachment( struct OLEUNWRAP_object *oleuw, char *stream, s int imageoffset = 0; int search_size = 500; - DUW LOGGER_log("%s:%d:%s:DEBUG: searcing for image signatures",FL); + DUW LOGGER_log("%s:%d:%s:DEBUG: searcing for image signatures",FL,__func__); if (stream_size < (search_size +68)) search_size = (stream_size -69); /** just make sure we don't over-search the stream **/ imageoffset = OLEUNWRAP_seach_for_file_sig(oleuw, data_start_point, search_size); if (imageoffset >= 0) { - DUW LOGGER_log("%s:%d:%s:DEBUG: Image data found at offset %d",FL,imageoffset); + DUW LOGGER_log("%s:%d:%s:DEBUG: Image data found at offset %d",FL,__func__,imageoffset); data_start_point += imageoffset; } else { - DUW LOGGER_log("%s:%d:%s:DEBUG: Could not detect image signature, dumping whole stream",FL); + DUW LOGGER_log("%s:%d:%s:DEBUG: Could not detect image signature, dumping whole stream",FL,__func__); } } oh.attach_name = PLD_dprintf("image-%ld",oh.attach_size_1); oh.attach_size = oh.attach_size_1; oh.fname_1 = oh.fname_2 = NULL; - DUW LOGGER_log("%s:%d:%s:DEBUG: Setting attachment name to '%s', size = %d",FL,oh.attach_name, oh.attach_size); + DUW LOGGER_log("%s:%d:%s:DEBUG: Setting attachment name to '%s', size = %d",FL,__func__,oh.attach_name, oh.attach_size); } else { - DUW LOGGER_log("%s:%d:%s:DEBUG: Decoding file information header",FL); + DUW LOGGER_log("%s:%d:%s:DEBUG: Decoding file information header",FL,__func__); // Unknown memory segment memcpy( oh.data, sp, 2 ); sp += 2; @@ -398,7 +398,7 @@ int OLEUNWRAP_decode_attachment( struct OLEUNWRAP_object *oleuw, char *stream, s data_start_point = sp; } - DUW LOGGER_log("%s:%d:%s:DEBUG: Attachment %s:%s:%s size = %d\n",FL, oh.attach_name, oh.fname_1, oh.fname_2, oh.attach_size ); + DUW LOGGER_log("%s:%d:%s:DEBUG: Attachment %s:%s:%s size = %d\n",FL,__func__, oh.attach_name, oh.fname_1, oh.fname_2, oh.attach_size ); /** 20050119:2053:PLD - Added to sanitize 8-bit filenames **/ @@ -407,12 +407,12 @@ int OLEUNWRAP_decode_attachment( struct OLEUNWRAP_object *oleuw, char *stream, s OLEUNWRAP_sanitize_filename(oh.fname_1); OLEUNWRAP_sanitize_filename(oh.fname_2); - DUW LOGGER_log("%s:%d:%s:DEBUG: Sanitized attachment filenames",FL); + DUW LOGGER_log("%s:%d:%s:DEBUG: Sanitized attachment filenames",FL,__func__); result = OLEUNWRAP_save_stream( oleuw, oh.attach_name, unpack_metadata, data_start_point, oh.attach_size ); if (result == OLEUW_OK) { - DUW LOGGER_log("%s:%d:%s:DEBUG: Calling reporter for the filename",FL); + DUW LOGGER_log("%s:%d:%s:DEBUG: Calling reporter for the filename",FL,__func__); if ((oleuw->verbose > 0)&&(oleuw->filename_report_fn != NULL)) { oleuw->filename_report_fn(oh.attach_name); @@ -420,13 +420,13 @@ int OLEUNWRAP_decode_attachment( struct OLEUNWRAP_object *oleuw, char *stream, s // Do call back to reporting function } - DUW LOGGER_log("%s:%d:%s:DEBUG: Cleaning up",FL); + DUW LOGGER_log("%s:%d:%s:DEBUG: Cleaning up",FL,__func__); // Clean up our previously allocated data if (oh.fname_1 != NULL) free(oh.fname_1); if (oh.attach_name != NULL) free(oh.attach_name); if (oh.fname_2 != NULL) free(oh.fname_2); - DUW LOGGER_log("%s:%d:%s:DEBUG: done.",FL); + DUW LOGGER_log("%s:%d:%s:DEBUG: done.",FL,__func__); return OLEUW_OK; } @@ -453,16 +453,16 @@ int OLEUNWRAP_decodestream( struct OLEUNWRAP_object *oleuw, char *element_string if (strstr(element_string, OLEUW_ELEMENT_10NATIVE_STRING) != NULL) { - DUW LOGGER_log("%s:%d:%s:DEBUG: Debugging element '%s'",FL, element_string); + DUW LOGGER_log("%s:%d:%s:DEBUG: Debugging element '%s'",FL,__func__, element_string); OLEUNWRAP_decode_attachment( oleuw, stream, stream_size, unpack_metadata ); } else if (strstr(element_string, OLEUW_ELEMENT_DATA) != NULL) { - DUW LOGGER_log("%s:%d:%s:DEBUG: Debugging element '%s'",FL, element_string); + DUW LOGGER_log("%s:%d:%s:DEBUG: Debugging element '%s'",FL,__func__, element_string); OLEUNWRAP_decode_attachment( oleuw, stream, stream_size, unpack_metadata ); } else { - DUW LOGGER_log("%s:%d:%s:DEBUG: Unable to decode stream with element string '%s'\n", FL, element_string); + DUW LOGGER_log("%s:%d:%s:DEBUG: Unable to decode stream with element string '%s'\n", FL,__func__, element_string); result = OLEUW_STREAM_NOT_DECODED; } From 5f538bc74f041e28b9a63458ce961c791d19c33c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Sat, 26 Apr 2025 20:54:50 +0300 Subject: [PATCH 39/80] Simple UUE refactoring to `FILE` struct --- mime.c | 25 +++++++++++--- mime.h | 28 +++++++-------- uuencode.c | 99 +++++++++++++++++++++++++----------------------------- uuencode.h | 15 +++++---- 4 files changed, 88 insertions(+), 79 deletions(-) diff --git a/mime.c b/mime.c index ed7c962..8ca1347 100644 --- a/mime.c +++ b/mime.c @@ -1275,12 +1275,16 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME if (file_has_uuencode) { char full_decode_path[512]; + FILE *fuue = NULL; + FFGET_FILE * ffg = NULL; + snprintf(full_decode_path,sizeof(full_decode_path),"%s/%s",unpack_metadata->dir,hinfo->filename); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UUencoded data\n",FL,__func__); if ( hinfo->content_transfer_encoding == _CTRANS_ENCODING_UUENCODE ) decode_entire_file = 0; - //result = UUENCODE_decode_uu(NULL, hinfo->filename, hinfo->uudec_name, decode_entire_file, keep ); - result = UUENCODE_decode_uu(NULL, full_decode_path, hinfo->uudec_name, decode_entire_file, keep, unpack_metadata, hinfo ); + fuue = UUENCODE_make_file_obj (full_decode_path); + ffg = UUENCODE_make_sourcestream(fuue); + result = UUENCODE_decode_uu(ffg , hinfo->uudec_name, decode_entire_file, keep, unpack_metadata, hinfo ); if (result == -1) { switch (uuencode_error) { @@ -1417,6 +1421,8 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM // if (file_has_uuencode) { + FILE *fuue = NULL; + FFGET_FILE * ffg = NULL; char ffname[256]; snprintf(ffname,256,"%s/%s", unpack_metadata->dir, hinfo->filename); // PLD-20040627-1212 @@ -1433,8 +1439,11 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM // NOTE - this function returns the NUMBER of attachments it decoded in the return value! Don't // propergate this value unintentionally to parent functions (ie, if you were thinking it was // an error-status return value + fuue = UUENCODE_make_file_obj (ffname); + ffg = UUENCODE_make_sourcestream(fuue); - result = UUENCODE_decode_uu( NULL, ffname, hinfo->uudec_name, 1, keep, unpack_metadata, hinfo ); + result = UUENCODE_decode_uu( ffg, hinfo->uudec_name, 1, keep, unpack_metadata, hinfo ); + fclose(fuue); if (result == -1) { switch (uuencode_error) { @@ -1842,9 +1851,15 @@ int MIME_doubleCR_decode( char *filename, RIPMIME_output *unpack_metadata, struc } else if (UUENCODE_is_diskfile_uuencoded(h.filename)) { + FILE *fuue = NULL; + FFGET_FILE * ffg = NULL; + if (MIME_VERBOSE) LOGGER_log("Attempting to decode UUENCODED attachment from Double-CR delimeted attachment '%s'\n",filename); + fuue = UUENCODE_make_file_obj (filename); + ffg = UUENCODE_make_sourcestream(fuue); UUENCODE_set_doubleCR_mode(1); - result = UUENCODE_decode_uu(NULL, filename, h.uudec_name, 1, 1, unpack_metadata, hinfo ); + result = UUENCODE_decode_uu(ffg, h.uudec_name, 1, 1, unpack_metadata, hinfo ); + fclose(fuue); UUENCODE_set_doubleCR_mode(0); glb.attachment_count += result; result = 0; @@ -2310,7 +2325,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UUENCODED format\n",FL,__func__); // Added as a test - remove if we can get this to work in a better way snprintf(hinfo->uudec_name,sizeof(hinfo->uudec_name),"%s",hinfo->filename); - result = UUENCODE_decode_uu(f, hinfo->filename, hinfo->uudec_name, 0, keep, unpack_metadata, hinfo ); + result = UUENCODE_decode_uu(f, hinfo->uudec_name, 0, keep, unpack_metadata, hinfo ); glb.attachment_count += result; // Because this is a file-count, it's not really an 'error result' as such, so, set the // return code back to 0! diff --git a/mime.h b/mime.h index c2211f3..ef633dc 100644 --- a/mime.h +++ b/mime.h @@ -6,20 +6,20 @@ #define LIBMIME_VERSION "200811050000" /* Exit Error codes */ -#define _EXITERR_UNDEFINED_BOUNDARY 100 -#define _EXITERR_PRINT_QUOTABLE_INPUT_NOT_OPEN 200 -#define _EXITERR_PRINT_QUOTABLE_OUTPUT_NOT_OPEN 201 -#define _EXITERR_BASE64_OUTPUT_NOT_OPEN 210 -#define _EXITERR_BASE64_UNABLE_TO_OUTPUT 211 -#define _EXITERR_MIMEREAD_CANNOT_OPEN_OUTPUT 220 -#define _EXITERR_MIMEREAD_CANNOT_WRITE_OUTPUT 221 -#define _EXITERR_MIMEREAD_CANNOT_OPEN_INPUT 222 -#define _EXITERR_MIMEUNPACK_CANNOT_OPEN_INPUT_FILE 230 -#define _EXITERR_MIMEUNPACK_CANNOT_OPEN_HEADERS_FILE 231 - -#define MIME_ERROR_RECURSION_LIMIT_REACHED 240 -#define MIME_ERROR_FFGET_EMPTY 241 -#define MIME_ERROR_B64_INPUT_STREAM_EOF 242 +#define _EXITERR_UNDEFINED_BOUNDARY 100 +#define _EXITERR_PRINT_QUOTABLE_INPUT_NOT_OPEN 200 +#define _EXITERR_PRINT_QUOTABLE_OUTPUT_NOT_OPEN 201 +#define _EXITERR_BASE64_OUTPUT_NOT_OPEN 210 +#define _EXITERR_BASE64_UNABLE_TO_OUTPUT 211 +#define _EXITERR_MIMEREAD_CANNOT_OPEN_OUTPUT 220 +#define _EXITERR_MIMEREAD_CANNOT_WRITE_OUTPUT 221 +#define _EXITERR_MIMEREAD_CANNOT_OPEN_INPUT 222 +#define _EXITERR_MIMEUNPACK_CANNOT_OPEN_INPUT_FILE 230 +#define _EXITERR_MIMEUNPACK_CANNOT_OPEN_HEADERS_FILE 231 + +#define MIME_ERROR_RECURSION_LIMIT_REACHED 240 +#define MIME_ERROR_FFGET_EMPTY 241 +#define MIME_ERROR_B64_INPUT_STREAM_EOF 242 #define _MIME_RENAME_METHOD_INFIX 1 #define _MIME_RENAME_METHOD_PREFIX 2 diff --git a/uuencode.c b/uuencode.c index 881afc1..5d055f1 100644 --- a/uuencode.c +++ b/uuencode.c @@ -45,19 +45,19 @@ The biggest issue is that the interfaces to the decoding functions are too speci static unsigned char uudec[256]={ 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,\ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,\ - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,\ + 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, 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, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,\ - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,\ + 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, 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, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,\ - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,\ + 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, 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, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,\ - 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,\ + 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, 26, 27, 28, 29, 30, 31 \ }; @@ -68,6 +68,7 @@ struct UUENCODE_globals { int decode; int doubleCR_mode; int (*filename_decoded_report)(char *, char *); // Pointer to our filename reporting function + FFGET_FILE ffinf; }; static struct UUENCODE_globals glb; @@ -336,17 +337,40 @@ int UUENCODE_is_diskfile_uuencoded( char *fname ) return result; } +FILE * UUENCODE_make_file_obj (char *input_filename) +{ + FILE *inf = fopen(input_filename,"r"); + if (!inf) + { + LOGGER_log("%s:%d:UUENCODE_decode_uu:ERROR: Cannot open file '%s' for reading (%s)", FL, input_filename, strerror(errno)); + uuencode_error = UUENCODE_STATUS_CANNOT_OPEN_FILE; + return NULL; + } + return inf; +} + +FFGET_FILE * UUENCODE_make_sourcestream( FILE *f) +{ + if (f == NULL) + return NULL; + fseek(f, 0, SEEK_SET); + FFGET_setstream(&(glb.ffinf), f); + FFGET_set_watch_SDL( glb.doubleCR_mode ); + + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Creation done. [FFGET-FILE=%p, FILE=%p]\n", FL, &(glb.ffinf), f); + return &(glb.ffinf); +} + /*-----------------------------------------------------------------\ Function Name : UUENCODE_decode_uu Returns Type : int ----Parameter List - 1. FFGET_FILE *f, Source Data Stream - 2. char *input_filename, The fully pathed input filename, containing UU data - 3. char *out_filename, Pointer to a buffer where we will write the filename of the UU data - 4. int decode_whole_file, 0 == only first segment, >0 == all - 5. int keep , Keep the files we create, don't delete - 6. unpack file metadata - 7. related MIME headers + 1. FFGET_FILE *f, Source Data Stream + 2. char *out_filename, Pointer to a buffer where we will write the filename of the UU data + 3. int decode_whole_file, 0 == only first segment, >0 == all + 4. int keep , Keep the files we create, don't delete + 5. unpack file metadata + 6. related MIME headers ------------------ Exit Codes : Returns the number of attachments decoded in the data Side Effects : @@ -357,7 +381,7 @@ int UUENCODE_is_diskfile_uuencoded( char *fname ) Changes: \------------------------------------------------------------------*/ -int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, int decode_whole_file, int keep, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ) +int UUENCODE_decode_uu( FFGET_FILE *f, char *out_filename, int decode_whole_file, int keep, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ) { int filename_found = 0; char buf[ UUENCODE_STRLEN_MAX ]; @@ -370,41 +394,13 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, int loop = 0; int buflen = 0; int filecount = 0; - FFGET_FILE ffinf; // Local static FFGET struct used if *f is NULL - FFGET_FILE *finf; // Points to either *f or &ffinf - FILE *inf = NULL; + int output_filename_supplied = (out_filename != NULL) && (out_filename[0] != '\0'); int start_found = 0; bp = buf; - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Starting.(input=%s,output=%s)\n", FL, input_filename,out_filename ); - - - // If no FFGET_FILE param is passed to us directly, then we must create out own. - if (!f) - { - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: NULL FFGET source stream given to us, create our own.\n",FL); - - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Full INPUT file path set as '%s'\n", FL, input_filename); - - inf = fopen(input_filename,"r"); - if (!inf) - { - LOGGER_log("%s:%d:UUENCODE_decode_uu:ERROR: Cannot open file '%s' for reading (%s)", FL, input_filename, strerror(errno)); - uuencode_error = UUENCODE_STATUS_CANNOT_OPEN_FILE; - return -1; - } - - FFGET_setstream(&ffinf, inf); - FFGET_set_watch_SDL( glb.doubleCR_mode ); - finf = &ffinf; - - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Creation done. [FFGET-FILE=%p, FILE=%p]\n", FL, finf, inf); - } else { - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: File handle already exists to read from, using",FL); - finf = f; - } + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Starting.(input=%s,output=%s)\n", FL, hinfo->filename,out_filename ); writebuffer = malloc( UUENCODE_WRITE_BUFFER_SIZE *sizeof(unsigned char)); if (!writebuffer) @@ -418,14 +414,14 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, wbcount = 0; } - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Beginning.(%s)\n",FL,input_filename); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Beginning.(%s)\n",FL,hinfo->filename); - while (!FFGET_feof(finf)) + while (!FFGET_feof(f)) { filename_found = 0; // First lets locate the BEGIN line of this UUDECODE file { - while (FFGET_fgets(buf, sizeof(buf), finf)) + while (FFGET_fgets(buf, sizeof(buf), f)) { if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: BUFFER: \n%s\n", FL, buf); @@ -458,7 +454,6 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, { LOGGER_log("%s:%d:UUENCODE_decode_uu:WARNING: unable to obtain filename from UUencoded text file header", FL); if (writebuffer) free(writebuffer); - fclose(inf); uuencode_error = UUENCODE_STATUS_CANNOT_FIND_FILENAME; return -1; } @@ -511,11 +506,11 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, while (cur_mime->f) { // for each input line - FFGET_fgets(buf, sizeof(buf), finf); + FFGET_fgets(buf, sizeof(buf), f); if (UUENCODE_DPEDANTIC) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Read line:\n%s",FL,buf); - if (FFGET_feof(finf) != 0) + if (FFGET_feof(f) != 0) { - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:WARNING: Short file (%s)\n",FL, input_filename); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:WARNING: Short file (%s)\n",FL, hinfo->filename); if (writebuffer != NULL) free(writebuffer); uuencode_error = UUENCODE_STATUS_SHORT_FILE; return -1; @@ -616,9 +611,9 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, { if (glb.filename_decoded_report == NULL) { - LOGGER_log("Decoded: %s\n", input_filename); + LOGGER_log("Decoded: %s\n", hinfo->filename); } else { - glb.filename_decoded_report( input_filename, (glb.verbosity_contenttype>0?"uuencoded":NULL) ); + glb.filename_decoded_report( hinfo->filename, (glb.verbosity_contenttype>0?"uuencoded":NULL) ); } } @@ -643,8 +638,6 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Completed\n",FL); - if (inf) fclose(inf); - return filecount; } diff --git a/uuencode.h b/uuencode.h index 3854f86..e81a976 100644 --- a/uuencode.h +++ b/uuencode.h @@ -1,10 +1,9 @@ -#define UUENCODE_STATUS_SHORT_FILE 100 -#define UUENCODE_STATUS_CANNOT_OPEN_FILE 101 -#define UUENCODE_STATUS_CANNOT_ALLOCATE_MEMORY 102 -#define UUENCODE_STATUS_CANNOT_FIND_FILENAME 103 -#define UUENCODE_STATUS_OK 0 -#include "mime.h" +#define UUENCODE_STATUS_SHORT_FILE 100 +#define UUENCODE_STATUS_CANNOT_OPEN_FILE 101 +#define UUENCODE_STATUS_CANNOT_ALLOCATE_MEMORY 102 +#define UUENCODE_STATUS_CANNOT_FIND_FILENAME 103 +#define UUENCODE_STATUS_OK 0 extern int uuencode_error; @@ -22,5 +21,7 @@ int UUENCODE_set_filename_report_fn( int (*ptr_to_fn)(char *, char *) ); int UUENCODE_is_uuencode_header( char *line ); int UUENCODE_is_diskfile_uuencoded( char *fname ); -int UUENCODE_decode_uu( FFGET_FILE *f, char *input_filename, char *out_filename, int decode_whole_file, int keep, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ); +int UUENCODE_decode_uu( FFGET_FILE *f, char *out_filename, int decode_whole_file, int keep, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ); +FILE * UUENCODE_make_file_obj (char *input_filename); +FFGET_FILE * UUENCODE_make_sourcestream( FILE *f); From 2095bb7336ac3aae4a2e4ae2854bc3c452de7a17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Sat, 26 Apr 2025 21:03:55 +0300 Subject: [PATCH 40/80] Refactor UUE debug and cleanup header --- mime_headers.h | 116 ++++++++++++++++++++++++------------------------- uuencode.c | 56 ++++++++++++------------ 2 files changed, 86 insertions(+), 86 deletions(-) diff --git a/mime_headers.h b/mime_headers.h index 904b0b4..8a3837a 100644 --- a/mime_headers.h +++ b/mime_headers.h @@ -5,64 +5,64 @@ #define _CTYPE_RANGE 99 -#define _CTYPE_UNSPECIFIED -1 -#define _CTYPE_MESSAGE_START 1 -#define _CTYPE_MESSAGE 1 -#define _CTYPE_MESSAGE_END 100 - -#define _CTYPE_MULTIPART_START 100 -#define _CTYPE_MULTIPART 100 -#define _CTYPE_MULTIPART_MIXED 101 -#define _CTYPE_MULTIPART_APPLEDOUBLE 102 -#define _CTYPE_MULTIPART_RELATED 103 -#define _CTYPE_MULTIPART_ALTERNATIVE 104 -#define _CTYPE_MULTIPART_REPORT 105 -#define _CTYPE_MULTIPART_SIGNED 106 -#define _CTYPE_MULTIPART_END 199 - -#define _CTYPE_TEXT_START 200 -#define _CTYPE_TEXT 200 -#define _CTYPE_TEXT_PLAIN 201 -#define _CTYPE_TEXT_UNKNOWN 202 -#define _CTYPE_TEXT_HTML 203 -#define _CTYPE_TEXT_CALENDAR 204 -#define _CTYPE_TEXT_END 299 - -#define _CTYPE_IMAGE_START 300 -#define _CTYPE_IMAGE 300 -#define _CTYPE_IMAGE_GIF 301 -#define _CTYPE_IMAGE_JPEG 302 -#define _CTYPE_IMAGE_PNG 303 -#define _CTYPE_IMAGE_END 399 - -#define _CTYPE_AUDIO_START 400 -#define _CTYPE_AUDIO 400 -#define _CTYPE_AUDIO_END 499 - -#define _CTYPE_OCTECT 800 -#define _CTYPE_RFC822 500 -#define _CTYPE_TNEF 600 -#define _CTYPE_APPLICATION 700 -#define _CTYPE_APPLICATION_APPLEFILE 701 -#define _CTYPE_UNKNOWN 0 - -#define _CTRANS_ENCODING_UNSPECIFIED -1 -#define _CTRANS_ENCODING_B64 100 -#define _CTRANS_ENCODING_7BIT 101 -#define _CTRANS_ENCODING_8BIT 102 -#define _CTRANS_ENCODING_QP 103 -#define _CTRANS_ENCODING_RAW 104 -#define _CTRANS_ENCODING_BINARY 105 -#define _CTRANS_ENCODING_UUENCODE 106 -#define _CTRANS_ENCODING_UNKNOWN 0 - -#define _CDISPOSITION_UNSPECIFIED -1 -#define _CDISPOSITION_INLINE 100 -#define _CDISPOSITION_ATTACHMENT 200 -#define _CDISPOSITION_FORMDATA 300 -#define _CDISPOSITION_UNKNOWN 0 - -#define _MIMEH_FOUND_FROM 100 +#define _CTYPE_UNSPECIFIED -1 +#define _CTYPE_MESSAGE_START 1 +#define _CTYPE_MESSAGE 1 +#define _CTYPE_MESSAGE_END 100 + +#define _CTYPE_MULTIPART_START 100 +#define _CTYPE_MULTIPART 100 +#define _CTYPE_MULTIPART_MIXED 101 +#define _CTYPE_MULTIPART_APPLEDOUBLE 102 +#define _CTYPE_MULTIPART_RELATED 103 +#define _CTYPE_MULTIPART_ALTERNATIVE 104 +#define _CTYPE_MULTIPART_REPORT 105 +#define _CTYPE_MULTIPART_SIGNED 106 +#define _CTYPE_MULTIPART_END 199 + +#define _CTYPE_TEXT_START 200 +#define _CTYPE_TEXT 200 +#define _CTYPE_TEXT_PLAIN 201 +#define _CTYPE_TEXT_UNKNOWN 202 +#define _CTYPE_TEXT_HTML 203 +#define _CTYPE_TEXT_CALENDAR 204 +#define _CTYPE_TEXT_END 299 + +#define _CTYPE_IMAGE_START 300 +#define _CTYPE_IMAGE 300 +#define _CTYPE_IMAGE_GIF 301 +#define _CTYPE_IMAGE_JPEG 302 +#define _CTYPE_IMAGE_PNG 303 +#define _CTYPE_IMAGE_END 399 + +#define _CTYPE_AUDIO_START 400 +#define _CTYPE_AUDIO 400 +#define _CTYPE_AUDIO_END 499 + +#define _CTYPE_OCTECT 800 +#define _CTYPE_RFC822 500 +#define _CTYPE_TNEF 600 +#define _CTYPE_APPLICATION 700 +#define _CTYPE_APPLICATION_APPLEFILE 701 +#define _CTYPE_UNKNOWN 0 + +#define _CTRANS_ENCODING_UNSPECIFIED -1 +#define _CTRANS_ENCODING_B64 100 +#define _CTRANS_ENCODING_7BIT 101 +#define _CTRANS_ENCODING_8BIT 102 +#define _CTRANS_ENCODING_QP 103 +#define _CTRANS_ENCODING_RAW 104 +#define _CTRANS_ENCODING_BINARY 105 +#define _CTRANS_ENCODING_UUENCODE 106 +#define _CTRANS_ENCODING_UNKNOWN 0 + +#define _CDISPOSITION_UNSPECIFIED -1 +#define _CDISPOSITION_INLINE 100 +#define _CDISPOSITION_ATTACHMENT 200 +#define _CDISPOSITION_FORMDATA 300 +#define _CDISPOSITION_UNKNOWN 0 + +#define _MIMEH_FOUND_FROM 100 #ifndef PATH_MAX #define PATH_MAX 4096 diff --git a/uuencode.c b/uuencode.c index 5d055f1..38a52f6 100644 --- a/uuencode.c +++ b/uuencode.c @@ -270,7 +270,7 @@ int UUENCODE_is_uuencode_header( char *line ) if (fp) { - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_is_uuencode_header:DEBUG: PERMISSIONS = %s\n", FL, fp); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: PERMISSIONS = %s\n", FL,__func__, fp); if ((atoi(fp) == 0)||(atoi(fp) > 777)) // Maximum is 777, because R+W+X = 7 { @@ -280,7 +280,7 @@ int UUENCODE_is_uuencode_header( char *line ) } else { - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_is_uuencode_header:WARNING: Cannot read permissions for UUENCODED data file (%s)\n", FL, line); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:%s:WARNING: Cannot read permissions for UUENCODED data file (%s)\n", FL,__func__, line); } } @@ -296,7 +296,7 @@ int UUENCODE_is_file_uuencoded( FILE *f ) while ((linecount < limit)&&(fgets(line, sizeof(line), f))) { - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_is_file_uuencoded:DEBUG: Testing line '%s'\n", FL, line); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Testing line '%s'\n", FL,__func__, line); if (UUENCODE_is_uuencode_header( line )) { result = 1; @@ -327,7 +327,7 @@ int UUENCODE_is_diskfile_uuencoded( char *fname ) f = fopen(fname,"r"); if (!f) { - LOGGER_log("%s:%d:UUENCODE_is_diskfile_uuencoded:ERROR: cannot open file '%s' for reading (%s)", FL, fname,strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: cannot open file '%s' for reading (%s)", FL,__func__, fname,strerror(errno)); uuencode_error = UUENCODE_STATUS_CANNOT_OPEN_FILE; return -1; } @@ -342,7 +342,7 @@ FILE * UUENCODE_make_file_obj (char *input_filename) FILE *inf = fopen(input_filename,"r"); if (!inf) { - LOGGER_log("%s:%d:UUENCODE_decode_uu:ERROR: Cannot open file '%s' for reading (%s)", FL, input_filename, strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: Cannot open file '%s' for reading (%s)", FL,__func__, input_filename, strerror(errno)); uuencode_error = UUENCODE_STATUS_CANNOT_OPEN_FILE; return NULL; } @@ -357,7 +357,7 @@ FFGET_FILE * UUENCODE_make_sourcestream( FILE *f) FFGET_setstream(&(glb.ffinf), f); FFGET_set_watch_SDL( glb.doubleCR_mode ); - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Creation done. [FFGET-FILE=%p, FILE=%p]\n", FL, &(glb.ffinf), f); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Creation done. [FFGET-FILE=%p, FILE=%p]\n", FL,__func__, &(glb.ffinf), f); return &(glb.ffinf); } @@ -400,12 +400,12 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *out_filename, int decode_whole_file bp = buf; - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Starting.(input=%s,output=%s)\n", FL, hinfo->filename,out_filename ); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Starting.(input=%s,output=%s)\n", FL,__func__, hinfo->filename,out_filename ); writebuffer = malloc( UUENCODE_WRITE_BUFFER_SIZE *sizeof(unsigned char)); if (!writebuffer) { - LOGGER_log("%s:%d:UUENCODE_decode_uu:ERROR: cannot allocate 100K of memory for the write buffer",FL); + LOGGER_log("%s:%d:%s:ERROR: cannot allocate 100K of memory for the write buffer",FL,__func__); uuencode_error = UUENCODE_STATUS_CANNOT_ALLOCATE_MEMORY; return -1; } @@ -414,7 +414,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *out_filename, int decode_whole_file wbcount = 0; } - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Beginning.(%s)\n",FL,hinfo->filename); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Beginning.(%s)\n",FL,__func__,hinfo->filename); while (!FFGET_feof(f)) { @@ -423,26 +423,26 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *out_filename, int decode_whole_file { while (FFGET_fgets(buf, sizeof(buf), f)) { - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: BUFFER: \n%s\n", FL, buf); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: BUFFER: \n%s\n", FL,__func__, buf); // Check for the presence of 'BEGIN', but make sure it's not followed by a // colon ( which indicates a VCARD instead of UUENCODE if ((strncasecmp(buf,"begin",5)==0)&&(buf[5] !=':')&&(isspace((int)buf[5]))) { - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Located BEGIN\n",FL); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Located BEGIN\n",FL,__func__); // Okay, so the line contains begin at the start, now, lets get the decode details fp = fn = NULL; bp = PLD_strtok(&tx, buf, " \n\r\t"); // Get the begin - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: BEGIN = '%s'\n", FL, bp); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: BEGIN = '%s'\n", FL,__func__, bp); if (bp) fp = PLD_strtok(&tx, NULL, " \n\r\t"); // Get the file-permissions - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Permissions/Name = '%s'\n", FL, fp); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Permissions/Name = '%s'\n", FL,__func__, fp); if (fp) fn = PLD_strtok(&tx, NULL, "\n\r"); // Get the file-name - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Name = '%s'\n", FL, fn); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Name = '%s'\n", FL,__func__, fn); if (!fn) { @@ -452,13 +452,13 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *out_filename, int decode_whole_file if ((!bp)&&(!f)) { - LOGGER_log("%s:%d:UUENCODE_decode_uu:WARNING: unable to obtain filename from UUencoded text file header", FL); + LOGGER_log("%s:%d:%s:WARNING: unable to obtain filename from UUencoded text file header", FL,__func__); if (writebuffer) free(writebuffer); uuencode_error = UUENCODE_STATUS_CANNOT_FIND_FILENAME; return -1; } - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Full path = (%s)\n",FL,bp); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Full path = (%s)\n",FL,__func__,bp); filename_found = 1; break; @@ -473,7 +473,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *out_filename, int decode_whole_file { filename_found = 1; bp = out_filename; - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Output filename set to '%s'",FL, bp); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Output filename set to '%s'",FL,__func__, bp); } // If we have a filename, and we have our bp as NON-null, then we shall commence @@ -483,7 +483,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *out_filename, int decode_whole_file { MIME_element* cur_mime = NULL; - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Located filename (%s), now decoding.\n", FL, bp); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Located filename (%s), now decoding.\n", FL,__func__, bp); // Clean up the file name FNFILTER_filter( bp, 255 ); /* the longest for most of filesystems */ @@ -492,7 +492,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *out_filename, int decode_whole_file if (output_filename_supplied == 0) out_filename = strdup(bp); - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Filename = (%s)\n", FL, fn); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Filename = (%s)\n", FL,__func__, fn); cur_mime = MIME_element_add (NULL, unpack_metadata, fn, hinfo->content_type_string, hinfo->content_transfer_encoding_string, hinfo->name, hinfo->current_recursion_level, 1, filecount); @@ -507,10 +507,10 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *out_filename, int decode_whole_file { // for each input line FFGET_fgets(buf, sizeof(buf), f); - if (UUENCODE_DPEDANTIC) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Read line:\n%s",FL,buf); + if (UUENCODE_DPEDANTIC) LOGGER_log("%s:%d:%s:DEBUG: Read line:\n%s",FL,__func__,buf); if (FFGET_feof(f) != 0) { - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:WARNING: Short file (%s)\n",FL, hinfo->filename); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:%s:WARNING: Short file (%s)\n",FL,__func__, hinfo->filename); if (writebuffer != NULL) free(writebuffer); uuencode_error = UUENCODE_STATUS_SHORT_FILE; return -1; @@ -520,13 +520,13 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *out_filename, int decode_whole_file if (strncasecmp(buf,"end",3)==0) { - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: End of UUencoding detected\n",FL); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: End of UUencoding detected\n",FL,__func__); break; } if ( !strpbrk(buf,"\r\n") ) { - LOGGER_log("%s:%d:UUENCODE_decode_uu:WARNING: Excessive length line\n",FL); + LOGGER_log("%s:%d:%s:WARNING: Excessive length line\n",FL,__func__); } // The first char of the line indicates how many bytes are to be expected @@ -569,7 +569,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *out_filename, int decode_whole_file size_t bc; bc = fwrite(writebuffer, 1, wbcount, cur_mime->f); if (bc != wbcount) { - LOGGER_log("%s:%d:ERROR: Attempted to write %ld bytes, only wrote %ld\n", FL, wbcount, bc); + LOGGER_log("%s:%d:ERROR: Attempted to write %ld bytes, only wrote %ld\n", FL,__func__, wbcount, bc); } wbpos = writebuffer; wbcount = 0; @@ -599,7 +599,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *out_filename, int decode_whole_file size_t bc = fwrite(writebuffer, 1, wbcount, cur_mime->f); if (bc != wbcount) { - LOGGER_log("%s:%d:ERROR: Attempted to write %ld bytes, only wrote %ld\n", FL, wbcount, bc); + LOGGER_log("%s:%d:ERROR: Attempted to write %ld bytes, only wrote %ld\n", FL,__func__, wbcount, bc); } } @@ -623,10 +623,10 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *out_filename, int decode_whole_file else { out_filename[0] = '\0'; - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: No FILENAME was found in data...\n",FL); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: No FILENAME was found in data...\n",FL,__func__); } - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Segment completed\n",FL); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Segment completed\n",FL,__func__); // If this file was a result of the x-uuencode content encoding, then we need to exit out // as we're reading in the -stream-, and we dont want to carry on reading because we'll @@ -636,7 +636,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *out_filename, int decode_whole_file if (writebuffer) free(writebuffer); - if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:UUENCODE_decode_uu:DEBUG: Completed\n",FL); + if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Completed\n",FL,__func__); return filecount; } From 0581d65e62a07a177087d873afd0bc50b81b1b6f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Sat, 26 Apr 2025 21:39:29 +0300 Subject: [PATCH 41/80] Refactor `mime_headers` to `MIME_element` --- mime.c | 2 +- mime_element.c | 21 +++++++++++++++++++++ mime_headers.c | 46 +++++++++++++++++----------------------------- mime_headers.h | 8 ++++---- 4 files changed, 43 insertions(+), 34 deletions(-) diff --git a/mime.c b/mime.c index 8ca1347..63d00bb 100644 --- a/mime.c +++ b/mime.c @@ -49,8 +49,8 @@ #include "boundary-stack.h" #include "ffget.h" #include "strstack.h" -#include "mime_headers.h" #include "mime_element.h" +#include "mime_headers.h" #include "mime.h" #include "tnef/tnef_api.h" #include "ripOLE/ole.h" diff --git a/mime_element.c b/mime_element.c index 807a770..38ecbea 100644 --- a/mime_element.c +++ b/mime_element.c @@ -90,6 +90,27 @@ void MIME_element_remove (MIME_element* cur) free(cur); } +/* + + FILE *fo; + struct stat st; + + // Determine a file name we can use. + do { + + } + while (stat(glb.doubleCRname, &st) == 0); + + + fo = fopen(glb.doubleCRname,"w"); + if (!fo) + { + LOGGER_log("%s:%d:MIMEH_save_doubleCR:ERROR: unable to open '%s' to write (%s)", FL,glb.doubleCRname,strerror(errno)); + return -1; + } +*/ + + //------Dynamic array function definitions------ // Array initialization void arrayInit(dynamic_array** arr_ptr) diff --git a/mime_headers.c b/mime_headers.c index 0b4688c..7de5702 100644 --- a/mime_headers.c +++ b/mime_headers.c @@ -35,6 +35,7 @@ #include "strstack.h" #include "boundary-stack.h" #include "filename-filters.h" +#include "mime_element.h" #include "mime_headers.h" #ifndef FL @@ -727,43 +728,30 @@ int MIMEH_are_headers_RFC822( char *headers ) Changes: \------------------------------------------------------------------*/ -int MIMEH_save_doubleCR( FFGET_FILE *f, char * output_dir ) +int MIMEH_save_doubleCR( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ) { - //char c; int c; - FILE *fo; - struct stat st; + MIME_element* cur_mime = NULL; + + glb.doubleCR_count++; + snprintf(glb.doubleCRname,_MIMEH_STRLEN_MAX,"%s/%s_doubleCR.%d_", unpack_metadata->dir, hinfo->filename, glb.doubleCR_count); - // Determine a file name we can use. - do { - glb.doubleCR_count++; - snprintf(glb.doubleCRname,_MIMEH_STRLEN_MAX,"%s/doubleCR.%d",output_dir,glb.doubleCR_count); - } - while (stat(glb.doubleCRname, &st) == 0); - - - fo = fopen(glb.doubleCRname,"w"); - if (!fo) - { - LOGGER_log("%s:%d:MIMEH_save_doubleCR:ERROR: unable to open '%s' to write (%s)", FL,glb.doubleCRname,strerror(errno)); - return -1; - } + cur_mime = MIME_element_add (NULL, unpack_metadata, glb.doubleCRname, "doubleCR", NULL, "doubleCR", hinfo->current_recursion_level + 1, 0, 0); if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIME_save_doubleCR:DEBUG: Saving DoubleCR header: %s\n", FL,glb.doubleCRname); while (1) { - c = FFGET_fgetc(f); + c = FFGET_fgetc(cur_mime->f); fprintf(fo,"%c",c); if ((c == EOF)||(c == '\n')) { break; } } - fclose(fo); + MIME_element_remove(cur_mime); return 0; } - /*-----------------------------------------------------------------\ Function Name : * Returns Type : char @@ -1050,7 +1038,7 @@ Purpose: Reads from the stream F until it detects a From line, or a blank Output: Errors: ------------------------------------------------------------------------*/ -int MIMEH_read_headers( struct MIMEH_header_info *hinfo, FFGET_FILE *f, char* output_dir ) +int MIMEH_read_headers( struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata ) { char buffer[_MIMEH_STRLEN_MAX+1]; int totalsize=0; @@ -1225,7 +1213,7 @@ int MIMEH_read_headers( struct MIMEH_header_info *hinfo, FFGET_FILE *f, char* ou { if (glb.doubleCR_save != 0) { - MIMEH_save_doubleCR(f, output_dir); + MIMEH_save_doubleCR(f, unpack_metadata, hinfo); glb.doubleCR = 1; } FFGET_doubleCR = 0; @@ -2889,7 +2877,7 @@ int MIMEH_headers_clearcount( struct MIMEH_header_info *hinfo ) { Changes: \------------------------------------------------------------------*/ -int MIMEH_headers_get( struct MIMEH_header_info *hinfo, FFGET_FILE *f, char* output_dir ) +int MIMEH_headers_get( struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata ) { int result = 0; @@ -2930,7 +2918,7 @@ int MIMEH_headers_get( struct MIMEH_header_info *hinfo, FFGET_FILE *f, char* out // Read from the file, the headers we need FFGET_set_watch_SDL(1); - result = MIMEH_read_headers(hinfo, f, output_dir); + result = MIMEH_read_headers(hinfo, f, unpack_metadata); FFGET_set_watch_SDL(0); if (hinfo->lf_count > hinfo->crlf_count) { @@ -3006,7 +2994,7 @@ int MIMEH_headers_cleanup( void ) Changes: \------------------------------------------------------------------*/ -int MIMEH_parse_headers( FFGET_FILE *f, struct MIMEH_header_info *hinfo, char *output_dir ) +int MIMEH_parse_headers( FFGET_FILE *f, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata ) { int result = 0; DMIMEH LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: Start [F=%p, hinfo=%p]\n", FL, f, hinfo); @@ -3016,7 +3004,7 @@ int MIMEH_parse_headers( FFGET_FILE *f, struct MIMEH_header_info *hinfo, char *o /** Proceed to read, process and finish headers **/ DMIMEH LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: Getting headers",FL); - if ( result == 0 ) result = MIMEH_headers_get( hinfo, f, output_dir ); + if ( result == 0 ) result = MIMEH_headers_get( hinfo, f, unpack_metadata ); DMIMEH LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: Processing headers",FL); if ( result == 0 ) result = MIMEH_headers_process( hinfo, glb.headerline ); DMIMEH LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: cleanup of headers",FL); @@ -3108,7 +3096,7 @@ int MIMEH_get_defect_count( struct MIMEH_header_info *hinfo ) Changes: \------------------------------------------------------------------*/ -int MIMEH_read_primary_headers( char *fname, struct MIMEH_header_info *hinfo, char* output_dir ) +int MIMEH_read_primary_headers( char *fname, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata ) { FFGET_FILE F; FILE *f; @@ -3120,7 +3108,7 @@ int MIMEH_read_primary_headers( char *fname, struct MIMEH_header_info *hinfo, ch } FFGET_setstream(&F,f); - MIMEH_parse_headers(&F, hinfo, output_dir); + MIMEH_parse_headers(&F, hinfo, unpack_metadata); fclose(f); return 0; diff --git a/mime_headers.h b/mime_headers.h index 8a3837a..4db19a1 100644 --- a/mime_headers.h +++ b/mime_headers.h @@ -195,12 +195,12 @@ char *MIMEH_get_doubleCR_name( void ); int MIMEH_set_header_longsearch( int level ); int MIMEH_headers_clearcount( struct MIMEH_header_info *hinfo ); -int MIMEH_read_headers( struct MIMEH_header_info *hinfo, FFGET_FILE *f, char* output_dir ); +int MIMEH_read_headers( struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata ); -int MIMEH_headers_get( struct MIMEH_header_info *hinfo, FFGET_FILE *f, char* output_dir); +int MIMEH_headers_get( struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata); int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ); int MIMEH_headers_cleanup(); -int MIMEH_parse_headers( FFGET_FILE *f, struct MIMEH_header_info *hinfo, char* output_dir ); +int MIMEH_parse_headers( FFGET_FILE *f, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata ); int MIMEH_display_info( struct MIMEH_header_info *hinfo ); @@ -213,6 +213,6 @@ int MIMEH_get_defect_count( struct MIMEH_header_info *hinfo ); int MIMEH_set_report_MIME( int level ); -int MIMEH_read_primary_headers( char *fname, struct MIMEH_header_info *hinfo, char* output_dir ); +int MIMEH_read_primary_headers( char *fname, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata ); #endif From a8ce66b3efa1100b1126d62d82854cbc038cc63f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Sat, 26 Apr 2025 21:48:02 +0300 Subject: [PATCH 42/80] Fix compilation and cleanup `mime_headers` --- mime_headers.c | 31 +++---------------------------- uuencode.c | 2 +- 2 files changed, 4 insertions(+), 29 deletions(-) diff --git a/mime_headers.c b/mime_headers.c index 7de5702..c321d9f 100644 --- a/mime_headers.c +++ b/mime_headers.c @@ -741,8 +741,8 @@ int MIMEH_save_doubleCR( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIME_save_doubleCR:DEBUG: Saving DoubleCR header: %s\n", FL,glb.doubleCRname); while (1) { - c = FFGET_fgetc(cur_mime->f); - fprintf(fo,"%c",c); + c = FFGET_fgetc(f); + fprintf(cur_mime->f,"%c",c); if ((c == EOF)||(c == '\n')) { break; @@ -1262,8 +1262,6 @@ int MIMEH_read_headers( struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_ return result; } - - /*------------------------------------------------------------------------ Procedure: MIMEH_display_info ID:1 Purpose: DEBUGGING - Displays the values of the hinfo structure to @@ -1294,7 +1292,6 @@ int MIMEH_display_info( struct MIMEH_header_info *hinfo ) fflush(stdout); } return 0; - } /*-----------------------------------------------------------------\ @@ -1343,10 +1340,7 @@ int MIMEH_decode_multivalue_language_string( char *input ) } DMIMEH LOGGER_log("%s:%d:MIMEH_decode_multivalue_language_string:DEBUG: Output = '%s'",FL,q); - - return 0; - } /*-----------------------------------------------------------------\ @@ -1398,7 +1392,7 @@ int MIMEH_recompose_multivalue( struct MIMEH_header_info *hinfo, char *header_na { char *q; char *buffer_start; - int is_quoted = 0; + int is_quoted = 0; // Setup our buffer insertion point for what ever new data we extract buffer_start = buffer +strlen(buffer); @@ -1498,7 +1492,6 @@ int MIMEH_recompose_multivalue( struct MIMEH_header_info *hinfo, char *header_na return result; } - /*-----------------------------------------------------------------\ Function Name : MIMEH_parse_header_parameter Returns Type : int @@ -1792,15 +1785,8 @@ int MIMEH_parse_header_parameter( struct MIMEH_header_info *hinfo, char *data, if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_header_parameter:DEBUG: [return=%d] Done seeking for '%s' data_end_point=%p (from %p)",FL, return_value, searchstr, *data_end_point, data); return return_value; - } - - - - - - /*-----------------------------------------------------------------\ Function Name : MIMEH_is_valid_header_prefix Returns Type : int @@ -1852,8 +1838,6 @@ int MIMEH_is_valid_header_prefix( char *data, char *prefix_name ) return 1; } - - /*-----------------------------------------------------------------\ Function Name : MIMEH_parse_contenttype_linear Returns Type : int @@ -1896,10 +1880,7 @@ int MIMEH_parse_contenttype_linear_EXPERIMENT( char *header_name, char *header_v // if (strncasecmp(chv, "boundary" } - - return 0; - } /*-----------------------------------------------------------------\ @@ -1921,7 +1902,6 @@ int MIMEH_parse_contenttype_linear_EXPERIMENT( char *header_name, char *header_v \------------------------------------------------------------------*/ int MIMEH_parse_contenttype( char *header_name, char *header_value, struct MIMEH_header_info *hinfo ) { - int return_value; char *p, *q; char *hv = strdup( header_value ); @@ -2180,7 +2160,6 @@ int MIMEH_parse_contentlocation( char *header_name, char *header_value, struct M snprintf(hinfo->filename, sizeof(hinfo->filename),"%s",p); FNFILTER_filter(hinfo->filename, _MIMEH_FILENAMELEN_MAX); SS_push(&(hinfo->ss_filenames), hinfo->filename, strlen(hinfo->filename)); - } } return 0; @@ -2858,7 +2837,6 @@ int MIMEH_headers_clearcount( struct MIMEH_header_info *hinfo ) { hinfo->lf_count = 0; hinfo->crcr_count = 0; return 0; - } /*-----------------------------------------------------------------\ @@ -2976,8 +2954,6 @@ int MIMEH_headers_cleanup( void ) return 0; } - - /*-----------------------------------------------------------------\ Function Name : MIMEH_parse_headers Returns Type : int @@ -3074,7 +3050,6 @@ int MIMEH_dump_defects( struct MIMEH_header_info *hinfo ) \------------------------------------------------------------------*/ int MIMEH_get_defect_count( struct MIMEH_header_info *hinfo ) { - return hinfo->header_defect_count; } diff --git a/uuencode.c b/uuencode.c index 38a52f6..9df57de 100644 --- a/uuencode.c +++ b/uuencode.c @@ -20,8 +20,8 @@ The biggest issue is that the interfaces to the decoding functions are too speci #include "ffget.h" #include "filename-filters.h" #include "strstack.h" -#include "mime_headers.h" #include "mime_element.h" +#include "mime_headers.h" #include "uuencode.h" From d31b2407026aa5c480169e3896b3b781311c8d35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Sat, 26 Apr 2025 22:03:27 +0300 Subject: [PATCH 43/80] Remove stat header, add stream copy draft --- mime_element.c | 37 +++++++++++++++++++++++++++++++++++++ mime_headers.c | 1 - 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/mime_element.c b/mime_element.c index 38ecbea..7f3783c 100644 --- a/mime_element.c +++ b/mime_element.c @@ -108,6 +108,43 @@ void MIME_element_remove (MIME_element* cur) LOGGER_log("%s:%d:MIMEH_save_doubleCR:ERROR: unable to open '%s' to write (%s)", FL,glb.doubleCRname,strerror(errno)); return -1; } + + FILE *fptr1, *fptr2; + char filename[100]; + int c; + + printf("Enter the filename to open for reading: "); + scanf("%s", filename); + + // Open one file for reading + fptr1 = fopen(filename, "r"); + if (fptr1 == NULL) + { + printf("Cannot open file %s\n", filename); + exit(1); + } + + printf("Enter the filename to open for writing: "); + scanf("%s", filename); + + // Open another file for writing + fptr2 = fopen(filename, "w"); + if (fptr2 == NULL) + { + printf("Cannot open file %s\n", filename); + exit(1); + } + + // Read contents from file + while ((c = fgetc(fptr1)) != EOF) + { + fputc(c, fptr2); + } + + printf("Contents copied to %s\n", filename); + + fclose(fptr1); + fclose(fptr2); */ diff --git a/mime_headers.c b/mime_headers.c index c321d9f..bbef045 100644 --- a/mime_headers.c +++ b/mime_headers.c @@ -24,7 +24,6 @@ #include #include #include -#include #include #include From d6008204e3d561793ffe9da313f08ca61bcea64a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Sat, 26 Apr 2025 22:05:56 +0300 Subject: [PATCH 44/80] Simple code cleanup --- mime.c | 8 ++++---- mime_headers.h | 1 - 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/mime.c b/mime.c index 63d00bb..97baf65 100644 --- a/mime.c +++ b/mime.c @@ -103,15 +103,15 @@ int MIME_handle_rfc822( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M #define DMIME if (glb.debug >= _MIME_DEBUG_NORMAL) /* our base 64 decoder table */ -static unsigned char b64[256]={ +static unsigned char b64[256] = { 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 62, 128, 128, 128, 63,\ - 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 128, 128, 128, 0, 128, 128,\ + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 128, 128, 128, 0, 128, 128,\ 128, 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, 128, 128, 128, 128, 128,\ + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 128, 128, 128, 128, 128,\ 128, 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, 128, 128, 128, 128, 128,\ + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 128, 128, 128, 128, 128,\ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\ 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128,\ diff --git a/mime_headers.h b/mime_headers.h index 4db19a1..da729a1 100644 --- a/mime_headers.h +++ b/mime_headers.h @@ -162,7 +162,6 @@ struct MIMEH_email_info { }; #endif - int MIMEH_version(void); void MIMEH_init( void ); From bbbdfc520d13ee1281955a85c1b2bd59b1ce598f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Sat, 26 Apr 2025 22:52:55 +0300 Subject: [PATCH 45/80] Collapse UUE decoding to C `FILE` instead of filesystem operations --- mime.c | 28 +++++++++++----------------- uuencode.c | 2 +- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/mime.c b/mime.c index 97baf65..0494223 100644 --- a/mime.c +++ b/mime.c @@ -75,12 +75,12 @@ int MIME_handle_rfc822( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M #define FL __FILE__,__LINE__ #endif -#define _ENC_UNKNOWN 0 -#define _ENC_BASE64 1 -#define _ENC_PLAINTEXT 2 -#define _ENC_QUOTED 3 -#define _ENC_EMBEDDED 4 -#define _ENC_NOFILE -1 +#define _ENC_UNKNOWN 0 +#define _ENC_BASE64 1 +#define _ENC_PLAINTEXT 2 +#define _ENC_QUOTED 3 +#define _ENC_EMBEDDED 4 +#define _ENC_NOFILE -1 #define _MIME_CHARS_PER_LINE 32 #define _MIME_MAX_CHARS_PER_LINE 76 #define _RECURSION_LEVEL_DEFAULT 20 @@ -1249,10 +1249,7 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME if ((!file_has_uuencode)&&(UUENCODE_is_uuencode_header( buffer ))) { - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: UUENCODED is YES (buffer=[%p]\n",FL,__func__,buffer); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: File contains UUENCODED data(%s)\n",FL,__func__,buffer); - file_has_uuencode = 1; } @@ -1268,22 +1265,17 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Completed reading RAW data\n",FL,__func__); free(buffer); - MIME_element_remove(cur_mime); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Closed file and free'd buffer\n",FL,__func__); + // If there was UUEncoded portions [potentially] in the email, the // try to extract them using the MIME_decode_uu() if (file_has_uuencode) { - char full_decode_path[512]; - FILE *fuue = NULL; FFGET_FILE * ffg = NULL; - snprintf(full_decode_path,sizeof(full_decode_path),"%s/%s",unpack_metadata->dir,hinfo->filename); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UUencoded data\n",FL,__func__); if ( hinfo->content_transfer_encoding == _CTRANS_ENCODING_UUENCODE ) decode_entire_file = 0; - - fuue = UUENCODE_make_file_obj (full_decode_path); - ffg = UUENCODE_make_sourcestream(fuue); + + ffg = UUENCODE_make_sourcestream(cur_mime->f); /* fseek to begin is here */ result = UUENCODE_decode_uu(ffg , hinfo->uudec_name, decode_entire_file, keep, unpack_metadata, hinfo ); if (result == -1) { @@ -1318,6 +1310,8 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME else LOGGER_log("%s:%d:%s:WARNING: hinfo has been clobbered.\n",FL,__func__); } } + MIME_element_remove(cur_mime); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Closed file and free'd buffer\n",FL,__func__); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: End[result = %d]\n",FL,__func__,result); return result; diff --git a/uuencode.c b/uuencode.c index 9df57de..126091a 100644 --- a/uuencode.c +++ b/uuencode.c @@ -42,7 +42,7 @@ The biggest issue is that the interfaces to the decoding functions are too speci #define UUENCODE_WRITE_BUFFER_SIZE 4096 #define UUENCODE_WRITE_BUFFER_LIMIT 4000 -static unsigned char uudec[256]={ +static unsigned char uudec[256] = { 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,\ 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,\ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,\ From f145a512d18e5f81d0cbf727345f4c7546e40834 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Sat, 26 Apr 2025 23:01:19 +0300 Subject: [PATCH 46/80] Reafctor TNEF to C `FILE` --- tnef/tnef.c | 57 ++++++++++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/tnef/tnef.c b/tnef/tnef.c index e94a377..9c23808 100644 --- a/tnef/tnef.c +++ b/tnef/tnef.c @@ -669,30 +669,11 @@ int TNEF_decode_tnef(uint8 *tnef_stream, int size, char* file_dir) return 0; } -/*------------------------------------------------------------------------ -Procedure: TNEF_main ID:1 -Purpose: Decodes a given TNEF encoded file -Input: -Output: -Errors: -------------------------------------------------------------------------*/ -int TNEF_main( char *filename, char *file_dir ) +int TNEF_file_processing( FILE *fp, char *file_dir ) { - FILE *fp; uint8 *tnef_stream; int size, nread; - if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Start, decoding %s\n",FL,__func__, filename); - - // Attempt to open up the TNEF encoded file... if it fails - // then report the failed condition to syslog - if ((fp = fopen(filename,"r")) == NULL) - { - LOGGER_log("%s:%d:%s:ERROR: opening file %s for reading (%s)\n", FL,__func__, filename,strerror(errno)); - if (TNEF_glb.tnef_home) free(TNEF_glb.tnef_home); - return -1; - } - // Get the filesize fseek(fp, 0L, SEEK_END); size = ftell(fp); @@ -713,7 +694,6 @@ int TNEF_main( char *filename, char *file_dir ) return -1; } - // Attempt to read in the entire file nread = fread(tnef_stream, sizeof(uint8), size, fp); @@ -722,14 +702,11 @@ int TNEF_main( char *filename, char *file_dir ) // If we did not read in all the bytes, then let syslogs know! if (nread < size) { - LOGGER_log("%s:%d:%s:ERROR: while reading stream from file %s (%s)\n", FL,__func__, filename,strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: while reading stream from TNEF file (%s)\n", FL,__func__, strerror(errno)); if (TNEF_glb.tnef_home) free(TNEF_glb.tnef_home); return -1; } - // Close the file - fclose(fp); - // Proceed to decode the file TNEF_decode_tnef(tnef_stream,size, file_dir); @@ -740,4 +717,34 @@ int TNEF_main( char *filename, char *file_dir ) return 0; } +/*------------------------------------------------------------------------ +Procedure: TNEF_main ID:1 +Purpose: Decodes a given TNEF encoded file +Input: +Output: +Errors: +------------------------------------------------------------------------*/ +int TNEF_main( char *filename, char *file_dir ) +{ + FILE *fp; + uint8 *tnef_stream; + int size, nread; + + if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Start, decoding %s\n",FL,__func__, filename); + + // Attempt to open up the TNEF encoded file... if it fails + // then report the failed condition to syslog + if ((fp = fopen(filename,"r")) == NULL) + { + LOGGER_log("%s:%d:%s:ERROR: opening file %s for reading (%s)\n", FL,__func__, filename,strerror(errno)); + if (TNEF_glb.tnef_home) free(TNEF_glb.tnef_home); + return -1; + } + TNEF_file_processing(fp, file_dir); + + // Close the file + fclose(fp); + return 0; +} + //--------------------------END. From 85b9521fd91727e55ae7606c6b72e8c61baf885b Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Mon, 28 Apr 2025 13:40:25 +0300 Subject: [PATCH 47/80] Move `MIME_test_uniquename` to `MIME_element` --- mime.c | 148 ++++------------------------------ mime.h | 8 -- mime_element.c | 215 ++++++++++++++++++++++++++++++++++++++----------- mime_element.h | 13 ++- 4 files changed, 196 insertions(+), 188 deletions(-) diff --git a/mime.c b/mime.c index 0494223..bf56f03 100644 --- a/mime.c +++ b/mime.c @@ -34,7 +34,6 @@ #include #include #include -#include #include #include #include @@ -161,7 +160,7 @@ struct MIME_globals { int blankzone_saved; int blankzone_save_option; - char blankzone_filename[_MIMEH_STRLEN_MAX +1]; + char blankzone_filename[_MIMEH_STRLEN_MAX + 1]; int (*filename_decoded_reporter)(char *, char *); // Pointer to the reporting function for filenames as they are decoded @@ -250,6 +249,7 @@ int MIME_set_debug( int level ) MDECODE_set_debug(level); UUENCODE_set_debug(level); FNFILTER_set_debug(level); + MIMEELEMENT_set_debug(level); return glb.debug; } @@ -895,120 +895,6 @@ int MIME_get_attachment_count( void ) return glb.attachment_count; } -int get_random_value(void) { - int randval; - FILE *fp; - fp = fopen("/dev/urandom", "r"); - fread(&randval, sizeof(randval), 1, fp); - fclose(fp); - if (randval < 0) { randval = randval *( -1); }; - return randval; -} - -/*------------------------------------------------------------------------ -Procedure: MIME_test_uniquename ID:1 -Purpose: Checks to see that the filename specified is unique. If it's not -unique, it will modify the filename -Input: char *path: Path in which to look for similar filenames -char *fname: Current filename -int method: Method of altering the filename (infix, postfix, prefix, randinfix, randpostfix, randprefix) -Output: -Errors: -------------------------------------------------------------------------*/ -int MIME_test_uniquename( char *path, char *fname, int method ) -{ - struct stat buf; - - char newname[_MIME_STRLEN_MAX +1]; - char scr[_MIME_STRLEN_MAX +1]; /** Scratch var **/ - char *frontname, *extention; - - int cleared = 0; - int count = 1; - - int randval = get_random_value(); - - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Start (%s)",FL,__func__,fname); - - frontname = extention = NULL; // shuts the compiler up - - if (method == _MIME_RENAME_METHOD_INFIX) - { - PLD_strncpy(scr,fname, _MIMEH_STRLEN_MAX); - frontname = scr; - extention = strrchr(scr,'.'); - - if (extention) - { - *extention = '\0'; - extention++; - } - else - { - method = _MIME_RENAME_METHOD_POSTFIX; - } - } - - if (method == _MIME_RENAME_METHOD_RANDINFIX) - { - PLD_strncpy(scr,fname, _MIMEH_STRLEN_MAX); - frontname = scr; - extention = strrchr(scr,'.'); - - if (extention) - { - *extention = '\0'; - extention++; - } - else - { - method = _MIME_RENAME_METHOD_RANDPOSTFIX; - } - } - - snprintf(newname,_MIME_STRLEN_MAX,"%s/%s",path,fname); - while (!cleared) - { - if ((stat(newname, &buf) == -1)) - { - cleared++; - } - else - { - switch (method) { - case _MIME_RENAME_METHOD_PREFIX: - snprintf(newname,_MIME_STRLEN_MAX,"%s/%d_%s",path,count,fname); - break; - case _MIME_RENAME_METHOD_INFIX: - snprintf(newname,_MIME_STRLEN_MAX,"%s/%s_%d.%s",path,frontname,count,extention); - break; - case _MIME_RENAME_METHOD_POSTFIX: - snprintf(newname,_MIME_STRLEN_MAX,"%s/%s_%d",path,fname,count); - break; - case _MIME_RENAME_METHOD_RANDPREFIX: - snprintf(newname,_MIME_STRLEN_MAX,"%s/%d_%d_%s",path,count,randval,fname); - break; - case _MIME_RENAME_METHOD_RANDINFIX: - snprintf(newname,_MIME_STRLEN_MAX,"%s/%s_%d_%d.%s",path,frontname,count,randval,extention); - break; - case _MIME_RENAME_METHOD_RANDPOSTFIX: - snprintf(newname,_MIME_STRLEN_MAX,"%s/%s_%d_%d",path,fname,count,randval); - } - count++; - } - } - if (count > 1) - { - frontname = strrchr(newname,'/'); - if (frontname) frontname++; - else frontname = newname; - - PLD_strncpy(fname, frontname, _MIMEH_FILENAMELEN_MAX); //FIXME - this assumes that the buffer space is at least MIME_STRLEN_MAX sized. - } - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done (%s)",FL,__func__,fname); - return 0; -} - /*------------------------------------------------------------------------ Procedure: MIME_is_RFC Purpose: Determines if the file handed to it is a MIME type email file. @@ -1226,7 +1112,7 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME { int result = 0; int bufsize=1024; - char *buffer = malloc((bufsize +1)*sizeof(char)); + char *buffer = malloc((bufsize + 1)*sizeof(char)); size_t readcount; int file_has_uuencode = 0; int decode_entire_file = 0; @@ -1603,11 +1489,11 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH char scratch[1024]; FFGET_fgets(scratch,sizeof(scratch), f); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Scratch = '%s'", FL,__func__, scratch); - hit = BS_cmp(scratch,strlen(scratch) +1); + hit = BS_cmp(scratch,strlen(scratch) + 1); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Boundary hit = %d", FL,__func__, hit); } else { *p = '\0'; - hit = BS_cmp((f->startpoint -1),strlen(f->startpoint) +1); + hit = BS_cmp((f->startpoint -1),strlen(f->startpoint) + 1); *p = '\n'; } if (hit > 0) { @@ -1895,7 +1781,7 @@ size_t MIME_read_raw( char *src_mpname, char *dest_mpname, size_t rw_buffer_size } } - rw_buffer = malloc( (rw_buffer_size +1) *sizeof(char) ); + rw_buffer = malloc( (rw_buffer_size + 1) *sizeof(char) ); if ( !rw_buffer ) { LOGGER_log("%s:%d:%s:ERROR: could not allocate %d of memory for file read buffer\n",FL,__func__, rw_buffer_size ); @@ -2359,7 +2245,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct if (MIME_is_diskfile_RFC822(hinfo->scratch) > 0 ) { // 20040305-1304:PLD: unpack the file, propagate result upwards - result = MIME_unpack_single( unpack_metadata, hinfo->scratch, (hinfo->current_recursion_level+1),ss ); + result = MIME_unpack_single( unpack_metadata, hinfo->scratch, (hinfo->current_recursion_level+ 1),ss ); } } break; @@ -2426,7 +2312,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct snprintf(hinfo->scratch,sizeof(hinfo->scratch),"%s/%s",unpack_metadata->dir,hinfo->filename); // 20040305-1304:PLD: unpack the file, propagate result upwards - result = MIME_unpack_single( unpack_metadata, hinfo->scratch, (hinfo->current_recursion_level+1),ss ); + result = MIME_unpack_single( unpack_metadata, hinfo->scratch, (hinfo->current_recursion_level+ 1),ss ); } } // Decode MHT files } // If result != -1 @@ -2525,7 +2411,7 @@ int MIME_handle_multipart( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struc // we need to now adjust the input-filename so that it correctly is prefixed // with the directory we unpacked to. - //result = MIME_unpack_single( unpackdir, fn, current_recursion_level +1, ss); + //result = MIME_unpack_single( unpackdir, fn, current_recursion_level + 1, ss); result = MIME_unpack_single( unpack_metadata, fn, current_recursion_level, ss ); free(fn); } @@ -2731,7 +2617,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M // Circumvent attempts to trick the boundary // Even though we may end up pushing 3 boundaries to the stack // they will be popped off if they're not needed. - if (*fbc == '"') { BS_push((fbc +1)); MIMEH_set_defect( hinfo, MIMEH_DEFECT_UNBALANCED_BOUNDARY_QUOTE); } + if (*fbc == '"') { BS_push((fbc + 1)); MIMEH_set_defect( hinfo, MIMEH_DEFECT_UNBALANCED_BOUNDARY_QUOTE); } if (*lbc == '"') { *lbc = '\0'; BS_push(h->boundary); *lbc = '"'; MIMEH_set_defect( hinfo, MIMEH_DEFECT_UNBALANCED_BOUNDARY_QUOTE); } h->boundary_located = 0; } else { @@ -2838,8 +2724,8 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M snprintf(scratch,sizeof(scratch),"%s/%s",unpackdir, h->filename); snprintf(h->filename,sizeof(h->filename),"%s",scratch); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Now calling MIME_unpack_single() on the file '%s' for our RFC822 decode operation.",FL,__func__, scratch); - //result = MIME_unpack_single( unpackdir, h->filename, current_recursion_level +1, ss); - result = MIME_unpack( unpackdir, h->filename, current_recursion_level +1 ); + //result = MIME_unpack_single( unpackdir, h->filename, current_recursion_level + 1, ss); + result = MIME_unpack( unpackdir, h->filename, current_recursion_level + 1 ); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Unpack result = %d", FL,__func__, result); result = 0; } else return result; // 20040305-1312:PLD @@ -2887,7 +2773,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M // with the directory we unpacked to. fn = malloc(fn_l); snprintf(fn,fn_l,"%s/%s",unpack_metadata->dir,h->filename); - //result = MIME_unpack_single( unpackdir, fn, current_recursion_level +1, ss); + //result = MIME_unpack_single( unpackdir, fn, current_recursion_level + 1, ss); result = MIME_unpack_single( unpack_metadata, fn, current_recursion_level,ss ); free(fn); } @@ -2918,8 +2804,8 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Testing '%s' for email type",FL,__func__,mime_fname); if (MIME_is_diskfile_RFC822(mime_fname)) { - //MIME_unpack_single( unpackdir, mime_fname, (hinfo->current_recursion_level+1), ss); - MIME_unpack_single( unpack_metadata, mime_fname, current_recursion_level+1,ss); + //MIME_unpack_single( unpackdir, mime_fname, (hinfo->current_recursion_level+ 1), ss); + MIME_unpack_single( unpack_metadata, mime_fname, current_recursion_level+ 1,ss); } free(mime_fname); } @@ -3191,7 +3077,7 @@ int MIME_unpack_single_fp( RIPMIME_output *unpack_metadata, FILE *fi, int curren if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: preparing to decode, calling stage2...\n",FL,__func__); // 20040318-0001:PLD - result = MIME_unpack_stage2(&f, unpack_metadata, &h, current_recursion_level +1, ss); + result = MIME_unpack_stage2(&f, unpack_metadata, &h, current_recursion_level + 1, ss); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: done decoding ( in stage2 ) result=%d, to %s\n",FL,__func__, result, unpack_metadata->dir); // fclose(fi); 20040208-1726:PLD if ( headers_save_set_here > 0 ) @@ -3236,7 +3122,7 @@ int MIME_unpack( RIPMIME_output *unpack_metadata, char *mpname, int current_recu else { if (MIME_DNORMAL) LOGGER_log("%s:%d:%s: Unpacking standard mailpack",FL,__func__,mpname,unpack_metadata->dir,current_recursion_level); - result = MIME_unpack_single( unpack_metadata, mpname, (current_recursion_level +1), &ss ); + result = MIME_unpack_single( unpack_metadata, mpname, (current_recursion_level + 1), &ss ); } if (glb.no_nameless) diff --git a/mime.h b/mime.h index ef633dc..708fc21 100644 --- a/mime.h +++ b/mime.h @@ -21,14 +21,6 @@ #define MIME_ERROR_FFGET_EMPTY 241 #define MIME_ERROR_B64_INPUT_STREAM_EOF 242 -#define _MIME_RENAME_METHOD_INFIX 1 -#define _MIME_RENAME_METHOD_PREFIX 2 -#define _MIME_RENAME_METHOD_POSTFIX 3 -#define _MIME_RENAME_METHOD_RANDINFIX 4 -#define _MIME_RENAME_METHOD_RANDPREFIX 5 -#define _MIME_RENAME_METHOD_RANDPOSTFIX 6 - - #define _MIME_STRLEN_MAX 1023 /* Debug levels */ diff --git a/mime_element.c b/mime_element.c index 7f3783c..e20e357 100644 --- a/mime_element.c +++ b/mime_element.c @@ -2,14 +2,20 @@ #include #include #include +#include +#include #include "mime_element.h" #include "logger.h" +#include "pldstr.h" #ifndef FL #define FL __FILE__, __LINE__ #endif +#define _FS_PATH_MAX 1023 +#define _FS_FILE_MAX 254 + /* Dynamic array support*/ #define INITIAL_SIZE 8 @@ -23,6 +29,21 @@ void updateItem(dynamic_array* container, int i, MIME_element* item); MIME_element* getItem(dynamic_array* container, int i); void deleteItem(dynamic_array* container, int i); +struct MIME_globals { + int debug; +}; + +static struct MIME_globals glb; + +#define MIME_DNORMAL (glb.debug) + +int MIMEELEMENT_set_debug( int level ) +{ + glb.debug = level; + return glb.debug; +} + + void all_MIME_elements_init (void) { all_MIME_elements.mime_count = 0; @@ -36,7 +57,7 @@ static char * dup_ini (char* s) return (s != NULL) ? strdup(s) : "\0"; } -MIME_element* MIME_element_add (void* parent, RIPMIME_output *unpack_metadata, char* filename, char* content_type_string, char* content_transfer_encoding, char* name, int current_recursion_level, int attachment_count, int filecount) +MIME_element* MIME_element_add (MIME_element* parent, RIPMIME_output *unpack_metadata, char* filename, char* content_type_string, char* content_transfer_encoding, char* name, int current_recursion_level, int attachment_count, int filecount) { MIME_element *cur = malloc(sizeof(MIME_element)); int fullpath_len = 0; @@ -47,6 +68,7 @@ MIME_element* MIME_element_add (void* parent, RIPMIME_output *unpack_metadata, c insertItem(all_MIME_elements.mime_arr, cur); cur->parent = parent; cur->id = all_MIME_elements.mime_count++; + cur->directory = unpack_metadata->dir; cur->filename = dup_ini(filename); cur->content_type_string = dup_ini(content_type_string); cur->content_transfer_encoding = dup_ini(content_transfer_encoding); @@ -60,7 +82,7 @@ MIME_element* MIME_element_add (void* parent, RIPMIME_output *unpack_metadata, c return cur; } - // LOGGER_log("%s:%d:%s:DEBUG: Decoding [encoding=%d] to %s\n",FL,__func__, content_transfer_encoding, cur->fullpath); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding [encoding=%d] to %s\n",FL,__func__, content_transfer_encoding, cur->fullpath); if (unpack_metadata != NULL && unpack_metadata->unpack_mode == RIPMIME_UNPACK_MODE_LIST_FILES && cur->f != NULL) { fprintf (stdout, "%d|%d|%d|%d|%s|%s\n", all_MIME_elements.mime_count, attachment_count, filecount, current_recursion_level, cur->content_type_string, cur->filename); @@ -76,7 +98,7 @@ static void dup_free(char *s) void MIME_element_remove (MIME_element* cur) { - // LOGGER_log("%s:%d:%s:start\n",FL,__func__); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:start\n",FL,__func__); if (cur->f != NULL) { fclose(cur->f); @@ -90,63 +112,162 @@ void MIME_element_remove (MIME_element* cur) free(cur); } -/* +static inline int get_random_value(void) { + int randval; + FILE *fp; - FILE *fo; - struct stat st; + fp = fopen("/dev/urandom", "r"); + fread(&randval, sizeof(randval), 1, fp); + fclose(fp); + if (randval < 0) + { randval = randval *( -1); }; + return randval; +} + +/*------------------------------------------------------------------------ +Procedure: MIME_test_uniquename ID:1 +Purpose: Checks to see that the filename specified is unique. If it's not +unique, it will modify the filename +Input: char *path: Path in which to look for similar filenames +char *fname: Current filename +int method: Method of altering the filename (infix, postfix, prefix, randinfix, randpostfix, randprefix) +Output: +Errors: +------------------------------------------------------------------------*/ +int MIME_test_uniquename( char *path, char *fname, int method ) +{ + struct stat buf; + + char newname[ _FS_PATH_MAX + 1]; + char scr[ _FS_PATH_MAX + 1]; /** Scratch var **/ + char *frontname, *extention; + int cleared = 0; + int count = 1; - // Determine a file name we can use. - do { + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Start (%s)",FL,__func__,fname); - } - while (stat(glb.doubleCRname, &st) == 0); + frontname = extention = NULL; // shuts the compiler up + if (method == _MIME_RENAME_METHOD_INFIX) + { + PLD_strncpy(scr,fname, _FS_PATH_MAX); + frontname = scr; + extention = strrchr(scr,'.'); - fo = fopen(glb.doubleCRname,"w"); - if (!fo) - { - LOGGER_log("%s:%d:MIMEH_save_doubleCR:ERROR: unable to open '%s' to write (%s)", FL,glb.doubleCRname,strerror(errno)); - return -1; - } - - FILE *fptr1, *fptr2; - char filename[100]; - int c; + if (extention) + { + *extention = '\0'; + extention++; + } + else + { + method = _MIME_RENAME_METHOD_POSTFIX; + } + } - printf("Enter the filename to open for reading: "); - scanf("%s", filename); + if (method == _MIME_RENAME_METHOD_RANDINFIX) + { + PLD_strncpy(scr,fname, _FS_PATH_MAX); + frontname = scr; + extention = strrchr(scr,'.'); - // Open one file for reading - fptr1 = fopen(filename, "r"); - if (fptr1 == NULL) - { - printf("Cannot open file %s\n", filename); - exit(1); - } + if (extention) + { + *extention = '\0'; + extention++; + } + else + { + method = _MIME_RENAME_METHOD_RANDPOSTFIX; + } + } - printf("Enter the filename to open for writing: "); - scanf("%s", filename); + snprintf(newname, _FS_PATH_MAX,"%s/%s",path,fname); + while (!cleared) + { + if ((stat(newname, &buf) == -1)) + { + cleared++; + } + else + { + int randval = get_random_value(); + + switch (method) { + case _MIME_RENAME_METHOD_PREFIX: + snprintf(newname, _FS_PATH_MAX,"%s/%d_%s",path,count,fname); + break; + case _MIME_RENAME_METHOD_INFIX: + snprintf(newname, _FS_PATH_MAX,"%s/%s_%d.%s",path,frontname,count,extention); + break; + case _MIME_RENAME_METHOD_POSTFIX: + snprintf(newname, _FS_PATH_MAX,"%s/%s_%d",path,fname,count); + break; + case _MIME_RENAME_METHOD_RANDPREFIX: + snprintf(newname, _FS_PATH_MAX,"%s/%d_%d_%s",path,count,randval,fname); + break; + case _MIME_RENAME_METHOD_RANDINFIX: + snprintf(newname, _FS_PATH_MAX,"%s/%s_%d_%d.%s",path,frontname,count,randval,extention); + break; + case _MIME_RENAME_METHOD_RANDPOSTFIX: + snprintf(newname, _FS_PATH_MAX,"%s/%s_%d_%d",path,fname,count,randval); + } + count++; + } + } + if (count > 1) + { + frontname = strrchr(newname,'/'); + if (frontname) frontname++; + else frontname = newname; - // Open another file for writing - fptr2 = fopen(filename, "w"); - if (fptr2 == NULL) - { - printf("Cannot open file %s\n", filename); - exit(1); - } + PLD_strncpy(fname, frontname, _FS_PATH_MAX); //FIXME - this assumes that the buffer space is at least MIME_STRLEN_MAX sized. + } + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done (%s)",FL,__func__,fname); + return 0; +} - // Read contents from file - while ((c = fgetc(fptr1)) != EOF) - { - fputc(c, fptr2); - } +static void copyFILEcontent(FILE* src, FILE* dst) +{ + int c; /* Not CHAR! for EOF */ - printf("Contents copied to %s\n", filename); + fseek(src, 0, SEEK_SET); + // Read contents from file + while (EOF != (c = fgetc(src))) + { + fputc(c, dst); + } +} - fclose(fptr1); - fclose(fptr2); -*/ +void write_FS_file (MIME_element* cur, RIPMIME_output *unpack_metadata, int method) +{ + char * wr_filename = NULL; + FILE* wf = NULL; + int fn_l = 0; + + MIME_test_uniquename(cur->directory, cur->filename, method); + fn_l = strlen(cur->directory) + strlen(cur->filename) + sizeof(char) * 2; + wr_filename = malloc(fn_l); + snprintf(wr_filename,fn_l,"%s/%s",cur->directory,cur->filename); + if (cur->f == NULL) { + LOGGER_log("%s:%d:%s:ERROR: Cannot copy data from mime_element memory file, programming error (for %s)", FL,__func__, wr_filename, strerror(errno)); + free(wr_filename); + return; + } + // Prepend the unpackdir path to the headers file name + wf = fopen(wr_filename,"w"); + if (wf == NULL) + { + LOGGER_log("%s:%d:%s:ERROR: Cannot open '%s' for writing (%s)", FL,__func__, wr_filename, strerror(errno)); + free(wr_filename); + return; + } + copyFILEcontent(cur->f, wf); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Memory FILE have cpoied to %s",FL,__func__,wr_filename); + free(wr_filename); + fclose(wf); +} //------Dynamic array function definitions------ // Array initialization diff --git a/mime_element.h b/mime_element.h index cfd27f7..ddc711b 100644 --- a/mime_element.h +++ b/mime_element.h @@ -8,6 +8,13 @@ #define RIPMIME_UNPACK_MODE_COUNT_FILES 1 #define RIPMIME_UNPACK_MODE_LIST_FILES 2 +#define _MIME_RENAME_METHOD_INFIX 1 +#define _MIME_RENAME_METHOD_PREFIX 2 +#define _MIME_RENAME_METHOD_POSTFIX 3 +#define _MIME_RENAME_METHOD_RANDINFIX 4 +#define _MIME_RENAME_METHOD_RANDPREFIX 5 +#define _MIME_RENAME_METHOD_RANDPOSTFIX 6 + struct mime_output { char *dir; @@ -17,7 +24,7 @@ struct mime_output typedef struct mime_output RIPMIME_output; typedef struct { - void* parent; + struct MIME_element* parent; int id; char* directory; char* filename; @@ -42,9 +49,11 @@ typedef struct { extern all_MIME_elements_s all_MIME_elements; void all_MIME_elements_init (void); -MIME_element* MIME_element_add (void* parent, RIPMIME_output *unpack_metadata, char* filename, char* content_type_string, char* content_transfer_encoding, char* name, int current_recursion_level, int attachment_count, int filecount); +MIME_element* MIME_element_add (MIME_element* parent, RIPMIME_output *unpack_metadata, char* filename, char* content_type_string, char* content_transfer_encoding, char* name, int current_recursion_level, int attachment_count, int filecount); void MIME_element_remove (MIME_element* cur); void printArray(dynamic_array* container); void freeArray(dynamic_array* container); +int MIME_test_uniquename( char *path, char *fname, int method ); + #endif From 7b8ca73a68743b9474a7d1e8ca26ecbadecfd0b8 Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Mon, 28 Apr 2025 13:58:41 +0300 Subject: [PATCH 48/80] Link `pldstr` to `ripOLE` instead of copy, rename to `MIME_element_free` --- mime.c | 8 +- mime_element.c | 14 +- mime_element.h | 2 +- mime_headers.c | 2 +- ripOLE/ole.c | 2 +- ripOLE/olestream-unwrap.c | 2 +- ripOLE/pldstr.c | 771 +------------------------------------- ripOLE/pldstr.h | 41 +- uuencode.c | 2 +- 9 files changed, 18 insertions(+), 826 deletions(-) mode change 100644 => 120000 ripOLE/pldstr.c mode change 100644 => 120000 ripOLE/pldstr.h diff --git a/mime.c b/mime.c index bf56f03..f82d354 100644 --- a/mime.c +++ b/mime.c @@ -1196,7 +1196,7 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME else LOGGER_log("%s:%d:%s:WARNING: hinfo has been clobbered.\n",FL,__func__); } } - MIME_element_remove(cur_mime); + MIME_element_free(cur_mime); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Closed file and free'd buffer\n",FL,__func__); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: End[result = %d]\n",FL,__func__,result); @@ -1276,7 +1276,7 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM } // while if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done writing output file '%s'...now attempting to close.",FL,__func__, cur_mime->fullpath); - MIME_element_remove(cur_mime); + MIME_element_free(cur_mime); if (linecount == 0) { @@ -1551,7 +1551,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: input stream broken for base64 decoding for file %s. %ld bytes of data in buffer to be written out\n",FL,__func__,hinfo->filename,wbcount); status = MIME_ERROR_B64_INPUT_STREAM_EOF; fwrite(writebuffer, 1, wbcount, cur_mime->f); - MIME_element_remove(cur_mime); + MIME_element_free(cur_mime); if (writebuffer) free(writebuffer); return status; break; @@ -1644,7 +1644,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH fwrite(writebuffer, 1, wbcount, cur_mime->f); } /* close the output file, we're done writing to it */ - MIME_element_remove(cur_mime); + MIME_element_free(cur_mime); /* if we didn't really write anything, then trash the file */ if (bytecount == 0) { diff --git a/mime_element.c b/mime_element.c index e20e357..e8dad12 100644 --- a/mime_element.c +++ b/mime_element.c @@ -96,7 +96,7 @@ static void dup_free(char *s) free(s); } -void MIME_element_remove (MIME_element* cur) +void MIME_element_free (MIME_element* cur) { if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:start\n",FL,__func__); @@ -276,7 +276,7 @@ void arrayInit(dynamic_array** arr_ptr) dynamic_array *container; container = (dynamic_array*)malloc(sizeof(dynamic_array)); if(!container) { - printf("Memory Allocation Failed\n"); + LOGGER_log("%s:%d:%s:ERROR: Memory allocation failed", FL,__func__); exit(0); } @@ -284,7 +284,7 @@ void arrayInit(dynamic_array** arr_ptr) container->capacity = INITIAL_SIZE; container->array = (MIME_element **)malloc(INITIAL_SIZE * sizeof(MIME_element*)); if (!container->array){ - printf("Memory Allocation Failed\n"); + LOGGER_log("%s:%d:%s:ERROR: Memory allocation failed", FL,__func__); exit(0); } @@ -299,7 +299,7 @@ void insertItem(dynamic_array* container, MIME_element* item) container->capacity <<= 1; container->array = realloc(container->array, container->capacity * sizeof(MIME_element*)); if(!container->array) { - printf("Out of Memory\n"); + LOGGER_log("%s:%d:%s:ERROR: Out of memory reallocation", FL,__func__); container->array = temp; return; } @@ -311,7 +311,7 @@ void insertItem(dynamic_array* container, MIME_element* item) MIME_element* getItem(dynamic_array* container, int index) { if(index >= container->size) { - printf("Index Out of Bounds\n"); + LOGGER_log("%s:%d:%s:ERROR: Index %d is out of bounds", FL,__func__, index); return NULL; } return container->array[index]; @@ -321,7 +321,7 @@ MIME_element* getItem(dynamic_array* container, int index) void updateItem(dynamic_array* container, int index, MIME_element* item) { if (index >= container->size) { - printf("Index Out of Bounds\n"); + LOGGER_log("%s:%d:%s:ERROR: Index %d is out of bounds", FL,__func__, index); return; } container->array[index] = item; @@ -331,7 +331,7 @@ void updateItem(dynamic_array* container, int index, MIME_element* item) void deleteItem(dynamic_array* container, int index) { if(index >= container->size) { - printf("Index Out of Bounds\n"); + LOGGER_log("%s:%d:%s:ERROR: Index %d is out of bounds", FL,__func__, index); return; } diff --git a/mime_element.h b/mime_element.h index ddc711b..73e1f5e 100644 --- a/mime_element.h +++ b/mime_element.h @@ -50,7 +50,7 @@ extern all_MIME_elements_s all_MIME_elements; void all_MIME_elements_init (void); MIME_element* MIME_element_add (MIME_element* parent, RIPMIME_output *unpack_metadata, char* filename, char* content_type_string, char* content_transfer_encoding, char* name, int current_recursion_level, int attachment_count, int filecount); -void MIME_element_remove (MIME_element* cur); +void MIME_element_free (MIME_element* cur); void printArray(dynamic_array* container); void freeArray(dynamic_array* container); diff --git a/mime_headers.c b/mime_headers.c index bbef045..b26aaf4 100644 --- a/mime_headers.c +++ b/mime_headers.c @@ -747,7 +747,7 @@ int MIMEH_save_doubleCR( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct break; } } - MIME_element_remove(cur_mime); + MIME_element_free(cur_mime); return 0; } diff --git a/ripOLE/ole.c b/ripOLE/ole.c index f7a83e3..9576da3 100644 --- a/ripOLE/ole.c +++ b/ripOLE/ole.c @@ -1519,7 +1519,7 @@ int OLE_store_stream( struct OLE_object *ole, char *stream_name, RIPMIME_output { LOGGER_log("%s:%d:%s:WARNING: Only wrote %d of %d bytes to file %s",FL,__func__,written_bytes,stream_size,cur_mime->fullpath); } - MIME_element_remove (cur_mime); + MIME_element_free (cur_mime); if ((OLE_VNORMAL(ole->verbose))&&(ole->filename_report_fn != NULL)) { diff --git a/ripOLE/olestream-unwrap.c b/ripOLE/olestream-unwrap.c index fccf709..c1fa377 100644 --- a/ripOLE/olestream-unwrap.c +++ b/ripOLE/olestream-unwrap.c @@ -167,7 +167,7 @@ int OLEUNWRAP_save_stream( struct OLEUNWRAP_object *oleuw, char *fname, RIPMIME_ LOGGER_log("%s:%d:%s:WARNING: Only wrote %d of %d bytes to file %s\n",FL,__func__, write_count, bytes, cur_mime->fullpath ); } - MIME_element_remove (cur_mime); + MIME_element_free (cur_mime); DUW LOGGER_log("%s:%d:%s:DEBUG: Done saving '%s'",FL,__func__, fname); return result; diff --git a/ripOLE/pldstr.c b/ripOLE/pldstr.c deleted file mode 100644 index ca29de3..0000000 --- a/ripOLE/pldstr.c +++ /dev/null @@ -1,770 +0,0 @@ - -#include -#include -#include -#include -#include -#include -#include - -#include "logger.h" -#include "pldstr.h" - - -/*-----------------------------------------------------------------\ - Function Name : *PLD_strstr - Returns Type : char - ----Parameter List - 1. char *haystack, - 2. char *needle, - 3. int insensitive, - ------------------ - Exit Codes : - Side Effects : --------------------------------------------------------------------- - Comments: - --------------------------------------------------------------------- - Changes: - -\------------------------------------------------------------------*/ -char *PLD_strstr(char *haystack, char *needle, int insensitive) -{ - char *hs, *ne; - char *result; - -// LOGGER_log("%s:%d:\nHS=%s\nNE=%s\nIS=%d\n",FL, haystack, needle, insensitive ); - - if (insensitive > 0) - { - hs = strdup(haystack); - PLD_strlower(hs); - ne = strdup(needle); - PLD_strlower(ne); - } else { - hs = haystack; - ne = needle; - } - - result = strstr(hs, ne); -// if (result) LOGGER_log("%s:%d:HIT: %s",FL, result); -// else LOGGER_log("%s:%d:MISS (looking for %s|%s)",FL, needle,ne); - - if ((result != NULL)&&(insensitive > 0)) - { - result = result -hs +haystack; -// free(hs); -// free(ne); - -// LOGGER_log("%s:%d:HIT - %s",FL, result ); - } - if (insensitive) { - free(hs); - free(ne); - } - - return result; -} - -/*------------------------------------------------------------------------ -Procedure: PLD_strncpy ID:1 -Purpose: Copy characters from 'src' to 'dst', writing not more than 'len' -characters to the destination, including the terminating \0. -Thus, for any effective copying, len must be > 1. -Input: char *dst: Destination string -char *src: Source string -size_t len: length of string -Output: Returns a pointer to the destination string. -Errors: -------------------------------------------------------------------------*/ -char *PLD_strncpy (char *dst, const char *src, size_t len) -{ - - // Thanks go to 'defrost' of #c for providing the replacement - // code which you now see here. It covers the errors better - // than my own previous code. - - // If we have no buffer space, then it's futile attempting - // to copy anything, just return NULL - if (len==0) return NULL; - - // Providing our destination pointer isn't NULL, we can - // commence copying data across - - if (dst) - { - char *dp = dst; - - // If our source string exists, start moving it to the - // destination string character at a time. - if (src) - { - char *sp = (char *)src; - while ((--len)&&(*sp)) { *dp=*sp; dp++; sp++; } - } - - *dp='\0'; - } - - return dst; -} - - - -/*------------------------------------------------------------------------ -Procedure: PLD_strncat ID:1 -Purpose: Buffer size limited string concat function for two strings. -Input: char *dst: Destination string -char *src: Source string -size_t len: Destination string buffer size - total string size cannot exceed this -Output: -Errors: If the length of both strings in total is greater than the available buffer space -in *dst, we copy the maximum possible amount of chars from *src such that -buffer does not overflow. A suffixed '\0' will always be appended. -------------------------------------------------------------------------*/ -char *PLD_strncat( char *dst, const char *src, size_t len ) -{ - char *dp = dst; - const char *sp = src; - size_t cc; - - if (len == 0) return dst; - - len--; - - // Locate the end of the current string. - cc = 0; - while ((*dp)&&(cc < len)) { dp++; cc++; } - - // If we have no more buffer space, then return the destination - - if (cc >= len) return dst; - - // While we have more source, and there's more char space left in the buffer - - while ((*sp)&&(cc < len)) - { - cc++; - *dp = *sp; - dp++; - sp++; - } - - // Terminate dst, as a gaurantee of string ending. - - *dp = '\0'; - - return dst; -} - - -/*------------------------------------------------------------------------ -Procedure: PLD_strncate ID:1 -Purpose: Catencates a source string to the destination string starting from a given -endpoint. This allows for faster catencation of strings by avoiding the -computation required to locate the endpoint of the destination string. -Input: char *dst: Destination string -char *src: Source string -size_t len: Destination buffer size -char *endpoint: Endpoint of destination string, location from where new -string will be appended -Output: -Errors: -------------------------------------------------------------------------*/ -char *PLD_strncate( char *dst, const char *src, size_t len, char *endpoint ) -{ - char *dp = dst; - const char *sp = src; - size_t cc = 0; - - if (len == 0) return dst; - - len--; - - // If endpoint does not relate correctly, then force manual detection - // of the endpoint. - - if ((!endpoint)||(endpoint == dst)||((endpoint -dst +1)>(int)len)) - { - // Locate the end of the current string. - cc = 0; - while ((*dp != '\0')&&(cc < len)) { dp++; cc++; } - } - else { - cc = endpoint -dst +1; - dp = endpoint; - } - - // If we have no more buffer space, then return the destination - - if (cc >= len) return dst; - - // While we have more source, and there's more char space left in the buffer - - while ((*sp)&&(cc < len)) - { - cc++; - *dp = *sp; - dp++; - sp++; - } - - // Terminate dst, as a gaurantee of string ending. - - *dp = '\0'; - - return dst; -} - - - - - -/*------------------------------------------------------------------------ -Procedure: XAM_strncasecmp ID:1 -Purpose: Portable version of strncasecmp(), this may be removed in later -versions as the strncase* type functions are more widely -implemented -Input: -Output: -Errors: -------------------------------------------------------------------------*/ -int PLD_strncasecmp( char *s1, char *s2, int n ) -{ - char *ds1 = s1, *ds2 = s2; - char c1, c2; - int result = 0; - - while(n > 0) - { - c1 = tolower(*ds1); - c2 = tolower(*ds2); - - if (c1 == c2) - { - n--; - ds1++; - ds2++; - } - else - { - result = c2 - c1; - n = 0; - } - - } - - return result; - -} - - - - - -/*------------------------------------------------------------------------ -Procedure: XAM_strtok ID:1 -Purpose: A thread safe version of strtok() -Input: -Output: -Errors: -------------------------------------------------------------------------*/ -char *PLD_strtok( struct PLD_strtok *st, char *line, char *delimeters ) -{ - char *stop; - char *dc; - char *result = NULL; - - if ( line ) - { - st->start = line; - } - - //Strip off any leading delimeters - - dc = delimeters; - while ((st->start)&&(*dc != '\0')) - { - if (*dc == *(st->start)) - { - st->start++; - dc = delimeters; - } - else dc++; - } - - // Where we are left, is the start of our token. - - result = st->start; - - if ((st->start)&&(*(st->start) != '\0')) - { - stop = strpbrk( st->start, delimeters ); /* locate our next delimeter */ - - // If we found a delimeter, then that is good. We must now break the string here - // and don't forget to store the character which we stopped on. Very useful bit - // of information for programs which process expressions. - - if (stop) - { - - // Store our delimeter. - - st->delimeter = *stop; - - // Terminate our token. - - *stop = '\0'; - - - // Because we're emulating strtok() behaviour here, we have to - // absorb all the concurrent delimeters, that is, unless we - // reach the end of the string, we cannot return a string with - // no chars. - - stop++; - dc = delimeters; - while (*dc != '\0') - { - if (*dc == *stop) - { - stop++; - dc = delimeters; - } - else dc++; - } // While - - if (*stop == '\0') st->start = NULL; - else st->start = stop; - - } - else { - st->start = NULL; - st->delimeter = '\0'; - } - } - else { - st->start = NULL; - result = NULL; - } - - - return result; -} - - - -/*------------------------------------------------------------------------ -Procedure: PLD_strlower ID:1 -Purpose: Converts a string to lowercase -Input: char *convertme : string to convert -Output: -Errors: -Comments: Really need to validate against high-ASCII chars. - Tested against strings like; - Logo de la République française - Македонски - -------------------------------------------------------------------------*/ -int PLD_strlower( char *convertme ) -{ - - char *c = convertme; - - while ( *c != '\0') {*c = (unsigned char)tolower((int)*c); c++;} - - return 0; -} - - -/*-----------------------------------------------------------------\ - Function Name : *PLD_strreplace - Returns Type : char - ----Parameter List - 1. char *source, Original buffer, \0 terminated - 2. char *searchfor, String sequence to search for - 3. char *replacewith, String sequence to replace 'searchfor' with - 4. int replacenumber , How many times to replace 'searchfor', 0 == unlimited - ------------------ - Exit Codes : Returns a pointer to the new buffer space. The original - buffer will still remain intact - ensure that the calling - program free()'s the original buffer if it's no longer - needed - Side Effects : - -------------------------------------------------------------------- -Comments: -Start out with static text matching - upgrade to regex later. - --------------------------------------------------------------------- -Changes: - -\------------------------------------------------------------------*/ -char *PLD_strreplace_general( struct PLD_strreplace *replace_details ) -{ - char *new_buffer=NULL; - char *source_end; - char *segment_start, *segment_end, *segment_p; - char *new_p; - char *preexist_location=NULL; - char *postexist_location=NULL; - int replace_count=0; - int size_required; - int size_difference; - int source_length; - int searchfor_length; - int replacewith_length; - int segment_ok; - - if (replace_details->source == NULL) return NULL; - - source_length = strlen( replace_details->source ); - source_end = replace_details->source +source_length; - searchfor_length = strlen(replace_details->searchfor); - replacewith_length = strlen(replace_details->replacewith); - size_difference = replacewith_length -searchfor_length; - size_required = source_length; - replace_count = replace_details->replacenumber; - - if ((replace_details->preexist != NULL)&&(strlen(replace_details->preexist) < 1)) replace_details->preexist = NULL; - if ((replace_details->postexist != NULL)&&(strlen(replace_details->postexist) < 1)) replace_details->postexist = NULL; - - // If we have a 'pre-exist' request, then we need to check this out first - // because if the pre-exist string cannot be found, then there's very - // little point us continuing on in our search ( because without the - // preexist string existing, we are thus not qualified to replace anything ) - if (replace_details->preexist != NULL) - { - preexist_location = PLD_strstr(replace_details->source, replace_details->preexist, replace_details->insensitive); - if (preexist_location == NULL) - { - return replace_details->source; - } - } - - // Determine if initial POSTexist tests will pass, if we don't pick up - // anything here, then there's no point in continuing either - if (replace_details->postexist != NULL) - { - char *p = replace_details->source; - postexist_location = NULL; - do { - p = PLD_strstr(p, replace_details->postexist, replace_details->insensitive); - if (p != NULL) - { - postexist_location = p; - p = p +strlen(replace_details->postexist); - } - } while (p != NULL); - - if (postexist_location == NULL) - { - return replace_details->source; - } - } - - - // Step 1 - determine the MAXIMUM number of times we might have to replace this string ( or the limit - // set by replacenumber - // - // Note - we only need this number if the string we're going to be inserting into the - // source is larger than the one we're replacing - this is so that we can ensure that - // we have sufficient memory available in the buffer. - if (size_difference > 0) - { - if (replace_count == 0) - { - char *p, *q; - - p = replace_details->source; - q = PLD_strstr(p, replace_details->searchfor, replace_details->insensitive); - while (q != NULL) - { - replace_count++; - //size_required += size_difference; - p = q +searchfor_length; - q = PLD_strstr(p, replace_details->searchfor, replace_details->insensitive); - } - - } - size_required = source_length +(size_difference *replace_count) +1; - } else size_required = source_length +1; - - - // Allocate the memory required to hold the new string [at least], check to see that - // all went well, if not, then return an error - new_buffer = malloc( sizeof(char) *size_required); - if (new_buffer == NULL) - { - LOGGER_log("%s:%d:PLD_strreplace:ERROR: Cannot allocate %d bytes of memory to perform replacement operation", FL, size_required); - return replace_details->source; - } - - // Our segment must always start at the beginning of the source, - // on the other hand, the segment_end can be anything from the - // next byte to NULL ( which is specially treated to mean to - // the end of the source ) - segment_start = replace_details->source; - - - // Locate the first segment - segment_ok = 0; - segment_end = PLD_strstr(replace_details->source, replace_details->searchfor, replace_details->insensitive); - - // Determine if the first segment is valid in the presence of the - // pre-exist and post-exist requirements - while ((segment_end != NULL)&&(segment_ok == 0)\ - &&((replace_details->preexist != NULL)||(replace_details->postexist != NULL))) - { - int pre_ok = 0; - int post_ok = 0; - - // The PREexist test assumes a couple of factors - please ensure these are - // relevant if you change any code prior to this point. - // - // 1. preexist_location has already been computed and is not NULL - // - // 2. By relative position, the first preexist_location will be a valid location - // on which to validate for ALL replacements beyond that point, thus, we - // never actually have to recompute preexist_location again. - // - // 3. Conversely, the last computed postexist_location is valid for all - // matches before it - // - if (preexist_location == NULL) pre_ok = 1; - else if (preexist_location < segment_end){ pre_ok = 1;} - - if (postexist_location == NULL) post_ok = 1; - else if (postexist_location > segment_end){ post_ok = 1;} - - if ((pre_ok == 0)||(post_ok == 0)) { segment_end = PLD_strstr(segment_end +searchfor_length, replace_details->searchfor, replace_details->insensitive); } - else segment_ok = 1; - } - - segment_p = segment_start; - new_p = new_buffer; - while (segment_start != NULL) - { - int replacewith_count; - char *replacewith_p; - - if (segment_end == NULL) segment_end = source_end; - - replace_count--; - - // Perform the segment copy - segment_p = segment_start; - while ((segment_p < segment_end)&&(size_required > 0)) - { - *new_p = *segment_p; - new_p++; - segment_p++; - size_required--; - } - - // Perform the string replacement - if (segment_end < source_end) - { - replacewith_count = replacewith_length; - replacewith_p = replace_details->replacewith; - while ((replacewith_count--)&&(size_required > 0)) - { - *new_p = *replacewith_p; - new_p++; - replacewith_p++; - size_required--; - } - } - - if (size_required < 1 ) - { - LOGGER_log("%s:%d:PLD_strreplace_general: Allocated memory ran out while replacing '%s' with '%s'",FL, replace_details->searchfor, replace_details->replacewith); - *new_p='\0'; - break; - } - - // Find the next segment - segment_start = segment_end +searchfor_length; - - // If we've reached the end of the number of replacements we're supposed - // to do, then we prepare the termination of the while loop by setting - // our segment end to the end of the source. - // - // NOTE: Remember that the replace_count is pre-decremented at the start - // of the while loop, so, if the caller requested '0' replacements - // this will now be -1, thus, it won't get terminated from this == 0 - // match. Just thought you'd like to be reminded of that incase you - // were wondering "Huh? this would terminate an unlimited replacement" - if (replace_count == 0) - { - segment_end = NULL; - } else { - // If our new segment to copy starts after the - // end of the source, then we actually have - // nothing else to copy, thus, we prepare the - // segment_start varible to cause the while loop - // to terminate. - // - // Otherwise, we try and locate the next segment - // ending point, and set the starting point to - // be on the 'other side' of the 'searchfor' string - // which we found in the last search. - // - if (segment_start > source_end) - { - segment_start = NULL; - } else { - - // Try find the next segment - segment_ok = 0; - segment_end = PLD_strstr(segment_end +searchfor_length, replace_details->searchfor, replace_details->insensitive); - - // If we have a pre/post-exist requirement, then enter into this - // series of tests. NOTE - at least one of the pre or post tests - // must fire to give an meaningful result - else we'll end up with - // a loop which simply goes to the end of the searchspace buffer - while ((segment_end != NULL)&&(segment_ok == 0)\ - &&((replace_details->preexist != NULL)||(replace_details->postexist != NULL))) - { - int pre_ok = 0; - int post_ok = 0; - - // The PREexist test assumes a couple of factors - please ensure these are - // relevant if you change any code prior to this point. - // - // 1. preexist_location has already been computed and is not NULL - // - // 2. By relative position, the first preexist_location will be a valid location - // on which to validate for ALL replacements beyond that point, thus, we - // never actually have to recompute preexist_location again. - // - // 3. Conversely, the last computed postexist_location is valid for all - // matches before it - // - if (preexist_location == NULL) pre_ok = 1; - else if (preexist_location < segment_end){ pre_ok = 1;} - - if (postexist_location == NULL) post_ok = 1; - else if (postexist_location > segment_end){ post_ok = 1;} - - if ((pre_ok == 0)||(post_ok == 0)) { segment_end = PLD_strstr(segment_end +searchfor_length, replace_details->searchfor, replace_details->insensitive); } - else segment_ok = 1; - } - - } // If-else segment_start > source_end - - } - - } - - *new_p = '\0'; - -// if (replace_details->source != NULL) free (replace_details->source); -// replace_details->source = new_buffer; - return new_buffer; -} - -/*-----------------------------------------------------------------\ - Function Name : *PLD_strreplace - Returns Type : char - ----Parameter List - 1. char **source, - 2. char *searchfor, - 3. char *replacewith, - 4. int replacenumber , - ------------------ - Exit Codes : - Side Effects : - -------------------------------------------------------------------- -Comments: - --------------------------------------------------------------------- -Changes: - -\------------------------------------------------------------------*/ -char *PLD_strreplace( char **source, char *searchfor, char *replacewith, int replacenumber ) -{ - struct PLD_strreplace replace_details; - char *tmp_source; - - replace_details.source = *source; - replace_details.searchfor = searchfor; - replace_details.replacewith = replacewith; - replace_details.replacenumber = replacenumber; - replace_details.preexist = NULL; - replace_details.postexist = NULL; - replace_details.insensitive = 0; - - tmp_source = PLD_strreplace_general( &replace_details ); - - if (tmp_source != *source) *source = tmp_source; - - return *source; -} - - -/*-----------------------------------------------------------------\ - Function Name : *PLD_dprintf - Returns Type : char - ----Parameter List - 1. const char *format, - 2. ..., - ------------------ - Exit Codes : - Side Effects : --------------------------------------------------------------------- - Comments: - This is a dynamic string allocation function, not as fast as some - other methods, but it works across the board with both glibc 2.0 - and 2.1 series. - --------------------------------------------------------------------- - Changes: - -\------------------------------------------------------------------*/ -char *PLD_dprintf(const char *format, ...) -{ - int n, size = 1024; // Assume we don't need more than 1K to start with - char *p; - va_list ap; - - // Attempt to allocate and then check - p = malloc(size *sizeof(char)); - if (p == NULL) return NULL; - - while (1) - { - // Attempt to print out string out into the allocated space - va_start(ap, format); - n = vsnprintf (p, size, format, ap); - va_end(ap); - - // If things went well, then return the new string - if ((n > -1) && (n < size)) return p; - - // If things didn't go well, then we have to allocate more space - // based on which glibc we're using ( fortunately, the return codes - // tell us which glibc is being used! *phew* - // - // If n > -1, then we're being told precisely how much space we need - // else (older glibc) we have to just guess again ... - - if (n > -1) size = n+1; // Allocate precisely what is needed - else size *= 2; // Double the amount allocated, note, we could just increase by 1K, but if we have a long string, we'd end up using a lot of realloc's - - // We could just realloc 'blind', but that'd be wrong and potentially cause a DoS, so - // instead, we'll be good and first attempt to realloc to a temp variable then, if all - // is well, we go ahead and update - if (1) - { - char *tmp_p; - - tmp_p = realloc(p, size); - if (tmp_p == NULL){ if (p != NULL) free(p); return NULL; } - else p = tmp_p; - } - } - -} - - -//-----------------END. diff --git a/ripOLE/pldstr.c b/ripOLE/pldstr.c new file mode 120000 index 0000000..4ffd4b6 --- /dev/null +++ b/ripOLE/pldstr.c @@ -0,0 +1 @@ +../pldstr.c \ No newline at end of file diff --git a/ripOLE/pldstr.h b/ripOLE/pldstr.h deleted file mode 100644 index 338a314..0000000 --- a/ripOLE/pldstr.h +++ /dev/null @@ -1,40 +0,0 @@ -#ifndef __PLDSTR__ -#define __PLDSTR__ - -#ifndef FL -#define FL __FILE__,__LINE__ -#endif - -struct PLD_strtok -{ - char *start; - char delimeter; -}; - -struct PLD_strreplace -{ - char *source; - char *searchfor; - char *replacewith; - - char *preexist; - char *postexist; - - int replacenumber; - - int insensitive; -}; - -char *PLD_strstr(char *haystack, char *needle, int insensitive); -char *PLD_strncpy( char *dst, const char *src, size_t len ); -char *PLD_strncat( char *dst, const char *src, size_t len ); -char *PLD_strncate( char *dst, const char *src, size_t len, char *endpoint ); -char *PLD_strtok( struct PLD_strtok *st, char *line, char *delimeters ); -int PLD_strncasecmp( char *s1, char *s2, int n ); -int PLD_strlower( char *convertme ); - -char *PLD_strreplace_general( struct PLD_strreplace *replace_details ); -char *PLD_strreplace( char **source, char *searchfor, char *replacewith, int replacenumber ); -char *PLD_dprintf(const char *fmt, ...); - -#endif diff --git a/ripOLE/pldstr.h b/ripOLE/pldstr.h new file mode 120000 index 0000000..da26a11 --- /dev/null +++ b/ripOLE/pldstr.h @@ -0,0 +1 @@ +../pldstr.h \ No newline at end of file diff --git a/uuencode.c b/uuencode.c index 126091a..3ca2438 100644 --- a/uuencode.c +++ b/uuencode.c @@ -603,7 +603,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *out_filename, int decode_whole_file } } - MIME_element_remove(cur_mime); + MIME_element_free(cur_mime); // Call our reporting function, else, if no function is defined, use the default // standard call From 47a4cbd3cbdaac6609b5649fc4daf08ffb71aeed Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Mon, 28 Apr 2025 16:55:53 +0300 Subject: [PATCH 49/80] Refactor rename_method processing, cleanup unused in TNEF --- mime.c | 18 ++++++++++++------ mime.h | 2 +- mime_element.c | 24 ++++++++++++++++++++---- mime_element.h | 6 ++++-- mime_headers.c | 2 +- ripOLE/ole.c | 2 +- ripOLE/olestream-unwrap.c | 2 +- ripmime.c | 20 ++++++++++---------- tnef/tnef.c | 2 -- uuencode.c | 2 +- 10 files changed, 51 insertions(+), 29 deletions(-) diff --git a/mime.c b/mime.c index f82d354..0113222 100644 --- a/mime.c +++ b/mime.c @@ -1196,7 +1196,7 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME else LOGGER_log("%s:%d:%s:WARNING: hinfo has been clobbered.\n",FL,__func__); } } - MIME_element_free(cur_mime); + MIME_element_deactivate(cur_mime, unpack_metadata); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Closed file and free'd buffer\n",FL,__func__); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: End[result = %d]\n",FL,__func__,result); @@ -1276,7 +1276,7 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM } // while if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done writing output file '%s'...now attempting to close.",FL,__func__, cur_mime->fullpath); - MIME_element_free(cur_mime); + MIME_element_deactivate(cur_mime, unpack_metadata); if (linecount == 0) { @@ -1551,7 +1551,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: input stream broken for base64 decoding for file %s. %ld bytes of data in buffer to be written out\n",FL,__func__,hinfo->filename,wbcount); status = MIME_ERROR_B64_INPUT_STREAM_EOF; fwrite(writebuffer, 1, wbcount, cur_mime->f); - MIME_element_free(cur_mime); + MIME_element_deactivate(cur_mime, unpack_metadata); if (writebuffer) free(writebuffer); return status; break; @@ -1644,7 +1644,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH fwrite(writebuffer, 1, wbcount, cur_mime->f); } /* close the output file, we're done writing to it */ - MIME_element_free(cur_mime); + MIME_element_deactivate(cur_mime, unpack_metadata); /* if we didn't really write anything, then trash the file */ if (bytecount == 0) { @@ -3168,9 +3168,15 @@ int MIME_unpack( RIPMIME_output *unpack_metadata, char *mpname, int current_recu /*-------------------------------------------------------------------- * MIME_close * - * Closes the files used in MIME_unpack, such as headers etc */ -void MIME_close( void ) + * Closes the files used in MIME_unpack, such as headers etc + * Writes memory FILE object content to disk if there is in-memory mode + */ +void MIME_close(RIPMIME_output *unpack_metadata, int rename_method) { + + if (unpack_metadata->unpack_mode == RIPMIME_UNPACK_MODE_EXTRACT_ONE) + write_all_to_FS_files(unpack_metadata, rename_method); + if (MIME_DNORMAL) { LOGGER_log("%s:%d:%s: start.",FL,__func__); printArray(all_MIME_elements.mime_arr); diff --git a/mime.h b/mime.h index 708fc21..79a86b4 100644 --- a/mime.h +++ b/mime.h @@ -105,7 +105,7 @@ char *MIME_get_blankfileprefix( void ); char *MIME_get_headersname( void ); char *MIME_get_subject( void ); void MIME_init( void ); -void MIME_close( void ); +void MIME_close( RIPMIME_output *unpack_metadata, int rename_method ); int MIME_set_tmpdir( char *tmpdir ); #endif diff --git a/mime_element.c b/mime_element.c index e8dad12..1173295 100644 --- a/mime_element.c +++ b/mime_element.c @@ -52,12 +52,12 @@ void all_MIME_elements_init (void) all_MIME_elements_s all_MIME_elements; -static char * dup_ini (char* s) +static char * dup_ini(char* s) { return (s != NULL) ? strdup(s) : "\0"; } -MIME_element* MIME_element_add (MIME_element* parent, RIPMIME_output *unpack_metadata, char* filename, char* content_type_string, char* content_transfer_encoding, char* name, int current_recursion_level, int attachment_count, int filecount) +MIME_element* MIME_element_add(struct MIME_element* parent, RIPMIME_output *unpack_metadata, char* filename, char* content_type_string, char* content_transfer_encoding, char* name, int current_recursion_level, int attachment_count, int filecount) { MIME_element *cur = malloc(sizeof(MIME_element)); int fullpath_len = 0; @@ -96,6 +96,11 @@ static void dup_free(char *s) free(s); } +void MIME_element_deactivate(MIME_element* cur, RIPMIME_output *unpack_metadata) +{ + MIME_element_free(cur); +} + void MIME_element_free (MIME_element* cur) { if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:start\n",FL,__func__); @@ -239,13 +244,13 @@ static void copyFILEcontent(FILE* src, FILE* dst) } } -void write_FS_file (MIME_element* cur, RIPMIME_output *unpack_metadata, int method) +void write_FS_file(MIME_element* cur, int rename_method) { char * wr_filename = NULL; FILE* wf = NULL; int fn_l = 0; - MIME_test_uniquename(cur->directory, cur->filename, method); + MIME_test_uniquename(cur->directory, cur->filename, rename_method); fn_l = strlen(cur->directory) + strlen(cur->filename) + sizeof(char) * 2; wr_filename = malloc(fn_l); snprintf(wr_filename,fn_l,"%s/%s",cur->directory,cur->filename); @@ -269,6 +274,17 @@ void write_FS_file (MIME_element* cur, RIPMIME_output *unpack_metadata, int meth fclose(wf); } +void write_all_to_FS_files(RIPMIME_output *unpack_metadata, int rename_method) +{ + int i = 0; + + for (; i < all_MIME_elements.mime_count; i++); + { + MIME_element* m = getItem(all_MIME_elements.mime_arr, i); + write_FS_file(m, rename_method); + } +} + //------Dynamic array function definitions------ // Array initialization void arrayInit(dynamic_array** arr_ptr) diff --git a/mime_element.h b/mime_element.h index 73e1f5e..f896cec 100644 --- a/mime_element.h +++ b/mime_element.h @@ -7,6 +7,7 @@ #define RIPMIME_UNPACK_MODE_TO_DIRECTORY 0 #define RIPMIME_UNPACK_MODE_COUNT_FILES 1 #define RIPMIME_UNPACK_MODE_LIST_FILES 2 +#define RIPMIME_UNPACK_MODE_EXTRACT_ONE 3 #define _MIME_RENAME_METHOD_INFIX 1 #define _MIME_RENAME_METHOD_PREFIX 2 @@ -49,8 +50,9 @@ typedef struct { extern all_MIME_elements_s all_MIME_elements; void all_MIME_elements_init (void); -MIME_element* MIME_element_add (MIME_element* parent, RIPMIME_output *unpack_metadata, char* filename, char* content_type_string, char* content_transfer_encoding, char* name, int current_recursion_level, int attachment_count, int filecount); -void MIME_element_free (MIME_element* cur); +MIME_element* MIME_element_add (struct MIME_element* parent, RIPMIME_output *unpack_metadata, char* filename, char* content_type_string, char* content_transfer_encoding, char* name, int current_recursion_level, int attachment_count, int filecount); +// void MIME_element_free (MIME_element* cur); +void MIME_element_deactivate (MIME_element* cur, RIPMIME_output *unpack_metadata); void printArray(dynamic_array* container); void freeArray(dynamic_array* container); diff --git a/mime_headers.c b/mime_headers.c index b26aaf4..2cb4415 100644 --- a/mime_headers.c +++ b/mime_headers.c @@ -747,7 +747,7 @@ int MIMEH_save_doubleCR( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct break; } } - MIME_element_free(cur_mime); + MIME_element_deactivate(cur_mime, unpack_metadata); return 0; } diff --git a/ripOLE/ole.c b/ripOLE/ole.c index 9576da3..5222634 100644 --- a/ripOLE/ole.c +++ b/ripOLE/ole.c @@ -1519,7 +1519,7 @@ int OLE_store_stream( struct OLE_object *ole, char *stream_name, RIPMIME_output { LOGGER_log("%s:%d:%s:WARNING: Only wrote %d of %d bytes to file %s",FL,__func__,written_bytes,stream_size,cur_mime->fullpath); } - MIME_element_free (cur_mime); + MIME_element_deactivate (cur_mime, unpack_metadata); if ((OLE_VNORMAL(ole->verbose))&&(ole->filename_report_fn != NULL)) { diff --git a/ripOLE/olestream-unwrap.c b/ripOLE/olestream-unwrap.c index c1fa377..f229e62 100644 --- a/ripOLE/olestream-unwrap.c +++ b/ripOLE/olestream-unwrap.c @@ -167,7 +167,7 @@ int OLEUNWRAP_save_stream( struct OLEUNWRAP_object *oleuw, char *fname, RIPMIME_ LOGGER_log("%s:%d:%s:WARNING: Only wrote %d of %d bytes to file %s\n",FL,__func__, write_count, bytes, cur_mime->fullpath ); } - MIME_element_free (cur_mime); + MIME_element_deactivate (cur_mime, unpack_metadata); DUW LOGGER_log("%s:%d:%s:DEBUG: Done saving '%s'",FL,__func__, fname); return result; diff --git a/ripmime.c b/ripmime.c index f55a44f..b29c5d3 100644 --- a/ripmime.c +++ b/ripmime.c @@ -43,6 +43,7 @@ struct RIPMIME_globals int quiet; int verbose_defects; int verbose; + int rename_method; }; //-b [blankzone file name] : Dump the contents of the MIME blankzone to 'filename' @@ -322,28 +323,28 @@ int RIPMIME_parse_parameters (struct RIPMIME_globals *glb, int argc, char **argv } else if (strncmp (&(argv[i][2]), "prefix", 6) == 0) { - MIME_set_renamemethod (_MIME_RENAME_METHOD_PREFIX); + glb->rename_method = _MIME_RENAME_METHOD_PREFIX; } else if (strncmp (&(argv[i][2]), "postfix", 7) == 0) { - MIME_set_renamemethod (_MIME_RENAME_METHOD_POSTFIX); + glb->rename_method = _MIME_RENAME_METHOD_POSTFIX; } else if (strncmp (&(argv[i][2]), "infix", 5) == 0) { - MIME_set_renamemethod (_MIME_RENAME_METHOD_INFIX); + glb->rename_method = _MIME_RENAME_METHOD_INFIX; } /* New methods of generating file name: add counter and random number*/ else if (strncmp (&(argv[i][2]), "randprefix", 10) == 0) { - MIME_set_renamemethod (_MIME_RENAME_METHOD_RANDPREFIX); + glb->rename_method = _MIME_RENAME_METHOD_RANDPREFIX; } else if (strncmp (&(argv[i][2]), "randpostfix", 11) == 0) { - MIME_set_renamemethod (_MIME_RENAME_METHOD_RANDPOSTFIX); + glb->rename_method = _MIME_RENAME_METHOD_RANDPOSTFIX; } else if (strncmp (&(argv[i][2]), "randinfix", 9) == 0) { - MIME_set_renamemethod (_MIME_RENAME_METHOD_RANDINFIX); + glb->rename_method = _MIME_RENAME_METHOD_RANDINFIX; } else if (strncmp (&(argv[i][2]), "overwrite", 9) == 0) { @@ -511,7 +512,6 @@ int RIPMIME_parse_parameters (struct RIPMIME_globals *glb, int argc, char **argv } - /*-----------------------------------------------------------------\ Function Name : RIPMIME_init Returns Type : int @@ -538,7 +538,7 @@ int RIPMIME_init (struct RIPMIME_globals *glb, RIPMIME_output *o) glb->quiet = 0; glb->verbose_defects = 0; glb->verbose = 0; - + glb->rename_method = _MIME_RENAME_METHOD_INFIX; return 0; } @@ -594,7 +594,7 @@ int RIPMIME_unpack_single( struct RIPMIME_globals *glb, char *fname ) // do any last minute things - MIME_close (); + MIME_close (glb->output, glb->rename_method); return result; } @@ -721,9 +721,9 @@ int main (int argc, char **argv) MIME_set_uniquenames (1); MIME_set_paranoid (0); MIME_set_header_longsearch(1); // 20040310-0117:PLD - Added by default as it seems stable, use --disable-qmail-bounce to turn off - MIME_set_renamemethod (_MIME_RENAME_METHOD_INFIX); RIPMIME_parse_parameters (&glb, argc, argv); + MIME_set_renamemethod (glb.rename_method); // if our input filename wasn't specified, then we better let the user know! if (!glb.inputfile) diff --git a/tnef/tnef.c b/tnef/tnef.c index 9c23808..0b73612 100644 --- a/tnef/tnef.c +++ b/tnef/tnef.c @@ -727,8 +727,6 @@ Purpose: Decodes a given TNEF encoded file int TNEF_main( char *filename, char *file_dir ) { FILE *fp; - uint8 *tnef_stream; - int size, nread; if (TNEF_DEBUG) LOGGER_log("%s:%d:%s:DEBUG: Start, decoding %s\n",FL,__func__, filename); diff --git a/uuencode.c b/uuencode.c index 3ca2438..e63f95b 100644 --- a/uuencode.c +++ b/uuencode.c @@ -603,7 +603,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *out_filename, int decode_whole_file } } - MIME_element_free(cur_mime); + MIME_element_deactivate(cur_mime, unpack_metadata); // Call our reporting function, else, if no function is defined, use the default // standard call From b3163644cc9270e1cc1470ddbaccaedd8174c807 Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Tue, 29 Apr 2025 11:38:07 +0300 Subject: [PATCH 50/80] Refactor `MIME_element` to in-memory output file operations --- mime.c | 11 ++++++----- mime_element.c | 37 ++++++++++++++++++++++++++----------- mime_element.h | 21 ++++++++++++++++----- mime_headers.c | 2 +- ripOLE/ole.c | 2 +- ripOLE/olestream-unwrap.c | 2 +- ripmime.c | 2 +- uuencode.c | 2 +- 8 files changed, 53 insertions(+), 26 deletions(-) diff --git a/mime.c b/mime.c index 0113222..4a148a8 100644 --- a/mime.c +++ b/mime.c @@ -1127,7 +1127,7 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Start\n",FL,__func__); - cur_mime = MIME_element_add (NULL, unpack_metadata, hinfo->filename, hinfo->content_type_string, hinfo->content_transfer_encoding_string, hinfo->name, hinfo->current_recursion_level, glb.attachment_count, glb.filecount); + cur_mime = MIME_element_add (NULL, unpack_metadata, hinfo->filename, hinfo->content_type_string, hinfo->content_transfer_encoding_string, hinfo->name, hinfo->current_recursion_level, glb.attachment_count, glb.filecount, __func__); while ((readcount=FFGET_raw(f, (unsigned char *) buffer,bufsize)) > 0) { @@ -1225,7 +1225,7 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding TEXT [encoding=%d] to %s\n",FL,__func__, hinfo->content_transfer_encoding, hinfo->filename); - cur_mime = MIME_element_add (NULL, unpack_metadata, hinfo->filename, hinfo->content_type_string, hinfo->content_transfer_encoding_string, hinfo->name, hinfo->current_recursion_level, glb.attachment_count, glb.filecount); + cur_mime = MIME_element_add (NULL, unpack_metadata, hinfo->filename, hinfo->content_type_string, hinfo->content_transfer_encoding_string, hinfo->name, hinfo->current_recursion_level, glb.attachment_count, glb.filecount, __func__); if (!f) { /** If we cannot open the file for reading, leave an error and return -1 **/ @@ -1399,7 +1399,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: attempting to decode '%s'", FL,__func__, hinfo->filename); - cur_mime = MIME_element_add (NULL, unpack_metadata, hinfo->filename, hinfo->content_type_string, hinfo->content_transfer_encoding_string, hinfo->name, hinfo->current_recursion_level, glb.attachment_count, glb.filecount); + cur_mime = MIME_element_add (NULL, unpack_metadata, hinfo->filename, hinfo->content_type_string, hinfo->content_transfer_encoding_string, hinfo->name, hinfo->current_recursion_level, glb.attachment_count, glb.filecount, __func__); if (cur_mime->f == NULL) { return -1; @@ -3174,7 +3174,7 @@ int MIME_unpack( RIPMIME_output *unpack_metadata, char *mpname, int current_recu void MIME_close(RIPMIME_output *unpack_metadata, int rename_method) { - if (unpack_metadata->unpack_mode == RIPMIME_UNPACK_MODE_EXTRACT_ONE) + if (unpack_metadata->unpack_mode == RIPMIME_UNPACK_MODE_IN_MEMORY) write_all_to_FS_files(unpack_metadata, rename_method); if (MIME_DNORMAL) { @@ -3182,6 +3182,7 @@ void MIME_close(RIPMIME_output *unpack_metadata, int rename_method) printArray(all_MIME_elements.mime_arr); } - freeArray(all_MIME_elements.mime_arr); + freeArray(all_MIME_elements.mime_arr, unpack_metadata); } +/* EOF */ diff --git a/mime_element.c b/mime_element.c index 1173295..67636f7 100644 --- a/mime_element.c +++ b/mime_element.c @@ -57,7 +57,7 @@ static char * dup_ini(char* s) return (s != NULL) ? strdup(s) : "\0"; } -MIME_element* MIME_element_add(struct MIME_element* parent, RIPMIME_output *unpack_metadata, char* filename, char* content_type_string, char* content_transfer_encoding, char* name, int current_recursion_level, int attachment_count, int filecount) +MIME_element* MIME_element_add(struct MIME_element* parent, RIPMIME_output *unpack_metadata, char* filename, char* content_type_string, char* content_transfer_encoding, char* name, int current_recursion_level, int attachment_count, int filecount, char* func) { MIME_element *cur = malloc(sizeof(MIME_element)); int fullpath_len = 0; @@ -76,15 +76,19 @@ MIME_element* MIME_element_add(struct MIME_element* parent, RIPMIME_output *unpa cur->fullpath = (char*)malloc(fullpath_len); snprintf(cur->fullpath,fullpath_len,"%s/%s",unpack_metadata->dir,filename); - cur->f = fopen(cur->fullpath,"wb"); + if (unpack_metadata->unpack_mode == RIPMIME_UNPACK_MODE_TO_DIRECTORY) + cur->f = fopen(cur->fullpath,"wb"); + else + cur->f = open_memstream (&cur->mem_filearea, &cur->mem_filearea_l); + if (cur->f == NULL) { - LOGGER_log("%s:%d:%s:ERROR: cannot open %s for writing",FL,__func__,cur->fullpath); + LOGGER_log("%s:%d:%s:ERROR: cannot open %s for writing",FL,func,cur->fullpath); return cur; } if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding [encoding=%d] to %s\n",FL,__func__, content_transfer_encoding, cur->fullpath); - if (unpack_metadata != NULL && unpack_metadata->unpack_mode == RIPMIME_UNPACK_MODE_LIST_FILES && cur->f != NULL) { + if (unpack_metadata != NULL && unpack_metadata->unpack_mode == RIPMIME_UNPACK_MODE_LIST_MIME && cur->f != NULL) { fprintf (stdout, "%d|%d|%d|%d|%s|%s\n", all_MIME_elements.mime_count, attachment_count, filecount, current_recursion_level, cur->content_type_string, cur->filename); } return cur; @@ -96,15 +100,15 @@ static void dup_free(char *s) free(s); } -void MIME_element_deactivate(MIME_element* cur, RIPMIME_output *unpack_metadata) -{ - MIME_element_free(cur); -} - void MIME_element_free (MIME_element* cur) { if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:start\n",FL,__func__); + if (cur == NULL) { + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:NULL, nothing to free\n",FL,__func__); + return; + } + if (cur->f != NULL) { fclose(cur->f); } @@ -115,6 +119,13 @@ void MIME_element_free (MIME_element* cur) dup_free(cur->name); free(cur); + cur == NULL; +} + +void MIME_element_deactivate(MIME_element* cur, RIPMIME_output *unpack_metadata) +{ + if (unpack_metadata->unpack_mode == RIPMIME_UNPACK_MODE_TO_DIRECTORY) + MIME_element_free(cur); } static inline int get_random_value(void) { @@ -371,9 +382,13 @@ void printArray(dynamic_array* container) } // Freeing the memory allocated to the array -void freeArray(dynamic_array* container) +void freeArray(dynamic_array* container, RIPMIME_output *unpack_metadata) { + if (unpack_metadata->unpack_mode != RIPMIME_UNPACK_MODE_TO_DIRECTORY) + for (int i = 0; i < container->size; i++) { + MIME_element_free(container->array[i]); + } free(container->array); free(container); } -/*----------END OF MIME.c------------*/ +/*---------- EOF -----------*/ diff --git a/mime_element.h b/mime_element.h index f896cec..7c89c57 100644 --- a/mime_element.h +++ b/mime_element.h @@ -5,9 +5,8 @@ /* unpack modes */ #define RIPMIME_UNPACK_MODE_TO_DIRECTORY 0 -#define RIPMIME_UNPACK_MODE_COUNT_FILES 1 -#define RIPMIME_UNPACK_MODE_LIST_FILES 2 -#define RIPMIME_UNPACK_MODE_EXTRACT_ONE 3 +#define RIPMIME_UNPACK_MODE_IN_MEMORY 1 +#define RIPMIME_UNPACK_MODE_LIST_MIME 2 #define _MIME_RENAME_METHOD_INFIX 1 #define _MIME_RENAME_METHOD_PREFIX 2 @@ -34,6 +33,8 @@ typedef struct { char* content_type_string; char* content_transfer_encoding; char* name; + char* mem_filearea; + size_t mem_filearea_l; } MIME_element; typedef struct { @@ -50,11 +51,21 @@ typedef struct { extern all_MIME_elements_s all_MIME_elements; void all_MIME_elements_init (void); -MIME_element* MIME_element_add (struct MIME_element* parent, RIPMIME_output *unpack_metadata, char* filename, char* content_type_string, char* content_transfer_encoding, char* name, int current_recursion_level, int attachment_count, int filecount); +MIME_element* MIME_element_add ( + struct MIME_element* parent, + RIPMIME_output *unpack_metadata, + char* filename, + char* content_type_string, + char* content_transfer_encoding, + char* name, + int current_recursion_level, + int attachment_count, + int filecount, + char* func); // void MIME_element_free (MIME_element* cur); void MIME_element_deactivate (MIME_element* cur, RIPMIME_output *unpack_metadata); void printArray(dynamic_array* container); -void freeArray(dynamic_array* container); +void freeArray(dynamic_array* container, RIPMIME_output *unpack_metadata); int MIME_test_uniquename( char *path, char *fname, int method ); diff --git a/mime_headers.c b/mime_headers.c index 2cb4415..669d131 100644 --- a/mime_headers.c +++ b/mime_headers.c @@ -735,7 +735,7 @@ int MIMEH_save_doubleCR( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct glb.doubleCR_count++; snprintf(glb.doubleCRname,_MIMEH_STRLEN_MAX,"%s/%s_doubleCR.%d_", unpack_metadata->dir, hinfo->filename, glb.doubleCR_count); - cur_mime = MIME_element_add (NULL, unpack_metadata, glb.doubleCRname, "doubleCR", NULL, "doubleCR", hinfo->current_recursion_level + 1, 0, 0); + cur_mime = MIME_element_add (NULL, unpack_metadata, glb.doubleCRname, "doubleCR", NULL, "doubleCR", hinfo->current_recursion_level + 1, 0, 0, __func__); if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIME_save_doubleCR:DEBUG: Saving DoubleCR header: %s\n", FL,glb.doubleCRname); while (1) diff --git a/ripOLE/ole.c b/ripOLE/ole.c index 5222634..02462db 100644 --- a/ripOLE/ole.c +++ b/ripOLE/ole.c @@ -1512,7 +1512,7 @@ int OLE_set_filename_report_fn( struct OLE_object *ole, int (*ptr_to_fn)(char *) \------------------------------------------------------------------*/ int OLE_store_stream( struct OLE_object *ole, char *stream_name, RIPMIME_output *unpack_metadata, char *stream, size_t stream_size ) { - MIME_element* cur_mime = MIME_element_add (NULL, unpack_metadata, stream_name, "OLE", "OLE", "OLE", 0, 1, 0); + MIME_element* cur_mime = MIME_element_add (NULL, unpack_metadata, stream_name, "OLE", "OLE", "OLE", 0, 1, 0, __func__); size_t written_bytes = fwrite( stream, 1, stream_size, cur_mime->f ); if (written_bytes != stream_size) diff --git a/ripOLE/olestream-unwrap.c b/ripOLE/olestream-unwrap.c index f229e62..5b5b2b7 100644 --- a/ripOLE/olestream-unwrap.c +++ b/ripOLE/olestream-unwrap.c @@ -159,7 +159,7 @@ int OLEUNWRAP_save_stream( struct OLEUNWRAP_object *oleuw, char *fname, RIPMIME_ ,bytes ); - cur_mime = MIME_element_add (NULL, unpack_metadata, fname, "OLE", "OLE", "OLE", 0, 1, 0); + cur_mime = MIME_element_add (NULL, unpack_metadata, fname, "OLE", "OLE", "OLE", 0, 1, 0, __func__); write_count = fwrite( stream, 1, bytes, cur_mime->f ); if (write_count != bytes) diff --git a/ripmime.c b/ripmime.c index b29c5d3..75ae00b 100644 --- a/ripmime.c +++ b/ripmime.c @@ -266,7 +266,7 @@ int RIPMIME_parse_parameters (struct RIPMIME_globals *glb, int argc, char **argv #endif case 'l': - glb->output->unpack_mode = RIPMIME_UNPACK_MODE_LIST_FILES; + glb->output->unpack_mode = RIPMIME_UNPACK_MODE_LIST_MIME; break; case 'v': diff --git a/uuencode.c b/uuencode.c index e63f95b..423780c 100644 --- a/uuencode.c +++ b/uuencode.c @@ -494,7 +494,7 @@ int UUENCODE_decode_uu( FFGET_FILE *f, char *out_filename, int decode_whole_file if (UUENCODE_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Filename = (%s)\n", FL,__func__, fn); - cur_mime = MIME_element_add (NULL, unpack_metadata, fn, hinfo->content_type_string, hinfo->content_transfer_encoding_string, hinfo->name, hinfo->current_recursion_level, 1, filecount); + cur_mime = MIME_element_add (NULL, unpack_metadata, fn, hinfo->content_type_string, hinfo->content_transfer_encoding_string, hinfo->name, hinfo->current_recursion_level, 1, filecount, __func__); // Allocate the write buffer. By using the write buffer we gain an additional 10% in performance // due to the lack of function call (fwrite) overheads From 30665344fe14c7cc0a89ffb770f9f6a19f147fb8 Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Tue, 29 Apr 2025 11:56:35 +0300 Subject: [PATCH 51/80] Unify naming --- mime.c | 56 ++++++++++++++++++++++++++++---------------------------- mime.h | 4 ++-- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/mime.c b/mime.c index 4a148a8..c47102e 100644 --- a/mime.c +++ b/mime.c @@ -60,8 +60,8 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int current_recursion_level, struct SS_object *ss ); -int MIME_unpack_single( RIPMIME_output *unpack_metadata, char *mpname, int current_recursion_level, struct SS_object *ss ); -int MIME_unpack_single_fp( RIPMIME_output *unpack_metadata, FILE *fi, int current_recursion_level, struct SS_object *ss ); +int MIME_unpack_single_diskfile( RIPMIME_output *unpack_metadata, char *mpname, int current_recursion_level, struct SS_object *ss ); +int MIME_unpack_single_file( RIPMIME_output *unpack_metadata, FILE *fi, int current_recursion_level, struct SS_object *ss ); int MIME_unpack_mailbox( RIPMIME_output *unpack_metadata, char *mpname, int current_recursion_level, struct SS_object *ss ); int MIME_handle_multipart( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ); int MIME_handle_rfc822( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ); @@ -2245,7 +2245,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct if (MIME_is_diskfile_RFC822(hinfo->scratch) > 0 ) { // 20040305-1304:PLD: unpack the file, propagate result upwards - result = MIME_unpack_single( unpack_metadata, hinfo->scratch, (hinfo->current_recursion_level+ 1),ss ); + result = MIME_unpack_single_diskfile( unpack_metadata, hinfo->scratch, (hinfo->current_recursion_level+ 1),ss ); } } break; @@ -2312,7 +2312,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct snprintf(hinfo->scratch,sizeof(hinfo->scratch),"%s/%s",unpack_metadata->dir,hinfo->filename); // 20040305-1304:PLD: unpack the file, propagate result upwards - result = MIME_unpack_single( unpack_metadata, hinfo->scratch, (hinfo->current_recursion_level+ 1),ss ); + result = MIME_unpack_single_diskfile( unpack_metadata, hinfo->scratch, (hinfo->current_recursion_level+ 1),ss ); } } // Decode MHT files } // If result != -1 @@ -2407,12 +2407,12 @@ int MIME_handle_multipart( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struc fn = malloc(fn_l); snprintf(fn,fn_l,"%s/%s",unpack_metadata->dir,h->filename); - // Because we're calling MIME_unpack_single again [ie, recursively calling it + // Because we're calling MIME_unpack_single_diskfile again [ie, recursively calling it // we need to now adjust the input-filename so that it correctly is prefixed // with the directory we unpacked to. - //result = MIME_unpack_single( unpackdir, fn, current_recursion_level + 1, ss); - result = MIME_unpack_single( unpack_metadata, fn, current_recursion_level, ss ); + //result = MIME_unpack_single_diskfile( unpackdir, fn, current_recursion_level + 1, ss); + result = MIME_unpack_single_diskfile( unpack_metadata, fn, current_recursion_level, ss ); free(fn); } @@ -2480,12 +2480,12 @@ int MIME_handle_rfc822( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M fn = malloc(fn_l); snprintf(fn,fn_l,"%s/%s",unpack_metadata->dir,h->filename); - /** Because we're calling MIME_unpack_single again [ie, recursively calling it + /** Because we're calling MIME_unpack_single_diskfile again [ie, recursively calling it we need to now adjust the input-filename so that it correctly is prefixed with the directory we unpacked to. **/ if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Now attempting to extract contents of '%s'",FL,__func__,h->filename); - result = MIME_unpack_single( unpack_metadata, fn, current_recursion_level, ss ); + result = MIME_unpack_single_diskfile( unpack_metadata, fn, current_recursion_level, ss ); free(fn); result = 0; } @@ -2525,9 +2525,9 @@ int MIME_handle_plain( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MI snprintf(h->scratch,sizeof(h->scratch),"%s/%s",unpack_metadata->dir,h->filename); if (MIME_is_diskfile_RFC822(h->scratch)==1) { - /** If the file is RFC822, then decode it using MIME_unpack_single() **/ + /** If the file is RFC822, then decode it using MIME_unpack_single_diskfile() **/ if (glb.header_longsearch != 0) MIMEH_set_header_longsearch(glb.header_longsearch); - result = MIME_unpack_single( unpack_metadata, h->scratch, current_recursion_level, ss ); + result = MIME_unpack_single_diskfile( unpack_metadata, h->scratch, current_recursion_level, ss ); if (glb.header_longsearch != 0) MIMEH_set_header_longsearch(0); } } @@ -2723,8 +2723,8 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M { snprintf(scratch,sizeof(scratch),"%s/%s",unpackdir, h->filename); snprintf(h->filename,sizeof(h->filename),"%s",scratch); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Now calling MIME_unpack_single() on the file '%s' for our RFC822 decode operation.",FL,__func__, scratch); - //result = MIME_unpack_single( unpackdir, h->filename, current_recursion_level + 1, ss); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Now calling MIME_unpack_single_diskfile() on the file '%s' for our RFC822 decode operation.",FL,__func__, scratch); + //result = MIME_unpack_single_diskfile( unpackdir, h->filename, current_recursion_level + 1, ss); result = MIME_unpack( unpackdir, h->filename, current_recursion_level + 1 ); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Unpack result = %d", FL,__func__, result); result = 0; @@ -2768,13 +2768,13 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M int fn_l = strlen(unpack_metadata->dir) + strlen(h->filename) + sizeof(char) * 2; if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Now running ripMIME over decoded RFC822 message...\n",FL,__func__); - // Because we're calling MIME_unpack_single again [ie, recursively calling it + // Because we're calling MIME_unpack_single_diskfile again [ie, recursively calling it // we need to now adjust the input-filename so that it correctly is prefixed // with the directory we unpacked to. fn = malloc(fn_l); snprintf(fn,fn_l,"%s/%s",unpack_metadata->dir,h->filename); - //result = MIME_unpack_single( unpackdir, fn, current_recursion_level + 1, ss); - result = MIME_unpack_single( unpack_metadata, fn, current_recursion_level,ss ); + //result = MIME_unpack_single_diskfile( unpackdir, fn, current_recursion_level + 1, ss); + result = MIME_unpack_single_diskfile( unpack_metadata, fn, current_recursion_level,ss ); free(fn); } @@ -2804,8 +2804,8 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Testing '%s' for email type",FL,__func__,mime_fname); if (MIME_is_diskfile_RFC822(mime_fname)) { - //MIME_unpack_single( unpackdir, mime_fname, (hinfo->current_recursion_level+ 1), ss); - MIME_unpack_single( unpack_metadata, mime_fname, current_recursion_level+ 1,ss); + //MIME_unpack_single_diskfile( unpackdir, mime_fname, (hinfo->current_recursion_level+ 1), ss); + MIME_unpack_single_diskfile( unpack_metadata, mime_fname, current_recursion_level+ 1,ss); } free(mime_fname); } @@ -2880,9 +2880,9 @@ int MIME_unpack_mailbox( RIPMIME_output *unpack_metadata, char *mpname, int curr // Close the mailpack fclose(fo); // Now, decode the mailpack - //MIME_unpack_single(unpackdir, fname, current_recursion_level, ss); + //MIME_unpack_single_diskfile(unpackdir, fname, current_recursion_level, ss); // 20040317-2358:PLD - MIME_unpack_single(unpack_metadata, fname, current_recursion_level ,ss ); + MIME_unpack_single_diskfile(unpack_metadata, fname, current_recursion_level ,ss ); // Remove the now unpacked mailpack result = remove(fname); if (result == -1) @@ -2925,9 +2925,9 @@ int MIME_unpack_mailbox( RIPMIME_output *unpack_metadata, char *mpname, int curr fclose(fo); // Now, decode the mailpack - //MIME_unpack_single(unpackdir, fname, current_recursion_level, ss); + //MIME_unpack_single_diskfile(unpackdir, fname, current_recursion_level, ss); // 20040317-2358:PLD - MIME_unpack_single(unpack_metadata, fname, current_recursion_level , ss ); + MIME_unpack_single_diskfile(unpack_metadata, fname, current_recursion_level , ss ); // Remove the now unpacked mailpack result = remove(fname); if (result == -1) @@ -2938,7 +2938,7 @@ int MIME_unpack_mailbox( RIPMIME_output *unpack_metadata, char *mpname, int curr } /*-----------------------------------------------------------------\ - Function Name : MIME_unpack_single + Function Name : MIME_unpack_single_diskfile Returns Type : int ----Parameter List 1. RIPMIME_output *unpack_metadata, @@ -2954,7 +2954,7 @@ int MIME_unpack_mailbox( RIPMIME_output *unpack_metadata, char *mpname, int curr Changes: \------------------------------------------------------------------*/ -int MIME_unpack_single( RIPMIME_output *unpack_metadata, char *mpname, int current_recursion_level, struct SS_object *ss ) +int MIME_unpack_single_diskfile( RIPMIME_output *unpack_metadata, char *mpname, int current_recursion_level, struct SS_object *ss ) { FILE *fi; /* Pointer for the MIME file we're going to be going through */ int result = 0; @@ -2990,7 +2990,7 @@ int MIME_unpack_single( RIPMIME_output *unpack_metadata, char *mpname, int curre return -1; } // 20040317-2359:PLD - result = MIME_unpack_single_fp(unpack_metadata,fi,current_recursion_level , ss); + result = MIME_unpack_single_file(unpack_metadata,fi,current_recursion_level , ss); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: result = %d, recursion = %d, filename = '%s'", FL,__func__, result, current_recursion_level, mpname ); if ((current_recursion_level > 1)&&(result == 241)) result = 0; fclose(fi); @@ -2998,7 +2998,7 @@ int MIME_unpack_single( RIPMIME_output *unpack_metadata, char *mpname, int curre } /*------------------------------------------------------------------------ -Procedure: MIME_unpack_single ID:1 +Procedure: MIME_unpack_single_diskfile ID:1 Purpose: Decodes a single mailpack file (as apposed to mailbox format) into its possible attachments and text bodies Input: RIPMIME_output *unpack_metadata: Directory to unpack the attachments to @@ -3007,7 +3007,7 @@ int current_recusion_level: Level of recursion we're currently at. Output: Errors: ------------------------------------------------------------------------*/ -int MIME_unpack_single_fp( RIPMIME_output *unpack_metadata, FILE *fi, int current_recursion_level, struct SS_object *ss ) +int MIME_unpack_single_file( RIPMIME_output *unpack_metadata, FILE *fi, int current_recursion_level, struct SS_object *ss ) { struct MIMEH_header_info h; int result = 0; @@ -3122,7 +3122,7 @@ int MIME_unpack( RIPMIME_output *unpack_metadata, char *mpname, int current_recu else { if (MIME_DNORMAL) LOGGER_log("%s:%d:%s: Unpacking standard mailpack",FL,__func__,mpname,unpack_metadata->dir,current_recursion_level); - result = MIME_unpack_single( unpack_metadata, mpname, (current_recursion_level + 1), &ss ); + result = MIME_unpack_single_diskfile( unpack_metadata, mpname, (current_recursion_level + 1), &ss ); } if (glb.no_nameless) diff --git a/mime.h b/mime.h index 79a86b4..3925b9d 100644 --- a/mime.h +++ b/mime.h @@ -52,8 +52,8 @@ int MIME_version( void ); size_t MIME_read_raw( char *src_mpname, char *dest_mpname, size_t rw_buffer_size ); int MIME_read( char *mpname ); /* returns filesize in KB */ int MIME_unpack( RIPMIME_output *unpack_metadata, char *mpname, int current_recusion_level ); -//int MIME_unpack_single( RIPMIME_output *unpack_metadata, char *mpname, int current_recusion_level ); -//int MIME_unpack_single_fp( RIPMIME_output *unpack_metadata, FILE *fi, int current_recusion_level ); +//int MIME_unpack_single_disk_file( RIPMIME_output *unpack_metadata, char *mpname, int current_recusion_level ); +//int MIME_unpack_single_disk_file_fp( RIPMIME_output *unpack_metadata, FILE *fi, int current_recusion_level ); //int MIME_unpack_mailbox( RIPMIME_output *unpack_metadata, char *mpname, int current_recursion_level ); int MIME_insert_Xheader( char *fname, char *xheader ); int MIME_set_blankfileprefix( char *prefix ); From b2d0ed8279f705d75ef17ab32059c932c3f99643 Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Tue, 29 Apr 2025 12:03:13 +0300 Subject: [PATCH 52/80] Rename and unify input file variable --- mime.c | 96 +++++++++++++++++++++++++++++----------------------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/mime.c b/mime.c index c47102e..71f1dcc 100644 --- a/mime.c +++ b/mime.c @@ -59,12 +59,12 @@ #include "logger.h" -int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int current_recursion_level, struct SS_object *ss ); +int MIME_unpack_stage2( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int current_recursion_level, struct SS_object *ss ); int MIME_unpack_single_diskfile( RIPMIME_output *unpack_metadata, char *mpname, int current_recursion_level, struct SS_object *ss ); int MIME_unpack_single_file( RIPMIME_output *unpack_metadata, FILE *fi, int current_recursion_level, struct SS_object *ss ); int MIME_unpack_mailbox( RIPMIME_output *unpack_metadata, char *mpname, int current_recursion_level, struct SS_object *ss ); -int MIME_handle_multipart( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ); -int MIME_handle_rfc822( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ); +int MIME_handle_multipart( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ); +int MIME_handle_rfc822( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ); // Predefined filenames #define MIME_BLANKZONE_FILENAME_DEFAULT "_blankzone_" @@ -2050,13 +2050,13 @@ int MIME_generate_multiple_hardlink_filenames(struct MIMEH_header_info *hinfo, R /*------------------------------------------------------------------------ Procedure: MIME_decode_encoding ID:1 Purpose: Based on the contents of hinfo, this function will call the -required function needed to decode the contents of the file -which is contained within the MIME structure + required function needed to decode the contents of the file + which is contained within the MIME structure Input: Output: Errors: ------------------------------------------------------------------------*/ -int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, struct SS_object *ss ) +int MIME_decode_encoding( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, struct SS_object *ss ) { int keep = 1; int result = -1; @@ -2170,7 +2170,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct { case _CTRANS_ENCODING_B64: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding BASE64 format\n",FL,__func__); - result = MIME_decode_64(f, unpack_metadata, hinfo); + result = MIME_decode_64(input_f, unpack_metadata, hinfo); switch (result) { case MIME_ERROR_B64_INPUT_STREAM_EOF: break; @@ -2178,7 +2178,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct result = 0; break; case 0: - result = MIME_decode_64_cleanup(f, unpack_metadata, hinfo); + result = MIME_decode_64_cleanup(input_f, unpack_metadata, hinfo); break; default: break; @@ -2186,26 +2186,26 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct break; case _CTRANS_ENCODING_7BIT: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding 7BIT format\n",FL,__func__); - result = MIME_decode_text(f, unpack_metadata, hinfo, keep); + result = MIME_decode_text(input_f, unpack_metadata, hinfo, keep); break; case _CTRANS_ENCODING_8BIT: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding 8BIT format\n",FL,__func__); - result = MIME_decode_text(f, unpack_metadata, hinfo, keep); + result = MIME_decode_text(input_f, unpack_metadata, hinfo, keep); break; case _CTRANS_ENCODING_BINARY: case _CTRANS_ENCODING_RAW: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding RAW format\n",FL,__func__); - result = MIME_decode_raw(f, unpack_metadata, hinfo, keep); + result = MIME_decode_raw(input_f, unpack_metadata, hinfo, keep); break; case _CTRANS_ENCODING_QP: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding Quoted-Printable format\n",FL,__func__); - result = MIME_decode_text(f, unpack_metadata, hinfo, keep); + result = MIME_decode_text(input_f, unpack_metadata, hinfo, keep); break; case _CTRANS_ENCODING_UUENCODE: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UUENCODED format\n",FL,__func__); // Added as a test - remove if we can get this to work in a better way snprintf(hinfo->uudec_name,sizeof(hinfo->uudec_name),"%s",hinfo->filename); - result = UUENCODE_decode_uu(f, hinfo->uudec_name, 0, keep, unpack_metadata, hinfo ); + result = UUENCODE_decode_uu(input_f, hinfo->uudec_name, 0, keep, unpack_metadata, hinfo ); glb.attachment_count += result; // Because this is a file-count, it's not really an 'error result' as such, so, set the // return code back to 0! @@ -2215,17 +2215,17 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct switch (hinfo->content_disposition) { case _CDISPOSITION_FORMDATA: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UNKNOWN format of FORMDATA disposition\n",FL,__func__); - result = MIME_decode_raw(f, unpack_metadata, hinfo, keep); + result = MIME_decode_raw(input_f, unpack_metadata, hinfo, keep); break; default: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UNKNOWN format\n",FL,__func__); - result = MIME_decode_text(f, unpack_metadata, hinfo, keep); + result = MIME_decode_text(input_f, unpack_metadata, hinfo, keep); } if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: UNKNOWN Decode completed, result = %d\n",FL,__func__,result); break; case _CTRANS_ENCODING_UNSPECIFIED: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UNSPECIFIED format\n",FL,__func__); - result = MIME_decode_text(f, unpack_metadata, hinfo, keep); + result = MIME_decode_text(input_f, unpack_metadata, hinfo, keep); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding result for UNSPECIFIED format = %d\n",FL,__func__, result); // 20040114-1236:PLD: Added nested mail checking // @@ -2251,7 +2251,7 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct break; default: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding format is not defined (%d)\n",FL,__func__, hinfo->content_transfer_encoding); - result = MIME_decode_raw(f, unpack_metadata, hinfo, keep); + result = MIME_decode_raw(input_f, unpack_metadata, hinfo, keep); break; } // Analyze our results @@ -2267,8 +2267,8 @@ int MIME_decode_encoding( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct case MIME_ERROR_RECURSION_LIMIT_REACHED: return result; break; - default: - return result; + default: + return result; } if ((result != -1)&&(result != MIME_STATUS_ZERO_FILE)) @@ -2359,7 +2359,7 @@ int MIME_postdecode_cleanup( RIPMIME_output *unpack_metadata, struct SS_object * Function Name : MIME_handle_multipart Returns Type : int ----Parameter List - 1. FFGET_FILE *f, + 1. FFGET_FILE *input_f, 2. RIPMIME_output *unpack_metadata, 3. struct MIMEH_header_info *hinfo, 4. int current_recursion_level, @@ -2374,7 +2374,7 @@ int MIME_postdecode_cleanup( RIPMIME_output *unpack_metadata, struct SS_object * Changes: \------------------------------------------------------------------*/ -int MIME_handle_multipart( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ) +int MIME_handle_multipart( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ) { int result = 0; if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding multipart/embedded \n",FL,__func__); @@ -2394,12 +2394,12 @@ int MIME_handle_multipart( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struc // headers and decodes. if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Non base64 encoding AND no filename, embedded message\n",FL,__func__); h->boundary_located = 0; - result = MIME_unpack_stage2(f, unpack_metadata, h, current_recursion_level , ss); + result = MIME_unpack_stage2(input_f, unpack_metadata, h, current_recursion_level , ss); p = BS_top(); if (p) PLD_strncpy(h->boundary, p,sizeof(h->boundary)); } else { if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Embedded message has a filename, decoding to file %s",FL,__func__,h->filename); - result = MIME_decode_encoding( f, unpack_metadata, h, ss ); + result = MIME_decode_encoding( input_f, unpack_metadata, h, ss ); if (result == 0) { char * fn; @@ -2425,7 +2425,7 @@ int MIME_handle_multipart( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struc Function Name : MIME_handle_rfc822 Returns Type : int ----Parameter List - 1. FFGET_FILE *f, Input stream + 1. FFGET_FILE *input_f, Input stream 2. RIPMIME_output *unpack_metadata, Directory to write files to 3. struct MIMEH_header_info *hinfo, Header information structure 4. int current_recursion_level, Current recursion level @@ -2440,9 +2440,9 @@ int MIME_handle_multipart( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struc Changes: \------------------------------------------------------------------*/ -int MIME_handle_rfc822( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ) +int MIME_handle_rfc822( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ) { - /** Decode a RFC822 encoded stream of data from *f **/ + /** Decode a RFC822 encoded stream of data from *input_f **/ int result = 0; if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding RFC822 message\n",FL,__func__); /** If there is no filename, then we have a "standard" @@ -2465,13 +2465,13 @@ int MIME_handle_rfc822( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M // headers and decodes. DMIME LOGGER_log("%s:%d:%s:DEBUG: Non base64 encoding AND no filename, embedded message\n",FL,__func__); h->boundary_located = 0; - result = MIME_unpack_stage2(f, unpack_metadata, h, current_recursion_level , ss); + result = MIME_unpack_stage2(input_f, unpack_metadata, h, current_recursion_level , ss); p = BS_top(); if (p) PLD_strncpy(h->boundary, p,sizeof(h->boundary)); } else { /** ...else... if the section has a filename or B64 type encoding, we need to put it through extra decoding **/ if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Embedded message has a filename, decoding to file %s",FL,__func__,h->filename); - result = MIME_decode_encoding( f, unpack_metadata, h, ss ); + result = MIME_decode_encoding( input_f, unpack_metadata, h, ss ); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Result of extracting %s is %d",FL,__func__,h->filename, result); if (result == 0) { char * fn; @@ -2498,7 +2498,7 @@ int MIME_handle_rfc822( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M Function Name : MIME_handle_plain Returns Type : int ----Parameter List - 1. FFGET_FILE *f, + 1. FFGET_FILE *input_f, 2. RIPMIME_output *unpack_metadata, 3. struct MIMEH_header_info *h, 4. int current_recursion_level, @@ -2513,12 +2513,12 @@ int MIME_handle_rfc822( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M Changes: \------------------------------------------------------------------*/ -int MIME_handle_plain( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ) +int MIME_handle_plain( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ) { - /** Handle a plain text encoded data stream from *f **/ + /** Handle a plain text encoded data stream from *input_f **/ int result = 0; if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Handling plain email",FL,__func__); - result = MIME_decode_encoding( f, unpack_metadata, h, ss ); + result = MIME_decode_encoding( input_f, unpack_metadata, h, ss ); if ((result == MIME_ERROR_FFGET_EMPTY)||(result == 0)) { /** Test for RFC822 content... if so, go decode it **/ @@ -2542,7 +2542,7 @@ as required by the MIME structure of the file. Output: Errors: ------------------------------------------------------------------------*/ -int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int current_recursion_level, struct SS_object *ss ) +int MIME_unpack_stage2( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int current_recursion_level, struct SS_object *ss ) { int result = 0; struct MIMEH_header_info *h; @@ -2564,7 +2564,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M /** 20041216-1102:PLD: Keep attempting to read headers until we get a sane set **/ do { /** Read next set of headers, repeat until a sane set of headers are found **/ - result = MIMEH_parse_headers(f,h,unpack_metadata->dir); + result = MIMEH_parse_headers(input_f,h,unpack_metadata->dir); DMIME LOGGER_log("%s:%d:%s:DEBUG: Parsing of headers done, sanity = %d, result = %d",FL,__func__,h->sanity, result); } while ((h->sanity == 0)&&(result != -1)); @@ -2626,14 +2626,14 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M { // Pass off to the RFC822 handler if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding with RFC822 decoder\n",FL,__func__); - result = MIME_handle_rfc822(f,unpack_metadata,h,current_recursion_level,ss); + result = MIME_handle_rfc822(input_f,unpack_metadata,h,current_recursion_level,ss); } else if (MIMEH_is_contenttype(_CTYPE_MULTIPART, h->content_type)) { // Pass off to the multipart handler if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding with Multipart decoder\n",FL,__func__); - result = MIME_handle_multipart(f,unpack_metadata,h,current_recursion_level,ss); + result = MIME_handle_multipart(input_f,unpack_metadata,h,current_recursion_level,ss); } else { if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding boundaryless file (%s)...\n",FL,__func__,h->filename); - result = MIME_handle_plain( f, unpack_metadata,h,current_recursion_level,ss); + result = MIME_handle_plain( input_f, unpack_metadata,h,current_recursion_level,ss); } // else-if content was RFC822 or multi-part return result; } // End of the boundary-LESS mode ( processing the mail which has no boundaries in the primary headers ) @@ -2646,7 +2646,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M // Decode the data in the current MIME segment // based on the header information retrieved from // the start of this function. - result = MIME_decode_encoding(f, unpack_metadata, h, ss); + result = MIME_decode_encoding(input_f, unpack_metadata, h, ss); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done decoding, result = %d",FL,__func__,result); if (result == 0) { @@ -2665,7 +2665,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding headers...\n",FL,__func__); do { - result = MIMEH_parse_headers(f,h,unpack_metadata->dir); + result = MIMEH_parse_headers(input_f,h,unpack_metadata->dir); } while ((h->sanity == 0)&&(result != -1)); glb.header_defect_count += MIMEH_get_defect_count(h); @@ -2712,9 +2712,9 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M // however, it is a rather robust/reliable way. if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Chose Content-type == RFC822 clause",FL,__func__); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Calling MIME_decode_encoding()",FL,__func__); - result = MIME_handle_rfc822(f,unpack_metadata,h,current_recursion_level,ss); + result = MIME_handle_rfc822(input_f,unpack_metadata,h,current_recursion_level,ss); // First up - extract the RFC822 body out of the parent mailpack - // XX result = MIME_decode_encoding( f, unpackdir, h, ss ); + // XX result = MIME_decode_encoding( input_f, unpackdir, h, ss ); // Now decode the RFC822 body. /** @@ -2739,7 +2739,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M // they are. Shame on me for not remembering, in future I must comment more. if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: NON-BASE64 DECODE\n",FL,__func__); h->boundary_located = 0; - result = MIME_unpack_stage2(f, unpack_metadata, h, current_recursion_level , ss); + result = MIME_unpack_stage2(input_f, unpack_metadata, h, current_recursion_level , ss); // When we've exited from decoding the sub-mailpack, we need to restore the original // boundary @@ -2756,11 +2756,11 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M // layout of other nested emails if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Handle Appledouble explicitly",FL,__func__); - result = MIME_unpack_stage2(f, unpack_metadata, h, current_recursion_level , ss); + result = MIME_unpack_stage2(input_f, unpack_metadata, h, current_recursion_level , ss); } else { if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: RFC822 Message to be decoded...\n",FL,__func__); - result = MIME_decode_encoding( f, unpack_metadata, h, ss ); + result = MIME_decode_encoding( input_f, unpack_metadata, h, ss ); if (result != 0) return result; // 20040305-1313:PLD else { @@ -2784,7 +2784,7 @@ int MIME_unpack_stage2( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct M // multipart or RFC822 embedded email, we can then simply use // the normal decoding function to interpret its data. if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding a normal attachment \n",FL,__func__); - result = MIME_decode_encoding( f, unpack_metadata, h, ss ); + result = MIME_decode_encoding( input_f, unpack_metadata, h, ss ); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding a normal attachment '%s' done. \n",FL,__func__, h->filename); // See if we have an attachment output which is actually another @@ -2837,7 +2837,7 @@ Purpose: Decodes mailbox formatted email files ------------------------------------------------------------------------*/ int MIME_unpack_mailbox( RIPMIME_output *unpack_metadata, char *mpname, int current_recursion_level, struct SS_object *ss ) { - FFGET_FILE f; + FFGET_FILE input_f; FILE *fi; FILE *fo; char fname[1024]; @@ -2868,8 +2868,8 @@ int MIME_unpack_mailbox( RIPMIME_output *unpack_metadata, char *mpname, int curr return -1; } - FFGET_setstream(&f, fi); - while (FFGET_fgets(line,sizeof(line),&f)) + FFGET_setstream(&input_f, fi); + while (FFGET_fgets(line,sizeof(line),&input_f)) { // If we have the construct of "\n\rFrom ", then we // can be -pretty- sure that a new email is about From 9bcf09cd6a4ec6cb8aabeffd4a56d93ddbfd1524 Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Tue, 29 Apr 2025 16:29:16 +0300 Subject: [PATCH 53/80] Refactor signatures --- mime.c | 29 +++++++++++++++-------------- mime.h | 3 --- 2 files changed, 15 insertions(+), 17 deletions(-) diff --git a/mime.c b/mime.c index 71f1dcc..d1f6e11 100644 --- a/mime.c +++ b/mime.c @@ -1108,7 +1108,7 @@ Purpose: Decodes a binary type attachment, ie, no encoding, just raw data. Output: Errors: ------------------------------------------------------------------------*/ -int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int keep ) +int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int keep, MIME_element* decoded_mime ) { int result = 0; int bufsize=1024; @@ -1212,7 +1212,7 @@ keep : if set, retain the file Output: Errors: ------------------------------------------------------------------------*/ -int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int keep ) +int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int keep, MIME_element* decoded_mime ) { int linecount = 0; // The number of lines int file_has_uuencode = 0; // Flag to indicate this text has UUENCODE in it @@ -1373,7 +1373,7 @@ struct MIMEH_header_info *hinfo: Auxillairy information such as the destination Output: Errors: ------------------------------------------------------------------------*/ -int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ) +int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, MIME_element* decoded_mime ) { int i; int cr_total = 0; @@ -1682,7 +1682,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH Changes: \------------------------------------------------------------------*/ -int MIME_decode_64_cleanup( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo) +int MIME_decode_64_cleanup( FFGET_FILE *f) { int result = 0; char buffer[128]; @@ -2060,6 +2060,7 @@ int MIME_decode_encoding( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, { int keep = 1; int result = -1; + MIME_element* decoded_mime = NULL; if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Start:DEBUG: (%s)\n",FL,__func__, hinfo->filename); @@ -2170,7 +2171,7 @@ int MIME_decode_encoding( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, { case _CTRANS_ENCODING_B64: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding BASE64 format\n",FL,__func__); - result = MIME_decode_64(input_f, unpack_metadata, hinfo); + result = MIME_decode_64(input_f, unpack_metadata, hinfo, decoded_mime); switch (result) { case MIME_ERROR_B64_INPUT_STREAM_EOF: break; @@ -2178,7 +2179,7 @@ int MIME_decode_encoding( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, result = 0; break; case 0: - result = MIME_decode_64_cleanup(input_f, unpack_metadata, hinfo); + result = MIME_decode_64_cleanup(input_f); break; default: break; @@ -2186,20 +2187,20 @@ int MIME_decode_encoding( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, break; case _CTRANS_ENCODING_7BIT: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding 7BIT format\n",FL,__func__); - result = MIME_decode_text(input_f, unpack_metadata, hinfo, keep); + result = MIME_decode_text(input_f, unpack_metadata, hinfo, keep, decoded_mime); break; case _CTRANS_ENCODING_8BIT: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding 8BIT format\n",FL,__func__); - result = MIME_decode_text(input_f, unpack_metadata, hinfo, keep); + result = MIME_decode_text(input_f, unpack_metadata, hinfo, keep, decoded_mime); break; case _CTRANS_ENCODING_BINARY: case _CTRANS_ENCODING_RAW: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding RAW format\n",FL,__func__); - result = MIME_decode_raw(input_f, unpack_metadata, hinfo, keep); + result = MIME_decode_raw(input_f, unpack_metadata, hinfo, keep, decoded_mime); break; case _CTRANS_ENCODING_QP: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding Quoted-Printable format\n",FL,__func__); - result = MIME_decode_text(input_f, unpack_metadata, hinfo, keep); + result = MIME_decode_text(input_f, unpack_metadata, hinfo, keep, decoded_mime); break; case _CTRANS_ENCODING_UUENCODE: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UUENCODED format\n",FL,__func__); @@ -2215,17 +2216,17 @@ int MIME_decode_encoding( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, switch (hinfo->content_disposition) { case _CDISPOSITION_FORMDATA: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UNKNOWN format of FORMDATA disposition\n",FL,__func__); - result = MIME_decode_raw(input_f, unpack_metadata, hinfo, keep); + result = MIME_decode_raw(input_f, unpack_metadata, hinfo, keep, decoded_mime); break; default: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UNKNOWN format\n",FL,__func__); - result = MIME_decode_text(input_f, unpack_metadata, hinfo, keep); + result = MIME_decode_text(input_f, unpack_metadata, hinfo, keep, decoded_mime); } if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: UNKNOWN Decode completed, result = %d\n",FL,__func__,result); break; case _CTRANS_ENCODING_UNSPECIFIED: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UNSPECIFIED format\n",FL,__func__); - result = MIME_decode_text(input_f, unpack_metadata, hinfo, keep); + result = MIME_decode_text(input_f, unpack_metadata, hinfo, keep, decoded_mime); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding result for UNSPECIFIED format = %d\n",FL,__func__, result); // 20040114-1236:PLD: Added nested mail checking // @@ -2251,7 +2252,7 @@ int MIME_decode_encoding( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, break; default: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding format is not defined (%d)\n",FL,__func__, hinfo->content_transfer_encoding); - result = MIME_decode_raw(input_f, unpack_metadata, hinfo, keep); + result = MIME_decode_raw(input_f, unpack_metadata, hinfo, keep, decoded_mime); break; } // Analyze our results diff --git a/mime.h b/mime.h index 3925b9d..dc2a0af 100644 --- a/mime.h +++ b/mime.h @@ -52,9 +52,6 @@ int MIME_version( void ); size_t MIME_read_raw( char *src_mpname, char *dest_mpname, size_t rw_buffer_size ); int MIME_read( char *mpname ); /* returns filesize in KB */ int MIME_unpack( RIPMIME_output *unpack_metadata, char *mpname, int current_recusion_level ); -//int MIME_unpack_single_disk_file( RIPMIME_output *unpack_metadata, char *mpname, int current_recusion_level ); -//int MIME_unpack_single_disk_file_fp( RIPMIME_output *unpack_metadata, FILE *fi, int current_recusion_level ); -//int MIME_unpack_mailbox( RIPMIME_output *unpack_metadata, char *mpname, int current_recursion_level ); int MIME_insert_Xheader( char *fname, char *xheader ); int MIME_set_blankfileprefix( char *prefix ); int MIME_set_recursion_level(int level); From a97f93de4ee10afae180608b50b6c1c173782415 Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Tue, 29 Apr 2025 16:31:40 +0300 Subject: [PATCH 54/80] unify OLE functions naming --- mime.c | 4 ++-- ripOLE/ole.c | 16 ++++++++-------- ripOLE/ole.h | 4 ++-- ripOLE/ripole.c | 4 ++-- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/mime.c b/mime.c index d1f6e11..662a74d 100644 --- a/mime.c +++ b/mime.c @@ -1090,9 +1090,9 @@ int MIME_decode_OLE( RIPMIME_output *unpack_metadata, struct MIMEH_header_info * OLE_set_filename_report_fn(&ole, MIME_report_filename_decoded_RIPOLE ); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Starting OLE Decode",FL,__func__); - result = OLE_decode_file(&ole, fullpath, unpack_metadata ); + result = OLE_decode_diskfile(&ole, fullpath, unpack_metadata ); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decode done, cleaning up.",FL,__func__); - OLE_decode_file_done(&ole); + OLE_decode_done(&ole); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decode returned with code = %d",FL,__func__,result); return result; diff --git a/ripOLE/ole.c b/ripOLE/ole.c index 02462db..59a486e 100644 --- a/ripOLE/ole.c +++ b/ripOLE/ole.c @@ -1529,7 +1529,7 @@ int OLE_store_stream( struct OLE_object *ole, char *stream_name, RIPMIME_output } /*-----------------------------------------------------------------\ - Function Name : OLE_decode_file_done + Function Name : OLE_decode_done Returns Type : int ----Parameter List 1. struct OLE_object *ole , @@ -1543,7 +1543,7 @@ int OLE_store_stream( struct OLE_object *ole, char *stream_name, RIPMIME_output Changes: \------------------------------------------------------------------*/ -void OLE_decode_file_done( struct OLE_object *ole ) +void OLE_decode_done( struct OLE_object *ole ) { DOLE LOGGER_log("%s:%d:%s:DEBUG: ole->f close",FL,__func__); if (ole->f) fclose(ole->f); @@ -1575,7 +1575,7 @@ void OLE_decode_file_done( struct OLE_object *ole ) \------------------------------------------------------------------*/ int OLE_terminate_and_return( struct OLE_object *ole, int result ) { - OLE_decode_file_done(ole); + OLE_decode_done(ole); return result; } @@ -1628,7 +1628,7 @@ int OLE_decode_stream( struct OLE_object *ole, struct OLE_directory_entry *adir, if (stream_data == NULL) { DOLE LOGGER_log("%s:%d:%s:DEBUG: Terminating from stream data being NULL ",FL,__func__); - //OLE_decode_file_done(ole); + //OLE_decode_done(ole); return OLEER_MINISTREAM_STREAM_READ_FAIL; } DOLE LOGGER_log("%s:%d:%s:DEBUG: Normal decode START. element name ='%s' stream size = '%ld'",FL,__func__, element_name, adir->stream_size); @@ -1645,7 +1645,7 @@ int OLE_decode_stream( struct OLE_object *ole, struct OLE_directory_entry *adir, if (stream_data == NULL) { DOLE LOGGER_log("%s:%d:%s:DEBUG: Ministream was non-existant, terminating",FL,__func__); - //OLE_decode_file_done(ole); + //OLE_decode_done(ole); return OLEER_NORMALSTREAM_STREAM_READ_FAIL; } DOLE LOGGER_log("%s:%d:%s:DEBUG: Mini decode START.",FL,__func__); @@ -1775,13 +1775,13 @@ int OLE_decode( struct OLE_object *ole, RIPMIME_output *unpack_metadata ) DOLE LOGGER_log("%s:%d:%s:DEBUG: Finished",FL,__func__); - /* OLE_decode_file_done(ole); + /* OLE_decode_done(ole); */ return OLE_OK; } /*-----------------------------------------------------------------\ - Function Name : OLE_decode_file + Function Name : OLE_decode_diskfile Returns Type : int ----Parameter List 1. char *fname, @@ -1796,7 +1796,7 @@ int OLE_decode( struct OLE_object *ole, RIPMIME_output *unpack_metadata ) Changes: \------------------------------------------------------------------*/ -int OLE_decode_file( struct OLE_object *ole, char *fname, RIPMIME_output *unpack_metadata ) +int OLE_decode_diskfile( struct OLE_object *ole, char *fname, RIPMIME_output *unpack_metadata ) { int result = 0; diff --git a/ripOLE/ole.h b/ripOLE/ole.h index 17d162f..8e8770b 100644 --- a/ripOLE/ole.h +++ b/ripOLE/ole.h @@ -141,8 +141,8 @@ int OLE_follow_minichain( struct OLE_object *ole, int miniFAT_sector_start ); unsigned char *OLE_load_minichain( struct OLE_object *ole, int miniFAT_sector_start ); unsigned char *OLE_load_chain( struct OLE_object *ole, int FAT_sector_start ); int OLE_open_file( struct OLE_object *ole, char *fullpath ); -int OLE_decode_file( struct OLE_object *ole, char *fname, RIPMIME_output *unpack_metadata ); -void OLE_decode_file_done( struct OLE_object *ole ); +int OLE_decode_diskfile( struct OLE_object *ole, char *fname, RIPMIME_output *unpack_metadata ); +void OLE_decode_done( struct OLE_object *ole ); // Our callbacks. int OLE_set_filename_report_fn( struct OLE_object *ole, int (*ptr_to_fn)(char *) ); diff --git a/ripOLE/ripole.c b/ripOLE/ripole.c index 998b3c3..6395ef8 100644 --- a/ripOLE/ripole.c +++ b/ripOLE/ripole.c @@ -338,8 +338,8 @@ int main( int argc, char **argv ) o.dir = role.outputdir; o.unpack_mode = RIPMIME_UNPACK_MODE_TO_DIRECTORY; - result = OLE_decode_file( ole, role.inputfile, &o ); - OLE_decode_file_done(ole); + result = OLE_decode_diskfile( ole, role.inputfile, &o ); + OLE_decode_done(ole); if ((result != 0)) { if (role.verbose) { From 46bd5a420b97bfe3ca2f02f8a671fc12ba66393f Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Tue, 29 Apr 2025 16:40:27 +0300 Subject: [PATCH 55/80] Refactor OLE, drafts for in-memory `FILE` usage --- ripOLE/ole.c | 21 ++++++++++++++++++++- ripOLE/ole.h | 1 + 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/ripOLE/ole.c b/ripOLE/ole.c index 59a486e..a82bada 100644 --- a/ripOLE/ole.c +++ b/ripOLE/ole.c @@ -1434,7 +1434,7 @@ int OLE_open_file( struct OLE_object *ole, char *fullpath ) return -1; } else - return OLE_input_file_data_ini(ole); + return OLE_OK; } /*-----------------------------------------------------------------\ @@ -1811,5 +1811,24 @@ int OLE_decode_diskfile( struct OLE_object *ole, char *fname, RIPMIME_output *un result = OLE_open_file( ole, fname ); if (result != 0) return result; + result = OLE_input_file_data_ini(ole); + if (result != 0) return result; + return OLE_decode( ole, unpack_metadata ); +} + +int OLE_decode_file( struct OLE_object *ole, FILE *f, RIPMIME_output *unpack_metadata ) +{ + int result = 0; + + DOLE LOGGER_log("%s:%d:%s:DEBUG: opening %s", FL,__func__); + // Reject any bad paramters. + if (ole == NULL) return OLEER_DECODE_NULL_OBJECT; + if (f == NULL) return OLEER_DECODE_NULL_FILENAME; + if (unpack_metadata == NULL || unpack_metadata->dir == NULL) return OLEER_DECODE_NULL_PATH; + + ole->f = f; + + result = OLE_input_file_data_ini(ole); + if (result != 0) return result; return OLE_decode( ole, unpack_metadata ); } diff --git a/ripOLE/ole.h b/ripOLE/ole.h index 8e8770b..3f44e88 100644 --- a/ripOLE/ole.h +++ b/ripOLE/ole.h @@ -142,6 +142,7 @@ unsigned char *OLE_load_minichain( struct OLE_object *ole, int miniFAT_sector_st unsigned char *OLE_load_chain( struct OLE_object *ole, int FAT_sector_start ); int OLE_open_file( struct OLE_object *ole, char *fullpath ); int OLE_decode_diskfile( struct OLE_object *ole, char *fname, RIPMIME_output *unpack_metadata ); +int OLE_decode_file( struct OLE_object *ole, FILE *f, RIPMIME_output *unpack_metadata ); void OLE_decode_done( struct OLE_object *ole ); // Our callbacks. From 2d27fcd66356b46f7bff363f3b5c9ad8d7697201 Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Tue, 29 Apr 2025 16:45:43 +0300 Subject: [PATCH 56/80] continie OLE memorize --- mime.c | 3 ++- ripOLE/ole.c | 4 ++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/mime.c b/mime.c index 662a74d..923dad3 100644 --- a/mime.c +++ b/mime.c @@ -1093,6 +1093,8 @@ int MIME_decode_OLE( RIPMIME_output *unpack_metadata, struct MIMEH_header_info * result = OLE_decode_diskfile(&ole, fullpath, unpack_metadata ); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decode done, cleaning up.",FL,__func__); OLE_decode_done(&ole); + if (ole.f) + fclose(ole.f); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decode returned with code = %d",FL,__func__,result); return result; @@ -1100,7 +1102,6 @@ int MIME_decode_OLE( RIPMIME_output *unpack_metadata, struct MIMEH_header_info * #endif - /*------------------------------------------------------------------------ Procedure: MIME_decode_raw ID:1 Purpose: Decodes a binary type attachment, ie, no encoding, just raw data. diff --git a/ripOLE/ole.c b/ripOLE/ole.c index a82bada..4b5613a 100644 --- a/ripOLE/ole.c +++ b/ripOLE/ole.c @@ -1545,8 +1545,6 @@ int OLE_store_stream( struct OLE_object *ole, char *stream_name, RIPMIME_output \------------------------------------------------------------------*/ void OLE_decode_done( struct OLE_object *ole ) { - DOLE LOGGER_log("%s:%d:%s:DEBUG: ole->f close",FL,__func__); - if (ole->f) fclose(ole->f); /** Why weren't these active? (they were commented out ) **/ DOLE LOGGER_log("%s:%d:%s:DEBUG: OLE FAT",FL,__func__); if (ole->FAT) free(ole->FAT); @@ -1576,6 +1574,8 @@ void OLE_decode_done( struct OLE_object *ole ) int OLE_terminate_and_return( struct OLE_object *ole, int result ) { OLE_decode_done(ole); + DOLE LOGGER_log("%s:%d:%s:DEBUG: ole->f close",FL,__func__); + if (ole->f) fclose(ole->f); return result; } From dc9af9682de26a5e27ca5332f4e8ee979831ee57 Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Tue, 29 Apr 2025 16:46:43 +0300 Subject: [PATCH 57/80] Rename OLE decoding for a disk file --- mime.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/mime.c b/mime.c index 923dad3..0c38cbb 100644 --- a/mime.c +++ b/mime.c @@ -1058,7 +1058,7 @@ int MIME_report_filename_decoded_RIPOLE(char *filename) } /*-----------------------------------------------------------------\ - Function Name : MIME_decode_OLE + Function Name : MIME_decode_OLE_diskfile Returns Type : int ----Parameter List 1. RIPMIME_output *unpack_metadata, @@ -1074,7 +1074,7 @@ int MIME_report_filename_decoded_RIPOLE(char *filename) Changes: \------------------------------------------------------------------*/ -int MIME_decode_OLE( RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int keep ) +int MIME_decode_OLE_diskfile( RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int keep ) { struct OLE_object ole; char fullpath[1024]; @@ -2283,7 +2283,7 @@ int MIME_decode_encoding( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, // performance of the ripMIME decoding engine if (glb.decode_ole > 0) { - MIME_decode_OLE( unpack_metadata, hinfo, 0 ); + MIME_decode_OLE_diskfile( unpack_metadata, hinfo, 0 ); } #endif From 170e85a535b567f7c8cd974ece8fdaa0439582a4 Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Wed, 30 Apr 2025 09:16:44 +0300 Subject: [PATCH 58/80] Un`scratch` MIME headers, use dynamic memory instead --- mime.c | 77 ++++++++++++++++++++++++++++++++++++-------------- mime_element.c | 4 ++- mime_element.h | 4 ++- mime_headers.h | 1 - 4 files changed, 62 insertions(+), 24 deletions(-) diff --git a/mime.c b/mime.c index 0c38cbb..781eb86 100644 --- a/mime.c +++ b/mime.c @@ -1099,6 +1099,28 @@ int MIME_decode_OLE_diskfile( RIPMIME_output *unpack_metadata, struct MIMEH_head if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decode returned with code = %d",FL,__func__,result); return result; } + +int MIME_decode_OLE_file( RIPMIME_output *unpack_metadata, FILE *f, int keep ) +{ + struct OLE_object ole; + int result; + + OLE_init(&ole); + OLE_set_quiet(&ole,glb.quiet); + OLE_set_verbose(&ole,glb.verbosity); + OLE_set_debug(&ole,glb.debug); + OLE_set_save_unknown_streams(&ole,0); + OLE_set_filename_report_fn(&ole, MIME_report_filename_decoded_RIPOLE ); + + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Starting OLE Decode",FL,__func__); + result = OLE_decode_file(&ole, f, unpack_metadata ); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decode done, cleaning up.",FL,__func__); + OLE_decode_done(&ole); + if (ole.f) + fseek(ole.f, 0, SEEK_SET); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decode returned with code = %d",FL,__func__,result); + return result; +} #endif @@ -2240,16 +2262,17 @@ int MIME_decode_encoding( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, // because dud headers will always result in a UNSPECIFIED encoding // // Original sample mailpack was sent by Farit - thanks. - if (1) + char *fn; + int fn_l = strlen(unpack_metadata->dir) + strlen(hinfo->filename) + sizeof(char) * 2; + fn = malloc(fn_l); + snprintf(fn,fn_l,"%s/%s",unpack_metadata->dir,hinfo->filename); + LOGGER_log("%s:%d:%s:DEBUG:REMOVEME: Testing for RFC822 headers in file %s",FL,__func__,fn); + if (MIME_is_diskfile_RFC822(fn) > 0 ) { - snprintf(hinfo->scratch,sizeof(hinfo->scratch),"%s/%s",unpack_metadata->dir,hinfo->filename); - LOGGER_log("%s:%d:%s:DEBUG:REMOVEME: Testing for RFC822 headers in file %s",FL,__func__,hinfo->scratch); - if (MIME_is_diskfile_RFC822(hinfo->scratch) > 0 ) - { - // 20040305-1304:PLD: unpack the file, propagate result upwards - result = MIME_unpack_single_diskfile( unpack_metadata, hinfo->scratch, (hinfo->current_recursion_level+ 1),ss ); - } + // 20040305-1304:PLD: unpack the file, propagate result upwards + result = MIME_unpack_single_diskfile( unpack_metadata, fn, (hinfo->current_recursion_level+ 1),ss ); } + free(fn); break; default: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding format is not defined (%d)\n",FL,__func__, hinfo->content_transfer_encoding); @@ -2309,12 +2332,17 @@ int MIME_decode_encoding( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, { if (glb.decode_mht != 0) { + char *fn; + int fn_l = strlen(unpack_metadata->dir) + strlen(hinfo->filename) + sizeof(char) * 2; + // Patched 26-01-03: supplied by Chris Hine if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Microsoft MHT format email filename='%s'\n",FL,__func__, hinfo->filename); - snprintf(hinfo->scratch,sizeof(hinfo->scratch),"%s/%s",unpack_metadata->dir,hinfo->filename); + fn = malloc(fn_l); + snprintf(fn,fn_l,"%s/%s",unpack_metadata->dir,hinfo->filename); // 20040305-1304:PLD: unpack the file, propagate result upwards - result = MIME_unpack_single_diskfile( unpack_metadata, hinfo->scratch, (hinfo->current_recursion_level+ 1),ss ); + result = MIME_unpack_single_diskfile( unpack_metadata, fn, (hinfo->current_recursion_level+ 1),ss ); + free(fn); } } // Decode MHT files } // If result != -1 @@ -2334,9 +2362,7 @@ Output: none ------------------------------------------------------------------------*/ int MIME_postdecode_cleanup( RIPMIME_output *unpack_metadata, struct SS_object *ss ) { - char fullpath[256]; - int result; - result = 0; + int result = 0; do { char *filename; if (MIME_DNORMAL) LOGGER_log("%s:%d:%s Popping file...",FL,__func__); @@ -2345,13 +2371,18 @@ int MIME_postdecode_cleanup( RIPMIME_output *unpack_metadata, struct SS_object * if (MIME_DNORMAL) LOGGER_log("%s:%d:%s Popped file '%s'",FL,__func__, filename); if ( strncmp( glb.blankfileprefix, filename, strlen( glb.blankfileprefix ) ) == 0 ) { - snprintf( fullpath, sizeof(fullpath), "%s/%s", unpack_metadata->dir, filename ); - result = unlink( fullpath ); + char *fn; + int fn_l = strlen(unpack_metadata->dir) + strlen(filename) + sizeof(char) * 2; + + snprintf(fn,fn_l,"%s/%s",unpack_metadata->dir,filename); + + result = unlink( fn ); if (MIME_VERBOSE) { - if (result == -1) LOGGER_log("Error removing '%s'; %s", fullpath, strerror(errno)); - else LOGGER_log("Removed %s [status = %d]\n", fullpath, result ); + if (result == -1) LOGGER_log("Error removing '%s'; %s", fn, strerror(errno)); + else LOGGER_log("Removed %s [status = %d]\n", fn, result ); } + free(fn); } } while (1); return 0; @@ -2404,7 +2435,7 @@ int MIME_handle_multipart( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, result = MIME_decode_encoding( input_f, unpack_metadata, h, ss ); if (result == 0) { - char * fn; + char *fn; int fn_l = strlen(unpack_metadata->dir) + strlen(h->filename) + sizeof(char) * 2; fn = malloc(fn_l); @@ -2524,14 +2555,18 @@ int MIME_handle_plain( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, str if ((result == MIME_ERROR_FFGET_EMPTY)||(result == 0)) { /** Test for RFC822 content... if so, go decode it **/ - snprintf(h->scratch,sizeof(h->scratch),"%s/%s",unpack_metadata->dir,h->filename); - if (MIME_is_diskfile_RFC822(h->scratch)==1) + char *fn; + int fn_l = strlen(unpack_metadata->dir) + strlen(h->filename) + sizeof(char) * 2; + fn = malloc(fn_l); + snprintf(fn,fn_l,"%s/%s",unpack_metadata->dir,h->filename); + if (MIME_is_diskfile_RFC822(fn)==1) { /** If the file is RFC822, then decode it using MIME_unpack_single_diskfile() **/ if (glb.header_longsearch != 0) MIMEH_set_header_longsearch(glb.header_longsearch); - result = MIME_unpack_single_diskfile( unpack_metadata, h->scratch, current_recursion_level, ss ); + result = MIME_unpack_single_diskfile( unpack_metadata, fn, current_recursion_level, ss ); if (glb.header_longsearch != 0) MIMEH_set_header_longsearch(0); } + free(fn); } return result; } diff --git a/mime_element.c b/mime_element.c index 67636f7..03ce02e 100644 --- a/mime_element.c +++ b/mime_element.c @@ -57,7 +57,9 @@ static char * dup_ini(char* s) return (s != NULL) ? strdup(s) : "\0"; } -MIME_element* MIME_element_add(struct MIME_element* parent, RIPMIME_output *unpack_metadata, char* filename, char* content_type_string, char* content_transfer_encoding, char* name, int current_recursion_level, int attachment_count, int filecount, char* func) +MIME_element* MIME_element_add(struct MIME_element* parent, RIPMIME_output *unpack_metadata, + char* filename, char* content_type_string, char* content_transfer_encoding, char* name, + int current_recursion_level, int attachment_count, int filecount, const char* func) { MIME_element *cur = malloc(sizeof(MIME_element)); int fullpath_len = 0; diff --git a/mime_element.h b/mime_element.h index 7c89c57..ac57638 100644 --- a/mime_element.h +++ b/mime_element.h @@ -50,6 +50,8 @@ typedef struct { extern all_MIME_elements_s all_MIME_elements; +int MIMEELEMENT_set_debug( int level ); + void all_MIME_elements_init (void); MIME_element* MIME_element_add ( struct MIME_element* parent, @@ -61,7 +63,7 @@ MIME_element* MIME_element_add ( int current_recursion_level, int attachment_count, int filecount, - char* func); + const char* func); // void MIME_element_free (MIME_element* cur); void MIME_element_deactivate (MIME_element* cur, RIPMIME_output *unpack_metadata); void printArray(dynamic_array* container); diff --git a/mime_headers.h b/mime_headers.h index da729a1..2268d00 100644 --- a/mime_headers.h +++ b/mime_headers.h @@ -97,7 +97,6 @@ struct MIMEH_header_info { - char scratch[_MIMEH_STRLEN_MAX + 1]; int content_type; char content_type_string[ _MIMEH_CONTENT_TYPE_MAX + 1 ]; char content_description_string[ _MIMEH_CONTENT_DESCRIPTION_MAX + 1 ]; From 300585e0a630adec457843c86aea508775384ca2 Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Wed, 30 Apr 2025 09:42:19 +0300 Subject: [PATCH 59/80] Un`keep` because unused and potentially can be stored in `RIPMIME_output` --- mime.c | 55 +++++++++++++++++++++++++------------------------- mime_element.c | 9 +++++++-- uuencode.c | 7 +++---- uuencode.h | 2 +- 4 files changed, 39 insertions(+), 34 deletions(-) diff --git a/mime.c b/mime.c index 781eb86..56d05a7 100644 --- a/mime.c +++ b/mime.c @@ -1035,7 +1035,7 @@ Purpose: Decodes TNEF encoded attachments Output: Errors: ------------------------------------------------------------------------*/ -int MIME_decode_TNEF( RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int keep ) +int MIME_decode_TNEF( RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ) { int result = TNEF_main( hinfo->filename, unpack_metadata->dir ); @@ -1063,7 +1063,6 @@ int MIME_report_filename_decoded_RIPOLE(char *filename) ----Parameter List 1. RIPMIME_output *unpack_metadata, 2. struct MIMEH_header_info *hinfo, - 3. int keep , ------------------ Exit Codes : Side Effects : @@ -1074,7 +1073,7 @@ int MIME_report_filename_decoded_RIPOLE(char *filename) Changes: \------------------------------------------------------------------*/ -int MIME_decode_OLE_diskfile( RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int keep ) +int MIME_decode_OLE_diskfile( RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ) { struct OLE_object ole; char fullpath[1024]; @@ -1100,7 +1099,7 @@ int MIME_decode_OLE_diskfile( RIPMIME_output *unpack_metadata, struct MIMEH_head return result; } -int MIME_decode_OLE_file( RIPMIME_output *unpack_metadata, FILE *f, int keep ) +int MIME_decode_OLE_file( RIPMIME_output *unpack_metadata, FILE *f ) { struct OLE_object ole; int result; @@ -1131,7 +1130,7 @@ Purpose: Decodes a binary type attachment, ie, no encoding, just raw data. Output: Errors: ------------------------------------------------------------------------*/ -int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int keep, MIME_element* decoded_mime ) +int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, MIME_element* decoded_mime ) { int result = 0; int bufsize=1024; @@ -1185,7 +1184,7 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME if ( hinfo->content_transfer_encoding == _CTRANS_ENCODING_UUENCODE ) decode_entire_file = 0; ffg = UUENCODE_make_sourcestream(cur_mime->f); /* fseek to begin is here */ - result = UUENCODE_decode_uu(ffg , hinfo->uudec_name, decode_entire_file, keep, unpack_metadata, hinfo ); + result = UUENCODE_decode_uu(ffg , hinfo->uudec_name, decode_entire_file, unpack_metadata, hinfo ); if (result == -1) { switch (uuencode_error) { @@ -1214,7 +1213,7 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME { if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding TNEF format\n",FL,__func__); snprintf(hinfo->filename, 128, "%s", hinfo->uudec_name); - MIME_decode_TNEF( unpack_metadata, hinfo, keep); + MIME_decode_TNEF( unpack_metadata, hinfo); } else LOGGER_log("%s:%d:%s:WARNING: hinfo has been clobbered.\n",FL,__func__); } @@ -1231,11 +1230,10 @@ Procedure: MIME_decode_text ID:1 Purpose: Decodes an input stream into a text file. Input: unpackdir : directory where to place new text file hinfo : struct containing information from the last parsed headers -keep : if set, retain the file Output: Errors: ------------------------------------------------------------------------*/ -int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, int keep, MIME_element* decoded_mime ) +int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, MIME_element* decoded_mime ) { int linecount = 0; // The number of lines int file_has_uuencode = 0; // Flag to indicate this text has UUENCODE in it @@ -1333,7 +1331,7 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM // hinfo->uudec_name[0] = '\0'; if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UUencoded data in file '%s'\n",FL,__func__,hinfo->filename); - //result = UUENCODE_decode_uu( NULL, info->filename, hinfo->uudec_name, 1, keep ); + //result = UUENCODE_decode_uu( NULL, info->filename, hinfo->uudec_name, 1 ); // Attempt to decode the UUENCODED data in the file, // NOTE - hinfo->uudec_name is a blank buffer which will be filled by the UUENCODE_decode_uu // function once it has located a filename in the UUENCODED data. A bit of a problem here @@ -1345,7 +1343,7 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM fuue = UUENCODE_make_file_obj (ffname); ffg = UUENCODE_make_sourcestream(fuue); - result = UUENCODE_decode_uu( ffg, hinfo->uudec_name, 1, keep, unpack_metadata, hinfo ); + result = UUENCODE_decode_uu( ffg, hinfo->uudec_name, 1, unpack_metadata, hinfo ); fclose(fuue); if (result == -1) { @@ -1372,7 +1370,7 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM { if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding TNEF format\n",FL,__func__); snprintf(hinfo->filename, 128, "%s", hinfo->uudec_name); - MIME_decode_TNEF( unpack_metadata, hinfo, keep ); + MIME_decode_TNEF( unpack_metadata, hinfo ); } if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Completed decoding UUencoded data.\n",FL,__func__); } @@ -1761,7 +1759,7 @@ int MIME_doubleCR_decode( char *filename, RIPMIME_output *unpack_metadata, struc fuue = UUENCODE_make_file_obj (filename); ffg = UUENCODE_make_sourcestream(fuue); UUENCODE_set_doubleCR_mode(1); - result = UUENCODE_decode_uu(ffg, h.uudec_name, 1, 1, unpack_metadata, hinfo ); + result = UUENCODE_decode_uu(ffg, h.uudec_name, 1, unpack_metadata, hinfo ); fclose(fuue); UUENCODE_set_doubleCR_mode(0); glb.attachment_count += result; @@ -2175,13 +2173,16 @@ int MIME_decode_encoding( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, } // If we were using the new filename telling format } // If we were telling the filename (verbosity) - if (1) + { char *fp; /** Find the start of the filename. **/ fp = strrchr(hinfo->filename, '/'); - if (fp) fp++; else fp = hinfo->filename; - //LOGGER_log("%s:%d:%s:DEBUG: Pushing filename %s to the stack",FL,__func__,fp); + if (fp) + fp++; + else + fp = hinfo->filename; + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Pushing filename %s to the stack",FL,__func__,fp); // 20040305-1419:PLD // Store the filename we're going to use to save the file to in the filename stack SS_push(ss, fp, strlen(fp)); @@ -2210,26 +2211,26 @@ int MIME_decode_encoding( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, break; case _CTRANS_ENCODING_7BIT: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding 7BIT format\n",FL,__func__); - result = MIME_decode_text(input_f, unpack_metadata, hinfo, keep, decoded_mime); + result = MIME_decode_text(input_f, unpack_metadata, hinfo, decoded_mime); break; case _CTRANS_ENCODING_8BIT: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding 8BIT format\n",FL,__func__); - result = MIME_decode_text(input_f, unpack_metadata, hinfo, keep, decoded_mime); + result = MIME_decode_text(input_f, unpack_metadata, hinfo, decoded_mime); break; case _CTRANS_ENCODING_BINARY: case _CTRANS_ENCODING_RAW: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding RAW format\n",FL,__func__); - result = MIME_decode_raw(input_f, unpack_metadata, hinfo, keep, decoded_mime); + result = MIME_decode_raw(input_f, unpack_metadata, hinfo, decoded_mime); break; case _CTRANS_ENCODING_QP: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding Quoted-Printable format\n",FL,__func__); - result = MIME_decode_text(input_f, unpack_metadata, hinfo, keep, decoded_mime); + result = MIME_decode_text(input_f, unpack_metadata, hinfo, decoded_mime); break; case _CTRANS_ENCODING_UUENCODE: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UUENCODED format\n",FL,__func__); // Added as a test - remove if we can get this to work in a better way snprintf(hinfo->uudec_name,sizeof(hinfo->uudec_name),"%s",hinfo->filename); - result = UUENCODE_decode_uu(input_f, hinfo->uudec_name, 0, keep, unpack_metadata, hinfo ); + result = UUENCODE_decode_uu(input_f, hinfo->uudec_name, 0, unpack_metadata, hinfo ); glb.attachment_count += result; // Because this is a file-count, it's not really an 'error result' as such, so, set the // return code back to 0! @@ -2239,17 +2240,17 @@ int MIME_decode_encoding( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, switch (hinfo->content_disposition) { case _CDISPOSITION_FORMDATA: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UNKNOWN format of FORMDATA disposition\n",FL,__func__); - result = MIME_decode_raw(input_f, unpack_metadata, hinfo, keep, decoded_mime); + result = MIME_decode_raw(input_f, unpack_metadata, hinfo, decoded_mime); break; default: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UNKNOWN format\n",FL,__func__); - result = MIME_decode_text(input_f, unpack_metadata, hinfo, keep, decoded_mime); + result = MIME_decode_text(input_f, unpack_metadata, hinfo, decoded_mime); } if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: UNKNOWN Decode completed, result = %d\n",FL,__func__,result); break; case _CTRANS_ENCODING_UNSPECIFIED: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UNSPECIFIED format\n",FL,__func__); - result = MIME_decode_text(input_f, unpack_metadata, hinfo, keep, decoded_mime); + result = MIME_decode_text(input_f, unpack_metadata, hinfo, decoded_mime); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding result for UNSPECIFIED format = %d\n",FL,__func__, result); // 20040114-1236:PLD: Added nested mail checking // @@ -2276,7 +2277,7 @@ int MIME_decode_encoding( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, break; default: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding format is not defined (%d)\n",FL,__func__, hinfo->content_transfer_encoding); - result = MIME_decode_raw(input_f, unpack_metadata, hinfo, keep, decoded_mime); + result = MIME_decode_raw(input_f, unpack_metadata, hinfo, decoded_mime); break; } // Analyze our results @@ -2306,7 +2307,7 @@ int MIME_decode_encoding( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, // performance of the ripMIME decoding engine if (glb.decode_ole > 0) { - MIME_decode_OLE_diskfile( unpack_metadata, hinfo, 0 ); + MIME_decode_OLE_diskfile( unpack_metadata, hinfo ); } #endif @@ -2320,7 +2321,7 @@ int MIME_decode_encoding( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, { if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding TNEF format\n",FL,__func__); glb.attachment_count++; - MIME_decode_TNEF( unpack_metadata, hinfo, 0 ); + MIME_decode_TNEF( unpack_metadata, hinfo ); } // Decode TNEF // Look for Microsoft MHT files... and try decode them. diff --git a/mime_element.c b/mime_element.c index 03ce02e..9519213 100644 --- a/mime_element.c +++ b/mime_element.c @@ -121,7 +121,7 @@ void MIME_element_free (MIME_element* cur) dup_free(cur->name); free(cur); - cur == NULL; + cur = NULL; } void MIME_element_deactivate(MIME_element* cur, RIPMIME_output *unpack_metadata) @@ -133,9 +133,14 @@ void MIME_element_deactivate(MIME_element* cur, RIPMIME_output *unpack_metadata) static inline int get_random_value(void) { int randval; FILE *fp; + size_t res = 0; fp = fopen("/dev/urandom", "r"); - fread(&randval, sizeof(randval), 1, fp); + res = fread(&randval, sizeof(randval), 1, fp); + if (res == 0) { + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s: /dev/urandom Read error\n",FL,__func__); + exit(1); + } fclose(fp); if (randval < 0) { randval = randval *( -1); }; diff --git a/uuencode.c b/uuencode.c index 423780c..87949db 100644 --- a/uuencode.c +++ b/uuencode.c @@ -368,9 +368,8 @@ FFGET_FILE * UUENCODE_make_sourcestream( FILE *f) 1. FFGET_FILE *f, Source Data Stream 2. char *out_filename, Pointer to a buffer where we will write the filename of the UU data 3. int decode_whole_file, 0 == only first segment, >0 == all - 4. int keep , Keep the files we create, don't delete - 5. unpack file metadata - 6. related MIME headers + 4. unpack file metadata + 5. related MIME headers ------------------ Exit Codes : Returns the number of attachments decoded in the data Side Effects : @@ -381,7 +380,7 @@ FFGET_FILE * UUENCODE_make_sourcestream( FILE *f) Changes: \------------------------------------------------------------------*/ -int UUENCODE_decode_uu( FFGET_FILE *f, char *out_filename, int decode_whole_file, int keep, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ) +int UUENCODE_decode_uu( FFGET_FILE *f, char *out_filename, int decode_whole_file, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ) { int filename_found = 0; char buf[ UUENCODE_STRLEN_MAX ]; diff --git a/uuencode.h b/uuencode.h index e81a976..cedbca6 100644 --- a/uuencode.h +++ b/uuencode.h @@ -21,7 +21,7 @@ int UUENCODE_set_filename_report_fn( int (*ptr_to_fn)(char *, char *) ); int UUENCODE_is_uuencode_header( char *line ); int UUENCODE_is_diskfile_uuencoded( char *fname ); -int UUENCODE_decode_uu( FFGET_FILE *f, char *out_filename, int decode_whole_file, int keep, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ); +int UUENCODE_decode_uu( FFGET_FILE *f, char *out_filename, int decode_whole_file, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ); FILE * UUENCODE_make_file_obj (char *input_filename); FFGET_FILE * UUENCODE_make_sourcestream( FILE *f); From ce5d2e9a3fdd59b9c8edc6db89526e0ea563d723 Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Wed, 30 Apr 2025 10:02:30 +0300 Subject: [PATCH 60/80] Rename to `MIME_process_content_transfer_encoding` --- mime.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/mime.c b/mime.c index 56d05a7..b0f6143 100644 --- a/mime.c +++ b/mime.c @@ -2069,7 +2069,7 @@ int MIME_generate_multiple_hardlink_filenames(struct MIMEH_header_info *hinfo, R } /*------------------------------------------------------------------------ -Procedure: MIME_decode_encoding ID:1 +Procedure: MIME_process_content_transfer_encoding ID:1 Purpose: Based on the contents of hinfo, this function will call the required function needed to decode the contents of the file which is contained within the MIME structure @@ -2077,7 +2077,7 @@ Purpose: Based on the contents of hinfo, this function will call the Output: Errors: ------------------------------------------------------------------------*/ -int MIME_decode_encoding( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, struct SS_object *ss ) +int MIME_process_content_transfer_encoding( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, struct SS_object *ss ) { int keep = 1; int result = -1; @@ -2433,7 +2433,7 @@ int MIME_handle_multipart( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, if (p) PLD_strncpy(h->boundary, p,sizeof(h->boundary)); } else { if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Embedded message has a filename, decoding to file %s",FL,__func__,h->filename); - result = MIME_decode_encoding( input_f, unpack_metadata, h, ss ); + result = MIME_process_content_transfer_encoding( input_f, unpack_metadata, h, ss ); if (result == 0) { char *fn; @@ -2505,7 +2505,7 @@ int MIME_handle_rfc822( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, st } else { /** ...else... if the section has a filename or B64 type encoding, we need to put it through extra decoding **/ if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Embedded message has a filename, decoding to file %s",FL,__func__,h->filename); - result = MIME_decode_encoding( input_f, unpack_metadata, h, ss ); + result = MIME_process_content_transfer_encoding( input_f, unpack_metadata, h, ss ); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Result of extracting %s is %d",FL,__func__,h->filename, result); if (result == 0) { char * fn; @@ -2552,7 +2552,7 @@ int MIME_handle_plain( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, str /** Handle a plain text encoded data stream from *input_f **/ int result = 0; if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Handling plain email",FL,__func__); - result = MIME_decode_encoding( input_f, unpack_metadata, h, ss ); + result = MIME_process_content_transfer_encoding( input_f, unpack_metadata, h, ss ); if ((result == MIME_ERROR_FFGET_EMPTY)||(result == 0)) { /** Test for RFC822 content... if so, go decode it **/ @@ -2684,7 +2684,7 @@ int MIME_unpack_stage2( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, st // Decode the data in the current MIME segment // based on the header information retrieved from // the start of this function. - result = MIME_decode_encoding(input_f, unpack_metadata, h, ss); + result = MIME_process_content_transfer_encoding(input_f, unpack_metadata, h, ss); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done decoding, result = %d",FL,__func__,result); if (result == 0) { @@ -2749,10 +2749,10 @@ int MIME_unpack_stage2( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, st // the most efficent way of dealing with nested emails // however, it is a rather robust/reliable way. if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Chose Content-type == RFC822 clause",FL,__func__); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Calling MIME_decode_encoding()",FL,__func__); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Calling MIME_process_content_transfer_encoding()",FL,__func__); result = MIME_handle_rfc822(input_f,unpack_metadata,h,current_recursion_level,ss); // First up - extract the RFC822 body out of the parent mailpack - // XX result = MIME_decode_encoding( input_f, unpackdir, h, ss ); + // XX result = MIME_process_content_transfer_encoding( input_f, unpackdir, h, ss ); // Now decode the RFC822 body. /** @@ -2798,7 +2798,7 @@ int MIME_unpack_stage2( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, st } else { if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: RFC822 Message to be decoded...\n",FL,__func__); - result = MIME_decode_encoding( input_f, unpack_metadata, h, ss ); + result = MIME_process_content_transfer_encoding( input_f, unpack_metadata, h, ss ); if (result != 0) return result; // 20040305-1313:PLD else { @@ -2822,7 +2822,7 @@ int MIME_unpack_stage2( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, st // multipart or RFC822 embedded email, we can then simply use // the normal decoding function to interpret its data. if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding a normal attachment \n",FL,__func__); - result = MIME_decode_encoding( input_f, unpack_metadata, h, ss ); + result = MIME_process_content_transfer_encoding( input_f, unpack_metadata, h, ss ); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding a normal attachment '%s' done. \n",FL,__func__, h->filename); // See if we have an attachment output which is actually another From 0112feaeb90636460376bd66ad16638dd38be59e Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Wed, 30 Apr 2025 11:32:08 +0300 Subject: [PATCH 61/80] Remove and refactor to local unused globals for MIME headers --- mime.c | 12 ++--- mime_headers.c | 117 ++++++------------------------------------------- mime_headers.h | 10 +++-- 3 files changed, 26 insertions(+), 113 deletions(-) diff --git a/mime.c b/mime.c index b0f6143..8a366cd 100644 --- a/mime.c +++ b/mime.c @@ -2602,7 +2602,7 @@ int MIME_unpack_stage2( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, st /** 20041216-1102:PLD: Keep attempting to read headers until we get a sane set **/ do { /** Read next set of headers, repeat until a sane set of headers are found **/ - result = MIMEH_parse_headers(input_f,h,unpack_metadata->dir); + result = MIMEH_parse_headers(NULL, NULL, input_f,h,unpack_metadata->dir, 0); DMIME LOGGER_log("%s:%d:%s:DEBUG: Parsing of headers done, sanity = %d, result = %d",FL,__func__,h->sanity, result); } while ((h->sanity == 0)&&(result != -1)); @@ -2703,7 +2703,7 @@ int MIME_unpack_stage2( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, st if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding headers...\n",FL,__func__); do { - result = MIMEH_parse_headers(input_f,h,unpack_metadata->dir); + result = MIMEH_parse_headers(NULL, NULL, input_f, h, unpack_metadata->dir, 0); } while ((h->sanity == 0)&&(result != -1)); glb.header_defect_count += MIMEH_get_defect_count(h); @@ -3053,6 +3053,7 @@ int MIME_unpack_single_file( RIPMIME_output *unpack_metadata, FILE *fi, int curr FFGET_FILE f; FILE *hf = NULL; + h.header_file = NULL; // Because this MIME module gets used in both CLI and daemon modes // we should check to see that we can report to stderr // @@ -3064,7 +3065,8 @@ int MIME_unpack_single_file( RIPMIME_output *unpack_metadata, FILE *fi, int curr return MIME_ERROR_RECURSION_LIMIT_REACHED; // 20040305-1302:PLD //return 0; // 20040208-1723:PLD } - else h.current_recursion_level = current_recursion_level; + else + h.current_recursion_level = current_recursion_level; glb.current_line = 0; if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: recursion level checked...%d\n",FL,__func__, current_recursion_level); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: DumpHeaders = %d\n",FL,__func__, glb.save_headers); @@ -3086,7 +3088,7 @@ int MIME_unpack_single_file( RIPMIME_output *unpack_metadata, FILE *fi, int curr else { headers_save_set_here = 1; - MIMEH_set_headers_save(hf); + h.header_file = hf; } free(fn); } @@ -3122,7 +3124,7 @@ int MIME_unpack_single_file( RIPMIME_output *unpack_metadata, FILE *fi, int curr { if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Closing header file.\n",FL,__func__); fflush(stdout); - MIMEH_set_headers_nosave(); + h.header_file = NULL; fclose(hf); } diff --git a/mime_headers.c b/mime_headers.c index 669d131..e5ab148 100644 --- a/mime_headers.c +++ b/mime_headers.c @@ -71,7 +71,6 @@ struct MIMEH_globals { char *headerline; char *headerline_original; // Holds the original header-form without decoding. int save_headers; - int save_headers_original; int test_mailbox; int debug; int webform; @@ -82,10 +81,6 @@ struct MIMEH_globals { int header_longsearch; // keep searching until valid headers are found - this is used to filter out qmail bounced emails - breaks RFC's but people are wanting it :-( int longsearch_limit; // how many segments do we attempt to look ahead... - - FILE *header_file; - FILE *original_header_file; - int original_header_save_to_file; }; static struct MIMEH_globals glb; @@ -131,12 +126,8 @@ void MIMEH_init( void ) glb.doubleCR = 0; glb.headerline = NULL; glb.headerline_original = NULL; - glb.header_file = NULL; - glb.original_header_file = NULL; - glb.original_header_save_to_file = 0; glb.save_headers = 0; - glb.save_headers_original = 0; glb.test_mailbox = 0; glb.debug = 0; glb.webform = 0; @@ -398,66 +389,6 @@ int MIMEH_get_verbosity_contenttype( void ) return glb.verbose_contenttype; } -/*------------------------------------------------------------------------ -Procedure: MIMEH_set_headers_save ID:1 -Purpose: Sets MIMEH's headers save file (where MIMEH will save the -headers it reads in from the mailpack) -Input: -Output: -Errors: -------------------------------------------------------------------------*/ -int MIMEH_set_headers_save( FILE *f ) -{ - glb.header_file = f; - glb.save_headers = 1; - return 0; -} - -/*-----------------------------------------------------------------\ - Function Name : MIMEH_set_headers_original_save_to_file - Returns Type : int - ----Parameter List - 1. FILE *f , - ------------------ - Exit Codes : - Side Effects : - -------------------------------------------------------------------- -Comments: - --------------------------------------------------------------------- -Changes: - -\------------------------------------------------------------------*/ -int MIMEH_set_headers_original_save_to_file( FILE *f ) -{ - if (f == NULL) glb.original_header_save_to_file = 0; - else glb.original_header_save_to_file = 1; - glb.original_header_file = f; - - return glb.original_header_save_to_file; -} - -/*-----------------------------------------------------------------\ - Function Name : MIMEH_set_headers_nosave - Returns Type : int - ----Parameter List - 1. void , - ------------------ - Exit Codes : - Side Effects : - -------------------------------------------------------------------- -Comments: - --------------------------------------------------------------------- -Changes: - -\------------------------------------------------------------------*/ -int MIMEH_set_headers_nosave( void ) -{ - glb.header_file = NULL; - glb.save_headers = 0; - return 0; -} /*-----------------------------------------------------------------\ Function Name : MIMEH_get_headers_save @@ -480,28 +411,6 @@ int MIMEH_get_headers_save( void ) } -/*-----------------------------------------------------------------\ - Function Name : MIMEH_set_headers_save_original - Returns Type : int - ----Parameter List - 1. int level , - ------------------ - Exit Codes : - Side Effects : - -------------------------------------------------------------------- -Comments: - --------------------------------------------------------------------- -Changes: - -\------------------------------------------------------------------*/ -int MIMEH_set_headers_save_original( int level ) -{ - glb.save_headers_original = level; - return glb.save_headers_original; -} - - /*-----------------------------------------------------------------\ Function Name : MIMEH_get_headers_ptr Returns Type : int @@ -1037,7 +946,7 @@ Purpose: Reads from the stream F until it detects a From line, or a blank Output: Errors: ------------------------------------------------------------------------*/ -int MIMEH_read_headers( struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata ) +int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original ) { char buffer[_MIMEH_STRLEN_MAX+1]; int totalsize=0; @@ -1089,15 +998,15 @@ int MIMEH_read_headers( struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_ // If we are being told to copy the input data to an output file // then do so here (this is for the originals) - if ((glb.original_header_save_to_file > 0)&&(glb.original_header_file != NULL)) + if (hinfo->original_header_file != NULL) { if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_read_headers:DEBUG: saving to file...",FL); - fprintf(glb.original_header_file,"%s",linestart); + fprintf(hinfo->original_header_file,"%s",linestart); } // if we are being told to keep a copy of the original data // as it comes in from ffget, then do the storage here - if (glb.save_headers_original > 0) + if (save_headers_original) { if (MIMEH_DNORMAL) LOGGER_log("MIMEH_read_headers:DEBUG:Data-In:[%d:%d] '%s'", strlen(linestart), linesize, linestart); tmp_original = realloc(glb.headerline_original, totalsize_original+linesize+1); @@ -1197,7 +1106,7 @@ int MIMEH_read_headers( struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_ if ((glb.save_headers)&&(glb.headerline)) { if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIME_read_headers:DEBUG: Saving header line.",FL); - fprintf(glb.header_file,"%s",glb.headerline); + fprintf(hinfo->header_file,"%s",glb.headerline); } if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIME_read_headers:DEBUG: Final Headers\n------------------\n%s---------------", FL,glb.headerline); //result = 1; @@ -1238,7 +1147,7 @@ int MIMEH_read_headers( struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_ if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIME_read_headers:DEBUG: Saving header line.",FL); MIMEH_fix_header_mistakes( glb.headerline ); MDECODE_decode_ISO( glb.headerline, totalsize ); - fprintf(glb.header_file,"%s",glb.headerline); + fprintf(hinfo->header_file,"%s",glb.headerline); } result = -1; @@ -2854,7 +2763,7 @@ int MIMEH_headers_clearcount( struct MIMEH_header_info *hinfo ) { Changes: \------------------------------------------------------------------*/ -int MIMEH_headers_get( struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata ) +int MIMEH_headers_get(FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original ) { int result = 0; @@ -2892,10 +2801,10 @@ int MIMEH_headers_get( struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_o snprintf( hinfo->content_type_string, _MIMEH_CONTENT_TYPE_MAX , "text/plain" ); - // Read from the file, the headers we need FFGET_set_watch_SDL(1); - result = MIMEH_read_headers(hinfo, f, unpack_metadata); + + result = MIMEH_read_headers(header_file, original_header_file, hinfo, f, unpack_metadata, save_headers_original); FFGET_set_watch_SDL(0); if (hinfo->lf_count > hinfo->crlf_count) { @@ -2969,7 +2878,7 @@ int MIMEH_headers_cleanup( void ) Changes: \------------------------------------------------------------------*/ -int MIMEH_parse_headers( FFGET_FILE *f, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata ) +int MIMEH_parse_headers( FILE* header_file, FILE* original_header_file, FFGET_FILE *f, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata, int save_headers_original ) { int result = 0; DMIMEH LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: Start [F=%p, hinfo=%p]\n", FL, f, hinfo); @@ -2979,7 +2888,7 @@ int MIMEH_parse_headers( FFGET_FILE *f, struct MIMEH_header_info *hinfo, RIPMIME /** Proceed to read, process and finish headers **/ DMIMEH LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: Getting headers",FL); - if ( result == 0 ) result = MIMEH_headers_get( hinfo, f, unpack_metadata ); + if ( result == 0 ) result = MIMEH_headers_get( header_file, original_header_file, hinfo, f, unpack_metadata, save_headers_original); DMIMEH LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: Processing headers",FL); if ( result == 0 ) result = MIMEH_headers_process( hinfo, glb.headerline ); DMIMEH LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: cleanup of headers",FL); @@ -3070,7 +2979,7 @@ int MIMEH_get_defect_count( struct MIMEH_header_info *hinfo ) Changes: \------------------------------------------------------------------*/ -int MIMEH_read_primary_headers( char *fname, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata ) +int MIMEH_read_primary_headers( FILE* header_file, FILE* original_header_file, char *fname, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata, int save_headers_original ) { FFGET_FILE F; FILE *f; @@ -3082,7 +2991,7 @@ int MIMEH_read_primary_headers( char *fname, struct MIMEH_header_info *hinfo, RI } FFGET_setstream(&F,f); - MIMEH_parse_headers(&F, hinfo, unpack_metadata); + MIMEH_parse_headers(header_file, original_header_file, &F, hinfo, unpack_metadata, save_headers_original); fclose(f); return 0; diff --git a/mime_headers.h b/mime_headers.h index 2268d00..a8d6ee7 100644 --- a/mime_headers.h +++ b/mime_headers.h @@ -97,6 +97,8 @@ struct MIMEH_header_info { + FILE *header_file; + FILE *original_header_file; int content_type; char content_type_string[ _MIMEH_CONTENT_TYPE_MAX + 1 ]; char content_description_string[ _MIMEH_CONTENT_DESCRIPTION_MAX + 1 ]; @@ -193,12 +195,12 @@ char *MIMEH_get_doubleCR_name( void ); int MIMEH_set_header_longsearch( int level ); int MIMEH_headers_clearcount( struct MIMEH_header_info *hinfo ); -int MIMEH_read_headers( struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata ); +int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original ); -int MIMEH_headers_get( struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata); +int MIMEH_headers_get(FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original ); int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ); int MIMEH_headers_cleanup(); -int MIMEH_parse_headers( FFGET_FILE *f, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata ); +int MIMEH_parse_headers( FILE* header_file, FILE* original_header_file, FFGET_FILE *f, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata, int save_headers_original ); int MIMEH_display_info( struct MIMEH_header_info *hinfo ); @@ -211,6 +213,6 @@ int MIMEH_get_defect_count( struct MIMEH_header_info *hinfo ); int MIMEH_set_report_MIME( int level ); -int MIMEH_read_primary_headers( char *fname, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata ); +int MIMEH_read_primary_headers( FILE* header_file, FILE* original_header_file, char *fname, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata, int save_headers_original ); #endif From 6f1589243ec70eefff4eb87a88a4a8c2df71b0c8 Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Wed, 30 Apr 2025 11:43:03 +0300 Subject: [PATCH 62/80] Move `save_headers` to functions params from globals --- mime.c | 6 +++--- mime_headers.c | 44 ++++++++++---------------------------------- mime_headers.h | 8 ++++---- 3 files changed, 17 insertions(+), 41 deletions(-) diff --git a/mime.c b/mime.c index 8a366cd..a35ef62 100644 --- a/mime.c +++ b/mime.c @@ -2602,7 +2602,7 @@ int MIME_unpack_stage2( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, st /** 20041216-1102:PLD: Keep attempting to read headers until we get a sane set **/ do { /** Read next set of headers, repeat until a sane set of headers are found **/ - result = MIMEH_parse_headers(NULL, NULL, input_f,h,unpack_metadata->dir, 0); + result = MIMEH_parse_headers(NULL, NULL, input_f,h,unpack_metadata->dir, 0, 0); DMIME LOGGER_log("%s:%d:%s:DEBUG: Parsing of headers done, sanity = %d, result = %d",FL,__func__,h->sanity, result); } while ((h->sanity == 0)&&(result != -1)); @@ -2703,7 +2703,7 @@ int MIME_unpack_stage2( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, st if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding headers...\n",FL,__func__); do { - result = MIMEH_parse_headers(NULL, NULL, input_f, h, unpack_metadata->dir, 0); + result = MIMEH_parse_headers(NULL, NULL, input_f, h, unpack_metadata->dir, 0, 0); } while ((h->sanity == 0)&&(result != -1)); glb.header_defect_count += MIMEH_get_defect_count(h); @@ -3070,7 +3070,7 @@ int MIME_unpack_single_file( RIPMIME_output *unpack_metadata, FILE *fi, int curr glb.current_line = 0; if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: recursion level checked...%d\n",FL,__func__, current_recursion_level); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: DumpHeaders = %d\n",FL,__func__, glb.save_headers); - if ((!hf)&&(glb.save_headers)&&(MIMEH_get_headers_save()==0)) + if ((!hf)&&(glb.save_headers)) { char * fn; int fn_l = strlen(unpack_metadata->dir) + strlen(glb.headersname) + sizeof(char) * 2; diff --git a/mime_headers.c b/mime_headers.c index e5ab148..3e88ee9 100644 --- a/mime_headers.c +++ b/mime_headers.c @@ -70,7 +70,6 @@ struct MIMEH_globals { char *headerline; char *headerline_original; // Holds the original header-form without decoding. - int save_headers; int test_mailbox; int debug; int webform; @@ -127,7 +126,6 @@ void MIMEH_init( void ) glb.headerline = NULL; glb.headerline_original = NULL; - glb.save_headers = 0; glb.test_mailbox = 0; glb.debug = 0; glb.webform = 0; @@ -389,28 +387,6 @@ int MIMEH_get_verbosity_contenttype( void ) return glb.verbose_contenttype; } - -/*-----------------------------------------------------------------\ - Function Name : MIMEH_get_headers_save - Returns Type : int - ----Parameter List - 1. void , - ------------------ - Exit Codes : - Side Effects : - -------------------------------------------------------------------- -Comments: - --------------------------------------------------------------------- -Changes: - -\------------------------------------------------------------------*/ -int MIMEH_get_headers_save( void ) -{ - return glb.save_headers; -} - - /*-----------------------------------------------------------------\ Function Name : MIMEH_get_headers_ptr Returns Type : int @@ -946,7 +922,7 @@ Purpose: Reads from the stream F until it detects a From line, or a blank Output: Errors: ------------------------------------------------------------------------*/ -int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original ) +int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers ) { char buffer[_MIMEH_STRLEN_MAX+1]; int totalsize=0; @@ -1103,7 +1079,7 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI MIMEH_fix_header_mistakes( glb.headerline ); MDECODE_decode_ISO( glb.headerline, totalsize ); - if ((glb.save_headers)&&(glb.headerline)) + if ((save_headers)&&(glb.headerline)) { if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIME_read_headers:DEBUG: Saving header line.",FL); fprintf(hinfo->header_file,"%s",glb.headerline); @@ -1140,9 +1116,9 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIME_read_headers:ERROR: FFGET module ran out of input while reading headers",FL); /** If we're meant to be saving the headers, we better do that now, even though we couldn't ** read everything we wanted to **/ - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIME_read_headers:DEBUG: save_headers=%d totalsize=%d headerline=%s", FL, glb.save_headers, totalsize, glb.headerline); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIME_read_headers:DEBUG: save_headers=%d totalsize=%d headerline=%s", FL, save_headers, totalsize, glb.headerline); - if ((glb.save_headers)&&(glb.headerline)) + if ((save_headers)&&(glb.headerline)) { if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIME_read_headers:DEBUG: Saving header line.",FL); MIMEH_fix_header_mistakes( glb.headerline ); @@ -2763,7 +2739,7 @@ int MIMEH_headers_clearcount( struct MIMEH_header_info *hinfo ) { Changes: \------------------------------------------------------------------*/ -int MIMEH_headers_get(FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original ) +int MIMEH_headers_get(FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers ) { int result = 0; @@ -2804,7 +2780,7 @@ int MIMEH_headers_get(FILE* header_file, FILE* original_header_file, struct MIM // Read from the file, the headers we need FFGET_set_watch_SDL(1); - result = MIMEH_read_headers(header_file, original_header_file, hinfo, f, unpack_metadata, save_headers_original); + result = MIMEH_read_headers(header_file, original_header_file, hinfo, f, unpack_metadata, save_headers_original, save_headers); FFGET_set_watch_SDL(0); if (hinfo->lf_count > hinfo->crlf_count) { @@ -2878,7 +2854,7 @@ int MIMEH_headers_cleanup( void ) Changes: \------------------------------------------------------------------*/ -int MIMEH_parse_headers( FILE* header_file, FILE* original_header_file, FFGET_FILE *f, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata, int save_headers_original ) +int MIMEH_parse_headers( FILE* header_file, FILE* original_header_file, FFGET_FILE *f, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers ) { int result = 0; DMIMEH LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: Start [F=%p, hinfo=%p]\n", FL, f, hinfo); @@ -2888,7 +2864,7 @@ int MIMEH_parse_headers( FILE* header_file, FILE* original_header_file, FFGET_FI /** Proceed to read, process and finish headers **/ DMIMEH LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: Getting headers",FL); - if ( result == 0 ) result = MIMEH_headers_get( header_file, original_header_file, hinfo, f, unpack_metadata, save_headers_original); + if ( result == 0 ) result = MIMEH_headers_get( header_file, original_header_file, hinfo, f, unpack_metadata, save_headers_original, save_headers); DMIMEH LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: Processing headers",FL); if ( result == 0 ) result = MIMEH_headers_process( hinfo, glb.headerline ); DMIMEH LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: cleanup of headers",FL); @@ -2979,7 +2955,7 @@ int MIMEH_get_defect_count( struct MIMEH_header_info *hinfo ) Changes: \------------------------------------------------------------------*/ -int MIMEH_read_primary_headers( FILE* header_file, FILE* original_header_file, char *fname, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata, int save_headers_original ) +int MIMEH_read_primary_headers( FILE* header_file, FILE* original_header_file, char *fname, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers ) { FFGET_FILE F; FILE *f; @@ -2991,7 +2967,7 @@ int MIMEH_read_primary_headers( FILE* header_file, FILE* original_header_file, c } FFGET_setstream(&F,f); - MIMEH_parse_headers(header_file, original_header_file, &F, hinfo, unpack_metadata, save_headers_original); + MIMEH_parse_headers(header_file, original_header_file, &F, hinfo, unpack_metadata, save_headers_original, save_headers); fclose(f); return 0; diff --git a/mime_headers.h b/mime_headers.h index a8d6ee7..ca8f2cb 100644 --- a/mime_headers.h +++ b/mime_headers.h @@ -195,12 +195,12 @@ char *MIMEH_get_doubleCR_name( void ); int MIMEH_set_header_longsearch( int level ); int MIMEH_headers_clearcount( struct MIMEH_header_info *hinfo ); -int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original ); +int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers ); -int MIMEH_headers_get(FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original ); +int MIMEH_headers_get(FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers ); int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ); int MIMEH_headers_cleanup(); -int MIMEH_parse_headers( FILE* header_file, FILE* original_header_file, FFGET_FILE *f, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata, int save_headers_original ); +int MIMEH_parse_headers( FILE* header_file, FILE* original_header_file, FFGET_FILE *f, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers ); int MIMEH_display_info( struct MIMEH_header_info *hinfo ); @@ -213,6 +213,6 @@ int MIMEH_get_defect_count( struct MIMEH_header_info *hinfo ); int MIMEH_set_report_MIME( int level ); -int MIMEH_read_primary_headers( FILE* header_file, FILE* original_header_file, char *fname, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata, int save_headers_original ); +int MIMEH_read_primary_headers( FILE* header_file, FILE* original_header_file, char *fname, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers ); #endif From 21a197f376df06dba8b489aca587e6112140df30 Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Wed, 30 Apr 2025 11:47:51 +0300 Subject: [PATCH 63/80] Remove unused --- mime_headers.c | 40 ---------------------------------------- 1 file changed, 40 deletions(-) diff --git a/mime_headers.c b/mime_headers.c index 3e88ee9..af2b1b0 100644 --- a/mime_headers.c +++ b/mime_headers.c @@ -387,46 +387,6 @@ int MIMEH_get_verbosity_contenttype( void ) return glb.verbose_contenttype; } -/*-----------------------------------------------------------------\ - Function Name : MIMEH_get_headers_ptr - Returns Type : int - ----Parameter List - 1. void , - ------------------ - Exit Codes : - Side Effects : - -------------------------------------------------------------------- -Comments: - --------------------------------------------------------------------- -Changes: - -\------------------------------------------------------------------*/ -char *MIMEH_get_headers_ptr( void ) -{ - return glb.headerline; -} - -/*-----------------------------------------------------------------\ - Function Name : *MIMEH_get_headers_original_ptr - Returns Type : char - ----Parameter List - 1. void , - ------------------ - Exit Codes : - Side Effects : - -------------------------------------------------------------------- -Comments: - --------------------------------------------------------------------- -Changes: - -\------------------------------------------------------------------*/ -char *MIMEH_get_headers_original_ptr( void ) -{ - return glb.headerline_original; -} - /*-----------------------------------------------------------------\ Function Name : MIMEH_set_header_longsearch Returns Type : int From efb74b680387dbd0dd363a5d75f2178d9b7d3961 Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Wed, 30 Apr 2025 12:29:48 +0300 Subject: [PATCH 64/80] Unglobal `headerline_original` used in one function only --- mime_headers.c | 82 ++++++++++++++++++++------------------------------ 1 file changed, 33 insertions(+), 49 deletions(-) diff --git a/mime_headers.c b/mime_headers.c index af2b1b0..9dc52d3 100644 --- a/mime_headers.c +++ b/mime_headers.c @@ -69,7 +69,6 @@ struct MIMEH_globals { char subject[_MIMEH_STRLEN_MAX +1]; char *headerline; - char *headerline_original; // Holds the original header-form without decoding. int test_mailbox; int debug; int webform; @@ -124,7 +123,6 @@ void MIMEH_init( void ) { glb.doubleCR = 0; glb.headerline = NULL; - glb.headerline_original = NULL; glb.test_mailbox = 0; glb.debug = 0; @@ -914,10 +912,10 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI ,FFGET_ftell(f) ); do { + char *headerline_original = NULL; // Holds the original header-form without decoding. search_count++; glb.headerline = NULL; - glb.headerline_original = NULL; tmp_original = NULL; while ((fget_result=FFGET_fgets(buffer,_MIMEH_STRLEN_MAX, f))) @@ -945,26 +943,26 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI if (save_headers_original) { if (MIMEH_DNORMAL) LOGGER_log("MIMEH_read_headers:DEBUG:Data-In:[%d:%d] '%s'", strlen(linestart), linesize, linestart); - tmp_original = realloc(glb.headerline_original, totalsize_original+linesize+1); + tmp_original = realloc(headerline_original, totalsize_original+linesize+1); if (tmp_original == NULL) { LOGGER_log("%s:%d:MIMEH_read_headers:ERROR: Cannot allocate %d bytes to contain new headers_original ", FL,totalsize_original +linesize +1); - if (glb.headerline_original != NULL) free(glb.headerline_original); - glb.headerline_original = NULL; + if (headerline_original != NULL) free(headerline_original); + headerline_original = NULL; return -1; } - if (glb.headerline_original == NULL) + if (headerline_original == NULL) { - glb.headerline_original = tmp_original; + headerline_original = tmp_original; totalsize_original = linesize +1; - PLD_strncpy( glb.headerline_original, linestart, (linesize+1)); - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_read_headers:DEBUG: '%s'", FL, glb.headerline_original); + PLD_strncpy( headerline_original, linestart, (linesize+1)); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_read_headers:DEBUG: '%s'", FL, headerline_original); } else { - glb.headerline_original = tmp_original; - PLD_strncpy( (glb.headerline_original +totalsize_original -1), linestart, (linesize +1)); + headerline_original = tmp_original; + PLD_strncpy( (headerline_original +totalsize_original -1), linestart, (linesize +1)); totalsize_original += linesize; - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_read_headers:DEBUG: HO = '%s'", FL, glb.headerline_original); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_read_headers:DEBUG: HO = '%s'", FL, headerline_original); } //LOGGER_log("DEBUG:linesize=%d data='%s'",linesize, linestart); } @@ -975,7 +973,8 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI if (tmp == NULL) { LOGGER_log("%s:%d:MIMEH_read_headers:ERROR: Cannot allocate %d bytes to contain new headers ", FL,totalsize +linesize +1); - if (glb.headerline != NULL) free(glb.headerline); + if (glb.headerline != NULL) + free(glb.headerline); glb.headerline = NULL; return -1; } @@ -1096,7 +1095,16 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI { /** If not RFC822 headers, then clean up everything we allocated in here **/ DMIMEH LOGGER_log("%s:%d:MIME_read_headers:DEBUG: No RFC822 headers detected, cleanup.", FL); - MIMEH_headers_cleanup(); + if (headerline_original != NULL) + { + free(headerline_original); + headerline_original = NULL; + } + if (glb.headerline != NULL) + { + free(glb.headerline); + glb.headerline = NULL; + } } } } @@ -2751,7 +2759,11 @@ int MIMEH_headers_get(FILE* header_file, FILE* original_header_file, struct MIM // flag this, free up the headers, and return. if (result == -1) { - if (glb.headerline) free(glb.headerline); + if (glb.headerline) + { + free(glb.headerline); + glb.headerline = NULL; + } return result; } @@ -2766,38 +2778,6 @@ int MIMEH_headers_get(FILE* header_file, FILE* original_header_file, struct MIM return result; } -/*-----------------------------------------------------------------\ - Function Name : MIMEH_headers_cleanup - Returns Type : int - ----Parameter List - 1. void , - ------------------ - Exit Codes : - Side Effects : - -------------------------------------------------------------------- -Comments: - --------------------------------------------------------------------- -Changes: - -\------------------------------------------------------------------*/ -int MIMEH_headers_cleanup( void ) -{ - if (glb.headerline != NULL) - { - free(glb.headerline); - glb.headerline = NULL; - } - - if (glb.headerline_original != NULL) - { - free(glb.headerline_original); - glb.headerline_original = NULL; - } - - return 0; -} - /*-----------------------------------------------------------------\ Function Name : MIMEH_parse_headers Returns Type : int @@ -2828,7 +2808,11 @@ int MIMEH_parse_headers( FILE* header_file, FILE* original_header_file, FFGET_FI DMIMEH LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: Processing headers",FL); if ( result == 0 ) result = MIMEH_headers_process( hinfo, glb.headerline ); DMIMEH LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: cleanup of headers",FL); - if ( result == 0 ) result = MIMEH_headers_cleanup(); + if (glb.headerline != NULL) + { + free(glb.headerline); + glb.headerline = NULL; + } if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: END [F=%p, hinfo=%p, sanity=%d]\n", FL, f, hinfo, hinfo->sanity); return result; From c358d3c6c8819bb316dc40d81333b663b71af06c Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Wed, 30 Apr 2025 13:02:02 +0300 Subject: [PATCH 65/80] Prepare `headerline` removing from globals --- mime_headers.c | 12 ++++++++---- mime_headers.h | 6 +++--- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/mime_headers.c b/mime_headers.c index 9dc52d3..e0ef5ec 100644 --- a/mime_headers.c +++ b/mime_headers.c @@ -59,6 +59,10 @@ char *MIMEH_defect_description_array[_MIMEH_DEFECT_ARRAY_SIZE]; +int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers, char* headerline ); +int MIMEH_headers_get(FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers, char* headerline ); +int MIMEH_read_primary_headers( FILE* header_file, FILE* original_header_file, char *fname, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers ); + struct MIMEH_globals { int doubleCR; int doubleCR_save; @@ -880,7 +884,7 @@ Purpose: Reads from the stream F until it detects a From line, or a blank Output: Errors: ------------------------------------------------------------------------*/ -int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers ) +int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers, char* headerline ) { char buffer[_MIMEH_STRLEN_MAX+1]; int totalsize=0; @@ -2707,7 +2711,7 @@ int MIMEH_headers_clearcount( struct MIMEH_header_info *hinfo ) { Changes: \------------------------------------------------------------------*/ -int MIMEH_headers_get(FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers ) +int MIMEH_headers_get(FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers, char* headerline ) { int result = 0; @@ -2748,7 +2752,7 @@ int MIMEH_headers_get(FILE* header_file, FILE* original_header_file, struct MIM // Read from the file, the headers we need FFGET_set_watch_SDL(1); - result = MIMEH_read_headers(header_file, original_header_file, hinfo, f, unpack_metadata, save_headers_original, save_headers); + result = MIMEH_read_headers(header_file, original_header_file, hinfo, f, unpack_metadata, save_headers_original, save_headers, glb.headerline); FFGET_set_watch_SDL(0); if (hinfo->lf_count > hinfo->crlf_count) { @@ -2804,7 +2808,7 @@ int MIMEH_parse_headers( FILE* header_file, FILE* original_header_file, FFGET_FI /** Proceed to read, process and finish headers **/ DMIMEH LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: Getting headers",FL); - if ( result == 0 ) result = MIMEH_headers_get( header_file, original_header_file, hinfo, f, unpack_metadata, save_headers_original, save_headers); + if ( result == 0 ) result = MIMEH_headers_get( header_file, original_header_file, hinfo, f, unpack_metadata, save_headers_original, save_headers, glb.headerline); DMIMEH LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: Processing headers",FL); if ( result == 0 ) result = MIMEH_headers_process( hinfo, glb.headerline ); DMIMEH LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: cleanup of headers",FL); diff --git a/mime_headers.h b/mime_headers.h index ca8f2cb..128d61c 100644 --- a/mime_headers.h +++ b/mime_headers.h @@ -195,11 +195,11 @@ char *MIMEH_get_doubleCR_name( void ); int MIMEH_set_header_longsearch( int level ); int MIMEH_headers_clearcount( struct MIMEH_header_info *hinfo ); -int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers ); -int MIMEH_headers_get(FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers ); + int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ); int MIMEH_headers_cleanup(); + int MIMEH_parse_headers( FILE* header_file, FILE* original_header_file, FFGET_FILE *f, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers ); @@ -213,6 +213,6 @@ int MIMEH_get_defect_count( struct MIMEH_header_info *hinfo ); int MIMEH_set_report_MIME( int level ); -int MIMEH_read_primary_headers( FILE* header_file, FILE* original_header_file, char *fname, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers ); + #endif From 42f70f8cf98998d93f4806a3a98d8619be54dcd3 Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Wed, 30 Apr 2025 13:17:11 +0300 Subject: [PATCH 66/80] Refactor MIME headers debug --- mime_headers.c | 224 ++++++++++++++++++++++++------------------------- 1 file changed, 112 insertions(+), 112 deletions(-) diff --git a/mime_headers.c b/mime_headers.c index e0ef5ec..cffc0ad 100644 --- a/mime_headers.c +++ b/mime_headers.c @@ -436,7 +436,7 @@ int MIMEH_set_defect( struct MIMEH_header_info *hinfo, int defect ) { hinfo->defects[defect]++; hinfo->header_defect_count++; - DMIMEH LOGGER_log("%s:%d:MIMEH_set_defect:DEBUG: Setting defect index '%d' to '%d'",FL, defect, hinfo->defects[defect]); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Setting defect index '%d' to '%d'",FL, __func__, defect, hinfo->defects[defect]); } return 0; } @@ -523,11 +523,11 @@ int MIMEH_are_headers_RFC822( char *headers ) if (headers == NULL) { - DMIMEH LOGGER_log("%s:%d:MIMEH_are_headers_RFC822:DEBUG: Headers are NULL"); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Headers are NULL"); return 0; } - DMIMEH LOGGER_log("%s:%d:MIMEH_are_headers_RFC822:DEBUG:----\n%s\n----",FL,headers); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG:----\n%s\n----",FL,headers); lc_headers = strdup(headers); if (lc_headers == NULL) return 0; @@ -535,13 +535,13 @@ int MIMEH_are_headers_RFC822( char *headers ) //PLD_strlower((unsigned char *)lc_headers); PLD_strlower(lc_headers); - DMIMEH LOGGER_log("%s:%d:MIMEH_are_headers_RFC822:DEBUG:----(lowercase)----\n%s\n----",FL,lc_headers); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG:----(lowercase)----\n%s\n----",FL,lc_headers); for (condition_item=0; condition_item < 6; condition_item++) { char *p; - DMIMEH LOGGER_log("%s:%d:MIMEH_are_headers_RFC822:DEBUG: Condition test item[%d] = '%s'",FL,condition_item,conditions[condition_item]); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Condition test item[%d] = '%s'",FL,condition_item,conditions[condition_item]); p = strstr(lc_headers, conditions[condition_item]); if (p != NULL) { @@ -584,7 +584,7 @@ int MIMEH_save_doubleCR( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct cur_mime = MIME_element_add (NULL, unpack_metadata, glb.doubleCRname, "doubleCR", NULL, "doubleCR", hinfo->current_recursion_level + 1, 0, 0, __func__); - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIME_save_doubleCR:DEBUG: Saving DoubleCR header: %s\n", FL,glb.doubleCRname); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Saving DoubleCR header: %s\n", FL,glb.doubleCRname); while (1) { c = FFGET_fgetc(f); @@ -675,7 +675,7 @@ int MIMEH_strip_comments( char *input ) { int stop_searching = 0; - DMIMEH LOGGER_log("%s:%d:MIMEH_strip_comments:DEBUG: Located open ( at %s",FL,p); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Located open ( at %s",FL,p); // If we did locate an opening parenthesis, look for the closing one // NOTE - we cannot have a breaking \n or \r inbetween // q = strpbrk(p, ")\n\r"); @@ -699,7 +699,7 @@ int MIMEH_strip_comments( char *input ) break; case ')': - DMIMEH LOGGER_log("%s:%d:MIMEH_strip_comments:DEBUG: Located closing ) at %s",FL,q); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Located closing ) at %s",FL,q); if (in_quote == 0) stop_searching = 1; break; } @@ -720,7 +720,7 @@ int MIMEH_strip_comments( char *input ) // Move q to the first char after the closing parenthesis q++; - DMIMEH LOGGER_log("%s:%d:MIMEH_strip_comments:DEBUG: located closing ) at %s ",FL, q); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: located closing ) at %s ",FL, __func__, q); // While there's more chars in string, copy them to where // the opening parenthesis is while (*q != '\0') @@ -729,7 +729,7 @@ int MIMEH_strip_comments( char *input ) p++; q++; } // While q != '\0' - DMIMEH LOGGER_log("%s:%d:MIMEH_strip_comments:DEBUG: char copy done",FL); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: char copy done",FL, __func__); // Terminate the string *p = '\0'; @@ -739,7 +739,7 @@ int MIMEH_strip_comments( char *input ) } // if p == NULL } while ((p != NULL)&&(p_org != NULL)); // do-while more comments to remove - DMIMEH LOGGER_log("%s:%d:MIMEH_strip_comments:DEBUG: Final string = '%s'",FL,input); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Final string = '%s'",FL, __func__, input); return 0; } @@ -811,7 +811,7 @@ int MIMEH_fix_header_mistakes( char *data ) int result = 0; char *p; - DMIMEH LOGGER_log("%s:%d:MIMEH_fix_header_mistakes:DEBUG: Checking and fixing headers in '%s'",FL,data); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Checking and fixing headers in '%s'",FL, __func__, data); if (glb.header_fix == 0) return result; @@ -825,7 +825,7 @@ int MIMEH_fix_header_mistakes( char *data ) q = p+1; - DMIMEH LOGGER_log("%s:%d:MIMEH_fix_header_mistakes:DEBUG: Located ';' at offset %d '%20s",FL, p -data, p); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Located ';' at offset %d '%20s",FL, __func__, p -data, p); if ((*q == '\n')||(*q == '\r')) { nonblank_detected = 0; @@ -852,7 +852,7 @@ int MIMEH_fix_header_mistakes( char *data ) } /** ELSE - if *q wasn't a line break char **/ if (nonblank_detected == 1) { - DMIMEH LOGGER_log("%s:%d:MIMEH_fix_header_mistakes:DEBUG: Line was normal/safe, continue...",FL); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Line was normal/safe, continue...",FL, __func__); p++; continue; } /** if nonblank_detected == 1 **/ @@ -860,11 +860,11 @@ int MIMEH_fix_header_mistakes( char *data ) ** line, then we need to pull up the next line **/ if (*q != '\0') { if(!MIMEH_check_ct(q)){ - DMIMEH LOGGER_log("%s:%d:MIMEH_fix_header_mistakes:DEBUG: Line needs fixing",FL); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Line needs fixing",FL, __func__); *q = ' '; q++; if ((*q == '\n')||(*q == '\r')) *q = ' '; - DMIMEH LOGGER_log("%s:%d:MIMEH_fix_header_mistakes:DEBUG: Line fixed",FL); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Line fixed",FL, __func__); p = q; }else{ p++; @@ -872,7 +872,7 @@ int MIMEH_fix_header_mistakes( char *data ) } /** If q wasn't the end of data **/ } /** while looking for more ';' chars **/ - DMIMEH LOGGER_log("%s:%d:MIMEH_fix_header_mistakes:DEBUG: Done",FL); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Done",FL, __func__); return result; } @@ -910,7 +910,7 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI to be overly intelligent about interpreting data. **/ - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_read_headers:DEBUG: File position = %ld [0x%0X]" + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: File position = %ld [0x%0X]" ,FL ,FFGET_ftell(f) ,FFGET_ftell(f) @@ -932,13 +932,13 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI else if (strstr(linestart,"\r\r")) hinfo->crcr_count++; else if (strchr(linestart,'\n')) hinfo->lf_count++; - if (MIMEH_DNORMAL)LOGGER_log("%s:%d:MIMEH_read_headers: [CRLF=%d, CRCR=%d, LF=%d] Data In=[sz=%d:tb=%d:mem=%p]'%s'",FL, hinfo->crlf_count, hinfo->crcr_count, hinfo->lf_count, linesize, f->trueblank, glb.headerline, buffer); + if (MIMEH_DNORMAL)LOGGER_log("%s:%d:%s: [CRLF=%d, CRCR=%d, LF=%d] Data In=[sz=%d:tb=%d:mem=%p]'%s'",FL, __func__, hinfo->crlf_count, hinfo->crcr_count, hinfo->lf_count, linesize, f->trueblank, glb.headerline, buffer); // If we are being told to copy the input data to an output file // then do so here (this is for the originals) if (hinfo->original_header_file != NULL) { - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_read_headers:DEBUG: saving to file...",FL); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: saving to file...",FL, __func__); fprintf(hinfo->original_header_file,"%s",linestart); } @@ -950,7 +950,7 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI tmp_original = realloc(headerline_original, totalsize_original+linesize+1); if (tmp_original == NULL) { - LOGGER_log("%s:%d:MIMEH_read_headers:ERROR: Cannot allocate %d bytes to contain new headers_original ", FL,totalsize_original +linesize +1); + LOGGER_log("%s:%d:%s:ERROR: Cannot allocate %d bytes to contain new headers_original ", FL, __func__, totalsize_original +linesize +1); if (headerline_original != NULL) free(headerline_original); headerline_original = NULL; return -1; @@ -961,22 +961,22 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI headerline_original = tmp_original; totalsize_original = linesize +1; PLD_strncpy( headerline_original, linestart, (linesize+1)); - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_read_headers:DEBUG: '%s'", FL, headerline_original); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: '%s'", FL, __func__, headerline_original); } else { headerline_original = tmp_original; PLD_strncpy( (headerline_original +totalsize_original -1), linestart, (linesize +1)); totalsize_original += linesize; - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_read_headers:DEBUG: HO = '%s'", FL, headerline_original); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: HO = '%s'", FL, __func__, headerline_original); } //LOGGER_log("DEBUG:linesize=%d data='%s'",linesize, linestart); } /** Normal processing of the headers now starts. **/ - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_read_headers:DEBUG: realloc'ing dataspace",FL); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: realloc'ing dataspace",FL, __func__); tmp = realloc(glb.headerline, totalsize+linesize+1); if (tmp == NULL) { - LOGGER_log("%s:%d:MIMEH_read_headers:ERROR: Cannot allocate %d bytes to contain new headers ", FL,totalsize +linesize +1); + LOGGER_log("%s:%d:%s:ERROR: Cannot allocate %d bytes to contain new headers ", FL, __func__, totalsize +linesize +1); if (glb.headerline != NULL) free(glb.headerline); glb.headerline = NULL; @@ -985,14 +985,14 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI if (glb.headerline == NULL) { - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_read_headers:DEBUG: Initial appending of head to dataspace headerline = NULL realloc block = %p linestart = %p linesize = %d",FL, tmp, linestart, linesize); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Initial appending of head to dataspace headerline = NULL realloc block = %p linestart = %p linesize = %d",FL, __func__, tmp, linestart, linesize); glb.headerline = tmp; totalsize = linesize; PLD_strncpy(glb.headerline, linestart, (linesize +1)); } // If the global headerline is currently NULL else { - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_read_headers:DEBUG: Appending of new data to existing header existing-headerline = %p new realloc block = %p linestart = %p linesize = %d",FL, glb.headerline, tmp, linestart, linesize); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Appending of new data to existing header existing-headerline = %p new realloc block = %p linestart = %p linesize = %d",FL, __func__, glb.headerline, tmp, linestart, linesize); // Perform header unfolding by removing any CRLF's // of the last line if the first characters of the @@ -1015,10 +1015,10 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI // we are going to append the newly read line p = glb.headerline +totalsize -1; - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_read_headers:DEBUG: unwrapping headers headers=%p, p = %p",FL,glb.headerline, p); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: unwrapping headers headers=%p, p = %p",FL, __func__, glb.headerline, p); while ((p >= glb.headerline)&&(( *p == '\n' )||( *p == '\r' ))) { - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_read_headers:DEBUG: Removing trailing space p=[%p]%c",FL, p, *p); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Removing trailing space p=[%p]%c",FL, __func__, p, *p); *p = '\0'; p--; totalsize--; @@ -1027,7 +1027,7 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI p = glb.headerline +totalsize -1; } - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_read_headers:DEBUG: Memcopying line, source = %p, dest = %p, size = %d", FL, linestart, glb.headerline +totalsize, linesize); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Memcopying line, source = %p, dest = %p, size = %d", FL, __func__, linestart, glb.headerline +totalsize, linesize); memcpy((glb.headerline +totalsize), linestart, (linesize)); totalsize += linesize; *(glb.headerline +totalsize) = '\0'; @@ -1036,18 +1036,18 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI if (f->trueblank) { - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIME_read_headers:DEBUG: Trueblank line detected in header reading",FL); - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIME_read_headers:DEBUG: Headers /before/ decoding\n-------\n%s\n-------------------",FL, glb.headerline); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Trueblank line detected in header reading",FL, __func__); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Headers /before/ decoding\n-------\n%s\n-------------------",FL, __func__, glb.headerline); MIMEH_fix_header_mistakes( glb.headerline ); MDECODE_decode_ISO( glb.headerline, totalsize ); if ((save_headers)&&(glb.headerline)) { - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIME_read_headers:DEBUG: Saving header line.",FL); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Saving header line.",FL, __func__); fprintf(hinfo->header_file,"%s",glb.headerline); } - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIME_read_headers:DEBUG: Final Headers\n------------------\n%s---------------", FL,glb.headerline); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Final Headers\n------------------\n%s---------------", FL, __func__, glb.headerline); //result = 1; //result = 0; break; @@ -1076,14 +1076,14 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI if (!fget_result) { - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIME_read_headers:ERROR: FFGET module ran out of input while reading headers",FL); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:ERROR: FFGET module ran out of input while reading headers",FL, __func__); /** If we're meant to be saving the headers, we better do that now, even though we couldn't ** read everything we wanted to **/ - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIME_read_headers:DEBUG: save_headers=%d totalsize=%d headerline=%s", FL, save_headers, totalsize, glb.headerline); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: save_headers=%d totalsize=%d headerline=%s", FL, __func__, save_headers, totalsize, glb.headerline); if ((save_headers)&&(glb.headerline)) { - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIME_read_headers:DEBUG: Saving header line.",FL); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Saving header line.",FL, __func__); MIMEH_fix_header_mistakes( glb.headerline ); MDECODE_decode_ISO( glb.headerline, totalsize ); fprintf(hinfo->header_file,"%s",glb.headerline); @@ -1098,7 +1098,7 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI if (is_RFC822_headers == 0) { /** If not RFC822 headers, then clean up everything we allocated in here **/ - DMIMEH LOGGER_log("%s:%d:MIME_read_headers:DEBUG: No RFC822 headers detected, cleanup.", FL); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: No RFC822 headers detected, cleanup.", FL, __func__); if (headerline_original != NULL) { free(headerline_original); @@ -1114,7 +1114,7 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI } } while ((is_RFC822_headers==0)&&(glb.header_longsearch>0)&&(result==0)&&(search_count 0) { *s = *(s+1); @@ -1627,7 +1627,7 @@ int MIMEH_parse_header_parameter( struct MIMEH_header_info *hinfo, char *data, // Now that our string is all cleaned up, save it to our output value snprintf( output_value, output_value_size, "%s", string ); - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_header_parameter:DEBUG: Final value = '%s'",FL, output_value); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Final value = '%s'",FL, __func__, output_value); } // If the first non-whitespace char wasn't a '=' } // If the first char after the search-string wasn't a '*' @@ -1638,7 +1638,7 @@ int MIMEH_parse_header_parameter( struct MIMEH_header_info *hinfo, char *data, if (hl != NULL) free(hl); - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_header_parameter:DEBUG: [return=%d] Done seeking for '%s' data_end_point=%p (from %p)",FL, return_value, searchstr, *data_end_point, data); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: [return=%d] Done seeking for '%s' data_end_point=%p (from %p)",FL, __func__, return_value, searchstr, *data_end_point, data); return return_value; } @@ -1764,12 +1764,12 @@ int MIMEH_parse_contenttype( char *header_name, char *header_value, struct MIMEH // CONTENT TYPE ------------------------------- - DMIMEH LOGGER_log("%s:%d:MIMEH_parse_contenttype:DEBUG: Start",FL); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Start",FL, __func__); p = strstr(header_name,"content-type"); if (p != NULL) { - DMIMEH LOGGER_log("%s:%d:MIMEH_parse_contenttype: Content-type string found in header-name",FL); + DMIMEH LOGGER_log("%s:%d:%s: Content-type string found in header-name",FL, __func__); /** 20041216-1106:PLD: Increase our sanity **/ hinfo->sanity++; @@ -1821,7 +1821,7 @@ int MIMEH_parse_contenttype( char *header_name, char *header_value, struct MIMEH ** with that char normally, we'll convert it to something ** else **/ - DMIMEH LOGGER_log("%s:%d:MIMEH_parse_contenttype:DEBUG: Located x-mac attachment",FL); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Located x-mac attachment",FL, __func__); hinfo->x_mac = 1; FNFILTER_set_mac(hinfo->x_mac); } @@ -1900,16 +1900,16 @@ int MIMEH_parse_contenttype( char *header_name, char *header_value, struct MIMEH // Move the parameter search point up to where we stopped // processing the data in the MIMEH_parse_header_parameter() call - DMIMEH LOGGER_log("%s:%d:MIMEH_parse_contenttype:DEBUG: Pushing new filename to stack '%s'",FL, hinfo->name); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Pushing new filename to stack '%s'",FL, __func__, hinfo->name); /** Step 1: Check to see if this filename already ** exists in the stack. We do this so that we don't ** duplicate entries and also to prevent false ** bad-header reports. **/ if (SS_cmp(&(hinfo->ss_names), hinfo->name, strlen(hinfo->name))==NULL) { - DMIMEH LOGGER_log("%s:%d:MIMEH_parse_contenttype:DEBUG: Filtering '%s'",FL, hinfo->name); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Filtering '%s'",FL, __func__, hinfo->name); FNFILTER_filter(hinfo->name, _MIMEH_FILENAMELEN_MAX); - DMIMEH LOGGER_log("%s:%d:MIMEH_parse_contenttype:DEBUG: Pushing '%s'",FL, hinfo->name); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Pushing '%s'",FL, __func__, hinfo->name); SS_push(&(hinfo->ss_names),hinfo->name,strlen(hinfo->name)); if (SS_count(&(hinfo->ss_names)) > 1) { @@ -1927,10 +1927,10 @@ int MIMEH_parse_contenttype( char *header_name, char *header_value, struct MIMEH ** Look for the MIME Boundary specification in the headers **/ return_value = MIMEH_parse_header_parameter(hinfo, param, "boundary", hinfo->boundary, sizeof(hinfo->boundary), &data_end_point); - DMIMEH LOGGER_log("%s:%d:MIMEH_parse_contenttype:DEBUG: Param<=>data_end gap = %d", FL,data_end_point -param); - DMIMEH LOGGER_log("%s:%d:MIMEH_parse_contenttype:DEBUG: param start pos = '%s'",FL, param); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Param<=>data_end gap = %d", FL,data_end_point -param); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: param start pos = '%s'",FL, __func__, param); if (data_end_point > param) param = data_end_point; - DMIMEH LOGGER_log("%s:%d:MIMEH_parse_contenttype:DEBUG: param start pos = '%s'",FL, param); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: param start pos = '%s'",FL, __func__, param); if ( return_value == 0 ) { // Move the parameter search point up to where we stopped @@ -1938,9 +1938,9 @@ int MIMEH_parse_contenttype( char *header_name, char *header_value, struct MIMEH //hinfo->boundary_located = 1; hinfo->boundary_located++; - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_contenttype:DEBUG: Pushed boundary to stack (%s)",FL, hinfo->boundary); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Pushed boundary to stack (%s)",FL, __func__, hinfo->boundary); BS_push(hinfo->boundary); - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_contenttype:DEBUG: Setting hinfo->boundary_located to %d",FL, hinfo->boundary_located ); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Setting hinfo->boundary_located to %d",FL, __func__, hinfo->boundary_located ); if (hinfo->boundary_located > 1) { @@ -1955,9 +1955,9 @@ int MIMEH_parse_contenttype( char *header_name, char *header_value, struct MIMEH //param = PLD_strtok( &tx, NULL, ";\n\r" ); // * PLD:20040831-22H15: Added 'if (param != NULL)' prefix to debugging lines // * In response to bug #32, submitted by ICL ZA - if (param != NULL) DMIMEH LOGGER_log("%s:%d:MIMEH_parse_contenttype:DEBUG: param start pos = '%s'",FL, param); + if (param != NULL) DMIMEH LOGGER_log("%s:%d:%s:DEBUG: param start pos = '%s'",FL, __func__, param); param = strpbrk( param, ";\n\r " ); - if (param != NULL) DMIMEH LOGGER_log("%s:%d:MIMEH_parse_contenttype:DEBUG: param start pos = '%s'",FL, param); + if (param != NULL) DMIMEH LOGGER_log("%s:%d:%s:DEBUG: param start pos = '%s'",FL, __func__, param); } // While } @@ -1965,7 +1965,7 @@ int MIMEH_parse_contenttype( char *header_name, char *header_value, struct MIMEH if (hv != NULL) free(hv); - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_contenttype:DEBUG: end.",FL); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: end.",FL, __func__); return 0; } @@ -1999,7 +1999,7 @@ int MIMEH_parse_contentlocation( char *header_name, char *header_value, struct M /** 20041216-1108:PLD: Increase our sanity **/ hinfo->sanity++; - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIME_parse_contentlocation:DEBUG: Content Location line found - '%s'\n", FL, header_value); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Content Location line found - '%s'\n", FL, __func__, header_value); p = q = header_value; @@ -2011,7 +2011,7 @@ int MIMEH_parse_contentlocation( char *header_name, char *header_value, struct M if (p) { - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIME_parse_contentlocation:DEBUG: filename = %s\n", FL, p); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: filename = %s\n", FL, __func__, p); snprintf(hinfo->name, sizeof(hinfo->name),"%s",p); snprintf(hinfo->filename, sizeof(hinfo->filename),"%s",p); FNFILTER_filter(hinfo->filename, _MIMEH_FILENAMELEN_MAX); @@ -2064,27 +2064,27 @@ int MIMEH_parse_contenttransferencoding( char *header_name, char *header_value, if (strstr(p,"base64")) { hinfo->content_transfer_encoding = _CTRANS_ENCODING_B64; - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_contenttransferencoding: Encoding set to BASE64", FL); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s: Encoding set to BASE64", FL, __func__); } else if (strstr(p,"7bit")) { hinfo->content_transfer_encoding = _CTRANS_ENCODING_7BIT; - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_contenttransferencoding: Encoding set to 7-BIT ", FL); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s: Encoding set to 7-BIT ", FL, __func__); } else if (strstr(p,"8bit")) { hinfo->content_transfer_encoding = _CTRANS_ENCODING_8BIT; - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_contenttransferencoding: Encoding set to 8-BIT", FL); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s: Encoding set to 8-BIT", FL, __func__); } else if (strstr(p,"quoted-printable")) { hinfo->content_transfer_encoding = _CTRANS_ENCODING_QP; - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_contenttransferencoding: Encoding set to Quoted-Printable", FL); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s: Encoding set to Quoted-Printable", FL, __func__); } else if (strstr(p,"binary")) { hinfo->content_transfer_encoding = _CTRANS_ENCODING_BINARY; - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_contenttransferencoding: Encoding set to Binary", FL); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s: Encoding set to Binary", FL, __func__); } else if ( (strstr(p,"uu")) @@ -2093,7 +2093,7 @@ int MIMEH_parse_contenttransferencoding( char *header_name, char *header_value, ) { hinfo->content_transfer_encoding = _CTRANS_ENCODING_UUENCODE; - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_contenttransferencoding: Encoding set to UUENCODE", FL); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s: Encoding set to UUENCODE", FL, __func__); } else hinfo->content_transfer_encoding = _CTRANS_ENCODING_RAW; @@ -2199,7 +2199,7 @@ int MIMEH_parse_contentdisposition( char *header_name, char *header_value, struc *q = '\0'; } - DMIMEH LOGGER_log("%s:%d:MIMEH_parse_contentdisposition:DEBUG: Disposition string = '%s'",FL, hv); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Disposition string = '%s'",FL, __func__, hv); // Commence to decode the disposition string into its components. p = strpbrk( hv, ";\t\n\r " ); @@ -2217,7 +2217,7 @@ int MIMEH_parse_contentdisposition( char *header_name, char *header_value, struc int parse_result; char *data_end_point; - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_contentdisposition:DEBUG: Parsing '%s'",FL,param); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Parsing '%s'",FL,param); // Seek out possible 'filename' parameters @@ -2247,7 +2247,7 @@ int MIMEH_parse_contentdisposition( char *header_name, char *header_value, struc if ( hinfo->content_type == _CTYPE_MULTIPART_APPLEDOUBLE ) { snprintf( glb.appledouble_filename, sizeof(glb.appledouble_filename), "%s", hinfo->filename ); - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_contentdisposition:DEBUG: Setting appledouble filename to: '%s'",FL,glb.appledouble_filename); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Setting appledouble filename to: '%s'",FL,glb.appledouble_filename); } } // If the header-value contained ;'s ( indicating parameters ) @@ -2281,7 +2281,7 @@ int MIMEH_parse_generic( char *header_name, char *header_value, struct MIMEH_hea int compare_result = 0; int tlen; - DMIMEH LOGGER_log("%s:%d:MIMEH_parse_generic:DEBUG: Searching for %s in %s",FL,tokenstr,header_name); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Searching for %s in %s",FL,tokenstr,header_name); /** Sanity check the parameters **/ if (hinfo == NULL) return -1; if (tokenstr == NULL) return -1; @@ -2300,7 +2300,7 @@ int MIMEH_parse_generic( char *header_name, char *header_value, struct MIMEH_hea case ' ': case '\t': case '\0': - DMIMEH LOGGER_log("%s:%d:MIMEH_parse_generic:DEBUG: Located! Sanity up +1",FL); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Located! Sanity up +1",FL, __func__); snprintf( buffer, bsize, "%s", header_value ); hinfo->sanity++; break; @@ -2465,7 +2465,7 @@ int MIMEH_parse_charset( char *header_name, char *header_value, struct MIMEH_hea int result = 0; result = MIMEH_parse_generic( header_name, header_value, hinfo, "charset", hinfo->charset, sizeof(hinfo->charset) ); - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_charset:DEBUG: Charset value = '%s'", FL, hinfo->charset); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Charset value = '%s'", FL, __func__, hinfo->charset); return result; } @@ -2494,7 +2494,7 @@ int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ) char *current_header_position; int headerlength; - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: Start [hinfo=%p]\n",FL, hinfo); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Start [hinfo=%p]\n",FL, __func__, hinfo); h = headers; @@ -2505,7 +2505,7 @@ int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ) safehl = malloc(sizeof(char) *(headerlength+1)); PLD_strncpy(safehl, h, headerlength+1); - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIME_parse_headers:DEBUG: Header length = %d\n", FL,headerlength); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Header length = %d\n", FL,headerlength); MIMEH_strip_comments(h); @@ -2520,7 +2520,7 @@ int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ) char *header_name_end_position; char *header_value_end_position; - DMIMEH LOGGER_log("%s:%d:MIMEH_headers_process:DEBUG: Processing '%s'",FL,current_header_position); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Processing '%s'",FL,current_header_position); /** Tokenise for the header 'name', ie, content-type, subject etc **/ header_name = current_header_position; @@ -2536,11 +2536,11 @@ int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ) // NOTE: this may activate on the true-blank lines, hence why we // dump the source string, just for confirmation - DMIMEH LOGGER_log("%s:%d:MIMEH_headers_process:DEBUG: Could not locate ':' separator, using whitespace (source='%s')",FL,header_name); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Could not locate ':' separator, using whitespace (source='%s')",FL,header_name); header_name_end_position = strpbrk( header_name, "\t " ); if (header_name_end_position == NULL) { - DMIMEH LOGGER_log("%s:%d:MIMEH_headers_process:DEBUG: Cannot find a header name:value pair in '%s'",FL, header_name); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Cannot find a header name:value pair in '%s'",FL, __func__, header_name); } } @@ -2579,7 +2579,7 @@ int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ) // we will 'ignore' them rather than throwing up our arms. This // ensures that we are not made to break over spurilous data. - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: This line contains no header:value pair (%s)", FL, current_header_position); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: This line contains no header:value pair (%s)", FL, __func__, current_header_position); continue; } else { @@ -2596,8 +2596,8 @@ int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ) *header_value_end_position = '\0'; if (MIMEH_DNORMAL) { - LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: Header Name ='%s'", FL, header_name ); - LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: Header Value='%s'", FL, header_value ); + LOGGER_log("%s:%d:%s:DEBUG: Header Name ='%s'", FL, __func__, header_name ); + LOGGER_log("%s:%d:%s:DEBUG: Header Value='%s'", FL, __func__, header_value ); } // To make parsing simpler, convert our @@ -2627,7 +2627,7 @@ int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ) } } else { - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_headerss:DEBUG: Header value end position is NULL",FL); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%ss:DEBUG: Header value end position is NULL",FL, __func__); } } } // while @@ -2664,9 +2664,9 @@ int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ) { free(safehl); } - else LOGGER_log("%s:%d:MIME_parse_headers:WARNING: Unable to free HEADERS allocated memory\n", FL); + else LOGGER_log("%s:%d:%s:WARNING: Unable to free HEADERS allocated memory\n", FL, __func__); - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: END [hinfo=%p]\n", FL, hinfo); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: END [hinfo=%p]\n", FL, __func__, hinfo); return 0; } @@ -2732,15 +2732,15 @@ int MIMEH_headers_get(FILE* header_file, FILE* original_header_file, struct MIM hinfo->lf_count=0; if (f->linebreak == (FFGET_LINEBREAK_CR|FFGET_LINEBREAK_LF)) { - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_headers_get:DEBUG: Setting to CRLF based on ffget value of %d", FL, f->linebreak); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Setting to CRLF based on ffget value of %d", FL, __func__, f->linebreak); snprintf(hinfo->delimeter,sizeof(hinfo->delimeter),"\r\n"); hinfo->crlf_count = 1; } else if (f->linebreak == FFGET_LINEBREAK_LF) { - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_headers_get:DEBUG: Setting to LF based on ffget value of %d", FL, f->linebreak); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Setting to LF based on ffget value of %d", FL, __func__, f->linebreak); snprintf(hinfo->delimeter,sizeof(hinfo->delimeter),"\n"); hinfo->lf_count = 1; } else { - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_headers_get:DEBUG: ffget value of %d offered us no guide for the delimeter", FL, f->linebreak); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: ffget value of %d offered us no guide for the delimeter", FL, __func__, f->linebreak); } // Initialise header defects array. @@ -2775,7 +2775,7 @@ int MIMEH_headers_get(FILE* header_file, FILE* original_header_file, struct MIM // headers, then flag off an error if (glb.headerline == NULL) { - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIME_parse_headers:DEBUG: null headerline\n", FL); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: null headerline\n", FL, __func__); return 1; } @@ -2801,23 +2801,23 @@ int MIMEH_headers_get(FILE* header_file, FILE* original_header_file, struct MIM int MIMEH_parse_headers( FILE* header_file, FILE* original_header_file, FFGET_FILE *f, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers ) { int result = 0; - DMIMEH LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: Start [F=%p, hinfo=%p]\n", FL, f, hinfo); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Start [F=%p, hinfo=%p]\n", FL, __func__, f, hinfo); /** 20041216-1100:PLD: Set the header sanity to zero **/ if ( result == 0 ) hinfo->sanity = 0; /** Proceed to read, process and finish headers **/ - DMIMEH LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: Getting headers",FL); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Getting headers",FL, __func__); if ( result == 0 ) result = MIMEH_headers_get( header_file, original_header_file, hinfo, f, unpack_metadata, save_headers_original, save_headers, glb.headerline); - DMIMEH LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: Processing headers",FL); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Processing headers",FL, __func__); if ( result == 0 ) result = MIMEH_headers_process( hinfo, glb.headerline ); - DMIMEH LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: cleanup of headers",FL); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: cleanup of headers",FL, __func__); if (glb.headerline != NULL) { free(glb.headerline); glb.headerline = NULL; } - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:MIMEH_parse_headers:DEBUG: END [F=%p, hinfo=%p, sanity=%d]\n", FL, f, hinfo, hinfo->sanity); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: END [F=%p, hinfo=%p, sanity=%d]\n", FL, __func__, f, hinfo, hinfo->sanity); return result; } @@ -2910,7 +2910,7 @@ int MIMEH_read_primary_headers( FILE* header_file, FILE* original_header_file, c f = fopen(fname,"r"); if (!f) { - LOGGER_log("%s:%d:MIMEH_read_primary_headers:ERROR: Cannot open mailpack '%s' (%s)", FL, fname, strerror(errno)); + LOGGER_log("%s:%d:%s:ERROR: Cannot open mailpack '%s' (%s)", FL, __func__, fname, strerror(errno)); return 0; } FFGET_setstream(&F,f); From fe222e03001e954dff9c3b47341c8d8aab530430 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Thu, 1 May 2025 12:09:05 +0300 Subject: [PATCH 67/80] Refactor std unpackers --- mime.c | 183 ++++++++++++++++++++++++++++++++----------------- mime_element.c | 6 +- mime_element.h | 2 + mime_headers.c | 2 +- 4 files changed, 127 insertions(+), 66 deletions(-) diff --git a/mime.c b/mime.c index a35ef62..6cf3973 100644 --- a/mime.c +++ b/mime.c @@ -66,6 +66,11 @@ int MIME_unpack_mailbox( RIPMIME_output *unpack_metadata, char *mpname, int curr int MIME_handle_multipart( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ); int MIME_handle_rfc822( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ); +MIME_element* MIME_decode_std_raw( MIME_element* parent, FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo); +MIME_element* MIME_decode_std_text( MIME_element* parent, FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ); +MIME_element* MIME_decode_std_64( MIME_element* parent, FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ); + + // Predefined filenames #define MIME_BLANKZONE_FILENAME_DEFAULT "_blankzone_" #define MIME_HEADERS_FILENAME "_headers_" @@ -1124,13 +1129,13 @@ int MIME_decode_OLE_file( RIPMIME_output *unpack_metadata, FILE *f ) /*------------------------------------------------------------------------ -Procedure: MIME_decode_raw ID:1 +Procedure: MIME_decode_std_raw ID:1 Purpose: Decodes a binary type attachment, ie, no encoding, just raw data. Input: Output: Errors: ------------------------------------------------------------------------*/ -int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, MIME_element* decoded_mime ) +MIME_element* MIME_decode_std_raw(MIME_element* parent, FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo) { int result = 0; int bufsize=1024; @@ -1182,7 +1187,7 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UUencoded data\n",FL,__func__); if ( hinfo->content_transfer_encoding == _CTRANS_ENCODING_UUENCODE ) decode_entire_file = 0; - + ffg = UUENCODE_make_sourcestream(cur_mime->f); /* fseek to begin is here */ result = UUENCODE_decode_uu(ffg , hinfo->uudec_name, decode_entire_file, unpack_metadata, hinfo ); if (result == -1) @@ -1197,12 +1202,14 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME case UUENCODE_STATUS_CANNOT_ALLOCATE_MEMORY: LOGGER_log("%s:%d:%s:ERROR: Failure to allocate memory for UUdecode process",FL,__func__); - return -1; + cur_mime->decode_result_code = -1; + return cur_mime; break; default: LOGGER_log("%s:%d:%s:ERROR: Unknown return code from UUDecode process [%d]",FL,__func__,uuencode_error); - return -1; + cur_mime->decode_result_code = -1; + return cur_mime; } } if (result == UUENCODE_STATUS_SHORT_FILE) result = 0; @@ -1222,18 +1229,19 @@ int MIME_decode_raw( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIME if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Closed file and free'd buffer\n",FL,__func__); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: End[result = %d]\n",FL,__func__,result); - return result; + cur_mime->decode_result_code = result; + return cur_mime; } /*------------------------------------------------------------------------ -Procedure: MIME_decode_text ID:1 +Procedure: MIME_decode_std_text ID:1 Purpose: Decodes an input stream into a text file. Input: unpackdir : directory where to place new text file hinfo : struct containing information from the last parsed headers Output: Errors: ------------------------------------------------------------------------*/ -int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, MIME_element* decoded_mime ) +MIME_element* MIME_decode_std_text( MIME_element* parent, FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ) { int linecount = 0; // The number of lines int file_has_uuencode = 0; // Flag to indicate this text has UUENCODE in it @@ -1251,7 +1259,8 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM { /** If we cannot open the file for reading, leave an error and return -1 **/ LOGGER_log("%s:%d:%s:ERROR: print-quotable input stream broken.",FL,__func__); - return _EXITERR_MIMEREAD_CANNOT_OPEN_INPUT; + cur_mime->decode_result_code = _EXITERR_MIMEREAD_CANNOT_OPEN_INPUT; + return cur_mime; } if (f) { @@ -1301,8 +1310,8 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM if (linecount == 0) { - result = MIME_STATUS_ZERO_FILE; - return result; + cur_mime->decode_result_code = MIME_STATUS_ZERO_FILE; + return cur_mime; } if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Closed.",FL,__func__); } // if main input file stream was open @@ -1356,11 +1365,13 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM break; case UUENCODE_STATUS_CANNOT_ALLOCATE_MEMORY: LOGGER_log("%s:%d:%s:ERROR: Failure to allocate memory for UUdecode process",FL,__func__); - return -1; + cur_mime->decode_result_code = -1; + return cur_mime; break; default: LOGGER_log("%s:%d:%s:ERROR: Unknown return code from UUDecode process [%d]",FL,__func__,uuencode_error); - return -1; + cur_mime->decode_result_code = -1; + return cur_mime; } } if ( result > 0 ) { glb.attachment_count += result; result = 0; } @@ -1375,11 +1386,12 @@ int MIME_decode_text( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIM if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Completed decoding UUencoded data.\n",FL,__func__); } if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: result=%d ----------------Done\n",FL,__func__,result); - return result; + cur_mime->decode_result_code = result; + return cur_mime; } /*------------------------------------------------------------------------ -Procedure: MIME_decode_64 ID:1 +Procedure: MIME_decode_std_64 ID:1 Purpose: This routine is very very very important, it's the key to ensuring we get our attachments out of the email file without trauma! NOTE - this has been -slightly altered- in order to make provision @@ -1394,7 +1406,7 @@ struct MIMEH_header_info *hinfo: Auxillairy information such as the destination Output: Errors: ------------------------------------------------------------------------*/ -int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, MIME_element* decoded_mime ) +MIME_element* MIME_decode_std_64( MIME_element* parent, FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ) { int i; int cr_total = 0; @@ -1423,7 +1435,8 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH cur_mime = MIME_element_add (NULL, unpack_metadata, hinfo->filename, hinfo->content_type_string, hinfo->content_transfer_encoding_string, hinfo->name, hinfo->current_recursion_level, glb.attachment_count, glb.filecount, __func__); if (cur_mime->f == NULL) { - return -1; + cur_mime->decode_result_code = -1; + return cur_mime; } // Allocate the write buffer. By using the write buffer we gain an additional 10% in performance @@ -1432,7 +1445,8 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH if (!writebuffer) { LOGGER_log("%s:%d:%s:ERROR: cannot allocate %dbytes of memory for the write buffer",FL,__func__, _MIME_WRITE_BUFFER_SIZE); - return -1; + cur_mime->decode_result_code = -1; + return cur_mime; } else { wbpos = writebuffer; @@ -1573,8 +1587,10 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH status = MIME_ERROR_B64_INPUT_STREAM_EOF; fwrite(writebuffer, 1, wbcount, cur_mime->f); MIME_element_deactivate(cur_mime, unpack_metadata); - if (writebuffer) free(writebuffer); - return status; + if (writebuffer) + free(writebuffer); + cur_mime->decode_result_code = status; + return cur_mime; break; } /* if c was the EOF */ else if (c == '=') @@ -1678,16 +1694,20 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH status = MIME_BASE64_STATUS_HIT_BOUNDARY; // was _BOUNDARY_CRASH } if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: File size = %ld bytes, Exit Status = %d, Boundary Crash = %d\n",FL,__func__, bytecount, status, boundary_crash); - if (writebuffer) free(writebuffer); - return status; + if (writebuffer) + free(writebuffer); + cur_mime->decode_result_code = status; + return cur_mime; } // if End-of-MIME or Stopchars appeared } // while - if (writebuffer) free(writebuffer); - return status; + if (writebuffer) + free(writebuffer); + cur_mime->decode_result_code = status; + return cur_mime; } /*-----------------------------------------------------------------\ - Function Name : MIME_decode_64_cleanup + Function Name : MIME_decode_std_64_cleanup Returns Type : int ----Parameter List 1. FFGET_FILE *f, @@ -1703,7 +1723,7 @@ int MIME_decode_64( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH Changes: \------------------------------------------------------------------*/ -int MIME_decode_64_cleanup( FFGET_FILE *f) +int MIME_decode_std_64_cleanup( FFGET_FILE *f) { int result = 0; char buffer[128]; @@ -2068,6 +2088,17 @@ int MIME_generate_multiple_hardlink_filenames(struct MIMEH_header_info *hinfo, R } +MIME_element * resencapsulate(MIME_element *decoded_mime, int decode_result, struct MIMEH_header_info *hinfo) +{ + if (decoded_mime == NULL) + { + decoded_mime = malloc(sizeof(MIME_element)); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoded MIME NULL!! filename = '%s'",FL,__func__,hinfo->filename); + } + decoded_mime->decode_result_code = decode_result; + return decoded_mime; +} + /*------------------------------------------------------------------------ Procedure: MIME_process_content_transfer_encoding ID:1 Purpose: Based on the contents of hinfo, this function will call the @@ -2077,10 +2108,10 @@ Purpose: Based on the contents of hinfo, this function will call the Output: Errors: ------------------------------------------------------------------------*/ -int MIME_process_content_transfer_encoding( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, struct SS_object *ss ) +MIME_element* MIME_process_content_transfer_encoding( MIME_element* parent_mime, FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo, struct SS_object *ss ) { int keep = 1; - int result = -1; + int decode_result = -1; MIME_element* decoded_mime = NULL; if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Start:DEBUG: (%s)\n",FL,__func__, hinfo->filename); @@ -2195,15 +2226,16 @@ int MIME_process_content_transfer_encoding( FFGET_FILE *input_f, RIPMIME_output { case _CTRANS_ENCODING_B64: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding BASE64 format\n",FL,__func__); - result = MIME_decode_64(input_f, unpack_metadata, hinfo, decoded_mime); - switch (result) { + decoded_mime = MIME_decode_std_64(parent_mime, input_f, unpack_metadata, hinfo); + decode_result = decoded_mime->decode_result_code; + switch (decode_result) { case MIME_ERROR_B64_INPUT_STREAM_EOF: break; case MIME_BASE64_STATUS_HIT_BOUNDARY: - result = 0; + decode_result = 0; break; case 0: - result = MIME_decode_64_cleanup(input_f); + decode_result = MIME_decode_std_64_cleanup(input_f); break; default: break; @@ -2211,47 +2243,55 @@ int MIME_process_content_transfer_encoding( FFGET_FILE *input_f, RIPMIME_output break; case _CTRANS_ENCODING_7BIT: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding 7BIT format\n",FL,__func__); - result = MIME_decode_text(input_f, unpack_metadata, hinfo, decoded_mime); + + decoded_mime = MIME_decode_std_text(parent_mime, input_f, unpack_metadata, hinfo);decode_result = decoded_mime->decode_result_code; break; case _CTRANS_ENCODING_8BIT: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding 8BIT format\n",FL,__func__); - result = MIME_decode_text(input_f, unpack_metadata, hinfo, decoded_mime); + decoded_mime = MIME_decode_std_text(parent_mime, input_f, unpack_metadata, hinfo); + decode_result = decoded_mime->decode_result_code; break; case _CTRANS_ENCODING_BINARY: case _CTRANS_ENCODING_RAW: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding RAW format\n",FL,__func__); - result = MIME_decode_raw(input_f, unpack_metadata, hinfo, decoded_mime); + decoded_mime = MIME_decode_std_raw(parent_mime, input_f, unpack_metadata, hinfo); + decode_result = decoded_mime->decode_result_code; break; case _CTRANS_ENCODING_QP: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding Quoted-Printable format\n",FL,__func__); - result = MIME_decode_text(input_f, unpack_metadata, hinfo, decoded_mime); + decoded_mime = MIME_decode_std_text(parent_mime, input_f, unpack_metadata, hinfo); + decode_result = decoded_mime->decode_result_code; break; case _CTRANS_ENCODING_UUENCODE: + int fcount; if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UUENCODED format\n",FL,__func__); // Added as a test - remove if we can get this to work in a better way snprintf(hinfo->uudec_name,sizeof(hinfo->uudec_name),"%s",hinfo->filename); - result = UUENCODE_decode_uu(input_f, hinfo->uudec_name, 0, unpack_metadata, hinfo ); - glb.attachment_count += result; + fcount = UUENCODE_decode_uu(input_f, hinfo->uudec_name, 0, unpack_metadata, hinfo ); + glb.attachment_count += fcount; // Because this is a file-count, it's not really an 'error result' as such, so, set the // return code back to 0! - result = 0; + decode_result = 0; break; case _CTRANS_ENCODING_UNKNOWN: switch (hinfo->content_disposition) { case _CDISPOSITION_FORMDATA: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UNKNOWN format of FORMDATA disposition\n",FL,__func__); - result = MIME_decode_raw(input_f, unpack_metadata, hinfo, decoded_mime); + decoded_mime = MIME_decode_std_raw(parent_mime, input_f, unpack_metadata, hinfo); + decode_result = decoded_mime->decode_result_code; break; default: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UNKNOWN format\n",FL,__func__); - result = MIME_decode_text(input_f, unpack_metadata, hinfo, decoded_mime); + decoded_mime = MIME_decode_std_text(parent_mime, input_f, unpack_metadata, hinfo); + decode_result = decoded_mime->decode_result_code; } - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: UNKNOWN Decode completed, result = %d\n",FL,__func__,result); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: UNKNOWN Decode completed, result = %d\n",FL,__func__,decode_result); break; case _CTRANS_ENCODING_UNSPECIFIED: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding UNSPECIFIED format\n",FL,__func__); - result = MIME_decode_text(input_f, unpack_metadata, hinfo, decoded_mime); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding result for UNSPECIFIED format = %d\n",FL,__func__, result); + decoded_mime = MIME_decode_std_text(parent_mime, input_f, unpack_metadata, hinfo); + decode_result = decoded_mime->decode_result_code; + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding result for UNSPECIFIED format = %d\n",FL,__func__, decode_result); // 20040114-1236:PLD: Added nested mail checking // // Sometimes mailpacks have false headers at the start, resulting @@ -2271,33 +2311,34 @@ int MIME_process_content_transfer_encoding( FFGET_FILE *input_f, RIPMIME_output if (MIME_is_diskfile_RFC822(fn) > 0 ) { // 20040305-1304:PLD: unpack the file, propagate result upwards - result = MIME_unpack_single_diskfile( unpack_metadata, fn, (hinfo->current_recursion_level+ 1),ss ); + decode_result = MIME_unpack_single_diskfile( unpack_metadata, fn, (hinfo->current_recursion_level+ 1),ss ); } - free(fn); + free(fn); break; default: if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding format is not defined (%d)\n",FL,__func__, hinfo->content_transfer_encoding); - result = MIME_decode_raw(input_f, unpack_metadata, hinfo, decoded_mime); + decoded_mime = MIME_decode_std_raw(parent_mime, input_f, unpack_metadata, hinfo); + decode_result = decoded_mime->decode_result_code; break; } // Analyze our results - switch (result) { + switch (decode_result) { case 0: break; case MIME_STATUS_ZERO_FILE: - return MIME_STATUS_ZERO_FILE; + return resencapsulate(decoded_mime, MIME_STATUS_ZERO_FILE, hinfo); break; case MIME_ERROR_FFGET_EMPTY: - return result; + return resencapsulate(decoded_mime, decode_result, hinfo); break; case MIME_ERROR_RECURSION_LIMIT_REACHED: - return result; + return resencapsulate(decoded_mime, decode_result, hinfo); break; default: - return result; + return resencapsulate(decoded_mime, decode_result, hinfo); } - if ((result != -1)&&(result != MIME_STATUS_ZERO_FILE)) + if ((decode_result != -1)&&(decode_result != MIME_STATUS_ZERO_FILE)) { #ifdef RIPOLE // If we have OLE decoding active and compiled in, then @@ -2342,16 +2383,16 @@ int MIME_process_content_transfer_encoding( FFGET_FILE *input_f, RIPMIME_output snprintf(fn,fn_l,"%s/%s",unpack_metadata->dir,hinfo->filename); // 20040305-1304:PLD: unpack the file, propagate result upwards - result = MIME_unpack_single_diskfile( unpack_metadata, fn, (hinfo->current_recursion_level+ 1),ss ); + decode_result = MIME_unpack_single_diskfile( unpack_metadata, fn, (hinfo->current_recursion_level+ 1),ss ); free(fn); } } // Decode MHT files - } // If result != -1 + } // If decode_result != -1 // End. MIME_generate_multiple_hardlink_filenames(hinfo,unpack_metadata); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done for filename = '%s'",FL,__func__,hinfo->filename); - return result; + return resencapsulate(decoded_mime, decode_result, hinfo); } /*------------------------------------------------------------------------ @@ -2375,6 +2416,7 @@ int MIME_postdecode_cleanup( RIPMIME_output *unpack_metadata, struct SS_object * char *fn; int fn_l = strlen(unpack_metadata->dir) + strlen(filename) + sizeof(char) * 2; + fn = malloc(fn_l); snprintf(fn,fn_l,"%s/%s",unpack_metadata->dir,filename); result = unlink( fn ); @@ -2410,6 +2452,9 @@ int MIME_postdecode_cleanup( RIPMIME_output *unpack_metadata, struct SS_object * \------------------------------------------------------------------*/ int MIME_handle_multipart( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ) { + MIME_element* parent_mime = NULL; + MIME_element* decoded_mime = NULL; + int result = 0; if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding multipart/embedded \n",FL,__func__); // If there is no filename, then we have a "standard" @@ -2433,7 +2478,8 @@ int MIME_handle_multipart( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, if (p) PLD_strncpy(h->boundary, p,sizeof(h->boundary)); } else { if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Embedded message has a filename, decoding to file %s",FL,__func__,h->filename); - result = MIME_process_content_transfer_encoding( input_f, unpack_metadata, h, ss ); + decoded_mime = MIME_process_content_transfer_encoding( parent_mime, input_f, unpack_metadata, h, ss ); + result = decoded_mime->decode_result_code; if (result == 0) { char *fn; @@ -2476,6 +2522,9 @@ int MIME_handle_multipart( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, \------------------------------------------------------------------*/ int MIME_handle_rfc822( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ) { + MIME_element* parent_mime = NULL; + MIME_element* decoded_mime = NULL; + /** Decode a RFC822 encoded stream of data from *input_f **/ int result = 0; if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding RFC822 message\n",FL,__func__); @@ -2505,7 +2554,8 @@ int MIME_handle_rfc822( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, st } else { /** ...else... if the section has a filename or B64 type encoding, we need to put it through extra decoding **/ if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Embedded message has a filename, decoding to file %s",FL,__func__,h->filename); - result = MIME_process_content_transfer_encoding( input_f, unpack_metadata, h, ss ); + decoded_mime = MIME_process_content_transfer_encoding( parent_mime, input_f, unpack_metadata, h, ss ); + result = decoded_mime->decode_result_code; if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Result of extracting %s is %d",FL,__func__,h->filename, result); if (result == 0) { char * fn; @@ -2549,10 +2599,14 @@ int MIME_handle_rfc822( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, st \------------------------------------------------------------------*/ int MIME_handle_plain( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ) { + MIME_element* parent_mime = NULL; + MIME_element* decoded_mime = NULL; + /** Handle a plain text encoded data stream from *input_f **/ int result = 0; if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Handling plain email",FL,__func__); - result = MIME_process_content_transfer_encoding( input_f, unpack_metadata, h, ss ); + decoded_mime = MIME_process_content_transfer_encoding( parent_mime, input_f, unpack_metadata, h, ss ); + result = decoded_mime->decode_result_code; if ((result == MIME_ERROR_FFGET_EMPTY)||(result == 0)) { /** Test for RFC822 content... if so, go decode it **/ @@ -2584,6 +2638,8 @@ int MIME_unpack_stage2( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, st { int result = 0; struct MIMEH_header_info *h; + MIME_element* parent_mime = NULL; + MIME_element* decoded_mime = NULL; char *p; if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Start, recursion %d\n",FL,__func__, current_recursion_level); @@ -2684,7 +2740,8 @@ int MIME_unpack_stage2( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, st // Decode the data in the current MIME segment // based on the header information retrieved from // the start of this function. - result = MIME_process_content_transfer_encoding(input_f, unpack_metadata, h, ss); + decoded_mime = MIME_process_content_transfer_encoding( parent_mime, input_f, unpack_metadata, h, ss); + result = decoded_mime->decode_result_code; if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done decoding, result = %d",FL,__func__,result); if (result == 0) { @@ -2798,7 +2855,8 @@ int MIME_unpack_stage2( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, st } else { if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: RFC822 Message to be decoded...\n",FL,__func__); - result = MIME_process_content_transfer_encoding( input_f, unpack_metadata, h, ss ); + decoded_mime = MIME_process_content_transfer_encoding( parent_mime, input_f, unpack_metadata, h, ss ); + result = decoded_mime->decode_result_code; if (result != 0) return result; // 20040305-1313:PLD else { @@ -2822,7 +2880,8 @@ int MIME_unpack_stage2( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, st // multipart or RFC822 embedded email, we can then simply use // the normal decoding function to interpret its data. if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding a normal attachment \n",FL,__func__); - result = MIME_process_content_transfer_encoding( input_f, unpack_metadata, h, ss ); + decoded_mime = MIME_process_content_transfer_encoding( parent_mime, input_f, unpack_metadata, h, ss ); + result = decoded_mime->decode_result_code; if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding a normal attachment '%s' done. \n",FL,__func__, h->filename); // See if we have an attachment output which is actually another diff --git a/mime_element.c b/mime_element.c index 9519213..71f1bd6 100644 --- a/mime_element.c +++ b/mime_element.c @@ -69,6 +69,7 @@ MIME_element* MIME_element_add(struct MIME_element* parent, RIPMIME_output *unpa fullpath_len = strlen(unpack_metadata->dir) + strlen(filename) + 3 * sizeof(char); insertItem(all_MIME_elements.mime_arr, cur); cur->parent = parent; + cur->decode_result_code = -1; cur->id = all_MIME_elements.mime_count++; cur->directory = unpack_metadata->dir; cur->filename = dup_ini(filename); @@ -294,9 +295,8 @@ void write_FS_file(MIME_element* cur, int rename_method) void write_all_to_FS_files(RIPMIME_output *unpack_metadata, int rename_method) { - int i = 0; - - for (; i < all_MIME_elements.mime_count; i++); + int i; + for (i = 0; i < all_MIME_elements.mime_count; i++) { MIME_element* m = getItem(all_MIME_elements.mime_arr, i); write_FS_file(m, rename_method); diff --git a/mime_element.h b/mime_element.h index ac57638..ee3c031 100644 --- a/mime_element.h +++ b/mime_element.h @@ -35,6 +35,7 @@ typedef struct { char* name; char* mem_filearea; size_t mem_filearea_l; + int decode_result_code; } MIME_element; typedef struct { @@ -68,6 +69,7 @@ MIME_element* MIME_element_add ( void MIME_element_deactivate (MIME_element* cur, RIPMIME_output *unpack_metadata); void printArray(dynamic_array* container); void freeArray(dynamic_array* container, RIPMIME_output *unpack_metadata); +void write_all_to_FS_files(RIPMIME_output *unpack_metadata, int rename_method); int MIME_test_uniquename( char *path, char *fname, int method ); diff --git a/mime_headers.c b/mime_headers.c index cffc0ad..2066f20 100644 --- a/mime_headers.c +++ b/mime_headers.c @@ -578,7 +578,7 @@ int MIMEH_save_doubleCR( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct { int c; MIME_element* cur_mime = NULL; - + glb.doubleCR_count++; snprintf(glb.doubleCRname,_MIMEH_STRLEN_MAX,"%s/%s_doubleCR.%d_", unpack_metadata->dir, hinfo->filename, glb.doubleCR_count); From e17ed6c8f2c1476a3d3593d3c5f6af72b9981c45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Thu, 1 May 2025 12:17:01 +0300 Subject: [PATCH 68/80] Memorize a path creating for OLE --- mime.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/mime.c b/mime.c index 6cf3973..878f63c 100644 --- a/mime.c +++ b/mime.c @@ -1081,10 +1081,12 @@ int MIME_report_filename_decoded_RIPOLE(char *filename) int MIME_decode_OLE_diskfile( RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ) { struct OLE_object ole; - char fullpath[1024]; int result; + char *fn; + int fn_l = strlen(unpack_metadata->dir) + strlen(hinfo->filename) + sizeof(char) * 2; - snprintf(fullpath,sizeof(fullpath),"%s/%s",unpack_metadata->dir,hinfo->filename); + fn = malloc(fn_l); + snprintf(fn,fn_l,"%s/%s",unpack_metadata->dir,hinfo->filename); OLE_init(&ole); OLE_set_quiet(&ole,glb.quiet); @@ -1094,7 +1096,8 @@ int MIME_decode_OLE_diskfile( RIPMIME_output *unpack_metadata, struct MIMEH_head OLE_set_filename_report_fn(&ole, MIME_report_filename_decoded_RIPOLE ); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Starting OLE Decode",FL,__func__); - result = OLE_decode_diskfile(&ole, fullpath, unpack_metadata ); + result = OLE_decode_diskfile(&ole, fn, unpack_metadata ); + free(fn); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decode done, cleaning up.",FL,__func__); OLE_decode_done(&ole); if (ole.f) From 1cf2c576d1ba38df7bc9a2e7e74e24a066b6f149 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Thu, 1 May 2025 12:26:46 +0300 Subject: [PATCH 69/80] Memorize other filename --- mime.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/mime.c b/mime.c index 878f63c..577208a 100644 --- a/mime.c +++ b/mime.c @@ -1336,8 +1336,12 @@ MIME_element* MIME_decode_std_text( MIME_element* parent, FFGET_FILE *f, RIPMIME { FILE *fuue = NULL; FFGET_FILE * ffg = NULL; - char ffname[256]; - snprintf(ffname,256,"%s/%s", unpack_metadata->dir, hinfo->filename); + char *fn; + int fn_l = strlen(unpack_metadata->dir) + strlen(hinfo->filename) + sizeof(char) * 2; + + fn = malloc(fn_l); + snprintf(fn,fn_l,"%s/%s",unpack_metadata->dir,hinfo->filename); + // PLD-20040627-1212 // Make sure uudec_name is blank too // @@ -1352,7 +1356,8 @@ MIME_element* MIME_decode_std_text( MIME_element* parent, FFGET_FILE *f, RIPMIME // NOTE - this function returns the NUMBER of attachments it decoded in the return value! Don't // propergate this value unintentionally to parent functions (ie, if you were thinking it was // an error-status return value - fuue = UUENCODE_make_file_obj (ffname); + fuue = UUENCODE_make_file_obj (fn); + free(fn); ffg = UUENCODE_make_sourcestream(fuue); result = UUENCODE_decode_uu( ffg, hinfo->uudec_name, 1, unpack_metadata, hinfo ); From 5184f2e5cd08668c390fe1092c63679303e2c7aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Thu, 1 May 2025 12:32:56 +0300 Subject: [PATCH 70/80] Other path memorize --- mime.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/mime.c b/mime.c index 577208a..cf8a082 100644 --- a/mime.c +++ b/mime.c @@ -2017,15 +2017,18 @@ hardlinks to replicate this in our output. Changes: \------------------------------------------------------------------*/ -int MIME_generate_multiple_hardlink_filenames(struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata) +void MIME_generate_multiple_hardlink_filenames(struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata) { char *name; - char oldname[1024]; + char *oldfn; + int fn_l = strlen(unpack_metadata->dir) + strlen(hinfo->filename) + sizeof(char) * 2; if (glb.multiple_filenames == 0) return 0; //LOGGER_log("%s:%d:MIME_generate_multiple_hardlink_filenames:DEBUG: Generating hardlinks for %s",FL,__func__, hinfo->filename); - snprintf(oldname,sizeof(oldname),"%s/%s",unpack_metadata->dir, hinfo->filename); + oldfn = malloc(fn_l); + snprintf(oldfn,fn_l,"%s/%s",unpack_metadata->dir,hinfo->filename); + if (SS_count(&(hinfo->ss_names)) > 1){ do { @@ -2042,13 +2045,13 @@ int MIME_generate_multiple_hardlink_filenames(struct MIMEH_header_info *hinfo, R if (np) np++; else np = name; snprintf(newname,sizeof(newname),"%s/%s",unpack_metadata->dir, np); - //LOGGER_log("%s:%d:MIME_generate_multiple_hardlink_filenames:DEBUG: Linking %s->%s",FL,__func__,newname, oldname); - rv = link(oldname, newname); + //LOGGER_log("%s:%d:MIME_generate_multiple_hardlink_filenames:DEBUG: Linking %s->%s",FL,__func__,newname, oldfn); + rv = link(oldfn, newname); if (rv == -1) { if (errno != EEXIST) { - LOGGER_log("%s:%d:%s:WARNING: While trying to create '%s' link to '%s' (%s)",FL,__func__, newname, oldname,strerror(errno)); + LOGGER_log("%s:%d:%s:WARNING: While trying to create '%s' link to '%s' (%s)",FL,__func__, newname, oldfn,strerror(errno)); } } else { @@ -2072,13 +2075,13 @@ int MIME_generate_multiple_hardlink_filenames(struct MIMEH_header_info *hinfo, R int rv; snprintf(newname,sizeof(newname),"%s/%s",unpack_metadata->dir, name); - //LOGGER_log("%s:%d:MIME_generate_multiple_hardlink_filenames:DEBUG: Linking %s->%s",FL,__func__,newname, oldname); - rv = link(oldname, newname); + //LOGGER_log("%s:%d:MIME_generate_multiple_hardlink_filenames:DEBUG: Linking %s->%s",FL,__func__,newname, oldfn); + rv = link(oldfn, newname); if (rv == -1) { if (errno != EEXIST) { - LOGGER_log("%s:%d:%s:WARNING: While trying to create '%s' link to '%s' (%s)",FL,__func__, newname, oldname,strerror(errno)); + LOGGER_log("%s:%d:%s:WARNING: While trying to create '%s' link to '%s' (%s)",FL,__func__, newname, oldfn,strerror(errno)); } } else { @@ -2091,9 +2094,7 @@ int MIME_generate_multiple_hardlink_filenames(struct MIMEH_header_info *hinfo, R } while(name != NULL); } - - return 0; - + free(oldfn); } MIME_element * resencapsulate(MIME_element *decoded_mime, int decode_result, struct MIMEH_header_info *hinfo) From 2d14ffa81d378b01546f3de37756aae454f86cfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Thu, 1 May 2025 12:38:02 +0300 Subject: [PATCH 71/80] Fix function call, code style and other GCC warnings --- mime.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/mime.c b/mime.c index cf8a082..e31c8ea 100644 --- a/mime.c +++ b/mime.c @@ -2023,16 +2023,18 @@ void MIME_generate_multiple_hardlink_filenames(struct MIMEH_header_info *hinfo, char *oldfn; int fn_l = strlen(unpack_metadata->dir) + strlen(hinfo->filename) + sizeof(char) * 2; - if (glb.multiple_filenames == 0) return 0; + if (glb.multiple_filenames == 0) + return; //LOGGER_log("%s:%d:MIME_generate_multiple_hardlink_filenames:DEBUG: Generating hardlinks for %s",FL,__func__, hinfo->filename); oldfn = malloc(fn_l); snprintf(oldfn,fn_l,"%s/%s",unpack_metadata->dir,hinfo->filename); - if (SS_count(&(hinfo->ss_names)) > 1){ - do { - + if (SS_count(&(hinfo->ss_names)) > 1) + { + do + { name = SS_pop(&(hinfo->ss_names)); if (name != NULL) { @@ -2061,13 +2063,12 @@ void MIME_generate_multiple_hardlink_filenames(struct MIMEH_header_info *hinfo, } } } - } while(name != NULL); } if (SS_count(&(hinfo->ss_filenames)) > 1) { - do { - + do + { name = SS_pop(&(hinfo->ss_filenames)); if (name != NULL) { @@ -2091,7 +2092,6 @@ void MIME_generate_multiple_hardlink_filenames(struct MIMEH_header_info *hinfo, } } } - } while(name != NULL); } free(oldfn); @@ -2667,7 +2667,7 @@ int MIME_unpack_stage2( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, st /** 20041216-1102:PLD: Keep attempting to read headers until we get a sane set **/ do { /** Read next set of headers, repeat until a sane set of headers are found **/ - result = MIMEH_parse_headers(NULL, NULL, input_f,h,unpack_metadata->dir, 0, 0); + result = MIMEH_parse_headers(NULL, NULL, input_f,h,unpack_metadata, 0, 0); DMIME LOGGER_log("%s:%d:%s:DEBUG: Parsing of headers done, sanity = %d, result = %d",FL,__func__,h->sanity, result); } while ((h->sanity == 0)&&(result != -1)); @@ -2769,7 +2769,7 @@ int MIME_unpack_stage2( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, st if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding headers...\n",FL,__func__); do { - result = MIMEH_parse_headers(NULL, NULL, input_f, h, unpack_metadata->dir, 0, 0); + result = MIMEH_parse_headers(NULL, NULL, input_f, h, unpack_metadata, 0, 0); } while ((h->sanity == 0)&&(result != -1)); glb.header_defect_count += MIMEH_get_defect_count(h); From 63a87a8091c307411ee41fa90b86ee4c6e6a084a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Fri, 2 May 2025 16:58:51 +0300 Subject: [PATCH 72/80] Refactor to provide `parent_mime` --- mime.c | 29 +++++++++++++---------------- 1 file changed, 13 insertions(+), 16 deletions(-) diff --git a/mime.c b/mime.c index e31c8ea..a2a9805 100644 --- a/mime.c +++ b/mime.c @@ -63,8 +63,8 @@ int MIME_unpack_stage2( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, st int MIME_unpack_single_diskfile( RIPMIME_output *unpack_metadata, char *mpname, int current_recursion_level, struct SS_object *ss ); int MIME_unpack_single_file( RIPMIME_output *unpack_metadata, FILE *fi, int current_recursion_level, struct SS_object *ss ); int MIME_unpack_mailbox( RIPMIME_output *unpack_metadata, char *mpname, int current_recursion_level, struct SS_object *ss ); -int MIME_handle_multipart( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ); -int MIME_handle_rfc822( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ); +int MIME_handle_multipart( MIME_element* parent_mime, FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ); +int MIME_handle_rfc822( MIME_element* parent_mime, FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ); MIME_element* MIME_decode_std_raw( MIME_element* parent, FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo); MIME_element* MIME_decode_std_text( MIME_element* parent, FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *hinfo ); @@ -2459,9 +2459,8 @@ int MIME_postdecode_cleanup( RIPMIME_output *unpack_metadata, struct SS_object * Changes: \------------------------------------------------------------------*/ -int MIME_handle_multipart( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ) +int MIME_handle_multipart( MIME_element* parent_mime, FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ) { - MIME_element* parent_mime = NULL; MIME_element* decoded_mime = NULL; int result = 0; @@ -2514,11 +2513,11 @@ int MIME_handle_multipart( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, Function Name : MIME_handle_rfc822 Returns Type : int ----Parameter List - 1. FFGET_FILE *input_f, Input stream - 2. RIPMIME_output *unpack_metadata, Directory to write files to + 1. FFGET_FILE *input_f, Input stream + 2. RIPMIME_output *unpack_metadata, Directory to write files to 3. struct MIMEH_header_info *hinfo, Header information structure - 4. int current_recursion_level, Current recursion level - 5. struct SS_object *ss , String stack containing already decoded file names + 4. int current_recursion_level, Current recursion level + 5. struct SS_object *ss , String stack containing already decoded file names ------------------ Exit Codes : Side Effects : @@ -2529,9 +2528,8 @@ int MIME_handle_multipart( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, Changes: \------------------------------------------------------------------*/ -int MIME_handle_rfc822( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ) +int MIME_handle_rfc822( MIME_element* parent_mime, FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ) { - MIME_element* parent_mime = NULL; MIME_element* decoded_mime = NULL; /** Decode a RFC822 encoded stream of data from *input_f **/ @@ -2606,9 +2604,8 @@ int MIME_handle_rfc822( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, st Changes: \------------------------------------------------------------------*/ -int MIME_handle_plain( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ) +int MIME_handle_plain( MIME_element* parent_mime, FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, struct MIMEH_header_info *h, int current_recursion_level, struct SS_object *ss ) { - MIME_element* parent_mime = NULL; MIME_element* decoded_mime = NULL; /** Handle a plain text encoded data stream from *input_f **/ @@ -2729,14 +2726,14 @@ int MIME_unpack_stage2( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, st { // Pass off to the RFC822 handler if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding with RFC822 decoder\n",FL,__func__); - result = MIME_handle_rfc822(input_f,unpack_metadata,h,current_recursion_level,ss); + result = MIME_handle_rfc822( parent_mime, input_f,unpack_metadata,h,current_recursion_level,ss); } else if (MIMEH_is_contenttype(_CTYPE_MULTIPART, h->content_type)) { // Pass off to the multipart handler if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding with Multipart decoder\n",FL,__func__); - result = MIME_handle_multipart(input_f,unpack_metadata,h,current_recursion_level,ss); + result = MIME_handle_multipart( parent_mime, input_f,unpack_metadata,h,current_recursion_level,ss); } else { if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding boundaryless file (%s)...\n",FL,__func__,h->filename); - result = MIME_handle_plain( input_f, unpack_metadata,h,current_recursion_level,ss); + result = MIME_handle_plain( parent_mime, input_f, unpack_metadata,h,current_recursion_level,ss); } // else-if content was RFC822 or multi-part return result; } // End of the boundary-LESS mode ( processing the mail which has no boundaries in the primary headers ) @@ -2816,7 +2813,7 @@ int MIME_unpack_stage2( FFGET_FILE *input_f, RIPMIME_output *unpack_metadata, st // however, it is a rather robust/reliable way. if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Chose Content-type == RFC822 clause",FL,__func__); if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Calling MIME_process_content_transfer_encoding()",FL,__func__); - result = MIME_handle_rfc822(input_f,unpack_metadata,h,current_recursion_level,ss); + result = MIME_handle_rfc822(parent_mime, input_f,unpack_metadata,h,current_recursion_level,ss); // First up - extract the RFC822 body out of the parent mailpack // XX result = MIME_process_content_transfer_encoding( input_f, unpackdir, h, ss ); From a774e7c5be6e28d7856d5ff6586b71c8781a2dd4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Fri, 2 May 2025 22:31:54 +0300 Subject: [PATCH 73/80] Refactor `rename_method` --- mime.c | 30 +++--------------------------- mime.h | 3 +-- mime_element.c | 35 ++++++++++++++++++++++++++++++++--- mime_element.h | 3 ++- ripmime.c | 18 ++++++++---------- 5 files changed, 46 insertions(+), 43 deletions(-) diff --git a/mime.c b/mime.c index a2a9805..697c610 100644 --- a/mime.c +++ b/mime.c @@ -141,7 +141,6 @@ struct MIME_globals { int syslogging; int stderrlogging; int unique_names; - int rename_method; char headersname[_MIME_STRLEN_MAX]; char tempdirectory[_MIME_STRLEN_MAX]; int save_headers; @@ -846,28 +845,6 @@ int MIME_set_mailboxformat( int level ) return 0; } -/*------------------------------------------------------------------------ -Procedure: MIME_set_renamemethod ID:1 -Purpose: -Input: -Output: -Errors: -------------------------------------------------------------------------*/ -int MIME_set_renamemethod( int method ) -{ - if (( method >= _MIME_RENAME_METHOD_INFIX ) && ( method <= _MIME_RENAME_METHOD_RANDPOSTFIX )) - { - glb.rename_method = method; - } - else - { - LOGGER_log("%s:%d:%s:ERROR: selected method not within %d > x > %d range", FL,__func__, _MIME_RENAME_METHOD_INFIX, _MIME_RENAME_METHOD_POSTFIX ); - return -1; - } - - return 0; -} - /*-----------------------------------------------------------------\ Function Name : MIME_get_header_defect_count Returns Type : int @@ -1973,7 +1950,6 @@ void MIME_init( void ) glb.no_nameless = 0; glb.mailbox_format = 0; glb.name_by_type = 0; - glb.rename_method = _MIME_RENAME_METHOD_INFIX; glb.header_longsearch = 0; glb.max_recursion_level = _RECURSION_LEVEL_DEFAULT; @@ -2176,7 +2152,7 @@ MIME_element* MIME_process_content_transfer_encoding( MIME_element* parent_mime, // its tests here if ((glb.unique_names)&&(keep)) { - MIME_test_uniquename( unpack_metadata->dir, hinfo->filename, glb.rename_method ); + MIME_test_uniquename( unpack_metadata->dir, hinfo->filename, unpack_metadata->rename_method ); } // If the calling program requested verbosity, then indicate that we're decoding // the file here @@ -3276,11 +3252,11 @@ int MIME_unpack( RIPMIME_output *unpack_metadata, char *mpname, int current_recu * Closes the files used in MIME_unpack, such as headers etc * Writes memory FILE object content to disk if there is in-memory mode */ -void MIME_close(RIPMIME_output *unpack_metadata, int rename_method) +void MIME_close(RIPMIME_output *unpack_metadata) { if (unpack_metadata->unpack_mode == RIPMIME_UNPACK_MODE_IN_MEMORY) - write_all_to_FS_files(unpack_metadata, rename_method); + write_all_to_FS_files(unpack_metadata); if (MIME_DNORMAL) { LOGGER_log("%s:%d:%s: start.",FL,__func__); diff --git a/mime.h b/mime.h index dc2a0af..65da91e 100644 --- a/mime.h +++ b/mime.h @@ -97,12 +97,11 @@ int MIME_get_header_defect_count( void ); int MIME_is_file_mime( char *fname ); int MIME_is_file_uuenc( char *fname ); - char *MIME_get_blankfileprefix( void ); char *MIME_get_headersname( void ); char *MIME_get_subject( void ); void MIME_init( void ); -void MIME_close( RIPMIME_output *unpack_metadata, int rename_method ); +void MIME_close( RIPMIME_output *unpack_metadata ); int MIME_set_tmpdir( char *tmpdir ); #endif diff --git a/mime_element.c b/mime_element.c index 71f1bd6..b2604f8 100644 --- a/mime_element.c +++ b/mime_element.c @@ -64,7 +64,7 @@ MIME_element* MIME_element_add(struct MIME_element* parent, RIPMIME_output *unpa MIME_element *cur = malloc(sizeof(MIME_element)); int fullpath_len = 0; - // LOGGER_log("%s:%d:%s:start\n",FL,__func__); + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:start\n",FL,__func__); fullpath_len = strlen(unpack_metadata->dir) + strlen(filename) + 3 * sizeof(char); insertItem(all_MIME_elements.mime_arr, cur); @@ -97,6 +97,35 @@ MIME_element* MIME_element_add(struct MIME_element* parent, RIPMIME_output *unpa return cur; } +MIME_element* MIME_element_add_root(RIPMIME_output *unpack_metadata, + char* filename) +{ + MIME_element *cur = malloc(sizeof(MIME_element)); + int fullpath_len = 0; + + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:start\n",FL,__func__); + + fullpath_len = strlen(unpack_metadata->dir) + strlen(filename) + 3 * sizeof(char); + insertItem(all_MIME_elements.mime_arr, cur); + cur->parent = NULL; + cur->decode_result_code = 0; + cur->id = 0; + cur->directory = unpack_metadata->dir; + cur->filename = dup_ini(filename); + cur->content_type_string = NULL; + cur->content_transfer_encoding = NULL; + cur->name = NULL; + + cur->fullpath = (char*)malloc(fullpath_len); + snprintf(cur->fullpath,fullpath_len,"%s/%s",unpack_metadata->dir,filename); + cur->f = fopen(cur->fullpath,"r"); + if (cur->f == NULL) { + LOGGER_log("%s:%d:%s:ERROR: cannot open %s for reading",FL,"main",cur->fullpath); + return cur; + } + return cur; +} + static void dup_free(char *s) { if ((s != NULL) && s[0]) @@ -293,13 +322,13 @@ void write_FS_file(MIME_element* cur, int rename_method) fclose(wf); } -void write_all_to_FS_files(RIPMIME_output *unpack_metadata, int rename_method) +void write_all_to_FS_files(RIPMIME_output *unpack_metadata) { int i; for (i = 0; i < all_MIME_elements.mime_count; i++) { MIME_element* m = getItem(all_MIME_elements.mime_arr, i); - write_FS_file(m, rename_method); + write_FS_file(m, unpack_metadata->rename_method); } } diff --git a/mime_element.h b/mime_element.h index ee3c031..66fda72 100644 --- a/mime_element.h +++ b/mime_element.h @@ -20,6 +20,7 @@ struct mime_output char *dir; int unpack_mode; // int fragment_number; will be used later + int rename_method; }; typedef struct mime_output RIPMIME_output; @@ -69,7 +70,7 @@ MIME_element* MIME_element_add ( void MIME_element_deactivate (MIME_element* cur, RIPMIME_output *unpack_metadata); void printArray(dynamic_array* container); void freeArray(dynamic_array* container, RIPMIME_output *unpack_metadata); -void write_all_to_FS_files(RIPMIME_output *unpack_metadata, int rename_method); +void write_all_to_FS_files(RIPMIME_output *unpack_metadata); int MIME_test_uniquename( char *path, char *fname, int method ); diff --git a/ripmime.c b/ripmime.c index 75ae00b..ff2a63f 100644 --- a/ripmime.c +++ b/ripmime.c @@ -43,7 +43,6 @@ struct RIPMIME_globals int quiet; int verbose_defects; int verbose; - int rename_method; }; //-b [blankzone file name] : Dump the contents of the MIME blankzone to 'filename' @@ -323,28 +322,28 @@ int RIPMIME_parse_parameters (struct RIPMIME_globals *glb, int argc, char **argv } else if (strncmp (&(argv[i][2]), "prefix", 6) == 0) { - glb->rename_method = _MIME_RENAME_METHOD_PREFIX; + glb->output->rename_method = _MIME_RENAME_METHOD_PREFIX; } else if (strncmp (&(argv[i][2]), "postfix", 7) == 0) { - glb->rename_method = _MIME_RENAME_METHOD_POSTFIX; + glb->output->rename_method = _MIME_RENAME_METHOD_POSTFIX; } else if (strncmp (&(argv[i][2]), "infix", 5) == 0) { - glb->rename_method = _MIME_RENAME_METHOD_INFIX; + glb->output->rename_method = _MIME_RENAME_METHOD_INFIX; } /* New methods of generating file name: add counter and random number*/ else if (strncmp (&(argv[i][2]), "randprefix", 10) == 0) { - glb->rename_method = _MIME_RENAME_METHOD_RANDPREFIX; + glb->output->rename_method = _MIME_RENAME_METHOD_RANDPREFIX; } else if (strncmp (&(argv[i][2]), "randpostfix", 11) == 0) { - glb->rename_method = _MIME_RENAME_METHOD_RANDPOSTFIX; + glb->output->rename_method = _MIME_RENAME_METHOD_RANDPOSTFIX; } else if (strncmp (&(argv[i][2]), "randinfix", 9) == 0) { - glb->rename_method = _MIME_RENAME_METHOD_RANDINFIX; + glb->output->rename_method = _MIME_RENAME_METHOD_RANDINFIX; } else if (strncmp (&(argv[i][2]), "overwrite", 9) == 0) { @@ -532,13 +531,13 @@ int RIPMIME_init (struct RIPMIME_globals *glb, RIPMIME_output *o) glb->output = o; glb->output->dir = defaultdir; glb->output->unpack_mode = RIPMIME_UNPACK_MODE_TO_DIRECTORY; + glb->output->rename_method = _MIME_RENAME_METHOD_INFIX; glb->inputfile = NULL; glb->use_return_codes = 0; glb->timeout = 0; glb->quiet = 0; glb->verbose_defects = 0; glb->verbose = 0; - glb->rename_method = _MIME_RENAME_METHOD_INFIX; return 0; } @@ -594,7 +593,7 @@ int RIPMIME_unpack_single( struct RIPMIME_globals *glb, char *fname ) // do any last minute things - MIME_close (glb->output, glb->rename_method); + MIME_close (glb->output); return result; } @@ -723,7 +722,6 @@ int main (int argc, char **argv) MIME_set_header_longsearch(1); // 20040310-0117:PLD - Added by default as it seems stable, use --disable-qmail-bounce to turn off RIPMIME_parse_parameters (&glb, argc, argv); - MIME_set_renamemethod (glb.rename_method); // if our input filename wasn't specified, then we better let the user know! if (!glb.inputfile) From 24bf15d1956a3f20a1a8c9bdc36f5afe67d7be4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Fri, 2 May 2025 22:57:08 +0300 Subject: [PATCH 74/80] Refactor `unique_names` --- mime.c | 19 ++----------------- mime_element.c | 40 +++++++++++++++++++--------------------- mime_element.h | 3 ++- ripmime.c | 8 ++++---- 4 files changed, 27 insertions(+), 43 deletions(-) diff --git a/mime.c b/mime.c index 697c610..4330a7f 100644 --- a/mime.c +++ b/mime.c @@ -140,7 +140,6 @@ struct MIME_globals { int quiet; int syslogging; int stderrlogging; - int unique_names; char headersname[_MIME_STRLEN_MAX]; char tempdirectory[_MIME_STRLEN_MAX]; int save_headers; @@ -801,19 +800,6 @@ int MIME_set_no_nameless( int level ) return 0; } -/*------------------------------------------------------------------------ -Procedure: MIME_set_uniquenames ID:1 -Purpose: -Input: -Output: -Errors: -------------------------------------------------------------------------*/ -int MIME_set_uniquenames( int level ) -{ - glb.unique_names = level; - return 0; -} - /*------------------------------------------------------------------------ Procedure: MIME_set_noparanoid ID:1 Purpose: If set, will prevent MIME from clobbering what it considers @@ -1945,7 +1931,6 @@ void MIME_init( void ) glb.quiet = 0; glb.syslogging = 0; glb.stderrlogging = 1; - glb.unique_names = 0; glb.save_headers = 0; glb.no_nameless = 0; glb.mailbox_format = 0; @@ -2150,9 +2135,9 @@ MIME_element* MIME_process_content_transfer_encoding( MIME_element* parent_mime, // If we are required to have "unique" filenames for everything, rather than // allowing ripMIME to overwrite stuff, then we put the filename through // its tests here - if ((glb.unique_names)&&(keep)) + if ((unpack_metadata->unique_names)&&(keep)) { - MIME_test_uniquename( unpack_metadata->dir, hinfo->filename, unpack_metadata->rename_method ); + MIME_test_uniquename( unpack_metadata, hinfo->filename ); } // If the calling program requested verbosity, then indicate that we're decoding // the file here diff --git a/mime_element.c b/mime_element.c index b2604f8..659368c 100644 --- a/mime_element.c +++ b/mime_element.c @@ -181,13 +181,11 @@ static inline int get_random_value(void) { Procedure: MIME_test_uniquename ID:1 Purpose: Checks to see that the filename specified is unique. If it's not unique, it will modify the filename -Input: char *path: Path in which to look for similar filenames -char *fname: Current filename -int method: Method of altering the filename (infix, postfix, prefix, randinfix, randpostfix, randprefix) +char *fname: filename Output: Errors: ------------------------------------------------------------------------*/ -int MIME_test_uniquename( char *path, char *fname, int method ) +int MIME_test_uniquename( RIPMIME_output *unpack_metadata, char *fname ) { struct stat buf; @@ -201,7 +199,7 @@ int MIME_test_uniquename( char *path, char *fname, int method ) frontname = extention = NULL; // shuts the compiler up - if (method == _MIME_RENAME_METHOD_INFIX) + if (unpack_metadata->rename_method == _MIME_RENAME_METHOD_INFIX) { PLD_strncpy(scr,fname, _FS_PATH_MAX); frontname = scr; @@ -214,11 +212,11 @@ int MIME_test_uniquename( char *path, char *fname, int method ) } else { - method = _MIME_RENAME_METHOD_POSTFIX; + unpack_metadata->rename_method = _MIME_RENAME_METHOD_POSTFIX; } } - if (method == _MIME_RENAME_METHOD_RANDINFIX) + if (unpack_metadata->rename_method == _MIME_RENAME_METHOD_RANDINFIX) { PLD_strncpy(scr,fname, _FS_PATH_MAX); frontname = scr; @@ -231,11 +229,11 @@ int MIME_test_uniquename( char *path, char *fname, int method ) } else { - method = _MIME_RENAME_METHOD_RANDPOSTFIX; + unpack_metadata->rename_method = _MIME_RENAME_METHOD_RANDPOSTFIX; } } - snprintf(newname, _FS_PATH_MAX,"%s/%s",path,fname); + snprintf(newname, _FS_PATH_MAX,"%s/%s",unpack_metadata->dir,fname); while (!cleared) { if ((stat(newname, &buf) == -1)) @@ -246,24 +244,24 @@ int MIME_test_uniquename( char *path, char *fname, int method ) { int randval = get_random_value(); - switch (method) { + switch (unpack_metadata->rename_method) { case _MIME_RENAME_METHOD_PREFIX: - snprintf(newname, _FS_PATH_MAX,"%s/%d_%s",path,count,fname); + snprintf(newname, _FS_PATH_MAX,"%s/%d_%s",unpack_metadata->dir,count,fname); break; case _MIME_RENAME_METHOD_INFIX: - snprintf(newname, _FS_PATH_MAX,"%s/%s_%d.%s",path,frontname,count,extention); + snprintf(newname, _FS_PATH_MAX,"%s/%s_%d.%s",unpack_metadata->dir,frontname,count,extention); break; case _MIME_RENAME_METHOD_POSTFIX: - snprintf(newname, _FS_PATH_MAX,"%s/%s_%d",path,fname,count); + snprintf(newname, _FS_PATH_MAX,"%s/%s_%d",unpack_metadata->dir,fname,count); break; case _MIME_RENAME_METHOD_RANDPREFIX: - snprintf(newname, _FS_PATH_MAX,"%s/%d_%d_%s",path,count,randval,fname); + snprintf(newname, _FS_PATH_MAX,"%s/%d_%d_%s",unpack_metadata->dir,count,randval,fname); break; case _MIME_RENAME_METHOD_RANDINFIX: - snprintf(newname, _FS_PATH_MAX,"%s/%s_%d_%d.%s",path,frontname,count,randval,extention); + snprintf(newname, _FS_PATH_MAX,"%s/%s_%d_%d.%s",unpack_metadata->dir,frontname,count,randval,extention); break; case _MIME_RENAME_METHOD_RANDPOSTFIX: - snprintf(newname, _FS_PATH_MAX,"%s/%s_%d_%d",path,fname,count,randval); + snprintf(newname, _FS_PATH_MAX,"%s/%s_%d_%d",unpack_metadata->dir,fname,count,randval); } count++; } @@ -292,16 +290,16 @@ static void copyFILEcontent(FILE* src, FILE* dst) } } -void write_FS_file(MIME_element* cur, int rename_method) +void write_FS_file(RIPMIME_output *unpack_metadata, MIME_element* cur) { char * wr_filename = NULL; FILE* wf = NULL; int fn_l = 0; - MIME_test_uniquename(cur->directory, cur->filename, rename_method); - fn_l = strlen(cur->directory) + strlen(cur->filename) + sizeof(char) * 2; + MIME_test_uniquename(unpack_metadata, cur->filename); + fn_l = strlen(unpack_metadata->dir) + strlen(cur->filename) + sizeof(char) * 2; wr_filename = malloc(fn_l); - snprintf(wr_filename,fn_l,"%s/%s",cur->directory,cur->filename); + snprintf(wr_filename,fn_l,"%s/%s",unpack_metadata->dir,cur->filename); if (cur->f == NULL) { LOGGER_log("%s:%d:%s:ERROR: Cannot copy data from mime_element memory file, programming error (for %s)", FL,__func__, wr_filename, strerror(errno)); free(wr_filename); @@ -328,7 +326,7 @@ void write_all_to_FS_files(RIPMIME_output *unpack_metadata) for (i = 0; i < all_MIME_elements.mime_count; i++) { MIME_element* m = getItem(all_MIME_elements.mime_arr, i); - write_FS_file(m, unpack_metadata->rename_method); + write_FS_file(unpack_metadata, m); } } diff --git a/mime_element.h b/mime_element.h index 66fda72..1030142 100644 --- a/mime_element.h +++ b/mime_element.h @@ -21,6 +21,7 @@ struct mime_output int unpack_mode; // int fragment_number; will be used later int rename_method; + int unique_names; // flague }; typedef struct mime_output RIPMIME_output; @@ -72,6 +73,6 @@ void printArray(dynamic_array* container); void freeArray(dynamic_array* container, RIPMIME_output *unpack_metadata); void write_all_to_FS_files(RIPMIME_output *unpack_metadata); -int MIME_test_uniquename( char *path, char *fname, int method ); +int MIME_test_uniquename( RIPMIME_output *unpack_metadata, char *fname ); #endif diff --git a/ripmime.c b/ripmime.c index ff2a63f..58ec718 100644 --- a/ripmime.c +++ b/ripmime.c @@ -347,15 +347,15 @@ int RIPMIME_parse_parameters (struct RIPMIME_globals *glb, int argc, char **argv } else if (strncmp (&(argv[i][2]), "overwrite", 9) == 0) { - MIME_set_uniquenames (0); + glb->output->unique_names = 0; } else if (strncmp (&(argv[i][2]), "unique_names", 12) == 0) { - MIME_set_uniquenames (1); + glb->output->unique_names = 1; } else if (strncmp (&(argv[i][2]), "unique-names", 12) == 0) { - MIME_set_uniquenames (1); + glb->output->unique_names = 1; } else if (strncmp(&(argv[i][2]), "name-by-type", 12) == 0) { @@ -532,6 +532,7 @@ int RIPMIME_init (struct RIPMIME_globals *glb, RIPMIME_output *o) glb->output->dir = defaultdir; glb->output->unpack_mode = RIPMIME_UNPACK_MODE_TO_DIRECTORY; glb->output->rename_method = _MIME_RENAME_METHOD_INFIX; + glb->output->unique_names = 1; glb->inputfile = NULL; glb->use_return_codes = 0; glb->timeout = 0; @@ -717,7 +718,6 @@ int main (int argc, char **argv) // Setup our default behaviours */ - MIME_set_uniquenames (1); MIME_set_paranoid (0); MIME_set_header_longsearch(1); // 20040310-0117:PLD - Added by default as it seems stable, use --disable-qmail-bounce to turn off From a17127ab0c0b622d094a833c9e301b6973d33b1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Mon, 5 May 2025 06:34:31 +0300 Subject: [PATCH 75/80] Fix and refactor `MIME_headers` debug No `__func__` in some places cause segfault during debug --- mime_headers.c | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/mime_headers.c b/mime_headers.c index 2066f20..19f54ea 100644 --- a/mime_headers.c +++ b/mime_headers.c @@ -527,7 +527,7 @@ int MIMEH_are_headers_RFC822( char *headers ) return 0; } - DMIMEH LOGGER_log("%s:%d:%s:DEBUG:----\n%s\n----",FL,headers); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG:----\n%s\n----",FL, __func__, headers); lc_headers = strdup(headers); if (lc_headers == NULL) return 0; @@ -535,13 +535,13 @@ int MIMEH_are_headers_RFC822( char *headers ) //PLD_strlower((unsigned char *)lc_headers); PLD_strlower(lc_headers); - DMIMEH LOGGER_log("%s:%d:%s:DEBUG:----(lowercase)----\n%s\n----",FL,lc_headers); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG:----(lowercase)----\n%s\n----",FL, __func__, lc_headers); for (condition_item=0; condition_item < 6; condition_item++) { char *p; - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Condition test item[%d] = '%s'",FL,condition_item,conditions[condition_item]); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Condition test item[%d] = '%s'",FL, __func__, condition_item,conditions[condition_item]); p = strstr(lc_headers, conditions[condition_item]); if (p != NULL) { @@ -584,7 +584,7 @@ int MIMEH_save_doubleCR( FFGET_FILE *f, RIPMIME_output *unpack_metadata, struct cur_mime = MIME_element_add (NULL, unpack_metadata, glb.doubleCRname, "doubleCR", NULL, "doubleCR", hinfo->current_recursion_level + 1, 0, 0, __func__); - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Saving DoubleCR header: %s\n", FL,glb.doubleCRname); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Saving DoubleCR header: %s\n", FL, __func__, glb.doubleCRname); while (1) { c = FFGET_fgetc(f); @@ -675,7 +675,7 @@ int MIMEH_strip_comments( char *input ) { int stop_searching = 0; - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Located open ( at %s",FL,p); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Located open ( at %s",FL, __func__, p); // If we did locate an opening parenthesis, look for the closing one // NOTE - we cannot have a breaking \n or \r inbetween // q = strpbrk(p, ")\n\r"); @@ -699,7 +699,7 @@ int MIMEH_strip_comments( char *input ) break; case ')': - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Located closing ) at %s",FL,q); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Located closing ) at %s",FL, __func__, q); if (in_quote == 0) stop_searching = 1; break; } @@ -912,6 +912,7 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: File position = %ld [0x%0X]" ,FL + ,__func__ ,FFGET_ftell(f) ,FFGET_ftell(f) ); @@ -1195,7 +1196,7 @@ int MIMEH_decode_multivalue_language_string( char *input ) *input = '\0'; } - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Output = '%s'",FL,q); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Output = '%s'",FL, __func__, q); return 0; } @@ -1927,7 +1928,7 @@ int MIMEH_parse_contenttype( char *header_name, char *header_value, struct MIMEH ** Look for the MIME Boundary specification in the headers **/ return_value = MIMEH_parse_header_parameter(hinfo, param, "boundary", hinfo->boundary, sizeof(hinfo->boundary), &data_end_point); - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Param<=>data_end gap = %d", FL,data_end_point -param); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Param<=>data_end gap = %d", FL, __func__, data_end_point -param); DMIMEH LOGGER_log("%s:%d:%s:DEBUG: param start pos = '%s'",FL, __func__, param); if (data_end_point > param) param = data_end_point; DMIMEH LOGGER_log("%s:%d:%s:DEBUG: param start pos = '%s'",FL, __func__, param); @@ -2149,7 +2150,7 @@ int MIMEH_parse_contentdisposition( char *header_name, char *header_value, struc char *hv = strdup(header_value); // CONTENT DISPOSITION ------------------------------ - //LOGGER_log("%s:%d:DEBUG: Headers='%s'",FL,header_value); + //LOGGER_log("%s:%d:DEBUG: Headers='%s'",FL, __func__, header_value); p = strstr(header_name,"content-disposition"); if (p != NULL) { @@ -2217,7 +2218,7 @@ int MIMEH_parse_contentdisposition( char *header_name, char *header_value, struc int parse_result; char *data_end_point; - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Parsing '%s'",FL,param); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Parsing '%s'",FL, __func__, param); // Seek out possible 'filename' parameters @@ -2247,7 +2248,7 @@ int MIMEH_parse_contentdisposition( char *header_name, char *header_value, struc if ( hinfo->content_type == _CTYPE_MULTIPART_APPLEDOUBLE ) { snprintf( glb.appledouble_filename, sizeof(glb.appledouble_filename), "%s", hinfo->filename ); - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Setting appledouble filename to: '%s'",FL,glb.appledouble_filename); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Setting appledouble filename to: '%s'",FL, __func__, glb.appledouble_filename); } } // If the header-value contained ;'s ( indicating parameters ) @@ -2281,7 +2282,7 @@ int MIMEH_parse_generic( char *header_name, char *header_value, struct MIMEH_hea int compare_result = 0; int tlen; - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Searching for %s in %s",FL,tokenstr,header_name); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Searching for %s in %s",FL, __func__, tokenstr,header_name); /** Sanity check the parameters **/ if (hinfo == NULL) return -1; if (tokenstr == NULL) return -1; @@ -2505,7 +2506,7 @@ int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ) safehl = malloc(sizeof(char) *(headerlength+1)); PLD_strncpy(safehl, h, headerlength+1); - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Header length = %d\n", FL,headerlength); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Header length = %d\n", FL, __func__, headerlength); MIMEH_strip_comments(h); @@ -2520,7 +2521,7 @@ int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ) char *header_name_end_position; char *header_value_end_position; - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Processing '%s'",FL,current_header_position); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Processing '%s'",FL, __func__, current_header_position); /** Tokenise for the header 'name', ie, content-type, subject etc **/ header_name = current_header_position; @@ -2536,7 +2537,7 @@ int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ) // NOTE: this may activate on the true-blank lines, hence why we // dump the source string, just for confirmation - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Could not locate ':' separator, using whitespace (source='%s')",FL,header_name); + DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Could not locate ':' separator, using whitespace (source='%s')",FL, __func__, header_name); header_name_end_position = strpbrk( header_name, "\t " ); if (header_name_end_position == NULL) { @@ -2655,7 +2656,7 @@ int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ) || (strlen(hinfo->content_transfer_encoding_string) < 1) ) { - //LOGGER_log("%s:%d:DEBUG: Encoding pair was octet but no encoding, filename=%s\n",FL,hinfo->filename); + //LOGGER_log("%s:%d:DEBUG: Encoding pair was octet but no encoding, filename=%s\n",FL, __func__, hinfo->filename); hinfo->content_transfer_encoding = _CTRANS_ENCODING_RAW; } } From 96393bbeefe1bbd30f94f1bf1b29bd6c9148faa8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9C=D0=B8=D1=85=D0=B0=D0=B8=D0=BB?= Date: Mon, 5 May 2025 06:47:42 +0300 Subject: [PATCH 76/80] Unify debug macros in `MIME_headers` --- mime_headers.c | 108 ++++++++++++++++++++++++------------------------- 1 file changed, 53 insertions(+), 55 deletions(-) diff --git a/mime_headers.c b/mime_headers.c index 19f54ea..307c9a4 100644 --- a/mime_headers.c +++ b/mime_headers.c @@ -45,8 +45,6 @@ #define MIMEH_DPEDANTIC ((glb.debug >= _MIMEH_DEBUG_PEDANTIC)) #define MIMEH_DNORMAL ((glb.debug >= _MIMEH_DEBUG_NORMAL )) -#define DMIMEH if ((glb.debug >= _MIMEH_DEBUG_NORMAL)) - #define max(a,b) \ ({ __typeof__ (a) _a = (a); \ __typeof__ (b) _b = (b); \ @@ -436,7 +434,7 @@ int MIMEH_set_defect( struct MIMEH_header_info *hinfo, int defect ) { hinfo->defects[defect]++; hinfo->header_defect_count++; - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Setting defect index '%d' to '%d'",FL, __func__, defect, hinfo->defects[defect]); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Setting defect index '%d' to '%d'",FL, __func__, defect, hinfo->defects[defect]); } return 0; } @@ -523,11 +521,11 @@ int MIMEH_are_headers_RFC822( char *headers ) if (headers == NULL) { - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Headers are NULL"); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Headers are NULL"); return 0; } - DMIMEH LOGGER_log("%s:%d:%s:DEBUG:----\n%s\n----",FL, __func__, headers); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG:----\n%s\n----",FL, __func__, headers); lc_headers = strdup(headers); if (lc_headers == NULL) return 0; @@ -535,13 +533,13 @@ int MIMEH_are_headers_RFC822( char *headers ) //PLD_strlower((unsigned char *)lc_headers); PLD_strlower(lc_headers); - DMIMEH LOGGER_log("%s:%d:%s:DEBUG:----(lowercase)----\n%s\n----",FL, __func__, lc_headers); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG:----(lowercase)----\n%s\n----",FL, __func__, lc_headers); for (condition_item=0; condition_item < 6; condition_item++) { char *p; - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Condition test item[%d] = '%s'",FL, __func__, condition_item,conditions[condition_item]); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Condition test item[%d] = '%s'",FL, __func__, condition_item,conditions[condition_item]); p = strstr(lc_headers, conditions[condition_item]); if (p != NULL) { @@ -675,7 +673,7 @@ int MIMEH_strip_comments( char *input ) { int stop_searching = 0; - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Located open ( at %s",FL, __func__, p); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Located open ( at %s",FL, __func__, p); // If we did locate an opening parenthesis, look for the closing one // NOTE - we cannot have a breaking \n or \r inbetween // q = strpbrk(p, ")\n\r"); @@ -699,7 +697,7 @@ int MIMEH_strip_comments( char *input ) break; case ')': - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Located closing ) at %s",FL, __func__, q); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Located closing ) at %s",FL, __func__, q); if (in_quote == 0) stop_searching = 1; break; } @@ -720,7 +718,7 @@ int MIMEH_strip_comments( char *input ) // Move q to the first char after the closing parenthesis q++; - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: located closing ) at %s ",FL, __func__, q); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: located closing ) at %s ",FL, __func__, q); // While there's more chars in string, copy them to where // the opening parenthesis is while (*q != '\0') @@ -729,7 +727,7 @@ int MIMEH_strip_comments( char *input ) p++; q++; } // While q != '\0' - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: char copy done",FL, __func__); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: char copy done",FL, __func__); // Terminate the string *p = '\0'; @@ -739,7 +737,7 @@ int MIMEH_strip_comments( char *input ) } // if p == NULL } while ((p != NULL)&&(p_org != NULL)); // do-while more comments to remove - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Final string = '%s'",FL, __func__, input); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Final string = '%s'",FL, __func__, input); return 0; } @@ -811,7 +809,7 @@ int MIMEH_fix_header_mistakes( char *data ) int result = 0; char *p; - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Checking and fixing headers in '%s'",FL, __func__, data); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Checking and fixing headers in '%s'",FL, __func__, data); if (glb.header_fix == 0) return result; @@ -825,7 +823,7 @@ int MIMEH_fix_header_mistakes( char *data ) q = p+1; - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Located ';' at offset %d '%20s",FL, __func__, p -data, p); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Located ';' at offset %d '%20s",FL, __func__, p -data, p); if ((*q == '\n')||(*q == '\r')) { nonblank_detected = 0; @@ -852,7 +850,7 @@ int MIMEH_fix_header_mistakes( char *data ) } /** ELSE - if *q wasn't a line break char **/ if (nonblank_detected == 1) { - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Line was normal/safe, continue...",FL, __func__); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Line was normal/safe, continue...",FL, __func__); p++; continue; } /** if nonblank_detected == 1 **/ @@ -860,11 +858,11 @@ int MIMEH_fix_header_mistakes( char *data ) ** line, then we need to pull up the next line **/ if (*q != '\0') { if(!MIMEH_check_ct(q)){ - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Line needs fixing",FL, __func__); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Line needs fixing",FL, __func__); *q = ' '; q++; if ((*q == '\n')||(*q == '\r')) *q = ' '; - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Line fixed",FL, __func__); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Line fixed",FL, __func__); p = q; }else{ p++; @@ -872,7 +870,7 @@ int MIMEH_fix_header_mistakes( char *data ) } /** If q wasn't the end of data **/ } /** while looking for more ';' chars **/ - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Done",FL, __func__); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Done",FL, __func__); return result; } @@ -1099,7 +1097,7 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI if (is_RFC822_headers == 0) { /** If not RFC822 headers, then clean up everything we allocated in here **/ - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: No RFC822 headers detected, cleanup.", FL, __func__); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: No RFC822 headers detected, cleanup.", FL, __func__); if (headerline_original != NULL) { free(headerline_original); @@ -1172,7 +1170,7 @@ int MIMEH_decode_multivalue_language_string( char *input ) int language_set = 0; char *q = input; - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Decoding '%s'",FL, __func__, input); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Decoding '%s'",FL, __func__, input); // Count the single-quotes while ((*q != '\0')&&(sq_count != 2)) if (*q++ == '\'') sq_count++; if (sq_count < 2) @@ -1196,7 +1194,7 @@ int MIMEH_decode_multivalue_language_string( char *input ) *input = '\0'; } - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Output = '%s'",FL, __func__, q); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Output = '%s'",FL, __func__, q); return 0; } @@ -1240,7 +1238,7 @@ int MIMEH_recompose_multivalue( struct MIMEH_header_info *hinfo, char *header_na char *start_position = header_value; - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: seeking for %s in %s and appending to '%s'. Buffer size=%d", FL, __func__, header_name_prefix, header_value,buffer, buffer_size ); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: seeking for %s in %s and appending to '%s'. Buffer size=%d", FL, __func__, header_name_prefix, header_value,buffer, buffer_size ); // Locate the first part of the multipart string @@ -1268,7 +1266,7 @@ int MIMEH_recompose_multivalue( struct MIMEH_header_info *hinfo, char *header_na p = strstr(q, header_name_prefix); if (p == NULL) break; - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: prefix = '''%s'''", FL, __func__, p); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: prefix = '''%s'''", FL, __func__, p); q = strchr(p,'='); if (q == NULL) break; @@ -1282,12 +1280,12 @@ int MIMEH_recompose_multivalue( struct MIMEH_header_info *hinfo, char *header_na // Move the pointer past the '=' separator q++; - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: data = '''%s'''", FL, __func__, q); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: data = '''%s'''", FL, __func__, q); // Is our string quoted (ie, will likely contain whitespace) // if (*q == '"') { - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: multipart segment is quote-prefixed", FL, __func__); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: multipart segment is quote-prefixed", FL, __func__); is_quoted = 1; } @@ -1327,12 +1325,12 @@ int MIMEH_recompose_multivalue( struct MIMEH_header_info *hinfo, char *header_na MIMEH_decode_multivalue_language_string(q); } - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: segment value = '%s', appending to '%s'", FL, __func__, q, buffer); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: segment value = '%s', appending to '%s'", FL, __func__, q, buffer); snprintf(buffer_start,buffer_size,"%s",q); q_len = strlen(q); buffer_size -= q_len; buffer_start += q_len; - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Buffer[remaining=%d]= '%s'", FL, __func__, buffer_size,buffer); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Buffer[remaining=%d]= '%s'", FL, __func__, buffer_size,buffer); if (end_point != NULL) { @@ -1345,7 +1343,7 @@ int MIMEH_recompose_multivalue( struct MIMEH_header_info *hinfo, char *header_na } - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: End point set to: [%d] '%s'",FL, __func__, (*data_end_point -header_value), *data_end_point); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: End point set to: [%d] '%s'",FL, __func__, (*data_end_point -header_value), *data_end_point); return result; } @@ -1484,7 +1482,7 @@ int MIMEH_parse_header_parameter( struct MIMEH_header_info *hinfo, char *data, // which needs to be decoded (ie, name*1*=foo name*2*=bar ) if (*string == '*') { - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Found a '*' after the name, so attempting multipart value decode",FL, __func__); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Found a '*' after the name, so attempting multipart value decode",FL, __func__); // PLD:DEV:11/08/2004-18H30 // Issue: RFC2231 handling @@ -1531,7 +1529,7 @@ int MIMEH_parse_header_parameter( struct MIMEH_header_info *hinfo, char *data, // skip any spaces... again while ( isspace((int) *string ) ) string++; - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Extracting value out of '%s'",FL, __func__, string); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Extracting value out of '%s'",FL, __func__, string); // Because of all the potential exploits and bad behaviour // we have to be really careful about how we determine @@ -1552,7 +1550,7 @@ int MIMEH_parse_header_parameter( struct MIMEH_header_info *hinfo, char *data, // broken MUA or due to an exploit attempt. char *string_end; - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Using quoted-string tests",FL, __func__); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Using quoted-string tests",FL, __func__); // Remove multiple-sequential quotes string++; @@ -1565,7 +1563,7 @@ int MIMEH_parse_header_parameter( struct MIMEH_header_info *hinfo, char *data, string_end = strchr(string+1, '\"'); if (string_end != NULL) { - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: End of value found",FL, __func__); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: End of value found",FL, __func__); *string_end = '\0'; *data_end_point = string_end +1; } else { @@ -1594,7 +1592,7 @@ int MIMEH_parse_header_parameter( struct MIMEH_header_info *hinfo, char *data, { char *string_end; - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Using NON-quoted-string tests",FL, __func__); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Using NON-quoted-string tests",FL, __func__); string_end = strpbrk(string,"; \n\r\t"); if (string_end != NULL) { @@ -1608,7 +1606,7 @@ int MIMEH_parse_header_parameter( struct MIMEH_header_info *hinfo, char *data, break; } /** end of switch **/ - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Extracting value out of '%s'",FL, __func__, string); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Extracting value out of '%s'",FL, __func__, string); // Trim up and leading/trailing quotes if (((*string == '\"')&&(*(string +strlen(string)-1) == '\"')) || ((*string == '\'')&&(*(string +strlen(string)-1) == '\'')) ) @@ -1765,12 +1763,12 @@ int MIMEH_parse_contenttype( char *header_name, char *header_value, struct MIMEH // CONTENT TYPE ------------------------------- - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Start",FL, __func__); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Start",FL, __func__); p = strstr(header_name,"content-type"); if (p != NULL) { - DMIMEH LOGGER_log("%s:%d:%s: Content-type string found in header-name",FL, __func__); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s: Content-type string found in header-name",FL, __func__); /** 20041216-1106:PLD: Increase our sanity **/ hinfo->sanity++; @@ -1822,7 +1820,7 @@ int MIMEH_parse_contenttype( char *header_name, char *header_value, struct MIMEH ** with that char normally, we'll convert it to something ** else **/ - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Located x-mac attachment",FL, __func__); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Located x-mac attachment",FL, __func__); hinfo->x_mac = 1; FNFILTER_set_mac(hinfo->x_mac); } @@ -1901,16 +1899,16 @@ int MIMEH_parse_contenttype( char *header_name, char *header_value, struct MIMEH // Move the parameter search point up to where we stopped // processing the data in the MIMEH_parse_header_parameter() call - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Pushing new filename to stack '%s'",FL, __func__, hinfo->name); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Pushing new filename to stack '%s'",FL, __func__, hinfo->name); /** Step 1: Check to see if this filename already ** exists in the stack. We do this so that we don't ** duplicate entries and also to prevent false ** bad-header reports. **/ if (SS_cmp(&(hinfo->ss_names), hinfo->name, strlen(hinfo->name))==NULL) { - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Filtering '%s'",FL, __func__, hinfo->name); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Filtering '%s'",FL, __func__, hinfo->name); FNFILTER_filter(hinfo->name, _MIMEH_FILENAMELEN_MAX); - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Pushing '%s'",FL, __func__, hinfo->name); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Pushing '%s'",FL, __func__, hinfo->name); SS_push(&(hinfo->ss_names),hinfo->name,strlen(hinfo->name)); if (SS_count(&(hinfo->ss_names)) > 1) { @@ -1928,10 +1926,10 @@ int MIMEH_parse_contenttype( char *header_name, char *header_value, struct MIMEH ** Look for the MIME Boundary specification in the headers **/ return_value = MIMEH_parse_header_parameter(hinfo, param, "boundary", hinfo->boundary, sizeof(hinfo->boundary), &data_end_point); - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Param<=>data_end gap = %d", FL, __func__, data_end_point -param); - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: param start pos = '%s'",FL, __func__, param); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Param<=>data_end gap = %d", FL, __func__, data_end_point -param); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: param start pos = '%s'",FL, __func__, param); if (data_end_point > param) param = data_end_point; - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: param start pos = '%s'",FL, __func__, param); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: param start pos = '%s'",FL, __func__, param); if ( return_value == 0 ) { // Move the parameter search point up to where we stopped @@ -1956,9 +1954,9 @@ int MIMEH_parse_contenttype( char *header_name, char *header_value, struct MIMEH //param = PLD_strtok( &tx, NULL, ";\n\r" ); // * PLD:20040831-22H15: Added 'if (param != NULL)' prefix to debugging lines // * In response to bug #32, submitted by ICL ZA - if (param != NULL) DMIMEH LOGGER_log("%s:%d:%s:DEBUG: param start pos = '%s'",FL, __func__, param); + if (param != NULL) if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: param start pos = '%s'",FL, __func__, param); param = strpbrk( param, ";\n\r " ); - if (param != NULL) DMIMEH LOGGER_log("%s:%d:%s:DEBUG: param start pos = '%s'",FL, __func__, param); + if (param != NULL) if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: param start pos = '%s'",FL, __func__, param); } // While } @@ -2200,7 +2198,7 @@ int MIMEH_parse_contentdisposition( char *header_name, char *header_value, struc *q = '\0'; } - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Disposition string = '%s'",FL, __func__, hv); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Disposition string = '%s'",FL, __func__, hv); // Commence to decode the disposition string into its components. p = strpbrk( hv, ";\t\n\r " ); @@ -2282,7 +2280,7 @@ int MIMEH_parse_generic( char *header_name, char *header_value, struct MIMEH_hea int compare_result = 0; int tlen; - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Searching for %s in %s",FL, __func__, tokenstr,header_name); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Searching for %s in %s",FL, __func__, tokenstr,header_name); /** Sanity check the parameters **/ if (hinfo == NULL) return -1; if (tokenstr == NULL) return -1; @@ -2301,7 +2299,7 @@ int MIMEH_parse_generic( char *header_name, char *header_value, struct MIMEH_hea case ' ': case '\t': case '\0': - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Located! Sanity up +1",FL, __func__); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Located! Sanity up +1",FL, __func__); snprintf( buffer, bsize, "%s", header_value ); hinfo->sanity++; break; @@ -2521,7 +2519,7 @@ int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ) char *header_name_end_position; char *header_value_end_position; - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Processing '%s'",FL, __func__, current_header_position); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Processing '%s'",FL, __func__, current_header_position); /** Tokenise for the header 'name', ie, content-type, subject etc **/ header_name = current_header_position; @@ -2537,11 +2535,11 @@ int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ) // NOTE: this may activate on the true-blank lines, hence why we // dump the source string, just for confirmation - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Could not locate ':' separator, using whitespace (source='%s')",FL, __func__, header_name); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Could not locate ':' separator, using whitespace (source='%s')",FL, __func__, header_name); header_name_end_position = strpbrk( header_name, "\t " ); if (header_name_end_position == NULL) { - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Cannot find a header name:value pair in '%s'",FL, __func__, header_name); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Cannot find a header name:value pair in '%s'",FL, __func__, header_name); } } @@ -2802,17 +2800,17 @@ int MIMEH_headers_get(FILE* header_file, FILE* original_header_file, struct MIM int MIMEH_parse_headers( FILE* header_file, FILE* original_header_file, FFGET_FILE *f, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers ) { int result = 0; - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Start [F=%p, hinfo=%p]\n", FL, __func__, f, hinfo); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Start [F=%p, hinfo=%p]\n", FL, __func__, f, hinfo); /** 20041216-1100:PLD: Set the header sanity to zero **/ if ( result == 0 ) hinfo->sanity = 0; /** Proceed to read, process and finish headers **/ - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Getting headers",FL, __func__); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Getting headers",FL, __func__); if ( result == 0 ) result = MIMEH_headers_get( header_file, original_header_file, hinfo, f, unpack_metadata, save_headers_original, save_headers, glb.headerline); - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: Processing headers",FL, __func__); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Processing headers",FL, __func__); if ( result == 0 ) result = MIMEH_headers_process( hinfo, glb.headerline ); - DMIMEH LOGGER_log("%s:%d:%s:DEBUG: cleanup of headers",FL, __func__); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: cleanup of headers",FL, __func__); if (glb.headerline != NULL) { free(glb.headerline); From a80cbaa351740207e10240880f531994d81cdae3 Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Mon, 5 May 2025 09:58:04 +0300 Subject: [PATCH 77/80] Improve naming, add compilability for `sizeof(char) != 1` case --- ffget.h | 2 +- mime.c | 12 ++++++------ mime_headers.h | 48 +++++++++++++++++++++++++----------------------- ripmime.c | 22 +++++++++++----------- 4 files changed, 43 insertions(+), 41 deletions(-) diff --git a/ffget.h b/ffget.h index bb251d7..99b7442 100644 --- a/ffget.h +++ b/ffget.h @@ -27,7 +27,7 @@ struct _FFGET_FILE { FILE *f; - char buffer[FFGET_BUFFER_MAX+4]; + char buffer[FFGET_BUFFER_MAX + 4 * sizeof(char)]; char *startpoint; char *endpoint; char *buffer_end; diff --git a/mime.c b/mime.c index 4330a7f..46eafab 100644 --- a/mime.c +++ b/mime.c @@ -142,7 +142,7 @@ struct MIME_globals { int stderrlogging; char headersname[_MIME_STRLEN_MAX]; char tempdirectory[_MIME_STRLEN_MAX]; - int save_headers; + int dump_headers; int attachment_count; int current_line; int no_nameless; @@ -641,7 +641,7 @@ int MIME_set_filename_report_fn( int (*ptr_to_fn)(char *, char *) ) int MIME_set_dumpheaders( int level ) { - glb.save_headers = level; + glb.dump_headers = level; return 0; } @@ -1931,7 +1931,7 @@ void MIME_init( void ) glb.quiet = 0; glb.syslogging = 0; glb.stderrlogging = 1; - glb.save_headers = 0; + glb.dump_headers = 0; glb.no_nameless = 0; glb.mailbox_format = 0; glb.name_by_type = 0; @@ -3095,8 +3095,8 @@ int MIME_unpack_single_file( RIPMIME_output *unpack_metadata, FILE *fi, int curr h.current_recursion_level = current_recursion_level; glb.current_line = 0; if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: recursion level checked...%d\n",FL,__func__, current_recursion_level); - if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: DumpHeaders = %d\n",FL,__func__, glb.save_headers); - if ((!hf)&&(glb.save_headers)) + if (MIME_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: DumpHeaders = %d\n",FL,__func__, glb.dump_headers); + if ((!hf)&&(glb.dump_headers)) { char * fn; int fn_l = strlen(unpack_metadata->dir) + strlen(glb.headersname) + sizeof(char) * 2; @@ -3108,7 +3108,7 @@ int MIME_unpack_single_file( RIPMIME_output *unpack_metadata, FILE *fi, int curr hf = fopen(fn,"w"); if (!hf) { - glb.save_headers = 0; + glb.dump_headers = 0; LOGGER_log("%s:%d:%s:ERROR: Cannot open '%s' for writing (%s)", FL,__func__, fn, strerror(errno)); } else diff --git a/mime_headers.h b/mime_headers.h index 128d61c..ebd5441 100644 --- a/mime_headers.h +++ b/mime_headers.h @@ -65,20 +65,22 @@ #define _MIMEH_FOUND_FROM 100 #ifndef PATH_MAX -#define PATH_MAX 4096 +#define PATH_MAX 4096 * sizeof(char) #endif -#define _MIMEH_STRLEN_MAX 10230 +#define _MIMEH_STRLEN_MAX 10230 * sizeof(char) #define _MIMEH_FILENAMELEN_MAX PATH_MAX -#define _MIMEH_CONTENT_TYPE_MAX 1280 -#define _MIMEH_SUBJECTLEN_MAX 1280 -#define _MIMEH_CONTENT_DESCRIPTION_MAX 1280 -#define _MIMEH_CONTENT_TRANSFER_ENCODING_MAX 2560 -#define _MIMEH_CONTENT_DISPOSITION_MAX 2560 +#define _MIMEH_CONTENT_TYPE_MAX 1280 * sizeof(char) +#define _MIMEH_SUBJECTLEN_MAX 1280 * sizeof(char) +#define _MIMEH_CONTENT_DESCRIPTION_MAX 1280 * sizeof(char) +#define _MIMEH_CONTENT_TRANSFER_ENCODING_MAX 2560 * sizeof(char) +#define _MIMEH_CONTENT_DISPOSITION_MAX 2560 * sizeof(char) +#define _MIMEH_CHARSET_MAX 128 * sizeof(char) + #define _MIMEH_DEBUG_NORMAL 1 #define _MIMEH_DEBUG_PEDANTIC 10 #define _MIMEH_DEFECT_ARRAY_SIZE 100 -#define _MIMEH_CHARSET_MAX 128 + // Errors to throw back #define MIMEH_ERROR_DISK_FULL 128 @@ -100,20 +102,20 @@ struct MIMEH_header_info FILE *header_file; FILE *original_header_file; int content_type; - char content_type_string[ _MIMEH_CONTENT_TYPE_MAX + 1 ]; - char content_description_string[ _MIMEH_CONTENT_DESCRIPTION_MAX + 1 ]; - char boundary[_MIMEH_STRLEN_MAX + 1]; + char content_type_string[ _MIMEH_CONTENT_TYPE_MAX + 1 * sizeof(char) ]; + char content_description_string[ _MIMEH_CONTENT_DESCRIPTION_MAX + 1 * sizeof(char) ]; + char boundary[_MIMEH_STRLEN_MAX + 1 * sizeof(char)]; int boundary_located; - char subject[_MIMEH_SUBJECTLEN_MAX + 1]; - char filename[_MIMEH_FILENAMELEN_MAX + 1]; - char name[_MIMEH_STRLEN_MAX + 1]; + char subject[_MIMEH_SUBJECTLEN_MAX + 1 * sizeof(char)]; + char filename[_MIMEH_FILENAMELEN_MAX + 1 * sizeof(char)]; + char name[_MIMEH_STRLEN_MAX + 1 * sizeof(char)]; /** 20041217-1601:PLD: New header fields to keep **/ - char from[_MIMEH_STRLEN_MAX + 1]; - char date[_MIMEH_STRLEN_MAX + 1]; - char to[_MIMEH_STRLEN_MAX + 1]; - char messageid[_MIMEH_STRLEN_MAX + 1]; - char received[_MIMEH_STRLEN_MAX + 1]; + char from[_MIMEH_STRLEN_MAX + 1 * sizeof(char)]; + char date[_MIMEH_STRLEN_MAX + 1 * sizeof(char)]; + char to[_MIMEH_STRLEN_MAX + 1 * sizeof(char)]; + char messageid[_MIMEH_STRLEN_MAX + 1 * sizeof(char)]; + char received[_MIMEH_STRLEN_MAX + 1 * sizeof(char)]; /** end of new fields **/ // Store multiple filenames @@ -122,14 +124,14 @@ struct MIMEH_header_info struct SS_object ss_names; int content_transfer_encoding; - char content_transfer_encoding_string[ _MIMEH_CONTENT_TRANSFER_ENCODING_MAX + 1 ]; + char content_transfer_encoding_string[ _MIMEH_CONTENT_TRANSFER_ENCODING_MAX + 1 * sizeof(char) ]; int content_disposition; - char content_disposition_string[ _MIMEH_CONTENT_DISPOSITION_MAX + 1 ]; + char content_disposition_string[ _MIMEH_CONTENT_DISPOSITION_MAX + 1 * sizeof(char) ]; //int charset; - char charset[ _MIMEH_CHARSET_MAX + 1 ]; + char charset[ _MIMEH_CHARSET_MAX + 1 * sizeof(char) ]; int format; int file_has_uuencode; - char uudec_name[_MIMEH_FILENAMELEN_MAX + 1]; // UUDecode name. This is a post-decode information field. + char uudec_name[_MIMEH_FILENAMELEN_MAX + 1 * sizeof(char)]; // UUDecode name. This is a post-decode information field. int current_recursion_level; // Malformed email reporting diff --git a/ripmime.c b/ripmime.c index 58ec718..6e5114b 100644 --- a/ripmime.c +++ b/ripmime.c @@ -36,7 +36,7 @@ struct RIPMIME_globals { - char *inputfile; + char *input_path; RIPMIME_output *output; int use_return_codes; int timeout; @@ -183,14 +183,14 @@ int RIPMIME_parse_parameters (struct RIPMIME_globals *glb, int argc, char **argv case 'i': if (argv[i][2] != '\0') { - glb->inputfile = &argv[i][2]; + glb->input_path = &argv[i][2]; } else { i++; if (i < argc) { - glb->inputfile = argv[i]; + glb->input_path = argv[i]; } else { @@ -533,7 +533,7 @@ int RIPMIME_init (struct RIPMIME_globals *glb, RIPMIME_output *o) glb->output->unpack_mode = RIPMIME_UNPACK_MODE_TO_DIRECTORY; glb->output->rename_method = _MIME_RENAME_METHOD_INFIX; glb->output->unique_names = 1; - glb->inputfile = NULL; + glb->input_path = NULL; glb->use_return_codes = 0; glb->timeout = 0; glb->quiet = 0; @@ -559,7 +559,7 @@ Side Effects : \------------------------------------------------------------------*/ void RIPMIME_signal_alarm( int sig ) { - if (ripmime_globals->quiet == 0) LOGGER_log("%s:%d:RIPMIME_signal_alarm: ripMIME took too long to complete. Mailpack is \"%s\", output dir is \"%s\"",FL, ripmime_globals->inputfile, ripmime_globals->output->dir ); + if (ripmime_globals->quiet == 0) LOGGER_log("%s:%d:RIPMIME_signal_alarm: ripMIME took too long to complete. Mailpack is \"%s\", output dir is \"%s\"",FL, ripmime_globals->input_path, ripmime_globals->output->dir ); exit(RIPMIME_ERROR_TIMEOUT); } @@ -623,8 +623,8 @@ int RIPMIME_unpack( struct RIPMIME_globals *glb ) /** If we're not inputting from STDIN, check to see if the ** input is a directory **/ - if (strcmp(glb->inputfile,"-")!=0) { - stat_result = stat(glb->inputfile, &st); + if (strcmp(glb->input_path,"-")!=0) { + stat_result = stat(glb->input_path, &st); if (stat_result != 0) return -1; if (S_ISDIR(st.st_mode)) input_is_directory = 1; @@ -636,7 +636,7 @@ int RIPMIME_unpack( struct RIPMIME_globals *glb ) struct dirent *dir_entry; fprintf(stderr,"input file is a directory, recursing\n"); - dir = opendir(glb->inputfile); + dir = opendir(glb->input_path); if (dir == NULL) return -1; do { @@ -650,7 +650,7 @@ int RIPMIME_unpack( struct RIPMIME_globals *glb ) if (strcmp(dir_entry->d_name, ".")==0) continue; if (strcmp(dir_entry->d_name, "..")==0) continue; - snprintf(fullfilename,sizeof(fullfilename),"%s/%s", glb->inputfile, dir_entry->d_name); + snprintf(fullfilename,sizeof(fullfilename),"%s/%s", glb->input_path, dir_entry->d_name); stat_result = stat( fullfilename, &st ); if (stat_result != 0) continue; if (S_ISREG(st.st_mode)) { @@ -664,7 +664,7 @@ int RIPMIME_unpack( struct RIPMIME_globals *glb ) } else { /** If the supplied file was actually a normal file, then decode normally **/ - result = RIPMIME_unpack_single( glb, glb->inputfile ); + result = RIPMIME_unpack_single( glb, glb->input_path ); } return result; } @@ -724,7 +724,7 @@ int main (int argc, char **argv) RIPMIME_parse_parameters (&glb, argc, argv); // if our input filename wasn't specified, then we better let the user know! - if (!glb.inputfile) + if (!glb.input_path) { LOGGER_log("Error: No input file was specified\n"); return RIPMIME_ERROR_NO_INPUT_FILE; From a7b55aa35bcac1a714f3ade7c1b74c68ee6347fd Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Mon, 5 May 2025 10:00:42 +0300 Subject: [PATCH 78/80] FFget macros for `sizeof(char) != 1` case --- ffget.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/ffget.h b/ffget.h index 99b7442..4cf878d 100644 --- a/ffget.h +++ b/ffget.h @@ -13,16 +13,16 @@ #define FFGET_DNORMAL ((FFGET_debug >= FFGET_DEBUG_NORMAL )) #define FFGET_DPEDANTIC ((FFGET_debug >= FFGET_DEBUG_PEDANTIC)) -#define FFGET_MAX_LINE_LEN 1024 -#define FFGET_BUFFER_MAX 8192 +#define FFGET_MAX_LINE_LEN 1024 * sizeof(char) +#define FFGET_BUFFER_MAX 8192 * sizeof(char) #define FFGET_BUFFER_PADDING 1 #define FFGET_DEBUG_NORMAL 1 #define FFGET_DEBUG_PEDANTIC 10 -#define FFGET_LINEBREAK_NONE 0 -#define FFGET_LINEBREAK_LF 1 -#define FFGET_LINEBREAK_CR 2 +#define FFGET_LINEBREAK_NONE 0 +#define FFGET_LINEBREAK_LF 1 +#define FFGET_LINEBREAK_CR 2 struct _FFGET_FILE { From 0db7b314801c8f50ccd75918ec89a71b285f3ec4 Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Mon, 5 May 2025 10:35:57 +0300 Subject: [PATCH 79/80] Refactor `headerline` from global to `MIME_headers` member also improve naming --- mime_headers.c | 117 +++++++++++++++++++++---------------------------- mime_headers.h | 4 +- ripmime.c | 2 +- 3 files changed, 54 insertions(+), 69 deletions(-) diff --git a/mime_headers.c b/mime_headers.c index 307c9a4..1934c31 100644 --- a/mime_headers.c +++ b/mime_headers.c @@ -57,8 +57,8 @@ char *MIMEH_defect_description_array[_MIMEH_DEFECT_ARRAY_SIZE]; -int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers, char* headerline ); -int MIMEH_headers_get(FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers, char* headerline ); +int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers ); +int MIMEH_headers_get(FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers ); int MIMEH_read_primary_headers( FILE* header_file, FILE* original_header_file, char *fname, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers ); struct MIMEH_globals { @@ -70,7 +70,6 @@ struct MIMEH_globals { char subject[_MIMEH_STRLEN_MAX +1]; - char *headerline; int test_mailbox; int debug; int webform; @@ -124,8 +123,6 @@ int MIMEH_version(void) void MIMEH_init( void ) { glb.doubleCR = 0; - glb.headerline = NULL; - glb.test_mailbox = 0; glb.debug = 0; glb.webform = 0; @@ -882,7 +879,7 @@ Purpose: Reads from the stream F until it detects a From line, or a blank Output: Errors: ------------------------------------------------------------------------*/ -int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers, char* headerline ) +int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers ) { char buffer[_MIMEH_STRLEN_MAX+1]; int totalsize=0; @@ -918,7 +915,7 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI char *headerline_original = NULL; // Holds the original header-form without decoding. search_count++; - glb.headerline = NULL; + hinfo->headerline_buffer = NULL; tmp_original = NULL; while ((fget_result=FFGET_fgets(buffer,_MIMEH_STRLEN_MAX, f))) @@ -931,7 +928,7 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI else if (strstr(linestart,"\r\r")) hinfo->crcr_count++; else if (strchr(linestart,'\n')) hinfo->lf_count++; - if (MIMEH_DNORMAL)LOGGER_log("%s:%d:%s: [CRLF=%d, CRCR=%d, LF=%d] Data In=[sz=%d:tb=%d:mem=%p]'%s'",FL, __func__, hinfo->crlf_count, hinfo->crcr_count, hinfo->lf_count, linesize, f->trueblank, glb.headerline, buffer); + if (MIMEH_DNORMAL)LOGGER_log("%s:%d:%s: [CRLF=%d, CRCR=%d, LF=%d] Data In=[sz=%d:tb=%d:mem=%p]'%s'",FL, __func__, hinfo->crlf_count, hinfo->crcr_count, hinfo->lf_count, linesize, f->trueblank, hinfo->headerline_buffer, buffer); // If we are being told to copy the input data to an output file // then do so here (this is for the originals) @@ -945,7 +942,7 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI // as it comes in from ffget, then do the storage here if (save_headers_original) { - if (MIMEH_DNORMAL) LOGGER_log("MIMEH_read_headers:DEBUG:Data-In:[%d:%d] '%s'", strlen(linestart), linesize, linestart); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG:Data-In:[%d:%d] '%s'", FL, __func__, strlen(linestart), linesize, linestart); tmp_original = realloc(headerline_original, totalsize_original+linesize+1); if (tmp_original == NULL) { @@ -972,32 +969,32 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI /** Normal processing of the headers now starts. **/ if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: realloc'ing dataspace",FL, __func__); - tmp = realloc(glb.headerline, totalsize+linesize+1); + tmp = realloc(hinfo->headerline_buffer, totalsize+linesize+1); if (tmp == NULL) { LOGGER_log("%s:%d:%s:ERROR: Cannot allocate %d bytes to contain new headers ", FL, __func__, totalsize +linesize +1); - if (glb.headerline != NULL) - free(glb.headerline); - glb.headerline = NULL; + if (hinfo->headerline_buffer != NULL) + free(hinfo->headerline_buffer); + hinfo->headerline_buffer = NULL; return -1; } - if (glb.headerline == NULL) + if (hinfo->headerline_buffer == NULL) { if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Initial appending of head to dataspace headerline = NULL realloc block = %p linestart = %p linesize = %d",FL, __func__, tmp, linestart, linesize); - glb.headerline = tmp; + hinfo->headerline_buffer = tmp; totalsize = linesize; - PLD_strncpy(glb.headerline, linestart, (linesize +1)); + PLD_strncpy(hinfo->headerline_buffer, linestart, (linesize +1)); } // If the global headerline is currently NULL else { - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Appending of new data to existing header existing-headerline = %p new realloc block = %p linestart = %p linesize = %d",FL, __func__, glb.headerline, tmp, linestart, linesize); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Appending of new data to existing header existing-headerline = %p new realloc block = %p linestart = %p linesize = %d",FL, __func__, hinfo->headerline_buffer, tmp, linestart, linesize); // Perform header unfolding by removing any CRLF's // of the last line if the first characters of the // newline are blank/space - glb.headerline = tmp; + hinfo->headerline_buffer = tmp; if ((linestart < lineend)&&((*linestart == '\t')||(*linestart == ' '))) { @@ -1013,9 +1010,9 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI // 'p' holds the location at the -end- of the current headers where // we are going to append the newly read line - p = glb.headerline +totalsize -1; - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: unwrapping headers headers=%p, p = %p",FL, __func__, glb.headerline, p); - while ((p >= glb.headerline)&&(( *p == '\n' )||( *p == '\r' ))) + p = hinfo->headerline_buffer + totalsize -1; + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: unwrapping headers headers=%p, p = %p",FL, __func__, hinfo->headerline_buffer, p); + while ((p >= hinfo->headerline_buffer)&&(( *p == '\n' )||( *p == '\r' ))) { if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Removing trailing space p=[%p]%c",FL, __func__, p, *p); *p = '\0'; @@ -1023,30 +1020,30 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI totalsize--; } - p = glb.headerline +totalsize -1; + p = hinfo->headerline_buffer + totalsize -1; } - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Memcopying line, source = %p, dest = %p, size = %d", FL, __func__, linestart, glb.headerline +totalsize, linesize); - memcpy((glb.headerline +totalsize), linestart, (linesize)); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Memcopying line, source = %p, dest = %p, size = %d", FL, __func__, linestart, hinfo->headerline_buffer + totalsize, linesize); + memcpy((hinfo->headerline_buffer + totalsize), linestart, (linesize)); totalsize += linesize; - *(glb.headerline +totalsize) = '\0'; + *(hinfo->headerline_buffer + totalsize) = '\0'; - } // If the glb.headerline already is allocated and we're appending to it. + } // If the hinfo->headerline_buffer already is allocated and we're appending to it. if (f->trueblank) { if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Trueblank line detected in header reading",FL, __func__); - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Headers /before/ decoding\n-------\n%s\n-------------------",FL, __func__, glb.headerline); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Headers /before/ decoding\n-------\n%s\n-------------------",FL, __func__, hinfo->headerline_buffer); - MIMEH_fix_header_mistakes( glb.headerline ); - MDECODE_decode_ISO( glb.headerline, totalsize ); + MIMEH_fix_header_mistakes( hinfo->headerline_buffer ); + MDECODE_decode_ISO( hinfo->headerline_buffer, totalsize ); - if ((save_headers)&&(glb.headerline)) + if ((save_headers)&&(hinfo->headerline_buffer)) { if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Saving header line.",FL, __func__); - fprintf(hinfo->header_file,"%s",glb.headerline); + fprintf(hinfo->header_file,"%s",hinfo->headerline_buffer); } - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Final Headers\n------------------\n%s---------------", FL, __func__, glb.headerline); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Final Headers\n------------------\n%s---------------", FL, __func__, hinfo->headerline_buffer); //result = 1; //result = 0; break; @@ -1078,14 +1075,14 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:ERROR: FFGET module ran out of input while reading headers",FL, __func__); /** If we're meant to be saving the headers, we better do that now, even though we couldn't ** read everything we wanted to **/ - if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: save_headers=%d totalsize=%d headerline=%s", FL, __func__, save_headers, totalsize, glb.headerline); + if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: save_headers=%d totalsize=%d headerline=%s", FL, __func__, save_headers, totalsize, hinfo->headerline_buffer); - if ((save_headers)&&(glb.headerline)) + if ((save_headers)&&(hinfo->headerline_buffer)) { if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Saving header line.",FL, __func__); - MIMEH_fix_header_mistakes( glb.headerline ); - MDECODE_decode_ISO( glb.headerline, totalsize ); - fprintf(hinfo->header_file,"%s",glb.headerline); + MIMEH_fix_header_mistakes( hinfo->headerline_buffer ); + MDECODE_decode_ISO( hinfo->headerline_buffer, totalsize ); + fprintf(hinfo->header_file,"%s",hinfo->headerline_buffer); } result = -1; @@ -1093,7 +1090,7 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI if (glb.header_longsearch > 0) { /** Test the headers for RFC compliance... **/ - is_RFC822_headers = MIMEH_are_headers_RFC822(glb.headerline); + is_RFC822_headers = MIMEH_are_headers_RFC822(hinfo->headerline_buffer); if (is_RFC822_headers == 0) { /** If not RFC822 headers, then clean up everything we allocated in here **/ @@ -1103,10 +1100,10 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI free(headerline_original); headerline_original = NULL; } - if (glb.headerline != NULL) + if (hinfo->headerline_buffer != NULL) { - free(glb.headerline); - glb.headerline = NULL; + free(hinfo->headerline_buffer); + hinfo->headerline_buffer = NULL; } } } @@ -2485,35 +2482,33 @@ int MIMEH_parse_charset( char *header_name, char *header_value, struct MIMEH_hea Changes: \------------------------------------------------------------------*/ -int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ) +void MIMEH_headers_process( struct MIMEH_header_info *hinfo ) { /** scan through our headers string looking for information that is ** valid **/ - char *h, *safehl; + char *safehl; char *current_header_position; int headerlength; if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Start [hinfo=%p]\n",FL, __func__, hinfo); - h = headers; - /** Duplicate the headers for processing - this way we don't 'taint' the ** original headers during our searching / altering. **/ - headerlength = strlen(h); + headerlength = strlen(hinfo->headerline_buffer); safehl = malloc(sizeof(char) *(headerlength+1)); - PLD_strncpy(safehl, h, headerlength+1); + PLD_strncpy(safehl, hinfo->headerline_buffer, headerlength+1); if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Header length = %d\n", FL, __func__, headerlength); - MIMEH_strip_comments(h); + MIMEH_strip_comments(hinfo->headerline_buffer); - current_header_position = h; + current_header_position = hinfo->headerline_buffer; // Searching through the headers, we seek out header 'name:value;value;value' sets, // Each set is then cleaned up, seperated and parsed. - while ((current_header_position != NULL)&&( current_header_position <= (h +headerlength) )) + while ((current_header_position != NULL)&&( current_header_position <= (hinfo->headerline_buffer +headerlength) )) { char *header_name, *header_value; char *header_name_end_position; @@ -2667,7 +2662,6 @@ int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ) if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: END [hinfo=%p]\n", FL, __func__, hinfo); - return 0; } /*-----------------------------------------------------------------\ @@ -2710,7 +2704,7 @@ int MIMEH_headers_clearcount( struct MIMEH_header_info *hinfo ) { Changes: \------------------------------------------------------------------*/ -int MIMEH_headers_get(FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers, char* headerline ) +int MIMEH_headers_get(FILE* header_file, FILE* original_header_file, struct MIMEH_header_info *hinfo, FFGET_FILE *f, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers ) { int result = 0; @@ -2751,7 +2745,7 @@ int MIMEH_headers_get(FILE* header_file, FILE* original_header_file, struct MIM // Read from the file, the headers we need FFGET_set_watch_SDL(1); - result = MIMEH_read_headers(header_file, original_header_file, hinfo, f, unpack_metadata, save_headers_original, save_headers, glb.headerline); + result = MIMEH_read_headers(header_file, original_header_file, hinfo, f, unpack_metadata, save_headers_original, save_headers); FFGET_set_watch_SDL(0); if (hinfo->lf_count > hinfo->crlf_count) { @@ -2762,17 +2756,12 @@ int MIMEH_headers_get(FILE* header_file, FILE* original_header_file, struct MIM // flag this, free up the headers, and return. if (result == -1) { - if (glb.headerline) - { - free(glb.headerline); - glb.headerline = NULL; - } return result; } // If we came back with an OKAY result, but there's nothing in the // headers, then flag off an error - if (glb.headerline == NULL) + if (hinfo->headerline_buffer == NULL) { if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: null headerline\n", FL, __func__); return 1; @@ -2807,15 +2796,9 @@ int MIMEH_parse_headers( FILE* header_file, FILE* original_header_file, FFGET_FI /** Proceed to read, process and finish headers **/ if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Getting headers",FL, __func__); - if ( result == 0 ) result = MIMEH_headers_get( header_file, original_header_file, hinfo, f, unpack_metadata, save_headers_original, save_headers, glb.headerline); + if ( result == 0 ) result = MIMEH_headers_get( header_file, original_header_file, hinfo, f, unpack_metadata, save_headers_original, save_headers); if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Processing headers",FL, __func__); - if ( result == 0 ) result = MIMEH_headers_process( hinfo, glb.headerline ); - if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: cleanup of headers",FL, __func__); - if (glb.headerline != NULL) - { - free(glb.headerline); - glb.headerline = NULL; - } + MIMEH_headers_process( hinfo ); if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: END [F=%p, hinfo=%p, sanity=%d]\n", FL, __func__, f, hinfo, hinfo->sanity); return result; diff --git a/mime_headers.h b/mime_headers.h index ebd5441..44db247 100644 --- a/mime_headers.h +++ b/mime_headers.h @@ -151,6 +151,8 @@ struct MIMEH_header_info int crlf_count; // 200811151149:PLD: Tally's the number of CRLF lines int crcr_count; // 200811151149:PLD: Tally's the number of CRLF lines int lf_count; // 200811151149:PLD: Tally's the number of LF only lines + + char *headerline_buffer; }; #ifdef RIPMIME_V2XX @@ -199,7 +201,7 @@ int MIMEH_headers_clearcount( struct MIMEH_header_info *hinfo ); -int MIMEH_headers_process( struct MIMEH_header_info *hinfo, char *headers ); +void MIMEH_headers_process( struct MIMEH_header_info *hinfo ); int MIMEH_headers_cleanup(); int MIMEH_parse_headers( FILE* header_file, FILE* original_header_file, FFGET_FILE *f, struct MIMEH_header_info *hinfo, RIPMIME_output *unpack_metadata, int save_headers_original, int save_headers ); diff --git a/ripmime.c b/ripmime.c index 6e5114b..c295e99 100644 --- a/ripmime.c +++ b/ripmime.c @@ -726,7 +726,7 @@ int main (int argc, char **argv) // if our input filename wasn't specified, then we better let the user know! if (!glb.input_path) { - LOGGER_log("Error: No input file was specified\n"); + LOGGER_log("Error: No input path or file was specified\n"); return RIPMIME_ERROR_NO_INPUT_FILE; } From e0cac25aea7481aefde43f48a0bddce88a8b56e3 Mon Sep 17 00:00:00 2001 From: mkgrgis Date: Mon, 5 May 2025 10:45:42 +0300 Subject: [PATCH 80/80] Improve code style --- mime_headers.c | 34 ++++++++++++++++------------------ mime_headers.h | 2 +- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/mime_headers.c b/mime_headers.c index 1934c31..6003647 100644 --- a/mime_headers.c +++ b/mime_headers.c @@ -64,11 +64,11 @@ int MIMEH_read_primary_headers( FILE* header_file, FILE* original_header_file, c struct MIMEH_globals { int doubleCR; int doubleCR_save; - char doubleCRname[_MIMEH_STRLEN_MAX +1]; + char doubleCRname[_MIMEH_STRLEN_MAX + 1 * sizeof(char)]; - char appledouble_filename[_MIMEH_STRLEN_MAX +1]; + char appledouble_filename[_MIMEH_STRLEN_MAX + 1 * sizeof(char)]; - char subject[_MIMEH_STRLEN_MAX +1]; + char subject[_MIMEH_STRLEN_MAX + 1 * sizeof(char)]; int test_mailbox; int debug; @@ -946,7 +946,7 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI tmp_original = realloc(headerline_original, totalsize_original+linesize+1); if (tmp_original == NULL) { - LOGGER_log("%s:%d:%s:ERROR: Cannot allocate %d bytes to contain new headers_original ", FL, __func__, totalsize_original +linesize +1); + LOGGER_log("%s:%d:%s:ERROR: Cannot allocate %d bytes to contain new headers_original ", FL, __func__, totalsize_original +linesize + 1); if (headerline_original != NULL) free(headerline_original); headerline_original = NULL; return -1; @@ -955,12 +955,12 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI if (headerline_original == NULL) { headerline_original = tmp_original; - totalsize_original = linesize +1; + totalsize_original = linesize + 1; PLD_strncpy( headerline_original, linestart, (linesize+1)); if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: '%s'", FL, __func__, headerline_original); } else { headerline_original = tmp_original; - PLD_strncpy( (headerline_original +totalsize_original -1), linestart, (linesize +1)); + PLD_strncpy( (headerline_original +totalsize_original -1), linestart, (linesize + 1)); totalsize_original += linesize; if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: HO = '%s'", FL, __func__, headerline_original); } @@ -972,7 +972,7 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI tmp = realloc(hinfo->headerline_buffer, totalsize+linesize+1); if (tmp == NULL) { - LOGGER_log("%s:%d:%s:ERROR: Cannot allocate %d bytes to contain new headers ", FL, __func__, totalsize +linesize +1); + LOGGER_log("%s:%d:%s:ERROR: Cannot allocate %d bytes to contain new headers ", FL, __func__, totalsize +linesize + 1); if (hinfo->headerline_buffer != NULL) free(hinfo->headerline_buffer); hinfo->headerline_buffer = NULL; @@ -984,7 +984,7 @@ int MIMEH_read_headers( FILE* header_file, FILE* original_header_file, struct MI if (MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Initial appending of head to dataspace headerline = NULL realloc block = %p linestart = %p linesize = %d",FL, __func__, tmp, linestart, linesize); hinfo->headerline_buffer = tmp; totalsize = linesize; - PLD_strncpy(hinfo->headerline_buffer, linestart, (linesize +1)); + PLD_strncpy(hinfo->headerline_buffer, linestart, (linesize + 1)); } // If the global headerline is currently NULL else { @@ -1332,7 +1332,7 @@ int MIMEH_recompose_multivalue( struct MIMEH_header_info *hinfo, char *header_na if (end_point != NULL) { *end_point = end_point_char; - q = end_point +1; + q = end_point + 1; } else q = NULL; @@ -1503,7 +1503,7 @@ int MIMEH_parse_header_parameter( struct MIMEH_header_info *hinfo, char *data, // Eliminate multiple = separators. // Reference: c030804-006a // PLD:DEV: 11/08/2004-15H15 - while ((*(string +1) == '=')&&(*(string+1) != '\0')) { string++; MIMEH_set_defect(hinfo,MIMEH_DEFECT_MULTIPLE_EQUALS_SEPARATORS); } + while ((*(string + 1) == '=')&&(*(string+1) != '\0')) { string++; MIMEH_set_defect(hinfo,MIMEH_DEFECT_MULTIPLE_EQUALS_SEPARATORS); } // Get the end of our string @@ -1562,7 +1562,7 @@ int MIMEH_parse_header_parameter( struct MIMEH_header_info *hinfo, char *data, { if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: End of value found",FL, __func__); *string_end = '\0'; - *data_end_point = string_end +1; + *data_end_point = string_end + 1; } else { // If string_end == NULL // @@ -1576,7 +1576,7 @@ int MIMEH_parse_header_parameter( struct MIMEH_header_info *hinfo, char *data, if (string_end != NULL) { *string_end = '\0'; - *data_end_point = string_end +1; + *data_end_point = string_end + 1; } else { // There is no termination to the string, instead the // end of the string is \0. @@ -1594,7 +1594,7 @@ int MIMEH_parse_header_parameter( struct MIMEH_header_info *hinfo, char *data, if (string_end != NULL) { *string_end = '\0'; - *data_end_point = string_end +1; + *data_end_point = string_end + 1; } else { // There is no termination to the string, instead the // end of the string is \0. @@ -2296,7 +2296,7 @@ int MIMEH_parse_generic( char *header_name, char *header_value, struct MIMEH_hea case ' ': case '\t': case '\0': - if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Located! Sanity up +1",FL, __func__); + if(MIMEH_DNORMAL) LOGGER_log("%s:%d:%s:DEBUG: Located! Sanity up + 1",FL, __func__); snprintf( buffer, bsize, "%s", header_value ); hinfo->sanity++; break; @@ -2582,7 +2582,7 @@ void MIMEH_headers_process( struct MIMEH_header_info *hinfo ) // parse the data through our various parsing // functions. - header_value = header_name_end_position +1; + header_value = header_name_end_position + 1; header_value_end_position = strpbrk( header_value, "\n\r" ); if ( header_value_end_position != NULL ) { @@ -2820,7 +2820,7 @@ Displays a list of the located defects Changes: \------------------------------------------------------------------*/ -int MIMEH_dump_defects( struct MIMEH_header_info *hinfo ) +void MIMEH_dump_defects( struct MIMEH_header_info *hinfo ) { int i; @@ -2843,8 +2843,6 @@ int MIMEH_dump_defects( struct MIMEH_header_info *hinfo ) } } - - return 0; } /*-----------------------------------------------------------------\ diff --git a/mime_headers.h b/mime_headers.h index 44db247..435365b 100644 --- a/mime_headers.h +++ b/mime_headers.h @@ -212,7 +212,7 @@ int MIMEH_set_webform( int level ); int MIMEH_set_outputdir( char *dir ); int MIMEH_set_defect( struct MIMEH_header_info *hinfo, int defect ); -int MIMEH_dump_defects( struct MIMEH_header_info *hinfo ); +void MIMEH_dump_defects( struct MIMEH_header_info *hinfo ); int MIMEH_get_defect_count( struct MIMEH_header_info *hinfo ); int MIMEH_set_report_MIME( int level );