Skip to content

Commit cce291d

Browse files
Added supports for address type and category fields. Added exception handling for incorrect BIN database.
1 parent ce61844 commit cce291d

7 files changed

Lines changed: 503 additions & 373 deletions

File tree

CHANGELOG.TXT

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
1-
8.0.0 Support Index BIN
1+
8.0.0 Support Index BIN
2+
3+
8.1.0 Added supports for address type and category fields. Added exception handling for incorrect BIN database.

IP2Loc_DBInterface.pas

Lines changed: 50 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
////////////////////////////////////////////////
22
// Unit : IP2Loc_DBInterface.pas //
3-
// Version : 8.0.0 //
4-
// Date : July 2016 //
5-
// Translator: Benbaha Abdelkrim AKA DeadC0der //
3+
// Version : 8.1.0 //
4+
// Date : June 2021 //
5+
// Translator: IP2Location //
66
// Email : support@ip2location.com //
7-
// License : Included :) //
7+
// License : MIT //
88
////////////////////////////////////////////////
99

1010
unit IP2Loc_DBInterface;
@@ -45,7 +45,7 @@ implementation
4545
var
4646
DB_access_type:TIP2Location_mem_type=IP2LOCATION_FILE_IO;
4747
cache_shm_ptr :pointer;
48-
shm_fd :integer;
48+
shm_fd :integer;
4949

5050
function IP2Location_DB_Load_to_mem(filehandle:integer;memory:pointer;size:int64):integer;
5151
begin
@@ -68,9 +68,8 @@ function IP2Location_DB_set_memory_cache(filehandle:integer):integer;
6868
if assigned(cache_shm_ptr) and (IP2Location_DB_Load_to_mem(filehandle,cache_shm_ptr,_size)<>-1) then
6969
begin
7070
result:=0;
71-
DB_access_type:=IP2LOCATION_CACHE_MEMORY;
72-
end;
73-
71+
DB_access_type:=IP2LOCATION_CACHE_MEMORY;
72+
end;
7473
end;
7574
end;
7675

