/ Check-in [e2f7f182]
Login

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

Overview
Comment:Add a comment to the output buffer allocation in sqlite3VdbeMemTranslate() (CVS 1673)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: e2f7f182987fbfe8611ead8bd1f12b2e8b47f6dc
User & Date: danielk1977 2004-06-23 00:23:49
Context
2004-06-23
01:05
Handle corrupt journal file headers correctly. (CVS 1674) check-in: 46107da7 user: danielk1977 tags: trunk
00:23
Add a comment to the output buffer allocation in sqlite3VdbeMemTranslate() (CVS 1673) check-in: e2f7f182 user: danielk1977 tags: trunk
2004-06-22
22:54
Fix another uninitialized Mem.xDel problem. (CVS 1672) check-in: cb4e242e user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/utf.c.

     8      8   **    May you find forgiveness for yourself and forgive others.
     9      9   **    May you share freely, never taking more than you give.
    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains routines used to translate between UTF-8, 
    13     13   ** UTF-16, UTF-16BE, and UTF-16LE.
    14     14   **
    15         -** $Id: utf.c,v 1.23 2004/06/22 22:04:46 drh Exp $
           15  +** $Id: utf.c,v 1.24 2004/06/23 00:23:49 danielk1977 Exp $
    16     16   **
    17     17   ** Notes on UTF-8:
    18     18   **
    19     19   **   Byte-0    Byte-1    Byte-2    Byte-3    Value
    20     20   **  0xxxxxxx                                 00000000 00000000 0xxxxxxx
    21     21   **  110yyyyy  10xxxxxx                       00000000 00000yyy yyxxxxxx
    22     22   **  1110zzzz  10yyyyyy  10xxxxxx             00000000 zzzzyyyy yyxxxxxx
................................................................................
   257    257         *zIn = *(zIn+1);
   258    258         zIn++;
   259    259         *zIn++ = temp;
   260    260       }
   261    261       pMem->enc = desiredEnc;
   262    262       goto translate_out;
   263    263     }
          264  +
          265  +  /* Set len to the maximum number of bytes required in the output buffer. */
          266  +  if( desiredEnc==SQLITE_UTF8 ){
          267  +    /* When converting from UTF-16, the maximum growth results from
          268  +    ** translating a 2-byte character to a 3-byte UTF-8 character (i.e.
          269  +    ** code-point 0xFFFC). A single byte is required for the output string
          270  +    ** nul-terminator.
          271  +    */
          272  +    len = (pMem->n/2) * 3 + 1;
          273  +  }else{
          274  +    /* When converting from UTF-8 to UTF-16 the maximum growth is caused
          275  +    ** when a 1-byte UTF-8 character is translated into a 2-byte UTF-16
          276  +    ** character. Two bytes are required in the output buffer for the
          277  +    ** nul-terminator.
          278  +    */
          279  +    len = pMem->n * 2 + 2;
          280  +  }
   264    281   
   265    282     /* Set zIn to point at the start of the input buffer and zTerm to point 1
   266    283     ** byte past the end.
   267    284     **
   268    285     ** Variable zOut is set to point at the output buffer. This may be space
   269    286     ** obtained from malloc(), or Mem.zShort, if it large enough and not in
   270    287     ** use, or the zShort array on the stack (see above).
   271    288     */
   272    289     zIn = pMem->z;
   273    290     zTerm = &zIn[pMem->n];
   274         -  len = pMem->n*2 + 2;
   275    291     if( len>NBFS ){
   276    292       zOut = sqliteMallocRaw(len);
   277    293       if( !zOut ) return SQLITE_NOMEM;
   278    294     }else{
   279    295       if( pMem->z==pMem->zShort ){
   280    296         zOut = zShort;
   281    297       }else{
................................................................................
   317    333         while( zIn<zTerm ){
   318    334           READ_UTF16BE(zIn, c); 
   319    335           WRITE_UTF8(z, c);
   320    336         }
   321    337         WRITE_UTF8(z, 0);
   322    338         pMem->n = (z-zOut)-1;
   323    339       }
   324         -    assert( pMem->n+1<=len );
   325    340     }
          341  +  assert( (pMem->n+(desiredEnc==SQLITE_UTF8?1:2))<=len );
   326    342   
   327    343     sqlite3VdbeMemRelease(pMem);
   328    344     pMem->flags &= ~(MEM_Static|MEM_Dyn|MEM_Ephem|MEM_Short);
   329    345     pMem->enc = desiredEnc;
   330    346     if( (char *)zOut==pMem->zShort ){
   331    347       pMem->flags |= (MEM_Term|MEM_Short);
   332    348     }else if( zOut==zShort ){