Remove the encode/decode from the version 3.0 source tree. (CVS 1959)

FossilOrigin-Name: c1f1320be5ce0b6e52491577078ba2b939882fbd
This commit is contained in:
drh 2004-09-13 13:46:01 +00:00
parent 90b6bb1995
commit fd241b0ea4
6 changed files with 53 additions and 277 deletions

View File

@ -92,7 +92,6 @@ SRC = \
$(TOP)/src/build.c \
$(TOP)/src/date.c \
$(TOP)/src/delete.c \
$(TOP)/src/encode.c \
$(TOP)/src/expr.c \
$(TOP)/src/func.c \
$(TOP)/src/hash.c \
@ -258,9 +257,6 @@ date.lo: $(TOP)/src/date.c $(HDR)
delete.lo: $(TOP)/src/delete.c $(HDR)
$(LTCOMPILE) -c $(TOP)/src/delete.c
encode.lo: $(TOP)/src/encode.c
$(LTCOMPILE) -c $(TOP)/src/encode.c
expr.lo: $(TOP)/src/expr.c $(HDR)
$(LTCOMPILE) -c $(TOP)/src/expr.c

View File

@ -73,7 +73,6 @@ SRC = \
$(TOP)/src/build.c \
$(TOP)/src/date.c \
$(TOP)/src/delete.c \
$(TOP)/src/encode.c \
$(TOP)/src/expr.c \
$(TOP)/src/func.c \
$(TOP)/src/hash.c \
@ -232,9 +231,6 @@ date.o: $(TOP)/src/date.c $(HDR)
delete.o: $(TOP)/src/delete.c $(HDR)
$(TCCX) -c $(TOP)/src/delete.c
encode.o: $(TOP)/src/encode.c
$(TCCX) -c $(TOP)/src/encode.c
expr.o: $(TOP)/src/expr.c $(HDR)
$(TCCX) -c $(TOP)/src/expr.c

View File

@ -1,6 +1,6 @@
C The\sTCL\sinterface\sresponds\scorrectly\sto\s"break",\s"continue",\sand\s"return"\ninside\sof\sthe\sscript\sof\san\seval\sstatement.\s(CVS\s1958)
D 2004-09-13T13:16:32
F Makefile.in 7f481bb8cdb032491df611526e6f4eb6af79691d
C Remove\sthe\sencode/decode\sfrom\sthe\sversion\s3.0\ssource\stree.\s(CVS\s1959)
D 2004-09-13T13:46:01
F Makefile.in 9cdfc3af2647055085969968ca2394f24c3c6166
F Makefile.linux-gcc a9e5a0d309fa7c38e7c14d3ecf7690879d3a5457
F README f1de682fbbd94899d50aca13d387d1b3fd3be2dd
F VERSION d28cfc2e7714a747851e50cc1a5ee8ab0ded7a78
@ -17,7 +17,7 @@ F doc/lemon.html f0f682f50210928c07e562621c3b7e8ab912a538
F doc/report1.txt a031aaf37b185e4fa540223cb516d3bccec7eeac
F install-sh 9d4de14ab9fb0facae2f48780b874848cbf2f895
F ltmain.sh f6b283068efa69f06eb8aa1fe4bddfdbdeb35826
F main.mk 8a090784820241a245b96a0079712775a7f4405a
F main.mk fbe4b06ed4d32c529f1514595defe73a424a357e
F mkdll.sh 468d4f41d3ea98221371df4825cfbffbaac4d7e4
F mkso.sh 7b67da1d63070875ba948e749aee9ef50ce36e3d
F publish.sh 72bde067dda3fc2d33e92f20253b924e3b97da30
@ -33,7 +33,6 @@ F src/btree.h 94dfec0a1722d33359b23e7e310f2b64ffedf029
F src/build.c 662f4e56a64aacadc97c481492b08c842b9ec0f4
F src/date.c eb8d5fa1a6d5cfc09031c8852d10ff742a94b15b
F src/delete.c e887f44aae1e33da1643df58abe86cd9cde45ad1
F src/encode.c a876af473d1d636faa3dca51c7571f2e007eea37
F src/expr.c 9130794d8c86af2cbf2b8cdc66f2158167fd15b1
F src/func.c 1fbc5256639586573fd0e70814d6dcd8bc10afc1
F src/hash.c a97721a55440b7bea31ffe471bb2f6b4123cddd5
@ -170,7 +169,7 @@ F test/sort.test 35e9d6bd6930969050606c8feb9c6745469720e3
F test/subselect.test 50f98723f00e97b1839d36410ee63597ca82d775
F test/table.test fd9a0f4992230e4ca89cd37ae3191a12750df1d0
F test/tableapi.test b21ab097e87a5484bb61029e69e1a4e5c5e65ede
F test/tclsqlite.test de670beccf910163bf49973a3a96eaa00cdf127e
F test/tclsqlite.test 1288e6278e094c58ce650d7cbf3c4f39317d9a34
F test/temptable.test 63a16e3ad19adf073cfbcdf7624c92ac5236522c
F test/tester.tcl 1ff1170dd4203d87f572871080cdb64330dade99
F test/thread1.test 776c9e459b75ba905193b351926ac4019b049f35
@ -248,7 +247,7 @@ F www/tclsqlite.tcl 560ecd6a916b320e59f2917317398f3d59b7cc25
F www/vdbe.tcl 59288db1ac5c0616296b26dce071c36cb611dfe9
F www/version3.tcl 092a01f5ef430d2c4acc0ae558d74c4bb89638a0
F www/whentouse.tcl a8335bce47cc2fddb07f19052cb0cb4d9129a8e4
P ee335b501cf45727929aac12c0b74547dd54729e
R ecb736557a3f73e3cac4e72650b0e555
P dd62224ae8d1047db388acdc4b91eb56fb9e966a
R b72a839b56c08880706fc5af1e49a3b1
U drh
Z 9a080417236581205e2b636a7a8ab66c
Z 55b7cec5724fd9cc6c0ba5706bebd5ec

