Hello everybody,
reading as few bytes as possible, how can I tell a P7M and a P7S
(P7M detached) apart? Please consider all the possible situations
(optional content, content present but empty).
Kind regards,
Giacomo

These *Parser classes are designed for lazy stream-parsing; in
particular the final readObject call (for 'content') is only reading the
tag/length (if the content is present) before returning a parser (or null).

Re: Tell P7M and P7S apart

Hi Giacomo,

There's no reason it shouldn't work with definite lengths I assume you
are seeing the "corrupted stream" exception because you are only passing
in the beginning of the file, or fiddling with the stream in some other way.

In any case, to avoid the bounds checking, you can use:
new ASN1StreamParser(seqStream, Integer.MAX_VALUE);

I suppose the code I gave is not strictly the minimum number of bytes
possible, since it will read the tag and length of the 'content' tagged
object, when in theory you only need to read (or not) the first byte of
its tag.

If somehow those extra few bytes are important, you might need to copy
some of the internal code and adapt it to the bare minimum.

Regards,
Pete Dettman

On 5/06/2017 2:56 AM, Giacomo Boccardo wrote:

> Hi Peter,
>
> thanks for your solution, but unfortunately I've already tried that
> and it doesn't work if you have definite lengths.
>
> I have many
>
> java.io.IOException: corrupted stream - out of bounds length found
>
> on the second instruction because
>
> int length = ASN1InputStream.readLength(_in, _limit);
>
> is called and if you don't have enough bytes on the stream it raises
> that exception.
>
> If I pass all the stream it works, but I should investigate further to
> be sure that all those calls are really lazy: I need a quick detection
> using as few bytes as possible.

Re: Tell P7M and P7S apart

On 06/05/17 15:06:45, Peter Dettman wrote:
> Hi Giacomo,
Hi Peter,
>
> There's no reason it shouldn't work with definite lengths I assume you
> are seeing the "corrupted stream" exception because you are only passing
> in the beginning of the file, or fiddling with the stream in some other way.
That's exactly the reason.
> In any case, to avoid the bounds checking, you can use:
> new ASN1StreamParser(seqStream, Integer.MAX_VALUE);
>
> I suppose the code I gave is not strictly the minimum number of bytes
> possible, since it will read the tag and length of the 'content' tagged
> object, when in theory you only need to read (or not) the first byte of
> its tag.
> If somehow those extra few bytes are important, you might need to copy
> some of the internal code and adapt it to the bare minimum.
A few extra bytes are not a problem: I simply need not to read all the
content just to test if it's present :)
If it works like that, it's surely better than reading it as I did.
>
> Regards,
> Pete Dettman
>
Kind regards,
Giacomo

> On 5/06/2017 2:56 AM, Giacomo Boccardo wrote:
>> Hi Peter,
>>
>> thanks for your solution, but unfortunately I've already tried that
>> and it doesn't work if you have definite lengths.
>>
>> I have many
>>
>> java.io.IOException: corrupted stream - out of bounds length found
>>
>> on the second instruction because
>>
>> int length = ASN1InputStream.readLength(_in, _limit);
>>
>> is called and if you don't have enough bytes on the stream it raises
>> that exception.
>>
>> If I pass all the stream it works, but I should investigate further to
>> be sure that all those calls are really lazy: I need a quick detection
>> using as few bytes as possible.
>