3 Copyright 1997-1999,2001 Gisle Aas
5 This library is free software; you can redistribute it and/or
6 modify it under the same terms as Perl itself.
9 The tables and some of the code that used to be here was borrowed from
10 metamail, which comes with this message:
12 Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
14 Permission to use, copy, modify, and distribute this material
15 for any purpose and without fee is hereby granted, provided
16 that the above copyright notice and this permission notice
17 appear in all copies, and that the name of Bellcore not be
18 used in advertising or publicity pertaining to this
19 material without the specific, prior written permission
20 of an authorized representative of Bellcore. BELLCORE
21 MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY
22 OF THIS MATERIAL FOR ANY PURPOSE. IT IS PROVIDED "AS IS",
23 WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
39 #define MAX_LINE 76 /* size of encoded lines */
41 static char basis_64[] =
42 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
44 #define XX 255 /* illegal base64 char */
45 #define EQ 254 /* padding */
48 static unsigned char index_64[256] = {
49 XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
50 XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
51 XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,62, XX,XX,XX,63,
52 52,53,54,55, 56,57,58,59, 60,61,XX,XX, XX,EQ,XX,XX,
53 XX, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
54 15,16,17,18, 19,20,21,22, 23,24,25,XX, XX,XX,XX,XX,
55 XX,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
56 41,42,43,44, 45,46,47,48, 49,50,51,XX, XX,XX,XX,XX,
58 XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
59 XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
60 XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
61 XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
62 XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
63 XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
64 XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
65 XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX, XX,XX,XX,XX,
70 MODULE = MIME::Base64 PACKAGE = MIME::Base64
78 char *str; /* string to encode */
79 SSize_t len; /* length of the string */
80 char *eol; /* the end-of-line sequence to use */
81 STRLEN eollen; /* length of the EOL sequence */
82 char *r; /* result string */
83 STRLEN rlen; /* length of result string */
84 unsigned char c1, c2, c3;
88 sv_utf8_downgrade(sv, FALSE);
89 str = SvPV(sv, rlen); /* SvPV(sv, len) gives warning for signed len */
92 /* set up EOL from the second argument if present, default to "\n" */
93 if (items > 1 && SvOK(ST(1))) {
94 eol = SvPV(ST(1), eollen);
100 /* calculate the length of the result */
101 rlen = (len+2) / 3 * 4; /* encoded bytes */
103 /* add space for EOL */
104 rlen += ((rlen-1) / MAX_LINE + 1) * eollen;
107 /* allocate a result buffer */
108 RETVAL = newSV(rlen ? rlen : 1);
110 SvCUR_set(RETVAL, rlen);
114 for (chunk=0; len > 0; len -= 3, chunk++) {
115 if (chunk == (MAX_LINE/4)) {
117 char *e = eol + eollen;
124 *r++ = basis_64[c1>>2];
125 *r++ = basis_64[((c1 & 0x3)<< 4) | ((c2 & 0xF0) >> 4)];
128 *r++ = basis_64[((c2 & 0xF) << 2) | ((c3 & 0xC0) >>6)];
129 *r++ = basis_64[c3 & 0x3F];
130 } else if (len == 2) {
131 *r++ = basis_64[(c2 & 0xF) << 2];
133 } else { /* len == 1 */
139 /* append eol to the result string */
141 char *e = eol + eollen;
145 *r = '\0'; /* every SV in perl should be NUL-terminated */
157 register unsigned char *str = (unsigned char*)SvPVbyte(sv, len);
158 unsigned char const* end = str + len;
164 /* always enough, but might be too much */
165 STRLEN rlen = len * 3 / 4;
166 RETVAL = newSV(rlen ? rlen : 1);
174 unsigned char uc = index_64[NATIVE_TO_ASCII(*str++)];
181 warn("Premature end of base64 data");
182 if (i < 2) goto thats_it;
183 if (i == 2) c[2] = EQ;
190 if (c[0] == EQ || c[1] == EQ) {
191 if (PL_dowarn) warn("Premature padding of base64 data");
194 /* printf("c0=%d,c1=%d,c2=%d,c3=%d\n", c[0],c[1],c[2],c[3]);/**/
196 *r++ = (c[0] << 2) | ((c[1] & 0x30) >> 4);
200 *r++ = ((c[1] & 0x0F) << 4) | ((c[2] & 0x3C) >> 2);
204 *r++ = ((c[2] & 0x03) << 6) | c[3];
208 SvCUR_set(RETVAL, r - SvPVX(RETVAL));