simple_components_4.62.0_a5c16c1d/gnat-sockets-connection_state_machine-asn1-sets-implicit.adb

   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
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
--                                                                    --
--  package                         Copyright (c)  Dmitry A. Kazakov  --
--     GNAT.Sockets.Connection_State_Machine.      Luebeck            --
--     ASN1.Sets.Implicit                                   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 GNAT.Sockets.Connection_State_Machine.ASN1.Choices;
use  GNAT.Sockets.Connection_State_Machine.ASN1.Choices;

with Ada.Unchecked_Conversion;
with System.Address_To_Access_Conversions;

package body GNAT.Sockets.Connection_State_Machine.ASN1.Sets.Implicit is

   function Always_Constructed
            (  Item : Implicit_Set_Data_Item
            )  return Boolean is
   begin
      return True;
   end Always_Constructed;

   function Always_Constructed
            (  Item : Implicit_External_Set_Of_Data_Item
            )  return Boolean is
   begin
      return True;
   end Always_Constructed;

   procedure Append
             (  Item : in out Implicit_External_Set_Of_Data_Item
             )  is
   begin
      if Item.Container = null then
         Uninitialized (Item);
      end if;
      declare
         Stream  : aliased Initialization_Stream;
         Element : constant Abstract_ASN1_Data_Item_Ptr :=
                            Create
                            (  Implicit_External_Set_Of_Data_Item'Class
                               (  Item
                               ) 'Unchecked_Access
                            );
      begin
         Register_Allocator (Item);
         Stream.Shared := Item.Container.all'Unchecked_Access;
         Abstract_ASN1_Data_Item'Class'Write
         (  Stream'Access,
            Element.all
         );
         Put (Item.Map, Item.Map.Length + 1, Element);
         Item.Map.Length := Item.Map.Length + 1;
      end;
   end Append;

   procedure Check
             (  Container : ASN1_Data_Item'Class;
                Item      : Data_Item'Class
             )  is
   begin
      if Item not in Abstract_ASN1_Data_Item'Class then
         Raise_Exception
         (  Use_Error'Identity,
            (  "ASN.1 "
            &  Image (Get_ASN1_Type (Container))
            &  " contains non ASN.1 object "
            &  Expanded_Name (Item'Tag)
         )  );
      end if;
   end Check;

   procedure Encode
             (  Item    : Implicit_Set_Data_Item;
                Data    : in out Stream_Element_Array;
                Pointer : in out Stream_Element_Offset
             )  is
   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);
      end if;
      declare
         Index : Stream_Element_Offset := Pointer;
      begin
         for Position in Item.List'Range loop
            declare
               Tag        : constant Tag_Type := Item.List (Position);
               Descriptor : constant Element_Descriptor :=
                                     Get (Item.Map, Tag);
               Value      : Abstract_ASN1_Data_Item'Class renames
                            Abstract_ASN1_Data_Item'Class
                            (  Descriptor.Item.all
                            );
            begin
               if not Descriptor.Unset then
                  if (  Tag.Class = Universal_Tag
                     and then
                        Tag.Value = 0
                     )  then
                     declare
                        Selected : Abstract_ASN1_Data_Item'Class renames
                                   Resolve_Selected (Value).all;
                     begin
                        Put
                        (  Data,
                           Index,
                           (  Universal_Tag,
                              Get_ASN1_Type
                              (  ASN1_Data_Item'Class (Selected)
                              ),
                              False
                           ),
                           Always_Constructed (Selected)
                        );
                     end;
                  else
                     Put
                     (  Data,
                        Index,
                        Tag,
                        (  Always_Constructed (Value)
                        or else
                           (  Tag.Class /= Universal_Tag
                           and then
                              not Is_Implicit (Value)
                     )  )  );
                  end if;
                  Definite_Encode (Value, Data, Index);
               end if;
            end;
         end loop;
         Pointer := Index;
      end;
   end Encode;

   procedure Encode
             (  Item    : Implicit_External_Set_Of_Data_Item;
                Data    : in out Stream_Element_Array;
                Pointer : in out Stream_Element_Offset
             )  is
      Index : Stream_Element_Offset := Pointer;
   begin
      for Position in 1..Item.Map.Length loop
         Encode (Get (Item.Map, Position).all, Data, Index);
      end loop;
      Pointer := Index;
   end Encode;

   procedure End_Of_Subsequence
             (  Item    : in out Implicit_Set_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.Definite then
         if Item.Total_Length = Get_Length (Item.Current) then
            if (  (  Item.Elements_Count -- Only optionals left
                  +  Item.Optional_Count
                  )
               >= Item.List'Length
               )  then
               State := 0;
               return;
            end if;
            Raise_Exception (Data_Error'Identity, Invalid_Length);
         elsif Item.Total_Length < Get_Length (Item.Current) then
            Raise_Exception (Data_Error'Identity, Invalid_Length);
         end if;
         Item.Total_Length :=
            Item.Total_Length - Get_Length (Item.Current);
      end if;
   end End_Of_Subsequence;

   procedure End_Of_Subsequence
             (  Item    : in out Implicit_External_Set_Of_Data_Item;
                Data    : Stream_Element_Array;
                Pointer : Stream_Element_Offset;
                Client  : in out State_Machine'Class;
                State   : in out Stream_Element_Offset
             )  is
      Count : constant Stream_Element_Offset :=
              Stream_Element_Offset (Client.Fed - Item.Fed_Count);
   begin
      if Item.Definite then
         if Item.Total_Length = Count then
            State := 0;
            return;
         elsif Item.Total_Length < Count then
            Raise_Exception (Data_Error'Identity, Invalid_Length);
         end if;
         Item.Total_Length := Item.Total_Length - Count;
      end if;
   end End_Of_Subsequence;

   procedure Enumerate
             (  Stream : access Root_Stream_Type'Class;
                Item   : Implicit_Set_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  : Implicit_Set_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;
               Object.Elements_Count := 0;
               Implicit_Set_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 (Item, 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),
                        (  Unset    => True,
                           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 Enumerate
             (  Stream : access Root_Stream_Type'Class;
                Item   : Implicit_External_Set_Of_Data_Item
             )  is
      procedure Find
                (  Object : in out
                            Implicit_External_Set_Of_Data_Item'Class;
                   Shared : Shared_Data_Item_Ptr
                )  is
         This : Shared_Data_Item_Ptr := Shared;
      begin
         loop
            if This = null then
               Raise_Exception (Use_Error'Identity, No_Container);
            end if;
            exit when This.all in External_String_Buffer'Class;
            This := This.Previous;
         end loop;
         Object.Container :=
            External_String_Buffer'Class (This.all)'Unchecked_Access;
      end Find;
   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 Item.Container = null then
            if This.Parent = null then
               Find (Self (Item).all, This.Shared);
            else
               Find (Self (Item).all, This.Parent.all);
            end if;
         end if;
         Enumerate (Stream, ASN1_Data_Item (Item));
         Initialized (Self (Item).all);
      end;
   end Enumerate;

   procedure Feed_Initialize
             (  Item : in out Implicit_Set_Data_Item'Class
             )  is
   begin
      Item.Elements_Count := 0;
      Item.Optional_Count := 0;
      for Index in 1..Get_Size (Item.Map) loop
         declare
            Element : Element_Descriptor := Get (Item.Map, Index);
         begin
            Element.Unset := True;
            if Item.List (Index).Optional then
               Item.Optional_Count := Item.Optional_Count + 1;
            end if;
            Replace (Item.Map, Index, Element);
         end;
      end loop;
   end Feed_Initialize;

   Get_Element   : constant := 1;
   Get_Tag_Value : constant := 2;
   Select_By_Tag : constant := 3;
   First_Zero    : constant := 4;
   Second_Zero   : constant := 5;

   procedure Explicit_Feed
             (  Item    : in out Implicit_Set_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
         Check
         (  Data (Pointer),
            (1 => Get_ASN1_Type (Implicit_Set_Data_Item'Class (Item)))
         );
         Pointer := Pointer + 1;
         State   := Start_Length;
         Feed_Initialize (Item);
         if Pointer > Data'Last then
            return;
         end if;
      end if;
      if State < 0 then
         while not Is_Length_Ready (State) loop
            if Pointer > Data'Last then
               return;
            end if;
            Embedded_Feed (Data, Pointer, State);
         end loop;
         Item.Definite  := not Is_Indefinite (State);
         if Item.Definite then
            Item.Total_Length := Get_Length (State);
            if Item.Total_Length = 0 then
               if (  (  Item.Elements_Count -- Only optionals left
                     +  Item.Optional_Count
                     )
                  >= Item.List'Length
                  )  then
                  State := 0;
                  return;
               else
                  Raise_Exception
                  (  Data_Error'Identity,
                     Invalid_Length
                  );
               end if;
            end if;
         else
            Item.Total_Length := 0;
         end if;
         State := Get_Element;
      end if;
      Feed
      (  Item    => Item,
         Data    => Data,
         Pointer => Pointer,
         Client  => Client,
         State   => State
      );
   end Explicit_Feed;

   procedure Explicit_Feed
             (  Item    : in out Implicit_External_Set_Of_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
         if Item.Container = null then
            Uninitialized (Item);
         end if;
         Check
         (  Data (Pointer),
            (  1 => Get_ASN1_Type
                    (  Implicit_External_Set_Of_Data_Item'Class (Item)
         )  )       );
         Item.Map.Length := 0;
         State   := Start_Length;
         Pointer := Pointer + 1;
         if Pointer > Data'Last then
            return;
         end if;
      end if;
      if State < 0 then
         while not Is_Length_Ready (State) loop
            if Pointer > Data'Last then
               return;
            end if;
            Embedded_Feed (Data, Pointer, State);
         end loop;
         Item.Definite := not Is_Indefinite (State);
         if Item.Definite then
            Item.Total_Length := Get_Length (State);
            if Item.Total_Length = 0 then
               State := 0;
               return;
            end if;
         else
            Item.Total_Length := 0;
         end if;
         State := Get_Element;
      end if;
      Feed
      (  Item    => Item,
         Data    => Data,
         Pointer => Pointer,
         Client  => Client,
         State   => State
      );
   end Explicit_Feed;

   procedure Feed
             (  Item    : in out Implicit_Set_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
      procedure Call_Item (Element : Element_Descriptor) is
      begin
         State := Get_Element;
         Call
         (  Item    => Item.Current,
            Callee  => Element.Item.all,
            Tag     => Item.Current.Tag,
            Pointer => Pointer,
            Client  => Client,
            State   => State
         );
         Item.Current.Position := Element.Position;
         State := 0;
      end Call_Item;

      procedure Begin_Tag_Header is
      begin
         Item.Elements_Count := Item.Elements_Count + 1;
         case ASN1_Tag (Data (Pointer)) and 2#1100_0000# is
            when Application_Class =>
               Item.Current.Tag.Class := Application_Tag;
            when Context_Specific_Class =>
               Item.Current.Tag.Class := Context_Specific_Tag;
            when Private_Class =>
               Item.Current.Tag.Class := Private_Tag;
            when others =>
               Item.Current.Tag.Class := Universal_Tag;
         end case;
         Item.Current.Tag.Value :=
            ASN1_Type (Data (Pointer) and 16#1F#);
         if Item.Current.Tag.Value < 31 then
            State := Select_By_Tag;
         else
            State := Get_Tag_Value;
         end if;
         Pointer := Pointer + 1;
         if Item.Definite then
            if Item.Total_Length = 0 then
               Raise_Exception (Data_Error'Identity, Invalid_Length);
            end if;
            Item.Total_Length := Item.Total_Length - 1;
         end if;
      end Begin_Tag_Header;
   begin
      if State = 0 then
         Feed_Initialize (Item);
         State := Start_Length;
      end if;
      while Pointer <= Data'Last loop
         case State is
            when Get_Element =>
               if Item.Elements_Count >= Item.List'Length then
                  if Item.Definite then
                     if Item.Total_Length /= 0 then
                        Raise_Exception
                        (  Data_Error'Identity,
                           Invalid_Length
                        );
                     end if;
                     State := 0;
                     return;
                  else
                     State := First_Zero;
                  end if;
               elsif (  (  Item.Elements_Count -- Only optionals left
                        +  Item.Optional_Count
                        )
                     >= Item.List'Length
                     )  then
                  if Item.Definite then
                     if Item.Total_Length = 0 then
                        State := 0;
                        return;
                     end if;
                     if Data (Pointer) = 0 then
                        Raise_Exception
                        (  Data_Error'Identity,
                           "ASN.1 set element tag is expected"
                        );
                     else
                        Begin_Tag_Header;
                     end if;
                  else
                     if Data (Pointer) = 0 then
                        Pointer := Pointer + 1;
                        State   := Second_Zero;
                     else
                        Begin_Tag_Header;
                     end if;
                  end if;
               else
                  if Data (Pointer) = 0 then
                     Pointer := Pointer + 1;
                     State   := Second_Zero;
                  else
                     Begin_Tag_Header;
                  end if;
               end if;
            when Get_Tag_Value =>
               while Pointer <= Data'Last loop
                  declare
                     This : constant ASN1_Type :=
                                     ASN1_Type (Data (Pointer));
                     Tag  : Tag_Type renames Item.Current.Tag;
                  begin
                     Pointer := Pointer + 1;
                     if This > 127 then
                        Tag.Value := Tag.Value * 128 + (This - 128);
                     else
                        Tag.Value := Tag.Value * 128 + This;
                        State     := Select_By_Tag;
                        exit;
                     end if;
                     if Item.Definite then
                        if Item.Total_Length = 0 then
                           Raise_Exception
                           (  Data_Error'Identity,
                              Invalid_Length
                           );
                        end if;
                        Item.Total_Length := Item.Total_Length - 1;
                     end if;
                  exception
                     when Constraint_Error =>
                        Raise_Exception
                        (  Data_Error'Identity,
                           Tag_Too_Large
                        );
                  end;
               end loop;
            when Select_By_Tag =>
               declare
                  Element : Element_Descriptor;
                  Offset  : constant Integer :=
                                     Find (Item.Map, Item.Current.Tag);
               begin
                  if Offset <= 0 then
                     Raise_Exception
                     (  Data_Error'Identity,
                        (  "ASN.1 unexpected set tag "
                        &  Image (Item.Current.Tag)
                     )  );
                  end if;
                  Element := Get (Item.Map, Offset);
                  if not Element.Unset then
                     Raise_Exception
                     (  Data_Error'Identity,
                        (  "ASN.1 duplicated set element tagged by "
                        &  Image (Item.Current.Tag)
                     )  );
                  end if;
                  Element.Unset := False;
                  if Item.List (Element.Position).Optional then
                     Item.Optional_Count := Item.Optional_Count - 1;
                  end if;
                  Replace (Item.Map, Offset, Element);
                  Call_Item (Element);
                  return;
               end;
            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 Implicit_External_Set_Of_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
      procedure Call_Item is
         List    : Sequence renames Item.Current;
         Stream  : aliased Initialization_Stream;
         Element : constant Abstract_ASN1_Data_Item_Ptr :=
                            Create
                            (  Implicit_External_Set_Of_Data_Item'Class
                               (  Item
                               ) 'Unchecked_Access
                            );
      begin
         Register_Allocator (Item);
         Stream.Shared := Item.Container.all'Unchecked_Access;
         Abstract_ASN1_Data_Item'Class'Write
         (  Stream'Access,
            Element.all
         );
         Put (Item.Map, Item.Map.Length + 1, Element);
         Item.Map.Length := Item.Map.Length + 1;
         List.Caller   := null;
         List.State    := 0;
         List.Current  := 1;
         List.List (1) := Element.all'Unchecked_Access;
         Call (Client, Pointer, List'Unchecked_Access, Get_Element);
         Item.Fed_Count := Client.Fed;
         State := 0;
      end Call_Item;
   begin
      if State = 0 then
         if Item.Container = null then
            Uninitialized (Item);
         end if;
         Item.Map.Length := 0;
         State := Get_Element;
      end if;
      while Pointer <= Data'Last loop
         case State is
            when Get_Element =>
               if Item.Definite then
                  if Item.Total_Length = 0 then
                     State := 0;
                     return;
                  end if;
                  if Data (Pointer) = 0 then
                     Raise_Exception
                     (  Data_Error'Identity,
                        (  "ASN.1 "
                        &  Image (Get_ASN1_Type (Item))
                        &  " element tag is expected"
                     )  );
                  else
                     Call_Item;
                     return;
                  end if;
               else
                  if Data (Pointer) = 0 then
                     Pointer := Pointer + 1;
                     State   := Second_Zero;
                  else
                     Call_Item;
                     return;
                  end if;
               end if;
            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 Finalize
             (  Item : in out Implicit_Set_Data_Item
             )  is
      procedure Free is
         new Ada.Unchecked_Deallocation
             (  Tag_Type_Array,
                Tag_Type_Array_Ptr
             );
   begin
      Free (Item.List);
      Finalize (ASN1_Data_Item (Item));
   end Finalize;

   procedure Finalize
             (  Item : in out Implicit_External_Set_Of_Data_Item
             )  is
   begin
      Freed (Item);
      Finalize (ASN1_Data_Item (Item));
   end Finalize;

   procedure Freed (Item : in out Implicit_External_Set_Of_Data_Item) is
   begin
      if Item.Registered then
         declare
            type Element_Type_Ptr is access Abstract_ASN1_Data_Item'Class;
            for Element_Type_Ptr'Storage_Pool use Item.Container.Pool;
            function Convert is
               new Ada.Unchecked_Conversion
                   (  Abstract_ASN1_Data_Item_Ptr,
                      Element_Type_Ptr
                   );
            procedure Free is
               new Ada.Unchecked_Deallocation
                   (  Abstract_ASN1_Data_Item'Class,
                      Element_Type_Ptr
                   );
            This : Element_Type_Ptr;
         begin
            Item.Registered := False;
            for Index in 1..Item.Map.Length loop
               This := Convert (Get (Item.Map, Index));
               Free (This);
            end loop;
         end;
      end if;
      Item.Map.Length := 0;
   end Freed;

   function Get
            (  Item  : Implicit_External_Set_Of_Data_Item;
               Index : Positive
            )  return Abstract_ASN1_Data_Item_Ptr is
   begin
      if Item.Container = null then
         Uninitialized (Item);
      elsif Index > Item.Map.Length then
         Raise_Exception (Constraint_Error'Identity, Wrong_Index);
      end if;
      return Get (Item.Map, Index);
   end Get;

   function Get
            (  Item     : Reference_Data_Item;
               Allocate : Boolean := False
            )  return Abstract_ASN1_Data_Item_Ptr is
   begin
      if Item.Container = null then
         Uninitialized (Item);
      elsif Item.Target = null then
         if not Allocate then
            Raise_Exception (Data_Error'Identity, Unset);
         end if;
         declare
            Stream : aliased Initialization_Stream;
            Object : Reference_Data_Item'Class renames Self (Item).all;
         begin
            Register_Allocator (Object);
            Object.Target := Create (Object'Access);
            Stream.Shared := Item.Container.all'Unchecked_Access;
            Abstract_ASN1_Data_Item'Class'Write
            (  Stream'Access,
               Item.Target.all
            );
         end;
      end if;
      return Item.Target;
   end Get;

   function Get_ASN1_Type
            (  Item : Implicit_Set_Data_Item
            )  return ASN1_Type is
   begin
      return ASN1.Set_Tag;
   end Get_ASN1_Type;

   function Get_ASN1_Type
            (  Item : Implicit_External_Set_Of_Data_Item
            )  return ASN1_Type is
   begin
      return ASN1.Set_Tag;
   end Get_ASN1_Type;

   function Get_Children
            (  Item : Implicit_External_Set_Of_Data_Item
            )  return Data_Item_Ptr_Array is
   begin
      if Item.Container = null then
         Uninitialized (Item);
      end if;
      declare
         Result : Data_Item_Ptr_Array
                  (  1
                  .. Data_Item_Offset (Item.Map.Length)
                  );
      begin
         for Index in Result'Range loop
            Result (Index) :=
               Get (Item.Map, Integer (Index)).all'Unchecked_Access;
         end loop;
         return Result;
      end;
   end Get_Children;

   function Get_Children
            (  Item : Implicit_Set_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_Container
            (  Item : Implicit_External_Set_Of_Data_Item
            )  return External_String_Buffer_Ptr is
   begin
      return Item.Container;
   end Get_Container;

   function Get_Length
            (  Item : Implicit_Set_Data_Item
            )  return Natural is
   begin
      if not Item.Initialized then
         Uninitialized (Item);
      end if;
      return Item.List'Length;
   end Get_Length;

   function Get_Length
            (  Item : Implicit_External_Set_Of_Data_Item
            )  return Natural is
   begin
      return Item.Map.Length;
   end Get_Length;

   function Get_Size
            (  Item : Implicit_Set_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_Size
            (  Item : Implicit_External_Set_Of_Data_Item
            )  return Natural is
      Result : Natural := 1;
   begin
      if Item.Container /= null then
         for Index in 1..Get_Size (Item.Container.all) loop
            Result := Result + Item.Map.Length;
         end loop;
      end if;
      return Result;
   end Get_Size;

   function Get_Size
            (  Item : Reference_Data_Item
            )  return Natural is
   begin
      if Item.Target = null then
         return 1;
      else
         return Get_Size (Item.Target.all);
      end if;
   end Get_Size;

   function Get_Tag
            (  Item  : Implicit_Set_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 Implicit_Set_Data_Item) is
   begin
      null;
   end Initialized;

   procedure Initialized
             (  Item : in out Implicit_External_Set_Of_Data_Item
             )  is
   begin
      null;
   end Initialized;

   function Is_Implicit
            (  Item : Implicit_Set_Data_Item
            )  return Boolean is
   begin
      return True;
   end Is_Implicit;

   function Is_Implicit
            (  Item : Implicit_External_Set_Of_Data_Item
            )  return Boolean is
   begin
      return True;
   end Is_Implicit;

   function Is_Set
            (  Item  : Implicit_Set_Data_Item;
               Index : Positive
            )  return Boolean is
   begin
      if not Item.Initialized then
         Uninitialized (Item);
      end if;
      if Index > Item.List'Last then
         Raise_Exception (Constraint_Error'Identity, Wrong_Index);
      end if;
      return not Get (Item.Map, Item.List (Index)).Unset;
   end Is_Set;

   function Is_Untagged
            (  Item  : Implicit_Set_Data_Item;
               Index : Positive
            )  return Boolean is
   begin
      if not Item.Initialized then
         Uninitialized (Item);
      end if;
      if Index > Item.List'Last then
         Raise_Exception (Constraint_Error'Identity, Wrong_Index);
      end if;
      return False;
   end Is_Untagged;

   procedure Register_Allocator
             (  Item : in out Implicit_External_Set_Of_Data_Item
             )  is
   begin
      if not Item.Registered then
         Item.Registered := True;
         Item.Notifier.Previous := Item.Container.Allocators;
         Item.Container.Allocators := Item.Notifier'Unchecked_Access;
      end if;
   end Register_Allocator;

   procedure Reset
             (  Item  : in out Implicit_Set_Data_Item;
                Index : Positive;
                Unset : Boolean
             )  is
   begin
      if not Item.Initialized then
         Uninitialized (Item);
      elsif Index > Get_Length (Item) then
         Raise_Exception (Constraint_Error'Identity, Wrong_Index);
      else
         declare
            Location : constant Tag_Type  := Item.List (Index);
            This     : Element_Descriptor := Get (Item.Map, Location);
         begin
            This.Unset := Unset;
            Replace (Item.Map, Location, This);
         end;
      end if;
   end Reset;

   function Self
            (  Item : Implicit_Set_Data_Item'Class
            )  return Implicit_Set_Data_Item_Ptr is
      package From_Set_Address is
         new System.Address_To_Access_Conversions
             (  Implicit_Set_Data_Item'Class
             );
      use From_Set_Address;
   begin
      return To_Pointer (Item'Address).all'Unchecked_Access;
   end Self;

   function Self
            (  Item : Implicit_External_Set_Of_Data_Item'Class
            )  return Implicit_External_Set_Of_Data_Item_Ptr is
      package From_Set_Address is
         new System.Address_To_Access_Conversions
             (  Implicit_External_Set_Of_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 Implicit_Set_Data_Item;
                Tag    : Tag_Type;
                Length : Stream_Element_Offset
             )  is
   begin
      if Length >= 0 then
         Item.Total_Length := Length;
         Item.Definite     := True;
      else
         Item.Total_Length := 0;
         Item.Definite     := False;
      end if;
   end Set_Implicit_Tag;

   procedure Set_Implicit_Tag
             (  Item   : in out Implicit_External_Set_Of_Data_Item;
                Tag    : Tag_Type;
                Length : Stream_Element_Offset
             )  is
   begin
      if Length >= 0 then
         Item.Total_Length := Length;
         Item.Definite     := True;
      else
         Item.Total_Length := 0;
         Item.Definite     := False;
      end if;
   end Set_Implicit_Tag;

   procedure Set_Optional
             (  Item     : in out Implicit_Set_Data_Item;
                Index    : Positive;
                Optional : Boolean
             )  is
   begin
      if not Item.Initialized then
         Uninitialized (Item);
      end if;
      if Index > Item.List'Last then
         Raise_Exception (Constraint_Error'Identity, Wrong_Index);
      end if;
      declare
         Element : Element_Descriptor;
         Offset  : Integer;
         Tag     : Tag_Type renames Item.List (Index);
      begin
         Tag.Optional := Optional;
         Offset  := Find (Item.Map, Tag);
         Element := Get (Item.Map, Offset);
         Element.Unset := Optional;
         Replace (Item.Map, Offset, Element);
      end;
   end Set_Optional;

   procedure Set_Tag
             (  Item  : in out Implicit_Set_Data_Item;
                Index : Positive;
                Tag   : Tag_Type;
                Unset : Boolean := False
             )  is
   begin
      if not Item.Initialized then
         Uninitialized (Item);
      end if;
      if Index > Item.List'Last then
         Raise_Exception (Constraint_Error'Identity, Wrong_Index);
      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;
            Element.Unset := Unset;
            Replace (Item.Map, Offset, Element);
         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;
            Element.Unset := Unset;
            Remove (Item.Map, Offset);
            Add (Item.Map, Tag, Element);
            Item.List (Index) := Tag;
         end if;
      end;
   end Set_Tag;

   procedure Write
             (  Stream : access Root_Stream_Type'Class;
                Item   : Unbounded_Element_Array
             )  is
   begin
      null;
   end Write;

end GNAT.Sockets.Connection_State_Machine.ASN1.Sets.Implicit;