[Forensics-changes] [SCM] debian-forensics/reglookup branch, upstream, updated. upstream/0.4.0-1-g82ee5ec

Christophe Monniez christophe.monniez at fccu.be
Tue Aug 12 08:17:54 UTC 2008


The following commit has been merged in the upstream branch:
commit 82ee5ecbb1e62395de94c128263dd1c40ef36529
Author: Christophe Monniez <christophe.monniez at fccu.be>
Date:   Tue Aug 12 10:15:42 2008 +0200

    Adding upstream version 0.9.0.

diff --git a/LICENSE b/LICENSE
index 960fe74..4432540 100644
--- a/LICENSE
+++ b/LICENSE
@@ -1,280 +1,676 @@
+
 		    GNU GENERAL PUBLIC LICENSE
-		       Version 2, June 1991
+		       Version 3, 29 June 2007
 
- Copyright (C) 1989, 1991 Free Software Foundation, Inc.
-                       59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
  Everyone is permitted to copy and distribute verbatim copies
  of this license document, but changing it is not allowed.
 
 			    Preamble
 
-  The licenses for most software are designed to take away your
-freedom to share and change it.  By contrast, the GNU General Public
-License is intended to guarantee your freedom to share and change free
-software--to make sure the software is free for all its users.  This
-General Public License applies to most of the Free Software
-Foundation's software and to any other program whose authors commit to
-using it.  (Some other Free Software Foundation software is covered by
-the GNU Library General Public License instead.)  You can apply it to
+  The GNU General Public License is a free, copyleft license for
+software and other kinds of works.
+
+  The licenses for most software and other practical works are designed
+to take away your freedom to share and change the works.  By contrast,
+the GNU General Public License is intended to guarantee your freedom to
+share and change all versions of a program--to make sure it remains free
+software for all its users.  We, the Free Software Foundation, use the
+GNU General Public License for most of our software; it applies also to
+any other work released this way by its authors.  You can apply it to
 your programs, too.
 
   When we speak of free software, we are referring to freedom, not
 price.  Our General Public Licenses are designed to make sure that you
 have the freedom to distribute copies of free software (and charge for
-this service if you wish), that you receive source code or can get it
-if you want it, that you can change the software or use pieces of it
-in new free programs; and that you know you can do these things.
+them if you wish), that you receive source code or can get it if you
+want it, that you can change the software or use pieces of it in new
+free programs, and that you know you can do these things.
 
-  To protect your rights, we need to make restrictions that forbid
-anyone to deny you these rights or to ask you to surrender the rights.
-These restrictions translate to certain responsibilities for you if you
-distribute copies of the software, or if you modify it.
+  To protect your rights, we need to prevent others from denying you
+these rights or asking you to surrender the rights.  Therefore, you have
+certain responsibilities if you distribute copies of the software, or if
+you modify it: responsibilities to respect the freedom of others.
 
   For example, if you distribute copies of such a program, whether
-gratis or for a fee, you must give the recipients all the rights that
-you have.  You must make sure that they, too, receive or can get the
-source code.  And you must show them these terms so they know their
-rights.
-
-  We protect your rights with two steps: (1) copyright the software, and
-(2) offer you this license which gives you legal permission to copy,
-distribute and/or modify the software.
-
-  Also, for each author's protection and ours, we want to make certain
-that everyone understands that there is no warranty for this free
-software.  If the software is modified by someone else and passed on, we
-want its recipients to know that what they have is not the original, so
-that any problems introduced by others will not reflect on the original
-authors' reputations.
-
-  Finally, any free program is threatened constantly by software
-patents.  We wish to avoid the danger that redistributors of a free
-program will individually obtain patent licenses, in effect making the
-program proprietary.  To prevent this, we have made it clear that any
-patent must be licensed for everyone's free use or not licensed at all.
+gratis or for a fee, you must pass on to the recipients the same
+freedoms that you received.  You must make sure that they, too, receive
+or can get the source code.  And you must show them these terms so they
+know their rights.
+
+  Developers that use the GNU GPL protect your rights with two steps:
+(1) assert copyright on the software, and (2) offer you this License
+giving you legal permission to copy, distribute and/or modify it.
+
+  For the developers' and authors' protection, the GPL clearly explains
+that there is no warranty for this free software.  For both users' and
+authors' sake, the GPL requires that modified versions be marked as
+changed, so that their problems will not be attributed erroneously to
+authors of previous versions.
+
+  Some devices are designed to deny users access to install or run
+modified versions of the software inside them, although the manufacturer
+can do so.  This is fundamentally incompatible with the aim of
+protecting users' freedom to change the software.  The systematic
+pattern of such abuse occurs in the area of products for individuals to
+use, which is precisely where it is most unacceptable.  Therefore, we
+have designed this version of the GPL to prohibit the practice for those
+products.  If such problems arise substantially in other domains, we
+stand ready to extend this provision to those domains in future versions
+of the GPL, as needed to protect the freedom of users.
+
+  Finally, every program is threatened constantly by software patents.
+States should not allow patents to restrict development and use of
+software on general-purpose computers, but in those that do, we wish to
+avoid the special danger that patents applied to a free program could
+make it effectively proprietary.  To prevent this, the GPL assures that
+patents cannot be used to render the program non-free.
 
   The precise terms and conditions for copying, distribution and
 modification follow.
-
-		    GNU GENERAL PUBLIC LICENSE
-   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
-
-  0. This License applies to any program or other work which contains
-a notice placed by the copyright holder saying it may be distributed
-under the terms of this General Public License.  The "Program", below,
-refers to any such program or work, and a "work based on the Program"
-means either the Program or any derivative work under copyright law:
-that is to say, a work containing the Program or a portion of it,
-either verbatim or with modifications and/or translated into another
-language.  (Hereinafter, translation is included without limitation in
-the term "modification".)  Each licensee is addressed as "you".
-
-Activities other than copying, distribution and modification are not
-covered by this License; they are outside its scope.  The act of
-running the Program is not restricted, and the output from the Program
-is covered only if its contents constitute a work based on the
-Program (independent of having been made by running the Program).
-Whether that is true depends on what the Program does.
-
-  1. You may copy and distribute verbatim copies of the Program's
-source code as you receive it, in any medium, provided that you
-conspicuously and appropriately publish on each copy an appropriate
-copyright notice and disclaimer of warranty; keep intact all the
-notices that refer to this License and to the absence of any warranty;
-and give any other recipients of the Program a copy of this License
-along with the Program.
-
-You may charge a fee for the physical act of transferring a copy, and
-you may at your option offer warranty protection in exchange for a fee.
-
-  2. You may modify your copy or copies of the Program or any portion
-of it, thus forming a work based on the Program, and copy and
-distribute such modifications or work under the terms of Section 1
-above, provided that you also meet all of these conditions:
-
-    a) You must cause the modified files to carry prominent notices
-    stating that you changed the files and the date of any change.
-
-    b) You must cause any work that you distribute or publish, that in
-    whole or in part contains or is derived from the Program or any
-    part thereof, to be licensed as a whole at no charge to all third
-    parties under the terms of this License.
-
-    c) If the modified program normally reads commands interactively
-    when run, you must cause it, when started running for such
-    interactive use in the most ordinary way, to print or display an
-    announcement including an appropriate copyright notice and a
-    notice that there is no warranty (or else, saying that you provide
-    a warranty) and that users may redistribute the program under
-    these conditions, and telling the user how to view a copy of this
-    License.  (Exception: if the Program itself is interactive but
-    does not normally print such an announcement, your work based on
-    the Program is not required to print an announcement.)
-
-These requirements apply to the modified work as a whole.  If
-identifiable sections of that work are not derived from the Program,
-and can be reasonably considered independent and separate works in
-themselves, then this License, and its terms, do not apply to those
-sections when you distribute them as separate works.  But when you
-distribute the same sections as part of a whole which is a work based
-on the Program, the distribution of the whole must be on the terms of
-this License, whose permissions for other licensees extend to the
-entire whole, and thus to each and every part regardless of who wrote it.
-
-Thus, it is not the intent of this section to claim rights or contest
-your rights to work written entirely by you; rather, the intent is to
-exercise the right to control the distribution of derivative or
-collective works based on the Program.
-
-In addition, mere aggregation of another work not based on the Program
-with the Program (or with a work based on the Program) on a volume of
-a storage or distribution medium does not bring the other work under
-the scope of this License.
-
-  3. You may copy and distribute the Program (or a work based on it,
-under Section 2) in object code or executable form under the terms of
-Sections 1 and 2 above provided that you also do one of the following:
-
-    a) Accompany it with the complete corresponding machine-readable
-    source code, which must be distributed under the terms of Sections
-    1 and 2 above on a medium customarily used for software interchange; or,
-
-    b) Accompany it with a written offer, valid for at least three
-    years, to give any third party, for a charge no more than your
-    cost of physically performing source distribution, a complete
-    machine-readable copy of the corresponding source code, to be
-    distributed under the terms of Sections 1 and 2 above on a medium
-    customarily used for software interchange; or,
-
-    c) Accompany it with the information you received as to the offer
-    to distribute corresponding source code.  (This alternative is
-    allowed only for noncommercial distribution and only if you
-    received the program in object code or executable form with such
-    an offer, in accord with Subsection b above.)
-
-The source code for a work means the preferred form of the work for
-making modifications to it.  For an executable work, complete source
-code means all the source code for all modules it contains, plus any
-associated interface definition files, plus the scripts used to
-control compilation and installation of the executable.  However, as a
-special exception, the source code distributed need not include
-anything that is normally distributed (in either source or binary
-form) with the major components (compiler, kernel, and so on) of the
-operating system on which the executable runs, unless that component
-itself accompanies the executable.
-
-If distribution of executable or object code is made by offering
-access to copy from a designated place, then offering equivalent
-access to copy the source code from the same place counts as
-distribution of the source code, even though third parties are not
-compelled to copy the source along with the object code.
-
-  4. You may not copy, modify, sublicense, or distribute the Program
-except as expressly provided under this License.  Any attempt
-otherwise to copy, modify, sublicense or distribute the Program is
-void, and will automatically terminate your rights under this License.
-However, parties who have received copies, or rights, from you under
-this License will not have their licenses terminated so long as such
-parties remain in full compliance.
-
-  5. You are not required to accept this License, since you have not
-signed it.  However, nothing else grants you permission to modify or
-distribute the Program or its derivative works.  These actions are
-prohibited by law if you do not accept this License.  Therefore, by
-modifying or distributing the Program (or any work based on the
-Program), you indicate your acceptance of this License to do so, and
-all its terms and conditions for copying, distributing or modifying
-the Program or works based on it.
-
-  6. Each time you redistribute the Program (or any work based on the
-Program), the recipient automatically receives a license from the
-original licensor to copy, distribute or modify the Program subject to
-these terms and conditions.  You may not impose any further
-restrictions on the recipients' exercise of the rights granted herein.
-You are not responsible for enforcing compliance by third parties to
+
+		       TERMS AND CONDITIONS
+
+  0. Definitions.
+
+  "This License" refers to version 3 of the GNU General Public License.
+
+  "Copyright" also means copyright-like laws that apply to other kinds of
+works, such as semiconductor masks.
+ 
+  "The Program" refers to any copyrightable work licensed under this
+License.  Each licensee is addressed as "you".  "Licensees" and
+"recipients" may be individuals or organizations.
+
+  To "modify" a work means to copy from or adapt all or part of the work
+in a fashion requiring copyright permission, other than the making of an
+exact copy.  The resulting work is called a "modified version" of the
+earlier work or a work "based on" the earlier work.
+
+  A "covered work" means either the unmodified Program or a work based
+on the Program.
+
+  To "propagate" a work means to do anything with it that, without
+permission, would make you directly or secondarily liable for
+infringement under applicable copyright law, except executing it on a
+computer or modifying a private copy.  Propagation includes copying,
+distribution (with or without modification), making available to the
+public, and in some countries other activities as well.
+
+  To "convey" a work means any kind of propagation that enables other
+parties to make or receive copies.  Mere interaction with a user through
+a computer network, with no transfer of a copy, is not conveying.
+
+  An interactive user interface displays "Appropriate Legal Notices"
+to the extent that it includes a convenient and prominently visible
+feature that (1) displays an appropriate copyright notice, and (2)
+tells the user that there is no warranty for the work (except to the
+extent that warranties are provided), that licensees may convey the
+work under this License, and how to view a copy of this License.  If
+the interface presents a list of user commands or options, such as a
+menu, a prominent item in the list meets this criterion.
+
+  1. Source Code.
+
+  The "source code" for a work means the preferred form of the work
+for making modifications to it.  "Object code" means any non-source
+form of a work.
+
+  A "Standard Interface" means an interface that either is an official
+standard defined by a recognized standards body, or, in the case of
+interfaces specified for a particular programming language, one that
+is widely used among developers working in that language.
+
+  The "System Libraries" of an executable work include anything, other
+than the work as a whole, that (a) is included in the normal form of
+packaging a Major Component, but which is not part of that Major
+Component, and (b) serves only to enable use of the work with that
+Major Component, or to implement a Standard Interface for which an
+implementation is available to the public in source code form.  A
+"Major Component", in this context, means a major essential component
+(kernel, window system, and so on) of the specific operating system
+(if any) on which the executable work runs, or a compiler used to
+produce the work, or an object code interpreter used to run it.
+
+  The "Corresponding Source" for a work in object code form means all
+the source code needed to generate, install, and (for an executable
+work) run the object code and to modify the work, including scripts to
+control those activities.  However, it does not include the work's
+System Libraries, or general-purpose tools or generally available free
+programs which are used unmodified in performing those activities but
+which are not part of the work.  For example, Corresponding Source
+includes interface definition files associated with source files for
+the work, and the source code for shared libraries and dynamically
+linked subprograms that the work is specifically designed to require,
+such as by intimate data communication or control flow between those
+subprograms and other parts of the work.
+
+  The Corresponding Source need not include anything that users
+can regenerate automatically from other parts of the Corresponding
+Source.
+
+  The Corresponding Source for a work in source code form is that
+same work.
+
+  2. Basic Permissions.
+
+  All rights granted under this License are granted for the term of
+copyright on the Program, and are irrevocable provided the stated
+conditions are met.  This License explicitly affirms your unlimited
+permission to run the unmodified Program.  The output from running a
+covered work is covered by this License only if the output, given its
+content, constitutes a covered work.  This License acknowledges your
+rights of fair use or other equivalent, as provided by copyright law.
+
+  You may make, run and propagate covered works that you do not
+convey, without conditions so long as your license otherwise remains
+in force.  You may convey covered works to others for the sole purpose
+of having them make modifications exclusively for you, or provide you
+with facilities for running those works, provided that you comply with
+the terms of this License in conveying all material for which you do
+not control copyright.  Those thus making or running the covered works
+for you must do so exclusively on your behalf, under your direction
+and control, on terms that prohibit them from making any copies of
+your copyrighted material outside their relationship with you.
+
+  Conveying under any other circumstances is permitted solely under
+the conditions stated below.  Sublicensing is not allowed; section 10
+makes it unnecessary.
+
+  3. Protecting Users' Legal Rights From Anti-Circumvention Law.
+
+  No covered work shall be deemed part of an effective technological
+measure under any applicable law fulfilling obligations under article
+11 of the WIPO copyright treaty adopted on 20 December 1996, or
+similar laws prohibiting or restricting circumvention of such
+measures.
+
+  When you convey a covered work, you waive any legal power to forbid
+circumvention of technological measures to the extent such circumvention
+is effected by exercising rights under this License with respect to
+the covered work, and you disclaim any intention to limit operation or
+modification of the work as a means of enforcing, against the work's
+users, your or third parties' legal rights to forbid circumvention of
+technological measures.
+
+  4. Conveying Verbatim Copies.
+
+  You may convey verbatim copies of the Program's source code as you
+receive it, in any medium, provided that you conspicuously and
+appropriately publish on each copy an appropriate copyright notice;
+keep intact all notices stating that this License and any
+non-permissive terms added in accord with section 7 apply to the code;
+keep intact all notices of the absence of any warranty; and give all
+recipients a copy of this License along with the Program.
+
+  You may charge any price or no price for each copy that you convey,
+and you may offer support or warranty protection for a fee.
+
+  5. Conveying Modified Source Versions.
+
+  You may convey a work based on the Program, or the modifications to
+produce it from the Program, in the form of source code under the
+terms of section 4, provided that you also meet all of these conditions:
+
+    a) The work must carry prominent notices stating that you modified
+    it, and giving a relevant date.
+
+    b) The work must carry prominent notices stating that it is
+    released under this License and any conditions added under section
+    7.  This requirement modifies the requirement in section 4 to
+    "keep intact all notices".
+
+    c) You must license the entire work, as a whole, under this
+    License to anyone who comes into possession of a copy.  This
+    License will therefore apply, along with any applicable section 7
+    additional terms, to the whole of the work, and all its parts,
+    regardless of how they are packaged.  This License gives no
+    permission to license the work in any other way, but it does not
+    invalidate such permission if you have separately received it.
+
+    d) If the work has interactive user interfaces, each must display
+    Appropriate Legal Notices; however, if the Program has interactive
+    interfaces that do not display Appropriate Legal Notices, your
+    work need not make them do so.
+
+  A compilation of a covered work with other separate and independent
+works, which are not by their nature extensions of the covered work,
+and which are not combined with it such as to form a larger program,
+in or on a volume of a storage or distribution medium, is called an
+"aggregate" if the compilation and its resulting copyright are not
+used to limit the access or legal rights of the compilation's users
+beyond what the individual works permit.  Inclusion of a covered work
+in an aggregate does not cause this License to apply to the other
+parts of the aggregate.
+
+  6. Conveying Non-Source Forms.
+
+  You may convey a covered work in object code form under the terms
+of sections 4 and 5, provided that you also convey the
+machine-readable Corresponding Source under the terms of this License,
+in one of these ways:
+
+    a) Convey the object code in, or embodied in, a physical product
+    (including a physical distribution medium), accompanied by the
+    Corresponding Source fixed on a durable physical medium
+    customarily used for software interchange.
+
+    b) Convey the object code in, or embodied in, a physical product
+    (including a physical distribution medium), accompanied by a
+    written offer, valid for at least three years and valid for as
+    long as you offer spare parts or customer support for that product
+    model, to give anyone who possesses the object code either (1) a
+    copy of the Corresponding Source for all the software in the
+    product that is covered by this License, on a durable physical
+    medium customarily used for software interchange, for a price no
+    more than your reasonable cost of physically performing this
+    conveying of source, or (2) access to copy the
+    Corresponding Source from a network server at no charge.
+
+    c) Convey individual copies of the object code with a copy of the
+    written offer to provide the Corresponding Source.  This
+    alternative is allowed only occasionally and noncommercially, and
+    only if you received the object code with such an offer, in accord
+    with subsection 6b.
+
+    d) Convey the object code by offering access from a designated
+    place (gratis or for a charge), and offer equivalent access to the
+    Corresponding Source in the same way through the same place at no
+    further charge.  You need not require recipients to copy the
+    Corresponding Source along with the object code.  If the place to
+    copy the object code is a network server, the Corresponding Source
+    may be on a different server (operated by you or a third party)
+    that supports equivalent copying facilities, provided you maintain
+    clear directions next to the object code saying where to find the
+    Corresponding Source.  Regardless of what server hosts the
+    Corresponding Source, you remain obligated to ensure that it is
+    available for as long as needed to satisfy these requirements.
+
+    e) Convey the object code using peer-to-peer transmission, provided
+    you inform other peers where the object code and Corresponding
+    Source of the work are being offered to the general public at no
+    charge under subsection 6d.
+
+  A separable portion of the object code, whose source code is excluded
+from the Corresponding Source as a System Library, need not be
+included in conveying the object code work.
+
+  A "User Product" is either (1) a "consumer product", which means any
+tangible personal property which is normally used for personal, family,
+or household purposes, or (2) anything designed or sold for incorporation
+into a dwelling.  In determining whether a product is a consumer product,
+doubtful cases shall be resolved in favor of coverage.  For a particular
+product received by a particular user, "normally used" refers to a
+typical or common use of that class of product, regardless of the status
+of the particular user or of the way in which the particular user
+actually uses, or expects or is expected to use, the product.  A product
+is a consumer product regardless of whether the product has substantial
+commercial, industrial or non-consumer uses, unless such uses represent
+the only significant mode of use of the product.
+
+  "Installation Information" for a User Product means any methods,
+procedures, authorization keys, or other information required to install
+and execute modified versions of a covered work in that User Product from
+a modified version of its Corresponding Source.  The information must
+suffice to ensure that the continued functioning of the modified object
+code is in no case prevented or interfered with solely because
+modification has been made.
+
+  If you convey an object code work under this section in, or with, or
+specifically for use in, a User Product, and the conveying occurs as
+part of a transaction in which the right of possession and use of the
+User Product is transferred to the recipient in perpetuity or for a
+fixed term (regardless of how the transaction is characterized), the
+Corresponding Source conveyed under this section must be accompanied
+by the Installation Information.  But this requirement does not apply
+if neither you nor any third party retains the ability to install
+modified object code on the User Product (for example, the work has
+been installed in ROM).
+
+  The requirement to provide Installation Information does not include a
+requirement to continue to provide support service, warranty, or updates
+for a work that has been modified or installed by the recipient, or for
+the User Product in which it has been modified or installed.  Access to a
+network may be denied when the modification itself materially and
+adversely affects the operation of the network or violates the rules and
+protocols for communication across the network.
+
+  Corresponding Source conveyed, and Installation Information provided,
+in accord with this section must be in a format that is publicly
+documented (and with an implementation available to the public in
+source code form), and must require no special password or key for
+unpacking, reading or copying.
+
+  7. Additional Terms.
+
+  "Additional permissions" are terms that supplement the terms of this
+License by making exceptions from one or more of its conditions.
+Additional permissions that are applicable to the entire Program shall
+be treated as though they were included in this License, to the extent
+that they are valid under applicable law.  If additional permissions
+apply only to part of the Program, that part may be used separately
+under those permissions, but the entire Program remains governed by
+this License without regard to the additional permissions.
+
+  When you convey a copy of a covered work, you may at your option
+remove any additional permissions from that copy, or from any part of
+it.  (Additional permissions may be written to require their own
+removal in certain cases when you modify the work.)  You may place
+additional permissions on material, added by you to a covered work,
+for which you have or can give appropriate copyright permission.
+
+  Notwithstanding any other provision of this License, for material you
+add to a covered work, you may (if authorized by the copyright holders of
+that material) supplement the terms of this License with terms:
+
+    a) Disclaiming warranty or limiting liability differently from the
+    terms of sections 15 and 16 of this License; or
+
+    b) Requiring preservation of specified reasonable legal notices or
+    author attributions in that material or in the Appropriate Legal
+    Notices displayed by works containing it; or
+
+    c) Prohibiting misrepresentation of the origin of that material, or
+    requiring that modified versions of such material be marked in
+    reasonable ways as different from the original version; or
+
+    d) Limiting the use for publicity purposes of names of licensors or
+    authors of the material; or
+
+    e) Declining to grant rights under trademark law for use of some
+    trade names, trademarks, or service marks; or
+
+    f) Requiring indemnification of licensors and authors of that
+    material by anyone who conveys the material (or modified versions of
+    it) with contractual assumptions of liability to the recipient, for
+    any liability that these contractual assumptions directly impose on
+    those licensors and authors.
+
+  All other non-permissive additional terms are considered "further
+restrictions" within the meaning of section 10.  If the Program as you
+received it, or any part of it, contains a notice stating that it is
+governed by this License along with a term that is a further
+restriction, you may remove that term.  If a license document contains
+a further restriction but permits relicensing or conveying under this
+License, you may add to a covered work material governed by the terms
+of that license document, provided that the further restriction does
+not survive such relicensing or conveying.
+
+  If you add terms to a covered work in accord with this section, you
+must place, in the relevant source files, a statement of the
+additional terms that apply to those files, or a notice indicating
+where to find the applicable terms.
+
+  Additional terms, permissive or non-permissive, may be stated in the
+form of a separately written license, or stated as exceptions;
+the above requirements apply either way.
+
+  8. Termination.
+
+  You may not propagate or modify a covered work except as expressly
+provided under this License.  Any attempt otherwise to propagate or
+modify it is void, and will automatically terminate your rights under
+this License (including any patent licenses granted under the third
+paragraph of section 11).
+
+  However, if you cease all violation of this License, then your
+license from a particular copyright holder is reinstated (a)
+provisionally, unless and until the copyright holder explicitly and
+finally terminates your license, and (b) permanently, if the copyright
+holder fails to notify you of the violation by some reasonable means
+prior to 60 days after the cessation.
+
+  Moreover, your license from a particular copyright holder is
+reinstated permanently if the copyright holder notifies you of the
+violation by some reasonable means, this is the first time you have
+received notice of violation of this License (for any work) from that
+copyright holder, and you cure the violation prior to 30 days after
+your receipt of the notice.
+
+  Termination of your rights under this section does not terminate the
+licenses of parties who have received copies or rights from you under
+this License.  If your rights have been terminated and not permanently
+reinstated, you do not qualify to receive new licenses for the same
+material under section 10.
+
+  9. Acceptance Not Required for Having Copies.
+
+  You are not required to accept this License in order to receive or
+run a copy of the Program.  Ancillary propagation of a covered work
+occurring solely as a consequence of using peer-to-peer transmission
+to receive a copy likewise does not require acceptance.  However,
+nothing other than this License grants you permission to propagate or
+modify any covered work.  These actions infringe copyright if you do
+not accept this License.  Therefore, by modifying or propagating a
+covered work, you indicate your acceptance of this License to do so.
+
+  10. Automatic Licensing of Downstream Recipients.
+
+  Each time you convey a covered work, the recipient automatically
+receives a license from the original licensors, to run, modify and
+propagate that work, subject to this License.  You are not responsible
+for enforcing compliance by third parties with this License.
+
+  An "entity transaction" is a transaction transferring control of an
+organization, or substantially all assets of one, or subdividing an
+organization, or merging organizations.  If propagation of a covered
+work results from an entity transaction, each party to that
+transaction who receives a copy of the work also receives whatever
+licenses to the work the party's predecessor in interest had or could
+give under the previous paragraph, plus a right to possession of the
+Corresponding Source of the work from the predecessor in interest, if
+the predecessor has it or can get it with reasonable efforts.
+
+  You may not impose any further restrictions on the exercise of the
+rights granted or affirmed under this License.  For example, you may
+not impose a license fee, royalty, or other charge for exercise of
+rights granted under this License, and you may not initiate litigation
+(including a cross-claim or counterclaim in a lawsuit) alleging that
+any patent claim is infringed by making, using, selling, offering for
+sale, or importing the Program or any portion of it.
+
+  11. Patents.
+
+  A "contributor" is a copyright holder who authorizes use under this
+License of the Program or a work on which the Program is based.  The
+work thus licensed is called the contributor's "contributor version".
+
+  A contributor's "essential patent claims" are all patent claims
+owned or controlled by the contributor, whether already acquired or
+hereafter acquired, that would be infringed by some manner, permitted
+by this License, of making, using, or selling its contributor version,
+but do not include claims that would be infringed only as a
+consequence of further modification of the contributor version.  For
+purposes of this definition, "control" includes the right to grant
+patent sublicenses in a manner consistent with the requirements of
 this License.
 
