From e8a49c12a604d63f57ff384d3d76fb600bec323c Mon Sep 17 00:00:00 2001 From: drh Date: Thu, 25 Apr 2002 11:45:41 +0000 Subject: [PATCH] Added the "encode.c" source file that contains two utility subroutines that can be used to encode binary data for use in INSERT and UPDATE statements. This is just an initial checking. The code has not yet been integrated into the library. (CVS 545) FossilOrigin-Name: 57f7c59713299b03b10ba9c1a5883e2c08a8b138 --- manifest | 11 ++-- manifest.uuid | 2 +- src/encode.c | 142 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 149 insertions(+), 6 deletions(-) create mode 100644 src/encode.c diff --git a/manifest b/manifest index a47c417579..8b6cff4e62 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\sfor\sticket\s#26:\sDocument\sthe\sfact\sthat\sCREATE\sTABLE\smight\snot\sbe\nimmediately\svisible\sto\sother\sprocesses\sthat\sare\sholding\sthe\sdatabase\sopen.\s(CVS\s544) -D 2002-04-25T00:21:50 +C Added\sthe\s"encode.c"\ssource\sfile\sthat\scontains\stwo\sutility\ssubroutines\sthat\ncan\sbe\sused\sto\sencode\sbinary\sdata\sfor\suse\sin\sINSERT\sand\sUPDATE\sstatements.\nThis\sis\sjust\san\sinitial\schecking.\s\sThe\scode\shas\snot\syet\sbeen\sintegrated\sinto\nthe\slibrary.\s(CVS\s545) +D 2002-04-25T11:45:42 F Makefile.in 50f1b3351df109b5774771350d8c1b8d3640130d F Makefile.template 89e373b2dad0321df00400fa968dc14b61a03296 F README a4c0ba11354ef6ba0776b400d057c59da47a4cc0 @@ -24,6 +24,7 @@ F src/btree.c 7dd7ddc66459982dd0cb9800958c1f8d65a32d9f F src/btree.h 8abeabfe6e0b1a990b64fa457592a6482f6674f3 F src/build.c d01b81f41481e733e27ab2fa8e1bfcc64f24257d F src/delete.c 6a6b8192cdff5e4b083da3bc63de099f3790d01f +F src/encode.c cf929a63c3db5d893c24c89377871cb89ede44e2 F src/expr.c cf8d2ea17e419fc83b23e080195b2952e0be4164 F src/func.c a31dcba85bc2ecb9b752980289cf7e6cd0cafbce F src/hash.c cc259475e358baaf299b00a2c7370f2b03dda892 @@ -131,7 +132,7 @@ F www/speed.tcl da8afcc1d3ccc5696cfb388a68982bc3d9f7f00f F www/sqlite.tcl 8b5884354cb615049aed83039f8dfe1552a44279 F www/tclsqlite.tcl 1db15abeb446aad0caf0b95b8b9579720e4ea331 F www/vdbe.tcl 2013852c27a02a091d39a766bc87cff329f21218 -P a06d9acdd5af0dc69b3a4d024de082631254aead -R cfeb54c57cf6f9ec54bd35534c55c306 +P 18b31b7ab90ab330e271e0ed5d316f63846845be +R 6048f8bfc097ad26d4d3ee4d7998e4f3 U drh -Z d14450a4a0ba6fe1648be69426d0ab18 +Z fcb6af942153d3111f8e4a67cbb6971e diff --git a/manifest.uuid b/manifest.uuid index ffa096e990..aa415508f9 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -18b31b7ab90ab330e271e0ed5d316f63846845be \ No newline at end of file +57f7c59713299b03b10ba9c1a5883e2c08a8b138 \ No newline at end of file diff --git a/src/encode.c b/src/encode.c new file mode 100644 index 0000000000..d37d8b7401 --- /dev/null +++ b/src/encode.c @@ -0,0 +1,142 @@ +/* +** 2002 April 25 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** 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. +** +************************************************************************* +** This file contains helper routines used to translate binary data into +** a null-terminated string (suitable for use in SQLite) and back again. +** These are convenience routines for use by people who want to store binary +** data in an SQLite database. The code in this file is used by any other +** part of the SQLite library. +** +** $Id: encode.c,v 1.1 2002/04/25 11:45:42 drh Exp $ +*/ +#include "sqliteInt.h" +#include "encode.h" + +/* +** Encode a binary buffer "in" of size n bytes so that it contains +** no instances of characters '\'' or '\000'. The output is +** null-terminated and can be used as a string value in an INSERT +** or UPDATE statement. +** +** The result is written into a preallocated output buffer "out". +** "out" must be able to hold at least 2 + (n+255)*3/256 + n bytes. +** In other words, the output will be expanded by as much as 3 +** bytes for every 256 bytes of input plus 2 bytes of fixed overhead. +*/ +void sqlite_encode_binary(const unsigned char *in, int n, unsigned char *out){ + int i, j, e, m; + int cnt[256]; + memset(cnt, 0, sizeof(cnt)); + for(i=n-1; i>=0; i--){ cnt[in[i]]++; } + m = n; + for(i=1; i<256; i++){ + int sum; + if( i=='\'' ) continue; + sum = cnt[i] + cnt[(i+1)&0xff] + cnt[(i+'\'')&0xff]; + if( sum%d ", n, strlen(out)+1); + m = 2 + (n+255)*3/256 + n; + if( strlen(out)+1>m ){ + printf(" ERROR output too big\n"); + exit(1); + } + for(j=0; out[j]; j++){ + if( out[j]=='\'' ){ + printf(" ERROR contains (')\n"); + exit(1); + } + } + j = sqlite_decode_binary(out, out); + if( j!=n ){ + printf(" ERROR decode size %d\n", j); + exit(1); + } + if( memcmp(in, out, n)!=0 ){ + printf(" ERROR decode mismatch\n"); + exit(1); + } + printf(" OK\n"); + } +} +#endif /* ENCODER_TEST */