Module: BMFF::BinaryAccessor

Defined in:
lib/bmff/binary_accessor.rb

Overview

vim: set expandtab tabstop=2 shiftwidth=2 softtabstop=2 autoindent:

Constant Summary collapse

BYTE_ORDER =
"\xFE\xFF".unpack("s").first == 0xFEFF ? :be : :le
STRING_ENCODINGS =

UTF-8, Shift_JIS or ASCII-8BIT (fallback)

%w(UTF-8 Shift_JIS ASCII-8BIT)

Instance Method Summary collapse

Instance Method Details

#get_ascii(size) ⇒ Object



97
98
99
# File 'lib/bmff/binary_accessor.rb', line 97

def get_ascii(size)
  _read(size).unpack("a*").first
end

#get_byte(size = 1) ⇒ Object



106
107
108
# File 'lib/bmff/binary_accessor.rb', line 106

def get_byte(size = 1)
  _read(size)
end

#get_int16Object



28
29
30
# File 'lib/bmff/binary_accessor.rb', line 28

def get_int16
  flip_byte_if_needed(_read(2)).unpack("s").first
end

#get_int32Object



56
57
58
# File 'lib/bmff/binary_accessor.rb', line 56

def get_int32
  flip_byte_if_needed(_read(4)).unpack("l").first
end

#get_int64Object



74
75
76
77
78
# File 'lib/bmff/binary_accessor.rb', line 74

def get_int64
  b1 = flip_byte_if_needed(_read(4)).unpack("l").first
  b2 = _read(4).unpack("N").first
  (b1 << 32) | b2
end

#get_int8Object



10
11
12
# File 'lib/bmff/binary_accessor.rb', line 10

def get_int8
  _read(1).unpack("c").first
end

#get_iso639_2_languageObject

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.



145
146
147
148
149
150
151
# File 'lib/bmff/binary_accessor.rb', line 145

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

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)



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/bmff/binary_accessor.rb', line 119

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_uint16Object



37
38
39
# File 'lib/bmff/binary_accessor.rb', line 37

def get_uint16
  _read(2).unpack("n").first
end

#get_uint24Object



46
47
48
# File 'lib/bmff/binary_accessor.rb', line 46

def get_uint24
  (get_uint8 << 16) | get_uint16
end

#get_uint32Object



65
66
67
# File 'lib/bmff/binary_accessor.rb', line 65

def get_uint32
  _read(4).unpack("N").first
end

#get_uint64Object



87
88
89
90
# File 'lib/bmff/binary_accessor.rb', line 87

def get_uint64
  b1, b2 = _read(8).unpack("N2")
  (b1 << 32) | b2
end

#get_uint8Object



19
20
21
# File 'lib/bmff/binary_accessor.rb', line 19

def get_uint8
  _read(1).unpack("C").first
end

#get_uuidObject



153
154
155
156
# File 'lib/bmff/binary_accessor.rb', line 153

def get_uuid
  # TODO: create and return UUID type.
  _read(16)
end

#write_ascii(ascii) ⇒ Object

Raises:

  • (TypeError)


101
102
103
104
# File 'lib/bmff/binary_accessor.rb', line 101

def write_ascii(ascii)
  raise TypeError unless ascii.kind_of?(String)
  write([ascii].pack("a*"))
end

#write_byte(byte) ⇒ Object

Raises:

  • (TypeError)


110
111
112
113
# File 'lib/bmff/binary_accessor.rb', line 110

def write_byte(byte)
  raise TypeError unless byte.kind_of?(String)
  write(byte)
end

#write_int16(num) ⇒ Object



32
33
34
35
# File 'lib/bmff/binary_accessor.rb', line 32

def write_int16(num)
  expected_int(num, -32768, 32767)
  write(flip_byte_if_needed([num].pack("s")))
end

#write_int32(num) ⇒ Object



60
61
62
63
# File 'lib/bmff/binary_accessor.rb', line 60

def write_int32(num)
  expected_int(num, -2147483648, 2147483647)
  write(flip_byte_if_needed([num].pack("l")))
end

#write_int64(num) ⇒ Object



80
81
82
83
84
85
# File 'lib/bmff/binary_accessor.rb', line 80

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



14
15
16
17
# File 'lib/bmff/binary_accessor.rb', line 14

def write_int8(num)
  expected_int(num, -128, 127)
  write([num].pack("c"))
end

#write_null_terminated_string(str) ⇒ Object

Raises:

  • (TypeError)


136
137
138
139
140
# File 'lib/bmff/binary_accessor.rb', line 136

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



41
42
43
44
# File 'lib/bmff/binary_accessor.rb', line 41

def write_uint16(num)
  expected_int(num, 0, 65535)
  write([num].pack("n"))
end

#write_uint24(num) ⇒ Object



50
51
52
53
54
# File 'lib/bmff/binary_accessor.rb', line 50

def write_uint24(num)
  expected_int(num, 0, 16777215)
  write_uint8(num >> 16)
  write_uint16(num & 0xFFFF)
end

#write_uint32(num) ⇒ Object



69
70
71
72
# File 'lib/bmff/binary_accessor.rb', line 69

def write_uint32(num)
  expected_int(num, 0, 4294967295)
  write([num].pack("N"))
end

#write_uint64(num) ⇒ Object



92
93
94
95
# File 'lib/bmff/binary_accessor.rb', line 92

def write_uint64(num)
  expected_int(num, 0, 18446744073709551615)
  write([num >> 32, num & 0xFFFFFFFF].pack("N2"))
end

#write_uint8(num) ⇒ Object



23
24
25
26
# File 'lib/bmff/binary_accessor.rb', line 23

def write_uint8(num)
  expected_int(num, 0, 255)
  write([num].pack("C"))
end