Tech Note
Tech Note
00 - Technical information ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ THE ARCHIVE FORMAT DESCRIBED BELOW IS ONLY VALID FOR VERSIONS SINCE 1.50 ========================================================================== RAR archive file format ========================================================================== Archive file consists of variable length blocks. The order of these blocks may vary, but the first block must be a marker block followed by an archive header block. Each block begins with the following fields: HEAD_CRC HEAD_TYPE HEAD_FLAGS HEAD_SIZE ADD_SIZE 2 1 2 2 4 bytes byte bytes bytes bytes CRC of total block or block part Block type Block flags Block size Optional field - added block size
Field ADD_SIZE present only if (HEAD_FLAGS & 0x8000) != 0 Total block size is HEAD_SIZE if (HEAD_FLAGS & 0x8000) == 0 and HEAD_SIZE+ADD_SIZE if the field ADD_SIZE is present - when (HEAD_FLAGS & 0x8000) != 0. In each block the followings bits in HEAD_FLAGS have the same meaning: 0x4000 - if set, older and remove it if clear, the file when the RAR versions will ignore the block when the archive is updated. block is copied to the new archive archive is updated;
0x8000 - if set, ADD_SIZE field is present and the full block size is HEAD_SIZE+ADD_SIZE. Declared block types: HEAD_TYPE=0x72 HEAD_TYPE=0x73 HEAD_TYPE=0x74 HEAD_TYPE=0x75 HEAD_TYPE=0x76 HEAD_TYPE=0x77 HEAD_TYPE=0x78 HEAD_TYPE=0x79 marker block archive header file header comment header old style authenticity information subblock recovery record authenticity information
Comment block is actually used only within other blocks and doesn't exist separately. Archive processing is made in the following manner: 1. 2. 3. 4. Read and check marker block Read archive header Read or skip HEAD_SIZE-sizeof(MAIN_HEAD) bytes If end of archive encountered then terminate archive processing, else read 7 bytes into fields HEAD_CRC, HEAD_TYPE, HEAD_FLAGS, HEAD_SIZE.
5. Check HEAD_TYPE. if HEAD_TYPE==0x74 read file header ( first 7 bytes already read ) read or skip HEAD_SIZE-sizeof(FILE_HEAD) bytes if (HEAD_FLAGS & 0x100) read or skip HIGH_PACK_SIZE*0x100000000+PACK_SIZE bytes else read or skip PACK_SIZE bytes else read corresponding HEAD_TYPE block: read HEAD_SIZE-7 bytes if (HEAD_FLAGS & 0x8000) read ADD_SIZE bytes 6. go to 4. ========================================================================== Block Formats ========================================================================== Marker block ( MARK_HEAD ) HEAD_CRC 2 bytes HEAD_TYPE 1 byte HEAD_FLAGS 2 bytes HEAD_SIZE 2 bytes Always 0x6152 Header type: 0x72 Always 0x1a21 Block size = 0x0007
The marker block is actually considered as a fixed byte sequence: 0x52 0x61 0x72 0x21 0x1a 0x07 0x00
Archive header ( MAIN_HEAD ) HEAD_CRC 2 bytes HEAD_TYPE 1 byte HEAD_FLAGS 2 bytes CRC of fields HEAD_TYPE to RESERVED2 Header type: 0x73 Bit flags: 0x01 0x02 0x04 0x08 0x10 0x20 0x40 0x80 Volume attribute (archive volume) Archive comment present Archive lock attribute Solid attribute (solid archive) New volume naming scheme ('volname.partN.rar') Authenticity information present Recovery record present Block headers are encrypted
other bits in HEAD_FLAGS are reserved for internal use HEAD_SIZE 2 bytes RESERVED1 2 bytes RESERVED2 4 bytes Comment block Archive header total size including archive comments Reserved Reserved
File header (File in archive) HEAD_CRC 2 bytes HEAD_TYPE 1 byte HEAD_FLAGS 2 bytes CRC of fields from HEAD_TYPE to FILEATTR and file name Header type: 0x74 Bit flags: 0x01 0x02 0x04 0x08 0x10 file continued from previous volume file continued in next volume file encrypted with password file comment present information from previous files is used (solid flag) (for RAR 2.0 and later)
bits 7 6 5 (for RAR 2.0 and later) 0 0 0 0 1 1 1 1 0 0 1 1 0 0 1 1 0 1 0 1 0 1 0 1 dictionary size 64 dictionary size 128 dictionary size 256 dictionary size 512 dictionary size 1024 dictionary size 2048 dictionary size 4096 file is directory KB KB KB KB KB KB KB
0x100 - HIGH_PACK_SIZE and HIGH_UNP_SIZE fields are present. These fields are used to archive only very large files (larger than 2Gb), for smaller files these fields are absent. 0x200 - FILE_NAME contains both usual and encoded Unicode name separated by zero. In this case NAME_SIZE field is equal to the length of usual name plus encoded Unicode name plus 1. 0x400 - the header contains additional 8 bytes after the file name, which are required to increase encryption security (so called 'salt').
0x800 - Version flag. It is an old file version, a version number is appended to file name as ';n'. 0x8000 - this bit always is set, so the complete block size is HEAD_SIZE + PACK_SIZE (and plus HIGH_PACK_SIZE, if bit 0x100 is set) HEAD_SIZE 2 bytes PACK_SIZE 4 bytes UNP_SIZE 4 bytes HOST_OS 1 byte File header full size including file name and comments Compressed file size Uncompressed file size Operating system used for archiving 0 - MS DOS 1 - OS/2 2 - Win32 3 - Unix 4 - Mac OS 5 - BeOS File CRC Date and time in standard MS DOS format RAR version needed to extract file Packing method File name size File attributes
FILE_CRC 4 bytes FTIME 4 bytes UNP_VER 1 byte METHOD 1 byte NAME_SIZE 2 bytes ATTR 4 bytes
HIGH_PACK_SIZE High 4 bytes of 64 bit value of compressed file size. 4 bytes Optional value, presents only if bit 0x100 in HEAD_FLAGS is set. HIGH_UNP_SIZE 4 bytes FILE_NAME Comment block High 4 bytes of 64 bit value of uncompressed file size. Optional value, presents only if bit 0x100 in HEAD_FLAGS is set. File name - string of NAME_SIZE bytes size present if (HEAD_FLAGS & 0x08) != 0
Comment block
HEAD_CRC 2 bytes HEAD_TYPE 1 byte HEAD_FLAGS 2 bytes HEAD_SIZE 2 bytes UNP_SIZE 2 bytes UNP_VER 1 byte METHOD 1 byte COMM_CRC 2 bytes COMMENT
CRC of fields from HEAD_TYPE to COMM_CRC Header type: 0x75 Bit flags Comment header size + comment size Uncompressed comment size RAR version needed to extract comment Packing method Comment CRC Comment text
Extra info block HEAD_CRC 2 bytes HEAD_TYPE 1 byte HEAD_FLAGS 2 bytes HEAD_SIZE 2 bytes INFO Subblock An object in the archive (the block or header) can be followed by a subblock. The subblock is dependent upon the main object. Subblock can be erased or moved to a new version of the archive when it is updated. The subblock contains the following fields: HEAD_CRC 2 bytes HEAD_TYPE Block CRC Header type: 0x77 Block CRC Header type: 0x76 Bit flags Total block size Other data
1 byte HEAD_FLAGS 2 bytes Bit flags (HEAD_FLAGS & 0x8000) == 1, because full block size is HEAD_SIZE + DATA_SIZE HEAD_SIZE 2 bytes DATA_SIZE 4 bytes SUB_TYPE 2 bytes RESERVED 1 byte Other fields Total block size Total data size Subblock type Must be 0 Other fields depending on the subblock type
OS/2 extended attributes subblock HEAD_CRC 2 bytes HEAD_TYPE 1 byte HEAD_FLAGS 2 bytes Block CRC Header type: 0x77 Bit flags (HEAD_FLAGS & 0x8000) == 1, because full block size is HEAD_SIZE + DATA_SIZE HEAD_SIZE 2 bytes DATA_SIZE 4 bytes SUB_TYPE 2 bytes RESERVED 1 byte UNP_SIZE 4 bytes UNP_VER 1 byte METHOD 1 byte EA_CRC 4 bytes Total block size Total data size (packed extended attributes size) 0x100 Must be 0 Uncompressed extended attributes size RAR version needed to extract extended attributes Packing method Extended attributes CRC
========================================================================== Application notes ========================================================================== 1. To process an SFX archive you need to skip the SFX module searching for the marker block in the archive. There is no marker block sequence (0x52 0x61 0x72 0x21 0x1a 0x07 0x00) in the SFX module itself. 2. The CRC is calculated using the standard polynomial 0xEDB88320. In case the size of the CRC is less than 4 bytes, only the low order bytes are used. 3. Packing 0x30 0x31 0x32 0x33 0x34 0x35 method encoding: - storing - fastest compression - fast compression - normal compression - good compression - best compression
4. The RAR extraction version number is encoded as 10 * Major version + minor version.