1 /* -------------------------------------------------------------------------- |
|
2 |
|
3 MusicBrainz -- The Internet music metadatabase |
|
4 |
|
5 Copyright (C) 2000 Robert Kaye |
|
6 |
|
7 This library is free software; you can redistribute it and/or |
|
8 modify it under the terms of the GNU Lesser General Public |
|
9 License as published by the Free Software Foundation; either |
|
10 version 2.1 of the License, or (at your option) any later version. |
|
11 |
|
12 This library is distributed in the hope that it will be useful, |
|
13 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
15 Lesser General Public License for more details. |
|
16 |
|
17 You should have received a copy of the GNU Lesser General Public |
|
18 License along with this library; if not, write to the Free Software |
|
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
20 |
|
21 $Id: base64.c,v 1.1 2005-12-28 18:06:50 tpm Exp $ |
|
22 |
|
23 ----------------------------------------------------------------------------*/ |
|
24 /* |
|
25 * Program: RFC-822 routines (originally from SMTP) |
|
26 * |
|
27 * Author: Mark Crispin |
|
28 * Networks and Distributed Computing |
|
29 * Computing & Communications |
|
30 * University of Washington |
|
31 * Administration Building, AG-44 |
|
32 * Seattle, WA 98195 |
|
33 * Internet: MRC@CAC.Washington.EDU |
|
34 * |
|
35 * Date: 27 July 1988 |
|
36 * Last Edited: 10 September 1998 |
|
37 * |
|
38 * Sponsorship: The original version of this work was developed in the |
|
39 * Symbolic Systems Resources Group of the Knowledge Systems |
|
40 * Laboratory at Stanford University in 1987-88, and was funded |
|
41 * by the Biomedical Research Technology Program of the National |
|
42 * Institutes of Health under grant number RR-00785. |
|
43 * |
|
44 * Original version Copyright 1988 by The Leland Stanford Junior University |
|
45 * Copyright 1998 by the University of Washington |
|
46 * |
|
47 * Permission to use, copy, modify, and distribute this software and its |
|
48 * documentation for any purpose and without fee is hereby granted, provided |
|
49 * that the above copyright notices appear in all copies and that both the |
|
50 * above copyright notices and this permission notice appear in supporting |
|
51 * documentation, and that the name of the University of Washington or The |
|
52 * Leland Stanford Junior University not be used in advertising or publicity |
|
53 * pertaining to distribution of the software without specific, written prior |
|
54 * permission. This software is made available "as is", and |
|
55 * THE UNIVERSITY OF WASHINGTON AND THE LELAND STANFORD JUNIOR UNIVERSITY |
|
56 * DISCLAIM ALL WARRANTIES, EXPRESS OR IMPLIED, WITH REGARD TO THIS SOFTWARE, |
|
57 * INCLUDING WITHOUT LIMITATION ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
|
58 * FITNESS FOR A PARTICULAR PURPOSE, AND IN NO EVENT SHALL THE UNIVERSITY OF |
|
59 * WASHINGTON OR THE LELAND STANFORD JUNIOR UNIVERSITY BE LIABLE FOR ANY |
|
60 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER |
|
61 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF |
|
62 * CONTRACT, TORT (INCLUDING NEGLIGENCE) OR STRICT LIABILITY, ARISING OUT OF |
|
63 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
|
64 * |
|
65 */ |
|
66 |
|
67 #include <ctype.h> |
|
68 #include <stdio.h> |
|
69 #include <time.h> |
|
70 #include <stdlib.h> |
|
71 |
|
72 #include "base64.h" |
|
73 |
|
74 /* NOTE: This is not true RFC822 anymore. The use of the characters |
|
75 '/', '+', and '=' is no bueno when the ID will be used as part of a URL. |
|
76 '_', '.', and '-' have been used instead |
|
77 */ |
|
78 |
|
79 /* Convert binary contents to BASE64 |
|
80 * Accepts: source |
|
81 * length of source |
|
82 * pointer to return destination length |
|
83 * Returns: destination as BASE64 |
|
84 */ |
|
85 #ifdef __SYMBIAN32__ |
|
86 EXPORT_C |
|
87 #endif |
|
88 |
|
89 |
|
90 unsigned char * |
|
91 rfc822_binary (void *src, unsigned long srcl, unsigned long *len) |
|
92 { |
|
93 unsigned char *ret, *d; |
|
94 unsigned char *s = (unsigned char *) src; |
|
95 char *v = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._"; |
|
96 unsigned long i = ((srcl + 2) / 3) * 4; |
|
97 |
|
98 *len = i += 2 * ((i / 60) + 1); |
|
99 d = ret = (unsigned char *) malloc ((size_t)++ i); |
|
100 for (i = 0; srcl; s += 3) { /* process tuplets */ |
|
101 *d++ = v[s[0] >> 2]; /* byte 1: high 6 bits (1) */ |
|
102 /* byte 2: low 2 bits (1), high 4 bits (2) */ |
|
103 *d++ = v[((s[0] << 4) + (--srcl ? (s[1] >> 4) : 0)) & 0x3f]; |
|
104 /* byte 3: low 4 bits (2), high 2 bits (3) */ |
|
105 *d++ = srcl ? v[((s[1] << 2) + (--srcl ? (s[2] >> 6) : 0)) & 0x3f] : '-'; |
|
106 /* byte 4: low 6 bits (3) */ |
|
107 *d++ = srcl ? v[s[2] & 0x3f] : '-'; |
|
108 if (srcl) |
|
109 srcl--; /* count third character if processed */ |
|
110 if ((++i) == 15) { /* output 60 characters? */ |
|
111 i = 0; /* restart line break count, insert CRLF */ |
|
112 *d++ = '\015'; |
|
113 *d++ = '\012'; |
|
114 } |
|
115 } |
|
116 *d = '\0'; /* tie off string */ |
|
117 |
|
118 return ret; /* return the resulting string */ |
|
119 } |
|