Skip to content

Commit 1db7199

Browse files
committed
External base64 library.
1 parent 1950ce6 commit 1db7199

File tree

11 files changed

+655
-0
lines changed

11 files changed

+655
-0
lines changed

ext/base64/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
add_library(BASE64 INTERFACE)
2+
3+
target_include_directories(BASE64 INTERFACE src)
4+
5+
zephyr_library()
6+
target_include_directories(BASE64 INTERFACE
7+
include
8+
port/zephyr/include
9+
)
10+
11+
zephyr_library_sources(
12+
src/base64.c
13+
src/hex.c
14+
)
15+
16+
zephyr_library_link_libraries(BASE64)
17+
target_link_libraries(BASE64 INTERFACE zephyr_interface)

ext/base64/include/base64/base64.h

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
#ifndef __UTIL_BASE64_H
20+
#define __UTIL_BASE64_H
21+
22+
#include <stdint.h>
23+
#include <string.h>
24+
25+
#ifdef __cplusplus
26+
extern "C" {
27+
#endif
28+
29+
int base64_encode(const void *, int, char *, uint8_t);
30+
int base64_decode(const char *, void *buf);
31+
int base64_pad(char *, int);
32+
int base64_decode_len(const char *str);
33+
34+
#define BASE64_ENCODE_SIZE(__size) (((((__size) - 1) / 3) * 4) + 4)
35+
36+
#ifdef __cplusplus
37+
}
38+
#endif
39+
40+
#endif /* __UTIL_BASE64_H__ */

ext/base64/include/base64/hex.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
#ifndef _UTIL_HEX_H_
20+
#define _UTIL_HEX_H_
21+
22+
#ifdef __cplusplus
23+
extern "C" {
24+
#endif
25+
26+
char *hex_format(void *src_v, int src_len, char *dst, int dst_len);
27+
int hex_parse(const char *src, int src_len, void *dst_v, int dst_len);
28+
29+
#ifdef __cplusplus
30+
}
31+
#endif
32+
33+
#endif /* _UTIL_HEX_H_ */

ext/base64/pkg.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one
3+
# or more contributor license agreements. See the NOTICE file
4+
# distributed with this work for additional information
5+
# regarding copyright ownership. The ASF licenses this file
6+
# to you under the Apache License, Version 2.0 (the
7+
# "License"); you may not use this file except in compliance
8+
# with the License. You may obtain a copy of the License at
9+
#
10+
# http://www.apache.org/licenses/LICENSE-2.0
11+
#
12+
# Unless required by applicable law or agreed to in writing,
13+
# software distributed under the License is distributed on an
14+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
# KIND, either express or implied. See the License for the
16+
# specific language governing permissions and limitations
17+
# under the License.
18+
#
19+
20+
pkg.name: ext/base64
21+
pkg.description: Libary for base64 encoding and decoding.
22+
pkg.author: "Apache Mynewt <[email protected]>"
23+
pkg.homepage: "http://mynewt.apache.org/"
24+
pkg.keywords:
25+
- base64
26+
- hex

