new mdb-array.c and changes to data.c (Karl)

database password stuff from (Carl)
multiple backend support (Carl and Brian)
This commit is contained in:
brianb
2000-04-02 17:08:30 +00:00
parent 418d57bc0c
commit c12b9ec987
20 changed files with 352 additions and 43 deletions

View File

@@ -67,6 +67,13 @@ enum {
MDB_REPID = 0x0f
};
/* hash to store registered backends */
GHashTable *mdb_backends;
typedef struct {
char **types_table;
} MdbBackend;
typedef struct {
int fd;
char *filename;
@@ -79,6 +86,8 @@ typedef struct {
int pg_size;
guint32 jet_version;
guint32 db_key;
char db_passwd[14];
MdbBackend *default_backend;
} MdbHandle;
typedef struct {
@@ -120,6 +129,8 @@ typedef struct {
} MdbColumn;
/* mem.c */
extern void mdb_init();
extern void mdb_exit();
extern MdbHandle *mdb_alloc_handle();
extern void mdb_free_handle(MdbHandle *mdb);
extern void mdb_free_catalog(MdbHandle *mdb);
@@ -150,6 +161,12 @@ extern char *mdb_col_to_string(MdbHandle *mdb, int start, int datatype, int size
/* dump.c */
void buffer_dump(const unsigned char* buf, int start, int end);
extern void buffer_dump(const unsigned char* buf, int start, int end);
/* backend.c */
extern char *mdb_get_coltype_string(MdbBackend *backend, int col_type);
extern void mdb_init_backends();
extern void mdb_register_backend(MdbBackend *backend, char *backend_name);
extern int mdb_set_default_backend(MdbHandle *mdb, char *backend_name);
#endif /* _mdbtools_h_ */

View File

@@ -20,7 +20,7 @@ INSTALL= @INSTALL@
LIBLIST = libmdb.a
INC = -I ../include `glib-config --cflags`
OBJS = catalog.o mem.o file.o kkd.o table.o data.o dump.o
OBJS = catalog.o mem.o file.o kkd.o table.o data.o dump.o backend.o
all: libmdb

View File

@@ -20,7 +20,7 @@ INSTALL= @INSTALL@
LIBLIST = libmdb.a
INC = -I ../include `glib-config --cflags`
OBJS = catalog.o mem.o file.o kkd.o table.o data.o dump.o
OBJS = catalog.o mem.o file.o kkd.o table.o data.o dump.o backend.o
all: libmdb

128
src/libmdb/backend.c Normal file
View File

@@ -0,0 +1,128 @@
/* 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.
*/
/*
** functions to deal with different backend database engines
*/
#include "mdbtools.h"
/* Access data types */
char *mdb_access_types[] =
{"Unknown 0x00",
"Boolean",
"Byte",
"Integer",
"Long Integer",
"Currency",
"Single",
"Double",
"DateTime (Short)",
"Unknown 0x09",
"Text",
"OLE",
"Memo/Hyperlink",
"Unknown 0x0d",
"Unknown 0x0e",
"Replication ID"};
/* Oracle data types */
char *mdb_oracle_types[] =
{"Oracle_Unknown 0x00",
"CHAR",
"NUMBER",
"NUMBER",
"NUMBER",
"NUMBER",
"NUMBER",
"NUMBER",
"DATE",
"Oracle_Unknown 0x09",
"VARCHAR2",
"BLOB",
"BLOB",
"Oracle_Unknown 0x0d",
"Oracle_Unknown 0x0e",
"NUMBER"};
/* Sybase/MSSQL data types */
char *mdb_sybase_types[] =
{"Sybase_Unknown 0x00",
"bit",
"char",
"smallint",
"int",
"money",
"real",
"float",
"smalldatetime",
"Sybase_Unknown 0x09",
"varchar",
"varbinary",
"text",
"Sybase_Unknown 0x0d",
"Sybase_Unknown 0x0e",
"Sybase_Replication ID"};
char *mdb_get_coltype_string(MdbBackend *backend, int col_type)
{
if (col_type > 0x0f) {
return NULL;
} else {
return backend->types_table[col_type];
}
}
/*
** mdb_init_backends() initializes the mdb_backends hash and loads the builtin
** backends
*/
void mdb_init_backends()
{
MdbBackend *backend;
mdb_backends = g_hash_table_new(g_str_hash, g_str_equal);
backend = (MdbBackend *) g_malloc0(sizeof(MdbBackend));
backend->types_table = mdb_access_types;
mdb_register_backend(backend, "access");
backend = (MdbBackend *) g_malloc0(sizeof(MdbBackend));
backend->types_table = mdb_sybase_types;
mdb_register_backend(backend, "sybase");
backend = (MdbBackend *) g_malloc0(sizeof(MdbBackend));
backend->types_table = mdb_oracle_types;
mdb_register_backend(backend, "oracle");
}
void mdb_register_backend(MdbBackend *backend, char *backend_name)
{
g_hash_table_insert(mdb_backends,backend_name,backend);
}
int mdb_set_default_backend(MdbHandle *mdb, char *backend_name)
{
MdbBackend *backend;
backend = (MdbBackend *) g_hash_table_lookup(mdb_backends, backend_name);
if (backend) {
mdb->default_backend = backend;
return 1;
} else {
return 0;
}
}

View File

@@ -258,14 +258,14 @@ static char text[256];
break;
case MDB_TEXT:
if (size<0) {
return "(oops)";
return "";
}
strncpy(text, &mdb->pg_buf[start], size);
text[size]='\0';
return text;
case MDB_MEMO:
if (size<MDB_MEMO_OVERHEAD) {
return "(oops)";
return "";
}
strncpy(text, &mdb->pg_buf[start + MDB_MEMO_OVERHEAD],
size - MDB_MEMO_OVERHEAD);

View File

@@ -22,6 +22,8 @@
MdbHandle *mdb_open(char *filename)
{
MdbHandle *mdb;
int key[] = {0x86, 0xfb, 0xec, 0x37, 0x5d, 0x44, 0x9c, 0xfa, 0xc6, 0x5e, 0x28, 0xe6, 0x13, 0xb6};
int j,pos;
mdb = mdb_alloc_handle();
mdb->filename = (char *) malloc(strlen(filename)+1);
@@ -47,6 +49,17 @@ MdbHandle *mdb;
mdb->db_key = mdb_get_int32(mdb, 0x3e);
mdb->db_key ^= 0xe15e01b9;
/* get the db password located at 0x42 bytes into the file */
for (pos=0;pos<14;pos++) {
j = mdb_get_int32(mdb,0x42+pos);
j ^= key[pos];
if ( j != 0)
mdb->db_passwd[pos] = j;
else
mdb->db_passwd[pos] = '\0';
}
return mdb;
}

View File

@@ -19,6 +19,17 @@
#include "mdbtools.h"
void mdb_init()
{
mdb_init_backends();
}
void mdb_exit()
{
/* FIX ME -- need to deallocate backend structures */
g_hash_table_destroy(mdb_backends);
}
MdbHandle *mdb_alloc_handle()
{
MdbHandle *mdb;
@@ -26,6 +37,7 @@ MdbHandle *mdb;
mdb = (MdbHandle *) malloc(sizeof(MdbHandle));
memset(mdb, '\0', sizeof(MdbHandle));
mdb->pg_size = MDB_PGSIZE;
mdb_set_default_backend(mdb, "access");
return mdb;
}

View File

@@ -20,38 +20,6 @@
#include "mdbtools.h"
char *mdb_get_coltype_string(int col_type)
{
/*
** These are the Access datatype names, each backend will have to provide
** its own mapping.
*/
static char *type_name[] = {"Unknown 0x00",
"Boolean",
"Byte",
"Integer",
"Long Integer",
"Currency",
"Single",
"Double",
"DateTime (Short)",
"Unknown 0x09",
"Text",
"OLE",
"Memo/Hyperlink",
"Unknown 0x0d",
"Unknown 0x0e",
"Replication ID"
};
if (col_type > 0x0f) {
return NULL;
} else {
return type_name[col_type];
}
}
unsigned char mdb_col_needs_size(int col_type)
{
if (col_type == MDB_TEXT) {
@@ -137,7 +105,7 @@ int i;
fprintf(stdout,"column %d Name: %-20s Type: %s(%d)\n",
i, col->name,
mdb_get_coltype_string(col->col_type),
mdb_get_coltype_string(mdb->default_backend, col->col_type),
col->col_size);
}
}

View File

@@ -20,7 +20,7 @@ LIBS =
INSTALL= @INSTALL@
INC = -I ../include `glib-config --cflags`
LIBS = -L ../libmdb -lmdb `glib-config --libs`
PROGS = prcat prkkd prtable prdata prdump mdb-schema mdb-export mdb-tables mdb-header mdb-parsecsv
PROGS = prcat prkkd prtable prdata prdump mdb-schema mdb-export mdb-tables mdb-header mdb-parsecsv mdb-array
PRCATOBJS = prcat.o
PRKKDOBJS = prkkd.o
PRTABLEOBJS = prtable.o
@@ -31,6 +31,7 @@ EXPORTOBJS = mdb-export.o
HEADEROBJS = header.o
TABLEOBJS = tables.o
CSVOBJS = parsecsv.o
ARROBJS = mdb-array.o
all: $(PROGS)
@@ -64,6 +65,9 @@ mdb-header: $(HEADEROBJS)
mdb-parsecsv: $(CSVOBJS)
$(CC) -g -o $@ $(CSVOBJS) $(LIBS)
mdb-array: $(ARROBJS)
$(CC) -g -o $@ $(ARROBJS) $(LIBS)
clean:
rm -f core *.o $(PROGS)

View File

@@ -20,7 +20,7 @@ LIBS = @LIBS@
INSTALL= @INSTALL@
INC = -I ../include `glib-config --cflags`
LIBS = -L ../libmdb -lmdb `glib-config --libs`
PROGS = prcat prkkd prtable prdata prdump mdb-schema mdb-export mdb-tables mdb-header mdb-parsecsv
PROGS = prcat prkkd prtable prdata prdump mdb-schema mdb-export mdb-tables mdb-header mdb-parsecsv mdb-array
PRCATOBJS = prcat.o
PRKKDOBJS = prkkd.o
PRTABLEOBJS = prtable.o
@@ -31,6 +31,7 @@ EXPORTOBJS = mdb-export.o
HEADEROBJS = header.o
TABLEOBJS = tables.o
CSVOBJS = parsecsv.o
ARROBJS = mdb-array.o
all: $(PROGS)
@@ -64,6 +65,9 @@ mdb-header: $(HEADEROBJS)
mdb-parsecsv: $(CSVOBJS)
$(CC) -g -o $@ $(CSVOBJS) $(LIBS)
mdb-array: $(ARROBJS)
$(CC) -g -o $@ $(ARROBJS) $(LIBS)
clean:
rm -f core *.o $(PROGS)

View File

@@ -45,10 +45,13 @@ FILE *cfile;
exit (1);
}
mdb_init();
/* open the database */
mdb = mdb_open (argv[1]);
if (!mdb) {
mdb_exit();
exit(1);
}
@@ -147,5 +150,6 @@ FILE *cfile;
fclose (cfile);
mdb_free_handle (mdb);
mdb_exit();
}

131
src/util/mdb-array.c Normal file
View File

@@ -0,0 +1,131 @@
/* 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.
*/
/* this is inherited from mdb-export.c, modified to make arrays similar to
those generated by parsecsv.c */
#include "mdbtools.h"
main (int argc, char **argv)
{
int rows;
int i, j;
unsigned char buf [2048];
MdbHandle *mdb;
MdbCatalogEntry entry;
MdbTableDef *table;
MdbColumn *col;
/* doesn't handle tables > 256 columns. Can that happen? */
char *bound_values [256];
char delimiter [] = ", ";
char quote_text = 1;
int count;
int started;
if (argc < 2)
{
fprintf (stderr, "Usage: %s <file> <table>\n", argv [0]);
exit (1);
}
mdb_init();
mdb = mdb_open (argv [1]);
mdb_read_catalog (mdb, MDB_TABLE);
for (i = 0; i < mdb->num_catalog; i++)
{
entry = g_array_index (mdb->catalog, MdbCatalogEntry, i);
if (!strcmp (entry.object_name, argv [2]))
{
table = mdb_read_table (&entry);
mdb_read_columns (table);
mdb_rewind_table (table);
for (j = 0; j < table->num_cols; j++)
{
bound_values [j] = (char *) malloc (256);
bound_values [j] [0] = '\0';
mdb_bind_column (table, j + 1, bound_values [j]);
}
fprintf (stdout, "/******************************************************************/\n");
fprintf (stdout, "/* THIS IS AN AUTOMATICALLY GENERATED FILE. DO NOT EDIT IT!!!!!! */\n");
fprintf (stdout, "/******************************************************************/\n");
fprintf (stdout, "\n");
fprintf (stdout, "#include <stdio.h>\n");
fprintf (stdout, "#include \"types.h\"\n");
fprintf (stdout, "#include \"dump.h\"\n");
fprintf (stdout, "\n");
fprintf (stdout, "const %s %s_array [] = {\n", argv [2], argv [2]);
count = 0;
started = 0;
while (mdb_fetch_row (table))
{
if (started != 0)
{
fprintf (stdout, ",\n");
}
started = 1;
fprintf (stdout, "{\t\t\t\t/* %6d */\n\t", count);
for (j = 0; j < table->num_cols; j++)
{
fprintf (stdout, "\t");
col = g_ptr_array_index (table->columns, j);
if (quote_text &&
(col->col_type == MDB_TEXT ||
col->col_type == MDB_MEMO))
{
fprintf (stdout, "\"%s\"", bound_values [j]);
}
else
{
fprintf (stdout, "%s", bound_values [j]);
}
if (j != table->num_cols - 1)
{
fprintf (stdout, "%s\n", delimiter);
}
else
{
fprintf (stdout, "\n");
}
}
fprintf (stdout, "}");
count++;
}
for (j = 0; j < table->num_cols; j++)
{
free (bound_values [j]);
}
}
}
mdb_free_handle (mdb);
mdb_exit();
fprintf (stdout, "\n};\n");
fprintf (stdout, "\nconst int %s_array_length = %d;\n",
argv [2],
count);
}

View File

@@ -40,6 +40,8 @@ char quote_text = 1;
exit(1);
}
mdb_init();
mdb = mdb_open(argv[1]);
mdb_read_catalog(mdb, MDB_TABLE);
@@ -87,5 +89,6 @@ char quote_text = 1;
}
mdb_free_handle(mdb);
mdb_exit();
}

