]> matita.cs.unibo.it Git - helm.git/blob - helm/matita/matitaMoo.ml
- added integrity checks on .moo files
[helm.git] / helm / matita / matitaMoo.ml
1 (* Copyright (C) 2005, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 exception Checksum_failure of string
27 exception Corrupt_moo of string
28 exception Version_mismatch of string
29
30 let marshal_flags = []
31
32 (** .moo file format
33  * - an integer -- magic number -- denoting the version of the dumped data
34  *   structure. Different magic numbers stand for incompatible data structures
35  * - an integer -- checksum -- denoting the hash value (computed with
36  *   Hashtbl.hash) of the string representation of the dumped data structur
37  * - marshalled list of GrafiteAst.command
38  *)
39
40 let save_moo ~fname moo =
41  let oc = open_out fname in
42  let marshalled_moo = Marshal.to_string (List.rev moo) marshal_flags in
43  let checksum = Hashtbl.hash marshalled_moo in
44  output_binary_int oc GrafiteAst.magic;
45  output_binary_int oc checksum;
46  output_string oc marshalled_moo;
47  close_out oc
48
49 let load_moo ~fname =
50   let ic = open_in fname in
51   HExtlib.finally
52     (fun () -> close_in ic)
53     (fun () ->
54       try
55         let moo_magic = input_binary_int ic in
56         if moo_magic <> GrafiteAst.magic then raise (Version_mismatch fname);
57         let moo_checksum = input_binary_int ic in
58         let marshalled_moo = HExtlib.input_all ic in
59         let checksum = Hashtbl.hash marshalled_moo in
60         if checksum <> moo_checksum then raise (Checksum_failure fname);
61         let (moo: MatitaTypes.ast_command list) =
62           Marshal.from_string marshalled_moo 0
63         in
64         moo
65       with End_of_file -> raise (Corrupt_moo fname))
66     ()
67