summaryrefslogtreecommitdiff
path: root/mailnews/mime/src/mimeenc.cpp
blob: d565a6067c95a8afc82425de4aec73225853aca4 (plain)
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
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include <stdio.h>
#include "mimei.h"
#include "prmem.h"
#include "mimeobj.h"
#include "mozilla/RangedPtr.h"
#include "mozilla/mailnews/MimeEncoder.h"

typedef enum mime_encoding {
  mime_Base64, mime_QuotedPrintable, mime_uuencode, mime_yencode
} mime_encoding;

typedef enum mime_decoder_state {
  DS_BEGIN, DS_BODY, DS_END
} mime_decoder_state;

struct MimeDecoderData {
  mime_encoding encoding;    /* Which encoding to use */

  /* A read-buffer used for QP and B64. */
  char token[4];
  int token_size;

  /* State and read-buffer used for uudecode and yencode. */
  mime_decoder_state ds_state;
  char *line_buffer;
  int line_buffer_size;

  MimeObject *objectToDecode; // might be null, only used for QP currently
  /* Where to write the decoded data */
  MimeConverterOutputCallback write_buffer;
  void *closure;
};


static int
mime_decode_qp_buffer (MimeDecoderData *data, const char *buffer,
          int32_t length, int32_t *outSize)
{
  /* Warning, we are overwriting the buffer which was passed in.
   This is ok, because decoding these formats will never result
   in larger data than the input, only smaller. */
  const char *in  = buffer;
  char *out = (char *) buffer;
  char token [3];
  int i;

  NS_ASSERTION(data->encoding == mime_QuotedPrintable, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");
  if (data->encoding != mime_QuotedPrintable) return -1;

  /* For the first pass, initialize the token from the unread-buffer. */
  i = 0;
  while (i < 3 && data->token_size > 0)
  {
    token [i] = data->token[i];
    data->token_size--;
    i++;
  }

  /* #### BUG: when decoding quoted-printable, we are required to
   strip trailing whitespace from lines -- since when encoding in
   qp, one is required to quote such trailing whitespace, any
   trailing whitespace which remains must have been introduced
   by a stupid gateway. */

  /* Treat null bytes as spaces when format_out is
   nsMimeOutput::nsMimeMessageBodyDisplay (see bug 243199 comment 7) */
  bool treatNullAsSpace = data->objectToDecode &&
                            data->objectToDecode->options->format_out == nsMimeOutput::nsMimeMessageBodyDisplay;

  while (length > 0 || i != 0)
  {
    while (i < 3 && length > 0)
    {
      token [i++] = *in;
      in++;
      length--;
    }

    if (i < 3)
    {
      /* Didn't get enough for a complete token.
       If it might be a token, unread it.
       Otherwise, just dump it.
       */
      memcpy (data->token, token, i);
      data->token_size = i;
      i = 0;
      length = 0;
      break;
    }
    i = 0;

    if (token [0] == '=')
    {
      unsigned char c = 0;
      if (token[1] >= '0' && token[1] <= '9')
      c = token[1] - '0';
      else if (token[1] >= 'A' && token[1] <= 'F')
      c = token[1] - ('A' - 10);
      else if (token[1] >= 'a' && token[1] <= 'f')
      c = token[1] - ('a' - 10);
      else if (token[1] == '\r' || token[1] == '\n')
      {
        /* =\n means ignore the newline. */
        if (token[1] == '\r' && token[2] == '\n')
        ;    /* swallow all three chars */
        else
        {
          in--;  /* put the third char back */
          length++;
        }
        continue;
      }
      else
      {
        /* = followed by something other than hex or newline -
         pass it through unaltered, I guess.  (But, if
         this bogus token happened to occur over a buffer
         boundary, we can't do this, since we don't have
         space for it.  Oh well.  Screw it.)  */
        if (in > out) *out++ = token[0];
        if (in > out) *out++ = token[1];
        if (in > out) *out++ = token[2];
        continue;
      }

      /* Second hex digit */
      c = (c << 4);
      if (token[2] >= '0' && token[2] <= '9')
      c += token[2] - '0';
      else if (token[2] >= 'A' && token[2] <= 'F')
      c += token[2] - ('A' - 10);
      else if (token[2] >= 'a' && token[2] <= 'f')
      c += token[2] - ('a' - 10);
      else
      {
        /* We got =xy where "x" was hex and "y" was not, so
         treat that as a literal "=", x, and y.  (But, if
         this bogus token happened to occur over a buffer
         boundary, we can't do this, since we don't have
         space for it.  Oh well.  Screw it.) */
        if (in > out) *out++ = token[0];
        if (in > out) *out++ = token[1];
        if (in > out) *out++ = token[2];
        continue;
      }

      *out++ = c ? (char) c : ((treatNullAsSpace) ? ' ' : (char) c);
    }
    else
    {
      *out++ = token[0];

      token[0] = token[1];
      token[1] = token[2];
      i = 2;
    }
  }

  // Fill the size
  if (outSize)
    *outSize = out - buffer;

  /* Now that we've altered the data in place, write it. */
  if (out > buffer)
  return data->write_buffer (buffer, (out - buffer), data->closure);
  else
  return 1;
}


static int
mime_decode_base64_token (const char *in, char *out)
{
  /* reads 4, writes 0-3.  Returns bytes written.
   (Writes less than 3 only at EOF.) */
  int j;
  int eq_count = 0;
  unsigned long num = 0;

  for (j = 0; j < 4; j++)
  {
    unsigned char c = 0;
    if (in[j] >= 'A' && in[j] <= 'Z')     c = in[j] - 'A';
    else if (in[j] >= 'a' && in[j] <= 'z') c = in[j] - ('a' - 26);
    else if (in[j] >= '0' && in[j] <= '9') c = in[j] - ('0' - 52);
    else if (in[j] == '+')         c = 62;
    else if (in[j] == '/')         c = 63;
    else if (in[j] == '=')         c = 0, eq_count++;
    else
    NS_ERROR("Invalid character");
    num = (num << 6) | c;
  }

  *out++ = (char) (num >> 16);
  *out++ = (char) ((num >> 8) & 0xFF);
  *out++ = (char) (num & 0xFF);

  if (eq_count == 0)
  return 3;        /* No "=" padding means 4 bytes mapped to 3. */
  else if (eq_count == 1)
  return 2;        /* "xxx=" means 3 bytes mapped to 2. */
  else if (eq_count == 2)
  return 1;        /* "xx==" means 2 bytes mapped to 1. */
  else
  {
    // "x===" can't happen, because "x" would then be encoding only
    // 6 bits, not the min of 8.
    NS_ERROR("Count is 6 bits, should be at least 8");
    return 1;
  }
}


static int
mime_decode_base64_buffer (MimeDecoderData *data,
               const char *buffer, int32_t length, int32_t *outSize)
{
  /* Warning, we are overwriting the buffer which was passed in.
   This is ok, because decoding these formats will never result
   in larger data than the input, only smaller. */
  const char *in  = buffer;
  char *out = (char *) buffer;
  char token [4];
  int i;
  bool leftover = (data->token_size > 0);

  NS_ASSERTION(data->encoding == mime_Base64, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");

  /* For the first pass, initialize the token from the unread-buffer. */
  i = 0;
  while (i < 4 && data->token_size > 0)
  {
    token [i] = data->token[i];
    data->token_size--;
    i++;
  }

  while (length > 0)
  {
    while (i < 4 && length > 0)
    {
      if ((*in >= 'A' && *in <= 'Z') ||
        (*in >= 'a' && *in <= 'z') ||
        (*in >= '0' && *in <= '9') ||
        *in == '+' || *in == '/' || *in == '=')
      token [i++] = *in;
      in++;
      length--;
    }

    if (i < 4)
    {
      /* Didn't get enough for a complete token. */
      memcpy (data->token, token, i);
      data->token_size = i;
      length = 0;
      break;
    }
    i = 0;

    if (leftover)
    {
      /* If there are characters left over from the last time around,
       we might not have space in the buffer to do our dirty work
       (if there were 2 or 3 left over, then there is only room for
       1 or 2 in the buffer right now, and we need 3.)  This is only
       a problem for the first chunk in each buffer, so in that
       case, just write prematurely. */
      int n;
      n = mime_decode_base64_token (token, token);
      n = data->write_buffer (token, n, data->closure);
      if (n < 0) /* abort */
      return n;

      /* increment buffer so that we don't write the 1 or 2 unused
       characters now at the front. */
      buffer = in;
      out = (char *) buffer;

      leftover = false;
    }
    else
    {
      int n = mime_decode_base64_token (token, out);
      /* Advance "out" by the number of bytes just written to it. */
      out += n;
    }
  }

  if (outSize)
    *outSize = out - buffer;
  /* Now that we've altered the data in place, write it. */
  if (out > buffer)
  return data->write_buffer (buffer, (out - buffer), data->closure);
  else
  return 1;
}


static int
mime_decode_uue_buffer (MimeDecoderData *data,
            const char *input_buffer, int32_t input_length, int32_t *outSize)
{
  /* First, copy input_buffer into state->line_buffer until we have
   a complete line.

   Then decode that line in place (in the line_buffer) and write
   it out.

   Then pull the next line into line_buffer and continue.
   */
  if (!data->line_buffer)
  {
    data->line_buffer_size = 128;
    data->line_buffer = (char *)PR_MALLOC(data->line_buffer_size);
    if (!data->line_buffer)
      return -1;
    data->line_buffer[0] = 0;
  }

  int status = 0;
  char *line = data->line_buffer;
  char *line_end = data->line_buffer + data->line_buffer_size - 1;

  NS_ASSERTION(data->encoding == mime_uuencode, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");
  if (data->encoding != mime_uuencode) return -1;

  if (data->ds_state == DS_END)
  {
    status = 0;
    goto DONE;
  }

  while (input_length > 0)
  {
    /* Copy data from input_buffer to `line' until we have a complete line,
     or until we've run out of input.

     (line may have data in it already if the last time we were called,
     we weren't called with a buffer that ended on a line boundary.)
     */
    {
    char *out = line + strlen(line);
    while (input_length > 0 &&
         out < line_end)
      {
      *out++ = *input_buffer++;
      input_length--;

      if (out[-1] == '\r' || out[-1] == '\n')
        {
        /* If we just copied a CR, and an LF is waiting, grab it too.
         */
        if (out[-1] == '\r' &&
          input_length > 0 &&
          *input_buffer == '\n')
          input_buffer++, input_length--;

        /* We have a line. */
        break;
        }
      }
    *out = 0;

    /* Ignore blank lines.
     */
    if (*line == '\r' || *line == '\n')
      {
      *line = 0;
      continue;
      }

    /* If this line was bigger than our buffer, truncate it.
       (This means the data was way corrupted, and there's basically
       no chance of decoding it properly, but give it a shot anyway.)
     */
    if (out == line_end)
      {
      out--;
      out[-1] = '\r';
      out[0] = 0;
      }

    /* If we didn't get a complete line, simply return; we'll be called
       with the rest of this line next time.
     */
    if (out[-1] != '\r' && out[-1] != '\n')
      {
      NS_ASSERTION (input_length == 0, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");
      break;
      }
    }


    /* Now we have a complete line.  Deal with it.
     */


    if (data->ds_state == DS_BODY &&
      line[0] == 'e' &&
      line[1] == 'n' &&
      line[2] == 'd' &&
      (line[3] == '\r' ||
       line[3] == '\n'))
    {
      /* done! */
      data->ds_state = DS_END;
      *line = 0;
      break;
    }
    else if (data->ds_state == DS_BEGIN)
    {
      if (!strncmp (line, "begin ", 6))
      data->ds_state = DS_BODY;
      *line = 0;
      continue;
    }
    else
    {
      /* We're in DS_BODY.  Decode the line. */
      char *in, *out;
      int32_t i;
      long lost;

      NS_ASSERTION (data->ds_state == DS_BODY, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");

      /* We map down `line', reading four bytes and writing three.
       That means that `out' always stays safely behind `in'.
       */
      in = line;
      out = line;

# undef DEC
# define DEC(c) (((c) - ' ') & 077)
      i = DEC (*in); /* get length */

      /* all the parens and casts are because gcc was doing something evil.
       */
      lost = ((long) i) - (((((long) strlen (in)) - 2L) * 3L) / 4L);

      if (lost > 0) /* Short line!! */
      {
        /* If we get here, then the line is shorter than the length byte
         at the beginning says it should be.  However, the case where
         the line is short because it was at the end of the buffer and
         we didn't get the whole line was handled earlier (up by the
         "didn't get a complete line" comment.)  So if we've gotten
         here, then this is a complete line which is internally
         inconsistent.  We will parse from it what we can...

         This probably happened because some gateway stripped trailing
         whitespace from the end of the line -- so pretend the line
         was padded with spaces (which map to \000.)
         */
        i -= lost;
      }

      for (++in; i > 0; in += 4, i -= 3)
      {
        char ch;
        NS_ASSERTION(out <= in, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");

        if (i >= 3)
        {
          /* We read four; write three. */
          ch = DEC (in[0]) << 2 | DEC (in[1]) >> 4;
          *out++ = ch;

          NS_ASSERTION(out <= in+1, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");

          ch = DEC (in[1]) << 4 | DEC (in[2]) >> 2;
          *out++ = ch;

          NS_ASSERTION(out <= in+2, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");

          ch = DEC (in[2]) << 6 | DEC (in[3]);
          *out++ = ch;

          NS_ASSERTION(out <= in+3, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");
        }
        else
        {
          /* Handle a line that isn't a multiple of 4 long.
           (We read 1, 2, or 3, and will write 1 or 2.)
           */
          NS_ASSERTION (i > 0 && i < 3, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");

          ch = DEC (in[0]) << 2 | DEC (in[1]) >> 4;
          *out++ = ch;

          NS_ASSERTION(out <= in+1, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");

          if (i == 2)
          {
            ch = DEC (in[1]) << 4 | DEC (in[2]) >> 2;
            *out++ = ch;

            NS_ASSERTION(out <= in+2, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");
          }
        }
      }

      /* If the line was truncated, pad the missing bytes with 0 (SPC). */
      while (lost > 0)
      {
        *out++ = 0;
        lost--;
        in = out+1; /* just to prevent the assert, below. */
      }
# undef DEC

      /* Now write out what we decoded for this line.
       */
      NS_ASSERTION(out >= line && out < in, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");
      if (out > line)
      status = data->write_buffer (line, (out - line), data->closure);

      // The assertion above tells us this is >= 0
      if (outSize)
        *outSize = out - line;

      /* Reset the line so that we don't think it's partial next time. */
      *line = 0;

      if (status < 0) /* abort */
      goto DONE;
    }
  }

  status = 1;

 DONE:

  return status;
}

static int
mime_decode_yenc_buffer (MimeDecoderData *data,
            const char *input_buffer, int32_t input_length, int32_t *outSize)
{
  /* First, copy input_buffer into state->line_buffer until we have
   a complete line.

   Then decode that line in place (in the line_buffer) and write
   it out.

   Then pull the next line into line_buffer and continue.
   */
  if (!data->line_buffer)
  {
    data->line_buffer_size = 1000; // let make sure we have plenty of space for the header line
    data->line_buffer = (char *)PR_MALLOC(data->line_buffer_size);
    if (!data->line_buffer)
      return -1;
    data->line_buffer[0] = 0;
  }

  int status = 0;
  char *line = data->line_buffer;
  char *line_end = data->line_buffer + data->line_buffer_size - 1;

  NS_ASSERTION(data->encoding == mime_yencode, "wrong decoder!");
  if (data->encoding != mime_yencode) return -1;

  if (data->ds_state == DS_END)
    return 0;

  while (input_length > 0)
  {
    /* Copy data from input_buffer to `line' until we have a complete line,
       or until we've run out of input.

       (line may have data in it already if the last time we were called,
       we weren't called with a buffer that ended on a line boundary.)
    */
    {
      char *out = line + strlen(line);
      while (input_length > 0 && out < line_end)
      {
        *out++ = *input_buffer++;
        input_length--;

        if (out[-1] == '\r' || out[-1] == '\n')
        {
          /* If we just copied a CR, and an LF is waiting, grab it too. */
          if (out[-1] == '\r' &&
                  input_length > 0 &&
                  *input_buffer == '\n')
            input_buffer++, input_length--;

           /* We have a line. */
           break;
        }
      }
      *out = 0;

      /* Ignore blank lines. */
      if (*line == '\r' || *line == '\n')
      {
        *line = 0;
        continue;
      }

      /* If this line was bigger than our buffer, truncate it.
         (This means the data was way corrupted, and there's basically
         no chance of decoding it properly, but give it a shot anyway.)
      */
      if (out == line_end)
      {
        out--;
        out[-1] = '\r';
        out[0] = 0;
      }

      /* If we didn't get a complete line, simply return; we'll be called
         with the rest of this line next time.
      */
      if (out[-1] != '\r' && out[-1] != '\n')
      {
        NS_ASSERTION (input_length == 0, "empty buffer!");
        break;
      }
    }


    /* Now we have a complete line.  Deal with it.
     */
    const char * endOfLine = line + strlen(line);

    if (data->ds_state == DS_BEGIN)
    {
      int new_line_size = 0;
      /* this yenc decoder does not support yenc v2 or multipart yenc.
         Therefore, we are looking first for "=ybegin line="
      */
      if ((endOfLine - line) >= 13 && !strncmp (line, "=ybegin line=", 13))
      {
        /* ...then couple digits. */
        for (line += 13; line < endOfLine; line ++)
        {
          if (*line < '0' || *line > '9')
            break;
          new_line_size = (new_line_size * 10) + *line - '0';
        }

        /* ...next, look for <space>size= */
        if ((endOfLine - line) >= 6 && !strncmp (line, " size=", 6))
        {
          /* ...then couple digits. */
          for (line += 6; line < endOfLine; line ++)
            if (*line < '0' || *line > '9')
              break;

          /* ...next, look for <space>name= */
          if ((endOfLine - line) >= 6 && !strncmp (line, " name=", 6))
          {
            /* we have found the yenc header line.
               Now check if we need to grow our buffer line
            */
            data->ds_state = DS_BODY;
            if (new_line_size > data->line_buffer_size && new_line_size <= 997) /* don't let bad value hurt us! */
            {
              PR_Free(data->line_buffer);
              data->line_buffer_size = new_line_size + 4; //extra chars for line ending and potential escape char
              data->line_buffer = (char *)PR_MALLOC(data->line_buffer_size);
              if (!data->line_buffer)
                return -1;
            }
          }
        }

      }
      *data->line_buffer = 0;
      continue;
    }

    if (data->ds_state == DS_BODY && line[0] == '=')
    {
      /* look if this this the final line */
      if (!strncmp (line, "=yend size=", 11))
      {
        /* done! */
        data->ds_state = DS_END;
        *line = 0;
        break;
      }
    }

    /* We're in DS_BODY.  Decode the line in place. */
    {
      char *src = line;
      char *dest = src;
      char c;
      for (; src < line_end; src ++)
      {
        c = *src;
        if (!c || c == '\r' || c == '\n')
          break;

        if (c == '=')
        {
          src++;
          c = *src;
          if (c == 0)
            return -1;  /* last character cannot be escape char */
          c -= 64;
        }
        c -= 42;
        *dest = c;
        dest ++;
      }

      // The assertion below is helpful, too
      if (outSize)
        *outSize = dest - line;

      /* Now write out what we decoded for this line. */
      NS_ASSERTION(dest >= line && dest <= src, "nothing to write!");
      if (dest > line)
      {
        status = data->write_buffer (line, dest - line, data->closure);
        if (status < 0) /* abort */
          return status;
      }

      /* Reset the line so that we don't think it's partial next time. */
      *line = 0;
    }
  }

  return 1;
}

int
MimeDecoderDestroy (MimeDecoderData *data, bool abort_p)
{
  int status = 0;
  /* Flush out the last few buffered characters. */
  if (!abort_p &&
    data->token_size > 0 &&
    data->token[0] != '=')
  {
    if (data->encoding == mime_Base64)
    while ((unsigned int)data->token_size < sizeof (data->token))
      data->token [data->token_size++] = '=';

    status = data->write_buffer (data->token, data->token_size,
                   data->closure);
  }

  if (data->line_buffer)
    PR_Free(data->line_buffer);
  PR_Free (data);
  return status;
}


static MimeDecoderData *
mime_decoder_init (mime_encoding which,
           MimeConverterOutputCallback output_fn,
           void *closure)
{
  MimeDecoderData *data = PR_NEW(MimeDecoderData);
  if (!data) return 0;
  memset(data, 0, sizeof(*data));
  data->encoding = which;
  data->write_buffer = output_fn;
  data->closure = closure;
  data->line_buffer_size = 0;
  data->line_buffer = nullptr;

  return data;
}

MimeDecoderData *
MimeB64DecoderInit (MimeConverterOutputCallback output_fn, void *closure)
{
  return mime_decoder_init (mime_Base64, output_fn, closure);
}

MimeDecoderData *
MimeQPDecoderInit (MimeConverterOutputCallback output_fn,
           void *closure, MimeObject *object)
{
  MimeDecoderData *retData = mime_decoder_init (mime_QuotedPrintable, output_fn, closure);
  if (retData)
    retData->objectToDecode = object;
  return retData;
}

MimeDecoderData *
MimeUUDecoderInit (MimeConverterOutputCallback output_fn,
           void *closure)
{
  return mime_decoder_init (mime_uuencode, output_fn, closure);
}

MimeDecoderData *
MimeYDecoderInit (MimeConverterOutputCallback output_fn,
           void *closure)
{
  return mime_decoder_init (mime_yencode, output_fn, closure);
}

int
MimeDecoderWrite (MimeDecoderData *data, const char *buffer, int32_t size,
          int32_t *outSize)
{
  NS_ASSERTION(data, "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");
  if (!data) return -1;
  switch(data->encoding)
  {
  case mime_Base64:
    return mime_decode_base64_buffer (data, buffer, size, outSize);
  case mime_QuotedPrintable:
    return mime_decode_qp_buffer (data, buffer, size, outSize);
  case mime_uuencode:
    return mime_decode_uue_buffer (data, buffer, size, outSize);
  case mime_yencode:
    return mime_decode_yenc_buffer (data, buffer, size, outSize);
  default:
    NS_ERROR("Invalid decoding");
    return -1;
  }
}


namespace mozilla {
namespace mailnews {

MimeEncoder::MimeEncoder(OutputCallback callback, void *closure)
: mCallback(callback),
  mClosure(closure),
  mCurrentColumn(0)
{}

class Base64Encoder : public MimeEncoder {
  unsigned char in_buffer[3];
  int32_t in_buffer_count;

public:
  Base64Encoder(OutputCallback callback, void *closure)
    : MimeEncoder(callback, closure),
      in_buffer_count(0) {}
  virtual ~Base64Encoder() {}

  virtual nsresult Write(const char *buffer, int32_t size) override;
  virtual nsresult Flush() override;

private:
  static void Base64EncodeBits(RangedPtr<char> &out, uint32_t bits);
};

nsresult Base64Encoder::Write(const char *buffer, int32_t size)
{
  if (size == 0)
    return NS_OK;
  else if (size < 0)
  {
    NS_ERROR("Size is less than 0");
    return NS_ERROR_FAILURE;
  }

  // If this input buffer is too small, wait until next time.
  if (size < (3 - in_buffer_count))
  {
    NS_ASSERTION(size == 1 || size == 2, "Unexpected size");
    in_buffer[in_buffer_count++] = buffer[0];
    if (size == 2)
      in_buffer[in_buffer_count++] = buffer[1];
    NS_ASSERTION(in_buffer_count < 3, "Unexpected out buffer size");
    return NS_OK;
  }


  // If there are bytes that were put back last time, take them now.
  uint32_t i = in_buffer_count, bits = 0;
  if (in_buffer_count > 0) bits = in_buffer[0];
  if (in_buffer_count > 1) bits = (bits << 8) + in_buffer[1];
  in_buffer_count = 0;

  // If this buffer is not a multiple of three, put one or two bytes back.
  uint32_t excess = ((size + i) % 3);
  if (excess)
  {
    in_buffer[0] = buffer[size - excess];
    if (excess > 1)
      in_buffer [1] = buffer[size - excess + 1];
    in_buffer_count = excess;
    size -= excess;
    NS_ASSERTION (! ((size + i) % 3), "1.1 <rhp@netscape.com> 19 Mar 1999 12:00");
  }

  const uint8_t *in = (const uint8_t *)buffer;
  const uint8_t *end = (const uint8_t *)(buffer + size);
  MOZ_ASSERT((end - in + i) % 3 == 0, "Need a multiple of 3 bytes to decode");

  // Populate the out_buffer with base64 data, one line at a time.
  char out_buffer[80]; // Max line length will be 80, so this is safe.
  RangedPtr<char> out(out_buffer);
  while (in < end)
  {
    // Accumulate the input bits.
    while (i < 3)
    {
      bits = (bits << 8) | *in++;
      i++;
    }
    i = 0;

    Base64EncodeBits(out, bits);

    mCurrentColumn += 4;
    if (mCurrentColumn >= 72)
    {
      // Do a linebreak before column 76.  Flush out the line buffer.
      mCurrentColumn = 0;
      *out++ = '\x0D';
      *out++ = '\x0A';
      nsresult rv = mCallback(out_buffer, (out.get() - out_buffer), mClosure);
      NS_ENSURE_SUCCESS(rv, rv);
      out = out_buffer;
    }
  }

  // Write out the unwritten portion of the last line buffer.
  if (out.get() > out_buffer)
  {
    nsresult rv = mCallback(out_buffer, out.get() - out_buffer, mClosure);
    NS_ENSURE_SUCCESS(rv, rv);
  }

  return NS_OK;
}

nsresult Base64Encoder::Flush()
{
  if (in_buffer_count == 0)
    return NS_OK;

  // Since we need to some buffering to get a multiple of three bytes on each
  // block, there may be a few bytes left in the buffer after the last block has
  // been written. We need to flush those out now.
  char buf[4];
  RangedPtr<char> out(buf);
  uint32_t bits = ((uint32_t)in_buffer[0]) << 16;
  if (in_buffer_count > 1)
    bits |= (((uint32_t)in_buffer[1]) << 8);

  Base64EncodeBits(out, bits);

  // Pad with equal-signs.
  if (in_buffer_count == 1)
    buf[2] = '=';
  buf[3] = '=';

  return mCallback(buf, 4, mClosure);
}

void Base64Encoder::Base64EncodeBits(RangedPtr<char> &out, uint32_t bits)
{
  // Convert 3 bytes to 4 base64 bytes
  for (int32_t j = 18; j >= 0; j -= 6)
  {
    unsigned int k = (bits >> j) & 0x3F;
    if (k < 26)       *out++ = k      + 'A';
    else if (k < 52)  *out++ = k - 26 + 'a';
    else if (k < 62)  *out++ = k - 52 + '0';
    else if (k == 62) *out++ = '+';
    else if (k == 63) *out++ = '/';
    else MOZ_CRASH("6 bits should only be between 0 and 64");
  }
}

class QPEncoder : public MimeEncoder {
public:
  QPEncoder(OutputCallback callback, void *closure)
    : MimeEncoder(callback, closure) {}
  virtual ~QPEncoder() {}

  virtual nsresult Write(const char *buffer, int32_t size) override;
};

nsresult QPEncoder::Write(const char *buffer, int32_t size)
{
  nsresult rv = NS_OK;
  static const char *hexdigits = "0123456789ABCDEF";
  char out_buffer[80];
  RangedPtr<char> out(out_buffer);
  bool white = false;

  // Populate the out_buffer with quoted-printable data, one line at a time.
  const uint8_t *in = (uint8_t *)buffer;
  const uint8_t *end = in + size;
  for (; in < end; in++)
  {
    if (*in == '\r' || *in == '\n')
    {
      // If it's CRLF, swallow two chars instead of one.
      if (in + 1 < end && in[0] == '\r' && in[1] == '\n')
        in++;

      // Whitespace cannot be allowed to occur at the end of the line, so we
      // back up and replace the whitespace with its code.
      if (white)
      {
        out--;
        char whitespace_char = *out;
        *out++ = '=';
        *out++ = hexdigits[whitespace_char >> 4];
        *out++ = hexdigits[whitespace_char & 0xF];
      }

      // Now write out the newline.
      *out++ = '\r';
      *out++ = '\n';
      white = false;

      rv = mCallback(out_buffer, out.get() - out_buffer, mClosure);
      NS_ENSURE_SUCCESS(rv, rv);
      out = out_buffer;
      mCurrentColumn = 0;
    }
    else if (mCurrentColumn == 0 && *in == '.')
    {
      // Just to be SMTP-safe, if "." appears in column 0, encode it.
      goto HEX;
    }
    else if (mCurrentColumn == 0 && *in == 'F'
               && (in >= end-1 || in[1] == 'r')
               && (in >= end-2 || in[2] == 'o')
               && (in >= end-3 || in[3] == 'm')
               && (in >= end-4 || in[4] == ' '))
    {
      // If this line begins with "From " (or it could but we don't have enough
      // data in the buffer to be certain), encode the 'F' in hex to avoid
      // potential problems with BSD mailbox formats.
      goto HEX;
    }
    else if ((*in >= 33 && *in <= 60) |
             (*in >= 62 && *in <= 126)) // Printable characters except for '='
    {
      white = false;
      *out++ = *in;
      mCurrentColumn++;
    }
    else if (*in == ' ' || *in == '\t') // Whitespace
    {
      white = true;
      *out++ = *in;
      mCurrentColumn++;
    }
    else
    {
      // Encode the characters here
HEX:
      white = false;
      *out++ = '=';
      *out++ = hexdigits[*in >> 4];
      *out++ = hexdigits[*in & 0xF];
      mCurrentColumn += 3;
    }

    MOZ_ASSERT(mCurrentColumn <= 76, "Why haven't we added a line break yet?");

    if (mCurrentColumn >= 73) // Soft line break for readability
    {
      *out++ = '=';
      *out++ = '\r';
      *out++ = '\n';

      rv = mCallback(out_buffer, out.get() - out_buffer, mClosure);
      NS_ENSURE_SUCCESS(rv, rv);
      out = out_buffer;
      white = false;
      mCurrentColumn = 0;
    }
  }

  // Write out the unwritten portion of the last line buffer.
  if (out.get() != out_buffer)
  {
    rv = mCallback(out_buffer, out.get() - out_buffer, mClosure);
    NS_ENSURE_SUCCESS(rv, rv);
  }

  return NS_OK;
}

MimeEncoder *MimeEncoder::GetBase64Encoder(OutputCallback callback,
    void *closure)
{
  return new Base64Encoder(callback, closure);
}

MimeEncoder *MimeEncoder::GetQPEncoder(OutputCallback callback, void *closure)
{
  return new QPEncoder(callback, closure);
}

} // namespace mailnews
} // namespace mozilla