From aa935088811c0b47a3b590d2e1fae36d03afaadf Mon Sep 17 00:00:00 2001 From: Mylloon Date: Tue, 4 Jul 2023 13:12:45 +0200 Subject: [PATCH] fix: decode of the compressed name --- lib/response.ml | 11 ++++++++--- lib/utils.ml | 6 ++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/response.ml b/lib/response.ml index a478551..1cc3e2a 100644 --- a/lib/response.ml +++ b/lib/response.ml @@ -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 })) ;; diff --git a/lib/utils.ml b/lib/utils.ml index 896ded7..bae90de 100644 --- a/lib/utils.ml +++ b/lib/utils.ml @@ -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