⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 vdbemem.c

📁 1.编译色情sqlite源代码为dll;2.运用sqlite3数据库存储二进制数据到数据库
💻 C
📖 第 1 页 / 共 3 页
字号:
/*** 2004 May 26**** 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 code use to manipulate "Mem" structure.  A "Mem"** stores a single value in the VDBE.  Mem is an opaque structure visible** only within the VDBE.  Interface routines refer to a Mem using the** name sqlite_value*/#include "sqliteInt.h"#include <ctype.h>#include "vdbeInt.h"/*** Call sqlite3VdbeMemExpandBlob() on the supplied value (type Mem*)** P if required.*/#define expandBlob(P) (((P)->flags&MEM_Zero)?sqlite3VdbeMemExpandBlob(P):0)/*** If pMem is an object with a valid string representation, this routine** ensures the internal encoding for the string representation is** 'desiredEnc', one of SQLITE_UTF8, SQLITE_UTF16LE or SQLITE_UTF16BE.**** If pMem is not a string object, or the encoding of the string** representation is already stored using the requested encoding, then this** routine is a no-op.**** SQLITE_OK is returned if the conversion is successful (or not required).** SQLITE_NOMEM may be returned if a malloc() fails during conversion** between formats.*/int sqlite3VdbeChangeEncoding(Mem *pMem, int desiredEnc){  int rc;  if( !(pMem->flags&MEM_Str) || pMem->enc==desiredEnc ){    return SQLITE_OK;  }  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );#ifdef SQLITE_OMIT_UTF16  return SQLITE_ERROR;#else  /* MemTranslate() may return SQLITE_OK or SQLITE_NOMEM. If NOMEM is returned,  ** then the encoding of the value may not have changed.  */  rc = sqlite3VdbeMemTranslate(pMem, desiredEnc);  assert(rc==SQLITE_OK    || rc==SQLITE_NOMEM);  assert(rc==SQLITE_OK    || pMem->enc!=desiredEnc);  assert(rc==SQLITE_NOMEM || pMem->enc==desiredEnc);  return rc;#endif}/*** Make the given Mem object MEM_Dyn.**** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.*/int sqlite3VdbeMemDynamicify(Mem *pMem){  int n;  u8 *z;  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );  expandBlob(pMem);  if( (pMem->flags & (MEM_Ephem|MEM_Static|MEM_Short))==0 ){    return SQLITE_OK;  }  assert( (pMem->flags & MEM_Dyn)==0 );  n = pMem->n;  assert( pMem->flags & (MEM_Str|MEM_Blob) );  z = sqlite3DbMallocRaw(pMem->db, n+2 );  if( z==0 ){    return SQLITE_NOMEM;  }  pMem->flags |= MEM_Dyn|MEM_Term;  pMem->xDel = 0;  memcpy(z, pMem->z, n );  z[n] = 0;  z[n+1] = 0;  pMem->z = (char*)z;  pMem->flags &= ~(MEM_Ephem|MEM_Static|MEM_Short);  return SQLITE_OK;}/*** If the given Mem* has a zero-filled tail, turn it into an ordinary** blob stored in dynamically allocated space.*/#ifndef SQLITE_OMIT_INCRBLOBint sqlite3VdbeMemExpandBlob(Mem *pMem){  if( pMem->flags & MEM_Zero ){    char *pNew;    int nByte;    assert( (pMem->flags & MEM_Blob)!=0 );    nByte = pMem->n + pMem->u.i;    if( nByte<=0 ) nByte = 1;    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );    pNew = sqlite3DbMallocRaw(pMem->db, nByte);    if( pNew==0 ){      return SQLITE_NOMEM;    }    memcpy(pNew, pMem->z, pMem->n);    memset(&pNew[pMem->n], 0, pMem->u.i);    sqlite3VdbeMemRelease(pMem);    pMem->z = pNew;    pMem->n += pMem->u.i;    pMem->u.i = 0;    pMem->flags &= ~(MEM_Zero|MEM_Static|MEM_Ephem|MEM_Short|MEM_Term);    pMem->flags |= MEM_Dyn;  }  return SQLITE_OK;}#endif/*** Make the given Mem object either MEM_Short or MEM_Dyn so that bytes** of the Mem.z[] array can be modified.**** Return SQLITE_OK on success or SQLITE_NOMEM if malloc fails.*/int sqlite3VdbeMemMakeWriteable(Mem *pMem){  int n;  u8 *z;  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );  expandBlob(pMem);  if( (pMem->flags & (MEM_Ephem|MEM_Static))==0 ){    return SQLITE_OK;  }  assert( (pMem->flags & MEM_Dyn)==0 );  assert( pMem->flags & (MEM_Str|MEM_Blob) );  if( (n = pMem->n)+2<sizeof(pMem->zShort) ){    z = (u8*)pMem->zShort;    pMem->flags |= MEM_Short|MEM_Term;  }else{    z = sqlite3DbMallocRaw(pMem->db, n+2 );    if( z==0 ){      return SQLITE_NOMEM;    }    pMem->flags |= MEM_Dyn|MEM_Term;    pMem->xDel = 0;  }  memcpy(z, pMem->z, n );  z[n] = 0;  z[n+1] = 0;  pMem->z = (char*)z;  pMem->flags &= ~(MEM_Ephem|MEM_Static);  assert(0==(1&(int)pMem->z));  return SQLITE_OK;}/*** Make sure the given Mem is \u0000 terminated.*/int sqlite3VdbeMemNulTerminate(Mem *pMem){  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );  if( (pMem->flags & MEM_Term)!=0 || (pMem->flags & MEM_Str)==0 ){    return SQLITE_OK;   /* Nothing to do */  }  if( pMem->flags & (MEM_Static|MEM_Ephem) ){    return sqlite3VdbeMemMakeWriteable(pMem);  }else{    char *z;     sqlite3VdbeMemExpandBlob(pMem);    z = sqlite3DbMallocRaw(pMem->db, pMem->n+2);    if( !z ){       return SQLITE_NOMEM;    }    memcpy(z, pMem->z, pMem->n);    z[pMem->n] = 0;    z[pMem->n+1] = 0;    if( pMem->xDel ){      pMem->xDel(pMem->z);    }else{      sqlite3_free(pMem->z);    }    pMem->xDel = 0;    pMem->z = z;    pMem->flags |= MEM_Term;  }  return SQLITE_OK;}/*** Add MEM_Str to the set of representations for the given Mem.  Numbers** are converted using sqlite3_snprintf().  Converting a BLOB to a string** is a no-op.**** Existing representations MEM_Int and MEM_Real are *not* invalidated.**** A MEM_Null value will never be passed to this function. This function is** used for converting values to text for returning to the user (i.e. via** sqlite3_value_text()), or for ensuring that values to be used as btree** keys are strings. In the former case a NULL pointer is returned the** user and the later is an internal programming error.*/int sqlite3VdbeMemStringify(Mem *pMem, int enc){  int rc = SQLITE_OK;  int fg = pMem->flags;  char *z = pMem->zShort;  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );  assert( !(fg&MEM_Zero) );  assert( !(fg&(MEM_Str|MEM_Blob)) );  assert( fg&(MEM_Int|MEM_Real) );  /* For a Real or Integer, use sqlite3_snprintf() to produce the UTF-8  ** string representation of the value. Then, if the required encoding  ** is UTF-16le or UTF-16be do a translation.  **   ** FIX ME: It would be better if sqlite3_snprintf() could do UTF-16.  */  if( fg & MEM_Int ){    sqlite3_snprintf(NBFS, z, "%lld", pMem->u.i);  }else{    assert( fg & MEM_Real );    sqlite3_snprintf(NBFS, z, "%!.15g", pMem->r);  }  pMem->n = strlen(z);  pMem->z = z;  pMem->enc = SQLITE_UTF8;  pMem->flags |= MEM_Str | MEM_Short | MEM_Term;  sqlite3VdbeChangeEncoding(pMem, enc);  return rc;}/*** Memory cell pMem contains the context of an aggregate function.** This routine calls the finalize method for that function.  The** result of the aggregate is stored back into pMem.**** Return SQLITE_ERROR if the finalizer reports an error.  SQLITE_OK** otherwise.*/int sqlite3VdbeMemFinalize(Mem *pMem, FuncDef *pFunc){  int rc = SQLITE_OK;  if( pFunc && pFunc->xFinalize ){    sqlite3_context ctx;    assert( (pMem->flags & MEM_Null)!=0 || pFunc==pMem->u.pDef );    assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );    ctx.s.flags = MEM_Null;    ctx.s.z = pMem->zShort;    ctx.s.db = pMem->db;    ctx.pMem = pMem;    ctx.pFunc = pFunc;    ctx.isError = 0;    pFunc->xFinalize(&ctx);    if( pMem->z && pMem->z!=pMem->zShort ){      sqlite3_free( pMem->z );    }    *pMem = ctx.s;    if( pMem->flags & MEM_Short ){      pMem->z = pMem->zShort;    }    rc = (ctx.isError?SQLITE_ERROR:SQLITE_OK);  }  return rc;}/*** Release any memory held by the Mem. This may leave the Mem in an** inconsistent state, for example with (Mem.z==0) and** (Mem.type==SQLITE_TEXT).*/void sqlite3VdbeMemRelease(Mem *p){  assert( p->db==0 || sqlite3_mutex_held(p->db->mutex) );  if( p->flags & (MEM_Dyn|MEM_Agg) ){    if( p->xDel ){      if( p->flags & MEM_Agg ){        sqlite3VdbeMemFinalize(p, p->u.pDef);        assert( (p->flags & MEM_Agg)==0 );        sqlite3VdbeMemRelease(p);      }else{        p->xDel((void *)p->z);      }    }else{      sqlite3_free(p->z);    }    p->z = 0;    p->xDel = 0;  }}/*** Convert a 64-bit IEEE double into a 64-bit signed integer.** If the double is too large, return 0x8000000000000000.**** Most systems appear to do this simply by assigning** variables and without the extra range tests.  But** there are reports that windows throws an expection** if the floating point value is out of range. (See ticket #2880.)** Because we do not completely understand the problem, we will** take the conservative approach and always do range tests** before attempting the conversion.*/static i64 doubleToInt64(double r){  /*  ** Many compilers we encounter do not define constants for the  ** minimum and maximum 64-bit integers, or they define them  ** inconsistently.  And many do not understand the "LL" notation.  ** So we define our own static constants here using nothing  ** larger than a 32-bit integer constant.  */  static const i64 maxInt = (((i64)0x7fffffff)<<32)|0xffffffff;  static const i64 minInt = ((i64)0x80000000)<<32;  if( r<(double)minInt ){    return minInt;  }else if( r>(double)maxInt ){    return minInt;  }else{    return (i64)r;  }}/*** Return some kind of integer value which is the best we can do** at representing the value that *pMem describes as an integer.** If pMem is an integer, then the value is exact.  If pMem is** a floating-point then the value returned is the integer part.** If pMem is a string or blob, then we make an attempt to convert** it into a integer and return that.  If pMem is NULL, return 0.**** If pMem is a string, its encoding might be changed.*/i64 sqlite3VdbeIntValue(Mem *pMem){  int flags;  assert( pMem->db==0 || sqlite3_mutex_held(pMem->db->mutex) );  flags = pMem->flags;  if( flags & MEM_Int ){    return pMem->u.i;  }else if( flags & MEM_Real ){    return doubleToInt64(pMem->r);  }else if( flags & (MEM_Str|MEM_Blob) ){    i64 value;    pMem->flags |= MEM_Str;    if( sqlite3VdbeChangeEncoding(pMem, SQLITE_UTF8)       || sqlite3VdbeMemNulTerminate(pMem) ){      return 0;    }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -