1 /* @(#)base64.c 1.10 09/07/10 Copyright 1998,1999 Heiko Eissfeldt, Copyright 2006-2009 J. Schilling */
2 #include "config.h"
3 #ifndef lint
4 static UConst char sccsid[] =
5 "@(#)base64.c 1.10 09/07/10 Copyright 1998,1999 Heiko Eissfeldt, Copyright 2006-2009 J. Schilling";
6
7 #endif
8 /*
9 * A hacked version of rfc822_binary() for the Internet CD Index
10 *
11 * This is not a true RFC822 rfc822_binary() anymore. The characters
12 * '/', '+', and '=' do not work well as part of an URL.
13 * '_', '.', and '-' are used as an replacement.
14 * A final CRLF is not added.
15 */
16 /*
17 * Program: RFC-822 routines (originally from SMTP)
18 *
19 * Author: Mark Crispin
20 * Networks and Distributed Computing
21 * Computing & Communications
22 * University of Washington
23 * Administration Building, AG-44
24 * Seattle, WA 98195
25 * Internet: MRC@CAC.Washington.EDU
26 *
27 * Date: 27 July 1988
28 * Last Edited: 10 September 1998
29 *
30 * Sponsorship: The original version of this work was developed in the
31 * Symbolic Systems Resources Group of the Knowledge Systems
32 * Laboratory at Stanford University in 1987-88, and was funded
33 * by the Biomedical Research Technology Program of the National
34 * Institutes of Health under grant number RR-00785.
35 *
36 * Original version Copyright 1988 by The Leland Stanford Junior University
37 * Copyright 1998 by the University of Washington
38 *
39 * Permission to use, copy, modify, and distribute this software and its
40 * documentation for any purpose and without fee is hereby granted, provided
41 * that the above copyright notices appear in all copies and that both the
42 * above copyright notices and this permission notice appear in supporting
43 * documentation, and that the name of the University of Washington or The
44 * Leland Stanford Junior University not be used in advertising or publicity
45 * pertaining to distribution of the software without specific, written prior
46 * permission. This software is made available "as is", and
47 * THE UNIVERSITY OF WASHINGTON AND THE LELAND STANFORD JUNIOR UNIVERSITY
48 * DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO THIS SOFTWARE,
49 * INCLUDING WITHOUT LIMITATION ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
50 * FITNESS FOR A PARTICULAR PURPOSE, AND IN NO EVENT SHALL THE UNIVERSITY OF
51 * WASHINGTON OR THE LELAND STANFORD JUNIOR UNIVERSITY BE LIABLE FOR ANY
52 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
53 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
54 * CONTRACT, TORT (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF
55 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
56 *
57 */
58
59 #include "config.h"
60 #include <schily/stdio.h>
61 #include <schily/stdlib.h>
62
63 #include "base64.h"
64
65 /*
66 * Convert binary contents to BASE64
67 * Accepts: source
68 * length of source
69 * pointer to return destination length
70 * Returns: destination as BASE64
71 */
72
73 unsigned char *
rfc822_binary(src,srcl,len)74 rfc822_binary(src, srcl, len)
75 void *src;
76 unsigned long srcl;
77 unsigned long *len;
78 {
79 unsigned char *ret;
80 unsigned char *d;
81 unsigned char *s = (unsigned char *) src;
82 /* char *v = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";*/
83 char *v = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._";
84 unsigned long i = ((srcl + 2) / 3) * 4;
85
86 *len = i += 2 * ((i / 60) + 1);
87 d = ret = malloc((size_t) ++i);
88 for (i = 0; srcl; s += 3) { /* process tuplets */
89 *d++ = v[s[0] >> 2]; /* byte 1: high 6 bits (1) */
90 /* byte 2: low 2 bits (1), high 4 bits (2) */
91 *d++ = v[((s[0] << 4) + (--srcl ? (s[1] >> 4) : 0)) & 0x3f];
92 /* byte 3: low 4 bits (2), high 2 bits (3) */
93 *d++ = srcl ? v[((s[1] << 2) + (--srcl ? (s[2] >> 6) : 0)) & 0x3f] : '-';
94 /* byte 4: low 6 bits (3) */
95 *d++ = srcl ? v[s[2] & 0x3f] : '-';
96 if (srcl) srcl--; /* count third character if processed */
97 if ((++i) == 15) { /* output 60 characters? */
98 i = 0; /* restart line break count, insert CRLF */
99 *d++ = '\015'; *d++ = '\012';
100 }
101 }
102 *d = '\0'; /* tie off string */
103
104 return (ret); /* return the resulting string */
105 }
106