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 |
| 14 | ** testing of the SQLite library. |
| 15 | ** |
| 16 | ** $Id: |
| 17 | */ |
| 18 | #include "sqliteInt.h" |
| 19 | #include "tcl.h" |
| 20 | #include <stdlib.h> |
| 21 | #include <string.h> |
| 22 | |
| 23 | /* |
| 24 | ** Return the number of bytes up to and including the first \u0000 |
| 25 | ** character in *pStr. |
| 26 | */ |
| 27 | static int utf16_length(const unsigned char *pZ){ |
| 28 | const unsigned char *pC1 = pZ; |
| 29 | const unsigned char *pC2 = pZ+1; |
| 30 | while( *pC1 || *pC2 ){ |
| 31 | pC1 += 2; |
| 32 | pC2 += 2; |
| 33 | } |
| 34 | return (pC1-pZ)+2; |
| 35 | } |
| 36 | |
| 37 | static int sqlite_utf8to16le( |
| 38 | void * clientData, |
| 39 | Tcl_Interp *interp, |
| 40 | int objc, |
| 41 | Tcl_Obj *CONST objv[] |
| 42 | ){ |
| 43 | unsigned char *out; |
| 44 | unsigned char *in; |
| 45 | Tcl_Obj *res; |
| 46 | |
| 47 | if( objc!=2 ){ |
| 48 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 49 | Tcl_GetStringFromObj(objv[0], 0), "<utf-8 encoded-string>", 0); |
| 50 | return TCL_ERROR; |
| 51 | } |
| 52 | |
| 53 | in = Tcl_GetByteArrayFromObj(objv[1], 0); |
| 54 | out = (unsigned char *)sqlite3utf8to16le(in, -1); |
| 55 | res = Tcl_NewByteArrayObj(out, utf16_length(ret)); |
| 56 | sqliteFree(out); |
| 57 | |
| 58 | Tcl_SetObjResult(interp, res); |
| 59 | |
| 60 | return TCL_OK; |
| 61 | } |
| 62 | |
| 63 | static int sqlite_utf8to16be( |
| 64 | void * clientData, |
| 65 | Tcl_Interp *interp, |
| 66 | int objc, |
| 67 | Tcl_Obj *CONST objv[] |
| 68 | ){ |
| 69 | unsigned char *out; |
| 70 | unsigned char *in; |
| 71 | Tcl_Obj *res; |
| 72 | |
| 73 | if( objc!=2 ){ |
| 74 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 75 | Tcl_GetStringFromObj(objv[0], 0), "<utf-8 encoded-string>", 0); |
| 76 | return TCL_ERROR; |
| 77 | } |
| 78 | |
| 79 | in = Tcl_GetByteArrayFromObj(objv[1], 0); |
| 80 | out = (unsigned char *)sqlite3utf8to16be(in, -1); |
| 81 | res = Tcl_NewByteArrayObj(out, utf16_length(ret)); |
| 82 | sqliteFree(out); |
| 83 | |
| 84 | Tcl_SetObjResult(interp, res); |
| 85 | |
| 86 | return TCL_OK; |
| 87 | } |
| 88 | |
| 89 | static int sqlite_utf16to16le( |
| 90 | void * clientData, |
| 91 | Tcl_Interp *interp, |
| 92 | int objc, |
| 93 | Tcl_Obj *CONST objv[] |
| 94 | ){ |
| 95 | unsigned char *out; |
| 96 | unsigned char *in; |
| 97 | int in_len; |
| 98 | Tcl_Obj *res; |
| 99 | |
| 100 | if( objc!=2 ){ |
| 101 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 102 | Tcl_GetStringFromObj(objv[0], 0), "<utf-16 encoded-string>", 0); |
| 103 | return TCL_ERROR; |
| 104 | } |
| 105 | |
| 106 | in = Tcl_GetByteArrayFromObj(objv[1], &in_len); |
| 107 | out = (unsigned char *)sqliteMalloc(in_len); |
| 108 | memcpy(out, in, in_len); |
| 109 | |
| 110 | sqlite3utf16to16le(out, -1); |
| 111 | res = Tcl_NewByteArrayObj(out, utf16_length(ret)); |
| 112 | sqliteFree(out); |
| 113 | |
| 114 | Tcl_SetObjResult(interp, res); |
| 115 | |
| 116 | return TCL_OK; |
| 117 | } |
| 118 | |
| 119 | static int sqlite_utf16to16be( |
| 120 | void * clientData, |
| 121 | Tcl_Interp *interp, |
| 122 | int objc, |
| 123 | Tcl_Obj *CONST objv[] |
| 124 | ){ |
| 125 | unsigned char *out; |
| 126 | unsigned char *in; |
| 127 | int in_len; |
| 128 | Tcl_Obj *res; |
| 129 | |
| 130 | if( objc!=2 ){ |
| 131 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 132 | Tcl_GetStringFromObj(objv[0], 0), "<utf-16 encoded-string>", 0); |
| 133 | return TCL_ERROR; |
| 134 | } |
| 135 | |
| 136 | in = Tcl_GetByteArrayFromObj(objv[1], &in_len); |
| 137 | out = (unsigned char *)sqliteMalloc(in_len); |
| 138 | memcpy(out, in, in_len); |
| 139 | |
| 140 | sqlite3utf16to16be(out, -1); |
| 141 | res = Tcl_NewByteArrayObj(out, utf16_length(ret)); |
| 142 | sqliteFree(out); |
| 143 | |
| 144 | Tcl_SetObjResult(interp, res); |
| 145 | |
| 146 | return TCL_OK; |
| 147 | } |
| 148 | |
| 149 | static int sqlite_utf16to8( |
| 150 | void * clientData, |
| 151 | Tcl_Interp *interp, |
| 152 | int objc, |
| 153 | Tcl_Obj *CONST objv[] |
| 154 | ){ |
| 155 | unsigned char *out; |
| 156 | unsigned char *in; |
| 157 | Tcl_Obj *res; |
| 158 | |
| 159 | if( objc!=2 ){ |
| 160 | Tcl_AppendResult(interp, "wrong # args: should be \"", |
| 161 | Tcl_GetStringFromObj(objv[0], 0), "<utf-16 encoded-string>", 0); |
| 162 | return TCL_ERROR; |
| 163 | } |
| 164 | |
| 165 | in = Tcl_GetByteArrayFromObj(objv[1], 0); |
| 166 | out = sqlite3utf16to8(in, -1); |
| 167 | res = Tcl_NewByteArrayObj(out, strlen(ret)); |
| 168 | sqliteFree(out); |
| 169 | |
| 170 | Tcl_SetObjResult(interp, res); |
| 171 | |
| 172 | return TCL_OK; |
| 173 | } |
| 174 | |
| 175 | |
| 176 | /* |
| 177 | ** Register commands with the TCL interpreter. |
| 178 | */ |
| 179 | int Sqlitetest5_Init(Tcl_Interp *interp){ |
| 180 | static struct { |
| 181 | char *zName; |
| 182 | Tcl_CmdProc *xProc; |
| 183 | } aCmd[] = { |
| 184 | { "sqlite_utf16to8", (Tcl_CmdProc*)sqlite_utf16to8 }, |
| 185 | { "sqlite_utf8to16le", (Tcl_CmdProc*)sqlite_utf8to16le }, |
| 186 | { "sqlite_utf8to16be", (Tcl_CmdProc*)sqlite_utf8to16be }, |
| 187 | { "sqlite_utf16to16le", (Tcl_CmdProc*)sqlite_utf16to16le }, |
| 188 | { "sqlite_utf16to16be", (Tcl_CmdProc*)sqlite_utf16to16be } |
| 189 | }; |
| 190 | int i; |
| 191 | for(i=0; i<sizeof(aCmd)/sizeof(aCmd[0]); i++){ |
| 192 | Tcl_CreateCommand(interp, aCmd[i].zName, aCmd[i].xProc, 0, 0); |
| 193 | } |
| 194 | |
| 195 | return TCL_OK; |
| 196 | } |