1 /*
2 ** 2007 May 6
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 ** $Id: icu.c,v 1.7 2007/12/13 21:54:11 drh Exp $
13 **
14 ** This file implements an integration between the ICU library
15 ** ("International Components for Unicode", an open-source library
16 ** for handling unicode data) and SQLite. The integration uses
17 ** ICU to provide the following to SQLite:
18 **
19 ** * An implementation of the SQL regexp() function (and hence REGEXP
20 ** operator) using the ICU uregex_XX() APIs.
21 **
22 ** * Implementations of the SQL scalar upper() and lower() functions
23 ** for case mapping.
24 **
25 ** * Integration of ICU and SQLite collation seqences.
26 **
27 ** * An implementation of the LIKE operator that uses ICU to
28 ** provide case-independent matching.
29 */
30
31 #include "sqliteicu.h"
32
33 #if !defined(SQLITE_CORE) || defined(SQLITE_ENABLE_ICU)
34
35 /* Include ICU headers */
36 #include <unicode/utypes.h>
37 #include <unicode/uregex.h>
38 #include <unicode/ustring.h>
39 #include <unicode/ucol.h>
40 #include <unicode/uvernum.h>
41 #if U_ICU_VERSION_MAJOR_NUM>=51
42 #include <unicode/utf_old.h>
43 #endif
44
45 #include <assert.h>
46
47 #ifndef SQLITE_CORE
48 #include "sqlite3ext.h"
49 SQLITE_EXTENSION_INIT1
50 #else
51 #include "sqlite3.h"
52 #endif
53
54 /*
55 ** Maximum length (in bytes) of the pattern in a LIKE or GLOB
56 ** operator.
57 */
58 #ifndef SQLITE_MAX_LIKE_PATTERN_LENGTH
59 # define SQLITE_MAX_LIKE_PATTERN_LENGTH 50000
60 #endif
61
62 /*
63 ** Version of sqlite3_free() that is always a function, never a macro.
64 */
xFree(void * p)65 static void xFree(void *p){
66 sqlite3_free(p);
67 }
68
69 /*
70 ** Compare two UTF-8 strings for equality where the first string is
71 ** a "LIKE" expression. Return true (1) if they are the same and
72 ** false (0) if they are different.
73 */
icuLikeCompare(const uint8_t * zPattern,const uint8_t * zString,const UChar32 uEsc)74 static int icuLikeCompare(
75 const uint8_t *zPattern, /* LIKE pattern */
76 const uint8_t *zString, /* The UTF-8 string to compare against */
77 const UChar32 uEsc /* The escape character */
78 ){
79 static const int MATCH_ONE = (UChar32)'_';
80 static const int MATCH_ALL = (UChar32)'%';
81
82 int iPattern = 0; /* Current byte index in zPattern */
83 int iString = 0; /* Current byte index in zString */
84
85 int prevEscape = 0; /* True if the previous character was uEsc */
86
87 while( zPattern[iPattern]!=0 ){
88
89 /* Read (and consume) the next character from the input pattern. */
90 UChar32 uPattern;
91 U8_NEXT_UNSAFE(zPattern, iPattern, uPattern);
92 assert(uPattern!=0);
93
94 /* There are now 4 possibilities:
95 **
96 ** 1. uPattern is an unescaped match-all character "%",
97 ** 2. uPattern is an unescaped match-one character "_",
98 ** 3. uPattern is an unescaped escape character, or
99 ** 4. uPattern is to be handled as an ordinary character
100 */
101 if( !prevEscape && uPattern==MATCH_ALL ){
102 /* Case 1. */
103 uint8_t c;
104
105 /* Skip any MATCH_ALL or MATCH_ONE characters that follow a
106 ** MATCH_ALL. For each MATCH_ONE, skip one character in the
107 ** test string.
108 */
109 while( (c=zPattern[iPattern]) == MATCH_ALL || c == MATCH_ONE ){
110 if( c==MATCH_ONE ){
111 if( zString[iString]==0 ) return 0;
112 U8_FWD_1_UNSAFE(zString, iString);
113 }
114 iPattern++;
115 }
116
117 if( zPattern[iPattern]==0 ) return 1;
118
119 while( zString[iString] ){
120 if( icuLikeCompare(&zPattern[iPattern], &zString[iString], uEsc) ){
121 return 1;
122 }
123 U8_FWD_1_UNSAFE(zString, iString);
124 }
125 return 0;
126
127 }else if( !prevEscape && uPattern==MATCH_ONE ){
128 /* Case 2. */
129 if( zString[iString]==0 ) return 0;
130 U8_FWD_1_UNSAFE(zString, iString);
131
132 }else if( !prevEscape && uPattern==uEsc){
133 /* Case 3. */
134 prevEscape = 1;
135
136 }else{
137 /* Case 4. */
138 UChar32 uString;
139 U8_NEXT_UNSAFE(zString, iString, uString);
140 uString = u_foldCase(uString, U_FOLD_CASE_DEFAULT);
141 uPattern = u_foldCase(uPattern, U_FOLD_CASE_DEFAULT);
142 if( uString!=uPattern ){
143 return 0;
144 }
145 prevEscape = 0;
146 }
147 }
148
149 return zString[iString]==0;
150 }
151
152 /*
153 ** Implementation of the like() SQL function. This function implements
154 ** the build-in LIKE operator. The first argument to the function is the
155 ** pattern and the second argument is the string. So, the SQL statements:
156 **
157 ** A LIKE B
158 **
159 ** is implemented as like(B, A). If there is an escape character E,
160 **
161 ** A LIKE B ESCAPE E
162 **
163 ** is mapped to like(B, A, E).
164 */
icuLikeFunc(sqlite3_context * context,int argc,sqlite3_value ** argv)165 static void icuLikeFunc(
166 sqlite3_context *context,
167 int argc,
168 sqlite3_value **argv
169 ){
170 const unsigned char *zA = sqlite3_value_text(argv[0]);
171 const unsigned char *zB = sqlite3_value_text(argv[1]);
172 UChar32 uEsc = 0;
173
174 /* Limit the length of the LIKE or GLOB pattern to avoid problems
175 ** of deep recursion and N*N behavior in patternCompare().
176 */
177 if( sqlite3_value_bytes(argv[0])>SQLITE_MAX_LIKE_PATTERN_LENGTH ){
178 sqlite3_result_error(context, "LIKE or GLOB pattern too complex", -1);
179 return;
180 }
181
182
183 if( argc==3 ){
184 /* The escape character string must consist of a single UTF-8 character.
185 ** Otherwise, return an error.
186 */
187 int nE= sqlite3_value_bytes(argv[2]);
188 const unsigned char *zE = sqlite3_value_text(argv[2]);
189 int i = 0;
190 if( zE==nullptr ) return;
191 U8_NEXT(zE, i, nE, uEsc);
192 if( i!=nE){
193 sqlite3_result_error(context,
194 "ESCAPE expression must be a single character", -1);
195 return;
196 }
197 }
198
199 if( zA && zB ){
200 sqlite3_result_int(context, icuLikeCompare(zA, zB, uEsc));
201 }
202 }
203
204 /*
205 ** This function is called when an ICU function called from within
206 ** the implementation of an SQL scalar function returns an error.
207 **
208 ** The scalar function context passed as the first argument is
209 ** loaded with an error message based on the following two args.
210 */
icuFunctionError(sqlite3_context * pCtx,const char * zName,UErrorCode e)211 static void icuFunctionError(
212 sqlite3_context *pCtx, /* SQLite scalar function context */
213 const char *zName, /* Name of ICU function that failed */
214 UErrorCode e /* Error code returned by ICU function */
215 ){
216 char zBuf[128];
217 sqlite3_snprintf(128, zBuf, "ICU error: %s(): %s", zName, u_errorName(e));
218 zBuf[127] = '\0';
219 sqlite3_result_error(pCtx, zBuf, -1);
220 }
221
222 /*
223 ** Function to delete compiled regexp objects. Registered as
224 ** a destructor function with sqlite3_set_auxdata().
225 */
icuRegexpDelete(void * p)226 static void icuRegexpDelete(void *p){
227 URegularExpression *pExpr = (URegularExpression *)p;
228 uregex_close(pExpr);
229 }
230
231 /*
232 ** Implementation of SQLite REGEXP operator. This scalar function takes
233 ** two arguments. The first is a regular expression pattern to compile
234 ** the second is a string to match against that pattern. If either
235 ** argument is an SQL NULL, then NULL is returned. Otherwise, the result
236 ** is 1 if the string matches the pattern, or 0 otherwise.
237 **
238 ** SQLite maps the regexp() function to the regexp() operator such
239 ** that the following two are equivalent:
240 **
241 ** zString REGEXP zPattern
242 ** regexp(zPattern, zString)
243 **
244 ** Uses the following ICU regexp APIs:
245 **
246 ** uregex_open()
247 ** uregex_matches()
248 ** uregex_close()
249 */
icuRegexpFunc(sqlite3_context * p,int nArg,sqlite3_value ** apArg)250 static void icuRegexpFunc(sqlite3_context *p, int nArg, sqlite3_value **apArg){
251 UErrorCode status = U_ZERO_ERROR;
252 URegularExpression *pExpr;
253 UBool res;
254 const UChar *zString = static_cast<const UChar *>(sqlite3_value_text16(apArg[1]));
255
256 (void)nArg; /* Unused parameter */
257
258 /* If the left hand side of the regexp operator is NULL,
259 ** then the result is also NULL.
260 */
261 if( !zString ){
262 return;
263 }
264
265 pExpr = static_cast<URegularExpression*>(sqlite3_get_auxdata(p, 0));
266 if( !pExpr ){
267 const UChar *zPattern = static_cast<const UChar *>(sqlite3_value_text16(apArg[0]));
268 if( !zPattern ){
269 return;
270 }
271 pExpr = uregex_open(zPattern, -1, 0, nullptr, &status);
272
273 if( U_SUCCESS(status) ){
274 sqlite3_set_auxdata(p, 0, pExpr, icuRegexpDelete);
275 }else{
276 assert(!pExpr);
277 icuFunctionError(p, "uregex_open", status);
278 return;
279 }
280 }
281
282 /* Configure the text that the regular expression operates on. */
283 uregex_setText(pExpr, zString, -1, &status);
284 if( !U_SUCCESS(status) ){
285 icuFunctionError(p, "uregex_setText", status);
286 return;
287 }
288
289 /* Attempt the match */
290 res = uregex_matches(pExpr, 0, &status);
291 if( !U_SUCCESS(status) ){
292 icuFunctionError(p, "uregex_matches", status);
293 return;
294 }
295
296 /* Set the text that the regular expression operates on to a NULL
297 ** pointer. This is not really necessary, but it is tidier than
298 ** leaving the regular expression object configured with an invalid
299 ** pointer after this function returns.
300 */
301 uregex_setText(pExpr, nullptr, 0, &status);
302
303 /* Return 1 or 0. */
304 sqlite3_result_int(p, res ? 1 : 0);
305 }
306
307 /*
308 ** Implementations of scalar functions for case mapping - upper() and
309 ** lower(). Function upper() converts its input to upper-case (ABC).
310 ** Function lower() converts to lower-case (abc).
311 **
312 ** ICU provides two types of case mapping, "general" case mapping and
313 ** "language specific". Refer to ICU documentation for the differences
314 ** between the two.
315 **
316 ** To utilise "general" case mapping, the upper() or lower() scalar
317 ** functions are invoked with one argument:
318 **
319 ** upper('ABC') -> 'abc'
320 ** lower('abc') -> 'ABC'
321 **
322 ** To access ICU "language specific" case mapping, upper() or lower()
323 ** should be invoked with two arguments. The second argument is the name
324 ** of the locale to use. Passing an empty string ("") or SQL NULL value
325 ** as the second argument is the same as invoking the 1 argument version
326 ** of upper() or lower().
327 **
328 ** lower('I', 'en_us') -> 'i'
329 ** lower('I', 'tr_tr') -> 'ı' (small dotless i)
330 **
331 ** https://www.icu-project.org/userguide/posix.html#case_mappings
332 */
icuCaseFunc16(sqlite3_context * p,int nArg,sqlite3_value ** apArg)333 static void icuCaseFunc16(sqlite3_context *p, int nArg, sqlite3_value **apArg){
334 const UChar *zInput;
335 UChar *zOutput;
336 int nInput;
337 int nOutput;
338
339 UErrorCode status = U_ZERO_ERROR;
340 const unsigned char *zLocale = nullptr;
341
342 assert(nArg==1 || nArg==2);
343 if( nArg==2 ){
344 zLocale = static_cast<const unsigned char *>(sqlite3_value_text(apArg[1]));
345 }
346
347 zInput = static_cast<const UChar *>(sqlite3_value_text16(apArg[0]));
348 if( !zInput ){
349 return;
350 }
351 nInput = sqlite3_value_bytes16(apArg[0]);
352
353 nOutput = nInput * 2 + 2;
354 zOutput = static_cast<UChar *>(sqlite3_malloc(nOutput));
355 if( !zOutput ){
356 return;
357 }
358
359 if( sqlite3_user_data(p) ){
360 u_strToUpper(zOutput, nOutput/2, zInput, nInput/2, reinterpret_cast<const char*>(zLocale), &status);
361 }else{
362 u_strToLower(zOutput, nOutput/2, zInput, nInput/2, reinterpret_cast<const char*>(zLocale), &status);
363 }
364
365 if( !U_SUCCESS(status) ){
366 icuFunctionError(p, "u_strToLower()/u_strToUpper", status);
367 return;
368 }
369
370 sqlite3_result_text16(p, zOutput, -1, xFree);
371 }
372
373 /*
374 ** Collation sequence destructor function. The pCtx argument points to
375 ** a UCollator structure previously allocated using ucol_open().
376 */
icuCollationDel(void * pCtx)377 static void icuCollationDel(void *pCtx){
378 UCollator *p = (UCollator *)pCtx;
379 ucol_close(p);
380 }
381
382 /*
383 ** Collation sequence comparison function. The pCtx argument points to
384 ** a UCollator structure previously allocated using ucol_open().
385 */
icuCollationColl(void * pCtx,int nLeft,const void * zLeft,int nRight,const void * zRight)386 static int icuCollationColl(
387 void *pCtx,
388 int nLeft,
389 const void *zLeft,
390 int nRight,
391 const void *zRight
392 ){
393 UCollationResult res;
394 UCollator *p = (UCollator *)pCtx;
395 res = ucol_strcoll(p, (UChar *)zLeft, nLeft/2, (UChar *)zRight, nRight/2);
396 switch( res ){
397 case UCOL_LESS: return -1;
398 case UCOL_GREATER: return +1;
399 case UCOL_EQUAL: return 0;
400 }
401 assert(!"Unexpected return value from ucol_strcoll()");
402 return 0;
403 }
404
405 /*
406 ** Implementation of the scalar function icu_load_collation().
407 **
408 ** This scalar function is used to add ICU collation based collation
409 ** types to an SQLite database connection. It is intended to be called
410 ** as follows:
411 **
412 ** SELECT icu_load_collation(<locale>, <collation-name>);
413 **
414 ** Where <locale> is a string containing an ICU locale identifier (i.e.
415 ** "en_AU", "tr_TR" etc.) and <collation-name> is the name of the
416 ** collation sequence to create.
417 */
icuLoadCollation(sqlite3_context * p,int nArg,sqlite3_value ** apArg)418 static void icuLoadCollation(
419 sqlite3_context *p,
420 int nArg,
421 sqlite3_value **apArg
422 ){
423 sqlite3 *db = (sqlite3 *)sqlite3_user_data(p);
424 UErrorCode status = U_ZERO_ERROR;
425 const char *zLocale; /* Locale identifier - (eg. "jp_JP") */
426 const char *zName; /* SQL Collation sequence name (eg. "japanese") */
427 UCollator *pUCollator; /* ICU library collation object */
428 int rc; /* Return code from sqlite3_create_collation_x() */
429
430 assert(nArg==2);
431 zLocale = (const char *)sqlite3_value_text(apArg[0]);
432 zName = (const char *)sqlite3_value_text(apArg[1]);
433
434 if( !zLocale || !zName ){
435 return;
436 }
437
438 pUCollator = ucol_open(zLocale, &status);
439 if( !U_SUCCESS(status) ){
440 icuFunctionError(p, "ucol_open", status);
441 return;
442 }
443 assert(p);
444
445 rc = sqlite3_create_collation_v2(db, zName, SQLITE_UTF16, (void *)pUCollator,
446 icuCollationColl, icuCollationDel
447 );
448 if( rc!=SQLITE_OK ){
449 ucol_close(pUCollator);
450 sqlite3_result_error(p, "Error registering collation function", -1);
451 }
452 }
453
454 /*
455 ** Register the ICU extension functions with database db.
456 */
sqlite3IcuInit(sqlite3 * db)457 KDB_SQLITE_ICU_EXPORT int sqlite3IcuInit(sqlite3 *db){
458 struct IcuScalar {
459 const char *zName; /* Function name */
460 int nArg; /* Number of arguments */
461 int enc; /* Optimal text encoding */
462 void *pContext; /* sqlite3_user_data() context */
463 void (*xFunc)(sqlite3_context*,int,sqlite3_value**);
464 } scalars[] = {
465 {"regexp", 2, SQLITE_ANY, nullptr, icuRegexpFunc},
466
467 {"lower", 1, SQLITE_UTF16, nullptr, icuCaseFunc16},
468 {"lower", 2, SQLITE_UTF16, nullptr, icuCaseFunc16},
469 {"upper", 1, SQLITE_UTF16, (void*)1, icuCaseFunc16},
470 {"upper", 2, SQLITE_UTF16, (void*)1, icuCaseFunc16},
471
472 {"lower", 1, SQLITE_UTF8, nullptr, icuCaseFunc16},
473 {"lower", 2, SQLITE_UTF8, nullptr, icuCaseFunc16},
474 {"upper", 1, SQLITE_UTF8, (void*)1, icuCaseFunc16},
475 {"upper", 2, SQLITE_UTF8, (void*)1, icuCaseFunc16},
476
477 {"like", 2, SQLITE_UTF8, nullptr, icuLikeFunc},
478 {"like", 3, SQLITE_UTF8, nullptr, icuLikeFunc},
479
480 {"icu_load_collation", 2, SQLITE_UTF8, (void*)db, icuLoadCollation},
481 };
482
483 int rc = SQLITE_OK;
484 int i;
485
486 for(i=0; rc==SQLITE_OK && i<(int)(sizeof(scalars)/sizeof(scalars[0])); i++){
487 struct IcuScalar *p = &scalars[i];
488 rc = sqlite3_create_function(
489 db, p->zName, p->nArg, p->enc, p->pContext, p->xFunc, nullptr, nullptr
490 );
491 }
492
493 return rc;
494 }
495
496 #if !defined SQLITE_CORE || !SQLITE_CORE
sqlite3_extension_init(sqlite3 * db,char ** pzErrMsg,const struct sqlite3_api_routines * pApi)497 KDB_SQLITE_ICU_EXPORT int sqlite3_extension_init(
498 sqlite3 *db,
499 char **pzErrMsg,
500 const struct sqlite3_api_routines *pApi
501 ){
502 (void)pzErrMsg; /* Unused parameter */
503 SQLITE_EXTENSION_INIT2(pApi)
504 return sqlite3IcuInit(db);
505 }
506 #endif
507
508 #endif
509