View File

@@ -35,10 +35,13 @@ MdbHandle *mdb;
exit(1);
}
mdb_init();
mdb = mdb_open(argv[1]);
mdb_dump_catalog(mdb,(argc > 2) ? atoi(argv[2]) : MDB_TABLE);
mdb_free_handle(mdb);
mdb_exit();
}

View File

@@ -36,6 +36,7 @@ GList *l;
exit(1);
}
mdb_init();
mdb = mdb_open(argv[1]);
mdb_read_catalog(mdb, MDB_TABLE);
@@ -50,5 +51,6 @@ GList *l;
}
mdb_free_handle(mdb);
mdb_exit();
}

View File

@@ -37,6 +37,7 @@ int page, start, stop;
exit(1);
}
mdb_init();
mdb = mdb_open(argv[1]);
mdb_read_catalog(mdb, MDB_TABLE);
@@ -51,5 +52,6 @@ int page, start, stop;
}
mdb_free_handle(mdb);
mdb_exit();
}

View File

@@ -32,6 +32,7 @@ MdbCatalogEntry entry;
exit(1);
}
mdb_init();
mdb = mdb_open(argv[1]);
mdb_read_pg(mdb, MDB_CATALOG_PG);
@@ -46,5 +47,6 @@ MdbCatalogEntry entry;
}
mdb_free_handle(mdb);
mdb_exit();
}

