Back to index

lightning-sunbird  0.9+nobinonly
Defines | Functions | Variables
util.c File Reference
#include "sqliteInt.h"
#include "os.h"
#include <stdarg.h>
#include <ctype.h>

Go to the source code of this file.

Defines

#define MAX(x, y)   ((x)>(y)?(x):(y))
#define sqlite3_release_memory(x)   0 /* 0 == no memory freed */
#define OSMALLOC(x)   sqlite3OsMalloc(x)
#define OSREALLOC(x, y)   sqlite3OsRealloc(x,y)
#define OSFREE(x)   sqlite3OsFree(x)
#define OSSIZEOF(x)   sqlite3OsAllocationSize(x)
#define OSMALLOC_FAILED()
#define enforceSoftLimit(X)   1
#define updateMemoryUsedCount(x)   /* no-op */
#define UpperToLower   sqlite3UpperToLower

Functions

voidsqlite3MallocRaw (int n, int doMemManage)
voidsqlite3Realloc (void *p, int n)
void sqlite3FreeX (void *p)
voidsqlite3MallocX (int n)
voidsqlite3Malloc (int n, int doMemManage)
void sqlite3ReallocOrFree (void **pp, int n)
char * sqlite3StrDup (const char *z)
char * sqlite3StrNDup (const char *z, int n)
void sqlite3SetString (char **pz,...)
void sqlite3Error (sqlite3 *db, int err_code, const char *zFormat,...)
void sqlite3ErrorMsg (Parse *pParse, const char *zFormat,...)
void sqlite3ErrorClear (Parse *pParse)
void sqlite3Dequote (char *z)
int sqlite3StrICmp (const char *zLeft, const char *zRight)
int sqlite3StrNICmp (const char *zLeft, const char *zRight, int N)
int sqlite3IsNumber (const char *z, int *realnum, u8 enc)
int sqlite3AtoF (const char *z, double *pResult)
int sqlite3atoi64 (const char *zNum, i64 *pNum)
static int sqlite3FitsIn32Bits (const char *zNum)
int sqlite3GetInt32 (const char *zNum, int *pValue)
int sqlite3FitsIn64Bits (const char *zNum)
int sqlite3SafetyOn (sqlite3 *db)
int sqlite3SafetyOff (sqlite3 *db)
int sqlite3SafetyCheck (sqlite3 *db)
int sqlite3PutVarint (unsigned char *p, u64 v)
int sqlite3GetVarint (const unsigned char *p, u64 *v)
int sqlite3GetVarint32 (const unsigned char *p, u32 *v)
int sqlite3VarintLen (u64 v)
static int hexToInt (int h)
voidsqlite3HexToBlob (const char *z)
ThreadDatasqlite3ThreadData ()
const ThreadDatasqlite3ThreadDataReadOnly ()
void sqlite3ReleaseThreadData ()
int sqlite3ApiExit (sqlite3 *db, int rc)
int sqlite3MallocFailed ()
void sqlite3FailedMalloc ()

Variables

const unsigned char sqlite3UpperToLower []
static int mallocHasFailed = 0

Define Documentation

#define enforceSoftLimit (   X)    1

Definition at line 556 of file util.c.

#define MAX (   x,
  y 
)    ((x)>(y)?(x):(y))

Definition at line 67 of file util.c.

#define OSFREE (   x)    sqlite3OsFree(x)

Definition at line 520 of file util.c.

Definition at line 518 of file util.c.

Definition at line 522 of file util.c.

#define OSREALLOC (   x,
  y 
)    sqlite3OsRealloc(x,y)

Definition at line 519 of file util.c.

Definition at line 521 of file util.c.

#define sqlite3_release_memory (   x)    0 /* 0 == no memory freed */

Definition at line 94 of file util.c.

#define updateMemoryUsedCount (   x)    /* no-op */

Definition at line 579 of file util.c.

Definition at line 923 of file util.c.


Function Documentation

static int hexToInt ( int  h) [static]

Definition at line 1334 of file util.c.

                          {
  if( h>='0' && h<='9' ){
    return h - '0';
  }else if( h>='a' && h<='f' ){
    return h - 'a' + 10;
  }else{
    assert( h>='A' && h<='F' );
    return h - 'A' + 10;
  }
}

