Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
skythen committed Jan 7, 2021
0 parents commit 34043ae
Show file tree
Hide file tree
Showing 7 changed files with 1,833 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: go

go:
- 1.x

before_install:
- export GO111MODULE=on
- go get github.com/mattn/goveralls
- if ! go get github.com/golang/tools/cmd/cover; then go get golang.org/x/tools/cmd/cover; fi

script:
- go test -c -covermode=count -coverpkg=github.com/skythen/bertlv
- ./bertlv.test -test.coverprofile coverage.cov
- $HOME/gopath/bin/goveralls -service=travis-ci -coverprofile=coverage.cov
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 skythen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# BER-TLV

[![Build Status](https://travis-ci.org/skythen/bertlv.svg?branch=master)](https://travis-ci.org/skythen/bertlv)
[![Coverage Status](https://coveralls.io/repos/github/bertlv/badge.svg)](https://coveralls.io/github/skythen/bertlv)
[![GoDoc](https://godoc.org/github.com/skythen/bertlv?status.svg)](http://godoc.org/github.com/skythen/bertlv)
[![Go Report Card](https://goreportcard.com/badge/github.com/skythen/bertlv)](https://goreportcard.com/report/github.com/skythen/bertlv)

Package bertlv implements parsing and building of BER-TLV structures.

Please note that this is not a complete implementation of the X.690 standard as it is agnostic about classes (Universal, Application, Context-specific, Private) and therefore does not check for correct encoding of tags/values.

`go get github.com/skythen/bertlv`

## Parse

You can parse BER-TLV encoded data from bytes:

```go
b := []byte{0x71, 0x10, 0xB0, 0x0E, 0x0F, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05, 0x0E, 0x05, 0x05, 0x04, 0x03, 0x02, 0x01}
bertlvs, err := Parse(b)
```
### Constructed objects
You can check if a BerTLV is constructed and get first or all children or filter child objects by tag:
```go
if bertlvs[0].Tag.IsConstructed() {
child := bertlvs[0].FirstChild(NewOneByteTag(0x0F))
}
```

## Create
You can create single BER-TLVs with NewBerTLV:
```go
val := []byte{0xB0, 0x0E, 0x0F, 0x05, 0x01, 0x02, 0x03, 0x04, 0x05, 0x0E, 0x05, 0x05, 0x04, 0x03, 0x02, 0x01}
bertlv, err := NewBerTLV(NewOneByteTag(0x71), val)
```

If you want to create complex constructed objects you use the Builder:
```go
builder := Builder{}
nestedBuilder := Builder{}
anotherNestedBuilder := Builder{}

berTlvs, err := builder.AddBytes(NewOneByteTag(0x71), // first level, constructed object
nestedBuilder.AddBytes(NewOneByteTag(0xB0), // second level, constructed object
anotherNestedBuilder.
AddBytes(NewOneByteTag(0x0F), []byte{0x01, 0x02, 0x03, 0x04, 0x05}). // third level primitive object
AddBytes(NewOneByteTag(0x0E), []byte{0x05, 0x04, 0x03, 0x02, 0x01}). // third level primitive object
Bytes()).
Bytes()).
BuildBerTLVs()
```

You can also use
- builder.AddEmpty() to add objects without value
- builder.AddByte() to add objects with a value that consists of a single byte
- builder.AddRaw() to add raw bytes
Loading

0 comments on commit 34043ae

Please sign in to comment.