annotate src/xspf/base64.c @ 993:febbc2c05d33 trunk

[svn] - A simpler version of flac_write()
author ertzing
date Tue, 01 May 2007 08:30:24 -0700
parents 85d7e55b29d2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
1 /**
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
2 * \file base64.c
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
3 * \brief base-64 conversion routines.
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
4 *
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
5 * \author Eric S. Raymond <esr@snark.thyrsus.com>.
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
6 *
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
7 * For license terms, see the file COPYING.
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
8 *
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
9 * This base 64 encoding is defined in RFC2045 section 6.8,
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
10 * "Base64 Content-Transfer-Encoding", but lines must not be broken in the
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
11 * scheme used here.
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
12 */
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
13 #include <ctype.h>
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
14
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
15
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
16 static const char base64digits[] =
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
17 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
18
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
19 #define BAD -1
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
20 static const char base64val[] = {
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
21 BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
22 BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD,
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
23 BAD,BAD,BAD,BAD, BAD,BAD,BAD,BAD, BAD,BAD,BAD, 62, BAD,BAD,BAD, 63,
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
24 52, 53, 54, 55, 56, 57, 58, 59, 60, 61,BAD,BAD, BAD,BAD,BAD,BAD,
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
25 BAD, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
26 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25,BAD, BAD,BAD,BAD,BAD,
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
27 BAD, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
28 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,BAD, BAD,BAD,BAD,BAD
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
29 };
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
30 #define DECODE64(c) (isascii(c) ? base64val[c] : BAD)
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
31
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
32 /**
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
33 * \brief Raw bytes in quasi-big-endian order to base 64 string (NUL-terminated)
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
34 *
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
35 * \param[out] out A pointer to a char to hold the converted string
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
36 * \param[in] in String to convert
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
37 * \param[in] inlen Length of the string to be converted
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
38 */
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
39 void to64frombits(unsigned char *out, const unsigned char *in, int inlen)
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
40 {
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
41 for (; inlen >= 3; inlen -= 3)
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
42 {
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
43 *out++ = base64digits[in[0] >> 2];
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
44 *out++ = base64digits[((in[0] << 4) & 0x30) | (in[1] >> 4)];
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
45 *out++ = base64digits[((in[1] << 2) & 0x3c) | (in[2] >> 6)];
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
46 *out++ = base64digits[in[2] & 0x3f];
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
47 in += 3;
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
48 }
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
49
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
50 if (inlen > 0)
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
51 {
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
52 unsigned char fragment;
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
53
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
54 *out++ = base64digits[in[0] >> 2];
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
55 fragment = (in[0] << 4) & 0x30;
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
56
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
57 if (inlen > 1)
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
58 fragment |= in[1] >> 4;
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
59
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
60 *out++ = base64digits[fragment];
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
61 *out++ = (inlen < 2) ? '=' : base64digits[(in[1] << 2) & 0x3c];
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
62 *out++ = '=';
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
63 }
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
64
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
65 *out = '\0';
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
66 }
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
67
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
68 /**
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
69 * \brief base 64 to raw bytes in quasi-big-endian order, returning count of bytes
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
70 *
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
71 * \param[out] out Where to save the converted string
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
72 * \param[in] in String to convert
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
73 * \return Number of converted bytes.
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
74 */
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
75 int from64tobits(char *out, const char *in)
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
76 {
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
77 int len = 0;
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
78 register unsigned char digit1, digit2, digit3, digit4;
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
79
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
80 if (in[0] == '+' && in[1] == ' ')
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
81 in += 2;
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
82 if (*in == '\r')
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
83 return(0);
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
84
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
85 do {
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
86 digit1 = in[0];
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
87 if (DECODE64(digit1) == BAD)
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
88 return(-1);
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
89 digit2 = in[1];
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
90 if (DECODE64(digit2) == BAD)
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
91 return(-1);
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
92 digit3 = in[2];
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
93 if (digit3 != '=' && DECODE64(digit3) == BAD)
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
94 return(-1);
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
95 digit4 = in[3];
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
96 if (digit4 != '=' && DECODE64(digit4) == BAD)
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
97 return(-1);
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
98 in += 4;
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
99 *out++ = (DECODE64(digit1) << 2) | (DECODE64(digit2) >> 4);
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
100 ++len;
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
101 if (digit3 != '=')
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
102 {
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
103 *out++ = ((DECODE64(digit2) << 4) & 0xf0) | (DECODE64(digit3) >> 2);
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
104 ++len;
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
105 if (digit4 != '=')
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
106 {
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
107 *out++ = ((DECODE64(digit3) << 6) & 0xc0) | DECODE64(digit4);
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
108 ++len;
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
109 }
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
110 }
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
111 } while (*in && *in != '\r' && digit4 != '=');
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
112
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
113 return (len);
85d7e55b29d2 [svn] - redundant metadata has been removed.
yaz
parents:
diff changeset
114 }