mirror of
https://github.com/mdbtools/mdbtools.git
synced 2025-09-19 18:47:54 +08:00
Fix and tidy up read_pg_in* functions
This commit is contained in:
@@ -1,5 +1,9 @@
|
|||||||
Sat Dec 17 03:49:30 CST 2005 Jeff Smith <whydoubt@yahoo.com>
|
Sat Dec 17 03:49:30 CST 2005 Jeff Smith <whydoubt@yahoo.com>
|
||||||
* src/util/mdb-sql.c: Add fflush after each newline (Pedro Gutierrez)
|
* src/util/mdb-sql.c: Add fflush after each newline (Pedro Gutierrez)
|
||||||
|
* src/libmdb/table.c: Bugfix to read_pg_if_n (Mike Prudence)
|
||||||
|
* src/libmdb/table.c:
|
||||||
|
* src/libmdb/index.c:
|
||||||
|
* include/mdbtools.h: Tidy up read_pg_in* functions
|
||||||
|
|
||||||
Wed Nov 9 07:23:01 CST 2005 Jeff Smith <whydoubt@yahoo.com>
|
Wed Nov 9 07:23:01 CST 2005 Jeff Smith <whydoubt@yahoo.com>
|
||||||
* src/util/mdb-array.c:
|
* src/util/mdb-array.c:
|
||||||
|
@@ -426,10 +426,10 @@ extern void mdb_append_column(GPtrArray *columns, MdbColumn *in_col);
|
|||||||
extern void mdb_free_columns(GPtrArray *columns);
|
extern void mdb_free_columns(GPtrArray *columns);
|
||||||
extern GPtrArray *mdb_read_columns(MdbTableDef *table);
|
extern GPtrArray *mdb_read_columns(MdbTableDef *table);
|
||||||
extern void mdb_table_dump(MdbCatalogEntry *entry);
|
extern void mdb_table_dump(MdbCatalogEntry *entry);
|
||||||
|
extern guint8 read_pg_if_8(MdbHandle *mdb, int *cur_pos);
|
||||||
extern guint16 read_pg_if_16(MdbHandle *mdb, int *cur_pos);
|
extern guint16 read_pg_if_16(MdbHandle *mdb, int *cur_pos);
|
||||||
extern guint32 read_pg_if_32(MdbHandle *mdb, int *cur_pos);
|
extern guint32 read_pg_if_32(MdbHandle *mdb, int *cur_pos);
|
||||||
extern int read_pg_if(MdbHandle *mdb, int *cur_pos, int offset);
|
extern void *read_pg_if_n(MdbHandle *mdb, void *buf, int *cur_pos, size_t len);
|
||||||
extern guint16 read_pg_if_n(MdbHandle *mdb, void *buf, int *cur_pos, int len);
|
|
||||||
extern int mdb_is_user_table(MdbCatalogEntry *entry);
|
extern int mdb_is_user_table(MdbCatalogEntry *entry);
|
||||||
extern int mdb_is_system_table(MdbCatalogEntry *entry);
|
extern int mdb_is_system_table(MdbCatalogEntry *entry);
|
||||||
|
|
||||||
|
@@ -90,7 +90,6 @@ mdb_read_indices(MdbTableDef *table)
|
|||||||
tmpbuf = (gchar *) g_malloc(idx2_sz);
|
tmpbuf = (gchar *) g_malloc(idx2_sz);
|
||||||
for (i=0;i<table->num_idxs;i++) {
|
for (i=0;i<table->num_idxs;i++) {
|
||||||
read_pg_if_n(mdb, tmpbuf, &cur_pos, idx2_sz);
|
read_pg_if_n(mdb, tmpbuf, &cur_pos, idx2_sz);
|
||||||
cur_pos += idx2_sz;
|
|
||||||
pidx = (MdbIndex *) g_malloc0(sizeof(MdbIndex));
|
pidx = (MdbIndex *) g_malloc0(sizeof(MdbIndex));
|
||||||
pidx->table = table;
|
pidx->table = table;
|
||||||
pidx->index_num = mdb_get_int16(tmpbuf, 4);
|
pidx->index_num = mdb_get_int16(tmpbuf, 4);
|
||||||
@@ -103,14 +102,11 @@ mdb_read_indices(MdbTableDef *table)
|
|||||||
pidx = g_ptr_array_index (table->indices, i);
|
pidx = g_ptr_array_index (table->indices, i);
|
||||||
if (IS_JET4(mdb)) {
|
if (IS_JET4(mdb)) {
|
||||||
name_sz=read_pg_if_16(mdb, &cur_pos);
|
name_sz=read_pg_if_16(mdb, &cur_pos);
|
||||||
cur_pos += 2;
|
|
||||||
} else {
|
} else {
|
||||||
read_pg_if(mdb, &cur_pos, 0);
|
name_sz=read_pg_if_8(mdb, &cur_pos);
|
||||||
name_sz=mdb->pg_buf[cur_pos++];
|
|
||||||
}
|
}
|
||||||
tmpbuf = g_malloc(name_sz);
|
tmpbuf = g_malloc(name_sz);
|
||||||
read_pg_if_n(mdb, tmpbuf, &cur_pos, name_sz);
|
read_pg_if_n(mdb, tmpbuf, &cur_pos, name_sz);
|
||||||
cur_pos += name_sz;
|
|
||||||
mdb_unicode2ascii(mdb, tmpbuf, name_sz, pidx->name, MDB_MAX_OBJ_NAME);
|
mdb_unicode2ascii(mdb, tmpbuf, name_sz, pidx->name, MDB_MAX_OBJ_NAME);
|
||||||
g_free(tmpbuf);
|
g_free(tmpbuf);
|
||||||
//fprintf(stderr, "index name %s\n", pidx->name);
|
//fprintf(stderr, "index name %s\n", pidx->name);
|
||||||
@@ -142,24 +138,21 @@ mdb_read_indices(MdbTableDef *table)
|
|||||||
key_num=0;
|
key_num=0;
|
||||||
for (j=0;j<MDB_MAX_IDX_COLS;j++) {
|
for (j=0;j<MDB_MAX_IDX_COLS;j++) {
|
||||||
col_num=read_pg_if_16(mdb,&cur_pos);
|
col_num=read_pg_if_16(mdb,&cur_pos);
|
||||||
cur_pos += 2;
|
if (col_num == 0xFFFF) {
|
||||||
read_pg_if(mdb, &cur_pos, 0);
|
cur_pos++;
|
||||||
cur_pos++;
|
|
||||||
if (col_num == 0xFFFF)
|
|
||||||
continue;
|
continue;
|
||||||
|
}
|
||||||
/* set column number to a 1 based column number and store */
|
/* set column number to a 1 based column number and store */
|
||||||
pidx->key_col_num[key_num] = col_num + 1;
|
pidx->key_col_num[key_num] = col_num + 1;
|
||||||
pidx->key_col_order[key_num] =
|
pidx->key_col_order[key_num] =
|
||||||
(mdb->pg_buf[cur_pos-1]) ? MDB_ASC : MDB_DESC;
|
(read_pg_if_8(mdb, &cur_pos)) ? MDB_ASC : MDB_DESC;
|
||||||
key_num++;
|
key_num++;
|
||||||
}
|
}
|
||||||
pidx->num_keys = key_num;
|
pidx->num_keys = key_num;
|
||||||
|
|
||||||
cur_pos += 4;
|
cur_pos += 4;
|
||||||
pidx->first_pg = read_pg_if_32(mdb, &cur_pos);
|
pidx->first_pg = read_pg_if_32(mdb, &cur_pos);
|
||||||
cur_pos += 4;
|
pidx->flags = read_pg_if_8(mdb, &cur_pos);
|
||||||
read_pg_if(mdb, &cur_pos, 0);
|
|
||||||
pidx->flags = mdb->pg_buf[cur_pos++];
|
|
||||||
if (IS_JET4(mdb)) cur_pos += 9;
|
if (IS_JET4(mdb)) cur_pos += 9;
|
||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@@ -128,60 +128,63 @@ MdbTableDef *mdb_read_table_by_name(MdbHandle *mdb, gchar *table_name, int obj_t
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
** read the next page if offset is > pg_size
|
|
||||||
** return true if page was read
|
|
||||||
*/
|
|
||||||
int
|
|
||||||
read_pg_if(MdbHandle *mdb, int *cur_pos, int offset)
|
|
||||||
{
|
|
||||||
if (*cur_pos + offset >= mdb->fmt->pg_size) {
|
|
||||||
mdb_read_pg(mdb, mdb_get_int32(mdb->pg_buf,4));
|
|
||||||
*cur_pos = 8 - (mdb->fmt->pg_size - (*cur_pos));
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
guint32
|
guint32
|
||||||
read_pg_if_32(MdbHandle *mdb, int *cur_pos)
|
read_pg_if_32(MdbHandle *mdb, int *cur_pos)
|
||||||
{
|
{
|
||||||
unsigned char c[4];
|
char c[4];
|
||||||
int i, rc = 0;
|
|
||||||
|
|
||||||
for (i=0;i<4;i++) {
|
read_pg_if_n(mdb, c, cur_pos, 4);
|
||||||
rc += read_pg_if(mdb, cur_pos, i);
|
|
||||||
c[i] = mdb->pg_buf[(*cur_pos) + i];
|
|
||||||
}
|
|
||||||
return mdb_get_int32(c, 0);
|
return mdb_get_int32(c, 0);
|
||||||
}
|
}
|
||||||
guint16
|
guint16
|
||||||
read_pg_if_16(MdbHandle *mdb, int *cur_pos)
|
read_pg_if_16(MdbHandle *mdb, int *cur_pos)
|
||||||
{
|
{
|
||||||
unsigned char low_byte, high_byte;
|
char c[2];
|
||||||
int rc = 0;
|
|
||||||
|
|
||||||
rc += read_pg_if(mdb, cur_pos, 0);
|
read_pg_if_n(mdb, c, cur_pos, 2);
|
||||||
low_byte = mdb->pg_buf[*cur_pos];
|
return mdb_get_int16(c, 0);
|
||||||
rc += read_pg_if(mdb, cur_pos, 1);
|
|
||||||
high_byte = mdb->pg_buf[(*cur_pos) + 1];
|
|
||||||
|
|
||||||
return (high_byte * 256 + low_byte);
|
|
||||||
}
|
}
|
||||||
guint16
|
guint8
|
||||||
read_pg_if_n(MdbHandle *mdb, void *buf, int *cur_pos, int len)
|
read_pg_if_8(MdbHandle *mdb, int *cur_pos)
|
||||||
{
|
{
|
||||||
if (*cur_pos + len < mdb->fmt->pg_size) {
|
guint8 c;
|
||||||
memcpy(buf, &mdb->pg_buf[*cur_pos], len);
|
|
||||||
return 0;
|
read_pg_if_n(mdb, &c, cur_pos, 1);
|
||||||
} else {
|
return c;
|
||||||
int half = mdb->fmt->pg_size - *cur_pos;
|
|
||||||
memcpy(buf, &mdb->pg_buf[*cur_pos], half);
|
|
||||||
mdb_read_pg(mdb, mdb_get_int32(mdb->pg_buf,4));
|
|
||||||
memcpy(buf + half, &mdb->pg_buf[8], len - half);
|
|
||||||
*cur_pos = 8 - half;
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
/*
|
||||||
|
* Read data into a buffer, advancing pages and setting the
|
||||||
|
* page cursor as needed. In the case that buf in NULL, pages
|
||||||
|
* are still advanced and the page cursor is still updated.
|
||||||
|
*/
|
||||||
|
void *
|
||||||
|
read_pg_if_n(MdbHandle *mdb, void *buf, int *cur_pos, size_t len)
|
||||||
|
{
|
||||||
|
/* Advance to page which contains the first byte */
|
||||||
|
while (*cur_pos >= mdb->fmt->pg_size) {
|
||||||
|
mdb_read_pg(mdb, mdb_get_int32(mdb->pg_buf,4));
|
||||||
|
*cur_pos -= (mdb->fmt->pg_size - 8);
|
||||||
|
}
|
||||||
|
/* Copy pages into buffer */
|
||||||
|
while (*cur_pos + len >= mdb->fmt->pg_size) {
|
||||||
|
int piece_len = mdb->fmt->pg_size - *cur_pos;
|
||||||
|
if (buf) {
|
||||||
|
memcpy(buf, mdb->pg_buf + *cur_pos, piece_len);
|
||||||
|
buf += piece_len;
|
||||||
|
}
|
||||||
|
len -= piece_len;
|
||||||
|
mdb_read_pg(mdb, mdb_get_int32(mdb->pg_buf,4));
|
||||||
|
*cur_pos = 8;
|
||||||
|
}
|
||||||
|
/* Copy into buffer from final page */
|
||||||
|
if (len && buf) {
|
||||||
|
memcpy(buf, mdb->pg_buf + *cur_pos, len);
|
||||||
|
}
|
||||||
|
*cur_pos += len;
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void mdb_append_column(GPtrArray *columns, MdbColumn *in_col)
|
void mdb_append_column(GPtrArray *columns, MdbColumn *in_col)
|
||||||
{
|
{
|
||||||
@@ -203,7 +206,8 @@ GPtrArray *mdb_read_columns(MdbTableDef *table)
|
|||||||
MdbColumn *pcol;
|
MdbColumn *pcol;
|
||||||
unsigned char *col;
|
unsigned char *col;
|
||||||
unsigned int i;
|
unsigned int i;
|
||||||
int cur_pos, name_sz;
|
int cur_pos;
|
||||||
|
size_t name_sz;
|
||||||
|
|
||||||
table->columns = g_ptr_array_new();
|
table->columns = g_ptr_array_new();
|
||||||
|
|
||||||
@@ -223,7 +227,6 @@ GPtrArray *mdb_read_columns(MdbTableDef *table)
|
|||||||
buffer_dump(mdb->pg_buf, cur_pos, fmt->tab_col_entry_size); */
|
buffer_dump(mdb->pg_buf, cur_pos, fmt->tab_col_entry_size); */
|
||||||
#endif
|
#endif
|
||||||
read_pg_if_n(mdb, col, &cur_pos, fmt->tab_col_entry_size);
|
read_pg_if_n(mdb, col, &cur_pos, fmt->tab_col_entry_size);
|
||||||
cur_pos += fmt->tab_col_entry_size;
|
|
||||||
pcol = (MdbColumn *) g_malloc0(sizeof(MdbColumn));
|
pcol = (MdbColumn *) g_malloc0(sizeof(MdbColumn));
|
||||||
|
|
||||||
pcol->col_type = col[0];
|
pcol->col_type = col[0];
|
||||||
@@ -275,11 +278,8 @@ GPtrArray *mdb_read_columns(MdbTableDef *table)
|
|||||||
|
|
||||||
if (IS_JET4(mdb)) {
|
if (IS_JET4(mdb)) {
|
||||||
name_sz = read_pg_if_16(mdb, &cur_pos);
|
name_sz = read_pg_if_16(mdb, &cur_pos);
|
||||||
cur_pos += 2;
|
|
||||||
} else if (IS_JET3(mdb)) {
|
} else if (IS_JET3(mdb)) {
|
||||||
read_pg_if(mdb, &cur_pos, 0);
|
name_sz = read_pg_if_8(mdb, &cur_pos);
|
||||||
name_sz = mdb->pg_buf[cur_pos];
|
|
||||||
cur_pos++;
|
|
||||||
} else {
|
} else {
|
||||||
fprintf(stderr,"Unknown MDB version\n");
|
fprintf(stderr,"Unknown MDB version\n");
|
||||||
continue;
|
continue;
|
||||||
@@ -288,7 +288,6 @@ GPtrArray *mdb_read_columns(MdbTableDef *table)
|
|||||||
read_pg_if_n(mdb, tmp_buf, &cur_pos, name_sz);
|
read_pg_if_n(mdb, tmp_buf, &cur_pos, name_sz);
|
||||||
mdb_unicode2ascii(mdb, tmp_buf, name_sz, pcol->name, MDB_MAX_OBJ_NAME);
|
mdb_unicode2ascii(mdb, tmp_buf, name_sz, pcol->name, MDB_MAX_OBJ_NAME);
|
||||||
g_free(tmp_buf);
|
g_free(tmp_buf);
|
||||||
cur_pos += name_sz;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user