Skip to content
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 method to get a BER encoded tag's number #39

Merged
merged 1 commit into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions encoding/tlv/ber.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,31 @@
return Class(t >> (alignedBitLen - 2))
}

// Returns the BER encoded number of the tag
func (t Tag) BERNumber() uint {
bitLen := bits.Len(uint(t))
var byteLen int
if bitLen%8 == 0 {
byteLen = bitLen / 8
} else {
byteLen = (bitLen + 8 - (bitLen % 8)) / 8
}
switch byteLen {
case 0:
return 0

Check warning on line 64 in encoding/tlv/ber.go

View check run for this annotation

Codecov / codecov/patch

encoding/tlv/ber.go#L63-L64

Added lines #L63 - L64 were not covered by tests
case 1:
return uint(t) & 0x1F
case 2:
return uint(t) & 0x7F
case 3:
return ((uint(t) & (0x7F << 8)) >> 1) | (uint(t) & 0x7F)
case 4:
return ((uint(t) & (0x7F << 16)) >> 2) | ((uint(t) & (0x7F << 8)) >> 1) | (uint(t) & 0x7F)
default:
return 0

Check warning on line 74 in encoding/tlv/ber.go

View check run for this annotation

Codecov / codecov/patch

encoding/tlv/ber.go#L73-L74

Added lines #L73 - L74 were not covered by tests
}
}

func (t Tag) IsConstructed() bool {
u := t
for u > 0xFF {
Expand Down
20 changes: 11 additions & 9 deletions encoding/tlv/ber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,23 @@ func TestTagBER(t *testing.T) {

cases := []struct {
tag tlv.Tag
number uint
class tlv.Class
constructed bool
}{
{tlv.NewBERTag(0x2, tlv.ClassContext), tlv.ClassContext, false},
{tlv.NewBERTag(0x20, tlv.ClassUniversal), tlv.ClassUniversal, false},
{tlv.NewBERTag(0x200, tlv.ClassContext), tlv.ClassContext, false},
{tlv.NewBERTag(0x20000, tlv.ClassPrivate), tlv.ClassPrivate, false},
{0x21, tlv.ClassUniversal, true},
{0x01, tlv.ClassUniversal, false},
{0x41, tlv.ClassApplication, false},
{0x81, tlv.ClassContext, false},
{0xC1, tlv.ClassPrivate, false},
{tlv.NewBERTag(0x2, tlv.ClassContext), 0x2, tlv.ClassContext, false},
{tlv.NewBERTag(0x20, tlv.ClassUniversal), 0x20, tlv.ClassUniversal, false},
{tlv.NewBERTag(0x200, tlv.ClassContext), 0x200, tlv.ClassContext, false},
{tlv.NewBERTag(0x20000, tlv.ClassPrivate), 0x20000, tlv.ClassPrivate, false},
{0x21, 0x01, tlv.ClassUniversal, true},
{0x01, 0x01, tlv.ClassUniversal, false},
{0x41, 0x01, tlv.ClassApplication, false},
{0x81, 0x01, tlv.ClassContext, false},
{0xC1, 0x01, tlv.ClassPrivate, false},
}

for _, c := range cases {
require.Equal(c.number, c.tag.BERNumber())
require.Equal(c.class, c.tag.Class())
require.Equal(c.constructed, c.tag.IsConstructed())
}
Expand Down