View File

@ -1 +1 @@
dd62224ae8d1047db388acdc4b91eb56fb9e966a
c1f1320be5ce0b6e52491577078ba2b939882fbd

View File

@ -1,257 +0,0 @@
/*
** 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 not used by any other
** part of the SQLite library.
**
** $Id: encode.c,v 1.13 2004/05/08 08:23:24 danielk1977 Exp $
*/
#include <string.h>
#include <assert.h>
/*
** How This Encoder Works
**
** The output is allowed to contain any character except 0x27 (') and
** 0x00. This is accomplished by using an escape character to encode
** 0x27 and 0x00 as a two-byte sequence. The escape character is always
** 0x01. An 0x00 is encoded as the two byte sequence 0x01 0x01. The
** 0x27 character is encoded as the two byte sequence 0x01 0x28. Finally,
** the escape character itself is encoded as the two-character sequence
** 0x01 0x02.
**
** To summarize, the encoder works by using an escape sequences as follows:
**
** 0x00 -> 0x01 0x01
** 0x01 -> 0x01 0x02
** 0x27 -> 0x01 0x28
**
** If that were all the encoder did, it would work, but in certain cases
** it could double the size of the encoded string. For example, to
** encode a string of 100 0x27 characters would require 100 instances of
** the 0x01 0x03 escape sequence resulting in a 200-character output.
** We would prefer to keep the size of the encoded string smaller than
** this.
**
** To minimize the encoding size, we first add a fixed offset value to each
** byte in the sequence. The addition is modulo 256. (That is to say, if
** the sum of the original character value and the offset exceeds 256, then
** the higher order bits are truncated.) The offset is chosen to minimize
** the number of characters in the string that need to be escaped. For
** example, in the case above where the string was composed of 100 0x27
** characters, the offset might be 0x01. Each of the 0x27 characters would
** then be converted into an 0x28 character which would not need to be
** escaped at all and so the 100 character input string would be converted
** into just 100 characters of output. Actually 101 characters of output -
** we have to record the offset used as the first byte in the sequence so
** that the string can be decoded. Since the offset value is stored as
** part of the output string and the output string is not allowed to contain
** characters 0x00 or 0x27, the offset cannot be 0x00 or 0x27.
**
** Here, then, are the encoding steps:
**
** (1) Choose an offset value and make it the first character of
** output.
**
** (2) Copy each input character into the output buffer, one by
** one, adding the offset value as you copy.
**
** (3) If the value of an input character plus offset is 0x00, replace
** that one character by the two-character sequence 0x01 0x01.
** If the sum is 0x01, replace it with 0x01 0x02. If the sum
** is 0x27, replace it with 0x01 0x03.
**
** (4) Put a 0x00 terminator at the end of the output.
**
** Decoding is obvious:
**
** (5) Copy encoded characters except the first into the decode
** buffer. Set the first encoded character aside for use as
** the offset in step 7 below.
**
** (6) Convert each 0x01 0x01 sequence into a single character 0x00.
** Convert 0x01 0x02 into 0x01. Convert 0x01 0x28 into 0x27.
**
** (7) Subtract the offset value that was the first character of
** the encoded buffer from all characters in the output buffer.
**
** The only tricky part is step (1) - how to compute an offset value to
** minimize the size of the output buffer. This is accomplished by testing
** all offset values and picking the one that results in the fewest number
** of escapes. To do that, we first scan the entire input and count the
** number of occurances of each character value in the input. Suppose
** the number of 0x00 characters is N(0), the number of occurances of 0x01
** is N(1), and so forth up to the number of occurances of 0xff is N(255).
** An offset of 0 is not allowed so we don't have to test it. The number
** of escapes required for an offset of 1 is N(1)+N(2)+N(40). The number
** of escapes required for an offset of 2 is N(2)+N(3)+N(41). And so forth.
** In this way we find the offset that gives the minimum number of escapes,
** and thus minimizes the length of the output string.
*/
/*
** 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. Use sqlite_decode_binary() to convert the
** string back into its original binary.
**
** The result is written into a preallocated output buffer "out".
** "out" must be able to hold at least 2 +(257*n)/254 bytes.
** In other words, the output will be expanded by as much as 3
** bytes for every 254 bytes of input plus 2 bytes of fixed overhead.
** (This is approximately 2 + 1.0118*n or about a 1.2% size increase.)
**
** The return value is the number of characters in the encoded
** string, excluding the "\000" terminator.
**
** If out==NULL then no output is generated but the routine still returns
** the number of characters that would have been generated if out had
** not been NULL.
*/
int sqlite_encode_binary(const unsigned char *in, int n, unsigned char *out){
int i, j, e, m;
unsigned char x;
int cnt[256];
if( n<=0 ){
if( out ){
out[0] = 'x';
out[1] = 0;
}
return 1;
}
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<m ){
m = sum;
e = i;
if( m==0 ) break;
}
}
if( out==0 ){
return n+m+1;
}
out[0] = e;
j = 1;
for(i=0; i<n; i++){
x = in[i] - e;
if( x==0 || x==1 || x=='\''){
out[j++] = 1;
x++;
}
out[j++] = x;
}
out[j] = 0;
assert( j==n+m+1 );
return j;
}
/*
** Decode the string "in" into binary data and write it into "out".
** This routine reverses the encoding created by sqlite_encode_binary().
** The output will always be a few bytes less than the input. The number
** of bytes of output is returned. If the input is not a well-formed
** encoding, -1 is returned.
**
** The "in" and "out" parameters may point to the same buffer in order
** to decode a string in place.
*/
int sqlite_decode_binary(const unsigned char *in, unsigned char *out){
int i, e;
unsigned char c;
e = *(in++);
i = 0;
while( (c = *(in++))!=0 ){
if( c==1 ){
c = *(in++) - 1;
}
out[i++] = c + e;
}
return i;
}
#ifdef ENCODER_TEST
#include <stdio.h>
/*
** The subroutines above are not tested by the usual test suite. To test
** these routines, compile just this one file with a -DENCODER_TEST=1 option
** and run the result.
*/
int main(int argc, char **argv){
int i, j, n, m, nOut, nByteIn, nByteOut;
unsigned char in[30000];
unsigned char out[33000];
nByteIn = nByteOut = 0;
for(i=0; i<sizeof(in); i++){
printf("Test %d: ", i+1);
n = rand() % (i+1);
if( i%100==0 ){
int k;
for(j=k=0; j<n; j++){
/* if( k==0 || k=='\'' ) k++; */
in[j] = k;
k = (k+1)&0xff;
}
}else{
for(j=0; j<n; j++) in[j] = rand() & 0xff;
}
nByteIn += n;
nOut = sqlite_encode_binary(in, n, out);
nByteOut += nOut;
if( nOut!=strlen(out) ){
printf(" ERROR return value is %d instead of %d\n", nOut, strlen(out));
exit(1);
}
if( nOut!=sqlite_encode_binary(in, n, 0) ){
printf(" ERROR actual output size disagrees with predicted size\n");
exit(1);
}
m = (256*n + 1262)/253;
printf("size %d->%d (max %d)", n, strlen(out)+1, m);
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");
}
fprintf(stderr,"Finished. Total encoding: %d->%d bytes\n",
nByteIn, nByteOut);
fprintf(stderr,"Avg size increase: %.3f%%\n",
(nByteOut-nByteIn)*100.0/(double)nByteIn);
}
#endif /* ENCODER_TEST */

