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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
use libc;
use libc::toupper;

use crate::charconv::*;
use crate::mailimf::*;
use crate::mailmime_content::*;
use crate::mailmime_types::*;
use crate::mmapstring::*;
use crate::other::*;

pub const MAIL_CHARCONV_ERROR_CONV: libc::c_uint = 3;
pub const MAIL_CHARCONV_ERROR_UNKNOWN_CHARSET: libc::c_uint = 1;
pub const MAIL_CHARCONV_ERROR_MEMORY: libc::c_uint = 2;
pub const TYPE_WORD: libc::c_uint = 1;
pub const TYPE_ENCODED_WORD: libc::c_uint = 2;
pub const MAILMIME_ENCODING_Q: libc::c_uint = 1;
pub const MAILMIME_ENCODING_B: libc::c_uint = 0;
pub const TYPE_ERROR: libc::c_uint = 0;

pub unsafe fn mailmime_encoded_phrase_parse(
    mut default_fromcode: *const libc::c_char,
    mut message: *const libc::c_char,
    mut length: size_t,
    mut indx: *mut size_t,
    mut tocode: *const libc::c_char,
    mut result: *mut *mut libc::c_char,
) -> libc::c_int {
    let mut current_block: u64;
    let mut gphrase: *mut MMAPString = 0 as *mut MMAPString;
    let mut word: *mut mailmime_encoded_word = 0 as *mut mailmime_encoded_word;
    let mut first: libc::c_int = 0;
    let mut cur_token: size_t = 0;
    let mut r: libc::c_int = 0;
    let mut res: libc::c_int = 0;
    let mut str: *mut libc::c_char = 0 as *mut libc::c_char;
    let mut wordutf8: *mut libc::c_char = 0 as *mut libc::c_char;
    let mut type_0: libc::c_int = 0;
    let mut missing_closing_quote: libc::c_int = 0;
    cur_token = *indx;
    gphrase = mmap_string_new(b"\x00" as *const u8 as *const libc::c_char);
    if gphrase.is_null() {
        res = MAILIMF_ERROR_MEMORY as libc::c_int
    } else {
        first = 1i32;
        type_0 = TYPE_ERROR as libc::c_int;
        loop {
            let mut has_fwd: libc::c_int = 0;
            word = 0 as *mut mailmime_encoded_word;
            r = mailmime_encoded_word_parse(
                message,
                length,
                &mut cur_token,
                &mut word,
                &mut has_fwd,
                &mut missing_closing_quote,
            );
            if r == MAILIMF_NO_ERROR as libc::c_int {
                if 0 == first && 0 != has_fwd {
                    if type_0 != TYPE_ENCODED_WORD as libc::c_int {
                        if mmap_string_append_c(gphrase, ' ' as i32 as libc::c_char).is_null() {
                            mailmime_encoded_word_free(word);
                            res = MAILIMF_ERROR_MEMORY as libc::c_int;
                            current_block = 13246848547199022064;
                            break;
                        }
                    }
                }
                type_0 = TYPE_ENCODED_WORD as libc::c_int;
                wordutf8 = 0 as *mut libc::c_char;
                r = charconv(
                    tocode,
                    (*word).wd_charset,
                    (*word).wd_text,
                    strlen((*word).wd_text),
                    &mut wordutf8,
                );
                match r {
                    2 => {
                        mailmime_encoded_word_free(word);
                        res = MAILIMF_ERROR_MEMORY as libc::c_int;
                        current_block = 13246848547199022064;
                        break;
                    }
                    1 => {
                        r = charconv(
                            tocode,
                            b"iso-8859-1\x00" as *const u8 as *const libc::c_char,
                            (*word).wd_text,
                            strlen((*word).wd_text),
                            &mut wordutf8,
                        )
                    }
                    3 => {
                        mailmime_encoded_word_free(word);
                        res = MAILIMF_ERROR_PARSE as libc::c_int;
                        current_block = 13246848547199022064;
                        break;
                    }
                    _ => {}
                }
                match r {
                    2 => {
                        mailmime_encoded_word_free(word);
                        res = MAILIMF_ERROR_MEMORY as libc::c_int;
                        current_block = 13246848547199022064;
                        break;
                    }
                    3 => {
                        mailmime_encoded_word_free(word);
                        res = MAILIMF_ERROR_PARSE as libc::c_int;
                        current_block = 13246848547199022064;
                        break;
                    }
                    _ => {
                        if !wordutf8.is_null() {
                            if mmap_string_append(gphrase, wordutf8).is_null() {
                                mailmime_encoded_word_free(word);
                                free(wordutf8 as *mut libc::c_void);
                                res = MAILIMF_ERROR_MEMORY as libc::c_int;
                                current_block = 13246848547199022064;
                                break;
                            } else {
                                free(wordutf8 as *mut libc::c_void);
                            }
                        }
                        mailmime_encoded_word_free(word);
                        first = 0i32
                    }
                }
            } else if !(r == MAILIMF_ERROR_PARSE as libc::c_int) {
                /* do nothing */
                res = r;
                current_block = 13246848547199022064;
                break;
            }
            if !(r == MAILIMF_ERROR_PARSE as libc::c_int) {
                continue;
            }
            let mut raw_word: *mut libc::c_char = 0 as *mut libc::c_char;
            raw_word = 0 as *mut libc::c_char;
            r = mailmime_non_encoded_word_parse(
                message,
                length,
                &mut cur_token,
                &mut raw_word,
                &mut has_fwd,
            );
            if r == MAILIMF_NO_ERROR as libc::c_int {
                if 0 == first && 0 != has_fwd {
                    if mmap_string_append_c(gphrase, ' ' as i32 as libc::c_char).is_null() {
                        free(raw_word as *mut libc::c_void);
                        res = MAILIMF_ERROR_MEMORY as libc::c_int;
                        current_block = 13246848547199022064;
                        break;
                    }
                }
                type_0 = TYPE_WORD as libc::c_int;
                wordutf8 = 0 as *mut libc::c_char;
                r = charconv(
                    tocode,
                    default_fromcode,
                    raw_word,
                    strlen(raw_word),
                    &mut wordutf8,
                );
                match r {
                    2 => {
                        free(raw_word as *mut libc::c_void);
                        res = MAILIMF_ERROR_MEMORY as libc::c_int;
                        current_block = 13246848547199022064;
                        break;
                    }
                    1 | 3 => {
                        free(raw_word as *mut libc::c_void);
                        res = MAILIMF_ERROR_PARSE as libc::c_int;
                        current_block = 13246848547199022064;
                        break;
                    }
                    _ => {
                        if mmap_string_append(gphrase, wordutf8).is_null() {
                            free(wordutf8 as *mut libc::c_void);
                            free(raw_word as *mut libc::c_void);
                            res = MAILIMF_ERROR_MEMORY as libc::c_int;
                            current_block = 13246848547199022064;
                            break;
                        } else {
                            free(wordutf8 as *mut libc::c_void);
                            free(raw_word as *mut libc::c_void);
                            first = 0i32
                        }
                    }
                }
            } else if r == MAILIMF_ERROR_PARSE as libc::c_int {
                r = mailimf_fws_parse(message, length, &mut cur_token);
                if r != MAILIMF_NO_ERROR as libc::c_int {
                    current_block = 5005389895767293342;
                    break;
                }
                if mmap_string_append_c(gphrase, ' ' as i32 as libc::c_char).is_null() {
                    res = MAILIMF_ERROR_MEMORY as libc::c_int;
                    current_block = 13246848547199022064;
                    break;
                } else {
                    first = 0i32;
                    current_block = 5005389895767293342;
                    break;
                }
            } else {
                res = r;
                current_block = 13246848547199022064;
                break;
            }
        }
        match current_block {
            5005389895767293342 => {
                if 0 != first {
                    if cur_token != length {
                        res = MAILIMF_ERROR_PARSE as libc::c_int;
                        current_block = 13246848547199022064;
                    } else {
                        current_block = 7072655752890836508;
                    }
                } else {
                    current_block = 7072655752890836508;
                }
                match current_block {
                    13246848547199022064 => {}
                    _ => {
                        str = strdup((*gphrase).str_0);
                        if str.is_null() {
                            res = MAILIMF_ERROR_MEMORY as libc::c_int
                        } else {
                            mmap_string_free(gphrase);
                            *result = str;
                            *indx = cur_token;
                            return MAILIMF_NO_ERROR as libc::c_int;
                        }
                    }
                }
            }
            _ => {}
        }
        mmap_string_free(gphrase);
    }
    return res;
}
unsafe fn mailmime_non_encoded_word_parse(
    mut message: *const libc::c_char,
    mut length: size_t,
    mut indx: *mut size_t,
    mut result: *mut *mut libc::c_char,
    mut p_has_fwd: *mut libc::c_int,
) -> libc::c_int {
    let mut end: libc::c_int = 0;
    let mut cur_token: size_t = 0;
    let mut res: libc::c_int = 0;
    let mut text: *mut libc::c_char = 0 as *mut libc::c_char;
    let mut r: libc::c_int = 0;
    let mut begin: size_t = 0;
    let mut state: libc::c_int = 0;
    let mut has_fwd: libc::c_int = 0;
    cur_token = *indx;
    has_fwd = 0i32;
    r = mailimf_fws_parse(message, length, &mut cur_token);
    if r == MAILIMF_NO_ERROR as libc::c_int {
        has_fwd = 1i32
    }
    if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int {
        res = r
    } else {
        begin = cur_token;
        state = 0i32;
        end = 0i32;
        while !(cur_token >= length) {
            let mut current_block_17: u64;
            match *message.offset(cur_token as isize) as libc::c_int {
                32 | 9 | 13 | 10 => {
                    state = 0i32;
                    end = 1i32;
                    current_block_17 = 16924917904204750491;
                }
                61 => {
                    state = 1i32;
                    current_block_17 = 16924917904204750491;
                }
                63 => {
                    if state == 1i32 {
                        cur_token = cur_token.wrapping_sub(1);
                        end = 1i32
                    }
                    current_block_17 = 10192508258555769664;
                }
                _ => {
                    current_block_17 = 10192508258555769664;
                }
            }
            match current_block_17 {
                10192508258555769664 => state = 0i32,
                _ => {}
            }
            if 0 != end {
                break;
            }
            cur_token = cur_token.wrapping_add(1)
        }
        if cur_token.wrapping_sub(begin) == 0i32 as libc::size_t {
            res = MAILIMF_ERROR_PARSE as libc::c_int
        } else {
            text = malloc(
                cur_token
                    .wrapping_sub(begin)
                    .wrapping_add(1i32 as libc::size_t),
            ) as *mut libc::c_char;
            if text.is_null() {
                res = MAILIMF_ERROR_MEMORY as libc::c_int
            } else {
                memcpy(
                    text as *mut libc::c_void,
                    message.offset(begin as isize) as *const libc::c_void,
                    cur_token.wrapping_sub(begin),
                );
                *text.offset(cur_token.wrapping_sub(begin) as isize) =
                    '\u{0}' as i32 as libc::c_char;
                *indx = cur_token;
                *result = text;
                *p_has_fwd = has_fwd;
                return MAILIMF_NO_ERROR as libc::c_int;
            }
        }
    }
    return res;
}

