I'm reading a bunch of bytes from a binary file. It is a RAR file. I'm interested in the 11th and 12th bytes of the file because the header specifications state:
HEAD_FLAGS Bit flags: 2 bytes
0x0001 - Volume attribute (archive volume) 0x0002 - Archive comment present RAR 3.x uses the separate comment block and does not set this flag. 0x0004 - Archive lock attribute 0x0008 - Solid attribute (solid archive) 0x0010 - New volume naming scheme ('volname.partN.rar') 0x0020 - Authenticity information present RAR 3.x does not set this flag. 0x0040 - Recovery record present 0x0080 - Block headers are encrypted 0x0100 - First volume (set only by RAR 3.0 and later) other bits in HEAD_FLAGS are reserved for internal use
The file that I'm playing with has 00
and 0D
as positions 11 and 12 respectively.
I can't make sense of these two values as they are bit flags (which I have failed to understand).
I have these two values in byte
array that is 12 bytes long. What I need to check in this sequence is whether the flag 0x0100
and 0x0001
is set or not.
I'm lost with this. Thanks.
I've inspected some of the files in a Hex editor and what i've seen is that 11th and 12th bytes need to be read together. That's why the specs list all the bit flags are 4 letter hex codes. Checking the bit flags individually yields incorrect results.
Assimilating as much information from the answers/tips, 've solved this in the following way:
FileInputStream fisFileInputStream = new FileInputStream((new File("C:\\testarchive.r00"));
byte[] bytHeader = new byte[20]; //The first 20 bytes are the RAR header.
fisFileInputStream.read(bytHeader);
short val=(short)( ((bytHeader[10]&0xFF)<<8) | (bytHeader[11]&0xFF) ); //Joining the two bytes into a short
System.out.println("Volume Attribute (0x0001): " + ((val & 0x0001) != 0));
System.out.println("First volume (0x0100): " + ((val & 0x0100) != 0));
I've tried this code with multiple RAR archives — spanned ones, non-spanned ones, the first file of a spanned archive, another file of a spanned archive.
The code itself works fine except for a very minor quirk. I get the opposite results from my hex values i.e.
When inspecting a file which is not the first file in a spanned archive, I get the volume atrribute 0x0001
as not set (false
), and the "First volume" 0x100
as set (true
).
When inspecting a file which is the first file in a spanned archive, I get the exact opposite results.
Now I modify my code to believe that the original specs are wrong (highly unlikely) and that 0x0001
means that is is the first file in a spanned archive and 0x0100
means that is a spanned archive, then it's all okay.
..but I guess I'm doing something wrong here with my bit flag logic. Any ideas?