@@ -171,6 +171,68 @@ void native_key_str(JNIEnv* env, jobject object, jstring jKey)
171171 env->ReleaseStringUTFChars (jKey, key);
172172}
173173
174+ void native_rekey_char (JNIEnv* env, jobject object, jcharArray jKey)
175+ {
176+ char *keyUtf8 = 0 ;
177+ int lenUtf8 = 0 ;
178+ jchar* keyUtf16 = 0 ;
179+ jsize lenUtf16 = 0 ;
180+ UErrorCode status = U_ZERO_ERROR;
181+ UConverter *encoding = 0 ;
182+
183+ sqlite3 * handle = (sqlite3 *)env->GetIntField (object, offset_db_handle);
184+
185+ keyUtf16 = env->GetCharArrayElements (jKey, 0 );
186+ lenUtf16 = env->GetArrayLength (jKey);
187+
188+ if ( lenUtf16 == 0 ) goto done;
189+
190+ encoding = ucnv_open (" UTF-8" , &status);
191+ if ( U_FAILURE (status) ) {
192+ throw_sqlite3_exception (env, " native_key_char: opening encoding converter failed" );
193+ goto done;
194+ }
195+
196+ lenUtf8 = ucnv_fromUChars (encoding, NULL , 0 , keyUtf16, lenUtf16, &status);
197+ status = (status == U_BUFFER_OVERFLOW_ERROR) ? U_ZERO_ERROR : status;
198+ if ( U_FAILURE (status) ) {
199+ throw_sqlite3_exception (env, " native_key_char: utf8 length unknown" );
200+ goto done;
201+ }
202+
203+ keyUtf8 = (char *) malloc (lenUtf8 * sizeof (char ));
204+ ucnv_fromUChars (encoding, keyUtf8, lenUtf8, keyUtf16, lenUtf16, &status);
205+ if ( U_FAILURE (status) ) {
206+ throw_sqlite3_exception (env, " native_key_char: utf8 conversion failed" );
207+ goto done;
208+ }
209+
210+ if ( sqlite3_rekey (handle, keyUtf8, lenUtf8) != SQLITE_OK ) {
211+ throw_sqlite3_exception (env, handle);
212+ }
213+
214+ done:
215+ env->ReleaseCharArrayElements (jKey, keyUtf16, 0 );
216+ if (encoding != 0 ) ucnv_close (encoding);
217+ if (keyUtf8 != 0 ) free (keyUtf8);
218+ }
219+
220+ void native_rekey_str (JNIEnv* env, jobject object, jstring jKey)
221+ {
222+ sqlite3 * handle = (sqlite3 *)env->GetIntField (object, offset_db_handle);
223+
224+ char const * key = env->GetStringUTFChars (jKey, NULL );
225+ jsize keyLen = env->GetStringUTFLength (jKey);
226+
227+ if ( keyLen > 0 ) {
228+ int status = sqlite3_rekey (handle, key, keyLen);
229+ if ( status != SQLITE_OK ) {
230+ throw_sqlite3_exception (env, handle);
231+ }
232+ }
233+ env->ReleaseStringUTFChars (jKey, key);
234+ }
235+
174236void native_rawExecSQL (JNIEnv* env, jobject object, jstring sql)
175237{
176238 sqlite3 * handle = (sqlite3 *)env->GetIntField (object, offset_db_handle);
@@ -561,6 +623,8 @@ static JNINativeMethod sMethods[] =
561623 {" native_status" , " (IZ)I" , (void *)native_status},
562624 {" native_key" , " ([C)V" , (void *)native_key_char},
563625 {" native_key" , " (Ljava/lang/String;)V" , (void *)native_key_str},
626+ {" native_rekey" , " ([C)V" , (void *)native_rekey_char},
627+ {" native_rekey" , " (Ljava/lang/String;)V" , (void *)native_rekey_str},
564628};
565629
566630int register_android_database_SQLiteDatabase (JNIEnv *env)
0 commit comments