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>
int32_t main_encryptionTutorial (int32_t argc, const char *const *argv)
{
RDM_TFS tfs;
RDM_UNREF (argc);
RDM_UNREF (argv);
/* Allocate a TFS Handle */
rc = rdm_rdmAllocTFS (&tfs);
if (rc == sOKAY)
{
rc = rdm_tfsInitialize (tfs);
if (rc == sOKAY)
{
/* Allocate an encryption handle */
#if defined(LINK_ENC_SIMPLE)
rc = rdm_tfsAllocEncrypt (tfs, "XOR:testing", &enc);
#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)
{
/* Override default DURABLE setting to CONSISTENT */
rc = rdm_dbSetOptions (db, "durability=consistent");
}
if (rc == sOKAY)
{
RDM_TABLE_ID tables[] = {TABLE_ENCRYPTIONTABLE};
ENCRYPTIONTABLE insertions[3];
RDM_CURSOR cursor = NULL;
/* 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 (
db, "core18Example", RDM_OPEN_EXCLUSIVE);
}
if (rc == sOKAY)
{
printf ("Opened the core18Example database to be "
"encrypted.\n");
}
else if (rc == eINVENCRYPT)
{
/* 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, "core18Example", RDM_OPEN_EXCLUSIVE);
if (rc == sOKAY)
{
printf (
"Opened the core18Example database "
"to be decrypted.\n");
decrypt = RDM_TRUE;
}
}
}
/* We will now either encrypt or decrypt the open database
*/
if (rc == sOKAY)
{
if (decrypt == RDM_TRUE)
{
/* Decrypt the database by specifying a NULL
* encryption handle */
rc = rdm_dbEncrypt (db, NULL, "");
}
else
{
/* Encrypt the database by specifying the encryption
* handle */
rc = rdm_dbEncrypt (db, enc, "");
}
/* Close the database */
}
/* 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 */
if (rc == sOKAY && decrypt == RDM_FALSE)
{
rc = rdm_dbSetEncrypt (db, enc);
}
if (rc == sOKAY)
{
/* Open the database */
rc = rdm_dbOpen (
db, "core18Example", RDM_OPEN_SHARED);
}
if (rc == sOKAY)
{
/* Start a database transaction and remove any existing
* data. */
db, tables, RDM_LEN (tables), NULL, 0, NULL);
}
if (rc == sOKAY)
{
/* Remove all rows from the database */
if (rc == sOKAY)
{
rc = rdm_dbEnd (db);
}
else
{
}
}
/* Insert some rows into the table */
if (rc == sOKAY)
{
RDM_BCD_T rdmBCD;
(void) rdm_bcdFromString ("72.5", &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. */
db, tables, RDM_LEN (tables), NULL, 0, NULL);
}
if (rc == sOKAY)
{
uint32_t counter;
for (counter = 0;
rc == sOKAY && counter < RDM_LEN (insertions);
counter++)
{
char outBuffer[100];
&insertions[counter].id, RDM_FALSE, outBuffer,
sizeof (outBuffer), NULL);
printf ("BCD Inserted: %s\n", outBuffer);
db, TABLE_ENCRYPTIONTABLE, &insertions[counter],
sizeof (insertions[counter]), NULL);
}
if (rc == sOKAY)
{
rc = rdm_dbEnd (db);
}
else
{
}
}
/* Read the rows from the table */
if (rc == sOKAY)
{
/* Allocate a cursor */
rc = rdm_dbAllocCursor (db, &cursor);
}
if (rc == sOKAY)
{
/* Start a read transaction */
db, tables, RDM_LEN (tables), NULL);
if (rc == sOKAY)
{
/* Fill the cursor with rows */
if (rc == sOKAY)
{
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 */
cursor, &row, sizeof (row), NULL);
if (rc == sOKAY)
{
char outBuffer[100];
&row.id, RDM_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 */
if (rc == sENDOFCURSOR)
{
rc = sOKAY;
}
/* End the read transaction */
rdm_dbEnd (db);
}
/* free the cursor */
rdm_cursorFree (cursor);
}
}
}
}
}
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.
RDM_RETCODE rdm_cursorMoveToFirst(RDM_CURSOR cursor)
Position a cursor to the first row in the collection.
const char * rdm_retcodeGetName(RDM_RETCODE retcode)
Get the mnemonic name for an error or status code.
RDM_RETCODE rdm_cursorMoveToNext(RDM_CURSOR cursor)
Position a cursor to the next row in the collection.
RDM_RETCODE rdm_dbEncrypt(RDM_DB db, RDM_ENCRYPT enc, const char *optString)
Encrypt a database.
RDM_RETCODE rdm_dbSetOptions(RDM_DB db, const char *optString)
Set RDM options.
Header for the native RDM Runtime API.
RDM_RETCODE rdm_rdmAllocTFS(RDM_TFS *phTFS)
Allocate a TFS handle.
Header for the RDM Core API.
@ eINVENCRYPT
Definition: rdmretcodetypes.h:273
@ RDM_FALSE
Definition: psptypes.h:59
struct RDM_ENCRYPT_S * RDM_ENCRYPT
Definition: rdmtypes.h:309
RDM_RETCODE rdm_dbEnd(RDM_DB db)
End a transactional operation.
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.
struct RDM_CURSOR_S * RDM_CURSOR
Definition: rdmtypes.h:306
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_OPEN_EXCLUSIVE
Definition: rdmtypes.h:254
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.
uint32_t RDM_TABLE_ID
Definition: rdmtypes.h:27
RDM_RETCODE rdm_dbSetEncrypt(RDM_DB db, RDM_ENCRYPT enc)
Associate an encryption context with a database handle.
RDM_RETCODE rdm_dbFree(RDM_DB db)
Free a database handle.
RDM_RETCODE rdm_dbOpen(RDM_DB db, const char *dbNameSpec, RDM_OPEN_MODE mode)
Open an existing RDM database using the specified database handle.
@ sOKAY
Definition: rdmretcodetypes.h:96
@ RDM_TRUE
Definition: psptypes.h:60
RDM_RETCODE rdm_cursorFree(RDM_CURSOR cursor)
Free an RDM_CURSOR.
RDM_RETCODE rdm_tfsAllocDatabase(RDM_TFS tfs, RDM_DB *pDb)
Allocate memory for a new RDM db.
Header for the Transactional File Server (TFS) API.
#define RDM_STARTUP_EXAMPLE(name)
Definition: rdmstartuptypes.h:73
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 RDM error handler.
RDM_RETCODE rdm_dbEndRollback(RDM_DB db)
End and rollback a transactional operation.
RDM_BOOL_T
Definition: psptypes.h:58
RDM_RETCODE rdm_bcdFromInt32(int32_t operand, RDM_BCD_T *result)
Convert an int32_t to a BCD number.
RDM_RETCODE rdm_dbAllocCursor(RDM_DB db, RDM_CURSOR *pCursor)
Allocate a cursor.
The RDM Binary Coded Decimal (BCD) data structure.
Definition: rdmbcdtypes.h:65
#define RDM_UNREF(a)
Definition: psptypes.h:45
@ RDM_OPEN_SHARED
Definition: rdmtypes.h:253
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.
struct RDM_TFS_S * RDM_TFS
RDM TFS Handle.
Definition: rdmtfstypes.h:21
Header that defines the name of the package.
struct RDM_DB_S * RDM_DB
Definition: rdmtypes.h:305
Header for Binary-coded decimal (BCD) API.
RDM_RETCODE rdm_tfsFree(RDM_TFS hTFS)
Terminate a TFS service.
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.
#define RDM_LEN(x)
Definition: psptypes.h:78
RDM_RETCODE rdm_encryptFree(RDM_ENCRYPT enc)
Free an encryption context.
@ sENDOFCURSOR
Definition: rdmretcodetypes.h:59
Internal RDM Startup API used by startup macros.
RDM_RETCODE
RDM status and error return codes.
Definition: rdmretcodetypes.h:44
RDM_RETCODE rdm_tfsInitialize(RDM_TFS tfs)
Initialize a RDM_TFS instance.
RDM_RETCODE rdm_bcdToString(const RDM_BCD_T *operand, RDM_BOOL_T useExponent, char *result, size_t inBytes, size_t *bytesOut)
Convert a BCD number to a string.