Skip to content

Commit af1d7ab

Browse files
committed
move base64 code into own file
it will be needed to add support for upstream proxy auth.
1 parent bd04ed0 commit af1d7ab

4 files changed

Lines changed: 88 additions & 35 deletions

File tree

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ tinyproxy_SOURCES = \
4848
vector.c vector.h \
4949
upstream.c upstream.h \
5050
basicauth.c basicauth.h \
51+
base64.c base64.h \
5152
connect-ports.c connect-ports.h
5253

5354
EXTRA_tinyproxy_SOURCES = filter.c filter.h \

src/base64.c

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* tinyproxy - A fast light-weight HTTP proxy
2+
* this file Copyright (C) 2016-2018 rofl0r
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License along
15+
* with this program; if not, write to the Free Software Foundation, Inc.,
16+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17+
*/
18+
19+
#include "base64.h"
20+
21+
static const char base64_tbl[64] =
22+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
23+
24+
/*
25+
rofl0r's base64 impl (taken from libulz)
26+
takes count bytes from src, writing base64 encoded string into dst.
27+
dst needs to be at least BASE64ENC_BYTES(count) + 1 bytes in size.
28+
the string in dst will be zero-terminated.
29+
*/
30+
void base64enc(char *dst, const void* src, size_t count)
31+
{
32+
unsigned const char *s = src;
33+
char* d = dst;
34+
while(count) {
35+
int i = 0, n = *s << 16;
36+
s++;
37+
count--;
38+
if(count) {
39+
n |= *s << 8;
40+
s++;
41+
count--;
42+
i++;
43+
}
44+
if(count) {
45+
n |= *s;
46+
s++;
47+
count--;
48+
i++;
49+
}
50+
*d++ = base64_tbl[(n >> 18) & 0x3f];
51+
*d++ = base64_tbl[(n >> 12) & 0x3f];
52+
*d++ = i ? base64_tbl[(n >> 6) & 0x3f] : '=';
53+
*d++ = i == 2 ? base64_tbl[n & 0x3f] : '=';
54+
}
55+
*d = 0;
56+
}
57+

src/base64.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* tinyproxy - A fast light-weight HTTP proxy
2+
* this file Copyright (C) 2016-2018 rofl0r
3+
*
4+
* This program is free software; you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation; either version 2 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License along
15+
* with this program; if not, write to the Free Software Foundation, Inc.,
16+
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17+
*/
18+
19+
#ifndef TINYPROXY_BASE64_H
20+
#define TINYPROXY_BASE64_H
21+
22+
#include <stddef.h>
23+
24+
/* calculates number of bytes base64-encoded stream of N bytes will take. */
25+
#define BASE64ENC_BYTES(N) (((N+2)/3)*4)
26+
void base64enc(char *dst, const void* src, size_t count);
27+
28+
#endif
29+

src/basicauth.c

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -24,41 +24,7 @@
2424
#include "html-error.h"
2525
#include "log.h"
2626
#include "conf.h"
27-
28-
/* calculates number of bytes base64-encoded stream of N bytes will take. */
29-
#define BASE64ENC_BYTES(N) (((N+2)/3)*4)
30-
31-
static const char base64_tbl[64] =
32-
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
33-
34-
/* my own base64 impl (taken from libulz) */
35-
static void base64enc(char *dst, const void* src, size_t count)
36-
{
37-
unsigned const char *s = src;
38-
char* d = dst;
39-
while(count) {
40-
int i = 0, n = *s << 16;
41-
s++;
42-
count--;
43-
if(count) {
44-
n |= *s << 8;
45-
s++;
46-
count--;
47-
i++;
48-
}
49-
if(count) {
50-
n |= *s;
51-
s++;
52-
count--;
53-
i++;
54-
}
55-
*d++ = base64_tbl[(n >> 18) & 0x3f];
56-
*d++ = base64_tbl[(n >> 12) & 0x3f];
57-
*d++ = i ? base64_tbl[(n >> 6) & 0x3f] : '=';
58-
*d++ = i == 2 ? base64_tbl[n & 0x3f] : '=';
59-
}
60-
*d = 0;
61-
}
27+
#include "base64.h"
6228

6329
/*
6430
* Add entry to the basicauth list

0 commit comments

Comments
 (0)