SoFunction
Updated on 2025-04-12

Easy Language Pure IP Database Query Code Example

The following is the content code we have shared with you about the Yi Language IP database query. You can test it

.Version 2
.Support library spec

.Assembly Assembly1

.Subprogram _启动Subprogram, Integer type, , 请在本Subprogram中放置易模块初始化代码


_临时Subprogram () ' Call the test code after the initialization code is executed
return (0) ' Can return any value according to your needs

 .Subprogram_Temporary Subprogram

 ' 本名称Subprogram用作测试程序用,Only valid in development and debugging environments,The program will be automatically cleared by the system before compiling and publishing,请将所有用作测试的临时代码放在本Subprogram中。 ***注意不要修改本Subprogram的名称、parameter及return值类型。
Debug output (QueryIPPlace of belonging (“192.168.0.0”))


.Subprogram QueryIPPlace of belonging, Text type, public, QueryIPGeographic location of the address
.parameter IPaddress, Text type, , Can be a domain name
.parameter Database file name, Text type, Available
.Local variables File number, Integer type
.Local variables File header byte set, Byte set
.Local variables IPquantity, 长Integer type
.Local variables Calculate variables, Integer type
.Local variables startIPByte set, Byte set
.Local variables FinishIPOffset, 长Integer type
.Local variables FinishIPByte set, Byte set
.Local variables 地区Offset, 长Integer type
.Local variables QueryIPValue, 长Integer type
.Local variables Regional text, Text type
.Local variables IPSection information, Text type
.Local variables startIPRead location, 长Integer type
.Local variables startip, Text type
.Local variables FinishIP, Text type
.Local variables first_start_ip_offset, 长Integer type
.Local variables last_start_ip_offset, 长Integer type
.Local variables startIPValue, 长Integer type
.Local variables FinishIPValue, 长Integer type

.If true (IPaddress = “”)
  return (“”)
.If trueFinish
.If true (Is it empty or not (Database file name))
  Database file name = “.\”
