smalltalk
/
osmo-st-asn1
Archived
1
0
Fork 0

ber: Start with BER encoding/decoding for streams

Start with a very simple tag class
This commit is contained in:
Holger Hans Peter Freyther 2011-03-29 21:25:20 +02:00
parent 3034cb9fc1
commit ec1e5a94db
3 changed files with 187 additions and 0 deletions

140
BERTLVStream.st Normal file
View File

@ -0,0 +1,140 @@
"
(C) 2011 by Holger Hans Peter Freyther
All Rights Reserved
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"
Object subclass: BERTag [
| classType tagValue constructed |
<category: 'osmo-asn1'>
<comment: 'I am a TAG as of X.690'>
BERTag class >> classUniversal [
<category: 'class'>
^ 0
]
BERTag class >> classApplication [
<category: 'class'>
^ 1
]
BERTag class >> classContext [
<category: 'class'>
^ 2
]
BERTag class >> classPrivate [
<category: 'class'>
^ 3
]
BERTag class >> new [
<category: 'creation'>
^ super new initialize
]
BERTag class >> parseFrom: aStream [
<category: 'creation'>
^ self new
parseFrom: aStream;
yourself
]
BERTag class >> fromTuple: aTuple [
<category: 'creation'>
^ self new
fromTuple: aTuple;
yourself
]
initialize [
<category: 'init'>
classType := BERTag classUniversal.
tagValue := 0.
constructed := false.
]
parseExtendedTag: aStream [
<category: 'decoding'>
^ self error: 'Extended tags are not implemented yet'.
]
parseFrom: aStream [
| tmp |
<category: 'decoding'>
tmp := aStream next.
classType := (tmp bitAnd: 16rC0) bitShift: -6.
constructed := (tmp bitAnd: 16r20) > 0.
tagValue := tmp bitAnd: 16r1F.
"This is an extended tag"
(tagValue = 16r1F) ifTrue: [
self parseExtendedTag: aStream.
].
]
fromTuple: aTuple [
<category: 'decoding'>
classType := aTuple first bitAnd: 16r3.
constructed := aTuple second not.
tagValue := aTuple third.
]
writeExtendedTag: aStream [
<category: 'encoding'>
self error: 'Cannot encode extended tag.'
]
writeOn: aStream [
<category: 'encoding'>
tagValue >= 16r1F
ifTrue: [self writeExtendedTag: aStream.]
ifFalse: [| tag |
tag := classType bitShift: 6.
self isConstructed ifTrue: [tag := tag bitOr: 16r20].
tag := tag bitOr: self tagValue.
aStream nextPut: tag.
].
]
classType [
<category: 'accessing'>
^ classType
]
isConstructed [
<category: 'accessing'>
^ constructed
]
isPrimitive [
<category: 'accessing'>
^ self isConstructed not
]
tagValue [
<category: 'accessing'>
^ tagValue
]
asTuple [
<category: 'conversion'>
^ Array with: self classType with: self isPrimitive with: self tagValue.
]
]

43
BERTLVStreamTest.st Normal file
View File

@ -0,0 +1,43 @@
"
(C) 2011 by Holger Hans Peter Freyther
All Rights Reserved
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"
TestCase subclass: BERTagTest [
testSimpleTag [
<category: 'test'>
self assert: (BERTag parseFrom: #(16rA1) asByteArray readStream) asTuple = #(2 false 1).
]
testFromTuple [
| tuple |
<category: 'test'>
tuple := #(2 false 1).
self assert: (BERTag fromTuple: tuple) asTuple = tuple.
]
testWriteTuple [
| tuple stream |
<category: 'test'>
tuple := #(2 false 1).
stream := WriteStream on: (ByteArray new: 1).
(BERTag fromTuple: tuple) writeOn: stream.
self assert: stream contents = #(16rA1) asByteArray
]
]

View File

@ -3,10 +3,14 @@
<namespace>Osmo</namespace>
<filein>BER.st</filein>
<filein>BERTLVStream.st</filein>
<test>
<sunit>Osmo.BERTest</sunit>
<sunit>Osmo.BERTagTest</sunit>
<sunit>Osmo.BERTLVStreamTest</sunit>
<filein>Tests.st</filein>
<filein>BERTLVStreamTest.st</filein>
</test>
<file>BER.st</file>