Skip to content
This repository has been archived by the owner on Apr 23, 2023. It is now read-only.

Most efficient way to parse structures? #45

Closed
afk11 opened this issue Jul 17, 2015 · 3 comments
Closed

Most efficient way to parse structures? #45

afk11 opened this issue Jul 17, 2015 · 3 comments

Comments

@afk11
Copy link
Collaborator

afk11 commented Jul 17, 2015

This library makes it easy to create encoded structures, but when it comes to parsing them, I'm a little unsure about the best way.

I have been imperatively checking the type of each element in the structure after doing Object::fromBinary().

I wondered if it's possible to do anything similar to this, and parse a structure according to a template? If not I might take a look at PRing something for this.

I notice some of the structures have optional parameters (eg, tbsCertificateInfo), so I'd hope to handle that also.

$structure = new Sequence(
   new Set(
      new ObjectIdentifier(),
      new Sequence(
         new Integer(),
         new BitString()
      )
   )
);

$structure->fromBinary($byteString);
@afk11 afk11 changed the title Most efficient way to parse structure? Most efficient way to parse structures? Jul 17, 2015
@fgrosse
Copy link
Owner

fgrosse commented Jul 18, 2015

So far I recommend defining a class for the thing you want to parse then move the parsing logic into there (including checking types etc) (check X509/CertificateSubject if you haven't already).
This gives you full error handling capacities, you can handle optional arguments and have full type savety with phpdoc.

The idea of defining templates sounds interesting because it can remove the necessity for the boiler plate code. However, the approach you suggested would only work if I would change the constructors to always accept no arguments which I would rather not do. Instead we could have a nested nested array of type identifiers to define such a template.

If you have yet another idea on how to traverse parsed objects while maintaining type savety and error handling I am all ears :)

I was also thinking about writing a ASN.1 compiler which could take a ASN.1 definition and compile php code which uses PHPASN1 from that. This would be a separate repository though.

@fgrosse
Copy link
Owner

fgrosse commented Jul 18, 2015

I have create an experimental TemplateParser which you can check out using the experimental/template-parser branch.

I guess this is already pretty useful so I will probably merge this into master soon after I did some more error handling and improved the exception.

@afk11
Copy link
Collaborator Author

afk11 commented Aug 18, 2015

Thanks, I'll bear this advice in mind. Probably will be able to send the Certificate classes your way.

Re: informing a parser about the structure, maybe passing Identifer::XX is best, otherwise I would be thinking of doing Integer::class.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants