aboutsummaryrefslogtreecommitdiff
path: root/ber/base128.go
blob: 47776515e62b90aa2ec93c572f6ca4ca83317d74 (plain)
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
package ber

func base128(n int) (b []byte) {
	if n == 0 {
		return []byte{0}
	}
	l := 0
	for i := n; i > 0; i >>= 7 {
		l++
	}
	for i := l - 1; i >= 0; i-- {
		o := byte(n>>uint(i*7)) & 0x7f
		if i != 0 {
			o |= 0x80
		}
		b = append(b, o)
	}
	return
}

func debase128(b []byte) (i, n int) {
	for _, v := range b {
		i <<= 7
		i |= int(v & 0x7f)
		n++
		if v&0x80 == 0 {
			return
		}
	}
	return
}