1 #define _POSIX_C_SOURCE 200809L
8 static void hash_byte(struct sha256_state *h, int ch) {
12 sha256_process(h, buf, 1);
15 /* i will be positive, we are hashing column sizes */
16 static void hash_int(struct sha256_state *h, int i) {
24 hash_byte(h, (int)(z & 0xff));
30 * Implementation of the sha3_query(SQL,SIZE) function.
32 * This function compiles and runs the SQL statement(s) given in the argument.
33 * The results are hashed using a SIZE-bit SHA3. The default size is 256.
35 * The format of the byte stream that is hashed is summarized as follows:
44 * <sql> is the original SQL text for each statement run and <n> is the size of
45 * that text. The SQL text is UTF-8. A single R character occurs before the
46 * start of each row. N means a NULL value. I mean an 8-byte little-endian
47 * integer <int>. F is a floating point number with an 8-byte little-endian
48 * IEEE floating point value <ieee-float>. B means blobs of <size> bytes. T
49 * means text rendered as <size> bytes of UTF-8. The <size> values are
50 * expressed as little endian 8 byte integers.
53 * There are zero or more R segments, one for each row in the
54 * result set. After each R, there are one or more N, I, F, B, or T segments,
55 * one for each column in the result set. Segments are concatentated directly
56 * with no delimiters of any kind.
59 static void hash_query(struct zpm *zpm, const char *zSql, struct sha256_state *h) {
61 sqlite3_stmt *pStmt = 0;
62 int nCol; /* Number of columns in the result set */
65 const unsigned char *data;
79 rc = sqlite3_prepare_v2(db, zSql, -1, &pStmt, &zSql);
81 zpm->dberrmsg = strdup(sqlite3_errmsg(db));
82 sqlite3_finalize(pStmt);
86 nCol = sqlite3_column_count(pStmt);
88 while (sqlite3_step(pStmt) == SQLITE_ROW) {
89 sha256_process(h, (const unsigned char *)"R", 1);
90 for (i = 0; i < nCol; i++) {
91 switch (sqlite3_column_type(pStmt, i)) {
97 v = sqlite3_column_int64(pStmt, i);
99 for (j = 8; j >= 1; j--) {
108 r = sqlite3_column_double(pStmt, i);
110 for (j = 8; j >= 1; j--) {
119 bytes = sqlite3_column_bytes(pStmt, i);
120 data = sqlite3_column_text(pStmt, i);
125 bytes = sqlite3_column_bytes(pStmt, i);
126 data = sqlite3_column_blob(pStmt, i);
135 sha256_process(h, data, bytes);
138 sqlite3_finalize(pStmt);
141 int zpm_package_hash(struct zpm *zpm, char *pkgid, char *hash) {
142 struct sha256_state d;
145 unsigned char tmp[32];
154 sql = sqlite3_mprintf("select package,version,release,description,architecture,url,licenses,packager,build_time from packages_pkgid where pkgid = %Q", pkgid);
156 hash_query(zpm, sql, &d);
159 /* hash package files */
161 sql = sqlite3_mprintf("select path, mode, username, groupname, configuration, "
162 "filetype, target, device, mtime, hash "
163 "from packagefiles_pkgid where pkgid = %Q order by path",
165 hash_query(zpm, sql, &d);
168 sha256_done(&d, tmp);
169 for (i=0; i<32; i++) {
170 sprintf(hash+i*2, "%02x", (unsigned)tmp[i]);
178 int zpm_package_sethash(struct zpm *zpm, char *pkgid, char *hash) {
179 char buf[ZPM_HASH_STRLEN + 1];
186 zpm_package_hash(zpm, pkgid, hash);
188 sql = sqlite3_mprintf("update packages_pkgid set hash = %Q where pkgid = %Q", hash, pkgid);
190 zpm_exec(zpm, sql, NULL, NULL, NULL);