Skip to content

Commit 6ff27d6

Browse files
Fixed wrong value returned by CIDRToIPv6 function
1 parent ed9bb4f commit 6ff27d6

File tree

6 files changed

+33
-41
lines changed

6 files changed

+33
-41
lines changed

com/ip2location/Country.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
* <p>
1818
*
1919
* @author IP2Location.com
20-
* @version 8.11.1
20+
* @version 8.11.2
2121
*/
2222
public class Country {
2323
private final Map<String, Map<String, String>> records = new HashMap<>();

com/ip2location/IP2Location.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
* <p>
3535
*
3636
* @author IP2Location.com
37-
* @version 8.11.1
37+
* @version 8.11.2
3838
*/
3939
public class IP2Location {
4040
private static final Pattern pattern = Pattern.compile("^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$"); // IPv4

com/ip2location/IP2LocationWebService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* <p>
2727
*
2828
* @author IP2Location.com
29-
* @version 8.11.1
29+
* @version 8.11.2
3030
*/
3131
public class IP2LocationWebService {
3232
private static final Pattern pattern = Pattern.compile("^[\\dA-Z]{10}$");

com/ip2location/IPResult.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* <p>
88
*
99
* @author IP2Location.com
10-
* @version 8.11.1
10+
* @version 8.11.2
1111
*/
1212
public class IPResult {
1313
static final String NOT_SUPPORTED = "Not_Supported";
@@ -39,7 +39,7 @@ public class IPResult {
3939
String as;
4040
String status;
4141
boolean delay = false;
42-
String version = "Version 8.11.1";
42+
String version = "Version 8.11.2";
4343

4444
IPResult(String ipstring) {
4545
ip_address = ipstring;

com/ip2location/IPTools.java

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.ip2location;
22

3+
import org.apache.commons.lang3.StringUtils;
4+
35
import java.math.BigInteger;
46
import java.net.Inet4Address;
57
import java.net.Inet6Address;
@@ -32,7 +34,7 @@
3234
* <p>
3335
*
3436
* @author IP2Location.com
35-
* @version 8.11.1
37+
* @version 8.11.2
3638
*/
3739
public class IPTools {
3840
private static final BigInteger MAX_IPV4_RANGE = new BigInteger("4294967295");
@@ -413,8 +415,7 @@ public List<String> IPv6ToCIDR(String IPFrom, String IPTo) throws UnknownHostExc
413415

414416
} while (ipFromBin.compareTo(ipToBin) < 0);
415417

416-
for (Map.Entry<String, Integer>
417-
entry : networks.entrySet()) {
418+
for (Map.Entry<String, Integer> entry : networks.entrySet()) {
418419
result.add(CompressIPv6(BinaryToIP(entry.getKey())) + "/" + entry.getValue());
419420
}
420421

@@ -489,33 +490,26 @@ public String[] CIDRToIPv6(String CIDR) throws UnknownHostException {
489490
ip = arr[0];
490491
prefix = Integer.parseInt(arr[1]);
491492

492-
String hexStartAddress = ExpandIPv6(ip).replaceAll(":", "");
493-
String hexEndAddress = hexStartAddress;
493+
String[] parts = ExpandIPv6(ip).split("\\:");
494494

495-
int bits = 128 - prefix;
496-
int x;
497-
String y;
498-
int pos = 31;
499-
List<Integer> values;
500-
char[] tmp;
501-
while (bits > 0) {
502-
values = Arrays.asList(4, bits);
503-
x = Integer.parseInt(String.valueOf(hexEndAddress.charAt(pos)), 16);
504-
y = String.format("%x", (x | (int) (Math.pow(2, Collections.min(values)) - 1))); // single hex char
505-
506-
// replace char
507-
tmp = hexEndAddress.toCharArray();
508-
tmp[pos] = y.charAt(0);
509-
hexEndAddress = String.valueOf(tmp);
510-
511-
bits -= 4;
512-
pos -= 1;
495+
String bitStart = StringUtils.repeat('1', prefix) + StringUtils.repeat('0', 128 - prefix);
496+
String bitEnd = StringUtils.repeat('0', prefix) + StringUtils.repeat('1', 128 - prefix);
497+
498+
int chunkSize = 16;
499+
500+
String[] floors = bitStart.split("(?<=\\G.{" + chunkSize + "})");
501+
String[] ceilings = bitEnd.split("(?<=\\G.{" + chunkSize + "})");
502+
503+
List<String> startIP = new ArrayList(8);
504+
List<String> endIP = new ArrayList(8);
505+
506+
for (int x = 0; x < 8; x++) {
507+
startIP.add(Integer.toHexString(Integer.parseInt(parts[x], 16) & Integer.parseInt(floors[x], 2)));
508+
endIP.add(Integer.toHexString(Integer.parseInt(parts[x], 16) | Integer.parseInt(ceilings[x], 2)));
513509
}
514510

515-
hexStartAddress = hexStartAddress.replaceAll("(.{4})", "$1:");
516-
hexStartAddress = hexStartAddress.substring(0, hexStartAddress.length() - 1);
517-
hexEndAddress = hexEndAddress.replaceAll("(.{4})", "$1:");
518-
hexEndAddress = hexEndAddress.substring(0, hexEndAddress.length() - 1);
511+
String hexStartAddress = ExpandIPv6(StringUtils.join(startIP, ":"));
512+
String hexEndAddress = ExpandIPv6(StringUtils.join(endIP, ":"));
519513

520514
return new String[]{hexStartAddress, hexEndAddress};
521515
}

com/ip2location/Region.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package com.ip2location;
22

3+
import com.opencsv.*;
4+
import com.opencsv.exceptions.*;
5+
36
import java.io.File;
47
import java.io.FileReader;
58
import java.io.IOException;
@@ -8,17 +11,14 @@
811
import java.util.List;
912
import java.util.Map;
1013

11-
import com.opencsv.*;
12-
import com.opencsv.exceptions.*;
13-
1414
/**
1515
* This class parses region information CSV and returns the region code.
1616
* <p>
1717
* Copyright (c) 2002-2023 IP2Location.com
1818
* <p>
1919
*
2020
* @author IP2Location.com
21-
* @version 8.11.1
21+
* @version 8.11.2
2222
*/
2323
public class Region {
2424
private final Map<String, List<Map<String, String>>> records = new HashMap<>();
@@ -66,21 +66,19 @@ public String getRegionCode(final String countryCode, final String regionName) t
6666
throw new IOException("No record available.");
6767
} else {
6868
final List<Map<String, String>> items = records.get(countryCode);
69-
if (items == null)
70-
return null;
69+
if (items == null) return null;
7170

7271
final String region = regionName.toUpperCase();
7372
for (Map<String, String> item : items) {
7473
final String regionCode = item.get(region);
75-
if (regionCode != null)
76-
return regionCode;
74+
if (regionCode != null) return regionCode;
7775
}
7876
}
7977
return null;
8078
}
8179

8280
/**
83-
* @deprecated Use {{@link #getRegionCode(String, String)} instead.
81+
* @deprecated Use {{@link #getRegionCode(String, String)} instead.
8482
*/
8583
@Deprecated
8684
public String GetRegionCode(final String countryCode, final String regionName) throws IOException {

0 commit comments

Comments
 (0)