Skip to content

Commit a1ad712

Browse files
author
Alexander Barkov
committed
Fixing connect.dbf test failures on big endian machines.
1 parent 5c83368 commit a1ad712

File tree

1 file changed

+38
-24
lines changed

1 file changed

+38
-24
lines changed

storage/connect/filamdbf.cpp

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,28 @@ typedef struct _dbfheader {
7474
//uchar Dbfox :4; /* FoxPro if equal to 3 */
7575
uchar Version; /* Version information flags */
7676
char Filedate[3]; /* date, YYMMDD, binary. YY=year-1900 */
77-
uint Records; /* records in the file */
78-
ushort Headlen; /* bytes in the header */
79-
ushort Reclen; /* bytes in a record */
80-
ushort Fields; /* Reserved but used to store fields */
77+
private:
78+
/* The following four members are stored in little-endian format on disk */
79+
char m_RecordsBuf[4]; /* records in the file */
80+
char m_HeadlenBuf[2]; /* bytes in the header */
81+
char m_ReclenBuf[2]; /* bytes in a record */
82+
char m_FieldsBuf[2]; /* Reserved but used to store fields */
83+
public:
8184
char Incompleteflag; /* 01 if incomplete, else 00 */
8285
char Encryptflag; /* 01 if encrypted, else 00 */
8386
char Reserved2[12]; /* for LAN use */
8487
char Mdxflag; /* 01 if production .mdx, else 00 */
8588
char Language; /* Codepage */
8689
char Reserved3[2];
90+
91+
uint Records() const { return uint4korr(m_RecordsBuf); }
92+
ushort Headlen() const { return uint2korr(m_HeadlenBuf); }
93+
ushort Reclen() const { return uint2korr(m_ReclenBuf); }
94+
ushort Fields() const { return uint2korr(m_FieldsBuf); }
95+
96+
void SetHeadlen(ushort num) { int2store(m_HeadlenBuf, num); }
97+
void SetReclen(ushort num) { int2store(m_ReclenBuf, num); }
98+
void SetFields(ushort num) { int2store(m_FieldsBuf, num); }
8799
} DBFHEADER;
88100

89101
/****************************************************************************/
@@ -143,7 +155,7 @@ static int dbfhead(PGLOBAL g, FILE *file, PSZ fn, DBFHEADER *buf)
143155
strcpy(g->Message, MSG(DBASE_FILE));
144156

145157
// Check last byte(s) of header
146-
if (fseek(file, buf->Headlen - dbc, SEEK_SET) != 0) {
158+
if (fseek(file, buf->Headlen() - dbc, SEEK_SET) != 0) {
147159
sprintf(g->Message, MSG(BAD_HEADER), fn);
148160
return RC_FX;
149161
} // endif fseek
@@ -163,7 +175,7 @@ static int dbfhead(PGLOBAL g, FILE *file, PSZ fn, DBFHEADER *buf)
163175
} // endif endmark
164176

165177
// Calculate here the number of fields while we have the dbc info
166-
buf->Fields = (buf->Headlen - dbc - 1) / 32;
178+
buf->SetFields((buf->Headlen() - dbc - 1) / 32);
167179
fseek(file, HEADLEN, SEEK_SET);
168180
return rc;
169181
} // end of dbfhead
@@ -219,7 +231,7 @@ PQRYRES DBFColumns(PGLOBAL g, char *dp, const char *fn, bool info)
219231
/************************************************************************/
220232
/* Allocate the structures used to refer to the result set. */
221233
/************************************************************************/
222-
fields = mainhead.Fields;
234+
fields = mainhead.Fields();
223235
} else
224236
fields = 0;
225237

@@ -236,11 +248,11 @@ PQRYRES DBFColumns(PGLOBAL g, char *dp, const char *fn, bool info)
236248
if (trace) {
237249
htrc("Structure of %s\n", filename);
238250
htrc("headlen=%hd reclen=%hd degree=%d\n",
239-
mainhead.Headlen, mainhead.Reclen, fields);
251+
mainhead.Headlen(), mainhead.Reclen(), fields);
240252
htrc("flags(iem)=%d,%d,%d cp=%d\n", mainhead.Incompleteflag,
241253
mainhead.Encryptflag, mainhead.Mdxflag, mainhead.Language);
242254
htrc("%hd records, last changed %02d/%02d/%d\n",
243-
mainhead.Records, mainhead.Filedate[1], mainhead.Filedate[2],
255+
mainhead.Records(), mainhead.Filedate[1], mainhead.Filedate[2],
244256
mainhead.Filedate[0] + (mainhead.Filedate[0] <= 30) ? 2000 : 1900);
245257
htrc("Field Type Offset Len Dec Set Mdx\n");
246258
} // endif trace
@@ -398,13 +410,13 @@ int DBFBASE::ScanHeader(PGLOBAL g, PSZ fname, int lrecl, char *defpath)
398410
} else if (rc == RC_FX)
399411
return -1;
400412

