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 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* GMime
* Copyright (C) 2000-2014 Jeffrey Stedfast and Michael Zucchi
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "gmime-table-private.h"
#include "gmime-encodings.h"
#ifdef ENABLE_WARNINGS
#define w(x) x
#else
#define w(x)
#endif /* ENABLE_WARNINGS */
#define d(x)
/**
* SECTION: gmime-encodings
* @title: gmime-encodings
* @short_description: MIME encoding functions
* @see_also:
*
* Utility functions to encode or decode MIME
* Content-Transfer-Encodings.
**/
#define GMIME_UUENCODE_CHAR(c) ((c) ? (c) + ' ' : '`')
#define GMIME_UUDECODE_CHAR(c) (((c) - ' ') & 077)
static char base64_alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static unsigned char gmime_base64_rank[256] = {
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255, 62,255,255,255, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61,255,255,255, 0,255,255,
255, 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,255,255,255,255,255,
255, 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,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,
};
static unsigned char gmime_uu_rank[256] = {
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47,
48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31,
};
static unsigned char tohex[16] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
/**
* g_mime_content_encoding_from_string:
* @str: a string representing a Content-Transfer-Encoding value
*
* Gets the appropriate #GMimeContentEncoding enumeration value based
* on the input string.
*
* Returns: the #GMimeContentEncoding specified by @str or
* #GMIME_CONTENT_ENCODING_DEFAULT on error.
**/
GMimeContentEncoding
g_mime_content_encoding_from_string (const char *str)
{
if (!g_ascii_strcasecmp (str, "7bit"))
return GMIME_CONTENT_ENCODING_7BIT;
else if (!g_ascii_strcasecmp (str, "8bit"))
return GMIME_CONTENT_ENCODING_8BIT;
else if (!g_ascii_strcasecmp (str, "7-bit"))
return GMIME_CONTENT_ENCODING_7BIT;
else if (!g_ascii_strcasecmp (str, "8-bit"))
return GMIME_CONTENT_ENCODING_8BIT;
else if (!g_ascii_strcasecmp (str, "binary"))
return GMIME_CONTENT_ENCODING_BINARY;
else if (!g_ascii_strcasecmp (str, "base64"))
return GMIME_CONTENT_ENCODING_BASE64;
else if (!g_ascii_strcasecmp (str, "quoted-printable"))
return GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE;
else if (!g_ascii_strcasecmp (str, "uuencode"))
return GMIME_CONTENT_ENCODING_UUENCODE;
else if (!g_ascii_strcasecmp (str, "x-uuencode"))
return GMIME_CONTENT_ENCODING_UUENCODE;
else if (!g_ascii_strcasecmp (str, "x-uue"))
return GMIME_CONTENT_ENCODING_UUENCODE;
else
return GMIME_CONTENT_ENCODING_DEFAULT;
}
/**
* g_mime_content_encoding_to_string:
* @encoding: a #GMimeContentEncoding
*
* Gets the string value of the content encoding.
*
* Returns: the encoding type as a string or %NULL on error. Available
* values for the encoding are: #GMIME_CONTENT_ENCODING_DEFAULT,
* #GMIME_CONTENT_ENCODING_7BIT, #GMIME_CONTENT_ENCODING_8BIT,
* #GMIME_CONTENT_ENCODING_BINARY, #GMIME_CONTENT_ENCODING_BASE64,
* #GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE and
* #GMIME_CONTENT_ENCODING_UUENCODE.
**/
const char *
g_mime_content_encoding_to_string (GMimeContentEncoding encoding)
{
switch (encoding) {
case GMIME_CONTENT_ENCODING_7BIT:
return "7bit";
case GMIME_CONTENT_ENCODING_8BIT:
return "8bit";
case GMIME_CONTENT_ENCODING_BINARY:
return "binary";
case GMIME_CONTENT_ENCODING_BASE64:
return "base64";
case GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE:
return "quoted-printable";
case GMIME_CONTENT_ENCODING_UUENCODE:
return "x-uuencode";
default:
/* I guess this is a good default... */
return NULL;
}
}
/**
* g_mime_encoding_init_encode:
* @state: a #GMimeEncoding to initialize
* @encoding: a #GMimeContentEncoding to use
*
* Initializes a #GMimeEncoding state machine for encoding to
* @encoding.
**/
void
g_mime_encoding_init_encode (GMimeEncoding *state, GMimeContentEncoding encoding)
{
state->encoding = encoding;
state->encode = TRUE;
g_mime_encoding_reset (state);
}
/**
* g_mime_encoding_init_decode:
* @state: a #GMimeEncoding to initialize
* @encoding: a #GMimeContentEncoding to use
*
* Initializes a #GMimeEncoding state machine for decoding from
* @encoding.
**/
void
g_mime_encoding_init_decode (GMimeEncoding *state, GMimeContentEncoding encoding)
{
state->encoding = encoding;
state->encode = FALSE;
g_mime_encoding_reset (state);
}
/**
* g_mime_encoding_reset:
* @state: a #GMimeEncoding to reset
*
* Resets the state of the #GMimeEncoding.
**/
void
g_mime_encoding_reset (GMimeEncoding *state)
{
if (state->encode) {
if (state->encoding == GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE)
state->state = -1;
else
state->state = 0;
} else {
state->state = 0;
}
state->save = 0;
}
/**
* g_mime_encoding_outlen:
* @state: a #GMimeEncoding
* @inlen: an input length
*
* Given the input length, @inlen, calculate the needed output length
* to perform an encoding or decoding step.
*
* Returns: the maximum number of bytes needed to encode or decode a
* buffer of @inlen bytes.
**/
size_t
g_mime_encoding_outlen (GMimeEncoding *state, size_t inlen)
{
switch (state->encoding) {
case GMIME_CONTENT_ENCODING_BASE64:
if (state->encode)
return GMIME_BASE64_ENCODE_LEN (inlen);
else
return inlen + 3;
case GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE:
if (state->encode)
return GMIME_QP_ENCODE_LEN (inlen);
else
return inlen + 2;
case GMIME_CONTENT_ENCODING_UUENCODE:
if (state->encode)
return GMIME_UUENCODE_LEN (inlen);
else
return inlen + 3;
default:
return inlen;
}
}
/**
* g_mime_encoding_step:
* @state: a #GMimeEncoding
* @inbuf: an input buffer to encode or decode
* @inlen: input buffer length
* @outbuf: an output buffer
*
* Incrementally encodes or decodes (depending on @state) an input
* stream by 'stepping' through a block of input at a time.
*
* You should make sure @outbuf is large enough by calling
* g_mime_encoding_outlen() to find out how large @outbuf might need
* to be.
*
* Returns: the number of bytes written to @outbuf.
**/
size_t
g_mime_encoding_step (GMimeEncoding *state, const char *inbuf, size_t inlen, char *outbuf)
{
const unsigned char *inptr = (const unsigned char *) inbuf;
unsigned char *outptr = (unsigned char *) outbuf;
switch (state->encoding) {
case GMIME_CONTENT_ENCODING_BASE64:
if (state->encode)
return g_mime_encoding_base64_encode_step (inptr, inlen, outptr, &state->state, &state->save);
else
return g_mime_encoding_base64_decode_step (inptr, inlen, outptr, &state->state, &state->save);
case GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE:
if (state->encode)
return g_mime_encoding_quoted_encode_step (inptr, inlen, outptr, &state->state, &state->save);
else
return g_mime_encoding_quoted_decode_step (inptr, inlen, outptr, &state->state, &state->save);
case GMIME_CONTENT_ENCODING_UUENCODE:
if (state->encode)
return g_mime_encoding_uuencode_step (inptr, inlen, outptr, state->uubuf, &state->state, &state->save);
else
return g_mime_encoding_uudecode_step (inptr, inlen, outptr, &state->state, &state->save);
default:
memcpy (outbuf, inbuf, inlen);
return inlen;
}
}
/**
* g_mime_encoding_flush:
* @state: a #GMimeEncoding
* @inbuf: an input buffer to encode or decode
* @inlen: input buffer length
* @outbuf: an output buffer
*
* Completes the incremental encode or decode of the input stream (see
* g_mime_encoding_step() for details).
*
* Returns: the number of bytes written to @outbuf.
**/
size_t
g_mime_encoding_flush (GMimeEncoding *state, const char *inbuf, size_t inlen, char *outbuf)
{
const unsigned char *inptr = (const unsigned char *) inbuf;
unsigned char *outptr = (unsigned char *) outbuf;
switch (state->encoding) {
case GMIME_CONTENT_ENCODING_BASE64:
if (state->encode)
return g_mime_encoding_base64_encode_close (inptr, inlen, outptr, &state->state, &state->save);
else
return g_mime_encoding_base64_decode_step (inptr, inlen, outptr, &state->state, &state->save);
case GMIME_CONTENT_ENCODING_QUOTEDPRINTABLE:
if (state->encode)
return g_mime_encoding_quoted_encode_close (inptr, inlen, outptr, &state->state, &state->save);
else
return g_mime_encoding_quoted_decode_step (inptr, inlen, outptr, &state->state, &state->save);
case GMIME_CONTENT_ENCODING_UUENCODE:
if (state->encode)
return g_mime_encoding_uuencode_close (inptr, inlen, outptr, state->uubuf, &state->state, &state->save);
else
return g_mime_encoding_uudecode_step (inptr, inlen, outptr, &state->state, &state->save);
default:
memcpy (outbuf, inbuf, inlen);
return inlen;
}
}
/**
* g_mime_encoding_base64_encode_close:
* @inbuf: input buffer
* @inlen: input buffer length
* @outbuf: output buffer
* @state: holds the number of bits that are stored in @save
* @save: leftover bits that have not yet been encoded
*
* Base64 encodes the input stream to the output stream. Call this
* when finished encoding data with g_mime_encoding_base64_encode_step()
* to flush off the last little bit.
*
* Returns: the number of bytes encoded.
**/
size_t
g_mime_encoding_base64_encode_close (const unsigned char *inbuf, size_t inlen, unsigned char *outbuf, int *state, guint32 *save)
{
unsigned char *outptr = outbuf;
int c1, c2;
if (inlen > 0)
outptr += g_mime_encoding_base64_encode_step (inbuf, inlen, outptr, state, save);
c1 = ((unsigned char *)save)[1];
c2 = ((unsigned char *)save)[2];
switch (((unsigned char *)save)[0]) {
case 2:
outptr[2] = base64_alphabet [(c2 & 0x0f) << 2];
goto skip;
case 1:
outptr[2] = '=';
skip:
outptr[0] = base64_alphabet [c1 >> 2];
outptr[1] = base64_alphabet [c2 >> 4 | ((c1 & 0x3) << 4)];
outptr[3] = '=';
outptr += 4;
break;
}
*outptr++ = '\n';
*save = 0;
*state = 0;
return (outptr - outbuf);
}
/**
* g_mime_encoding_base64_encode_step:
* @inbuf: input buffer
* @inlen: input buffer length
* @outbuf: output buffer
* @state: holds the number of bits that are stored in @save
* @save: leftover bits that have not yet been encoded
*
* Base64 encodes a chunk of data. Performs an 'encode step', only
* encodes blocks of 3 characters to the output at a time, saves
* left-over state in state and save (initialise to 0 on first
* invocation).
*
* Returns: the number of bytes encoded.
**/
size_t
g_mime_encoding_base64_encode_step (const unsigned char *inbuf, size_t inlen, unsigned char *outbuf, int *state, guint32 *save)
{
register const unsigned char *inptr;
register unsigned char *outptr;
if (inlen == 0)
return 0;
outptr = outbuf;
inptr = inbuf;
if (inlen + ((unsigned char *)save)[0] > 2) {
const unsigned char *inend = inbuf + inlen - 2;
register int c1 = 0, c2 = 0, c3 = 0;
register int already;
already = *state;
switch (((char *)save)[0]) {
case 1: c1 = ((unsigned char *)save)[1]; goto skip1;
case 2: c1 = ((unsigned char *)save)[1];
c2 = ((unsigned char *)save)[2]; goto skip2;
}
/* yes, we jump into the loop, no i'm not going to change it, its beautiful! */
while (inptr < inend) {
c1 = *inptr++;
skip1:
c2 = *inptr++;
skip2:
c3 = *inptr++;
*outptr++ = base64_alphabet [c1 >> 2];
*outptr++ = base64_alphabet [(c2 >> 4) | ((c1 & 0x3) << 4)];
*outptr++ = base64_alphabet [((c2 & 0x0f) << 2) | (c3 >> 6)];
*outptr++ = base64_alphabet [c3 & 0x3f];
/* this is a bit ugly ... */
if ((++already) >= 19) {
*outptr++ = '\n';
already = 0;
}
}
((unsigned char *)save)[0] = 0;
inlen = 2 - (inptr - inend);
*state = already;
}
d(printf ("state = %d, inlen = %d\n", (int)((char *)save)[0], inlen));
if (inlen > 0) {
register char *saveout;
/* points to the slot for the next char to save */
saveout = & (((char *)save)[1]) + ((char *)save)[0];
/* inlen can only be 0, 1 or 2 */
switch (inlen) {
case 2: *saveout++ = *inptr++;
case 1: *saveout++ = *inptr++;
}
((char *)save)[0] += (char) inlen;
}
d(printf ("mode = %d\nc1 = %c\nc2 = %c\n",
(int)((char *)save)[0],
(int)((char *)save)[1],
(int)((char *)save)[2]));
return (outptr - outbuf);
}
/**
* g_mime_encoding_base64_decode_step:
* @inbuf: input buffer
* @inlen: input buffer length
* @outbuf: output buffer
* @state: holds the number of bits that are stored in @save
* @save: leftover bits that have not yet been decoded
*
* Decodes a chunk of base64 encoded data.
*
* Returns: the number of bytes decoded (which have been dumped in
* @outbuf).
**/
size_t
g_mime_encoding_base64_decode_step (const unsigned char *inbuf, size_t inlen, unsigned char *outbuf, int *state, guint32 *save)
{
register const unsigned char *inptr;
register unsigned char *outptr;
const unsigned char *inend;
register guint32 saved;
unsigned char c;
int npad, n, i;
inend = inbuf + inlen;
outptr = outbuf;
inptr = inbuf;
npad = (*state >> 8) & 0xff;
n = *state & 0xff;
saved = *save;
/* convert 4 base64 bytes to 3 normal bytes */
while (inptr < inend) {
c = gmime_base64_rank[*inptr++];
if (c != 0xff) {
saved = (saved << 6) | c;
n++;
if (n == 4) {
*outptr++ = saved >> 16;
*outptr++ = saved >> 8;
*outptr++ = saved;
n = 0;
if (npad > 0) {
outptr -= npad;
npad = 0;
}
}
}
}
/* quickly scan back for '=' on the end somewhere */
/* fortunately we can drop 1 output char for each trailing '=' (up to 2) */
for (i = 2; inptr > inbuf && i; ) {
inptr--;
if (gmime_base64_rank[*inptr] != 0xff) {
if (*inptr == '=' && outptr > outbuf) {
if (n == 0) {
/* we've got a complete quartet so it's
safe to drop an output character. */
outptr--;
} else if (npad < 2) {
/* keep a record of the number of ='s at
the end of the input stream, up to 2 */
npad++;
}
}
i--;
}
}
*state = (npad << 8) | n;
*save = n ? saved : 0;
return (outptr - outbuf);
}
/**
* g_mime_encoding_uuencode_close:
* @inbuf: input buffer
* @inlen: input buffer length
* @outbuf: output buffer
* @uubuf: temporary buffer of 60 bytes
* @state: holds the number of bits that are stored in @save
* @save: leftover bits that have not yet been encoded
*
* Uuencodes a chunk of data. Call this when finished encoding data
* with g_mime_encoding_uuencode_step() to flush off the last little bit.
*
* Returns: the number of bytes encoded.
**/
size_t
g_mime_encoding_uuencode_close (const unsigned char *inbuf, size_t inlen, unsigned char *outbuf, unsigned char *uubuf, int *state, guint32 *save)
{
register unsigned char *outptr, *bufptr;
register guint32 saved;
int uulen, uufill, i;
outptr = outbuf;
if (inlen > 0)
outptr += g_mime_encoding_uuencode_step (inbuf, inlen, outbuf, uubuf, state, save);
uufill = 0;
saved = *save;
i = *state & 0xff;
uulen = (*state >> 8) & 0xff;
bufptr = uubuf + ((uulen / 3) * 4);
if (i > 0) {
while (i < 3) {
saved <<= 8;
uufill++;
i++;
}
if (i == 3) {
/* convert 3 normal bytes into 4 uuencoded bytes */
unsigned char b0, b1, b2;
b0 = (saved >> 16) & 0xff;
b1 = (saved >> 8) & 0xff;
b2 = saved & 0xff;
*bufptr++ = GMIME_UUENCODE_CHAR ((b0 >> 2) & 0x3f);
*bufptr++ = GMIME_UUENCODE_CHAR (((b0 << 4) | ((b1 >> 4) & 0xf)) & 0x3f);
*bufptr++ = GMIME_UUENCODE_CHAR (((b1 << 2) | ((b2 >> 6) & 0x3)) & 0x3f);
*bufptr++ = GMIME_UUENCODE_CHAR (b2 & 0x3f);
uulen += 3;
saved = 0;
i = 0;
}
}
if (uulen > 0) {
int cplen = ((uulen / 3) * 4);
*outptr++ = GMIME_UUENCODE_CHAR ((uulen - uufill) & 0xff);
memcpy (outptr, uubuf, cplen);
outptr += cplen;
*outptr++ = '\n';
uulen = 0;
}
*outptr++ = GMIME_UUENCODE_CHAR (uulen & 0xff);
*outptr++ = '\n';
*save = 0;
*state = 0;
return (outptr - outbuf);
}
/**
* g_mime_encoding_uuencode_step:
* @inbuf: input buffer
* @inlen: input buffer length
* @outbuf: output stream
* @uubuf: temporary buffer of 60 bytes
* @state: holds the number of bits that are stored in @save
* @save: leftover bits that have not yet been encoded
*
* Uuencodes a chunk of data. Performs an 'encode step', only encodes
* blocks of 45 characters to the output at a time, saves left-over
* state in @uubuf, @state and @save (initialize to 0 on first
* invocation).
*
* Returns: the number of bytes encoded.
**/
size_t
g_mime_encoding_uuencode_step (const unsigned char *inbuf, size_t inlen, unsigned char *outbuf, unsigned char *uubuf, int *state, guint32 *save)
{
register unsigned char *outptr, *bufptr;
register const unsigned char *inptr;
const unsigned char *inend;
unsigned char b0, b1, b2;
guint32 saved;
int uulen, i;
if (inlen == 0)
return 0;
inend = inbuf + inlen;
outptr = outbuf;
inptr = inbuf;
saved = *save;
i = *state & 0xff;
uulen = (*state >> 8) & 0xff;
if ((inlen + uulen) < 45) {
/* not enough input to write a full uuencoded line */
bufptr = uubuf + ((uulen / 3) * 4);
} else {
bufptr = outptr + 1;
if (uulen > 0) {
/* copy the previous call's tmpbuf to outbuf */
memcpy (bufptr, uubuf, ((uulen / 3) * 4));
bufptr += ((uulen / 3) * 4);
}
}
if (i == 2) {
b0 = (saved >> 8) & 0xff;
b1 = saved & 0xff;
saved = 0;
i = 0;
goto skip2;
} else if (i == 1) {
if ((inptr + 2) < inend) {
b0 = saved & 0xff;
saved = 0;
i = 0;
goto skip1;
}
while (inptr < inend) {
saved = (saved << 8) | *inptr++;
i++;
}
}
while (inptr < inend) {
while (uulen < 45 && (inptr + 3) <= inend) {
b0 = *inptr++;
skip1:
b1 = *inptr++;
skip2:
b2 = *inptr++;
/* convert 3 normal bytes into 4 uuencoded bytes */
*bufptr++ = GMIME_UUENCODE_CHAR ((b0 >> 2) & 0x3f);
*bufptr++ = GMIME_UUENCODE_CHAR (((b0 << 4) | ((b1 >> 4) & 0xf)) & 0x3f);
*bufptr++ = GMIME_UUENCODE_CHAR (((b1 << 2) | ((b2 >> 6) & 0x3)) & 0x3f);
*bufptr++ = GMIME_UUENCODE_CHAR (b2 & 0x3f);
uulen += 3;
}
if (uulen >= 45) {
/* output the uu line length */
*outptr = GMIME_UUENCODE_CHAR (uulen & 0xff);
outptr += ((45 / 3) * 4) + 1;
*outptr++ = '\n';
uulen = 0;
if ((inptr + 45) <= inend) {
/* we have enough input to output another full line */
bufptr = outptr + 1;
} else {
bufptr = uubuf;
}
} else {
/* not enough input to continue... */
for (i = 0, saved = 0; inptr < inend; i++)
saved = (saved << 8) | *inptr++;
}
}
*save = saved;
*state = ((uulen & 0xff) << 8) | (i & 0xff);
return (outptr - outbuf);
}
/**
* g_mime_encoding_uudecode_step:
* @inbuf: input buffer
* @inlen: input buffer length
* @outbuf: output buffer
* @state: holds the number of bits that are stored in @save
* @save: leftover bits that have not yet been decoded
*
* Uudecodes a chunk of data. Performs a 'decode step' on a chunk of
* uuencoded data. Assumes the "begin mode filename" line has
* been stripped off.
*
* Returns: the number of bytes decoded.
**/
size_t
g_mime_encoding_uudecode_step (const unsigned char *inbuf, size_t inlen, unsigned char *outbuf, int *state, guint32 *save)
{
register const unsigned char *inptr;
register unsigned char *outptr;
const unsigned char *inend;
unsigned char ch;
register guint32 saved;
gboolean last_was_eoln;
int uulen, i;
if (*state & GMIME_UUDECODE_STATE_END)
return 0;
saved = *save;
i = *state & 0xff;
uulen = (*state >> 8) & 0xff;
if (uulen == 0)
last_was_eoln = TRUE;
else
last_was_eoln = FALSE;
inend = inbuf + inlen;
outptr = outbuf;
inptr = inbuf;
while (inptr < inend) {
if (*inptr == '\n') {
last_was_eoln = TRUE;
inptr++;
continue;
} else if (!uulen || last_was_eoln) {
/* first octet on a line is the uulen octet */
uulen = gmime_uu_rank[*inptr];
last_was_eoln = FALSE;
if (uulen == 0) {
*state |= GMIME_UUDECODE_STATE_END;
break;
}
inptr++;
continue;
}
ch = *inptr++;
if (uulen > 0) {
/* save the byte */
saved = (saved << 8) | ch;
i++;
if (i == 4) {
/* convert 4 uuencoded bytes to 3 normal bytes */
unsigned char b0, b1, b2, b3;
b0 = saved >> 24;
b1 = saved >> 16 & 0xff;
b2 = saved >> 8 & 0xff;
b3 = saved & 0xff;
if (uulen >= 3) {
*outptr++ = gmime_uu_rank[b0] << 2 | gmime_uu_rank[b1] >> 4;
*outptr++ = gmime_uu_rank[b1] << 4 | gmime_uu_rank[b2] >> 2;
*outptr++ = gmime_uu_rank[b2] << 6 | gmime_uu_rank[b3];
uulen -= 3;
} else {
if (uulen >= 1) {
*outptr++ = gmime_uu_rank[b0] << 2 | gmime_uu_rank[b1] >> 4;
uulen--;
}
if (uulen >= 1) {
*outptr++ = gmime_uu_rank[b1] << 4 | gmime_uu_rank[b2] >> 2;
uulen--;
}
}
i = 0;
saved = 0;
}
} else {
break;
}
}
*save = saved;
*state = (*state & GMIME_UUDECODE_STATE_MASK) | ((uulen & 0xff) << 8) | (i & 0xff);
return (outptr - outbuf);
}
/**
* g_mime_encoding_quoted_encode_close:
* @inbuf: input buffer
* @inlen: input buffer length
* @outbuf: output buffer
* @state: holds the number of bits that are stored in @save
* @save: leftover bits that have not yet been encoded
*
* Quoted-printable encodes a block of text. Call this when finished
* encoding data with g_mime_encoding_quoted_encode_step() to flush off
* the last little bit.
*
* Returns: the number of bytes encoded.
**/
size_t
g_mime_encoding_quoted_encode_close (const unsigned char *inbuf, size_t inlen, unsigned char *outbuf, int *state, guint32 *save)
{
register unsigned char *outptr = outbuf;
int last;
if (inlen > 0)
outptr += g_mime_encoding_quoted_encode_step (inbuf, inlen, outptr, state, save);
last = *state;
if (last != -1) {
/* space/tab must be encoded if its the last character on
the line */
if (is_qpsafe (last) && !is_blank (last)) {
*outptr++ = last;
} else {
*outptr++ = '=';
*outptr++ = tohex[(last >> 4) & 0xf];
*outptr++ = tohex[last & 0xf];
}
}
if (last != '\n') {
/* we end with =\n so that the \n isn't interpreted as a real
\n when it gets decoded later */
*outptr++ = '=';
*outptr++ = '\n';
}
*save = 0;
*state = -1;
return (outptr - outbuf);
}
/**
* g_mime_encoding_quoted_encode_step:
* @inbuf: input buffer
* @inlen: input buffer length
* @outbuf: output buffer
* @state: holds the number of bits that are stored in @save
* @save: leftover bits that have not yet been encoded
*
* Quoted-printable encodes a block of text. Performs an 'encode
* step', saves left-over state in state and save (initialise to -1 on
* first invocation).
*
* Returns: the number of bytes encoded.
**/
size_t
g_mime_encoding_quoted_encode_step (const unsigned char *inbuf, size_t inlen, unsigned char *outbuf, int *state, guint32 *save)
{
register const unsigned char *inptr = inbuf;
const unsigned char *inend = inbuf + inlen;
register unsigned char *outptr = outbuf;
register guint32 sofar = *save; /* keeps track of how many chars on a line */
register int last = *state; /* keeps track if last char to end was a space cr etc */
unsigned char c;
while (inptr < inend) {
c = *inptr++;
if (c == '\r') {
if (last != -1) {
*outptr++ = '=';
*outptr++ = tohex[(last >> 4) & 0xf];
*outptr++ = tohex[last & 0xf];
sofar += 3;
}
last = c;
} else if (c == '\n') {
if (last != -1 && last != '\r') {
*outptr++ = '=';
*outptr++ = tohex[(last >> 4) & 0xf];
*outptr++ = tohex[last & 0xf];
}
*outptr++ = '\n';
sofar = 0;
last = -1;
} else {
if (last != -1) {
if (is_qpsafe (last)) {
*outptr++ = last;
sofar++;
} else {
*outptr++ = '=';
*outptr++ = tohex[(last >> 4) & 0xf];
*outptr++ = tohex[last & 0xf];
sofar += 3;
}
}
if (is_qpsafe (c)) {
if (sofar > 74) {
*outptr++ = '=';
*outptr++ = '\n';
sofar = 0;
}
/* delay output of space char */
if (is_blank (c)) {
last = c;
} else {
*outptr++ = c;
sofar++;
last = -1;
}
} else {
if (sofar > 72) {
*outptr++ = '=';
*outptr++ = '\n';
sofar = 3;
} else
sofar += 3;
*outptr++ = '=';
*outptr++ = tohex[(c >> 4) & 0xf];
*outptr++ = tohex[c & 0xf];
last = -1;
}
}
}
*save = sofar;
*state = last;
return (outptr - outbuf);
}
/**
* g_mime_encoding_quoted_decode_step:
* @inbuf: input buffer
* @inlen: input buffer length
* @outbuf: output buffer
* @state: holds the number of bits that are stored in @save
* @save: leftover bits that have not yet been decoded
*
* Decodes a block of quoted-printable encoded data. Performs a
* 'decode step' on a chunk of QP encoded data.
*
* Returns: the number of bytes decoded.
**/
size_t
g_mime_encoding_quoted_decode_step (const unsigned char *inbuf, size_t inlen, unsigned char *outbuf, int *state, guint32 *save)
{
/* FIXME: this does not strip trailing spaces from lines (as
* it should, rfc 2045, section 6.7) Should it also
* canonicalise the end of line to CR LF??
*
* Note: Trailing rubbish (at the end of input), like = or =x
* or =\r will be lost.
*/
const register unsigned char *inptr = inbuf;
const unsigned char *inend = inbuf + inlen;
register unsigned char *outptr = outbuf;
guint32 isave = *save;
int istate = *state;
unsigned char c;
d(printf ("quoted-printable, decoding text '%.*s'\n", inlen, inbuf));
while (inptr < inend) {
switch (istate) {
case 0:
while (inptr < inend) {
c = *inptr++;
/* FIXME: use a specials table to avoid 3 comparisons for the common case */
if (c == '=') {
istate = 1;
break;
}
#ifdef CANONICALISE_EOL
/*else if (c=='\r') {
state = 3;
} else if (c=='\n') {
*outptr++ = '\r';
*outptr++ = c;
} */
#endif
else {
*outptr++ = c;
}
}
break;
case 1:
c = *inptr++;
if (c == '\n') {
/* soft break ... unix end of line */
istate = 0;
} else {
isave = c;
istate = 2;
}
break;
case 2:
c = *inptr++;
if (isxdigit (c) && isxdigit (isave)) {
c = toupper ((int) c);
isave = toupper ((int) isave);
*outptr++ = (((isave >= 'A' ? isave - 'A' + 10 : isave - '0') & 0x0f) << 4)
| ((c >= 'A' ? c - 'A' + 10 : c - '0') & 0x0f);
} else if (c == '\n' && isave == '\r') {
/* soft break ... canonical end of line */
} else {
/* just output the data */
*outptr++ = '=';
*outptr++ = isave;
*outptr++ = c;
}
istate = 0;
break;
#ifdef CANONICALISE_EOL
case 3:
/* convert \n -> to \r\n, leaves \r\n alone */
c = *inptr++;
if (c == '\n') {
*outptr++ = '\r';
*outptr++ = c;
} else {
*outptr++ = '\r';
*outptr++ = '\n';
*outptr++ = c;
}
istate = 0;
break;
#endif
}
}
*state = istate;
*save = isave;
return (outptr - outbuf);
}
|