danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2001 September 15 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ************************************************************************* |
| 12 | ** Code for testing the utf.c module in SQLite. This code |
| 13 | ** is not included in the SQLite library. It is used for automated |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 14 | ** testing of the SQLite library. Specifically, the code in this file |
| 15 | ** is used for testing the SQLite routines for converting between |
| 16 | ** the various supported unicode encodings. |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 17 | */ |
| 18 | #include "sqliteInt.h" |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 19 | #include "vdbeInt.h" |
mistachkin | 52b1dbb | 2016-07-28 14:37:04 +0000 | [diff] [blame] | 20 | #if defined(INCLUDE_SQLITE_TCL_H) |
| 21 | # include "sqlite_tcl.h" |
| 22 | #else |
| 23 | # include "tcl.h" |
| 24 | #endif |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
| 27 | |
| 28 | /* |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 29 | ** The first argument is a TCL UTF-8 string. Return the byte array |
| 30 | ** object with the encoded representation of the string, including |
| 31 | ** the NULL terminator. |
| 32 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 33 | static int SQLITE_TCLAPI binarize( |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 34 | void * clientData, |
| 35 | Tcl_Interp *interp, |
| 36 | int objc, |
| 37 | Tcl_Obj *CONST objv[] |
| 38 | ){ |
| 39 | int len; |
| 40 | char *bytes; |
| 41 | Tcl_Obj *pRet; |
| 42 | assert(objc==2); |
| 43 | |
| 44 | bytes = Tcl_GetStringFromObj(objv[1], &len); |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 45 | pRet = Tcl_NewByteArrayObj((u8*)bytes, len+1); |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 46 | Tcl_SetObjResult(interp, pRet); |
| 47 | return TCL_OK; |
| 48 | } |
| 49 | |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 50 | /* |
| 51 | ** Usage: test_value_overhead <repeat-count> <do-calls>. |
| 52 | ** |
| 53 | ** This routine is used to test the overhead of calls to |
| 54 | ** sqlite3_value_text(), on a value that contains a UTF-8 string. The idea |
| 55 | ** is to figure out whether or not it is a problem to use sqlite3_value |
| 56 | ** structures with collation sequence functions. |
| 57 | ** |
| 58 | ** If <do-calls> is 0, then the calls to sqlite3_value_text() are not |
| 59 | ** actually made. |
| 60 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 61 | static int SQLITE_TCLAPI test_value_overhead( |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 62 | void * clientData, |
| 63 | Tcl_Interp *interp, |
| 64 | int objc, |
| 65 | Tcl_Obj *CONST objv[] |
| 66 | ){ |
| 67 | int do_calls; |
| 68 | int repeat_count; |
| 69 | int i; |
| 70 | Mem val; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 71 | |
| 72 | if( objc!=3 ){ |
| 73 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 74 | Tcl_GetStringFromObj(objv[0], 0), " <repeat-count> <do-calls>", 0); |
| 75 | return TCL_ERROR; |
| 76 | } |
| 77 | |
| 78 | if( Tcl_GetIntFromObj(interp, objv[1], &repeat_count) ) return TCL_ERROR; |
| 79 | if( Tcl_GetIntFromObj(interp, objv[2], &do_calls) ) return TCL_ERROR; |
| 80 | |
| 81 | val.flags = MEM_Str|MEM_Term|MEM_Static; |
| 82 | val.z = "hello world"; |
danielk1977 | dc8453f | 2004-06-12 00:42:34 +0000 | [diff] [blame] | 83 | val.enc = SQLITE_UTF8; |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 84 | |
| 85 | for(i=0; i<repeat_count; i++){ |
| 86 | if( do_calls ){ |
drh | caffb1a | 2012-01-30 18:00:31 +0000 | [diff] [blame] | 87 | sqlite3_value_text(&val); |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | return TCL_OK; |
| 92 | } |
| 93 | |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 94 | static u8 name_to_enc(Tcl_Interp *interp, Tcl_Obj *pObj){ |
| 95 | struct EncName { |
| 96 | char *zName; |
| 97 | u8 enc; |
| 98 | } encnames[] = { |
| 99 | { "UTF8", SQLITE_UTF8 }, |
| 100 | { "UTF16LE", SQLITE_UTF16LE }, |
| 101 | { "UTF16BE", SQLITE_UTF16BE }, |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 102 | { "UTF16", SQLITE_UTF16 }, |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 103 | { 0, 0 } |
| 104 | }; |
| 105 | struct EncName *pEnc; |
| 106 | char *z = Tcl_GetString(pObj); |
| 107 | for(pEnc=&encnames[0]; pEnc->zName; pEnc++){ |
| 108 | if( 0==sqlite3StrICmp(z, pEnc->zName) ){ |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | if( !pEnc->enc ){ |
| 113 | Tcl_AppendResult(interp, "No such encoding: ", z, 0); |
| 114 | } |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 115 | if( pEnc->enc==SQLITE_UTF16 ){ |
| 116 | return SQLITE_UTF16NATIVE; |
| 117 | } |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 118 | return pEnc->enc; |
| 119 | } |
| 120 | |
danielk1977 | 1ba1b55 | 2004-06-23 13:46:32 +0000 | [diff] [blame] | 121 | /* |
| 122 | ** Usage: test_translate <string/blob> <from enc> <to enc> ?<transient>? |
| 123 | ** |
| 124 | */ |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 125 | static int SQLITE_TCLAPI test_translate( |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 126 | void * clientData, |
| 127 | Tcl_Interp *interp, |
| 128 | int objc, |
| 129 | Tcl_Obj *CONST objv[] |
| 130 | ){ |
| 131 | u8 enc_from; |
| 132 | u8 enc_to; |
| 133 | sqlite3_value *pVal; |
| 134 | |
danielk1977 | 1ba1b55 | 2004-06-23 13:46:32 +0000 | [diff] [blame] | 135 | char *z; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 136 | int len; |
danielk1977 | 1ba1b55 | 2004-06-23 13:46:32 +0000 | [diff] [blame] | 137 | void (*xDel)(void *p) = SQLITE_STATIC; |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 138 | |
danielk1977 | 1ba1b55 | 2004-06-23 13:46:32 +0000 | [diff] [blame] | 139 | if( objc!=4 && objc!=5 ){ |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 140 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 141 | Tcl_GetStringFromObj(objv[0], 0), |
| 142 | " <string/blob> <from enc> <to enc>", 0 |
| 143 | ); |
| 144 | return TCL_ERROR; |
| 145 | } |
danielk1977 | 1ba1b55 | 2004-06-23 13:46:32 +0000 | [diff] [blame] | 146 | if( objc==5 ){ |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 147 | xDel = sqlite3_free; |
danielk1977 | 1ba1b55 | 2004-06-23 13:46:32 +0000 | [diff] [blame] | 148 | } |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 149 | |
| 150 | enc_from = name_to_enc(interp, objv[2]); |
| 151 | if( !enc_from ) return TCL_ERROR; |
| 152 | enc_to = name_to_enc(interp, objv[3]); |
| 153 | if( !enc_to ) return TCL_ERROR; |
| 154 | |
danielk1977 | 1e53695 | 2007-08-16 10:09:01 +0000 | [diff] [blame] | 155 | pVal = sqlite3ValueNew(0); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 156 | |
| 157 | if( enc_from==SQLITE_UTF8 ){ |
| 158 | z = Tcl_GetString(objv[1]); |
danielk1977 | 1ba1b55 | 2004-06-23 13:46:32 +0000 | [diff] [blame] | 159 | if( objc==5 ){ |
drh | b975598 | 2010-07-24 16:34:37 +0000 | [diff] [blame] | 160 | z = sqlite3_mprintf("%s", z); |
danielk1977 | 1ba1b55 | 2004-06-23 13:46:32 +0000 | [diff] [blame] | 161 | } |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 162 | sqlite3ValueSetStr(pVal, -1, z, enc_from, xDel); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 163 | }else{ |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 164 | z = (char*)Tcl_GetByteArrayFromObj(objv[1], &len); |
danielk1977 | 1ba1b55 | 2004-06-23 13:46:32 +0000 | [diff] [blame] | 165 | if( objc==5 ){ |
| 166 | char *zTmp = z; |
drh | 1743575 | 2007-08-16 04:30:38 +0000 | [diff] [blame] | 167 | z = sqlite3_malloc(len); |
danielk1977 | 1ba1b55 | 2004-06-23 13:46:32 +0000 | [diff] [blame] | 168 | memcpy(z, zTmp, len); |
| 169 | } |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 170 | sqlite3ValueSetStr(pVal, -1, z, enc_from, xDel); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 171 | } |
| 172 | |
drh | b21c8cd | 2007-08-21 19:33:56 +0000 | [diff] [blame] | 173 | z = (char *)sqlite3ValueText(pVal, enc_to); |
| 174 | len = sqlite3ValueBytes(pVal, enc_to) + (enc_to==SQLITE_UTF8?1:2); |
drh | 03d847e | 2005-12-09 20:21:58 +0000 | [diff] [blame] | 175 | Tcl_SetObjResult(interp, Tcl_NewByteArrayObj((u8*)z, len)); |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 176 | |
| 177 | sqlite3ValueFree(pVal); |
| 178 | |
| 179 | return TCL_OK; |
| 180 | } |
| 181 | |
| 182 | /* |
| 183 | ** Usage: translate_selftest |
| 184 | ** |
drh | ee85813 | 2007-05-08 20:37:38 +0000 | [diff] [blame] | 185 | ** Call sqlite3UtfSelfTest() to run the internal tests for unicode |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 186 | ** translation. If there is a problem an assert() will fail. |
| 187 | **/ |
danielk1977 | 44a376f | 2008-08-12 15:04:58 +0000 | [diff] [blame] | 188 | void sqlite3UtfSelfTest(void); |
mistachkin | 7617e4a | 2016-07-28 17:11:20 +0000 | [diff] [blame] | 189 | static int SQLITE_TCLAPI test_translate_selftest( |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 190 | void * clientData, |
| 191 | Tcl_Interp *interp, |
| 192 | int objc, |
| 193 | Tcl_Obj *CONST objv[] |
| 194 | ){ |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 195 | #ifndef SQLITE_OMIT_UTF16 |
drh | ee85813 | 2007-05-08 20:37:38 +0000 | [diff] [blame] | 196 | sqlite3UtfSelfTest(); |
drh | 6c62608 | 2004-11-14 21:56:29 +0000 | [diff] [blame] | 197 | #endif |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 198 | return SQLITE_OK; |
| 199 | } |
| 200 | |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 201 | |
| 202 | /* |
| 203 | ** Register commands with the TCL interpreter. |
| 204 | */ |
| 205 | int Sqlitetest5_Init(Tcl_Interp *interp){ |
| 206 | static struct { |
| 207 | char *zName; |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 208 | Tcl_ObjCmdProc *xProc; |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 209 | } aCmd[] = { |
danielk1977 | 24162fe | 2004-06-04 06:22:00 +0000 | [diff] [blame] | 210 | { "binarize", (Tcl_ObjCmdProc*)binarize }, |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 211 | { "test_value_overhead", (Tcl_ObjCmdProc*)test_value_overhead }, |
danielk1977 | bfd6cce | 2004-06-18 04:24:54 +0000 | [diff] [blame] | 212 | { "test_translate", (Tcl_ObjCmdProc*)test_translate }, |
| 213 | { "translate_selftest", (Tcl_ObjCmdProc*)test_translate_selftest}, |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 214 | }; |
| 215 | int i; |
| 216 | for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ |
danielk1977 | 295ba55 | 2004-05-19 10:34:51 +0000 | [diff] [blame] | 217 | Tcl_CreateObjCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 218 | } |
danielk1977 | 0202b29 | 2004-06-09 09:55:16 +0000 | [diff] [blame] | 219 | return SQLITE_OK; |
danielk1977 | 998b56c | 2004-05-06 23:37:52 +0000 | [diff] [blame] | 220 | } |