forked from xluohome/phonedata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
phonedata.go
188 lines (172 loc) · 4.33 KB
/
phonedata.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package phonedata
import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"os"
"path"
"runtime"
)
const (
CMCC byte = iota + 0x01 //中国移动
CUCC //中国联通
CTCC //中国电信
CTCC_v //电信虚拟运营商
CUCC_v //联通虚拟运营商
CMCC_v //移动虚拟运营商
CBCC //中国广电
CBCC_v //广电虚拟运营商
INT_LEN = 4
CHAR_LEN = 1
HEAD_LENGTH = 8
PHONE_INDEX_LENGTH = 9
PHONE_DAT = "phone.dat"
)
type PhoneRecord struct {
PhoneNum string
Province string
City string
ZipCode string
AreaZone string
CardType string
}
var (
content []byte
CardTypemap = map[byte]string{
CMCC: "中国移动",
CUCC: "中国联通",
CTCC: "中国电信",
CBCC: "中国广电",
CTCC_v: "中国电信虚拟运营商",
CUCC_v: "中国联通虚拟运营商",
CMCC_v: "中国移动虚拟运营商",
CBCC_v: "中国广电虚拟运营商",
}
total_len, firstoffset int32
)
func init() {
dir := os.Getenv("PHONE_DATA_DIR")
if dir == "" {
_, fulleFilename, _, _ := runtime.Caller(0)
dir = path.Dir(fulleFilename)
}
var err error
content, err = ioutil.ReadFile(path.Join(dir, PHONE_DAT))
if err != nil {
panic(err)
}
total_len = int32(len(content))
firstoffset = get4(content[INT_LEN : INT_LEN*2])
}
func Debug() {
fmt.Println(version())
fmt.Println(totalRecord())
fmt.Println(firstRecordOffset())
}
func (pr PhoneRecord) String() string {
return fmt.Sprintf("PhoneNum: %s\nAreaZone: %s\nCardType: %s\nCity: %s\nZipCode: %s\nProvince: %s\n", pr.PhoneNum, pr.AreaZone, pr.CardType, pr.City, pr.ZipCode, pr.Province)
}
func get4(b []byte) int32 {
if len(b) < 4 {
return 0
}
return int32(b[0]) | int32(b[1])<<8 | int32(b[2])<<16 | int32(b[3])<<24
}
func getN(s string) (uint32, error) {
var n, cutoff, maxVal uint32
i := 0
base := 10
cutoff = (1<<32-1)/10 + 1
maxVal = 1<<uint(32) - 1
for ; i < len(s); i++ {
var v byte
d := s[i]
switch {
case '0' <= d && d <= '9':
v = d - '0'
case 'a' <= d && d <= 'z':
v = d - 'a' + 10
case 'A' <= d && d <= 'Z':
v = d - 'A' + 10
default:
return 0, errors.New("invalid syntax")
}
if v >= byte(base) {
return 0, errors.New("invalid syntax")
}
if n >= cutoff {
// n*base overflows
n = (1<<32 - 1)
return n, errors.New("value out of range")
}
n *= uint32(base)
n1 := n + uint32(v)
if n1 < n || n1 > maxVal {
// n+v overflows
n = (1<<32 - 1)
return n, errors.New("value out of range")
}
n = n1
}
return n, nil
}
func version() string {
return string(content[0:INT_LEN])
}
func totalRecord() int32 {
return (int32(len(content)) - firstRecordOffset()) / PHONE_INDEX_LENGTH
}
func firstRecordOffset() int32 {
return get4(content[INT_LEN : INT_LEN*2])
}
// 二分法查询phone数据
func Find(phone_num string) (pr *PhoneRecord, err error) {
if len(phone_num) < 7 || len(phone_num) > 11 {
return nil, errors.New("illegal phone length")
}
var left int32
phone_seven_int, err := getN(phone_num[0:7])
if err != nil {
return nil, errors.New("illegal phone number")
}
phone_seven_int32 := int32(phone_seven_int)
right := (total_len - firstoffset) / PHONE_INDEX_LENGTH
for {
if left > right {
break
}
mid := (left + right) / 2
offset := firstoffset + mid*PHONE_INDEX_LENGTH
if offset >= total_len {
break
}
cur_phone := get4(content[offset : offset+INT_LEN])
record_offset := get4(content[offset+INT_LEN : offset+INT_LEN*2])
card_type := content[offset+INT_LEN*2 : offset+INT_LEN*2+CHAR_LEN][0]
switch {
case cur_phone > phone_seven_int32:
right = mid - 1
case cur_phone < phone_seven_int32:
left = mid + 1
default:
cbyte := content[record_offset:]
end_offset := int32(bytes.Index(cbyte, []byte("\000")))
data := bytes.Split(cbyte[:end_offset], []byte("|"))
card_str, ok := CardTypemap[card_type]
if !ok {
card_str = "未知电信运营商"
}
pr = &PhoneRecord{
PhoneNum: phone_num,
Province: string(data[0]),
City: string(data[1]),
ZipCode: string(data[2]),
AreaZone: string(data[3]),
CardType: card_str,
}
return
}
}
return nil, errors.New("phone's data not found")
}