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.
100 lines
3.1 KiB
100 lines
3.1 KiB
10 years ago
|
#include <errno.h>
|
||
|
#include <fcntl.h>
|
||
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <string.h>
|
||
|
#include <sys/stat.h>
|
||
|
#include <sys/types.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
8 years ago
|
#define SIMDB_REC_LEN 48
|
||
10 years ago
|
#define BLK_SIZE 1000
|
||
|
|
||
|
void usage(const char *message) {
|
||
|
if (message)
|
||
|
printf("error: %s\n", message);
|
||
8 years ago
|
printf("Usage: simdb_convert_1to2 <infile> <outfile>\n");
|
||
10 years ago
|
exit(EXIT_FAILURE);
|
||
|
}
|
||
|
|
||
|
int main(int argc, char **argv) {
|
||
|
int in, out;
|
||
8 years ago
|
long unsigned int simdb_rec_total, rec_first, rec_last, records;
|
||
|
unsigned char in_buf[SIMDB_REC_LEN * BLK_SIZE];
|
||
|
unsigned char out_buf[SIMDB_REC_LEN * BLK_SIZE];
|
||
|
unsigned char header[SIMDB_REC_LEN];
|
||
10 years ago
|
unsigned char *src, *dst;
|
||
|
struct stat st;
|
||
|
ssize_t bytes;
|
||
|
|
||
8 years ago
|
memset(header, 0x0, SIMDB_REC_LEN);
|
||
|
snprintf((char *) header, SIMDB_REC_LEN, "IMDB v%02u, CAPS: %s;", 2, "M-R");
|
||
10 years ago
|
|
||
|
if (argc < 2)
|
||
|
usage(NULL);
|
||
|
|
||
|
errno = 0;
|
||
|
if ((in = open(argv[1], O_RDONLY)) < 0)
|
||
|
usage(strerror(errno));
|
||
|
|
||
|
if ((out = open(argv[2], O_WRONLY | O_TRUNC | O_CREAT, 0644)) < 0)
|
||
|
usage(strerror(errno));
|
||
|
|
||
|
if (fstat(in, &st) != 0)
|
||
|
usage(strerror(errno));
|
||
|
|
||
8 years ago
|
if ((st.st_size % SIMDB_REC_LEN) != 0)
|
||
10 years ago
|
usage("database size expected to be multiples to 48");
|
||
|
|
||
10 years ago
|
if (lseek(in, 0, SEEK_SET) < 0)
|
||
|
usage(strerror(errno));
|
||
|
|
||
8 years ago
|
if (read(in, in_buf, SIMDB_REC_LEN) != SIMDB_REC_LEN)
|
||
10 years ago
|
usage("can't read header of database");
|
||
|
|
||
|
if (memcmp(in_buf, "DB of image fingerprints (ver 1)", 32) != 0)
|
||
|
usage("wrong database header / version mismatch");
|
||
|
|
||
8 years ago
|
simdb_rec_total = (st.st_size / SIMDB_REC_LEN);
|
||
|
printf("Processing %lu records\n", simdb_rec_total - 1);
|
||
10 years ago
|
|
||
8 years ago
|
if (lseek(in, SIMDB_REC_LEN, SEEK_SET) < 0)
|
||
10 years ago
|
usage(strerror(errno));
|
||
|
|
||
|
if (lseek(out, 0, SEEK_SET) < 0)
|
||
|
usage(strerror(errno));
|
||
8 years ago
|
if (write(out, header, SIMDB_REC_LEN) != SIMDB_REC_LEN)
|
||
10 years ago
|
usage(strerror(errno));
|
||
|
|
||
8 years ago
|
for (unsigned int block = 0; block <= (simdb_rec_total / BLK_SIZE); block++) {
|
||
10 years ago
|
rec_first = rec_last = (block * BLK_SIZE) + 1;
|
||
8 years ago
|
records = ((rec_first + BLK_SIZE) > simdb_rec_total) ? simdb_rec_total - rec_first : BLK_SIZE;
|
||
10 years ago
|
rec_last = rec_first + records;
|
||
|
printf("* block %u, %4lu records [%lu, %lu]\n",
|
||
|
block + 1, records, rec_first, rec_last - 1);
|
||
8 years ago
|
lseek(in, rec_first * SIMDB_REC_LEN, SEEK_SET);
|
||
|
lseek(out, rec_first * SIMDB_REC_LEN, SEEK_SET);
|
||
|
bytes = read(in, in_buf, SIMDB_REC_LEN * records);
|
||
|
if (bytes != (ssize_t) records * SIMDB_REC_LEN) {
|
||
10 years ago
|
printf("Read size mismatch, expected %lu, got: %zi\n",
|
||
8 years ago
|
SIMDB_REC_LEN * records, bytes);
|
||
10 years ago
|
exit(EXIT_FAILURE);
|
||
|
}
|
||
8 years ago
|
memset(out_buf, 0x0, records * SIMDB_REC_LEN);
|
||
10 years ago
|
for (unsigned int i = 0; i < records; i++) {
|
||
8 years ago
|
src = &in_buf[i * SIMDB_REC_LEN];
|
||
|
dst = &out_buf[i * SIMDB_REC_LEN];
|
||
10 years ago
|
memcpy(dst + 0, src + 0, sizeof(char) * 1); // usage flag
|
||
|
memcpy(dst + 16, src + 2, sizeof(char) * 32); // image bitmap
|
||
|
}
|
||
8 years ago
|
bytes = write(out, out_buf, SIMDB_REC_LEN * records);
|
||
|
if (bytes != (ssize_t) records * SIMDB_REC_LEN) {
|
||
10 years ago
|
printf("Write size mismatch, expected %lu, got: %zi\n",
|
||
8 years ago
|
SIMDB_REC_LEN * records, bytes);
|
||
10 years ago
|
exit(EXIT_FAILURE);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
exit(EXIT_SUCCESS);
|
||
|
}
|