View File

@@ -30,11 +30,12 @@ MdbCatalogEntry entry;
GList *l;
if (argc<2) {
if (argc<3) {
fprintf(stderr,"Usage: %s <file> <table>\n",argv[0]);
exit(1);
}
mdb_init();
mdb = mdb_open(argv[1]);
mdb_read_catalog(mdb, MDB_TABLE);
@@ -47,5 +48,6 @@ GList *l;
}
mdb_free_handle(mdb);
mdb_exit();
}

View File

@@ -29,13 +29,22 @@ MdbTableDef *table;
MdbColumn *col;
if (argc < 2) {
fprintf (stderr, "Usage: %s <file>\n",argv[0]);
fprintf (stderr, "Usage: %s <file> [<backend>]\n",argv[0]);
exit (1);
}
mdb_init();
/* open the database */
mdb = mdb_open (argv[1]);
if (argc>2) {
if (!mdb_set_default_backend(mdb, argv[2])) {
fprintf(stderr,"Invalid backend type\n");
mdb_exit();
exit(1);
}
}
/* read the catalog */
@@ -78,7 +87,7 @@ MdbColumn *col;
col = g_ptr_array_index (table->columns, k);
fprintf (stdout, "\t%s\t\t\t%s", col->name,
mdb_get_coltype_string (col->col_type));
mdb_get_coltype_string (mdb->default_backend, col->col_type));
if (col->col_size != 0)
fprintf (stdout, " (%d)", col->col_size);
@@ -98,5 +107,6 @@ MdbColumn *col;
}
mdb_free_handle (mdb);
mdb_exit();
}

View File

@@ -33,6 +33,9 @@ MdbColumn *col;
exit (1);
}
/* initialize the library */
mdb_init();
/* open the database */
mdb = mdb_open (argv[1]);
@@ -68,5 +71,6 @@ MdbColumn *col;
fprintf (stdout, "\n");
mdb_free_handle (mdb);
mdb_exit();
}