forked from warthog618/sms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
semioctet.go
100 lines (91 loc) · 2.36 KB
/
semioctet.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// SPDX-License-Identifier: MIT
//
// Copyright © 2018 Kent Gibson <[email protected]>.
// Package semioctet provides conversions to and from semioctet format.
package semioctet
import (
"errors"
"fmt"
)
// Mapping from half-octet value to UTF-8 digit.
// The trailing 'F' is never returned but is provided to detect fill.
var decodeDigits = "0123456789*#abcF"
// Decode converts a semi-octet encoded field into the corresponding array of
// UTF-8 digits, as per 3GPP TS 23.040 Section 9.1.2.3
//
// Conversion is terminated by the length of the src or dst.
// The length of the dst indicates the maximum number of digits to return.
// The return values are the decoded field (a slice of dst), the number of
// bytes read from src, and any error detected during the conversion.
func Decode(dst, src []byte) ([]byte, int, error) {
wi := 0
ri := 0
for wi < len(dst) && ri < len(src) {
d := decodeDigits[src[ri]&0x0f]
if d != 'F' {
dst[wi] = d
wi++
}
d = decodeDigits[src[ri]>>4]
ri++
if wi == len(dst) && d != 'F' {
return nil, ri, ErrMissingFill
}
if d == 'F' {
continue
}
dst[wi] = d
wi++
}
return dst[:wi], ri, nil
}
var encodeDigits = map[byte]int{
'0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7,
'8': 8, '9': 9, '*': 10, '#': 11, 'a': 12, 'b': 13, 'c': 14,
}
// Encode converts an array of UTF-8 digits into semi-octet format, as per 3GPP
// TS 23.040 Section 9.1.2.3
//
// The return values are the encoded field, and any error detected during the
// conversion.
func Encode(src []byte) ([]byte, error) {
l := (len(src) + 1) / 2
if l == 0 {
return append(src[:0:0], src...), nil
}
b := make([]byte, l)
hi := false
p := 0
wi := 0
for _, d := range src {
e, ok := encodeDigits[d]
if !ok {
return nil, ErrInvalidDigit(d)
}
if hi {
p = p | int(e<<4)
b[wi] = byte(p)
wi++
hi = false
} else {
p = e
hi = true
}
}
if hi {
p = p | 0xf0
b[wi] = byte(p)
}
return b, nil
}
// ErrInvalidDigit indicates that the digit can not be encoded into semioctet
// format.
type ErrInvalidDigit byte
func (e ErrInvalidDigit) Error() string {
return fmt.Sprintf("semioctet: invalid digit: '%c' - 0x%x", byte(e), int(e))
}
var (
// ErrMissingFill indicates the final src octet does not contain the
// expected fill character 'F'.
ErrMissingFill = errors.New("semioctet: last src octet missing expected fill")
)