Statistics
| Branch: | Tag: | Revision:

root / libsylph / base64.c @ aebfd4cc

History | View | Annotate | Download (3.49 KB)

1
/*
2
 * LibSylph -- E-Mail client library
3
 * Copyright (C) 1999-2011 Hiroyuki Yamamoto
4
 */
5

    
6
#include <glib.h>
7
#include <ctype.h>
8
#include <string.h>
9

    
10
#include "base64.h"
11

    
12
static const gchar base64char[64] =
13
        "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
14

    
15
static const gchar base64val[128] = {
16
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
17
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
18
        -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
19
        52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1,
20
        -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14,
21
        15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
22
        -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
23
        41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1
24
};
25

    
26
#define BASE64VAL(c)        (isascii((guchar)c) ? base64val[(gint)(c)] : -1)
27

    
28
void base64_encode(gchar *out, const guchar *in, gint inlen)
29
{
30
        const guchar *inp = in;
31
        gchar *outp = out;
32

    
33
        while (inlen >= 3) {
34
                *outp++ = base64char[(inp[0] >> 2) & 0x3f];
35
                *outp++ = base64char[((inp[0] & 0x03) << 4) |
36
                                     ((inp[1] >> 4) & 0x0f)];
37
                *outp++ = base64char[((inp[1] & 0x0f) << 2) |
38
                                     ((inp[2] >> 6) & 0x03)];
39
                *outp++ = base64char[inp[2] & 0x3f];
40

    
41
                inp += 3;
42
                inlen -= 3;
43
        }
44

    
45
        if (inlen > 0) {
46
                *outp++ = base64char[(inp[0] >> 2) & 0x3f];
47
                if (inlen == 1) {
48
                        *outp++ = base64char[(inp[0] & 0x03) << 4];
49
                        *outp++ = '=';
50
                } else {
51
                        *outp++ = base64char[((inp[0] & 0x03) << 4) |
52
                                             ((inp[1] >> 4) & 0x0f)];
53
                        *outp++ = base64char[((inp[1] & 0x0f) << 2)];
54
                }
55
                *outp++ = '=';
56
        }
57

    
58
        *outp = '\0';
59
}
60

    
61
gint base64_decode(guchar *out, const gchar *in, gint inlen)
62
{
63
        const gchar *inp = in;
64
        guchar *outp = out;
65
        gchar buf[4];
66

    
67
        if (inlen < 0)
68
                inlen = G_MAXINT;
69

    
70
        while (inlen >= 4 && *inp != '\0') {
71
                buf[0] = *inp++;
72
                inlen--;
73
                if (BASE64VAL(buf[0]) == -1) break;
74

    
75
                buf[1] = *inp++;
76
                inlen--;
77
                if (BASE64VAL(buf[1]) == -1) break;
78

    
79
                buf[2] = *inp++;
80
                inlen--;
81
                if (buf[2] != '=' && BASE64VAL(buf[2]) == -1) break;
82

    
83
                buf[3] = *inp++;
84
                inlen--;
85
                if (buf[3] != '=' && BASE64VAL(buf[3]) == -1) break;
86

    
87
                *outp++ = ((BASE64VAL(buf[0]) << 2) & 0xfc) |
88
                          ((BASE64VAL(buf[1]) >> 4) & 0x03);
89
                if (buf[2] != '=') {
90
                        *outp++ = ((BASE64VAL(buf[1]) & 0x0f) << 4) |
91
                                  ((BASE64VAL(buf[2]) >> 2) & 0x0f);
92
                        if (buf[3] != '=') {
93
                                *outp++ = ((BASE64VAL(buf[2]) & 0x03) << 6) |
94
                                           (BASE64VAL(buf[3]) & 0x3f);
95
                        }
96
                }
97
        }
98

    
99
        return outp - out;
100
}
101

    
102
Base64Decoder *base64_decoder_new(void)
103
{
104
        Base64Decoder *decoder;
105

    
106
        decoder = g_new0(Base64Decoder, 1);
107
        return decoder;
108
}
109

    
110
void base64_decoder_free(Base64Decoder *decoder)
111
{
112
        g_free(decoder);
113
}
114

    
115
gint base64_decoder_decode(Base64Decoder *decoder,
116
                           const gchar *in, guchar *out)
117
{
118
        gint len, total_len = 0;
119
        gint buf_len;
120
        gchar buf[4];
121

    
122
        g_return_val_if_fail(decoder != NULL, -1);
123
        g_return_val_if_fail(in != NULL, -1);
124
        g_return_val_if_fail(out != NULL, -1);
125

    
126
        buf_len = decoder->buf_len;
127
        memcpy(buf, decoder->buf, sizeof(buf));
128

    
129
        for (;;) {
130
                while (buf_len < 4) {
131
                        gchar c = *in;
132

    
133
                        in++;
134
                        if (c == '\0') break;
135
                        if (c == '\r' || c == '\n') continue;
136
                        if (c != '=' && BASE64VAL(c) == -1)
137
                                return -1;
138
                        buf[buf_len++] = c;
139
                }
140
                if (buf_len < 4 || buf[0] == '=' || buf[1] == '=') {
141
                        decoder->buf_len = buf_len;
142
                        memcpy(decoder->buf, buf, sizeof(buf));
143
                        return total_len;
144
                }
145
                len = base64_decode(out, buf, 4);
146
                out += len;
147
                total_len += len;
148
                buf_len = 0;
149
                if (len < 3) {
150
                        decoder->buf_len = 0;
151
                        return total_len;
152
                }
153
        }
154
}