module BMFF::BinaryAccessor
Constants
- BYTE_ORDER
- STRING_ENCODINGS
UTF-8, Shift_JIS or ASCII-8BIT (fallback)
Public Instance Methods
get_ascii(size)
click to toggle source
# File lib/bmff/binary_accessor.rb, line 99 def get_ascii(size) _read(size).unpack("a*").first end
get_byte(size = 1)
click to toggle source
# File lib/bmff/binary_accessor.rb, line 108 def get_byte(size = 1) _read(size) end
get_int16()
click to toggle source
# File lib/bmff/binary_accessor.rb, line 30 def get_int16 flip_byte_if_needed(_read(2)).unpack("s").first end
get_int32()
click to toggle source
# File lib/bmff/binary_accessor.rb, line 58 def get_int32 flip_byte_if_needed(_read(4)).unpack("l").first end
get_int64()
click to toggle source
# File lib/bmff/binary_accessor.rb, line 76 def get_int64 b1 = flip_byte_if_needed(_read(4)).unpack("l").first b2 = _read(4).unpack("N").first (b1 << 32) | b2 end
get_int8()
click to toggle source
# File lib/bmff/binary_accessor.rb, line 12 def get_int8 _read(1).unpack("c").first end
get_iso639_2_language()
click to toggle source
Return ISO 639-2/T code Each character is compressed into 5-bit width. The bit 5 and 6 values are always 1. The bit 7 value is always 0.
# File lib/bmff/binary_accessor.rb, line 147 def get_iso639_2_language lang = get_uint16 c1 = (lang >> 10) & 0x1F | 0x60 c2 = (lang >> 5) & 0x1F | 0x60 c3 = lang & 0x1F | 0x60 sprintf("%c%c%c", c1, c2, c3) end
get_null_terminated_string(max_byte = nil)
click to toggle source
Null-terminated string An encoding of this string is maybe UTF-8. Other encodings are possible. (e.g. Apple Media Handler outputs non UTF-8 string)
# File lib/bmff/binary_accessor.rb, line 121 def get_null_terminated_string(max_byte = nil) buffer = "" read_byte = 0 until eof? b = read(1) read_byte += 1 break if b == "\x00" buffer << b break if max_byte && read_byte >= max_byte end STRING_ENCODINGS.each do |encoding| buffer.force_encoding(encoding) break if buffer.valid_encoding? end buffer end
get_uint16()
click to toggle source
# File lib/bmff/binary_accessor.rb, line 39 def get_uint16 _read(2).unpack("n").first end
get_uint24()
click to toggle source
# File lib/bmff/binary_accessor.rb, line 48 def get_uint24 (get_uint8 << 16) | get_uint16 end
get_uint32()
click to toggle source
# File lib/bmff/binary_accessor.rb, line 67 def get_uint32 _read(4).unpack("N").first end
get_uint64()
click to toggle source
# File lib/bmff/binary_accessor.rb, line 89 def get_uint64 b1, b2 = _read(8).unpack("N2") (b1 << 32) | b2 end
get_uint8()
click to toggle source
# File lib/bmff/binary_accessor.rb, line 21 def get_uint8 _read(1).unpack("C").first end
get_uuid()
click to toggle source
# File lib/bmff/binary_accessor.rb, line 155 def get_uuid UUIDTools::UUID.parse_raw(_read(16)) end
write_ascii(ascii)
click to toggle source
# File lib/bmff/binary_accessor.rb, line 103 def write_ascii(ascii) raise TypeError unless ascii.kind_of?(String) write([ascii].pack("a*")) end
write_byte(byte)
click to toggle source
# File lib/bmff/binary_accessor.rb, line 112 def write_byte(byte) raise TypeError unless byte.kind_of?(String) write(byte) end
write_int16(num)
click to toggle source
# File lib/bmff/binary_accessor.rb, line 34 def write_int16(num) expected_int(num, -32768, 32767) write(flip_byte_if_needed([num].pack("s"))) end
write_int32(num)
click to toggle source
# File lib/bmff/binary_accessor.rb, line 62 def write_int32(num) expected_int(num, -2147483648, 2147483647) write(flip_byte_if_needed([num].pack("l"))) end
write_int64(num)
click to toggle source
# File lib/bmff/binary_accessor.rb, line 82 def write_int64(num) expected_int(num, -9223372036854775808, 9223372036854775807) b1 = flip_byte_if_needed([num >> 32].pack("l")) b2 = [num & 0xFFFFFFFF].pack("N") write(b1 + b2) end
write_int8(num)
click to toggle source
# File lib/bmff/binary_accessor.rb, line 16 def write_int8(num) expected_int(num, -128, 127) write([num].pack("c")) end
write_null_terminated_string(str)
click to toggle source
# File lib/bmff/binary_accessor.rb, line 138 def write_null_terminated_string(str) raise TypeError unless str.kind_of?(String) terminator = str.end_with?("\x00") ? "" : "\x00" write(str + terminator) end
write_uint16(num)
click to toggle source
# File lib/bmff/binary_accessor.rb, line 43 def write_uint16(num) expected_int(num, 0, 65535) write([num].pack("n")) end
write_uint24(num)
click to toggle source
# File lib/bmff/binary_accessor.rb, line 52 def write_uint24(num) expected_int(num, 0, 16777215) write_uint8(num >> 16) write_uint16(num & 0xFFFF) end
write_uint32(num)
click to toggle source
# File lib/bmff/binary_accessor.rb, line 71 def write_uint32(num) expected_int(num, 0, 4294967295) write([num].pack("N")) end
write_uint64(num)
click to toggle source
# File lib/bmff/binary_accessor.rb, line 94 def write_uint64(num) expected_int(num, 0, 18446744073709551615) write([num >> 32, num & 0xFFFFFFFF].pack("N2")) end
write_uint8(num)
click to toggle source
# File lib/bmff/binary_accessor.rb, line 25 def write_uint8(num) expected_int(num, 0, 255) write([num].pack("C")) end
write_uuid(uuid)
click to toggle source
# File lib/bmff/binary_accessor.rb, line 159 def write_uuid(uuid) uuid_to_write = nil case uuid when UUIDTools::UUID uuid_to_write = uuid when String uuid_to_write = UUIDTools::UUID.parse(uuid) else raise TypeError end write(uuid_to_write.raw) end
Private Instance Methods
_read(size)
click to toggle source
# File lib/bmff/binary_accessor.rb, line 180 def _read(size) raise TypeError unless size.kind_of?(Integer) raise RangeError if size <= 0 data = read(size) raise EOFError unless data raise EOFError unless data.bytesize == size data end
expected_int(val, min, max)
click to toggle source
# File lib/bmff/binary_accessor.rb, line 189 def expected_int(val, min, max) if val.kind_of?(Integer) if val >= min && val <= max return true else raise RangeError end else raise TypeError end end
flip_byte_if_needed(data)
click to toggle source
# File lib/bmff/binary_accessor.rb, line 173 def flip_byte_if_needed(data) if BYTE_ORDER == :le return data.force_encoding("ascii-8bit").reverse end return data end