Module: BMFF::BinaryAccessor

Defined in:
lib/bmff/binary_accessor.rb

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



99
100
101
# File 'lib/bmff/binary_accessor.rb', line 99

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

#get_byte(size = 1) ⇒ Object



108
109
110
# File 'lib/bmff/binary_accessor.rb', line 108

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

#get_int16Object



30
31
32
# File 'lib/bmff/binary_accessor.rb', line 30

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

#get_int32Object



58
59
60
# File 'lib/bmff/binary_accessor.rb', line 58

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

#get_int64Object



76
77
78
79
80
# 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_int8Object



12
13
14
# File 'lib/bmff/binary_accessor.rb', line 12

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.



147
148
149
150
151
152
153
# 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) ⇒ 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)



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# 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_uint16Object



39
40
41
# File 'lib/bmff/binary_accessor.rb', line 39

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

#get_uint24Object



48
49
50
# File 'lib/bmff/binary_accessor.rb', line 48

def get_uint24
  (get_uint8 << 16) | get_uint16
end

#get_uint32Object



67
68
69
# File 'lib/bmff/binary_accessor.rb', line 67

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

#get_uint64Object



89
90
91
92
# File 'lib/bmff/binary_accessor.rb', line 89

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

#get_uint8Object



21
22
23
# File 'lib/bmff/binary_accessor.rb', line 21

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

#get_uuidObject



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

def get_uuid
  UUIDTools::UUID.parse_raw(_read(16))
end

#write_ascii(ascii) ⇒ Object

Raises:

  • (TypeError)


103
104
105
106
# 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) ⇒ Object

Raises:

  • (TypeError)


112
113
114
115
# 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) ⇒ Object



34
35
36
37
# 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) ⇒ Object



62
63
64
65
# 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) ⇒ Object



82
83
84
85
86
87
# 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) ⇒ Object



16
17
18
19
# 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) ⇒ Object

Raises:

  • (TypeError)


138
139
140
141
142
# 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) ⇒ Object



43
44
45
46
# 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) ⇒ Object



52
53
54
55
56
# 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) ⇒ Object



71
72
73
74
# 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) ⇒ Object



94
95
96
97
# 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) ⇒ Object



25
26
27
28
# 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) ⇒ Object



159
160
161
162
163
164
165
166
167
168
169
170
# 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