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