-  7. If, as a consequence of a court judgment or allegation of patent
-infringement or for any other reason (not limited to patent issues),
-conditions are imposed on you (whether by court order, agreement or
+  Each contributor grants you a non-exclusive, worldwide, royalty-free
+patent license under the contributor's essential patent claims, to
+make, use, sell, offer for sale, import and otherwise run, modify and
+propagate the contents of its contributor version.
+
+  In the following three paragraphs, a "patent license" is any express
+agreement or commitment, however denominated, not to enforce a patent
+(such as an express permission to practice a patent or covenant not to
+sue for patent infringement).  To "grant" such a patent license to a
+party means to make such an agreement or commitment not to enforce a
+patent against the party.
+
+  If you convey a covered work, knowingly relying on a patent license,
+and the Corresponding Source of the work is not available for anyone
+to copy, free of charge and under the terms of this License, through a
+publicly available network server or other readily accessible means,
+then you must either (1) cause the Corresponding Source to be so
+available, or (2) arrange to deprive yourself of the benefit of the
+patent license for this particular work, or (3) arrange, in a manner
+consistent with the requirements of this License, to extend the patent
+license to downstream recipients.  "Knowingly relying" means you have
+actual knowledge that, but for the patent license, your conveying the
+covered work in a country, or your recipient's use of the covered work
+in a country, would infringe one or more identifiable patents in that
+country that you have reason to believe are valid.
+  
+  If, pursuant to or in connection with a single transaction or
+arrangement, you convey, or propagate by procuring conveyance of, a
+covered work, and grant a patent license to some of the parties
+receiving the covered work authorizing them to use, propagate, modify
+or convey a specific copy of the covered work, then the patent license
+you grant is automatically extended to all recipients of the covered
+work and works based on it.
+
+  A patent license is "discriminatory" if it does not include within
+the scope of its coverage, prohibits the exercise of, or is
+conditioned on the non-exercise of one or more of the rights that are
+specifically granted under this License.  You may not convey a covered
+work if you are a party to an arrangement with a third party that is
+in the business of distributing software, under which you make payment
+to the third party based on the extent of your activity of conveying
+the work, and under which the third party grants, to any of the
+parties who would receive the covered work from you, a discriminatory
+patent license (a) in connection with copies of the covered work
+conveyed by you (or copies made from those copies), or (b) primarily
+for and in connection with specific products or compilations that
+contain the covered work, unless you entered into that arrangement,
+or that patent license was granted, prior to 28 March 2007.
+
+  Nothing in this License shall be construed as excluding or limiting
+any implied license or other defenses to infringement that may
+otherwise be available to you under applicable patent law.
+
+  12. No Surrender of Others' Freedom.
+
+  If conditions are imposed on you (whether by court order, agreement or
 otherwise) that contradict the conditions of this License, they do not
-excuse you from the conditions of this License.  If you cannot
-distribute so as to satisfy simultaneously your obligations under this
-License and any other pertinent obligations, then as a consequence you
-may not distribute the Program at all.  For example, if a patent
-license would not permit royalty-free redistribution of the Program by
-all those who receive copies directly or indirectly through you, then
-the only way you could satisfy both it and this License would be to
-refrain entirely from distribution of the Program.
-
-If any portion of this section is held invalid or unenforceable under
-any particular circumstance, the balance of the section is intended to
-apply and the section as a whole is intended to apply in other
-circumstances.
-
-It is not the purpose of this section to induce you to infringe any
-patents or other property right claims or to contest validity of any
-such claims; this section has the sole purpose of protecting the
-integrity of the free software distribution system, which is
-implemented by public license practices.  Many people have made
-generous contributions to the wide range of software distributed
-through that system in reliance on consistent application of that
-system; it is up to the author/donor to decide if he or she is willing
-to distribute software through any other system and a licensee cannot
-impose that choice.
-
-This section is intended to make thoroughly clear what is believed to
-be a consequence of the rest of this License.
-
-  8. If the distribution and/or use of the Program is restricted in
-certain countries either by patents or by copyrighted interfaces, the
-original copyright holder who places the Program under this License
-may add an explicit geographical distribution limitation excluding
-those countries, so that distribution is permitted only in or among
-countries not thus excluded.  In such case, this License incorporates
-the limitation as if written in the body of this License.
-
-  9. The Free Software Foundation may publish revised and/or new versions
-of the General Public License from time to time.  Such new versions will
+excuse you from the conditions of this License.  If you cannot convey a
+covered work so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you may
+not convey it at all.  For example, if you agree to terms that obligate you
+to collect a royalty for further conveying from those to whom you convey
+the Program, the only way you could satisfy both those terms and this
+License would be to refrain entirely from conveying the Program.
+
+  13. Use with the GNU Affero General Public License.
+
+  Notwithstanding any other provision of this License, you have
+permission to link or combine any covered work with a work licensed
+under version 3 of the GNU Affero General Public License into a single
+combined work, and to convey the resulting work.  The terms of this
+License will continue to apply to the part which is the covered work,
+but the special requirements of the GNU Affero General Public License,
+section 13, concerning interaction through a network will apply to the
+combination as such.
+
+  14. Revised Versions of this License.
+
+  The Free Software Foundation may publish revised and/or new versions of
+the GNU General Public License from time to time.  Such new versions will
 be similar in spirit to the present version, but may differ in detail to
 address new problems or concerns.
 
-Each version is given a distinguishing version number.  If the Program
-specifies a version number of this License which applies to it and "any
-later version", you have the option of following the terms and conditions
-either of that version or of any later version published by the Free
-Software Foundation.  If the Program does not specify a version number of
-this License, you may choose any version ever published by the Free Software
-Foundation.
-
-  10. If you wish to incorporate parts of the Program into other free
-programs whose distribution conditions are different, write to the author
-to ask for permission.  For software which is copyrighted by the Free
-Software Foundation, write to the Free Software Foundation; we sometimes
-make exceptions for this.  Our decision will be guided by the two goals
-of preserving the free status of all derivatives of our free software and
-of promoting the sharing and reuse of software generally.
-
-			    NO WARRANTY
-
-  11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
-FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW.  EXCEPT WHEN
-OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
-PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
-OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
-MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.  THE ENTIRE RISK AS
-TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU.  SHOULD THE
-PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
-REPAIR OR CORRECTION.
-
-  12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
-WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
-REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
-INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
-OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
-TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
-YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
-PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
-POSSIBILITY OF SUCH DAMAGES.
+  Each version is given a distinguishing version number.  If the
+Program specifies that a certain numbered version of the GNU General
+Public License "or any later version" applies to it, you have the
+option of following the terms and conditions either of that numbered
+version or of any later version published by the Free Software
+Foundation.  If the Program does not specify a version number of the
+GNU General Public License, you may choose any version ever published
+by the Free Software Foundation.
+
+  If the Program specifies that a proxy can decide which future
+versions of the GNU General Public License can be used, that proxy's
+public statement of acceptance of a version permanently authorizes you
+to choose that version for the Program.
+
+  Later license versions may give you additional or different
+permissions.  However, no additional obligations are imposed on any
+author or copyright holder as a result of your choosing to follow a
+later version.
+
+  15. Disclaimer of Warranty.
+
+  THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
+APPLICABLE LAW.  EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
+HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
+OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
+THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE.  THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
+IS WITH YOU.  SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
+ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+  16. Limitation of Liability.
+
+  IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
+THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
+GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
+USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
+DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
+PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
+EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGES.
+
+  17. Interpretation of Sections 15 and 16.
+
+  If the disclaimer of warranty and limitation of liability provided
+above cannot be given local legal effect according to their terms,
+reviewing courts shall apply local law that most closely approximates
+an absolute waiver of all civil liability in connection with the
+Program, unless a warranty or assumption of liability accompanies a
+copy of the Program in return for a fee.
 
 		     END OF TERMS AND CONDITIONS
+
+	    How to Apply These Terms to Your New Programs
+
+  If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+  To do so, attach the following notices to the program.  It is safest
+to attach them to the start of each source file to most effectively
+state the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+    <one line to give the program's name and a brief idea of what it does.>
+    Copyright (C) <year>  <name of author>
+
+    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
+    the Free Software Foundation, either version 3 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License
+    along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+Also add information on how to contact you by electronic and paper mail.
+
+  If the program does terminal interaction, make it output a short
+notice like this when it starts in an interactive mode:
+
+    <program>  Copyright (C) <year>  <name of author>
+    This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
+    This is free software, and you are welcome to redistribute it
+    under certain conditions; type `show c' for details.
+
+The hypothetical commands `show w' and `show c' should show the appropriate
+parts of the General Public License.  Of course, your program's commands
+might be different; for a GUI interface, you would use an "about box".
+
+  You should also get your employer (if you work as a programmer) or school,
+if any, to sign a "copyright disclaimer" for the program, if necessary.
+For more information on this, and how to apply and follow the GNU GPL, see
+<http://www.gnu.org/licenses/>.
+
+  The GNU General Public License does not permit incorporating your program
+into proprietary programs.  If your program is a subroutine library, you
+may consider it more useful to permit linking proprietary applications with
+the library.  If this is what you want to do, use the GNU Lesser General
+Public License instead of this License.  But first, please read
+<http://www.gnu.org/philosophy/why-not-lgpl.html>.
+
diff --git a/Makefile b/Makefile
index abe0938..9e58a26 100644
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,4 @@
-# $Id: Makefile 88 2007-03-07 01:35:18Z tim $
+# $Id: Makefile 111 2008-05-01 04:06:22Z tim $
 
 # Installation prefixes.  Change to install elsewhere.
 
@@ -10,9 +10,9 @@ MAN_PREFIX=$(PREFIX)/man
 ################################################################################
 
 CC=gcc
-OPTS=-std=gnu89 -pedantic -Wall
+OPTS=-std=gnu89 -pedantic -Wall -ggdb
 INC=-I/usr/local/include
-LIB=-L/usr/local/lib
+LIB=-L/usr/local/lib -lm
 
 UNAME := $(shell uname)
 ifneq ($(UNAME),Linux) 	
diff --git a/doc/Makefile b/doc/Makefile
index a275a50..f30d9da 100644
--- a/doc/Makefile
+++ b/doc/Makefile
@@ -1,17 +1,23 @@
-# $Id: Makefile 64 2006-07-23 16:02:53Z tim $
+# $Id: Makefile 119 2008-08-09 05:55:45Z tim $
 
-BUILD_FILES=$(BUILD_DOC)/man/man1/reglookup.1.gz $(BUILD_DOC)/man/man1/reglookup-timeline.1.gz
+BUILD_FILES=$(BUILD_DOC)/man/man1/reglookup.1.gz\
+	$(BUILD_DOC)/man/man1/reglookup-timeline.1.gz\
+	$(BUILD_DOC)/man/man1/reglookup-recover.1.gz
 
 default: $(BUILD_FILES)
 
-$(BUILD_DOC)/man/man1/reglookup.1.gz: man/man1/reglookup.1.gz
+$(BUILD_DOC)/man/man1:
 	mkdir -p $(BUILD_DOC)/man/man1
+
+$(BUILD_DOC)/man/man1/reglookup.1.gz: man/man1/reglookup.1.gz $(BUILD_DOC)/man/man1
 	cp man/man1/reglookup.1.gz $@
 
-$(BUILD_DOC)/man/man1/reglookup-timeline.1.gz: man/man1/reglookup-timeline.1.gz
-	mkdir -p $(BUILD_DOC)/man/man1
+$(BUILD_DOC)/man/man1/reglookup-timeline.1.gz: man/man1/reglookup-timeline.1.gz $(BUILD_DOC)/man/man1
 	cp man/man1/reglookup-timeline.1.gz $@
 
+$(BUILD_DOC)/man/man1/reglookup-recover.1.gz: man/man1/reglookup-recover.1.gz $(BUILD_DOC)/man/man1
+	cp man/man1/reglookup-recover.1.gz $@
+
 install:
 	cp -r $(BUILD_DOC)/* $(DOC_PREFIX)
 	ln -sf $(DOC_PREFIX)/man/man1/*  $(MAN_PREFIX)/man1
@@ -21,5 +27,7 @@ install:
 release:
 	docbook2x-man --to-stdout reglookup.1.docbook > man/man1/reglookup.1
 	docbook2x-man --to-stdout reglookup-timeline.1.docbook > man/man1/reglookup-timeline.1
+	docbook2x-man --to-stdout reglookup-recover.1.docbook > man/man1/reglookup-recover.1
 	cd man/man1 && gzip -9 -f reglookup.1
 	cd man/man1 && gzip -9 -f reglookup-timeline.1
+	cd man/man1 && gzip -9 -f reglookup-recover.1
diff --git a/doc/devel/TODO b/doc/devel/TODO
index 28815d4..dba1326 100644
--- a/doc/devel/TODO
+++ b/doc/devel/TODO
@@ -1,8 +1,16 @@
-$Id: TODO 86 2007-01-19 15:19:54Z tim $
+$Id: TODO 122 2008-08-09 20:24:01Z tim $
 
 If you are interested in contributing to this project, here's a few
 things you could look into:
 
+ - Currently there is no way on the command line to search for exotic 
+   paths/types.  For instance, if reglookup encounters an unknown VK
+   type, it just prints it out in Hex.  However, if you wanted to search
+   specifically for that type, there is no way to do it.  Similarly, it
+   isn't possible to specify certain binary or weird characters in
+   paths.  Reglookup should take the user path and unquote each path
+   component using the \xQQ syntax prior to searching.
+
  - Testing, testing, and more testing.  reglookup needs to be tested on 
    NT/XP/2k3/Vista.  A regression test suite would be nice too.  Some 
    thoughts on this include a script which randomly fuzzes an existing
@@ -32,7 +40,7 @@ things you could look into:
    I think this will speed things up greatly.
 
  - It might be nice to have a way to filter results by security 
-   descriptor information.
+   descriptor information.  Maybe by MTIME as well.
 
  - Documentation.  The security descriptor output format needs to be 
    documented.  Also, function contracts should be added to the 
diff --git a/doc/devel/references.txt b/doc/devel/references.txt
index 3ab8b8c..457bfa9 100644
--- a/doc/devel/references.txt
+++ b/doc/devel/references.txt
@@ -1,3 +1,10 @@
+- The Windows NT Registry File Format
+  (A work in progress to support this tool.)
+  http://sentinelchicken.com/research/registry_format/
+
+- Recovering Deleted Data From the Windows Registry
+  (The research that is implemented as a PoC in reglookup-recover.)
+  http://sentinelchicken.com/research/registry_recovery/
 
 - Petter Nordahl-Hagen.  Windows NT registry file format description.
   (The file 'winntreg.txt' included in this distribution is derived from this.)
diff --git a/doc/devel/winntreg.txt b/doc/devel/winntreg.txt
index 35f242d..31bbdf1 100644
--- a/doc/devel/winntreg.txt
+++ b/doc/devel/winntreg.txt
@@ -22,7 +22,7 @@ Offset            Size      Contents
 0x00000028      D-Word      Size of the data-blocks (Filesize-4kb)
 0x0000002C      D-Word      1
 0x000001FC      D-Word      Sum of all D-Words from 0x00000000 to
-0x000001FB  //XOR of all words. Nigel
+                            0x000001FB  //XOR of all words. Nigel
 
 I have analyzed more registry files (from multiple machines running
 NT 4.0 german version) and could not find an explanation for the values
@@ -109,9 +109,12 @@ Offset      Size      Contents
 0x0000      Word      ID: ASCII-"nk" = 0x6B6E
 0x0002      Word      for the root-key: 0x2C, otherwise 0x20  //key symbolic links 0x10. Nigel
 0x0004      Q-Word      write-date/time in windows nt notation
+0x000C      D-Word      UNKNOWN // added by TDM
 0x0010      D-Word      Offset of Owner/Parent key
 0x0014      D-Word      number of sub-Keys
+0x0018      D-Word      UNKNOWN // added by TDM
 0x001C      D-Word      Offset of the sub-key lf-Records
+0x0020      D-Word      UNKNOWN // added by TDM
 0x0024      D-Word      number of values
 0x0028      D-Word      Offset of the Value-List
 0x002C      D-Word      Offset of the sk-Record
@@ -157,10 +160,10 @@ Wert      Beteutung
 0x0007      RegMultiSZ:      multiple strings, seperated with 0
                   (UNICODE!)
 
-The "lf"-record
+The "lf"/"lh"/"ri"-record (hash list header)
 ===============
 Offset      Size      Contents
-0x0000      Word      ID: ASCII-"lf" = 0x666C
+0x0000      Word      ID: ASCII-"lf" = 0x666C  (or "lh" or "ri")
 0x0002      Word      number of keys
 0x0004      ????      Hash-Records
 
@@ -168,8 +171,9 @@ Hash-Record
 ===========
 Offset      Size      Contents
 0x0000      D-Word      Offset of corresponding "nk"-Record
-0x0004      D-Word      ASCII: the first 4 characters of the key-name, padded with 0-s. Case sensitiv!
-
+0x0004      D-Word      ASCII: the first 4 characters of the key-name, padded with 0-s. Case sensitive! 
+			(the hash value may be computed differently for the various header types 
+			 "lf"/"lh"/"ri"/etc)
 Keep in mind, that the value at 0x0004 is used for checking the data-consistency! If you change the 
 key-name you have to change the hash-value too!
 
diff --git a/doc/man/man1/reglookup-recover.1.gz b/doc/man/man1/reglookup-recover.1.gz
new file mode 100644
index 0000000..7deef08
Binary files /dev/null and b/doc/man/man1/reglookup-recover.1.gz differ
diff --git a/doc/man/man1/reglookup-timeline.1.gz b/doc/man/man1/reglookup-timeline.1.gz
index f07d13a..81cdc68 100644
Binary files a/doc/man/man1/reglookup-timeline.1.gz and b/doc/man/man1/reglookup-timeline.1.gz differ
diff --git a/doc/man/man1/reglookup.1.gz b/doc/man/man1/reglookup.1.gz
index 72eb363..27fb131 100644
Binary files a/doc/man/man1/reglookup.1.gz and b/doc/man/man1/reglookup.1.gz differ
diff --git a/doc/reglookup-recover.1.docbook b/doc/reglookup-recover.1.docbook
new file mode 100644
index 0000000..41266d9
--- /dev/null
+++ b/doc/reglookup-recover.1.docbook
@@ -0,0 +1,235 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<refentry id='reglookup-recover.1'>
+  <!--  $Id: reglookup-recover.1.docbook 119 2008-08-09 05:55:45Z tim $ -->
+  <refmeta>
+    <refentrytitle>reglookup</refentrytitle>
+    <manvolnum>1</manvolnum>
+    <refmiscinfo class="sectdesc">File Conversion Utilities</refmiscinfo>
+  </refmeta>
+  <refnamediv id='name'>
+    <refname>reglookup-recover</refname>
+    <refpurpose>Windows NT+ registry deleted data recovery tool</refpurpose>
+  </refnamediv>
+
+  <refsect1 id='synopsis'>
+    <title>SYNOPSIS</title>
+    <para>
+      <command>
+	reglookup-recover [options] <replaceable>registry-file</replaceable>
+      </command> 
+    </para>
+  </refsect1>
+
+  <refsect1 id='description'>
+    <title>DESCRIPTION</title>
+    <para>
+        reglookup-recover attempts to scour a Windows registry hive for 
+        deleted data structures and outputs those found in a CSV-like format.
+        print them out to stdout in a CSV-like format.
+    </para>
+  </refsect1>
+
+  <refsect1 id='options'>
+    <title>OPTIONS</title>
+    <para>
+      <command>reglookup-recover</command> accepts the following parameters:
+    </para>
+
+    <variablelist remap='IP'>
+      <varlistentry>
+        <term>
+          <option>-v</option>
+        </term>
+        <listitem>
+	  <para>
+	    Verbose output. (Currently does little to nothing.)
+	  </para>
+        </listitem>
+      </varlistentry>
+    </variablelist>
+
+    <variablelist remap='IP'>
+      <varlistentry>
+        <term>
+          <option>-h</option>
+        </term>
+        <listitem>
+	  <para>
+	    Enables the printing of a column header row. (default)
+	  </para>
+        </listitem>
+      </varlistentry>
+    </variablelist>
+
+    <variablelist remap='IP'>
+      <varlistentry>
+        <term>
+          <option>-H</option>
+        </term>
+        <listitem>
+	  <para>
+	    Disables the printing of a column header row.
+	  </para>
+        </listitem>
+      </varlistentry>
+    </variablelist>
+
+    <variablelist remap='IP'>
+      <varlistentry>
+        <term>
+          <option>-l</option>
+        </term>
+        <listitem>
+	  <para>
+            Display cells which could not be interpreted as valid
+            registry structures at the end of the output.
+	  </para>
+        </listitem>
+      </varlistentry>
+    </variablelist>
+
+    <variablelist remap='IP'>
+      <varlistentry>
+        <term>
+          <option>-L</option>
+        </term>
+        <listitem>
+	  <para>
+            Do not display cells which could not be interpreted as valid
+            registry structures.  This is the default behavior.
+	  </para>
+        </listitem>
+      </varlistentry>
+    </variablelist>
+
+    <variablelist remap='IP'>
+      <varlistentry>
+        <term>
+          <option>-r</option>
+        </term>
+        <listitem>
+	  <para>
+            Display raw cell contents for cells which were interpreted as intact
+            data structures.  This additional output will appear on the same
+            line as the interpreted data.
+	  </para>
+        </listitem>
+      </varlistentry>
+    </variablelist>
+
+    <variablelist remap='IP'>
+      <varlistentry>
+        <term>
+          <option>-R</option>
+        </term>
+        <listitem>
+	  <para>
+            Do not display raw cell contents for cells which were interpreted
+            as intact data structures.  This is the default behavior.
+	  </para>
+        </listitem>
+      </varlistentry>
+    </variablelist>
+
+    <variablelist remap='IP'>
+      <varlistentry>
+        <term>
+          <option><replaceable>registry-file</replaceable></option>
+        </term>
+        <listitem>
+	  <para>
+	    Required argument.  Specifies the location of the
+	    registry file to read.  The system registry files should be
+	    found under:
+	    <command>%SystemRoot%/system32/config</command>.
+	  </para>
+        </listitem>
+      </varlistentry>
+    </variablelist>
+  </refsect1>
+
+  <refsect1 id='output'>
+    <title>OUTPUT</title>
+    <para>
+      <!-- XXX: this should be a bit more formal -->
+      <command>reglookup-recover</command> generates a comma-separated values (CSV) 
+      like output and writes it to stdout. For more information on the syntax of 
+      the general format, see <command>reglookup(1)</command>.
+    </para>
+    <para>
+      This tool is new and the output format, particularly the included columns, 
+      may change in future revisions.  When this format stablizes, additional
+      documentation will be included here.
+    </para>
+  </refsect1>
+
+  <refsect1 id='examples'>
+    <title>EXAMPLES</title>
+    <para>
+      To dump the recoverable contents of a system registry hive:
+    </para>
+    <para>
+      <screen>
+	reglookup-recover /mnt/win/c/WINDOWS/system32/config/system
+      </screen>
+    </para>
+    <para>
+      Extract all available unallocated data, including unparsable unallocated
+      space and the raw data associated with parsed cells in a user-specific
+      registry:
+    </para>
+    <para>
+      <screen>
+	reglookup-recover -r -l '/mnt/win/c/Documents and Settings/user/NTUSER.DAT'
+      </screen>
+    </para>
+  </refsect1>
+
+  <refsect1 id='bugs'>
+    <title>BUGS</title>
+    <para>
+      This program has been smoke-tested against most current Windows target 
+      platforms, but a comprehensive test suite has not yet been developed.
+      (Please report results to the development mailing list if you encounter
+       any bugs.  Sample registry files and/or patches are greatly appreciated.)
+    </para>
+    <para>
+      This program is new as of RegLookup release 0.9.0 and should be considered 
+      unstable.
+    </para>
+    <para>
+      For more information on registry format details and the recovery 
+      algorithm, see:
+        http://sentinelchicken.com/research/registry_format/
+        http://sentinelchicken.com/research/registry_recovery/
+    </para>
+  </refsect1>
+
+  <refsect1 id='credits'>
+    <title>CREDITS</title>
+    <para>
+      This program was written by Timothy D. Morgan.
+    </para>
+  </refsect1>
+
+  <refsect1 id='license'>
+    <title>LICENSE</title>
+    <para>
+      Please see the file "LICENSE" included with this software
+      distribution.
+    </para>
+    <para>      
+      This program is distributed in the hope that it will be useful,
+      but WITHOUT ANY WARRANTY; without even the implied warranty of
+      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+      GNU General Public License version 3 for more details.
+    </para>
+  </refsect1>
+
+  <refsect1 id='seealso'>
+    <title>SEE ALSO</title>
+    <para>
+      reglookup-timeline(1) reglookup-recover(1)
+    </para>
+  </refsect1>
+</refentry>
diff --git a/doc/reglookup-timeline.1.docbook b/doc/reglookup-timeline.1.docbook
index dfedce8..cb0f61b 100644
--- a/doc/reglookup-timeline.1.docbook
+++ b/doc/reglookup-timeline.1.docbook
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <refentry id='reglookup-timeline.1'>
-  <!--  $Id: reglookup-timeline.1.docbook 91 2007-03-28 19:26:37Z tim $ -->
+  <!--  $Id: reglookup-timeline.1.docbook 119 2008-08-09 05:55:45Z tim $ -->
   <refmeta>
     <refentrytitle>reglookup-timeline</refentrytitle>
     <manvolnum>1</manvolnum>
@@ -8,7 +8,7 @@
   </refmeta>
   <refnamediv id='name'>
     <refname>reglookup-timeline</refname>
-    <refpurpose>windows NT+ registry MTIME timeline generator</refpurpose>
+    <refpurpose>Windows NT+ registry MTIME timeline generator</refpurpose>
   </refnamediv>
 
   <refsect1 id='synopsis'>
@@ -27,7 +27,7 @@
     <para>
       This script is a wrapper for <command>reglookup(1)</command>, and reads
       one or more registry
-      files to produce an MTIME sorted output.  This is helpful when building
+      files to produce an MTIME-sorted output.  This is helpful when building
       timelines for forensic investigations.
     </para>
   </refsect1>
@@ -98,18 +98,18 @@
       Please see the file "LICENSE" included with this software
       distribution.
     </para>
-    <para>      
+    <para>
       This program is distributed in the hope that it will be useful,
       but WITHOUT ANY WARRANTY; without even the implied warranty of
       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-      GNU General Public License version 2 for more details.
+      GNU General Public License version 3 for more details.
     </para>
   </refsect1>
 
   <refsect1 id='seealso'>
     <title>SEE ALSO</title>
     <para>
-      reglookup(1)
+      reglookup(1) reglookup-recover(1)
     </para>
   </refsect1>
 </refentry>
diff --git a/doc/reglookup.1.docbook b/doc/reglookup.1.docbook
index e6808c1..c7e8d5d 100644
--- a/doc/reglookup.1.docbook
+++ b/doc/reglookup.1.docbook
@@ -1,6 +1,6 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <refentry id='reglookup.1'>
-  <!--  $Id: reglookup.1.docbook 87 2007-02-04 01:55:18Z tim $ -->
+  <!--  $Id: reglookup.1.docbook 119 2008-08-09 05:55:45Z tim $ -->
   <refmeta>
     <refentrytitle>reglookup</refentrytitle>
     <manvolnum>1</manvolnum>
@@ -8,7 +8,7 @@
   </refmeta>
   <refnamediv id='name'>
     <refname>reglookup</refname>
-    <refpurpose>windows NT+ registry reader/lookup tool</refpurpose>
+    <refpurpose>Windows NT+ registry reader/lookup tool</refpurpose>
   </refnamediv>
 
   <refsect1 id='synopsis'>
@@ -26,8 +26,8 @@
         reglookup is designed to read windows registry elements and
         print them out to stdout in a CSV-like format.  It has filtering
         options to narrow the focus of the output.  This tool is
-        designed to work with on windows NT/2K/XP/2K3 registries, though
-        your mileage may vary.
+        designed to work with on Windows NT/2K/XP/2K3/Vista registries, 
+        though your mileage may vary.
     </para>
   </refsect1>
 
@@ -60,7 +60,7 @@
           <para>
 	    Specify a type filter.  Only elements which match this
 	    registry data type will be printed.  Acceptable values
-	    are: 
+	    are:
             <command>
 	      NONE, SZ, EXPAND_SZ, BINARY, DWORD, DWORD_BE,
 	      LINK, MULTI_SZ, RSRC_LIST, RSRC_DESC, RSRC_REQ_LIST, QWORD
@@ -110,7 +110,7 @@
 	    Adds four additional columns to output containing 
 	    information from key security descriptors.  The columns 
 	    are: owner, group, sacl, dacl.
-	    (This feature's output probably contains bugs right now.)
+	    (This feature's output has not been extensively tested.)
 	  </para>
         </listitem>
       </varlistentry>
@@ -151,8 +151,8 @@
         <listitem>
 	  <para>
 	    Required argument.  Specifies the location of the
-	    registry file to read.  Typically, these files will be
-	    found on a NTFS partition under
+	    registry file to read.  The system registry files should be
+	    found under:
 	    <command>%SystemRoot%/system32/config</command>.
 	  </para>
         </listitem>
@@ -292,10 +292,10 @@
   <refsect1 id='bugs'>
     <title>BUGS</title>
     <para>
-      This program has only been tested on a few different systems.
-      (Please report results to the development list if you test it 
-      on Windows NT 4.0, 2003, or Vista registries.  Also, if you 
-      test on any 64-bit architecture, please contact us.)
+      This program has been smoke-tested against most current Windows target 
+      platforms, but a comprehensive test suite has not yet been developed.
+      (Please report results to the development mailing list if you encounter
+       any bugs.  Sample registry files and/or patches are greatly appreciated.)
     </para>
     <para>
       Verbose output is not working.
@@ -313,15 +313,11 @@
     <para>
       Backslashes are currently considered special characters, to make 
       parsing easier for automated tools.  However, this causes paths 
-      to be difficult to read.
+      to be difficult to read by mere mortals.
     </para>
     <para>
-      You'll notice that registry paths aren't all the same as the
-      equivalents you see in the windows registry editor.  This is because
-      Windows constructs the registry view from multiple registry files, 
-      each with their own roots.  This utility merely shows what exists 
-      under a single root.  This isn't really a bug, but one should be 
-      aware of the differences in path.
+      For more information on registry format details, see:
+        http://sentinelchicken.com/research/registry_format/
     </para>
   </refsect1>
 
@@ -349,14 +345,14 @@
       This program is distributed in the hope that it will be useful,
       but WITHOUT ANY WARRANTY; without even the implied warranty of
       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-      GNU General Public License version 2 for more details.
+      GNU General Public License version 3 for more details.
     </para>
   </refsect1>
 
   <refsect1 id='seealso'>
     <title>SEE ALSO</title>
     <para>
-      reglookup-timeline(1)
+      reglookup-timeline(1) reglookup-recover(1)
     </para>
   </refsect1>
 </refentry>
diff --git a/include/byteorder.h b/include/byteorder.h
index 9772f4b..9ae20d7 100644
--- a/include/byteorder.h
+++ b/include/byteorder.h
@@ -10,7 +10,7 @@
  * 
  * 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
- * the Free Software Foundation; version 2 of the License.
+ * the Free Software Foundation; version 3 of the License.
  * 
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -21,7 +21,7 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  *
- * $Id: byteorder.h 30 2005-07-16 14:31:27Z tim $
+ * $Id: byteorder.h 111 2008-05-01 04:06:22Z tim $
  */
 
 #ifndef _BYTEORDER_H
diff --git a/include/lru_cache.h b/include/lru_cache.h
new file mode 100644
index 0000000..5b1ec8c
--- /dev/null
+++ b/include/lru_cache.h
@@ -0,0 +1,78 @@
+/*
+ * Copyright (C) 2008 Timothy D. Morgan
+ *
+ * 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
+ * the Free Software Foundation; version 3 of the License.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
+ *
+ * $Id: lru_cache.h 122 2008-08-09 20:24:01Z tim $
+ */
+
+#ifndef LRU_CACHE_H
+#define LRU_CACHE_H
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+struct lru_cache_element;
+typedef struct lru_cache_element lru_cache_element; 
+
+struct lru_cache_element
+{
+  void* index;
+  uint32_t index_len;
+  void* data;
+  lru_cache_element* next;
+  lru_cache_element* older;
+  lru_cache_element* newer;
+};
+
+typedef struct _lru_cache
+{
+  uint32_t secret;
+  uint32_t num_keys;
+  uint32_t num_buckets;
+  uint32_t max_keys;
+  lru_cache_element* oldest;
+  lru_cache_element* newest;
+  lru_cache_element** table;
+  bool free_data;
+} lru_cache;
+
+
+lru_cache* lru_cache_create(uint32_t max_keys, uint32_t secret, bool free_data);
+void lru_cache_destroy(lru_cache* ht);
+
+/* Returns a pointer to the old, replaced data stored at index.  
+ * Returns NULL if no entry was overwritten.
+ */
+bool lru_cache_update(lru_cache* ht, const void* index, 
+		      uint32_t index_len, void* data);
+
+/* Returns pointer to data previously stored at index.
+ * If no data was found at index, NULL is returned.
+ */
+void* lru_cache_find(lru_cache* ht, const void* index, 
+		     uint32_t index_len);
+
+/* Removes entry from table at index.
+ * Returns pointer to data that was there previously.  
+ * Returns NULL if no entry is at index.
+ */
+bool lru_cache_remove(lru_cache* ht, const void* index, 
+		      uint32_t index_len);
+
+#endif
diff --git a/include/range_list.h b/include/range_list.h
new file mode 100644
index 0000000..7ef9536
--- /dev/null
+++ b/include/range_list.h
@@ -0,0 +1,193 @@
+/*
+ * Copyright (C) 2008 Timothy D. Morgan
+ *
+ * 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
+ * the Free Software Foundation; version 3 of the License.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
+ *
+ * $Id: range_list.h 122 2008-08-09 20:24:01Z tim $
+ */
+
+#include <stdlib.h>
+#include <stdbool.h>
+#include <stdint.h>
+#include <string.h>
+
+#ifndef _RANGE_LIST_H
+#define _RANGE_LIST_H
+
+
+typedef struct _range_list_element
+{
+  uint32_t offset;
+  uint32_t length;
+  void* data;
+} range_list_element;
+
+
+typedef struct _range_list
+{
+  range_list_element** elements;
+  uint32_t elem_alloced;
+  uint32_t size;
+} range_list;
+
+
+/* range_list_new(): 
+ *  Allocates a new range_list.
+ *
+ * Returns: 
+ *  A newly allocated range_list, or NULL if an error occurred.
+ */
+range_list* range_list_new();
+
+
+/* range_list_free(): 
+ *  Frees the memory associated with a range_list, including the elements, but
+ *  not any data parameters referenced by those elements.  If rl is NULL, does
+ *  nothing.
+ *
+ * Arguments:
+ *  rl -- the range_list to be free()d.
+ */
+void range_list_free(range_list* rl);
+
+
+/* range_list_size(): 
+ *  Query the current number of elements on a range_list
+ *
+ * Arguments:
+ *  rl -- the range_list to query
+ *
+ * Returns:
+ *  The number of elements currently in the list.
+ */
+uint32_t range_list_size(const range_list* rl);
+
+
+/* range_list_add():
+ *  Adds an element to the range_list.  
+ *  The new element must not overlap with others.
+ *  NOTE: this is a slow operation.
+ *
+ * Arguments:
+ *  rl     -- the range list to update
+ *  offset -- the starting point for the range
+ *  length -- the length of the range
+ *  data   -- misc data associated with this range element
+ * Returns:
+ *  true on success, false on failure.
+ *  Failures can occur due to memory limitations, max_size limitations,
+ *  or if the submitted range overlaps with an existing element.  Other
+ *  errors may also be possible.
+ */
+bool range_list_add(range_list* rl, uint32_t offset, uint32_t length, void* data);
+
+
+/* range_list_remove():
+ *  Removes an element from the list.  The element data structure will be 
+ *  freed, but the data property will not be.
+ *
+ * Arguments:
+ *  rl     -- the range_list to modify
+ *  index  -- the element index to remove
+ *
+ * Returns:
+ *  true if the element was successfully removed, false otherwise.
+ */
+bool range_list_remove(range_list* rl, uint32_t index);
+
+
+/* range_list_get():
+ *  Retrieves the element for a given index.
+ *
+ * Arguments:
+ *  rl    -- the range_list being queried.
+ *  index -- the element index desired.
+ * 
+ * Returns:
+ *  The element for a given index, or NULL if the element is not available.
+ */
+const range_list_element* range_list_get(const range_list* rl, uint32_t index);
+
+
+/* range_list_find():
+ *  Attempts to find the unique element whose range encompasses offset.
+ *
+ * Arguments:
+ *  rl     -- the range_list being queried.
+ *  offset -- the location for which an element is desired.
+ *
+ * Returns:
+ *  A matching element index or a negative value if none could be found.
+ */
+int32_t range_list_find(const range_list* rl, uint32_t offset);
+
+
+/* range_list_find_data():
+ *  Same as range_list_find(), but returns the data associated with an element.
+ *
+ * Arguments:
+ *  rl     -- the range_list being queried.
+ *  offset -- the address to search for in the ranges
+ *
+ * Returns:
+ *  The data element of the matching element index or NULL if none could
+ *  be found.
+ *
+ *  NOTE: May also return NULL if an element matched but if the data
+ *        element was never set.
+ */
+void* range_list_find_data(const range_list* rl, uint32_t offset);
+
+
+/* range_list_split_element():
+ *  Splits an existing element into two elements in place.
+ *
+ *  The resulting list will contain an additional element whose offset 
+ *  is the one provided and whose length extends to the end of the old element
+ *  (the one identified by the index).  The original element's offset will 
+ *  remain the same while it's length is shortened such that it is contiguous
+ *  with the newly created element.  The newly created element will have an index 
+ *  of one more than the current element.
+ *
+ *  Both the original element and the newly created element will reference the 
+ *  original element's data.
+ *
+ * Arguments:
+ *  rl     -- the range_list to modify
+ *  index  -- the index of the element to be split
+ *  offset -- the at which the element will be split
+ *
+ * Returns:
+ *  true if the element was successfully split, false otherwise.
+ *   
+ *
+ */
+bool range_list_split_element(range_list* rl, uint32_t index, uint32_t offset);
+
+
+/* range_list_has_range():
+ *  Determines whether or not a specified range exists contiguously within the
+ *  range_list.
+ *
+ * Arguments:
+ *  rl     -- the range_list to search
+ *  start  -- the offset at the beginning of the range
+ *  length -- the length of the range
+ *
+ * Returns:
+ *  true if the specified range exists and is complete, false otherwise.
+ */
+bool range_list_has_range(range_list* rl, uint32_t start, uint32_t length);
+
+#endif
diff --git a/include/regfi.h b/include/regfi.h
index 1ef2b9e..f4165d3 100644
--- a/include/regfi.h
+++ b/include/regfi.h
@@ -5,7 +5,7 @@
  * Unix SMB/CIFS implementation.
  * Windows NT registry I/O library
  *
- * Copyright (C) 2005-2007 Timothy D. Morgan
+ * Copyright (C) 2005-2008 Timothy D. Morgan
  * Copyright (C) 2005 Gerald (Jerry) Carter
  *
  * This program is free software; you can redistribute it and/or modify
@@ -21,7 +21,7 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  *
- * $Id: regfi.h 84 2007-01-19 14:52:25Z tim $
+ * $Id: regfi.h 121 2008-08-09 17:22:26Z tim $
  */
 
 /************************************************************
@@ -47,6 +47,8 @@
 
 #include "smb_deps.h"
 #include "void_stack.h"
+#include "range_list.h"
+#include "lru_cache.h"
 
 /******************************************************************************/
 /* Macros */
@@ -69,108 +71,123 @@
 /* Not a real type in the registry */
 #define REG_KEY                        0x7FFFFFFF
 
-
-#define REGF_BLOCKSIZE		0x1000
-#define REGF_ALLOC_BLOCK	0x1000
-#define REGF_MAX_DEPTH		512
+#define REGF_BLOCKSIZE		   0x1000
+#define REGF_ALLOC_BLOCK	   0x1000 /* Minimum allocation unit for HBINs */
+#define REGF_MAX_DEPTH		   512
 
 /* header sizes for various records */
+#define REGF_MAGIC_SIZE		   4
+#define HBIN_MAGIC_SIZE		   4
+#define HBIN_HEADER_REC_SIZE	   0x20
+#define REC_HDR_SIZE		   2
+
+#define REGF_OFFSET_NONE           0xffffffff
+#define REGFI_NK_MIN_LENGTH        0x4C
+#define REGFI_VK_MIN_LENGTH        0x14
+#define REGFI_SK_MIN_LENGTH        0x14
+#define REGFI_HASH_LIST_MIN_LENGTH 0x4
+
+/* Constants used for validation */
+ /* Minimum time is Jan 1, 1990 00:00:00 */
+#define REGFI_MTIME_MIN_HIGH       0x01B41E6D
+#define REGFI_MTIME_MIN_LOW        0x26F98000
+ /* Maximum time is Jan 1, 2290 00:00:00
+  * (We hope no one is using Windows by then...) 
+  */
+#define REGFI_MTIME_MAX_HIGH       0x03047543
+#define REGFI_MTIME_MAX_LOW        0xC80A4000
 
-#define REGF_HDR_SIZE		4
-#define HBIN_HDR_SIZE		4
-#define HBIN_HEADER_REC_SIZE	0x24
-#define REC_HDR_SIZE		2
-
-#define REGF_OFFSET_NONE	0xffffffff
 
 /* Flags for the vk records */
+#define VK_FLAG_NAME_PRESENT	   0x0001
+#define VK_DATA_IN_OFFSET	   0x80000000
+#define VK_MAX_DATA_LENGTH         1024*1024
 
-#define VK_FLAG_NAME_PRESENT	0x0001
-#define VK_DATA_IN_OFFSET	0x80000000
-#define VK_MAX_DATA_LENGTH      1024*1024
-
-/* NK record macros */
-
-#define NK_TYPE_LINKKEY		0x0010
-#define NK_TYPE_NORMALKEY	0x0020
-#define NK_TYPE_ROOTKEY		0x002c
-
-#define HBIN_STORE_REF(x, y) { x->hbin = y; y->ref_count++ };
-/* if the count == 0; we can clean up */
-#define HBIN_REMOVE_REF(x, y){ x->hbin = NULL; y->ref_count-- };
+/* NK record types */
+#define NK_TYPE_LINKKEY		   0x0010
+#define NK_TYPE_NORMALKEY	   0x0020
+#define NK_TYPE_ROOTKEY		   0x002c
+ /* TODO: Unknown type that shows up in Vista registries */
+#define NK_TYPE_UNKNOWN1           0x1020
 
 
 /* HBIN block */
-struct regf_hbin;
-typedef struct regf_hbin {
-  struct regf_hbin* prev;
-  struct regf_hbin* next;
+typedef struct regf_hbin 
+{
   uint32 file_off;       /* my offset in the registry file */
-  uint32 free_off;       /* offset to free space within the hbin record */
-  uint32 free_size;      /* amount of data left in the block */
   uint32 ref_count;      /* how many active records are pointing to this
                           * block (not used currently) 
 			  */
   
   uint32 first_hbin_off; /* offset from first hbin block */
-  uint32 block_size;     /* block size of this block is
-                          * usually a multiple of 4096Kb 
+  uint32 block_size;     /* block size of this block 
+                          * Should be a multiple of 4096 (0x1000)
+			  */
+  uint32 next_block;     /* relative offset to next block.  
+			  * NOTE: This value may be unreliable!
 			  */
-  uint8  header[HBIN_HDR_SIZE]; /* "hbin" */
-  prs_struct ps;	 /* data */
-  bool dirty;            /* has this hbin block been modified? */
+
+  uint8 magic[HBIN_MAGIC_SIZE]; /* "hbin" */
 } REGF_HBIN;
 
-/* ??? List -- list of key offsets and hashed names for consistency */
-typedef struct {
+
+/* Hash List -- list of key offsets and hashed names for consistency */
+typedef struct 
+{
   uint32 nk_off;
-  uint8 keycheck[sizeof(uint32)];
-} REGF_HASH_REC;
+  uint32 hash;
+} REGF_HASH_LIST_ELEM;
+
 
-typedef struct {
+typedef struct 
+{
+  uint32 offset;	/* Real offset of this record's cell in the file */
+  uint32 cell_size;	 /* ((start_offset - end_offset) & 0xfffffff8) */
   REGF_HBIN* hbin;       /* pointer to HBIN record (in memory) containing 
 			  * this nk record 
 			  */
-  REGF_HASH_REC* hashes;
   uint32 hbin_off;	 /* offset from beginning of this hbin block */
-  uint32 rec_size;	 /* ((start_offset - end_offset) & 0xfffffff8) */
+  REGF_HASH_LIST_ELEM* hashes;
   
-  uint8 header[REC_HDR_SIZE];
+  uint8 magic[REC_HDR_SIZE];
   uint16 num_keys;
-} REGF_LF_REC;
+} REGF_HASH_LIST;
+
 
 /* Key Value */
+typedef struct 
+{
+  uint32 offset;	/* Real offset of this record's cell in the file */
+  uint32 cell_size;	/* ((start_offset - end_offset) & 0xfffffff8) */
 
-typedef struct {
   REGF_HBIN* hbin;	/* pointer to HBIN record (in memory) containing 
 			 * this nk record 
 			 */
-  char*  valuename;
   uint8* data;
+  uint16 name_length;
+  char*  valuename;
   uint32 hbin_off;	/* offset from beginning of this hbin block */
-  uint32 rec_size;	/* ((start_offset - end_offset) & 0xfffffff8) */
-  uint32 rec_off;	/* offset stored in the value list */
   
   uint32 data_size;
-  uint32 data_off;
+  uint32 data_off;      /* offset of data cell (virtual) */
   uint32 type;
-  uint8  header[REC_HDR_SIZE];
+  uint8  magic[REC_HDR_SIZE];
   uint16 flag;
+  uint16 unknown1;
+  bool data_in_offset;
 } REGF_VK_REC;
 
 
 /* Key Security */
 struct _regf_sk_rec;
 
-typedef struct _regf_sk_rec {
-  struct _regf_sk_rec* next;
-  struct _regf_sk_rec* prev;
-  REGF_HBIN* hbin;	/* pointer to HBIN record (in memory) containing 
-			 * this nk record 
-			 */
+typedef struct _regf_sk_rec 
+{
+  uint32 offset;        /* Real file offset of this record */
+  uint32 cell_size;	/* ((start_offset - end_offset) & 0xfffffff8) */
+
   SEC_DESC* sec_desc;
   uint32 hbin_off;	/* offset from beginning of this hbin block */
-  uint32 rec_size;	/* ((start_offset - end_offset) & 0xfffffff8) */
   
   uint32 sk_off;	/* offset parsed from NK record used as a key
 			 * to lookup reference to this SK record 
@@ -179,28 +196,31 @@ typedef struct _regf_sk_rec {
   uint32 prev_sk_off;
   uint32 next_sk_off;
   uint32 ref_count;
-  uint32 size;
-  uint8  header[REC_HDR_SIZE];
+  uint32 desc_size;     /* size of security descriptor */
+  uint16 unknown_tag;
+  uint8  magic[REC_HDR_SIZE];
 } REGF_SK_REC;
 
 
-/* Key Name */ 
-typedef struct {
-  uint32 hbin_off;	/* offset from beginning of this hbin block */
-  uint32 rec_size;	/* ((start_offset - end_offset) & 0xfffffff8) */
-  REGF_HBIN *hbin;	/* pointer to HBIN record (in memory) containing 
-			 * this nk record */
+/* Key Name */
+typedef struct
+{
+  uint32 offset;	/* Real offset of this record's cell in the file */
+  uint32 cell_size;	/* Actual or estimated length of the cell.  
+			 * Always in multiples of 8. 
+			 */
 
   /* link in the other records here */
-  REGF_VK_REC* values;
-  REGF_SK_REC* sec_desc;
-  REGF_LF_REC subkeys;
+  REGF_VK_REC** values;
+  REGF_HASH_LIST* subkeys;
   
   /* header information */
   /* XXX: should we be looking for types other than the root key type? */
-  uint16 key_type;      
-  uint8  header[REC_HDR_SIZE];
+  uint16 key_type;
+  uint8  magic[REC_HDR_SIZE];
   NTTIME mtime;
+  uint16 name_length;
+  uint16 classname_length;
   char* classname;
   char* keyname;
   uint32 parent_off;	/* back pointer in registry hive */
@@ -213,6 +233,9 @@ typedef struct {
   uint32 max_bytes_value;           /* max value data size */
   
   /* unknowns */
+  uint32 unknown1;
+  uint32 unknown2;
+  uint32 unknown3;
   uint32 unk_index;		    /* nigel says run time index ? */
   
   /* children */
@@ -220,52 +243,61 @@ typedef struct {
   uint32 subkeys_off;	/* hash records that point to NK records */	
   uint32 num_values;
   uint32 values_off;	/* value lists which point to VK records */
-  uint32 sk_off;	/* offset to SK record */
-  
+  uint32 sk_off;	/* offset to SK record */  
 } REGF_NK_REC;
 
 
+
 /* REGF block */
-typedef struct {
+typedef struct 
+{
   /* run time information */
   int fd;	  /* file descriptor */
-  int open_flags; /* flags passed to the open() call */
+  /* For sanity checking (not part of the registry header) */
+  uint32 file_length;
   void* mem_ctx;  /* memory context for run-time file access information */
-  REGF_HBIN* block_list; /* list of open hbin blocks */
-  
-  /* file format information */
-  REGF_SK_REC* sec_desc_list;	/* list of security descriptors referenced 
-				 * by NK records 
-				 */
-  
-  uint8  header[REGF_HDR_SIZE];	/* "regf" */
+
+  /* Experimental hbin lists */
+  range_list* hbins;
+
+  /* file format information */  
+  uint8  magic[REGF_MAGIC_SIZE];/* "regf" */
   NTTIME mtime;
   uint32 data_offset;		/* offset to record in the first (or any?) 
 				 * hbin block 
 				 */
   uint32 last_block;		/* offset to last hbin block in file */
-  uint32 checksum;		/* XOR of bytes 0x0000 - 0x01FB */
+
+  uint32 checksum;		/* Stored checksum. */
+  uint32 computed_checksum;     /* Our own calculation of the checksum.
+				 * (XOR of bytes 0x0000 - 0x01FB) 
+				 */
   
-  /* unknowns */
+  /* unknown data structure values */
   uint32 unknown1;
   uint32 unknown2;
   uint32 unknown3;
   uint32 unknown4;
   uint32 unknown5;
   uint32 unknown6;
+  uint32 unknown7;
 } REGF_FILE;
 
 
-typedef struct {
+
+typedef struct 
+{
   REGF_FILE* f;
   void_stack* key_positions;
+  lru_cache* sk_recs;
   REGF_NK_REC* cur_key;
   uint32 cur_subkey;
   uint32 cur_value;
 } REGFI_ITERATOR;
 
 
-typedef struct {
+typedef struct 
+{
   REGF_NK_REC* nk;
   uint32 cur_subkey;
   /* We could store a cur_value here as well, but didn't see 
@@ -276,7 +308,7 @@ typedef struct {
 
 /******************************************************************************/
 /* Function Declarations */
-
+/*  Main API */
 const char*           regfi_type_val2str(unsigned int val);
 int                   regfi_type_str2val(const char* str);
 
@@ -299,6 +331,7 @@ bool                  regfi_iterator_find_subkey(REGFI_ITERATOR* i,
 bool                  regfi_iterator_walk_path(REGFI_ITERATOR* i, 
 					       const char** path);
 const REGF_NK_REC*    regfi_iterator_cur_key(REGFI_ITERATOR* i);
+const REGF_SK_REC*    regfi_iterator_cur_sk(REGFI_ITERATOR* i);
 const REGF_NK_REC*    regfi_iterator_first_subkey(REGFI_ITERATOR* i);
 const REGF_NK_REC*    regfi_iterator_cur_subkey(REGFI_ITERATOR* i);
 const REGF_NK_REC*    regfi_iterator_next_subkey(REGFI_ITERATOR* i);
@@ -309,10 +342,62 @@ const REGF_VK_REC*    regfi_iterator_first_value(REGFI_ITERATOR* i);
 const REGF_VK_REC*    regfi_iterator_cur_value(REGFI_ITERATOR* i);
 const REGF_VK_REC*    regfi_iterator_next_value(REGFI_ITERATOR* i);
 
+/************************************/
+/*  Low-layer data structure access */
+/************************************/
+REGF_FILE*            regfi_parse_regf(int fd, bool strict);
+REGF_HBIN*            regfi_parse_hbin(REGF_FILE* file, uint32 offset, 
+				       bool strict);
+
+
+/* regfi_parse_nk: Parses an NK record.
+ *
+ * Arguments:
+ *   f        -- the registry file structure
+ *   offset   -- the offset of the cell (not the record) to be parsed.
+ *   max_size -- the maximum size the NK cell could be. (for validation)
+ *   strict   -- if true, rejects any malformed records.  Otherwise,
+ *               tries to minimally validate integrity.
+ * Returns:
+ *   A newly allocated NK record structure, or NULL on failure.
+ */
+REGF_NK_REC*          regfi_parse_nk(REGF_FILE* file, uint32 offset, 
+				     uint32 max_size, bool strict);
+
 
 /* Private Functions */
 REGF_NK_REC*          regfi_rootkey(REGF_FILE* file);
 void                  regfi_key_free(REGF_NK_REC* nk);
+uint32                regfi_read(int fd, uint8* buf, uint32* length);
+
+
+
+/****************/
+/* Experimental */
+/****************/
+REGF_NK_REC* regfi_load_key(REGF_FILE* file, uint32 offset, bool strict);
+
+REGF_HASH_LIST* regfi_load_hashlist(REGF_FILE* file, uint32 offset, 
+				    uint32 num_keys, uint32 max_size, 
+				    bool strict);
+
+REGF_VK_REC** regfi_load_valuelist(REGF_FILE* file, uint32 offset, 
+				   uint32 num_values, uint32 max_size, 
+				   bool strict);
+
+REGF_VK_REC* regfi_parse_vk(REGF_FILE* file, uint32 offset, 
+			    uint32 max_size, bool strict);
+
+uint8* regfi_parse_data(REGF_FILE* file, uint32 offset, 
+			uint32 length, bool strict);
+
+REGF_SK_REC* regfi_parse_sk(REGF_FILE* file, uint32 offset, uint32 max_size, bool strict);
+
+range_list* regfi_parse_unalloc_cells(REGF_FILE* file);
+
+REGF_HBIN* regfi_lookup_hbin(REGF_FILE* file, uint32 offset);
 
+bool regfi_parse_cell(int fd, uint32 offset, uint8* hdr, uint32 hdr_len,
+		      uint32* cell_length, bool* unalloc);
 
 #endif	/* _REGFI_H */
diff --git a/include/smb_deps.h b/include/smb_deps.h
index ca4ab25..9602e94 100644
--- a/include/smb_deps.h
+++ b/include/smb_deps.h
@@ -9,7 +9,7 @@
  *
  * 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
- * the Free Software Foundation; version 2 of the License.
+ * the Free Software Foundation; version 3 of the License.
  * 
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -20,7 +20,7 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  *
- * $Id: smb_deps.h 84 2007-01-19 14:52:25Z tim $
+ * $Id: smb_deps.h 111 2008-05-01 04:06:22Z tim $
  */
 
 #include <stdlib.h>
diff --git a/include/void_stack.h b/include/void_stack.h
index d04dcfb..7d5f415 100644
--- a/include/void_stack.h
+++ b/include/void_stack.h
@@ -3,7 +3,7 @@
  *
  * 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
- * the Free Software Foundation; version 2 of the License.
+ * the Free Software Foundation; version 3 of the License.
  * 
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -14,7 +14,7 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
  *
- * $Id: void_stack.h 89 2007-03-09 15:00:09Z tim $
+ * $Id: void_stack.h 111 2008-05-01 04:06:22Z tim $
  */
 
 #include <stdlib.h>
diff --git a/lib/Makefile b/lib/Makefile
index 2aab6de..c406e16 100644
--- a/lib/Makefile
+++ b/lib/Makefile
@@ -2,7 +2,7 @@
 
 ################################################################################
 
-FILES=regfi.o smb_deps.o void_stack.o
+FILES=regfi.o smb_deps.o void_stack.o range_list.o lru_cache.o
 
 all: $(FILES)
 
@@ -15,5 +15,11 @@ smb_deps.o: smb_deps.c
 void_stack.o: void_stack.c
 	$(CC) $(CFLAGS) $(OPTS) $(INC) -c -o $@ void_stack.c
 
+range_list.o: range_list.c
+	$(CC) $(CFLAGS) $(OPTS) $(INC) -c -o $@ range_list.c
+
+lru_cache.o: lru_cache.c
+	$(CC) $(CFLAGS) $(OPTS) $(INC) -c -o $@ lru_cache.c
+
 clean:
 	rm -f $(FILES)
diff --git a/lib/lru_cache.c b/lib/lru_cache.c
new file mode 100644
index 0000000..987717d
--- /dev/null
+++ b/lib/lru_cache.c
@@ -0,0 +1,363 @@
+/*
+ * Copyright (C) 2008 Timothy D. Morgan
+ *
+ * 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
+ * the Free Software Foundation; version 3 of the License.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
+ *
+ * $Id: lru_cache.c 122 2008-08-09 20:24:01Z tim $
+ */
+
+#include "../include/lru_cache.h"
+
+
+#define LRU_CACHE_DEBUG 0
+
+/* XXX: really should replace this with a real universal hash or other
+ *      fast HMAC.
+ */ 
+static uint32_t lru_cache_compute_hash(uint32_t num_buckets,
+				       uint32_t secret,
+				       const void* buf,
+				       uint32_t buf_len)
+{
+  uint32_t i;
+  uint32_t ret_val = 0x243f6a88;
+  unsigned char* s = (unsigned char*)&secret;
+  const unsigned char* b = (unsigned char*)buf;
+
+  for(i=0; i<buf_len; i++)
+    ret_val = (ret_val+(i^s[i%4])*b[i]) % num_buckets;
+  
+  return ret_val;
+}
+
+/* Returns approximately floor(log_2(n)) (log base 2 of n, floored) 
+ * If n == 0, returns 0
+ */
+static uint32_t lru_cache_floor_log2(uint32_t n)
+{
+  uint32_t ret_val;
+  
+  for(ret_val=31; ret_val > 1; ret_val--)
+    if((n & (1 << ret_val)) != 0)
+      return ret_val;
+
+  return 0;
+}
+
+#if 0
+static void lru_cache_print(lru_cache* ht)
+{
+  uint32_t i;
+  lru_cache_element* cur;
+
+  printf("from newest to oldest:\n");
+  for(cur=ht->newest; cur != NULL; cur=cur->older)
+  {
+    /*    write(STDOUT_FILENO, cur->index, cur->index_len);*/
+    printf("%p", (void*)cur);
+    printf("\n");
+    if(cur->older == ht->newest)
+    {
+      printf("??? Loop in LRU list!!");
+      break;
+    }
+  }
+  printf("\n");
+
+  printf("table:\n");
+  for(i=0; i<ht->num_buckets; i++)
+  {
+    printf("%.8X: ", i);
+    for(cur=ht->table[i]; cur != NULL; cur=cur->next)
+    {
+      /*      write(STDOUT_FILENO, cur->index, cur->index_len);*/
+      printf("%p", (void*)cur);
+      printf("|");
+
+      if(cur->next == ht->table[i])
+      {
+	printf("??? Loop in table chain!!");
+	break;
+      }
+    }
+    printf("\n");
+  }
+}
+#endif
+
+lru_cache* lru_cache_create(uint32_t max_keys, uint32_t secret, bool free_data)
+{
+  lru_cache* ret_val;
+
+  ret_val = (lru_cache*)malloc(sizeof(lru_cache));
+  if(ret_val == NULL)
+    return NULL;
+
+  if(max_keys == 0)
+    ret_val->num_buckets = 2048;
+  else
+  {
+    ret_val->num_buckets = max_keys/lru_cache_floor_log2(max_keys);
+    if(ret_val->num_buckets < 1)
+      ret_val->num_buckets = 1;
+  }
+
+  ret_val->table 
+    = (lru_cache_element**)malloc(sizeof(lru_cache_element*) 
+				  * ret_val->num_buckets);
+  if(ret_val->table == NULL)
+  {
+    free(ret_val);
+    return NULL;
+  }
+  
+  ret_val->oldest = NULL;
+  ret_val->newest = NULL;
+  ret_val->max_keys = max_keys;
+  ret_val->secret = secret;
+  ret_val->free_data = free_data;
+  ret_val->num_keys = 0;
+  memset(ret_val->table, 0, ret_val->num_buckets*sizeof(lru_cache_element*));
+
+  return ret_val;
+}
+
+
+void lru_cache_destroy(lru_cache* ht)
+{
+  lru_cache_element* cur;
+  lru_cache_element* last = NULL;
+  
+  for(cur=ht->oldest; cur != NULL; last=cur,cur=cur->newer)
+  {
+    if(last != NULL)
+    {
+      if(ht->free_data)
+	free(last->data);
+      free(last->index);
+      free(last);
+    }
+  }
+  free(ht->table);
+  ht->secret = 0;
+  free(ht);
+}
+
+
+
+bool lru_cache_update(lru_cache* ht, const void* index, 
+		      uint32_t index_len, void* data)
+{
+  uint32_t hash, lru_hash;
+  lru_cache_element* cur;
+  lru_cache_element* last = NULL;
+  lru_cache_element* e = NULL;
+  void* tmp_index;
+
+  hash = lru_cache_compute_hash(ht->num_buckets, ht->secret, index, index_len);
+  for(cur = ht->table[hash]; cur != NULL && e == NULL; cur=cur->next)
+  {
+    if((index_len == cur->index_len) 
+       && memcmp(cur->index, index, index_len) == 0)
+    { e = cur; }
+  }
+  
+  if(e != NULL)
+  { /* We found the index, so we're going to overwrite the data.
+     * We also need to reposition the element to the newest position,
+     * so remove it from the list for now.
+     */
+    if(ht->free_data)
+      free(e->data);
+
+    if(e->newer == NULL)
+      ht->newest = e->older;
+    else
+      e->newer->older = e->older;
+
+    if(e->older == NULL)
+      ht->oldest = e->newer;
+    else
+      e->older->newer = e->newer;
+  }
+  else
+  { /* We didn't find an identical index. */
+    
+    if((ht->max_keys != 0) && (ht->num_keys >= ht->max_keys))
+    { /* Eliminate the least recently used item, but reuse the element
+       * structure to minimize reallocation. 
+       */
+      e = ht->oldest;
+      if(ht->newest == ht->oldest)
+      {
+	ht->newest = NULL;
+	ht->oldest = NULL;
+      }
+      else
+      {
+	ht->oldest = e->newer;
+	e->newer->older = NULL;
+      }
+      e->newer = NULL;
+      e->older = NULL;
+
+      last = NULL;
+      lru_hash = lru_cache_compute_hash(ht->num_buckets, ht->secret, 
+					e->index, e->index_len);
+      for(cur = ht->table[lru_hash]; cur != e && cur != NULL; 
+	  last=cur, cur=cur->next)
+      {	continue; }
+
+      if(last == NULL)
+	ht->table[lru_hash] = e->next;
+      else
+	last->next = e->next;
+      e->next = NULL;
+
+      if(ht->free_data)
+	free(e->data);
+
+      tmp_index = realloc(e->index, index_len);
+      if(tmp_index == NULL)
+      {
+	free(e->index);
+	free(e);
+	return false;
+      }
+    }
+    else
+    { /* Brand new element because we have room to spare. */
+
+      e = (lru_cache_element*)malloc(sizeof(lru_cache_element));
+      if(e == NULL)
+	return false;
+      
+      e->index = malloc(index_len);
+      if(e->index == NULL)
+      {
+	free(e);
+	return false;
+      }
+      
+      /* New entry, increment counters. */
+      ht->num_keys++;
+    }
+    memcpy(e->index, index, index_len);
+    e->index_len = index_len;
+
+    /* Insert at beginning of chain, in a vaguely LRU style */
+    e->next = ht->table[hash];
+    ht->table[hash] = e;
+  }
+  e->data = data;
+
+  /* Finally, let's insert the element to the newest position in the LRU list.*/
+  if(ht->newest != NULL)
+    ht->newest->newer = e;
+  e->newer = NULL;
+  e->older = ht->newest;
+  ht->newest = e;
+  if(ht->oldest == NULL)
+    ht->oldest = e;
+
+  return true;
+}
+
+
+void* lru_cache_find(lru_cache* ht, const void* index,
+		     uint32_t index_len)
+{
+  uint32_t hash;
+  lru_cache_element* cur;
+
+  hash = lru_cache_compute_hash(ht->num_buckets, ht->secret, index, index_len);
+  for(cur = ht->table[hash]; (cur != NULL); cur = cur->next)
+  {
+    if((index_len == cur->index_len)
+       && memcmp(cur->index, index, index_len) == 0)
+    { break; }
+  }
+  
+  if(cur != NULL && cur->newer != NULL)
+  { /* Need to move this element up to the newest slot. */
+
+    cur->newer->older = cur->older;
+
+    if(cur->older == NULL)
+      ht->oldest = cur->newer;
+    else
+      cur->older->newer = cur->newer;
+
+    cur->newer = NULL;
+    cur->older = ht->newest;
+    ht->newest->newer = cur;
+    ht->newest = cur;
+  }
+
+  if(cur != NULL)
+    return cur->data;
+  else
+    return NULL;
+}
+
+
+
+bool lru_cache_remove(lru_cache* ht, const void* index, 
+		      uint32_t index_len)
+{
+  uint32_t hash;
+  lru_cache_element* cur;
+  lru_cache_element* last = NULL;
+
+  hash = lru_cache_compute_hash(ht->num_buckets, ht->secret,
+				index, index_len);
+  for(cur=ht->table[hash]; (cur != NULL);
+      last=cur, cur=cur->next)
+  {
+    if((index_len == cur->index_len) 
+       && memcmp(cur->index, index, index_len) == 0)
+    { break; }
+  }
+
+  if(cur == NULL)
+    return false;
+
+  if(ht->free_data)
+    free(cur->data);
+
+  /* Detach from list */
+  if(cur->newer == NULL)
+    ht->newest = cur->older;
+  else
+    cur->newer->older = cur->older;
+  
+  if(cur->older == NULL)
+    ht->oldest = cur->newer;
+  else
+    cur->older->newer = cur->newer;
+
+  /* Detach from hash table */
+  if(last == NULL)
+    ht->table[hash] = cur->next;
+  else
+    last->next = cur->next;
+
+  free(cur->index);
+  free(cur);
+  
+  /* Removing entry, decrement counters. */
+  ht->num_keys--;
+  
+  return true;
+}
diff --git a/lib/range_list.c b/lib/range_list.c
new file mode 100644
index 0000000..e874cff
--- /dev/null
+++ b/lib/range_list.c
@@ -0,0 +1,348 @@
+/*
+ * Copyright (C) 2008 Timothy D. Morgan
+ *
+ * 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
+ * the Free Software Foundation; version 3 of the License.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
+ *
+ * $Id: range_list.c 122 2008-08-09 20:24:01Z tim $
+ */
+
+#include <math.h>
+#include "../include/range_list.h"
+
+
+/*******************/
+/* Private symbols */
+/*******************/
+#define RANGE_LIST_ALLOC_SIZE 256
+
+#if 0
+#include <stdio.h>
+static void range_list_print(const range_list* rl)
+{
+  uint32_t i;
+  for(i=0; i<rl->size; i++)
+    fprintf(stderr, " %d=%p,%d,%d,%p", i, (void*)rl->elements[i],
+	    rl->elements[i]->offset, rl->elements[i]->length, 
+	    rl->elements[i]->data);
+  fprintf(stderr, "\n");
+}
+#endif
+
+/*
+ * Inserts elem into rl at the specified index and updates rl->size. 
+ * Memory reallocation of rl->elements is handled when necessary, and
+ * rl->elem_alloced is updated in this case..  Returns false if memory
+ * could not be allocated.  
+ */
+static bool range_list_insert(range_list* rl, range_list_element* elem, uint32_t index)
+{
+  uint32_t i;
+  range_list_element** tmp;
+
+  if(rl->size == rl->elem_alloced)
+  {
+    tmp = (range_list_element**)realloc(rl->elements, 
+					(rl->elem_alloced+RANGE_LIST_ALLOC_SIZE)
+					* sizeof(range_list_element*));
+    if(tmp == NULL)
+      return false;
+    rl->elements = tmp;
+    rl->elem_alloced += RANGE_LIST_ALLOC_SIZE;
+  }
+
+  /* Do the shuffle to the right. */
+  for(i=rl->size; i > index; i--)
+    rl->elements[i] = rl->elements[i-1];
+  rl->elements[index] = elem;
+
+  rl->size++;
+  return true;
+}
+
+/*
+ * Finds the element with the closest offset to that provided, such that
+ * the element's offset <= the provided offset.  If no such element
+ * exists, this returns -1 which indicates that the provided offset
+ * appears before all elements.
+ */
+static int32_t range_list_find_previous(const range_list* rl, uint32_t offset)
+{
+  uint32_t h_idx, l_idx, cur_idx;
+  uint32_t h_val, l_val;
+  range_list_element* cur_elem;
+
+  if((rl->size == 0) || (offset < rl->elements[0]->offset))
+    return -1;
+
+  if(offset >= rl->elements[rl->size-1]->offset)
+    return rl->size-1;
+
+  h_idx = rl->size-1;
+  l_idx = 0;
+  while(h_idx != l_idx)
+  {
+    h_val = rl->elements[h_idx]->offset + rl->elements[h_idx]->length;
+    l_val = rl->elements[l_idx]->offset;
+    /* Make an educated guess as to the "middle" index based on the
+     * ratios of the offset and high/low values.
+     */
+    cur_idx = (uint32_t)ceil((((double)offset-l_val)/(h_val-l_val))*(h_idx-l_idx));
+    if(cur_idx > h_idx)
+      cur_idx = h_idx;
+    if(cur_idx < l_idx)
+      cur_idx = l_idx;
+    cur_elem = rl->elements[cur_idx];
+
+    if((offset >= cur_elem->offset) && (offset < rl->elements[cur_idx+1]->offset))
+      return cur_idx;
+    
+    if(offset < cur_elem->offset)
+      h_idx = cur_idx-1;
+    else
+      l_idx = cur_idx+1;
+  }
+
+  return h_idx;
+}
+
+
+/******************/
+/* Public symbols */
+/******************/
+range_list* range_list_new()
+{
+  range_list* rl;
+
+  rl = (range_list*)malloc(sizeof(range_list));
+  if(rl == NULL)
+    return NULL;
+
+  rl->elements = (range_list_element**)malloc(sizeof(range_list_element*)
+					      * RANGE_LIST_ALLOC_SIZE);
+
+  if(rl->elements == NULL)
+  {
+    free(rl);
+    return NULL;
+  }
+
+  rl->elem_alloced = RANGE_LIST_ALLOC_SIZE;
+  rl->size = 0;
+
+  return rl;
+}
+
+
+void range_list_free(range_list* rl)
+{
+  uint32_t i;
+
+  if(rl == NULL)
+    return;
+
+  for(i=0; i < rl->size; i++)
+    free(rl->elements[i]);
+
+  free(rl->elements);
+  free(rl);
+}
+
+
+uint32_t range_list_size(const range_list* rl)
+{
+  return rl->size;
+}
+
+
+
+bool range_list_add(range_list* rl, uint32_t offset, uint32_t length, void* data)
+{
+  uint32_t insert_index;
+  range_list_element* elem;
+  range_list_element* prev_elem;
+  /*fprintf(stderr, "DEBUG: rl->size=%d\n", rl->size);*/
+  /* Sorry, limited to 2**31-1 elements. */
+  if(rl->size >= 0x7FFFFFFF)
+    return false;
+
+  /* 0-length ranges aren't allowed. */
+  if(length == 0)
+    return false;
+  
+  /* Check for integer overflows */
+  if((uint32_t)(offset+length) < offset || (uint32_t)(offset+length) < length)
+    return false;
+
+  /* Find insertion point and validate there are no overlaps */
+  insert_index = range_list_find_previous(rl, offset)+1;
+  
+  /* Does the previous element overlap with this one? */
+  if(insert_index > 0)
+  {
+    prev_elem = rl->elements[insert_index-1];
+    if(offset < prev_elem->length + prev_elem->offset)
+      return false;
+  }
+
+  /* Does this new element overlap with the next one? */
+  if((insert_index+1 < rl->size) 
+     && (offset+length > rl->elements[insert_index+1]->offset))
+    return false;
+
+  elem = (range_list_element*)malloc(sizeof(range_list_element));
+  if(elem == NULL)
+    return false;
+  elem->offset = offset;
+  elem->length = length;
+  elem->data = data;
+  
+  if(!range_list_insert(rl, elem, insert_index))
+  {
+    free(elem);
+    return false;
+  }
+
+  return true;
+}
+
+
+bool range_list_remove(range_list* rl, uint32_t index)
+{
+  uint32_t i;
+  range_list_element** tmp;
+
+  if(index >= rl->size)
+    return false;
+
+  free(rl->elements[index]);
+
+  /* Do the shuffle to the left. */
+  for(i=index; i < (rl->size-1); i++)
+    rl->elements[i] = rl->elements[i+1];
+  rl->elements[rl->size-1] = NULL;
+  rl->size--;
+
+  /* Try to keep memory usage down */
+  if(rl->size + 2 * RANGE_LIST_ALLOC_SIZE  < rl->elem_alloced)
+  {
+    tmp = (range_list_element**)realloc(rl->elements, 
+					(rl->elem_alloced-2*RANGE_LIST_ALLOC_SIZE)
+					* sizeof(range_list_element*));
+    if(tmp != NULL)
+    {
+      rl->elements = tmp;
+      rl->elem_alloced -= 2*RANGE_LIST_ALLOC_SIZE;
+    }
+  }
+
+  return true;
+}
+
+
+const range_list_element* range_list_get(const range_list* rl, uint32_t index)
+{
+  if(index >= rl->size)
+    return NULL;
+
+  return rl->elements[index];
+}
+
+
+int32_t range_list_find(const range_list* rl, uint32_t offset)
+{
+  uint32_t prev_idx;
+  range_list_element* elem;
+
+  if((offset < rl->elements[0]->offset)
+     || (offset > rl->elements[rl->size-1]->offset 
+	 + rl->elements[rl->size-1]->length))
+    return -1;
+
+  prev_idx = range_list_find_previous(rl, offset);
+  elem = rl->elements[prev_idx];
+  if(offset < elem->offset+elem->length)
+    return prev_idx;
+
+  return -2;
+}
+
+
+void* range_list_find_data(const range_list* rl, uint32_t offset)
+{
+  int32_t index = range_list_find(rl, offset);
+  if(index < 0)
+    return NULL;
+
+  return rl->elements[index]->data;
+}
+
+
+bool range_list_split_element(range_list* rl, uint32_t index, uint32_t offset)
+{
+  range_list_element* cur_elem;
+  range_list_element* new_elem;
+
+  if(index >= rl->size)
+    return false;
+
+  cur_elem = rl->elements[index];
+  if((offset <= cur_elem->offset) 
+     || (offset >= cur_elem->offset+cur_elem->length))
+    return false;
+
+  new_elem = (range_list_element*)malloc(sizeof(range_list_element));
+  if(new_elem == NULL)
+    return false;
+  
+  new_elem->offset = offset;
+  new_elem->length = cur_elem->offset + cur_elem->length - offset;
+  new_elem->data = cur_elem->data;
+  
+  if(!range_list_insert(rl, new_elem, index+1))
+  {
+    free(new_elem);
+    return false;
+  }
+
+  cur_elem->length = new_elem->offset - cur_elem->offset;
+
+  return true;
+}
+
+
+bool range_list_has_range(range_list* rl, uint32_t start, uint32_t length)
+{
+  int32_t idx1, idx2;
+
+  idx1 = range_list_find(rl, start);
+  if(idx1 < 0)
+    return false;
+
+  idx2 = range_list_find(rl, start+length);
+  if(idx2 < 0)
+    return false;
+
+  if(idx1 == idx2)
+    return true;
+
+  while(idx1 != idx2)
+  {
+    if(rl->elements[idx1]->offset + rl->elements[idx1]->length 
+       != rl->elements[idx1+1]->offset)
+      return false;
+    idx1++;
+  }
+
+  return true;
+}
diff --git a/lib/regfi.c b/lib/regfi.c
index 872f941..2736c84 100644
--- a/lib/regfi.c
+++ b/lib/regfi.c
@@ -5,12 +5,12 @@
  * Unix SMB/CIFS implementation.
  * Windows NT registry I/O library
  *
- * Copyright (C) 2005-2007 Timothy D. Morgan
+ * Copyright (C) 2005-2008 Timothy D. Morgan
  * Copyright (C) 2005 Gerald (Jerry) Carter
  *
  * 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
- * the Free Software Foundation; version 2 of the License.
+ * the Free Software Foundation; version 3 of the License.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -21,7 +21,7 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
  *
- * $Id: regfi.c 96 2007-03-29 01:41:33Z tim $
+ * $Id: regfi.c 116 2008-08-03 19:34:27Z tim $
  */
 
 #include "../include/regfi.h"
@@ -336,1107 +336,616 @@ char* regfi_get_group(SEC_DESC *sec_desc)
 }
 
 
-
-/*******************************************************************
- *******************************************************************/
-static int read_block( REGF_FILE *file, prs_struct *ps, uint32 file_offset, 
-		       uint32 block_size )
+/*****************************************************************************
+ * This function is just like read(2), except that it continues to
+ * re-try reading from the file descriptor if EINTR or EAGAIN is received.  
+ * regfi_read will attempt to read length bytes from fd and write them to buf.
+ *
+ * On success, 0 is returned.  Upon failure, an errno code is returned.
+ *
+ * The number of bytes successfully read is returned through the length 
+ * parameter by reference.  If both the return value and length parameter are 
+ * returned as 0, then EOF was encountered immediately
+ *****************************************************************************/
+uint32 regfi_read(int fd, uint8* buf, uint32* length)
 {
-  const int hdr_size = 0x20;
-  int bytes_read, returned;
-  char *buffer;
-  SMB_STRUCT_STAT sbuf;
-
-  /* check for end of file */
-
-  if ( fstat( file->fd, &sbuf ) ) {
-    /*DEBUG(0,("read_block: stat() failed! (%s)\n", strerror(errno)));*/
-    return -1;
-  }
-
-  if ( (size_t)file_offset >= sbuf.st_size )
-    return -1;
-	
-  /* if block_size == 0, we are parsnig HBIN records and need 
-     to read some of the header to get the block_size from there */
-	   
-  if ( block_size == 0 ) {
-    uint8 hdr[0x20];
-
-    if ( lseek( file->fd, file_offset, SEEK_SET ) == -1 ) {
-      /*DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno) ));*/
-      return -1;
-    }
-
-    bytes_read = returned = 0;
-    while (bytes_read < hdr_size)
-    {
-      returned = read(file->fd, hdr + bytes_read, hdr_size - bytes_read);
-      if(returned == -1 && errno != EINTR && errno != EAGAIN)
-      {
-	/*DEBUG(0,("read_block: read of hdr failed (%s)\n",strerror(errno)));*/
-	return -1;
-      }
-
-      if(returned == 0)
-	return -1;
+  uint32 rsize = 0;
+  uint32 rret = 0;
 
-      bytes_read += returned;
-    }
-
-    /* make sure this is an hbin header */
-
-    if ( strncmp( (char*)hdr, "hbin", HBIN_HDR_SIZE ) != 0 ) {
-      /*DEBUG(0,("read_block: invalid block header!\n"));*/
-      return -1;
-    }
-
-    block_size = IVAL( hdr, 0x08 );
-  }
-
-  /*DEBUG(10,("read_block: block_size == 0x%x\n", block_size ));*/
-
-  /* set the offset, initialize the buffer, and read the block from disk */
-
-  if ( lseek( file->fd, file_offset, SEEK_SET ) == -1 ) {
-    /*DEBUG(0,("read_block: lseek() failed! (%s)\n", strerror(errno) ));*/
-    return -1;
-  }
-	
-  prs_init( ps, block_size, file->mem_ctx, UNMARSHALL );
-  buffer = ps->data_p;
-  bytes_read = returned = 0;
-
-  while ( bytes_read < block_size ) 
+  do
   {
-    returned = read(file->fd, buffer+bytes_read, block_size-bytes_read);
-    if(returned == -1 && errno != EINTR && errno != EAGAIN)
-    {
-      /*DEBUG(0,("read_block: read() failed (%s)\n", strerror(errno) ));*/
-      return -1;
-    }
-
-    if ((returned == 0) && (bytes_read < block_size)) 
-    {
-      /*DEBUG(0,("read_block: not a vald registry file ?\n" ));*/
-      return -1;
-    }	
+    rret = read(fd, buf + rsize, *length - rsize);
+    if(rret > 0)
+      rsize += rret;
+  }while(*length - rsize > 0 
+         && (rret > 0 || (rret == -1 && (errno == EAGAIN || errno == EINTR))));
+  
+  *length = rsize;
+  if (rret == -1 && errno != EINTR && errno != EAGAIN)
+    return errno;
 
-    bytes_read += returned;
-  }
-	
-  return bytes_read;
+  return 0;
 }
 
 
-/*******************************************************************
- *******************************************************************/
-static bool prs_regf_block(const char *desc, prs_struct *ps, 
-			   int depth, REGF_FILE *file)
-{
-  depth++;
-	
-  if(!prs_uint8s("header", ps, depth, file->header, sizeof(file->header)))
-    return false;
-	
-  /* yes, these values are always identical so store them only once */
-	
-  if ( !prs_uint32( "unknown1", ps, depth, &file->unknown1 ))
-    return false;
-  if ( !prs_uint32( "unknown1 (again)", ps, depth, &file->unknown1 ))
-    return false;
-
-  /* get the modtime */
-	
-  if ( !prs_set_offset( ps, 0x0c ) )
-    return false;
-  if ( !smb_io_time( "modtime", &file->mtime, ps, depth ) )
-    return false;
-
-  /* constants */
-	
-  if ( !prs_uint32( "unknown2", ps, depth, &file->unknown2 ))
-    return false;
-  if ( !prs_uint32( "unknown3", ps, depth, &file->unknown3 ))
-    return false;
-  if ( !prs_uint32( "unknown4", ps, depth, &file->unknown4 ))
-    return false;
-  if ( !prs_uint32( "unknown5", ps, depth, &file->unknown5 ))
-    return false;
-
-  /* get file offsets */
-	
-  if ( !prs_set_offset( ps, 0x24 ) )
-    return false;
-  if ( !prs_uint32( "data_offset", ps, depth, &file->data_offset ))
-    return false;
-  if ( !prs_uint32( "last_block", ps, depth, &file->last_block ))
-    return false;
-		
-  /* one more constant */
-	
-  if ( !prs_uint32( "unknown6", ps, depth, &file->unknown6 ))
-    return false;
-		
-  /* get the checksum */
-	
-  if ( !prs_set_offset( ps, 0x01fc ) )
-    return false;
-  if ( !prs_uint32( "checksum", ps, depth, &file->checksum ))
-    return false;
-	
-  return true;
-}
-
-
-/*******************************************************************
- *******************************************************************/
-static bool prs_hbin_block(const char *desc, prs_struct *ps, 
-			   int depth, REGF_HBIN *hbin)
+/*****************************************************************************
+ *
+ *****************************************************************************/
+bool regfi_parse_cell(int fd, uint32 offset, uint8* hdr, uint32 hdr_len,
+		      uint32* cell_length, bool* unalloc)
 {
-  uint32 block_size2;
+  uint32 length;
+  int32 raw_length;
+  uint8 tmp[4];
 
-  depth++;
-	
-  if(!prs_uint8s("header", ps, depth, hbin->header, sizeof(hbin->header)))
-    return false;
-
-  if ( !prs_uint32( "first_hbin_off", ps, depth, &hbin->first_hbin_off ))
-    return false;
-
-  /* The dosreg.cpp comments say that the block size is at 0x1c.
-     According to a WINXP NTUSER.dat file, this is wrong.  The block_size
-     is at 0x08 */
-
-  if ( !prs_uint32( "block_size", ps, depth, &hbin->block_size ))
-    return false;
-
-  block_size2 = hbin->block_size;
-  prs_set_offset( ps, 0x1c );
-  if ( !prs_uint32( "block_size2", ps, depth, &block_size2 ))
-    return false;
-
-  if ( !ps->io )
-    hbin->dirty = true;
-	
-
-  return true;
-}
-
-
-/*******************************************************************
- *******************************************************************/
-static bool prs_nk_rec( const char *desc, prs_struct *ps, 
-			int depth, REGF_NK_REC *nk )
-{
-  uint16 class_length, name_length;
-  uint32 start;
-  uint32 data_size, start_off, end_off;
-  uint32 unknown_off = REGF_OFFSET_NONE;
-
-  nk->hbin_off = ps->data_offset;
-  start = nk->hbin_off;
-	
-  depth++;
-	
-  /* back up and get the data_size */	
-  if ( !prs_set_offset( ps, ps->data_offset-sizeof(uint32)) )
-    return false;
-  start_off = ps->data_offset;
-  if ( !prs_uint32( "rec_size", ps, depth, &nk->rec_size ))
-    return false;
-	
-  if (!prs_uint8s("header", ps, depth, nk->header, sizeof(nk->header)))
-    return false;
-		
-  if ( !prs_uint16( "key_type", ps, depth, &nk->key_type ))
-    return false;
-  if ( !smb_io_time( "mtime", &nk->mtime, ps, depth ))
-    return false;
-		
-  if ( !prs_set_offset( ps, start+0x0010 ) )
-    return false;
-  if ( !prs_uint32( "parent_off", ps, depth, &nk->parent_off ))
-    return false;
-  if ( !prs_uint32( "num_subkeys", ps, depth, &nk->num_subkeys ))
-    return false;
-		
-  if ( !prs_set_offset( ps, start+0x001c ) )
-    return false;
-  if ( !prs_uint32( "subkeys_off", ps, depth, &nk->subkeys_off ))
-    return false;
-  if ( !prs_uint32( "unknown_off", ps, depth, &unknown_off) )
-    return false;
-		
-  if ( !prs_set_offset( ps, start+0x0024 ) )
-    return false;
-  if ( !prs_uint32( "num_values", ps, depth, &nk->num_values ))
-    return false;
-  if ( !prs_uint32( "values_off", ps, depth, &nk->values_off ))
-    return false;
-  if ( !prs_uint32( "sk_off", ps, depth, &nk->sk_off ))
-    return false;
-  if ( !prs_uint32( "classname_off", ps, depth, &nk->classname_off ))
+  if(lseek(fd, offset, SEEK_SET) == -1)
     return false;
 
-  if (!prs_uint32("max_bytes_subkeyname", ps, depth, &nk->max_bytes_subkeyname))
-    return false;
-  if ( !prs_uint32( "max_bytes_subkeyclassname", ps, 
-		    depth, &nk->max_bytes_subkeyclassname))
-  { return false; }
-  if ( !prs_uint32( "max_bytes_valuename", ps, depth, &nk->max_bytes_valuename))
-    return false;
-  if ( !prs_uint32( "max_bytes_value", ps, depth, &nk->max_bytes_value))
-    return false;
-  if ( !prs_uint32( "unknown index", ps, depth, &nk->unk_index))
+  length = 4;
+  if((regfi_read(fd, tmp, &length) != 0) || length != 4)
     return false;
+  raw_length = IVALS(tmp, 0);
 
-  name_length = nk->keyname ? strlen(nk->keyname) : 0 ;
-  class_length = nk->classname ? strlen(nk->classname) : 0 ;
-  if ( !prs_uint16( "name_length", ps, depth, &name_length ))
-    return false;
-  if ( !prs_uint16( "class_length", ps, depth, &class_length ))
-    return false;	
-		
-  if ( class_length ) 
+  if(raw_length < 0)
   {
-    /* XXX: why isn't this parsed? */
-    ;;
+    (*cell_length) = raw_length*(-1);
+    (*unalloc) = false;
   }
-	
-  if ( name_length ) 
+  else
   {
-    if(ps->io && !(nk->keyname = (char*)zcalloc(sizeof(char), name_length+1)))
-	return false;
-
-    if(!prs_uint8s("name", ps, depth, (uint8*)nk->keyname, name_length))
-      return false;
-
-    if(ps->io)
-      nk->keyname[name_length] = '\0';
+    (*cell_length) = raw_length;
+    (*unalloc) = true;
   }
 
-  end_off = ps->data_offset;
-
-  /* data_size must be divisible by 8 and large enough to hold 
-     the original record */
+  if(*cell_length - 4 < hdr_len)
+    return false;
 
-  data_size = ((start_off - end_off) & 0xfffffff8 );
-  /*if ( data_size > nk->rec_size )
-      DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, nk->rec_size));*/
+  if(hdr_len > 0)
+  {
+    length = hdr_len;
+    if((regfi_read(fd, hdr, &length) != 0) || length != hdr_len)
+      return false;
+  }
 
-  if ( !ps->io )
-    nk->hbin->dirty = true;
-  
   return true;
 }
 
 
 /*******************************************************************
+ * Given an offset and an hbin, is the offset within that hbin?
+ * The offset is a virtual file offset.
  *******************************************************************/
-static uint32 regf_block_checksum( prs_struct *ps )
+static bool regfi_offset_in_hbin(REGF_HBIN* hbin, uint32 offset)
 {
-  char *buffer = ps->data_p;
-  uint32 checksum, x;
-  int i;
+  if(!hbin)
+    return false;
 
-  /* XOR of all bytes 0x0000 - 0x01FB */
+  if((offset > hbin->first_hbin_off) 
+     && (offset < (hbin->first_hbin_off + hbin->block_size)))
+    return true;
 		
-  checksum = x = 0;
-	
-  for ( i=0; i<0x01FB; i+=4 ) {
-    x = IVAL(buffer, i );
-    checksum ^= x;
-  }
-	
-  return checksum;
+  return false;
 }
 
 
+
 /*******************************************************************
+ * Given a virtual offset, and receive the correpsonding HBIN 
+ * block for it.  NULL if one doesn't exist.
  *******************************************************************/
-static bool read_regf_block( REGF_FILE *file )
+REGF_HBIN* regfi_lookup_hbin(REGF_FILE* file, uint32 offset)
 {
-  prs_struct ps;
-  uint32 checksum;
-	
-  /* grab the first block from the file */
-		
-  if ( read_block( file, &ps, 0, REGF_BLOCKSIZE ) == -1 )
-    return false;
-	
-  /* parse the block and verify the checksum */
-	
-  if ( !prs_regf_block( "regf_header", &ps, 0, file ) )
-    return false;	
-		
-  checksum = regf_block_checksum( &ps );
-	
-  if(ps.is_dynamic)
-    SAFE_FREE(ps.data_p);
-  ps.is_dynamic = false;
-  ps.buffer_size = 0;
-  ps.data_offset = 0;
-
-  if ( file->checksum !=  checksum ) {
-    /*DEBUG(0,("read_regf_block: invalid checksum\n" ));*/
-    return false;
-  }
-
-  return true;
+  return (REGF_HBIN*)range_list_find_data(file->hbins, offset+REGF_BLOCKSIZE);
 }
 
 
+
 /*******************************************************************
  *******************************************************************/
-static REGF_HBIN* read_hbin_block( REGF_FILE *file, off_t offset )
+REGF_HASH_LIST* regfi_load_hashlist(REGF_FILE* file, uint32 offset, 
+				    uint32 num_keys, uint32 max_size, 
+				    bool strict)
 {
-  REGF_HBIN *hbin;
-  uint32 record_size, curr_off, block_size, header;
-	
-  if ( !(hbin = (REGF_HBIN*)zalloc(sizeof(REGF_HBIN))) ) 
-    return NULL;
-  hbin->file_off = offset;
-  hbin->free_off = -1;
-		
-  if ( read_block( file, &hbin->ps, offset, 0 ) == -1 )
+  REGF_HASH_LIST* ret_val;
+  uint32 i, cell_length, length;
+  uint8* hashes;
+  uint8 buf[REGFI_HASH_LIST_MIN_LENGTH];
+  bool unalloc;
+
+  if(!regfi_parse_cell(file->fd, offset, buf, REGFI_HASH_LIST_MIN_LENGTH, 
+		       &cell_length, &unalloc))
     return NULL;
-	
-  if ( !prs_hbin_block( "hbin", &hbin->ps, 0, hbin ) )
-    return NULL;	
-
-  /* this should be the same thing as hbin->block_size but just in case */
-
-  block_size = hbin->ps.buffer_size;
-
-  /* Find the available free space offset.  Always at the end,
-     so walk the record list and stop when you get to the end.
-     The end is defined by a record header of 0xffffffff.  The 
-     previous 4 bytes contains the amount of free space remaining 
-     in the hbin block. */
-
-  /* remember that the record_size is in the 4 bytes preceeding the record itself */
 
-  if ( !prs_set_offset( &hbin->ps, file->data_offset+HBIN_HDR_SIZE-sizeof(uint32) ) )
-    return false;
-
-  record_size = 0;
-  curr_off = hbin->ps.data_offset;
-  while ( header != 0xffffffff ) {
-    /* not done yet so reset the current offset to the 
-       next record_size field */
-
-    curr_off = curr_off+record_size;
+  ret_val = (REGF_HASH_LIST*)zalloc(sizeof(REGF_HASH_LIST));
+  if(ret_val == NULL)
+    return NULL;
 
-    /* for some reason the record_size of the last record in
-       an hbin block can extend past the end of the block
-       even though the record fits within the remaining 
-       space....aaarrrgggghhhhhh */
+  ret_val->offset = offset;
+  if(cell_length > max_size)
+  {
+    if(strict)
+      return NULL;
+    cell_length = max_size & 0xFFFFFFF8;
+  }
+  ret_val->cell_size = cell_length;
 
-    if ( curr_off >= block_size ) {
-      record_size = -1;
-      curr_off = -1;
-      break;
-    }
+  if((buf[0] != 'l' || buf[1] != 'f') && (buf[0] != 'l' || buf[1] != 'h')
+     && (buf[0] != 'r' || buf[1] != 'i'))
+  {
+    /*printf("DEBUG: lf->header=%c%c\n", buf[0], buf[1]);*/
+    free(ret_val);
+    return NULL;
+  }
 
-    if ( !prs_set_offset( &hbin->ps, curr_off) )
-      return false;
+  if(buf[0] == 'r' && buf[1] == 'i')
+  {
+    fprintf(stderr, "WARNING: ignoring encountered \"ri\" record.\n");
+    free(ret_val);
+    return NULL;
+  }
 
-    if ( !prs_uint32( "rec_size", &hbin->ps, 0, &record_size ) )
-      return false;
-    if ( !prs_uint32( "header", &hbin->ps, 0, &header ) )
-      return false;
-		
-    assert( record_size != 0 );
+  ret_val->magic[0] = buf[0];
+  ret_val->magic[1] = buf[1];
 
-    if ( record_size & 0x80000000 ) {
-      /* absolute_value(record_size) */
-      record_size = (record_size ^ 0xffffffff) + 1;
+  ret_val->num_keys = SVAL(buf, 0x2);
+  if(num_keys != ret_val->num_keys)
+  {
+    if(strict)
+    {
+      free(ret_val);
+      return NULL;
     }
+    /* XXX: Not sure which should be authoritative, the number from the 
+     *      NK record, or the number in the hash list.  Go with the larger
+     *      of the two to ensure all keys are found.  Note the length checks
+     *      on the cell later ensure that there won't be any critical errors.
+     */
+    if(num_keys < ret_val->num_keys)
+      num_keys = ret_val->num_keys;
+    else
+      ret_val->num_keys = num_keys;
   }
 
-  /* save the free space offset */
-
-  if ( header == 0xffffffff ) {
-
-    /* account for the fact that the curr_off is 4 bytes behind the actual 
-       record header */
+  if(cell_length - REGFI_HASH_LIST_MIN_LENGTH - sizeof(uint32) 
+     < ret_val->num_keys*sizeof(REGF_HASH_LIST_ELEM))
+    return NULL;
 
-    hbin->free_off = curr_off + sizeof(uint32);
-    hbin->free_size = record_size;
+  length = sizeof(REGF_HASH_LIST_ELEM)*ret_val->num_keys;
+  ret_val->hashes = (REGF_HASH_LIST_ELEM*)zalloc(length);
+  if(ret_val->hashes == NULL)
+  {
+    free(ret_val);
+    return NULL;
   }
 
-  /*DEBUG(10,("read_hbin_block: free space offset == 0x%x\n", hbin->free_off));*/
-
-  if ( !prs_set_offset( &hbin->ps, file->data_offset+HBIN_HDR_SIZE )  )
-    return false;
-	
-  return hbin;
-}
-
-
-/*******************************************************************
- Input a randon offset and receive the correpsonding HBIN 
- block for it
-*******************************************************************/
-static bool hbin_contains_offset( REGF_HBIN *hbin, uint32 offset )
-{
-  if ( !hbin )
-    return false;
-	
-  if ( (offset > hbin->first_hbin_off) && (offset < (hbin->first_hbin_off+hbin->block_size)) )
-    return true;
-		
-  return false;
-}
-
-
-/*******************************************************************
- Input a randon offset and receive the correpsonding HBIN 
- block for it
-*******************************************************************/
-static REGF_HBIN* lookup_hbin_block( REGF_FILE *file, uint32 offset )
-{
-  REGF_HBIN *hbin = NULL;
-  uint32 block_off;
-
-  /* start with the open list */
-
-  for ( hbin=file->block_list; hbin; hbin=hbin->next ) {
-    /* DEBUG(10,("lookup_hbin_block: address = 0x%x [0x%x]\n", hbin->file_off, (uint32)hbin ));*/
-    if ( hbin_contains_offset( hbin, offset ) )
-      return hbin;
+  hashes = (uint8*)zalloc(length);
+  if(hashes == NULL)
+  {
+    free(ret_val->hashes);
+    free(ret_val);
+    return NULL;
   }
-	
-  if ( !hbin ) {
-    /* start at the beginning */
 
-    block_off = REGF_BLOCKSIZE;
-    do {
-      /* cleanup before the next round */
-      if ( hbin )
-      {
-	if(hbin->ps.is_dynamic)
-	  SAFE_FREE(hbin->ps.data_p);
-	hbin->ps.is_dynamic = false;
-	hbin->ps.buffer_size = 0;
-	hbin->ps.data_offset = 0;
-      }
-
-      hbin = read_hbin_block( file, block_off );
-
-      if ( hbin ) 
-	block_off = hbin->file_off + hbin->block_size;
-
-    } while ( hbin && !hbin_contains_offset( hbin, offset ) );
+  if(regfi_read(file->fd, hashes, &length) != 0
+     || length != sizeof(REGF_HASH_LIST_ELEM)*ret_val->num_keys)
+  {
+    free(ret_val->hashes);
+    free(ret_val);
+    return NULL;
   }
 
-  if ( hbin )
-    /* XXX: this kind of caching needs to be re-evaluated */
-    DLIST_ADD( file->block_list, hbin );
+  for (i=0; i < ret_val->num_keys; i++)
+  {
+    ret_val->hashes[i].nk_off = IVAL(hashes, i*sizeof(REGF_HASH_LIST_ELEM));
+    ret_val->hashes[i].hash = IVAL(hashes, i*sizeof(REGF_HASH_LIST_ELEM)+4);
+  }
+  free(hashes);
 
-  return hbin;
+  return ret_val;
 }
 
 
-/*******************************************************************
- *******************************************************************/
-static bool prs_hash_rec( const char *desc, prs_struct *ps, int depth, REGF_HASH_REC *hash )
-{
-  depth++;
-
-  if ( !prs_uint32( "nk_off", ps, depth, &hash->nk_off ))
-    return false;
-  if ( !prs_uint8s("keycheck", ps, depth, hash->keycheck, sizeof( hash->keycheck )) )
-    return false;
-	
-  return true;
-}
-
 
 /*******************************************************************
  *******************************************************************/
-static bool hbin_prs_lf_records(const char *desc, REGF_HBIN *hbin, 
-				int depth, REGF_NK_REC *nk)
+REGF_SK_REC* regfi_parse_sk(REGF_FILE* file, uint32 offset, uint32 max_size, bool strict)
 {
-  int i;
-  REGF_LF_REC *lf = &nk->subkeys;
-  uint32 data_size, start_off, end_off;
+  REGF_SK_REC* ret_val;
+  uint32 cell_length, length;
+  prs_struct ps;
+  uint8 sk_header[REGFI_SK_MIN_LENGTH];
+  bool unalloc = false;
 
-  depth++;
 
-  /* check if we have anything to do first */
-	
-  if ( nk->num_subkeys == 0 )
-    return true;
+  if(!regfi_parse_cell(file->fd, offset, sk_header, REGFI_SK_MIN_LENGTH,
+		       &cell_length, &unalloc))
+    return NULL;
+   
+  if(sk_header[0] != 's' || sk_header[1] != 'k')
+    return NULL;
+  
+  ret_val = (REGF_SK_REC*)zalloc(sizeof(REGF_SK_REC));
+  if(ret_val == NULL)
+    return NULL;
 
-  /* move to the LF record */
+  ret_val->offset = offset;
+  /* XXX: Is there a way to be more conservative (shorter) with 
+   *      cell length when cell is unallocated?
+   */
+  ret_val->cell_size = cell_length;
 
-  if ( !prs_set_offset( &hbin->ps, nk->subkeys_off + HBIN_HDR_SIZE - hbin->first_hbin_off ) )
-    return false;
+  if(ret_val->cell_size > max_size)
+    ret_val->cell_size = max_size & 0xFFFFFFF8;
+  if((ret_val->cell_size < REGFI_SK_MIN_LENGTH) 
+     || (strict && ret_val->cell_size != (ret_val->cell_size & 0xFFFFFFF8)))
+  {
+    free(ret_val);
+    return NULL;
+  }
 
-  /* backup and get the data_size */
-	
-  if ( !prs_set_offset( &hbin->ps, hbin->ps.data_offset-sizeof(uint32)) )
-    return false;
-  start_off = hbin->ps.data_offset;
-  if ( !prs_uint32( "rec_size", &hbin->ps, depth, &lf->rec_size ))
-    return false;
+  ret_val->magic[0] = sk_header[0];
+  ret_val->magic[1] = sk_header[1];
 
-  if(!prs_uint8s("header", &hbin->ps, depth, 
-		 lf->header, sizeof(lf->header)))
-    return false;
-		
-  if ( !prs_uint16( "num_keys", &hbin->ps, depth, &lf->num_keys))
-    return false;
+  /* XXX: Can additional validation be added here? */
+  ret_val->unknown_tag = SVAL(sk_header, 0x2);
+  ret_val->prev_sk_off = IVAL(sk_header, 0x4);
+  ret_val->next_sk_off = IVAL(sk_header, 0x8);
+  ret_val->ref_count = IVAL(sk_header, 0xC);
+  ret_val->desc_size = IVAL(sk_header, 0x10);
 
-  if ( hbin->ps.io ) {
-    if ( !(lf->hashes = (REGF_HASH_REC*)zcalloc(sizeof(REGF_HASH_REC), lf->num_keys )) )
-      return false;
+  if(ret_val->desc_size + REGFI_SK_MIN_LENGTH > ret_val->cell_size)
+  {
+    free(ret_val);
+    return NULL;
   }
 
-  for ( i=0; i<lf->num_keys; i++ ) {
-    if ( !prs_hash_rec( "hash_rec", &hbin->ps, depth, &lf->hashes[i] ) )
-      return false;
+  /* XXX: need to get rid of this, but currently the security descriptor
+   * code depends on the ps structure.
+   */
+  if(!prs_init(&ps, ret_val->desc_size, NULL, UNMARSHALL))
+  {
+    free(ret_val);
+    return NULL;
   }
 
-  end_off = hbin->ps.data_offset;
-
-  /* data_size must be divisible by 8 and large enough to hold the original record */
+  length = ret_val->desc_size;
+  if(regfi_read(file->fd, (uint8*)ps.data_p, &length) != 0 
+     || length != ret_val->desc_size)
+  {
+    free(ret_val);
+    return NULL;
+  }
 
-  data_size = ((start_off - end_off) & 0xfffffff8 );
-  /*  if ( data_size > lf->rec_size )*/
-    /*DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, lf->rec_size));*/
+  if (!sec_io_desc("sec_desc", &ret_val->sec_desc, &ps, 0))
+  {
+    free(ret_val);
+    return NULL;
+  }
 
-  if ( !hbin->ps.io )
-    hbin->dirty = true;
+  free(ps.data_p);
 
-  return true;
+  return ret_val;
 }
 
 
-/*******************************************************************
- *******************************************************************/
-static bool hbin_prs_sk_rec( const char *desc, REGF_HBIN *hbin, int depth, REGF_SK_REC *sk )
+uint32* regfi_parse_valuelist(REGF_FILE* file, uint32 offset, 
+			      uint32 num_values, bool strict)
 {
-  prs_struct *ps = &hbin->ps;
-  uint16 tag = 0xFFFF;
-  uint32 data_size, start_off, end_off;
-
-
-  depth++;
-
-  if ( !prs_set_offset( &hbin->ps, sk->sk_off + HBIN_HDR_SIZE - hbin->first_hbin_off ) )
-    return false;
+  uint32* ret_val;
+  uint32 i, cell_length, length, read_len;
+  bool unalloc;
 
-  /* backup and get the data_size */
-	
-  if ( !prs_set_offset( &hbin->ps, hbin->ps.data_offset-sizeof(uint32)) )
-    return false;
-  start_off = hbin->ps.data_offset;
-  if ( !prs_uint32( "rec_size", &hbin->ps, depth, &sk->rec_size ))
-    return false;
-
-  if (!prs_uint8s("header", ps, depth, sk->header, sizeof(sk->header)))
-    return false;
-  if ( !prs_uint16( "tag", ps, depth, &tag))
-    return false;
-
-  if ( !prs_uint32( "prev_sk_off", ps, depth, &sk->prev_sk_off))
-    return false;
-  if ( !prs_uint32( "next_sk_off", ps, depth, &sk->next_sk_off))
-    return false;
-  if ( !prs_uint32( "ref_count", ps, depth, &sk->ref_count))
-    return false;
-  if ( !prs_uint32( "size", ps, depth, &sk->size))
-    return false;
-
-  if ( !sec_io_desc( "sec_desc", &sk->sec_desc, ps, depth )) 
-    return false;
-
-  end_off = hbin->ps.data_offset;
-
-  /* data_size must be divisible by 8 and large enough to hold the original record */
-
-  data_size = ((start_off - end_off) & 0xfffffff8 );
-  /*  if ( data_size > sk->rec_size )*/
-    /*DEBUG(10,("Encountered reused record (0x%x < 0x%x)\n", data_size, sk->rec_size));*/
-
-  if ( !hbin->ps.io )
-    hbin->dirty = true;
-
-  return true;
-}
-
-
-/*******************************************************************
- *******************************************************************/
-static bool hbin_prs_vk_rec( const char *desc, REGF_HBIN *hbin, int depth, 
-			     REGF_VK_REC *vk, REGF_FILE *file )
-{
-  uint32 offset;
-  uint16 name_length;
-  prs_struct *ps = &hbin->ps;
-  uint32 data_size, start_off, end_off;
-
-  depth++;
-
-  /* backup and get the data_size */
-	
-  if ( !prs_set_offset( &hbin->ps, hbin->ps.data_offset-sizeof(uint32)) )
-    return false;
-  start_off = hbin->ps.data_offset;
-  if ( !prs_uint32( "rec_size", &hbin->ps, depth, &vk->rec_size ))
-    return false;
-
-  if ( !prs_uint8s("header", ps, depth, vk->header, sizeof( vk->header )) )
-    return false;
-
-  if ( !hbin->ps.io )
-    name_length = strlen(vk->valuename);
-
-  if ( !prs_uint16( "name_length", ps, depth, &name_length ))
-    return false;
-  if ( !prs_uint32( "data_size", ps, depth, &vk->data_size ))
-    return false;
-  if ( !prs_uint32( "data_off", ps, depth, &vk->data_off ))
-    return false;
-  if ( !prs_uint32( "type", ps, depth, &vk->type))
-    return false;
-  if ( !prs_uint16( "flag", ps, depth, &vk->flag))
-    return false;
-
-  offset = ps->data_offset;
-  offset += 2;	/* skip 2 bytes */
-  prs_set_offset( ps, offset );
-
-  /* get the name */
-
-  if ( vk->flag&VK_FLAG_NAME_PRESENT ) {
+  if(!regfi_parse_cell(file->fd, offset, NULL, 0, &cell_length, &unalloc))
+    return NULL;
 
-    if ( hbin->ps.io ) {
-      if ( !(vk->valuename = (char*)zcalloc(sizeof(char), name_length+1 )))
-	return false;
-    }
-    if ( !prs_uint8s("name", ps, depth, 
-		     (uint8*)vk->valuename, name_length) )
-      return false;
+  if(cell_length != (cell_length & 0xFFFFFFF8))
+  {
+    if(strict)
+      return NULL;
+    cell_length = cell_length & 0xFFFFFFF8;
   }
+  if((num_values * sizeof(uint32)) > cell_length-sizeof(uint32))
+    return NULL;
 
-  end_off = hbin->ps.data_offset;
-
-  /* get the data if necessary */
+  read_len = num_values*sizeof(uint32);
+  ret_val = (uint32*)malloc(read_len);
+  if(ret_val == NULL)
+    return NULL;
 
-  if ( vk->data_size != 0 ) 
+  length = read_len;
+  if((regfi_read(file->fd, (uint8*)ret_val, &length) != 0) || length != read_len)
   {
-    /* the data is stored in the offset if the size <= 4 */
-    if ( !(vk->data_size & VK_DATA_IN_OFFSET) ) 
-    {
-      REGF_HBIN *hblock = hbin;
-      uint32 data_rec_size;
-
-      if ( hbin->ps.io ) 
-      {
-	if ( !(vk->data = (uint8*)zcalloc(sizeof(uint8), vk->data_size) ) )
-	  return false;
-      }
+    free(ret_val);
+    return NULL;
+  }
+  
+  for(i=0; i < num_values; i++)
+  {
+    /* Fix endianness */
+    ret_val[i] = IVAL(&ret_val[i], 0);
 
-      /* this data can be in another hbin */
-      if ( !hbin_contains_offset( hbin, vk->data_off ) ) 
-      {
-	if ( !(hblock = lookup_hbin_block( file, vk->data_off )) )
-	  return false;
-      }
-      if (!(prs_set_offset(&hblock->ps, 
-			   (vk->data_off
-			    + HBIN_HDR_SIZE
-			    - hblock->first_hbin_off)
-			   - sizeof(uint32))))
-      {	return false; }
-
-      if ( !hblock->ps.io ) 
+    /* Validate the first num_values values to ensure they make sense */
+    if(strict)
+    {
+      if((ret_val[i] + REGF_BLOCKSIZE > file->file_length)
+	 || ((ret_val[i] & 0xFFFFFFF8) != ret_val[i]))
       {
-	data_rec_size = ( (vk->data_size+sizeof(uint32)) & 0xfffffff8 ) + 8;
-	data_rec_size = ( data_rec_size - 1 ) ^ 0xFFFFFFFF;
+	free(ret_val);
+	return NULL;
       }
-      if ( !prs_uint32( "data_rec_size", &hblock->ps, depth, &data_rec_size ))
-	return false;
-      if(!prs_uint8s("data", &hblock->ps, depth, 
-		     vk->data, vk->data_size))
-	return false;
-
-      if ( !hblock->ps.io )
-	hblock->dirty = true;
-    }
-    else 
-    {
-      if(!(vk->data = zcalloc(sizeof(uint8), 4)))
-	return false;
-      SIVAL( vk->data, 0, vk->data_off );
     }
-		
   }
 
-  /* data_size must be divisible by 8 and large enough to hold the original record */
-
-  data_size = ((start_off - end_off ) & 0xfffffff8 );
-  /* XXX: should probably print a warning here */
-  /*if ( data_size !=  vk->rec_size )
-    DEBUG(10,("prs_vk_rec: data_size check failed (0x%x < 0x%x)\n", data_size, vk->rec_size));*/
-
-  if ( !hbin->ps.io )
-    hbin->dirty = true;
-
-  return true;
+  return ret_val;
 }
 
 
-/*******************************************************************
- read a VK record which is contained in the HBIN block stored 
- in the prs_struct *ps.
-*******************************************************************/
-static bool hbin_prs_vk_records(const char *desc, REGF_HBIN *hbin, 
-				int depth, REGF_NK_REC *nk, REGF_FILE *file)
+
+/******************************************************************************
+ * If !strict, the list may contain NULLs, VK records may point to NULL.
+ ******************************************************************************/
+REGF_VK_REC** regfi_load_valuelist(REGF_FILE* file, uint32 offset, 
+				   uint32 num_values, uint32 max_size, 
+				   bool strict)
 {
-  int i;
-  uint32 record_size;
+  REGF_VK_REC** ret_val;
+  REGF_HBIN* hbin;
+  uint32 i, vk_offset, vk_max_length, usable_num_values;
+  uint32* voffsets;
 
-  depth++;
-  
-  /* check if we have anything to do first */
-  if(nk->num_values == 0)
-    return true;
-  	
-  if(hbin->ps.io)
+  if((num_values+1) * sizeof(uint32) > max_size)
   {
-    if (!(nk->values = (REGF_VK_REC*)zcalloc(sizeof(REGF_VK_REC), 
-					      nk->num_values )))
-      return false;
+    if(strict)
+      return NULL;
+    usable_num_values = max_size/sizeof(uint32) - sizeof(uint32);
   }
-  
-  /* convert the offset to something relative to this HBIN block */
-  if (!prs_set_offset(&hbin->ps, 
-		      nk->values_off
-		      + HBIN_HDR_SIZE
-		      - hbin->first_hbin_off
-		      - sizeof(uint32)))
-  { return false; }
+  else
+    usable_num_values = num_values;
 
-  if ( !hbin->ps.io ) 
-  { 
-    record_size = ( ( nk->num_values * sizeof(uint32) ) & 0xfffffff8 ) + 8;
-    record_size = (record_size - 1) ^ 0xFFFFFFFF;
-  }
+  voffsets = regfi_parse_valuelist(file, offset, usable_num_values, strict);
+  if(voffsets == NULL)
+    return NULL;
 
-  if ( !prs_uint32( "record_size", &hbin->ps, depth, &record_size ) )
-    return false;
-  	
-  for ( i=0; i<nk->num_values; i++ ) 
+  ret_val = (REGF_VK_REC**)zalloc(sizeof(REGF_VK_REC*) * num_values);
+  if(ret_val == NULL)
   {
-    if ( !prs_uint32( "vk_off", &hbin->ps, depth, &nk->values[i].rec_off ) )
-      return false;
+    free(voffsets);
+    return NULL;
   }
-
-  for ( i=0; i<nk->num_values; i++ ) 
+  
+  for(i=0; i < usable_num_values; i++)
   {
-    REGF_HBIN *sub_hbin = hbin;
-    uint32 new_offset;
-	
-    if ( !hbin_contains_offset( hbin, nk->values[i].rec_off ) ) 
+    hbin = regfi_lookup_hbin(file, voffsets[i]);
+    if(!hbin)
     {
-      sub_hbin = lookup_hbin_block( file, nk->values[i].rec_off );
-      if ( !sub_hbin ) 
+      free(voffsets);
+      free(ret_val);
+      return NULL;
+    }
+    
+    vk_offset =  voffsets[i] + REGF_BLOCKSIZE;
+    vk_max_length = hbin->block_size - vk_offset + sizeof(uint32);
+    ret_val[i] = regfi_parse_vk(file, vk_offset, vk_max_length, strict);
+    if(ret_val[i] == NULL)
+    { /* If we're being strict, throw out the whole list.
+       * Otherwise, let it be NULL.
+       */
+      if(strict)
       {
-	/*DEBUG(0,("hbin_prs_vk_records: Failed to find HBIN block containing offset [0x%x]\n", 
-	  nk->values[i].hbin_off));*/
-	return false;
+	free(voffsets);
+	free(ret_val);
+	return NULL;
       }
     }
-  	
-    new_offset = nk->values[i].rec_off 
-      + HBIN_HDR_SIZE 
-      - sub_hbin->first_hbin_off;
-
-    if (!prs_set_offset(&sub_hbin->ps, new_offset))
-      return false;
-    if (!hbin_prs_vk_rec("vk_rec", sub_hbin, depth, &nk->values[i], file))
-      return false;
   }
 
-  if ( !hbin->ps.io )
-    hbin->dirty = true;
-
-  return true;
+  free(voffsets);
+  return ret_val;
 }
 
 
-/*******************************************************************
- *******************************************************************/
-static REGF_SK_REC* find_sk_record_by_offset( REGF_FILE *file, uint32 offset )
-{
-  REGF_SK_REC *p_sk;
-  
-  for ( p_sk=file->sec_desc_list; p_sk; p_sk=p_sk->next ) {
-    if ( p_sk->sk_off == offset ) 
-      return p_sk;
-  }
-  
-  return NULL;
-}
-
 
 /*******************************************************************
+ * XXX: Need to add full key caching using a 
+ *      custom cache structure.
  *******************************************************************/
-static REGF_SK_REC* find_sk_record_by_sec_desc( REGF_FILE *file, SEC_DESC *sd )
+REGF_NK_REC* regfi_load_key(REGF_FILE* file, uint32 offset, bool strict)
 {
-  REGF_SK_REC *p;
-
-  for ( p=file->sec_desc_list; p; p=p->next ) {
-    if ( sec_desc_equal( p->sec_desc, sd ) )
-      return p;
-  }
-
-  /* failure */
-
-  return NULL;
-}
-
+  REGF_HBIN* hbin;
+  REGF_HBIN* sub_hbin;
+  REGF_NK_REC* nk;
+  uint32 max_length, off;
 
-/*******************************************************************
- *******************************************************************/
-static bool hbin_prs_key( REGF_FILE *file, REGF_HBIN *hbin, REGF_NK_REC *nk )
-{
-  int depth = 0;
-  REGF_HBIN *sub_hbin;
-  
-  depth++;
+  hbin = regfi_lookup_hbin(file, offset-REGF_BLOCKSIZE);
+  if (hbin == NULL) 
+    return NULL;
 
   /* get the initial nk record */
-  if (!prs_nk_rec("nk_rec", &hbin->ps, depth, nk))
-    return false;
+  max_length = hbin->block_size + hbin->file_off - offset;
+  if ((nk = regfi_parse_nk(file, offset, max_length, true)) == NULL)
+    return NULL;
 
   /* fill in values */
-  if ( nk->num_values && (nk->values_off!=REGF_OFFSET_NONE) ) 
+  if(nk->num_values && (nk->values_off!=REGF_OFFSET_NONE)) 
   {
     sub_hbin = hbin;
-    if ( !hbin_contains_offset( hbin, nk->values_off ) ) 
+    if(!regfi_offset_in_hbin(hbin, nk->values_off)) 
+      sub_hbin = regfi_lookup_hbin(file, nk->values_off);
+    
+    if(sub_hbin == NULL)
     {
-      sub_hbin = lookup_hbin_block( file, nk->values_off );
-      if ( !sub_hbin ) 
+      if(strict)
       {
-	/*DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing value_list_offset [0x%x]\n", 
-	  nk->values_off));*/
-	return false;
+	free(nk);
+	return NULL;
       }
+      else
+	nk->values = NULL;
     }
-		
-    if(!hbin_prs_vk_records("vk_rec", sub_hbin, depth, nk, file))
-      return false;
-  }
-		
-  /* now get subkeys */
-  if ( nk->num_subkeys && (nk->subkeys_off!=REGF_OFFSET_NONE) ) 
-  {
-    sub_hbin = hbin;
-    if ( !hbin_contains_offset( hbin, nk->subkeys_off ) ) 
+    else
     {
-      sub_hbin = lookup_hbin_block( file, nk->subkeys_off );
-      if ( !sub_hbin ) 
+      off = nk->values_off + REGF_BLOCKSIZE;
+      max_length = sub_hbin->block_size + sub_hbin->file_off - off;
+      nk->values = regfi_load_valuelist(file, off, nk->num_values, max_length, 
+					true);
+      if(strict && nk->values == NULL)
       {
-	/*DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing subkey_offset [0x%x]\n", 
-	  nk->subkeys_off));*/
-	return false;
+	free(nk);
+	return NULL;
       }
     }
-		
-    if (!hbin_prs_lf_records("lf_rec", sub_hbin, depth, nk))
-      return false;
   }
 
-  /* get the to the security descriptor.  First look if we have already parsed it */
-	
-  if ((nk->sk_off!=REGF_OFFSET_NONE) 
-      && !(nk->sec_desc = find_sk_record_by_offset( file, nk->sk_off )))
+  /* now get subkeys */
+  if(nk->num_subkeys && (nk->subkeys_off != REGF_OFFSET_NONE)) 
   {
     sub_hbin = hbin;
-    if (!hbin_contains_offset(hbin, nk->sk_off))
+    if(!regfi_offset_in_hbin(hbin, nk->subkeys_off))
+      sub_hbin = regfi_lookup_hbin(file, nk->subkeys_off);
+
+    if (sub_hbin == NULL) 
     {
-      sub_hbin = lookup_hbin_block( file, nk->sk_off );
-      if ( !sub_hbin ) {
-	/*DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing sk_offset [0x%x]\n", 
-	  nk->subkeys_off));*/
-	return false;
+      if(strict)
+      {
+	regfi_key_free(nk);
+	return NULL;
+      }
+      else
+	nk->subkeys = NULL;
+    }
+    else
+    {
+      off = nk->subkeys_off + REGF_BLOCKSIZE;
+      max_length = sub_hbin->block_size + sub_hbin->file_off - off;
+      nk->subkeys = regfi_load_hashlist(file, off, nk->num_subkeys, 
+					max_length, true);
+      if(nk->subkeys == NULL)
+      {
+	/* XXX: Temporary hack to get around 'ri' records */
+	nk->num_subkeys = 0;
       }
     }
-		
-    if ( !(nk->sec_desc = (REGF_SK_REC*)zalloc(sizeof(REGF_SK_REC) )) )
-      return false;
-    nk->sec_desc->sk_off = nk->sk_off;
-    if ( !hbin_prs_sk_rec( "sk_rec", sub_hbin, depth, nk->sec_desc ))
-      return false;
-			
-    /* add to the list of security descriptors (ref_count has been read from the files) */
-
-    nk->sec_desc->sk_off = nk->sk_off;
-    /* XXX: this kind of caching needs to be re-evaluated */
-    DLIST_ADD( file->sec_desc_list, nk->sec_desc );
   }
-		
-  return true;
+
+  return nk;
 }
 
 
-/*******************************************************************
- *******************************************************************/
-static bool next_record( REGF_HBIN *hbin, const char *hdr, bool *eob )
+/******************************************************************************
+
+ ******************************************************************************/
+static bool regfi_find_root_nk(REGF_FILE* file, uint32 offset, uint32 hbin_size,
+			       uint32* root_offset)
 {
-  uint8 header[REC_HDR_SIZE] = "";
-  uint32 record_size;
-  uint32 curr_off, block_size;
+  uint8 tmp[4];
+  int32 record_size;
+  uint32 length, hbin_offset = 0;
+  REGF_NK_REC* nk = NULL;
   bool found = false;
-  prs_struct *ps = &hbin->ps;
-	
-  curr_off = ps->data_offset;
-  if ( curr_off == 0 )
-    prs_set_offset( ps, HBIN_HEADER_REC_SIZE );
-
-  /* assume that the current offset is at the reacord header 
-     and we need to backup to read the record size */
-  curr_off -= sizeof(uint32);
-
-  block_size = ps->buffer_size;
-  record_size = 0;
-  while ( !found ) 
-  {
-    curr_off = curr_off+record_size;
-    if ( curr_off >= block_size ) 
-      break;
 
-    if ( !prs_set_offset( &hbin->ps, curr_off) )
-      return false;
-
-    if ( !prs_uint32( "record_size", ps, 0, &record_size ) )
+  for(record_size=0; !found && (hbin_offset < hbin_size); )
+  {
+    if(lseek(file->fd, offset+hbin_offset, SEEK_SET) == -1)
       return false;
-    if ( !prs_uint8s("header", ps, 0, header, REC_HDR_SIZE ) )
+    
+    length = 4;
+    if((regfi_read(file->fd, tmp, &length) != 0) || length != 4)
       return false;
+    record_size = IVALS(tmp, 0);
 
-    if ( record_size & 0x80000000 ) {
-      /* absolute_value(record_size) */
-      record_size = (record_size ^ 0xffffffff) + 1;
-    }
-
-    if ( memcmp( header, hdr, REC_HDR_SIZE ) == 0 ) {
-      found = true;
-      curr_off += sizeof(uint32);
+    if(record_size < 0)
+    {
+      record_size = record_size*(-1);
+      nk = regfi_parse_nk(file, offset+hbin_offset, hbin_size-hbin_offset, true);
+      if(nk != NULL)
+      {
+	if(nk->key_type == NK_TYPE_ROOTKEY)
+	{
+	  found = true;
+	  *root_offset = nk->offset;
+	}
+	free(nk);
+      }
     }
-  } 
 
-  /* mark prs_struct as done ( at end ) if no more SK records */
-  /* mark end-of-block as true */	
-  if ( !found )
-  {
-    prs_set_offset( &hbin->ps, hbin->ps.buffer_size );
-    *eob = true;
-    return false;
+    hbin_offset += record_size;
   }
 
-  if (!prs_set_offset(ps, curr_off))
-    return false;
-
-  return true;
+  return found;
 }
 
 
 /*******************************************************************
+ * Open the registry file and then read in the REGF block to get the
+ * first hbin offset.
  *******************************************************************/
-static bool next_nk_record(REGF_FILE *file, REGF_HBIN *hbin, 
-			   REGF_NK_REC *nk, bool *eob)
-{
-  if (next_record(hbin, "nk", eob) 
-      && hbin_prs_key(file, hbin, nk))
-    return true;
-	
-  return false;
-}
-
-
-/*******************************************************************
- Open the registry file and then read in the REGF block to get the 
- first hbin offset.
-*******************************************************************/
-REGF_FILE* regfi_open( const char *filename )
+REGF_FILE* regfi_open(const char* filename)
 {
-  REGF_FILE *rb;
-  int flags = O_RDONLY;
-
-  if ( !(rb = (REGF_FILE*)malloc(sizeof(REGF_FILE))) ) {
-    /* DEBUG(0,("ERROR allocating memory\n")); */
-    return NULL;
-  }
-  memset(rb, 0, sizeof(REGF_FILE));
-  rb->fd = -1;
-	
-  /*	if ( !(rb->mem_ctx = talloc_init( "read_regf_block" )) ) 
-    {
-    regfi_close( rb );
-    return NULL;
-    }
-  */
-  rb->open_flags = flags;
-	
-  /* open and existing file */
+  REGF_FILE* rb;
+  REGF_HBIN* hbin = NULL;
+  uint32 hbin_off;
+  int fd;
+  bool rla;
 
-  if ( (rb->fd = open(filename, flags)) == -1 ) {
+  /* open an existing file */
+  if ((fd = open(filename, O_RDONLY)) == -1) 
+  {
     /* DEBUG(0,("regfi_open: failure to open %s (%s)\n", filename, strerror(errno)));*/
-    regfi_close( rb );
     return NULL;
   }
-	
+  
   /* read in an existing file */
-	
-  if ( !read_regf_block( rb ) ) {
+  if ((rb = regfi_parse_regf(fd, true)) == NULL) 
+  {
     /* DEBUG(0,("regfi_open: Failed to read initial REGF block\n"));*/
-    regfi_close( rb );
+    close(fd);
     return NULL;
   }
-	
+  
+  rb->hbins = range_list_new();
+  if(rb->hbins == NULL)
+  {
+    range_list_free(rb->hbins);
+    close(fd);
+    free(rb);
+    return NULL;
+  }
+  
+  rla = true;
+  hbin_off = REGF_BLOCKSIZE;
+  hbin = regfi_parse_hbin(rb, hbin_off, true);
+  while(hbin && rla)
+  {
+    hbin_off = hbin->file_off + hbin->block_size;
+    rla = range_list_add(rb->hbins, hbin->file_off, hbin->block_size, hbin);
+    hbin = regfi_parse_hbin(rb, hbin_off, true);
+  }
+
   /* success */
-	
   return rb;
 }
 
 
 /*******************************************************************
-XXX: should this be nuked?
- *******************************************************************/
-static void regfi_mem_free( REGF_FILE *file )
-{
-  /* free any zalloc()'d memory */
-	
-  /*	if ( file && file->mem_ctx )
-    free(file->mem_ctx);
-  */
-}
-
-
-/*******************************************************************
  *******************************************************************/
 int regfi_close( REGF_FILE *file )
 {
   int fd;
-
-  regfi_mem_free( file );
+  uint32 i;
 
   /* nothing to do if there is no open file */
-
-  if ( !file || (file->fd == -1) )
+  if ((file == NULL) || (file->fd == -1))
     return 0;
-		
+
   fd = file->fd;
   file->fd = -1;
-  SAFE_FREE( file );
+  for(i=0; i < range_list_size(file->hbins); i++)
+    free(range_list_get(file->hbins, i)->data);
+  range_list_free(file->hbins);
+
+  free(file);
 
-  return close( fd );
+  return close(fd);
 }
 
 
@@ -1444,58 +953,32 @@ int regfi_close( REGF_FILE *file )
  * There should be only *one* root key in the registry file based 
  * on my experience.  --jerry
  *****************************************************************************/
-REGF_NK_REC* regfi_rootkey( REGF_FILE *file )
+REGF_NK_REC* regfi_rootkey(REGF_FILE *file)
 {
-  REGF_NK_REC *nk;
-  REGF_HBIN   *hbin;
-  uint32      offset = REGF_BLOCKSIZE;
-  bool        found = false;
-  bool        eob;
-	
-  if ( !file )
-    return NULL;
-		
-  if ( !(nk = (REGF_NK_REC*)zalloc(sizeof(REGF_NK_REC) )) ) {
-    /*DEBUG(0,("regfi_rootkey: zalloc() failed!\n"));*/
+  REGF_NK_REC* nk = NULL;
+  REGF_HBIN*   hbin;
+  uint32       root_offset, i, num_hbins;
+  
+  if(!file)
     return NULL;
-  }
-	
-  /* scan through the file on HBIN block at a time looking 
+
+  /* Scan through the file one HBIN block at a time looking 
      for an NK record with a type == 0x002c.
      Normally this is the first nk record in the first hbin 
      block (but I'm not assuming that for now) */
-	
-  while ( (hbin = read_hbin_block( file, offset )) ) {
-    eob = false;
 
-    while ( !eob) {
-      if ( next_nk_record( file, hbin, nk, &eob ) ) {
-	if ( nk->key_type == NK_TYPE_ROOTKEY ) {
-	  found = true;
-	  break;
-	}
-      }
-      if(hbin->ps.is_dynamic)
-	SAFE_FREE(hbin->ps.data_p);
-      hbin->ps.is_dynamic = false;
-      hbin->ps.buffer_size = 0;
-      hbin->ps.data_offset = 0;
-    }
-		
-    if ( found ) 
+  num_hbins = range_list_size(file->hbins);
+  for(i=0; i < num_hbins; i++)
+  {
+    hbin = (REGF_HBIN*)range_list_get(file->hbins, i)->data;
+    if(regfi_find_root_nk(file, hbin->file_off+HBIN_HEADER_REC_SIZE, 
+			  hbin->block_size-HBIN_HEADER_REC_SIZE, &root_offset))
+    {
+      nk = regfi_load_key(file, root_offset, true);
       break;
-
-    offset += hbin->block_size;
-  }
-	
-  if ( !found ) {
-    /*DEBUG(0,("regfi_rootkey: corrupt registry file ?  No root key record located\n"));*/
-    return NULL;
+    }
   }
 
-  /* XXX: this kind of caching needs to be re-evaluated */
-  DLIST_ADD( file->block_list, hbin );
-
   return nk;
 }
 
@@ -1510,10 +993,11 @@ void regfi_key_free(REGF_NK_REC* nk)
   {
     for(i=0; i < nk->num_values; i++)
     {
-      if(nk->values[i].valuename != NULL)
-	free(nk->values[i].valuename);
-      if(nk->values[i].data != NULL)
-	free(nk->values[i].data);
+      if(nk->values[i]->valuename != NULL)
+	free(nk->values[i]->valuename);
+      if(nk->values[i]->data != NULL)
+	free(nk->values[i]->data);
+      free(nk->values[i]);
     }
     free(nk->values);
   }
@@ -1553,6 +1037,14 @@ REGFI_ITERATOR* regfi_iterator_new(REGF_FILE* fh)
     return NULL;
   }
 
+  /* This secret isn't very secret, but we don't need a good one.  This 
+   * secret is just designed to prevent someone from trying to blow our
+   * caching and make things slow.
+   */
+  ret_val->sk_recs = lru_cache_create(127, 0x15DEAD05^time(NULL)
+				           ^(getpid()<<16)^(getppid()<<8),
+				      true);
+
   ret_val->f = fh;
   ret_val->cur_key = root;
   ret_val->cur_subkey = 0;
@@ -1577,6 +1069,8 @@ void regfi_iterator_free(REGFI_ITERATOR* i)
     free(cur);
   }
   
+  lru_cache_destroy(i->sk_recs);
+
   free(i);
 }
 
@@ -1720,6 +1214,42 @@ const REGF_NK_REC* regfi_iterator_cur_key(REGFI_ITERATOR* i)
 
 /******************************************************************************
  *****************************************************************************/
+const REGF_SK_REC* regfi_iterator_cur_sk(REGFI_ITERATOR* i)
+{
+  REGF_SK_REC* ret_val;
+  REGF_HBIN* hbin;
+  uint32 max_length, off;
+
+  if(i->cur_key == NULL)
+    return NULL;
+  
+  /* First look if we have already parsed it */
+  if((i->cur_key->sk_off!=REGF_OFFSET_NONE)
+     && !(ret_val =(REGF_SK_REC*)lru_cache_find(i->sk_recs, 
+						&i->cur_key->sk_off, 4)))
+  {
+    hbin = regfi_lookup_hbin(i->f, i->cur_key->sk_off);
+
+    if(hbin == NULL)
+      return NULL;
+
+    off = i->cur_key->sk_off + REGF_BLOCKSIZE;
+    max_length = hbin->block_size + hbin->file_off - off;
+    ret_val = regfi_parse_sk(i->f, off, max_length, true);
+    if(ret_val == NULL)
+      return NULL;
+
+    ret_val->sk_off = i->cur_key->sk_off;
+    lru_cache_update(i->sk_recs, &i->cur_key->sk_off, 4, ret_val);
+  }
+
+  return ret_val;
+}
+
+
+
+/******************************************************************************
+ *****************************************************************************/
 const REGF_NK_REC* regfi_iterator_first_subkey(REGFI_ITERATOR* i)
 {
   i->cur_subkey = 0;
@@ -1731,8 +1261,6 @@ const REGF_NK_REC* regfi_iterator_first_subkey(REGFI_ITERATOR* i)
  *****************************************************************************/
 const REGF_NK_REC* regfi_iterator_cur_subkey(REGFI_ITERATOR* i)
 {
-  REGF_NK_REC* subkey;
-  REGF_HBIN* hbin;
   uint32 nk_offset;
 
   /* see if there is anything left to report */
@@ -1740,32 +1268,9 @@ const REGF_NK_REC* regfi_iterator_cur_subkey(REGFI_ITERATOR* i)
       || (i->cur_subkey >= i->cur_key->num_subkeys))
     return NULL;
 
-  nk_offset = i->cur_key->subkeys.hashes[i->cur_subkey].nk_off;
-
-  /* find the HBIN block which should contain the nk record */
-  hbin = lookup_hbin_block(i->f, nk_offset);
-  if(!hbin)
-  {
-    /* XXX: should print out some kind of error message every time here */
-    /*DEBUG(0,("hbin_prs_key: Failed to find HBIN block containing offset [0x%x]\n", 
-      i->cur_key->subkeys.hashes[i->cur_subkey].nk_off));*/
-    return NULL;
-  }
+  nk_offset = i->cur_key->subkeys->hashes[i->cur_subkey].nk_off;
   
-  if(!prs_set_offset(&hbin->ps, 
-		     HBIN_HDR_SIZE + nk_offset - hbin->first_hbin_off))
-    return NULL;
-		
-  if(!(subkey = (REGF_NK_REC*)zalloc(sizeof(REGF_NK_REC))))
-    return NULL;
-
-  if(!hbin_prs_key(i->f, hbin, subkey))
-  {
-    regfi_key_free(subkey);
-    return NULL;
-  }
-
-  return subkey;
+  return regfi_load_key(i->f, nk_offset+REGF_BLOCKSIZE, true);
 }
 
 
@@ -1828,7 +1333,7 @@ const REGF_VK_REC* regfi_iterator_cur_value(REGFI_ITERATOR* i)
 {
   REGF_VK_REC* ret_val = NULL;
   if(i->cur_value < i->cur_key->num_values)
-    ret_val = &(i->cur_key->values[i->cur_value]);
+    ret_val = i->cur_key->values[i->cur_value];
 
   return ret_val;
 }
@@ -1847,3 +1352,510 @@ const REGF_VK_REC* regfi_iterator_next_value(REGFI_ITERATOR* i)
 
   return ret_val;
 }
+
+
+
+/*******************************************************************
+ * Computes the checksum of the registry file header.
+ * buffer must be at least the size of an regf header (4096 bytes).
+ *******************************************************************/
+static uint32 regfi_compute_header_checksum(uint8* buffer)
+{
+  uint32 checksum, x;
+  int i;
+
+  /* XOR of all bytes 0x0000 - 0x01FB */
+
+  checksum = x = 0;
+  
+  for ( i=0; i<0x01FB; i+=4 ) {
+    x = IVAL(buffer, i );
+    checksum ^= x;
+  }
+  
+  return checksum;
+}
+
+
+/*******************************************************************
+ * XXX: Add way to return more detailed error information.
+ *******************************************************************/
+REGF_FILE* regfi_parse_regf(int fd, bool strict)
+{
+  uint8 file_header[REGF_BLOCKSIZE];
+  uint32 length;
+  uint32 file_length;
+  struct stat sbuf;
+  REGF_FILE* ret_val;
+
+  /* Determine file length.  Must be at least big enough 
+   * for the header and one hbin. 
+   */
+  if (fstat(fd, &sbuf) == -1)
+    return NULL;
+  file_length = sbuf.st_size;
+  if(file_length < REGF_BLOCKSIZE+REGF_ALLOC_BLOCK)
+    return NULL;
+
+  ret_val = (REGF_FILE*)zalloc(sizeof(REGF_FILE));
+  if(ret_val == NULL)
+    return NULL;
+
+  ret_val->fd = fd;
+  ret_val->file_length = file_length;
+
+  length = REGF_BLOCKSIZE;
+  if((regfi_read(fd, file_header, &length)) != 0 
+     || length != REGF_BLOCKSIZE)
+  {
+    free(ret_val);
+    return NULL;
+  }
+
+  ret_val->checksum = IVAL(file_header, 0x1FC);
+  ret_val->computed_checksum = regfi_compute_header_checksum(file_header);
+  if (strict && (ret_val->checksum != ret_val->computed_checksum))
+  {
+    free(ret_val);
+    return NULL;
+  }
+
+  memcpy(ret_val->magic, file_header, 4);
+  if(strict && (memcmp(ret_val->magic, "regf", 4) != 0))
+  {
+    free(ret_val);
+    return NULL;
+  }
+  
+  ret_val->unknown1 = IVAL(file_header, 0x4);
+  ret_val->unknown2 = IVAL(file_header, 0x8);
+
+  ret_val->mtime.low = IVAL(file_header, 0xC);
+  ret_val->mtime.high = IVAL(file_header, 0x10);
+
+  ret_val->unknown3 = IVAL(file_header, 0x14);
+  ret_val->unknown4 = IVAL(file_header, 0x18);
+  ret_val->unknown5 = IVAL(file_header, 0x1C);
+  ret_val->unknown6 = IVAL(file_header, 0x20);
+  
+  ret_val->data_offset = IVAL(file_header, 0x24);
+  ret_val->last_block = IVAL(file_header, 0x28);
+
+  ret_val->unknown7 = IVAL(file_header, 0x2C);
+
+  return ret_val;
+}
+
+
+
+/*******************************************************************
+ * Given real file offset, read and parse the hbin at that location
+ * along with it's associated cells.
+ *******************************************************************/
+/* XXX: Need a way to return types of errors.
+ */
+REGF_HBIN* regfi_parse_hbin(REGF_FILE* file, uint32 offset, bool strict)
+{
+  REGF_HBIN *hbin;
+  uint8 hbin_header[HBIN_HEADER_REC_SIZE];
+  uint32 length;
+  
+  if(offset >= file->file_length)
+    return NULL;
+
+  if(lseek(file->fd, offset, SEEK_SET) == -1)
+    return NULL;
+
+  length = HBIN_HEADER_REC_SIZE;
+  if((regfi_read(file->fd, hbin_header, &length) != 0) 
+     || length != HBIN_HEADER_REC_SIZE)
+    return NULL;
+
+
+  if(lseek(file->fd, offset, SEEK_SET) == -1)
+    return NULL;
+
+  if(!(hbin = (REGF_HBIN*)zalloc(sizeof(REGF_HBIN)))) 
+    return NULL;
+  hbin->file_off = offset;
+
+  memcpy(hbin->magic, hbin_header, 4);
+  if(strict && (memcmp(hbin->magic, "hbin", 4) != 0))
+  {
+    free(hbin);
+    return NULL;
+  }
+
+  hbin->first_hbin_off = IVAL(hbin_header, 0x4);
+  hbin->block_size = IVAL(hbin_header, 0x8);
+  /* this should be the same thing as hbin->block_size but just in case */
+  hbin->next_block = IVAL(hbin_header, 0x1C);
+
+
+  /* Ensure the block size is a multiple of 0x1000 and doesn't run off 
+   * the end of the file. 
+   */
+  /* XXX: This may need to be relaxed for dealing with 
+   *      partial or corrupt files. 
+   */
+  if((offset + hbin->block_size > file->file_length)
+     || (hbin->block_size & 0xFFFFF000) != hbin->block_size)
+  {
+    free(hbin);
+    return NULL;
+  }
+
+  return hbin;
+}
+
+
+
+REGF_NK_REC* regfi_parse_nk(REGF_FILE* file, uint32 offset, 
+			    uint32 max_size, bool strict)
+{
+  uint8 nk_header[REGFI_NK_MIN_LENGTH];
+  REGF_NK_REC* ret_val;
+  uint32 length;
+  uint32 cell_length;
+  bool unalloc = false;
+
+  if(!regfi_parse_cell(file->fd, offset, nk_header, REGFI_NK_MIN_LENGTH,
+		       &cell_length, &unalloc))
+     return NULL;
+ 
+  /* A bit of validation before bothering to allocate memory */
+  if((nk_header[0x0] != 'n') || (nk_header[0x1] != 'k'))
+  {
+    /* XXX: Deal with subkey-lists that reference other subkey-lists
+     *      (e.g. 'ri' records). 
+     */
+    return NULL;
+  }
+
+  ret_val = (REGF_NK_REC*)zalloc(sizeof(REGF_NK_REC));
+  if(ret_val == NULL)
+    return NULL;
+
+  ret_val->offset = offset;
+  ret_val->cell_size = cell_length;
+
+  if(ret_val->cell_size > max_size)
+    ret_val->cell_size = max_size & 0xFFFFFFF8;
+  if((ret_val->cell_size < REGFI_NK_MIN_LENGTH) 
+     || (strict && ret_val->cell_size != (ret_val->cell_size & 0xFFFFFFF8)))
+  {
+    free(ret_val);
+    return NULL;
+  }
+
+  ret_val->magic[0] = nk_header[0x0];
+  ret_val->magic[1] = nk_header[0x1];
+  ret_val->key_type = SVAL(nk_header, 0x2);
+  if((ret_val->key_type != NK_TYPE_NORMALKEY)
+     && (ret_val->key_type != NK_TYPE_ROOTKEY) 
+     && (ret_val->key_type != NK_TYPE_LINKKEY)
+     && (ret_val->key_type != NK_TYPE_UNKNOWN1))
+  {
+    free(ret_val);
+    return NULL;
+  }
+
+  ret_val->mtime.low = IVAL(nk_header, 0x4);
+  ret_val->mtime.high = IVAL(nk_header, 0x8);
+  /* If the key is unallocated and the MTIME is earlier than Jan 1, 1990
+   * or later than Jan 1, 2290, we consider this a bad key.  This helps
+   * weed out some false positives during deleted data recovery.
+   */
+  if(unalloc
+     && ((ret_val->mtime.high < REGFI_MTIME_MIN_HIGH 
+	  && ret_val->mtime.low < REGFI_MTIME_MIN_LOW)
+	 || (ret_val->mtime.high > REGFI_MTIME_MAX_HIGH 
+	     && ret_val->mtime.low > REGFI_MTIME_MAX_LOW)))
+    return NULL;
+
+  ret_val->unknown1 = IVAL(nk_header, 0xC);
+  ret_val->parent_off = IVAL(nk_header, 0x10);
+  ret_val->num_subkeys = IVAL(nk_header, 0x14);
+  ret_val->unknown2 = IVAL(nk_header, 0x18);
+  ret_val->subkeys_off = IVAL(nk_header, 0x1C);
+  ret_val->unknown3 = IVAL(nk_header, 0x20);
+  ret_val->num_values = IVAL(nk_header, 0x24);
+  ret_val->values_off = IVAL(nk_header, 0x28);
+  ret_val->sk_off = IVAL(nk_header, 0x2C);
+  /* XXX: currently we do nothing with class names.  Need to investigate. */
+  ret_val->classname_off = IVAL(nk_header, 0x30);
+
+  ret_val->max_bytes_subkeyname = IVAL(nk_header, 0x34);
+  ret_val->max_bytes_subkeyclassname = IVAL(nk_header, 0x38);
+  ret_val->max_bytes_valuename = IVAL(nk_header, 0x3C);
+  ret_val->max_bytes_value = IVAL(nk_header, 0x40);
+  ret_val->unk_index = IVAL(nk_header, 0x44);
+
+  ret_val->name_length = SVAL(nk_header, 0x48);
+  ret_val->classname_length = SVAL(nk_header, 0x4A);
+
+  if(ret_val->name_length + REGFI_NK_MIN_LENGTH > ret_val->cell_size)
+  {
+    if(strict)
+    {
+      free(ret_val);
+      return NULL;
+    }
+    else
+      ret_val->name_length = ret_val->cell_size - REGFI_NK_MIN_LENGTH;
+  }
+  else if (unalloc)
+  { /* Truncate cell_size if it's much larger than the apparent total record length. */
+    /* Round up to the next multiple of 8 */
+    length = (ret_val->name_length + REGFI_NK_MIN_LENGTH) & 0xFFFFFFF8;
+    if(length < ret_val->name_length + REGFI_NK_MIN_LENGTH)
+      length+=8;
+
+    /* If cell_size is still greater, truncate. */
+    if(length < ret_val->cell_size)
+      ret_val->cell_size = length;
+  }
+
+  ret_val->keyname = (char*)zalloc(sizeof(char)*(ret_val->name_length+1));
+  if(ret_val->keyname == NULL)
+  {
+    free(ret_val);
+    return NULL;
+  }
+
+  /* Don't need to seek, should be at the right offset */
+  length = ret_val->name_length;
+  if((regfi_read(file->fd, (uint8*)ret_val->keyname, &length) != 0)
+     || length != ret_val->name_length)
+  {
+    free(ret_val->keyname);
+    free(ret_val);
+    return NULL;
+  }
+  ret_val->keyname[ret_val->name_length] = '\0';
+
+  return ret_val;
+}
+
+
+
+/*******************************************************************
+ *******************************************************************/
+REGF_VK_REC* regfi_parse_vk(REGF_FILE* file, uint32 offset, 
+			    uint32 max_size, bool strict)
+{
+  REGF_VK_REC* ret_val;
+  uint8 vk_header[REGFI_VK_MIN_LENGTH];
+  uint32 raw_data_size, length, cell_length;
+  bool unalloc = false;
+
+  if(!regfi_parse_cell(file->fd, offset, vk_header, REGFI_VK_MIN_LENGTH,
+		       &cell_length, &unalloc))
+    return NULL;
+
+  ret_val = (REGF_VK_REC*)zalloc(sizeof(REGF_VK_REC));
+  if(ret_val == NULL)
+    return NULL;
+
+  ret_val->offset = offset;
+  ret_val->cell_size = cell_length;
+
+  if(ret_val->cell_size > max_size)
+    ret_val->cell_size = max_size & 0xFFFFFFF8;
+  if((ret_val->cell_size < REGFI_VK_MIN_LENGTH) 
+     || ret_val->cell_size != (ret_val->cell_size & 0xFFFFFFF8))
+  {
+    free(ret_val);
+    return NULL;
+  }
+
+  ret_val->magic[0] = vk_header[0x0];
+  ret_val->magic[1] = vk_header[0x1];
+  if((ret_val->magic[0] != 'v') || (ret_val->magic[1] != 'k'))
+  {
+    free(ret_val);
+    return NULL;
+  }
+
+  ret_val->name_length = SVAL(vk_header, 0x2);
+  raw_data_size = IVAL(vk_header, 0x4);
+  ret_val->data_size = raw_data_size & ~VK_DATA_IN_OFFSET;
+  ret_val->data_in_offset = (bool)(raw_data_size & VK_DATA_IN_OFFSET);
+  ret_val->data_off = IVAL(vk_header, 0x8);
+  ret_val->type = IVAL(vk_header, 0xC);
+  ret_val->flag = SVAL(vk_header, 0x10);
+  ret_val->unknown1 = SVAL(vk_header, 0x12);
+
+  if(ret_val->flag & VK_FLAG_NAME_PRESENT)
+  {
+    if(ret_val->name_length + REGFI_VK_MIN_LENGTH + 4 > ret_val->cell_size)
+    {
+      if(strict)
+      {
+	free(ret_val);
+	return NULL;
+      }
+      else
+	ret_val->name_length = ret_val->cell_size - REGFI_VK_MIN_LENGTH - 4;
+    }
+
+    /* Round up to the next multiple of 8 */
+    cell_length = (ret_val->name_length + REGFI_VK_MIN_LENGTH + 4) & 0xFFFFFFF8;
+    if(cell_length < ret_val->name_length + REGFI_VK_MIN_LENGTH + 4)
+      cell_length+=8;
+
+    ret_val->valuename = (char*)zalloc(sizeof(char)*(ret_val->name_length+1));
+    if(ret_val->valuename == NULL)
+    {
+      free(ret_val);
+      return NULL;
+    }
+
+    length = ret_val->name_length;
+    if((regfi_read(file->fd, (uint8*)ret_val->valuename, &length) != 0)
+       || length != ret_val->name_length)
+    {
+      free(ret_val->valuename);
+      free(ret_val);
+      return NULL;
+    }
+    ret_val->valuename[ret_val->name_length] = '\0';
+  }
+  else
+    cell_length = REGFI_VK_MIN_LENGTH + 4;
+
+  if(unalloc)
+  {
+    /* If cell_size is still greater, truncate. */
+    if(cell_length < ret_val->cell_size)
+      ret_val->cell_size = cell_length;
+  }
+
+  if(ret_val->data_size == 0)
+    ret_val->data = NULL;
+  else
+  {
+    ret_val->data = regfi_parse_data(file, ret_val->data_off+REGF_BLOCKSIZE,
+				     raw_data_size, strict);
+    if(strict && (ret_val->data == NULL))
+    {
+      free(ret_val->valuename);
+      free(ret_val);
+      return NULL;
+    }
+  }
+
+  return ret_val;
+}
+
+
+uint8* regfi_parse_data(REGF_FILE* file, uint32 offset, uint32 length, bool strict)
+{
+  uint8* ret_val;
+  uint32 read_length, cell_length;
+  uint8 i;
+  bool unalloc;
+
+  /* The data is stored in the offset if the size <= 4 */
+  if (length & VK_DATA_IN_OFFSET)
+  {
+    length = length & ~VK_DATA_IN_OFFSET;
+    if(length > 4)
+      return NULL;
+
+    if((ret_val = (uint8*)zalloc(sizeof(uint8)*length)) == NULL)
+      return NULL;
+
+    offset = offset - REGF_BLOCKSIZE;
+    for(i = 0; i < length; i++)
+      ret_val[i] = (uint8)((offset >> i*8) & 0xFF);
+  }
+  else
+  {
+    if(!regfi_parse_cell(file->fd, offset, NULL, 0,
+			 &cell_length, &unalloc))
+      return NULL;
+
+    if((cell_length & 0xFFFFFFF8) != cell_length)
+      return NULL;
+
+    if(cell_length - 4 < length)
+    {
+      /* XXX: This strict condition has been triggered in multiple registries.
+       *      Not sure the cause, but the data length values are very large,
+       *      such as 53392.
+       */
+      if(strict)
+	return NULL;
+      else
+	length = cell_length - 4;
+    }
+
+    /* XXX: There is currently no check to ensure the data 
+     *      cell doesn't cross HBIN boundary.
+     */
+
+    if((ret_val = (uint8*)zalloc(sizeof(uint8)*length)) == NULL)
+      return NULL;
+
+    read_length = length;
+    if((regfi_read(file->fd, ret_val, &read_length) != 0) 
+       || read_length != length)
+    {
+      free(ret_val);
+      return NULL;
+    }
+  }
+
+  return ret_val;
+}
+
+
+range_list* regfi_parse_unalloc_cells(REGF_FILE* file)
+{
+  range_list* ret_val;
+  REGF_HBIN* hbin;
+  const range_list_element* hbins_elem;
+  uint32 i, num_hbins, curr_off, cell_len;
+  bool is_unalloc;
+
+  ret_val = range_list_new();
+  if(ret_val == NULL)
+    return NULL;
+
+  num_hbins = range_list_size(file->hbins);
+  for(i=0; i<num_hbins; i++)
+  {
+    hbins_elem = range_list_get(file->hbins, i);
+    if(hbins_elem == NULL)
+      break;
+    hbin = (REGF_HBIN*)hbins_elem->data;
+
+    curr_off = HBIN_HEADER_REC_SIZE;
+    while(curr_off < hbin->block_size)
+    {
+      if(!regfi_parse_cell(file->fd, hbin->file_off+curr_off, NULL, 0,
+			   &cell_len, &is_unalloc))
+	break;
+      
+      if((cell_len == 0) || ((cell_len & 0xFFFFFFF8) != cell_len))
+	/* XXX: should report an error here. */
+	break;
+      
+      /* for some reason the record_size of the last record in
+	 an hbin block can extend past the end of the block
+	 even though the record fits within the remaining 
+	 space....aaarrrgggghhhhhh */  
+      if(curr_off + cell_len >= hbin->block_size)
+	cell_len = hbin->block_size - curr_off;
+      
+      if(is_unalloc)
+	range_list_add(ret_val, hbin->file_off+curr_off, 
+		       cell_len, NULL);
+      
+      curr_off = curr_off+cell_len;
+    }
+  }
+
+  return ret_val;
+}
diff --git a/lib/smb_deps.c b/lib/smb_deps.c
index d2be9e1..da7345d 100644
--- a/lib/smb_deps.c
+++ b/lib/smb_deps.c
@@ -9,7 +9,7 @@
  *
  * 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
- * the Free Software Foundation; version 2 of the License.
+ * the Free Software Foundation; version 3 of the License.
  * 
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -20,7 +20,7 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  *
- * $Id: smb_deps.c 84 2007-01-19 14:52:25Z tim $
+ * $Id: smb_deps.c 111 2008-05-01 04:06:22Z tim $
  */
 
 #include "../include/smb_deps.h"
diff --git a/lib/void_stack.c b/lib/void_stack.c
index d2b3d89..402df0b 100644
--- a/lib/void_stack.c
+++ b/lib/void_stack.c
@@ -7,7 +7,7 @@
  *
  * 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
- * the Free Software Foundation; version 2 of the License.
+ * the Free Software Foundation; version 3 of the License.
  * 
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -18,7 +18,7 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
  *
- * $Id: void_stack.c 89 2007-03-09 15:00:09Z tim $
+ * $Id: void_stack.c 111 2008-05-01 04:06:22Z tim $
  */
 
 #include "../include/void_stack.h"
diff --git a/src/Makefile b/src/Makefile
index ffaa4f4..8fde7f1 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -1,19 +1,26 @@
-# $Id: Makefile 90 2007-03-28 19:22:38Z tim $
+# $Id: Makefile 110 2008-04-29 22:59:55Z tim $
 
 ################################################################################
 
 REGLOOKUP=$(BUILD_BIN)/reglookup
+REGLOOKUP_RECOVER=$(BUILD_BIN)/reglookup-recover
 OBJ=$(wildcard ../lib/*.o)
-FILES=$(REGLOOKUP)
+FILES=$(REGLOOKUP) $(REGLOOKUP_RECOVER)
 
 all: $(FILES)
 
 $(REGLOOKUP): reglookup.o $(OBJ)
 	$(CC) $(CFLAGS) $(OPTS) $(LIB) -o $@ reglookup.o $(OBJ)
 
+$(REGLOOKUP_RECOVER): reglookup-recover.o $(OBJ)
+	$(CC) $(CFLAGS) $(OPTS) $(LIB) -o $@ reglookup-recover.o $(OBJ)
+
 reglookup.o: reglookup.c
 	$(CC) $(CFLAGS) $(OPTS) $(INC) -c -o $@ reglookup.c
 
+reglookup-recover.o: reglookup-recover.c
+	$(CC) $(CFLAGS) $(OPTS) $(INC) -c -o $@ reglookup-recover.c
+
 install:
 	install -m 0755 $(FILES) $(BIN_PREFIX)
 
diff --git a/src/common.c b/src/common.c
new file mode 100644
index 0000000..4012c4b
--- /dev/null
+++ b/src/common.c
@@ -0,0 +1,376 @@
+/*
+ * This file stores code common to the command line tools.
+ * XXX: This should be converted to a proper library.
+ *
+ * Copyright (C) 2005-2008 Timothy D. Morgan
+ * Copyright (C) 2002 Richard Sharpe, rsharpe at richardsharpe.com
+ *
+ * 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
+ * the Free Software Foundation; version 3 of the License.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
+ *
+ * $Id: common.c 121 2008-08-09 17:22:26Z tim $
+ */
+
+#include <iconv.h>
+iconv_t conv_desc;
+
+const char* key_special_chars = ",\"\\/";
+const char* subfield_special_chars = ",\"\\|";
+const char* common_special_chars = ",\"\\";
+
+#define REGLOOKUP_VERSION "0.9.0"
+
+
+void bailOut(int code, char* message)
+{
+  fprintf(stderr, message);
+  exit(code);
+}
+
+
+/* Returns a newly malloc()ed string which contains original buffer,
+ * except for non-printable or special characters are quoted in hex
+ * with the syntax '\xQQ' where QQ is the hex ascii value of the quoted
+ * character.  A null terminator is added, since only ascii, not binary,
+ * is returned.
+ */
+static char* quote_buffer(const unsigned char* str, 
+			  unsigned int len, const char* special)
+{
+  unsigned int i, added_len;
+  unsigned int num_written = 0;
+
+  unsigned int buf_len = sizeof(char)*(len+1);
+  char* ret_val = malloc(buf_len);
+  char* tmp_buf;
+
+  if(ret_val == NULL)
+    return NULL;
+
+  for(i=0; i<len; i++)
+  {
+    if(buf_len <= (num_written+5))
+    {
+      /* Expand the buffer by the memory consumption rate seen so far 
+       * times the amount of input left to process.  The expansion is bounded 
+       * below by a minimum safety increase, and above by the maximum possible 
+       * output string length.  This should minimize both the number of 
+       * reallocs() and the amount of wasted memory.
+       */
+      added_len = (len-i)*num_written/(i+1);
+      if((buf_len+added_len) > (len*4+1))
+	buf_len = len*4+1;
+      else
+      {
+	if (added_len < 5)
+	  buf_len += 5;
+	else
+	  buf_len += added_len;
+      }
+
+      tmp_buf = realloc(ret_val, buf_len);
+      if(tmp_buf == NULL)
+      {
+	free(ret_val);
+	return NULL;
+      }
+      ret_val = tmp_buf;
+    }
+    
+    if(str[i] < 32 || str[i] > 126 || strchr(special, str[i]) != NULL)
+    {
+      num_written += snprintf(ret_val + num_written, buf_len - num_written,
+			      "\\x%.2X", str[i]);
+    }
+    else
+      ret_val[num_written++] = str[i];
+  }
+  ret_val[num_written] = '\0';
+
+  return ret_val;
+}
+
+
+/* Returns a newly malloc()ed string which contains original string, 
+ * except for non-printable or special characters are quoted in hex
+ * with the syntax '\xQQ' where QQ is the hex ascii value of the quoted
+ * character.
+ */
+static char* quote_string(const char* str, const char* special)
+{
+  unsigned int len;
+
+  if(str == NULL)
+    return NULL;
+
+  len = strlen(str);
+  return quote_buffer((const unsigned char*)str, len, special);
+}
+
+
+/*
+ * Convert from UTF-16LE to ASCII.  Accepts a Unicode buffer, uni, and
+ * it's length, uni_max.  Writes ASCII to the buffer ascii, whose size
+ * is ascii_max.  Writes at most (ascii_max-1) bytes to ascii, and null
+ * terminates the string.  Returns the length of the string stored in
+ * ascii.  On error, returns a negative errno code.
+ */
+static int uni_to_ascii(unsigned char* uni, char* ascii, 
+			unsigned int uni_max, unsigned int ascii_max)
+{
+  char* inbuf = (char*)uni;
+  char* outbuf = ascii;
+  size_t in_len = (size_t)uni_max;
+  size_t out_len = (size_t)(ascii_max-1);
+  int ret;
+
+  /* Set up conversion descriptor. */
+  conv_desc = iconv_open("US-ASCII", "UTF-16LE");
+
+  ret = iconv(conv_desc, &inbuf, &in_len, &outbuf, &out_len);
+  if(ret == -1)
+  {
+    iconv_close(conv_desc);
+    return -errno;
+  }
+  *outbuf = '\0';
+
+  iconv_close(conv_desc);  
+  return strlen(ascii);
+}
+
+
+/*
+ * Convert a data value to a string for display.  Returns NULL on error,
+ * and the string to display if there is no error, or a non-fatal
+ * error.  On any error (fatal or non-fatal) occurs, (*error_msg) will
+ * be set to a newly allocated string, containing an error message.  If
+ * a memory allocation failure occurs while generating the error
+ * message, both the return value and (*error_msg) will be NULL.  It
+ * is the responsibility of the caller to free both a non-NULL return
+ * value, and a non-NULL (*error_msg).
+ */
+static char* data_to_ascii(unsigned char* datap, uint32 len, uint32 type, 
+			   char** error_msg)
+{
+  char* asciip;
+  char* ascii;
+  unsigned char* cur_str;
+  char* cur_ascii;
+  char* cur_quoted;
+  char* tmp_err;
+  const char* str_type;
+  uint32 i;
+  uint32 cur_str_len;
+  uint32 ascii_max, cur_str_max;
+  uint32 str_rem, cur_str_rem, alen;
+  int ret_err;
+  unsigned short num_nulls;
+
+  if(datap == NULL)
+  {
+    *error_msg = (char*)malloc(24);
+    if(*error_msg == NULL)
+      return NULL;
+    strcpy(*error_msg, "Data pointer was NULL.");
+    return NULL;
+  }
+  *error_msg = NULL;
+
+  switch (type) 
+  {
+  case REG_SZ:
+  case REG_EXPAND_SZ:
+    /* REG_LINK is a symbolic link, stored as a unicode string. */
+  case REG_LINK:
+    ascii_max = sizeof(char)*(len+1);
+    ascii = malloc(ascii_max);
+    if(ascii == NULL)
+      return NULL;
+    
+    /* Sometimes values have binary stored in them.  If the unicode
+     * conversion fails, just quote it raw.
+     */
+    ret_err = uni_to_ascii(datap, ascii, len, ascii_max);
+    if(ret_err < 0)
+    {
+      tmp_err = strerror(-ret_err);
+      str_type = regfi_type_val2str(type);
+      *error_msg = (char*)malloc(65+strlen(str_type)+strlen(tmp_err)+1);
+      if(*error_msg == NULL)
+      {
+	free(ascii);
+	return NULL;
+      }
+      sprintf(*error_msg, "Unicode conversion failed on %s field; "
+	       "printing as binary.  Error: %s", str_type, tmp_err);
+      
+      cur_quoted = quote_buffer(datap, len, common_special_chars);
+    }
+    else
+      cur_quoted = quote_string(ascii, common_special_chars);
+    free(ascii);
+    if(cur_quoted == NULL)
+    {
+      *error_msg = (char*)malloc(27+1);
+      if(*error_msg != NULL)
+	strcpy(*error_msg, "Buffer could not be quoted.");
+    }
+    return cur_quoted;
+    break;
+
+  case REG_DWORD:
+    ascii_max = sizeof(char)*(8+2+1);
+    ascii = malloc(ascii_max);
+    if(ascii == NULL)
+      return NULL;
+
+    snprintf(ascii, ascii_max, "0x%.2X%.2X%.2X%.2X", 
+	     datap[3], datap[2], datap[1], datap[0]);
+    return ascii;
+    break;
+
+  case REG_DWORD_BE:
+    ascii_max = sizeof(char)*(8+2+1);
+    ascii = malloc(ascii_max);
+    if(ascii == NULL)
+      return NULL;
+
+    snprintf(ascii, ascii_max, "0x%.2X%.2X%.2X%.2X", 
+	     datap[0], datap[1], datap[2], datap[3]);
+    return ascii;
+    break;
+
+  case REG_QWORD:
+    ascii_max = sizeof(char)*(16+2+1);
+    ascii = malloc(ascii_max);
+    if(ascii == NULL)
+      return NULL;
+
+    snprintf(ascii, ascii_max, "0x%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X",
+	     datap[7], datap[6], datap[5], datap[4],
+	     datap[3], datap[2], datap[1], datap[0]);
+    return ascii;
+    break;
+    
+
+  /* XXX: this MULTI_SZ parser is pretty inefficient.  Should be
+   *      redone with fewer malloc calls and better string concatenation.
+   *      Also, gives lame output when "\0\0" is the string.
+   */
+  case REG_MULTI_SZ:
+    ascii_max = sizeof(char)*(len*4+1);
+    cur_str_max = sizeof(char)*(len+1);
+    cur_str = malloc(cur_str_max);
+    cur_ascii = malloc(cur_str_max);
+    ascii = malloc(ascii_max);
+    if(ascii == NULL || cur_str == NULL || cur_ascii == NULL)
+      return NULL;
+
+    /* Reads until it reaches 4 consecutive NULLs, 
+     * which is two nulls in unicode, or until it reaches len, or until we
+     * run out of buffer.  The latter should never happen, but we shouldn't
+     * trust our file to have the right lengths/delimiters.
+     */
+    asciip = ascii;
+    num_nulls = 0;
+    str_rem = ascii_max;
+    cur_str_rem = cur_str_max;
+    cur_str_len = 0;
+
+    for(i=0; (i < len) && str_rem > 0; i++)
+    {
+      *(cur_str+cur_str_len) = *(datap+i);
+      if(*(cur_str+cur_str_len) == 0)
+	num_nulls++;
+      else
+	num_nulls = 0;
+      cur_str_len++;
+
+      if(num_nulls == 2)
+      {
+	ret_err = uni_to_ascii(cur_str, cur_ascii, cur_str_len-1, cur_str_max);
+	if(ret_err < 0)
+	{
+	  /* XXX: should every sub-field error be enumerated? */
+	  if(*error_msg == NULL)
+	  {
+	    tmp_err = strerror(-ret_err);
+	    *error_msg = (char*)malloc(90+strlen(tmp_err)+1);
+	    if(*error_msg == NULL)
+	    {
+	      free(cur_str);
+	      free(cur_ascii);
+	      free(ascii);
+	      return NULL;
+	    }
+	    sprintf(*error_msg, "Unicode conversion failed on at least one "
+		    "MULTI_SZ sub-field; printing as binary.  Error: %s",
+		    tmp_err);
+	  }
+	  cur_quoted = quote_buffer(cur_str, cur_str_len-1, 
+				    subfield_special_chars);
+	}
+	else
+	  cur_quoted = quote_string(cur_ascii, subfield_special_chars);
+
+	alen = snprintf(asciip, str_rem, "%s", cur_quoted);
+	asciip += alen;
+	str_rem -= alen;
+	free(cur_quoted);
+
+	if(*(datap+i+1) == 0 && *(datap+i+2) == 0)
+	  break;
+	else
+	{
+	  if(str_rem > 0)
+	  {
+	    asciip[0] = '|';
+	    asciip[1] = '\0';
+	    asciip++;
+	    str_rem--;
+	  }
+	  memset(cur_str, 0, cur_str_max);
+	  cur_str_len = 0;
+	  num_nulls = 0;
+	  /* To eliminate leading nulls in subsequent strings. */
+	  i++;
+	}
+      }
+    }
+    *asciip = 0;
+    free(cur_str);
+    free(cur_ascii);
+    return ascii;
+    break;
+
+  /* XXX: Dont know what to do with these yet, just print as binary... */
+  default:
+    /* XXX: It would be really nice if this message somehow included the
+     *      name of the current value we're having trouble with, since
+     *      stderr/stdout don't always sync nicely.
+     */
+    fprintf(stderr, "WARNING: Unrecognized registry data type (0x%.8X); quoting as binary.\n", type);
+    
+  case REG_NONE:
+  case REG_RESOURCE_LIST:
+  case REG_FULL_RESOURCE_DESCRIPTOR:
+  case REG_RESOURCE_REQUIREMENTS_LIST:
+
+  case REG_BINARY:
+    return quote_buffer(datap, len, common_special_chars);
+    break;
+  }
+
+  return NULL;
+}
diff --git a/src/reglookup-recover.c b/src/reglookup-recover.c
new file mode 100644
index 0000000..e761a41
--- /dev/null
+++ b/src/reglookup-recover.c
@@ -0,0 +1,864 @@
+/*
+ * This program attempts to recover deleted data structures in a registry hive.
+ *
+ * Copyright (C) 2008 Timothy D. Morgan
+ *
+ * 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
+ * the Free Software Foundation; version 3 of the License.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
+ *
+ * $Id: reglookup-recover.c 121 2008-08-09 17:22:26Z tim $
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <sysexits.h>
+
+#include "../include/regfi.h"
+#include "../include/range_list.h"
+#include "../include/lru_cache.h"
+
+
+/* Globals, influenced by command line parameters */
+bool print_verbose = false;
+bool print_security = false;
+bool print_header = true;
+bool print_leftover = false;
+bool print_parsedraw = false;
+char* registry_file = NULL;
+
+#include "common.c"
+
+
+char* getQuotedData(int fd, uint32 offset, uint32 length)
+{
+  uint8* buf;
+  char* quoted_buf;
+  uint32 len;
+
+  if((lseek(fd, offset, SEEK_SET)) == -1)
+    return NULL;
+
+  buf = (uint8*)malloc(length);
+  if(buf == NULL)
+    return NULL;
+
+  len = length;
+  if((regfi_read(fd, buf, &length) != 0) || length != len)
+  {
+    free(buf);
+    return NULL;
+  }
+
+  quoted_buf = quote_buffer(buf, length, common_special_chars);
+  free(buf);
+
+  return quoted_buf;
+}
+
+
+void printKey(REGF_FILE* f, REGF_NK_REC* nk, const char* prefix)
+{
+  char mtime[20];
+  time_t tmp_time[1];
+  struct tm* tmp_time_s = NULL;
+  char* quoted_name = NULL;
+  char* quoted_raw = "";
+
+  *tmp_time = nt_time_to_unix(&nk->mtime);
+  tmp_time_s = gmtime(tmp_time);
+  strftime(mtime, sizeof(mtime), "%Y-%m-%d %H:%M:%S", tmp_time_s);
+
+  quoted_name = quote_string(nk->keyname, key_special_chars);
+  if (quoted_name == NULL)
+  {
+    quoted_name = malloc(1*sizeof(char));
+    if(quoted_name == NULL)
+      bailOut(EX_OSERR, "ERROR: Could not allocate sufficient memory.\n");
+    quoted_name[0] = '\0';
+
+    fprintf(stderr, "WARNING: NULL key name in NK record at offset %.8X.\n",
+	    nk->offset);
+  }
+
+  if(print_parsedraw)
+    quoted_raw = getQuotedData(f->fd, nk->offset, nk->cell_size);
+
+  printf("%.8X,%.8X,KEY,%s,%s,%s,%d,,,,,,,,%s\n", nk->offset, nk->cell_size,
+	 prefix, quoted_name, mtime, nk->num_values, quoted_raw);
+  
+  if(print_parsedraw)
+    free(quoted_raw);
+}
+
+
+void printValue(REGF_FILE* f, const REGF_VK_REC* vk, const char* prefix)
+{
+  char* quoted_value = NULL;
+  char* quoted_name = NULL;
+  char* quoted_raw = "";
+  char* conv_error = NULL;
+  const char* str_type = NULL;
+  uint32 size = vk->data_size;
+
+  /* Microsoft's documentation indicates that "available memory" is 
+   * the limit on value sizes.  Annoying.  We limit it to 1M which 
+   * should rarely be exceeded, unless the file is corrupt or 
+   * malicious. For more info, see:
+   *   http://msdn2.microsoft.com/en-us/library/ms724872.aspx
+   */
+  /* XXX: Should probably do something different here for this tool.
+   *      Also, It would be really nice if this message somehow included the
+   *      name of the current value we're having trouble with, since
+   *      stderr/stdout don't always sync nicely.
+   */
+  if(size > VK_MAX_DATA_LENGTH)
+  {
+    fprintf(stderr, "WARNING: value data size %d larger than "
+	    "%d, truncating...\n", size, VK_MAX_DATA_LENGTH);
+    size = VK_MAX_DATA_LENGTH;
+  }
+  
+  quoted_name = quote_string(vk->valuename, key_special_chars);
+  if (quoted_name == NULL)
+  { /* Value names are NULL when we're looking at the "(default)" value.
+     * Currently we just return a 0-length string to try an eliminate 
+     * ambiguity with a literal "(default)" value.  The data type of a line
+     * in the output allows one to differentiate between the parent key and
+     * this value.
+     */
+    quoted_name = malloc(1*sizeof(char));
+    if(quoted_name == NULL)
+      bailOut(EX_OSERR, "ERROR: Could not allocate sufficient memory.\n");
+    quoted_name[0] = '\0';
+  }
+
+  quoted_value = data_to_ascii(vk->data, size, vk->type, &conv_error);
+  if(quoted_value == NULL)
+  {
+    quoted_value = malloc(1*sizeof(char));
+    if(quoted_value == NULL)
+      bailOut(EX_OSERR, "ERROR: Could not allocate sufficient memory.\n");
+    quoted_value[0] = '\0';
+
+    if(conv_error == NULL)
+      fprintf(stderr, "WARNING: Could not quote value for '%s/%s'.  "
+	      "Memory allocation failure likely.\n", prefix, quoted_name);
+    else if(print_verbose)
+      fprintf(stderr, "WARNING: Could not quote value for '%s/%s'.  "
+	      "Returned error: %s\n", prefix, quoted_name, conv_error);
+  }
+  /* XXX: should these always be printed? */
+  else if(conv_error != NULL && print_verbose)
+    fprintf(stderr, "VERBOSE: While quoting value for '%s/%s', "
+	    "warning returned: %s\n", prefix, quoted_name, conv_error);
+
+
+  if(print_parsedraw)
+    quoted_raw = getQuotedData(f->fd, vk->offset, vk->cell_size);
+
+  str_type = regfi_type_val2str(vk->type);
+  if(str_type == NULL)
+    printf("%.8X,%.8X,VALUE,%s,%s,,,0x%.8X,%s,%d,,,,,%s\n", 
+	   vk->offset, vk->cell_size, prefix, quoted_name, 
+	   vk->type, quoted_value, vk->data_size, quoted_raw);
+  else
+    printf("%.8X,%.8X,VALUE,%s,%s,,,%s,%s,%d,,,,,%s\n", 
+	   vk->offset, vk->cell_size, prefix, quoted_name, 
+	   str_type, quoted_value, vk->data_size, quoted_raw);
+
+  if(print_parsedraw)
+    free(quoted_raw);
+  if(quoted_value != NULL)
+    free(quoted_value);
+  if(quoted_name != NULL)
+    free(quoted_name);
+  if(conv_error != NULL)
+    free(conv_error);
+}
+
+
+void printSK(REGF_FILE* f, REGF_SK_REC* sk)
+{
+  char* quoted_raw = NULL;
+  char* empty_str = "";
+  char* owner = regfi_get_owner(sk->sec_desc);
+  char* group = regfi_get_group(sk->sec_desc);
+  char* sacl = regfi_get_sacl(sk->sec_desc);
+  char* dacl = regfi_get_dacl(sk->sec_desc);
+
+  if(print_parsedraw)
+    quoted_raw = getQuotedData(f->fd, sk->offset, sk->cell_size);
+
+  if(owner == NULL)
+    owner = empty_str;
+  if(group == NULL)
+    group = empty_str;
+  if(sacl == NULL)
+    sacl = empty_str;
+  if(dacl == NULL)
+    dacl = empty_str;
+
+  printf("%.8X,%.8X,SK,,,,,,,,%s,%s,%s,%s,%s\n", sk->offset, sk->cell_size,
+	 owner, group, sacl, dacl, quoted_raw);
+  
+  if(owner != empty_str)
+    free(owner);
+  if(group != empty_str)
+    free(group);
+  if(sacl != empty_str)
+    free(sacl);
+  if(dacl != empty_str)
+    free(dacl);
+
+  if(print_parsedraw)
+    free(quoted_raw);
+}
+
+
+int printCell(REGF_FILE* f, uint32 offset)
+{
+  char* quoted_buf;
+  uint32 cell_length;
+  bool unalloc;
+
+  if(!regfi_parse_cell(f->fd, offset, NULL, 0, &cell_length, &unalloc))
+    return 1;
+
+  quoted_buf = getQuotedData(f->fd, offset, cell_length);
+  if(quoted_buf == NULL)
+    return 2;
+
+  printf("%.8X,%.8X,RAW,,,,,,,,,,,,%s\n", offset, cell_length, quoted_buf);
+
+  free(quoted_buf);
+  return 0;
+}
+
+
+/* This function returns a properly quoted parent path or partial parent 
+ * path for a given key.  Returns NULL on error, "" if no path was available.
+ * Paths returned must be free()d.
+ */
+/* XXX: This is not terribly efficient, as it may reparse many keys 
+ *      repeatedly.  Should try to add caching.  Also, piecing the path 
+ *      together is slow and redundant.
+ */
+char* getParentPath(REGF_FILE* f, REGF_NK_REC* nk)
+{
+  void_stack* path_stack = void_stack_new(REGF_MAX_DEPTH);
+  REGF_HBIN* hbin;
+  REGF_NK_REC* cur_ancestor;
+  char* ret_val;
+  char* path_element;
+  char* tmp_str;
+  uint32 virt_offset, i, stack_size, ret_val_size, ret_val_left, element_size;
+  uint32 max_length;
+
+  /* The path_stack size limit should guarantee that we don't recurse forever. */
+  virt_offset = nk->parent_off;
+  while(virt_offset != REGF_OFFSET_NONE)
+  {  
+    hbin = regfi_lookup_hbin(f, virt_offset);
+    if(hbin == NULL)
+      virt_offset = REGF_OFFSET_NONE;
+    else
+    {
+      max_length = hbin->block_size + hbin->file_off 
+	- (virt_offset+REGF_BLOCKSIZE);
+      cur_ancestor = regfi_parse_nk(f, virt_offset+REGF_BLOCKSIZE, 
+				    max_length, true);
+      if(cur_ancestor == NULL)
+	virt_offset = REGF_OFFSET_NONE;
+      else
+      {
+	if(cur_ancestor->key_type == NK_TYPE_ROOTKEY)
+	  virt_offset = REGF_OFFSET_NONE;
+	else
+	  virt_offset = cur_ancestor->parent_off;
+	
+	path_element = quote_string(cur_ancestor->keyname, key_special_chars);
+	if(path_element == NULL || !void_stack_push(path_stack, path_element))
+	{
+	  free(cur_ancestor->keyname);
+	  free(cur_ancestor);
+	  void_stack_free_deep(path_stack);
+	  return NULL;
+	}
+
+	regfi_key_free(cur_ancestor);
+      }
+    }
+  }
+  
+  stack_size = void_stack_size(path_stack);
+  ret_val_size = 16*stack_size;
+  if(ret_val_size == 0)
+    ret_val_size = 1;
+  ret_val_left = ret_val_size;
+  ret_val = malloc(ret_val_size);
+  if(ret_val == NULL)
+  {
+    void_stack_free_deep(path_stack);
+    return NULL;
+  }
+  ret_val[0] = '\0';
+
+  for(i=0; i<stack_size; i++)
+  {
+    path_element = void_stack_pop(path_stack);
+    element_size = strlen(path_element);
+    if(ret_val_left < element_size+2)
+    {
+      ret_val_size += element_size+16;
+      ret_val_left += element_size+16;
+      tmp_str = (char*)realloc(ret_val, ret_val_size);
+      if(tmp_str == NULL)
+      {
+	free(ret_val);
+	void_stack_free_deep(path_stack);
+	return NULL;
+      }
+      ret_val = tmp_str;
+    }
+
+    ret_val_left -= snprintf(ret_val+ret_val_size-ret_val_left,ret_val_left, "/%s", path_element);
+    free(path_element);
+  }
+  void_stack_free(path_stack);
+
+  return ret_val;
+}
+
+
+static void usage(void)
+{
+  fprintf(stderr, "Usage: reglookup-recover [options] <REGISTRY_FILE>\n");
+  fprintf(stderr, "Version: %s\n", REGLOOKUP_VERSION);
+  fprintf(stderr, "Options:\n");
+  fprintf(stderr, "\t-v\t sets verbose mode.\n");
+  fprintf(stderr, "\t-h\t enables header row. (default)\n");
+  fprintf(stderr, "\t-H\t disables header row.\n");
+  fprintf(stderr, "\t-l\t enables leftover(raw) cell output.\n");
+  fprintf(stderr, "\t-L\t disables leftover(raw) cell output. (default)\n");
+  fprintf(stderr, "\t-r\t enables raw cell output for parsed cells.\n");
+  fprintf(stderr, "\t-R\t disables raw cell output for parsed cells. (default)\n");
+  fprintf(stderr, "\n");
+}
+
+
+bool removeRange(range_list* rl, uint32 offset, uint32 length)
+{
+  int32 rm_idx;
+  const range_list_element* cur_elem;
+
+  rm_idx = range_list_find(rl, offset);
+  if(rm_idx < 0)
+  {
+    fprintf(stderr, "DEBUG: removeRange: rm_idx < 0; (%d)\n", rm_idx);
+    return false;
+  }
+
+  cur_elem = range_list_get(rl, rm_idx);
+  if(cur_elem == NULL)
+  {
+    fprintf(stderr, "DEBUG: removeRange: cur_elem == NULL.  rm_idx=%d\n", rm_idx);
+    return false;
+  }
+
+  if(offset > cur_elem->offset)
+  {
+    if(!range_list_split_element(rl, rm_idx, offset))
+    {
+      fprintf(stderr, "DEBUG: removeRange: first split failed\n");
+      return false;
+    }
+    rm_idx++;
+    cur_elem = range_list_get(rl, rm_idx);
+    if(cur_elem == NULL)
+    {
+      fprintf(stderr, 
+	      "DEBUG: removeRange: cur_elem == NULL after first split.  rm_idx=%d\n",
+	      rm_idx);
+      return false;
+    }
+  }
+  
+  if(offset+length < cur_elem->offset+cur_elem->length)
+  {
+    if(!range_list_split_element(rl, rm_idx, offset+length))
+    {
+      fprintf(stderr, "DEBUG: removeRange: second split failed\n");
+      return false;
+    }
+  }
+  
+  if(!range_list_remove(rl, rm_idx))
+  {
+    fprintf(stderr, "DEBUG: removeRange: remove failed\n");
+    return false;
+  }
+
+  return true;
+}
+
+
+/* NOTE: unalloc_keys should be an empty range_list. */
+int extractKeys(REGF_FILE* f, 
+		range_list* unalloc_cells, 
+		range_list* unalloc_keys)
+{
+  const range_list_element* cur_elem;
+  REGF_NK_REC* key;
+  uint32 i, j;
+
+  for(i=0; i < range_list_size(unalloc_cells); i++)
+  {
+    cur_elem = range_list_get(unalloc_cells, i);
+    for(j=0; cur_elem->length > REGFI_NK_MIN_LENGTH 
+	  && j <= cur_elem->length-REGFI_NK_MIN_LENGTH; j+=8)
+    {
+      key = regfi_parse_nk(f, cur_elem->offset+j,
+			   cur_elem->length-j, false);
+      if(key != NULL)
+      {
+	if(!range_list_add(unalloc_keys, key->offset, 
+			   key->cell_size, key))
+	{
+	  fprintf(stderr, "ERROR: Couldn't add key to unalloc_keys.\n");
+	  return 20;
+	}
+	j+=key->cell_size-8;
+      }
+    }
+  }
+
+  for(i=0; i<range_list_size(unalloc_keys); i++)
+  {
+    cur_elem = range_list_get(unalloc_keys, i);
+    if(!removeRange(unalloc_cells, cur_elem->offset, cur_elem->length))
+      return 30;
+  }
+
+  return 0;
+}
+
+
+int extractValueLists(REGF_FILE* f,
+		      range_list* unalloc_cells,
+		      range_list* unalloc_keys)
+{
+  REGF_NK_REC* nk;
+  REGF_HBIN* hbin;
+  const range_list_element* cur_elem;
+  uint32 i, j, num_keys, off, values_length, max_length;
+
+  num_keys=range_list_size(unalloc_keys);
+  for(i=0; i<num_keys; i++)
+  {
+    cur_elem = range_list_get(unalloc_keys, i);
+    if(cur_elem == NULL)
+      return 10;
+    nk = cur_elem->data;
+
+    if(nk->num_values && (nk->values_off!=REGF_OFFSET_NONE))
+    {
+      hbin = regfi_lookup_hbin(f, nk->values_off);
+      
+      if(hbin != NULL)
+      {
+	off = nk->values_off + REGF_BLOCKSIZE;
+	max_length = hbin->block_size + hbin->file_off - off;
+	/* XXX: This is a hack.  We parse all value-lists, VK records,
+	 *      and data records without regard for current allocation status.  
+	 *      On the off chance that such a record correctly parsed but is 
+	 *      actually a reallocated structure used by something else, we 
+	 *      simply prune it after the fact.  Would be faster to check this
+	 *      up front somehow.
+	 */
+	nk->values = regfi_load_valuelist(f, off, nk->num_values, max_length,
+					  false);
+	values_length = (nk->num_values+1)*sizeof(uint32);
+	if(values_length != (values_length & 0xFFFFFFF8))
+	  values_length = (values_length & 0xFFFFFFF8) + 8;
+
+	if(nk->values != NULL)
+	{
+	  if(!range_list_has_range(unalloc_cells, off, values_length))
+	  { /* We've parsed a values-list which isn't in the unallocated list,
+	     * so prune it. 
+	     */
+	    for(j=0; j<nk->num_values; j++)
+	    {
+	      if(nk->values[j] != NULL)
+	      {
+		if(nk->values[j]->data != NULL)
+		  free(nk->values[j]->data);
+		free(nk->values[j]);
+	      }
+	    }
+	    free(nk->values);
+	    nk->values = NULL;
+	  }
+	  else
+	  { /* Values-list was recovered.  Remove from unalloc_cells and 
+	     * inspect values. 
+	     */
+	    if(!removeRange(unalloc_cells, off, values_length))
+	      return 20;
+
+	    for(j=0; j < nk->num_values; j++)
+	    {
+	      if(nk->values[j] != NULL)
+	      {
+		if(!range_list_has_range(unalloc_cells, nk->values[j]->offset, 
+					 nk->values[j]->cell_size))
+		{ /* We've parsed a value which isn't in the unallocated list,
+		   * so prune it.
+		   */
+		  if(nk->values[j]->data != NULL)
+		    free(nk->values[j]->data);
+		  free(nk->values[j]);
+		  nk->values[j] = NULL;
+		}
+		else
+		{
+		  /* A VK record was recovered.  Remove from unalloc_cells
+		   * and inspect data.
+		   */
+		  if(!removeRange(unalloc_cells, nk->values[j]->offset,
+				  nk->values[j]->cell_size))
+		    return 21;
+
+		  /* Don't bother pruning or removing from unalloc_cells if 
+		   * there is no data, or it is stored in the offset.
+		   */
+		  if(nk->values[j]->data != NULL && !nk->values[j]->data_in_offset)
+		  {
+		    off = nk->values[j]->data_off+REGF_BLOCKSIZE;
+		    if(!range_list_has_range(unalloc_cells, off, 
+					     nk->values[j]->data_size))
+		    { /* We've parsed a data cell which isn't in the unallocated 
+		       * list, so prune it.
+		       */
+		      free(nk->values[j]->data);
+		      nk->values[j]->data = NULL;
+		    }
+		    else
+		    { /*A data record was recovered. Remove from unalloc_cells.*/
+		      if(!removeRange(unalloc_cells, off, 
+				      nk->values[j]->data_size))
+			return 22;
+		    }
+		  }
+		}
+	      }
+	    }
+	  }
+	}
+      }
+    }
+  }
+
+  return 0;
+}
+
+
+/* NOTE: unalloc_values should be an empty range_list. */
+int extractValues(REGF_FILE* f,
+		  range_list* unalloc_cells,
+		  range_list* unalloc_values)
+{
+  const range_list_element* cur_elem;
+  REGF_VK_REC* vk;
+  uint32 i, j, off;
+
+  for(i=0; i < range_list_size(unalloc_cells); i++)
+  {
+    cur_elem = range_list_get(unalloc_cells, i);
+    for(j=0; j <= cur_elem->length; j+=8)
+    {
+      vk = regfi_parse_vk(f, cur_elem->offset+j, 
+			   cur_elem->length-j, false);
+      if(vk != NULL)
+      {
+	if(!range_list_add(unalloc_values, vk->offset,
+			   vk->cell_size, vk))
+	{
+	  fprintf(stderr, "ERROR: Couldn't add value to unalloc_values.\n");
+	  return 20;
+	}
+	j+=vk->cell_size-8;
+      }
+    }
+  }
+  
+  /* Remove value ranges from the unalloc_cells before we continue. */
+  for(i=0; i<range_list_size(unalloc_values); i++)
+  {
+    cur_elem = range_list_get(unalloc_values, i);
+    if(!removeRange(unalloc_cells, cur_elem->offset, cur_elem->length))
+      return 30;
+  }
+
+  /* Now see if the data associated with each value is intact */
+  for(i=0; i<range_list_size(unalloc_values); i++)
+  {
+    cur_elem = range_list_get(unalloc_values, i);
+    vk = (REGF_VK_REC*)cur_elem->data;
+    if(vk == NULL)
+      return 40;
+
+    if(vk->data != NULL && !vk->data_in_offset)
+    {
+      off = vk->data_off+REGF_BLOCKSIZE;
+      if(!range_list_has_range(unalloc_cells, off, vk->data_size))
+      { /* We've parsed a data cell which isn't in the unallocated 
+	 * list, so prune it.
+	 */
+	free(vk->data);
+	vk->data = NULL;
+      }
+      else
+      { /*A data record was recovered. Remove from unalloc_cells.*/
+	if(!removeRange(unalloc_cells, off, vk->data_size))
+	  return 50;
+      }
+    }
+  }
+
+  return 0;
+}
+
+
+/* NOTE: unalloc_sks should be an empty range_list. */
+int extractSKs(REGF_FILE* f, 
+	       range_list* unalloc_cells,
+	       range_list* unalloc_sks)
+{
+  const range_list_element* cur_elem;
+  REGF_SK_REC* sk;
+  uint32 i, j;
+
+  for(i=0; i < range_list_size(unalloc_cells); i++)
+  {
+    cur_elem = range_list_get(unalloc_cells, i);
+    for(j=0; j <= cur_elem->length; j+=8)
+    {
+      sk = regfi_parse_sk(f, cur_elem->offset+j, 
+			  cur_elem->length-j, false);
+      if(sk != NULL)
+      {
+	if(!range_list_add(unalloc_sks, sk->offset,
+			   sk->cell_size, sk))
+	{
+	  fprintf(stderr, "ERROR: Couldn't add sk to unalloc_sks.\n");
+	  return 20;
+	}
+	j+=sk->cell_size-8;
+      }
+    }
+  }
+
+  for(i=0; i<range_list_size(unalloc_sks); i++)
+  {
+    cur_elem = range_list_get(unalloc_sks, i);
+    if(!removeRange(unalloc_cells, cur_elem->offset, cur_elem->length))
+      return 30;
+  }
+
+  return 0;
+}
+
+
+int main(int argc, char** argv)
+{ 
+  REGF_FILE* f;
+  const range_list_element* cur_elem;
+  range_list* unalloc_cells;
+  range_list* unalloc_keys;
+  range_list* unalloc_values;
+  range_list* unalloc_sks;
+  char** parent_paths;
+  char* tmp_name;
+  char* tmp_path;
+  REGF_NK_REC* tmp_key;
+  REGF_VK_REC* tmp_value;
+  uint32 argi, arge, i, j, ret, num_unalloc_keys;
+  /* uint32 test_offset;*/
+  
+  /* Process command line arguments */
+  if(argc < 2)
+  {
+    usage();
+    bailOut(EX_USAGE, "ERROR: Requires at least one argument.\n");
+  }
+  
+  arge = argc-1;
+  for(argi = 1; argi < arge; argi++)
+  {
+    if (strcmp("-v", argv[argi]) == 0)
+      print_verbose = true;
+    else if (strcmp("-h", argv[argi]) == 0)
+      print_header = true;
+    else if (strcmp("-H", argv[argi]) == 0)
+      print_header = false;
+    else if (strcmp("-l", argv[argi]) == 0)
+      print_leftover = true;
+    else if (strcmp("-L", argv[argi]) == 0)
+      print_leftover = false;
+    else if (strcmp("-r", argv[argi]) == 0)
+      print_parsedraw = true;
+    else if (strcmp("-R", argv[argi]) == 0)
+      print_parsedraw = false;
+    else
+    {
+      usage();
+      fprintf(stderr, "ERROR: Unrecognized option: %s\n", argv[argi]);
+      bailOut(EX_USAGE, "");
+    }
+  }
+  /*test_offset = strtol(argv[argi++], NULL, 16);*/
+
+  if((registry_file = strdup(argv[argi])) == NULL)
+    bailOut(EX_OSERR, "ERROR: Memory allocation problem.\n");
+
+  f = regfi_open(registry_file);
+  if(f == NULL)
+  {
+    fprintf(stderr, "ERROR: Couldn't open registry file: %s\n", registry_file);
+    bailOut(EX_NOINPUT, "");
+  }
+
+  if(print_header)
+    printf("OFFSET,REC_LENGTH,REC_TYPE,PATH,NAME,"
+	   "NK_MTIME,NK_NVAL,VK_TYPE,VK_VALUE,VK_DATA_LEN,"
+	   "SK_OWNER,SK_GROUP,SK_SACL,SK_DACL,RAW_CELL\n");
+
+  unalloc_cells = regfi_parse_unalloc_cells(f);
+  if(unalloc_cells == NULL)
+  {
+    fprintf(stderr, "ERROR: Could not obtain list of unallocated cells.\n");
+    return 1;
+  }
+
+  unalloc_keys = range_list_new();
+  if(unalloc_keys == NULL)
+    return 10;
+
+  unalloc_values = range_list_new();
+  if(unalloc_values == NULL)
+    return 10;
+
+  unalloc_sks = range_list_new();
+  if(unalloc_sks == NULL)
+    return 10;
+
+  ret = extractKeys(f, unalloc_cells, unalloc_keys);
+  if(ret != 0)
+  {
+    fprintf(stderr, "ERROR: extractKeys() failed with %d.\n", ret);
+    return ret;
+  }
+
+  ret = extractValueLists(f, unalloc_cells, unalloc_keys);
+  if(ret != 0)
+  {
+    fprintf(stderr, "ERROR: extractValueLists() failed with %d.\n", ret);
+    return ret;
+  }
+
+  /* Carve any orphan values and associated data */
+  ret = extractValues(f, unalloc_cells, unalloc_values);
+  if(ret != 0)
+  {
+    fprintf(stderr, "ERROR: extractValues() failed with %d.\n", ret);
+    return ret;
+  }
+
+  /* Carve any SK records */
+  ret = extractSKs(f, unalloc_cells, unalloc_sks);
+  if(ret != 0)
+  {
+    fprintf(stderr, "ERROR: extractSKs() failed with %d.\n", ret);
+    return ret;
+  }
+
+  /* Now that we're done carving, associate recovered keys with parents, 
+   * if at all possible.
+   */
+  num_unalloc_keys = range_list_size(unalloc_keys);
+  parent_paths = (char**)malloc(sizeof(char*)*num_unalloc_keys);
+  if(parent_paths == NULL)
+    return 10;
+
+  for(i=0; i < num_unalloc_keys; i++)
+  {
+    cur_elem = range_list_get(unalloc_keys, i);
+    tmp_key = (REGF_NK_REC*)cur_elem->data;
+
+    if(tmp_key == NULL)
+      return 20;
+    
+    parent_paths[i] = getParentPath(f, tmp_key);
+    if(parent_paths[i] == NULL)
+      return 20;
+  }
+  
+  /* Now start the output */
+
+  for(i=0; i < num_unalloc_keys; i++)
+  {
+    cur_elem = range_list_get(unalloc_keys, i);
+    tmp_key = (REGF_NK_REC*)cur_elem->data;
+
+    printKey(f, tmp_key, parent_paths[i]);
+    if(tmp_key->num_values > 0 && tmp_key->values != NULL)
+    {
+      tmp_name = quote_string(tmp_key->keyname, key_special_chars);
+      tmp_path = (char*)malloc(strlen(parent_paths[i])+strlen(tmp_name)+2);
+      if(tmp_path == NULL)
+	return 10;
+      sprintf(tmp_path, "%s/%s", parent_paths[i], tmp_name);
+      for(j=0; j < tmp_key->num_values; j++)
+      {
+	tmp_value = tmp_key->values[j];
+	if(tmp_value != NULL)
+	  printValue(f, tmp_value, tmp_path);
+      }
+      free(tmp_path);
+      free(tmp_name);
+      free(parent_paths[i]);
+    }
+  }
+  free(parent_paths);
+
+  /* Print out orphaned values */
+  for(i=0; i < range_list_size(unalloc_values); i++)
+  {
+    cur_elem = range_list_get(unalloc_values, i);
+    tmp_value = (REGF_VK_REC*)cur_elem->data; 
+
+    printValue(f, tmp_value, "");
+  }
+  
+  if(print_leftover)
+  {
+    for(i=0; i < range_list_size(unalloc_cells); i++)
+    {
+      cur_elem = range_list_get(unalloc_cells, i);
+      printCell(f, cur_elem->offset);
+    }
+  }
+
+  return 0;
+}
diff --git a/src/reglookup.c b/src/reglookup.c
index ebdac0b..4eb43c9 100644
--- a/src/reglookup.c
+++ b/src/reglookup.c
@@ -2,12 +2,12 @@
  * A utility to read a Windows NT/2K/XP/2K3 registry file, using 
  * Gerald Carter''s regfio interface.
  *
- * Copyright (C) 2005-2007 Timothy D. Morgan
+ * Copyright (C) 2005-2008 Timothy D. Morgan
  * Copyright (C) 2002 Richard Sharpe, rsharpe at richardsharpe.com
  *
  * 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
- * the Free Software Foundation; version 2 of the License.
+ * the Free Software Foundation; version 3 of the License.
  * 
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -18,7 +18,7 @@
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  
  *
- * $Id: reglookup.c 96 2007-03-29 01:41:33Z tim $
+ * $Id: reglookup.c 121 2008-08-09 17:22:26Z tim $
  */
 
 
@@ -28,7 +28,6 @@
 #include <string.h>
 #include <strings.h>
 #include <time.h>
-#include <iconv.h>
 #include "../include/regfi.h"
 #include "../include/void_stack.h"
 
@@ -43,345 +42,95 @@ int type_filter;
 char* registry_file = NULL;
 
 /* Other globals */
-const char* key_special_chars = ",\"\\/";
-const char* subfield_special_chars = ",\"\\|";
-const char* common_special_chars = ",\"\\";
+REGF_FILE* f;
 
-iconv_t conv_desc;
 
-
-void bailOut(int code, char* message)
-{
-  fprintf(stderr, message);
-  exit(code);
-}
-
-
-/* Returns a newly malloc()ed string which contains original buffer,
- * except for non-printable or special characters are quoted in hex
- * with the syntax '\xQQ' where QQ is the hex ascii value of the quoted
- * character.  A null terminator is added, since only ascii, not binary,
- * is returned.
+/* XXX: A hack to share some functions with reglookup-recover.c.
+ *      Should move these into a properly library at some point.
  */
-static char* quote_buffer(const unsigned char* str, 
-			  unsigned int len, const char* special)
-{
-  unsigned int i, added_len;
-  unsigned int num_written = 0;
+#include "common.c"
 
-  unsigned int buf_len = sizeof(char)*(len+1);
-  char* ret_val = malloc(buf_len);
-  char* tmp_buf;
 
-  if(ret_val == NULL)
-    return NULL;
+void printValue(const REGF_VK_REC* vk, char* prefix)
+{
+  char* quoted_value = NULL;
+  char* quoted_name = NULL;
+  char* conv_error = NULL;
+  const char* str_type = NULL;
+  uint32 size = vk->data_size;
 
-  for(i=0; i<len; i++)
+  /* Microsoft's documentation indicates that "available memory" is 
+   * the limit on value sizes.  Annoying.  We limit it to 1M which 
+   * should rarely be exceeded, unless the file is corrupt or 
+   * malicious. For more info, see:
+   *   http://msdn2.microsoft.com/en-us/library/ms724872.aspx
+   */
+  if(size > VK_MAX_DATA_LENGTH)
   {
-    if(buf_len <= (num_written+5))
-    {
-      /* Expand the buffer by the memory consumption rate seen so far 
-       * times the amount of input left to process.  The expansion is bounded 
-       * below by a minimum safety increase, and above by the maximum possible 
-       * output string length.  This should minimize both the number of 
-       * reallocs() and the amount of wasted memory.
-       */
-      added_len = (len-i)*num_written/(i+1);
-      if((buf_len+added_len) > (len*4+1))
-	buf_len = len*4+1;
-      else
-      {
-	if (added_len < 5)
-	  buf_len += 5;
-	else
-	  buf_len += added_len;
-      }
-
-      tmp_buf = realloc(ret_val, buf_len);
-      if(tmp_buf == NULL)
-      {
-	free(ret_val);
-	return NULL;
-      }
-      ret_val = tmp_buf;
-    }
-    
-    if(str[i] < 32 || str[i] > 126 || strchr(special, str[i]) != NULL)
-    {
-      num_written += snprintf(ret_val + num_written, buf_len - num_written,
-			      "\\x%.2X", str[i]);
-    }
-    else
-      ret_val[num_written++] = str[i];
+    fprintf(stderr, "WARNING: value data size %d larger than "
+	    "%d, truncating...\n", size, VK_MAX_DATA_LENGTH);
+    size = VK_MAX_DATA_LENGTH;
+  }
+  
+  quoted_name = quote_string(vk->valuename, key_special_chars);
+  if (quoted_name == NULL)
+  { /* Value names are NULL when we're looking at the "(default)" value.
+     * Currently we just return a 0-length string to try an eliminate 
+     * ambiguity with a literal "(default)" value.  The data type of a line
+     * in the output allows one to differentiate between the parent key and
+     * this value.
+     */
+    quoted_name = malloc(1*sizeof(char));
+    if(quoted_name == NULL)
+      bailOut(EX_OSERR, "ERROR: Could not allocate sufficient memory.\n");
+    quoted_name[0] = '\0';
   }
-  ret_val[num_written] = '\0';
-
-  return ret_val;
-}
-
-
-/* Returns a newly malloc()ed string which contains original string, 
- * except for non-printable or special characters are quoted in hex
- * with the syntax '\xQQ' where QQ is the hex ascii value of the quoted
- * character.
- */
-static char* quote_string(const char* str, const char* special)
-{
-  unsigned int len;
-
-  if(str == NULL)
-    return NULL;
-
-  len = strlen(str);
-  return quote_buffer((const unsigned char*)str, len, special);
-}
-
-
-/*
- * Convert from UTF-16LE to ASCII.  Accepts a Unicode buffer, uni, and
- * it's length, uni_max.  Writes ASCII to the buffer ascii, whose size
- * is ascii_max.  Writes at most (ascii_max-1) bytes to ascii, and null
- * terminates the string.  Returns the length of the string stored in
- * ascii.  On error, returns a negative errno code.
- */
-static int uni_to_ascii(unsigned char* uni, char* ascii, 
-			unsigned int uni_max, unsigned int ascii_max)
-{
-  char* inbuf = (char*)uni;
-  char* outbuf = ascii;
-  size_t in_len = (size_t)uni_max;
-  size_t out_len = (size_t)(ascii_max-1);
-  int ret;
-
-  /* Set up conversion descriptor. */
-  conv_desc = iconv_open("US-ASCII", "UTF-16LE");
 
-  ret = iconv(conv_desc, &inbuf, &in_len, &outbuf, &out_len);
-  if(ret == -1)
+  quoted_value = data_to_ascii(vk->data, size, vk->type, &conv_error);
+  if(quoted_value == NULL)
   {
-    iconv_close(conv_desc);
-    return -errno;
+    if(conv_error == NULL)
+      fprintf(stderr, "WARNING: Could not quote value for '%s/%s'.  "
+	      "Memory allocation failure likely.\n", prefix, quoted_name);
+    else if(print_verbose)
+      fprintf(stderr, "WARNING: Could not quote value for '%s/%s'.  "
+	      "Returned error: %s\n", prefix, quoted_name, conv_error);
   }
-  *outbuf = '\0';
-
-  iconv_close(conv_desc);  
-  return strlen(ascii);
-}
-
-
-/*
- * Convert a data value to a string for display.  Returns NULL on error,
- * and the string to display if there is no error, or a non-fatal
- * error.  On any error (fatal or non-fatal) occurs, (*error_msg) will
- * be set to a newly allocated string, containing an error message.  If
- * a memory allocation failure occurs while generating the error
- * message, both the return value and (*error_msg) will be NULL.  It
- * is the responsibility of the caller to free both a non-NULL return
- * value, and a non-NULL (*error_msg).
- */
-static char* data_to_ascii(unsigned char *datap, uint32 len, uint32 type, 
-			   char** error_msg)
-{
-  char* asciip;
-  char* ascii;
-  unsigned char* cur_str;
-  char* cur_ascii;
-  char* cur_quoted;
-  char* tmp_err;
-  const char* str_type;
-  uint32 i;
-  uint32 cur_str_len;
-  uint32 ascii_max, cur_str_max;
-  uint32 str_rem, cur_str_rem, alen;
-  int ret_err;
-  unsigned short num_nulls;
-
-  *error_msg = NULL;
+  /* XXX: should these always be printed? */
+  else if(conv_error != NULL && print_verbose)
+    fprintf(stderr, "VERBOSE: While quoting value for '%s/%s', "
+	    "warning returned: %s\n", prefix, quoted_name, conv_error);
 
-  switch (type) 
+  str_type = regfi_type_val2str(vk->type);
+  if(print_security)
   {
-  case REG_SZ:
-  case REG_EXPAND_SZ:
-    /* REG_LINK is a symbolic link, stored as a unicode string. */
-  case REG_LINK:
-    ascii_max = sizeof(char)*(len+1);
-    ascii = malloc(ascii_max);
-    if(ascii == NULL)
-      return NULL;
-    
-    /* Sometimes values have binary stored in them.  If the unicode
-     * conversion fails, just quote it raw.
-     */
-    ret_err = uni_to_ascii(datap, ascii, len, ascii_max);
-    if(ret_err < 0)
-    {
-      tmp_err = strerror(-ret_err);
-      str_type = regfi_type_val2str(type);
-      *error_msg = (char*)malloc(65+strlen(str_type)+strlen(tmp_err)+1);
-      if(*error_msg == NULL)
-      {
-	free(ascii);
-	return NULL;
-      }
-      sprintf(*error_msg, "Unicode conversion failed on %s field; "
-	       "printing as binary.  Error: %s", str_type, tmp_err);
-      
-      cur_quoted = quote_buffer(datap, len, common_special_chars);
-    }
+    if(str_type == NULL)
+      printf("%s/%s,0x%.8X,%s,,,,,\n", prefix, quoted_name,
+	     vk->type, quoted_value);
     else
-      cur_quoted = quote_string(ascii, common_special_chars);
-    free(ascii);
-    if(cur_quoted == NULL)
-    {
-      *error_msg = (char*)malloc(27+1);
-      if(*error_msg != NULL)
-	strcpy(*error_msg, "Buffer could not be quoted.");
-    }
-    return cur_quoted;
-    break;
-
-  case REG_DWORD:
-    ascii_max = sizeof(char)*(8+2+1);
-    ascii = malloc(ascii_max);
-    if(ascii == NULL)
-      return NULL;
-
-    snprintf(ascii, ascii_max, "0x%.2X%.2X%.2X%.2X", 
-	     datap[0], datap[1], datap[2], datap[3]);
-    return ascii;
-    break;
-
-  case REG_DWORD_BE:
-    ascii_max = sizeof(char)*(8+2+1);
-    ascii = malloc(ascii_max);
-    if(ascii == NULL)
-      return NULL;
-
-    snprintf(ascii, ascii_max, "0x%.2X%.2X%.2X%.2X", 
-	     datap[3], datap[2], datap[1], datap[0]);
-    return ascii;
-    break;
-
-  case REG_QWORD:
-    ascii_max = sizeof(char)*(16+2+1);
-    ascii = malloc(ascii_max);
-    if(ascii == NULL)
-      return NULL;
-
-    snprintf(ascii, ascii_max, "0x%.2X%.2X%.2X%.2X%.2X%.2X%.2X%.2X",
-	     datap[7], datap[6], datap[5], datap[4],
-	     datap[3], datap[2], datap[1], datap[0]);
-    return ascii;
-    break;
-    
-
-  /* XXX: this MULTI_SZ parser is pretty inefficient.  Should be
-   *      redone with fewer malloc calls and better string concatenation. 
-   */
-  case REG_MULTI_SZ:
-    ascii_max = sizeof(char)*(len*4+1);
-    cur_str_max = sizeof(char)*(len+1);
-    cur_str = malloc(cur_str_max);
-    cur_ascii = malloc(cur_str_max);
-    ascii = malloc(ascii_max);
-    if(ascii == NULL || cur_str == NULL || cur_ascii == NULL)
-      return NULL;
-
-    /* Reads until it reaches 4 consecutive NULLs, 
-     * which is two nulls in unicode, or until it reaches len, or until we
-     * run out of buffer.  The latter should never happen, but we shouldn't
-     * trust our file to have the right lengths/delimiters.
-     */
-    asciip = ascii;
-    num_nulls = 0;
-    str_rem = ascii_max;
-    cur_str_rem = cur_str_max;
-    cur_str_len = 0;
-
-    for(i=0; (i < len) && str_rem > 0; i++)
-    {
-      *(cur_str+cur_str_len) = *(datap+i);
-      if(*(cur_str+cur_str_len) == 0)
-	num_nulls++;
-      else
-	num_nulls = 0;
-      cur_str_len++;
-
-      if(num_nulls == 2)
-      {
-	ret_err = uni_to_ascii(cur_str, cur_ascii, cur_str_len-1, cur_str_max);
-	if(ret_err < 0)
-	{
-	  /* XXX: should every sub-field error be enumerated? */
-	  if(*error_msg == NULL)
-	  {
-	    tmp_err = strerror(-ret_err);
-	    *error_msg = (char*)malloc(90+strlen(tmp_err)+1);
-	    if(*error_msg == NULL)
-	    {
-	      free(cur_str);
-	      free(cur_ascii);
-	      free(ascii);
-	      return NULL;
-	    }
-	    sprintf(*error_msg, "Unicode conversion failed on at least one "
-		    "MULTI_SZ sub-field; printing as binary.  Error: %s",
-		    tmp_err);
-	  }
-	  cur_quoted = quote_buffer(cur_str, cur_str_len-1, 
-				    subfield_special_chars);
-	}
-	else
-	  cur_quoted = quote_string(cur_ascii, subfield_special_chars);
-
-	alen = snprintf(asciip, str_rem, "%s", cur_quoted);
-	asciip += alen;
-	str_rem -= alen;
-	free(cur_quoted);
-
-	if(*(datap+i+1) == 0 && *(datap+i+2) == 0)
-	  break;
-	else
-	{
-	  if(str_rem > 0)
-	  {
-	    asciip[0] = '|';
-	    asciip[1] = '\0';
-	    asciip++;
-	    str_rem--;
-	  }
-	  memset(cur_str, 0, cur_str_max);
-	  cur_str_len = 0;
-	  num_nulls = 0;
-	  /* To eliminate leading nulls in subsequent strings. */
-	  i++;
-	}
-      }
-    }
-    *asciip = 0;
-    free(cur_str);
-    free(cur_ascii);
-    return ascii;
-    break;
-
-  /* XXX: Dont know what to do with these yet, just print as binary... */
-  default:
-    fprintf(stderr, "WARNING: Unrecognized registry data type (0x%.8X); quoting as binary.\n", type);
-    
-  case REG_NONE:
-  case REG_RESOURCE_LIST:
-  case REG_FULL_RESOURCE_DESCRIPTOR:
-  case REG_RESOURCE_REQUIREMENTS_LIST:
-
-  case REG_BINARY:
-    return quote_buffer(datap, len, common_special_chars);
-    break;
+      printf("%s/%s,%s,%s,,,,,\n", prefix, quoted_name,
+	     str_type, quoted_value);
+  }
+  else
+  {
+    if(str_type == NULL)
+      printf("%s/%s,0x%.8X,%s,\n", prefix, quoted_name,
+	     vk->type, quoted_value);
+    else
+      printf("%s/%s,%s,%s,\n", prefix, quoted_name,
+	     str_type, quoted_value);
   }
 
-  return NULL;
+  if(quoted_value != NULL)
+    free(quoted_value);
+  if(quoted_name != NULL)
+    free(quoted_name);
+  if(conv_error != NULL)
+    free(conv_error);
 }
 
 
+
 /* XXX: Each chunk must be unquoted after it is split out. 
  *      Quoting syntax may need to be standardized and pushed into the API 
  *      to deal with this issue and others.
@@ -524,109 +273,6 @@ char* iter2Path(REGFI_ITERATOR* i)
 }
 
 
-void printValue(const REGF_VK_REC* vk, char* prefix)
-{
-  char* quoted_value = NULL;
-  char* quoted_name = NULL;
-  char* conv_error = NULL;
-  const char* str_type = NULL;
-  uint32 size;
-  uint8 tmp_buf[4];
-
-  /* Thanks Microsoft for making this process so straight-forward!!! */
-  /* XXX: this logic should be abstracted  and pushed into the regfi 
-   *      interface.  This includes the size limits.
-   */
-  size = (vk->data_size & ~VK_DATA_IN_OFFSET);
-  if(vk->data_size & VK_DATA_IN_OFFSET)
-  {
-    tmp_buf[0] = (uint8)((vk->data_off >> 3) & 0xFF);
-    tmp_buf[1] = (uint8)((vk->data_off >> 2) & 0xFF);
-    tmp_buf[2] = (uint8)((vk->data_off >> 1) & 0xFF);
-    tmp_buf[3] = (uint8)(vk->data_off & 0xFF);
-    if(size > 4)
-    {
-      fprintf(stderr, "WARNING: value stored in offset larger than 4. "
-	      "Truncating...\n");
-      size = 4;
-    }
-    quoted_value = data_to_ascii(tmp_buf, 4, vk->type, &conv_error);
-  }
-  else
-  {
-    /* Microsoft's documentation indicates that "available memory" is 
-     * the limit on value sizes.  Annoying.  We limit it to 1M which 
-     * should rarely be exceeded, unless the file is corrupt or 
-     * malicious. For more info, see:
-     *   http://msdn2.microsoft.com/en-us/library/ms724872.aspx
-     */
-    if(size > VK_MAX_DATA_LENGTH)
-    {
-      fprintf(stderr, "WARNING: value data size %d larger than "
-	      "%d, truncating...\n", size, VK_MAX_DATA_LENGTH);
-      size = VK_MAX_DATA_LENGTH;
-    }
-
-    quoted_value = data_to_ascii(vk->data, vk->data_size, 
-				 vk->type, &conv_error);
-  }
-  
-  /* XXX: Sometimes value names can be NULL in registry.  Need to
-   *      figure out why and when, and generate the appropriate output
-   *      for that condition.
-   */
-  quoted_name = quote_string(vk->valuename, common_special_chars);
-  if (quoted_name == NULL)
-  {
-    quoted_name = malloc(1*sizeof(char));
-    if(quoted_name == NULL)
-      bailOut(EX_OSERR, "ERROR: Could not allocate sufficient memory.\n");
-    quoted_name[0] = '\0';
-  }
-
-  if(quoted_value == NULL)
-  {
-    if(conv_error == NULL)
-      fprintf(stderr, "WARNING: Could not quote value for '%s/%s'.  "
-	      "Memory allocation failure likely.\n", prefix, quoted_name);
-    else
-      fprintf(stderr, "WARNING: Could not quote value for '%s/%s'.  "
-	      "Returned error: %s\n", prefix, quoted_name, conv_error);
-  }
-  /* XXX: should these always be printed? */
-  else if(conv_error != NULL && print_verbose)
-      fprintf(stderr, "VERBOSE: While quoting value for '%s/%s', "
-	      "warning returned: %s\n", prefix, quoted_name, conv_error);
-
-  str_type = regfi_type_val2str(vk->type);
-  if(print_security)
-  {
-    if(str_type == NULL)
-      printf("%s/%s,0x%.8X,%s,,,,,\n", prefix, quoted_name,
-	     vk->type, quoted_value);
-    else
-      printf("%s/%s,%s,%s,,,,,\n", prefix, quoted_name,
-	     str_type, quoted_value);
-  }
-  else
-  {
-    if(str_type == NULL)
-      printf("%s/%s,0x%.8X,%s,\n", prefix, quoted_name,
-	     vk->type, quoted_value);
-    else
-      printf("%s/%s,%s,%s,\n", prefix, quoted_name,
-	     str_type, quoted_value);
-  }
-
-  if(quoted_value != NULL)
-    free(quoted_value);
-  if(quoted_name != NULL)
-    free(quoted_name);
-  if(conv_error != NULL)
-    free(conv_error);
-}
-
-
 void printValueList(REGFI_ITERATOR* i, char* prefix)
 {
   const REGF_VK_REC* value;
@@ -641,7 +287,7 @@ void printValueList(REGFI_ITERATOR* i, char* prefix)
 }
 
 
-void printKey(const REGF_NK_REC* k, char* full_path)
+void printKey(REGFI_ITERATOR* i, char* full_path)
 {
   static char empty_str[1] = "";
   char* owner = NULL;
@@ -651,17 +297,19 @@ void printKey(const REGF_NK_REC* k, char* full_path)
   char mtime[20];
   time_t tmp_time[1];
   struct tm* tmp_time_s = NULL;
+  const REGF_SK_REC* sk;
+  const REGF_NK_REC* k = regfi_iterator_cur_key(i);
 
   *tmp_time = nt_time_to_unix(&k->mtime);
   tmp_time_s = gmtime(tmp_time);
   strftime(mtime, sizeof(mtime), "%Y-%m-%d %H:%M:%S", tmp_time_s);
 
-  if(print_security)
+  if(print_security && (sk=regfi_iterator_cur_sk(i)))
   {
-    owner = regfi_get_owner(k->sec_desc->sec_desc);
-    group = regfi_get_group(k->sec_desc->sec_desc);
-    sacl = regfi_get_sacl(k->sec_desc->sec_desc);
-    dacl = regfi_get_dacl(k->sec_desc->sec_desc);
+    owner = regfi_get_owner(sk->sec_desc);
+    group = regfi_get_group(sk->sec_desc);
+    sacl = regfi_get_sacl(sk->sec_desc);
+    dacl = regfi_get_dacl(sk->sec_desc);
     if(owner == NULL)
       owner = empty_str;
     if(group == NULL)
@@ -712,7 +360,7 @@ void printKeyTree(REGFI_ITERATOR* iter)
 	bailOut(EX_OSERR, "ERROR: Could not construct iterator's path.\n");
       
       if(!type_filter_enabled || (key_type == type_filter))
-	printKey(cur, path);
+	printKey(iter, path);
       if(!type_filter_enabled || (key_type != type_filter))
 	printValueList(iter, path);
       
@@ -753,6 +401,7 @@ void printKeyTree(REGFI_ITERATOR* iter)
 }
 
 
+/* XXX: what if there is BOTH a value AND a key with that name?? */
 /*
  * Returns 0 if path was not found.
  * Returns 1 if path was found as value.
@@ -812,7 +461,8 @@ int retrievePath(REGFI_ITERATOR* iter, char** path)
     if((value == NULL) || (tmp_path_joined == NULL))
       bailOut(EX_OSERR, "ERROR: Unexpected error before printValue.\n");
 
-    printValue(value, tmp_path_joined);
+    if(!type_filter_enabled || (value->type == type_filter))
+      printValue(value, tmp_path_joined);
 
     free(tmp_path);
     free(tmp_path_joined);
@@ -841,7 +491,7 @@ static void usage(void)
   fprintf(stderr, "Usage: reglookup [-v] [-s]"
 	  " [-p <PATH_FILTER>] [-t <TYPE_FILTER>]"
 	  " <REGISTRY_FILE>\n");
-  fprintf(stderr, "Version: 0.4.0\n");
+  fprintf(stderr, "Version: %s\n", REGLOOKUP_VERSION);
   fprintf(stderr, "Options:\n");
   fprintf(stderr, "\t-v\t sets verbose mode.\n");
   fprintf(stderr, "\t-h\t enables header row. (default)\n");
@@ -857,7 +507,6 @@ static void usage(void)
 int main(int argc, char** argv)
 {
   char** path = NULL;
-  REGF_FILE* f;
   REGFI_ITERATOR* iter;
   int retr_path_ret;
   uint32 argi, arge;

-- 
debian-forensics/reglookup



More information about the forensics-changes mailing list