-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase64.cc
58 lines (52 loc) · 1.99 KB
/
base64.cc
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
/*
* This is port of OpenHttpStreamer for win32
* copyright (c) 2011 [email protected]
*
* Originally:
* copyright (c) 2010 ZAO Inventos (inventos.ru)
* copyright (c) 2010 [email protected]
* copyright (c) 2010 [email protected]
* copyright (c) 2010 [email protected]
*
* This file is part of mp4frag.
*
* mp4grag is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* mp4frag is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#include "base64.hh"
namespace base64 {
namespace {
const char letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
}
void encode(std::streambuf *buf, const char *bytes, size_t size) {
unsigned rest = size % 3;
const char *limit = bytes + size - rest;
for ( const char *ptr = bytes; ptr < limit; ptr += 3 ) {
buf->sputc(letters[ (ptr[0] >> 2) & 0x3F ]);
buf->sputc(letters[ ((ptr[0] << 4) & 0x30) | ((ptr[1] >> 4) & 0xF) ]);
buf->sputc(letters[ ((ptr[1] << 2) & 0x3C) | ((ptr[2] >> 6) & 0x3) ]);
buf->sputc(letters[ ptr[2] & 0x3F ]);
}
switch ( rest ) {
case 1:
buf->sputc(letters[ (limit[0] >> 2) & 0x3F ]);
buf->sputc(letters[(limit[0] << 4) & 0x30]);
buf->sputc('=');
buf->sputc('=');
break;
case 2:
buf->sputc(letters[ (limit[0] >> 2) & 0x3F ]);
buf->sputc(letters[((limit[0] << 4) & 0x30) | ((limit[1] >> 4) & 0xF)]);
buf->sputc(letters[(limit[1] << 2) & 0x3C]);
buf->sputc('=');
break;
}
}
}