Skip to content

Commit 9416893

Browse files
Refactor codes to remove redundancies
1 parent 5d078ee commit 9416893

2 files changed

Lines changed: 25 additions & 46 deletions

File tree

ip2location.erl

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
-define(IF(Cond), (case (Cond) of true -> (0); false -> (1) end)).
3131

3232
apiversion() ->
33-
"8.6.2".
33+
"8.6.3".
3434

3535
getapiversion() ->
3636
io:format("API Version: ~p~n", [apiversion()]).
@@ -302,37 +302,49 @@ searchtree(S, Ipnum, Dbtype, Low, High, BaseAddr, Colsize, Iptype) ->
302302
end.
303303

304304
search4(S, Ipnum, Dbtype, Low, High, Baseaddr, Indexbaseaddr, Colsize) ->
305+
if
306+
Ipnum == 4294967295 ->
307+
Ipnum2 = Ipnum - 1;
308+
true ->
309+
Ipnum2 = Ipnum
310+
end,
305311
if
306312
Indexbaseaddr > 0 ->
307-
Indexpos = ((Ipnum bsr 16) bsl 3) + Indexbaseaddr,
313+
Indexpos = ((Ipnum2 bsr 16) bsl 3) + Indexbaseaddr,
308314
case file:pread(S, Indexpos - 1, 8) of % 4 bytes for each IP From & IP To
309315
eof ->
310316
io:format("Error: IP address not found.~n", []),
311317
{}; % return empty
312318
{ok, R} ->
313319
Low2 = readuint32row(R, 0),
314320
High2 = readuint32row(R, 4),
315-
searchtree(S, Ipnum, Dbtype, Low2, High2, Baseaddr, Colsize, ipv4)
321+
searchtree(S, Ipnum2, Dbtype, Low2, High2, Baseaddr, Colsize, ipv4)
316322
end;
317323
true ->
318-
searchtree(S, Ipnum, Dbtype, Low, High, Baseaddr, Colsize, ipv4)
324+
searchtree(S, Ipnum2, Dbtype, Low, High, Baseaddr, Colsize, ipv4)
319325
end.
320326

321327
search6(S, Ipnum, Dbtype, Low, High, Baseaddr, Indexbaseaddr, Colsize) ->
328+
if
329+
Ipnum == 340282366920938463463374607431768211455 ->
330+
Ipnum2 = Ipnum - 1;
331+
true ->
332+
Ipnum2 = Ipnum
333+
end,
322334
if
323335
Indexbaseaddr > 0 ->
324-
Indexpos = ((Ipnum bsr 112) bsl 3) + Indexbaseaddr,
336+
Indexpos = ((Ipnum2 bsr 112) bsl 3) + Indexbaseaddr,
325337
case file:pread(S, Indexpos - 1, 8) of % 4 bytes for each IP From & IP To
326338
eof ->
327339
io:format("Error: IP address not found.~n", []),
328340
{}; % return empty
329341
{ok, R} ->
330342
Low2 = readuint32row(R, 0),
331343
High2 = readuint32row(R, 4),
332-
searchtree(S, Ipnum, Dbtype, Low2, High2, Baseaddr, Colsize, ipv6)
344+
searchtree(S, Ipnum2, Dbtype, Low2, High2, Baseaddr, Colsize, ipv6)
333345
end;
334346
true ->
335-
searchtree(S, Ipnum, Dbtype, Low, High, Baseaddr, Colsize, ipv6)
347+
searchtree(S, Ipnum2, Dbtype, Low, High, Baseaddr, Colsize, ipv6)
336348
end.
337349

338350
query(Ip) ->
@@ -369,53 +381,20 @@ query(Ip) ->
369381
Result = case inet:parse_address(Ip) of
370382
{ok, {X1, X2, X3, X4}} ->
371383
Ipnum = (X1 bsl 24) + (X2 bsl 16) + (X3 bsl 8) + (X4),
372-
if
373-
Ipnum == 4294967295 ->
374-
Ipnum2 = Ipnum - 1;
375-
true ->
376-
Ipnum2 = Ipnum
377-
end,
378-
search4(S, Ipnum2, Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize);
384+
search4(S, Ipnum, Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize);
379385
{ok, {X1, X2, X3, X4, X5, X6, X7, X8}} ->
380386
Ipnum = (X1 bsl 112) + (X2 bsl 96) + (X3 bsl 80) + (X4 bsl 64) + (X5 bsl 48) + (X6 bsl 32) + (X7 bsl 16) + X8,
381387
if
382388
Ipnum >= Fromv4mapped andalso Ipnum =< Tov4mapped ->
383-
Ipnum2 = Ipnum - Fromv4mapped,
384-
if
385-
Ipnum2 == 4294967295 ->
386-
Ipnum3 = Ipnum2 - 1;
387-
true ->
388-
Ipnum3 = Ipnum2
389-
end,
390-
search4(S, Ipnum3, Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize);
389+
search4(S, (Ipnum - Fromv4mapped), Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize);
391390
Ipnum >= From6to4 andalso Ipnum =< To6to4 ->
392-
Ipnum2 = (Ipnum bsr 80) band Last32bits,
393-
if
394-
Ipnum2 == 4294967295 ->
395-
Ipnum3 = Ipnum2 - 1;
396-
true ->
397-
Ipnum3 = Ipnum2
398-
end,
399-
search4(S, Ipnum3, Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize);
391+
search4(S, ((Ipnum bsr 80) band Last32bits), Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize);
400392
Ipnum >= Fromteredo andalso Ipnum =< Toteredo ->
401-
Ipnum2 = (bnot Ipnum) band Last32bits,
402-
if
403-
Ipnum2 == 4294967295 ->
404-
Ipnum3 = Ipnum2 - 1;
405-
true ->
406-
Ipnum3 = Ipnum2
407-
end,
408-
search4(S, Ipnum3, Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize);
393+
search4(S, ((bnot Ipnum) band Last32bits), Databasetype, 0, Ipv4databasecount, Ipv4databaseaddr, Ipv4indexbaseaddr, Ipv4columnsize);
409394
true ->
410395
if
411396
Ipv6databasecount > 0 ->
412-
if
413-
Ipnum == 340282366920938463463374607431768211455 ->
414-
Ipnum2 = Ipnum - 1;
415-
true ->
416-
Ipnum2 = Ipnum
417-
end,
418-
search6(S, Ipnum2, Databasetype, 0, Ipv6databasecount, Ipv6databaseaddr, Ipv6indexbaseaddr, Ipv6columnsize);
397+
search6(S, Ipnum, Databasetype, 0, Ipv6databasecount, Ipv6databaseaddr, Ipv6indexbaseaddr, Ipv6columnsize);
419398
true ->
420399
io:format("Error: IPv6 address is missing in IPv4 BIN.~n", []),
421400
{} % return empty

mix.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ defmodule IP2LocationErlang.MixProject do
44
def project() do
55
[
66
app: :ip2location_erlang,
7-
version: "8.6.2",
7+
version: "8.6.3",
88
elixir: "~> 1.0",
99
build_embedded: Mix.env == :prod,
1010
start_permanent: Mix.env == :prod,

0 commit comments

Comments
 (0)