Here is the caller graph for this function:

int sqlite3ApiExit ( sqlite3 db,
int  rc 
)

Definition at line 1440 of file util.c.

Here is the call graph for this function:

Here is the caller graph for this function:

int sqlite3AtoF ( const char *  z,
double *  pResult 
)

Definition at line 990 of file util.c.

                                               {
#ifndef SQLITE_OMIT_FLOATING_POINT
  int sign = 1;
  const char *zBegin = z;
  LONGDOUBLE_TYPE v1 = 0.0;
  while( isspace(*z) ) z++;
  if( *z=='-' ){
    sign = -1;
    z++;
  }else if( *z=='+' ){
    z++;
  }
  while( isdigit(*(u8*)z) ){
    v1 = v1*10.0 + (*z - '0');
    z++;
  }
  if( *z=='.' ){
    LONGDOUBLE_TYPE divisor = 1.0;
    z++;
    while( isdigit(*(u8*)z) ){
      v1 = v1*10.0 + (*z - '0');
      divisor *= 10.0;
      z++;
    }
    v1 /= divisor;
  }
  if( *z=='e' || *z=='E' ){
    int esign = 1;
    int eval = 0;
    LONGDOUBLE_TYPE scale = 1.0;
    z++;
    if( *z=='-' ){
      esign = -1;
      z++;
    }else if( *z=='+' ){
      z++;
    }
    while( isdigit(*(u8*)z) ){
      eval = eval*10 + *z - '0';
      z++;
    }
    while( eval>=64 ){ scale *= 1.0e+64; eval -= 64; }
    while( eval>=16 ){ scale *= 1.0e+16; eval -= 16; }
    while( eval>=4 ){ scale *= 1.0e+4; eval -= 4; }
    while( eval>=1 ){ scale *= 1.0e+1; eval -= 1; }
    if( esign<0 ){
      v1 /= scale;
    }else{
      v1 *= scale;
    }
  }
  *pResult = sign<0 ? -v1 : v1;
  return z - zBegin;
#else
  return sqlite3atoi64(z, pResult);
#endif /* SQLITE_OMIT_FLOATING_POINT */
}

Here is the call graph for this function:

Here is the caller graph for this function:

int sqlite3atoi64 ( const char *  zNum,
i64 pNum 
)

Definition at line 1059 of file util.c.

                                              {
  i64 v = 0;
  int neg;
  int i, c;
  while( isspace(*zNum) ) zNum++;
  if( *zNum=='-' ){
    neg = 1;
    zNum++;
  }else if( *zNum=='+' ){
    neg = 0;
    zNum++;
  }else{
    neg = 0;
  }
  for(i=0; (c=zNum[i])>='0' && c<='9'; i++){
    v = v*10 + c - '0';
  }
  *pNum = neg ? -v : v;
  return c==0 && i>0 && 
      (i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0));
}

Here is the call graph for this function:

Here is the caller graph for this function:

void sqlite3Dequote ( char *  z)

Definition at line 856 of file util.c.

                            {
  int quote;
  int i, j;
  if( z==0 ) return;
  quote = z[0];
  switch( quote ){
    case '\'':  break;
    case '"':   break;
    case '`':   break;                /* For MySQL compatibility */
    case '[':   quote = ']';  break;  /* For MS SqlServer compatibility */
    default:    return;
  }
  for(i=1, j=0; z[i]; i++){
    if( z[i]==quote ){
      if( z[i+1]==quote ){
        z[j++] = quote;
        i++;
      }else{
        z[j++] = 0;
        break;
      }
    }else{
      z[j++] = z[i];
    }
  }
}

Here is the caller graph for this function:

void sqlite3Error ( sqlite3 db,
int  err_code,
const char *  zFormat,
  ... 
)

