/*-------------------------------------------------------------------- ** c 2 b : Unicode version ** User-Defined Function to convert characters to "bits" (i.e. a ** printable string of 0s and 1s). One argument: ** 1. input string ** ** 2008-05-30 G. Rommel -- initial release replace function sysdba.c2b ( varchar(256) character set unicode) returns varchar(2048) language C no sql parameter style td_general deterministic returns null on null input external name 'CS!c2b_u!c2b_u.c!F!c2b_u'; ** **------------------------------------------------------------------*/ #define SQL_TEXT Latin_Text #include #include #include #define IN_MAXIMUM 256 typedef unsigned char Byte; void c2b_u ( VARCHAR_UNICODE *in_string, VARCHAR_LATIN *result, char sqlstate[6] ) { int len, word, in_word, word_found; VARCHAR_UNICODE *input_ptr; VARCHAR_UNICODE *end_ptr; VARCHAR_LATIN *result_ptr; unsigned char top4, bottom4; char bitz[16][5] = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111" }; /* Determine length of input string. */ input_ptr = in_string; len = 0; for (; *input_ptr != 0; input_ptr++) { len++; } /* in_string length must be less than defined maximum. */ if (len > (IN_MAXIMUM - 1)) { strcpy(sqlstate, "U0001"); /* Invalid input */ return; } /* If the input string is empty, so is the output. */ if (len == 0) { result[0] = 0; strcpy(sqlstate, "00000"); /* No error */ return; } end_ptr = in_string + len; result_ptr = result; for (input_ptr = in_string; input_ptr < end_ptr; ++input_ptr) { /* Unicode is officially big-endian, so we start with the "high-order" bits. */ top4 = (*input_ptr) >> 12; bottom4 = (*input_ptr >> 8) & 0x0F; memcpy(result_ptr, bitz[top4], 4); result_ptr += 4; memcpy(result_ptr, bitz[bottom4], 4); result_ptr += 4; top4 = (*input_ptr >> 4) & 0x0F; bottom4 = (*input_ptr) & 0x0F; memcpy(result_ptr, bitz[top4], 4); result_ptr += 4; memcpy(result_ptr, bitz[bottom4], 4); result_ptr += 4; } *result_ptr = '\0'; strcpy(sqlstate, "00000"); /* No Error */ return; }