cangrepp

Some cryptographic attacks
Log | Files | Refs | README

dbtool.c (3168B)


      1 /**
      2  * Tool for interacting with the database.
      3  */
      4 
      5 #include <err.h>
      6 #include <fmpz.h>
      7 #include <fmpz_vec.h>
      8 #include <sqlite3.h>
      9 #include <stdio.h>
     10 #include <stdlib.h>
     11 #include <string.h>
     12 #include <unistd.h>
     13 
     14 #include "fmpzio.h"
     15 #include "prodtree.h"
     16 
     17 enum command { INSERT, CREATE, NONE };
     18 
     19 // layout of tables to create as (table name, column definition) pairs
     20 const char *layout[][2] = {
     21     {"factor", "modulus TEXT, factor TEXT, callback INTEGER"},
     22     {"same", "id INTEGER PRIMARY KEY, row INTEGER"},
     23     {"products", "id INTEGER PRIMARY KEY, product TEXT"},
     24     {"tree", "node_id INTEGER, left_row INTEGER, right_row INTEGER"},
     25     {"forest", "root_row INTEGER"},
     26     {NULL, NULL}};
     27 
     28 static void
     29 usage(char *c)
     30 {
     31   printf("Usage: %s <db> <command> [..]\n"
     32          "  where\n"
     33          "<db>       path to databasee\n"
     34          "<command>  command to use\n",
     35          c);
     36   exit(1);
     37 }
     38 
     39 static void
     40 create_tables(sqlite3 *db)
     41 {
     42   int rc;
     43   sqlite3_stmt *ppStmt;
     44   char buffer[4096];
     45 
     46   size_t i = 0;
     47   while (layout[i][0] != NULL) {
     48     fprintf(stderr, "[+] Creating the \"%s\" table...\n", layout[i][0]);
     49 
     50     snprintf(buffer, 4096, "CREATE TABLE %s (%s);", layout[i][0],
     51              layout[i][1]);
     52 
     53     rc = sqlite3_prepare(db, buffer, -1, &ppStmt, NULL);
     54 
     55     if (rc != SQLITE_OK) {
     56       errx(EXIT_FAILURE, "prepare statement error: %s\n",
     57            sqlite3_errmsg(db));
     58     } else {
     59       fprintf(stderr, "  [-] Prepared statement successfully\n");
     60     }
     61 
     62     while ((rc = sqlite3_step(ppStmt)) == SQLITE_BUSY) {
     63       fprintf(stderr, "  [-] Busy, sleeping...\n");
     64       sleep(1);
     65     }
     66 
     67     if (rc != SQLITE_DONE) {
     68       errx(EXIT_FAILURE, "step error: %s\n", sqlite3_errmsg(db));
     69     } else {
     70       fprintf(stderr, "  [-] Table creation returned okay\n");
     71     }
     72 
     73     rc = sqlite3_finalize(ppStmt);
     74 
     75     if (rc != SQLITE_OK) {
     76       errx(EXIT_FAILURE, "finalize prepared statement error: %s\n",
     77            sqlite3_errmsg(db));
     78     }
     79 
     80     fprintf(stderr, "[+] Done creating the factor table\n");
     81 
     82     i++;
     83   }
     84 }
     85 
     86 static void
     87 insert(sqlite3 *db)
     88 {
     89   fmpz *v;
     90   slong nv = read_hex_lines(&v);
     91 
     92   fmpz *tree;
     93   slong ntree = prodtree_compute(&tree, v, nv);
     94 
     95   prodtree_pprint(tree, ntree);
     96 
     97   _fmpz_vec_clear(v, nv);
     98   _fmpz_vec_clear(tree, ntree);
     99 }
    100 
    101 static enum command
    102 get_command(char *str)
    103 {
    104   if (strcmp(str, "insert") == 0) {
    105     return INSERT;
    106   } else if (strcmp(str, "create") == 0) {
    107     return CREATE;
    108   } else {
    109     return NONE;
    110   }
    111 }
    112 
    113 int
    114 main(int argc, char *argv[])
    115 {
    116   sqlite3 *db;
    117   int rc;
    118   enum command com;
    119 
    120   if (argc < 3) {
    121     usage(argv[0]);
    122   }
    123 
    124   com = get_command(argv[2]);
    125   if (com == NONE) {
    126     fprintf(stderr, "Unknown command: %s\n\n", argv[2]);
    127     usage(argv[0]);
    128   }
    129 
    130   rc = sqlite3_open(argv[1], &db);
    131 
    132   if (rc) {
    133     errx(EXIT_FAILURE, "database opening error: %s\n",
    134          sqlite3_errmsg(db));
    135   } else {
    136     fprintf(stderr, "Opened database successfully\n");
    137   }
    138 
    139   switch (com) {
    140   case CREATE:
    141     create_tables(db);
    142     break;
    143   case INSERT:
    144     insert(db);
    145     fprintf(stderr, "insert not implemented\n");
    146     break;
    147   default:
    148     break;
    149   }
    150 
    151   while (sqlite3_close(db) != SQLITE_OK) {
    152   }
    153 
    154   return 0;
    155 }