// Copyright 2009 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package berimport ()const ( tagBoolean = 1 tagInteger = 2 tagBitString = 3 tagOctetString = 4 tagNull = 5 tagOID = 6 tagEnum = 10 tagUTF8String = 12 tagSequence = 16 tagSet = 17 tagNumericString = 18 tagPrintableString = 19 tagT61String = 20 tagIA5String = 22 tagUTCTime = 23 tagGeneralizedTime = 24 tagGeneralString = 27 tagBMPString = 30)const ( classUniversal = 0 classApplication = 1 classContextSpecific = 2 classPrivate = 3)type tagAndLength struct { class, tag, length int isCompound bool}// ASN.1 has IMPLICIT and EXPLICIT tags, which can be translated as "instead// of" and "in addition to". When not specified, every primitive type has a// default tag in the UNIVERSAL class.//// For example: a BIT STRING is tagged [UNIVERSAL 3] by default (although ASN.1// doesn't actually have a UNIVERSAL keyword). However, by saying [IMPLICIT// CONTEXT-SPECIFIC 42], that means that the tag is replaced by another.//// On the other hand, if it said [EXPLICIT CONTEXT-SPECIFIC 10], then an// /additional/ tag would wrap the default tag. This explicit tag will have the// compound flag set.//// (This is used in order to remove ambiguity with optional elements.)//// You can layer EXPLICIT and IMPLICIT tags to an arbitrary depth, however we// don't support that here. We support a single layer of EXPLICIT or IMPLICIT// tagging with tag strings on the fields of a structure.// fieldParameters is the parsed representation of tag string from a structure field.type fieldParameters struct { optional bool// true iff the field is OPTIONAL explicit bool// true iff an EXPLICIT tag is in use. application bool// true iff an APPLICATION tag is in use. private bool// true iff a PRIVATE tag is in use. defaultValue *int64// a default value for INTEGER typed fields (maybe nil). tag *int// the EXPLICIT or IMPLICIT tag (maybe nil). stringType int// the string tag to use when marshaling. timeType int// the time tag to use when marshaling. set bool// true iff this should be encoded as a SET omitEmpty bool// true iff this should be omitted if empty when marshaling.// Invariants: // if explicit is set, tag is non-nil.}// Given a tag string with the format specified in the package comment,// parseFieldParameters will parse it into a fieldParameters structure,// ignoring unknown parts of the string.func parseFieldParameters( string) ( fieldParameters) {for , := rangestrings.Split(, ",") {switch {case == "optional": .optional = truecase == "explicit": .explicit = trueif .tag == nil { .tag = new(int) }case == "generalized": .timeType = tagGeneralizedTimecase == "utc": .timeType = tagUTCTimecase == "ia5": .stringType = tagIA5Stringcase == "printable": .stringType = tagPrintableStringcase == "numeric": .stringType = tagNumericStringcase == "utf8": .stringType = tagUTF8Stringcasestrings.HasPrefix(, "default:"): , := strconv.ParseInt([8:], 10, 64)if == nil { .defaultValue = new(int64) *.defaultValue = }casestrings.HasPrefix(, "tag:"): , := strconv.Atoi([4:])if == nil { .tag = new(int) *.tag = }case == "set": .set = truecase == "application": .application = trueif .tag == nil { .tag = new(int) }case == "private": .private = trueif .tag == nil { .tag = new(int) }case == "omitempty": .omitEmpty = true } }return}// Given a reflected Go type, getUniversalType returns the default tag number// and expected compound flag.func getUniversalType( reflect.Type) ( bool, int, , bool) {switch {caserawValueType:returntrue, -1, false, truecaseobjectIdentifierType:returnfalse, tagOID, false, truecasebitStringType:returnfalse, tagBitString, false, truecasetimeType:returnfalse, tagUTCTime, false, truecaseenumeratedType:returnfalse, tagEnum, false, truecasebigIntType:returnfalse, tagInteger, false, true }switch .Kind() {casereflect.Bool:returnfalse, tagBoolean, false, truecasereflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:returnfalse, tagInteger, false, truecasereflect.Struct:returnfalse, tagSequence, true, truecasereflect.Slice:if .Elem().Kind() == reflect.Uint8 {returnfalse, tagOctetString, false, true }ifstrings.HasSuffix(.Name(), "SET") {returnfalse, tagSet, true, true }returnfalse, tagSequence, true, truecasereflect.String:returnfalse, tagPrintableString, false, true }returnfalse, 0, false, false}
The pages are generated with Goldsv0.6.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds.