-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandleCompress.go
84 lines (57 loc) · 1.49 KB
/
handleCompress.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
package balloon
import (
//"fmt"
"strings"
"strconv"
"encoding/hex"
"bufio"
)
func HandleCompress( new_ip []byte, str_port string, compare bool, scanner *bufio.Scanner ) ( string, string, []byte, bool, *bufio.Scanner ) {
//init variables
str_ip := ""
var line string
//MAIN
scanner.Scan()
line = scanner.Text()
//nothing left to scan
if len(line) == 0 {
return "", "", nil, false, nil
}
line = strings.TrimSuffix(line, "\n")
s := strings.Split(line,",")
if len(s) != 2 {
panic("Error parsing compressed-input list")
}
portHex, ipHex := s[0], s[1]
ipByte, err := hex.DecodeString(ipHex)
if err != nil {
panic(err)
}
//fmt.Printf("Prev ipByte: %d\n",new_ip)
//fmt.Printf("Cur ipByte: %d\n",ipByte)
portByte, emptyPort := strconv.ParseInt(portHex, 16, 16)
//fmt.Printf("Cur port: %d\n",portByte)
//fmt.Printf("Prev port: %s\n",str_port)
//fill in the rest of the IP
if compare {
for i, bytte := range ipByte {
new_ip[i] = bytte
}
} else {
compare = true
new_ip = ipByte
str_port = strconv.Itoa(int(portByte))
}
//conver array of bytes to ip address
for i, bytte := range new_ip {
str_ip +=strconv.Itoa(int(bytte))
if i < 3 {
str_ip += "."
}
}
if emptyPort == nil {
str_port = strconv.Itoa(int(portByte))
}
//return ip:port
return str_ip, str_port, new_ip, compare, scanner
}//end