Menu

null pointer decoding

2008-07-14
2013-04-23
  • Nobody/Anonymous

    Hi,

    I'm totally new to ASN.1 so please excuse my ignorance if I'm asking something stupid, but it seems to be a steep learning curve if you have a zero budget.
    So here goes: I'm trying to output some ASN.1 encoded files to plain text or anything similar (don't suppose there's already a tool for that? :) ).

    I generated the classes using the bnotes compiler and they compiled succesfully. However when I run the following sequence I get a null pointer exception. The data file I'm using is one I was supplied with, but I have no idea if it is correct (and indeed specifying a file which I know is incorrect resulted in the same error) or maybe I missed a step.

        public static void main(String[] args) throws Exception {
            IDecoder decoder = CoderFactory.getInstance().newDecoder("BER");
            File f = new File("D:\\path\\to\\file");
            FileInputStream i = new FileInputStream(f);
            ASNRecord r = decoder.decode(i , ASNRecord.class);
        }

    Exception in thread "main" java.lang.NullPointerException
            at org.bn.coders.Decoder.decode(Decoder.java:37)
            at asn1foobar.Main.main(Main.java:27)

    So:
    1) Is there some tool/ code to validate the data file against the asn file to make sure they match?
    2) Does bnotes provide the above mentioned functionality?
    3) How do I find out the cause of the error? I don't know if the file is incorrect or not.
    4) Am I having a bad dream? :)

     
    • Nobody/Anonymous

      Solved it by using the proper encoding, "DER" as opposed to "BER" and specifying the correct Class. To anyone having the same problem (and level of knowledge) I would advise for a quick fix to try the first few items in the ASN file as classes and check their encoding method (DER is non human readable form).

       
    • Nobody/Anonymous

      Okay ... next problem.

      When I read from an InputStream the second time, the program hangs. I believe it enters an infinite loop as I have placed a breakpoint over Decoder.java line 46 (if(objectClass.isAnnotationPresent(ASN1PreparedElement.class)) {) to break when the hit count is above 9k. For the first read, it is skipped, then for the second it is triggered. Does anyone know a way around this?

          public static void main(String[] args) throws Exception {
              // TODO code application logic here
              IDecoder decoder = CoderFactory.getInstance().newDecoder("DER");
              File f = new File("D:\\path\\to\\file");
              BufferedInputStream i = new BufferedInputStream(new FileInputStream(f));
              GPRSCallEventRecord x;
              System.out.println("INITIAL: " + i.available());
              x = decoder.decode(i , GPRSCallEventRecord.class);
              System.out.println("FIRST READ: " + i.available());
              x = decoder.decode(i , GPRSCallEventRecord.class);
              System.out.println("SECOND READ: " + i.available());
          }

      OUTPUT:
      INITIAL: 1890958
      FIRST READ: 1890777
      ---->hanging

       
    • Nobody/Anonymous

      The problem seems to be in BERDecoding.java line 180 and not connected to streams

      while(sizeOfSet < len && fieldEncoded);

      For some records the condition remains always true (the size of set isn't incremented to reach len)

       
    • Nobody/Anonymous

      Hello!
      Can you provide your test-case sources (ASN.1, Test classes, file with byte stream) ?
      Thanks for your interest!

       
      • Nobody/Anonymous

        and please submit these information over bug tracker.
        Thanks!

         
      • Nobody/Anonymous

        Your version is 1.5.2 from SVN?

         
        • Nobody/Anonymous

          My version is 1.5.1 from the download page. But I looked at the source code and if I'm right about the cause it seems that the problem is not solved.
          Unfortunately, I cannot submit the ASN.1 file nor the byte stream as they are marked confidential (they are for a customer company, not the one I work for).

          Here is where I think the problem is:

          In file BERDecoder.java the below sequence has a boolean fieldEncoded that is used in the exit condition from the loop. It is set to true, if decodeSequenceField() returns a non-null value.

          The exist condition looks at length and the abovementioned boolean value. However in my case, if the length condition remains true, the decodeSequenceField always returns null values. As such, the length is not incremented and the fieldEncoded value remains true.

          I am thinking that fieldEncoded should be set to false as the first instruction in the do loop, but I have no idea as to the logic behind it. If you could tell me how to compile the application from the sources I could make the modification and update you with the result.

          Please tell me if I should repost this in the bugtracker and continue the discussion there.

                  boolean fieldEncoded = false;
                  do {
                     
                      for(int i=0; i<fields.length; i++) {       
                          Field field = fields[i];
                          DecodedObject obj = decodeSequenceField(fieldTag,set,i, field,stream,elementInfo, false);
                          if(obj!=null) {
                              fieldEncoded = true;
                              sizeOfSet +=obj.getSize();               
          ..............................................................................
                          }
                      }   
                  }
                  while(sizeOfSet < len && fieldEncoded);

           
          • Nobody/Anonymous

            Forgot to mention the SET field has optional elements.

             

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.