parse the question

This commit is contained in:
Mylloon 2023-06-14 16:34:49 +02:00
parent 1e41cd5dde
commit 2fa8f1083b
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
2 changed files with 28 additions and 2 deletions

View file

@ -2,8 +2,32 @@ open Types
open Utils open Utils
let parse_header reader = let parse_header reader =
match List.init 6 (fun offset -> unpack_short_be reader (offset * 2)) with let max_size = 6 in
match List.init max_size (fun offset -> unpack_short_be reader (offset * 2)) with
| [ id; flags; num_questions; num_answers; num_authorities; num_additionals ] -> | [ id; flags; num_questions; num_answers; num_authorities; num_additionals ] ->
{ id; flags; num_questions; num_answers; num_authorities; num_additionals } ( bytes_forward reader (max_size * 2)
, { id; flags; num_questions; num_answers; num_authorities; num_additionals } )
| _ -> failwith "Invalid number of fields"
;;
let decode_name_simple reader =
let rec read_parts parts pos =
let length = int_of_char (Bytes.get reader pos) in
if length = 0
then 1, parts
else (
let part = Bytes.sub_string reader (pos + 1) length in
let last_length, parts' = read_parts (parts @ [ part ]) (pos + 1 + length) in
last_length + length + 1, parts')
in
let offset, parts = read_parts [] 0 in
bytes_forward reader offset, String.concat "." parts
;;
let parse_question reader =
let reader', name_b = decode_name_simple reader in
let name = String.to_bytes name_b in
match List.init 2 (fun offset -> unpack_short_be reader' (offset * 2)) with
| [ type_; class_ ] -> { name; type_; class_ }
| _ -> failwith "Invalid number of fields" | _ -> failwith "Invalid number of fields"
;; ;;

View file

@ -32,3 +32,5 @@ let unpack_short_be data offset =
let lsb = int_of_char (Bytes.get data (offset + 1)) in let lsb = int_of_char (Bytes.get data (offset + 1)) in
(msb lsl 8) + lsb (msb lsl 8) + lsb
;; ;;
let bytes_forward data offset = Bytes.sub data offset (Bytes.length data - offset)