From cc5cd0a1e02bf1519d251debd7890ca4f20ec5b3 Mon Sep 17 00:00:00 2001 From: Alexander Vdolainen Date: Wed, 6 Jul 2016 04:55:27 +0300 Subject: [PATCH] sxmp: old base64 code removed; --- include/Makefile.am | 1 - include/sxmp/base64.h | 33 ------------- sxmp/Makefile.am | 5 +- sxmp/base64.c | 110 ------------------------------------------ sxmp/stream.c | 8 +-- 5 files changed, 7 insertions(+), 150 deletions(-) delete mode 100644 include/sxmp/base64.h delete mode 100644 sxmp/base64.c diff --git a/include/Makefile.am b/include/Makefile.am index e597afc..e60f8f6 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -1,5 +1,4 @@ nobase_include_HEADERS = sxmp/sxmp.h sxmp/errno.h sxmp/limits.h sxmp/version.h \ - sxmp/base64.h \ sexpr/cstring.h sexpr/faststack.h sexpr/sexp_errors.h sexpr/sexp.h \ sexpr/sexp_memory.h sexpr/sexp_ops.h sexpr/sexp_vis.h \ tdata/bitwise.h tdata/idx_allocator.h tdata/macro.h tdata/tree.h \ diff --git a/include/sxmp/base64.h b/include/sxmp/base64.h deleted file mode 100644 index 7a34d79..0000000 --- a/include/sxmp/base64.h +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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 - * - * 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 ."; - */ - -#ifndef __SXMP_BASE64_H__ -#define __SXMP_BASE64_H__ - -size_t sx_rawlen2b64len(size_t raw_length); - -size_t sx_b64encode_in(const char *data, char *bdata, size_t data_len); - -char *sx_b64encode(const char *data, size_t data_len); - -size_t sx_b64decode_in(const char *idata, size_t idata_len, char *data, size_t data_len); - -#endif /* __SXMP_BASE64_H__ */ diff --git a/sxmp/Makefile.am b/sxmp/Makefile.am index 127d277..14d825b 100644 --- a/sxmp/Makefile.am +++ b/sxmp/Makefile.am @@ -16,7 +16,7 @@ lib_LTLIBRARIES = libsxmp.la libsxmp_la_SOURCES = \ sxmplv2.c hub.c channel.c message.c rpc.c \ uuid.c stream_generic_listops.c stream.c \ - base64.c error.c + error.c libsxmp_la_LDFLAGS = @@ -29,7 +29,8 @@ libsxmp_la_LIBADD += -lmman -luuid /mingw64/lib/libsexpr.a /mingw64/lib/libtdata else -libsxmp_la_LIBADD += $(LIBUUID_LIBS) ../tdata/.libs/libtdata.la ../sexpr/.libs/libsexpr.la +libsxmp_la_LIBADD += $(LIBUUID_LIBS) ../tdata/.libs/libtdata.la \ + ../sexpr/.libs/libsexpr.la ../sxt/.libs/libsxt.la endif !COND_WIN32 diff --git a/sxmp/base64.c b/sxmp/base64.c deleted file mode 100644 index e29a765..0000000 --- a/sxmp/base64.c +++ /dev/null @@ -1,110 +0,0 @@ -/* - * 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 - * - * 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 ."; - */ - -#include -#include -#include - -#include - -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; -} diff --git a/sxmp/stream.c b/sxmp/stream.c index d6c2b67..dd66f44 100644 --- a/sxmp/stream.c +++ b/sxmp/stream.c @@ -40,11 +40,11 @@ #include #include +#include #include #include #include -#include #include "internal.h" @@ -413,17 +413,17 @@ size_t sxstream_bwriteat(sxstream_t *stream, const void *buf, int idx, r; msglen = strlen(_SXSTREAMBWRITE_CMD) + sizeof(char)*8 + 32*sizeof(char); - if(MAX_RBBUF_LEN - msglen < sx_rawlen2b64len(buf_len)) /* cut the data */ + if(MAX_RBBUF_LEN - msglen < sxt_rawlen2b64len(buf_len)) /* cut the data */ buf_len = ((MAX_RBBUF_LEN - msglen)/4)*3 - 2; - msglen += sx_rawlen2b64len(buf_len); + msglen += sxt_rawlen2b64len(buf_len); if(!(mbuf = malloc(msglen))) { errno = SXE_ENOMEM; goto __fail; } msglen = snprintf(mbuf, msglen, "(%s %lu %lu %lu \"", _SXSTREAMBWRITE_CMD, stream->sid, off, buf_len); - wr = sx_b64encode_in(buf, mbuf + msglen, buf_len); + wr = sxt_b64encode_in(buf, mbuf + msglen, buf_len); if(wr < 0) goto __fail; else msglen += wr; msglen += snprintf(mbuf + msglen, MAX_RBBUF_LEN - msglen, "\")");