ext/base64/src/base64.c

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
/*
2+
* This file is based on roken from the FreeBSD source. It has been modified
3+
* to not use malloc() and instead expect static buffers, and tabs have been
4+
* replaced with spaces. Also, instead of strlen() on the resulting string,
5+
* pointer arithmitic is done, as p represents the end of the buffer.
6+
*/
7+
8+
/*
9+
* Copyright (c) 1995-2001 Kungliga Tekniska Högskolan
10+
* (Royal Institute of Technology, Stockholm, Sweden).
11+
* All rights reserved.
12+
*
13+
* Redistribution and use in source and binary forms, with or without
14+
* modification, are permitted provided that the following conditions
15+
* are met:
16+
*
17+
* 1. Redistributions of source code must retain the above copyright
18+
* notice, this list of conditions and the following disclaimer.
19+
*
20+
* 2. Redistributions in binary form must reproduce the above copyright
21+
* notice, this list of conditions and the following disclaimer in the
22+
* documentation and/or other materials provided with the distribution.
23+
*
24+
* 3. Neither the name of the Institute nor the names of its contributors
25+
* may be used to endorse or promote products derived from this software
26+
* without specific prior written permission.
27+
*
28+
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
29+
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31+
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
32+
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33+
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34+
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35+
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36+
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37+
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38+
* SUCH DAMAGE.
39+
*/
40+
41+
#include <stdlib.h>
42+
#include <string.h>
43+
44+
#include <stdio.h>
45+
46+
#include <base64/base64.h>
47+
48+
static const char base64_chars[] =
49+
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
50+
51+
static int
52+
pos(char c)
53+
{
54+
const char *p;
55+
for (p = base64_chars; *p; p++)
56+
if (*p == c)
57+
return p - base64_chars;
58+
return -1;
59+
}
60+
61+
int
62+
base64_encode(const void *data, int size, char *s, uint8_t should_pad)
63+
{
64+
char *p;
65+
int i;
66+
int c;
67+
const unsigned char *q;
68+
char *last;
69+
int diff;
70+
71+
p = s;
72+
73+
q = (const unsigned char *) data;
74+
last = NULL;
75+
i = 0;
76+
while (i < size) {
77+
c = q[i++];
78+
c *= 256;
79+
if (i < size)
80+
c += q[i];
81+
i++;
82+
c *= 256;
83+
if (i < size)
84+
c += q[i];
85+
i++;
86+
p[0] = base64_chars[(c & 0x00fc0000) >> 18];
87+
p[1] = base64_chars[(c & 0x0003f000) >> 12];
88+
p[2] = base64_chars[(c & 0x00000fc0) >> 6];
89+
p[3] = base64_chars[(c & 0x0000003f) >> 0];
90+
last = p;
91+
p += 4;
92+
}
93+
94+
if (last) {
95+
diff = i - size;
96+
if (diff > 0) {
97+
if (should_pad) {
98+
memset(last + (4 - diff), '=', diff);
99+
} else {
100+
p = last + (4 - diff);
101+
}
102+
}
103+
}
104+
105+
*p = 0;
106+
107+
return (p - s);
108+
}
109+
110+
int
111+
base64_pad(char *buf, int len)
112+
{
113+
int remainder;
114+
115+
remainder = len % 4;
116+
if (remainder == 0) {
117+
return (0);
118+
}
119+
120+
memset(buf, '=', 4 - remainder);
121+
122+
return (4 - remainder);
123+
}
124+
125+
#define DECODE_ERROR -1
126+
127+
static unsigned int
128+
token_decode(const char *token)
129+
{
130+
int i;
131+
unsigned int val = 0;
132+
int marker = 0;
133+
if (strlen(token) < 4)
134+
return DECODE_ERROR;
135+
for (i = 0; i < 4; i++) {
136+
val *= 64;
137+
if (token[i] == '=')
138+
marker++;
139+
else if (marker > 0)
140+
return DECODE_ERROR;
141+
else
142+
val += pos(token[i]);
143+
}
144+
if (marker > 2)
145+
return DECODE_ERROR;
146+
return (marker << 24) | val;
147+
}
148+
149+
int
150+
base64_decode(const char *str, void *data)
151+
{
152+
const char *p;
153+
unsigned char *q;
154+
155+
q = data;
156+
for (p = str; *p && (*p == '=' || strchr(base64_chars, *p)); p += 4) {
157+
unsigned int val = token_decode(p);
158+
unsigned int marker = (val >> 24) & 0xff;
159+
if (val == DECODE_ERROR)
160+
return -1;
161+
*q++ = (val >> 16) & 0xff;
162+
if (marker < 2)
163+
*q++ = (val >> 8) & 0xff;
164+
if (marker < 1)
165+
*q++ = val & 0xff;
166+
}
167+
return q - (unsigned char *) data;
168+
}
169+
170+
171+
int
172+
base64_decode_len(const char *str)
173+
{
174+
int len;
175+
176+
len = strlen(str);
177+
while (len && str[len - 1] == '=') {
178+
len--;
179+
}
180+
return len * 3 / 4;
181+
}

0 commit comments

Comments
 (0)