X-Git-Url: http://git.annexia.org/?a=blobdiff_plain;f=generator%2Fgenerator.ml;h=de103ed5d24fad04b52f21ccacba151595bf55dc;hb=49b89eec45780eceea5b72ad1a466f9549e788eb;hp=9722312b6423f8a1fad18f4d1f366a036bd967f5;hpb=d0e382c601cbba8bba6df4258f91c3414ee82766;p=hivex.git diff --git a/generator/generator.ml b/generator/generator.ml index 9722312..de103ed 100755 --- a/generator/generator.ml +++ b/generator/generator.ml @@ -1,6 +1,6 @@ #!/usr/bin/env ocaml (* hivex - * Copyright (C) 2009-2010 Red Hat Inc. + * Copyright (C) 2009-2011 Red Hat Inc. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,16 +19,16 @@ (* This script generates language bindings and some documentation for * hivex. - * + * * After editing this file, run it (./generator/generator.ml) to * regenerate all the output files. 'make' will rerun this * automatically when necessary. Note that if you are using a separate * build directory you must run generator.ml from the _source_ * directory. - * + * * IMPORTANT: This script should NOT print any warnings. If it prints * warnings, you should treat them as errors. - * + * * OCaml tips: (1) In emacs, install tuareg-mode to display and format * OCaml code correctly. 'vim' comes with a good OCaml editing mode by * default. (2) Read the resources at http://ocaml-tutorial.org/ @@ -73,7 +73,7 @@ and argt = (* Note, cannot be NULL/0 unless it (* Hive types, from: * https://secure.wikimedia.org/wikipedia/en/wiki/Windows_Registry#Keys_and_values - * + * * It's unfortunate that in our original C binding we strayed away from * the names that Windows uses (eg. REG_SZ for strings). We include * both our names and the Windows names. @@ -158,6 +158,15 @@ but instead are lost. See L."; "\ Return root node of the hive. All valid hives must contain a root node."; + "last_modified", (RInt64, [AHive]), + "return the modification time from the header of the hive", + "\ +Return the modification time from the header of the hive. + +The returned value is a Windows filetime. +To convert this to a Unix C see: +L"; + "node_name", (RString, [AHive; ANode "node"]), "return the name of the node", "\ @@ -170,6 +179,15 @@ only know the \"real\" name of the root node by knowing which registry file this hive originally comes from, which is knowledge that is outside the scope of this library."; + "node_timestamp", (RInt64, [AHive; ANode "node"]), + "return the modification time of the node", + "\ +Return the modification time of the node. + +The returned value is a Windows filetime. +To convert this to a Unix C see: +L"; + "node_children", (RNodeList, [AHive; ANode "node"]), "return children of node", "\ @@ -316,21 +334,6 @@ new key is added. Key matching is case insensitive. C is the node to modify."; ] -(* Used to memoize the result of pod2text. *) -let pod2text_memo_filename = "generator/.pod2text.data" -let pod2text_memo : ((int * string * string), string list) Hashtbl.t = - try - let chan = open_in pod2text_memo_filename in - let v = input_value chan in - close_in chan; - v - with - _ -> Hashtbl.create 13 -let pod2text_memo_updated () = - let chan = open_out pod2text_memo_filename in - output_value chan pod2text_memo; - close_out chan - (* Useful functions. * Note we don't want to use any external OCaml libraries which * makes this a bit harder than it should be. @@ -377,6 +380,64 @@ let trimr ?(test = isspace) str = let trim ?(test = isspace) str = trimr ~test (triml ~test str) +(* Used to memoize the result of pod2text. *) +let pod2text_memo_filename = "generator/.pod2text.data.version.2" +let pod2text_memo : ((int option * bool * bool * string * string), string list) Hashtbl.t = + try + let chan = open_in pod2text_memo_filename in + let v = input_value chan in + close_in chan; + v + with + _ -> Hashtbl.create 13 +let pod2text_memo_updated () = + let chan = open_out pod2text_memo_filename in + output_value chan pod2text_memo; + close_out chan + +(* Useful if you need the longdesc POD text as plain text. Returns a + * list of lines. + * + * Because this is very slow (the slowest part of autogeneration), + * we memoize the results. + *) +let pod2text ?width ?(trim = true) ?(discard = true) name longdesc = + let key = width, trim, discard, name, longdesc in + try Hashtbl.find pod2text_memo key + with Not_found -> + let filename, chan = Filename.open_temp_file "gen" ".tmp" in + fprintf chan "=head1 %s\n\n%s\n" name longdesc; + close_out chan; + let cmd = + match width with + | Some width -> + sprintf "pod2text -w %d %s" width (Filename.quote filename) + | None -> + sprintf "pod2text %s" (Filename.quote filename) in + let chan = open_process_in cmd in + let lines = ref [] in + let rec loop i = + let line = input_line chan in + if i = 1 && discard then (* discard the first line of output *) + loop (i+1) + else ( + let line = if trim then triml line else line in + lines := line :: !lines; + loop (i+1) + ) in + let lines = try loop 1 with End_of_file -> List.rev !lines in + unlink filename; + (match close_process_in chan with + | WEXITED 0 -> () + | WEXITED i -> + failwithf "pod2text: process exited with non-zero status (%d)" i + | WSIGNALED i | WSTOPPED i -> + failwithf "pod2text: process signalled or stopped by signal %d" i + ); + Hashtbl.add pod2text_memo key lines; + pod2text_memo_updated (); + lines + let rec find s sub = let len = String.length s in let sublen = String.length sub in @@ -686,6 +747,7 @@ let rec generate_c_header () = #ifndef HIVEX_H_ #define HIVEX_H_ +#include #include #ifdef __cplusplus @@ -701,6 +763,13 @@ typedef struct hive_h hive_h; typedef size_t hive_node_h; typedef size_t hive_value_h; +#include +#ifdef ENOKEY +# define HIVEX_NO_KEY ENOKEY +#else +# define HIVEX_NO_KEY ENOENT +#endif + /* Pre-defined types. */ enum hive_type { "; @@ -834,7 +903,7 @@ hivex - Windows Registry \"hive\" extraction library =head1 SYNOPSIS #include - + "; List.iter ( fun (shortname, style, _, _) -> @@ -868,7 +937,7 @@ If you just want to export or modify the Registry of a Windows virtual machine, you should look at L. Hivex is also comes with language bindings for -OCaml, Perl and Python. +OCaml, Perl, Python and Ruby. =head1 TYPES @@ -1273,7 +1342,7 @@ exhaustive): Corrupt or unsupported Registry file format. -=item ENOKEY +=item HIVEX_NO_KEY Missing root key. @@ -1764,7 +1833,7 @@ static void raise_closed (const char *) Noreturn; pr " rv = copy_type_value (r, len, t);\n"; pr " free (r);\n" | RInt32 -> pr " rv = caml_copy_int32 (r);\n" - | RInt64 -> pr " rv = caml_copy_int32 (r);\n" + | RInt64 -> pr " rv = caml_copy_int64 (r);\n" ); pr " CAMLreturn (rv);\n"; @@ -2181,10 +2250,7 @@ and generate_perl_xs () = #include #include - -#ifndef PRId64 -#define PRId64 \"lld\" -#endif +#include static SV * my_newSVll(long long val) { @@ -2198,10 +2264,6 @@ my_newSVll(long long val) { #endif } -#ifndef PRIu64 -#define PRIu64 \"llu\" -#endif - #if 0 static SV * my_newSVull(unsigned long long val) { @@ -3020,7 +3082,7 @@ initlibhivexmod (void) initialized = 1; } " - + and generate_python_py () = generate_header HashStyle LGPLv2plus; @@ -3097,6 +3159,354 @@ class Hivex: ) ) functions +and generate_ruby_c () = + generate_header CStyle LGPLv2plus; + + pr "\ +#include +#include +#include + +#include + +#include \"hivex.h\" + +#include \"extconf.h\" + +/* For Ruby < 1.9 */ +#ifndef RARRAY_LEN +#define RARRAY_LEN(r) (RARRAY((r))->len) +#endif + +static VALUE m_hivex; /* hivex module */ +static VALUE c_hivex; /* hive_h handle */ +static VALUE e_Error; /* used for all errors */ + +static void +ruby_hivex_free (void *hvp) +{ + hive_h *h = hvp; + + if (h) + hivex_close (h); +} + +static void +get_value (VALUE valv, hive_set_value *val) +{ + VALUE key = rb_hash_lookup (valv, ID2SYM (rb_intern (\"key\"))); + VALUE type = rb_hash_lookup (valv, ID2SYM (rb_intern (\"type\"))); + VALUE value = rb_hash_lookup (valv, ID2SYM (rb_intern (\"value\"))); + + val->key = StringValueCStr (key); + val->t = NUM2ULL (type); + val->len = RSTRING (value)->len; + val->value = RSTRING (value)->ptr; +} + +static hive_set_value * +get_values (VALUE valuesv, size_t *nr_values) +{ + size_t i; + hive_set_value *ret; + + *nr_values = RARRAY_LEN (valuesv); + ret = malloc (sizeof (*ret) * *nr_values); + if (ret == NULL) + abort (); + + for (i = 0; i < *nr_values; ++i) { + VALUE v = rb_ary_entry (valuesv, i); + get_value (v, &ret[i]); + } + + return ret; +} + +"; + + List.iter ( + fun (name, (ret, args), shortdesc, longdesc) -> + let () = + (* Generate rdoc. *) + let doc = replace_str longdesc "C "h." ^ name, args + | args -> "Hivex::" ^ name, args in + let args = filter_map ( + function + | AUnusedFlags -> None + | args -> Some (name_of_argt args) + ) args in + let args = String.concat ", " args in + + let ret = + match ret with + | RErr | RErrDispose -> "nil" + | RHive -> "Hivex::Hivex" + | RNode | RNodeNotFound -> "integer" + | RNodeList -> "list" + | RValue -> "integer" + | RValueList -> "list" + | RString -> "string" + | RStringList -> "list" + | RLenType -> "hash" + | RLenTypeVal -> "hash" + | RInt32 -> "integer" + | RInt64 -> "integer" in + + pr "\ +/* + * call-seq: + * %s(%s) -> %s + * + * %s + * + * %s + * + * (For the C API documentation for this function, see + * +hivex_%s+[http://libguestfs.org/hivex.3.html#hivex_%s]). + */ +" call args ret shortdesc doc name name in + + (* Generate the function. *) + pr "static VALUE\n"; + pr "ruby_hivex_%s (" name; + + let () = + (* If the first argument is not AHive, then this is a module-level + * function, and Ruby passes an implicit module argument which we + * must ignore. Otherwise the first argument is the hive handle. + *) + let args = + match args with + | AHive :: args -> pr "VALUE hv"; args + | args -> pr "VALUE modulev"; args in + List.iter ( + function + | AUnusedFlags -> () + | arg -> + pr ", VALUE %sv" (name_of_argt arg) + ) args; + pr ")\n" in + + pr "{\n"; + + List.iter ( + function + | AHive -> + pr " hive_h *h;\n"; + pr " Data_Get_Struct (hv, hive_h, h);\n"; + pr " if (!h)\n"; + pr " rb_raise (rb_eArgError, \"%%s: used handle after closing it\",\n"; + pr " \"%s\");\n" name; + | ANode n -> + pr " hive_node_h %s = NUM2ULL (%sv);\n" n n + | AValue n -> + pr " hive_value_h %s = NUM2ULL (%sv);\n" n n + | AString n -> + pr " const char *%s = StringValueCStr (%sv);\n" n n; + | AStringNullable n -> + pr " const char *%s =\n" n; + pr " !NIL_P (%sv) ? StringValueCStr (%sv) : NULL;\n" n n + | AOpenFlags -> + pr " int flags = 0;\n"; + List.iter ( + fun (n, flag, _) -> + pr " if (RTEST (rb_hash_lookup (flagsv, ID2SYM (rb_intern (\"%s\")))))\n" + (String.lowercase flag); + pr " flags += %d;\n" n + ) open_flags + | AUnusedFlags -> () + | ASetValues -> + pr " size_t nr_values;\n"; + pr " hive_set_value *values;\n"; + pr " values = get_values (valuesv, &nr_values);\n" + | ASetValue -> + pr " hive_set_value val;\n"; + pr " get_value (valv, &val);\n" + ) args; + pr "\n"; + + let error_code = + match ret with + | RErr -> pr " int r;\n"; "-1" + | RErrDispose -> pr " int r;\n"; "-1" + | RHive -> pr " hive_h *r;\n"; "NULL" + | RNode -> pr " hive_node_h r;\n"; "0" + | RNodeNotFound -> + pr " errno = 0;\n"; + pr " hive_node_h r;\n"; + "0 && errno != 0" + | RNodeList -> pr " hive_node_h *r;\n"; "NULL" + | RValue -> pr " hive_value_h r;\n"; "0" + | RValueList -> pr " hive_value_h *r;\n"; "NULL" + | RString -> pr " char *r;\n"; "NULL" + | RStringList -> pr " char **r;\n"; "NULL" + | RLenType -> + pr " int r;\n"; + pr " size_t len;\n"; + pr " hive_type t;\n"; + "-1" + | RLenTypeVal -> + pr " char *r;\n"; + pr " size_t len;\n"; + pr " hive_type t;\n"; + "NULL" + | RInt32 -> + pr " errno = 0;\n"; + pr " int32_t r;\n"; + "-1 && errno != 0" + | RInt64 -> + pr " errno = 0;\n"; + pr " int64_t r;\n"; + "-1 && errno != 0" in + pr "\n"; + + let c_params = + List.map (function + | ASetValues -> ["nr_values"; "values"] + | ASetValue -> ["&val"] + | AUnusedFlags -> ["0"] + | arg -> [name_of_argt arg]) args in + let c_params = + match ret with + | RLenType | RLenTypeVal -> c_params @ [["&t"; "&len"]] + | _ -> c_params in + let c_params = List.concat c_params in + + pr " r = hivex_%s (%s" name (List.hd c_params); + List.iter (pr ", %s") (List.tl c_params); + pr ");\n"; + pr "\n"; + + (* Dispose of the hive handle (even if hivex_close returns error). *) + (match ret with + | RErrDispose -> + pr " /* So we don't double-free in the finalizer. */\n"; + pr " DATA_PTR (hv) = NULL;\n"; + pr "\n"; + | _ -> () + ); + + List.iter ( + function + | AHive + | ANode _ + | AValue _ + | AString _ + | AStringNullable _ + | AOpenFlags + | AUnusedFlags -> () + | ASetValues -> + pr " free (values);\n" + | ASetValue -> () + ) args; + + (* Check for errors from C library. *) + pr " if (r == %s)\n" error_code; + pr " rb_raise (e_Error, \"%%s\", strerror (errno));\n"; + pr "\n"; + + (match ret with + | RErr | RErrDispose -> + pr " return Qnil;\n" + | RHive -> + pr " return Data_Wrap_Struct (c_hivex, NULL, ruby_hivex_free, r);\n" + | RNode + | RValue + | RInt64 -> + pr " return ULL2NUM (r);\n" + | RInt32 -> + pr " return INT2NUM (r);\n" + | RNodeNotFound -> + pr " if (r)\n"; + pr " return ULL2NUM (r);\n"; + pr " else\n"; + pr " return Qnil;\n" + | RNodeList + | RValueList -> + pr " size_t i, len = 0;\n"; + pr " for (i = 0; r[i] != 0; ++i) len++;\n"; + pr " VALUE rv = rb_ary_new2 (len);\n"; + pr " for (i = 0; r[i] != 0; ++i)\n"; + pr " rb_ary_push (rv, ULL2NUM (r[i]));\n"; + pr " free (r);\n"; + pr " return rv;\n" + | RString -> + pr " VALUE rv = rb_str_new2 (r);\n"; + pr " free (r);\n"; + pr " return rv;\n" + | RStringList -> + pr " size_t i, len = 0;\n"; + pr " for (i = 0; r[i] != NULL; ++i) len++;\n"; + pr " VALUE rv = rb_ary_new2 (len);\n"; + pr " for (i = 0; r[i] != NULL; ++i) {\n"; + pr " rb_ary_push (rv, rb_str_new2 (r[i]));\n"; + pr " free (r[i]);\n"; + pr " }\n"; + pr " free (r);\n"; + pr " return rv;\n" + | RLenType -> + pr " VALUE rv = rb_hash_new ();\n"; + pr " rb_hash_aset (rv, ID2SYM (rb_intern (\"len\")), INT2NUM (len));\n"; + pr " rb_hash_aset (rv, ID2SYM (rb_intern (\"type\")), INT2NUM (t));\n"; + pr " return rv;\n" + | RLenTypeVal -> + pr " VALUE rv = rb_hash_new ();\n"; + pr " rb_hash_aset (rv, ID2SYM (rb_intern (\"len\")), INT2NUM (len));\n"; + pr " rb_hash_aset (rv, ID2SYM (rb_intern (\"type\")), INT2NUM (t));\n"; + pr " rb_hash_aset (rv, ID2SYM (rb_intern (\"value\")), rb_str_new (r, len));\n"; + pr " free (r);\n"; + pr " return rv;\n" + ); + + pr "}\n"; + pr "\n" + ) functions; + + pr "\ +/* Initialize the module. */ +void Init__hivex () +{ + m_hivex = rb_define_module (\"Hivex\"); + c_hivex = rb_define_class_under (m_hivex, \"Hivex\", rb_cObject); + e_Error = rb_define_class_under (m_hivex, \"Error\", rb_eStandardError); + + /* XXX How to pass arguments? */ +#if 0 +#ifdef HAVE_RB_DEFINE_ALLOC_FUNC + rb_define_alloc_func (c_hivex, ruby_hivex_open); +#endif +#endif + +"; + + (* Methods. *) + List.iter ( + fun (name, (_, args), _, _) -> + let args = List.filter ( + function + | AUnusedFlags -> false + | _ -> true + ) args in + let nr_args = List.length args in + match args with + | AHive :: _ -> + pr " rb_define_method (c_hivex, \"%s\",\n" name; + pr " ruby_hivex_%s, %d);\n" name (nr_args-1) + | args -> (* class function *) + pr " rb_define_module_function (m_hivex, \"%s\",\n" name; + pr " ruby_hivex_%s, %d);\n" name nr_args + ) functions; + + pr "}\n" + let output_to filename k = let filename_new = filename ^ ".new" in chan := open_out filename_new; @@ -3164,6 +3574,8 @@ Run it from the top source directory using the command output_to "python/hivex.py" generate_python_py; output_to "python/hivex-py.c" generate_python_c; + output_to "ruby/ext/hivex/_hivex.c" generate_ruby_c; + (* Always generate this file last, and unconditionally. It's used * by the Makefile to know when we must re-run the generator. *)