/* vi: set sw=4 ts=4: */ #include "exception.h" #include "db_base.h" #include #include #include #include #include #include #include #include #define RESIZE 1024 #if 0 #define throw #define errorEx(a,b) printf("ERROR: %s : %s\n", (a), (b)) // db_base [5] = keys, reqs, certs temps, lists typedef struct { char *data; } db_base; #endif static int database = -1; static int h2n(char c) { if (c>='0' && c<='9') return c-'0'; if (c>='a' && c<='f') return c-'a'+10; if (c>='A' && c<='F') return c-'A'+10; return 0; } static char *read_data(int fd, char *buf, int buflen, int *retlen) { char *p; int len=0, binlen=0, alloclen=RESIZE; p = (char*)malloc(alloclen); if (!p) return NULL; buf[80] = '\0'; *retlen = 0; //printf("Buffer(%d) = %p, '%s'\n", buflen, buf, buf); while(buf[len]!='\n') { while (len0) { if (buf[0] == ' ') { if (database>=0 && database<5) db = dbs[database]; else db = NULL; p = read_data(fd, buf+1, ret-1, &retlen); lseek(fd, offs +(2* (retlen+1)), SEEK_SET); kv ^=1; if (db && !md5) { if (kv) { printf("Importing: %s ", p); pki = db->newPKI(); if (!pki) { close(fd); return -1; } pki->setIntName(p); } else { printf("size=%d\n", retlen); try { pki->oldFromData((unsigned char*)p, retlen); db->insert(pki); } catch (errorEx &err) { printf("Error catched for '%s'\n", CCHAR(pki->getIntName())); } } } else if (md5) { if (database == 5) { static int md5sum=0; printf("Settings: '%s'\n", p); if (kv) md5sum = (!strcmp(p, "pwhash")) ? 1:0; if (!kv && md5sum) { printf("MD5SUM = '%s'\n", p); strncpy(md5, p, 50); close(fd); return 0; } } } free(p); } else { if (kv) { close(fd); printf("Binary value expected\n"); return -1; } p = strchr(buf, '\n'); if (!p) { close(fd); return -1; } *p=0; ret = lseek(fd, offs + (p-buf) +1 , SEEK_SET); //printf("SEEK to %d\n", ret); handle_option(buf, ret); } offs = lseek(fd, 0, SEEK_CUR); } if (ret <0) { close(fd); throw errorEx(filename, strerror(errno)); return -1; } close(fd); return 0; } #if 0 int main(int argc, char *argv[]) { read_dump(argv[1], NULL); } #endif