pub unsafe fn mailmime_encoded_word_parse(
    mut message: *const libc::c_char,
    mut length: size_t,
    mut indx: *mut size_t,
    mut result: *mut *mut mailmime_encoded_word,
    mut p_has_fwd: *mut libc::c_int,
    mut p_missing_closing_quote: *mut libc::c_int,
) -> libc::c_int {
    let mut current_block: u64;
    /*
    Parse the following, when a unicode character encoding is split.
    =?UTF-8?B?4Lij4Liw4LmA4Lia4Li04LiU4LiE4Lin4Liy4Lih4Lih4Lix4LiZ4Liq4LmM?=
    =?UTF-8?B?4LmA4LiV4LmH4Lih4Lie4Li04LiB4Lix4LiUIFRSQU5TRk9STUVSUyA0IOC4?=
    =?UTF-8?B?oeC4seC4meC4quC5jOC4hOC4o+C4muC4l+C4uOC4geC4o+C4sOC4muC4miDg?=
    =?UTF-8?B?uJfguLXguYjguYDguJTguLXguKLguKfguYPguJnguYDguKHguLfguK3guIfg?=
    =?UTF-8?B?uYTguJfguKI=?=
    Expected result:
    ระเบิดความมันส์เต็มพิกัด TRANSFORMERS 4 มันส์ครบทุกระบบ ที่เดียวในเมืองไทย
    libetpan result:
    ระเบิดความมันส์เต็มพิกัด TRANSFORMERS 4 ?ันส์ครบทุกระบบ ??ี่เดียวในเมือง??ทย

    See https://github.com/dinhviethoa/libetpan/pull/211
    */
    let mut cur_token: size_t = 0;
    let mut charset: *mut libc::c_char = 0 as *mut libc::c_char;
    let mut encoding: libc::c_int = 0;
    let mut body: *mut libc::c_char = 0 as *mut libc::c_char;
    let mut old_body_len: size_t = 0;
    let mut text: *mut libc::c_char = 0 as *mut libc::c_char;
    let mut end_encoding: size_t = 0;
    let mut lookfwd_cur_token: size_t = 0;
    let mut lookfwd_charset: *mut libc::c_char = 0 as *mut libc::c_char;
    let mut lookfwd_encoding: libc::c_int = 0;
    let mut copy_len: size_t = 0;
    let mut decoded_token: size_t = 0;
    let mut decoded: *mut libc::c_char = 0 as *mut libc::c_char;
    let mut decoded_len: size_t = 0;
    let mut ew: *mut mailmime_encoded_word = 0 as *mut mailmime_encoded_word;
    let mut r: libc::c_int = 0;
    let mut res: libc::c_int = 0;
    let mut opening_quote: libc::c_int = 0;
    let mut end: libc::c_int = 0;
    let mut has_fwd: libc::c_int = 0;
    let mut missing_closing_quote: libc::c_int = 0;
    cur_token = *indx;
    text = 0 as *mut libc::c_char;
    lookfwd_charset = 0 as *mut libc::c_char;
    missing_closing_quote = 0i32;
    has_fwd = 0i32;
    r = mailimf_fws_parse(message, length, &mut cur_token);
    if r == MAILIMF_NO_ERROR as libc::c_int {
        has_fwd = 1i32
    }
    if r != MAILIMF_NO_ERROR as libc::c_int && r != MAILIMF_ERROR_PARSE as libc::c_int {
        res = r
    } else {
        opening_quote = 0i32;
        r = mailimf_char_parse(message, length, &mut cur_token, '\"' as i32 as libc::c_char);
        if r == MAILIMF_NO_ERROR as libc::c_int {
            opening_quote = 1i32;
            current_block = 17788412896529399552;
        } else if r == MAILIMF_ERROR_PARSE as libc::c_int {
            current_block = 17788412896529399552;
        } else {
            /* do nothing */
            res = r;
            current_block = 7995813543095296079;
        }
        match current_block {
            7995813543095296079 => {}
            _ => {
                r = mailimf_token_case_insensitive_len_parse(
                    message,
                    length,
                    &mut cur_token,
                    b"=?\x00" as *const u8 as *const libc::c_char as *mut libc::c_char,
                    strlen(b"=?\x00" as *const u8 as *const libc::c_char),
                );
                if r != MAILIMF_NO_ERROR as libc::c_int {
                    res = r
                } else {
                    r = mailmime_charset_parse(message, length, &mut cur_token, &mut charset);
                    if r != MAILIMF_NO_ERROR as libc::c_int {
                        res = r
                    } else {
                        r = mailimf_char_parse(
                            message,
                            length,
                            &mut cur_token,
                            '?' as i32 as libc::c_char,
                        );
                        if r != MAILIMF_NO_ERROR as libc::c_int {
                            res = r
                        } else {
                            r = mailmime_encoding_parse(
                                message,
                                length,
                                &mut cur_token,
                                &mut encoding,
                            );
                            if r != MAILIMF_NO_ERROR as libc::c_int {
                                res = r
                            } else {
                                r = mailimf_char_parse(
                                    message,
                                    length,
                                    &mut cur_token,
                                    '?' as i32 as libc::c_char,
                                );
                                if r != MAILIMF_NO_ERROR as libc::c_int {
                                    res = r
                                } else {
                                    lookfwd_cur_token = cur_token;
                                    body = 0 as *mut libc::c_char;
                                    old_body_len = 0i32 as size_t;
                                    loop {
                                        let mut has_base64_padding: libc::c_int = 0;
                                        end = 0i32;
                                        has_base64_padding = 0i32;
                                        end_encoding = cur_token;
                                        while !(end_encoding >= length) {
                                            if end_encoding.wrapping_add(1i32 as libc::size_t)
                                                < length
                                            {
                                                if *message.offset(end_encoding as isize)
                                                    as libc::c_int
                                                    == '?' as i32
                                                    && *message.offset(
                                                        end_encoding
                                                            .wrapping_add(1i32 as libc::size_t)
                                                            as isize,
                                                    )
                                                        as libc::c_int
                                                        == '=' as i32
                                                {
                                                    end = 1i32
                                                }
                                            }
                                            if 0 != end {
                                                break;
                                            }
                                            end_encoding = end_encoding.wrapping_add(1)
                                        }
                                        copy_len = end_encoding.wrapping_sub(lookfwd_cur_token);
                                        if copy_len > 0i32 as libc::size_t {
                                            if encoding == MAILMIME_ENCODING_B as libc::c_int {
                                                if end_encoding >= 1i32 as libc::size_t {
                                                    if *message.offset(
                                                        end_encoding
                                                            .wrapping_sub(1i32 as libc::size_t)
                                                            as isize,
                                                    )
                                                        as libc::c_int
                                                        == '=' as i32
                                                    {
                                                        has_base64_padding = 1i32
                                                    }
                                                }
                                            }
                                            body = realloc(
                                                body as *mut libc::c_void,
                                                old_body_len
                                                    .wrapping_add(copy_len)
                                                    .wrapping_add(1i32 as libc::size_t),
                                            )
                                                as *mut libc::c_char;
                                            if body.is_null() {
                                                res = MAILIMF_ERROR_MEMORY as libc::c_int;
                                                current_block = 13900684162107791171;
                                                break;
                                            } else {
                                                memcpy(
                                                    body.offset(old_body_len as isize)
                                                        as *mut libc::c_void,
                                                    &*message.offset(cur_token as isize)
                                                        as *const libc::c_char
                                                        as *const libc::c_void,
                                                    copy_len,
                                                );
                                                *body
                                                    .offset(old_body_len.wrapping_add(copy_len)
                                                        as isize) = '\u{0}' as i32 as libc::c_char;
                                                old_body_len = (old_body_len as libc::size_t)
                                                    .wrapping_add(copy_len)
                                                    as size_t
                                                    as size_t
                                            }
                                        }
                                        cur_token = end_encoding;
                                        r = mailimf_token_case_insensitive_len_parse(
                                            message,
                                            length,
                                            &mut cur_token,
                                            b"?=\x00" as *const u8 as *const libc::c_char
                                                as *mut libc::c_char,
                                            strlen(b"?=\x00" as *const u8 as *const libc::c_char),
                                        );
                                        if r != MAILIMF_NO_ERROR as libc::c_int {
                                            current_block = 2652804691515851435;
                                            break;
                                        }
                                        if 0 != has_base64_padding {
                                            current_block = 2652804691515851435;
                                            break;
                                        }
                                        lookfwd_cur_token = cur_token;
                                        r = mailimf_fws_parse(
                                            message,
                                            length,
                                            &mut lookfwd_cur_token,
                                        );
                                        if r != MAILIMF_NO_ERROR as libc::c_int
                                            && r != MAILIMF_ERROR_PARSE as libc::c_int
                                        {
                                            current_block = 2652804691515851435;
                                            break;
                                        }
                                        r = mailimf_token_case_insensitive_len_parse(
                                            message,
                                            length,
                                            &mut lookfwd_cur_token,
                                            b"=?\x00" as *const u8 as *const libc::c_char
                                                as *mut libc::c_char,
                                            strlen(b"=?\x00" as *const u8 as *const libc::c_char),
                                        );
                                        if r != MAILIMF_NO_ERROR as libc::c_int {
                                            current_block = 2652804691515851435;
                                            break;
                                        }
                                        r = mailmime_charset_parse(
                                            message,
                                            length,
                                            &mut lookfwd_cur_token,
                                            &mut lookfwd_charset,
                                        );
                                        if r != MAILIMF_NO_ERROR as libc::c_int {
                                            current_block = 2652804691515851435;
                                            break;
                                        }
                                        r = mailimf_char_parse(
                                            message,
                                            length,
                                            &mut lookfwd_cur_token,
                                            '?' as i32 as libc::c_char,
                                        );
                                        if r != MAILIMF_NO_ERROR as libc::c_int {
                                            current_block = 2652804691515851435;
                                            break;
                                        }
                                        r = mailmime_encoding_parse(
                                            message,
                                            length,
                                            &mut lookfwd_cur_token,
                                            &mut lookfwd_encoding,
                                        );
                                        if r != MAILIMF_NO_ERROR as libc::c_int {
                                            current_block = 2652804691515851435;
                                            break;
                                        }
                                        r = mailimf_char_parse(
                                            message,
                                            length,
                                            &mut lookfwd_cur_token,
                                            '?' as i32 as libc::c_char,
                                        );
                                        if r != MAILIMF_NO_ERROR as libc::c_int {
                                            current_block = 2652804691515851435;
                                            break;
                                        }
                                        if strcasecmp(charset, lookfwd_charset) == 0i32
                                            && encoding == lookfwd_encoding
                                        {
                                            cur_token = lookfwd_cur_token;
                                            mailmime_charset_free(lookfwd_charset);
                                            lookfwd_charset = 0 as *mut libc::c_char
                                        } else {
                                            /* the next charset is not matched with the current one,
                                            therefore exit the loop to decode the body appended so far */
                                            current_block = 2652804691515851435;
                                            break;
                                        }
                                    }
                                    match current_block {
                                        2652804691515851435 => {
                                            if !lookfwd_charset.is_null() {
                                                mailmime_charset_free(lookfwd_charset);
                                                lookfwd_charset = 0 as *mut libc::c_char
                                            }
                                            if body.is_null() {
                                                body = strdup(
                                                    b"\x00" as *const u8 as *const libc::c_char,
                                                );
                                                if body.is_null() {
                                                    res = MAILIMF_ERROR_MEMORY as libc::c_int;
                                                    current_block = 13900684162107791171;
                                                } else {
                                                    current_block = 16778110326724371720;
                                                }
                                            } else {
                                                current_block = 16778110326724371720;
                                            }
                                            match current_block {
                                                13900684162107791171 => {}
                                                _ => {
                                                    decoded_token = 0i32 as size_t;
                                                    decoded_len = 0i32 as size_t;
                                                    decoded = 0 as *mut libc::c_char;
                                                    match encoding {
                                                        0 => {
                                                            r = mailmime_base64_body_parse(
                                                                body,
                                                                strlen(body),
                                                                &mut decoded_token,
                                                                &mut decoded,
                                                                &mut decoded_len,
                                                            );
                                                            if r != MAILIMF_NO_ERROR as libc::c_int
                                                            {
                                                                res = r;
                                                                current_block =
                                                                    13900684162107791171;
                                                            } else {
                                                                current_block = 7337917895049117968;
                                                            }
                                                        }
                                                        1 => {
                                                            r =
                                                                mailmime_quoted_printable_body_parse(body,
                                                                                                     strlen(body),
                                                                                                     &mut decoded_token,
                                                                                                     &mut decoded,
                                                                                                     &mut decoded_len,
                                                                                                     1i32);
                                                            if r != MAILIMF_NO_ERROR as libc::c_int
                                                            {
                                                                res = r;
                                                                current_block =
                                                                    13900684162107791171;
                                                            } else {
                                                                current_block = 7337917895049117968;
                                                            }
                                                        }
                                                        _ => {
                                                            current_block = 7337917895049117968;
                                                        }
                                                    }
                                                    match current_block {
                                                        13900684162107791171 => {}
                                                        _ => {
                                                            text =
                                                                malloc(decoded_len.wrapping_add(
                                                                    1i32 as libc::size_t,
                                                                ))
                                                                    as *mut libc::c_char;
                                                            if text.is_null() {
                                                                res = MAILIMF_ERROR_MEMORY
                                                                    as libc::c_int
                                                            } else {
                                                                if decoded_len
                                                                    > 0i32 as libc::size_t
                                                                {
                                                                    memcpy(
                                                                        text as *mut libc::c_void,
                                                                        decoded
                                                                            as *const libc::c_void,
                                                                        decoded_len,
                                                                    );
                                                                }
                                                                *text
                                                                    .offset(decoded_len as isize) =
                                                                    '\u{0}' as i32 as libc::c_char;
                                                                if 0 != opening_quote {
                                                                    r = mailimf_char_parse(
                                                                        message,
                                                                        length,
                                                                        &mut cur_token,
                                                                        '\"' as i32 as libc::c_char,
                                                                    );
                                                                    if r == MAILIMF_ERROR_PARSE
                                                                        as libc::c_int
                                                                    {
                                                                        missing_closing_quote = 1i32
                                                                    }
                                                                }
                                                                if strcasecmp(
                                                                    charset,
                                                                    b"utf8\x00" as *const u8
                                                                        as *const libc::c_char,
                                                                ) == 0i32
                                                                {
                                                                    free(
                                                                        charset
                                                                            as *mut libc::c_void,
                                                                    );
                                                                    charset = strdup(
                                                                        b"utf-8\x00" as *const u8
                                                                            as *const libc::c_char,
                                                                    )
                                                                }
                                                                ew = mailmime_encoded_word_new(
                                                                    charset, text,
                                                                );
                                                                if ew.is_null() {
                                                                    res = MAILIMF_ERROR_MEMORY
                                                                        as libc::c_int
                                                                } else {
                                                                    *result = ew;
                                                                    *indx = cur_token;
                                                                    *p_has_fwd = has_fwd;
                                                                    *p_missing_closing_quote =
                                                                        missing_closing_quote;
                                                                    mailmime_decoded_part_free(
                                                                        decoded,
                                                                    );
                                                                    free(body as *mut libc::c_void);
                                                                    return MAILIMF_NO_ERROR
                                                                        as libc::c_int;
                                                                }
                                                            }
                                                            mailmime_decoded_part_free(decoded);
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                        _ => {}
                                    }
                                    free(body as *mut libc::c_void);
                                    mailmime_encoded_text_free(text);
                                }
                            }
                        }
                        mailmime_charset_free(charset);
                    }
                }
            }
        }
    }
    return res;
}
unsafe fn mailmime_encoding_parse(
    mut message: *const libc::c_char,
    mut length: size_t,
    mut indx: *mut size_t,
    mut result: *mut libc::c_int,
) -> libc::c_int {
    let mut cur_token: size_t = 0;
    let mut encoding: libc::c_int = 0;
    cur_token = *indx;
    if cur_token >= length {
        return MAILIMF_ERROR_PARSE as libc::c_int;
    }
    match toupper(*message.offset(cur_token as isize) as libc::c_uchar as libc::c_int)
        as libc::c_char as libc::c_int
    {
        81 => encoding = MAILMIME_ENCODING_Q as libc::c_int,
        66 => encoding = MAILMIME_ENCODING_B as libc::c_int,
        _ => return MAILIMF_ERROR_INVAL as libc::c_int,
    }
    cur_token = cur_token.wrapping_add(1);
    *result = encoding;
    *indx = cur_token;
    return MAILIMF_NO_ERROR as libc::c_int;
}

/*
 * libEtPan! -- a mail stuff library
 *
 * Copyright (C) 2001, 2005 - DINH Viet Hoa
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the libEtPan! project nor the names of its
 *    contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
/*
 * $Id: mailmime_decode.c,v 1.37 2010/11/16 20:52:28 hoa Exp $
 */
/*
  RFC 2047 : MIME (Multipurpose Internet Mail Extensions) Part Three:
             Message Header Extensions for Non-ASCII Text
*/
unsafe fn mailmime_charset_parse(
    mut message: *const libc::c_char,
    mut length: size_t,
    mut indx: *mut size_t,
    mut charset: *mut *mut libc::c_char,
) -> libc::c_int {
    return mailmime_etoken_parse(message, length, indx, charset);
}
unsafe fn mailmime_etoken_parse(
    mut message: *const libc::c_char,
    mut length: size_t,
    mut indx: *mut size_t,
    mut result: *mut *mut libc::c_char,
) -> libc::c_int {
    return mailimf_custom_string_parse(message, length, indx, result, Some(is_etoken_char));
}

pub unsafe fn is_etoken_char(mut ch: libc::c_char) -> libc::c_int {
    let mut uch: libc::c_uchar = ch as libc::c_uchar;
    if (uch as libc::c_int) < 31i32 {
        return 0i32;
    }
    match uch as libc::c_int {
        32 | 40 | 41 | 60 | 62 | 64 | 44 | 59 | 58 | 34 | 47 | 91 | 93 | 63 | 61 => return 0i32,
        _ => {}
    }
    return 1i32;
}