You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

361 lines
8.2 KiB

11 years ago
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor Boston, MA 02110-1301, USA
*/
11 years ago
#include "common.h"
11 years ago
#include "database.h"
10 years ago
#include "bitmap.h"
11 years ago
#define DB_SEEK(db, offset) \
errno = 0; \
if (lseek((db)->fd, (offset), SEEK_SET) == -1) { \
(db)->errstr = strerror(errno); \
return -1; \
}
#define DB_READ(db, buf, len) \
errno = 0; \
memset((buf), 0x0, (len)); \
bytes = read((db)->fd, (buf), (len)); \
if (errno) { \
(db)->errstr = strerror(errno); \
return -1; \
}
#define DB_WRITE(db, buf, len) \
errno = 0; \
bytes = write((db)->fd, (buf), (len)); \
if (errno) { \
(db)->errstr = strerror(errno); \
return -1; \
}
const char *imdb_hdr_fmt = "IMDB v%02u, CAPS: %s;";
10 years ago
int imdb_init(imdb_t *db, const char *path)
{
ssize_t bytes = 0;
unsigned char buf[IMDB_REC_LEN];
memset(buf, 0x0, sizeof(char) * IMDB_REC_LEN);
if ((db->fd = open(path, O_WRONLY | O_CREAT | O_EXCL, 0644)) == -1) {
db->errstr = strerror(errno);
return -1;
}
snprintf((char *) buf, IMDB_REC_LEN, imdb_hdr_fmt, IMDB_VERSION, "M-R");
DB_SEEK(db, 0);
DB_WRITE(db, buf, IMDB_REC_LEN);
if (bytes != IMDB_REC_LEN) {
db->errstr = "Can't write database header";
return -1;
}
return 0;
}
int imdb_open(imdb_t *db, const char *path)
11 years ago
{
ssize_t bytes = 0;
struct stat st;
10 years ago
char buf[IMDB_REC_LEN] = "\0";
11 years ago
assert(db != NULL);
assert(path != NULL);
memset(db, 0x0, sizeof(imdb_t));
11 years ago
db->fd = -1;
errno = 0;
if (stat(path, &st) == -1) {
db->errstr = strerror(errno);
return -1;
11 years ago
}
errno = 0;
if ((db->fd = open(path, OPEN_FLAGS)) == -1) {
11 years ago
db->errstr = strerror(errno);
return -1;
}
db->path = path;
memset(buf, 0x0, IMDB_REC_LEN);
DB_SEEK(db, 0);
DB_READ(db, buf, IMDB_REC_LEN);
if (bytes != IMDB_REC_LEN) {
db->errstr = "Empty or damaged database file";
return -1;
}
if (memcmp("IMDB", buf, 4) != 0) {
db->errstr = "Not a database file";
return -1;
}
if (atoi(buf + 6) != IMDB_VERSION) {
db->errstr = "Database version mismatch";
return -1;
}
if (memcmp("CAPS", buf + 10, 4) != 0) {
db->errstr = "Can't read database capabilities";
return -1;
11 years ago
}
memcpy(db->caps, buf + 16, sizeof(char) * 8);
11 years ago
return 0;
}
int imdb_close(imdb_t *db)
11 years ago
{
assert(db != NULL);
errno = 0;
if (close(db->fd) != 0) {
db->errstr = strerror(errno);
return -1;
}
db->fd = -1;
return 0;
}
int imdb_read_rec(imdb_t *db, imdb_rec_t *rec)
11 years ago
{
ssize_t bytes = 0;
assert(db != NULL);
assert(rec != NULL);
assert(rec->num > 0);
10 years ago
DB_SEEK(db, rec->num * IMDB_REC_LEN);
DB_READ(db, rec->data, IMDB_REC_LEN);
11 years ago
if (bytes != IMDB_REC_LEN)
return -1;
if (rec->data[0] != 0xFF)
return 0;
return 1;
11 years ago
}
int imdb_write_rec(imdb_t *db, imdb_rec_t *rec)
11 years ago
{
ssize_t bytes = 0;
assert(db != NULL);
assert(rec != NULL);
assert(rec->num > 0);
10 years ago
DB_SEEK(db, rec->num * IMDB_REC_LEN);
DB_WRITE(db, rec->data, IMDB_REC_LEN);
11 years ago
10 years ago
return bytes / IMDB_REC_LEN;
11 years ago
}
int imdb_read_blk(imdb_t *db, imdb_block_t *blk)
11 years ago
{
11 years ago
ssize_t bytes = 0;
11 years ago
assert(db != NULL);
assert(blk != NULL);
assert(blk->start > 0);
assert(blk->records > 0);
11 years ago
11 years ago
FREE(blk->data);
10 years ago
CALLOC(blk->data, blk->records, IMDB_REC_LEN);
DB_SEEK(db, blk->start * IMDB_REC_LEN);
DB_READ(db, blk->data, blk->records * IMDB_REC_LEN);
blk->records = bytes / IMDB_REC_LEN;
11 years ago
11 years ago
return blk->records;
11 years ago
}
int imdb_read_list(imdb_t *db, imdb_rec_t *list, size_t list_len)
11 years ago
{
imdb_rec_t *r = NULL;
11 years ago
ssize_t bytes;
unsigned int i = 0;
unsigned int processed = 0;
assert(db != NULL);
assert(list != NULL);
assert(list_len > 0);
r = list;
for (i = 0; i < list_len; i++, r++) {
10 years ago
DB_SEEK(db, r->num * IMDB_REC_LEN);
DB_READ(db, r->data, IMDB_REC_LEN);
if (bytes == IMDB_REC_LEN) { processed++; }
11 years ago
}
return processed;
}
int imdb_write_list(imdb_t *db, imdb_rec_t *list, size_t list_len)
11 years ago
{
imdb_rec_t *r = NULL;
11 years ago
ssize_t bytes;
unsigned int i = 0;
unsigned int processed = 0;
assert(db != NULL);
assert(list != NULL);
assert(list_len > 0);
r = list;
for (i = 0; i < list_len; i++, r++) {
10 years ago
DB_SEEK(db, r->num * IMDB_REC_LEN);
DB_WRITE(db, r->data, IMDB_REC_LEN);
if (bytes == IMDB_REC_LEN) { processed++; }
11 years ago
}
return processed;
}
10 years ago
uint64_t
imdb_records_count(imdb_t * const db) {
struct stat st;
off_t size = 0;
memset(&st, 0x0, sizeof(struct stat));
fstat(db->fd, &st);
size = st.st_size;
return size / IMDB_REC_LEN;
}
float
ratio_from_rec_data(unsigned char * const data) {
uint16_t iw, ih;
iw = *((uint16_t *)(data + REC_OFF_IW));
ih = *((uint16_t *)(data + REC_OFF_IH));
return (iw > 0 && ih > 0) ? (iw / ih) : 0.0;
}
int
imdb_search(imdb_t * const db,
imdb_rec_t * const sample,
imdb_search_t * const search,
imdb_match_t **matches)
10 years ago
{
imdb_block_t blk;
imdb_match_t match;
10 years ago
const int blk_size = 4096;
uint64_t found = 0;
unsigned int i = 0;
unsigned char *p = NULL;
float ratio_s = 0.0; /* source */
float ratio_t = 0.0; /* tested */
10 years ago
assert(db != NULL);
assert(sample != NULL);
assert(search != NULL);
10 years ago
assert(matches != NULL);
assert(search->maxdiff_ratio >= 0.0 && search->maxdiff_ratio <= 1.0);
assert(search->maxdiff_bitmap >= 0.0 && search->maxdiff_bitmap <= 1.0);
10 years ago
memset(&blk, 0x0, sizeof(imdb_block_t));
memset(&match, 0x0, sizeof(imdb_match_t));
10 years ago
blk.start = 1;
blk.records = blk_size;
if (imdb_read_rec(db, sample) < 1) {
10 years ago
db->errstr = "Can't read source sample";
return -1;
}
if (search->limit == 0)
search->limit = -1; /* unsigned -> max */
10 years ago
if (search->maxdiff_ratio > 0.0)
ratio_s = ratio_from_rec_data(sample->data);
CALLOC(*matches, search->limit, sizeof(imdb_match_t));
while (imdb_read_blk(db, &blk) > 0) {
10 years ago
p = blk.data;
10 years ago
for (i = 0; i < blk.records; i++, p += IMDB_REC_LEN) {
if (*(p + REC_OFF_RU) == 0x0)
continue; /* record missing */
match.diff_ratio = 0.0;
match.diff_bitmap = 0.0;
/* - compare ratio - cheap */
if (ratio_s > 0.0 && (ratio_t = ratio_from_rec_data(p)) > 0.0) {
match.diff_ratio = ratio_s - ratio_t;
match.diff_ratio *= (ratio_s > ratio_t) ? 1.0 : -1.0;
if (match.diff_ratio > search->maxdiff_ratio)
continue;
} else {
/* either source or target ratio not set, can't compare, skip test */
}
10 years ago
/* - compare bitmap - more expensive */
match.diff_bitmap = (float) bitmap_compare(p + REC_OFF_BM, sample->data + REC_OFF_BM);
match.diff_bitmap /= BITMAP_BITS;
if (match.diff_bitmap > search->maxdiff_bitmap)
continue;
10 years ago
/* create match record */
match.num = blk.start + i;
memcpy(&(*matches)[found], &match, sizeof(imdb_match_t));
10 years ago
found++;
if (found >= search->limit)
break;
10 years ago
}
FREE(blk.data);
if (found >= search->limit)
break;
10 years ago
blk.start += blk_size;
}
return found;
}
uint64_t
imdb_usage_map(imdb_t * const db,
char ** const map) {
const int blk_size = 4096;
imdb_block_t blk;
uint64_t records;
unsigned char *r; /* mnemonics : block, record */
char *m = NULL; /* mnemonics : map */
memset(&blk, 0x0, sizeof(imdb_block_t));
records = imdb_records_count(db);
CALLOC(*map, records + 1, sizeof(char));
m = *map;
blk.start = 1;
blk.records = blk_size;
while (imdb_read_blk(db, &blk) > 0) {
r = blk.data;
for (unsigned int i = 0; i < blk.records; i++, m++, r += IMDB_REC_LEN) {
*m = (r[REC_OFF_RU] == 0xFF) ? CHAR_USED : CHAR_NONE;
}
blk.start += blk_size;
}
FREE(blk.data);
return records;
}