File: base64.hpp

package info (click to toggle)
higan 098-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 11,904 kB
  • ctags: 13,286
  • sloc: cpp: 108,285; ansic: 778; makefile: 32; sh: 18
file content (67 lines) | stat: -rw-r--r-- 1,688 bytes parent folder | download
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
#pragma once

namespace nall { namespace Encode {

inline auto Base64(const uint8_t* data, unsigned size, const string& format = "MIME") -> string {
  vector<uint8_t> result;

  char lookup[65];
  for(unsigned n = 0; n < 26; n++) lookup[ 0 + n] = 'A' + n;
  for(unsigned n = 0; n < 26; n++) lookup[26 + n] = 'a' + n;
  for(unsigned n = 0; n < 10; n++) lookup[52 + n] = '0' + n;

  if(format == "MIME") {
    lookup[62] = '+';
    lookup[63] = '/';
    lookup[64] = '=';
  } else if(format == "URI") {
    lookup[62] = '-';
    lookup[63] = '_';
    lookup[64] = 0;
  } else return "";

  unsigned overflow = (3 - (size % 3)) % 3;  //bytes to round to nearest multiple of 3
  uint8_t buffer;

  for(unsigned i = 0; i < size; i++) {
    switch(i % 3) {
    case 0:
      buffer = data[i] >> 2;
      result.append(lookup[buffer]);
      buffer = (data[i] & 3) << 4;
      result.append(lookup[buffer]);
      break;

    case 1:
      buffer |= data[i] >> 4;
      result.last() = lookup[buffer];
      buffer = (data[i] & 15) << 2;
      result.append(lookup[buffer]);
      break;

    case 2:
      buffer |= data[i] >> 6;
      result.last() = lookup[buffer];
      buffer = (data[i] & 63);
      result.append(lookup[buffer]);
      break;
    }
  }

  if(lookup[64]) {
    if(overflow >= 1) result.append(lookup[64]);
    if(overflow >= 2) result.append(lookup[64]);
  }

  return result;
}

inline auto Base64(const vector<uint8_t>& buffer, const string& format = "MIME") -> string {
  return Base64(buffer.data(), buffer.size(), format);
}

inline auto Base64(const string& text, const string& format = "MIME") -> string {
  return Base64(text.binary(), text.size(), format);
}

}}