fix: decode of the compressed name

This commit is contained in:
Mylloon 2023-07-04 13:12:45 +02:00
parent b87c8913fe
commit aa93508881
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 14 additions and 3 deletions

View file

@ -27,7 +27,9 @@ and decode_name reader =
then 1, parts
else if length land 0b1100_0000 <> 0
then (
let decoded_name = decode_compressed_name reader length in
let decoded_name =
decode_compressed_name { reader with pointer = reader.pointer + pos } length
in
2, decoded_name :: parts)
else (
let part = Bytes.sub_string reader.data (reader.pointer + pos + 1) length in
@ -38,9 +40,12 @@ and decode_name reader =
String.to_bytes (String.concat "." parts), offset
and decode_compressed_name reader length =
let pointer =
(length land 0b0011_1111) + int_of_char (Bytes.get reader.data (reader.pointer + 1))
let pointer_bytes =
Bytes.cat
(int_to_bytes (length land 0b0011_1111))
(Bytes.sub reader.data (reader.pointer + 1) 1)
in
let pointer = unpack_short_be pointer_bytes 0 in
String.of_bytes (fst (decode_name { reader with pointer }))
;;

View file

@ -51,6 +51,12 @@ let unpack_short_be data offset =
(msb lsl 8) + lsb
;;
let int_to_bytes n =
let byte_array = Bytes.create 1 in
Bytes.set byte_array 0 (Char.chr (n land 255));
byte_array
;;
let unpack_int_be data offset =
let byte1 = int_of_char (Bytes.get data offset) in
let byte2 = int_of_char (Bytes.get data (offset + 1)) in