File: base64.c

package info (click to toggle)
squid3 3.4.8-6
  • links: PTS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 31,084 kB
  • sloc: cpp: 165,325; ansic: 21,998; sh: 12,166; makefile: 5,964; perl: 2,153; sql: 322; awk: 118
file content (211 lines) | stat: -rw-r--r-- 5,885 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 * AUTHOR: Markus Moeller
 *
 * Encoders adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c with adjustments.
 */

#include "squid.h"
#include "base64.h"

#if HAVE_STDIO_H
#include <stdio.h>
#endif
#if HAVE_STDLIB_H
#include <stdlib.h>
#endif

static void base64_init(void);

static int base64_initialized = 0;
#define BASE64_VALUE_SZ 256
#define BASE64_RESULT_SZ 8192
int base64_value[BASE64_VALUE_SZ];
const char base64_code[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

static void
base64_init(void)
{
    int i;

    for (i = 0; i < BASE64_VALUE_SZ; i++)
        base64_value[i] = -1;

    for (i = 0; i < 64; i++)
        base64_value[(int) base64_code[i]] = i;
    base64_value['='] = 0;

    base64_initialized = 1;
}

int
base64_decode_len(const char *data)
{
    if (!data || !*data)
        return 0;

    int terminatorLen = 0;
    int dataLen = strlen(data);
    int i;

    for (i = dataLen - 1; i >= 0; i--) {
        if (data[i] == '=')
            terminatorLen++;
        if (data[i] != '=')
            break;
    }
    return dataLen / 4 * 3 - terminatorLen;
}

int
base64_decode(char *result, unsigned int result_size, const char *p)
{
    int j = 0;
    int c;
    long val;
    if (!p || !result || result_size == 0)
        return j;
    if (!base64_initialized)
        base64_init();
    val = c = 0;
    for (; *p; p++) {
        unsigned int k = ((unsigned char) *p) % BASE64_VALUE_SZ;
        if (base64_value[k] < 0)
            continue;
        val <<= 6;
        val += base64_value[k];
        if (++c < 4)
            continue;
        /* One quantum of four encoding characters/24 bit */
        if (j+4 <= result_size) {
            // Speed optimization: plenty of space, avoid some per-byte checks.
            result[j++] = (val >> 16) & 0xff;	/* High 8 bits */
            result[j++] = (val >> 8) & 0xff;	/* Mid 8 bits */
            result[j++] = val & 0xff;		/* Low 8 bits */
        } else {
            // part-quantum goes a bit slower with per-byte checks
            result[j++] = (val >> 16) & 0xff;	/* High 8 bits */
            if (j == result_size)
                return j;
            result[j++] = (val >> 8) & 0xff;	/* Mid 8 bits */
            if (j == result_size)
                return j;
            result[j++] = val & 0xff;		/* Low 8 bits */
        }
        if (j == result_size)
            return j;
        val = c = 0;
    }
    return j;
}

int
base64_encode_len(int len)
{
    // NP: some magic numbers + potential nil-terminator
    return ((len + 2) / 3 * 4) + 1;
}

const char *
old_base64_encode(const char *decoded_str)
{
    static char result[BASE64_RESULT_SZ];
    base64_encode_str(result, sizeof(result), decoded_str, strlen(decoded_str));
    return result;
}

const char *
base64_encode_bin(const char *decoded_str, int len)
{
    static char result[BASE64_RESULT_SZ];
    base64_encode_str(result, sizeof(result), decoded_str, len);
    return result;
}

int
base64_encode_str(char *result, int result_max_size, const char *data, int data_size)
{
    if (result_max_size < 1)
        return 0;

    int used = base64_encode(result, result_max_size, data, data_size);
    /* terminate */
    if (used >= result_max_size) {
        result[result_max_size - 1] = '\0';
        return result_max_size;
    } else {
        result[used++] = '\0';
    }
    return used;
}

/* adopted from http://ftp.sunet.se/pub2/gnu/vm/base64-encode.c with adjustments */
int
base64_encode(char *result, int result_size, const char *data, int data_size)
{
    int bits = 0;
    int char_count = 0;
    int out_cnt = 0;

    if (!data || !*data || !result || result_size < 1 || data_size < 1)
        return 0;

    if (!base64_initialized)
        base64_init();

    while (data_size--) {
        int c = (unsigned char) *data++;
        bits += c;
        char_count++;
        if (char_count == 3) {
            if (out_cnt >= result_size)
                break;
            if (out_cnt+4 <= result_size) {
                result[out_cnt++] = base64_code[bits >> 18];
                result[out_cnt++] = base64_code[(bits >> 12) & 0x3f];
                result[out_cnt++] = base64_code[(bits >> 6) & 0x3f];
                result[out_cnt++] = base64_code[bits & 0x3f];
            } else {
                // part-quantum goes a bit slower with per-byte checks
                result[out_cnt++] = base64_code[bits >> 18];
                if (out_cnt >= result_size)
                    break;
                result[out_cnt++] = base64_code[(bits >> 12) & 0x3f];
                if (out_cnt >= result_size)
                    break;
                result[out_cnt++] = base64_code[(bits >> 6) & 0x3f];
                if (out_cnt >= result_size)
                    break;
                result[out_cnt++] = base64_code[bits & 0x3f];
            }
            bits = 0;
            char_count = 0;
        } else {
            bits <<= 8;
        }
    }
    if (char_count != 0) {
        bits <<= 16 - (8 * char_count);
        if (out_cnt >= result_size)
            return result_size;
        result[out_cnt++] = base64_code[bits >> 18];
        if (out_cnt >= result_size)
            return result_size;
        result[out_cnt++] = base64_code[(bits >> 12) & 0x3f];
        if (char_count == 1) {
            if (out_cnt >= result_size)
                return result_size;
            result[out_cnt++] = '=';
            if (out_cnt >= result_size)
                return result_size;
            result[out_cnt++] = '=';
        } else {
            if (out_cnt >= result_size)
                return result_size;
            result[out_cnt++] = base64_code[(bits >> 6) & 0x3f];
            if (out_cnt >= result_size)
                return result_size;
            result[out_cnt++] = '=';
        }
    }
    return (out_cnt >= result_size?result_size:out_cnt);
}