How to read hexadecimal values ​​from a binary file and decrypt some bytes containing bitflag values?

I am reading a bunch of bytes from a binary file. This is a rar file. I'm interested in the 11th and 12th bytes of the file, as the header specification states:

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

File in which I play is 00and 0Dhow to position 11 and 12 respectively.

I cannot understand these two values ​​because they are bit flags (which I did not understand).

I have these two values ​​in an array bytewhose length is 12 bytes. What I need to check in this sequence: is the 0x0100and flag set 0x0001.

I have lost it. Thank.


Hex-, , , , 11- 12- . - . .


/, :

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));

RAR- - , , , .

, . , ..

, , atrribute 0x0001 (false), " " 0x100 (true).

, , .

, , ( ) 0x0001 , , 0x0100 , , .

.. , - . ?

+1
3

, , .

, , , . . , , .

, , " ", " " " ". , 0x0004, 0x0010 0x0080. , 0x0004 + 0x0010 + 0x0080 = 0x0094, , . , , , , 2.

, 18 . 2.

, AND (&) . , " ", 0x0010. , , . , , .

if ((flags & 0x0010) != 0)

, . , .

, , , :

short flags = b[0] | b[1]<<8;

( . .)

b [0] b [1], , .

+3
      FileInputStream fis = new FileInputStream("file.rar");
      byte[] ba = new byte[13];
      fis.read(ba);
      byte b11 = ba[11];
      byte b12 = ba[12];
      boolean flagIs0x10 = b12 == 0x10;
      System.out.println("flag is 0x10 = "+flagIs0x10);

0x10, .. 16- ,

      boolean flagIs0x10 = b11 == 0 && b12 == 0x10;

      boolean flagIs0x10 = b12 == 0 && b11 == 0x10;
+2

. java BitSet .

static void setByte(BitSet bitSet, int byteNum, byte b) {
    int base = byteNum * 8;
    bitSet.set(base + 7, (b & 0x80) != 0);
    bitSet.set(base + 6, (b & 0x40) != 0);
    bitSet.set(base + 5, (b & 0x20) != 0);
    bitSet.set(base + 4, (b & 0x10) != 0);
    bitSet.set(base + 3, (b & 0x08) != 0);
    bitSet.set(base + 2, (b & 0x04) != 0);
    bitSet.set(base + 1, (b & 0x02) != 0);
    bitSet.set(base + 0, (b & 0x01) != 0);
}

"byteNum", , , .

0 0-7 1 8-15 .....

7, 0.

, . , (0x0800 - 0x8000), - (0x0001 - 0x0080), .

byte buf[] = new byte[2];
inputStream.read(buf, 0, 2);

BitSet bitSet = new BitSet();
// low order byte is the second one
setByte(bitSet, 0, bytes[1]);
// high order byte is first
setByte(bitSet, 1, byte1[0]);

boolean archiveVolume = bitSet.get(0);
boolean commentPresent = bitSet.get(1);
...
boolean firstVolume = bitSet.get(8);
0
source

Source: https://habr.com/ru/post/1731688/


All Articles