@@ -67,13 +67,19 @@ typedef struct {
6767 void * provider_ctx ;
6868} cipher_ctx ;
6969
70+ typedef struct {
71+ sqlite3_file * file ;
72+ char * filename ;
73+ } profile_ctx ;
74+
7075static unsigned int default_flags = DEFAULT_CIPHER_FLAGS ;
7176static unsigned char hmac_salt_mask = HMAC_SALT_MASK ;
7277static int default_kdf_iter = PBKDF2_ITER ;
7378static int default_page_size = SQLITE_DEFAULT_PAGE_SIZE ;
7479static unsigned int sqlcipher_activate_count = 0 ;
7580static sqlite3_mutex * sqlcipher_provider_mutex = NULL ;
7681static sqlcipher_provider * default_provider = NULL ;
82+ static profile_ctx * profile = NULL ;
7783
7884struct codec_ctx {
7985 int kdf_salt_sz ;
@@ -1202,27 +1208,54 @@ int sqlcipher_codec_add_random(codec_ctx *ctx, const char *zRight, int random_sz
12021208}
12031209
12041210int 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 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 ){
12151237 return SQLITE_ERROR ;
12161238 }
12171239 }
1218- sqlite3_profile (db , sqlcipher_profile_callback , f );
1240+ sqlite3_profile (db , sqlcipher_profile_callback , profile );
12191241 return SQLITE_OK ;
12201242}
12211243
12221244static 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+ }
12261259}
12271260
12281261int sqlcipher_codec_fips_status (codec_ctx * ctx ) {
0 commit comments