blob: d2f1802d64ec79eaeef9e371421009eca87f3072 [file] [log] [blame]
The Android Open Source Project7790ef52009-03-03 19:30:40 -08001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "sqlite3_android"
18
19#include <ctype.h>
20#include <stdlib.h>
21#include <string.h>
22#include <unistd.h>
23
Jiyong Parkb4076df2017-08-09 20:15:42 +090024#ifdef SQLITE_ENABLE_ICU
The Android Open Source Project7790ef52009-03-03 19:30:40 -080025#include <unicode/ucol.h>
Dmitri Plotnikov3a749622010-03-03 11:29:46 -080026#include <unicode/uiter.h>
The Android Open Source Project7790ef52009-03-03 19:30:40 -080027#include <unicode/ustring.h>
Dmitri Plotnikov3a749622010-03-03 11:29:46 -080028#include <unicode/utypes.h>
Jiyong Parkb4076df2017-08-09 20:15:42 +090029#endif //SQLITE_ENABLE_ICU
Colin Crossea170612017-04-19 17:36:28 -070030#include <log/log.h>
The Android Open Source Project7790ef52009-03-03 19:30:40 -080031
32#include "sqlite3_android.h"
33#include "PhoneNumberUtils.h"
34
35#define ENABLE_ANDROID_LOG 0
Dmitri Plotnikov3a749622010-03-03 11:29:46 -080036#define SMALL_BUFFER_SIZE 10
Dmitri Plotnikov46bdb5c2011-01-06 13:48:50 -080037#define PHONE_NUMBER_BUFFER_SIZE 40
The Android Open Source Project7790ef52009-03-03 19:30:40 -080038
Jiyong Parkb4076df2017-08-09 20:15:42 +090039#ifdef SQLITE_ENABLE_ICU
The Android Open Source Project7790ef52009-03-03 19:30:40 -080040static int collate16(void *p, int n1, const void *v1, int n2, const void *v2)
41{
42 UCollator *coll = (UCollator *) p;
43 UCollationResult result = ucol_strcoll(coll, (const UChar *) v1, n1,
44 (const UChar *) v2, n2);
45
46 if (result == UCOL_LESS) {
47 return -1;
48 } else if (result == UCOL_GREATER) {
49 return 1;
50 } else {
51 return 0;
52 }
53}
54
55static int collate8(void *p, int n1, const void *v1, int n2, const void *v2)
56{
57 UCollator *coll = (UCollator *) p;
58 UCharIterator i1, i2;
59 UErrorCode status = U_ZERO_ERROR;
60
61 uiter_setUTF8(&i1, (const char *) v1, n1);
62 uiter_setUTF8(&i2, (const char *) v2, n2);
63
64 UCollationResult result = ucol_strcollIter(coll, &i1, &i2, &status);
65
66 if (U_FAILURE(status)) {
Steve Blockcbfefda2012-01-06 19:14:58 +000067// ALOGE("Collation iterator error: %d\n", status);
The Android Open Source Project7790ef52009-03-03 19:30:40 -080068 }
69
70 if (result == UCOL_LESS) {
71 return -1;
72 } else if (result == UCOL_GREATER) {
73 return 1;
74 } else {
75 return 0;
76 }
77}
Jiyong Parkb4076df2017-08-09 20:15:42 +090078#endif // SQLITE_ENABLE_ICU
The Android Open Source Project7790ef52009-03-03 19:30:40 -080079
80static void phone_numbers_equal(sqlite3_context * context, int argc, sqlite3_value ** argv)
81{
Taesu Lee33fdf2e2019-05-29 14:09:11 +090082 if (argc != 2 && argc != 3 && argc != 4) {
The Android Open Source Project7790ef52009-03-03 19:30:40 -080083 sqlite3_result_int(context, 0);
84 return;
85 }
86
87 char const * num1 = (char const *)sqlite3_value_text(argv[0]);
88 char const * num2 = (char const *)sqlite3_value_text(argv[1]);
89
Daisuke Miyakawa948a1192009-09-19 19:19:53 -070090 bool use_strict = false;
Taesu Lee33fdf2e2019-05-29 14:09:11 +090091 int min_match = 0;
92 if (argc == 3 || argc == 4) {
Daisuke Miyakawa948a1192009-09-19 19:19:53 -070093 use_strict = (sqlite3_value_int(argv[2]) != 0);
Taesu Lee33fdf2e2019-05-29 14:09:11 +090094 if (!use_strict && argc == 4) {
95 min_match = sqlite3_value_int(argv[3]);
96 }
Daisuke Miyakawa948a1192009-09-19 19:19:53 -070097 }
98
The Android Open Source Project7790ef52009-03-03 19:30:40 -080099 if (num1 == NULL || num2 == NULL) {
100 sqlite3_result_null(context);
101 return;
102 }
103
Daisuke Miyakawa948a1192009-09-19 19:19:53 -0700104 bool equal =
Taesu Lee33fdf2e2019-05-29 14:09:11 +0900105 (use_strict ? android::phone_number_compare_strict(num1, num2)
106 : ((min_match > 0)
107 ? android::phone_number_compare_loose_with_minmatch(
108 num1, num2, min_match)
109 : android::phone_number_compare_loose(num1, num2)));
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800110
111 if (equal) {
112 sqlite3_result_int(context, 1);
113 } else {
114 sqlite3_result_int(context, 0);
115 }
116}
117
Dmitri Plotnikov46bdb5c2011-01-06 13:48:50 -0800118static void phone_number_stripped_reversed(sqlite3_context * context, int argc,
119 sqlite3_value ** argv)
120{
121 if (argc != 1) {
122 sqlite3_result_int(context, 0);
123 return;
124 }
125
126 char const * number = (char const *)sqlite3_value_text(argv[0]);
127 if (number == NULL) {
128 sqlite3_result_null(context);
129 return;
130 }
131
132 char out[PHONE_NUMBER_BUFFER_SIZE];
133 int outlen = 0;
134 android::phone_number_stripped_reversed_inter(number, out, PHONE_NUMBER_BUFFER_SIZE, &outlen);
135 sqlite3_result_text(context, (const char*)out, outlen, SQLITE_TRANSIENT);
136}
137
138
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800139#if ENABLE_ANDROID_LOG
140static void android_log(sqlite3_context * context, int argc, sqlite3_value ** argv)
141{
142 char const * tag = "sqlite_trigger";
143 char const * msg = "";
144 int msgIndex = 0;
145
146 switch (argc) {
147 case 2:
148 tag = (char const *)sqlite3_value_text(argv[0]);
149 if (tag == NULL) {
150 tag = "sqlite_trigger";
151 }
152 msgIndex = 1;
153 case 1:
154 msg = (char const *)sqlite3_value_text(argv[msgIndex]);
155 if (msg == NULL) {
156 msg = "";
157 }
Hyejin Kim165c6872013-02-28 16:21:20 +0900158 ALOG(LOG_INFO, tag, "%s", msg);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800159 sqlite3_result_int(context, 1);
160 return;
161
162 default:
163 sqlite3_result_int(context, 0);
164 return;
165 }
166}
167#endif
168
169static void delete_file(sqlite3_context * context, int argc, sqlite3_value ** argv)
170{
171 if (argc != 1) {
172 sqlite3_result_int(context, 0);
173 return;
174 }
175
176 char const * path = (char const *)sqlite3_value_text(argv[0]);
Mike Lockwood5a345992011-07-07 12:22:34 -0400177 // Don't allow ".." in paths
178 if (path == NULL || strstr(path, "/../") != NULL) {
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800179 sqlite3_result_null(context);
180 return;
181 }
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800182
Mike Lockwood5a345992011-07-07 12:22:34 -0400183 // We only allow deleting files in the EXTERNAL_STORAGE path, or one of the
184 // SECONDARY_STORAGE paths
185 bool good_path = false;
186 char const * external_storage = getenv("EXTERNAL_STORAGE");
187 if (external_storage && strncmp(external_storage, path, strlen(external_storage)) == 0) {
188 good_path = true;
189 } else {
190 // check SECONDARY_STORAGE, which should be a colon separated list of paths
191 char const * secondary_paths = getenv("SECONDARY_STORAGE");
192 while (secondary_paths && secondary_paths[0]) {
193 const char* colon = strchr(secondary_paths, ':');
194 int length = (colon ? colon - secondary_paths : strlen(secondary_paths));
195 if (strncmp(secondary_paths, path, length) == 0) {
196 good_path = true;
197 }
198 secondary_paths += length;
199 while (*secondary_paths == ':') secondary_paths++;
200 }
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800201 }
Mike Lockwood5a345992011-07-07 12:22:34 -0400202
203 if (!good_path) {
Marco Nelissen2da78c02009-05-06 11:08:08 -0700204 sqlite3_result_null(context);
205 return;
206 }
207
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800208 int err = unlink(path);
209 if (err != -1) {
210 // No error occured, return true
211 sqlite3_result_int(context, 1);
212 } else {
213 // An error occured, return false
214 sqlite3_result_int(context, 0);
215 }
216}
217
Jiyong Parkb4076df2017-08-09 20:15:42 +0900218#ifdef SQLITE_ENABLE_ICU
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800219static void tokenize_auxdata_delete(void * data)
220{
221 sqlite3_stmt * statement = (sqlite3_stmt *)data;
222 sqlite3_finalize(statement);
223}
224
225static void base16Encode(char* dest, const char* src, uint32_t size)
226{
227 static const char * BASE16_TABLE = "0123456789abcdef";
228 for (uint32_t i = 0; i < size; i++) {
229 char ch = *src++;
230 *dest++ = BASE16_TABLE[ (ch & 0xf0) >> 4 ];
231 *dest++ = BASE16_TABLE[ (ch & 0x0f) ];
232 }
233}
234
235struct SqliteUserData {
236 sqlite3 * handle;
237 UCollator* collator;
238};
239
240/**
241 * This function is invoked as:
242 *
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100243 * _TOKENIZE('<token_table>', <data_row_id>, <data>, <delimiter>,
244 * <use_token_index>, <data_tag>)
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800245 *
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100246 * If <use_token_index> is omitted, it is treated as 0.
247 * If <data_tag> is omitted, it is treated as NULL.
Bjorn Bringert687f1162009-05-13 22:13:09 +0100248 *
249 * It will split <data> on each instance of <delimiter> and insert each token
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100250 * into <token_table>. The following columns in <token_table> are used:
251 * token TEXT, source INTEGER, token_index INTEGER, tag (any type)
252 * The token_index column is not required if <use_token_index> is 0.
253 * The tag column is not required if <data_tag> is NULL.
Bjorn Bringert687f1162009-05-13 22:13:09 +0100254 *
255 * One row is inserted for each token in <data>.
256 * In each inserted row, 'source' is <data_row_id>.
257 * In the first inserted row, 'token' is the hex collation key of
258 * the entire <data> string, and 'token_index' is 0.
259 * In each row I (where 1 <= I < N, and N is the number of tokens in <data>)
260 * 'token' will be set to the hex collation key of the I:th token (0-based).
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100261 * If <use_token_index> != 0, 'token_index' is set to I.
262 * If <data_tag> is not NULL, 'tag' is set to <data_tag>.
Bjorn Bringert687f1162009-05-13 22:13:09 +0100263 *
264 * In other words, there will be one row for the entire string,
265 * and one row for each token except the first one.
266 *
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800267 * The function returns the number of tokens generated.
268 */
269static void tokenize(sqlite3_context * context, int argc, sqlite3_value ** argv)
270{
Steve Blockbcf3ccc2011-12-20 16:21:48 +0000271 //ALOGD("enter tokenize");
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800272 int err;
Bjorn Bringert687f1162009-05-13 22:13:09 +0100273 int useTokenIndex = 0;
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100274 int useDataTag = 0;
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800275
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100276 if (!(argc >= 4 || argc <= 6)) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000277 ALOGE("Tokenize requires 4 to 6 arguments");
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800278 sqlite3_result_null(context);
279 return;
280 }
281
Bjorn Bringert687f1162009-05-13 22:13:09 +0100282 if (argc > 4) {
283 useTokenIndex = sqlite3_value_int(argv[4]);
284 }
285
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100286 if (argc > 5) {
287 useDataTag = (sqlite3_value_type(argv[5]) != SQLITE_NULL);
288 }
289
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800290 sqlite3 * handle = sqlite3_context_db_handle(context);
291 UCollator* collator = (UCollator*)sqlite3_user_data(context);
292 char const * tokenTable = (char const *)sqlite3_value_text(argv[0]);
293 if (tokenTable == NULL) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000294 ALOGE("tokenTable null");
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800295 sqlite3_result_null(context);
296 return;
297 }
298
299 // Get or create the prepared statement for the insertions
300 sqlite3_stmt * statement = (sqlite3_stmt *)sqlite3_get_auxdata(context, 0);
301 if (!statement) {
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100302 char const * tokenIndexCol = useTokenIndex ? ", token_index" : "";
303 char const * tokenIndexParam = useTokenIndex ? ", ?" : "";
304 char const * dataTagCol = useDataTag ? ", tag" : "";
305 char const * dataTagParam = useDataTag ? ", ?" : "";
306 char * sql = sqlite3_mprintf("INSERT INTO %s (token, source%s%s) VALUES (?, ?%s%s);",
307 tokenTable, tokenIndexCol, dataTagCol, tokenIndexParam, dataTagParam);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800308 err = sqlite3_prepare_v2(handle, sql, -1, &statement, NULL);
309 sqlite3_free(sql);
310 if (err) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000311 ALOGE("prepare failed");
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800312 sqlite3_result_null(context);
313 return;
314 }
315 // This binds the statement to the table it was compiled against, which is argv[0].
316 // If this function is ever called with a different table the finalizer will be called
317 // and sqlite3_get_auxdata() will return null above, forcing a recompile for the new table.
318 sqlite3_set_auxdata(context, 0, statement, tokenize_auxdata_delete);
319 } else {
320 // Reset the cached statement so that binding the row ID will work properly
321 sqlite3_reset(statement);
322 }
323
324 // Bind the row ID of the source row
325 int64_t rowID = sqlite3_value_int64(argv[1]);
326 err = sqlite3_bind_int64(statement, 2, rowID);
327 if (err != SQLITE_OK) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000328 ALOGE("bind failed");
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800329 sqlite3_result_null(context);
330 return;
331 }
332
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100333 // Bind <data_tag> to the tag column
334 if (useDataTag) {
335 int dataTagParamIndex = useTokenIndex ? 4 : 3;
336 err = sqlite3_bind_value(statement, dataTagParamIndex, argv[5]);
337 if (err != SQLITE_OK) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000338 ALOGE("bind failed");
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100339 sqlite3_result_null(context);
340 return;
341 }
342 }
343
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800344 // Get the raw bytes for the string to tokenize
345 // the string will be modified by following code
346 // however, sqlite did not reuse the string, so it is safe to not dup it
347 UChar * origData = (UChar *)sqlite3_value_text16(argv[2]);
348 if (origData == NULL) {
349 sqlite3_result_null(context);
350 return;
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800351 }
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800352
353 // Get the raw bytes for the delimiter
354 const UChar * delim = (const UChar *)sqlite3_value_text16(argv[3]);
355 if (delim == NULL) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000356 ALOGE("can't get delimiter");
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800357 sqlite3_result_null(context);
358 return;
359 }
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800360
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800361 UChar * token = NULL;
362 UChar *state;
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800363 int numTokens = 0;
364
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800365 do {
366 if (numTokens == 0) {
367 token = origData;
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800368 }
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800369
370 // Reset the program so we can use it to perform the insert
371 sqlite3_reset(statement);
372 UErrorCode status = U_ZERO_ERROR;
373 char keybuf[1024];
374 uint32_t result = ucol_getSortKey(collator, token, -1, (uint8_t*)keybuf, sizeof(keybuf)-1);
375 if (result > sizeof(keybuf)) {
376 // TODO allocate memory for this super big string
Steve Blockcbfefda2012-01-06 19:14:58 +0000377 ALOGE("ucol_getSortKey needs bigger buffer %d", result);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800378 break;
379 }
380 uint32_t keysize = result-1;
381 uint32_t base16Size = keysize*2;
382 char *base16buf = (char*)malloc(base16Size);
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800383 base16Encode(base16buf, keybuf, keysize);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800384 err = sqlite3_bind_text(statement, 1, base16buf, base16Size, SQLITE_STATIC);
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800385
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800386 if (err != SQLITE_OK) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000387 ALOGE(" sqlite3_bind_text16 error %d", err);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800388 free(base16buf);
389 break;
390 }
391
Bjorn Bringert687f1162009-05-13 22:13:09 +0100392 if (useTokenIndex) {
393 err = sqlite3_bind_int(statement, 3, numTokens);
394 if (err != SQLITE_OK) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000395 ALOGE(" sqlite3_bind_int error %d", err);
Bjorn Bringert687f1162009-05-13 22:13:09 +0100396 free(base16buf);
397 break;
398 }
399 }
400
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800401 err = sqlite3_step(statement);
402 free(base16buf);
403
404 if (err != SQLITE_DONE) {
Steve Blockcbfefda2012-01-06 19:14:58 +0000405 ALOGE(" sqlite3_step error %d", err);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800406 break;
407 }
408 numTokens++;
409 if (numTokens == 1) {
410 // first call
411 u_strtok_r(origData, delim, &state);
412 }
413 } while ((token = u_strtok_r(NULL, delim, &state)) != NULL);
414 sqlite3_result_int(context, numTokens);
415}
416
417static void localized_collator_dtor(UCollator* collator)
418{
419 ucol_close(collator);
420}
421
422#define LOCALIZED_COLLATOR_NAME "LOCALIZED"
423
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900424// This collator may be removed in the near future, so you MUST not use now.
425#define PHONEBOOK_COLLATOR_NAME "PHONEBOOK"
426
Jiyong Parkb4076df2017-08-09 20:15:42 +0900427#endif // SQLITE_ENABLE_ICU
428
429extern "C" int register_localized_collators(sqlite3* handle __attribute((unused)),
430 const char* systemLocale __attribute((unused)),
431 int utf16Storage __attribute((unused)))
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800432{
Jiyong Parkb4076df2017-08-09 20:15:42 +0900433// This function is no-op for the VNDK, but should exist in case when some vendor
434// module has a reference to this function.
435#ifdef SQLITE_ENABLE_ICU
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800436 UErrorCode status = U_ZERO_ERROR;
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800437 UCollator* collator = ucol_open(systemLocale, &status);
438 if (U_FAILURE(status)) {
439 return -1;
440 }
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800441
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800442 ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_PRIMARY, &status);
443 if (U_FAILURE(status)) {
444 return -1;
445 }
446
Elliott Hughes153c1022016-09-13 17:13:29 -0700447 int err;
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800448 if (utf16Storage) {
449 err = sqlite3_create_collation_v2(handle, LOCALIZED_COLLATOR_NAME, SQLITE_UTF16, collator,
450 collate16, (void(*)(void*))localized_collator_dtor);
451 } else {
452 err = sqlite3_create_collation_v2(handle, LOCALIZED_COLLATOR_NAME, SQLITE_UTF8, collator,
453 collate8, (void(*)(void*))localized_collator_dtor);
454 }
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900455
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800456 if (err != SQLITE_OK) {
457 return err;
458 }
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800459
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800460 // Register the _TOKENIZE function
461 err = sqlite3_create_function(handle, "_TOKENIZE", 4, SQLITE_UTF16, collator, tokenize, NULL, NULL);
462 if (err != SQLITE_OK) {
463 return err;
Bjorn Bringert687f1162009-05-13 22:13:09 +0100464 }
465 err = sqlite3_create_function(handle, "_TOKENIZE", 5, SQLITE_UTF16, collator, tokenize, NULL, NULL);
466 if (err != SQLITE_OK) {
467 return err;
468 }
Bjorn Bringert754d83d2009-05-18 11:21:50 +0100469 err = sqlite3_create_function(handle, "_TOKENIZE", 6, SQLITE_UTF16, collator, tokenize, NULL, NULL);
470 if (err != SQLITE_OK) {
471 return err;
472 }
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800473
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900474 //// PHONEBOOK_COLLATOR
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900475 status = U_ZERO_ERROR;
Jay Shraunerdb8a3862012-12-17 11:12:30 -0800476 collator = ucol_open(systemLocale, &status);
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900477 if (U_FAILURE(status)) {
478 return -1;
479 }
480
481 status = U_ZERO_ERROR;
482 ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_PRIMARY, &status);
483 if (U_FAILURE(status)) {
484 return -1;
485 }
486
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900487 if (utf16Storage) {
488 err = sqlite3_create_collation_v2(handle, PHONEBOOK_COLLATOR_NAME, SQLITE_UTF16, collator,
489 collate16, (void(*)(void*))localized_collator_dtor);
490 } else {
491 err = sqlite3_create_collation_v2(handle, PHONEBOOK_COLLATOR_NAME, SQLITE_UTF8, collator,
492 collate8, (void(*)(void*))localized_collator_dtor);
493 }
494
495 if (err != SQLITE_OK) {
496 return err;
497 }
498 //// PHONEBOOK_COLLATOR
Jiyong Parkb4076df2017-08-09 20:15:42 +0900499#endif //SQLITE_ENABLE_ICU
Daisuke Miyakawa31089e02010-02-24 19:03:33 +0900500
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800501 return SQLITE_OK;
502}
503
504
Jiyong Parkb4076df2017-08-09 20:15:42 +0900505extern "C" int register_android_functions(sqlite3 * handle, int utf16Storage __attribute((unused)))
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800506{
507 int err;
Jiyong Parkb4076df2017-08-09 20:15:42 +0900508#ifdef SQLITE_ENABLE_ICU
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800509 UErrorCode status = U_ZERO_ERROR;
510
511 UCollator * collator = ucol_open(NULL, &status);
512 if (U_FAILURE(status)) {
513 return -1;
Dmitri Plotnikov3a749622010-03-03 11:29:46 -0800514 }
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800515
516 if (utf16Storage) {
517 // Note that text should be stored as UTF-16
518 err = sqlite3_exec(handle, "PRAGMA encoding = 'UTF-16'", 0, 0, 0);
519 if (err != SQLITE_OK) {
520 return err;
521 }
522
523 // Register the UNICODE collation
524 err = sqlite3_create_collation_v2(handle, "UNICODE", SQLITE_UTF16, collator, collate16,
525 (void(*)(void*))localized_collator_dtor);
526 } else {
527 err = sqlite3_create_collation_v2(handle, "UNICODE", SQLITE_UTF8, collator, collate8,
528 (void(*)(void*))localized_collator_dtor);
529 }
530
531 if (err != SQLITE_OK) {
532 return err;
533 }
Jiyong Parkb4076df2017-08-09 20:15:42 +0900534#endif // SQLITE_ENABLE_ICU
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800535
536 // Register the PHONE_NUM_EQUALS function
Daisuke Miyakawa948a1192009-09-19 19:19:53 -0700537 err = sqlite3_create_function(
538 handle, "PHONE_NUMBERS_EQUAL", 2,
539 SQLITE_UTF8, NULL, phone_numbers_equal, NULL, NULL);
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800540 if (err != SQLITE_OK) {
541 return err;
542 }
Daisuke Miyakawa948a1192009-09-19 19:19:53 -0700543
544 // Register the PHONE_NUM_EQUALS function with an additional argument "use_strict"
545 err = sqlite3_create_function(
546 handle, "PHONE_NUMBERS_EQUAL", 3,
547 SQLITE_UTF8, NULL, phone_numbers_equal, NULL, NULL);
548 if (err != SQLITE_OK) {
549 return err;
550 }
551
Taesu Lee33fdf2e2019-05-29 14:09:11 +0900552 // Register the PHONE_NUM_EQUALS function with additional arguments "use_strict" and "min_match"
553 err = sqlite3_create_function(
554 handle, "PHONE_NUMBERS_EQUAL", 4,
555 SQLITE_UTF8, NULL, phone_numbers_equal, NULL, NULL);
556 if (err != SQLITE_OK) {
557 return err;
558 }
559
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800560 // Register the _DELETE_FILE function
561 err = sqlite3_create_function(handle, "_DELETE_FILE", 1, SQLITE_UTF8, NULL, delete_file, NULL, NULL);
562 if (err != SQLITE_OK) {
563 return err;
564 }
565
566#if ENABLE_ANDROID_LOG
567 // Register the _LOG function
568 err = sqlite3_create_function(handle, "_LOG", 1, SQLITE_UTF8, NULL, android_log, NULL, NULL);
569 if (err != SQLITE_OK) {
570 return err;
571 }
572#endif
573
Dmitri Plotnikov46bdb5c2011-01-06 13:48:50 -0800574 // Register the _PHONE_NUMBER_STRIPPED_REVERSED function, which imitates
Elliott Hughes153c1022016-09-13 17:13:29 -0700575 // PhoneNumberUtils.getStrippedReversed. This function is used by
576 // packages/providers/ContactsProvider/src/com/android/providers/contacts/LegacyApiSupport.java
577 // to provide compatibility with Android 1.6 and earlier.
Dmitri Plotnikov46bdb5c2011-01-06 13:48:50 -0800578 err = sqlite3_create_function(handle,
579 "_PHONE_NUMBER_STRIPPED_REVERSED",
580 1, SQLITE_UTF8, NULL,
581 phone_number_stripped_reversed,
582 NULL, NULL);
583 if (err != SQLITE_OK) {
584 return err;
585 }
586
The Android Open Source Project7790ef52009-03-03 19:30:40 -0800587 return SQLITE_OK;
588}