-
Notifications
You must be signed in to change notification settings - Fork 163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for CBOR sequence file format #194
Changes from 7 commits
51031b8
27f3cbb
6d3338b
c3a652f
784b8dc
573bf2f
20a5ca6
64a3c81
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,3 +142,131 @@ func Marc(raw []byte, limit uint32) bool { | |
// Field terminator is present. | ||
return bytes.Contains(raw, []byte{0x1E}) | ||
} | ||
|
||
// CborSeq matches CBOR sequences | ||
func CborSeq(raw []byte, limit uint32) bool { | ||
if len(raw) == 0 { | ||
return false | ||
} | ||
offset, i := 0, 0 | ||
ok, oldok := true, true | ||
for ; ok && offset != len(raw); i++ { | ||
oldok = ok | ||
offset, ok = cborHelper(raw, offset) | ||
} | ||
if limit == uint32(len(raw)) { | ||
ok = oldok | ||
} | ||
return ok && i > 1 | ||
} | ||
|
||
func cborHelper(raw []byte, offset int) (int, bool) { | ||
raw_len := len(raw) - offset | ||
if raw_len == 0 { | ||
return 0, false | ||
} | ||
|
||
mt := uint8(raw[offset] & 0xe0) | ||
ai := raw[offset] & 0x1f | ||
val := int(ai) | ||
offset++ | ||
|
||
BgEn := binary.BigEndian | ||
switch ai { | ||
case 24: | ||
if raw_len < 2 { | ||
return 0, false | ||
} | ||
val = int(raw[offset]) | ||
offset++ | ||
if mt == 0xe0 && uint64(raw[offset]) < 32 { | ||
return 0, false | ||
} | ||
case 25: | ||
if raw_len < 3 { | ||
return 0, false | ||
} | ||
val = int(BgEn.Uint16(raw[offset : offset+2])) | ||
offset += 2 | ||
case 26: | ||
if raw_len < 5 { | ||
return 0, false | ||
} | ||
val = int(BgEn.Uint32(raw[offset : offset+4])) | ||
offset += 4 | ||
case 27: | ||
if raw_len < 9 { | ||
return 0, false | ||
} | ||
val = int(BgEn.Uint64(raw[offset : offset+8])) | ||
offset += 8 | ||
case 31: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In specification:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I am returning |
||
switch mt { | ||
case 0x00, 0x20, 0xc0: | ||
return 0, false | ||
case 0xe0: | ||
return 0, false | ||
} | ||
default: | ||
if ai > 24 { // ie. case 28: case 29: case 30 | ||
return 0, false | ||
} | ||
} | ||
|
||
switch mt { | ||
case 0x40, 0x60: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In specification:
|
||
if ai == 31 { | ||
return cborIndefinite(raw, mt, offset) | ||
} | ||
if val < 0 || len(raw)-offset < val { | ||
return 0, false | ||
} | ||
offset += val | ||
case 0x80, 0xa0: | ||
if ai == 31 { | ||
return cborIndefinite(raw, mt, offset) | ||
} | ||
if val < 0 { | ||
return 0, false | ||
} | ||
count := 1 | ||
if mt == 0xa0 { | ||
count = 2 | ||
} | ||
for i := 0; i < val*count; i++ { | ||
var ok bool | ||
offset, ok = cborHelper(raw, offset) | ||
if !ok { | ||
return 0, false | ||
} | ||
} | ||
case 0xc0: | ||
return cborHelper(raw, offset) | ||
default: | ||
return 0, false | ||
} | ||
return offset, true | ||
} | ||
|
||
func cborIndefinite(raw []byte, mt uint8, offset int) (int, bool) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I guess this function is the equivalent of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should be equivalent to the |
||
var ok bool | ||
i := 0 | ||
for { | ||
if len(raw) == offset { | ||
return 0, false | ||
} | ||
if raw[offset] == 0xff { | ||
offset++ | ||
break | ||
} | ||
offset, ok = cborHelper(raw, offset) | ||
if !ok { | ||
return 0, false | ||
} | ||
i++ | ||
} | ||
if mt == 0xa0 && i%2 == 1 { | ||
return 0, false | ||
} | ||
return offset, true | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z�t2013-03-21T20:04:00Z |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the specification mt is:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I used
0x40, 0x60, 0x80...
because those are the raw byte values that CBOR uses. A bitwise AND with0xe0
(11100000
) has the same effect as making 5 right shifts (>> 5
) - it discards the 5 last bits of the byte. Testing was a easier like this, but I will change it to2, 3, 4...
to avoid confusion.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No,
>> 5
is not the same as& 0xe0
. https://play.golang.org/p/NFgj0z0Mt9Q