Inno Setup 脚本中的基本 IP 验证

发布于 2024-11-16 17:11:55 字数 32 浏览 2 评论 0原文

当我收集用户输入时,如何检查它是否是 IP 地址?

When I collect input from a user, how can I check to see if it's an IP address?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

永不分离 2024-11-23 17:11:55

IP 地址(假设您指的是 IPv4)实际上是一个整数,但通常写为由 . 分隔的四个数字。每个数字都代表整数的一个字节值,因此每个数字都应该是 0 到 255(含)之间的数字。

function CheckIP(Input: String): Cardinal;
var
  IP: Cardinal;
  i : Integer;
  Part: Integer;
  PartValue: Cardinal;
  PartValid: Boolean;
begin
  Part := 3;
  PartValue := 0;
  PartValid := False;
  IP := 0;
  { When a '.' is encountered, the previous part is processed. Force processing }
  { the last part by adding a '.' to the input. }
  Input := Input + '.';

  for i := 1 to Length(Input) do
  begin

    { Check next character }
    if Input[i] = '.' then
    begin

       if PartValue <= 255 then
       begin
         if PartValid then
         begin
           { A valid part is encountered. Put it in the result. }
           IP := IP or (PartValue shl (Part * 8));
           { Stop processing if this is the last '.' we put in ourselves. }
           if i = Length(Input) then
             Break;
           { Else reset the temporary values. }
           PartValid := False;
           PartValue := 0;
           Dec(Part);
         end
         else
           RaiseException('Empty part');
       end
       else
         RaiseException('Part not within 0..255');

    end
    else if ((Input[i] >= '0') and (Input[i] <= '9')) then
    begin

      { A digit is found. Add it to the current part. }
      PartValue := PartValue * 10 + Cardinal((Ord(Input[i]) - Ord('0')));
      PartValid := True;

    end
    else
    begin

      { Any other character raises an exception }
      RaiseException('Invalid character');

    end;

    { If part < 0, we processed too many dots. }
    if Part < 0 then
      RaiseException('Too many dots');

  end;

  { Check if we found enough parts. }
  if Part > 0 then
    RaiseException('Address most consist of 4 numbers');

  { If Part is not valid after the loop, the input ended in a dot. }
  if not PartValid then
    RaiseException('IP cannot end in a dot');

  { Return the calculated IP address as a cardinal. }
  Result := IP;
end;

An IP Address (assuming you mean IPv4) is actually an integer, but it is usually written as four numbers separated by a .. Each of those numbers represent a byte value of the integer, so each of the numbers should be a number between 0 and 255 (incl).

function CheckIP(Input: String): Cardinal;
var
  IP: Cardinal;
  i : Integer;
  Part: Integer;
  PartValue: Cardinal;
  PartValid: Boolean;
begin
  Part := 3;
  PartValue := 0;
  PartValid := False;
  IP := 0;
  { When a '.' is encountered, the previous part is processed. Force processing }
  { the last part by adding a '.' to the input. }
  Input := Input + '.';

  for i := 1 to Length(Input) do
  begin

    { Check next character }
    if Input[i] = '.' then
    begin

       if PartValue <= 255 then
       begin
         if PartValid then
         begin
           { A valid part is encountered. Put it in the result. }
           IP := IP or (PartValue shl (Part * 8));
           { Stop processing if this is the last '.' we put in ourselves. }
           if i = Length(Input) then
             Break;
           { Else reset the temporary values. }
           PartValid := False;
           PartValue := 0;
           Dec(Part);
         end
         else
           RaiseException('Empty part');
       end
       else
         RaiseException('Part not within 0..255');

    end
    else if ((Input[i] >= '0') and (Input[i] <= '9')) then
    begin

      { A digit is found. Add it to the current part. }
      PartValue := PartValue * 10 + Cardinal((Ord(Input[i]) - Ord('0')));
      PartValid := True;

    end
    else
    begin

      { Any other character raises an exception }
      RaiseException('Invalid character');

    end;

    { If part < 0, we processed too many dots. }
    if Part < 0 then
      RaiseException('Too many dots');

  end;

  { Check if we found enough parts. }
  if Part > 0 then
    RaiseException('Address most consist of 4 numbers');

  { If Part is not valid after the loop, the input ended in a dot. }
  if not PartValid then
    RaiseException('IP cannot end in a dot');

  { Return the calculated IP address as a cardinal. }
  Result := IP;
