GNU Radio's SATNOGS Package
base64.h
Go to the documentation of this file.
1 /* -*- c++ -*- */
2 /*
3  * gr-satnogs: SatNOGS GNU Radio Out-Of-Tree Module
4  *
5  * Copyright (C) 2019, Libre Space Foundation <http://libre.space>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 /*
22  base64.cpp and base64.h
23  base64 encoding and decoding with C++.
24  Version: 1.01.00
25  Copyright (C) 2004-2017 RenĂ© Nyffenegger
26  This source code is provided 'as-is', without any express or implied
27  warranty. In no event will the author be held liable for any damages
28  arising from the use of this software.
29  Permission is granted to anyone to use this software for any purpose,
30  including commercial applications, and to alter it and redistribute it
31  freely, subject to the following restrictions:
32  1. The origin of this source code must not be misrepresented; you must not
33  claim that you wrote the original source code. If you use this source code
34  in a product, an acknowledgment in the product documentation would be
35  appreciated but is not required.
36  2. Altered source versions must be plainly marked as such, and must not be
37  misrepresented as being the original source code.
38  3. This notice may not be removed or altered from any source distribution.
39  RenĂ© Nyffenegger rene.nyffenegger@adp-gmbh.ch
40  */
41 
42 #ifndef INCLUDE_SATNOGS_BASE64_H_
43 #define INCLUDE_SATNOGS_BASE64_H_
44 
45 #include <iostream>
46 
47 static const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
48  "abcdefghijklmnopqrstuvwxyz"
49  "0123456789+/";
50 
51 static inline bool
52 is_base64(unsigned char c)
53 {
54  return (isalnum(c) || (c == '+') || (c == '/'));
55 }
56 
57 static std::string
58 base64_encode(unsigned char const *bytes_to_encode, unsigned int in_len)
59 {
60  std::string ret;
61  int i = 0;
62  int j = 0;
63  unsigned char char_array_3[3];
64  unsigned char char_array_4[4];
65 
66  while (in_len--) {
67  char_array_3[i++] = *(bytes_to_encode++);
68  if (i == 3) {
69  char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
70  char_array_4[1] = ((char_array_3[0] & 0x03) << 4)
71  + ((char_array_3[1] & 0xf0) >> 4);
72  char_array_4[2] = ((char_array_3[1] & 0x0f) << 2)
73  + ((char_array_3[2] & 0xc0) >> 6);
74  char_array_4[3] = char_array_3[2] & 0x3f;
75 
76  for (i = 0; (i < 4); i++) {
77  ret += base64_chars[char_array_4[i]];
78  }
79  i = 0;
80  }
81  }
82 
83  if (i) {
84  for (j = i; j < 3; j++) {
85  char_array_3[j] = '\0';
86  }
87 
88  char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
89  char_array_4[1] = ((char_array_3[0] & 0x03) << 4)
90  + ((char_array_3[1] & 0xf0) >> 4);
91  char_array_4[2] = ((char_array_3[1] & 0x0f) << 2)
92  + ((char_array_3[2] & 0xc0) >> 6);
93 
94  for (j = 0; (j < i + 1); j++) {
95  ret += base64_chars[char_array_4[j]];
96  }
97 
98  while ((i++ < 3)) {
99  ret += '=';
100  }
101 
102  }
103 
104  return ret;
105 
106 }
107 
108 static std::string
109 base64_decode(std::string const &encoded_string)
110 {
111  size_t in_len = encoded_string.size();
112  int i = 0;
113  int j = 0;
114  int in_ = 0;
115  unsigned char char_array_4[4], char_array_3[3];
116  std::string ret;
117 
118  while (in_len-- && (encoded_string[in_] != '=')
119  && is_base64(encoded_string[in_])) {
120  char_array_4[i++] = encoded_string[in_];
121  in_++;
122  if (i == 4) {
123  for (i = 0; i < 4; i++) {
124  char_array_4[i] = base64_chars.find(char_array_4[i]) & 0xff;
125  }
126 
127  char_array_3[0] = (char_array_4[0] << 2)
128  + ((char_array_4[1] & 0x30) >> 4);
129  char_array_3[1] = ((char_array_4[1] & 0xf) << 4)
130  + ((char_array_4[2] & 0x3c) >> 2);
131  char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];
132 
133  for (i = 0; (i < 3); i++) {
134  ret += char_array_3[i];
135  }
136  i = 0;
137  }
138  }
139 
140  if (i) {
141  for (j = 0; j < i; j++) {
142  char_array_4[j] = base64_chars.find(char_array_4[j]) & 0xff;
143  }
144 
145  char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
146  char_array_3[1] = ((char_array_4[1] & 0xf) << 4)
147  + ((char_array_4[2] & 0x3c) >> 2);
148 
149  for (j = 0; (j < i - 1); j++) {
150  ret += char_array_3[j];
151  }
152  }
153 
154  return ret;
155 }
156 
157 #endif /* INCLUDE_SATNOGS_BASE64_H_ */
int i
Definition: decode_rs.h:71
int j
Definition: decode_rs.h:71
static bool is_base64(unsigned char c)
Definition: base64.h:52
static const std::string base64_chars
Definition: base64.h:47
static std::string base64_encode(unsigned char const *bytes_to_encode, unsigned int in_len)
Definition: base64.h:58
static std::string base64_decode(std::string const &encoded_string)
Definition: base64.h:109