~ubuntu-branches/debian/stretch/haproxy/stretch

« back to all changes in this revision

Viewing changes to src/base64.c

  • Committer: Bazaar Package Importer
  • Author(s): Christo Buschek
  • Date: 2011-03-11 12:41:59 UTC
  • mfrom: (1.1.10 upstream)
  • Revision ID: james.westby@ubuntu.com-20110311124159-9foyp4juf1ilqipo
Tags: 1.4.13-1
* New maintainer upload (Closes: #615246)
* New upstream release
* Standards-version goes 3.9.1 (no change)
* Added patch bashism (Closes: #581109)
* Added a README.source file.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * ASCII <-> Base64 conversion as described in RFC1421.
3
3
 *
4
 
 * Copyright 2006-2008 Willy Tarreau <w@1wt.eu>
 
4
 * Copyright 2006-2010 Willy Tarreau <w@1wt.eu>
5
5
 * Copyright 2009-2010 Krzysztof Piotr Oledzki <ole@ans.pl>
6
6
 *
7
7
 * This program is free software; you can redistribute it and/or
135
135
 
136
136
        return convlen;
137
137
}
 
138
 
 
139
 
 
140
/* Converts the lower 30 bits of an integer to a 5-char base64 string. The
 
141
 * caller is responsible for ensuring that the output buffer can accept 6 bytes
 
142
 * (5 + the trailing zero). The pointer to the string is returned. The
 
143
 * conversion is performed with MSB first and in a format that can be
 
144
 * decoded with b64tos30(). This format is not padded and thus is not
 
145
 * compatible with usual base64 routines.
 
146
 */
 
147
const char *s30tob64(int in, char *out)
 
148
{
 
149
        int i;
 
150
        for (i = 0; i < 5; i++) {
 
151
                out[i] = base64tab[(in >> 24) & 0x3F];
 
152
                in <<= 6;
 
153
        }
 
154
        out[5] = '\0';
 
155
        return out;
 
156
}
 
157
 
 
158
/* Converts a 5-char base64 string encoded by s30tob64() into a 30-bit integer.
 
159
 * The caller is responsible for ensuring that the input contains at least 5
 
160
 * chars. If any unexpected character is encountered, a negative value is
 
161
 * returned.  Otherwise the decoded value is returned.
 
162
 */
 
163
int b64tos30(const char *in)
 
164
{
 
165
        int i, out;
 
166
        signed char b;
 
167
 
 
168
        out = 0;
 
169
        for (i = 0; i < 5; i++) {
 
170
                b = (signed char)in[i] - B64CMIN;
 
171
                if ((unsigned char)b > (B64CMAX - B64CMIN))
 
172
                        return -1;  /* input character out of range */
 
173
 
 
174
                b = base64rev[b] - B64BASE - 1;
 
175
                if (b < 0)          /* invalid character */
 
176
                        return -1;
 
177
 
 
178
                if (b == B64PADV)   /* padding not allowed */
 
179
                        return -1;
 
180
 
 
181
                out = (out << 6) + b;
 
182
        }
 
183
        return out;
 
184
}