Definition at line 795 of file util.c.

                                                                      {
  if( db && (db->pErr || (db->pErr = sqlite3ValueNew())!=0) ){
    db->errCode = err_code;
    if( zFormat ){
      char *z;
      va_list ap;
      va_start(ap, zFormat);
      z = sqlite3VMPrintf(zFormat, ap);
      va_end(ap);
      sqlite3ValueSetStr(db->pErr, -1, z, SQLITE_UTF8, sqlite3FreeX);
    }else{
      sqlite3ValueSetStr(db->pErr, 0, 0, SQLITE_UTF8, SQLITE_STATIC);
    }
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void sqlite3ErrorClear ( Parse pParse)

Definition at line 840 of file util.c.

                                     {
  sqliteFree(pParse->zErrMsg);
  pParse->zErrMsg = 0;
  pParse->nErr = 0;
}

Here is the caller graph for this function:

void sqlite3ErrorMsg ( Parse pParse,
const char *  zFormat,
  ... 
)

Definition at line 828 of file util.c.

                                                             {
  va_list ap;
  pParse->nErr++;
  sqliteFree(pParse->zErrMsg);
  va_start(ap, zFormat);
  pParse->zErrMsg = sqlite3VMPrintf(zFormat, ap);
  va_end(ap);
}

Here is the call graph for this function:

Definition at line 1461 of file util.c.

Here is the call graph for this function:

Here is the caller graph for this function:

static int sqlite3FitsIn32Bits ( const char *  zNum) [static]

Definition at line 1092 of file util.c.

                                                {
  int i, c;
  if( *zNum=='-' || *zNum=='+' ) zNum++;
  for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
  return i<10 || (i==10 && memcmp(zNum,"2147483647",10)<=0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

int sqlite3FitsIn64Bits ( const char *  zNum)

Definition at line 1122 of file util.c.

                                         {
  int i, c;
  if( *zNum=='-' || *zNum=='+' ) zNum++;
  for(i=0; (c=zNum[i])>='0' && c<='9'; i++){}
  return i<19 || (i==19 && memcmp(zNum,"9223372036854775807",19)<=0);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 635 of file util.c.

                          {
  if( p ){
    updateMemoryUsedCount(0 - OSSIZEOF(p));
    OSFREE(p);
  }
}

Here is the caller graph for this function:

int sqlite3GetInt32 ( const char *  zNum,
int pValue 
)

Definition at line 1103 of file util.c.

                                                  {
  if( sqlite3FitsIn32Bits(zNum) ){
    *pValue = atoi(zNum);
    return 1;
  }
  return 0;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int sqlite3GetVarint ( const unsigned char *  p,
u64 v 
)

Definition at line 1252 of file util.c.

                                                    {
  u32 x;
  u64 x64;
  int n;
  unsigned char c;
  if( ((c = p[0]) & 0x80)==0 ){
    *v = c;
    return 1;
  }
  x = c & 0x7f;
  if( ((c = p[1]) & 0x80)==0 ){
    *v = (x<<7) | c;
    return 2;
  }
  x = (x<<7) | (c&0x7f);
  if( ((c = p[2]) & 0x80)==0 ){
    *v = (x<<7) | c;
    return 3;
  }
  x = (x<<7) | (c&0x7f);
  if( ((c = p[3]) & 0x80)==0 ){
    *v = (x<<7) | c;
    return 4;
  }
  x64 = (x<<7) | (c&0x7f);
  n = 4;
  do{
    c = p[n++];
    if( n==9 ){
      x64 = (x64<<8) | c;
      break;
    }
    x64 = (x64<<7) | (c&0x7f);
  }while( (c & 0x80)!=0 );
  *v = x64;
  return n;
}
int sqlite3GetVarint32 ( const unsigned char *  p,
u32 v 
)

Definition at line 1294 of file util.c.

                                                      {
  u32 x;
  int n;
  unsigned char c;
  if( ((signed char*)p)[0]>=0 ){
    *v = p[0];
    return 1;
  }
  x = p[0] & 0x7f;
  if( ((signed char*)p)[1]>=0 ){
    *v = (x<<7) | p[1];
    return 2;
  }
  x = (x<<7) | (p[1] & 0x7f);
  n = 2;
  do{
    x = (x<<7) | ((c = p[n++])&0x7f);
  }while( (c & 0x80)!=0 && n<9 );
  *v = x;
  return n;
}

Here is the caller graph for this function:

void* sqlite3HexToBlob ( const char *  z)

Definition at line 1353 of file util.c.

                                     {
  char *zBlob;
  int i;
  int n = strlen(z);
  if( n%2 ) return 0;

  zBlob = (char *)sqliteMalloc(n/2);
  for(i=0; i<n; i+=2){
    zBlob[i/2] = (hexToInt(z[i])<<4) | hexToInt(z[i+1]);
  }
  return zBlob;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int sqlite3IsNumber ( const char *  z,
int realnum,
u8  enc 
)

Definition at line 952 of file util.c.

                                                        {
  int incr = (enc==SQLITE_UTF8?1:2);
  if( enc==SQLITE_UTF16BE ) z++;
  if( *z=='-' || *z=='+' ) z += incr;
  if( !isdigit(*(u8*)z) ){
    return 0;
  }
  z += incr;
  if( realnum ) *realnum = 0;
  while( isdigit(*(u8*)z) ){ z += incr; }
  if( *z=='.' ){
    z += incr;
    if( !isdigit(*(u8*)z) ) return 0;
    while( isdigit(*(u8*)z) ){ z += incr; }
    if( realnum ) *realnum = 1;
  }
  if( *z=='e' || *z=='E' ){
    z += incr;
    if( *z=='+' || *z=='-' ) z += incr;
    if( !isdigit(*(u8*)z) ) return 0;
    while( isdigit(*(u8*)z) ){ z += incr; }
    if( realnum ) *realnum = 1;
  }
  return *z==0;
}

Here is the caller graph for this function:

void* sqlite3Malloc ( int  n,
int  doMemManage 
)

Definition at line 657 of file util.c.

                                           {
  void *p = sqlite3MallocRaw(n, doMemManage);
  if( p ){
    memset(p, 0, n);
  }
  return p;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 1454 of file util.c.

                         {
  return (mallocHasFailed && sqlite3OsInMutex(1));
}

Here is the call graph for this function:

void* sqlite3MallocRaw ( int  n,
int  doMemManage 
)

Definition at line 587 of file util.c.

                                              {
  void *p = 0;
  if( n>0 && !sqlite3MallocFailed() && (!doMemManage || enforceSoftLimit(n)) ){
    while( (p = OSMALLOC(n))==0 && sqlite3_release_memory(n) ){}
    if( !p ){
      sqlite3FailedMalloc();
      OSMALLOC_FAILED();
    }else if( doMemManage ){
      updateMemoryUsedCount(OSSIZEOF(p));
    }
  }
  return p;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 646 of file util.c.

                           {
  return sqliteMalloc(n);
}

Here is the caller graph for this function:

int sqlite3PutVarint ( unsigned char *  p,
u64  v 
)

Definition at line 1223 of file util.c.

                                             {
  int i, j, n;
  u8 buf[10];
  if( v & (((u64)0xff000000)<<32) ){
    p[8] = v;
    v >>= 8;
    for(i=7; i>=0; i--){
      p[i] = (v & 0x7f) | 0x80;
      v >>= 7;
    }
    return 9;
  }    
  n = 0;
  do{
    buf[n++] = (v & 0x7f) | 0x80;
    v >>= 7;
  }while( v!=0 );
  buf[0] &= 0x7f;
  assert( n<=9 );
  for(i=0, j=n-1; j>=0; j--, i++){
    p[i] = buf[j];
  }
  return n;
}

Here is the caller graph for this function:

void* sqlite3Realloc ( void p,
int  n 
)

Definition at line 606 of file util.c.

                                    {
  if( sqlite3MallocFailed() ){
    return 0;
  }

  if( !p ){
    return sqlite3Malloc(n, 1);
  }else{
    void *np = 0;
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
    int origSize = OSSIZEOF(p);
#endif
    if( enforceSoftLimit(n - origSize) ){
      while( (np = OSREALLOC(p, n))==0 && sqlite3_release_memory(n) ){}
      if( !np ){
        sqlite3FailedMalloc();
        OSMALLOC_FAILED();
      }else{
        updateMemoryUsedCount(OSSIZEOF(np) - origSize);
      }
    }
    return np;
  }
}

Here is the call graph for this function:

Here is the caller graph for this function:

void sqlite3ReallocOrFree ( void **  pp,
int  n 
)

Definition at line 664 of file util.c.

                                           {
  void *p = sqlite3Realloc(*pp, n);
  if( !p ){
    sqlite3FreeX(*pp);
  }
  *pp = p;
}

Here is the call graph for this function:

Definition at line 1422 of file util.c.

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 1184 of file util.c.

                                   {
  int magic;
  if( db==0 ) return 1;
  magic = db->magic;
  if( magic!=SQLITE_MAGIC_CLOSED &&
         magic!=SQLITE_MAGIC_OPEN &&
         magic!=SQLITE_MAGIC_BUSY ) return 1;
  return 0;
}

Here is the caller graph for this function:

Definition at line 1164 of file util.c.

                                 {
  if( db->magic==SQLITE_MAGIC_BUSY ){
    db->magic = SQLITE_MAGIC_OPEN;
    return 0;
  }else if( db->magic==SQLITE_MAGIC_OPEN ){
    db->magic = SQLITE_MAGIC_ERROR;
    db->flags |= SQLITE_Interrupt;
  }
  return 1;
}

Here is the caller graph for this function:

Definition at line 1148 of file util.c.

                                {
  if( db->magic==SQLITE_MAGIC_OPEN ){
    db->magic = SQLITE_MAGIC_BUSY;
    return 0;
  }else if( db->magic==SQLITE_MAGIC_BUSY ){
    db->magic = SQLITE_MAGIC_ERROR;
    db->flags |= SQLITE_Interrupt;
  }
  return 1;
}

Here is the caller graph for this function:

void sqlite3SetString ( char **  pz,
  ... 
)

Definition at line 747 of file util.c.

                                     {
  va_list ap;
  int nByte;
  const char *z;
  char *zResult;

  if( pz==0 ) return;
  nByte = 1;
  va_start(ap, pz);
  while( (z = va_arg(ap, const char*))!=0 ){
    nByte += strlen(z);
  }
  va_end(ap);
  sqliteFree(*pz);
  *pz = zResult = sqliteMallocRaw( nByte );
  if( zResult==0 ){
    return;
  }
  *zResult = 0;
  va_start(ap, pz);
  while( (z = va_arg(ap, const char*))!=0 ){
    strcpy(zResult, z);
    zResult += strlen(zResult);
  }
  va_end(ap);
}

Here is the caller graph for this function:

char* sqlite3StrDup ( const char *  z)

Definition at line 722 of file util.c.

                                  {
  char *zNew;
  if( z==0 ) return 0;
  zNew = sqlite3MallocRaw(strlen(z)+1, 1);
  if( zNew ) strcpy(zNew, z);
  return zNew;
}

Here is the call graph for this function:

int sqlite3StrICmp ( const char *  zLeft,
const char *  zRight 
)

Definition at line 929 of file util.c.

                                                         {
  register unsigned char *a, *b;
  a = (unsigned char *)zLeft;
  b = (unsigned char *)zRight;
  while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
  return UpperToLower[*a] - UpperToLower[*b];
}

Here is the caller graph for this function:

char* sqlite3StrNDup ( const char *  z,
int  n 
)

Definition at line 729 of file util.c.

                                          {
  char *zNew;
  if( z==0 ) return 0;
  zNew = sqlite3MallocRaw(n+1, 1);
  if( zNew ){
    memcpy(zNew, z, n);
    zNew[n] = 0;
  }
  return zNew;
}

Here is the call graph for this function:

Here is the caller graph for this function:

int sqlite3StrNICmp ( const char *  zLeft,
const char *  zRight,
int  N 
)

Definition at line 936 of file util.c.

                                                                 {
  register unsigned char *a, *b;
  a = (unsigned char *)zLeft;
  b = (unsigned char *)zRight;
  while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; }
  return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b];
}

Here is the caller graph for this function:

Definition at line 1398 of file util.c.

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 1411 of file util.c.

                                             {
  static const ThreadData zeroData = {0};  /* Initializer to silence warnings
                                           ** from broken compilers */
  const ThreadData *pTd = sqlite3OsThreadSpecificData(0);
  return pTd ? pTd : &zeroData;
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 1320 of file util.c.

                           {
  int i = 0;
  do{
    i++;
    v >>= 7;
  }while( v!=0 && i<9 );
  return i;
}

Here is the caller graph for this function:


Variable Documentation

int mallocHasFailed = 0 [static]

Definition at line 1439 of file util.c.

const unsigned char sqlite3UpperToLower[]

Definition at line 886 of file util.c.