zip recognize

This commit is contained in:
nganhkhoa 2024-08-18 09:10:24 +07:00
parent 6328b43957
commit a405a6b303
5 changed files with 51 additions and 3 deletions

View File

@ -3,8 +3,25 @@
detect a given file for formats or ISA detect a given file for formats or ISA
*) *)
open Firmex
open Printf open Printf
let runner isa file = let try_unarchive (archive : Archive.t) : unit =
printf "Detect file isa=%b %s\n" isa file printf "archive file=%s type=%s" archive.file archive.typ
let try_detect file =
printf "detect file %s" file
let runner isa file =
if isa then
()
else
(* assume file exists *)
(* check if file is an archive, else try analyze as a firmware
for now, let's use binwalk result
in the future, finding for signatures inside the file
*)
match Archive.from file with
| Some archive -> try_unarchive archive
| None -> try_detect file

25
lib/archive/archive.ml Normal file
View File

@ -0,0 +1,25 @@
(** Base archive utitlities *)
module Zip = Zip
module Xz = Xz
module Cpio = Cpio
type t = { file : string; typ : string }
let is_signature infile signature =
(* assume infile pos is 0 *)
let accumulator acc s =
match In_channel.input_char infile with
| None -> false
| Some c -> s == c && acc
in
Bytes.fold_left accumulator true signature
(** matching common archive signatures *)
let from file =
let infile = In_channel.open_bin file in
(* support for zip file for now *)
if is_signature infile Zip.signature then
Some { file = file; typ = "zip"; }
else
None

1
lib/archive/cpio.ml Normal file
View File

@ -0,0 +1 @@
let signature = Bytes.of_string "\x30\x37\x30\x37\x30\x37"

2
lib/archive/xz.ml Normal file
View File

@ -0,0 +1,2 @@
(* this signature is the stream header signature *)
let signature = Bytes.of_string "\xFD\x37\x7A\x58\x5A\x00"

View File

@ -10,5 +10,8 @@
which has been deprecated or AES which has been deprecated or AES
*) *)
let signature = Bytes.of_string "PK\x00\x00" type t = { file : string; typ : string }
let signature = Bytes.of_string "PK\x03\x04"
let from file = { file = file; typ = "zip" }