core18Example_main.c
Simple example that shows how to encrypt and decrypt a database. This example needs a compiled schema, core18Example.sdl.
#include "rdm.h"
#include "rdmapi.h"
#include "rdmtfsapi.h"
#include "rdmbcdapi.h"
#include "core18Example_structs.h"
#include "core18Example_cat.h"
#include "pspplatpackage.h"
#include "rdmstartupapi.h"
#include <stdio.h>
#include <stdlib.h>
RDM_EXPORT int32_t main_encryptionTutorial (int32_t argc, const char *const *argv)
{
RDM_TFS tfs;
RDM_RETCODE rc;
RDM_UNREF (argc);
RDM_UNREF (argv);
/* Allocate a TFS Handle */
rc = rdm_rdmAllocTFS (&tfs);
{
rc = rdm_tfsInitialize (tfs);
if (rc == sOKAY)
{
RDM_ENCRYPT enc;
/* Allocate an encryption handle */
#if defined(LINK_ENC_SIMPLE)
#else
rc = rdm_tfsAllocEncrypt (tfs, "AES128:testing", &enc);
#endif
if (rc == sOKAY)
{
RDM_DB db;
/* Allocate a database hande */
rc = rdm_tfsAllocDatabase (tfs, &db);
if (rc == sOKAY)
{
RDM_TABLE_ID tables[] = {TABLE_ENCRYPTIONTABLE};
ENCRYPTIONTABLE insertions[3];
RDM_CURSOR cursor = NULL;
bool decrypt = false;
/* Associate the catalog with the database handle */
rc = rdm_dbSetCatalog (db, core18Example_cat);
if (rc == sOKAY)
{
/* Open the database - try without any encryption */
rc = rdm_dbOpen (
}
if (rc == sOKAY)
{
printf ("Opened the core18 database to be "
"encrypted.\n");
}
{
/* The database is already encrypted so we will decrypt
it, start by Associating the encryption handle with
the database handle so we can open the encrypted
database */
rc = rdm_dbSetEncrypt (db, enc);
if (rc == sOKAY)
{
/* Open the database - try with encryption */
rc = rdm_dbOpen (
db, "core18", RDM_OPEN_EXCLUSIVE);
if (rc == sOKAY)
{
printf (
"Opened the core18Example database "
"to be decrypted.\n");
decrypt = true;
}
}
}
/* We will now either encrypt or decrypt the open database
*/
if (rc == sOKAY)
{
if (decrypt == true)
{
/* Decrypt the database by specifying a NULL
* encryption handle */
}
else
{
/* Encrypt the database by specifying the encryption
* handle */
rc = rdm_dbEncrypt (db, enc, "");
}
/* Close the database */
rdm_dbClose (db);
}
/* If we encrypted the database we must add the encryption
key to the database handle in order to open it, if we
decrypted the database we don't need to do anything */
{
rc = rdm_dbSetEncrypt (db, enc);
}
if (rc == sOKAY)
{
/* Open the database */
rc = rdm_dbOpen (
}
if (rc == sOKAY)
{
/* Start a database transaction and remove any existing
* data. */
rc = rdm_dbStartUpdate (
db, tables, RDM_LEN (tables), NULL, 0, NULL);
}
if (rc == sOKAY)
{
/* Remove all rows from the database */
rc = rdm_dbDeleteAllRowsFromDatabase (db);
if (rc == sOKAY)
{
rc = rdm_dbEnd (db);
}
else
{
rdm_dbEndRollback (db);
}
}
/* Insert some rows into the table */
if (rc == sOKAY)
{
RDM_BCD_T rdmBCD;
insertions[0].id = rdmBCD;
(void) rdm_bcdFromInt32 (343, &rdmBCD);
insertions[1].id = rdmBCD;
(void) rdm_bcdFromDouble (1248.16, &rdmBCD);
insertions[2].id = rdmBCD;
/* Start a database transaction to insert some rows. */
rc = rdm_dbStartUpdate (
db, tables, RDM_LEN (tables), NULL, 0, NULL);
}
if (rc == sOKAY)
{
uint32_t counter;
for (counter = 0;
counter++)
{
char outBuffer[100];
(void) rdm_bcdToString (
&insertions[counter].id, false, outBuffer,
sizeof (outBuffer), NULL);
printf ("BCD Inserted: %s\n", outBuffer);
rc = rdm_dbInsertRow (
db, TABLE_ENCRYPTIONTABLE, &insertions[counter],
sizeof (insertions[counter]), NULL);
}
if (rc == sOKAY)
{
rc = rdm_dbEnd (db);
}
else
{
rdm_dbEndRollback (db);
}
}
/* Read the rows from the table */
if (rc == sOKAY)
{
/* Allocate a cursor */
rc = rdm_dbAllocCursor (db, &cursor);
}
if (rc == sOKAY)
{
/* Start a read transaction */
rc = rdm_dbStartRead (
db, tables, RDM_LEN (tables), NULL);
if (rc == sOKAY)
{
/* Fill the cursor with rows */
if (rc == sOKAY)
{
rc = rdm_dbGetRows (
db, TABLE_ENCRYPTIONTABLE, &cursor);
}
if (rc == sOKAY)
{
/* navigate to the first row in the cursor */
rc = rdm_cursorMoveToFirst (cursor);
}
while (rc == sOKAY)
{
ENCRYPTIONTABLE row;
/* Read the entire row */
rc = rdm_cursorReadRow (
cursor, &row, sizeof (row), NULL);
if (rc == sOKAY)
{
char outBuffer[100];
(void) rdm_bcdToString (
&row.id, false, outBuffer,
sizeof (outBuffer), NULL);
printf ("BCD Retrieved: %s\n", outBuffer);
/* navigate to the next row in the cursor */
rc = rdm_cursorMoveToNext (cursor);
}
}
/* We expect to break out of the loop with a
* sENDOFCURSOR */
{
rc = sOKAY;
}
/* End the read transaction */
rdm_dbEnd (db);
}
/* free the cursor */
rdm_cursorFree (cursor);
}
rdm_dbFree (db);
}
rdm_encryptFree (enc);
}
}
rdm_tfsFree (tfs);
}
if (rc != sOKAY)
{
printf (
"There was an error in this Tutorial (%s - %s)\n",
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
RDM_STARTUP_EXAMPLE (encryptionTutorial)
RDM_RETCODE rdm_bcdFromString(const char *operand, RDM_BCD_T *result)
Convert a string to a BCD number.
const char * rdm_retcodeGetName(RDM_RETCODE retcode)
Get the mnemonic name for an error or status code.
RDM_RETCODE rdm_dbEncrypt(RDM_DB db, RDM_ENCRYPT enc, const char *optString)
Encrypt a database.
Header for the native RaimaDB Runtime API.
Header for the RaimaDB Core API.
RDM_RETCODE rdm_bcdFromDouble(double operand, RDM_BCD_T *result)
Convert a double to a BCD number.
RDM_RETCODE rdm_dbDeleteAllRowsFromDatabase(RDM_DB db)
Remove all rows from a database.
RDM_RETCODE rdm_dbStartRead(RDM_DB db, const RDM_TABLE_ID *tableIds, uint32_t numTableIds, RDM_TRANS *pTrans)
Get read locks.
RDM_RETCODE rdm_cursorReadRow(RDM_CURSOR cursor, void *colValues, size_t bytesIn, size_t *bytesOut)
Read all columns from a row.
RDM_RETCODE rdm_dbClose(RDM_DB db)
Close the database associated with a database handle.
RDM_RETCODE rdm_dbSetCatalog(RDM_DB db, const char *catalog)
Associate a catalog with an allocated database.
RDM_RETCODE rdm_dbSetEncrypt(RDM_DB db, RDM_ENCRYPT enc)
Associate an encryption context with a database handle.
RDM_RETCODE rdm_dbOpen(RDM_DB db, const char *dbNameSpec, RDM_OPEN_MODE mode)
Open an existing RaimaDB database using the specified database handle.
RDM_RETCODE rdm_tfsAllocDatabase(RDM_TFS tfs, RDM_DB *pDb)
Allocate memory for a new RaimaDB db.
Header for the Transactional File Server (TFS) API.
RDM_RETCODE rdm_dbGetRows(RDM_DB db, RDM_TABLE_ID tableId, RDM_CURSOR *pCursor)
Associate an RDM_CURSOR with rows based on a table id.
const char * rdm_retcodeGetDescription(RDM_RETCODE retcode)
Invoke RaimaDB error handler.
RDM_RETCODE rdm_dbEndRollback(RDM_DB db)
End and rollback a transactional operation.
RDM_RETCODE rdm_bcdFromInt32(int32_t operand, RDM_BCD_T *result)
Convert an int32_t to a BCD number.
RDM_RETCODE rdm_dbInsertRow(RDM_DB db, RDM_TABLE_ID tableId, const void *colValues, size_t bytesIn, RDM_CURSOR *pCursor)
Insert a new row into a table at the specified rowId.
Header that defines the name of the package.
Header for Binary-coded decimal (BCD) API.
RDM_RETCODE rdm_tfsAllocEncrypt(RDM_TFS hTFS, const char *passcode, RDM_ENCRYPT *pEnc)
Allocate an encryption context.
RDM_RETCODE rdm_dbStartUpdate(RDM_DB db, const RDM_TABLE_ID *writeTableIds, uint32_t numWriteTableIds, const RDM_TABLE_ID *readTableIds, uint32_t numReadTableIds, RDM_TRANS *pTrans)
Get write locks.
RDM_RETCODE rdm_bcdToString(const RDM_BCD_T *operand, bool useExponent, char *result, size_t inBytes, size_t *bytesOut)
Convert a BCD number to a string.
Internal RaimaDB Startup API used by startup macros.