File: gbase64.cc

package info (click to toggle)
poppler 20.09.0-3.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 16,092 kB
  • sloc: cpp: 151,577; ansic: 30,155; python: 367; makefile: 42; sh: 36
file content (50 lines) | stat: -rw-r--r-- 1,674 bytes parent folder | download | duplicates (2)
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
//========================================================================
//
// gbase64.cc
//
// Implementation of a base64 encoder, because another one did not immediately
// avail itself.
//
// This file is licensed under the GPLv2 or later
//
// Copyright (C) 2018 Greg Knight <lyngvi@gmail.com>
//
//========================================================================

#include "gbase64.h"
#include <sstream>

static void b64encodeTriplet(char output[4], unsigned char a, unsigned char b, unsigned char c)
{
    static const char *base64table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
    output[0] = base64table[((a >> 2) & 0x3f)]; // upper 6 of first byte
    output[1] = base64table[((a << 4) & 0x30) | ((b >> 4) & 0x0f)]; // lower 2 of first byte, upper 4 of second byte
    output[2] = base64table[((b << 2) & 0x3c) | ((c >> 6) & 0x03)]; // lower 4 of second byte, upper 2 of third byte
    output[3] = base64table[((c)&0x3f)]; // lower 6 of third byte
}

std::string gbase64Encode(const void *input, size_t len)
{
    char quad[4];
    size_t pos = 0;
    std::stringstream buf;
    auto bytes = static_cast<const unsigned char *>(input);
    for (; pos + 3 <= len; pos += 3) {
        b64encodeTriplet(quad, bytes[0], bytes[1], bytes[2]);
        buf.write(&quad[0], 4);
        bytes += 3;
    }
    switch (len - pos) {
    case 1:
        b64encodeTriplet(quad, bytes[0], 0, 0);
        quad[2] = quad[3] = '=';
        buf.write(&quad[0], 4);
        break;
    case 2:
        b64encodeTriplet(quad, bytes[0], bytes[1], 0);
        quad[3] = '=';
        buf.write(&quad[0], 4);
        break;
    }
    return buf.str();
}