end;
别闹i 2024-11-23 17:11:55

我修改了代码,现在你可以将它与 Inno setup 一起使用:

//Validate an IPv4 address
function ValidateIP(
  Input: String
  ): Boolean;
var
  InputTemp : String;
  IP: Cardinal;
  i : Integer;
  Part: Integer;
  PartValue: Cardinal;
  PartValid: Boolean;
begin
  InputTemp := Input;
  Result := True;

  Part := 3;
  PartValue := 0;
  PartValid := False;
  IP := 0;
  // When a '.' is encountered, the previous part is processed. Force processing
  // the last part by adding a '.' to the input.
  Input := Input + '.';

  for i := 1 to Length(Input) do
  begin
    // Check next character
    if Input[i] = '.' then
    begin

       if PartValue <= 255 then
       begin
         if PartValid then
         begin
           // A valid part is encountered. Put it in the result.
           IP := IP or (PartValue shl (Part * 8));
           // Stop processing if this is the last '.' we put in ourselves.
           if i = Length(Input) then
             Break;
           // Else reset the temporary values.
           PartValid := False;
           PartValue := 0;
           Dec(Part);
         end
         else
           Result := False;
       end
       else
         Result := False;

    end
    else if ( (((Ord(Input[i]) - Ord('0'))) >= 0) and ((Ord(Input[i]) - Ord('0')) <= 9) ) then
    begin
      // A digit is found. Add it to the current part.
      PartValue := PartValue * 10 + Cardinal((Ord(Input[i]) - Ord('0')));
      PartValid := True;
    end
    else
    begin
      // Any other character 
     Result := False;
    end;
    // If part < 0, we processed too many dots.
    if Part < 0 then
      Result := False;
  end;
  // Check if we found enough parts.
  if Part > 0 then
    Result := False;
  // If Part is not valid after the loop, the input ended in a dot.
  if not PartValid then
    Result := False;
end;

I modified the code and now it you can use it with Inno setup:

//Validate an IPv4 address
function ValidateIP(
  Input: String
  ): Boolean;
var
  InputTemp : String;
  IP: Cardinal;
  i : Integer;
  Part: Integer;
  PartValue: Cardinal;
  PartValid: Boolean;
begin
  InputTemp := Input;
  Result := True;

  Part := 3;
  PartValue := 0;
  PartValid := False;
  IP := 0;
  // When a '.' is encountered, the previous part is processed. Force processing
  // the last part by adding a '.' to the input.
  Input := Input + '.';

  for i := 1 to Length(Input) do
  begin
    // Check next character
    if Input[i] = '.' then
    begin

       if PartValue <= 255 then
       begin
         if PartValid then
         begin
           // A valid part is encountered. Put it in the result.
           IP := IP or (PartValue shl (Part * 8));
           // Stop processing if this is the last '.' we put in ourselves.
           if i = Length(Input) then
             Break;
           // Else reset the temporary values.
           PartValid := False;
           PartValue := 0;
           Dec(Part);
         end
         else
           Result := False;
       end
       else
         Result := False;

    end
    else if ( (((Ord(Input[i]) - Ord('0'))) >= 0) and ((Ord(Input[i]) - Ord('0')) <= 9) ) then
    begin
      // A digit is found. Add it to the current part.
      PartValue := PartValue * 10 + Cardinal((Ord(Input[i]) - Ord('0')));
      PartValid := True;
    end
    else
    begin
      // Any other character 
     Result := False;
    end;
    // If part < 0, we processed too many dots.
    if Part < 0 then
      Result := False;
  end;
  // Check if we found enough parts.
  if Part > 0 then
    Result := False;
  // If Part is not valid after the loop, the input ended in a dot.
  if not PartValid then
    Result := False;
end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文