-
Notifications
You must be signed in to change notification settings - Fork 5
/
codec.go
78 lines (73 loc) · 1.17 KB
/
codec.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
package codec
const (
ENCODE_DEFAULT = iota
ENCODE_BIT_NOT
ENCODE_BYTE_RVS
ENCODE_LOOP_XOR
)
func Encode(tp int, buff []byte) {
switch tp {
case ENCODE_BIT_NOT:
{
for i := 0; i < len(buff); i++ {
buff[i] = ^buff[i]
}
}
case ENCODE_BYTE_RVS:
{
length := len(buff)
if len(buff)%2 != 0 {
length -= 1
}
for i := 0; i < length; i += 2 {
buff[i], buff[i+1] = buff[i+1], buff[i]
}
}
case ENCODE_LOOP_XOR:
{
length := len(buff)
if length <= 1 {
break
}
for i := 0; i < length-1; i++ {
buff[i+1] ^= buff[i]
}
buff[0] ^= buff[length-1]
}
case ENCODE_DEFAULT:
default:
}
}
func Decode(tp int, buff []byte) {
switch tp {
case ENCODE_BIT_NOT:
{
for i := 0; i < len(buff); i++ {
buff[i] = ^buff[i]
}
}
case ENCODE_BYTE_RVS:
{
length := len(buff)
if len(buff)%2 != 0 {
length -= 1
}
for i := 0; i < length; i += 2 {
buff[i], buff[i+1] = buff[i+1], buff[i]
}
}
case ENCODE_LOOP_XOR:
{
length := len(buff)
if length <= 1 {
break
}
buff[0] ^= buff[length-1]
for i := length - 1; i > 0; i-- {
buff[i] ^= buff[i-1]
}
}
case ENCODE_DEFAULT:
default:
}
}