github.com/egonelbre/exp@v0.0.0-20240430123955-ed1d3aa93911/smime/ber/common.go (about)

     1  package ber
     2  
     3  type Token struct {
     4  	Kind  Kind
     5  	Class int
     6  	Tag   int
     7  	Bytes []byte
     8  }
     9  
    10  type Kind int
    11  
    12  const (
    13  	Value Kind = iota
    14  	Constructed
    15  	EndConstructed
    16  )
    17  
    18  const (
    19  	Universal   = 0x00
    20  	Application = 0x01
    21  	Context     = 0x02
    22  	Private     = 0x03
    23  )
    24  
    25  const (
    26  	TagEOC              = 0x00
    27  	TagBool             = 0x01
    28  	TagInteger          = 0x02
    29  	TagBitString        = 0x03
    30  	TagOctetString      = 0x04
    31  	TagNULL             = 0x05
    32  	TagObjectIdentifier = 0x06
    33  	TagObjectDescriptor = 0x07
    34  	TagExternal         = 0x08
    35  	TagRealFloat        = 0x09
    36  	TagEnumerated       = 0x0a
    37  	TagEmbeddedPDV      = 0x0b
    38  	TagUTF8String       = 0x0c
    39  	TagRelativeOID      = 0x0d
    40  	TagSequence         = 0x10
    41  	TagSet              = 0x11
    42  	TagNumericString    = 0x12
    43  	TagPrintableString  = 0x13
    44  	TagT61String        = 0x14
    45  	TagVideotexString   = 0x15
    46  	TagIA5String        = 0x16
    47  	TagUTCTime          = 0x17
    48  	TagGeneralizedTime  = 0x18
    49  	TagGraphicString    = 0x19
    50  	TagVisibleString    = 0x1a
    51  	TagGeneralString    = 0x1b
    52  	TagUniversalString  = 0x1c
    53  	TagCharacterString  = 0x1d
    54  	TagBMPString        = 0x1e
    55  )
    56  
    57  // A StructuralError suggests that the ASN.1 data is valid, but the Go type
    58  // which is receiving it doesn't match.
    59  type StructuralError struct {
    60  	Msg string
    61  }
    62  
    63  func (e StructuralError) Error() string { return "asn1: structure error: " + e.Msg }
    64  
    65  // A SyntaxError suggests that the ASN.1 data is invalid.
    66  type SyntaxError struct {
    67  	Msg string
    68  }
    69  
    70  func (e SyntaxError) Error() string { return "asn1: syntax error: " + e.Msg }
    71  
    72  func parseBase128Int(bytes []byte, initOffset int) (ret, offset int, err error) {
    73  	offset = initOffset
    74  	for shifted := 0; offset < len(bytes); shifted++ {
    75  		if shifted > 4 {
    76  			err = StructuralError{"base 128 integer too large"}
    77  			return
    78  		}
    79  		ret <<= 7
    80  		b := bytes[offset]
    81  		ret |= int(b & 0x7f)
    82  		offset++
    83  		if b&0x80 == 0 {
    84  			return
    85  		}
    86  	}
    87  	err = SyntaxError{"truncated base 128 integer"}
    88  	return
    89  }
    90  
    91  var unimplementedType = StructuralError{"unimplemented type"}