Module: BytestreamReader

Included in:
File, Elf::File
Defined in:
lib/bytestream-reader.rb

Defined Under Namespace

Classes: File, UndefinedEndianness

Constant Summary collapse

BigEndian =
:BigEndian
LittleEndian =
:LittleEndian

Instance Method Summary collapse

Instance Method Details

#read_array_s16_be(size) ⇒ Object



139
140
141
142
# File 'lib/bytestream-reader.rb', line 139

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) ⇒ Object



144
145
146
147
# File 'lib/bytestream-reader.rb', line 144

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) ⇒ Object



149
150
151
152
# File 'lib/bytestream-reader.rb', line 149

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) ⇒ Object



154
155
156
157
# File 'lib/bytestream-reader.rb', line 154

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) ⇒ Object



159
160
161
162
# File 'lib/bytestream-reader.rb', line 159

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) ⇒ Object



164
165
166
167
# File 'lib/bytestream-reader.rb', line 164

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) ⇒ Object



135
136
137
# File 'lib/bytestream-reader.rb', line 135

def read_array_s8(size)
  readexactly(1*size).unpack("c*")
end

#read_array_u16_be(size) ⇒ Object



67
68
69
# File 'lib/bytestream-reader.rb', line 67

def read_array_u16_be(size)
  readexactly(2*size).unpack("n*")
end

#read_array_u16_le(size) ⇒ Object



71
72
73
# File 'lib/bytestream-reader.rb', line 71

def read_array_u16_le(size)
  readexactly(2*size).unpack("v*")
end

#read_array_u32_be(size) ⇒ Object



75
76
77
# File 'lib/bytestream-reader.rb', line 75

def read_array_u32_be(size)
  readexactly(4*size).unpack("N*")
end

#read_array_u32_le(size) ⇒ Object



79
80
81
# File 'lib/bytestream-reader.rb', line 79

def read_array_u32_le(size)
  readexactly(4*size).unpack("V*")
end

#read_array_u64_be(size) ⇒ Object



83
84
85
86
87
88
89
90
# File 'lib/bytestream-reader.rb', line 83

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) ⇒ Object



92
93
94
95
96
97
98
99
# File 'lib/bytestream-reader.rb', line 92

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) ⇒ Object



63
64
65
# File 'lib/bytestream-reader.rb', line 63

def read_array_u8(size)
  readexactly(1*size).unpack("C*")
end

#read_s16Object



207
208
209
210
211
212
213
# File 'lib/bytestream-reader.rb', line 207

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_beObject



174
175
176
177
# File 'lib/bytestream-reader.rb', line 174

def read_s16_be
  tmp = read_u16_be
  return (tmp & ~(1 << 15)) - (tmp & (1 << 15))
end

#read_s16_leObject



179
180
181
182
# File 'lib/bytestream-reader.rb', line 179

def read_s16_le
  tmp = read_u16_le
  return (tmp & ~(1 << 15)) - (tmp & (1 << 15))
end

#read_s32Object



215
216
217
218
219
220
221
# File 'lib/bytestream-reader.rb', line 215

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_beObject



184
185
186
187
# File 'lib/bytestream-reader.rb', line 184

def read_s32_be
  tmp = read_u32_be
  return (tmp & ~(1 << 31)) - (tmp & (1 << 31))
end

#read_s32_leObject



189
190
191
192
# File 'lib/bytestream-reader.rb', line 189

def read_s32_le
  tmp = read_u32_le
  return (tmp & ~(1 << 31)) - (tmp & (1 << 31))
end

#read_s64Object



223
224
225
226
227
228
229
# File 'lib/bytestream-reader.rb', line 223

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_beObject



194
195
196
197
# File 'lib/bytestream-reader.rb', line 194

def read_s64_be
  tmp = read_u64_be
  return (tmp & ~(1 << 63)) - (tmp & (1 << 63))
end

#read_s64_leObject



199
200
201
202
# File 'lib/bytestream-reader.rb', line 199

def read_s64_le
  tmp = read_u64_le
  return (tmp & ~(1 << 63)) - (tmp & (1 << 63))
end

#read_s8Object



169
170
171
172
# File 'lib/bytestream-reader.rb', line 169

def read_s8
  tmp = read_u8
  return (tmp & ~(1 << 7)) - (tmp & (1 << 7))
end

#read_u16Object



231
232
233
234
235
236
237
# File 'lib/bytestream-reader.rb', line 231

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_beObject



105
106
107
# File 'lib/bytestream-reader.rb', line 105

def read_u16_be
  read_array_u16_be(1)[0]
end

#read_u16_leObject



109
110
111
# File 'lib/bytestream-reader.rb', line 109

def read_u16_le
  read_array_u16_le(1)[0]
end

#read_u32Object



239
240
241
242
243
244
245
# File 'lib/bytestream-reader.rb', line 239

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_beObject



113
114
115
# File 'lib/bytestream-reader.rb', line 113

def read_u32_be
  read_array_u32_be(1)[0]
end

#read_u32_leObject



117
118
119
# File 'lib/bytestream-reader.rb', line 117

def read_u32_le
  read_array_u32_le(1)[0]
end

#read_u64Object



247
248
249
250
251
252
253
# File 'lib/bytestream-reader.rb', line 247

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_beObject



121
122
123
124
125
126
# File 'lib/bytestream-reader.rb', line 121

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_leObject



128
129
130
131
132
133
# File 'lib/bytestream-reader.rb', line 128

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_u8Object



101
102
103
# File 'lib/bytestream-reader.rb', line 101

def read_u8
  read_array_u8(1)[0]
end

#readexactly(length) ⇒ Object

Raises:

  • (EOFError)


44
45
46
47
48
49
50
51
52
# File 'lib/bytestream-reader.rb', line 44

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) ⇒ Object



255
256
257
258
259
260
261
262
# File 'lib/bytestream-reader.rb', line 255

def set_endian(endian)
  case endian
  when BigEndian, LittleEndian
    @endian = endian
  else
    raise ArgumentError.new
  end
end