-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddress.pas
341 lines (322 loc) · 8.63 KB
/
address.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
UNIT Address;
INTERFACE
Uses
Use32,
StrUnit,Incl;
Procedure SetAddressFromString(S:String;Var Address:TAddress);
Procedure SetExcludeAddressFromString(S:String;Var Address:TExcludeAddress);
Procedure SetExcludeAddressFromAddress(Addr:TAddress;Var Address:TExcludeAddress);
Procedure SetStringFromAddress(Var S:String;Address:TAddress);
Procedure SetStringFromExcludeAddress(Var S:String;Address:TExcludeAddress);
Function GetStringFromAddress(Address:TAddress):String;
Function GetStringFromExcludeAddress(Address:TExcludeAddress):String;
Function GetStringWithPointFromAddress(Address:TAddress):String;
Function GetStringAddressFromString(S:String):String;
Function IsAddressesEqual(Addr1,Addr2:TAddress):Boolean;
Function DigitMaskMatch(Mask,Source:String):Boolean;
Function CharsMaskMatch(Mask,Source:String):Boolean;
Function IsAddressMatch(Mask:String;Addr:TAddress):Boolean;
IMPLEMENTATION
Uses MCommon;
Procedure SetAddressFromString(S:String;Var Address:TAddress);
Var
BeginPos,EndPos,
BeginPos1:Integer;
Code:Integer;
Begin
S:=StrTrim(S);
FillChar(Address,SizeOf(Address),#0);
BeginPos:=Pos(' ',S);
If BeginPos>0 Then
Delete(S,BeginPos,Length(S));
BeginPos:=Pos(',',S);
If BeginPos>0 Then
Delete(S,1,BeginPos);
BeginPos:=Pos(':',S);
If BeginPos<>0 Then
Val(Copy(S,1,BeginPos-1),Address.Zone,Code);
EndPos:=Pos('/',S);
If (EndPos<>0) And (EndPos>BeginPos) Then
Val(Copy(S,BeginPos+1,EndPos-BeginPos-1),Address.Net,Code);
BeginPos:=EndPos;
EndPos:=Pos('.',S);
BeginPos1:=Pos('@',S);
If EndPos>0 Then
Begin
Val(Copy(S,BeginPos+1,EndPos-BeginPos-1),Address.Node,Code);
If (BeginPos1>0) Then
Begin
If (BeginPos1>EndPos) Then
Val(Copy(S,EndPos+1,BeginPos1-1-EndPos),Address.Point,Code);
End
Else
Val(Copy(S,EndPos+1,Length(S)),Address.Point,Code);
End
Else
Begin
If (BeginPos1>0) And (BeginPos>0) Then
Val(Copy(S,BeginPos+1,BeginPos1-1-BeginPos),Address.Node,Code)
Else
If (BeginPos>0) Then
Val(Copy(S,BeginPos+1,Length(S)-BeginPos),Address.Node,Code);
Address.Point:=0;
End;
If BeginPos1>0 Then
Begin
EndPos:=Pos(' ',Copy(S,BeginPos1,Length(S)));
If EndPos>0 Then
Address.Domain:=StrTrim(Copy(S,BeginPos1+1,EndPos-BeginPos1-1))
Else
{ Address.Domain:='';}
Address.Domain:=Copy(S,BeginPos1+1,Length(S));
End
Else
Address.Domain:='';
End;
Procedure SetExcludeAddressFromString(S:String;Var Address:TExcludeAddress);
Var
TmpAddress:TAddress;
Begin
SetAddressFromString(S,TmpAddress);
Address.Zone:=TmpAddress.Zone;
Address.Net:=TmpAddress.Net;
Address.Node:=TmpAddress.Node;
Address.Point:=TmpAddress.Point;
End;
Procedure SetExcludeAddressFromAddress(Addr:TAddress;Var Address:TExcludeAddress);
Begin
Address.Zone:=Addr.Zone;
Address.Net:=Addr.Net;
Address.Node:=Addr.Node;
Address.Point:=Addr.Point;
End;
Procedure SetStringFromAddress(Var S:String;Address:TAddress);
Begin
S:='';
S:=S+IntToStr(Address.Zone)+':'+IntToStr(Address.Net)+'/'+IntToStr(Address.Node);
End;
Procedure SetStringFromExcludeAddress(Var S:String;Address:TExcludeAddress);
Begin
S:='';
S:=S+IntToStr(Address.Zone)+':'+IntToStr(Address.Net)+'/'+IntToStr(Address.Node);
End;
Function IsAddressesEqual(Addr1,Addr2:TAddress):Boolean;
Begin
With Addr1 Do
Begin
If (Zone=Addr2.Zone) And (Net=Addr2.Net) And (Node=Addr2.Node) And
(Point=Addr2.Point) Then
IsAddressesEqual:=True
Else
IsAddressesEqual:=False;
End;
End;
Function GetStringFromAddress(Address:TAddress):String;
Var
S:String;
Begin
SetStringFromAddress(S,Address);
GetStringFromAddress:=S;
End;
Function GetStringFromExcludeAddress(Address:TExcludeAddress):String;
Var
S:String;
Begin
SetStringFromExcludeAddress(S,Address);
GetStringFromExcludeAddress:=S;
End;
Function GetStringWithPointFromAddress(Address:TAddress):String;
Var
S:String;
Begin
SetStringFromAddress(S,Address);
S:=S+'.'+IntToStr(Address.Point);
GetStringWithPointFromAddress:=S;
End;
Function GetStringAddressFromString(S:String):String;
Var
TA:TAddress;
SS:String;
Begin
SS:='';
SetAddressFromString(S,TA);
SetStringFromAddress(SS,TA);
GetStringAddressFromString:=SS;
End;
Function DigitMaskMatch(Mask,Source:String):Boolean;
Var
Count,Count1:Word;
Begin
DigitMaskMatch:=True;
Count:=1;
Count1:=1;
If Pos('*',Mask)= 0 Then
Begin
DigitMaskMatch:=StrUp(Mask)=StrUp(Source);
Exit;
End;
While Count<=Length(Mask) Do
Begin
If (Count>Length(Source)) or (Count>Length(Mask)) Then
Begin
DigitMaskMatch:=False;
Break;
End;
Case Mask[Count] Of
'0'..'9':
Begin
If Count<=Length(Source) Then
Begin
If Source[Count]=Mask[Count] Then
Begin
Inc(Count);
{ If (Length(Source)>Length(Mask)) And (Count>Length(Mask)) Then
DigitMaskMatch:=False;}
End
Else
Begin
DigitMaskMatch:=False;
Break;
End;
End
Else
Break;
End;
'*':
Begin
While Count<=Length(Source) Do
Begin
If Count<Length(Mask) Then
Begin
If Pos('*',Copy(Mask,Count+1,Length(Mask)))>0 Then
Begin
If Pos(Copy(Mask,Count+1,Pos('*',Copy(Mask,Count+1,Length(Mask)))-1),Source)> 0 Then
Begin
Count:=Pos('*',Copy(Mask,Count+1,Length(Mask)))+Count;
Break;
End
Else
Begin
DigitMaskMatch:=False;
Exit;
End;
End
Else
Begin
If Pos(Copy(Mask,Count+1,Length(Mask)),Source)> 0 Then
Begin
Count:=Length(Mask);
Break;
End
Else
Begin
DigitMaskMatch:=False;
Exit;
End;
End;
End
Else
Begin
Inc(Count);
Break;
End;
End;
End;
{ 'a'..'z','A'..'Z':
Begin
MaskMatch:=False;
Break;
End;}
#00..#41,#43..#47,
#58..#255 :
Begin
DigitMaskMatch:=False;
Break;
End;
End;
End;
End;
Function CharsMaskMatch(Mask,Source:String):Boolean;
Begin
CharsMaskMatch:=IsWildCardMatch(Mask,Source);
End;
Function IsAddressMatch(Mask:String;Addr:TAddress):Boolean;
Var
MaskZone,
MaskNet,
MaskNode,
MaskPoint,
Zone,
Net,
Node,
Point:String;
BeginPos,EndPos:Word;
Begin
IsAddressMatch:=True;
Mask:=StrTrim(Mask);
If Mask='' Then
Begin
IsAddressMatch:=False;
Exit;
End;
Zone:=IntToStr(Addr.Zone);
Net:=IntToStr(Addr.Net);
Node:=IntToStr(Addr.Node);
Point:=IntToStr(Addr.Point);
BeginPos:=Pos(':',Mask);
If BeginPos>0 Then
Begin
MaskZone:=Copy(Mask,1,BeginPos-1);
Delete(Mask,1,BeginPos);
If Not(DigitMaskMatch(MaskZone,Zone)) Then
Begin
IsAddressMatch:=False;
Exit;
End
End;
BeginPos:=Pos('/',Mask);
If BeginPos>0 Then
Begin
MaskNet:=Copy(Mask,1,BeginPos-1);
Delete(Mask,1,BeginPos);
If Not(DigitMaskMatch(MaskNet,Net)) Then
Begin
IsAddressMatch:=False;
Exit;
End
End;
BeginPos:=Pos('.',Mask);
If BeginPos>0 Then
Begin
MaskNode:=Copy(Mask,1,BeginPos-1);
Delete(Mask,1,BeginPos);
If Not(DigitMaskMatch(MaskNode,Node)) Then
Begin
IsAddressMatch:=False;
Exit;
End;
MaskPoint:=Mask;
If Not(DigitMaskMatch(Maskpoint,Point)) Then
Begin
IsAddressMatch:=False;
Exit;
End;
End
Else
Begin
MaskNode:=Mask;
If Not(DigitMaskMatch(MaskNode,Node)) Then
Begin
IsAddressMatch:=False;
Exit;
End;
MaskPoint:='0';
If Not(DigitMaskMatch(MaskPoint,Point)) Then
Begin
IsAddressMatch:=False;
Exit;
End;
End;
End;
Begin
End.