Commit 9ea47aab authored by Sean Finney's avatar Sean Finney

Imported Upstream version 5.2.6.dfsg.1

parent 993e1866
dBase
Jim Winstead
dnl
dnl $Id: config.m4,v 1.8 2005/05/29 23:16:40 sniper Exp $
dnl
PHP_ARG_ENABLE(dbase,whether to enable dbase support,
[ --enable-dbase Enable the bundled dbase library])
if test "$PHP_DBASE" = "yes"; then
AC_DEFINE(DBASE,1,[ ])
PHP_NEW_EXTENSION(dbase, dbf_head.c dbf_rec.c dbf_misc.c dbf_ndx.c dbase.c, $ext_shared)
fi
// $Id: config.w32,v 1.1 2003/12/07 00:09:48 edink Exp $
// vim:ft=javascript
ARG_ENABLE("dbase", "Enable the bundled dbase library", "no");
if (PHP_DBASE != "no") {
EXTENSION("dbase", "dbase.c dbf_head.c dbf_misc.c dbf_ndx.c dbf_rec.c");
AC_DEFINE('HAVE_DBASE', 1, 'dbase support');
ADD_FLAG("CFLAGS_DBASE", "/D DBASE=1");
}
This diff is collapsed.
# Microsoft Developer Studio Project File - Name="dbase" - Package Owner=<4>
# Microsoft Developer Studio Generated Build File, Format Version 6.00
# ** DO NOT EDIT **
# TARGTYPE "Win32 (x86) Dynamic-Link Library" 0x0102
CFG=dbase - Win32 Release_TS
!MESSAGE This is not a valid makefile. To build this project using NMAKE,
!MESSAGE use the Export Makefile command and run
!MESSAGE
!MESSAGE NMAKE /f "dbase.mak".
!MESSAGE
!MESSAGE You can specify a configuration when running NMAKE
!MESSAGE by defining the macro CFG on the command line. For example:
!MESSAGE
!MESSAGE NMAKE /f "dbase.mak" CFG="dbase - Win32 Release_TS"
!MESSAGE
!MESSAGE Possible choices for configuration are:
!MESSAGE
!MESSAGE "dbase - Win32 Release_TS" (based on "Win32 (x86) Dynamic-Link Library")
!MESSAGE "dbase - Win32 Debug_TS" (based on "Win32 (x86) Dynamic-Link Library")
!MESSAGE
# Begin Project
# PROP AllowPerConfigDependencies 0
# PROP Scc_ProjName ""
# PROP Scc_LocalPath ""
CPP=cl.exe
MTL=midl.exe
RSC=rc.exe
!IF "$(CFG)" == "dbase - Win32 Release_TS"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Release_TS"
# PROP BASE Intermediate_Dir "Release_TS"
# PROP BASE Ignore_Export_Lib 0
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Release_TS"
# PROP Intermediate_Dir "Release_TS"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /MT /W3 /GX /O2 /I "..\.." /I "..\..\..\Zend" /I "..\..\..\bindlib_w32" /I "..\..\..\TSRM" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "COMPILE_DL_DBASE" /D ZTS=1 /YX /FD /c
# ADD CPP /nologo /MD /W3 /GX /O2 /I "..\.." /I "..\..\Zend" /I "..\..\..\bindlib_w32" /I "..\..\TSRM" /I "..\..\main" /D "WIN32" /D "DBASE_EXPORTS" /D "COMPILE_DL_DBASE" /D ZTS=1 /D HAVE_DBASE=1 /D ZEND_DEBUG=0 /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "ZEND_WIN32" /D "PHP_WIN32" /D DBASE=1 /YX /FD /c
# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32
# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
# ADD BASE RSC /l 0x406 /d "NDEBUG"
# ADD RSC /l 0x406 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib php5ts.lib /nologo /dll /machine:I386
# ADD LINK32 php5ts.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /machine:I386 /out:"..\..\Release_TS/php_dbase.dll" /libpath:"..\..\Release_TS" /libpath:"..\..\Release_TS_Inline"
# SUBTRACT LINK32 /pdb:none
!ELSEIF "$(CFG)" == "dbase - Win32 Debug_TS"
# PROP BASE Use_MFC 0
# PROP BASE Use_Debug_Libraries 0
# PROP BASE Output_Dir "Debug_TS"
# PROP BASE Intermediate_Dir "Debug_TS"
# PROP BASE Ignore_Export_Lib 0
# PROP BASE Target_Dir ""
# PROP Use_MFC 0
# PROP Use_Debug_Libraries 0
# PROP Output_Dir "Debug_TS"
# PROP Intermediate_Dir "Debug_TS"
# PROP Ignore_Export_Lib 0
# PROP Target_Dir ""
# ADD BASE CPP /nologo /MT /W3 /GX /O2 /I "..\.." /I "..\..\Zend" /I "..\..\..\bindlib_w32" /I "..\..\TSRM" /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "COMPILE_DL_DBASE" /D ZTS=1 /YX /FD /c
# ADD CPP /nologo /MDd /W3 /GX /ZI /Od /I "..\.." /I "..\..\Zend" /I "..\..\..\bindlib_w32" /I "..\..\TSRM" /I "..\..\main" /D ZEND_DEBUG=1 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_MBCS" /D "_USRDLL" /D "DBASE_EXPORTS" /D "COMPILE_DL_DBASE" /D ZTS=1 /D "ZEND_WIN32" /D "PHP_WIN32" /D HAVE_DBASE=1 /D DBASE=1 /FR /YX /FD /c
# ADD BASE MTL /nologo /D "NDEBUG" /mktyplib203 /win32
# ADD MTL /nologo /D "NDEBUG" /mktyplib203 /win32
# ADD BASE RSC /l 0x406 /d "NDEBUG"
# ADD RSC /l 0x406 /d "NDEBUG"
BSC32=bscmake.exe
# ADD BASE BSC32 /nologo
# ADD BSC32 /nologo
LINK32=link.exe
# ADD BASE LINK32 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib php5ts.lib /nologo /dll /machine:I386
# ADD LINK32 php5ts_debug.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /incremental:yes /debug /machine:I386 /out:"..\..\Debug_TS/php_dbase.dll" /libpath:"..\..\Debug_TS"
# SUBTRACT LINK32 /pdb:none
!ENDIF
# Begin Target
# Name "dbase - Win32 Release_TS"
# Name "dbase - Win32 Debug_TS"
# Begin Group "Source Files"
# PROP Default_Filter "cpp;c;cxx;rc;def;r;odl;idl;hpj;bat"
# Begin Source File
SOURCE=.\dbase.c
# End Source File
# Begin Source File
SOURCE=.\dbf_head.c
# End Source File
# Begin Source File
SOURCE=.\dbf_misc.c
# End Source File
# Begin Source File
SOURCE=.\dbf_ndx.c
# End Source File
# Begin Source File
SOURCE=.\dbf_rec.c
# End Source File
# End Group
# Begin Group "Header Files"
# PROP Default_Filter "h;hpp;hxx;hm;inl"
# Begin Source File
SOURCE=.\dbf.h
# End Source File
# Begin Source File
SOURCE=.\dbf_head.h
# End Source File
# Begin Source File
SOURCE=.\dbf_misc.h
# End Source File
# Begin Source File
SOURCE=.\dbf_ndx.h
# End Source File
# Begin Source File
SOURCE=.\dbf_rec.h
# End Source File
# Begin Source File
SOURCE=.\php_dbase.h
# End Source File
# End Group
# Begin Group "Resource Files"
# PROP Default_Filter "ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe"
# End Group
# End Target
# End Project
/*
* Copyright (c) 1991, 1992, 1993 Brad Eacker,
* (Music, Intuition, Software, and Computers)
* All Rights Reserved
*/
/*
* dbf header structure on disk (pc dbase III)
*
* Basic info taken from:
* "File Formats for Popular PC Software"
* Jeff Walden
* (c) 1986 John Wiley & Sons, Inc.
*/
#ifndef DBF_H_
#define DBF_H_
#include <stdlib.h>
#include <string.h>
#ifdef WIN32
#include <io.h>
#else
#include <unistd.h>
#endif
/* So we can use O_BINARY on non-Win32 systems. */
#if !defined(O_BINARY) && !defined(WIN32)
#define O_BINARY (0)
#endif
struct dbf_dhead {
char dbh_dbt; /* memo (dbt) file present */
char dbh_date[3]; /* last update YY, MM, DD */
char dbh_records[4]; /* number of records LE */
char dbh_hlen[2]; /* header length LE */
char dbh_rlen[2]; /* record length LE */
char dbh_res[20]; /* padding */
};
#define DBH_DATE_YEAR 0 /* byte offset for year in dbh_date */
#define DBH_DATE_MONTH 1
#define DBH_DATE_DAY 2
/*
* field description on disk
*/
#define DBF_NAMELEN 11
struct dbf_dfield {
char dbf_name[DBF_NAMELEN]; /* name of field */
char dbf_type; /* type of field */
char dbf_fda[4]; /* something for dbase III */
char dbf_flen[2]; /* field length [and decimal if N] */
char dbf_res[14]; /* padding */
};
struct db_field {
char db_fname[DBF_NAMELEN+1]; /* 0 terminated */
char db_type; /* type of field */
int db_flen; /* length of field */
int db_fdc; /* number of decimals in field */
char *db_format; /* format for printing %s etc */
int db_foffset; /* offset within record */
};
typedef struct db_field dbfield_t;
struct db_head {
int db_fd;
unsigned char db_dbt; /* dbt present */
char db_date[9]; /* date of last update in db format */
long db_records; /* number of records */
int db_hlen; /* header length */
int db_rlen; /* record length */
int db_nfields; /* number of fields */
dbfield_t *db_fields; /* field info */
char *db_name; /* name of dbf file */
int db_cur_rec; /* current record */
};
typedef struct db_head dbhead_t;
#define DBH_TYPE_NORMAL 0x03
#define DBH_TYPE_MEMO 0x83
#define VALID_RECORD ' '
#define DELETED_RECORD '*'
#include "dbf_head.h"
#include "dbf_misc.h"
#include "dbf_rec.h"
#endif /* DBF_H_ */
/*
* Copyright (c) 1991, 1992, 1993 Brad Eacker,
* (Music, Intuition, Software, and Computers)
* All Rights Reserved
*/
#include <stdio.h>
#include <fcntl.h>
#include "php.h"
#include "dbf.h"
void free_dbf_head(dbhead_t *dbh);
int get_dbf_field(dbhead_t *dbh, dbfield_t *dbf);
/*
* get the header info from the file
* basic header info & field descriptions
*/
dbhead_t *get_dbf_head(int fd)
{
dbhead_t *dbh;
struct dbf_dhead dbhead;
dbfield_t *dbf, *cur_f, *tdbf;
int ret, nfields, offset, gf_retval;
if ((dbh = (dbhead_t *)calloc(1, sizeof(dbhead_t))) == NULL)
return NULL;
if (lseek(fd, 0, 0) < 0) {
free(dbh);
return NULL;
}
if ((ret = read(fd, &dbhead, sizeof(dbhead))) <= 0) {
free(dbh);
return NULL;
}
/* build in core info */
dbh->db_fd = fd;
dbh->db_dbt = dbhead.dbh_dbt;
dbh->db_records = get_long(dbhead.dbh_records);
dbh->db_hlen = get_short(dbhead.dbh_hlen);
dbh->db_rlen = get_short(dbhead.dbh_rlen);
db_set_date(dbh->db_date, dbhead.dbh_date[DBH_DATE_YEAR] + 1900,
dbhead.dbh_date[DBH_DATE_MONTH],
dbhead.dbh_date[DBH_DATE_DAY]);
/* malloc enough memory for the maximum number of fields:
32 * 1024 = 32K dBase5 (for Win) seems to allow that many */
tdbf = (dbfield_t *)calloc(1, sizeof(dbfield_t)*1024);
offset = 1;
nfields = 0;
gf_retval = 0;
for (cur_f = tdbf; gf_retval < 2 && nfields < 1024; cur_f++) {
gf_retval = get_dbf_field(dbh, cur_f);
if (gf_retval < 0) {
free_dbf_head(dbh);
free(tdbf);
return NULL;
}
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;
}
/*
* free up the header info built above
*/
void free_dbf_head(dbhead_t *dbh)
{
dbfield_t *dbf, *cur_f;
int nfields;
dbf = dbh->db_fields;
nfields = dbh->db_nfields;
for (cur_f = dbf; cur_f < &dbf[nfields]; cur_f++) {
if (cur_f->db_format) {
free(cur_f->db_format);
}
}
free(dbf);
free(dbh);
}
/*
* put out the header info
*/
int put_dbf_head(dbhead_t *dbh)
{
int fd = dbh->db_fd;
struct dbf_dhead dbhead;
int ret;
memset (&dbhead, 0, sizeof(dbhead));
/* build on disk info */
dbhead.dbh_dbt = dbh->db_dbt;
put_long(dbhead.dbh_records, dbh->db_records);
put_short(dbhead.dbh_hlen, dbh->db_hlen);
put_short(dbhead.dbh_rlen, dbh->db_rlen);
/* put the date spec'd into the on disk header */
dbhead.dbh_date[DBH_DATE_YEAR] =(char)(db_date_year(dbh->db_date) -
1900);
dbhead.dbh_date[DBH_DATE_MONTH]=(char)(db_date_month(dbh->db_date));
dbhead.dbh_date[DBH_DATE_DAY] =(char)(db_date_day(dbh->db_date));
if (lseek(fd, 0, 0) < 0)
return -1;
if ((ret = write(fd, &dbhead, sizeof(dbhead))) <= 0)
return -1;
return ret;
}
/*
* get a field off the disk from the current file offset
*/
int get_dbf_field(dbhead_t *dbh, dbfield_t *dbf)
{
struct dbf_dfield dbfield;
int ret;
if ((ret = read(dbh->db_fd, &dbfield, sizeof(dbfield))) <= 0) {
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);
dbf->db_type = dbfield.dbf_type;
switch (dbf->db_type) {
case 'N':
case 'F':
dbf->db_flen = dbfield.dbf_flen[0];
dbf->db_fdc = dbfield.dbf_flen[1];
break;
case 'D':
dbf->db_flen = 8;
break;
case 'L':
dbf->db_flen = 1;
break;
default:
dbf->db_flen = get_short(dbfield.dbf_flen);
break;
}
if ((dbf->db_format = get_dbf_f_fmt(dbf)) == NULL) {
/* something went wrong, most likely this fieldtype is not supported */
return -1;
}
return 0;
}
/*
* put a field out on the disk at the current file offset
*/
int put_dbf_field(dbhead_t *dbh, dbfield_t *dbf)
{
struct dbf_dfield dbfield;
char *scp, *dcp;
int ret;
memset (&dbfield, 0, sizeof(dbfield));
/* build the on disk field info */
scp = dbf->db_fname; dcp = dbfield.dbf_name;
strlcpy(dbfield.dbf_name, dbf->db_fname, DBF_NAMELEN + 1);
dbfield.dbf_type = dbf->db_type;
switch (dbf->db_type) {
case 'N':
dbfield.dbf_flen[0] = dbf->db_flen;
dbfield.dbf_flen[1] = dbf->db_fdc;
break;
case 'D':
dbf->db_flen = 8;
break;
case 'L':
dbf->db_flen = 1;
break;
default:
put_short(dbfield.dbf_flen, dbf->db_flen);
}
/* now write it out to disk */
if ((ret = write(dbh->db_fd, &dbfield, sizeof(dbfield))) <= 0) {
return ret;
}
return 1;
}
/*
* put out all the info at the top of the file...
*/
static char end_stuff[2] = {0x0d, 0};
void put_dbf_info(dbhead_t *dbh)
{
dbfield_t *dbf;
char *cp;
int fcnt;
if ((cp = db_cur_date(NULL))) {
strlcpy(dbh->db_date, cp, 9);
free(cp);
}
put_dbf_head(dbh);
dbf = dbh->db_fields;
for (fcnt = dbh->db_nfields; fcnt > 0; fcnt--, dbf++)
put_dbf_field(dbh, dbf);
write(dbh->db_fd, end_stuff, 1);
}
char *get_dbf_f_fmt(dbfield_t *dbf)
{
char format[100];
/* build the field format for printf */
switch (dbf->db_type) {
case 'C':
snprintf(format, sizeof(format), "%%-%ds", dbf->db_flen);
break;
case 'N':
case 'L':
case 'D':
case 'F':
snprintf(format, sizeof(format), "%%%ds", dbf->db_flen);
break;
case 'M':
strlcpy(format, "%s", sizeof(format));
break;
default:
return NULL;
}
return (char *)strdup(format);
}
dbhead_t *dbf_open(char *dp, int o_flags TSRMLS_DC)
{
int fd;
char *cp;
dbhead_t *dbh;
cp = dp;
if ((fd = VCWD_OPEN(cp, o_flags|O_BINARY)) < 0) {
return NULL;
}
if ((dbh = get_dbf_head(fd)) == NULL) {
return NULL;
}
dbh->db_cur_rec = 0;
return dbh;
}
void dbf_head_info(dbhead_t *dbh)
{
int nfields;
dbfield_t *dbf, *cur_f;
nfields = dbh->db_nfields;
printf("# fields: %d, record len: %d, total records %ld\n",
nfields, dbh->db_rlen, dbh->db_records);
dbf = dbh->db_fields;
for (cur_f = dbf; cur_f < &dbf[nfields] ; cur_f++) {
printf("# %s, %c, %d, %d\n", cur_f->db_fname,
cur_f->db_type, cur_f->db_flen, cur_f->db_fdc);
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: sw=4 ts=4 fdm=marker
* vim<600: sw=4 ts=4
*/
#include "php.h"
extern dbhead_t *get_dbf_head(int fd);
void free_dbf_head(dbhead_t *dbh);
extern int put_dbf_head(dbhead_t *dbh);
extern int get_dbf_field(dbhead_t *dbh, dbfield_t *dbf);
extern int put_dbf_field(dbhead_t *dbh, dbfield_t *dbf);
void put_dbf_info(dbhead_t *dbh);
extern char *get_dbf_f_fmt(dbfield_t *dbf);
extern dbhead_t *dbf_open(char *dp, int o_flags TSRMLS_DC);
void dbf_head_info(dbhead_t *dbh);
/*
* Copyright (c) 1991, 1992, 1993 Brad Eacker,
* (Music, Intuition, Software, and Computers)
* All Rights Reserved
*/
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include "dbf_misc.h"
#include "php_reentrancy.h"
/*
* routine to change little endian long to host long
*/
long get_long(char *cp)
{
int ret;
unsigned char *source = (unsigned char *)cp;
ret = *source++;
ret += ((*source++)<<8);
ret += ((*source++)<<16);
ret += ((*source++)<<24);
return ret;
}
void put_long(char *cp, long lval)
{
*cp++ = lval & 0xff;
*cp++ = (lval >> 8) & 0xff;
*cp++ = (lval >> 16) & 0xff;
*cp++ = (lval >> 24) & 0xff;
}
/*
* routine to change little endian short to host short
*/
int get_short(char *cp)
{
int ret;
unsigned char *source = (unsigned char *)cp;
ret = *source++;
ret += ((*source++)<<8);
return ret;
}
void put_short(char *cp, int sval)
{
*cp++ = sval & 0xff;
*cp++ = (sval >> 8) & 0xff;
}
double get_double(char *cp)
{
double ret;
unsigned char *dp = (unsigned char *)&ret;
dp[7] = *cp++;
dp[6] = *cp++;
dp[5] = *cp++;
dp[4] = *cp++;
dp[3] = *cp++;
dp[2] = *cp++;
dp[1] = *cp++;
dp[0] = *cp++;
return ret;
}
void put_double(char *cp, double fval)
{
unsigned char *dp = (unsigned char *)&fval;
cp[7] = *dp++;
cp[6] = *dp++;
cp[5] = *dp++;
cp[4] = *dp++;
cp[3] = *dp++;
cp[2] = *dp++;
cp[1] = *dp++;
cp[0] = *dp++;
}
void copy_fill(char *dp, char *sp, int len)
{
while (*sp && len > 0) {
*dp++ = *sp++;
len--;
}
while (len-- > 0)
*dp++ = ' ';
}
void copy_crimp(char *dp, char *sp, int len)
{
while (len-- > 0) {
*dp++ = *sp++;
}
*dp = 0;
for (dp-- ; *dp == ' '; dp--) {
*dp = 0;
}
}
void db_set_date(char *cp, int year, int month, int day)
{
if (month > 12)
month = 0;
if (day > 31)
day = 0;
snprintf(cp, 9, "%04d%02d%02d", year, month, day);
}
int db_date_year(char *cp)
{
int year, i;
for (year = 0, i = 0; i < 4; i++)
year = year * 10 + (cp[i] - '0');
return year;
}
int db_date_month(char *cp)
{
int month, i;
for (month = 0, i = 4; i < 6; i++)
month = month * 10 + (cp[i] - '0');
return month;
}
int db_date_day(char *cp)
{
int day, i;
for (day = 0, i = 6; i < 8; i++)
day = day * 10 + (cp[i] - '0');
return day;
}
#include <time.h>
char *db_cur_date(char *cp)
{
struct tm *ctm, tmbuf;
time_t c_time;
c_time = time((time_t *)NULL);
ctm = php_localtime_r(&c_time, &tmbuf);
if (cp == NULL)
cp = (char *)malloc(9);
if (ctm == NULL || cp == NULL)