.If trueFinish
File number = Open the file (Database file name, #read in, #unlimited).If true (File number = 0)
  Information box (“No database found”, 0, , )
  return (“”)
.If trueFinish
File header byte set = 读入Byte set (File number, 8)
first_start_ip_offset = 取Byte set数据 (取Byte set左边 (File header byte set, 4), #Integer, )last_start_ip_offset = 取Byte set数据 (取Byte set右边 (File header byte set, 4), #Integer, )IPquantity = (last_start_ip_offset - first_start_ip_offset) \ 7 + 1
QueryIPValue = IPtext转整Value (IPaddress)
startIPRead location = Dichotomy to determine position (File number, first_start_ip_offset, 1, IPquantity, QueryIPValue)
Move read and write positions (File number, , first_start_ip_offset + (startIPRead location - 1) × 7)
startIPByte set = 读入Byte set (File number, 7)
startip = IPText replenishment (Take hexadecimal text (取Byte set数据 (取Byte set左边 (startIPByte set, 4), #integer type, )))FinishIPOffset = 取Byte set数据 (取Byte set右边 (startIPByte set, 3), #Integer, )Move read and write positions (File number, , FinishIPOffset)
FinishIPByte set = 读入Byte set (File number, 4)
FinishIP = IPText replenishment (Take hexadecimal text (取Byte set数据 (FinishIPByte set, #integer type, )))地区Offset = FinishIPOffset + 4
startIPValue = Hexadecimal to decimal (startip)
FinishIPValue = Hexadecimal to decimal (FinishIP)
.If true (QueryIPValue ≥ startIPValue and QueryIPValue ≤ FinishIPValue)
  Regional text = 取Regional text (File number, 地区Offset)
.If trueFinish
Close the file (File number)
return (Regional text)


.Subprogram Dichotomy to determine position, Integer type
.parameter File number, Integer type
.parameter Base address, 长Integer type
.parameter Initial position, 长Integer type
.parameter Finish位置, 长Integer type
.parameter Query值, 长Integer type
.Local variables Intermediate read and write position, 长Integer type
.Local variables Middle position, 长Integer type
.Local variables middleIPtext, Text type
.Local variables middleIPValue, 长Integer type
.Local variables Finish读写位置, 长Integer type
.Local variables FinishIPtext, Text type
.Local variables FinishIPValue, 长Integer type

Middle position = (Initial position + Finish位置) \ 2
.If true (Initial position = Middle position)
  return (Initial position)
.If trueFinish
Finish读写位置 = Base address + (Finish位置 - 1) × 7
Move read and write positions (File number, , Finish读写位置)
FinishIPtext = IPText replenishment (Take hexadecimal text (取Byte set数据 (取Byte set左边 (读入Byte set (File number, 7), 4), #integer type, )))FinishIPValue = Hexadecimal to decimal (FinishIPtext)
.If true (Query值 ≥ FinishIPValue)
  return (Finish位置)
.If trueFinish
Intermediate read and write position = Base address + (Middle position - 1) × 7
Move read and write positions (File number, , Intermediate read and write position)
middleIPtext = IPText replenishment (Take hexadecimal text (取Byte set数据 (取Byte set左边 (读入Byte set (File number, 7), 4), #integer type, )))middleIPValue = Hexadecimal to decimal (middleIPtext)
.If true (Query值 < middleIPValue)
  .If true (Finish位置 - Initial position = 1)
    return (Initial position)
  .If trueFinish
  return (Dichotomy to determine position (File number, Base address, Initial position, Middle position, Query值))
.If trueFinish
.If true (Query值 > middleIPValue)
  return (Dichotomy to determine position (File number, Base address, Middle position, Finish位置, Query值))
.If trueFinish
.If true (Query值 = middleIPValue)
  return (Middle position)
.If trueFinish
return (0)

.Subprogram 取Regional text, Text type
.parameter File number, Integer type
.parameter 地区Offset, Integer type
.Local variables 首Byte set, Byte set
.Local variables Offset, Integer type
.Local variables National Records, Text type
.Local variables Regional Records, Text type
.Local variables length, Integer type

Move read and write positions (File number, , 地区Offset)
首Byte set = 读入Byte set (File number, 1)
.judgestart (首Byte set = { 1 }) ' model1的National Records后面不会再有Regional Records
  Offset = 取Byte set数据 (读入Byte set (File number, 3), #Integer, )  National Records = 读National Records (File number, Offset, length)
  Offset = Offset + length

  Regional Records = 读Regional Records (File number, Offset)

.judge (首Byte set = { 2 }) ' model2的National Records后会有Regional Records
  Offset = 取Byte set数据 (读入Byte set (File number, 3), #Integer, )  National Records = 读National Records (File number, Offset, length)
  Offset = 地区Offset + 4
  Regional Records = 读Regional Records (File number, Offset)

.default
  Offset = 地区Offset
  National Records = 读National Records (File number, Offset, length)

  Offset = Offset + length
  Regional Records = 读Regional Records (File number, Offset)
.judgeFinish

.If true (To capitalize (Delete the beginning and end empty (Regional Records)) = “”)
  Regional Records = “”
.If trueFinish
.If true (Regional Records = “” and National Records ≠ “”)
  return (National Records)
.If trueFinish
.If true (Regional Records ≠ “” and National Records = “”)
  return (Regional Records)
.If trueFinish
.If true (National Records = Regional Records)
  return (National Records)
.If trueFinish
return (National Records + “ ” + Regional Records)

.Subprogram 读National Records, Text type
.parameter File number, Integer type
.parameter 地区Offset, Integer type
.parameter length, Integer type, refer to, 记录所占的length
.Local variables 首Byte set, Byte set
.Local variables Offset, Integer type
.Local variables National Records, Text type
.Local variables 记录length, Integer type

Move read and write positions (File number, #File Head, Region Offset)首Byte set = 读入Byte set (File number, 1)
.if (首Byte set = { 2 }) ' The2次重定向一定是model2
  Offset = 取Byte set数据 (读入Byte set (File number, 3), #Integer, )  Move read and write positions (File number, #File Header, Offset)  首Byte set = 读入Byte set (File number, 1)
  .if (首Byte set = { 2 } or 首Byte set = { 1 }) ' There may be errors,No matter
    Offset = 取Byte set数据 (读入Byte set (File number, 3), #Integer, )    Move read and write positions (File number, #File Header, Offset)
  .otherwise
    ' Not a redirect
    Move read and write positions (File number, #Current location, -1) ' Move back One byte of data has been read above
  .ifFinish
  National Records = 读入text (File number, )
  length = 4
.otherwise
  ' Not a redirect
  Move read and write positions (File number, #Current location, -1) ' Move back One byte of data has been read above  National Records = 读入text (File number, )
  记录length = 取textlength (National Records)
  length = 记录length + 1
.ifFinish

return (National Records)

.Subprogram 读Regional Records, Text type
.parameter File number, Integer type
.parameter 地区Offset
.Local variables 首Byte set, Byte set
.Local variables Offset, Integer type
.Local variables Regional Records, Text type

Move read and write positions (File number, #File Head, Region Offset)首Byte set = 读入Byte set (File number, 1)
.if (首Byte set = { 1 } or 首Byte set = { 2 }) ' 对于Regional Records,model1和model2It's the same
  Offset = 取Byte set数据 (读入Byte set (File number, 3), #Integer, )  Move read and write positions (File number, #File Header, Offset)  首Byte set = 读入Byte set (File number, 1)
  .if (首Byte set = { 1 } or 首Byte set = { 2 }) ' 再来judge下是否是重定向,安全The一
    Offset = 取Byte set数据 (读入Byte set (File number, 3), #Integer, )    Move read and write positions (File number, #File Header, Offset)  .otherwise
    ' Not a redirect
    Move read and write positions (File number, #Current location, -1) ' Move back One byte of data has been read above .

 .otherwise
   ' Not a redirect
  Move read and write positions (File number, #Current location, -1) ' Move back One byte of data has been read above.ifFinish

Regional Records = 读入text (File number, )
return (Regional Records)

.Subprogram IPtext转整Value, 长Integer type
.parameter IPtext, Text type
.Local variables IPArray of variables, Text type, , "0"
.Local variables hexadecimalIPtext, Text type
.Local variables Decimal value, 长Integer type
.Local variables newip, Text type
.Local variables i, Integer type
.Local variables IPpart, Text type

IPArray of variables = 分割text (IPtext, “.”, )
.If true (Take the number of array members (IPArray of variables) ≠ 4)
  newip = Convert toIPaddress (IPtext)
  IPArray of variables = 分割text (newip, “.”, )
  .If true (Take the number of array members (IPArray of variables) ≠ 4)
    return (0)
  .If trueFinish

.If trueFinish
hexadecimalIPtext = “”
.Cycle first (4, i)
  IPpart = 取text右边 (“00” + Take hexadecimal text (to integer (IPArray of variables [5 - i])), 2)
  hexadecimalIPtext = hexadecimalIPtext + IPpart
.Calculate the cycle ()
Decimal value = Hexadecimal to decimal (倒转hexadecimaltext (hexadecimalIPtext))
return (Decimal value)

.Subprogram Hexadecimal to decimal, 长Integer type
.parameter hexadecimaltext, Text type
.Local variables Byte set, Byte set
.Local variables Byte setlength, Integer type
.Local variables Final value, 长Integer type
.Local variables i, Integer type, , , middle循环变量
.Local variables 临时Value, Integer type, , , Values ​​on each bit

Byte set = 到Byte set (To capitalize (hexadecimaltext))
Byte setlength = 取Byte setlength (Byte set)
.Cycle first (Byte setlength, i)
  临时Value = Byte set [Byte setlength - i + 1]
  .If true (临时Value > 64 and 临时Value < 71) ' A=65 F=70
    Final value = Final value + (临时Value - 65 + 10) × Seek the power (16, i - 1)
    To the end of the loop ()
  .If trueFinish
  Final value = Final value + to integer (character (临时Value)) × Seek the power (16, i - 1)
.Calculate the cycle ()
return (Final value)

.Subprogram 倒转hexadecimaltext, Text type
.parameter IPaddresstext, Text type
.Local variables Calculate variables, Integer type
.Local variables 临时text, Text type
.Local variables resulttext, Text type

临时text = IPText replenishment (IPaddresstext)
.Cycle first (4, Calculate variables)
  resulttext = resulttext + 取textmiddle (临时text, 8 - Calculate variables × 2 + 1, 2)
.Calculate the cycle ()
return (resulttext)


.Subprogram IPText replenishment, Text type
.parameter IPaddresstext, Text type
.Local variables textlength, Integer type
.Local variables 临时text, Text type
.Local variables Calculate variables, Integer type

临时text = IPaddresstext
textlength = 取textlength (IPaddresstext)
.If true (textlength < 8)
  临时text = 取重复text (8 - textlength, “0”) + 临时text
.If trueFinish
return (临时text)

.Subprogram hexadecimaltext到IPaddress, Text type
.parameter IPtext, Text type
.Local variables 临时text, Text type
.Local variables resulttext, Text type
.Local variables Calculate variables, Integer type
.Local variables resultip, Text type, , "4"
.Local variables IPaddress, Text type

临时text = IPText replenishment (IPtext)
.Cycle first (4, Calculate variables)
  resulttext = 取textmiddle (临时text, 1 + (Calculate variables - 1) × 2, 2)
  IPaddress = IPaddress + 到text (Hexadecimal to decimal (resulttext)) + “.”
.Calculate the cycle ()
return (取text左边 (IPaddress, 取textlength (IPaddress) - 1))

You can test the above code in the Easy Locale environment. Thank you for your support.