microsoft/onnxruntime-extensions

Public

mirrored from https://github.com/microsoft/onnxruntime-extensionsAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
debug_i

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

base/base64.cc

93lines · modeblame

a98c29f6Xavier Dupré5 years ago1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License.
3#include "base64.h"
4#include <stdexcept>
5
6const static std::string encodeLookup("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
7const static char padCharacter = '=';
8
9bool base64_encode(const std::vector<uint8_t>& input, std::string& encoded) {
10encoded.clear();
11encoded.reserve(((input.size() / 3) + (input.size() % 3 > 0)) * 4);
12uint32_t temp;
13std::vector<uint8_t>::const_iterator cursor = input.begin();
14for (size_t idx = 0; idx < input.size() / 3; idx++) {
15temp = (*cursor++) << 16; //Convert to big endian
16temp += (*cursor++) << 8;
17temp += (*cursor++);
18encoded.append(1, encodeLookup[(temp & 0x00FC0000) >> 18]);
19encoded.append(1, encodeLookup[(temp & 0x0003F000) >> 12]);
20encoded.append(1, encodeLookup[(temp & 0x00000FC0) >> 6]);
21encoded.append(1, encodeLookup[(temp & 0x0000003F)]);
22}
23switch (input.size() % 3) {
24case 1:
25temp = (*cursor++) << 16;
26encoded.append(1, encodeLookup[(temp & 0x00FC0000) >> 18]);
27encoded.append(1, encodeLookup[(temp & 0x0003F000) >> 12]);
28encoded.append(2, padCharacter);
29break;
30case 2:
31temp = (*cursor++) << 16;
32temp += (*cursor++) << 8;
33encoded.append(1, encodeLookup[(temp & 0x00FC0000) >> 18]);
34encoded.append(1, encodeLookup[(temp & 0x0003F000) >> 12]);
35encoded.append(1, encodeLookup[(temp & 0x00000FC0) >> 6]);
36encoded.append(1, padCharacter);
37break;
38}
39encoded = encoded;
40return true;
41}
42
43bool base64_decode(const std::string& input, std::vector<uint8_t>& decoded) {
44if (input.length() % 4)
45return false;
46size_t padding = 0;
47if (input.length()) {
48if (input[input.length() - 1] == padCharacter)
49padding++;
50if (input[input.length() - 2] == padCharacter)
51padding++;
52}
53
54decoded.clear();
55decoded.reserve(((input.length() / 4) * 3) - padding);
56uint32_t temp = 0;
57std::string::const_iterator cursor = input.begin();
58size_t quantumPosition;
59while (cursor != input.end()) {
60for (quantumPosition = 0; quantumPosition < 4; ++quantumPosition) {
61temp <<= 6;
62if (*cursor >= 0x41 && *cursor <= 0x5A)
63temp |= *cursor - 0x41;
64else if (*cursor >= 0x61 && *cursor <= 0x7A)
65temp |= *cursor - 0x47;
66else if (*cursor >= 0x30 && *cursor <= 0x39)
67temp |= *cursor + 0x04;
68else if (*cursor == 0x2B)
69temp |= 0x3E;
70else if (*cursor == 0x2F)
71temp |= 0x3F;
72else if (*cursor == padCharacter) {
73switch (input.end() - cursor) {
74case 1: //One pad character
75decoded.push_back((temp >> 16) & 0x000000FF);
76decoded.push_back((temp >> 8) & 0x000000FF);
77return true;
78case 2: //Two pad characters
79decoded.push_back((temp >> 10) & 0x000000FF);
80return true;
81default:
82return false;
83}
84} else
85return false;
86++cursor;
87}
88decoded.push_back((temp >> 16) & 0x000000FF);
89decoded.push_back((temp >> 8) & 0x000000FF);
90decoded.push_back((temp)&0x000000FF);
91}
92return true;
93}