401-
if ((int)header.Reclen != lrecl) {
402-
sprintf(g->Message, MSG(BAD_LRECL), lrecl, header.Reclen);
413+
if ((int) header.Reclen() != lrecl) {
414+
sprintf(g->Message, MSG(BAD_LRECL), lrecl, header.Reclen());
403415
return -1;
404416
} // endif Lrecl
405417

406-
Records = (int)header.Records;
407-
return (int)header.Headlen;
418+
Records = (int) header.Records();
419+
return (int) header.Headlen();
408420
} // end of ScanHeader
409421

410422
/* ---------------------------- Class DBFFAM ------------------------------ */
@@ -565,8 +577,8 @@ bool DBFFAM::AllocateBuffer(PGLOBAL g)
565577
header->Filedate[0] = datm->tm_year - 100;
566578
header->Filedate[1] = datm->tm_mon + 1;
567579
header->Filedate[2] = datm->tm_mday;
568-
header->Headlen = (ushort)hlen;
569-
header->Reclen = (ushort)reclen;
580+
header->SetHeadlen((ushort) hlen);
581+
header->SetReclen((ushort) reclen);
570582
descp = (DESCRIPTOR*)header;
571583

572584
// Currently only standard Xbase types are supported
@@ -627,13 +639,13 @@ bool DBFFAM::AllocateBuffer(PGLOBAL g)
627639
DBFHEADER header;
628640

629641
if ((rc = dbfhead(g, Stream, Tdbp->GetFile(g), &header)) == RC_OK) {
630-
if (Lrecl != (int)header.Reclen) {
631-
sprintf(g->Message, MSG(BAD_LRECL), Lrecl, header.Reclen);
642+
if (Lrecl != (int) header.Reclen()) {
643+
sprintf(g->Message, MSG(BAD_LRECL), Lrecl, header.Reclen());
632644
return true;
633645
} // endif Lrecl
634646

635-
Records = (int)header.Records;
636-
Headlen = (int)header.Headlen;
647+
Records = (int) header.Records();
648+
Headlen = (int) header.Headlen();
637649
} else if (rc == RC_NF) {
638650
Records = 0;
639651
Headlen = 0;
@@ -868,8 +880,10 @@ void DBFFAM::CloseTableFile(PGLOBAL g, bool abort)
868880
PlugSetPath(filename, To_File, Tdbp->GetPath());
869881
if ((Stream= global_fopen(g, MSGID_OPEN_MODE_STRERROR, filename, "r+b")))
870882
{
883+
char nRecords[4];
884+
int4store(&nRecords, n);
871885
fseek(Stream, 4, SEEK_SET); // Get header.Records position
872-
fwrite(&n, sizeof(int), 1, Stream);
886+
fwrite(&nRecords, sizeof(nRecords), 1, Stream);
873887
fclose(Stream);
874888
Stream= NULL;
875889
Records= n; // Update Records value
@@ -944,13 +958,13 @@ bool DBMFAM::AllocateBuffer(PGLOBAL g)
944958
/************************************************************************/
945959
DBFHEADER *hp = (DBFHEADER*)Memory;
946960

947-
if (Lrecl != (int)hp->Reclen) {
948-
sprintf(g->Message, MSG(BAD_LRECL), Lrecl, hp->Reclen);
961+
if (Lrecl != (int) hp->Reclen()) {
962+
sprintf(g->Message, MSG(BAD_LRECL), Lrecl, hp->Reclen());
949963
return true;
950964
} // endif Lrecl
951965

952-
Records = (int)hp->Records;
953-
Headlen = (int)hp->Headlen;
966+
Records = (int) hp->Records();
967+
Headlen = (int) hp->Headlen();
954968
} // endif Headlen
955969

956970
/**************************************************************************/

0 commit comments

Comments
 (0)