/
csv2bin.go
1742 lines (1639 loc) · 46.8 KB
/
csv2bin.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
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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"bufio"
"encoding/binary"
"encoding/csv"
"fmt"
"io"
"math/big"
"net"
"os"
"strconv"
"strings"
)
var countryPosition = [27]uint8{0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2}
var regionPosition = [27]uint8{0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3}
var cityPosition = [27]uint8{0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4}
var latitudePosition = [27]uint8{0, 0, 0, 0, 0, 5, 5, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}
var longitudePosition = [27]uint8{0, 0, 0, 0, 0, 6, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6}
var zipCodePosition = [27]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 0, 7, 7, 7, 0, 7, 0, 7, 7, 7, 0, 7, 7, 7}
var timeZonePosition = [27]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 8, 7, 8, 8, 8, 7, 8, 0, 8, 8, 8, 0, 8, 8, 8}
var ispPosition = [27]uint8{0, 0, 3, 0, 5, 0, 7, 5, 7, 0, 8, 0, 9, 0, 9, 0, 9, 0, 9, 7, 9, 0, 9, 7, 9, 9, 9}
var domainPosition = [27]uint8{0, 0, 0, 0, 0, 0, 0, 6, 8, 0, 9, 0, 10, 0, 10, 0, 10, 0, 10, 8, 10, 0, 10, 8, 10, 10, 10}
var netSpeedPosition = [27]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 8, 11, 0, 11, 8, 11, 0, 11, 0, 11, 0, 11, 11, 11}
var iddCodePosition = [27]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 12, 0, 12, 0, 12, 9, 12, 0, 12, 12, 12}
var areaCodePosition = [27]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 13, 0, 13, 0, 13, 10, 13, 0, 13, 13, 13}
var weatherStationCodePosition = [27]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 14, 0, 14, 0, 14, 0, 14, 14, 14}
var weatherStationNamePosition = [27]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 15, 0, 15, 0, 15, 0, 15, 15, 15}
var mccPosition = [27]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 16, 0, 16, 9, 16, 16, 16}
var mncPosition = [27]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 10, 17, 0, 17, 10, 17, 17, 17}
var mobileBrandPosition = [27]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 18, 0, 18, 11, 18, 18, 18}
var elevationPosition = [27]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 11, 19, 0, 19, 19, 19}
var usageTypePosition = [27]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 20, 20, 20}
var addressTypePosition = [27]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, 21}
var categoryPosition = [27]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 22, 22}
var districtPosition = [27]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 23}
var asnPosition = [27]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 24}
var asPosition = [27]uint8{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25}
var columnSize = [27]uint8{0, 2, 3, 4, 5, 6, 7, 6, 8, 7, 9, 8, 10, 8, 11, 10, 13, 10, 15, 11, 18, 11, 19, 12, 20, 22, 25}
type countryType struct {
addr uint32
long string
}
func WriteBIN(input string, output string, dbPackage string) {
var err error
var ispCase uint8 = 0 // need to perform some data manipulation if CSV is IPv6 and contains ISP field
var dbYear uint8 = 21
var dbMonth uint8 = 1
var dbDay uint8 = 20
var dbProductCode uint8 = 1 // 1 for IP2Location, 2 for IP2Proxy
var dbProductType uint8 = 3 // 1 for commercial, 2 for LITE, 3 for generated by this script
var dbFileSize uint32 = 0 // dummy, calculate later after file written
var dbType64 uint64
if dbType64, err = strconv.ParseUint(dbPackage, 10, 8); err != nil {
fmt.Println("Invalid DB package.")
return
}
dbType := uint8(dbType64)
var ipv4IndexBase uint32 = 64
var ipv6IndexBase uint32 = ipv4IndexBase + (256 * 256 * 8)
ipv4IndexRowMin := make(map[uint32]uint32, 65535)
ipv4IndexRowMax := make(map[uint32]uint32, 65535)
ipv6IndexRowMin := make(map[uint32]uint32, 65535)
ipv6IndexRowMax := make(map[uint32]uint32, 65535)
var ipv4Count uint32 = 0
var ipv4Base uint32 = ipv6IndexBase + (256 * 256 * 8)
var longSize uint32 = 4
dbColl := columnSize[dbType]
var ipv6Count uint32 = 0
var ipv6Base uint32 = 0
// cannot have initial size as we won't know the total elements for each until we read the CSV
country := map[string]*countryType{}
region := map[string]uint32{}
city := map[string]uint32{}
zipCode := map[string]uint32{}
timeZone := map[string]uint32{}
isp := map[string]uint32{}
domain := map[string]uint32{}
netSpeed := map[string]uint32{}
iddCode := map[string]uint32{}
areaCode := map[string]uint32{}
weatherStationCode := map[string]uint32{}
weatherStationName := map[string]uint32{}
mcc := map[string]uint32{}
mnc := map[string]uint32{}
mobileBrand := map[string]uint32{}
elevation := map[string]uint32{}
usageType := map[string]uint32{}
addressType := map[string]uint32{}
category := map[string]uint32{}
district := map[string]uint32{}
asn := map[string]uint32{}
as := map[string]uint32{}
countryEnabled := (countryPosition[dbType] > 0)
regionEnabled := (regionPosition[dbType] > 0)
cityEnabled := (cityPosition[dbType] > 0)
latitudeEnabled := (latitudePosition[dbType] > 0)
longitudeEnabled := (longitudePosition[dbType] > 0)
zipCodeEnabled := (zipCodePosition[dbType] > 0)
timeZoneEnabled := (timeZonePosition[dbType] > 0)
ispEnabled := (ispPosition[dbType] > 0)
domainEnabled := (domainPosition[dbType] > 0)
netSpeedEnabled := (netSpeedPosition[dbType] > 0)
iddCodeEnabled := (iddCodePosition[dbType] > 0)
areaCodeEnabled := (areaCodePosition[dbType] > 0)
weatherStationCodeEnabled := (weatherStationCodePosition[dbType] > 0)
weatherStationNameEnabled := (weatherStationNamePosition[dbType] > 0)
mccEnabled := (mccPosition[dbType] > 0)
mncEnabled := (mncPosition[dbType] > 0)
mobileBrandEnabled := (mobileBrandPosition[dbType] > 0)
elevationEnabled := (elevationPosition[dbType] > 0)
usageTypeEnabled := (usageTypePosition[dbType] > 0)
addressTypeEnabled := (addressTypePosition[dbType] > 0)
categoryEnabled := (categoryPosition[dbType] > 0)
districtEnabled := (districtPosition[dbType] > 0)
asnEnabled := (asnPosition[dbType] > 0)
asEnabled := (asPosition[dbType] > 0)
var countrySorted []string
var regionSorted []string
var citySorted []string
var zipCodeSorted []string
var timeZoneSorted []string
var ispSorted []string
var domainSorted []string
var netSpeedSorted []string
var iddCodeSorted []string
var areaCodeSorted []string
var weatherStationCodeSorted []string
var weatherStationNameSorted []string
var mccSorted []string
var mncSorted []string
var mobileBrandSorted []string
var elevationSorted []string
var usageTypeSorted []string
var addressTypeSorted []string
var categorySorted []string
var districtSorted []string
var asnSorted []string
var asSorted []string
lastIPv4To := ""
lastIPv6To := ""
var inFile *os.File
if inFile, err = os.Open(input); err != nil {
fmt.Printf("Invalid input file %v.\n", input)
return
}
defer inFile.Close()
delim := ','
var rdr *csv.Reader
inFileBuffered := bufio.NewReaderSize(inFile, 65536)
csvRdr := csv.NewReader(inFileBuffered)
csvRdr.Comma = delim
csvRdr.LazyQuotes = false
rdr = csvRdr
lines := 0
for {
parts, err := rdr.Read()
if err == io.EOF {
break
} else if err != nil {
fmt.Println("Unable to read input file.")
return
}
lines++
if parts[2] == "UK" {
parts[2] = "GB"
}
if countryEnabled {
if _, ok := country[parts[2]]; !ok { // if map key not exist
country[parts[2]] = &countryType{addr: 0, long: parts[3]}
}
}
if regionEnabled {
region[parts[regionPosition[dbType]+1]] = 1
}
if cityEnabled {
city[parts[cityPosition[dbType]+1]] = 1
}
if zipCodeEnabled {
zipCode[parts[zipCodePosition[dbType]+1]] = 1
}
if timeZoneEnabled {
timeZone[parts[timeZonePosition[dbType]+1]] = 1
}
if ispEnabled {
if lines == 1 {
if strings.Contains(parts[ispPosition[dbType]+1], "Broadcast RFC1700") {
ispCase = 4 // IPv4 case: no special handling required for the moment
} else {
ispCase = 6 // IPv6 case: need to manipulate some rows at the start due to differences in the original input CSV files (pure IPv4 & pure IPv6 vs. merged IP files)
}
}
isp[parts[ispPosition[dbType]+1]] = 1
}
if domainEnabled {
domain[parts[domainPosition[dbType]+1]] = 1
}
if netSpeedEnabled {
netSpeed[parts[netSpeedPosition[dbType]+1]] = 1
}
if iddCodeEnabled {
iddCode[parts[iddCodePosition[dbType]+1]] = 1
}
if areaCodeEnabled {
areaCode[parts[areaCodePosition[dbType]+1]] = 1
}
if weatherStationCodeEnabled {
weatherStationCode[parts[weatherStationCodePosition[dbType]+1]] = 1
}
if weatherStationNameEnabled {
weatherStationName[parts[weatherStationNamePosition[dbType]+1]] = 1
}
if mccEnabled {
mcc[parts[mccPosition[dbType]+1]] = 1
}
if mncEnabled {
mnc[parts[mncPosition[dbType]+1]] = 1
}
if mobileBrandEnabled {
mobileBrand[parts[mobileBrandPosition[dbType]+1]] = 1
}
if elevationEnabled {
elevation[parts[elevationPosition[dbType]+1]] = 1
}
if usageTypeEnabled {
usageType[parts[usageTypePosition[dbType]+1]] = 1
}
if addressTypeEnabled {
addressType[parts[addressTypePosition[dbType]+1]] = 1
}
if categoryEnabled {
category[parts[categoryPosition[dbType]+1]] = 1
}
if districtEnabled {
district[parts[districtPosition[dbType]+1]] = 1
}
if asnEnabled {
asn[parts[asnPosition[dbType]+1]] = 1
}
if asEnabled {
as[parts[asPosition[dbType]+1]] = 1
}
startNum := new(big.Int)
startNum, _ = startNum.SetString(parts[0], 10)
endNum := new(big.Int)
endNum, _ = endNum.SetString(parts[1], 10)
var startIP net.IP
var endIP net.IP
if ispCase == 6 && ((lines >= 1 && lines <= 4) || strings.Contains(parts[ispPosition[dbType]+1], "Broadcast RFC1700")) { // special case when ISP field is present in IPv6 CSV
// First 4 lines treat as IPv6 (16 bytes)
// After 4th line, insert special 5th line for IPv4Map (hardcode this line for insertion)
// Special case: Broadcast RFC1700 line convert to Ipv4 and insert under IPv4 section
if lines >= 1 && lines <= 4 {
// first 4 lines must treat as IPv6 to insert under IPv6 section
no2From, err := GetIPv6First2Octet(parts[0])
if err != nil {
fmt.Println("Unable to get first 2 octets.")
return
}
no2To, err := GetIPv6First2Octet(parts[1])
if err != nil {
fmt.Println("Unable to get first 2 octets.")
return
}
for no2 := no2From; no2 <= no2To; no2++ {
if _, ok := ipv6IndexRowMin[no2]; !ok { // if map key not exist
ipv6IndexRowMin[no2] = ipv6Count
}
ipv6IndexRowMax[no2] = ipv6Count
}
lastIPv6To = parts[1]
ipv6Count++
if lines == 4 {
// need to manually insert another line for IPv4Map
isp["IPv4Map"] = 1
no2From, err := GetIPv6First2Octet("281470681743360")
if err != nil {
fmt.Println("Unable to get first 2 octets.")
return
}
no2To, err := GetIPv6First2Octet("281474976710655")
if err != nil {
fmt.Println("Unable to get first 2 octets.")
return
}
for no2 := no2From; no2 <= no2To; no2++ {
if _, ok := ipv6IndexRowMin[no2]; !ok { // if map key not exist
ipv6IndexRowMin[no2] = ipv6Count
}
ipv6IndexRowMax[no2] = ipv6Count
}
lastIPv6To = "281474976710655"
ipv6Count++
}
} else {
// Broadcast RFC1700 case where we need to insert into IPv4 section with different IP numbers
no2From, err := GetIPv4First2Octet("0")
if err != nil {
fmt.Println("Unable to get first 2 octets.")
return
}
no2To, err := GetIPv4First2Octet("16777215")
if err != nil {
fmt.Println("Unable to get first 2 octets.")
return
}
for no2 := no2From; no2 <= no2To; no2++ {
if _, ok := ipv4IndexRowMin[no2]; !ok { // if map key not exist
ipv4IndexRowMin[no2] = ipv4Count
}
ipv4IndexRowMax[no2] = ipv4Count
}
lastIPv4To = "16777215"
ipv4Count++
}
} else { // normal case where ISP field not present or is IPv4 CSV or rows not covered by the above criteria
if startIP, err = DecimalToIPv4(startNum); err != nil {
if startIP, err = DecimalToIPv6(startNum); err != nil {
fmt.Println("Decimal to IP conversion failed.")
return
}
}
startIPStr := startIP.String() // this will return plain IPv4 from IPv4-mapped IPv6 since we only want plain IPv4
if endIP, err = DecimalToIPv4(endNum); err != nil {
if endIP, err = DecimalToIPv6(endNum); err != nil {
fmt.Println("Decimal to IP conversion failed.")
return
}
}
endIPStr := endIP.String() // this will return plain IPv4 from IPv4-mapped IPv6 since we only want plain IPv4
var newStartNum *big.Int
var newEndNum *big.Int
startIPType := 0
if IsIPv4(startIPStr) {
startIPType = 4
if newStartNum, err = IPv4ToDecimal(startIPStr); err != nil {
fmt.Println("IP to decimal conversion failed.")
return
}
parts[0] = newStartNum.String()
} else if IsIPv6(startIPStr) {
startIPType = 6
}
endIPType := 0
if IsIPv4(endIPStr) {
endIPType = 4
if newEndNum, err = IPv4ToDecimal(endIPStr); err != nil {
fmt.Println("IP to decimal conversion failed.")
return
}
parts[1] = newEndNum.String()
} else if IsIPv6(endIPStr) {
endIPType = 6
}
if startIPType == 4 && endIPType == 4 {
no2From, err := GetIPv4First2Octet(parts[0])
if err != nil {
fmt.Println("Unable to get first 2 octets.")
return
}
no2To, err := GetIPv4First2Octet(parts[1])
if err != nil {
fmt.Println("Unable to get first 2 octets.")
return
}
for no2 := no2From; no2 <= no2To; no2++ {
if _, ok := ipv4IndexRowMin[no2]; !ok { // if map key not exist
ipv4IndexRowMin[no2] = ipv4Count
}
ipv4IndexRowMax[no2] = ipv4Count
}
lastIPv4To = parts[1]
ipv4Count++
} else if startIPType == 6 && endIPType == 6 {
no2From, err := GetIPv6First2Octet(parts[0])
if err != nil {
fmt.Println("Unable to get first 2 octets.")
return
}
no2To, err := GetIPv6First2Octet(parts[1])
if err != nil {
fmt.Println("Unable to get first 2 octets.")
return
}
for no2 := no2From; no2 <= no2To; no2++ {
if _, ok := ipv6IndexRowMin[no2]; !ok { // if map key not exist
ipv6IndexRowMin[no2] = ipv6Count
}
ipv6IndexRowMax[no2] = ipv6Count
}
lastIPv6To = parts[1]
ipv6Count++
} else if startIPType == 4 && endIPType == 6 { // special boundary case where we need to split the range into IPv4 only and IPv6 only
// IPv4 range
no2From, err := GetIPv4First2Octet(parts[0])
if err != nil {
fmt.Println("Unable to get first 2 octets.")
return
}
no2To, err := GetIPv4First2Octet(maxIPv4Range.String()) // 255.255.255.255
if err != nil {
fmt.Println("Unable to get first 2 octets.")
return
}
for no2 := no2From; no2 <= no2To; no2++ {
if _, ok := ipv4IndexRowMin[no2]; !ok { // if map key not exist
ipv4IndexRowMin[no2] = ipv4Count
}
ipv4IndexRowMax[no2] = ipv4Count
}
lastIPv4To = maxIPv4Range.String()
ipv4Count++
// IPv6 range
no2From2, err := GetIPv6First2Octet(maxIPv4RangePlusOne.String())
if err != nil {
fmt.Println("Unable to get first 2 octets.")
return
}
no2To2, err := GetIPv6First2Octet(parts[1])
if err != nil {
fmt.Println("Unable to get first 2 octets.")
return
}
for no2 := no2From2; no2 <= no2To2; no2++ {
if _, ok := ipv6IndexRowMin[no2]; !ok { // if map key not exist
ipv6IndexRowMin[no2] = ipv6Count
}
ipv6IndexRowMax[no2] = ipv6Count
}
lastIPv6To = parts[1]
ipv6Count++
}
}
}
if lastIPv4To != "4294967295" {
fmt.Printf("The last IP address in the CSV file is %s not 4294967295.\n", lastIPv4To)
return
}
if lastIPv6To != "" && lastIPv6To != "340282366920938463463374607431768211455" { // blank means IPv4-only CSV
fmt.Printf("The last IP address in the CSV file is %s not 340282366920938463463374607431768211455.\n", lastIPv6To)
return
}
ipv4Count++
if lastIPv6To != "" { // if is IPv6 CSV
ipv6Count++
}
ipv6Base = ipv4Base + ipv4Count*longSize*uint32(dbColl)
addr := ipv6Base + ipv6Count*longSize*(uint32(dbColl)+3) // IPv6 address range is 4 bytes vs 1 byte in IPv4
if countryEnabled {
countrySorted = GetSortedKeysCountry(country)
for _, v := range countrySorted {
country[v].addr = addr
addr = addr + 1 + 2 + 1 + uint32(len(country[v].long))
}
}
if regionEnabled {
regionSorted = GetSortedKeys(region)
for _, v := range regionSorted {
region[v] = addr
addr = addr + 1 + uint32(len(v))
}
}
if cityEnabled {
citySorted = GetSortedKeys(city)
for _, v := range citySorted {
city[v] = addr
addr = addr + 1 + uint32(len(v))
}
}
if zipCodeEnabled {
zipCodeSorted = GetSortedKeys(zipCode)
for _, v := range zipCodeSorted {
zipCode[v] = addr
addr = addr + 1 + uint32(len(v))
}
}
if timeZoneEnabled {
timeZoneSorted = GetSortedKeys(timeZone)
for _, v := range timeZoneSorted {
timeZone[v] = addr
addr = addr + 1 + uint32(len(v))
}
}
if ispEnabled {
ispSorted = GetSortedKeys(isp)
for _, v := range ispSorted {
isp[v] = addr
addr = addr + 1 + uint32(len(v))
}
}
if domainEnabled {
domainSorted = GetSortedKeys(domain)
for _, v := range domainSorted {
domain[v] = addr
addr = addr + 1 + uint32(len(v))
}
}
if netSpeedEnabled {
netSpeedSorted = GetSortedKeys(netSpeed)
for _, v := range netSpeedSorted {
netSpeed[v] = addr
addr = addr + 1 + uint32(len(v))
}
}
if iddCodeEnabled {
iddCodeSorted = GetSortedKeys(iddCode)
for _, v := range iddCodeSorted {
iddCode[v] = addr
addr = addr + 1 + uint32(len(v))
}
}
if areaCodeEnabled {
areaCodeSorted = GetSortedKeys(areaCode)
for _, v := range areaCodeSorted {
areaCode[v] = addr
addr = addr + 1 + uint32(len(v))
}
}
if weatherStationCodeEnabled {
weatherStationCodeSorted = GetSortedKeys(weatherStationCode)
for _, v := range weatherStationCodeSorted {
weatherStationCode[v] = addr
addr = addr + 1 + uint32(len(v))
}
}
if weatherStationNameEnabled {
weatherStationNameSorted = GetSortedKeys(weatherStationName)
for _, v := range weatherStationNameSorted {
weatherStationName[v] = addr
addr = addr + 1 + uint32(len(v))
}
}
if mccEnabled {
mccSorted = GetSortedKeys(mcc)
for _, v := range mccSorted {
mcc[v] = addr
addr = addr + 1 + uint32(len(v))
}
}
if mncEnabled {
mncSorted = GetSortedKeys(mnc)
for _, v := range mncSorted {
mnc[v] = addr
addr = addr + 1 + uint32(len(v))
}
}
if mobileBrandEnabled {
mobileBrandSorted = GetSortedKeys(mobileBrand)
for _, v := range mobileBrandSorted {
mobileBrand[v] = addr
addr = addr + 1 + uint32(len(v))
}
}
if elevationEnabled {
elevationSorted = GetSortedKeys(elevation)
for _, v := range elevationSorted {
elevation[v] = addr
addr = addr + 1 + uint32(len(v))
}
}
if usageTypeEnabled {
usageTypeSorted = GetSortedKeys(usageType)
for _, v := range usageTypeSorted {
usageType[v] = addr
addr = addr + 1 + uint32(len(v))
}
}
if addressTypeEnabled {
addressTypeSorted = GetSortedKeys(addressType)
for _, v := range addressTypeSorted {
addressType[v] = addr
addr = addr + 1 + uint32(len(v))
}
}
if categoryEnabled {
categorySorted = GetSortedKeys(category)
for _, v := range categorySorted {
category[v] = addr
addr = addr + 1 + uint32(len(v))
}
}
if districtEnabled {
districtSorted = GetSortedKeys(district)
for _, v := range districtSorted {
district[v] = addr
addr = addr + 1 + uint32(len(v))
}
}
if asnEnabled {
asnSorted = GetSortedKeys(asn)
for _, v := range asnSorted {
asn[v] = addr
addr = addr + 1 + uint32(len(v))
}
}
if asEnabled {
asSorted = GetSortedKeys(as)
for _, v := range asSorted {
as[v] = addr
addr = addr + 1 + uint32(len(v))
}
}
var inFile2 *os.File
if inFile2, err = os.Open(input); err != nil {
fmt.Printf("Invalid input file %v.\n", input)
return
}
defer inFile2.Close()
var outFile *os.File
if outFile, err = os.Create(output); err != nil {
fmt.Printf("Could not create output file %v.\n", output)
return
}
defer outFile.Close()
var header = []any{
dbType,
dbColl,
dbYear,
dbMonth,
dbDay,
ipv4Count,
ipv4Base + 1,
ipv6Count,
ipv6Base + 1,
ipv4IndexBase + 1,
ipv6IndexBase + 1,
dbProductCode,
dbProductType,
dbFileSize,
}
for _, v := range header {
// due to different data types, we loop and let the Write perform the conversion to bytes
WriteMe(outFile, v)
}
bytes := make([]byte, 63-35+1) // bunch of zero bytes
WriteMe(outFile, bytes)
var sortedUint []uint32
sortedUint = GetSortedKeysUint(ipv4IndexRowMin)
for _, v := range sortedUint {
WriteMe(outFile, ipv4IndexRowMin[v])
WriteMe(outFile, ipv4IndexRowMax[v])
}
sortedUint = GetSortedKeysUint(ipv6IndexRowMin)
for _, v := range sortedUint {
WriteMe(outFile, ipv6IndexRowMin[v])
WriteMe(outFile, ipv6IndexRowMax[v])
}
p := Tell(outFile)
if p != 1048640 {
fmt.Println("Index out of range.")
return
}
var rdr2 *csv.Reader
inFileBuffered2 := bufio.NewReaderSize(inFile2, 65536)
csvRdr2 := csv.NewReader(inFileBuffered2)
csvRdr2.Comma = delim
csvRdr2.LazyQuotes = false
rdr2 = csvRdr2
lines = 0
// these 2 are for ISP IPv6 special cases to rearrange some rows
var row4 = []any{}
var row6 = []any{}
var outputV4Ending = 0 // force output the ending row for IPv4 in ISP IPv6 case
for {
parts, err := rdr2.Read()
if err == io.EOF {
break
} else if err != nil {
fmt.Println("Unable to read input file.")
return
}
lines++
startNum := new(big.Int)
startNum, _ = startNum.SetString(parts[0], 10)
endNum := new(big.Int)
endNum, _ = endNum.SetString(parts[1], 10)
var startIP net.IP
var endIP net.IP
if parts[2] == "UK" {
parts[2] = "GB"
}
var row = []any{}
if ispCase == 6 && ((lines >= 1 && lines <= 4) || strings.Contains(parts[ispPosition[dbType]+1], "Broadcast RFC1700")) { // special case when ISP field is present in IPv6 CSV
outputV4Ending = 1
if lines >= 1 && lines <= 4 {
// These 4 lines should be IPv6 even though first 3 lines are showing IPv4
v6Bytes, err := ForceAsIPv6(parts[0])
if err != nil {
fmt.Println("IP to bytes conversion failed.")
return
}
ReverseBytes(v6Bytes)
row6 = append(row6, v6Bytes)
if countryEnabled {
row6 = append(row6, country[parts[2]].addr)
}
if regionEnabled {
row6 = append(row6, region[parts[regionPosition[dbType]+1]])
}
if cityEnabled {
row6 = append(row6, city[parts[cityPosition[dbType]+1]])
}
if latitudeEnabled {
lat, err := strconv.ParseFloat(parts[latitudePosition[dbType]+1], 64)
if err != nil {
fmt.Println("String to float conversion failed.")
return
}
row6 = append(row6, float32(lat))
}
if longitudeEnabled {
long, err := strconv.ParseFloat(parts[longitudePosition[dbType]+1], 64)
if err != nil {
fmt.Println("String to float conversion failed.")
return
}
row6 = append(row6, float32(long))
}
if zipCodeEnabled {
row6 = append(row6, zipCode[parts[zipCodePosition[dbType]+1]])
}
if timeZoneEnabled {
row6 = append(row6, timeZone[parts[timeZonePosition[dbType]+1]])
}
if ispEnabled {
row6 = append(row6, isp[parts[ispPosition[dbType]+1]])
}
if domainEnabled {
row6 = append(row6, domain[parts[domainPosition[dbType]+1]])
}
if netSpeedEnabled {
row6 = append(row6, netSpeed[parts[netSpeedPosition[dbType]+1]])
}
if iddCodeEnabled {
row6 = append(row6, iddCode[parts[iddCodePosition[dbType]+1]])
}
if areaCodeEnabled {
row6 = append(row6, areaCode[parts[areaCodePosition[dbType]+1]])
}
if weatherStationCodeEnabled {
row6 = append(row6, weatherStationCode[parts[weatherStationCodePosition[dbType]+1]])
}
if weatherStationNameEnabled {
row6 = append(row6, weatherStationName[parts[weatherStationNamePosition[dbType]+1]])
}
if mccEnabled {
row6 = append(row6, mcc[parts[mccPosition[dbType]+1]])
}
if mncEnabled {
row6 = append(row6, mnc[parts[mncPosition[dbType]+1]])
}
if mobileBrandEnabled {
row6 = append(row6, mobileBrand[parts[mobileBrandPosition[dbType]+1]])
}
if elevationEnabled {
row6 = append(row6, elevation[parts[elevationPosition[dbType]+1]])
}
if usageTypeEnabled {
row6 = append(row6, usageType[parts[usageTypePosition[dbType]+1]])
}
if addressTypeEnabled {
row6 = append(row6, addressType[parts[addressTypePosition[dbType]+1]])
}
if categoryEnabled {
row6 = append(row6, category[parts[categoryPosition[dbType]+1]])
}
if districtEnabled {
row6 = append(row6, district[parts[districtPosition[dbType]+1]])
}
if asnEnabled {
row6 = append(row6, asn[parts[asnPosition[dbType]+1]])
}
if asEnabled {
row6 = append(row6, as[parts[asPosition[dbType]+1]])
}
if lines == 4 {
// need to manually insert another line for IPv4Map (should be IPv6)
v6Bytes, err := ForceAsIPv6("281470681743360")
if err != nil {
fmt.Println("IP to bytes conversion failed.")
return
}
ReverseBytes(v6Bytes)
row6 = append(row6, v6Bytes)
if countryEnabled {
row6 = append(row6, country["-"].addr)
}
if regionEnabled {
row6 = append(row6, region["-"])
}
if cityEnabled {
row6 = append(row6, city["-"])
}
if latitudeEnabled {
lat, err := strconv.ParseFloat("0.000000", 64)
if err != nil {
fmt.Println("String to float conversion failed.")
return
}
row6 = append(row6, float32(lat))
}
if longitudeEnabled {
long, err := strconv.ParseFloat("0.000000", 64)
if err != nil {
fmt.Println("String to float conversion failed.")
return
}
row6 = append(row6, float32(long))
}
if zipCodeEnabled {
row6 = append(row6, zipCode["-"])
}
if timeZoneEnabled {
row6 = append(row6, timeZone["-"])
}
if ispEnabled {
row6 = append(row6, isp["IPv4Map"])
}
if domainEnabled {
row6 = append(row6, domain["-"])
}
if netSpeedEnabled {
row6 = append(row6, netSpeed["-"])
}
if iddCodeEnabled {
row6 = append(row6, iddCode["-"])
}
if areaCodeEnabled {
row6 = append(row6, areaCode["-"])
}
if weatherStationCodeEnabled {
row6 = append(row6, weatherStationCode["-"])
}
if weatherStationNameEnabled {
row6 = append(row6, weatherStationName["-"])
}
if mccEnabled {
row6 = append(row6, mcc["-"])
}
if mncEnabled {
row6 = append(row6, mnc["-"])
}
if mobileBrandEnabled {
row6 = append(row6, mobileBrand["-"])
}
if elevationEnabled {
row6 = append(row6, elevation["0"])
}
if usageTypeEnabled {
row6 = append(row6, usageType["RSV"])
}
if addressTypeEnabled {
row6 = append(row6, addressType["U"])
}
if categoryEnabled {
row6 = append(row6, category["IAB24"])
}
if districtEnabled {
row6 = append(row6, district["-"])
}
if asnEnabled {
row6 = append(row6, asn["-"])
}
if asEnabled {
row6 = append(row6, as["-"])
}
}
} else {
// Broadcast RFC1700 case where we need to insert into IPv4 section with different IPv4 IP numbers
v4Bytes, err := ForceAsIPv4("0")
if err != nil {
fmt.Println("IP to bytes conversion failed.")
return
}
ReverseBytes(v4Bytes)
row = append(row, v4Bytes)
if countryEnabled {
row = append(row, country[parts[2]].addr)
}
if regionEnabled {
row = append(row, region[parts[regionPosition[dbType]+1]])
}
if cityEnabled {
row = append(row, city[parts[cityPosition[dbType]+1]])
}
if latitudeEnabled {
lat, err := strconv.ParseFloat(parts[latitudePosition[dbType]+1], 64)
if err != nil {
fmt.Println("String to float conversion failed.")
return
}
row = append(row, float32(lat))
}
if longitudeEnabled {
long, err := strconv.ParseFloat(parts[longitudePosition[dbType]+1], 64)
if err != nil {
fmt.Println("String to float conversion failed.")
return
}
row = append(row, float32(long))
}
if zipCodeEnabled {
row = append(row, zipCode[parts[zipCodePosition[dbType]+1]])
}
if timeZoneEnabled {
row = append(row, timeZone[parts[timeZonePosition[dbType]+1]])
}
if ispEnabled {
row = append(row, isp[parts[ispPosition[dbType]+1]])
}
if domainEnabled {
row = append(row, domain[parts[domainPosition[dbType]+1]])
}
if netSpeedEnabled {
row = append(row, netSpeed[parts[netSpeedPosition[dbType]+1]])
}
if iddCodeEnabled {
row = append(row, iddCode[parts[iddCodePosition[dbType]+1]])
}
if areaCodeEnabled {
row = append(row, areaCode[parts[areaCodePosition[dbType]+1]])
}
if weatherStationCodeEnabled {
row = append(row, weatherStationCode[parts[weatherStationCodePosition[dbType]+1]])