OLD | NEW |
1 /* | 1 /* |
2 ** 2001 September 15 | 2 ** 2001 September 15 |
3 ** | 3 ** |
4 ** The author disclaims copyright to this source code. In place of | 4 ** The author disclaims copyright to this source code. In place of |
5 ** a legal notice, here is a blessing: | 5 ** a legal notice, here is a blessing: |
6 ** | 6 ** |
7 ** May you do good and not evil. | 7 ** May you do good and not evil. |
8 ** May you find forgiveness for yourself and forgive others. | 8 ** May you find forgiveness for yourself and forgive others. |
9 ** May you share freely, never taking more than you give. | 9 ** May you share freely, never taking more than you give. |
10 ** | 10 ** |
11 ************************************************************************* | 11 ************************************************************************* |
12 ** Utility functions used throughout sqlite. | 12 ** Utility functions used throughout sqlite. |
13 ** | 13 ** |
14 ** This file contains functions for allocating memory, comparing | 14 ** This file contains functions for allocating memory, comparing |
15 ** strings, and stuff like that. | 15 ** strings, and stuff like that. |
16 ** | 16 ** |
17 */ | 17 */ |
18 #include "sqliteInt.h" | 18 #include "sqliteInt.h" |
19 #include <stdarg.h> | 19 #include <stdarg.h> |
20 #ifdef SQLITE_HAVE_ISNAN | 20 #if HAVE_ISNAN || SQLITE_HAVE_ISNAN |
21 # include <math.h> | 21 # include <math.h> |
22 #endif | 22 #endif |
23 | 23 |
24 /* | 24 /* |
25 ** Routine needed to support the testcase() macro. | 25 ** Routine needed to support the testcase() macro. |
26 */ | 26 */ |
27 #ifdef SQLITE_COVERAGE_TEST | 27 #ifdef SQLITE_COVERAGE_TEST |
28 void sqlite3Coverage(int x){ | 28 void sqlite3Coverage(int x){ |
29 static unsigned dummy = 0; | 29 static unsigned dummy = 0; |
30 dummy += (unsigned)x; | 30 dummy += (unsigned)x; |
(...skipping 20 matching lines...) Expand all Loading... |
51 | 51 |
52 #ifndef SQLITE_OMIT_FLOATING_POINT | 52 #ifndef SQLITE_OMIT_FLOATING_POINT |
53 /* | 53 /* |
54 ** Return true if the floating point value is Not a Number (NaN). | 54 ** Return true if the floating point value is Not a Number (NaN). |
55 ** | 55 ** |
56 ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN. | 56 ** Use the math library isnan() function if compiled with SQLITE_HAVE_ISNAN. |
57 ** Otherwise, we have our own implementation that works on most systems. | 57 ** Otherwise, we have our own implementation that works on most systems. |
58 */ | 58 */ |
59 int sqlite3IsNaN(double x){ | 59 int sqlite3IsNaN(double x){ |
60 int rc; /* The value return */ | 60 int rc; /* The value return */ |
61 #if !defined(SQLITE_HAVE_ISNAN) | 61 #if !SQLITE_HAVE_ISNAN && !HAVE_ISNAN |
62 /* | 62 /* |
63 ** Systems that support the isnan() library function should probably | 63 ** Systems that support the isnan() library function should probably |
64 ** make use of it by compiling with -DSQLITE_HAVE_ISNAN. But we have | 64 ** make use of it by compiling with -DSQLITE_HAVE_ISNAN. But we have |
65 ** found that many systems do not have a working isnan() function so | 65 ** found that many systems do not have a working isnan() function so |
66 ** this implementation is provided as an alternative. | 66 ** this implementation is provided as an alternative. |
67 ** | 67 ** |
68 ** This NaN test sometimes fails if compiled on GCC with -ffast-math. | 68 ** This NaN test sometimes fails if compiled on GCC with -ffast-math. |
69 ** On the other hand, the use of -ffast-math comes with the following | 69 ** On the other hand, the use of -ffast-math comes with the following |
70 ** warning: | 70 ** warning: |
71 ** | 71 ** |
72 ** This option [-ffast-math] should never be turned on by any | 72 ** This option [-ffast-math] should never be turned on by any |
73 ** -O option since it can result in incorrect output for programs | 73 ** -O option since it can result in incorrect output for programs |
74 ** which depend on an exact implementation of IEEE or ISO | 74 ** which depend on an exact implementation of IEEE or ISO |
75 ** rules/specifications for math functions. | 75 ** rules/specifications for math functions. |
76 ** | 76 ** |
77 ** Under MSVC, this NaN test may fail if compiled with a floating- | 77 ** Under MSVC, this NaN test may fail if compiled with a floating- |
78 ** point precision mode other than /fp:precise. From the MSDN | 78 ** point precision mode other than /fp:precise. From the MSDN |
79 ** documentation: | 79 ** documentation: |
80 ** | 80 ** |
81 ** The compiler [with /fp:precise] will properly handle comparisons | 81 ** The compiler [with /fp:precise] will properly handle comparisons |
82 ** involving NaN. For example, x != x evaluates to true if x is NaN | 82 ** involving NaN. For example, x != x evaluates to true if x is NaN |
83 ** ... | 83 ** ... |
84 */ | 84 */ |
85 #ifdef __FAST_MATH__ | 85 #ifdef __FAST_MATH__ |
86 # error SQLite will not work correctly with the -ffast-math option of GCC. | 86 # error SQLite will not work correctly with the -ffast-math option of GCC. |
87 #endif | 87 #endif |
88 volatile double y = x; | 88 volatile double y = x; |
89 volatile double z = y; | 89 volatile double z = y; |
90 rc = (y!=z); | 90 rc = (y!=z); |
91 #else /* if defined(SQLITE_HAVE_ISNAN) */ | 91 #else /* if HAVE_ISNAN */ |
92 rc = isnan(x); | 92 rc = isnan(x); |
93 #endif /* SQLITE_HAVE_ISNAN */ | 93 #endif /* HAVE_ISNAN */ |
94 testcase( rc ); | 94 testcase( rc ); |
95 return rc; | 95 return rc; |
96 } | 96 } |
97 #endif /* SQLITE_OMIT_FLOATING_POINT */ | 97 #endif /* SQLITE_OMIT_FLOATING_POINT */ |
98 | 98 |
99 /* | 99 /* |
100 ** Compute a string length that is limited to what can be stored in | 100 ** Compute a string length that is limited to what can be stored in |
101 ** lower 30 bits of a 32-bit signed integer. | 101 ** lower 30 bits of a 32-bit signed integer. |
102 ** | 102 ** |
103 ** The value returned will never be negative. Nor will it ever be greater | 103 ** The value returned will never be negative. Nor will it ever be greater |
104 ** than the actual length of the string. For very long strings (greater | 104 ** than the actual length of the string. For very long strings (greater |
105 ** than 1GiB) the value returned might be less than the true string length. | 105 ** than 1GiB) the value returned might be less than the true string length. |
106 */ | 106 */ |
107 int sqlite3Strlen30(const char *z){ | 107 int sqlite3Strlen30(const char *z){ |
108 const char *z2 = z; | |
109 if( z==0 ) return 0; | 108 if( z==0 ) return 0; |
110 while( *z2 ){ z2++; } | 109 return 0x3fffffff & (int)strlen(z); |
111 return 0x3fffffff & (int)(z2 - z); | |
112 } | 110 } |
113 | 111 |
114 /* | 112 /* |
115 ** Set the current error code to err_code and clear any prior error message. | 113 ** Set the current error code to err_code and clear any prior error message. |
116 */ | 114 */ |
117 void sqlite3Error(sqlite3 *db, int err_code){ | 115 void sqlite3Error(sqlite3 *db, int err_code){ |
118 assert( db!=0 ); | 116 assert( db!=0 ); |
119 db->errCode = err_code; | 117 db->errCode = err_code; |
120 if( db->pErr ) sqlite3ValueSetNull(db->pErr); | 118 if( db->pErr ) sqlite3ValueSetNull(db->pErr); |
121 } | 119 } |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
244 ** there is no consistency, we will define our own. | 242 ** there is no consistency, we will define our own. |
245 ** | 243 ** |
246 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and | 244 ** IMPLEMENTATION-OF: R-30243-02494 The sqlite3_stricmp() and |
247 ** sqlite3_strnicmp() APIs allow applications and extensions to compare | 245 ** sqlite3_strnicmp() APIs allow applications and extensions to compare |
248 ** the contents of two buffers containing UTF-8 strings in a | 246 ** the contents of two buffers containing UTF-8 strings in a |
249 ** case-independent fashion, using the same definition of "case | 247 ** case-independent fashion, using the same definition of "case |
250 ** independence" that SQLite uses internally when comparing identifiers. | 248 ** independence" that SQLite uses internally when comparing identifiers. |
251 */ | 249 */ |
252 int sqlite3_stricmp(const char *zLeft, const char *zRight){ | 250 int sqlite3_stricmp(const char *zLeft, const char *zRight){ |
253 register unsigned char *a, *b; | 251 register unsigned char *a, *b; |
| 252 if( zLeft==0 ){ |
| 253 return zRight ? -1 : 0; |
| 254 }else if( zRight==0 ){ |
| 255 return 1; |
| 256 } |
254 a = (unsigned char *)zLeft; | 257 a = (unsigned char *)zLeft; |
255 b = (unsigned char *)zRight; | 258 b = (unsigned char *)zRight; |
256 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } | 259 while( *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
257 return UpperToLower[*a] - UpperToLower[*b]; | 260 return UpperToLower[*a] - UpperToLower[*b]; |
258 } | 261 } |
259 int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){ | 262 int sqlite3_strnicmp(const char *zLeft, const char *zRight, int N){ |
260 register unsigned char *a, *b; | 263 register unsigned char *a, *b; |
| 264 if( zLeft==0 ){ |
| 265 return zRight ? -1 : 0; |
| 266 }else if( zRight==0 ){ |
| 267 return 1; |
| 268 } |
261 a = (unsigned char *)zLeft; | 269 a = (unsigned char *)zLeft; |
262 b = (unsigned char *)zRight; | 270 b = (unsigned char *)zRight; |
263 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } | 271 while( N-- > 0 && *a!=0 && UpperToLower[*a]==UpperToLower[*b]){ a++; b++; } |
264 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; | 272 return N<0 ? 0 : UpperToLower[*a] - UpperToLower[*b]; |
265 } | 273 } |
266 | 274 |
267 /* | 275 /* |
268 ** The string z[] is an text representation of a real number. | 276 ** The string z[] is an text representation of a real number. |
269 ** Convert this string to a double and write it into *pResult. | 277 ** Convert this string to a double and write it into *pResult. |
270 ** | 278 ** |
(...skipping 269 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
540 if( u>LARGEST_INT64 ){ | 548 if( u>LARGEST_INT64 ){ |
541 *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64; | 549 *pNum = neg ? SMALLEST_INT64 : LARGEST_INT64; |
542 }else if( neg ){ | 550 }else if( neg ){ |
543 *pNum = -(i64)u; | 551 *pNum = -(i64)u; |
544 }else{ | 552 }else{ |
545 *pNum = (i64)u; | 553 *pNum = (i64)u; |
546 } | 554 } |
547 testcase( i==18 ); | 555 testcase( i==18 ); |
548 testcase( i==19 ); | 556 testcase( i==19 ); |
549 testcase( i==20 ); | 557 testcase( i==20 ); |
550 if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) || i>19*incr || nonNum )
{ | 558 if( (c!=0 && &zNum[i]<zEnd) || (i==0 && zStart==zNum) |
| 559 || i>19*incr || nonNum ){ |
551 /* zNum is empty or contains non-numeric text or is longer | 560 /* zNum is empty or contains non-numeric text or is longer |
552 ** than 19 digits (thus guaranteeing that it is too large) */ | 561 ** than 19 digits (thus guaranteeing that it is too large) */ |
553 return 1; | 562 return 1; |
554 }else if( i<19*incr ){ | 563 }else if( i<19*incr ){ |
555 /* Less than 19 digits, so we know that it fits in 64 bits */ | 564 /* Less than 19 digits, so we know that it fits in 64 bits */ |
556 assert( u<=LARGEST_INT64 ); | 565 assert( u<=LARGEST_INT64 ); |
557 return 0; | 566 return 0; |
558 }else{ | 567 }else{ |
559 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */ | 568 /* zNum is a 19-digit numbers. Compare it against 9223372036854775808. */ |
560 c = compare2pow63(zNum, incr); | 569 c = compare2pow63(zNum, incr); |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
638 u = u*16 + sqlite3HexToInt(zNum[i]); | 647 u = u*16 + sqlite3HexToInt(zNum[i]); |
639 } | 648 } |
640 if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){ | 649 if( (u&0x80000000)==0 && sqlite3Isxdigit(zNum[i])==0 ){ |
641 memcpy(pValue, &u, 4); | 650 memcpy(pValue, &u, 4); |
642 return 1; | 651 return 1; |
643 }else{ | 652 }else{ |
644 return 0; | 653 return 0; |
645 } | 654 } |
646 } | 655 } |
647 #endif | 656 #endif |
| 657 while( zNum[0]=='0' ) zNum++; |
648 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){ | 658 for(i=0; i<11 && (c = zNum[i] - '0')>=0 && c<=9; i++){ |
649 v = v*10 + c; | 659 v = v*10 + c; |
650 } | 660 } |
651 | 661 |
652 /* The longest decimal representation of a 32 bit integer is 10 digits: | 662 /* The longest decimal representation of a 32 bit integer is 10 digits: |
653 ** | 663 ** |
654 ** 1234567890 | 664 ** 1234567890 |
655 ** 2^31 -> 2147483648 | 665 ** 2^31 -> 2147483648 |
656 */ | 666 */ |
657 testcase( i==10 ); | 667 testcase( i==10 ); |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
828 b &= SLOT_2_0; | 838 b &= SLOT_2_0; |
829 s = a; | 839 s = a; |
830 /* s: p0<<14 | p2 (masked) */ | 840 /* s: p0<<14 | p2 (masked) */ |
831 | 841 |
832 p++; | 842 p++; |
833 a = a<<14; | 843 a = a<<14; |
834 a |= *p; | 844 a |= *p; |
835 /* a: p0<<28 | p2<<14 | p4 (unmasked) */ | 845 /* a: p0<<28 | p2<<14 | p4 (unmasked) */ |
836 if (!(a&0x80)) | 846 if (!(a&0x80)) |
837 { | 847 { |
838 /* we can skip these cause they were (effectively) done above in calc'ing s
*/ | 848 /* we can skip these cause they were (effectively) done above |
| 849 ** while calculating s */ |
839 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */ | 850 /* a &= (0x7f<<28)|(0x7f<<14)|(0x7f); */ |
840 /* b &= (0x7f<<14)|(0x7f); */ | 851 /* b &= (0x7f<<14)|(0x7f); */ |
841 b = b<<7; | 852 b = b<<7; |
842 a |= b; | 853 a |= b; |
843 s = s>>18; | 854 s = s>>18; |
844 *v = ((u64)s)<<32 | a; | 855 *v = ((u64)s)<<32 | a; |
845 return 5; | 856 return 5; |
846 } | 857 } |
847 | 858 |
848 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ | 859 /* 2:save off p0<<21 | p1<<14 | p2<<7 | p3 (masked) */ |
(...skipping 200 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1049 return n; | 1060 return n; |
1050 } | 1061 } |
1051 #endif | 1062 #endif |
1052 } | 1063 } |
1053 | 1064 |
1054 /* | 1065 /* |
1055 ** Return the number of bytes that will be needed to store the given | 1066 ** Return the number of bytes that will be needed to store the given |
1056 ** 64-bit integer. | 1067 ** 64-bit integer. |
1057 */ | 1068 */ |
1058 int sqlite3VarintLen(u64 v){ | 1069 int sqlite3VarintLen(u64 v){ |
1059 int i = 0; | 1070 int i; |
1060 do{ | 1071 for(i=1; (v >>= 7)!=0; i++){ assert( i<9 ); } |
1061 i++; | |
1062 v >>= 7; | |
1063 }while( v!=0 && ALWAYS(i<9) ); | |
1064 return i; | 1072 return i; |
1065 } | 1073 } |
1066 | 1074 |
1067 | 1075 |
1068 /* | 1076 /* |
1069 ** Read or write a four-byte big-endian integer value. | 1077 ** Read or write a four-byte big-endian integer value. |
1070 */ | 1078 */ |
1071 u32 sqlite3Get4byte(const u8 *p){ | 1079 u32 sqlite3Get4byte(const u8 *p){ |
| 1080 #if SQLITE_BYTEORDER==4321 |
| 1081 u32 x; |
| 1082 memcpy(&x,p,4); |
| 1083 return x; |
| 1084 #elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ |
| 1085 && defined(__GNUC__) && GCC_VERSION>=4003000 |
| 1086 u32 x; |
| 1087 memcpy(&x,p,4); |
| 1088 return __builtin_bswap32(x); |
| 1089 #elif SQLITE_BYTEORDER==1234 && !defined(SQLITE_DISABLE_INTRINSIC) \ |
| 1090 && defined(_MSC_VER) && _MSC_VER>=1300 |
| 1091 u32 x; |
| 1092 memcpy(&x,p,4); |
| 1093 return _byteswap_ulong(x); |
| 1094 #else |
1072 testcase( p[0]&0x80 ); | 1095 testcase( p[0]&0x80 ); |
1073 return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; | 1096 return ((unsigned)p[0]<<24) | (p[1]<<16) | (p[2]<<8) | p[3]; |
| 1097 #endif |
1074 } | 1098 } |
1075 void sqlite3Put4byte(unsigned char *p, u32 v){ | 1099 void sqlite3Put4byte(unsigned char *p, u32 v){ |
| 1100 #if SQLITE_BYTEORDER==4321 |
| 1101 memcpy(p,&v,4); |
| 1102 #elif SQLITE_BYTEORDER==1234 && defined(__GNUC__) && GCC_VERSION>=4003000 |
| 1103 u32 x = __builtin_bswap32(v); |
| 1104 memcpy(p,&x,4); |
| 1105 #elif SQLITE_BYTEORDER==1234 && defined(_MSC_VER) && _MSC_VER>=1300 |
| 1106 u32 x = _byteswap_ulong(v); |
| 1107 memcpy(p,&x,4); |
| 1108 #else |
1076 p[0] = (u8)(v>>24); | 1109 p[0] = (u8)(v>>24); |
1077 p[1] = (u8)(v>>16); | 1110 p[1] = (u8)(v>>16); |
1078 p[2] = (u8)(v>>8); | 1111 p[2] = (u8)(v>>8); |
1079 p[3] = (u8)v; | 1112 p[3] = (u8)v; |
| 1113 #endif |
1080 } | 1114 } |
1081 | 1115 |
1082 | 1116 |
1083 | 1117 |
1084 /* | 1118 /* |
1085 ** Translate a single byte of Hex into an integer. | 1119 ** Translate a single byte of Hex into an integer. |
1086 ** This routine only works if h really is a valid hexadecimal | 1120 ** This routine only works if h really is a valid hexadecimal |
1087 ** character: 0..9a..fA..F | 1121 ** character: 0..9a..fA..F |
1088 */ | 1122 */ |
1089 u8 sqlite3HexToInt(int h){ | 1123 u8 sqlite3HexToInt(int h){ |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1355 if( x<10 ) return 1; | 1389 if( x<10 ) return 1; |
1356 n = x%10; | 1390 n = x%10; |
1357 x /= 10; | 1391 x /= 10; |
1358 if( n>=5 ) n -= 2; | 1392 if( n>=5 ) n -= 2; |
1359 else if( n>=1 ) n -= 1; | 1393 else if( n>=1 ) n -= 1; |
1360 if( x>=3 ){ | 1394 if( x>=3 ){ |
1361 return x>60 ? (u64)LARGEST_INT64 : (n+8)<<(x-3); | 1395 return x>60 ? (u64)LARGEST_INT64 : (n+8)<<(x-3); |
1362 } | 1396 } |
1363 return (n+8)>>(3-x); | 1397 return (n+8)>>(3-x); |
1364 } | 1398 } |
OLD | NEW |