Skip to content

Instantly share code, notes, and snippets.

@ghostmansd
Created June 25, 2014 12:45
Show Gist options
  • Save ghostmansd/6ed7c73633feecf69782 to your computer and use it in GitHub Desktop.
Save ghostmansd/6ed7c73633feecf69782 to your computer and use it in GitHub Desktop.
libmbfl: cannot determine UTF-16, UTF-32 and UHC
/*
* libmbfl UTF-16 and UTF-32 bug example.
* gcc -o mbfl_test mbfltest.c -ldl
* One may need to adjust CONST_MBFL_PATH to run this program.
*/
#include <stdio.h>
#include <dlfcn.h>
#include <stdlib.h>
#include <mbfl/mbfilter.h>
#include <mbfl/mbfilter.h>
#define CONST_MBFL_PATH "libmbfl.so.1"
#define CONST_TESTS_COUNT 7
#define CONST_BUFSIZE_UTF16 246
#define CONST_BUFSIZE_UTF32 252
#define CONST_BUFSIZE_SJIS2004 128
#define MACRO_DLCHECK(handle) \
do { \
if (handle == NULL) \
{ \
fprintf(stderr, "detect: %s\n", dlerror()); \
if (library != NULL) \
dlclose(library); \
return EXIT_FAILURE; \
} \
} while (0);
typedef struct mbfl_test_t {
enum mbfl_no_encoding encoding;
size_t const size;
char const *buffer;
} mbfl_test_t;
static mbfl_test_t testing_suite[CONST_TESTS_COUNT] = {
{mbfl_no_encoding_utf16,
CONST_BUFSIZE_UTF16, "\
\xcb\xdb\x5a\xdd\x16\xd9\x5b\xdf\
\xf7\xd8\x28\xde\xc3\xd8\xa8\xde\
\xd7\x17\xdf\xd9\x45\xdf\x38\xd8\
\xfb\xdc\x54\xd8\xbd\xdd\xff\xd8\
\xa1\xde\x64\xda\xa4\xdc\x59\xd9\
\xdc\xdd\x44\xda\x0b\xde\x3d\xdb\
\xf5\xdf\x45\xdb\xdc\xdc\x17\x95\
\x1e\xd8\xe8\xde\x6d\xd8\x35\xdd\
\xd0\xd8\x90\xdc\x91\xdb\x5f\xdc\
\x7d\xda\x28\xde\x4c\xd8\xde\xdd\
\xc9\xdb\x2d\xde\x40\xdb\x0e\xdf\
\x05\xd8\x30\xdc\xbd\xd8\x28\xdf\
\x96\xd9\x57\xde\xb6\xda\x7d\xdc\
\x92\xda\xfa\xdf\xae\xdb\x22\xdd\
\xf0\xda\x9b\xdf\xb7\xdb\x6e\xde\
\x1e\xdb\xbe\xdd\x7a\xdb\x21\xdc\
\x6e\xdb\x28\xdd\xda\xd9\x5c\xdc\
\xb1\xd9\x01\xdd\xf1\xd8\xb3\xde\
\x38\xda\xd5\xdd\xe5\xda\x2a\xdd\
\x99\xd8\xb9\xdf\x20\xd9\x3a\xde\
\xdd\xdb\x6e\xde\xd5\xd9\x8f\xdf\
\x65\xda\xb1\xdc\xee\xd9\x43\xdd\
\x4e\xdb\xb0\xde\x16\xda\xcf\xde\
\xdc\xda\x73\xde\x80\xd9\x58\xdd\
\x70\xd9\x05\xdf\xc2\xd9\x2b\xdf\
\x5a\xd9\xe9\xdf\x06\xd9\x65\xdc\
\xfd\xb7\x1b\xd8\xbe\xdf\xc4\xda\
\x74\xdf\xea\xdb\xbd\xdd\x5a\xd8\
\x32\xdf\x1e\xda\x63\xde\x26\xdb\
\x59\xdd\x5f\xda\xc4\xdf\x3b\xd8\
\x6b\xdc\xe1\xd9\x2e\xdd"},
{mbfl_no_encoding_utf16be,
CONST_BUFSIZE_UTF16, "\
\xdb\x44\xdf\xd5\xd9\x58\xdf\xd5\
\xdb\x74\xdd\x16\xd8\x51\xdd\x78\
\xda\x73\xdc\xcc\xda\xe3\xde\xd6\
\xd9\x89\xde\x83\xda\xb2\xde\xb3\
\xda\x72\xdc\xed\xda\x2e\xdd\x20\
\x7c\x52\xd8\xfa\xdf\x93\xd8\x51\
\xde\xcd\xd9\x8f\xdf\xa0\xdb\x26\
\xde\x62\xda\xe2\xde\xcc\xd9\xed\
\xdd\x27\xd9\x0e\xdd\x51\xda\xeb\
\xdd\x87\xd8\xab\xdc\x5b\x29\x2c\
\xd9\x62\xdd\x33\xd8\xdd\xde\xf3\
\xdb\x53\xde\x4e\x5c\x02\xd9\x31\
\xdd\x0f\xdb\xac\xdd\xb7\xda\xe9\
\xdf\xd4\xd8\x52\xdd\x65\xdb\x20\
\xdf\x0f\xdb\x01\xde\x80\xd8\xd0\
\xdf\x22\xd9\xbc\xdc\x61\xd9\xea\
\xde\xe9\xd9\xc3\xdd\x16\xdb\x74\
\xdd\xc8\xda\x2d\xdd\x20\xdb\xde\
\xde\x67\xd8\xd0\xdf\x57\xd9\xb1\
\xdf\xdb\xdb\x24\xdc\x13\xda\x50\
\xdd\x72\xdb\x73\xde\xbd\xd9\x58\
\xdd\xac\xd9\x74\xde\x8a\xda\xc4\
\xdc\x35\xdb\x7e\xdd\x3f\xda\x9d\
\xde\x98\xdb\xa9\xdd\x93\xda\xed\
\xdd\x94\xdb\x58\xdd\x33\xdb\x40\
\xdf\xe1\xda\x33\xdf\x91\xdb\x01\
\xdc\x96\xd8\x76\xdc\x6d\xdb\x81\
\xdc\x30\xd8\x73\xdd\xb8\xdb\x9d\
\xdc\xc2\xd9\x67\xdc\x2a\xd8\xfe\
\xdd\x34\xdb\x56\xdc\xf1\xdb\x84\
\xdd\x39\xda\xc0\xdc\x0a"},
{mbfl_no_encoding_utf16le,
CONST_BUFSIZE_UTF16, "\
\x4c\xda\x2b\xdd\x2b\xdb\x1b\xdd\
\x2a\xd9\x14\xde\x72\xdb\xcf\xde\
\x4d\xd9\xd7\xdd\xd5\xd9\x86\xdd\
\xc4\xda\x73\xdc\x7c\xd8\x28\xdc\
\x13\xda\xd9\xde\x09\xdb\x47\xde\
\xc9\xd8\x7a\xdd\xb7\xdb\x6c\xdd\
\x82\xdb\x1e\xdd\x78\xd8\xe7\xdd\
\x20\xd9\x07\xdc\x3e\xd9\x0b\xdd\
\xf3\xda\x72\xdc\xdc\xd9\x0a\xdc\
\x81\xd9\xbe\xde\x54\xd8\x0a\xdd\
\x50\xdb\x5f\xdf\x15\x88\xbc\xd8\
\xde\xdc\xa1\xda\x96\xdd\xe7\xdb\
\xed\xdc\x47\xd9\x95\xdf\x0b\xd8\
\x9d\xdd\xc1\xd9\x56\xdc\x8c\xd9\
\xd1\xdc\xe1\xda\x5b\xdf\xf6\xdb\
\x2d\xdc\x61\xd8\x83\xdf\xec\xda\
\x52\xdc\xb3\xd9\x31\xdd\xd9\xd9\
\x11\xdc\x51\xd9\x3f\xdd\x46\xdb\
\x64\xde\x88\xdb\x23\xdf\xc6\xaf\
\x4a\xd8\xcc\xdd\xbf\xdb\x35\xdf\
\xa2\xdb\xd2\xdc\xd6\xda\x16\xdf\
\x88\xdb\x80\xdc\x6b\xdb\xc7\xdc\
\x79\xd8\x76\xde\xaf\xd8\xb0\xdd\
\x67\xdb\xaf\xdf\x66\xd8\x2e\xdd\
\x41\xd9\x65\xdc\xe4\xd9\xe1\xdd\
\xe0\xd9\xf7\xdf\xb3\xd8\xf7\xdf\
\x2e\xc6\x09\xd8\x6e\xdf\x25\xd8\
\xf4\xdd\xc0\xdb\xac\xdf\xdc\xdb\
\x47\xdc\x89\xd9\xef\xdd\x54\xd9\
\xc6\xdc\xe5\xda\x4f\xde\xc8\xda\
\x8b\xdf\x01\xd9\xeb\xdc"},
{mbfl_no_encoding_utf32,
CONST_BUFSIZE_UTF32, "\
\x92\x6e\x01\x00\x9e\x34\x09\x00\
\x4a\x44\x01\x00\x9d\xa2\x0f\x00\
\x23\x05\x05\x00\xfd\xd7\x0f\x00\
\xfb\x0a\x08\x00\xe7\x39\x04\x00\
\x43\xa9\x09\x00\xb1\x2d\x02\x00\
\x20\xf2\x0e\x00\x13\xdd\x0c\x00\
\xbf\xc8\x0c\x00\xa0\x37\x06\x00\
\x99\x19\x0a\x00\xad\x6f\x04\x00\
\xf2\xc4\x04\x00\x19\xc8\x10\x00\
\x34\x94\x0c\x00\x2b\x17\x01\x00\
\x53\x7b\x0e\x00\x75\x5c\x10\x00\
\x49\xe3\x05\x00\xe2\xb2\x09\x00\
\xbe\x8f\x0c\x00\x45\xdf\x01\x00\
\xb5\x4e\x08\x00\xa3\x6f\x04\x00\
\x1a\x2d\x0b\x00\x92\x7f\x07\x00\
\x77\x12\x0a\x00\x37\x56\x08\x00\
\x7c\x39\x04\x00\x49\xab\x0b\x00\
\x24\xe1\x10\x00\x03\x56\x09\x00\
\x30\x89\x08\x00\x68\xb6\x04\x00\
\x76\x64\x00\x00\x27\x7c\x02\x00\
\x35\x3c\x0a\x00\x83\x8e\x0e\x00\
\x75\x2b\x01\x00\x14\x9d\x05\x00\
\xf4\x84\x0b\x00\xab\xc3\x0f\x00\
\x19\x92\x05\x00\xbb\xa0\x0c\x00\
\x0c\x89\x0b\x00\xc2\xdd\x02\x00\
\x45\x2d\x01\x00\xe8\x00\x10\x00\
\x7c\xc9\x01\x00\x90\x7f\x03\x00\
\x59\x81\x01\x00\x53\x54\x09\x00\
\xe3\x5d\x0e\x00\x81\x1b\x0c\x00\
\x9d\xf4\x08\x00\x17\x77\x0f\x00\
\x82\x07\x01\x00\xc4\xfd\x08\x00\
\x1a\xbb\x0c\x00"},
{mbfl_no_encoding_utf32be,
CONST_BUFSIZE_UTF32, "\
\x00\x07\xbd\x93\x00\x07\x1c\x25\
\x00\x0d\x4a\x2b\x00\x0b\x7f\xe8\
\x00\x08\xc2\x8f\x00\x0d\xea\x8a\
\x00\x0a\x3d\x83\x00\x02\x0b\x0f\
\x00\x0a\x10\x48\x00\x05\x59\x74\
\x00\x02\x4b\x2e\x00\x06\x1f\x98\
\x00\x0f\x5f\xa5\x00\x06\x43\xfd\
\x00\x0d\x3b\x04\x00\x10\xeb\x68\
\x00\x0b\xa9\x5c\x00\x0c\x39\xc0\
\x00\x03\x58\xbf\x00\x04\xd2\x8e\
\x00\x02\xd5\x5d\x00\x08\xaf\x74\
\x00\x0e\x2c\x65\x00\x0d\xf5\x9e\
\x00\x0b\x80\xc8\x00\x0c\xc3\x92\
\x00\x03\xa6\xd5\x00\x01\xca\x37\
\x00\x04\xfa\xd6\x00\x09\x28\x64\
\x00\x01\xdc\x0b\x00\x0e\x5d\x1b\
\x00\x01\x54\xf3\x00\x0f\x74\x51\
\x00\x0d\x11\x8e\x00\x07\xef\x9e\
\x00\x0b\x63\xb8\x00\x0d\x6e\x3e\
\x00\x02\x6d\xd1\x00\x04\xcc\x1e\
\x00\x00\x3d\x59\x00\x0f\xd7\x4f\
\x00\x09\x10\xc8\x00\x0d\x0b\x29\
\x00\x0c\x43\x74\x00\x07\x5e\xf6\
\x00\x0a\x2a\x2a\x00\x07\x81\xde\
\x00\x01\x3e\xc1\x00\x0c\xd1\xa0\
\x00\x03\x65\x71\x00\x08\xf8\x2e\
\x00\x04\xba\xc4\x00\x09\x02\xe2\
\x00\x03\x88\xcc\x00\x0d\x44\x24\
\x00\x0c\x4a\x56\x00\x0c\xd1\x52\
\x00\x08\x73\x1c\x00\x05\x12\xe6\
\x00\x05\x32\x24\x00\x0d\x27\x97\
\x00\x10\x2a\x80"},
{mbfl_no_encoding_utf32le,
CONST_BUFSIZE_UTF32, "\
\xbf\x55\x00\x00\xa8\x7a\x0e\x00\
\xa8\x5b\x02\x00\x84\x32\x03\x00\
\xe2\xa9\x03\x00\x8b\x15\x07\x00\
\x9f\x85\x0e\x00\x7f\x55\x0c\x00\
\xb6\x1c\x01\x00\x30\x4c\x0c\x00\
\x46\x26\x04\x00\x12\xfd\x0f\x00\
\x9a\xed\x06\x00\xb9\xfd\x02\x00\
\x87\x88\x0c\x00\x3b\xe4\x00\x00\
\xbf\x81\x0f\x00\x38\xac\x09\x00\
\xdc\x9f\x01\x00\x85\xa5\x01\x00\
\xdc\xeb\x07\x00\x8c\x47\x10\x00\
\x3d\x21\x06\x00\x73\xdf\x09\x00\
\x4c\x12\x04\x00\x75\x68\x07\x00\
\xb2\x1f\x07\x00\xf7\xcb\x02\x00\
\x92\xbd\x0a\x00\x17\x57\x05\x00\
\xd0\xd7\x0d\x00\x4c\x8e\x0f\x00\
\x16\x43\x07\x00\xc6\x9f\x08\x00\
\x69\x65\x07\x00\xb3\xdf\x04\x00\
\xdf\xb9\x10\x00\x00\xd6\x04\x00\
\xc6\x5c\x0b\x00\x15\xf2\x09\x00\
\x4f\xa6\x08\x00\x31\x63\x07\x00\
\x54\xdb\x10\x00\x4f\x7a\x04\x00\
\x76\x50\x0d\x00\x0f\x3a\x0b\x00\
\xc6\xf9\x0e\x00\x7e\x92\x0c\x00\
\xaa\xa2\x0f\x00\x71\x3d\x10\x00\
\xad\x91\x04\x00\x26\xfc\x0c\x00\
\xf2\x13\x00\x00\x48\xa2\x0f\x00\
\x82\x3f\x0c\x00\x3a\x5d\x08\x00\
\x6b\x15\x10\x00\x7b\x2c\x01\x00\
\xcf\xde\x07\x00\x02\xc8\x0b\x00\
\x5a\xa3\x0d\x00\x87\xaa\x0d\x00\
\x5f\xe4\x00\x00"},
{mbfl_no_encoding_sjis2004,
CONST_BUFSIZE_SJIS2004, "\
\x95\x95\x8b\x45\x9b\x87\xe0\x8b\
\xe3\xa9\x92\xd1\xfb\xb6\x97\xa1\
\xe5\x99\xfb\xad\x9a\xc7\x8d\x6c\
\xf1\xc0\x98\xf5\xe0\x7c\x8d\x45\
\xee\x75\x97\xa1\xe2\x50\x86\xf0\
\x82\xab\x8b\xf7\x8e\x6e\x86\x72\
\xe5\x78\xf7\xdb\x97\x47\xe0\x54\
\x92\x93\xf1\x7d\xe6\xa2\x9b\xc9\
\x8f\x41\xf6\xe4\xe2\x70\x84\x47\
\x84\x6c\xeb\x4d\x9d\xa4\xe5\x4b\
\xf9\x77\x9e\x76\xe2\xa8\xf5\xc6\
\x92\x5c\xfb\xfb\x83\xe5\x91\x98\
\x92\x5e\x9c\x45\x8f\xbc\xea\x82\
\xe3\xed\xe0\xbb\x9d\xad\xef\xdf\
\xe8\x52\xec\xdf\x9b\x75\x8e\xfa\
\x97\x76\xe8\x6b\x96\x74"}
};
int main(int argc, char const **argv)
{
size_t i = 0;
void *library = NULL;
mbfl_string string = {0};
mbfl_test_t *test = NULL;
mbfl_encoding const *real = NULL;
mbfl_encoding const *guess = NULL;
mbfl_encoding const *(*mbfl_identify_encoding2)(
mbfl_string *string,
const mbfl_encoding **elist,
int elistsz,
int strict) = NULL;
mbfl_encoding const *(*mbfl_no2encoding)(
enum mbfl_no_encoding no_encoding) = NULL;
library = dlopen(CONST_MBFL_PATH, RTLD_LAZY);
*(void**)(&mbfl_no2encoding) = \
dlsym(library, "mbfl_no2encoding");
MACRO_DLCHECK(mbfl_no2encoding);
*(void**)(&mbfl_identify_encoding2) = \
dlsym(library, "mbfl_identify_encoding2");
MACRO_DLCHECK(mbfl_identify_encoding2);
for (; i < CONST_TESTS_COUNT; ++i)
{
test = &testing_suite[i];
real = mbfl_no2encoding(test->encoding);
string.len = (unsigned int) test->size;
string.val = (unsigned char*) test->buffer;
guess = mbfl_identify_encoding2(&string, &real, 1, 0);
if (guess != NULL)
{
if (real->no_encoding != guess->no_encoding)
printf("WARNING \t %s != %s\n",
real->mime_name, guess->mime_name);
else
printf("SUCCESS \t %s == %s\n",
real->mime_name);
}
else
printf("FAILURE \t %s cannot be determined\n",
real->mime_name);
}
dlclose(library);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment