Initial revision

This commit is contained in:
brianb
2000-02-12 23:51:37 +00:00
commit 30ab45dc35
15 changed files with 1585 additions and 0 deletions

15
src/libmdb/Makefile Normal file
View File

@@ -0,0 +1,15 @@
CC = gcc
INC = -I ../include
OBJS = catalog.o mem.o file.o
all: libmdb
libmdb: $(OBJS)
ar cq libmdb.a $(OBJS)
clean:
rm -f core *.o *.a
.c.o:
$(CC) -g -c $< $(INC)

108
src/libmdb/catalog.c Normal file
View File

@@ -0,0 +1,108 @@
/* MDB Tools - A library for reading MS Access database file
* Copyright (C) 2000 Brian Bruns
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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 Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "mdbtools.h"
char *mdb_get_objtype_string(int obj_type)
{
static char *type_name[] = {"Form",
"Table",
"Macro",
"System Table",
"Report",
"Query",
"Linked Table",
"Module",
"Unknown 0x08",
"Unknown 0x09",
"Unknown 0x0a",
"Unknown 0x0b"
};
if (obj_type > 11) {
return NULL;
} else {
return type_name[obj_type];
}
}
MDB_CATALOG_ENTRY *mdb_catalog_entry(MDB_HANDLE *mdb, int rowid, MDB_CATALOG_ENTRY *entry)
{
int offset;
int rows;
int i,j;
rows = mdb_get_int16(mdb, 8);
if (rowid < 0 || rowid > rows) return NULL;
offset = mdb_get_int16(mdb, 10 + 2 * rowid);
/*
** ??? this happens, don't know what it means
*/
if (offset & 0xF000) return NULL;
/*
for (j=offset;j<offset+32;j++)
fprintf(stdout,"%02x ",mdb->pg_buf[j]);
fprintf(stdout,"\n");
*/
memset(entry, '\0', sizeof(MDB_CATALOG_ENTRY));
entry->object_type = mdb->pg_buf[offset+0x09];
j=0;
for (i=offset+31;isprint(mdb->pg_buf[i]);i++) {
if (j<=MDB_MAX_OBJ_NAME) {
entry->object_name[j++]=mdb->pg_buf[i];
}
}
entry->object_name[j] = '\0';
entry->kkd_pg = mdb_get_int16(mdb,offset+31+strlen(entry->object_name)+7);
entry->kkd_rowid = mdb->pg_buf[offset+31+strlen(entry->object_name)+6];
return entry;
}
int mdb_catalog_rows(MDB_HANDLE *mdb)
{
return mdb_get_int16(mdb, 0x08);
}
void mdb_catalog_dump(MDB_HANDLE *mdb, int obj_type)
{
int off, save_pos;
int rows;
int i,j;
unsigned char *buf;
MDB_CATALOG_ENTRY entry;
mdb_read_pg(mdb, MDB_CATALOG_PG);
rows = mdb_catalog_rows(mdb);
for (i=0;i<rows;i++) {
if (mdb_catalog_entry(mdb, i, &entry)) {
if (obj_type==-1 || obj_type==entry.object_type) {
fprintf(stdout,"Type: %-15s Name: %-30s KKD pg: %04x row: %2d\n",
mdb_get_objtype_string(entry.object_type),
entry.object_name,
entry.kkd_pg,
entry.kkd_rowid);
}
}
}
}

103
src/libmdb/file.c Normal file
View File

@@ -0,0 +1,103 @@
/* MDB Tools - A library for reading MS Access database files
* Copyright (C) 2000 Brian Bruns
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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 Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "mdbtools.h"
MDB_HANDLE *mdb_open(char *filename)
{
MDB_HANDLE *mdb;
mdb = mdb_alloc_handle();
mdb->filename = (char *) malloc(strlen(filename)+1);
strcpy(mdb->filename, filename);
mdb->fd = open(filename,O_RDONLY);
if (mdb->fd==-1) {
fprintf(stderr,"Couldn't open file %s\n",filename);
return NULL;
}
return mdb;
}
/*
** mdb_read a wrapper for read that bails if anything is wrong
*/
size_t mdb_read_pg(MDB_HANDLE *mdb, unsigned long pg)
{
size_t len;
off_t offset = pg * MDB_PGSIZE;
struct stat status;
fstat(mdb->fd, &status);
if (status.st_size < offset) {
fprintf(stderr,"offset %lu is beyond EOF\n",offset);
return 0;
}
lseek(mdb->fd, offset, SEEK_SET);
len = read(mdb->fd,mdb->pg_buf,MDB_PGSIZE);
if (len==-1) {
perror("read");
return 0;
}
else if (len<MDB_PGSIZE) {
fprintf(stderr,"EOF reached.\n");
return 0;
}
return len;
}
int mdb_get_int16(MDB_HANDLE *mdb, int offset)
{
unsigned char *c;
int i;
if (offset < 0 || offset+2 > MDB_PGSIZE) return -1;
c = &mdb->pg_buf[offset];
i = c[1]*256+c[0];
mdb->cur_pos+=2;
return i;
}
long mdb_get_int32(MDB_HANDLE *mdb, int offset)
{
long l;
unsigned char *c;
if (offset <0 || offset+4 > MDB_PGSIZE) return -1;
c = &mdb->pg_buf[offset];
l =c[3]; l<<=8;
l+=c[2]; l<<=8;
l+=c[1]; l<<=8;
l+=c[0];
mdb->cur_pos+=4;
return l;
}
int mdb_set_pos(MDB_HANDLE *mdb, int pos)
{
if (pos<0 || pos >= MDB_PGSIZE) return 0;
mdb->cur_pos=pos;
return pos;
}
int mdb_get_pos(MDB_HANDLE *mdb)
{
return mdb->cur_pos;
}

140
src/libmdb/kkd.c Normal file
View File

@@ -0,0 +1,140 @@
/* MDB Tools - A library for reading MS Access database file
* Copyright (C) 2000 Brian Bruns
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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 Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "mdbtools.h"
/*
** Note: This code is mostly garbage right now...just a test to parse out the
** KKD structures.
*/
void mdb_kkd_dump(MDB_HANDLE *mdb, int rowid)
{
int rows;
int kkd_start, kkd_end;
int i, j, tmp, pos, row_type, hdrpos=0, datapos=0;
int len;
int col_type, col_num, val_len;
int start;
unsigned char c;
rows = mdb_get_int16(mdb,8);
fprintf(stdout,"number of rows = %d\n",rows);
kkd_start = mdb_get_int16(mdb,10+rowid*2);
fprintf(stdout,"kkd start = %d %04x\n",kkd_start,kkd_start);
kkd_end = MDB_PGSIZE;
for (i=0;i<rows;i++) {
tmp = mdb_get_int16(mdb, 10+i*2);
if (tmp<MDB_PGSIZE &&
tmp>kkd_start &&
tmp<kkd_end) {
kkd_end = tmp;
}
}
fprintf(stdout,"kkd end = %d %04x\n",kkd_end,kkd_end);
pos = kkd_start + 4; /* 4 = K K D \0 */
while (pos < kkd_end) {
tmp = mdb_get_int16(mdb,pos);
row_type = mdb_get_int16(mdb,pos+4);
fprintf(stdout,"row size = %3d type = 0x%02x\n",tmp,row_type);
if (row_type==0x80) hdrpos = pos;
if (row_type==0x01) datapos = pos;
pos += tmp;
}
if (hdrpos) {
j=0;
fprintf(stdout,"\nheaders\n");
fprintf(stdout,"-------\n");
len = mdb_get_int16(mdb,hdrpos);
pos = hdrpos + 6;
while (pos < hdrpos+len) {
fprintf(stdout,"%3d ",j++);
tmp = mdb_get_int16(mdb,pos); /* length of string */
pos += 2;
for (i=0;i<tmp;i++)
fprintf(stdout,"%c",mdb->pg_buf[pos+i]);
fprintf(stdout,"\n");
pos += tmp;
}
}
if (datapos) {
fprintf(stdout,"\n data\n");
fprintf(stdout,"-------\n");
len = mdb_get_int16(mdb,datapos);
pos = datapos + 6;
while (pos < datapos+len) {
start = pos;
tmp = mdb_get_int16(mdb,pos); /* length of field */
pos += 2;
col_type = mdb_get_int16(mdb,pos); /* ??? */
pos += 2;
col_num = 0;
if (col_type) {
col_num = mdb_get_int16(mdb,pos);
pos += 2;
}
val_len = mdb_get_int16(mdb,pos);
pos += 2;
fprintf(stdout,"length = %3d %04x %2d %2d ",tmp, col_type, col_num, val_len);
for (i=0;i<val_len;i++) {
c = mdb->pg_buf[pos+i];
if (isprint(c))
fprintf(stdout," %c",c);
else
fprintf(stdout," %02x",c);
}
fprintf(stdout,"\n");
pos = start + tmp;
}
}
}
main(int argc, char **argv)
{
int rows;
int i;
unsigned char buf[2048];
MDB_HANDLE *mdb;
MDB_CATALOG_ENTRY entry;
if (argc<2) {
fprintf(stderr,"Usage: prtable <file> <table>\n");
exit(1);
}
mdb = mdb_open(argv[1]);
mdb_read_pg(mdb, MDB_CATALOG_PG);
rows = mdb_catalog_rows(mdb);
for (i=0;i<rows;i++) {
if (mdb_catalog_entry(mdb, i, &entry)) {
if (!strcmp(entry.object_name,argv[2])) {
mdb_read_pg(mdb, entry.kkd_pg);
mdb_kkd_dump(mdb, entry.kkd_rowid);
}
}
}
mdb_free_handle(mdb);
}

37
src/libmdb/mem.c Normal file
View File

@@ -0,0 +1,37 @@
/* MDB Tools - A library for reading MS Access database files
* Copyright (C) 2000 Brian Bruns
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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 Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "mdbtools.h"
MDB_HANDLE *mdb_alloc_handle()
{
MDB_HANDLE *mdb;
mdb = (MDB_HANDLE *) malloc(sizeof(MDB_HANDLE));
memset(mdb, '\0', sizeof(MDB_HANDLE));
return mdb;
}
void mdb_free_handle(MDB_HANDLE *mdb)
{
if (!mdb) return;
if (mdb->filename) free(mdb->filename);
free(mdb);
}