Fix and tidy up read_pg_in* functions

This commit is contained in:
whydoubt 2005-12-17 15:59:18 +00:00
parent 2b2e300513
commit bc71178167
4 changed files with 59 additions and 63 deletions

View File

@ -1,5 +1,9 @@
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/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>
* src/util/mdb-array.c:

View File

@ -426,10 +426,10 @@ extern void mdb_append_column(GPtrArray *columns, MdbColumn *in_col);
extern void mdb_free_columns(GPtrArray *columns);
extern GPtrArray *mdb_read_columns(MdbTableDef *table);
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 guint32 read_pg_if_32(MdbHandle *mdb, int *cur_pos);
extern int read_pg_if(MdbHandle *mdb, int *cur_pos, int offset);
extern guint16 read_pg_if_n(MdbHandle *mdb, void *buf, int *cur_pos, int len);
extern void *read_pg_if_n(MdbHandle *mdb, void *buf, int *cur_pos, size_t len);
extern int mdb_is_user_table(MdbCatalogEntry *entry);
extern int mdb_is_system_table(MdbCatalogEntry *entry);

View File

@ -90,7 +90,6 @@ mdb_read_indices(MdbTableDef *table)
tmpbuf = (gchar *) g_malloc(idx2_sz);
for (i=0;i<table->num_idxs;i++) {
read_pg_if_n(mdb, tmpbuf, &cur_pos, idx2_sz);
cur_pos += idx2_sz;
pidx = (MdbIndex *) g_malloc0(sizeof(MdbIndex));
pidx->table = table;
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);
if (IS_JET4(mdb)) {
name_sz=read_pg_if_16(mdb, &cur_pos);
cur_pos += 2;
} else {
read_pg_if(mdb, &cur_pos, 0);
name_sz=mdb->pg_buf[cur_pos++];
name_sz=read_pg_if_8(mdb, &cur_pos);
}
tmpbuf = g_malloc(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);
g_free(tmpbuf);
//fprintf(stderr, "index name %s\n", pidx->name);
@ -142,24 +138,21 @@ mdb_read_indices(MdbTableDef *table)
key_num=0;
for (j=0;j<MDB_MAX_IDX_COLS;j++) {
col_num=read_pg_if_16(mdb,&cur_pos);
cur_pos += 2;
read_pg_if(mdb, &cur_pos, 0);
cur_pos++;
if (col_num == 0xFFFF)
if (col_num == 0xFFFF) {
cur_pos++;
continue;
}
/* set column number to a 1 based column number and store */
pidx->key_col_num[key_num] = col_num + 1;
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++;
}
pidx->num_keys = key_num;
cur_pos += 4;
pidx->first_pg = read_pg_if_32(mdb, &cur_pos);
cur_pos += 4;
read_pg_if(mdb, &cur_pos, 0);
pidx->flags = mdb->pg_buf[cur_pos++];
pidx->flags = read_pg_if_8(mdb, &cur_pos);
if (IS_JET4(mdb)) cur_pos += 9;
}
return NULL;

View File

@ -128,60 +128,63 @@ MdbTableDef *mdb_read_table_by_name(MdbHandle *mdb, gchar *table_name, int obj_t
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
read_pg_if_32(MdbHandle *mdb, int *cur_pos)
{
unsigned char c[4];
int i, rc = 0;
char c[4];
for (i=0;i<4;i++) {
rc += read_pg_if(mdb, cur_pos, i);
c[i] = mdb->pg_buf[(*cur_pos) + i];
}
read_pg_if_n(mdb, c, cur_pos, 4);
return mdb_get_int32(c, 0);
}
guint16
read_pg_if_16(MdbHandle *mdb, int *cur_pos)
{
unsigned char low_byte, high_byte;
int rc = 0;
char c[2];
rc += read_pg_if(mdb, cur_pos, 0);
low_byte = mdb->pg_buf[*cur_pos];
rc += read_pg_if(mdb, cur_pos, 1);
high_byte = mdb->pg_buf[(*cur_pos) + 1];
return (high_byte * 256 + low_byte);
read_pg_if_n(mdb, c, cur_pos, 2);
return mdb_get_int16(c, 0);
}
guint16
read_pg_if_n(MdbHandle *mdb, void *buf, int *cur_pos, int len)
guint8
read_pg_if_8(MdbHandle *mdb, int *cur_pos)
{
if (*cur_pos + len < mdb->fmt->pg_size) {
memcpy(buf, &mdb->pg_buf[*cur_pos], len);
return 0;
} else {
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;
}
guint8 c;
read_pg_if_n(mdb, &c, cur_pos, 1);
return c;
}
/*
* 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)
{
@ -203,7 +206,8 @@ GPtrArray *mdb_read_columns(MdbTableDef *table)
MdbColumn *pcol;
unsigned char *col;
unsigned int i;
int cur_pos, name_sz;
int cur_pos;
size_t name_sz;
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); */
#endif
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->col_type = col[0];
@ -275,11 +278,8 @@ GPtrArray *mdb_read_columns(MdbTableDef *table)
if (IS_JET4(mdb)) {
name_sz = read_pg_if_16(mdb, &cur_pos);
cur_pos += 2;
} else if (IS_JET3(mdb)) {
read_pg_if(mdb, &cur_pos, 0);
name_sz = mdb->pg_buf[cur_pos];
cur_pos++;
name_sz = read_pg_if_8(mdb, &cur_pos);
} else {
fprintf(stderr,"Unknown MDB version\n");
continue;
@ -288,7 +288,6 @@ GPtrArray *mdb_read_columns(MdbTableDef *table)
read_pg_if_n(mdb, tmp_buf, &cur_pos, name_sz);
mdb_unicode2ascii(mdb, tmp_buf, name_sz, pcol->name, MDB_MAX_OBJ_NAME);
g_free(tmp_buf);
cur_pos += name_sz;
}