This repository has been archived by the owner on Jun 23, 2023. It is now read-only.
forked from jda/routeros-api-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmtbyteproto.go
84 lines (76 loc) · 1.7 KB
/
mtbyteproto.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
package routeros
import (
"bytes"
)
type mtbyteprotoError error
// Get just one byte because MT's size prefix is overoptimized
func (c *Client) getone() int {
charlet := make([]byte, 1)
_, err := c.conn.Read(charlet)
if err != nil {
panic(mtbyteprotoError(err))
}
numlet := int(charlet[0])
return numlet
}
// Decode RouterOS API Word Size Prefix / Figure out how much to read
// TODO: based on MT Docs. Look for way to make this cleaner later
func (client *Client) getlen() int64 {
c := int64(client.getone())
if (c & 0x80) == 0x00 {
} else if (c & 0xC0) == 0x80 {
c &= ^0xC0
c <<= 8
c += int64(client.getone())
} else if (c & 0xE0) == 0xC0 {
c &= ^0xE0
c <<= 8
c += int64(client.getone())
c <<= 8
c += int64(client.getone())
} else if (c & 0xF0) == 0xE0 {
c &= ^0xF0
c <<= 8
c += int64(client.getone())
c <<= 8
c += int64(client.getone())
c <<= 8
c += int64(client.getone())
} else if (c & 0xF8) == 0xF0 {
c = int64(client.getone())
c <<= 8
c += int64(client.getone())
c <<= 8
c += int64(client.getone())
c <<= 8
c += int64(client.getone())
}
return c
}
// Calculate RouterOS API Word Size Prefix
func prefixlen(l int) *bytes.Buffer {
var b bytes.Buffer
switch {
case l < 0x80:
b.WriteByte(byte(l))
case l < 0x4000:
b.WriteByte(byte(l>>8) | 0x80)
b.WriteByte(byte(l))
case l < 0x200000:
b.WriteByte(byte(l>>16) | 0xC0)
b.WriteByte(byte(l >> 8))
b.WriteByte(byte(l))
case l < 0x10000000:
b.WriteByte(byte(l>>24) | 0xE0)
b.WriteByte(byte(l >> 16))
b.WriteByte(byte(l >> 8))
b.WriteByte(byte(l))
default:
b.WriteByte(0xF0)
b.WriteByte(byte(l >> 24))
b.WriteByte(byte(l >> 16))
b.WriteByte(byte(l >> 8))
b.WriteByte(byte(l))
}
return &b
}