LIMA
Libre Multilingual Analyzer — C++ API
Loading...
Searching...
No Matches
base64.cpp
Go to the documentation of this file.
1// Copyright 2002-2013 CEA LIST
2// SPDX-FileCopyrightText: 2022 CEA LIST <gael.de-chalendar@cea.fr>
3//
4// SPDX-License-Identifier: MIT
5
6/*
7 base64.cpp and base64.h
8
9 Copyright (C) 2004-2008 René Nyffenegger
10
11 This source code is provided 'as-is', without any express or implied
12 warranty. In no event will the author be held liable for any damages
13 arising from the use of this software.
14
15 Permission is granted to anyone to use this software for any purpose,
16 including commercial applications, and to alter it and redistribute it
17 freely, subject to the following restrictions:
18
19 1. The origin of this source code must not be misrepresented; you must not
20 claim that you wrote the original source code. If you use this source code
21 in a product, an acknowledgment in the product documentation would be
22 appreciated but is not required.
23
24 2. Altered source versions must be plainly marked as such, and must not be
25 misrepresented as being the original source code.
26
27 3. This notice may not be removed or altered from any source distribution.
28
29 René Nyffenegger rene.nyffenegger@adp-gmbh.ch
30
31*/
32/***************************************************************************
33 * Copyright (C) 2008-2012 by CEA LIST *
34 * Changes by G. de Chalendar to handle strings directly *
35 ***************************************************************************/
36
37#include "base64.h"
38#include <iostream>
39
40static const std::string base64_chars =
41 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
42 "abcdefghijklmnopqrstuvwxyz"
43 "0123456789+/";
44
45
46static inline bool is_base64(uint8_t c) {
47 return (isalnum(c) || (c == '+') || (c == '/'));
48}
49
50std::string base64_encode(const std::string& s)
51{
52 uint8_t const*bytes_to_encode = reinterpret_cast<const uint8_t*>(s.c_str());
53 uint32_t in_len = s.length();
54 std::string ret;
55 int i = 0;
56 int j = 0;
57 uint8_t char_array_3[3];
58 uint8_t char_array_4[4];
59
60 while (in_len--) {
61 char_array_3[i++] = *(bytes_to_encode++);
62 if (i == 3) {
63 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
64 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
65 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
66 char_array_4[3] = char_array_3[2] & 0x3f;
67
68 for(i = 0; (i <4) ; i++)
69 ret += base64_chars[char_array_4[i]];
70 i = 0;
71 }
72 }
73
74 if (i)
75 {
76 for(j = i; j < 3; j++)
77 char_array_3[j] = ' ';
78
79 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
80 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
81 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
82 char_array_4[3] = char_array_3[2] & 0x3f;
83
84 for (j = 0; (j < i + 1); j++)
85 ret += base64_chars[char_array_4[j]];
86
87 while((i++ < 3))
88 ret += '=';
89
90 }
91
92 return ret;
93
94}
95
96std::string base64_decode(std::string const& encoded_string) {
97 int in_len = encoded_string.size();
98 int i = 0;
99 int j = 0;
100 int in_ = 0;
101 uint8_t char_array_4[4], char_array_3[3];
102 std::string ret;
103
104 while (in_len-- && ( encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
105 char_array_4[i++] = encoded_string[in_]; in_++;
106 if (i ==4) {
107 for (i = 0; i <4; i++)
108 char_array_4[i] = base64_chars.find(char_array_4[i]);
109
110 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
111 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
112 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
113
114 for (i = 0; (i < 3); i++)
115 ret += char_array_3[i];
116 i = 0;
117 }
118 }
119
120 if (i) {
121 for (j = i; j <4; j++)
122 char_array_4[j] = 0;
123
124 for (j = 0; j <4; j++)
125 char_array_4[j] = base64_chars.find(char_array_4[j]);
126
127 char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
128 char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
129 char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
130
131 for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
132 }
133
134 return ret;
135}
std::string base64_encode(const std::string &s)
Definition base64.cpp:50
static bool is_base64(uint8_t c)
Definition base64.cpp:46
std::string base64_decode(std::string const &encoded_string)
Definition base64.cpp:96
static const std::string base64_chars
Definition base64.cpp:40