Deleted switch statement for hashing algorithms, using the integer field directly...
[strongswan.git] / src / libimcv / tcg / pts / pts_database.c
1 /*
2 * Copyright (C) 2011 Sansar Choinyambuu
3 * HSR Hochschule fuer Technik Rapperswil
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 */
15
16 #include "pts_database.h"
17
18 #include <debug.h>
19 #include <crypto/hashers/hasher.h>
20
21
22 typedef struct private_pts_database_t private_pts_database_t;
23
24 /**
25 * Private data of a pts_database_t object.
26 *
27 */
28 struct private_pts_database_t {
29
30 /**
31 * Public pts_database_t interface.
32 */
33 pts_database_t public;
34
35 /**
36 * database instance
37 */
38 database_t *db;
39
40 };
41
42 METHOD(pts_database_t, create_file_enumerator, enumerator_t*,
43 private_pts_database_t *this, char *product)
44 {
45 enumerator_t *e;
46
47 /* look for all entries belonging to a product in the files table */
48 e = this->db->query(this->db,
49 "SELECT f.id, f.type, f.path FROM files AS f "
50 "JOIN product_file AS pf ON f.id = pf.file "
51 "JOIN products AS p ON p.id = pf.product "
52 "WHERE p.name = ?",
53 DB_TEXT, product, DB_INT, DB_INT, DB_TEXT);
54 return e;
55 }
56
57 METHOD(pts_database_t, create_meas_enumerator, enumerator_t*,
58 private_pts_database_t *this, char *product, int id, pts_meas_algorithms_t algorithm)
59 {
60 enumerator_t *e;
61
62 /* look for all entries belonging to a product and file in file_hashes table */
63 e = this->db->query(this->db,
64 "SELECT fh.hash FROM file_hashes AS fh "
65 "JOIN files AS f ON fh.file = f.id "
66 "JOIN products AS p ON fh.product = p.id "
67 "WHERE p.name = ? AND f.id = ? AND fh.algo = ?",
68 DB_TEXT, product, DB_INT, id, DB_INT, algorithm, DB_BLOB);
69 return e;
70 }
71
72 METHOD(pts_database_t, destroy, void,
73 private_pts_database_t *this)
74 {
75 this->db->destroy(this->db);
76 free(this);
77 }
78
79 /**
80 * See header
81 */
82 pts_database_t *pts_database_create(char *uri)
83 {
84 private_pts_database_t *this;
85
86 INIT(this,
87 .public = {
88 .create_file_enumerator = _create_file_enumerator,
89 .create_meas_enumerator = _create_meas_enumerator,
90 .destroy = _destroy,
91 },
92 .db = lib->db->create(lib->db, uri),
93 );
94
95 if (!this->db)
96 {
97 DBG1(DBG_TNC, "failed to connect to PTS file measurement database '%s'",
98 uri);
99 free(this);
100 return NULL;
101 }
102
103 return &this->public;
104 }
105