-
Notifications
You must be signed in to change notification settings - Fork 14
/
libmime-decoders.c
592 lines (439 loc) · 18.7 KB
/
libmime-decoders.c
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <syslog.h>
#include <ctype.h>
#include <sys/stat.h>
#include <unistd.h>
#include <time.h>
#include <errno.h>
#include <dirent.h>
#include "ffget.h"
#include "pldstr.h"
#include "logger.h"
#include "libmime-decoders.h"
#ifndef FL
#define FL __FILE__,__LINE__
#endif
// Debug precodes
#define MDECODE_DPEDANTIC ((glb.debug >= MDECODE_DEBUG_PEDANTIC))
#define MDECODE_DNORMAL ((glb.debug >= MDECODE_DEBUG_NORMAL ))
#define DMD if ((glb.debug >= MDECODE_DEBUG_NORMAL))
/* 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, 62, 128, 128, 128, 63,\
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,\
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,\
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, 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, 128, 128, 128, 128, 128, 128, 128, 128, 128, 128 \
};
/**
* Jan 17th 2007 jjohnston
* Fixed a bug that decoded invalid QP sequences
*/
static unsigned char hexconv[256]={
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,\
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,\
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,\
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 20, 20, 20, 20, 20,\
20, 10, 11, 12, 13, 14, 15, 20, 20, 20, 20, 20, 20, 20, 20, 20,\
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,\
20, 10, 11, 12, 13, 14, 15, 20, 20, 20, 20, 20, 20, 20, 20, 20,\
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,\
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,\
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,\
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,\
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,\
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,\
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,\
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20,\
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20 \
};
struct MDECODE_globals {
int debug;
int verbose;
int decode_qp;
int decode_b64;
};
static struct MDECODE_globals glb;
int MDECODE_init( void )
{
glb.debug = 0;
glb.verbose = 0;
glb.decode_qp = 1;
glb.decode_b64 = 1;
return 0;
}
/*------------------------------------------------------------------------
Procedure: MIME_set_debug ID:1
Purpose: Sets the debug level for reporting in MIME
Input: int level : What level of debugging to use, currently there
are only two levels, 0 = none, > 0 = debug info
Output:
Errors:
------------------------------------------------------------------------*/
int MDECODE_set_debug( int level )
{
glb.debug = level;
return glb.debug;
}
int MDECODE_set_verbose( int level )
{
glb.verbose = level;
return glb.verbose;
}
int MDECODE_set_decode_qp( int level )
{
glb.decode_qp = level;
return glb.decode_qp;
}
int MDECODE_set_decode_b64( int level )
{
glb.decode_b64 = level;
return glb.decode_b64;
}
/*------------------------------------------------------------------------
Procedure: MDECODE_decode_short64 ID:1
Purpose: Decodes a BASE64 encoded realm
Input: char *realm : base64 encoded NUL terminated string
Output: decoded data is written to the short64 char
Errors:
------------------------------------------------------------------------*/
int MDECODE_decode_short64( char *short64 )
{
int i;
int realm_size = strlen( short64 );
int stopcount = 0; /* How many stop (=) characters we've read in */
int c; /* a single char as retrieved using MDECODE_get_char() */
int char_count = 0; /* How many chars have been received */
char output[3]; /* The 4->3 byte output array */
char input[4]; /* The 4->3 byte input array */
char *outstring = short64;
char_count = 0;
while (char_count < realm_size)
{
/* Initialise the decode buffer */
input[0] = input[1] = input[2] = input[3] = 0;
/* snatch 4 characters from the input */
for (i = 0; i < 4; i++) {
/* get a char from the filestream */
c = *short64;
short64++;
/* assuming we've gotten this far, then we increment the char_count */
char_count++;
/* if we detect the "stopchar" then we better increment the STOP counter */
if (c == '=') {
stopcount++;
}
/* test for and discard invalid chars */
if (b64[c] == 0x80) {
i--;
continue;
}
/* do the conversion from encoded -> decoded */
input[i] = (char)b64[c];
} /* for */
/* now that our 4-char buffer is full, we can do some fancy bit-shifting and get the required 3-chars of 8-bit data */
output[0] = (input[0] << 2) | (input[1] >> 4);
output[1] = (input[1] << 4) | (input[2] >> 2);
output[2] = (input[2] << 6) | input[3];
/* determine how many chars to write write and check for errors if our input char count was 4 then we did receive a propper 4:3 Base64 block, hence write it */
if (i == 4) {
for (i = 0; i < (3 -stopcount); i++){
*outstring = output[i];
outstring++;
} /* copy our data across */
} /* if 4 chars were inputted */
} /* while more chars to proccess */
*outstring = '\0'; // Set the last char to NULL
return 0;
}
/*------------------------------------------------------------------------
Procedure: MDECODE_decode_quoted_printable ID:1
Purpose: Decodes quoted printable encoded data.
Input: char *line : \0 terminated string possibly containing quoted printable data
int qpmode : Selects which decoding ruleset to use ( refer to RFC2047 )
Output: Decoded string is superimposed over the provided line parameter
Returns: Returns the number of bytes decoded.
------------------------------------------------------------------------*/
int MDECODE_decode_quoted_printable( char *line, int qpmode, char esc_char )
{
char c; /* The Character to output */
int op, ip; /* OutputPointer and InputPointer */
int slen = strlen(line); /* Length of our line */
DMD LOGGER_log("%s:%d:MDECODE_decode_quoted_printable:DEBUG: input string = '%s' Input length = %d\n",FL, line, slen);
/* Initialise our "pointers" to the start of the encoded string */
ip=op=0;
/* for every character in the string... */
for (ip = 0; ip < slen; ip++)
{
c = line[ip];
/* if we have the quoted-printable esc char, then lets get cracking */
if (c == esc_char)
{
/* if we have another two chars... */
if ((ip +1) < slen )
{
int original_ip = ip;
/* Is our next char a \n\r ?
if it is, then we have to eliminate any further \r\n's etc
so as to turn the =\n\r into a 'soft return', which basically
means that we ignore it. Soft-breaks are used so we can
fit our long lines into the requirement of a maximum of 76 characters
per line.
So we move the input-pointer along skipping each character without
incrementing the output pointer.
*/
/** Absorb any trailing whitespaces **/
if (1)
{
char *w = &(line[ip +1]);
while ((*w == '\t') || (*w == ' ')) {w++;ip++;}
}
/** Do we now have a line break ? **/
if (( line[ip +1] == '\n') || (line[ip +1] == '\r' ))
{
ip++;
if ((ip+1 < slen)&&(( line[ip +1] == '\n') || (line[ip +1] == '\r' )))
{
ip++;
}
continue;
}
else
{
/*
if the characters following the '=' symbol are not
of the \n or \r pair, then we will [currently]
assume that the next two characters are in fact the
hexadecimal encodings of the character we do want
*/
/** Revert to original position **/
ip = original_ip;
/* convert our encoded character from HEX -> decimal */
if ( ip < slen-1 ) // was 2, proving - if there are 3 chars in string, =AB, slen = 3, ip = 1
{
/**
* Jan 17th 2007 jjohnston
* Fixed a bug that decoded invalid QP sequences
*/
if(hexconv[(int)line[ip+1]] == 20 || hexconv[(int)line[ip+2]] == 20) {
//LOGGER_log("%s:%d:MIME_decode_quoted_printable:NOTICE: Invalid characters for quoted-printable at '=%c%c'\n", FL, (int)&line[ip+1], (int)&line[ip+2]);
} else {
c = (char)hexconv[(int)line[ip+1]]*16 +hexconv[(int)line[ip+2]];
/* shuffle the pointer up two spaces */
ip+=2;
}
}
else {
LOGGER_log("%s:%d:MIME_decode_quoted_printable:WARNING: Ran out of characters when decoding end of '%s'\n", FL, &line[ip] );
}
}
} /* if there were two extra chars after the ='s */
/* if we didn't have enough characters, then we'll make the char the
* string terminator (such as what happens when we get a =\n
*/
else
{
/* 2002-12-16:18H31: changed from 'line[ip]' to 'line[op]' */
line[op] = '\0';
/* 2002-12-16:18H32: added break statement - if we're out of chars, then we quit the for loop */
break;
} /* else */
} /* if c was a encoding char */
else
if (( c == '_' ) && ( qpmode == MDECODE_QPMODE_ISO ))
{
// RFC2047 (Section 4.2.(2)(3)) says that if we encounter a '_' character in our ISO encodings then
// we must convert that to a space ( as we are not allowed to have spaces in any
c = ' ';
}
/* put in the new character, be it converted or not */
line[op] = c;
/* shuffle up the output line pointer */
op++;
} /* for loop */
/* terminate the line */
line[op]='\0';
DMD LOGGER_log("%s:%d:MDECODE_decode_quoted_printable:DEBUG: Output = '%s' Output length = %d\n", FL, line, strlen(line));
// 2003-01-26:PLD: Changed from (op -1) -=> op
return op;
}
/*------------------------------------------------------------------------
Procedure: MDECODE_decode_text_line ID:1
Purpose: Decodes a line of text, checking for Quoted-Printable characters
and converting them. Note - if the character converted is a \0
(after decoding) it shouldn't affect the calling parent because the
calling parent should read back the returned string byte size and
use fwrite() or other non-\0 affected writing/processing functions
Input: char *line: pointer to the buffer/line we wish to convert/scan
Output: int: size of final buffer in bytes.
Errors:
------------------------------------------------------------------------*/
int MDECODE_decode_qp_text( char *line )
{
if (glb.decode_qp == 0) return strlen(line);
return MDECODE_decode_quoted_printable( line, MDECODE_QPMODE_STD, '=' );
}
int MDECODE_decode_qp_ISO( char *line )
{
// return MDECODE_decode_quoted_printable( line, MDECODE_QPMODE_ISO, '=' );
return MDECODE_decode_quoted_printable( line, MDECODE_QPMODE_STD, '=' );
}
int MDECODE_decode_multipart( char *line )
{
return MDECODE_decode_quoted_printable( line, MDECODE_QPMODE_STD, '%' );
}
/*------------------------------------------------------------------------
Procedure: MDECODE_decode_ISO ID:1
Purpose: Decodes an ISO ( RFC2047 ) encoded string into native codepage dependent output
Input: char *isostring : String containing =?code-page?encoding-type?string?= format
int length : length of the string we're decoding
Output: isostring is overwritten with the decoded string.
Errors:
------------------------------------------------------------------------*/
int MDECODE_decode_ISO( char *isostring, int size )
{
char *start_pair, *end_pair;
char *iso, *iso_copy;
char encoding_type='-';
char encoding_charset[ MDECODE_ISO_CHARSET_SIZE_MAX ];
char *iso_start, *iso_end;
int iso_decoded;
DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: ISO-string='%s'",FL,isostring);
// Process of decoding the ISO encoded string sequence.
// ( this process is repeated until we run out of ISO sequences )
//
// 1. Check that the string has a =? sequence within it ( indicates the start of the ISO encoding
//
// 2. tokenise the sequence succeeding the =? token into its three (3) parts, namely the code-page, encoding-type and string respectively
//
// 3. decode the string based on the encoding type, Q = Quoted-Printable, B = BASE64
//
iso_end = iso_start = NULL;
start_pair = end_pair = NULL;
iso_copy = malloc( sizeof(char) *( size +1 ) );
do {
iso_decoded = 0;
start_pair = strstr( isostring, "=?" );
// if ( start_pair ) end_pair = strstr( start_pair +2, "?=" );
if (( start_pair != NULL ))
{
iso_start = start_pair;
// There's probably a better way of doing this, but, for us to find the end of this
// particular 'ISO' sequence, we need to hop past 3 more ?'s ( assuming we've already
// found the first one.
DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: ISO start = %s",FL,iso_start);
iso_end = strchr( iso_start +strlen("=?"), '?' ); // Jump past the encoding
if (iso_end) iso_end = strchr( iso_end +1, '?' ); // Jump past the Q or B
if (iso_end) iso_end = strpbrk( iso_end +1, "?\n\r\t;" ); // dropped the SPACE here.
if ((iso_end != NULL)&&(*iso_end == '?')) iso_end+=2;
if ( (iso_start) && (iso_end) )
{
char *token_end;
char restore_char='\0';
// Copy the Encoding page/code.
iso = iso_start +strlen("=?");
token_end = strchr(iso,'?');
if (token_end) *token_end = '\0';
snprintf( encoding_charset, sizeof( encoding_charset ), "%s", iso);
DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: ISO char set = '%s'",FL,encoding_charset);
iso = token_end +1;
// Get the encoding _type_ (BASE64/QuotedPrintable etc)
token_end = strchr(iso,'?');
encoding_type = *iso;
iso = token_end +1;
DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: ISO encoding char = '%c'",FL,encoding_type);
// Get the encoded string
token_end = strpbrk(iso,"?;\n\r\t"); //DROPPED THE SPACE here
if (token_end != NULL)
{
if ((*token_end != '?')&&(*token_end != ';'))
{
restore_char = *token_end;
}
*token_end = '\0';
}
if (iso)
{
DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: Encoded String = '%s'\n", FL, iso );
switch ( encoding_type ) {
case MDECODE_ISO_ENCODING_Q:
case MDECODE_ISO_ENCODING_q:
DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: Decoding filename using Quoted-Printable (%s)\n", FL, iso);
MDECODE_decode_qp_ISO(iso);
iso_decoded = 1;
break;
case MDECODE_ISO_ENCODING_B:
case MDECODE_ISO_ENCODING_b:
DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: Decoding filename using BASE64 (%s)\n", FL, iso);
MDECODE_decode_short64( iso );
iso_decoded = 1;
break;
default:
if (glb.verbose) LOGGER_log("%s:%d:MDECODE_decode_ISO:ERROR: The encoding character '%c' is not a valid type of encoding\n", FL, encoding_type );
}
// If we decoded the string okay, then we need to recompose the string
if ( iso_decoded == 1 )
{
char *new_end_pos;
DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: Decoded String = '%s'\n", FL, iso );
*iso_start = '\0'; // Terminate the original string before the start of the ISO data
// Because sometimes ISO strings are broken over multiple lines
// due to wrapping requirements of RFC(2)822, we need to
// sniff out these tab or spaces and crop them out of our
// final ISO string. We cannot simply search for the next
// =? sequence using strstr() because it might traverse
// beyond the end of the current 'line' (ie, \r\n termination)
if (token_end)
{
iso_end = token_end +1;
DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: iso_end = '%20s'",FL, iso_end);
while ((*iso_end == '?')||(*iso_end == '=')) iso_end++;
DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: iso_end = '%20s'",FL, iso_end);
new_end_pos = iso_end;
while ((*new_end_pos == ' ')||(*new_end_pos == '\t')) new_end_pos++;
if (strncmp(new_end_pos,"=?",2)==0) iso_end = new_end_pos;
} else {
iso_end = NULL;
}
DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: ISO-END = '%20s'",FL,iso_end);
/** We now have the string split into 3 peices,
** isostring = pointing to the start
** iso = newly decoded string
** iso_end = start of string after the non-decoded ISO portion
**/
/** Generate new string using the decoded ISO to a temporary string **/
if (restore_char != '\0')
{
DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: Recomposing string with restore-char of '%c'",FL,restore_char);
DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: ISO-end (start of end of string) is \n%s",FL,iso_end);
snprintf( iso_copy, size, "%s%s%c%s", isostring, iso, restore_char, (iso_end?iso_end:"") );
} else {
DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: Recomposing string with NO restore-char",FL,restore_char);
snprintf( iso_copy, size, "%s%s%s", isostring, iso, (iso_end?iso_end:"") );
}
/** Switch the new headers over to the original headers again **/
snprintf( isostring, size, "%s", iso_copy );
DMD LOGGER_log("%s:%d:MDECODE_decode_ISO:DEBUG: New ISO string = \n%s",FL,isostring);
}
}
}
} // if (iso_start)
}
while (iso_decoded == 1 );
if (iso_copy) free(iso_copy);
return 0;
}
//------------END libmime-decoders.c