@@ -85,31 +84,31 @@ function IP2Location_DB_set_shared_memory(filehandle:integer):integer;
8584
_size:=GetFileSize(filehandle,nil);
8685
if (filehandle <>-1) and (_size<>-1) then
8786
begin
88-
shm_fd:=CreateFileMapping(
87+
shm_fd:=CreateFileMapping(
8988
INVALID_HANDLE_VALUE,
9089
nil,
9190
PAGE_READWRITE,
9291
0,
9392
_size+1,
9493
PChar(IP2LOCATION_SHM));
95-
DB_loaded := (GetLastError = ERROR_ALREADY_EXISTS);
96-
if shm_fd <>0 then
97-
cache_shm_ptr := MapViewOfFile(
98-
shm_fd,
99-
FILE_MAP_WRITE,
100-
0,
101-
0,
102-
0)else exit;
103-
if not(DB_Loaded) and (IP2Location_DB_Load_to_mem(filehandle,cache_shm_ptr,_size)<>-1) then
104-
begin
105-
DB_access_type := IP2LOCATION_SHARED_MEMORY;
106-
result:=0;
107-
end
108-
else
109-
begin
110-
UnmapViewOfFile(cache_shm_ptr);
111-
CloseHandle(shm_fd);
112-
end;
94+
DB_loaded := (GetLastError = ERROR_ALREADY_EXISTS);
95+
if shm_fd <>0 then
96+
cache_shm_ptr := MapViewOfFile(
97+
shm_fd,
98+
FILE_MAP_WRITE,
99+
0,
100+
0,
101+
0)else exit;
102+
if not(DB_Loaded) and (IP2Location_DB_Load_to_mem(filehandle,cache_shm_ptr,_size)<>-1) then
103+
begin
104+
DB_access_type := IP2LOCATION_SHARED_MEMORY;
105+
result:=0;
106+
end
107+
else
108+
begin
109+
UnmapViewOfFile(cache_shm_ptr);
110+
CloseHandle(shm_fd);
111+
end;
113112
end;
114113
end;
115114

@@ -125,41 +124,39 @@ function IP2Location_DB_close(filehandle:integer):integer;
125124
DB_access_type := IP2LOCATION_FILE_IO;
126125
result:=0;
127126
end;
128-
127+
129128
function IP2Location_read8(handle:integer;position:Cardinal):byte;
130129
begin
131130
result:=0;
132-
if (DB_access_type = IP2LOCATION_FILE_IO) and (handle <> 0) then
133-
begin
134-
FileSeek(handle,position-1,0);
131+
if (DB_access_type = IP2LOCATION_FILE_IO) and (handle <> 0) then
132+
begin
133+
FileSeek(handle,position-1,0);
135134
FileRead(handle,result,1);
136-
end
137-
else
138-
result:=PByteArray(cache_shm_ptr)^[position-1];
139-
end;
140-
135+
end
136+
else
137+
result:=PByteArray(cache_shm_ptr)^[position-1];
138+
end;
139+
141140
function IP2Location_read32(handle:integer;position:cardinal):cardinal;
142141
var byte1,byte2,byte3,byte4:Byte;
143142
begin
144143
if (DB_access_type = IP2LOCATION_FILE_IO) and (handle <> 0) then
145-
begin
146-
FileSeek(handle,position-1,0);
144+
begin
145+
FileSeek(handle,position-1,0);
147146
FileRead(handle,byte1,1);
148-
FileRead(handle,byte2,1);
149-
FileRead(handle,byte3,1);
150-
FileRead(handle,byte4,1);
151-
end
147+
FileRead(handle,byte2,1);
148+
FileRead(handle,byte3,1);
149+
FileRead(handle,byte4,1);
150+
end
152151
else
153152
begin
154-
byte1:=PByteArray(cache_shm_ptr)^[position-1];
155-
byte2:=PByteArray(cache_shm_ptr)^[position];
156-
byte3:=PByteArray(cache_shm_ptr)^[position+1];
157-
byte4:=PByteArray(cache_shm_ptr)^[position+2];
158-
end;
159-
result:= (byte4 shl 24) or (byte3 shl 16) or (byte2 shl 8) or byte1;
153+
byte1:=PByteArray(cache_shm_ptr)^[position-1];
154+
byte2:=PByteArray(cache_shm_ptr)^[position];
155+
byte3:=PByteArray(cache_shm_ptr)^[position+1];
156+
byte4:=PByteArray(cache_shm_ptr)^[position+2];
157+
end;
158+
result:= (byte4 shl 24) or (byte3 shl 16) or (byte2 shl 8) or byte1;
160159
end;
161-
162-
163160

164161
function IP2Location_readStr(handle:integer;position:cardinal):PChar;
165162
var _size:byte;P_In:PAnsiChar;
@@ -183,7 +180,7 @@ function IP2Location_readStr(handle:integer;position:cardinal):PChar;
183180
Result:=AllocMem(StrLen(P_IN)*2+2);
184181
MultiByteToWideChar(CP_UTF8,0,P_IN,StrLen(P_IN),Result,StrLen(P_IN));
185182
{$ENDIF}
186-
end;
183+
end;
187184

188185
function IP2Location_readFloat(handle:integer;position:cardinal):single;
189186
begin
@@ -195,28 +192,24 @@ function IP2Location_readFloat(handle:integer;position:cardinal):single;
195192
end
196193
else
197194
CopyMemory(@result,@PByteArray(cache_shm_ptr)^[position-1],4);
198-
end;
195+
end;
199196

200-
201197
procedure IP2Location_DB_del_shm();
202198
begin
203199
{$IFDEF POSIX}
204200
{$ENDIF}
205201
end;
206202

207-
function IP2Location_readIPv6Address(handle:integer;position:Cardinal):Tin6_addr_local;
203+
function IP2Location_readIPv6Address(handle:integer;position:Cardinal):Tin6_addr_local;
208204
var i:integer;
209205
begin
210206
with result do
211207
for i:=0 to 15 do
212208
addr8[i]:=IP2Location_read8(handle, position + (15-i));
213209
end;
214210

215-
216-
217211
function IP2Location_DB_set_file_io():Cardinal;
218212
begin
219-
220213
end;
221214

222215
end.

0 commit comments

Comments
 (0)