From 90e4d95d14d5fa11d4cd535be2ae0d8ee0365044 Mon Sep 17 00:00:00 2001 From: danielk1977 Date: Mon, 10 May 2004 10:05:53 +0000 Subject: [PATCH] Add some functions to serialize and deserialize vdbe values (used by manifest typing). (CVS 1336) FossilOrigin-Name: 05434497ba5d9971d23144eb4b9d709c233f6546 --- manifest | 16 ++--- manifest.uuid | 2 +- src/sqliteInt.h | 4 +- src/util.c | 4 +- src/vdbeaux.c | 158 ++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 171 insertions(+), 13 deletions(-) diff --git a/manifest b/manifest index bc90828e62..4b4e10872a 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Add\sversions\sof\sOP_MakeRecord\sand\sOP_Column\sthat\suse\smanifest\styping\s(not\nactivated\syet).\s(CVS\s1335) -D 2004-05-10T07:17:32 +C Add\ssome\sfunctions\sto\sserialize\sand\sdeserialize\svdbe\svalues\s(used\nby\smanifest\styping).\s(CVS\s1336) +D 2004-05-10T10:05:54 F Makefile.in ab7b0d5118e2da97bac66be8684a1034e3500f5a F Makefile.linux-gcc b86a99c493a5bfb402d1d9178dcdc4bd4b32f906 F README f1de682fbbd94899d50aca13d387d1b3fd3be2dd @@ -49,7 +49,7 @@ F src/random.c eff68e3f257e05e81eae6c4d50a51eb88beb4ff3 F src/select.c d3868613e70ac09cf2fb4ddd211f088351d1fe51 F src/shell.c 9a242ad03c8726823f182bff81c5e12ef2882f93 F src/sqlite.h.in 532e08163a8df8b71faf7be4e69ba208938eb312 -F src/sqliteInt.h 987dd79044a37a37f1c386d5e3bcd702c62e27ab +F src/sqliteInt.h ad857ab418724bcecad9041de1c4cf3be10c6f55 F src/table.c 882b0ae9524c67758157540dd6467c9a5de52335 F src/tclsqlite.c 21147148e7b57a0bb7409cff5878c60470df9acc F src/test1.c 67a72fa1f5f6d41c838fa97f324f8dbfb4051413 @@ -61,12 +61,12 @@ F src/tokenize.c 256a3cf0ff938cd79774d7f95173d74281912df9 F src/trigger.c 8b07ff87b39f24997e5f737340ebbb5ce09ca481 F src/update.c 475465fc0582160dadf5455b05235cb13c9e21f9 F src/utf.c fc799748d43fe1982d157b871e3e420a19c85d4f -F src/util.c da3b43129c8912eeee1ef0599e740331ed5b8c39 +F src/util.c 06f472823240ec2012586c55fee33a523d32d07b F src/vacuum.c 91ba5a23eca2d9a8a11191cef577d417f9318976 F src/vdbe.c 3d90858d784c4353a6e5ee45a0e4d82b9ba2aa5c F src/vdbe.h 2dc4d1161b64f5684faa6a2d292e318a185ecb2e F src/vdbeInt.h e4ab46c223bac43754335cdd14c9c6a95885b9a8 -F src/vdbeaux.c 4dac7718b04e6773aba8586221faadd62aa742a7 +F src/vdbeaux.c 0256d8b9a355a90f24f992dd7b6f0d2373e3e957 F src/where.c 6db0291280f2c642bae2c69a70750325b53717a4 F test/all.test 569a92a8ee88f5300c057cc4a8f50fbbc69a3242 F test/attach.test ba8261d38da6b6a7d4f78ec543c548c4418582ef @@ -187,7 +187,7 @@ F www/sqlite.tcl 3c83b08cf9f18aa2d69453ff441a36c40e431604 F www/tclsqlite.tcl b9271d44dcf147a93c98f8ecf28c927307abd6da F www/vdbe.tcl 9b9095d4495f37697fd1935d10e14c6015e80aa1 F www/whentouse.tcl a8335bce47cc2fddb07f19052cb0cb4d9129a8e4 -P 8a66a502ba09e3d858d2f45df9b3b665ebb85d5b -R c990744bdc97fc1baad79ce9e836f1a6 +P 9ea8e8ab23d46adb2970ccf62340703ec42345fa +R 618584a23469029fa2139630ad76562b U danielk1977 -Z 4148e4ef2621bb583a3f2ac3e7707944 +Z 0608f60d741fd851f2193190bf722d0f diff --git a/manifest.uuid b/manifest.uuid index d2ca6f890f..d67194adc0 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -9ea8e8ab23d46adb2970ccf62340703ec42345fa \ No newline at end of file +05434497ba5d9971d23144eb4b9d709c233f6546 \ No newline at end of file diff --git a/src/sqliteInt.h b/src/sqliteInt.h index 8360f493ab..c2a5be9174 100644 --- a/src/sqliteInt.h +++ b/src/sqliteInt.h @@ -11,7 +11,7 @@ ************************************************************************* ** Internal interface definitions for SQLite. ** -** @(#) $Id: sqliteInt.h,v 1.226 2004/05/10 07:17:31 danielk1977 Exp $ +** @(#) $Id: sqliteInt.h,v 1.227 2004/05/10 10:05:54 danielk1977 Exp $ */ #include "config.h" #include "sqlite.h" @@ -1285,6 +1285,6 @@ void *sqlite3utf8to16le(const unsigned char *pIn, int N); void sqlite3utf16to16le(void *pData, int N); void sqlite3utf16to16be(void *pData, int N); int sqlite3PutVarint(unsigned char *, u64); -int sqlite3GetVarint(unsigned char *, u64 *); +int sqlite3GetVarint(const unsigned char *, u64 *); int sqlite3VarintLen(u64 v); diff --git a/src/util.c b/src/util.c index a3df127d3f..420a7c2072 100644 --- a/src/util.c +++ b/src/util.c @@ -14,7 +14,7 @@ ** This file contains functions for allocating memory, comparing ** strings, and stuff like that. ** -** $Id: util.c,v 1.77 2004/05/10 07:17:32 danielk1977 Exp $ +** $Id: util.c,v 1.78 2004/05/10 10:05:54 danielk1977 Exp $ */ #include "sqliteInt.h" #include @@ -1144,7 +1144,7 @@ int sqlite3PutVarint(unsigned char *p, u64 v){ return i; } -int sqlite3GetVarint(unsigned char *p, u64 *v){ +int sqlite3GetVarint(const unsigned char *p, u64 *v){ u64 x = p[0] & 0x7f; int n = 0; while( (p[n++]&0x80)!=0 ){ diff --git a/src/vdbeaux.c b/src/vdbeaux.c index 2900af6e61..1bfb10e77d 100644 --- a/src/vdbeaux.c +++ b/src/vdbeaux.c @@ -1100,6 +1100,31 @@ int sqlite2BtreeKeyCompare( return rc; } +/* +** The following three functions: +** +** sqlite3VdbeSerialize() +** sqlite3VdbeSerialLen() +** sqlite3VdbeDeserialize() +** +** encapsulate the code that serializes values for storage in SQLite +** databases. Each serialized value consists of a variable length integer +** followed by type specific storage. +** +** initial varint bytes to follow type +** -------------- --------------- --------------- +** 0 0 NULL +** 1 1 signed integer +** 2 2 signed integer +** 3 4 signed integer +** 4 8 signed integer +** 5 8 IEEE float +** 6..12 reserved for expansion +** N>=12 and even (N-12)/2 BLOB +** N>=13 and odd (N-13)/2 text +** +*/ + /* ** Write the serialized form of the value held by pMem into zBuf. Return ** the number of bytes written. @@ -1108,6 +1133,58 @@ int sqlite3VdbeSerialize( const Mem *pMem, /* Pointer to vdbe value to serialize */ unsigned char *zBuf /* Buffer to write to */ ){ + if( pMem->flags&MEM_Null ){ + return sqlite3PutVarint(zBuf, 0); + } + + if( pMem->flags&MEM_Real ){ + assert(!"TODO: float"); + } + + if( pMem->flags&MEM_Str ){ + int data_type_len; + u64 data_type = (pMem->n*2+31); + + data_type_len = sqlite3PutVarint(zBuf, data_type); + memcpy(&zBuf[data_type_len], pMem->z, pMem->n); + return pMem->n + data_type_len; + } + + if( pMem->flags& MEM_Int ){ + u64 absval; + int size = 8; + int ii; + + if( pMem->i<0 ){ + absval = pMem->i * -1; + }else{ + absval = pMem->i; + } + if( absval<=127 ){ + size = 1; + sqlite3PutVarint(zBuf, 1); + }else if( absval<=32767 ){ + size = 2; + sqlite3PutVarint(zBuf, 2); + }else if( absval<=2147483647 ){ + size = 4; + sqlite3PutVarint(zBuf, 3); + }else{ + size = 8; + sqlite3PutVarint(zBuf, 4); + } + + for(ii=0; iii >> (8*ii)) & 0xFF; + } + if( pMem->i<0 ){ + zBuf[size] = zBuf[size] & 0x80; + } + + return size+1; + } + + return -1; } /* @@ -1115,6 +1192,29 @@ int sqlite3VdbeSerialize( ** form of the value held by pMem. Return negative if an error occurs. */ int sqlite3VdbeSerialLen(const Mem *pMem){ + if( pMem->flags&MEM_Null ){ + return 1; /* Varint 0 is 1 byte */ + } + if( pMem->flags&MEM_Real ){ + return 9; /* Varing 5 (1 byte) + 8 bytes IEEE float */ + } + if( pMem->flags&MEM_Str ){ + return pMem->n + sqlite3VarintLen((pMem->n*2)+13); + } + if( pMem->flags& MEM_Int ){ + u64 absval; + if( pMem->i<0 ){ + absval = pMem->i * -1; + }else{ + absval = pMem->i; + } + if( absval<=127 ) return 2; /* 1 byte integer */ + if( absval<=32767 ) return 3; /* 2 byte integer */ + if( absval<=2147483647 ) return 5; /* 4 byte integer */ + return 9; /* 8 byte integer */ + } + + return -1; } /* @@ -1125,6 +1225,64 @@ int sqlite3VdbeDeserialize( Mem *pMem, /* structure to write new value to */ const unsigned char *zBuf /* Buffer to read from */ ){ + u64 data_type; + int ret; + int len; + + memset(pMem, 0, sizeof(Mem)); + ret = sqlite3GetVarint(zBuf, &data_type); + + if( data_type==0 ){ /* NULL */ + pMem->flags = MEM_Null; + return ret; + } + + /* FIX ME: update for 8-byte integers */ + if( data_type>0 && data_type<5 ){ /* 1, 2, 4 or 8 byte integer */ + int ii; + int bytes = 1 << (data_type-1); + + pMem->flags = MEM_Int; + pMem->i = 0; + + for(ii=0; iii = pMem->i<<8 + zBuf[ii+ret]; + } + + /* If this is a 1, 2 or 4 byte integer, extend the sign-bit if need be. */ + if( bytes<8 && pMem->i & (1<<(bytes*8-1)) ){ + pMem->i = pMem->i - (1<<(bytes*8)); + } + + return ret+bytes; + } + + if( data_type==5 ){ /* IEEE float */ + assert(!"TODO: float"); + } + + /* Must be text or a blob */ + assert( data_type>=12 ); + len = (data_type-12)/2; + pMem->flags = MEM_Str; /* FIX ME: there should be a MEM_Blob or similar */ + + /* If the length of the text or blob is greater than NBFS, use space + ** dynamically allocated. Otherwise, store the value in Mem::zShort. + */ + if( len>NBFS ){ + pMem->z = sqliteMalloc( len ); + if( !pMem->z ){ + return -1; + } + pMem->flags |= MEM_Dyn; + }else{ + pMem->z = pMem->zShort; + pMem->flags |= MEM_Short; + } + memcpy(pMem->z, &zBuf[ret], len); + ret += len; + + return ret; } /*