The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 1 | /* |
| 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 Park | b4076df | 2017-08-09 20:15:42 +0900 | [diff] [blame^] | 24 | // ICU is turned off when sqlite is built for VNDK |
| 25 | #ifndef __ANDROID_VNDK__ |
| 26 | #define SQLITE_ENABLE_ICU |
| 27 | #else |
| 28 | #undef SQLITE_ENABLE_ICU |
| 29 | #endif |
| 30 | |
| 31 | #ifdef SQLITE_ENABLE_ICU |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 32 | #include <unicode/ucol.h> |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame] | 33 | #include <unicode/uiter.h> |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 34 | #include <unicode/ustring.h> |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame] | 35 | #include <unicode/utypes.h> |
Jiyong Park | b4076df | 2017-08-09 20:15:42 +0900 | [diff] [blame^] | 36 | #endif //SQLITE_ENABLE_ICU |
Colin Cross | ea17061 | 2017-04-19 17:36:28 -0700 | [diff] [blame] | 37 | #include <log/log.h> |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 38 | |
| 39 | #include "sqlite3_android.h" |
| 40 | #include "PhoneNumberUtils.h" |
| 41 | |
| 42 | #define ENABLE_ANDROID_LOG 0 |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame] | 43 | #define SMALL_BUFFER_SIZE 10 |
Dmitri Plotnikov | 46bdb5c | 2011-01-06 13:48:50 -0800 | [diff] [blame] | 44 | #define PHONE_NUMBER_BUFFER_SIZE 40 |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 45 | |
Jiyong Park | b4076df | 2017-08-09 20:15:42 +0900 | [diff] [blame^] | 46 | #ifdef SQLITE_ENABLE_ICU |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 47 | static int collate16(void *p, int n1, const void *v1, int n2, const void *v2) |
| 48 | { |
| 49 | UCollator *coll = (UCollator *) p; |
| 50 | UCollationResult result = ucol_strcoll(coll, (const UChar *) v1, n1, |
| 51 | (const UChar *) v2, n2); |
| 52 | |
| 53 | if (result == UCOL_LESS) { |
| 54 | return -1; |
| 55 | } else if (result == UCOL_GREATER) { |
| 56 | return 1; |
| 57 | } else { |
| 58 | return 0; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | static int collate8(void *p, int n1, const void *v1, int n2, const void *v2) |
| 63 | { |
| 64 | UCollator *coll = (UCollator *) p; |
| 65 | UCharIterator i1, i2; |
| 66 | UErrorCode status = U_ZERO_ERROR; |
| 67 | |
| 68 | uiter_setUTF8(&i1, (const char *) v1, n1); |
| 69 | uiter_setUTF8(&i2, (const char *) v2, n2); |
| 70 | |
| 71 | UCollationResult result = ucol_strcollIter(coll, &i1, &i2, &status); |
| 72 | |
| 73 | if (U_FAILURE(status)) { |
Steve Block | cbfefda | 2012-01-06 19:14:58 +0000 | [diff] [blame] | 74 | // ALOGE("Collation iterator error: %d\n", status); |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | if (result == UCOL_LESS) { |
| 78 | return -1; |
| 79 | } else if (result == UCOL_GREATER) { |
| 80 | return 1; |
| 81 | } else { |
| 82 | return 0; |
| 83 | } |
| 84 | } |
Jiyong Park | b4076df | 2017-08-09 20:15:42 +0900 | [diff] [blame^] | 85 | #endif // SQLITE_ENABLE_ICU |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 86 | |
| 87 | static void phone_numbers_equal(sqlite3_context * context, int argc, sqlite3_value ** argv) |
| 88 | { |
Daisuke Miyakawa | 948a119 | 2009-09-19 19:19:53 -0700 | [diff] [blame] | 89 | if (argc != 2 && argc != 3) { |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 90 | sqlite3_result_int(context, 0); |
| 91 | return; |
| 92 | } |
| 93 | |
| 94 | char const * num1 = (char const *)sqlite3_value_text(argv[0]); |
| 95 | char const * num2 = (char const *)sqlite3_value_text(argv[1]); |
| 96 | |
Daisuke Miyakawa | 948a119 | 2009-09-19 19:19:53 -0700 | [diff] [blame] | 97 | bool use_strict = false; |
| 98 | if (argc == 3) { |
| 99 | use_strict = (sqlite3_value_int(argv[2]) != 0); |
| 100 | } |
| 101 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 102 | if (num1 == NULL || num2 == NULL) { |
| 103 | sqlite3_result_null(context); |
| 104 | return; |
| 105 | } |
| 106 | |
Daisuke Miyakawa | 948a119 | 2009-09-19 19:19:53 -0700 | [diff] [blame] | 107 | bool equal = |
| 108 | (use_strict ? |
| 109 | android::phone_number_compare_strict(num1, num2) : |
| 110 | android::phone_number_compare_loose(num1, num2)); |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 111 | |
| 112 | if (equal) { |
| 113 | sqlite3_result_int(context, 1); |
| 114 | } else { |
| 115 | sqlite3_result_int(context, 0); |
| 116 | } |
| 117 | } |
| 118 | |
Dmitri Plotnikov | 46bdb5c | 2011-01-06 13:48:50 -0800 | [diff] [blame] | 119 | static void phone_number_stripped_reversed(sqlite3_context * context, int argc, |
| 120 | sqlite3_value ** argv) |
| 121 | { |
| 122 | if (argc != 1) { |
| 123 | sqlite3_result_int(context, 0); |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | char const * number = (char const *)sqlite3_value_text(argv[0]); |
| 128 | if (number == NULL) { |
| 129 | sqlite3_result_null(context); |
| 130 | return; |
| 131 | } |
| 132 | |
| 133 | char out[PHONE_NUMBER_BUFFER_SIZE]; |
| 134 | int outlen = 0; |
| 135 | android::phone_number_stripped_reversed_inter(number, out, PHONE_NUMBER_BUFFER_SIZE, &outlen); |
| 136 | sqlite3_result_text(context, (const char*)out, outlen, SQLITE_TRANSIENT); |
| 137 | } |
| 138 | |
| 139 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 140 | #if ENABLE_ANDROID_LOG |
| 141 | static void android_log(sqlite3_context * context, int argc, sqlite3_value ** argv) |
| 142 | { |
| 143 | char const * tag = "sqlite_trigger"; |
| 144 | char const * msg = ""; |
| 145 | int msgIndex = 0; |
| 146 | |
| 147 | switch (argc) { |
| 148 | case 2: |
| 149 | tag = (char const *)sqlite3_value_text(argv[0]); |
| 150 | if (tag == NULL) { |
| 151 | tag = "sqlite_trigger"; |
| 152 | } |
| 153 | msgIndex = 1; |
| 154 | case 1: |
| 155 | msg = (char const *)sqlite3_value_text(argv[msgIndex]); |
| 156 | if (msg == NULL) { |
| 157 | msg = ""; |
| 158 | } |
Hyejin Kim | 165c687 | 2013-02-28 16:21:20 +0900 | [diff] [blame] | 159 | ALOG(LOG_INFO, tag, "%s", msg); |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 160 | sqlite3_result_int(context, 1); |
| 161 | return; |
| 162 | |
| 163 | default: |
| 164 | sqlite3_result_int(context, 0); |
| 165 | return; |
| 166 | } |
| 167 | } |
| 168 | #endif |
| 169 | |
| 170 | static void delete_file(sqlite3_context * context, int argc, sqlite3_value ** argv) |
| 171 | { |
| 172 | if (argc != 1) { |
| 173 | sqlite3_result_int(context, 0); |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | char const * path = (char const *)sqlite3_value_text(argv[0]); |
Mike Lockwood | 5a34599 | 2011-07-07 12:22:34 -0400 | [diff] [blame] | 178 | // Don't allow ".." in paths |
| 179 | if (path == NULL || strstr(path, "/../") != NULL) { |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 180 | sqlite3_result_null(context); |
| 181 | return; |
| 182 | } |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame] | 183 | |
Mike Lockwood | 5a34599 | 2011-07-07 12:22:34 -0400 | [diff] [blame] | 184 | // We only allow deleting files in the EXTERNAL_STORAGE path, or one of the |
| 185 | // SECONDARY_STORAGE paths |
| 186 | bool good_path = false; |
| 187 | char const * external_storage = getenv("EXTERNAL_STORAGE"); |
| 188 | if (external_storage && strncmp(external_storage, path, strlen(external_storage)) == 0) { |
| 189 | good_path = true; |
| 190 | } else { |
| 191 | // check SECONDARY_STORAGE, which should be a colon separated list of paths |
| 192 | char const * secondary_paths = getenv("SECONDARY_STORAGE"); |
| 193 | while (secondary_paths && secondary_paths[0]) { |
| 194 | const char* colon = strchr(secondary_paths, ':'); |
| 195 | int length = (colon ? colon - secondary_paths : strlen(secondary_paths)); |
| 196 | if (strncmp(secondary_paths, path, length) == 0) { |
| 197 | good_path = true; |
| 198 | } |
| 199 | secondary_paths += length; |
| 200 | while (*secondary_paths == ':') secondary_paths++; |
| 201 | } |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 202 | } |
Mike Lockwood | 5a34599 | 2011-07-07 12:22:34 -0400 | [diff] [blame] | 203 | |
| 204 | if (!good_path) { |
Marco Nelissen | 2da78c0 | 2009-05-06 11:08:08 -0700 | [diff] [blame] | 205 | sqlite3_result_null(context); |
| 206 | return; |
| 207 | } |
| 208 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 209 | int err = unlink(path); |
| 210 | if (err != -1) { |
| 211 | // No error occured, return true |
| 212 | sqlite3_result_int(context, 1); |
| 213 | } else { |
| 214 | // An error occured, return false |
| 215 | sqlite3_result_int(context, 0); |
| 216 | } |
| 217 | } |
| 218 | |
Jiyong Park | b4076df | 2017-08-09 20:15:42 +0900 | [diff] [blame^] | 219 | #ifdef SQLITE_ENABLE_ICU |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 220 | static void tokenize_auxdata_delete(void * data) |
| 221 | { |
| 222 | sqlite3_stmt * statement = (sqlite3_stmt *)data; |
| 223 | sqlite3_finalize(statement); |
| 224 | } |
| 225 | |
| 226 | static void base16Encode(char* dest, const char* src, uint32_t size) |
| 227 | { |
| 228 | static const char * BASE16_TABLE = "0123456789abcdef"; |
| 229 | for (uint32_t i = 0; i < size; i++) { |
| 230 | char ch = *src++; |
| 231 | *dest++ = BASE16_TABLE[ (ch & 0xf0) >> 4 ]; |
| 232 | *dest++ = BASE16_TABLE[ (ch & 0x0f) ]; |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | struct SqliteUserData { |
| 237 | sqlite3 * handle; |
| 238 | UCollator* collator; |
| 239 | }; |
| 240 | |
| 241 | /** |
| 242 | * This function is invoked as: |
| 243 | * |
Bjorn Bringert | 754d83d | 2009-05-18 11:21:50 +0100 | [diff] [blame] | 244 | * _TOKENIZE('<token_table>', <data_row_id>, <data>, <delimiter>, |
| 245 | * <use_token_index>, <data_tag>) |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 246 | * |
Bjorn Bringert | 754d83d | 2009-05-18 11:21:50 +0100 | [diff] [blame] | 247 | * If <use_token_index> is omitted, it is treated as 0. |
| 248 | * If <data_tag> is omitted, it is treated as NULL. |
Bjorn Bringert | 687f116 | 2009-05-13 22:13:09 +0100 | [diff] [blame] | 249 | * |
| 250 | * It will split <data> on each instance of <delimiter> and insert each token |
Bjorn Bringert | 754d83d | 2009-05-18 11:21:50 +0100 | [diff] [blame] | 251 | * into <token_table>. The following columns in <token_table> are used: |
| 252 | * token TEXT, source INTEGER, token_index INTEGER, tag (any type) |
| 253 | * The token_index column is not required if <use_token_index> is 0. |
| 254 | * The tag column is not required if <data_tag> is NULL. |
Bjorn Bringert | 687f116 | 2009-05-13 22:13:09 +0100 | [diff] [blame] | 255 | * |
| 256 | * One row is inserted for each token in <data>. |
| 257 | * In each inserted row, 'source' is <data_row_id>. |
| 258 | * In the first inserted row, 'token' is the hex collation key of |
| 259 | * the entire <data> string, and 'token_index' is 0. |
| 260 | * In each row I (where 1 <= I < N, and N is the number of tokens in <data>) |
| 261 | * 'token' will be set to the hex collation key of the I:th token (0-based). |
Bjorn Bringert | 754d83d | 2009-05-18 11:21:50 +0100 | [diff] [blame] | 262 | * If <use_token_index> != 0, 'token_index' is set to I. |
| 263 | * If <data_tag> is not NULL, 'tag' is set to <data_tag>. |
Bjorn Bringert | 687f116 | 2009-05-13 22:13:09 +0100 | [diff] [blame] | 264 | * |
| 265 | * In other words, there will be one row for the entire string, |
| 266 | * and one row for each token except the first one. |
| 267 | * |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 268 | * The function returns the number of tokens generated. |
| 269 | */ |
| 270 | static void tokenize(sqlite3_context * context, int argc, sqlite3_value ** argv) |
| 271 | { |
Steve Block | bcf3ccc | 2011-12-20 16:21:48 +0000 | [diff] [blame] | 272 | //ALOGD("enter tokenize"); |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 273 | int err; |
Bjorn Bringert | 687f116 | 2009-05-13 22:13:09 +0100 | [diff] [blame] | 274 | int useTokenIndex = 0; |
Bjorn Bringert | 754d83d | 2009-05-18 11:21:50 +0100 | [diff] [blame] | 275 | int useDataTag = 0; |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 276 | |
Bjorn Bringert | 754d83d | 2009-05-18 11:21:50 +0100 | [diff] [blame] | 277 | if (!(argc >= 4 || argc <= 6)) { |
Steve Block | cbfefda | 2012-01-06 19:14:58 +0000 | [diff] [blame] | 278 | ALOGE("Tokenize requires 4 to 6 arguments"); |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 279 | sqlite3_result_null(context); |
| 280 | return; |
| 281 | } |
| 282 | |
Bjorn Bringert | 687f116 | 2009-05-13 22:13:09 +0100 | [diff] [blame] | 283 | if (argc > 4) { |
| 284 | useTokenIndex = sqlite3_value_int(argv[4]); |
| 285 | } |
| 286 | |
Bjorn Bringert | 754d83d | 2009-05-18 11:21:50 +0100 | [diff] [blame] | 287 | if (argc > 5) { |
| 288 | useDataTag = (sqlite3_value_type(argv[5]) != SQLITE_NULL); |
| 289 | } |
| 290 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 291 | sqlite3 * handle = sqlite3_context_db_handle(context); |
| 292 | UCollator* collator = (UCollator*)sqlite3_user_data(context); |
| 293 | char const * tokenTable = (char const *)sqlite3_value_text(argv[0]); |
| 294 | if (tokenTable == NULL) { |
Steve Block | cbfefda | 2012-01-06 19:14:58 +0000 | [diff] [blame] | 295 | ALOGE("tokenTable null"); |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 296 | sqlite3_result_null(context); |
| 297 | return; |
| 298 | } |
| 299 | |
| 300 | // Get or create the prepared statement for the insertions |
| 301 | sqlite3_stmt * statement = (sqlite3_stmt *)sqlite3_get_auxdata(context, 0); |
| 302 | if (!statement) { |
Bjorn Bringert | 754d83d | 2009-05-18 11:21:50 +0100 | [diff] [blame] | 303 | char const * tokenIndexCol = useTokenIndex ? ", token_index" : ""; |
| 304 | char const * tokenIndexParam = useTokenIndex ? ", ?" : ""; |
| 305 | char const * dataTagCol = useDataTag ? ", tag" : ""; |
| 306 | char const * dataTagParam = useDataTag ? ", ?" : ""; |
| 307 | char * sql = sqlite3_mprintf("INSERT INTO %s (token, source%s%s) VALUES (?, ?%s%s);", |
| 308 | tokenTable, tokenIndexCol, dataTagCol, tokenIndexParam, dataTagParam); |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 309 | err = sqlite3_prepare_v2(handle, sql, -1, &statement, NULL); |
| 310 | sqlite3_free(sql); |
| 311 | if (err) { |
Steve Block | cbfefda | 2012-01-06 19:14:58 +0000 | [diff] [blame] | 312 | ALOGE("prepare failed"); |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 313 | sqlite3_result_null(context); |
| 314 | return; |
| 315 | } |
| 316 | // This binds the statement to the table it was compiled against, which is argv[0]. |
| 317 | // If this function is ever called with a different table the finalizer will be called |
| 318 | // and sqlite3_get_auxdata() will return null above, forcing a recompile for the new table. |
| 319 | sqlite3_set_auxdata(context, 0, statement, tokenize_auxdata_delete); |
| 320 | } else { |
| 321 | // Reset the cached statement so that binding the row ID will work properly |
| 322 | sqlite3_reset(statement); |
| 323 | } |
| 324 | |
| 325 | // Bind the row ID of the source row |
| 326 | int64_t rowID = sqlite3_value_int64(argv[1]); |
| 327 | err = sqlite3_bind_int64(statement, 2, rowID); |
| 328 | if (err != SQLITE_OK) { |
Steve Block | cbfefda | 2012-01-06 19:14:58 +0000 | [diff] [blame] | 329 | ALOGE("bind failed"); |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 330 | sqlite3_result_null(context); |
| 331 | return; |
| 332 | } |
| 333 | |
Bjorn Bringert | 754d83d | 2009-05-18 11:21:50 +0100 | [diff] [blame] | 334 | // Bind <data_tag> to the tag column |
| 335 | if (useDataTag) { |
| 336 | int dataTagParamIndex = useTokenIndex ? 4 : 3; |
| 337 | err = sqlite3_bind_value(statement, dataTagParamIndex, argv[5]); |
| 338 | if (err != SQLITE_OK) { |
Steve Block | cbfefda | 2012-01-06 19:14:58 +0000 | [diff] [blame] | 339 | ALOGE("bind failed"); |
Bjorn Bringert | 754d83d | 2009-05-18 11:21:50 +0100 | [diff] [blame] | 340 | sqlite3_result_null(context); |
| 341 | return; |
| 342 | } |
| 343 | } |
| 344 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 345 | // Get the raw bytes for the string to tokenize |
| 346 | // the string will be modified by following code |
| 347 | // however, sqlite did not reuse the string, so it is safe to not dup it |
| 348 | UChar * origData = (UChar *)sqlite3_value_text16(argv[2]); |
| 349 | if (origData == NULL) { |
| 350 | sqlite3_result_null(context); |
| 351 | return; |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame] | 352 | } |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 353 | |
| 354 | // Get the raw bytes for the delimiter |
| 355 | const UChar * delim = (const UChar *)sqlite3_value_text16(argv[3]); |
| 356 | if (delim == NULL) { |
Steve Block | cbfefda | 2012-01-06 19:14:58 +0000 | [diff] [blame] | 357 | ALOGE("can't get delimiter"); |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 358 | sqlite3_result_null(context); |
| 359 | return; |
| 360 | } |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame] | 361 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 362 | UChar * token = NULL; |
| 363 | UChar *state; |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame] | 364 | int numTokens = 0; |
| 365 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 366 | do { |
| 367 | if (numTokens == 0) { |
| 368 | token = origData; |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame] | 369 | } |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 370 | |
| 371 | // Reset the program so we can use it to perform the insert |
| 372 | sqlite3_reset(statement); |
| 373 | UErrorCode status = U_ZERO_ERROR; |
| 374 | char keybuf[1024]; |
| 375 | uint32_t result = ucol_getSortKey(collator, token, -1, (uint8_t*)keybuf, sizeof(keybuf)-1); |
| 376 | if (result > sizeof(keybuf)) { |
| 377 | // TODO allocate memory for this super big string |
Steve Block | cbfefda | 2012-01-06 19:14:58 +0000 | [diff] [blame] | 378 | ALOGE("ucol_getSortKey needs bigger buffer %d", result); |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 379 | break; |
| 380 | } |
| 381 | uint32_t keysize = result-1; |
| 382 | uint32_t base16Size = keysize*2; |
| 383 | char *base16buf = (char*)malloc(base16Size); |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame] | 384 | base16Encode(base16buf, keybuf, keysize); |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 385 | err = sqlite3_bind_text(statement, 1, base16buf, base16Size, SQLITE_STATIC); |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame] | 386 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 387 | if (err != SQLITE_OK) { |
Steve Block | cbfefda | 2012-01-06 19:14:58 +0000 | [diff] [blame] | 388 | ALOGE(" sqlite3_bind_text16 error %d", err); |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 389 | free(base16buf); |
| 390 | break; |
| 391 | } |
| 392 | |
Bjorn Bringert | 687f116 | 2009-05-13 22:13:09 +0100 | [diff] [blame] | 393 | if (useTokenIndex) { |
| 394 | err = sqlite3_bind_int(statement, 3, numTokens); |
| 395 | if (err != SQLITE_OK) { |
Steve Block | cbfefda | 2012-01-06 19:14:58 +0000 | [diff] [blame] | 396 | ALOGE(" sqlite3_bind_int error %d", err); |
Bjorn Bringert | 687f116 | 2009-05-13 22:13:09 +0100 | [diff] [blame] | 397 | free(base16buf); |
| 398 | break; |
| 399 | } |
| 400 | } |
| 401 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 402 | err = sqlite3_step(statement); |
| 403 | free(base16buf); |
| 404 | |
| 405 | if (err != SQLITE_DONE) { |
Steve Block | cbfefda | 2012-01-06 19:14:58 +0000 | [diff] [blame] | 406 | ALOGE(" sqlite3_step error %d", err); |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 407 | break; |
| 408 | } |
| 409 | numTokens++; |
| 410 | if (numTokens == 1) { |
| 411 | // first call |
| 412 | u_strtok_r(origData, delim, &state); |
| 413 | } |
| 414 | } while ((token = u_strtok_r(NULL, delim, &state)) != NULL); |
| 415 | sqlite3_result_int(context, numTokens); |
| 416 | } |
| 417 | |
| 418 | static void localized_collator_dtor(UCollator* collator) |
| 419 | { |
| 420 | ucol_close(collator); |
| 421 | } |
| 422 | |
| 423 | #define LOCALIZED_COLLATOR_NAME "LOCALIZED" |
| 424 | |
Daisuke Miyakawa | 31089e0 | 2010-02-24 19:03:33 +0900 | [diff] [blame] | 425 | // This collator may be removed in the near future, so you MUST not use now. |
| 426 | #define PHONEBOOK_COLLATOR_NAME "PHONEBOOK" |
| 427 | |
Jiyong Park | b4076df | 2017-08-09 20:15:42 +0900 | [diff] [blame^] | 428 | #endif // SQLITE_ENABLE_ICU |
| 429 | |
| 430 | extern "C" int register_localized_collators(sqlite3* handle __attribute((unused)), |
| 431 | const char* systemLocale __attribute((unused)), |
| 432 | int utf16Storage __attribute((unused))) |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 433 | { |
Jiyong Park | b4076df | 2017-08-09 20:15:42 +0900 | [diff] [blame^] | 434 | // This function is no-op for the VNDK, but should exist in case when some vendor |
| 435 | // module has a reference to this function. |
| 436 | #ifdef SQLITE_ENABLE_ICU |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 437 | UErrorCode status = U_ZERO_ERROR; |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 438 | UCollator* collator = ucol_open(systemLocale, &status); |
| 439 | if (U_FAILURE(status)) { |
| 440 | return -1; |
| 441 | } |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame] | 442 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 443 | ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_PRIMARY, &status); |
| 444 | if (U_FAILURE(status)) { |
| 445 | return -1; |
| 446 | } |
| 447 | |
Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 448 | int err; |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 449 | if (utf16Storage) { |
| 450 | err = sqlite3_create_collation_v2(handle, LOCALIZED_COLLATOR_NAME, SQLITE_UTF16, collator, |
| 451 | collate16, (void(*)(void*))localized_collator_dtor); |
| 452 | } else { |
| 453 | err = sqlite3_create_collation_v2(handle, LOCALIZED_COLLATOR_NAME, SQLITE_UTF8, collator, |
| 454 | collate8, (void(*)(void*))localized_collator_dtor); |
| 455 | } |
Daisuke Miyakawa | 31089e0 | 2010-02-24 19:03:33 +0900 | [diff] [blame] | 456 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 457 | if (err != SQLITE_OK) { |
| 458 | return err; |
| 459 | } |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame] | 460 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 461 | // Register the _TOKENIZE function |
| 462 | err = sqlite3_create_function(handle, "_TOKENIZE", 4, SQLITE_UTF16, collator, tokenize, NULL, NULL); |
| 463 | if (err != SQLITE_OK) { |
| 464 | return err; |
Bjorn Bringert | 687f116 | 2009-05-13 22:13:09 +0100 | [diff] [blame] | 465 | } |
| 466 | err = sqlite3_create_function(handle, "_TOKENIZE", 5, SQLITE_UTF16, collator, tokenize, NULL, NULL); |
| 467 | if (err != SQLITE_OK) { |
| 468 | return err; |
| 469 | } |
Bjorn Bringert | 754d83d | 2009-05-18 11:21:50 +0100 | [diff] [blame] | 470 | err = sqlite3_create_function(handle, "_TOKENIZE", 6, SQLITE_UTF16, collator, tokenize, NULL, NULL); |
| 471 | if (err != SQLITE_OK) { |
| 472 | return err; |
| 473 | } |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 474 | |
Daisuke Miyakawa | 31089e0 | 2010-02-24 19:03:33 +0900 | [diff] [blame] | 475 | //// PHONEBOOK_COLLATOR |
Daisuke Miyakawa | 31089e0 | 2010-02-24 19:03:33 +0900 | [diff] [blame] | 476 | status = U_ZERO_ERROR; |
Jay Shrauner | db8a386 | 2012-12-17 11:12:30 -0800 | [diff] [blame] | 477 | collator = ucol_open(systemLocale, &status); |
Daisuke Miyakawa | 31089e0 | 2010-02-24 19:03:33 +0900 | [diff] [blame] | 478 | if (U_FAILURE(status)) { |
| 479 | return -1; |
| 480 | } |
| 481 | |
| 482 | status = U_ZERO_ERROR; |
| 483 | ucol_setAttribute(collator, UCOL_STRENGTH, UCOL_PRIMARY, &status); |
| 484 | if (U_FAILURE(status)) { |
| 485 | return -1; |
| 486 | } |
| 487 | |
Daisuke Miyakawa | 31089e0 | 2010-02-24 19:03:33 +0900 | [diff] [blame] | 488 | if (utf16Storage) { |
| 489 | err = sqlite3_create_collation_v2(handle, PHONEBOOK_COLLATOR_NAME, SQLITE_UTF16, collator, |
| 490 | collate16, (void(*)(void*))localized_collator_dtor); |
| 491 | } else { |
| 492 | err = sqlite3_create_collation_v2(handle, PHONEBOOK_COLLATOR_NAME, SQLITE_UTF8, collator, |
| 493 | collate8, (void(*)(void*))localized_collator_dtor); |
| 494 | } |
| 495 | |
| 496 | if (err != SQLITE_OK) { |
| 497 | return err; |
| 498 | } |
| 499 | //// PHONEBOOK_COLLATOR |
Jiyong Park | b4076df | 2017-08-09 20:15:42 +0900 | [diff] [blame^] | 500 | #endif //SQLITE_ENABLE_ICU |
Daisuke Miyakawa | 31089e0 | 2010-02-24 19:03:33 +0900 | [diff] [blame] | 501 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 502 | return SQLITE_OK; |
| 503 | } |
| 504 | |
| 505 | |
Jiyong Park | b4076df | 2017-08-09 20:15:42 +0900 | [diff] [blame^] | 506 | extern "C" int register_android_functions(sqlite3 * handle, int utf16Storage __attribute((unused))) |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 507 | { |
| 508 | int err; |
Jiyong Park | b4076df | 2017-08-09 20:15:42 +0900 | [diff] [blame^] | 509 | #ifdef SQLITE_ENABLE_ICU |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 510 | UErrorCode status = U_ZERO_ERROR; |
| 511 | |
| 512 | UCollator * collator = ucol_open(NULL, &status); |
| 513 | if (U_FAILURE(status)) { |
| 514 | return -1; |
Dmitri Plotnikov | 3a74962 | 2010-03-03 11:29:46 -0800 | [diff] [blame] | 515 | } |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 516 | |
| 517 | if (utf16Storage) { |
| 518 | // Note that text should be stored as UTF-16 |
| 519 | err = sqlite3_exec(handle, "PRAGMA encoding = 'UTF-16'", 0, 0, 0); |
| 520 | if (err != SQLITE_OK) { |
| 521 | return err; |
| 522 | } |
| 523 | |
| 524 | // Register the UNICODE collation |
| 525 | err = sqlite3_create_collation_v2(handle, "UNICODE", SQLITE_UTF16, collator, collate16, |
| 526 | (void(*)(void*))localized_collator_dtor); |
| 527 | } else { |
| 528 | err = sqlite3_create_collation_v2(handle, "UNICODE", SQLITE_UTF8, collator, collate8, |
| 529 | (void(*)(void*))localized_collator_dtor); |
| 530 | } |
| 531 | |
| 532 | if (err != SQLITE_OK) { |
| 533 | return err; |
| 534 | } |
Jiyong Park | b4076df | 2017-08-09 20:15:42 +0900 | [diff] [blame^] | 535 | #endif // SQLITE_ENABLE_ICU |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 536 | |
| 537 | // Register the PHONE_NUM_EQUALS function |
Daisuke Miyakawa | 948a119 | 2009-09-19 19:19:53 -0700 | [diff] [blame] | 538 | err = sqlite3_create_function( |
| 539 | handle, "PHONE_NUMBERS_EQUAL", 2, |
| 540 | SQLITE_UTF8, NULL, phone_numbers_equal, NULL, NULL); |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 541 | if (err != SQLITE_OK) { |
| 542 | return err; |
| 543 | } |
Daisuke Miyakawa | 948a119 | 2009-09-19 19:19:53 -0700 | [diff] [blame] | 544 | |
| 545 | // Register the PHONE_NUM_EQUALS function with an additional argument "use_strict" |
| 546 | err = sqlite3_create_function( |
| 547 | handle, "PHONE_NUMBERS_EQUAL", 3, |
| 548 | SQLITE_UTF8, NULL, phone_numbers_equal, NULL, NULL); |
| 549 | if (err != SQLITE_OK) { |
| 550 | return err; |
| 551 | } |
| 552 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 553 | // Register the _DELETE_FILE function |
| 554 | err = sqlite3_create_function(handle, "_DELETE_FILE", 1, SQLITE_UTF8, NULL, delete_file, NULL, NULL); |
| 555 | if (err != SQLITE_OK) { |
| 556 | return err; |
| 557 | } |
| 558 | |
| 559 | #if ENABLE_ANDROID_LOG |
| 560 | // Register the _LOG function |
| 561 | err = sqlite3_create_function(handle, "_LOG", 1, SQLITE_UTF8, NULL, android_log, NULL, NULL); |
| 562 | if (err != SQLITE_OK) { |
| 563 | return err; |
| 564 | } |
| 565 | #endif |
| 566 | |
Dmitri Plotnikov | 46bdb5c | 2011-01-06 13:48:50 -0800 | [diff] [blame] | 567 | // Register the _PHONE_NUMBER_STRIPPED_REVERSED function, which imitates |
Elliott Hughes | 153c102 | 2016-09-13 17:13:29 -0700 | [diff] [blame] | 568 | // PhoneNumberUtils.getStrippedReversed. This function is used by |
| 569 | // packages/providers/ContactsProvider/src/com/android/providers/contacts/LegacyApiSupport.java |
| 570 | // to provide compatibility with Android 1.6 and earlier. |
Dmitri Plotnikov | 46bdb5c | 2011-01-06 13:48:50 -0800 | [diff] [blame] | 571 | err = sqlite3_create_function(handle, |
| 572 | "_PHONE_NUMBER_STRIPPED_REVERSED", |
| 573 | 1, SQLITE_UTF8, NULL, |
| 574 | phone_number_stripped_reversed, |
| 575 | NULL, NULL); |
| 576 | if (err != SQLITE_OK) { |
| 577 | return err; |
| 578 | } |
| 579 | |
The Android Open Source Project | 7790ef5 | 2009-03-03 19:30:40 -0800 | [diff] [blame] | 580 | return SQLITE_OK; |
| 581 | } |