Skip to content

Loading…

fixed signed 16bit amqp framing conversion #38

Merged
merged 2 commits into from

1 participant

@ghost

Apologies for before, This is the fix for the actual issue outlined in issue #36

@michaelklishin michaelklishin commented on an outdated diff
lib/amq/pack.rb
((7 lines not shown))
end
end
+
+ def self.convert_unsigned_to_signed(unsigned16bitValue)
@michaelklishin RabbitMQ client libraries for Ruby member

If this method assumes 16 bit inputs, it needs to be renamed. The input can then be renamed to just "value".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@michaelklishin michaelklishin merged commit 0c6dfc1 into ruby-amqp:master

1 check failed

Details default The Travis CI build failed
@michaelklishin
RabbitMQ client libraries for Ruby member

In 1.9.1..

@ghost ghost deleted the unknown repository branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Dec 18, 2013
  1. fixed signed 16bit amqp framing conversion

    Benjamin Conlan committed
This page is out of date. Refresh to see the latest.
Showing with 23 additions and 4 deletions.
  1. +2 −0 .gitignore
  2. +4 −3 lib/amq/pack.rb
  3. +17 −1 spec/amq/{hacks_spec.rb → pack_spec.rb}
View
2 .gitignore
@@ -3,6 +3,8 @@
/.ruby-version
tmp
*.pyc
+*.iml
+.idea
/vendor/bundle
/vendor/amq-*
/coverage
View
7 lib/amq/pack.rb
@@ -7,7 +7,8 @@ module AMQ
# compatible with Ruby 1.8+.
module Pack
UINT64 = "Q".freeze
- INT16 = "c".freeze
+ UINT16_BE = "n".freeze
+ INT16 = "c".freeze
if Endianness.big_endian?
def self.pack_uint64_big_endian(long_long)
@@ -42,8 +43,8 @@ def self.pack_int16_big_endian(short)
end
def self.unpack_int16_big_endian(data)
- data = data.bytes.to_a.reverse.map(&:chr).join
- data.unpack(INT16)
+ value = data.bytes.to_a.map(&:chr).join.unpack(UINT16_BE)[0]
+ [(value & ~(1 << 15)) - (value & (1 << 15))]
end
end
end
View
18 spec/amq/hacks_spec.rb → spec/amq/pack_spec.rb
@@ -4,7 +4,23 @@
module AMQ
- describe Hacks do
+ describe Pack do
+ context "16-bit big-endian packing / unpacking" do
+ let(:examples_16bit) {
+ {
+ 0x068D => "\x06\x8D" # 1677
+ }
+ }
+
+ it "unpacks signed integers from a string to a number" do
+ examples_16bit.each do |key, value|
+ described_class.unpack_int16_big_endian(value)[0].should == key
+ end
+ end
+ end
+
+
+
context "64-bit big-endian packing / unpacking" do
let(:examples) {
{
Something went wrong with that request. Please try again.