-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.c
2184 lines (2130 loc) · 103 KB
/
db.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***
Copyright 2010-2021 by Omar Alejandro Herrera Reyna
Caume Data Security Engine, also known as CaumeDSE is released under the
GNU General Public License by the Copyright holder, with the additional
exemption that compiling, linking, and/or using OpenSSL is allowed.
LICENSE
This file is part of Caume Data Security Engine, also called CaumeDSE.
CaumeDSE is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
CaumeDSE is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with CaumeDSE. If not, see <http://www.gnu.org/licenses/>.
INCLUDED SOFTWARE
This product includes software developed by the OpenSSL Project
for use in the OpenSSL Toolkit (http://www.openssl.org/).
This product includes cryptographic software written by
Eric Young ([email protected]).
This product includes software written by
Tim Hudson ([email protected]).
This product includes software from the SQLite library that is in
the public domain (http://www.sqlite.org/copyright.html).
This product includes software from the GNU Libmicrohttpd project, Copyright
© 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
2008, 2009, 2010 , 2011, 2012 Free Software Foundation, Inc.
This product includes software from Perl5, which is Copyright (C) 1993-2005,
by Larry Wall and others.
***/
#include "common.h"
//TODO (OHR#5#):create and call function to sanitize all input that will be included in DB queries!
int cmeDBCreateOpen (const char *filename, sqlite3 **ppDB)
{
int result;
result = sqlite3_open_v2(filename, ppDB,SQLITE_OPEN_READWRITE|
SQLITE_OPEN_CREATE|SQLITE_OPEN_FULLMUTEX, NULL);
if (result!=SQLITE_OK)
{
#ifdef ERROR_LOG
fprintf(stderr,"CaumeDSE Error: cmeDBCreateOpen(), sqlite_open_v2() error: %s\n",
sqlite3_errmsg(*ppDB));
#endif
sqlite3_close(*ppDB);
return(1);
}
else
{
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeDBCreateOpen(), Created/opened sqlite3 database file: %s.\n",filename);
#endif
return(0);
}
}
int cmeDBOpen (const char *filename, sqlite3 **ppDB)
{
int result;
result = sqlite3_open_v2(filename, ppDB,SQLITE_OPEN_READWRITE|
SQLITE_OPEN_FULLMUTEX, NULL);
if (result!=SQLITE_OK)
{
#ifdef ERROR_LOG
fprintf(stderr,"CaumeDSE Error: cmeDBOpen(), sqlite_open_v2() error: %s\n",
sqlite3_errmsg(*ppDB));
#endif
//sqlite3_close(*ppDB);
return(1);
}
else
{
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeDBOpen(), Opened sqlite3 database file: %s.\n",filename);
#endif
return(0);
}
}
int cmeDBClose (sqlite3 *ppDB)
{
int result=0;
result=sqlite3_close(ppDB);
if (result!=SQLITE_OK)
{
#ifdef ERROR_LOG
fprintf(stderr,"CaumeDSE Error: cmeDBClose(), sqlite_close() error: %s\n",
sqlite3_errmsg(ppDB));
#endif
return(1);
}
else
{
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeDBClose(), Closed sqlite3 database file.\n");
#endif
return(0);
}
}
//Based on code from: http://www.sqlite.org/backup.html
int cmeMemDBLoadOrSave(sqlite3 *pInMemory, const char *zFilename, int isSave)
{
int result=0;
sqlite3 *pFile=NULL;
sqlite3_backup *pBackup=NULL;
sqlite3 *pTo=NULL;
sqlite3 *pFrom=NULL;
if(isSave) // we save
{
result=cmeDBCreateOpen(zFilename,&pFile);
}
else //we load
{
result=cmeDBOpen(zFilename,&pFile);
}
if (result) //Error Opening or creating file.
{
return(1);
}
else //Everything is ok now.
{
// If this is a 'load' operation (isSave==0), then data is copied
// from the database file to database pInMemory.
// Otherwise, data is copied from pInMemory to pFile.
// TODO (ANY#8#): if isSave, Vacuum mem first, then save!
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeDBMemLoadOrSave(), Opened DB file %s for "
"R/W successfuly. isSave: %d \n",zFilename, isSave);
#endif
pFrom = (isSave ? pInMemory : pFile);
pTo = (isSave ? pFile : pInMemory);
// Set up the backup procedure to copy from the "main" database of
// connection pFile to the main database of connection pInMemory.
pBackup = sqlite3_backup_init(pTo, "main", pFrom, "main");
if( pBackup )
{
(void)sqlite3_backup_step(pBackup, -1); //Copy full database at once.
(void)sqlite3_backup_finish(pBackup);
}
result = sqlite3_errcode(pTo);
}
cmeDBClose(pFile); // Close the database connection opened on database file zFilename
return (result);
}
int cmeSQLIterate (const char *args,int numCols,char **pStrResults,char **pColNames)
{
int cont,cont2,result;
int maxResults=0;
int returnedResults=0;
char **perlResults=NULL;
char *fName=NULL;
char *sqlStatemnt=NULL;
#define cmeSQLIterateFree() \
do { \
cmeFree(fName); \
cmeFree(sqlStatemnt); \
if (perlResults) \
{ \
for (cont=0; cont<returnedResults;cont++) \
{ \
cmeFree(perlResults[cont]); \
} \
cmeFree(perlResults); \
} \
} while (0); //Local free() macro.
cmeStrConstrAppend(&fName,"%s",(char *)args);
maxResults=numCols;
perlResults=(char **)malloc((sizeof(char *))*numCols);
if (!cmeResultMemTable)//If this is the first iteration (i.e. cmeResultMemTable==NULL).
{
cmeResultMemTableCols=numCols; //Set column names of Tmp MemTable.
cmeResultMemTable=(char **)malloc(sizeof(char*)*numCols); //There can't be more results than there are columns (as of this version).
result=cmePerlParserScriptFunction(cmeDefaultPerlColNameSetupFunction,cdsePerl,pColNames,numCols, //Send column names on first iteration to special subroutine "cmePERLProcessColumnNames"
perlResults,maxResults,&returnedResults);
if (result) //Error
{
#ifdef ERROR_LOG
fprintf(stderr,"CaumeDSE Error: cmeSQLIterate(), Error in cmePerlParserScriptFunction()"
": %d.\n",result);
#endif
cmeSQLIterateFree();
return(result);
}
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeSQLIterate(), Prepared %d results"
" from perl function %s (first iteration).\n",returnedResults,cmeDefaultPerlColNameSetupFunction);
#endif
for(cont=0;cont<numCols;cont++) //Copy column names
{
cmeResultMemTable[cont]=NULL;
if (returnedResults==numCols) // If script returned column names (modified?) then use them
{
cmeStrConstrAppend(&(cmeResultMemTable[cont]),"%s",perlResults[numCols -1 -cont]);
}
else //Otherwise use the column names provided to this function
{
cmeStrConstrAppend(&(cmeResultMemTable[cont]),"%s",pColNames[cont]);
}
}
cmeResultMemTableRows=0; //Column names are always included (row 0), even if row count ignores them.
}
for (cont=0; cont<returnedResults;cont++) //Free results from first iteration (columns), if any...
{
cmeFree(perlResults[cont]);
}
cmeResultMemTableRows++; //Increase the number of rows included in the Tmp MemTable.
cmeResultMemTable=(char **)realloc(cmeResultMemTable,sizeof(char*)*(cmeResultMemTableCols*cmeResultMemTableRows+numCols)); //Add space for one additional row
if (!cmeResultMemTable) //Realloc error.
{
return(1);
}
for (cont=(cmeResultMemTableCols*cmeResultMemTableRows);cont<(cmeResultMemTableCols*cmeResultMemTableRows+numCols);cont++)
{
cmeResultMemTable[cont]=NULL; //initialize new row pointers.
}
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeSQLIterate(), Prepared to iterate on a row with"
" the following %d columns and values:\n",numCols);
for (cont=0; cont<numCols; cont++)
{
fprintf(stdout,"\t\t%d) col.name:%s value:%s\n",cont+1,pColNames[cont],pStrResults[cont]);
}
#endif
result=cmePerlParserScriptFunction(fName,cdsePerl,pStrResults,numCols,
perlResults,maxResults,&returnedResults);
if (result) //Error
{
#ifdef ERROR_LOG
fprintf(stderr,"CaumeDSE Error: cmeSQLIterate(), Error in cmePerlParserScriptFunction()"
": %d.\n",result);
#endif
cmeSQLIterateFree();
return(result);
}
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeSQLIterate(), Prepared %d results"
" from perl function %s.\n",returnedResults,fName);
#endif
cont2=cmeResultMemTableCols*cmeResultMemTableRows;
for (cont=returnedResults-1; cont>=0; cont--) //pop results from the stack in reverse (correct) order
{
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeSQLIterate(), cmePerlParserScriptFunction() result %d:"
" %s.\n",cont+1,perlResults[cont]);
#endif
cmeStrConstrAppend(&(cmeResultMemTable[cont2]),"%s",perlResults[cont]);
cont2++;
}
cmeSQLIterateFree();
return(0);
}
int cmeSQLRows (sqlite3 *db, const char *sqlQuery, char *perlScriptName,
PerlInterpreter *myPerl)
{
int result,cont,cont2,numCols;
int regCount=0;
sqlite3_stmt *sqlStatemnt=NULL;
char **columnNames=NULL;
char **columnValues=NULL;
const char *pSqlInstruction=NULL;
const char *tail;
const char *errMsg;
#define cmeSQLRowsFree() \
do { \
cmeFree(columnNames); \
cmeFree(columnValues); \
} while (0); //Local free() macro.
if (myPerl==NULL) //No Callback function defined
{
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeSQLRows(), sqlite3_exec() to be called with "
"no Callback function. Query:\n%s\n",sqlQuery);
#endif
//0) Open connection to Database; this is done outside of this function (DB is assumed to be open).
//1) Prepare SQL statement:
pSqlInstruction=sqlQuery;
do
{
numCols=-1; //Reset column counter for each SQL instruction.
result = sqlite3_prepare_v2(db,pSqlInstruction,-1,&sqlStatemnt,&tail);
if (result) // Error
{
#ifdef ERROR_LOG
errMsg=sqlite3_errmsg(db);
fprintf(stderr,"CaumeDSE Error: cmeSQLRows(), sqlite3_prepare_v2() error: %s\n",
errMsg);
#endif
cmeSQLRowsFree();
return(2);
}
//2) Execute statement and get each result row:
while (sqlite3_step(sqlStatemnt) == SQLITE_ROW)
{
if (numCols==-1) //First cycle.
{
numCols=sqlite3_data_count(sqlStatemnt); //Get number of columns in row.
columnNames=(char **)malloc(sizeof(char *)*numCols); //Reserve memory for column names' string pointers.
columnValues=(char **)malloc(sizeof(char *)*numCols); //Reserve memory for column values' string pointers.
for (cont=0;cont<numCols;cont++) //Point to column names.
{
columnNames[cont]=(char *)sqlite3_column_name(sqlStatemnt,cont);
}
if (cmeResultMemTable)//cmeResultMemTable is not empty, free all variables.
{
cmeResultMemTableClean();
}
//Insert column names into new table:
cmeResultMemTableCols=numCols; //Set column names of Tmp MemTable.
cmeResultMemTable=(char **)malloc(sizeof(char*)*numCols); //There can't be more results than there are columns (for now).
for(cont=0;cont<numCols;cont++) //Copy column names.
{
cmeResultMemTable[cont]=NULL;
cmeStrConstrAppend(&(cmeResultMemTable[cont]),"%s",columnNames[cont]);
}
cmeResultMemTableRows=0; //Column names are always included (row 0), even if row count ignores them.
}
cmeResultMemTableRows++; //Increase the number of rows included in the Tmp MemTable.
cmeResultMemTable=(char **)realloc(cmeResultMemTable,sizeof(char*)*(cmeResultMemTableCols*cmeResultMemTableRows+numCols)); //Add space for one additional row
if (!cmeResultMemTable) //Realloc error.
{
return(1);
}
for (cont=(cmeResultMemTableCols*cmeResultMemTableRows);cont<(cmeResultMemTableCols*cmeResultMemTableRows+numCols);cont++)
{
cmeResultMemTable[cont]=NULL; //initialize new row pointers.
}
for (cont=0;cont<numCols;cont++) //Point to column values; copy values to new row.
{
columnValues[cont]=(char *)sqlite3_column_text(sqlStatemnt,cont);
cmeStrConstrAppend(&(cmeResultMemTable[cmeResultMemTableCols*cmeResultMemTableRows+cont]),"%s",columnValues[cont]);
}
regCount++;
}
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeSQLRows(), sqlite3_step() cycle finished; '%d' result rows were obtained.\n",
regCount);
#endif
//3) Finalize statement:
sqlite3_finalize(sqlStatemnt);
//3.5) Cycle for next SQL instruction. if available
pSqlInstruction=tail;
} while (sqlStatemnt);
//4) Close DB connection; this will be done outside of this function.
}
else //Callback function required
{
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeSQLRows(), sqlite3_exec() to be called with "
"Callback Perl function: Script name: %s. Query:\n%s\n",
perlScriptName,sqlQuery);
#endif
//0) Open connection to Database; this is done outside of this function (DB is assumed to be open).
//1) Prepare SQL statement:
pSqlInstruction=sqlQuery;
do
{
numCols=-1; //Reset column counter for each SQL instruction.
result = sqlite3_prepare_v2(db,pSqlInstruction,-1,&sqlStatemnt,&tail);
if (result) // Error
{
#ifdef ERROR_LOG
errMsg=sqlite3_errmsg(db);
fprintf(stderr,"CaumeDSE Error: cmeSQLRows(), sqlite3_prepare_v2() error: %s\n",
errMsg);
#endif
cmeSQLRowsFree();
return(2);
}
//2) Execute statement and get each result row:
while (sqlite3_step(sqlStatemnt) == SQLITE_ROW)
{
if (numCols==-1) //First cycle.
{
numCols=sqlite3_data_count(sqlStatemnt); //Get number of columns in row.
columnNames=(char **)malloc(sizeof(char *)*numCols); //Reserve memory for column names' string pointers.
columnValues=(char **)malloc(sizeof(char *)*numCols); //Reserve memory for column values' string pointers.
for (cont=0;cont<numCols;cont++) //Point to column names.
{
columnNames[cont]=(char *)sqlite3_column_name(sqlStatemnt,cont);
}
if (cmeResultMemTable)//cmeResultMemTable is not empty, free all variables.
{
for (cont2=0;cont2<=cmeResultMemTableRows;cont2++) //Process each row. (Note that column names are put in row 0; we need to include this row).
{
for(cont=0;cont<cmeResultMemTableCols;cont++) //Process each column.
{
memset(cmeResultMemTable[cont2*cmeResultMemTableCols+cont],0,strlen(cmeResultMemTable[cont2*cmeResultMemTableCols+cont])); //Wipe sensitive data.
cmeFree(cmeResultMemTable[cont2*cmeResultMemTableCols+cont]);
}
}
cmeFree(cmeResultMemTable);
cmeResultMemTableCols=0;
cmeResultMemTableRows=0;
}
}
for (cont=0;cont<numCols;cont++) //Point to column values.
{
columnValues[cont]=(char *)sqlite3_column_text(sqlStatemnt,cont);
}
//Parse each row with the script iteration function:
cmeSQLIterate(perlScriptName,numCols,columnValues,columnNames);
regCount++;
}
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeSQLRows(), sqlite3_step() cycle finished; '%d' result rows were obtained.\n",
regCount);
#endif
//3) Finalize statement:
sqlite3_finalize(sqlStatemnt);
//3.5) Cycle for next SQL instruction. if available
pSqlInstruction=tail;
} while (sqlStatemnt);
//4) Close DB connection; this will be done outside of this function.
}
cmeSQLRowsFree();
return(0);
}
int cmeResultMemTableClean ()
{
int cont,cont2;
if (cmeResultMemTable)//cmeResultMemTable is not empty, free all variables.
{
for (cont2=0;cont2<=cmeResultMemTableRows;cont2++) //Process each row. (Note that column names are put in row 0; we need to include this row).
{
for(cont=0;cont<cmeResultMemTableCols;cont++) //Process each column.
{
memset(cmeResultMemTable[cont2*cmeResultMemTableCols+cont],0,strlen(cmeResultMemTable[cont2*cmeResultMemTableCols+cont])); //Wipe sensitive data.
cmeFree(cmeResultMemTable[cont2*cmeResultMemTableCols+cont]);
}
}
cmeFree(cmeResultMemTable);
cmeResultMemTableCols=0;
cmeResultMemTableRows=0;
}
return(0);
}
int cmeMemTable (sqlite3 *db, const char *sqlQuery,char ***pQueryResult,
int *numRows, int *numColumns)
{
int result __attribute__((unused)); char *pzErrmsg=NULL; // TODO (OHR#2#): Delete pErrmsg from parameters; passing back errors from sqlite3 is complicated since associated memory needs to be freed with sqlite3_free. We will deal with those errors only within functions that call sqlite functions directly!
//TODO (ANY#8#): replace call to sqlite3_get_table() with calls to sqlite3_exec() and use PRAGMA table_info
// This is because sqlite3_get_table is apparently obsolete (??) and should be avoided, according to docs.
result=sqlite3_get_table(db, sqlQuery, pQueryResult, numRows, numColumns,
&pzErrmsg);
//NOTE: numRows does not consider the header row at index 0.
//sqlite3_get_table allways includes a header row; real number of rows= numRows+1 !
if (pzErrmsg!=NULL) // Then we have a problem...
{
#ifdef ERROR_LOG
fprintf(stderr,"CaumeDSE Error: cmeSQLTable(), sqlite3_get_table() error: %s\n",
pzErrmsg);
sqlite3_free(pzErrmsg);
#endif
return(1);
}
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeSQLTable(), sqlite3_get_table() successful.\n");
#endif
return(0);
}
int cmeMemTableFinal (char **QueryResult)
{
sqlite3_free_table(QueryResult);
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeSQLTableFinal(), sqlite3_free_table() executed.\n");
#endif
return(0);
}
int cmeMemTableToMemDB (sqlite3 *dstMemDB, const char **srcMemTable, const int numRows,
const int numCols, char *sqlTableName)
{
int cont,cont2,cont3,result;
int rowsBlocks;
int rowsLastBlock;
int currentRow=1;
char *sqlInsertQuery=NULL;
char *sqlCreateCols=NULL;
char *sqlInsertCols=NULL;
char *sanitizedStr=NULL;
const char *defaultTableName="data";
#define cmeMemTableToMemDBFree() \
do { \
cmeFree(sqlInsertQuery); \
cmeFree(sqlInsertCols); \
cmeFree(sqlCreateCols); \
cmeFree(sanitizedStr); \
} while (0); //Local free() macro.
if (!numCols) //If MemTable is empty, we do nothing and exit
{
cmeMemTableToMemDBFree();
return(0);
}
if(!sqlTableName) //if table name is empty; assign a default name
{
sqlTableName=(char *)defaultTableName;
}
rowsBlocks=numRows / cmeDefaultInsertSqlRows;
rowsLastBlock=numRows % cmeDefaultInsertSqlRows;
cmeStrConstrAppend(&sqlCreateCols,"id INTEGER PRIMARY KEY,"); //First add the ID.
cmeStrConstrAppend(&sqlInsertCols,"id,"); //First insert the ID autoincrement.
for (cont=0;cont<numCols;cont++) //Create sqlCreateCols string.
{
cmeFree(sanitizedStr);
cmeSanitizeStrForSQL(srcMemTable[cont],&sanitizedStr); //Sanitize parameter for use in SQL statement.
cmeStrConstrAppend(&sqlCreateCols,"'%s' TEXT",sanitizedStr); //first row, contains the column name.
cmeStrConstrAppend(&sqlInsertCols,"'%s'",sanitizedStr);
if ((cont+1) < numCols) //If there is still another element pending, add comma.
{
cmeStrConstrAppend(&sqlCreateCols,",");
cmeStrConstrAppend(&sqlInsertCols,",");
}
}
cmeStrConstrAppend(&sqlInsertQuery,"BEGIN TRANSACTION; CREATE TABLE \"%s\" (%s); COMMIT;",
sqlTableName,sqlCreateCols); // Create table 'memtable'
result=cmeSQLRows(dstMemDB,sqlInsertQuery,NULL,NULL);
if (result) //Check Error Type
{
#ifdef ERROR_LOG
fprintf(stderr,"CaumeDSE Error: cmeMemTableToMemDB(), cmeSQLRows() Error, can't "
"create table: %s (%s) !\n",sqlTableName,sqlCreateCols);
#endif
cmeMemTableToMemDBFree();
return(1);
}
for (cont=0; cont < rowsBlocks; cont++) //process all blocks of cmeDefaultInsertSqlRows rows.
{
cmeFree(sqlInsertQuery);
result=cmeStrConstrAppend(&sqlInsertQuery,"BEGIN TRANSACTION;"); // First part of block
for (cont2=0; cont2 < cmeDefaultInsertSqlRows; cont2++) //Process all rows.
{
result=cmeStrConstrAppend(&sqlInsertQuery," INSERT INTO \"%s\" (%s) VALUES (NULL", //Prepare INSERT statement.
sqlTableName,sqlInsertCols);
if (numCols)
{
result=cmeStrConstrAppend(&sqlInsertQuery,",");
}
for (cont3=0; cont3<numCols; cont3++) //Process all fields in row.
{
cmeFree(sanitizedStr);
cmeSanitizeStrForSQL(srcMemTable[(currentRow*numCols)+cont3],&sanitizedStr); //Sanitize parameter for use in SQL statement.
result=cmeStrConstrAppend(&sqlInsertQuery,"'%s'",sanitizedStr);
if((cont3+1)<numCols)
{
result=cmeStrConstrAppend(&sqlInsertQuery,",");
}
}
currentRow++;
result=cmeStrConstrAppend(&sqlInsertQuery,");"); //Finish INSERT statement.
}
result=cmeStrConstrAppend(&sqlInsertQuery,"COMMIT;"); // Last part of block
if (cmeSQLRows(dstMemDB,sqlInsertQuery,NULL,NULL)) //insert row.
{
#ifdef ERROR_LOG
fprintf(stderr,"CaumeDSE Error: cmeMemTableToMemDB(), cmeSQLRows() Error, can't "
"insert values, sqlquery: %s in table: %s !\n",sqlInsertQuery,sqlTableName);
#endif
cmeMemTableToMemDBFree();
return(2);
}
}
//Process last block.
cmeFree(sqlInsertQuery);
result=cmeStrConstrAppend(&sqlInsertQuery,"BEGIN TRANSACTION;"); // First part of block.
for (cont2=0; cont2 < rowsLastBlock; cont2++) //process all columns in each row.
{
result=cmeStrConstrAppend(&sqlInsertQuery," INSERT INTO \"%s\" (%s) VALUES (NULL", //Prepare INSERT statement.
sqlTableName,sqlInsertCols);
if (numCols)
{
result=cmeStrConstrAppend(&sqlInsertQuery,",");
}
for (cont3=0; cont3<numCols; cont3++) //Add values
{
cmeFree(sanitizedStr);
cmeSanitizeStrForSQL(srcMemTable[(currentRow*numCols)+cont3],&sanitizedStr); //Sanitize parameter for use in SQL statement.
result=cmeStrConstrAppend(&sqlInsertQuery,"'%s'",sanitizedStr);
if((cont3+1)<numCols)
{
result=cmeStrConstrAppend(&sqlInsertQuery,",");
}
}
currentRow++;
result=cmeStrConstrAppend(&sqlInsertQuery,");"); //Finish INSERT statement.
}
result=cmeStrConstrAppend(&sqlInsertQuery,"COMMIT;"); // Last part of block.
if (cmeSQLRows(dstMemDB,sqlInsertQuery,NULL,NULL)) //insert row.
{
#ifdef ERROR_LOG
fprintf(stderr,"CaumeDSE Error: cmeMemTableToMemDB(), cmeSQLRows() Error, can't "
"insert values, sqlquery: %s in table: %s !\n",sqlInsertQuery,sqlTableName);
#endif
cmeMemTableToMemDBFree();
return(3);
}
cmeMemTableToMemDBFree();
return (0);
}
int cmeMemTableShuffle(char **sqlTable, const int numRows, const int numCols, const int skipHeaderRows,
const int skipIdCols) //Implements Durstenfeld's shuffling algorithm
//http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
{
int cont, cont2, rndIdx;
unsigned int randomInt=0;
unsigned char *prngBuf=NULL;
unsigned char *prngBufStr=NULL;
char *valueTmp=NULL;
#define cmeMemTableShuffleFree() \
do { \
cmeFree(prngBuf); \
cmeFree(prngBufStr); \
valueTmp=NULL; \
} while (0); //Local free() macro.
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeMemTableShuffle(), preparing to shuffle MemTable with %d rows (not counting header row)"
" and %d columns; headers to skip: %d; columns to skip: %d.\n",numRows,numCols,skipHeaderRows,
skipIdCols);
#endif
for (cont=numRows;cont>=skipHeaderRows; cont--) //Not cont=numRows-1, since we need to consider the header row.
{
cmePrngGetBytes(&prngBuf,sizeof(int));
cmeBytesToHexstr(prngBuf,&prngBufStr,sizeof(int));
sscanf((char *)prngBufStr,"%X",&randomInt);
cmeFree(prngBuf);
cmeFree(prngBufStr);
rndIdx=(int)(randomInt%numRows);
if (rndIdx<=(skipHeaderRows-1)) //Hits a row we need to skip.
{
rndIdx=(numRows); //Then exchange with a row at the other end.
}
for (cont2=(numCols-1);cont2>=skipIdCols;cont2--)
{
valueTmp=sqlTable[(cont*numCols)+cont2];
sqlTable[(cont*numCols)+cont2]=sqlTable[(rndIdx*numCols)+cont2];
sqlTable[(rndIdx*numCols)+cont2]=valueTmp;
}
}
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeMemTableShuffle(), finished shuffling MemTable with %d rows"
" and %d columns; headers to skip: %d; columns to skip: %d.\n",numRows,numCols,skipHeaderRows,
skipIdCols);
#endif
cmeMemTableShuffleFree();
return (0);
}
int cmeMemTableOrder(char **sqlTable, const int numRows, const int numCols, const int orderIdxCol, const int skipHeaderRows,
const int skipIdCols)
{
int cont,cont2,cont3;
char *valueTmp=NULL;
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeMemTableOrder(), preparing to reorder MemTable with %d rows (not counting header row)"
" and %d columns; headers to skip: %d; columns to skip: %d.\n",numRows,numCols,skipHeaderRows,
skipIdCols);
#endif
for (cont=numRows;cont>=skipHeaderRows; cont--) //Not cont=numRows-1, since we need to consider the header row.
{
for (cont2=cont;cont2>=skipHeaderRows; cont2--) //Search every row <= current row for the one that belongs to this position, and swap them.
{
if (cont==atoi(sqlTable[(cont2*numCols)+orderIdxCol])) //We found the right row. Swap and break.
{
for (cont3=(numCols-1);cont3>=skipIdCols;cont3--)
{
valueTmp=sqlTable[(cont*numCols)+cont3];
sqlTable[(cont*numCols)+cont3]=sqlTable[(cont2*numCols)+cont3];
sqlTable[(cont2*numCols)+cont3]=valueTmp;
}
break;
}
}
}
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeMemTableOrder(), finished ordering MemTable with %d rows"
" and %d columns; headers to skip: %d; columns to skip: %d.\n",numRows,numCols,skipHeaderRows,
skipIdCols);
#endif
return (0);
}
int cmeMemSecureDBProtect (sqlite3 *memSecureDB, const char *orgKey)
{
int cont,result,written;
int cont2=0;
int numColsData=0;
int numRowsData=0;
int numColsPMeta=0;
int numRowsPMeta=0;
char **memData=NULL;
char **memProtectMetaData=NULL;
char **currentDataSalt=NULL;
char *currentEncB64Data=NULL;
char *currentMetaAttribute=NULL;
char *currentMetaAttributeData=NULL;
char *currentMetaId=NULL;
char *currentMetaSalt=NULL;
char *currentMetaUserId=NULL;
char *currentMetaOrgId=NULL;
char *currentDataId=NULL;
char *currentDataEncAlg=NULL;
char *sqlQuery=NULL;
char *sanitizedStr=NULL;
unsigned char *rndBytes=NULL;
const EVP_CIPHER *cipher=NULL;
#define cmeMemSecureDBProtectFree() \
do { \
cmeFree(currentEncB64Data); \
cmeFree(currentMetaAttribute); \
cmeFree(currentMetaAttributeData); \
cmeFree(currentMetaId); \
cmeFree(currentMetaSalt); \
cmeFree(currentMetaUserId); \
cmeFree(currentMetaOrgId); \
cmeFree(currentDataId); \
cmeFree(currentDataEncAlg); \
cmeFree(sqlQuery); \
cmeFree(sanitizedStr); \
cmeFree(rndBytes); \
if (memData) \
{ \
cmeMemTableFinal(memData); \
memData=NULL; \
} \
if (memProtectMetaData) \
{ \
cmeMemTableFinal(memProtectMetaData); \
memProtectMetaData=NULL; \
} \
if (currentDataSalt) \
{ \
for (cont=0;cont<numRowsData;cont++) \
{ \
cmeFree(currentDataSalt[cont]); \
} \
cmeFree(currentDataSalt); \
} \
} while (0); //Local free() macro.
//Get information from table meta (we assume tables meta and data have not been protected yet).
result=cmeMemTable(memSecureDB,"SELECT * FROM meta;",&memProtectMetaData,&numRowsPMeta,&numColsPMeta);
if (result) //Error
{
#ifdef ERROR_LOG
fprintf(stderr,"CaumeDSE Error: cmeMemSecureDBProtect(), cmeMemTable() Error"
" can't execute 'SELECT * FROM meta;'!\n");
#endif
cmeMemSecureDBProtectFree();
return(1);
}
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeMemSecureDBProtect(), cmeMemTable() loaded memSecureDB, table meta.\n");
#endif
//Get information from table data (we assume tables meta and data have not been protected yet).
result=cmeMemTable(memSecureDB,"SELECT * FROM data;",&memData,&numRowsData,&numColsData);
if (result) //Error
{
#ifdef ERROR_LOG
fprintf(stderr,"CaumeDSE Error: cmeMemSecureDBProtect(), cmeMemTable() Error"
" can't execute 'SELECT * FROM data;'!\n");
#endif
cmeMemSecureDBProtectFree();
return(2);
}
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeMemSecureDBProtect(), cmeMemTable() loaded memSecureDB, table data.\n");
#endif
//Create salt array for data and update in data table. We skip first row of headers.
currentDataSalt=(char **)malloc(sizeof(char **)*(numRowsData)); //Salt array will be freed at the end of function.
for (cont=1;cont<=numRowsData;cont++)
{
cmeGetRndSaltAnySize(&(currentDataSalt[cont-1]),cmeDefaultSecureDBSaltLen);
//Update Salt info in data table.
cmeFree(sqlQuery);
cmeStrConstrAppend(¤tDataId,"%d",atoi(memData[cmeIDDColumnFileDataNumCols*cont+cmeIDDanydb_id])); //sanitize currentDataId with atoi()
cmeFree(sanitizedStr);
cmeSanitizeStrForSQL(currentDataSalt[cont-1],&sanitizedStr); //Sanitize parameter salt for use in SQL statement (salt is included as is).
cmeStrConstrAppend(&sqlQuery,"BEGIN;"
"UPDATE data SET salt='%s' WHERE id=%s; COMMIT;",sanitizedStr,currentDataId);
cmeFree(currentDataId);
result=cmeSQLRows(memSecureDB,sqlQuery,NULL,NULL);
if (result) //Error
{
#ifdef ERROR_LOG
fprintf(stderr,"CaumeDSE Error: cmeMemSecureDBProtect(), cmeSQLRows() Error"
" can't execute update query: %s !\n",sqlQuery);
#endif
cmeMemSecureDBProtectFree();
return(3);
}
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeMemSecureDBProtect(), updated 'salt' in data table "
" with query: %s.\n",sqlQuery);
#endif
cmeFree(sqlQuery);
}
//Apply each protection mecanism to the table:
for (cont=1; cont<=numRowsPMeta; cont++) //Iterate on each protection row in meta.
{
cmeGetRndSaltAnySize(¤tMetaSalt,cmeDefaultSecureDBSaltLen);
cmeStrConstrAppend(¤tMetaAttribute,"%s",memProtectMetaData[cont* //Get meta.attribute
cmeIDDColumnFileMetaNumCols+cmeIDDColumnFileMeta_attribute]);
cmeStrConstrAppend(¤tMetaAttributeData,"%s",memProtectMetaData[cont* //Get meta.attributeData
cmeIDDColumnFileMetaNumCols+cmeIDDColumnFileMeta_attributeData]);
cmeStrConstrAppend(¤tMetaUserId,"%s",memProtectMetaData[cont*
cmeIDDColumnFileMetaNumCols+cmeIDDanydb_userId]); //Get meta.userId
cmeStrConstrAppend(¤tMetaOrgId,"%s",memProtectMetaData[cont*
cmeIDDColumnFileMetaNumCols+cmeIDDanydb_orgId]); //Get meta.orgId
cmeStrConstrAppend(¤tMetaId,"%d",atoi(memProtectMetaData[cont*
cmeIDDColumnFileMetaNumCols+cmeIDDanydb_id])); //Get meta.id; sanitize it with atoi().
// Check if protection attribute = "name".
if (!strncmp(memProtectMetaData[cont*cmeIDDColumnFileMetaNumCols+cmeIDDColumnFileMeta_attribute],
cmeIDDColumnFileMeta_attribute_0, sizeof(cmeIDDColumnFileMeta_attribute_0)))
{
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeMemSecureDBProtect(): 'name' will be protected by default in Meta.\n");
#endif
}
// Check if protection attribute = "shuffle".
else if (!strncmp(memProtectMetaData[cont*cmeIDDColumnFileMetaNumCols+cmeIDDColumnFileMeta_attribute],
cmeIDDColumnFileMeta_attribute_1, sizeof(cmeIDDColumnFileMeta_attribute_1)))
{
cmeMemTableShuffle(memData,numRowsData,numColsData,1,1);
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeMemSecureDBProtect(), shuffle protection applied to table Data.\n");
#endif
for(cont2=1;cont2<=numRowsData;cont2++) //Updates all shuffled rows (leaves column 'id' untouched); protects 'rowOrder'.
{
cmeStrConstrAppend(¤tDataEncAlg,"%s",memProtectMetaData[cont*cmeIDDColumnFileMetaNumCols+
cmeIDDColumnFileMeta_attributeData]); //Get encryption algorithm.
result=cmeGetCipher(&cipher,currentDataEncAlg);
if (!result) //OK, supported algorithm
{
//Encrypt rowOrder and update memSQL table Data:
cmeStrConstrAppend(¤tDataId,"%d",atoi(memData[cmeIDDColumnFileDataNumCols*cont2+cmeIDDanydb_id])); //Sanitize id with atoi().
//Protect 'rowOrder' (data table):
result=cmeProtectDBSaltedValue(memData[cmeIDDColumnFileDataNumCols*cont2+cmeIDDColumnFileData_rowOrder], ¤tEncB64Data,
currentDataEncAlg, &(currentDataSalt[cont2-1]), orgKey, &written);
if (result) //Error
{
#ifdef ERROR_LOG
fprintf(stderr,"CaumeDSE Error: cmeMemSecureDBProtect(), cmeProtectDBSaltedValue() Error, can't "
"protect 'rowOrder' in data table: %s with algorithm %s!\n",
memData[cmeIDDColumnFileDataNumCols*cont2+cmeIDDColumnFileData_rowOrder],currentDataEncAlg);
#endif
cmeMemSecureDBProtectFree();
return(5);
}
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeMemSecureDBProtect(), protected"
" 'rowOrder'. Result: %s.\n",currentEncB64Data);
#endif
cmeStrConstrAppend(&sqlQuery,"BEGIN; UPDATE data SET"); //First part of query.
cmeFree(sanitizedStr);
cmeSanitizeStrForSQL(memData[cmeIDDColumnFileDataNumCols*cont2+cmeIDDanydb_userId],
&sanitizedStr); //Sanitize parameter userId for use in SQL statement.
cmeStrConstrAppend(&sqlQuery," userId='%s'",sanitizedStr);
cmeFree(sanitizedStr);
cmeSanitizeStrForSQL(memData[cmeIDDColumnFileDataNumCols*cont2+cmeIDDanydb_orgId],
&sanitizedStr); //Sanitize parameter orgId for use in SQL statement.
cmeStrConstrAppend(&sqlQuery,",orgId='%s'",sanitizedStr);
cmeFree(sanitizedStr);
cmeSanitizeStrForSQL(currentDataSalt[cont2-1],
&sanitizedStr); //Sanitize parameter salt for use in SQL statement.
cmeStrConstrAppend(&sqlQuery,",salt='%s'",sanitizedStr); //Include new salts.
cmeFree(sanitizedStr);
cmeSanitizeStrForSQL(memData[cmeIDDColumnFileDataNumCols*cont2+cmeIDDColumnFileData_value],
&sanitizedStr); //Sanitize parameter value for use in SQL statement.
cmeStrConstrAppend(&sqlQuery,",value='%s'",sanitizedStr);
cmeStrConstrAppend(&sqlQuery,",rowOrder='%s'",currentEncB64Data);
cmeFree(sanitizedStr);
cmeSanitizeStrForSQL(memData[cmeIDDColumnFileDataNumCols*cont2+cmeIDDColumnFileData_MAC],
&sanitizedStr); //Sanitize parameter MAC for use in SQL statement.
cmeStrConstrAppend(&sqlQuery,",MAC='%s'",sanitizedStr);
cmeFree(sanitizedStr);
cmeSanitizeStrForSQL(memData[cmeIDDColumnFileDataNumCols*cont2+cmeIDDColumnFileData_MACProtected],
&sanitizedStr); //Sanitize parameter MACProtected for use in SQL statement
cmeStrConstrAppend(&sqlQuery,",MACProtected='%s'",sanitizedStr);
cmeFree(sanitizedStr);
cmeSanitizeStrForSQL(memData[cmeIDDColumnFileDataNumCols*cont2+cmeIDDColumnFileData_sign],
&sanitizedStr); //Sanitize parameter sign for use in SQL statement
cmeStrConstrAppend(&sqlQuery,",sign='%s'",sanitizedStr);
cmeFree(sanitizedStr);
cmeSanitizeStrForSQL(memData[cmeIDDColumnFileDataNumCols*cont2+cmeIDDColumnFileData_signProtected],
&sanitizedStr); //Sanitize parameter signProtected for use in SQL statement
cmeStrConstrAppend(&sqlQuery,",signProtected='%s'",sanitizedStr);
cmeFree(sanitizedStr);
cmeSanitizeStrForSQL(memData[cmeIDDColumnFileDataNumCols*cont2+cmeIDDColumnFileData_otphDKey],
&sanitizedStr); //Sanitize parameter otphDkey for use in SQL statement
cmeStrConstrAppend(&sqlQuery,",otphDkey='%s'",sanitizedStr);
cmeStrConstrAppend(&sqlQuery," WHERE id=%s; COMMIT;",currentDataId); //Last part of query.
result=cmeSQLRows(memSecureDB,sqlQuery,NULL,NULL);
cmeFree(currentEncB64Data);
if (result) //Error
{
#ifdef ERROR_LOG
fprintf(stderr,"CaumeDSE Error: cmeMemSecureDBProtect(), cmeSQLRows() Error"
" can't execute update query: %s !\n",sqlQuery);
#endif
cmeMemSecureDBProtectFree();
return(6);
}
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeMemSecureDBProtect(), shuffle-protected row with id %s in table data.\n",
currentDataId);
#endif
cmeFree(sqlQuery);
cmeFree(currentDataId);
cmeFree(currentDataEncAlg);
}
else //Error: unsupported/unknown algorithm!
{
#ifdef ERROR_LOG
fprintf(stderr,"CaumeDSE Error: cmeMemSecureDBProtect(), Error, can't "
"protect 'rowOrder' in data table: %s with algorithm %s!\n",
memData[cmeIDDColumnFileDataNumCols*cont2+cmeIDDColumnFileData_value],currentDataEncAlg);
#endif
cmeMemSecureDBProtectFree();
return(7);
}
}
}
// Check if protection attribute = "protect".
else if (!strncmp(memProtectMetaData[cont*cmeIDDColumnFileMetaNumCols+cmeIDDColumnFileMeta_attribute],
cmeIDDColumnFileMeta_attribute_2, sizeof(cmeIDDColumnFileMeta_attribute_2)))
{
//Protect 'value' column in Data table, use random salt for each 'value' and store it as well:
cmeStrConstrAppend(¤tDataEncAlg,"%s",memProtectMetaData[cont*cmeIDDColumnFileMetaNumCols+
cmeIDDColumnFileMeta_attributeData]); //Get encryption algorithm.
result=cmeGetCipher(&cipher,currentDataEncAlg);
if (!result) //OK, supported algorithm
{
for(cont2=1;cont2<=numRowsData;cont2++) //We skip the header row.
{
cmeStrConstrAppend(¤tDataId,"%d",atoi(memData[cmeIDDColumnFileDataNumCols*cont2+cmeIDDanydb_id])); //Sanitize using atoi();
//Protect 'value' (data table):
result=cmeProtectDBSaltedValue(memData[cmeIDDColumnFileDataNumCols*cont2+cmeIDDColumnFileData_value], ¤tEncB64Data,
currentDataEncAlg, &(currentDataSalt[cont2-1]), orgKey, &written);
if (result) //Error
{
#ifdef ERROR_LOG
fprintf(stderr,"CaumeDSE Error: cmeMemSecureDBProtect(), cmeProtectDBSaltedValue() Error, can't "
"protect 'value' in data table: %s with algorithm %s!\n",
memData[cmeIDDColumnFileDataNumCols*cont2+cmeIDDColumnFileData_value],currentDataEncAlg);
#endif
cmeMemSecureDBProtectFree();
return(8);
}
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeMemSecureDBProtect(), protected"
" 'value'. Result: %s.\n",currentEncB64Data);
#endif
cmeStrConstrAppend(&sqlQuery,"BEGIN; UPDATE data SET value='%s'",currentEncB64Data); //First part of query.
cmeFree(currentEncB64Data);
//Protect 'userId' (data table):
result=cmeProtectDBSaltedValue (memData[cmeIDDColumnFileDataNumCols*cont2+cmeIDDanydb_userId], ¤tEncB64Data,
currentDataEncAlg, &(currentDataSalt[cont2-1]), orgKey, &written);
if (result) //Error
{
#ifdef ERROR_LOG
fprintf(stderr,"CaumeDSE Error: cmeMemSecureDBProtect(), cmeProtectDBSaltedValue() Error, can't "
"protect 'userId' in data table: %s with algorithm %s!\n",
memData[cmeIDDColumnFileDataNumCols*cont2+cmeIDDanydb_userId],currentDataEncAlg);
#endif
cmeMemSecureDBProtectFree();
return(9);
}
#ifdef DEBUG
fprintf(stdout,"CaumeDSE Debug: cmeMemSecureDBProtect(), protected"
" 'userId'. Result: %s.\n",currentEncB64Data);