View File

@ -15,7 +15,7 @@
# interface is pretty well tested. This file contains some addition
# tests for fringe issues that the main test suite does not cover.
#
# $Id: tclsqlite.test,v 1.31 2004/09/07 13:20:35 drh Exp $
# $Id: tclsqlite.test,v 1.32 2004/09/13 13:46:01 drh Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
@ -233,9 +233,51 @@ do_test tcl-5.3 {
set adata
} {686900686f00}
do_test tcl-5.4 {
execsql {
execsql {
SELECT typeof(a), typeof(b), typeof(c) FROM t3
}
} {blob text null}
# Operation of "break" and "continue" within row scripts
#
do_test tcl-6.1 {
db eval {SELECT * FROM t1} {
break
}
lappend a $b
} {10 20}
do_test tcl-6.2 {
set cnt 0
db eval {SELECT * FROM t1} {
if {$a>40} continue
incr cnt
}
set cnt
} {4}
do_test tcl-6.3 {
set cnt 0
db eval {SELECT * FROM t1} {
if {$a<40} continue
incr cnt
}
set cnt
} {5}
do_test tcl-6.4 {
proc return_test {x} {
db eval {SELECT * FROM t1} {
if {$a==$x} {return $b}
}
}
return_test 10
} 20
do_test tcl-6.5 {
return_test 20
} 40
do_test tcl-6.6 {
return_test 99
} 510
do_test tcl-6.7 {
return_test 0
} {}
finish_test