Back to index

php5  5.3.10
Functions
encode.c File Reference
#include <string.h>
#include <assert.h>

Go to the source code of this file.

Functions

int sqlite_encode_binary (const unsigned char *in, int n, unsigned char *out)
int sqlite_decode_binary (const unsigned char *in, unsigned char *out)

Function Documentation

int sqlite_decode_binary ( const unsigned char *  in,
unsigned char *  out 
)

Definition at line 175 of file encode.c.

                                                                     {
  int i, e;
  unsigned char c;
  e = *(in++);
  if (e == 0) {
    return 0;
  }
  i = 0;
  while( (c = *(in++))!=0 ){
    if (c == 1) {
      c = *(in++) - 1;
    }
    out[i++] = c + e;
  }
  return i;
}
int sqlite_encode_binary ( const unsigned char *  in,
int  n,
unsigned char *  out 
)

Definition at line 123 of file encode.c.

                                                                            {
  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;
}