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