Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
cbor-gen
Commits
534767cc
Unverified
Commit
534767cc
authored
Aug 13, 2020
by
Whyrusleeping
Committed by
GitHub
Aug 13, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #39 from Stebalien/feat/validate
Add a validate function.
parents
958ddffe
829e0e1a
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
101 additions
and
0 deletions
+101
-0
validate.go
validate.go
+59
-0
validate_test.go
validate_test.go
+42
-0
No files found.
validate.go
0 → 100644
View file @
534767cc
package
typegen
import
(
"bytes"
"fmt"
"io"
)
// ValidateCBOR validates that a byte array is a single valid CBOR object.
func
ValidateCBOR
(
b
[]
byte
)
error
{
// The code here is basically identical to the previous function, it
// just doesn't copy.
br
:=
bytes
.
NewReader
(
b
)
// Allocate some scratch space.
scratch
:=
make
([]
byte
,
maxHeaderSize
)
for
remaining
:=
uint64
(
1
);
remaining
>
0
;
remaining
--
{
maj
,
extra
,
err
:=
CborReadHeaderBuf
(
br
,
scratch
)
if
err
!=
nil
{
return
err
}
switch
maj
{
case
MajUnsignedInt
,
MajNegativeInt
,
MajOther
:
// nothing fancy to do
case
MajByteString
,
MajTextString
:
if
extra
>
ByteArrayMaxLen
{
return
maxLengthError
}
if
uint64
(
br
.
Len
())
<
extra
{
return
io
.
ErrUnexpectedEOF
}
if
_
,
err
:=
br
.
Seek
(
int64
(
extra
),
io
.
SeekCurrent
);
err
!=
nil
{
return
err
}
case
MajTag
:
remaining
++
case
MajArray
:
if
extra
>
MaxLength
{
return
maxLengthError
}
remaining
+=
extra
case
MajMap
:
if
extra
>
MaxLength
{
return
maxLengthError
}
remaining
+=
extra
*
2
default
:
return
fmt
.
Errorf
(
"unhandled deferred cbor type: %d"
,
maj
)
}
}
if
br
.
Len
()
>
0
{
return
fmt
.
Errorf
(
"unexpected %d unread bytes"
,
br
.
Len
())
}
return
nil
}
validate_test.go
0 → 100644
View file @
534767cc
package
typegen
import
(
"bytes"
"testing"
)
func
TestValidateShort
(
t
*
testing
.
T
)
{
var
buf
bytes
.
Buffer
if
err
:=
WriteMajorTypeHeader
(
&
buf
,
MajByteString
,
100
);
err
!=
nil
{
t
.
Fatal
(
"failed to write header"
)
}
if
err
:=
ValidateCBOR
(
buf
.
Bytes
());
err
==
nil
{
t
.
Fatal
(
"expected an error checking truncated cbor"
)
}
}
func
TestValidateDouble
(
t
*
testing
.
T
)
{
var
buf
bytes
.
Buffer
if
err
:=
WriteBool
(
&
buf
,
false
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
err
:=
WriteBool
(
&
buf
,
false
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
err
:=
ValidateCBOR
(
buf
.
Bytes
());
err
==
nil
{
t
.
Fatal
(
"expected an error checking cbor with two objects"
)
}
}
func
TestValidate
(
t
*
testing
.
T
)
{
var
buf
bytes
.
Buffer
if
err
:=
WriteBool
(
&
buf
,
false
);
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
err
:=
ValidateCBOR
(
buf
.
Bytes
());
err
!=
nil
{
t
.
Fatal
(
err
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment