mirror of
https://github.com/signalwire/freeswitch.git
synced 2026-11-15 22:08:33 +00:00
FS-9952: Add base64 code (from verto)
This commit is contained in:
1 parent
59b1ab829e
commit
2bce38afce
3 files changed
+186
-2
No files matched your search
@@ -0,0 +1,54 @@
|
||||
/*
|
||||
* Copyright (c) 2017 FreeSWITCH Solutions LLC
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of the original author; nor the names of any contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
||||
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#ifndef _KS_BASE64_H__
|
||||
#define _KS_BASE64_H__
|
||||
|
||||
#include <ks.h>
|
||||
|
||||
KS_DECLARE(ks_status_t) ks_b64_encode(unsigned char *in, ks_size_t ilen, unsigned char *out, ks_size_t olen);
|
||||
KS_DECLARE(ks_size_t) ks_b64_decode(char *in, char *out, ks_size_t olen);
|
||||
|
||||
#endif
|
||||
|
||||
/* For Emacs:
|
||||
* Local Variables:
|
||||
* mode:c
|
||||
* indent-tabs-mode:t
|
||||
* tab-width:4
|
||||
* c-basic-offset:4
|
||||
* End:
|
||||
* For VIM:
|
||||
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
|
||||
*/
|
||||
@@ -0,0 +1,130 @@
|
||||
/*
|
||||
* Copyright (c) 2017 FreeSWITCH Solutions LLC
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of the original author; nor the names of any contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
||||
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
|
||||
#include <ks_base64.h>
|
||||
|
||||
|
||||
static const char ks_b64_table[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||
|
||||
|
||||
KS_DECLARE(ks_status_t) ks_b64_encode(unsigned char *in, ks_size_t ilen, unsigned char *out, ks_size_t olen)
|
||||
{
|
||||
int y = 0, bytes = 0;
|
||||
size_t x = 0;
|
||||
unsigned int b = 0, l = 0;
|
||||
|
||||
for (x = 0; x < ilen; x++) {
|
||||
b = (b << 8) + in[x];
|
||||
l += 8;
|
||||
|
||||
while (l >= 6) {
|
||||
out[bytes++] = ks_b64_table[(b >> (l -= 6)) % 64];
|
||||
if (bytes >= (int)olen - 1) {
|
||||
goto end;
|
||||
}
|
||||
if (++y != 72) {
|
||||
continue;
|
||||
}
|
||||
/* out[bytes++] = '\n'; */
|
||||
y = 0;
|
||||
}
|
||||
}
|
||||
|
||||
if (l > 0) {
|
||||
out[bytes++] = ks_b64_table[((b % 16) << (6 - l)) % 64];
|
||||
}
|
||||
if (l != 0) {
|
||||
while (l < 6 && bytes < (int)olen - 1) {
|
||||
out[bytes++] = '=', l += 2;
|
||||
}
|
||||
}
|
||||
|
||||
end:
|
||||
|
||||
out[bytes] = '\0';
|
||||
|
||||
return KS_STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
KS_DECLARE(ks_size_t) ks_b64_decode(char *in, char *out, ks_size_t olen)
|
||||
{
|
||||
|
||||
char l64[256];
|
||||
int b = 0, c, l = 0, i;
|
||||
char *ip, *op = out;
|
||||
size_t ol = 0;
|
||||
|
||||
for (i = 0; i < 256; i++) {
|
||||
l64[i] = -1;
|
||||
}
|
||||
|
||||
for (i = 0; i < 64; i++) {
|
||||
l64[(int) ks_b64_table[i]] = (char) i;
|
||||
}
|
||||
|
||||
for (ip = in; ip && *ip; ip++) {
|
||||
c = l64[(int) *ip];
|
||||
if (c == -1) {
|
||||
continue;
|
||||
}
|
||||
|
||||
b = (b << 6) + c;
|
||||
l += 6;
|
||||
|
||||
while (l >= 8) {
|
||||
op[ol++] = (char) ((b >> (l -= 8)) % 256);
|
||||
if (ol >= olen - 2) {
|
||||
goto end;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
end:
|
||||
|
||||
op[ol++] = '\0';
|
||||
|
||||
return ol;
|
||||
}
|
||||
|
||||
|
||||
/* For Emacs:
|
||||
* Local Variables:
|
||||
* mode:c
|
||||
* indent-tabs-mode:t
|
||||
* tab-width:4
|
||||
* c-basic-offset:4
|
||||
* End:
|
||||
* For VIM:
|
||||
* vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
|
||||
*/
|
||||
Reference in new issue
Block a user