@@ -67,13 +67,19 @@ typedef struct {
67
67
void * provider_ctx ;
68
68
} cipher_ctx ;
69
69
70
+ typedef struct {
71
+ sqlite3_file * file ;
72
+ char * filename ;
73
+ } profile_ctx ;
74
+
70
75
static unsigned int default_flags = DEFAULT_CIPHER_FLAGS ;
71
76
static unsigned char hmac_salt_mask = HMAC_SALT_MASK ;
72
77
static int default_kdf_iter = PBKDF2_ITER ;
73
78
static int default_page_size = SQLITE_DEFAULT_PAGE_SIZE ;
74
79
static unsigned int sqlcipher_activate_count = 0 ;
75
80
static sqlite3_mutex * sqlcipher_provider_mutex = NULL ;
76
81
static sqlcipher_provider * default_provider = NULL ;
82
+ static profile_ctx * profile = NULL ;
77
83
78
84
struct codec_ctx {
79
85
int kdf_salt_sz ;
@@ -1202,27 +1208,54 @@ int sqlcipher_codec_add_random(codec_ctx *ctx, const char *zRight, int random_sz
1202
1208
}
1203
1209
1204
1210
int sqlcipher_cipher_profile (sqlite3 * db , const char * destination ){
1205
- FILE * f ;
1206
- if ( strcmp (destination ,"stdout" )== 0 ){
1207
- f = stdout ;
1208
- }else if ( strcmp (destination , "stderr" )== 0 ){
1209
- f = stderr ;
1210
- }else if ( strcmp (destination , "off" )== 0 ){
1211
- f = 0 ;
1212
- }else {
1213
- f = fopen (destination , "wb" );
1214
- if ( f == 0 ){
1211
+ int rc ;
1212
+ sqlite3_vfs * pVfs ;
1213
+ if (profile == NULL ){
1214
+ profile = sqlcipher_malloc (sizeof (profile_ctx ));
1215
+ }
1216
+ if (strcmp (destination , "off" ) == 0 ){
1217
+ if (profile != NULL && profile -> filename != NULL ){
1218
+ if (strcmp (profile -> filename , "/dev/stdout" ) != 0 &&
1219
+ strcmp (profile -> filename , "/dev/stderr" ) != 0 ) {
1220
+ sqlite3OsCloseFree (profile -> file );
1221
+ }
1222
+ sqlcipher_free (profile , sizeof (profile ));
1223
+ profile = NULL ;
1224
+ }
1225
+ } else {
1226
+ if (strcmp (destination ,"stdout" )== 0 ){
1227
+ profile -> filename = "/dev/stdout" ;
1228
+ } else<
10000
/span> if (strcmp (destination , "stderr" )== 0 ){
1229
+ profile -> filename = "/dev/stderr" ;
1230
+ } else {
1231
+ profile -> filename = (char * )destination ;
1232
+ }
1233
+ pVfs = sqlite3_vfs_find (0 );
1234
+ rc = sqlite3OsOpenMalloc (pVfs , profile -> filename , & (profile -> file ),
1235
+ (SQLITE_OPEN_CREATE |SQLITE_OPEN_READWRITE ), 0 );
1236
+ if (rc ){
1215
1237
return SQLITE_ERROR ;
1216
1238
}
1217
1239
}
1218
- sqlite3_profile (db , sqlcipher_profile_callback , f );
1240
+ sqlite3_profile (db , sqlcipher_profile_callback , profile );
1219
1241
return SQLITE_OK ;
1220
1242
}
1221
1243
1222
1244
static void sqlcipher_profile_callback (void * file , const char * sql , sqlite3_uint64 run_time ){
1223
- FILE * f = (FILE * )file ;
1224
- double elapsed = run_time /1000000.0 ;
1225
- if ( f ) fprintf (f , "Elapsed time:%.3f ms - %s\n" , elapsed , sql );
1245
+ i64 log_file_sz = 0 ;
1246
+ double elapsed = 0.0 ;
1247
+ char * log_message = 0 ;
1248
+ int log_message_sz = 0 ;
1249
+ profile_ctx * pro = (profile_ctx * )file ;
1250
+ CODEC_TRACE (("sqlcipher_profile_callback entered file:%p, sql:%s runtime:%llu\n" , file , sql , run_time ));
1251
+ if (pro != NULL && pro -> file != NULL ) {
1252
+ elapsed = run_time /1000000.0 ;
1253
+ log_message = sqlite3_mprintf ("Elapsed time:%.3f ms - %s\n" , elapsed , sql );
1254
+ log_message_sz = sqlite3Strlen30 (log_message );
1255
+ sqlite3OsFileSize (pro -> file , & log_file_sz );
1256
+ sqlite3OsWrite (pro -> file , log_message , log_message_sz , log_file_sz );
1257
+ sqlite3_free (log_message );
1258
+ }
1226
1259
}
1227
1260
1228
1261
int sqlcipher_codec_fips_status (codec_ctx * ctx ) {
0 commit comments