SQLite

Check-in [18c28519d9]
Login

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

Overview
Comment:Update the test logic and the comments on the encoder.c module. (CVS 546)
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 18c28519d973944756694b2c213bfef3153f4b1b
User & Date: drh 2002-04-25 23:06:47.000
Context
2002-04-26
09:47
Fix for ticket #28: Export the "sqlite_changes" function in Windows DLLs. (CVS 547) (check-in: f7cea4634d user: drh tags: trunk)
2002-04-25
23:06
Update the test logic and the comments on the encoder.c module. (CVS 546) (check-in: 18c28519d9 user: drh tags: trunk)
11:45
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) (check-in: 57f7c59713 user: drh tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/encode.c.
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

28
29
30
31
32

33
34
35
36
37
38
39
*************************************************************************
** 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;







|

<
<





|
>


|

|
>







11
12
13
14
15
16
17
18
19


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
*************************************************************************
** 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.2 2002/04/25 23:06:47 drh Exp $
*/



/*
** 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 (256*n + 1262)/253 bytes.
** In other words, the output will be expanded by as much as 3
** bytes for every 253 bytes of input plus 2 bytes of fixed overhead.
** (This is approximately 2 + 1.019*n or about a 2% size increase.)
*/
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;
103
104
105
106
107
108
109
110
111
112
113
114
115








116

117

118
119
120
121
122
123
124
125
126
/*
** 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;
  unsigned char in[20000];
  unsigned char out[30000];

  for(i=0; i<10000; i++){
    printf("Test %d: ", i+1);
    n = rand() % sizeof(in);








    for(j=0; j<n; j++) in[j] = rand() & 0xff;

    sqlite_encode_binary(in, n, out);

    printf("size %d->%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");







|
|

|

|
>
>
>
>
>
>
>
>
|
>

>
|
<







103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128

129
130
131
132
133
134
135
/*
** 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;
  unsigned char in[30000];
  unsigned char out[33000];

  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;
    }
    sqlite_encode_binary(in, n, out);
    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");