/ Check-in [05434497]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add some functions to serialize and deserialize vdbe values (used by manifest typing). (CVS 1336)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 05434497ba5d9971d23144eb4b9d709c233f6546
User & Date: danielk1977 2004-05-10 10:05:54
Context
2004-05-10
10:34
Change the names of external symbols from sqlite_XXX to sqlite3_XXX. (CVS 1337) check-in: ba2ba242 user: danielk1977 tags: trunk
10:05
Add some functions to serialize and deserialize vdbe values (used by manifest typing). (CVS 1336) check-in: 05434497 user: danielk1977 tags: trunk
07:17
Add versions of OP_MakeRecord and OP_Column that use manifest typing (not activated yet). (CVS 1335) check-in: 9ea8e8ab user: danielk1977 tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/sqliteInt.h.

7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
....
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** Internal interface definitions for SQLite.
**
** @(#) $Id: sqliteInt.h,v 1.226 2004/05/10 07:17:31 danielk1977 Exp $
*/
#include "config.h"
#include "sqlite.h"
#include "hash.h"
#include "parse.h"
#include <stdio.h>
#include <stdlib.h>
................................................................................

unsigned char *sqlite3utf16to8(const void *pData, int N);
void *sqlite3utf8to16be(const unsigned char *pIn, int N);
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 sqlite3VarintLen(u64 v);








|







 







|


7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
....
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
**    May you do good and not evil.
**    May you find forgiveness for yourself and forgive others.
**    May you share freely, never taking more than you give.
**
*************************************************************************
** Internal interface definitions for SQLite.
**
** @(#) $Id: sqliteInt.h,v 1.227 2004/05/10 10:05:54 danielk1977 Exp $
*/
#include "config.h"
#include "sqlite.h"
#include "hash.h"
#include "parse.h"
#include <stdio.h>
#include <stdlib.h>
................................................................................

unsigned char *sqlite3utf16to8(const void *pData, int N);
void *sqlite3utf8to16be(const unsigned char *pIn, int N);
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(const unsigned char *, u64 *);
int sqlite3VarintLen(u64 v);

Changes to src/util.c.

10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
....
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
**
*************************************************************************
** Utility functions used throughout sqlite.
**
** 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 $
*/
#include "sqliteInt.h"
#include <stdarg.h>
#include <ctype.h>

/*
** If malloc() ever fails, this global variable gets set to 1.
................................................................................
    p[i++] = (v & 0x7f) | 0x80;
    v >>= 7;
  }while( v!=0 );
  p[i-1] &= 0x7f;
  return i;
}

int sqlite3GetVarint(unsigned char *p, u64 *v){
  u64 x = p[0] & 0x7f;
  int n = 0;
  while( (p[n++]&0x80)!=0 ){
    x |= (p[n]&0x7f)<<(n*7);
  }
  *v = x;
  return n;







|







 







|







10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
....
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
**
*************************************************************************
** Utility functions used throughout sqlite.
**
** This file contains functions for allocating memory, comparing
** strings, and stuff like that.
**
** $Id: util.c,v 1.78 2004/05/10 10:05:54 danielk1977 Exp $
*/
#include "sqliteInt.h"
#include <stdarg.h>
#include <ctype.h>

/*
** If malloc() ever fails, this global variable gets set to 1.
................................................................................
    p[i++] = (v & 0x7f) | 0x80;
    v >>= 7;
  }while( v!=0 );
  p[i-1] &= 0x7f;
  return i;
}

int sqlite3GetVarint(const unsigned char *p, u64 *v){
  u64 x = p[0] & 0x7f;
  int n = 0;
  while( (p[n++]&0x80)!=0 ){
    x |= (p[n]&0x7f)<<(n*7);
  }
  *v = x;
  return n;

Changes to src/vdbeaux.c.

1096
1097
1098
1099
1100
1101
1102
1103

























1104
1105
1106
1107
1108
1109
1110


1111
1112


















































1113
1114
1115
1116
1117


1118


1119



















1120
1121
1122
1123
1124
1125
1126
1127


























































1128
1129
1130
1131
1132
1133
1134
  rc = sqlite3BtreeKey(pCur, 0, nCellKey, pCellKey);
  *pResult = memcmp(pCellKey, pKey, nKey>nCellKey?nCellKey:nKey);
  sqliteFree(pCellKey);

  return rc;
}

/*

























** Write the serialized form of the value held by pMem into zBuf. Return
** the number of bytes written.
*/
int sqlite3VdbeSerialize(
  const Mem *pMem,      /* Pointer to vdbe value to serialize */
  unsigned char *zBuf   /* Buffer to write to */
){


}



















































/*
** Return the number of bytes that would be consumed by the serialized
** form of the value held by pMem. Return negative if an error occurs.
*/
int sqlite3VdbeSerialLen(const Mem *pMem){


}






















/*
** Deserialize a value from zBuf and store it in *pMem. Return the number
** of bytes written, or negative if an error occurs.
*/
int sqlite3VdbeDeserialize(
  Mem *pMem,                   /* structure to write new value to */
  const unsigned char *zBuf    /* Buffer to read from */
){


























































}

/*
** The following is the comparison function for (non-integer)
** keys in the btrees.  This function returns negative, zero, or
** positive if the first key is less than, equal to, or greater than
** the second.








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







>
>
|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>





>
>
|
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
  rc = sqlite3BtreeKey(pCur, 0, nCellKey, pCellKey);
  *pResult = memcmp(pCellKey, pKey, nKey>nCellKey?nCellKey:nKey);
  sqliteFree(pCellKey);

  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.
*/
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; ii<size; ii++){
      zBuf[ii+1] = (pMem->i >> (8*ii)) & 0xFF;
    }
    if( pMem->i<0 ){
      zBuf[size] = zBuf[size] & 0x80;
    }

    return size+1;
  }

  return -1;
}

/*
** Return the number of bytes that would be consumed by the serialized
** 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;
}

/*
** Deserialize a value from zBuf and store it in *pMem. Return the number
** of bytes written, or negative if an error occurs.
*/
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; ii<bytes; ii++){
      pMem->i = 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;
}

/*
** The following is the comparison function for (non-integer)
** keys in the btrees.  This function returns negative, zero, or
** positive if the first key is less than, equal to, or greater than
** the second.