Module: Id3Taginator::Util::SyncUtil

Defined in:
lib/id3taginator/util/sync_util.rb

Class Method Summary collapse

Class Method Details

.add_synchronization(stream) ⇒ String

adds synchronization bytes to the given stream. 1111_1111 111X_XXXX will be changed to 1111_1111 0000_0000 111X_XXXX also 1111_1111 0000_0000 must be changed to 1111_1111 0000_0000 0000_0000

Parameters:

  • stream (StringIO, IO, File)

    the stream to add synchronization

Returns:

  • (String)

    the result byte array represented as a string (str.bytes is the bytes array)



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/id3taginator/util/sync_util.rb', line 13

def self.add_synchronization(stream)
  result = []

  until stream.eof?
    curr = stream.readbyte
    result << curr

    # curr is not FF > nothing to do
    next if curr != 255

    # if this is the end, add 00 for padding
    if stream.eof?
      result << 0
      break
    end

    next_byte = stream.readbyte
    # add 00 if next byte is 111x_xxxx or 00 (FF 00)
    result << 0 if next_byte >= 224 || next_byte.zero?
    stream.seek(-1, IO::SEEK_CUR)
  end

  result.pack('C*')
end

.undo_synchronization(stream) ⇒ String

undo synchroniation, means 1111_1111 0000_0000 111X_XXXX -> 1111_1111 111X_XXXX and 1111_1111 0000_0000 0000_0000 -> 1111_1111 0000_0000

Parameters:

  • stream (StringIO, IO, File)

    the stream to remove synchronization

Returns:

  • (String)

    the result byte array represented as a string (str.bytes is the bytes array)



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/id3taginator/util/sync_util.rb', line 44

def self.undo_synchronization(stream)
  result = []

  until stream.eof?
    curr = stream.readbyte
    result << curr

    # curr is not FF > nothing to do
    next if curr != 255

    # should never happen, since there should be a 00 padding, just in case catch the case if not
    break if stream.eof?

    next_byte = stream.readbyte
    result << next_byte unless next_byte.zero?
  end

  result.pack('C*')
end