cvs: php4 /ext/dbase dbf_head.c

From: Date: Mon, 08 Oct 2001 21:52:26 +0000
Subject: cvs: php4 /ext/dbase dbf_head.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-7728@lists.php.net to get a copy of this message
jimw Mon Oct 8 17:52:26 2001 EDT Modified files: /php4/ext/dbase dbf_head.c Log: handle dbase files with padded field headers. (patch from Landon Bradshaw <landon@bradshaw.org>) Index: php4/ext/dbase/dbf_head.c diff -u php4/ext/dbase/dbf_head.c:1.9 php4/ext/dbase/dbf_head.c:1.10 --- php4/ext/dbase/dbf_head.c:1.9 Sun Sep 9 09:28:46 2001 +++ php4/ext/dbase/dbf_head.c Mon Oct 8 17:52:26 2001 @@ -21,8 +21,8 @@ { dbhead_t *dbh; struct dbf_dhead dbhead; - dbfield_t *dbf, *cur_f; - int ret, nfields, offset; + dbfield_t *dbf, *cur_f, *tdbf; + int ret, nfields, offset, gf_retval; if ((dbh = (dbhead_t *)malloc(sizeof(dbhead_t))) == NULL) return NULL; @@ -42,21 +42,33 @@ dbhead.dbh_date[DBH_DATE_MONTH], dbhead.dbh_date[DBH_DATE_DAY]); - dbh->db_nfields = nfields = (dbh->db_hlen - sizeof(struct dbf_dhead)) / - sizeof(struct dbf_dfield); - - /* get all the field info */ - dbf = (dbfield_t *)malloc(sizeof(dbfield_t) * nfields); - + /* malloc enough memory for the maximum number of fields: + 32 * 254 = 8128 bytes */ + tdbf = (dbfield_t *)malloc(sizeof(dbfield_t)*254); + offset = 1; - for (cur_f = dbf; cur_f < &dbf[nfields] ; cur_f++) { - if (get_dbf_field(dbh, cur_f) < 0) { + nfields = 0; + gf_retval = 0; + for (cur_f = tdbf; gf_retval < 2 && nfields < 254; cur_f++) { + gf_retval = get_dbf_field(dbh, cur_f); + + if (gf_retval < 0) { free_dbf_head(dbh); return NULL; } - cur_f->db_foffset = offset; - offset += cur_f->db_flen; + if (gf_retval != 2 ) { + cur_f->db_foffset = offset; + offset += cur_f->db_flen; + nfields++; + } } + dbh->db_nfields = nfields; + + /* malloc the right amount of space for records, copy and destroy old */ + dbf = (dbfield_t *)malloc(sizeof(dbfield_t)*nfields); + memcpy(dbf, tdbf, sizeof(dbfield_t)*nfields); + free(tdbf); + dbh->db_fields = dbf; return dbh; @@ -124,6 +136,12 @@ return ret; } + /* Check for the '0Dh' field terminator , if found return '2' + which will tell the loop we are at the end of fields */ + if (dbfield.dbf_name[0]==0x0d) { + return 2; + } + /* build the field name */ copy_crimp(dbf->db_fname, dbfield.dbf_name, DBF_NAMELEN); @@ -135,6 +153,7 @@ break; default: dbf->db_flen = get_short(dbfield.dbf_flen); + break; } if ((dbf->db_format = get_dbf_f_fmt(dbf)) == NULL) {

« previous php.cvs (#7728) next »