| 1 | package ber |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "errors" |
| 6 | "fmt" |
| 7 | "math" |
| 8 | "strconv" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | func encodeFloat(v float64) []byte { |
| 13 | switch { |
| 14 | case math.IsInf(v, 1): |
| 15 | return []byte{0x40} |
| 16 | case math.IsInf(v, -1): |
| 17 | return []byte{0x41} |
| 18 | case math.IsNaN(v): |
| 19 | return []byte{0x42} |
| 20 | case v == 0.0: |
| 21 | if math.Signbit(v) { |
| 22 | return []byte{0x43} |
| 23 | } |
| 24 | return []byte{} |
| 25 | default: |
| 26 | // we take the easy part ;-) |
| 27 | value := []byte(strconv.FormatFloat(v, 'G', -1, 64)) |
| 28 | var ret []byte |
| 29 | if bytes.Contains(value, []byte{'E'}) { |
| 30 | ret = []byte{0x03} |
| 31 | } else { |
| 32 | ret = []byte{0x02} |
| 33 | } |
| 34 | ret = append(ret, value...) |
| 35 | return ret |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func ParseReal(v []byte) (val float64, err error) { |
| 40 | if len(v) == 0 { |
| 41 | return 0.0, nil |
| 42 | } |
| 43 | switch { |
| 44 | case v[0]&0x80 == 0x80: |
| 45 | val, err = parseBinaryFloat(v) |
| 46 | case v[0]&0xC0 == 0x40: |
| 47 | val, err = parseSpecialFloat(v) |
| 48 | case v[0]&0xC0 == 0x0: |
| 49 | val, err = parseDecimalFloat(v) |
| 50 | default: |
| 51 | return 0.0, fmt.Errorf("invalid info block") |
| 52 | } |
| 53 | if err != nil { |
| 54 | return 0.0, err |
| 55 | } |
| 56 | |
| 57 | if val == 0.0 && !math.Signbit(val) { |
| 58 | return 0.0, errors.New("REAL value +0 must be encoded with zero-length value block") |
| 59 | } |
| 60 | return val, nil |
| 61 | } |
| 62 | |
| 63 | func parseBinaryFloat(v []byte) (float64, error) { |
| 64 | var info byte |
| 65 | var buf []byte |
| 66 | |
| 67 | info, v = v[0], v[1:] |
| 68 | |
| 69 | var base int |
| 70 | switch info & 0x30 { |
| 71 | case 0x00: |
| 72 | base = 2 |
| 73 | case 0x10: |
| 74 | base = 8 |
| 75 | case 0x20: |
| 76 | base = 16 |
| 77 | case 0x30: |
| 78 | return 0.0, errors.New("bits 6 and 5 of information octet for REAL are equal to 11") |
| 79 | } |
| 80 | |
| 81 | scale := uint((info & 0x0c) >> 2) |
| 82 | |
| 83 | var expLen int |
| 84 | switch info & 0x03 { |
| 85 | case 0x00: |
| 86 | expLen = 1 |
| 87 | case 0x01: |
| 88 | expLen = 2 |
| 89 | case 0x02: |
| 90 | expLen = 3 |
| 91 | case 0x03: |
| 92 | expLen = int(v[0]) |
| 93 | if expLen > 8 { |
| 94 | return 0.0, errors.New("too big value of exponent") |
| 95 | } |
| 96 | v = v[1:] |
| 97 | } |
| 98 | buf, v = v[:expLen], v[expLen:] |
| 99 | exponent, err := ParseInt64(buf) |
| 100 | if err != nil { |
| 101 | return 0.0, err |
| 102 | } |
| 103 | |
| 104 | if len(v) > 8 { |
| 105 | return 0.0, errors.New("too big value of mantissa") |
| 106 | } |
| 107 | |
| 108 | mant, err := ParseInt64(v) |
| 109 | if err != nil { |
| 110 | return 0.0, err |
| 111 | } |
| 112 | mantissa := mant << scale |
| 113 | |
| 114 | if info&0x40 == 0x40 { |
| 115 | mantissa = -mantissa |
| 116 | } |
| 117 | |
| 118 | return float64(mantissa) * math.Pow(float64(base), float64(exponent)), nil |
| 119 | } |
| 120 | |
| 121 | func parseDecimalFloat(v []byte) (val float64, err error) { |
| 122 | switch v[0] & 0x3F { |
| 123 | case 0x01: // NR form 1 |
| 124 | var iVal int64 |
| 125 | iVal, err = strconv.ParseInt(strings.TrimLeft(string(v[1:]), " "), 10, 64) |
| 126 | val = float64(iVal) |
| 127 | case 0x02, 0x03: // NR form 2, 3 |
| 128 | val, err = strconv.ParseFloat(strings.Replace(strings.TrimLeft(string(v[1:]), " "), ",", ".", -1), 64) |
| 129 | default: |
| 130 | err = errors.New("incorrect NR form") |
| 131 | } |
| 132 | if err != nil { |
| 133 | return 0.0, err |
| 134 | } |
| 135 | |
| 136 | if val == 0.0 && math.Signbit(val) { |
| 137 | return 0.0, errors.New("REAL value -0 must be encoded as a special value") |
| 138 | } |
| 139 | return val, nil |
| 140 | } |
| 141 | |
| 142 | func parseSpecialFloat(v []byte) (float64, error) { |
| 143 | if len(v) != 1 { |
| 144 | return 0.0, errors.New(`encoding of "special value" must not contain exponent and mantissa`) |
| 145 | } |
| 146 | switch v[0] { |
| 147 | case 0x40: |
| 148 | return math.Inf(1), nil |
| 149 | case 0x41: |
| 150 | return math.Inf(-1), nil |
| 151 | case 0x42: |
| 152 | return math.NaN(), nil |
| 153 | case 0x43: |
| 154 | return math.Copysign(0, -1), nil |
| 155 | } |
| 156 | return 0.0, errors.New(`encoding of "special value" not from ASN.1 standard`) |
| 157 | } |