lib/parsers/ogg_parser.rb
# Since the magic bits may occur inside the body of the page we have to
# validate that what we found is actually an Ogg page by calculating the
# checksum. For this reason we have to read the entire page and calculate
# its checksum. In order to read the entire Ogg page we first have to read a
# part of its header to find out the size of the page.
Since the magic bits may occur inside the body of the page we have to
validate that what we found is actually an Ogg page by calculating the
checksum. For this reason we have to read the entire page and calculate
its checksum. In order to read the entire Ogg page we first have to read a
part of its header to find out the size of the page.
Since the magic bits may occur inside the body of the page we have to validate that what we found is actually an Ogg page by calculating the checksum . For this reason we have to read the entire page and calculate its checksum . In order to read the entire Ogg page we first have to read a part of its header to find out the size of the page .
def extract_granule_position_from_string_at(string, at)
    header_size = 27
    header_bytes = string.byteslice(at, header_size)
    return unless header_bytes && header_bytes.bytesize == header_size
    _capture_pattern,
    _version,
    _header_type,
    granule_position,
    _bitstream_serial_number,
    _page_sequence_number,
    checksum,
    num_bytes_page_segments = header_bytes.unpack('a4CCQ<VVVC')
     The segment table is a vector of 8-bit values, each indicating the length
     of the corresponding segment within the page body.
     If there are no segments in the segment table the page is certainly invalid
    return if num_bytes_page_segments == 0
     Read the segment table
    segment_table_pos = at + header_size
    segment_table = string.byteslice(segment_table_pos, num_bytes_page_segments)
    return unless segment_table && segment_table.bytesize == num_bytes_page_segments
     Calculate the size of the Ogg page
    num_bytes_used_for_segments = segment_table.unpack('C*').inject(&:+)
    page_size = header_size + num_bytes_page_segments + num_bytes_used_for_segments
     Read the entire page now that we know how much we have to read
    entire_page = string.byteslice(at, page_size)
    return unless entire_page && entire_page.bytesize == page_size
     Compute and check the checksum. If this check fails it means one of the two:
       - the data is corrupted
       - the "OggS" capture pattern occures inside the body of the page and is
         we were scanning a random piece of content which was not an Ogg page
    return unless checksum == calculate_checksum(entire_page)
     ...and only having gone through all these motions - return the granule position.
    granule_position
  end
def extract_granule_position_from_string_at ( string , at ) header_size = 27 header_bytes = string . byteslice ( at , header_size ) return unless header_bytes && header_bytes . bytesize == header_size _capture_pattern , _version , _header_type , granule_position , _bitstream_serial_number , _page_sequence_number , checksum , num_bytes_page_segments = header_bytes . unpack ( 'a4CCQ<VVVC' ) return if num_bytes_page_segments == 0 segment_table_pos = at + header_size segment_table = string . byteslice ( segment_table_pos , num_bytes_page_segments ) return unless segment_table && segment_table . bytesize == num_bytes_page_segments num_bytes_used_for_segments = segment_table . unpack ( 'C*' ) . inject ( & :+ ) page_size = header_size + num_bytes_page_segments + num_bytes_used_for_segments entire_page = string . byteslice ( at , page_size ) return unless entire_page && entire_page . bytesize == page_size return unless checksum == calculate_checksum ( entire_page ) granule_position end
Since the magic bits may occur inside the body of the page we have to
validate that what we found is actually an Ogg page by calculating the
checksum.
Since the magic bits may occur inside the body of the page we have to validate that what we found is actually an Ogg page by calculating the checksum .
# Read the Ogg page header excluding the segment table (in other words read # first 27 bytes). See https://en.wikipedia.org/wiki/Ogg#Page_structure # Read the segment table part of the Ogg page header. Its size is stored in page_segments. # # The segment table is a vector of 8-bit values, each indicating the length # of the corresponding segment within the page body. # If there are no segments in the segment table the page is certainly invalid # Read the segment table # Calculate the size of the Ogg page # Read the entire page now that we know how much we have to read # Compute and check the checksum. If this check fails it means one of the two: #   - the data is corrupted #   - the "OggS" capture pattern occures inside the body of the page and is #     we were scanning a random piece of content which was not an Ogg page # ...and only having gone through all these motions - return the granule position.
string
at