mdbtools/src/libmdb/file.c

379 lines
9.3 KiB
C
Raw Normal View History

2000-02-13 07:51:37 +08:00
/* 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"
#ifdef DMALLOC
#include "dmalloc.h"
#endif
/*
typedef struct {
int pg_size;
guint16 row_count_offset;
guint16 tab_num_rows_offset;
guint16 tab_num_cols_offset;
guint16 tab_num_idxs_offset;
guint16 tab_num_ridxs_offset;
guint16 tab_usage_map_offset;
guint16 tab_first_dpg_offset;
guint16 tab_cols_start_offset;
guint16 tab_ridx_entry_size;
guint16 col_fixed_offset;
guint16 col_size_offset;
guint16 col_num_offset;
guint16 tab_col_entry_size;
guint16 tab_free_map_offset;
guint16 tab_col_offset_var;
guint16 tab_col_offset_fixed;
guint16 tab_row_col_num_offset;
} MdbFormatConstants;
*/
2003-01-02 06:29:39 +08:00
MdbFormatConstants MdbJet4Constants = {
4096, 0x0c, 16, 45, 47, 51, 55, 56, 63, 12, 15, 23, 5, 25, 59, 7, 21, 9
2003-01-02 06:29:39 +08:00
};
MdbFormatConstants MdbJet3Constants = {
2048, 0x08, 12, 25, 27, 31, 35, 36, 43, 8, 13, 16, 1, 18, 39, 3, 14, 5
2003-01-02 06:29:39 +08:00
};
static ssize_t _mdb_read_pg(MdbHandle *mdb, unsigned char *pg_buf, unsigned long pg);
2004-06-01 02:21:43 +08:00
/**
* mdb_find_file:
* @filename: path to MDB (database) file
*
2004-06-22 12:27:42 +08:00
* Finds and returns the absolute path to an MDB file. Function will first try
* to fstat file as passed, then search through the $MDBPATH if not found.
*
* Return value: gchar pointer to absolute path. Caller is responsible for
* freeing.
**/
2004-06-01 02:21:43 +08:00
static gchar *mdb_find_file(char *file_name)
{
2004-06-01 02:21:43 +08:00
struct stat status;
gchar *mdbpath, **dir, *tmpfname;
unsigned int i = 0;
/* try the provided file name first */
if (!stat(file_name, &status)) {
2004-06-01 02:21:43 +08:00
return g_strdup(file_name);
}
/* Now pull apart $MDBPATH and try those */
2004-06-01 02:21:43 +08:00
mdbpath = (gchar *) getenv("MDBPATH");
/* no path, can't find file */
if (!mdbpath || !strlen(mdbpath)) return NULL;
2004-06-01 02:21:43 +08:00
dir = g_strsplit(mdbpath, ":", 0);
while (dir[i]) {
if (!strlen(dir[i])) continue;
tmpfname = g_strconcat(dir[i++], "/", file_name, NULL);
if (!stat(tmpfname, &status)) {
2004-06-01 02:21:43 +08:00
g_strfreev(dir);
return tmpfname;
}
g_free(tmpfname);
2004-06-01 02:21:43 +08:00
}
g_strfreev(dir);
return NULL;
}
2004-04-14 04:06:04 +08:00
/**
* mdb_open:
* @filename: path to MDB (database) file
* @flags: MDB_NOFLAGS for read-only, MDB_WRITABLE for read/write
*
* Opens an MDB file and returns an MdbHandle to it. MDB File may be relative
* to the current directory, a full path to the file, or relative to a
* component of $MDBPATH.
*
* Return value: pointer to MdbHandle structure.
**/
MdbHandle *mdb_open(char *filename, MdbFileFlags flags)
2000-02-13 07:51:37 +08:00
{
2004-06-01 02:21:43 +08:00
MdbHandle *mdb;
2000-02-13 07:51:37 +08:00
2004-05-30 13:06:26 +08:00
mdb = (MdbHandle *) g_malloc0(sizeof(MdbHandle));
mdb_set_default_backend(mdb, "access");
2003-01-02 06:29:39 +08:00
/* need something to bootstrap with, reassign after page 0 is read */
mdb->fmt = &MdbJet3Constants;
2004-06-01 02:21:43 +08:00
mdb->f = (MdbFile *) g_malloc0(sizeof(MdbFile));
2004-06-02 20:31:18 +08:00
mdb->f->refs = 1;
mdb->f->fd = -1;
2004-06-01 02:21:43 +08:00
mdb->f->filename = (char *) mdb_find_file(filename);
if (!mdb->f->filename) {
fprintf(stderr, "Can't alloc filename\n");
2004-06-02 20:31:18 +08:00
mdb_close(mdb);
2004-06-01 02:21:43 +08:00
return NULL;
}
2004-04-14 04:06:04 +08:00
if (flags & MDB_WRITABLE) {
2004-06-01 02:21:43 +08:00
mdb->f->writable = TRUE;
mdb->f->fd = open(mdb->f->filename,O_RDWR);
2003-01-02 06:29:39 +08:00
} else {
2004-06-01 02:21:43 +08:00
mdb->f->fd = open(mdb->f->filename,O_RDONLY);
2003-01-02 06:29:39 +08:00
}
2000-02-13 07:51:37 +08:00
2004-06-01 02:21:43 +08:00
if (mdb->f->fd==-1) {
fprintf(stderr,"Couldn't open file %s\n",mdb->f->filename);
2004-06-02 20:31:18 +08:00
mdb_close(mdb);
2000-02-13 07:51:37 +08:00
return NULL;
}
2000-03-22 10:12:31 +08:00
if (!mdb_read_pg(mdb, 0)) {
fprintf(stderr,"Couldn't read first page.\n");
2004-06-02 20:31:18 +08:00
mdb_close(mdb);
2000-03-22 10:12:31 +08:00
return NULL;
}
if (mdb->pg_buf[0] != 0) {
2004-06-02 20:31:18 +08:00
mdb_close(mdb);
return NULL;
}
2004-06-01 02:21:43 +08:00
mdb->f->jet_version = mdb_pg_get_int32(mdb, 0x14);
2003-01-02 06:29:39 +08:00
if (IS_JET4(mdb)) {
mdb->fmt = &MdbJet4Constants;
} else if (IS_JET3(mdb)) {
2003-01-02 06:29:39 +08:00
mdb->fmt = &MdbJet3Constants;
} else {
fprintf(stderr,"Unknown Jet version.\n");
2004-06-02 20:31:18 +08:00
mdb_close(mdb);
return NULL;
2000-03-22 10:12:31 +08:00
}
2004-11-26 13:22:52 +08:00
mdb_iconv_init(mdb);
2000-03-22 10:12:31 +08:00
2000-02-13 07:51:37 +08:00
return mdb;
}
2004-04-14 04:06:04 +08:00
2004-04-13 08:39:12 +08:00
/**
2004-04-14 04:06:04 +08:00
* mdb_close:
* @mdb: Handle to open MDB database file
2004-04-13 08:39:12 +08:00
*
2004-04-14 04:06:04 +08:00
* Dereferences MDB file, closes if reference count is 0, and destroys handle.
2004-04-13 08:39:12 +08:00
*
**/
void
mdb_close(MdbHandle *mdb)
{
if (!mdb) return;
mdb_free_catalog(mdb);
2004-08-19 13:12:21 +08:00
g_free(mdb->stats);
g_free(mdb->backend_name);
2003-01-02 06:29:39 +08:00
if (mdb->f) {
if (mdb->f->refs > 1) {
mdb->f->refs--;
} else {
2004-06-02 20:31:18 +08:00
if (mdb->f->fd != -1) close(mdb->f->fd);
2004-05-30 13:06:26 +08:00
g_free(mdb->f->filename);
g_free(mdb->f);
}
}
2004-11-26 13:22:52 +08:00
mdb_iconv_close(mdb);
2004-05-30 13:06:26 +08:00
g_free(mdb);
}
2004-04-14 04:06:04 +08:00
/**
* mdb_clone_handle:
* @mdb: Handle to open MDB database file
*
* Clones an existing database handle. Cloned handle shares the file descriptor
* but has its own page buffer, page position, and similar internal variables.
2004-04-14 04:06:04 +08:00
*
* Return value: new handle to the database.
*/
MdbHandle *mdb_clone_handle(MdbHandle *mdb)
{
MdbHandle *newmdb;
2004-04-24 13:53:05 +08:00
MdbCatalogEntry *entry, *data;
2004-07-09 20:47:04 +08:00
unsigned int i;
2004-05-30 13:06:26 +08:00
newmdb = (MdbHandle *) g_memdup(mdb, sizeof(MdbHandle));
newmdb->stats = NULL;
newmdb->catalog = g_ptr_array_new();
for (i=0;i<mdb->num_catalog;i++) {
entry = g_ptr_array_index(mdb->catalog,i);
2004-04-24 13:53:05 +08:00
data = g_memdup(entry,sizeof(MdbCatalogEntry));
g_ptr_array_add(newmdb->catalog, data);
}
mdb->backend_name = NULL;
if (mdb->f) {
mdb->f->refs++;
}
2004-11-26 13:22:52 +08:00
mdb_iconv_init(mdb);
return newmdb;
}
2000-02-13 07:51:37 +08:00
/*
** mdb_read a wrapper for read that bails if anything is wrong
*/
ssize_t mdb_read_pg(MdbHandle *mdb, unsigned long pg)
2000-02-13 07:51:37 +08:00
{
ssize_t len;
if (pg && mdb->cur_pg == pg) return mdb->fmt->pg_size;
len = _mdb_read_pg(mdb, mdb->pg_buf, pg);
//fprintf(stderr, "read page %d type %02x\n", pg, mdb->pg_buf[0]);
mdb->cur_pg = pg;
/* kan - reset the cur_pos on a new page read */
mdb->cur_pos = 0; /* kan */
return len;
}
ssize_t mdb_read_alt_pg(MdbHandle *mdb, unsigned long pg)
{
ssize_t len;
len = _mdb_read_pg(mdb, mdb->alt_pg_buf, pg);
return len;
}
static ssize_t _mdb_read_pg(MdbHandle *mdb, unsigned char *pg_buf, unsigned long pg)
{
ssize_t len;
struct stat status;
off_t offset = pg * mdb->fmt->pg_size;
2000-02-13 07:51:37 +08:00
2003-01-02 06:29:39 +08:00
fstat(mdb->f->fd, &status);
2000-02-13 07:51:37 +08:00
if (status.st_size < offset) {
fprintf(stderr,"offset %lu is beyond EOF\n",offset);
return 0;
}
if (mdb->stats && mdb->stats->collect)
mdb->stats->pg_reads++;
2003-01-02 06:29:39 +08:00
lseek(mdb->f->fd, offset, SEEK_SET);
len = read(mdb->f->fd,pg_buf,mdb->fmt->pg_size);
2000-02-13 07:51:37 +08:00
if (len==-1) {
perror("read");
return 0;
}
2003-01-02 06:29:39 +08:00
else if (len<mdb->fmt->pg_size) {
/* fprintf(stderr,"EOF reached %d bytes returned.\n",len, mdb->fmt->pg_size); */
2000-02-13 07:51:37 +08:00
return 0;
}
2000-02-13 07:51:37 +08:00
return len;
}
2002-12-11 07:35:24 +08:00
void mdb_swap_pgbuf(MdbHandle *mdb)
2002-04-22 12:54:59 +08:00
{
char tmpbuf[MDB_PGSIZE];
memcpy(tmpbuf,mdb->pg_buf, MDB_PGSIZE);
memcpy(mdb->pg_buf,mdb->alt_pg_buf, MDB_PGSIZE);
memcpy(mdb->alt_pg_buf,tmpbuf,MDB_PGSIZE);
}
2004-08-20 10:43:21 +08:00
/* really stupid, just here for consistancy */
unsigned char mdb_get_byte(unsigned char *buf, int offset)
{
return buf[offset];
}
unsigned char mdb_pg_get_byte(MdbHandle *mdb, int offset)
{
2004-08-20 10:43:21 +08:00
if (offset < 0 || offset+1 > mdb->fmt->pg_size) return -1;
mdb->cur_pos++;
2004-08-20 10:43:21 +08:00
return mdb->pg_buf[offset];
}
2004-08-20 10:43:21 +08:00
int mdb_get_int16(unsigned char *buf, int offset)
2003-01-02 06:29:39 +08:00
{
return buf[offset+1]*256+buf[offset];
}
2004-08-20 10:43:21 +08:00
int mdb_pg_get_int16(MdbHandle *mdb, int offset)
2000-02-13 07:51:37 +08:00
{
2003-01-02 06:29:39 +08:00
if (offset < 0 || offset+2 > mdb->fmt->pg_size) return -1;
2000-02-13 07:51:37 +08:00
mdb->cur_pos+=2;
2004-08-20 10:43:21 +08:00
return mdb_get_int16(mdb->pg_buf, offset);
2000-02-13 07:51:37 +08:00
}
2004-08-20 10:43:21 +08:00
gint32 mdb_pg_get_int24_msb(MdbHandle *mdb, int offset)
{
gint32 l = 0;
if (offset <0 || offset+3 > mdb->fmt->pg_size) return -1;
mdb->cur_pos+=3;
2004-08-20 10:43:21 +08:00
memcpy(&l, &(mdb->pg_buf[offset]), 3);
return GINT32_FROM_BE(l);
}
2004-08-20 10:43:21 +08:00
gint32 mdb_get_int24(unsigned char *buf, int offset)
2002-04-22 12:54:59 +08:00
{
2004-08-20 10:43:21 +08:00
gint32 l = 0;
memcpy(&l, &buf[offset], 3);
return GINT32_FROM_LE(l);
}
gint32 mdb_pg_get_int24(MdbHandle *mdb, int offset)
{
2004-08-20 10:43:21 +08:00
if (offset <0 || offset+3 > mdb->fmt->pg_size) return -1;
2002-04-22 12:54:59 +08:00
mdb->cur_pos+=3;
2004-08-20 10:43:21 +08:00
return mdb_get_int24(mdb->pg_buf, offset);
2002-04-22 12:54:59 +08:00
}
2004-08-20 10:43:21 +08:00
long mdb_get_int32(unsigned char *buf, int offset)
2000-02-13 07:51:37 +08:00
{
2004-08-20 10:43:21 +08:00
guint32 l;
memcpy(&l, &buf[offset], 4);
return (long)GINT32_FROM_LE(l);
2002-12-20 14:17:41 +08:00
}
long mdb_pg_get_int32(MdbHandle *mdb, int offset)
2002-12-20 14:17:41 +08:00
{
2003-01-02 06:29:39 +08:00
if (offset <0 || offset+4 > mdb->fmt->pg_size) return -1;
2000-02-13 07:51:37 +08:00
mdb->cur_pos+=4;
2004-08-20 10:43:21 +08:00
return mdb_get_int32(mdb->pg_buf, offset);
2000-02-13 07:51:37 +08:00
}
2004-08-20 10:43:21 +08:00
float mdb_get_single(unsigned char *buf, int offset)
{
union {guint32 g; float f;} f;
2004-08-20 10:43:21 +08:00
memcpy(&f, &buf[offset], 4);
f.g = GUINT32_FROM_LE(f.g);
return f.f;
}
2004-08-20 10:43:21 +08:00
float mdb_pg_get_single(MdbHandle *mdb, int offset)
{
if (offset <0 || offset+4 > mdb->fmt->pg_size) return -1;
mdb->cur_pos+=4;
return mdb_get_single(mdb->pg_buf, offset);
}
2004-08-20 10:43:21 +08:00
double mdb_get_double(unsigned char *buf, int offset)
{
union {guint64 g; double d;} d;
memcpy(&d, &buf[offset], 8);
d.g = GUINT64_FROM_LE(d.g);
return d.d;
}
2004-08-20 10:43:21 +08:00
double mdb_pg_get_double(MdbHandle *mdb, int offset)
{
2004-01-06 08:42:07 +08:00
if (offset <0 || offset+8 > mdb->fmt->pg_size) return -1;
mdb->cur_pos+=8;
return mdb_get_double(mdb->pg_buf, offset);
}
2004-08-20 10:43:21 +08:00
int
mdb_set_pos(MdbHandle *mdb, int pos)
2000-02-13 07:51:37 +08:00
{
2003-01-02 06:29:39 +08:00
if (pos<0 || pos >= mdb->fmt->pg_size) return 0;
2000-02-13 07:51:37 +08:00
mdb->cur_pos=pos;
return pos;
}
2000-02-28 03:24:44 +08:00
int mdb_get_pos(MdbHandle *mdb)
2000-02-13 07:51:37 +08:00
{
return mdb->cur_pos;
}