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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701 | -- --
-- package Copyright (c) Dmitry A. Kazakov --
-- GNAT.Sockets.Connection_State_Machine. Luebeck --
-- ASN1.Choices Summer, 2019 --
-- Implementation --
-- Last revision : 10:13 29 Nov 2020 --
-- --
-- This library is free software; you can redistribute it and/or --
-- modify it under the terms of the GNU General Public License as --
-- published by the Free Software Foundation; either version 2 of --
-- the License, or (at your option) any later version. This library --
-- is distributed in the hope that it will be useful, but WITHOUT --
-- ANY WARRANTY; without even the implied warranty of --
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU --
-- General Public License for more details. You should have --
-- received a copy of the GNU General Public License along with --
-- this library; if not, write to the Free Software Foundation, --
-- Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. --
-- --
-- As a special exception, if other files instantiate generics from --
-- this unit, or you link this unit with other files to produce an --
-- executable, this unit does not by itself cause the resulting --
-- executable to be covered by the GNU General Public License. This --
-- exception does not however invalidate any other reasons why the --
-- executable file might be covered by the GNU Public License. --
--____________________________________________________________________--
with Ada.Exceptions; use Ada.Exceptions;
with Ada.IO_Exceptions; use Ada.IO_Exceptions;
with Ada.Tags; use Ada.Tags;
with Strings_Edit.Integers; use Strings_Edit.Integers;
with Ada.Unchecked_Conversion;
with System.Address_To_Access_Conversions;
package body GNAT.Sockets.Connection_State_Machine.ASN1.Choices is
Invalid_EOC : constant String := "Invalid indefinite ASN.1 " &
"choice termination";
function Always_Constructed
( Item : Choice_Data_Item
) return Boolean is
begin
return True;
end Always_Constructed;
procedure Check (Item : Data_Item'Class) is
begin
if Item not in ASN1_Data_Item'Class then
Raise_Exception
( Use_Error'Identity,
Wrong_Choice & Expanded_Name (Item'Tag)
);
end if;
end Check;
procedure Uninitialized (Item : Abstract_ASN1_Data_Item'Class) is
begin
Raise_Exception
( Use_Error'Identity,
( "ASN.1 Set "
& Expanded_Name (Item'Tag)
& " was not properly initialized"
) );
end Uninitialized;
procedure Enable_Unsolicited
( Item : in out Choice_Data_Item;
Enable : Boolean
) is
begin
if not Item.Initialized then
Uninitialized (Item);
end if;
Item.Unsolicited := Enable;
end Enable_Unsolicited;
procedure Encode
( Item : Choice_Data_Item;
Data : in out Stream_Element_Array;
Pointer : in out Stream_Element_Offset
) is
Tag : Tag_Type;
begin
if ( Pointer < Data'First
or else
( Pointer > Data'Last
and then
Pointer - Data'Last /= 1
) ) then
Raise_Exception (Layout_Error'Identity, Out_Of_Bounds);
elsif Data'Last - Pointer < 0 then
Raise_Exception (End_Error'Identity, No_Room);
elsif not Item.Initialized then
Uninitialized (Item);
elsif Item.Current.Position = 0 then
Raise_Exception (Use_Error'Identity, Nothing_Selected);
end if;
Tag := Item.List (Item.Current.Position);
declare
This : Abstract_ASN1_Data_Item'Class renames
Abstract_ASN1_Data_Item'Class
( Get (Item.Map, Tag).Item.all
);
begin
Put (Data, Pointer, Tag, Always_Constructed (This));
Definite_Encode (This, Data, Pointer);
end;
end Encode;
Get_Element : constant := 1;
Get_Tag_Value : constant := 2;
Select_By_Tag : constant := 3;
Skip_Until_Zero : constant := 4;
Skip_Second_Zero : constant := 5;
Skip_Definite : constant := 6;
-- -- First_Zero : constant := 7;
-- -- Second_Zero : constant := 8;
-- procedure End_Of_Subsequence
-- ( Item : in out Choice_Data_Item;
-- Data : Stream_Element_Array;
-- Pointer : Stream_Element_Offset;
-- Client : in out State_Machine'Class;
-- State : in out Stream_Element_Offset
-- ) is
-- begin
-- if Item.Length >= 0 then
-- if Item.Length = Get_Length (Item.Current) then
-- State := 0;
-- else
-- Raise_Exception (Data_Error'Identity, Invalid_Length);
-- end if;
-- else
-- State := First_Zero;
-- end if;
-- end End_Of_Subsequence;
procedure Enumerate
( Stream : access Root_Stream_Type'Class;
Item : Choice_Data_Item
) is
begin
Check_Initialization_Stream (Item, Stream.all);
declare
Count : Data_Item_Offset := 0;
This : Initialization_Stream'Class renames
Initialization_Stream'Class (Stream.all);
begin
if not Item.Initialized then
declare
Counter : aliased Initialization_Stream;
Object : Choice_Data_Item'Class
renames Self (Item).all;
begin
if This.Parent = null then
Counter.Parent := This.Shared'Access;
else
Counter.Parent := This.Parent;
end if;
Object.Initialized := True;
Choice_Data_Item'Class'Write (Counter'Access, Object);
if Counter.Count <= 1 then
Raise_Exception
( Use_Error'Identity,
( "ASN.1 set "
& Expanded_Name (Object'Tag)
& " is empty (contains no items)"
) );
end if;
Object.List :=
new Tag_Type_Array
( 1
.. Natural (Counter.Count - 1)
);
for Index in 2..Counter.Count loop
declare
Position : constant Positive :=
Positive (Index - 1);
begin
Check (Counter.Data.Vector (Index).all);
Object.List (Position) :=
( Class => Context_Specific_Tag,
Value => ASN1_Type (Position - 1),
Optional => False
);
Add
( Object.Map,
Object.List (Position),
( Position => Position,
Item => Counter.Data.Vector (Index)
) );
end;
end loop;
Count := Data_Item_Offset (Get_Size (Item)) - 1;
Initialized (Object);
end;
end if;
Add (This, Item, Count);
end;
end Enumerate;
procedure Feed
( Item : in out Choice_Data_Item;
Data : Stream_Element_Array;
Pointer : in out Stream_Element_Offset;
Client : in out State_Machine'Class;
State : in out Stream_Element_Offset
) is
begin
if State = 0 then
Item.Current.Position := 0;
State := Get_Element;
end if;
while Pointer <= Data'Last loop
case State is
when Get_Element =>
-- if Item.Length = 0 then
-- Raise_Exception
-- ( Data_Error'Identity,
-- Invalid_Length
-- );
-- end if;
declare
Tag : Tag_Type renames Item.Current.Tag;
begin
Tag.Value := ASN1_Type (Data (Pointer) and 16#1F#);
case ASN1_Tag (Data (Pointer) and 2#1100_0000#) is
when Application_Class =>
Tag.Class := Application_Tag;
when Context_Specific_Class =>
Tag.Class := Context_Specific_Tag;
when Private_Class =>
Tag.Class := Private_Tag;
when others =>
Tag.Class := Universal_Tag;
end case;
if Tag.Value < 31 then
State := Select_By_Tag;
else
State := Get_Tag_Value;
end if;
end;
Pointer := Pointer + 1;
-- if Item.Length > 0 then
-- Item.Length := Item.Length - 1;
-- end if;
when Get_Tag_Value =>
while Pointer <= Data'Last loop
-- if Item.Length = 0 then
-- Raise_Exception
-- ( Data_Error'Identity,
-- Invalid_Length
-- );
-- end if;
declare
This : constant ASN1_Type :=
ASN1_Type (Data (Pointer));
Value : ASN1_Type renames Item.Current.Tag.Value;
begin
Pointer := Pointer + 1;
-- if Item.Length > 0 then
-- Item.Length := Item.Length - 1;
-- end if;
if This > 127 then
Value := Value * 128 + (This - 128);
else
Value := Value * 128 + This;
State := Select_By_Tag;
exit;
end if;
exception
when Constraint_Error =>
Raise_Exception
( Data_Error'Identity,
Tag_Too_Large
);
end;
end loop;
when Select_By_Tag =>
declare
Offset : constant Integer :=
Find (Item.Map, Item.Current.Tag);
begin
if Offset > 0 then
declare
Element : constant Element_Descriptor :=
Get (Item.Map, Offset);
begin
Item.Current.Position := Element.Position;
-- State := First_Zero;
State := 0;
Call
( Item => Item.Current,
Callee => Element.Item.all,
Tag => Item.Current.Tag,
Pointer => Pointer,
Client => Client,
State => State
);
return;
end;
elsif Item.Unsolicited then
State := Start_Length;
else
Raise_Exception
( Data_Error'Identity,
( "ASN.1 unexpected choice tag "
& Image (Item.Current.Tag)
) );
end if;
end;
when Skip_Until_Zero => -- Skip indefinite length object
while Data (Pointer) /= 0 loop
Pointer := Pointer + 1;
if Pointer > Data'Last then
return;
end if;
end loop;
State := Skip_Second_Zero;
when Skip_Second_Zero =>
if Data (Pointer) = 0 then
State := 0;
return;
else
State := Skip_Until_Zero;
end if;
when Stream_Element_Offset'First..-1 => -- Skipped length
while not Is_Length_Ready (State) loop
if Pointer > Data'Last then
return;
end if;
Embedded_Feed (Data, Pointer, State);
end loop;
if Is_Indefinite (State) then
State := Skip_Until_Zero;
else
State := Skip_Definite + Get_Length (State);
if State = Skip_Definite then
State := 0;
return;
end if;
end if;
when others => -- Skip definite length object
loop
Pointer := Pointer + 1;
State := State - 1;
if State <= Skip_Definite then
State := 0;
return;
elsif Pointer > Data'Last then
return;
end if;
end loop;
-- when First_Zero =>
-- if Data (Pointer) /= 0 then
-- Raise_Exception (Data_Error'Identity, Invalid_EOC);
-- end if;
-- State := Second_Zero;
-- Pointer := Pointer + 1;
-- when others =>
-- if Data (Pointer) /= 0 then
-- Raise_Exception (Data_Error'Identity, Invalid_EOC);
-- end if;
-- State := 0;
-- Pointer := Pointer + 1;
-- return;
end case;
end loop;
end Feed;
-- procedure Feed
-- ( Item : in out Choice_Data_Item;
-- Data : Stream_Element_Array;
-- Pointer : in out Stream_Element_Offset;
-- Client : in out State_Machine'Class;
-- State : in out Stream_Element_Offset
-- ) is
-- Skip_Until_Zero : constant := 1;
-- Skip_Definite : constant := 2;
-- begin
-- if Item.Position = 0 then
-- while Pointer <= Data'Last loop
-- ada.Text_IO.Put_Line(" feed ***IGNORED*** choice "
-- &" pointer="&stream_element_offset'image(pointer)
-- &" data=%"&image(integer(data(pointer)),base=>16)
-- &" state="&stream_element_offset'image(State)
-- &" length="&stream_element_offset'image(item.Length)
-- &" "&Image(data(data'first..pointer-1))&"|"&Image(data(pointer..data'last))
-- );
-- case State is
-- when 0 => -- Get ignored item length
-- if not Item.Unsolicited then
-- Raise_Exception
-- ( Data_Error'Identity,
-- ( "ASN.1 unexpected choice tag "
-- & Image (Item.Tag)
-- ) );
-- end if;
-- if Item.Length < 0 then
-- State := Skip_Until_Zero;
-- elsif Item.Length = 0 then -- Zero length
-- return;
-- else
-- State := Skip_Definite;
-- end if;
-- when Skip_Definite => -- Skip definite length object
-- loop
-- Pointer := Pointer + 1;
-- Item.Length := Item.Length - 1;
-- if Item.Length <= 0 then
-- State := 0;
-- return;
-- elsif Pointer > Data'Last then
-- return;
-- end if;
-- end loop;
-- when others => -- Skip indefinite length object
-- loop
-- if Data (Pointer) = 0 then -- The first zero, leave
-- State := 0; -- it be
-- return;
-- end if;
-- Pointer := Pointer + 1;
-- if Pointer > Data'Last then
-- return;
-- end if;
-- end loop;
-- end case;
-- end loop;
-- else
-- ada.Text_IO.Put_Line(" feed choice "
-- &" pointer="&stream_element_offset'image(pointer)
-- &" data=%"&image(integer(data(pointer)),base=>16)
-- &" state="&stream_element_offset'image(State)
-- &" length="&stream_element_offset'image(item.Length)
-- &" "&Image(data(data'first..pointer-1))&"|"&Image(data(pointer..data'last))
-- );
-- Feed
-- ( Get (Item.Map, Item.Position).Item.all,
-- Data,
-- Pointer,
-- Client,
-- State
-- );
-- end if;
-- end Feed;
procedure Finalize
( Item : in out Choice_Data_Item
) is
procedure Free is
new Ada.Unchecked_Deallocation
( Tag_Type_Array,
Tag_Type_Array_Ptr
);
begin
Free (Item.List);
end Finalize;
function Get_Children
( Item : Choice_Data_Item
) return Data_Item_Ptr_Array is
begin
if not Item.Initialized then
Uninitialized (Item);
end if;
declare
Result : Data_Item_Ptr_Array (1..Item.List'Length);
begin
for Index in Result'Range loop
Result (Index) := Get (Item.Map, Integer (Index)).Item;
end loop;
return Result;
end;
end Get_Children;
function Get_Length
( Item : Choice_Data_Item
) return Natural is
begin
if not Item.Initialized then
Uninitialized (Item);
end if;
return Item.List'Length;
end Get_Length;
function Get_Selected
( Item : Choice_Data_Item
) return Natural is
begin
if not Item.Initialized then
Uninitialized (Item);
end if;
return Item.Current.Position;
end Get_Selected;
function Get_Selected
( Item : Choice_Data_Item
) return Abstract_ASN1_Data_Item_Ptr is
begin
if not Item.Initialized then
Uninitialized (Item);
end if;
if Item.Current.Position = 0 then
return null;
else
return Abstract_ASN1_Data_Item'Class
( Get
( Item.Map,
Item.List (Item.Current.Position)
) .Item.all
) 'Unchecked_Access;
end if;
end Get_Selected;
function Get_Size (Item : Choice_Data_Item) return Natural is
begin
if not Item.Initialized then
Uninitialized (Item);
end if;
declare
Result : Natural := 1;
begin
for Index in 1..Get_Size (Item.Map) loop
Result :=
Result + Get_Size (Get (Item.Map, Index).Item.all);
end loop;
return Result;
end;
end Get_Size;
function Get_Tag
( Item : Choice_Data_Item;
Index : Positive
) return Tag_Type is
begin
if not Item.Initialized then
Uninitialized (Item);
end if;
if Index > Item.List'Last then
Raise_Exception (Constraint_Error'Identity, Wrong_Index);
else
return Item.List (Index);
end if;
end Get_Tag;
procedure Initialized (Item : in out Choice_Data_Item) is
begin
null;
end Initialized;
function Is_Implicit (Item : Choice_Data_Item) return Boolean is
begin
return True;
end Is_Implicit;
function Is_Unsolicited_Enabled
( Item : Choice_Data_Item
) return Boolean is
begin
if not Item.Initialized then
Uninitialized (Item);
end if;
return Item.Unsolicited;
end Is_Unsolicited_Enabled;
function Resolve_Selected
( Item : Abstract_ASN1_Data_Item'Class
) return Abstract_ASN1_Data_Item_Ptr is
Current : Abstract_ASN1_Data_Item_Ptr :=
Abstract_ASN1_Data_Item'Class
( Self (Item).all
) 'Unchecked_Access;
begin
loop
if Current.all in ASN1_Data_Item'Class then
return Current;
elsif Current.all not in Choice_Data_Item'Class then
Raise_Exception
( Data_Error'Identity,
Wrong_Choice & Expanded_Name (Current.all'Tag)
);
end if;
Current := Get_Selected (Choice_Data_Item'Class (Current.all));
if Current = null then
Raise_Exception (Data_Error'Identity, Nothing_Selected);
end if;
end loop;
end Resolve_Selected;
function Self
( Item : Choice_Data_Item'Class
) return Choice_Data_Item_Ptr is
package From_Set_Address is
new System.Address_To_Access_Conversions
( Choice_Data_Item'Class
);
use From_Set_Address;
begin
return To_Pointer (Item'Address).all'Unchecked_Access;
end Self;
procedure Set_Implicit_Tag
( Item : in out Choice_Data_Item;
Tag : Tag_Type;
Length : Stream_Element_Offset
) is
begin
if Length = 0 then
Raise_Exception (Data_Error'Identity, Invalid_Length);
end if;
end Set_Implicit_Tag;
procedure Set_Selected
( Item : in out Choice_Data_Item;
Index : Positive
) is
begin
if not Item.Initialized then
Uninitialized (Item);
elsif Index > Item.List'Last then
Raise_Exception (Constraint_Error'Identity, Wrong_Index);
else
Item.Current.Position := Index;
end if;
end Set_Selected;
procedure Set_Tag
( Item : in out Choice_Data_Item;
Index : Positive;
Tag : Tag_Type
) is
begin
if not Item.Initialized then
Uninitialized (Item);
elsif Index > Item.List'Last then
Raise_Exception (Constraint_Error'Identity, Wrong_Index);
elsif not Tag.Optional then
Raise_Exception
( Mode_Error'Identity,
"ASN.1 set element is not otpional"
);
end if;
declare
Element : Element_Descriptor;
Offset : Integer := Find (Item.Map, Tag);
begin
if Offset > 0 then
Element := Get (Item.Map, Offset);
if Element.Position /= Index then
Raise_Exception
( Constraint_Error'Identity,
( "ASN.1 set already contains element tagged by "
& Image (Tag)
) );
end if;
else
Offset := Find (Item.Map, Item.List (Index));
Element := Get (Item.Map, Offset);
if Tag.Class = Universal_Tag then
declare
Object : Abstract_ASN1_Data_Item'Class renames
Abstract_ASN1_Data_Item'Class
( Element.Item.all
);
begin
if not Is_Implicit (Object) then
Raise_Exception
( Mode_Error'Identity,
"Universally tagged object is not implicit"
);
elsif Object not in ASN1_Data_Item'Class then
Raise_Exception
( Mode_Error'Identity,
"Universally tagged object has no definite type"
);
elsif ( Tag.Value
/= Get_ASN1_Type (ASN1_Data_Item'Class (Object))
) then
Raise_Exception
( Mode_Error'Identity,
( "The universal tag value differs "
& "from the object type"
) );
end if;
end;
end if;
Remove (Item.Map, Offset);
Add (Item.Map, Tag, Element);
Item.List (Index) := Tag;
end if;
end;
end Set_Tag;
procedure Set_Untagged (Item : in out Choice_Data_Item) is
begin
null;
end Set_Untagged;
end GNAT.Sockets.Connection_State_Machine.ASN1.Choices;
|