module BytestreamReader

Constants

BigEndian
LittleEndian

Public Instance Methods

read_array_s16_be(size) click to toggle source
# File lib/bytestream-reader.rb, line 138
def read_array_s16_be(size)
  tmp = read_array_u16_be(size)
  tmp.collect { |val| (val & ~(1 << 15)) - (val & (1 << 15)) }
end
read_array_s16_le(size) click to toggle source
# File lib/bytestream-reader.rb, line 143
def read_array_s16_le(size)
  tmp = read_array_u16_le(size)
  tmp.collect { |val| (val & ~(1 << 15)) - (val & (1 << 15)) }
end
read_array_s32_be(size) click to toggle source
# File lib/bytestream-reader.rb, line 148
def read_array_s32_be(size)
  tmp = read_array_u32_be(size)
  tmp.collect { |val| (val & ~(1 << 31)) - (val & (1 << 31)) }
end
read_array_s32_le(size) click to toggle source
# File lib/bytestream-reader.rb, line 153
def read_array_s32_le(size)
  tmp = read_array_u32_le(size)
  tmp.collect { |val| (val & ~(1 << 31)) - (val & (1 << 31)) }
end
read_array_s64_be(size) click to toggle source
# File lib/bytestream-reader.rb, line 158
def read_array_s64_be(size)
  tmp = read_array_u64_be(size)
  tmp.collect { |val| (val & ~(1 << 63)) - (val & (1 << 63)) }
end
read_array_s64_le(size) click to toggle source
# File lib/bytestream-reader.rb, line 163
def read_array_s64_le(size)
  tmp = read_array_u64_le(size)
  tmp.collect { |val| (val & ~(1 << 63)) - (val & (1 << 63)) }
end
read_array_s8(size) click to toggle source
# File lib/bytestream-reader.rb, line 134
def read_array_s8(size)
  readexactly(1*size).unpack("c*")
end
read_array_u16_be(size) click to toggle source
# File lib/bytestream-reader.rb, line 66
def read_array_u16_be(size)
  readexactly(2*size).unpack("n*")
end
read_array_u16_le(size) click to toggle source
# File lib/bytestream-reader.rb, line 70
def read_array_u16_le(size)
  readexactly(2*size).unpack("v*")
end
read_array_u32_be(size) click to toggle source
# File lib/bytestream-reader.rb, line 74
def read_array_u32_be(size)
  readexactly(4*size).unpack("N*")
end
read_array_u32_le(size) click to toggle source
# File lib/bytestream-reader.rb, line 78
def read_array_u32_le(size)
  readexactly(4*size).unpack("V*")
end
read_array_u64_be(size) click to toggle source
# File lib/bytestream-reader.rb, line 82
def read_array_u64_be(size)
  buf = readexactly(8*size).unpack("N*")
  val = []
  size.times do |i|
    val[i] = buf[i*2] << 32 | buf[i*2+1];
  end
  return val
end
read_array_u64_le(size) click to toggle source
# File lib/bytestream-reader.rb, line 91
def read_array_u64_le(size)
  buf = readexactly(8*size).unpack("V*")
  val = []
  size.times do |i|
    val[i] = buf[i*2+1] << 32 | buf[i*2];
  end
  return val
end
read_array_u8(size) click to toggle source
# File lib/bytestream-reader.rb, line 62
def read_array_u8(size)
  readexactly(1*size).unpack("C*")
end
read_s16() click to toggle source
# File lib/bytestream-reader.rb, line 206
def read_s16
  case @endian
  when BigEndian then read_s16_be
  when LittleEndian then read_s16_le
  else raise UndefinedEndianness.new
  end
end
read_s16_be() click to toggle source
# File lib/bytestream-reader.rb, line 173
def read_s16_be
  tmp = read_u16_be
  return (tmp & ~(1 << 15)) - (tmp & (1 << 15))
end
read_s16_le() click to toggle source
# File lib/bytestream-reader.rb, line 178
def read_s16_le
  tmp = read_u16_le
  return (tmp & ~(1 << 15)) - (tmp & (1 << 15))
