let check_mo_header chn hdr =
let offset_min = Int32.of_int 28
in
let offset_max = Int32.of_int (in_channel_length chn)
in
let range_offset start_bound =
let end_bound =
Int32.add start_bound (
Int32.mul (Int32.pred hdr.number_of_strings) (Int32.of_int 8)
)
in
(offset_min,offset_max),(start_bound,end_bound)
in
let val_in_range (start_bound,end_bound) value =
Int32.compare start_bound value <= 0
&& Int32.compare value end_bound <= 0
in
let check_overlap start_bound1 start_bound2 =
let (_,(_,end_bound1)) =
range_offset start_bound1
in
let (_,(_,end_bound2)) =
range_offset start_bound2
in
val_in_range (start_bound1,end_bound1) start_bound2
|| val_in_range (start_bound1,end_bound1) end_bound2
|| val_in_range (start_bound2,end_bound2) start_bound1
|| val_in_range (start_bound2,end_bound2) end_bound1
in
let check_range_offset start_bound =
let (file,(start_bound,end_bound)) =
range_offset start_bound
in
not (
val_in_range file start_bound
&& val_in_range file end_bound
)
in
if Int32.compare hdr.number_of_strings Int32.zero < 0 then
raise MoInvalidHeaderNegativeStrings
else if check_range_offset hdr.offset_table_strings then
raise (
MoInvalidHeaderTableStringOutOfBound(
fst (range_offset hdr.offset_table_strings),
snd (range_offset hdr.offset_table_strings)
)
)
else if check_range_offset hdr.offset_table_translation then
raise (
MoInvalidHeaderTableTranslationOutOfBound(
fst (range_offset hdr.offset_table_translation),
snd (range_offset hdr.offset_table_translation)
)
)
else if check_overlap hdr.offset_table_translation hdr.offset_table_strings then
raise (
MoInvalidHeaderTableTranslationStringOverlap(
snd (range_offset hdr.offset_table_translation),
snd (range_offset hdr.offset_table_strings)
)
)
else
hdr