OLD | NEW |
| (Empty) |
1 /* | |
2 ** 2014 May 31 | |
3 ** | |
4 ** The author disclaims copyright to this source code. In place of | |
5 ** a legal notice, here is a blessing: | |
6 ** | |
7 ** May you do good and not evil. | |
8 ** May you find forgiveness for yourself and forgive others. | |
9 ** May you share freely, never taking more than you give. | |
10 ** | |
11 ****************************************************************************** | |
12 */ | |
13 | |
14 | |
15 | |
16 #include "fts5Int.h" | |
17 | |
18 int sqlite3Fts5BufferSize(int *pRc, Fts5Buffer *pBuf, int nByte){ | |
19 int nNew = pBuf->nSpace ? pBuf->nSpace*2 : 64; | |
20 u8 *pNew; | |
21 while( nNew<nByte ){ | |
22 nNew = nNew * 2; | |
23 } | |
24 pNew = sqlite3_realloc(pBuf->p, nNew); | |
25 if( pNew==0 ){ | |
26 *pRc = SQLITE_NOMEM; | |
27 return 1; | |
28 }else{ | |
29 pBuf->nSpace = nNew; | |
30 pBuf->p = pNew; | |
31 } | |
32 return 0; | |
33 } | |
34 | |
35 | |
36 /* | |
37 ** Encode value iVal as an SQLite varint and append it to the buffer object | |
38 ** pBuf. If an OOM error occurs, set the error code in p. | |
39 */ | |
40 void sqlite3Fts5BufferAppendVarint(int *pRc, Fts5Buffer *pBuf, i64 iVal){ | |
41 if( fts5BufferGrow(pRc, pBuf, 9) ) return; | |
42 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], iVal); | |
43 } | |
44 | |
45 void sqlite3Fts5Put32(u8 *aBuf, int iVal){ | |
46 aBuf[0] = (iVal>>24) & 0x00FF; | |
47 aBuf[1] = (iVal>>16) & 0x00FF; | |
48 aBuf[2] = (iVal>> 8) & 0x00FF; | |
49 aBuf[3] = (iVal>> 0) & 0x00FF; | |
50 } | |
51 | |
52 int sqlite3Fts5Get32(const u8 *aBuf){ | |
53 return (aBuf[0] << 24) + (aBuf[1] << 16) + (aBuf[2] << 8) + aBuf[3]; | |
54 } | |
55 | |
56 /* | |
57 ** Append buffer nData/pData to buffer pBuf. If an OOM error occurs, set | |
58 ** the error code in p. If an error has already occurred when this function | |
59 ** is called, it is a no-op. | |
60 */ | |
61 void sqlite3Fts5BufferAppendBlob( | |
62 int *pRc, | |
63 Fts5Buffer *pBuf, | |
64 int nData, | |
65 const u8 *pData | |
66 ){ | |
67 assert( *pRc || nData>=0 ); | |
68 if( fts5BufferGrow(pRc, pBuf, nData) ) return; | |
69 memcpy(&pBuf->p[pBuf->n], pData, nData); | |
70 pBuf->n += nData; | |
71 } | |
72 | |
73 /* | |
74 ** Append the nul-terminated string zStr to the buffer pBuf. This function | |
75 ** ensures that the byte following the buffer data is set to 0x00, even | |
76 ** though this byte is not included in the pBuf->n count. | |
77 */ | |
78 void sqlite3Fts5BufferAppendString( | |
79 int *pRc, | |
80 Fts5Buffer *pBuf, | |
81 const char *zStr | |
82 ){ | |
83 int nStr = (int)strlen(zStr); | |
84 sqlite3Fts5BufferAppendBlob(pRc, pBuf, nStr+1, (const u8*)zStr); | |
85 pBuf->n--; | |
86 } | |
87 | |
88 /* | |
89 ** Argument zFmt is a printf() style format string. This function performs | |
90 ** the printf() style processing, then appends the results to buffer pBuf. | |
91 ** | |
92 ** Like sqlite3Fts5BufferAppendString(), this function ensures that the byte | |
93 ** following the buffer data is set to 0x00, even though this byte is not | |
94 ** included in the pBuf->n count. | |
95 */ | |
96 void sqlite3Fts5BufferAppendPrintf( | |
97 int *pRc, | |
98 Fts5Buffer *pBuf, | |
99 char *zFmt, ... | |
100 ){ | |
101 if( *pRc==SQLITE_OK ){ | |
102 char *zTmp; | |
103 va_list ap; | |
104 va_start(ap, zFmt); | |
105 zTmp = sqlite3_vmprintf(zFmt, ap); | |
106 va_end(ap); | |
107 | |
108 if( zTmp==0 ){ | |
109 *pRc = SQLITE_NOMEM; | |
110 }else{ | |
111 sqlite3Fts5BufferAppendString(pRc, pBuf, zTmp); | |
112 sqlite3_free(zTmp); | |
113 } | |
114 } | |
115 } | |
116 | |
117 char *sqlite3Fts5Mprintf(int *pRc, const char *zFmt, ...){ | |
118 char *zRet = 0; | |
119 if( *pRc==SQLITE_OK ){ | |
120 va_list ap; | |
121 va_start(ap, zFmt); | |
122 zRet = sqlite3_vmprintf(zFmt, ap); | |
123 va_end(ap); | |
124 if( zRet==0 ){ | |
125 *pRc = SQLITE_NOMEM; | |
126 } | |
127 } | |
128 return zRet; | |
129 } | |
130 | |
131 | |
132 /* | |
133 ** Free any buffer allocated by pBuf. Zero the structure before returning. | |
134 */ | |
135 void sqlite3Fts5BufferFree(Fts5Buffer *pBuf){ | |
136 sqlite3_free(pBuf->p); | |
137 memset(pBuf, 0, sizeof(Fts5Buffer)); | |
138 } | |
139 | |
140 /* | |
141 ** Zero the contents of the buffer object. But do not free the associated | |
142 ** memory allocation. | |
143 */ | |
144 void sqlite3Fts5BufferZero(Fts5Buffer *pBuf){ | |
145 pBuf->n = 0; | |
146 } | |
147 | |
148 /* | |
149 ** Set the buffer to contain nData/pData. If an OOM error occurs, leave an | |
150 ** the error code in p. If an error has already occurred when this function | |
151 ** is called, it is a no-op. | |
152 */ | |
153 void sqlite3Fts5BufferSet( | |
154 int *pRc, | |
155 Fts5Buffer *pBuf, | |
156 int nData, | |
157 const u8 *pData | |
158 ){ | |
159 pBuf->n = 0; | |
160 sqlite3Fts5BufferAppendBlob(pRc, pBuf, nData, pData); | |
161 } | |
162 | |
163 int sqlite3Fts5PoslistNext64( | |
164 const u8 *a, int n, /* Buffer containing poslist */ | |
165 int *pi, /* IN/OUT: Offset within a[] */ | |
166 i64 *piOff /* IN/OUT: Current offset */ | |
167 ){ | |
168 int i = *pi; | |
169 if( i>=n ){ | |
170 /* EOF */ | |
171 *piOff = -1; | |
172 return 1; | |
173 }else{ | |
174 i64 iOff = *piOff; | |
175 int iVal; | |
176 fts5FastGetVarint32(a, i, iVal); | |
177 if( iVal==1 ){ | |
178 fts5FastGetVarint32(a, i, iVal); | |
179 iOff = ((i64)iVal) << 32; | |
180 fts5FastGetVarint32(a, i, iVal); | |
181 } | |
182 *piOff = iOff + (iVal-2); | |
183 *pi = i; | |
184 return 0; | |
185 } | |
186 } | |
187 | |
188 | |
189 /* | |
190 ** Advance the iterator object passed as the only argument. Return true | |
191 ** if the iterator reaches EOF, or false otherwise. | |
192 */ | |
193 int sqlite3Fts5PoslistReaderNext(Fts5PoslistReader *pIter){ | |
194 if( sqlite3Fts5PoslistNext64(pIter->a, pIter->n, &pIter->i, &pIter->iPos) ){ | |
195 pIter->bEof = 1; | |
196 } | |
197 return pIter->bEof; | |
198 } | |
199 | |
200 int sqlite3Fts5PoslistReaderInit( | |
201 const u8 *a, int n, /* Poslist buffer to iterate through */ | |
202 Fts5PoslistReader *pIter /* Iterator object to initialize */ | |
203 ){ | |
204 memset(pIter, 0, sizeof(*pIter)); | |
205 pIter->a = a; | |
206 pIter->n = n; | |
207 sqlite3Fts5PoslistReaderNext(pIter); | |
208 return pIter->bEof; | |
209 } | |
210 | |
211 int sqlite3Fts5PoslistWriterAppend( | |
212 Fts5Buffer *pBuf, | |
213 Fts5PoslistWriter *pWriter, | |
214 i64 iPos | |
215 ){ | |
216 static const i64 colmask = ((i64)(0x7FFFFFFF)) << 32; | |
217 int rc = SQLITE_OK; | |
218 if( 0==fts5BufferGrow(&rc, pBuf, 5+5+5) ){ | |
219 if( (iPos & colmask) != (pWriter->iPrev & colmask) ){ | |
220 pBuf->p[pBuf->n++] = 1; | |
221 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos>>32)); | |
222 pWriter->iPrev = (iPos & colmask); | |
223 } | |
224 pBuf->n += sqlite3Fts5PutVarint(&pBuf->p[pBuf->n], (iPos-pWriter->iPrev)+2); | |
225 pWriter->iPrev = iPos; | |
226 } | |
227 return rc; | |
228 } | |
229 | |
230 void *sqlite3Fts5MallocZero(int *pRc, int nByte){ | |
231 void *pRet = 0; | |
232 if( *pRc==SQLITE_OK ){ | |
233 pRet = sqlite3_malloc(nByte); | |
234 if( pRet==0 && nByte>0 ){ | |
235 *pRc = SQLITE_NOMEM; | |
236 }else{ | |
237 memset(pRet, 0, nByte); | |
238 } | |
239 } | |
240 return pRet; | |
241 } | |
242 | |
243 /* | |
244 ** Return a nul-terminated copy of the string indicated by pIn. If nIn | |
245 ** is non-negative, then it is the length of the string in bytes. Otherwise, | |
246 ** the length of the string is determined using strlen(). | |
247 ** | |
248 ** It is the responsibility of the caller to eventually free the returned | |
249 ** buffer using sqlite3_free(). If an OOM error occurs, NULL is returned. | |
250 */ | |
251 char *sqlite3Fts5Strndup(int *pRc, const char *pIn, int nIn){ | |
252 char *zRet = 0; | |
253 if( *pRc==SQLITE_OK ){ | |
254 if( nIn<0 ){ | |
255 nIn = (int)strlen(pIn); | |
256 } | |
257 zRet = (char*)sqlite3_malloc(nIn+1); | |
258 if( zRet ){ | |
259 memcpy(zRet, pIn, nIn); | |
260 zRet[nIn] = '\0'; | |
261 }else{ | |
262 *pRc = SQLITE_NOMEM; | |
263 } | |
264 } | |
265 return zRet; | |
266 } | |
267 | |
268 | |
269 /* | |
270 ** Return true if character 't' may be part of an FTS5 bareword, or false | |
271 ** otherwise. Characters that may be part of barewords: | |
272 ** | |
273 ** * All non-ASCII characters, | |
274 ** * The 52 upper and lower case ASCII characters, and | |
275 ** * The 10 integer ASCII characters. | |
276 ** * The underscore character "_" (0x5F). | |
277 ** * The unicode "subsitute" character (0x1A). | |
278 */ | |
279 int sqlite3Fts5IsBareword(char t){ | |
280 u8 aBareword[128] = { | |
281 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x00 .. 0x0F */ | |
282 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, /* 0x10 .. 0x1F */ | |
283 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 0x20 .. 0x2F */ | |
284 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, /* 0x30 .. 0x3F */ | |
285 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x40 .. 0x4F */ | |
286 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, /* 0x50 .. 0x5F */ | |
287 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, /* 0x60 .. 0x6F */ | |
288 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 /* 0x70 .. 0x7F */ | |
289 }; | |
290 | |
291 return (t & 0x80) || aBareword[(int)t]; | |
292 } | |
293 | |
294 | |
OLD | NEW |