end
read_s32() click to toggle source
# File lib/bytestream-reader.rb, line 214
def read_s32
  case @endian
  when BigEndian then read_s32_be
  when LittleEndian then read_s32_le
  else raise UndefinedEndianness.new
  end
end
read_s32_be() click to toggle source
# File lib/bytestream-reader.rb, line 183
def read_s32_be
  tmp = read_u32_be
  return (tmp & ~(1 << 31)) - (tmp & (1 << 31))
end
read_s32_le() click to toggle source
# File lib/bytestream-reader.rb, line 188
def read_s32_le
  tmp = read_u32_le
  return (tmp & ~(1 << 31)) - (tmp & (1 << 31))
end
read_s64() click to toggle source
# File lib/bytestream-reader.rb, line 222
def read_s64
  case @endian
  when BigEndian then read_s64_be
  when LittleEndian then read_s64_le
  else raise UndefinedEndianness.new
  end
end
read_s64_be() click to toggle source
# File lib/bytestream-reader.rb, line 193
def read_s64_be
  tmp = read_u64_be
  return (tmp & ~(1 << 63)) - (tmp & (1 << 63))
end
read_s64_le() click to toggle source
# File lib/bytestream-reader.rb, line 198
def read_s64_le
  tmp = read_u64_le
  return (tmp & ~(1 << 63)) - (tmp & (1 << 63))
end
read_s8() click to toggle source
# File lib/bytestream-reader.rb, line 168
def read_s8
  tmp = read_u8
  return (tmp & ~(1 << 7)) - (tmp & (1 << 7))
end
read_u16() click to toggle source
# File lib/bytestream-reader.rb, line 230
def read_u16
  case @endian
  when BigEndian then read_u16_be
  when LittleEndian then read_u16_le
  else raise UndefinedEndianness.new
  end
end
read_u16_be() click to toggle source
# File lib/bytestream-reader.rb, line 104
def read_u16_be
  read_array_u16_be(1)[0]
end
read_u16_le() click to toggle source
# File lib/bytestream-reader.rb, line 108
def read_u16_le
  read_array_u16_le(1)[0]
end
read_u32() click to toggle source
# File lib/bytestream-reader.rb, line 238
def read_u32
  case @endian
  when BigEndian then read_u32_be
  when LittleEndian then read_u32_le
  else raise UndefinedEndianness.new
  end
end
read_u32_be() click to toggle source
# File lib/bytestream-reader.rb, line 112
def read_u32_be
  read_array_u32_be(1)[0]
end
read_u32_le() click to toggle source
# File lib/bytestream-reader.rb, line 116
def read_u32_le
  read_array_u32_le(1)[0]
end
read_u64() click to toggle source
# File lib/bytestream-reader.rb, line 246
def read_u64
  case @endian
  when BigEndian then read_u64_be
  when LittleEndian then read_u64_le
  else raise UndefinedEndianness.new
  end
end
read_u64_be() click to toggle source
# File lib/bytestream-reader.rb, line 120
def read_u64_be
  # As there is no direct unpack method for 64-bit words, the one-value
  # function is considered a special case.
  buf = readexactly(8).unpack("N*")
  return buf[0] << 32 | buf[1]
end
read_u64_le() click to toggle source
# File lib/bytestream-reader.rb, line 127
def read_u64_le
  # As there is no direct unpack method for 64-bit words, the one-value
  # function is considered a special case.
  buf = readexactly(8).unpack("V*")
  return buf[1] << 32 | buf[0]
end
read_u8() click to toggle source
# File lib/bytestream-reader.rb, line 100
def read_u8
  read_array_u8(1)[0]
end
readexactly(length) click to toggle source
# File lib/bytestream-reader.rb, line 43
def readexactly(length)
  ret = readpartial(length)

  # Not strictly correct on Ruby 1.8 but we don't care since we
  # only use this piece of compatibility code on 1.9.
  raise EOFError if ret.size != length

  return ret
end
set_endian(endian) click to toggle source
# File lib/bytestream-reader.rb, line 254
def set_endian(endian)
  case endian
  when BigEndian, LittleEndian
    @endian = endian
  else
    raise ArgumentError.new
  end
end