On 08/12/2011 11:41 AM, Dmitriy Anisimkov wrote:
Is there way to decode binary stream with indefinite number of ASN.1 defined records ?


To know the record length I have to use asn1_get_tag_der and asn1_get_length_ber.
I made it in Ada.
------------------------------------------------
   function Get_Length
     (Data : Stream_Element_Array) return Stream_Element_Count
   is
      use type C.long;

      subtype Offset is Stream_Element_Offset;

      Tag_Len, BER_Len : aliased C.int;
      cls : aliased C.unsigned_char;
      tag : aliased C.unsigned_long;
      RC : constant C.int :=
        asn1_get_tag_der
          (Data (Data'First)'Unrestricted_Access, Data'Length, cls'Access,
           Tag_Len'Access, tag'Access);
      RL : C.long;

   begin
      if RC /= ASN1_SUCCESS then
         raise Constraint_Error with Error_Message (RC);
      end if;

      if Tag_Len >= Data'Length then
         return 0;
      end if;

      RL := asn1_get_length_ber
              (Data (Data'First + Offset (Tag_Len))'Unrestricted_Access,
               Data'Length - Tag_Len, BER_Len'Access);

      if RL < 0 then
         raise Constraint_Error with Error_Message (C.int (RL));
      end if;

      RL := RL + C.long (Tag_Len + BER_Len);

      if RL > Data'Length then
         return 0;
      end if;

      return Stream_Element_Count (RL);
   end Get_Length;
--------------------------------------------------

The better way would be if function asn1_der_decoding (or any similar new one) returned the decoded record length.


Reply via email to