Class: Omnizip::Formats::Rar::Header

Inherits:
Object
  • Object
show all
Includes:
Constants
Defined in:
lib/omnizip/formats/rar/header.rb

Overview

RAR header parser Parses RAR4 and RAR5 archive headers

Constant Summary

Constants included from Constants

Constants::ARCHIVE_AUTH_INFO, Constants::ARCHIVE_COMMENT, Constants::ARCHIVE_ENCRYPTED, Constants::ARCHIVE_FIRST_VOLUME, Constants::ARCHIVE_LOCKED, Constants::ARCHIVE_NEW_NAMING, Constants::ARCHIVE_RECOVERY, Constants::ARCHIVE_SOLID, Constants::ARCHIVE_VOLUME, Constants::BLOCK_ARCHIVE, Constants::BLOCK_COMMENT, Constants::BLOCK_ENDARC, Constants::BLOCK_FILE, Constants::BLOCK_MARKER, Constants::BLOCK_OLD_AUTH, Constants::BLOCK_OLD_EXTRA, Constants::BLOCK_OLD_RECOVERY, Constants::BLOCK_OLD_SUBBLOCK, Constants::BLOCK_SUBBLOCK, Constants::FILE_COMMENT, Constants::FILE_DIRECTORY, Constants::FILE_ENCRYPTED, Constants::FILE_EXT_TIME, Constants::FILE_LARGE, Constants::FILE_SALT, Constants::FILE_SOLID, Constants::FILE_SPLIT_AFTER, Constants::FILE_SPLIT_BEFORE, Constants::FILE_UNICODE, Constants::FILE_VERSION, Constants::METHOD_BEST, Constants::METHOD_FAST, Constants::METHOD_FASTEST, Constants::METHOD_GOOD, Constants::METHOD_NORMAL, Constants::METHOD_STORE, Constants::OS_BEOS, Constants::OS_MACOS, Constants::OS_MSDOS, Constants::OS_OS2, Constants::OS_UNIX, Constants::OS_WIN32, Constants::RAR4_SIGNATURE, Constants::RAR5_FLAG_CHILD_BLOCKS, Constants::RAR5_FLAG_DATA_AREA, Constants::RAR5_FLAG_DATA_INHERITED, Constants::RAR5_FLAG_EXTRA_AREA, Constants::RAR5_FLAG_IS_DIR, Constants::RAR5_FLAG_MULTI_VOLUME, Constants::RAR5_FLAG_UNKNOWN_BLOCKS, Constants::RAR5_HEADER_ENCRYPTION, Constants::RAR5_HEADER_END, Constants::RAR5_HEADER_FILE, Constants::RAR5_HEADER_MAIN, Constants::RAR5_HEADER_SERVICE, Constants::RAR5_SIGNATURE

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHeader

Initialize header



24
25
26
27
28
29
30
31
# File 'lib/omnizip/formats/rar/header.rb', line 24

def initialize
  @version = nil
  @flags = 0
  @is_multi_volume = false
  @is_solid = false
  @is_locked = false
  @comment_present = false
end

Instance Attribute Details

#comment_presentObject (readonly)

Returns the value of attribute comment_present.



11
12
13
# File 'lib/omnizip/formats/rar/header.rb', line 11

def comment_present
  @comment_present
end

#flagsObject (readonly)

Returns the value of attribute flags.



11
12
13
# File 'lib/omnizip/formats/rar/header.rb', line 11

def flags
  @flags
end

#is_lockedObject (readonly)

Returns the value of attribute is_locked.



11
12
13
# File 'lib/omnizip/formats/rar/header.rb', line 11

def is_locked
  @is_locked
end

#is_multi_volumeObject (readonly)

Returns the value of attribute is_multi_volume.



11
12
13
# File 'lib/omnizip/formats/rar/header.rb', line 11

def is_multi_volume
  @is_multi_volume
end

#is_solidObject (readonly)

Returns the value of attribute is_solid.



11
12
13
# File 'lib/omnizip/formats/rar/header.rb', line 11

def is_solid
  @is_solid
end

#versionObject (readonly)

Returns the value of attribute version.



11
12
13
# File 'lib/omnizip/formats/rar/header.rb', line 11

def version
  @version
end

Class Method Details

.read(io) ⇒ Header

Read and parse RAR header

Parameters:

  • io (IO)

    Input stream

Returns:

Raises:

  • (RuntimeError)

    if invalid header



19
20
21
# File 'lib/omnizip/formats/rar/header.rb', line 19

def self.read(io)
  new.tap { |h| h.parse(io) }
end

Instance Method Details

#parse(io) ⇒ Object

Parse header from IO

Parameters:

  • io (IO)

    Input stream

Raises:

  • (RuntimeError)

    if invalid header



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/omnizip/formats/rar/header.rb', line 37

def parse(io)
  # RAR4 signature is 7 bytes, RAR5 is 8 bytes
  # Read 7 bytes first to check for RAR4
  signature = io.read(7)
  return unless signature

  sig_bytes = signature.bytes

  if sig_bytes == RAR4_SIGNATURE
    @version = 4
    parse_rar4_header(io)
  elsif sig_bytes == RAR5_SIGNATURE[0..6]
    # Might be RAR5, read one more byte
    extra_byte = io.read(1)
    if extra_byte && (sig_bytes + extra_byte.bytes) == RAR5_SIGNATURE
      @version = 5
      parse_rar5_header(io)
    else
      raise "Invalid RAR signature: #{(sig_bytes + (extra_byte&.bytes || [])).inspect}"
    end
  else
    raise "Invalid RAR signature: #{sig_bytes.inspect}"
  end
end

#rar4?Boolean

Check if RAR4 format

Returns:

  • (Boolean)

    true if RAR4



79
80
81
# File 'lib/omnizip/formats/rar/header.rb', line 79

def rar4?
  @version == 4
end

#rar5?Boolean

Check if RAR5 format

Returns:

  • (Boolean)

    true if RAR5



72
73
74
# File 'lib/omnizip/formats/rar/header.rb', line 72

def rar5?
  @version == 5
end

#valid?Boolean

Check if header is valid

Returns:

  • (Boolean)

    true if valid



65
66
67
# File 'lib/omnizip/formats/rar/header.rb', line 65

def valid?
  !@version.nil?
end