Skip to content

Commit

Permalink
Add support for CBOR sequence file format
Browse files Browse the repository at this point in the history
  • Loading branch information
qiu-x committed Oct 17, 2021
1 parent 2163896 commit 3c06b20
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 2 deletions.
114 changes: 114 additions & 0 deletions internal/magic/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,117 @@ func Marc(raw []byte, limit uint32) bool {
// Field terminator is present.
return bytes.Contains(raw, []byte{0x1E})
}

func Cbor(raw []byte, limit uint32) bool {
if len(raw) == 0 {
return false
}
var ok bool = true
for ok {
ok = cborHelper(&raw)
}
if len(raw) == 0 {
return true
} else {
return false
}
}

func cborHelper(raw *[]byte) bool {
if len(*raw) == 0 {
return false
}
t := uint8((*raw)[0] & 0xe0)
ai := (*raw)[0] & 0x1f
uval := uint64(ai)
*raw = (*raw)[1:]
val, ok := cborHead(raw, t, ai, uval)
if ok == false {
return ok
}
switch t {
case 0x40, 0x60:
if val < 0 {
return false
}
if len(*raw) < val {
return false
}
*raw = (*raw)[val:]
case 0x80, 0xa0:
if val < 0 {
return false
}
count := 1
if t == 0xa0 {
count = 2
}
for i := 0; i < val*count; i++ {
if ok = cborHelper(raw); ok == false {
return ok
}
}
case 0xc0:
for {
if len(*raw) == 0 {
return false
}
if uint8((*raw)[0]&0xe0) != 0xc0 {
break
}
}
return cborHelper(raw)
default:
return false
}
return true
}

func cborHead(raw *[]byte, t uint8, ai byte, val uint64) (int, bool) {
dataLen := len(*raw)
if dataLen == 0 {
return 0, false
}
if ai < 24 {
return int(val), true
}
switch ai {
case 24:
if dataLen < 2 {
return 0, false
}
val = uint64((*raw)[0])
*raw = (*raw)[1:]
if t == 0xe0 && val < 32 {
return 0, false
}
case 25:
if dataLen < 3 {
return 0, false
}
val = uint64(binary.BigEndian.Uint16((*raw)[0 : 2]))
*raw = (*raw)[2:]
case 26:
if dataLen < 5 {
return 0, false
}
val = uint64(binary.BigEndian.Uint32((*raw)[0 : 4]))
*raw = (*raw)[4:]
case 27:
if dataLen < 9 {
return 0, false
}
val = binary.BigEndian.Uint64((*raw)[0 : 8])
*raw = (*raw)[8:]
case 31:
switch t {
case 0x00, 0x20, 0xc0:
return 0, false
case 0xe0:
return 0, false
}
default: // ai == 28, 29, 30
return 0, false
}
return int(val), true
}
1 change: 1 addition & 0 deletions mimetype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ var files = map[string]string{
"bpg.bpg": "image/bpg",
"bz2.bz2": "application/x-bzip2",
"cab.cab": "application/vnd.ms-cab-compressed",
"cbor": "application/cbor-seq",
"class.class": "application/x-java-applet",
"crx.crx": "application/x-chrome-extension",
"csv.csv": "text/csv",
Expand Down
3 changes: 2 additions & 1 deletion supported_mimes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 165 Supported MIME types
## 166 Supported MIME types
This file is automatically generated when running tests. Do not edit manually.

Extension | MIME type | Aliases
Expand Down Expand Up @@ -135,6 +135,7 @@ Extension | MIME type | Aliases
**.pat** | image/x-gimp-pat | -
**.gbr** | image/x-gimp-gbr | -
**.glb** | model/gltf-binary | -
**n/a** | application/cbor-seq | -
**.txt** | text/plain | -
**.html** | text/html | -
**.svg** | image/svg+xml | -
Expand Down
1 change: 1 addition & 0 deletions testdata/cbor
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z
3 changes: 2 additions & 1 deletion tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var root = newMIME("application/octet-stream", "",
gzip, class, swf, crx, ttf, woff, woff2, otf, eot, wasm, shx, dbf, dcm, rar,
djvu, mobi, lit, bpg, sqlite3, dwg, nes, lnk, macho, qcp, icns, heic,
heicSeq, heif, heifSeq, hdr, mrc, mdb, accdb, zstd, cab, rpm, xz, lzip,
torrent, cpio, tzif, xcf, pat, gbr, glb,
torrent, cpio, tzif, xcf, pat, gbr, glb, cbor,
// Keep text last because it is the slowest check
text,
)
Expand Down Expand Up @@ -247,4 +247,5 @@ var (
gbr = newMIME("image/x-gimp-gbr", ".gbr", magic.Gbr)
xfdf = newMIME("application/vnd.adobe.xfdf", ".xfdf", magic.Xfdf)
glb = newMIME("model/gltf-binary", ".glb", magic.Glb)
cbor = newMIME("application/cbor-seq", "", magic.Cbor)
)

0 comments on commit 3c06b20

Please sign in to comment.