Skip to content

Instantly share code, notes, and snippets.

@BrianLeishman

BrianLeishman/b64u.c

Last active Jul 3, 2018
Embed
What would you like to do?
Base64 encoding (URL style, with "-_" instead of "+/" and no "=") as a MySQL UDF
/*
* Copyright (c) 2017, Brian Leishman
* 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.
*
* 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 HOLDER 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.
*/
/*
* USAGE INSTRUCTIONS:
*
* make sure libmysqlclient-dev is installed:
* apt-get install libmysqlclient-dev
*
* Replace "/usr/lib/mysql/plugin" with your MySQL plugins directory (can be found by running "select @@plugin_dir;")
* gcc -Ofast -I/usr/include/mysql -o b64u.so -shared b64u.c -fPIC && cp b64u.so /usr/lib/mysql/plugin/b64u.so
*
* Then, on the server:
* create function`b64u`returns string soname'b64u.so';
*
* And use/test like:
* select `b64u`('yeet'); -- should return 'eWVldA'
*
* Yeet!
*
*/
#ifdef STANDARD
/* STANDARD is defined, don't use any mysql functions */
#include <string.h>
#ifdef __WIN__
typedef unsigned __int64 ulonglong; /* Microsofts 64 bit types */
typedef __int64 longlong;
#else
typedef unsigned long long ulonglong;
typedef long long longlong;
#endif /*__WIN__*/
#else
#include <my_global.h>
#include <my_sys.h>
#if defined(MYSQL_SERVER)
#include <m_string.h>
#else
/* when compiled as standalone */
#include <string.h>
#endif
#endif
#include <mysql.h>
#include <ctype.h>
#include <stdint.h>
#include <math.h>
#ifdef HAVE_DLOPEN
my_bool b64u_init(UDF_INIT *initid, UDF_ARGS *args, char *message);
void b64u_deinit(UDF_INIT *initid);
char *b64u(UDF_INIT *initid __attribute__ ((unused)), UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *message __attribute__ ((unused)));
my_bool b64u_init(UDF_INIT *initid, UDF_ARGS *args, char *message) {
if (args->arg_count != 1) {
strcpy(message, "`b64u`() requires 1 parameters: the string to be encoded");
return 1;
}
args->arg_type[1] = STRING_RESULT;
initid->maybe_null = 1; //can return null
return 0;
}
static const char basis_64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
int Base64encode(char *encoded, const char *string, int len) {
int i;
char *p;
p = encoded;
for (i = 0; i < len - 2; i += 3) {
*p++ = basis_64[(string[i] >> 2) & 0x3F];
*p++ = basis_64[((string[i] & 0x3) << 4) |
((int) (string[i + 1] & 0xF0) >> 4)];
*p++ = basis_64[((string[i + 1] & 0xF) << 2) |
((int) (string[i + 2] & 0xC0) >> 6)];
*p++ = basis_64[string[i + 2] & 0x3F];
}
if (i < len) {
*p++ = basis_64[(string[i] >> 2) & 0x3F];
if (i == (len - 1)) {
*p++ = basis_64[((string[i] & 0x3) << 4)];
} else {
*p++ = basis_64[((string[i] & 0x3) << 4) |
((int) (string[i + 1] & 0xF0) >> 4)];
*p++ = basis_64[((string[i + 1] & 0xF) << 2)];
}
}
return p - encoded;
}
char *b64u(UDF_INIT *initid __attribute__ ((unused)), UDF_ARGS *args, char *result, unsigned long *length, char *is_null, char *message __attribute__ ((unused))) {
if (args->args[0] == NULL) {
*is_null = 1;
return 0;
}
*length = (ceil(4.0 * args->lengths[0] / 3.0));
char *bytes = malloc(sizeof (char) * *length);
Base64encode(bytes, args->args[0], args->lengths[0]);
return bytes;
}
#endif /* HAVE_DLOPEN */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment