Am I missing something about BER-compressed integer?

  • Thread starter Francis Cianfrocca
  • Start date
F

Francis Cianfrocca

All:

The following:

p [9999].pack("w")

gives:

"\316\017"

but I expected to get:

"\047\017"

Notice the correct bit pattern for the upper octet (100111) appears in
the octet generated by Ruby (11001110) but shifted and with an extra
bit at the top. Is this a bug in pack("w"), or is my expectation
wrong?

Thanks,
-f
 
F

Francis Cianfrocca

All:

The following:

p [9999].pack("w")

gives:

"\316\017"

but I expected to get:

"\047\017"

Notice the correct bit pattern for the upper octet (100111) appears in
the octet generated by Ruby (11001110) but shifted and with an extra
bit at the top. Is this a bug in pack("w"), or is my expectation
wrong?

Thanks,
-f


I figured it out. There's no bug in Ruby. pack("w") produces the
integer representation that's occasionally used in BER to save space
when representing things like OIDs, not the "standard" BER
representation that requires a field-size prefix.
 
E

Edwin Fine

Francis said:
I figured it out. There's no bug in Ruby. pack("w") produces the
integer representation that's occasionally used in BER to save space
when representing things like OIDs, not the "standard" BER
representation that requires a field-size prefix.

Ruby uses BER as specified by Perl. Excerpt from the perlpacktut page:

The pack code w has been added to support a portable binary data
encoding scheme that goes way beyond simple integers. A BER (Binary
Encoded Representation) compressed unsigned integer stores base 128
digits, most significant digit first, with as few digits as possible.
Bit eight (the high bit) is set on each byte except the last. There is
no size limit to BER encoding, but Perl won't go to extremes.
---------
Applying the above rules:

irb(main):045:0> printf("%08b%08b", 0b10000000 | (9999/128), 9999%128)
1100111000001111=> nil

Ruby:

irb(main):046:0> [9999].pack("w").each_byte { |b| printf("%08b", b) }
1100111000001111=> "\316\017"
 
E

Edwin Fine

Francis said:
Thanks for this info. BER is not specified by Perl, of course, but
rather by
the ITU. The only normative language I've found on the "BER-compressed (snip)
Array#pack doesn't support this encoding as far as I know (I ended up
writing it by hand in the Net::BER module).

What I meant was not that Perl specified the BER encoding. I just meant
that Ruby's implementation behavior is based on the Perl pack behavior,
which itself was based on a project at Casbah.org (which I could not
locate). They explicitly say that it's not the BER from ASN.1. I don't
know if that means it is or is not the one defined by ITU.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top