From cf0a57dfc6d50e9c24e3c38f990e665c0eaa2510 Mon Sep 17 00:00:00 2001 From: FreeArtMan Date: Thu, 9 Nov 2017 23:29:45 +0000 Subject: Added base64 encoding for TODO command --- cmd/cmd_todo.c | 27 +++++++++-- cmd/cmd_todo.h | 1 + extlibs/b64.h | 68 +++++++++++++++++++++++++++ extlibs/b64_decode.c | 130 +++++++++++++++++++++++++++++++++++++++++++++++++++ extlibs/b64_encode.c | 91 ++++++++++++++++++++++++++++++++++++ 5 files changed, 313 insertions(+), 4 deletions(-) create mode 100644 extlibs/b64.h create mode 100644 extlibs/b64_decode.c create mode 100644 extlibs/b64_encode.c diff --git a/cmd/cmd_todo.c b/cmd/cmd_todo.c index 50218b0..e90aeb7 100644 --- a/cmd/cmd_todo.c +++ b/cmd/cmd_todo.c @@ -1,12 +1,20 @@ #include "cmd_todo.h" +//https://github.com/littlstar/b64.c +//https://www.google.nl/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&cad=rja&uact=8&ved=0ahUKEwiMu9_F5ZrXAhVLOMAKHZ6NDQYQFghDMAM&url=https%3A%2F%2Fopensource.apple.com%2Fsource%2FQuickTimeStreamingServer%2FQuickTimeStreamingServer-452%2FCommonUtilitiesLib%2Fbase64.c&usg=AOvVaw3tk0M33ne4ru28Bn_R1KI3 + static int add_todo(sqlite3 *db, char *user, char *todo) { int rc; - char sql_add_table[1024]; - snprintf(sql_add_table, 1024, "INSERT INTO todo(user,todo) VALUES('%s','%s');", user, todo); + char sql_add_table[3*256]; + char *b64_user, *b64_todo; + b64_user = b64_encode(user, strlen(user)); + b64_todo = b64_encode(todo, strlen(todo)); + snprintf(sql_add_table, 1024, "INSERT INTO todo(user,todo) VALUES('%s','%s');", b64_user, b64_todo); printf("%s\n", sql_add_table); + free(b64_user); + free(b64_todo); if ((rc = sqlite3_exec(db, sql_add_table, 0, 0, 0)) != SQLITE_OK) { @@ -33,7 +41,9 @@ static int cb_list_todo_table(void *param1, int argc, char **argv, char **cname) *out = sdscat(*out, " - "); } else if (strncmp(cname[i],"todo",4) == 0) { - *out = sdscat(*out, argv[i]); + char *b64_todo = b64_decode(argv[i], strlen(argv[i])); + *out = sdscat(*out, b64_todo); + free(b64_todo); *out = sdscat(*out, "\n"); } } @@ -49,7 +59,12 @@ static int list_todo(sqlite3 *db, char *user, sds *out) char *err_msg = NULL; char sql_list_table[1024]; - snprintf(sql_list_table, 1024, "SELECT * FROM todo WHERE user='%s'", user); + char *b64_user; + + b64_user = b64_encode(user, strlen(user)); + snprintf(sql_list_table, 1024, "SELECT * FROM todo WHERE user='%s'", b64_user); + printf("%s\n",sql_list_table); + free(b64_user); if ((rc = sqlite3_exec(db, sql_list_table, cb_list_todo_table, out, &err_msg )) != SQLITE_OK) { @@ -69,7 +84,11 @@ static int del_todo(sqlite3 *db, char *user, int id) char *err_msg; char sql_del_table[1024]; + char *b64_user; + + b64_user = b64_encode(user, strlen(user)); snprintf(sql_del_table, 1024, "DELETE FROM todo WHERE user='%s' AND id=%d;", user, id); + free(b64_user); if ((rc = sqlite3_exec(db, sql_del_table, 0, 0, &err_msg )) != SQLITE_OK) { diff --git a/cmd/cmd_todo.h b/cmd/cmd_todo.h index 210529e..e78de97 100644 --- a/cmd/cmd_todo.h +++ b/cmd/cmd_todo.h @@ -15,6 +15,7 @@ #include "debug.h" #include "nbrpc_call.h" #include "sds.h" +#include "b64.h" void *cmd_todo(void *data); diff --git a/extlibs/b64.h b/extlibs/b64.h new file mode 100644 index 0000000..19102f7 --- /dev/null +++ b/extlibs/b64.h @@ -0,0 +1,68 @@ + +/** + * `b64.h' - b64 + * + * copyright (c) 2014 joseph werle + */ + +#ifndef __B64_H +#define __B64_H 1 + +/** + * Memory allocation functions to use. You can define b64_malloc and + * b64_realloc to custom functions if you want. + */ + +#ifndef b64_malloc +# define b64_malloc(ptr) malloc(ptr) +#endif +#ifndef b64_realloc +# define b64_realloc(ptr, size) realloc(ptr, size) +#endif + +/** + * Base64 index table. + */ + +static const char b64_table[] = { + 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', + 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', + 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', + 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', + 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', + 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', + 'w', 'x', 'y', 'z', '0', '1', '2', '3', + '4', '5', '6', '7', '8', '9', '+', '/' +}; + +#ifdef __cplusplus +extern "C" { +#endif + +/** + * Encode `unsigned char *' source with `size_t' size. + * Returns a `char *' base64 encoded string. + */ + +char * +b64_encode (const unsigned char *, size_t); + +/** + * Dencode `char *' source with `size_t' size. + * Returns a `unsigned char *' base64 decoded string. + */ +unsigned char * +b64_decode (const char *, size_t); + +/** + * Dencode `char *' source with `size_t' size. + * Returns a `unsigned char *' base64 decoded string + size of decoded string. + */ +unsigned char * +b64_decode_ex (const char *, size_t, size_t *); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/extlibs/b64_decode.c b/extlibs/b64_decode.c new file mode 100644 index 0000000..7fb7b01 --- /dev/null +++ b/extlibs/b64_decode.c @@ -0,0 +1,130 @@ + +/** + * `decode.c' - b64 + * + * copyright (c) 2014 joseph werle + */ + +#include +#include +#include +#include "b64.h" + +#ifdef b64_USE_CUSTOM_MALLOC +extern void* b64_malloc(size_t); +#endif + +#ifdef b64_USE_CUSTOM_REALLOC +extern void* b64_realloc(void*, size_t); +#endif + +unsigned char * +b64_decode (const char *src, size_t len) { + return b64_decode_ex(src, len, NULL); +} + +unsigned char * +b64_decode_ex (const char *src, size_t len, size_t *decsize) { + int i = 0; + int j = 0; + int l = 0; + size_t size = 0; + unsigned char *dec = NULL; + unsigned char buf[3]; + unsigned char tmp[4]; + + // alloc + dec = (unsigned char *) b64_malloc(1); + if (NULL == dec) { return NULL; } + + // parse until end of source + while (len--) { + // break if char is `=' or not base64 char + if ('=' == src[j]) { break; } + if (!(isalnum(src[j]) || '+' == src[j] || '/' == src[j])) { break; } + + // read up to 4 bytes at a time into `tmp' + tmp[i++] = src[j++]; + + // if 4 bytes read then decode into `buf' + if (4 == i) { + // translate values in `tmp' from table + for (i = 0; i < 4; ++i) { + // find translation char in `b64_table' + for (l = 0; l < 64; ++l) { + if (tmp[i] == b64_table[l]) { + tmp[i] = l; + break; + } + } + } + + // decode + buf[0] = (tmp[0] << 2) + ((tmp[1] & 0x30) >> 4); + buf[1] = ((tmp[1] & 0xf) << 4) + ((tmp[2] & 0x3c) >> 2); + buf[2] = ((tmp[2] & 0x3) << 6) + tmp[3]; + + // write decoded buffer to `dec' + dec = (unsigned char *) b64_realloc(dec, size + 3); + if (dec != NULL){ + for (i = 0; i < 3; ++i) { + dec[size++] = buf[i]; + } + } else { + return NULL; + } + + // reset + i = 0; + } + } + + // remainder + if (i > 0) { + // fill `tmp' with `\0' at most 4 times + for (j = i; j < 4; ++j) { + tmp[j] = '\0'; + } + + // translate remainder + for (j = 0; j < 4; ++j) { + // find translation char in `b64_table' + for (l = 0; l < 64; ++l) { + if (tmp[j] == b64_table[l]) { + tmp[j] = l; + break; + } + } + } + + // decode remainder + buf[0] = (tmp[0] << 2) + ((tmp[1] & 0x30) >> 4); + buf[1] = ((tmp[1] & 0xf) << 4) + ((tmp[2] & 0x3c) >> 2); + buf[2] = ((tmp[2] & 0x3) << 6) + tmp[3]; + + // write remainer decoded buffer to `dec' + dec = (unsigned char *) b64_realloc(dec, size + (i - 1)); + if (dec != NULL){ + for (j = 0; (j < i - 1); ++j) { + dec[size++] = buf[j]; + } + } else { + return NULL; + } + } + + // Make sure we have enough space to add '\0' character at end. + dec = (unsigned char *) b64_realloc(dec, size + 1); + if (dec != NULL){ + dec[size] = '\0'; + } else { + return NULL; + } + + // Return back the size of decoded string if demanded. + if (decsize != NULL) { + *decsize = size; + } + + return dec; +} diff --git a/extlibs/b64_encode.c b/extlibs/b64_encode.c new file mode 100644 index 0000000..5e2920c --- /dev/null +++ b/extlibs/b64_encode.c @@ -0,0 +1,91 @@ + +/** + * `encode.c' - b64 + * + * copyright (c) 2014 joseph werle + */ + +#include +#include +#include "b64.h" + +#ifdef b64_USE_CUSTOM_MALLOC +extern void* b64_malloc(size_t); +#endif + +#ifdef b64_USE_CUSTOM_REALLOC +extern void* b64_realloc(void*, size_t); +#endif + +char * +b64_encode (const unsigned char *src, size_t len) { + int i = 0; + int j = 0; + char *enc = NULL; + size_t size = 0; + unsigned char buf[4]; + unsigned char tmp[3]; + + // alloc + enc = (char *) b64_malloc(1); + if (NULL == enc) { return NULL; } + + // parse until end of source + while (len--) { + // read up to 3 bytes at a time into `tmp' + tmp[i++] = *(src++); + + // if 3 bytes read then encode into `buf' + if (3 == i) { + buf[0] = (tmp[0] & 0xfc) >> 2; + buf[1] = ((tmp[0] & 0x03) << 4) + ((tmp[1] & 0xf0) >> 4); + buf[2] = ((tmp[1] & 0x0f) << 2) + ((tmp[2] & 0xc0) >> 6); + buf[3] = tmp[2] & 0x3f; + + // allocate 4 new byts for `enc` and + // then translate each encoded buffer + // part by index from the base 64 index table + // into `enc' unsigned char array + enc = (char *) b64_realloc(enc, size + 4); + for (i = 0; i < 4; ++i) { + enc[size++] = b64_table[buf[i]]; + } + + // reset index + i = 0; + } + } + + // remainder + if (i > 0) { + // fill `tmp' with `\0' at most 3 times + for (j = i; j < 3; ++j) { + tmp[j] = '\0'; + } + + // perform same codec as above + buf[0] = (tmp[0] & 0xfc) >> 2; + buf[1] = ((tmp[0] & 0x03) << 4) + ((tmp[1] & 0xf0) >> 4); + buf[2] = ((tmp[1] & 0x0f) << 2) + ((tmp[2] & 0xc0) >> 6); + buf[3] = tmp[2] & 0x3f; + + // perform same write to `enc` with new allocation + for (j = 0; (j < i + 1); ++j) { + enc = (char *) b64_realloc(enc, size + 1); + enc[size++] = b64_table[buf[j]]; + } + + // while there is still a remainder + // append `=' to `enc' + while ((i++ < 3)) { + enc = (char *) b64_realloc(enc, size + 1); + enc[size++] = '='; + } + } + + // Make sure we have enough space to add '\0' character at end. + enc = (char *) b64_realloc(enc, size + 1); + enc[size] = '\0'; + + return enc; +} -- cgit v1.2.3