OLD | NEW |
1 /* | 1 /* |
2 ** 2007 April 6 | 2 ** 2007 April 6 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
11 ************************************************************************* | 11 ************************************************************************* |
12 ** Code for testing all sorts of SQLite interfaces. This code | 12 ** Code for testing all sorts of SQLite interfaces. This code |
13 ** implements TCL commands for reading and writing the binary | 13 ** implements TCL commands for reading and writing the binary |
14 ** database files and displaying the content of those files as | 14 ** database files and displaying the content of those files as |
15 ** hexadecimal. We could, in theory, use the built-in "binary" | 15 ** hexadecimal. We could, in theory, use the built-in "binary" |
16 ** command of TCL to do a lot of this, but there are some issues | 16 ** command of TCL to do a lot of this, but there are some issues |
17 ** with historical versions of the "binary" command. So it seems | 17 ** with historical versions of the "binary" command. So it seems |
18 ** easier and safer to build our own mechanism. | 18 ** easier and safer to build our own mechanism. |
19 */ | 19 */ |
20 #include "sqliteInt.h" | 20 #include "sqliteInt.h" |
21 #include "tcl.h" | 21 #if defined(INCLUDE_SQLITE_TCL_H) |
| 22 # include "sqlite_tcl.h" |
| 23 #else |
| 24 # include "tcl.h" |
| 25 #endif |
22 #include <stdlib.h> | 26 #include <stdlib.h> |
23 #include <string.h> | 27 #include <string.h> |
24 #include <assert.h> | 28 #include <assert.h> |
25 | 29 |
26 | 30 |
27 /* | 31 /* |
28 ** Convert binary to hex. The input zBuf[] contains N bytes of | 32 ** Convert binary to hex. The input zBuf[] contains N bytes of |
29 ** binary data. zBuf[] is 2*n+1 bytes long. Overwrite zBuf[] | 33 ** binary data. zBuf[] is 2*n+1 bytes long. Overwrite zBuf[] |
30 ** with a hexadecimal representation of its original binary input. | 34 ** with a hexadecimal representation of its original binary input. |
31 */ | 35 */ |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 } | 91 } |
88 | 92 |
89 | 93 |
90 /* | 94 /* |
91 ** Usage: hexio_read FILENAME OFFSET AMT | 95 ** Usage: hexio_read FILENAME OFFSET AMT |
92 ** | 96 ** |
93 ** Read AMT bytes from file FILENAME beginning at OFFSET from the | 97 ** Read AMT bytes from file FILENAME beginning at OFFSET from the |
94 ** beginning of the file. Convert that information to hexadecimal | 98 ** beginning of the file. Convert that information to hexadecimal |
95 ** and return the resulting HEX string. | 99 ** and return the resulting HEX string. |
96 */ | 100 */ |
97 static int hexio_read( | 101 static int SQLITE_TCLAPI hexio_read( |
98 void * clientData, | 102 void * clientData, |
99 Tcl_Interp *interp, | 103 Tcl_Interp *interp, |
100 int objc, | 104 int objc, |
101 Tcl_Obj *CONST objv[] | 105 Tcl_Obj *CONST objv[] |
102 ){ | 106 ){ |
103 int offset; | 107 int offset; |
104 int amt, got; | 108 int amt, got; |
105 const char *zFile; | 109 const char *zFile; |
106 unsigned char *zBuf; | 110 unsigned char *zBuf; |
107 FILE *in; | 111 FILE *in; |
(...skipping 29 matching lines...) Expand all Loading... |
137 return TCL_OK; | 141 return TCL_OK; |
138 } | 142 } |
139 | 143 |
140 | 144 |
141 /* | 145 /* |
142 ** Usage: hexio_write FILENAME OFFSET DATA | 146 ** Usage: hexio_write FILENAME OFFSET DATA |
143 ** | 147 ** |
144 ** Write DATA into file FILENAME beginning at OFFSET from the | 148 ** Write DATA into file FILENAME beginning at OFFSET from the |
145 ** beginning of the file. DATA is expressed in hexadecimal. | 149 ** beginning of the file. DATA is expressed in hexadecimal. |
146 */ | 150 */ |
147 static int hexio_write( | 151 static int SQLITE_TCLAPI hexio_write( |
148 void * clientData, | 152 void * clientData, |
149 Tcl_Interp *interp, | 153 Tcl_Interp *interp, |
150 int objc, | 154 int objc, |
151 Tcl_Obj *CONST objv[] | 155 Tcl_Obj *CONST objv[] |
152 ){ | 156 ){ |
153 int offset; | 157 int offset; |
154 int nIn, nOut, written; | 158 int nIn, nOut, written; |
155 const char *zFile; | 159 const char *zFile; |
156 const unsigned char *zIn; | 160 const unsigned char *zIn; |
157 unsigned char *aOut; | 161 unsigned char *aOut; |
(...skipping 27 matching lines...) Expand all Loading... |
185 return TCL_OK; | 189 return TCL_OK; |
186 } | 190 } |
187 | 191 |
188 /* | 192 /* |
189 ** USAGE: hexio_get_int HEXDATA | 193 ** USAGE: hexio_get_int HEXDATA |
190 ** | 194 ** |
191 ** Interpret the HEXDATA argument as a big-endian integer. Return | 195 ** Interpret the HEXDATA argument as a big-endian integer. Return |
192 ** the value of that integer. HEXDATA can contain between 2 and 8 | 196 ** the value of that integer. HEXDATA can contain between 2 and 8 |
193 ** hexadecimal digits. | 197 ** hexadecimal digits. |
194 */ | 198 */ |
195 static int hexio_get_int( | 199 static int SQLITE_TCLAPI hexio_get_int( |
196 void * clientData, | 200 void * clientData, |
197 Tcl_Interp *interp, | 201 Tcl_Interp *interp, |
198 int objc, | 202 int objc, |
199 Tcl_Obj *CONST objv[] | 203 Tcl_Obj *CONST objv[] |
200 ){ | 204 ){ |
201 int val; | 205 int val; |
202 int nIn, nOut; | 206 int nIn, nOut; |
203 const unsigned char *zIn; | 207 const unsigned char *zIn; |
204 unsigned char *aOut; | 208 unsigned char *aOut; |
205 unsigned char aNum[4]; | 209 unsigned char aNum[4]; |
(...skipping 19 matching lines...) Expand all Loading... |
225 Tcl_SetObjResult(interp, Tcl_NewIntObj(val)); | 229 Tcl_SetObjResult(interp, Tcl_NewIntObj(val)); |
226 return TCL_OK; | 230 return TCL_OK; |
227 } | 231 } |
228 | 232 |
229 | 233 |
230 /* | 234 /* |
231 ** USAGE: hexio_render_int16 INTEGER | 235 ** USAGE: hexio_render_int16 INTEGER |
232 ** | 236 ** |
233 ** Render INTEGER has a 16-bit big-endian integer in hexadecimal. | 237 ** Render INTEGER has a 16-bit big-endian integer in hexadecimal. |
234 */ | 238 */ |
235 static int hexio_render_int16( | 239 static int SQLITE_TCLAPI hexio_render_int16( |
236 void * clientData, | 240 void * clientData, |
237 Tcl_Interp *interp, | 241 Tcl_Interp *interp, |
238 int objc, | 242 int objc, |
239 Tcl_Obj *CONST objv[] | 243 Tcl_Obj *CONST objv[] |
240 ){ | 244 ){ |
241 int val; | 245 int val; |
242 unsigned char aNum[10]; | 246 unsigned char aNum[10]; |
243 | 247 |
244 if( objc!=2 ){ | 248 if( objc!=2 ){ |
245 Tcl_WrongNumArgs(interp, 1, objv, "INTEGER"); | 249 Tcl_WrongNumArgs(interp, 1, objv, "INTEGER"); |
246 return TCL_ERROR; | 250 return TCL_ERROR; |
247 } | 251 } |
248 if( Tcl_GetIntFromObj(interp, objv[1], &val) ) return TCL_ERROR; | 252 if( Tcl_GetIntFromObj(interp, objv[1], &val) ) return TCL_ERROR; |
249 aNum[0] = val>>8; | 253 aNum[0] = val>>8; |
250 aNum[1] = val; | 254 aNum[1] = val; |
251 sqlite3TestBinToHex(aNum, 2); | 255 sqlite3TestBinToHex(aNum, 2); |
252 Tcl_SetObjResult(interp, Tcl_NewStringObj((char*)aNum, 4)); | 256 Tcl_SetObjResult(interp, Tcl_NewStringObj((char*)aNum, 4)); |
253 return TCL_OK; | 257 return TCL_OK; |
254 } | 258 } |
255 | 259 |
256 | 260 |
257 /* | 261 /* |
258 ** USAGE: hexio_render_int32 INTEGER | 262 ** USAGE: hexio_render_int32 INTEGER |
259 ** | 263 ** |
260 ** Render INTEGER has a 32-bit big-endian integer in hexadecimal. | 264 ** Render INTEGER has a 32-bit big-endian integer in hexadecimal. |
261 */ | 265 */ |
262 static int hexio_render_int32( | 266 static int SQLITE_TCLAPI hexio_render_int32( |
263 void * clientData, | 267 void * clientData, |
264 Tcl_Interp *interp, | 268 Tcl_Interp *interp, |
265 int objc, | 269 int objc, |
266 Tcl_Obj *CONST objv[] | 270 Tcl_Obj *CONST objv[] |
267 ){ | 271 ){ |
268 int val; | 272 int val; |
269 unsigned char aNum[10]; | 273 unsigned char aNum[10]; |
270 | 274 |
271 if( objc!=2 ){ | 275 if( objc!=2 ){ |
272 Tcl_WrongNumArgs(interp, 1, objv, "INTEGER"); | 276 Tcl_WrongNumArgs(interp, 1, objv, "INTEGER"); |
273 return TCL_ERROR; | 277 return TCL_ERROR; |
274 } | 278 } |
275 if( Tcl_GetIntFromObj(interp, objv[1], &val) ) return TCL_ERROR; | 279 if( Tcl_GetIntFromObj(interp, objv[1], &val) ) return TCL_ERROR; |
276 aNum[0] = val>>24; | 280 aNum[0] = val>>24; |
277 aNum[1] = val>>16; | 281 aNum[1] = val>>16; |
278 aNum[2] = val>>8; | 282 aNum[2] = val>>8; |
279 aNum[3] = val; | 283 aNum[3] = val; |
280 sqlite3TestBinToHex(aNum, 4); | 284 sqlite3TestBinToHex(aNum, 4); |
281 Tcl_SetObjResult(interp, Tcl_NewStringObj((char*)aNum, 8)); | 285 Tcl_SetObjResult(interp, Tcl_NewStringObj((char*)aNum, 8)); |
282 return TCL_OK; | 286 return TCL_OK; |
283 } | 287 } |
284 | 288 |
285 /* | 289 /* |
286 ** USAGE: utf8_to_utf8 HEX | 290 ** USAGE: utf8_to_utf8 HEX |
287 ** | 291 ** |
288 ** The argument is a UTF8 string represented in hexadecimal. | 292 ** The argument is a UTF8 string represented in hexadecimal. |
289 ** The UTF8 might not be well-formed. Run this string through | 293 ** The UTF8 might not be well-formed. Run this string through |
290 ** sqlite3Utf8to8() convert it back to hex and return the result. | 294 ** sqlite3Utf8to8() convert it back to hex and return the result. |
291 */ | 295 */ |
292 static int utf8_to_utf8( | 296 static int SQLITE_TCLAPI utf8_to_utf8( |
293 void * clientData, | 297 void * clientData, |
294 Tcl_Interp *interp, | 298 Tcl_Interp *interp, |
295 int objc, | 299 int objc, |
296 Tcl_Obj *CONST objv[] | 300 Tcl_Obj *CONST objv[] |
297 ){ | 301 ){ |
298 #ifdef SQLITE_DEBUG | 302 #ifdef SQLITE_DEBUG |
299 int n; | 303 int n; |
300 int nOut; | 304 int nOut; |
301 const unsigned char *zOrig; | 305 const unsigned char *zOrig; |
302 unsigned char *z; | 306 unsigned char *z; |
(...skipping 30 matching lines...) Expand all Loading... |
333 return (int) (q - (unsigned char *)p); | 337 return (int) (q - (unsigned char *)p); |
334 } | 338 } |
335 | 339 |
336 | 340 |
337 /* | 341 /* |
338 ** USAGE: read_fts3varint BLOB VARNAME | 342 ** USAGE: read_fts3varint BLOB VARNAME |
339 ** | 343 ** |
340 ** Read a varint from the start of BLOB. Set variable VARNAME to contain | 344 ** Read a varint from the start of BLOB. Set variable VARNAME to contain |
341 ** the interpreted value. Return the number of bytes of BLOB consumed. | 345 ** the interpreted value. Return the number of bytes of BLOB consumed. |
342 */ | 346 */ |
343 static int read_fts3varint( | 347 static int SQLITE_TCLAPI read_fts3varint( |
344 void * clientData, | 348 void * clientData, |
345 Tcl_Interp *interp, | 349 Tcl_Interp *interp, |
346 int objc, | 350 int objc, |
347 Tcl_Obj *CONST objv[] | 351 Tcl_Obj *CONST objv[] |
348 ){ | 352 ){ |
349 int nBlob; | 353 int nBlob; |
350 unsigned char *zBlob; | 354 unsigned char *zBlob; |
351 sqlite3_int64 iVal; | 355 sqlite3_int64 iVal; |
352 int nVal; | 356 int nVal; |
353 | 357 |
(...skipping 25 matching lines...) Expand all Loading... |
379 { "hexio_render_int32", hexio_render_int32 }, | 383 { "hexio_render_int32", hexio_render_int32 }, |
380 { "utf8_to_utf8", utf8_to_utf8 }, | 384 { "utf8_to_utf8", utf8_to_utf8 }, |
381 { "read_fts3varint", read_fts3varint }, | 385 { "read_fts3varint", read_fts3varint }, |
382 }; | 386 }; |
383 int i; | 387 int i; |
384 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ | 388 for(i=0; i<sizeof(aObjCmd)/sizeof(aObjCmd[0]); i++){ |
385 Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, 0, 0); | 389 Tcl_CreateObjCommand(interp, aObjCmd[i].zName, aObjCmd[i].xProc, 0, 0); |
386 } | 390 } |
387 return TCL_OK; | 391 return TCL_OK; |
388 } | 392 } |
OLD | NEW |