Skip to content

Commit

Permalink
Merge pull request #241 from gabriel-vasile/dbf
Browse files Browse the repository at this point in the history
Fix json detected as dbf when it starts with low ascii values
  • Loading branch information
gabriel-vasile committed Jan 30, 2022
2 parents b497e3d + 2345dc1 commit 272584c
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
17 changes: 14 additions & 3 deletions internal/magic/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,16 +64,27 @@ func MachO(raw []byte, limit uint32) bool {
// Dbf matches a dBase file.
// https://www.dbase.com/Knowledgebase/INT/db7_file_fmt.htm
func Dbf(raw []byte, limit uint32) bool {
if len(raw) < 4 {
if len(raw) < 68 {
return false
}

// 3rd and 4th bytes contain the last update month and day of month
// 3rd and 4th bytes contain the last update month and day of month.
if !(0 < raw[2] && raw[2] < 13 && 0 < raw[3] && raw[3] < 32) {
return false
}

// dbf type is dictated by the first byte
// 12, 13, 30, 31 are reserved bytes and always filled with 0x00.
if raw[12] != 0x00 || raw[13] != 0x00 || raw[30] != 0x00 || raw[31] != 0x00 {
return false
}
// Production MDX flag;
// 0x01 if a production .MDX file exists for this table;
// 0x00 if no .MDX file exists.
if raw[28] > 0x01 {
return false
}

// dbf type is dictated by the first byte.
dbfTypes := []byte{
0x02, 0x03, 0x04, 0x05, 0x30, 0x31, 0x32, 0x42, 0x62, 0x7B, 0x82,
0x83, 0x87, 0x8A, 0x8B, 0x8E, 0xB3, 0xCB, 0xE5, 0xF5, 0xF4, 0xFB,
Expand Down
1 change: 1 addition & 0 deletions mimetype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ var files = map[string]string{
"xpm.xpm": "image/x-xpixmap",
"js.js": "application/javascript",
"json.json": "application/json",
"json.lowascii.json": "application/json",
// json.{int,float,string}.txt contain a single JSON value. They are valid JSON
// documents, but they should not be detected as application/json. This mimics
// the behaviour of the file utility and seems the correct thing to do.
Expand Down
2 changes: 2 additions & 0 deletions testdata/json.lowascii.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{
"fixture for issue #": 239}

0 comments on commit 272584c

Please sign in to comment.