aboutsummaryrefslogtreecommitdiff
path: root/types/serialize.go
diff options
context:
space:
mode:
Diffstat (limited to 'types/serialize.go')
-rw-r--r--types/serialize.go28
1 files changed, 28 insertions, 0 deletions
diff --git a/types/serialize.go b/types/serialize.go
new file mode 100644
index 0000000..da4bd9f
--- /dev/null
+++ b/types/serialize.go
@@ -0,0 +1,28 @@
+package types
+
+import (
+ "fmt"
+
+ "github.com/google/certificate-transparency-go/tls"
+)
+
+// Marshal marshals a TLS-encodable structure
+func Marshal(item interface{}) ([]byte, error) {
+ serialized, err := tls.Marshal(item)
+ if err != nil {
+ return nil, fmt.Errorf("tls.Marshal: %v", err)
+ }
+ return serialized, nil
+}
+
+// Unmarshal unmarshals a TLS-encoded structure
+func Unmarshal(serialized []byte, out interface{}) error {
+ extra, err := tls.Unmarshal(serialized, out)
+ if err != nil {
+ return fmt.Errorf("tls.Unmarshal: %v", err)
+ }
+ if len(extra) > 0 {
+ return fmt.Errorf("tls.Unmarshal: extra data: %X", extra)
+ }
+ return nil
+}