You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
libsxmp/sxmp/base64.c

111 lines
3.2 KiB
C

/*
* Base64 encode/decode functions used in sxmp
* openssl stuff wasn't used to avoid additional memleaks and allocation,
* deallocation of BIO.
*
* (c) Alexander Vdolainen 2016 <avdolainen@zoho.com>
*
* libsxmp is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libsxmp is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.";
*/
#include <stdint.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sxmp/base64.h>
static const char b64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
static inline void encodeblock(unsigned char *in, unsigned char *out, int len)
{
out[0] = (unsigned char) b64[ (int)(in[0] >> 2) ];
out[1] = (unsigned char) b64[ (int)(((in[0] & 0x03) << 4) | ((in[1] & 0xf0) >> 4)) ];
out[2] = (unsigned char) (len > 1 ? b64[ (int)(((in[1] & 0x0f) << 2) | ((in[2] & 0xc0) >> 6)) ] : '=');
out[3] = (unsigned char) (len > 2 ? b64[ (int)(in[2] & 0x3f) ] : '=');
}
static inline void decodeblock(unsigned char *in, unsigned char *out)
{
out[0] = (unsigned char) (in[0] << 2 | in[1] >> 4);
out[1] = (unsigned char) (in[1] << 4 | in[2] >> 2);
out[2] = (unsigned char) (((in[2] << 6) & 0xc0) | in[3]);
}
size_t sx_rawlen2b64len(size_t raw_length)
{
return 4 * ((raw_length + 2) / 3);
}
size_t sx_b64encode_in(const char *data, char *bdata, size_t data_len)
{
size_t bdata_len = 0, c = 0, n = 0;
int len = 0, nil = 0, i;
unsigned char ib[4];
if(!bdata) return -1;
if(!data || !data_len) return -1;
else bdata_len = sx_rawlen2b64len(data_len);
while(n != bdata_len) {
if((c + 3) <= data_len) {
len = 3;
encodeblock((unsigned char *)data + c, (unsigned char *)bdata + n, len);
n += 4; c += 3;
} else {
nil = (c + 3) - data_len;
len = 0;
for(i = 0; i < 3; i++) {
if(i < nil) {
ib[i] = *((unsigned char *)data + (c + i));
len++;
} else ib[i] = (unsigned char)0;
}
encodeblock(ib, (unsigned char *)bdata + n, len);
}
}
return bdata_len;
}
char *sx_b64encode(const char *data, size_t data_len)
{
char *bdata = NULL;
size_t bdata_len = 0;
if(!data || !data_len) return NULL;
else bdata_len = sx_rawlen2b64len(data_len);
if(!(bdata = malloc(bdata_len))) return NULL;
sx_b64encode_in(data, bdata, data_len);
return bdata;
}
size_t sx_b64decode_in(const char *idata, size_t idata_len, char *data, size_t data_len)
{
size_t enc = 0, dec = 0;
if(!idata || !idata_len) return -1;
if(!data || !data_len) return -1;
for(enc = 0; enc != idata_len; enc += 4) {
if(dec >= data_len) return dec;
decodeblock((unsigned char *)idata + enc, (unsigned char *)data + dec);
dec += 3;
}
return dec;
}