当我从用户收集输入时,如何检查IP地址是否是IP地址?Inno Setup脚本中的基本IP验证
2
A
回答
5
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;
2
我修改了代码,现在你可以用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;
相关问题
- 1. Inno Setup脚本中的基本电子邮件验证
- 2. 验证Inno Setup脚本中文件的校验和?
- 3. Inno Setup的脚本安装
- 4. Inno Setup Pascal脚本文档
- 5. Inno Setup GetExceptionMessage在Inno Setup脚本中返回空消息
- 6. Inno Setup复制文件夹的脚本
- 7. 如何从批处理或Inno Setup脚本获取Inno Setup版本号?
- 8. inno setup安装脚本和Windows 7
- 9. Inno Setup - 如何本地化Pascal脚本中的字符串?
- 10. Inno-Setup ISS脚本可否包含外部ISS脚本文件?
- 11. Inno Setup/ISTool:检测本地计算机的IP地址
- 12. 如何使用Inno Setup脚本编辑文本文件?
- 13. 使用bash脚本验证IP地址
- 14. bash脚本 - 无法验证IP
- 15. 如何找出Inno Setup脚本中发生错误的位置?
- 16. 如何使用Inno-setup获得本地IP地址
- 17. Inno Setup的:由以前的版本
- 18. 如何从Inno Setup脚本中调用.NET DLL?
- 19. 能在Inno Setup脚本中使用环境变量吗?
- 20. Inno setup的每页内部版本号
- 21. Inno Setup的脚本:标识符预期的错误
- 22. 如何使用cscript和vbs参数从Inno Setup脚本调用vbs脚本?
- 23. 获取Inno Setup文件的最后修改日期Pascal脚本
- 24. Inno Setup的 - Pascal脚本 - 有条件地隐藏/显示任务
- 25. 我Inno Setup的脚本创建两个桌面图标
- 26. 在Inno Setup中验证用户的密码
- 27. 与基本身份验证的HTTP基本身份验证
- 28. HTTP基本验证
- 29. 如何将Inno Setup脚本分成多个文件?
- 30. Inno Setup:如何根据用户选项运行回发脚本?
我意识到这一点。我在帕斯卡问,如何做,为了inno的设置。 –
我不知道这个Inno安装脚本,所以我在Delphi中编写了一个函数,并试图排除任何可能是特定Delphi的函数。该函数检查输入字符串并将IP地址作为红衣主教返回。如果发现错误,该函数将引发异常。您可以使用结果进行进一步检查,看看IP是否在特定范围内。如果此脚本不支持异常,则必须找到另一个解决方案,如使该函数返回True或False,并将IP返回到* out *参数中。 – GolezTrol
对于Delphi来说,这段代码会非常复杂并且效率低下(不需要像char那样循环字符)。 – TLama