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
1010unit IP2Loc_DBInterface;
@@ -45,7 +45,7 @@ implementation
4545var
4646 DB_access_type:TIP2Location_mem_type=IP2LOCATION_FILE_IO;
4747 cache_shm_ptr :pointer;
48- shm_fd :integer;
48+ shm_fd :integer;
4949
5050function IP2Location_DB_Load_to_mem (filehandle:integer;memory:pointer;size:int64):integer;
5151begin
@@ -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 ;
7574end ;
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 ;
114113end ;
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+
141140function IP2Location_read32 (handle:integer;position:cardinal):cardinal;
142141var byte1,byte2,byte3,byte4:Byte;
143142begin
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
164161function IP2Location_readStr (handle:integer;position:cardinal):PChar;
165162var _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
188185function IP2Location_readFloat (handle:integer;position:cardinal):single;
189186begin
@@ -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-
201197procedure IP2Location_DB_del_shm ();
202198begin
203199{ $IFDEF POSIX}
204200{ $ENDIF}
205201end ;
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-
217211function IP2Location_DB_set_file_io ():Cardinal;
218212begin
219-
220213end ;
221214
222215end .
0 commit comments