2017-03-12 16:49:47 +00:00
|
|
|
/*
|
|
|
|
* xconv.c -- An SQLite3 C extension that uses libxconv to convert from
|
|
|
|
* ISO 6937 to UTF-8.
|
|
|
|
*/
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sqlite3ext.h>
|
|
|
|
#include <xconv.h>
|
|
|
|
|
|
|
|
SQLITE_EXTENSION_INIT1
|
|
|
|
|
|
|
|
void
|
|
|
|
sqlite_xconv(sqlite3_context *c, int argc, sqlite3_value **argv)
|
|
|
|
{
|
|
|
|
char *src, *dst;
|
|
|
|
size_t dlen;
|
|
|
|
|
|
|
|
if (argc != 1 || !(src = (char *)sqlite3_value_text(argv[0])))
|
|
|
|
{
|
|
|
|
sqlite3_result_error(c, "xconv requires 1 parameter", -1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-09-24 00:14:14 +00:00
|
|
|
dlen = strlen(src) * 2 + 14;
|
2017-03-12 16:49:47 +00:00
|
|
|
if (!(dst = (char *)malloc(dlen)))
|
|
|
|
{
|
|
|
|
sqlite3_result_error(c, "malloc() failed", -1);
|
|
|
|
return;
|
|
|
|
}
|
2022-09-24 00:14:14 +00:00
|
|
|
|
2017-03-12 16:49:47 +00:00
|
|
|
if (!xconv(src, dst, dlen))
|
|
|
|
{
|
|
|
|
free(dst);
|
|
|
|
dst = strdup(src);
|
|
|
|
}
|
2022-09-24 00:14:14 +00:00
|
|
|
|
|
|
|
sqlite3_result_text(c, dst, -1, free);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sqlite_utf8conv(sqlite3_context *c, int argc, sqlite3_value **argv)
|
|
|
|
{
|
|
|
|
char *src, *dst;
|
|
|
|
|
|
|
|
if (argc != 1 || !(src = (char *)sqlite3_value_text(argv[0])))
|
|
|
|
{
|
|
|
|
sqlite3_result_error(c, "utf8conv requires 1 parameter", -1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (src[0] == 0x10)
|
|
|
|
{
|
|
|
|
if (src[1] == 'i' && src[2] == '7')
|
|
|
|
{
|
|
|
|
size_t dlen;
|
|
|
|
|
|
|
|
src += 3;
|
|
|
|
dlen = strlen(src) * 2 + 14;
|
|
|
|
if (!(dst = (char *)malloc(dlen)))
|
|
|
|
{
|
|
|
|
sqlite3_result_error(c, "malloc() failed", -1);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!xconv(src, dst, dlen))
|
|
|
|
{
|
|
|
|
free(dst);
|
|
|
|
dst = strdup(src);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
dst = strdup("");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (*src < 0x20)
|
|
|
|
src += *src == 0x1F ? 2 : 1;
|
|
|
|
|
|
|
|
dst = strdup(src);
|
|
|
|
}
|
|
|
|
|
2017-03-12 16:49:47 +00:00
|
|
|
sqlite3_result_text(c, dst, -1, free);
|
|
|
|
}
|
|
|
|
|
2022-09-24 13:33:55 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
__declspec(dllexport)
|
|
|
|
#endif
|
2017-03-13 09:04:35 +00:00
|
|
|
int sqlite3_xconv_init(sqlite3 *db, char **err,
|
2017-03-12 16:49:47 +00:00
|
|
|
const sqlite3_api_routines *api)
|
|
|
|
{
|
2022-09-24 00:14:14 +00:00
|
|
|
int rc1, rc2;
|
2017-03-12 16:49:47 +00:00
|
|
|
|
2022-09-24 00:14:14 +00:00
|
|
|
SQLITE_EXTENSION_INIT2(api);
|
|
|
|
rc1 = sqlite3_create_function(db, "xconv", 1,
|
2017-03-12 16:49:47 +00:00
|
|
|
SQLITE_UTF8 | SQLITE_DETERMINISTIC, NULL,
|
|
|
|
sqlite_xconv, NULL, NULL);
|
2022-09-24 00:14:14 +00:00
|
|
|
rc2 = sqlite3_create_function(db, "utf8conv", 1,
|
|
|
|
SQLITE_UTF8 | SQLITE_DETERMINISTIC, NULL,
|
|
|
|
sqlite_utf8conv, NULL, NULL);
|
|
|
|
return rc1 != SQLITE_OK && rc2 != SQLITE_OK ? rc1 : SQLITE_OK;
|
2017-03-12 16:49:47 +00:00
|
|
|
}
|
|
|
|
|