/* ** 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 */