-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrangemaker
executable file
·378 lines (328 loc) · 10.2 KB
/
rangemaker
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#!/usr/bin/tclsh
interp recursionlimit {} 20000
package require inifile
package require cmdline
set myoptions {
{c.arg "" "Config file name"}
{b.arg "" "Big pieces filen ame"}
{s.arg "" "Small pieces file name"}
{t "print and sum the counts"}
{r "print ranges for inspection"}
{e "print the command file"}
}
set usage ": MyCommandName \[options] filename ...\noptions:"
set myusage "[::cmdline::getArgv0] <-c configfilename> <-b> bigpiecesfilename> <-s smallpiecesfilename>"
set myusage {}
append myusage \n { -r (print ranges for inspection)]}
append myusage \n { -c print range counts for inspection]}
append myusage \n { -e print a command file}
try {
array set cmdpars [::cmdline::getoptions argv $myoptions ""]
} trap {CMDLINE USAGE} {msg o} {
# Trap the usage signal, print the message, and exit the application.
# Note: Other errors are not caught and passed through to higher levels!
puts $msg
exit 1
}
if {![file exists $cmdpars(c)]} {
puts "config file $cmdpars(c) not found"
exit 1
}
if {![file exists $cmdpars(b)]} {
puts "big pieces file $cmdpars(b) not found"
exit 1
}
if {![file exists $cmdpars(s)]} {
puts "small pieces file $cmdpars(s) not found"
exit 1
}
try {
set cfg [::ini::open $cmdpars(c) r]
set mncmccmap [::ini::get $cfg MNCMCCtoCCNDC]
array set cmdfilepars [::ini::get $cfg CMDFILE]
::ini::close $cfg
} trap {} res {
puts $res
puts "There is an issue with the config file"
exit 1
}
# Create the required routing table entries for a range
proc range_squash { start end } {
if {$start == $end} {
return $start
}
set output {}
set len [string length $start]
#calculate the base
set cnt 0
while {$cnt < $len} {
if {[string range $start 0 $cnt] != [string range $end 0 $cnt]} {
set baselen [expr $cnt -1]
break
}
incr cnt 1
}
set base [string range $start 0 $baselen]
set left [string trimright [string range $start $cnt end] 0]
set right [string trimright [string range $end $cnt end] 9]
while {[string length $left] < [string length $right] } { append left 0}
while {[string length $right] < [string length $left] } { append right 9}
if {$left == $right} {
return [list $base$left ]
}
set bigleft [string range $left 0 0]
set bigright [string range $right 0 0]
if {[regexp {^0*$} $left]} {
set bigleft 0
set left {}
}
if {[regexp {^9*$} $right]} {
if {$bigright < 9} {
incr bigright 1
}
set right {}
}
while {$bigright >= $bigleft} {
lappend output $base$bigleft
incr bigleft 1
}
while {[string length $left] >= 2} {
set digcnt [string range $left end end]
set left [string range $left 0 [expr [string length $left] - 2]]
while {$digcnt <= 9} {
lappend output $base$left$digcnt
incr digcnt
}
}
while {[string length $right] >= 2} {
set digcnt [string range $right end end]
set right [string range $right 0 [expr [string length $right] - 2]]
while {$digcnt >= 0} {
if {[lsearch -glob $output $base$right$digcnt?] < 0 } {
lappend output $base$right$digcnt
}
incr digcnt -1
}
if {[regexp {^0*$} $right]} {
set right 0
}
}
set new_output {}
foreach entry $output {
if {[lsearch -regexp $output ^$entry\[0-9\] ] < 0} {
lappend new_output $entry
}
}
return $new_output
}
proc makearange {range} {
global smallpieces
global ranges
lassign $range biggerfirst biggerlast biggergtrc
set count 0
set biggerlastplace $biggerfirst
foreach smallpiece $smallpieces {
lassign $smallpiece smallerfirst smallerlast smallergtrc
if {$smallerfirst < $biggerfirst || $smallerlast > $biggerlast } {
# The range don't fit so let it go
continue
}
if {$smallerfirst == $biggerfirst && $smallerlast == $biggerlast} {
#Range overlap
#puts "WARNING $smallpiece overlaps $range"
continue
}
if {$smallergtrc == $biggergtrc} {
# This range goes to the same place as the containing range so skip it
#discard $smallpiece "same as containing"
continue
}
if {$smallerfirst >= $biggerfirst && $smallerlast <= $biggerlast } {
#The range fits, so process it
if {$smallerfirst != $biggerfirst} {
if {[makearange [list $biggerfirst [expr $smallerfirst - 1] $biggergtrc] ] == 0} {
lappend ranges [list $biggerfirst [expr $smallerfirst - 1] $biggergtrc]
}
incr count 1
}
if {[makearange [list $smallerfirst $smallerlast $smallergtrc] ] == 0} {
lappend ranges [list $smallerfirst $smallerlast $smallergtrc]
}
incr count 1
if {$smallerlast != $biggerlast} {
if {[makearange [list [expr $smallerlast + 1] $biggerlast $biggergtrc] ] == 0} {
lappend ranges [list [expr $smallerlast + 1] $biggerlast $biggergtrc]
}
incr count 1
}
set biggerfirst $biggerlast
}
}
return $count
}
#Utility to make the counters readable
proc commify {num {sep ,}} {
while {[regsub {^([-+]?\d+)(\d\d\d)} $num "\\1$sep\\2" num]} {}
return $num
}
proc expand {str dig len} {
regsub -all {[^0-9]} $str {} str
while { [string length $str] < $len } {
append str $dig
}
return $str
}
proc rangify {line len} {
if {[llength $line] == 2} {
set start [expand [mccmncremap [lindex $line 0]] 0 $len ]
set end [expand [mccmncremap [lindex $line 0]] 9 $len ]
set gtrc [lindex $line 1]
} elseif {[llength $line] == 3} {
set start [expand [mccmncremap [lindex $line 0]] 0 $len ]
set end [expand [mccmncremap [lindex $line 1]] 0 $len ]
set gtrc [lindex $line 2]
}
return [list $start $end $gtrc]
}
proc mccmncremap {text} {
global mncmccmap
foreach {from to} $mncmccmap {
regsub ^$from $text $to text
}
return $text
}
###############################################################################
#
# MAIN processing starts here
#
###############################################################################
#use the first MCCMNC in the config file to determine MSIN length
# According to this place: https://imei.org/blog/imsi-number
set msinlen [expr 15 - [string length [lindex $mncmccmap 0]]]
#use the first CCNDC in the config file for ccndclen
set ccndclen [string length [lindex $mncmccmap 1]]
set worklen [expr $msinlen + $ccndclen]
set bigpieces {}
set bh [open $cmdpars(b)]
while {[gets $bh line] >= 0} {
if {[regexp {[^0-9 \t]} $line] || $line=={}} {
continue
}
lappend bigpieces [rangify $line $worklen]
}
close $bh
set bigpieces [lsort -index 0 $bigpieces]
set smallpieces {}
set sh [open $cmdpars(s)]
while {[gets $sh line] >= 0} {
if {[regexp {[^0-9 \t]} $line] || $line=={}} {
continue
}
lappend smallpieces [rangify $line $worklen]
}
close $sh
set smallpieces [lsort -index 0 $smallpieces]
set ranges {}
foreach bigpiece $bigpieces {
if {[makearange $bigpiece] == 0} {
lappend ranges $bigpiece
}
}
if {$cmdpars(r)} {
set total 0
foreach range $ranges {
lassign $range start end gtrc
if {$cmdpars(t)} {
set size [expr $end - $start + 1]
incr total $size
puts [format {%14d %14d %-10s %15s} $start $end $gtrc [commify $size]]
} else {
puts [format {%14d %14d %-10s} $start $end $gtrc]
}
}
if {$cmdpars(t)} {
puts [format {%14s %14s %-10s %15s} "" "" "" [commify $total]]
}
puts ""
}
set output {}
foreach range $ranges {
lassign $range start end gtrc
foreach ns [range_squash $start $end] {
lappend output [list $ns $gtrc]
}
}
proc rtrim1 {ns} {
return [string range $ns 0 [expr [string length $ns] -2 ]]
}
set newoutput $output
set goagain 1
while {$goagain == 1} {
set stack {}
set regns {}
set reggt {}
set regct 0
set output $newoutput
set newoutput {}
set newlines {}
set goagain 0
foreach line $output {
lassign $line ns gt
if {$regns == {} } {
set regns [rtrim1 $ns]
set reggt $gt
set regct 0
lappend stack $line
continue
}
if {[rtrim1 $ns] == $regns && $gt == $reggt} {
incr regct 1
lappend stack $line
} else {
lappend newoutput {*}$stack
set stack {}
lappend stack $line
set regns [rtrim1 $ns]
set reggt $gt
set regct 0
}
if {$regct == 9} {
lappend newoutput "$regns $reggt"
set stack {}
set regns {}
set reggt {}
set goagain 1
}
}
lappend newoutput {*}$stack
}
set output $newoutput
if {$cmdpars(e)} {
#Print a command file
set start $cmdfilepars(CMD):
set sep ""
foreach par [split $cmdfilepars(PARS) ,] {
append start $sep $par = $cmdfilepars($par)
set sep ,
}
foreach line [lsort -index 0 $output] {
lassign $line ns gtrc
puts $start,NS=$ns,GTRC=$gtrc\;
}
} else {
#print only NS and GTRC
set total 0
foreach line [lsort -index 0 $output] {
lassign $line ns gtrc
if {$cmdpars(t)} {
set size [expr int(pow(10,[expr 14 - [string length $ns]]))]
incr total $size
puts [format {%-15s %-8s %16s} $ns $gtrc [commify $size]]
} else {
puts [format {%-15s %-8s} $ns $gtrc]
}
}
if {$cmdpars(t)} {
puts [format {%-15s %-8s %16s} "" "" "" [commify $total]]
}
}