1 /* MDB Tools - A library for reading MS Access database file
2  * Copyright (C) 2000 Brian Bruns
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA  02110-1301, USA.
18  */
19 
20 #include "mdbtools.h"
21 
22 #ifdef DMALLOC
23 #include "dmalloc.h"
24 #endif
25 
26 char *
mdb_get_objtype_string(int obj_type)27 mdb_get_objtype_string(int obj_type)
28 {
29 static char *type_name[] = {"Form",
30 			"Table",
31 			"Macro",
32 			"System Table",
33 			"Report",
34 			"Query",
35 			"Linked Table",
36 			"Module",
37 			"Relationship",
38 			"Unknown 0x09",
39 			"Unknown 0x0a",
40 			"Database"
41 		};
42 
43 	if (obj_type > 11) {
44 		return NULL;
45 	} else {
46 		return type_name[obj_type];
47 	}
48 }
49 
mdb_free_catalog(MdbHandle * mdb)50 void mdb_free_catalog(MdbHandle *mdb)
51 {
52 	unsigned int i;
53 
54 	if ((!mdb) || (!mdb->catalog)) return;
55 	for (i=0; i<mdb->catalog->len; i++)
56 		g_free (g_ptr_array_index(mdb->catalog, i));
57 	g_ptr_array_free(mdb->catalog, TRUE);
58 	mdb->catalog = NULL;
59 }
60 
mdb_read_catalog(MdbHandle * mdb,int objtype)61 GPtrArray *mdb_read_catalog (MdbHandle *mdb, int objtype)
62 {
63 	MdbCatalogEntry *entry, msysobj;
64 	MdbTableDef *table;
65 	char obj_id[256];
66 	char obj_name[256];
67 	char obj_type[256];
68 	char obj_flags[256];
69 	int type;
70 
71 	if (!mdb) return NULL;
72 	if (mdb->catalog) mdb_free_catalog(mdb);
73 	mdb->catalog = g_ptr_array_new();
74 	mdb->num_catalog = 0;
75 
76 	/* dummy up a catalog entry so we may read the table def */
77 	memset(&msysobj, 0, sizeof(MdbCatalogEntry));
78 	msysobj.mdb = mdb;
79 	msysobj.object_type = MDB_TABLE;
80 	msysobj.table_pg = 2;
81 	strcpy(msysobj.object_name, "MSysObjects");
82 
83 	/* mdb_table_dump(&msysobj); */
84 
85 	table = mdb_read_table(&msysobj);
86 	if (!table) return NULL;
87 
88 	mdb_read_columns(table);
89 
90 	mdb_bind_column_by_name(table, "Id", obj_id, NULL);
91 	mdb_bind_column_by_name(table, "Name", obj_name, NULL);
92 	mdb_bind_column_by_name(table, "Type", obj_type, NULL);
93 	mdb_bind_column_by_name(table, "Flags", obj_flags, NULL);
94 
95 	mdb_rewind_table(table);
96 
97 	while (mdb_fetch_row(table)) {
98 		type = atoi(obj_type);
99 		if (objtype==MDB_ANY || type == objtype) {
100 			// fprintf(stdout, "obj_id: %10ld objtype: %-3d obj_name: %s\n",
101 			// (atol(obj_id) & 0x00FFFFFF), type, obj_name);
102 			entry = (MdbCatalogEntry *) g_malloc0(sizeof(MdbCatalogEntry));
103 			entry->mdb = mdb;
104 			strcpy(entry->object_name, obj_name);
105 			entry->object_type = (type & 0x7F);
106 			entry->table_pg = atol(obj_id) & 0x00FFFFFF;
107 			entry->flags = atol(obj_flags);
108 			mdb->num_catalog++;
109 			g_ptr_array_add(mdb->catalog, entry);
110 		}
111 	}
112 	//mdb_dump_catalog(mdb, MDB_TABLE);
113 
114 	mdb_free_tabledef(table);
115 
116 	return mdb->catalog;
117 }
118 
119 void
mdb_dump_catalog(MdbHandle * mdb,int obj_type)120 mdb_dump_catalog(MdbHandle *mdb, int obj_type)
121 {
122 	unsigned int i;
123 	MdbCatalogEntry *entry;
124 
125 	mdb_read_catalog(mdb, obj_type);
126 	for (i=0;i<mdb->num_catalog;i++) {
127                 entry = g_ptr_array_index(mdb->catalog,i);
128 		if (obj_type==MDB_ANY || entry->object_type==obj_type) {
129 			fprintf(stdout,"Type: %-10s Name: %-18s T pg: %04x KKD pg: %04x row: %2d\n",
130 			mdb_get_objtype_string(entry->object_type),
131 			entry->object_name,
132 			(unsigned int) entry->table_pg,
133 			(unsigned int) entry->kkd_pg,
134 			entry->kkd_rowid);
135 		}
136         }
137 	return;
138 }
139 
140