diff --git a/.hgignore b/.hgignore
new file mode 100644
index 00000000..f3718634
--- /dev/null
+++ b/.hgignore
@@ -0,0 +1,26 @@
+syntax:glob
+*.exe
+*.o
+*~
+.deps/
+Makefile
+Makefile.in
+aclocal.m4
+autom4te.cache
+config.h
+config.h.in
+config.log
+config.status
+configure
+datpacker
+depcomp
+eclc
+ecldump
+install-sh
+lexer.c
+missing
+parser.c
+parser.h
+stamp-h1
+thanm
+ylwrap
diff --git a/COPYING b/COPYING
new file mode 100644
index 00000000..40136988
--- /dev/null
+++ b/COPYING
@@ -0,0 +1,26 @@
+Redistribution and use in source and binary forms, with
+or without modification, are permitted provided that the
+following conditions are met:
+
+1. Redistributions of source code must retain this list
+ of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce this
+ list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the
+ distribution.
+
+THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGE.
diff --git a/COPYING.autotools b/COPYING.autotools
new file mode 100644
index 00000000..e99939c7
--- /dev/null
+++ b/COPYING.autotools
@@ -0,0 +1,2 @@
+Generated files contain exceptions which allow them to be licensed under the
+same license as the parent project.
diff --git a/COPYING.bison b/COPYING.bison
new file mode 100644
index 00000000..527068f3
--- /dev/null
+++ b/COPYING.bison
@@ -0,0 +1,689 @@
+ GNU GENERAL PUBLIC LICENSE
+ Version 3, 29 June 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc.
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+ Preamble
+
+ 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
+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 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 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.
+
+ 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.
+
+ 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 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 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.
+
+
+ Copyright (C)
+
+ 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 .
+
+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:
+
+ Copyright (C)
+ 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
+.
+
+ 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
+.
+
+---
+
+As a special exception, you may create a larger work that contains
+part or all of the Bison parser skeleton and distribute that work
+under terms of your choice, so long as that work isn't itself a
+parser generator using the skeleton or a modified version thereof
+as a parser skeleton. Alternatively, if you modify or redistribute
+the parser skeleton itself, you may (at your option) remove this
+special exception, which will cause the skeleton and the resulting
+Bison output files to be licensed under the GNU General Public
+License without this special exception.
+
+This special exception was added by the Free Software Foundation in
+version 2.2 of Bison.
diff --git a/COPYING.flex b/COPYING.flex
new file mode 100644
index 00000000..684b0110
--- /dev/null
+++ b/COPYING.flex
@@ -0,0 +1,42 @@
+Flex carries the copyright used for BSD software, slightly modified
+because it originated at the Lawrence Berkeley (not Livermore!) Laboratory,
+which operates under a contract with the Department of Energy:
+
+Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006, 2007 The Flex Project.
+
+Copyright (c) 1990, 1997 The Regents of the University of California.
+All rights reserved.
+
+This code is derived from software contributed to Berkeley by
+Vern Paxson.
+
+The United States Government has rights in this work pursuant
+to contract no. DE-AC03-76SF00098 between the United States
+Department of Energy and the University of California.
+
+Redistribution and use in source and binary forms, with or without
+modification, are permitted provided that the following conditions
+are met:
+
+1. Redistributions of source code must retain the above copyright
+ notice, this list of conditions and the following disclaimer.
+2. Redistributions in binary form must reproduce the above copyright
+ notice, this list of conditions and the following disclaimer in the
+ documentation and/or other materials provided with the distribution.
+
+Neither the name of the University nor the names of its contributors
+may be used to endorse or promote products derived from this software
+without specific prior written permission.
+
+THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
+IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
+WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE.
+
+This basically says "do whatever you please with this software except
+remove this notice or take advantage of the University's (or the flex
+authors') name".
+
+Note that the "flex.skl" scanner skeleton carries no copyright notice.
+You are free to do whatever you please with scanners generated using flex;
+for them, you are not even bound by the above copyright.
diff --git a/COPYING.libiconv b/COPYING.libiconv
new file mode 100644
index 00000000..778d0bb5
--- /dev/null
+++ b/COPYING.libiconv
@@ -0,0 +1,482 @@
+ GNU LIBRARY GENERAL PUBLIC LICENSE
+ Version 2, June 1991
+
+ Copyright (C) 1991 Free Software Foundation, Inc.
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+[This is the first released version of the library GPL. It is
+ numbered 2 because it goes with version 2 of the ordinary GPL.]
+
+ Preamble
+
+ The licenses for most software are designed to take away your
+freedom to share and change it. By contrast, the GNU General Public
+Licenses are intended to guarantee your freedom to share and change
+free software--to make sure the software is free for all its users.
+
+ This license, the Library General Public License, applies to some
+specially designated Free Software Foundation software, and to any
+other libraries whose authors decide to use it. You can use it for
+your libraries, 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.
+
+ 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 library, or if you modify it.
+
+ For example, if you distribute copies of the library, whether gratis
+or for a fee, you must give the recipients all the rights that we gave
+you. You must make sure that they, too, receive or can get the source
+code. If you link a program with the library, you must provide
+complete object files to the recipients so that they can relink them
+with the library, after making changes to the library and recompiling
+it. And you must show them these terms so they know their rights.
+
+ Our method of protecting your rights has two steps: (1) copyright
+the library, and (2) offer you this license which gives you legal
+permission to copy, distribute and/or modify the library.
+
+ Also, for each distributor's protection, we want to make certain
+that everyone understands that there is no warranty for this free
+library. If the library is modified by someone else and passed on, we
+want its recipients to know that what they have is not the original
+version, 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 companies distributing free
+software will individually obtain patent licenses, thus in effect
+transforming the program into proprietary software. To prevent this,
+we have made it clear that any patent must be licensed for everyone's
+free use or not licensed at all.
+
+ Most GNU software, including some libraries, is covered by the ordinary
+GNU General Public License, which was designed for utility programs. This
+license, the GNU Library General Public License, applies to certain
+designated libraries. This license is quite different from the ordinary
+one; be sure to read it in full, and don't assume that anything in it is
+the same as in the ordinary license.
+
+ The reason we have a separate public license for some libraries is that
+they blur the distinction we usually make between modifying or adding to a
+program and simply using it. Linking a program with a library, without
+changing the library, is in some sense simply using the library, and is
+analogous to running a utility program or application program. However, in
+a textual and legal sense, the linked executable is a combined work, a
+derivative of the original library, and the ordinary General Public License
+treats it as such.
+
+ Because of this blurred distinction, using the ordinary General
+Public License for libraries did not effectively promote software
+sharing, because most developers did not use the libraries. We
+concluded that weaker conditions might promote sharing better.
+
+ However, unrestricted linking of non-free programs would deprive the
+users of those programs of all benefit from the free status of the
+libraries themselves. This Library General Public License is intended to
+permit developers of non-free programs to use free libraries, while
+preserving your freedom as a user of such programs to change the free
+libraries that are incorporated in them. (We have not seen how to achieve
+this as regards changes in header files, but we have achieved it as regards
+changes in the actual functions of the Library.) The hope is that this
+will lead to faster development of free libraries.
+
+ The precise terms and conditions for copying, distribution and
+modification follow. Pay close attention to the difference between a
+"work based on the library" and a "work that uses the library". The
+former contains code derived from the library, while the latter only
+works together with the library.
+
+ Note that it is possible for a library to be covered by the ordinary
+General Public License rather than by this special one.
+
+ GNU LIBRARY GENERAL PUBLIC LICENSE
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
+
+ 0. This License Agreement applies to any software library which
+contains a notice placed by the copyright holder or other authorized
+party saying it may be distributed under the terms of this Library
+General Public License (also called "this License"). Each licensee is
+addressed as "you".
+
+ A "library" means a collection of software functions and/or data
+prepared so as to be conveniently linked with application programs
+(which use some of those functions and data) to form executables.
+
+ The "Library", below, refers to any such software library or work
+which has been distributed under these terms. A "work based on the
+Library" means either the Library or any derivative work under
+copyright law: that is to say, a work containing the Library or a
+portion of it, either verbatim or with modifications and/or translated
+straightforwardly into another language. (Hereinafter, translation is
+included without limitation in the term "modification".)
+
+ "Source code" for a work means the preferred form of the work for
+making modifications to it. For a library, 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 library.
+
+ Activities other than copying, distribution and modification are not
+covered by this License; they are outside its scope. The act of
+running a program using the Library is not restricted, and output from
+such a program is covered only if its contents constitute a work based
+on the Library (independent of the use of the Library in a tool for
+writing it). Whether that is true depends on what the Library does
+and what the program that uses the Library does.
+
+ 1. You may copy and distribute verbatim copies of the Library's
+complete 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 distribute a copy of this License along with the
+Library.
+
+ 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 Library or any portion
+of it, thus forming a work based on the Library, 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) The modified work must itself be a software library.
+
+ b) You must cause the files modified to carry prominent notices
+ stating that you changed the files and the date of any change.
+
+ c) You must cause the whole of the work to be licensed at no
+ charge to all third parties under the terms of this License.
+
+ d) If a facility in the modified Library refers to a function or a
+ table of data to be supplied by an application program that uses
+ the facility, other than as an argument passed when the facility
+ is invoked, then you must make a good faith effort to ensure that,
+ in the event an application does not supply such function or
+ table, the facility still operates, and performs whatever part of
+ its purpose remains meaningful.
+
+ (For example, a function in a library to compute square roots has
+ a purpose that is entirely well-defined independent of the
+ application. Therefore, Subsection 2d requires that any
+ application-supplied function or table used by this function must
+ be optional: if the application does not supply it, the square
+ root function must still compute square roots.)
+
+These requirements apply to the modified work as a whole. If
+identifiable sections of that work are not derived from the Library,
+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 Library, 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 Library.
+
+In addition, mere aggregation of another work not based on the Library
+with the Library (or with a work based on the Library) on a volume of
+a storage or distribution medium does not bring the other work under
+the scope of this License.
+
+ 3. You may opt to apply the terms of the ordinary GNU General Public
+License instead of this License to a given copy of the Library. To do
+this, you must alter all the notices that refer to this License, so
+that they refer to the ordinary GNU General Public License, version 2,
+instead of to this License. (If a newer version than version 2 of the
+ordinary GNU General Public License has appeared, then you can specify
+that version instead if you wish.) Do not make any other change in
+these notices.
+
+ Once this change is made in a given copy, it is irreversible for
+that copy, so the ordinary GNU General Public License applies to all
+subsequent copies and derivative works made from that copy.
+
+ This option is useful when you wish to copy part of the code of
+the Library into a program that is not a library.
+
+ 4. You may copy and distribute the Library (or a portion or
+derivative of it, under Section 2) in object code or executable form
+under the terms of Sections 1 and 2 above provided that you 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.
+
+ If distribution of 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 satisfies the requirement to
+distribute the source code, even though third parties are not
+compelled to copy the source along with the object code.
+
+ 5. A program that contains no derivative of any portion of the
+Library, but is designed to work with the Library by being compiled or
+linked with it, is called a "work that uses the Library". Such a
+work, in isolation, is not a derivative work of the Library, and
+therefore falls outside the scope of this License.
+
+ However, linking a "work that uses the Library" with the Library
+creates an executable that is a derivative of the Library (because it
+contains portions of the Library), rather than a "work that uses the
+library". The executable is therefore covered by this License.
+Section 6 states terms for distribution of such executables.
+
+ When a "work that uses the Library" uses material from a header file
+that is part of the Library, the object code for the work may be a
+derivative work of the Library even though the source code is not.
+Whether this is true is especially significant if the work can be
+linked without the Library, or if the work is itself a library. The
+threshold for this to be true is not precisely defined by law.
+
+ If such an object file uses only numerical parameters, data
+structure layouts and accessors, and small macros and small inline
+functions (ten lines or less in length), then the use of the object
+file is unrestricted, regardless of whether it is legally a derivative
+work. (Executables containing this object code plus portions of the
+Library will still fall under Section 6.)
+
+ Otherwise, if the work is a derivative of the Library, you may
+distribute the object code for the work under the terms of Section 6.
+Any executables containing that work also fall under Section 6,
+whether or not they are linked directly with the Library itself.
+
+ 6. As an exception to the Sections above, you may also compile or
+link a "work that uses the Library" with the Library to produce a
+work containing portions of the Library, and distribute that work
+under terms of your choice, provided that the terms permit
+modification of the work for the customer's own use and reverse
+engineering for debugging such modifications.
+
+ You must give prominent notice with each copy of the work that the
+Library is used in it and that the Library and its use are covered by
+this License. You must supply a copy of this License. If the work
+during execution displays copyright notices, you must include the
+copyright notice for the Library among them, as well as a reference
+directing the user to the copy of this License. Also, you must do one
+of these things:
+
+ a) Accompany the work with the complete corresponding
+ machine-readable source code for the Library including whatever
+ changes were used in the work (which must be distributed under
+ Sections 1 and 2 above); and, if the work is an executable linked
+ with the Library, with the complete machine-readable "work that
+ uses the Library", as object code and/or source code, so that the
+ user can modify the Library and then relink to produce a modified
+ executable containing the modified Library. (It is understood
+ that the user who changes the contents of definitions files in the
+ Library will not necessarily be able to recompile the application
+ to use the modified definitions.)
+
+ b) Accompany the work with a written offer, valid for at
+ least three years, to give the same user the materials
+ specified in Subsection 6a, above, for a charge no more
+ than the cost of performing this distribution.
+
+ c) If distribution of the work is made by offering access to copy
+ from a designated place, offer equivalent access to copy the above
+ specified materials from the same place.
+
+ d) Verify that the user has already received a copy of these
+ materials or that you have already sent this user a copy.
+
+ For an executable, the required form of the "work that uses the
+Library" must include any data and utility programs needed for
+reproducing the executable from it. 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.
+
+ It may happen that this requirement contradicts the license
+restrictions of other proprietary libraries that do not normally
+accompany the operating system. Such a contradiction means you cannot
+use both them and the Library together in an executable that you
+distribute.
+
+ 7. You may place library facilities that are a work based on the
+Library side-by-side in a single library together with other library
+facilities not covered by this License, and distribute such a combined
+library, provided that the separate distribution of the work based on
+the Library and of the other library facilities is otherwise
+permitted, and provided that you do these two things:
+
+ a) Accompany the combined library with a copy of the same work
+ based on the Library, uncombined with any other library
+ facilities. This must be distributed under the terms of the
+ Sections above.
+
+ b) Give prominent notice with the combined library of the fact
+ that part of it is a work based on the Library, and explaining
+ where to find the accompanying uncombined form of the same work.
+
+ 8. You may not copy, modify, sublicense, link with, or distribute
+the Library except as expressly provided under this License. Any
+attempt otherwise to copy, modify, sublicense, link with, or
+distribute the Library 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.
+
+ 9. 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 Library or its derivative works. These actions are
+prohibited by law if you do not accept this License. Therefore, by
+modifying or distributing the Library (or any work based on the
+Library), you indicate your acceptance of this License to do so, and
+all its terms and conditions for copying, distributing or modifying
+the Library or works based on it.
+
+ 10. Each time you redistribute the Library (or any work based on the
+Library), the recipient automatically receives a license from the
+original licensor to copy, distribute, link with or modify the Library
+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
+this License.
+
+ 11. 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
+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 Library at all. For example, if a patent
+license would not permit royalty-free redistribution of the Library 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 Library.
+
+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.
+
+ 12. If the distribution and/or use of the Library is restricted in
+certain countries either by patents or by copyrighted interfaces, the
+original copyright holder who places the Library 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.
+
+ 13. The Free Software Foundation may publish revised and/or new
+versions of the Library 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 Library
+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 Library does not specify a
+license version number, you may choose any version ever published by
+the Free Software Foundation.
+
+ 14. If you wish to incorporate parts of the Library into other free
+programs whose distribution conditions are incompatible with these,
+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
+
+ 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO
+WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW.
+EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR
+OTHER PARTIES PROVIDE THE LIBRARY "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
+LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME
+THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+ 16. 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 LIBRARY 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
+LIBRARY (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 LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF
+SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
+DAMAGES.
+
+ END OF TERMS AND CONDITIONS
+
+ Appendix: How to Apply These Terms to Your New Libraries
+
+ If you develop a new library, and you want it to be of the greatest
+possible use to the public, we recommend making it free software that
+everyone can redistribute and change. You can do so by permitting
+redistribution under these terms (or, alternatively, under the terms of the
+ordinary General Public License).
+
+ To apply these terms, attach the following notices to the library. It is
+safest to attach them to the start of each source file to most effectively
+convey the exclusion of warranty; and each file should have at least the
+"copyright" line and a pointer to where the full notice is found.
+
+
+ Copyright (C)
+
+ This library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public
+ License as published by the Free Software Foundation; either
+ version 2 of the License, or (at your option) any later version.
+
+ This library 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
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with this library; if not, write to the Free
+ Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ MA 02110-1301, USA
+
+Also add information on how to contact you by electronic and paper mail.
+
+You should also get your employer (if you work as a programmer) or your
+school, if any, to sign a "copyright disclaimer" for the library, if
+necessary. Here is a sample; alter the names:
+
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the
+ library `Frob' (a library for tweaking knobs) written by James Random Hacker.
+
+ , 1 April 1990
+ Ty Coon, President of Vice
+
+That's all there is to it!
diff --git a/COPYING.libpng b/COPYING.libpng
new file mode 100644
index 00000000..968bbb47
--- /dev/null
+++ b/COPYING.libpng
@@ -0,0 +1,110 @@
+This copy of the libpng notices is provided for your convenience. In case of
+any discrepancy between this copy and the notices in the file png.h that is
+included in the libpng distribution, the latter shall prevail.
+
+COPYRIGHT NOTICE, DISCLAIMER, and LICENSE:
+
+If you modify libpng you may insert additional notices immediately following
+this sentence.
+
+This code is released under the libpng license.
+
+libpng versions 1.2.6, August 15, 2004, through 1.2.40, September 10, 2009, are
+Copyright (c) 2004, 2006-2009 Glenn Randers-Pehrson, and are
+distributed according to the same disclaimer and license as libpng-1.2.5
+with the following individual added to the list of Contributing Authors
+
+ Cosmin Truta
+
+libpng versions 1.0.7, July 1, 2000, through 1.2.5 - October 3, 2002, are
+Copyright (c) 2000-2002 Glenn Randers-Pehrson, and are
+distributed according to the same disclaimer and license as libpng-1.0.6
+with the following individuals added to the list of Contributing Authors
+
+ Simon-Pierre Cadieux
+ Eric S. Raymond
+ Gilles Vollant
+
+and with the following additions to the disclaimer:
+
+ There is no warranty against interference with your enjoyment of the
+ library or against infringement. There is no warranty that our
+ efforts or the library will fulfill any of your particular purposes
+ or needs. This library is provided with all faults, and the entire
+ risk of satisfactory quality, performance, accuracy, and effort is with
+ the user.
+
+libpng versions 0.97, January 1998, through 1.0.6, March 20, 2000, are
+Copyright (c) 1998, 1999 Glenn Randers-Pehrson, and are
+distributed according to the same disclaimer and license as libpng-0.96,
+with the following individuals added to the list of Contributing Authors:
+
+ Tom Lane
+ Glenn Randers-Pehrson
+ Willem van Schaik
+
+libpng versions 0.89, June 1996, through 0.96, May 1997, are
+Copyright (c) 1996, 1997 Andreas Dilger
+Distributed according to the same disclaimer and license as libpng-0.88,
+with the following individuals added to the list of Contributing Authors:
+
+ John Bowler
+ Kevin Bracey
+ Sam Bushell
+ Magnus Holmgren
+ Greg Roelofs
+ Tom Tanner
+
+libpng versions 0.5, May 1995, through 0.88, January 1996, are
+Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
+
+For the purposes of this copyright and license, "Contributing Authors"
+is defined as the following set of individuals:
+
+ Andreas Dilger
+ Dave Martindale
+ Guy Eric Schalnat
+ Paul Schmidt
+ Tim Wegner
+
+The PNG Reference Library is supplied "AS IS". The Contributing Authors
+and Group 42, Inc. disclaim all warranties, expressed or implied,
+including, without limitation, the warranties of merchantability and of
+fitness for any purpose. The Contributing Authors and Group 42, Inc.
+assume no liability for direct, indirect, incidental, special, exemplary,
+or consequential damages, which may result from the use of the PNG
+Reference Library, even if advised of the possibility of such damage.
+
+Permission is hereby granted to use, copy, modify, and distribute this
+source code, or portions hereof, for any purpose, without fee, subject
+to the following restrictions:
+
+1. The origin of this source code must not be misrepresented.
+
+2. Altered versions must be plainly marked as such and must not
+ be misrepresented as being the original source.
+
+3. This Copyright notice may not be removed or altered from any
+ source or altered source distribution.
+
+The Contributing Authors and Group 42, Inc. specifically permit, without
+fee, and encourage the use of this source code as a component to
+supporting the PNG file format in commercial products. If you use this
+source code in a product, acknowledgment is not required but would be
+appreciated.
+
+
+A "png_get_copyright" function is available, for convenient use in "about"
+boxes and the like:
+
+ printf("%s",png_get_copyright(NULL));
+
+Also, the PNG logo (in PNG format, of course) is supplied in the
+files "pngbar.png" and "pngbar.jpg (88x31) and "pngnow.png" (98x31).
+
+Libpng is OSI Certified Open Source Software. OSI Certified Open Source is a
+certification mark of the Open Source Initiative.
+
+Glenn Randers-Pehrson
+glennrp at users.sourceforge.net
+September 10, 2009
diff --git a/COPYING.zlib b/COPYING.zlib
new file mode 100644
index 00000000..a80a8473
--- /dev/null
+++ b/COPYING.zlib
@@ -0,0 +1,20 @@
+ (C) 1995-2004 Jean-loup Gailly and Mark Adler
+
+ This software is provided 'as-is', without any express or implied
+ warranty. In no event will the authors be held liable for any damages
+ arising from the use of this software.
+
+ Permission is granted to anyone to use this software for any purpose,
+ including commercial applications, and to alter it and redistribute it
+ freely, subject to the following restrictions:
+
+ 1. The origin of this software must not be misrepresented; you must not
+ claim that you wrote the original software. If you use this software
+ in a product, an acknowledgment in the product documentation would be
+ appreciated but is not required.
+ 2. Altered source versions must be plainly marked as such, and must not be
+ misrepresented as being the original software.
+ 3. This notice may not be removed or altered from any source distribution.
+
+ Jean-loup Gailly Mark Adler
+ jloup@gzip.org madler@alumni.caltech.edu
diff --git a/Makefile.am b/Makefile.am
new file mode 100644
index 00000000..036d0145
--- /dev/null
+++ b/Makefile.am
@@ -0,0 +1,71 @@
+AM_YFLAGS = -d
+AM_CFLAGS = -pedantic -Wall -Wextra
+LDADD = @iconv_LIBS@
+BUILT_SOURCES = parser.h parser.c lexer.c
+bin_PROGRAMS = datpacker eclc ecldump thanm
+
+if COMBINE
+am_datpacker_OBJECTS = datpacker.o
+am_eclc_OBJECTS = eclc.o
+am_ecldump_OBJECTS = ecldump.o
+am_thanm_OBJECTS = thanm.o
+
+datpacker.o: $(datpacker_SOURCES)
+ $(COMPILE) $(datpacker_CFLAGS) @combine_CFLAGS@ -c -o $@ $(filter %.c,$(datpacker_SOURCES))
+
+eclc.o: $(eclc_SOURCES)
+ $(COMPILE) $(eclc_CFLAGS) @combine_CFLAGS@ -c -o $@ $(filter %.c,$(eclc_SOURCES)) parser.c lexer.c
+
+ecldump.o: $(ecldump_SOURCES)
+ $(COMPILE) $(ecldump_CFLAGS) @combine_CFLAGS@ -c -o $@ $(filter %.c,$(ecldump_SOURCES))
+
+thanm.o: $(thanm_SOURCES)
+ $(COMPILE) $(thanm_CFLAGS) @combine_CFLAGS@ -c -o $@ $(filter %.c,$(thanm_SOURCES))
+endif
+
+util_SOURCES = \
+ program.c \
+ program.h \
+ util.c \
+ util.h
+
+datpacker_CFLAGS = $(OPENMP_CFLAGS)
+datpacker_SOURCES = \
+ bits.c \
+ bits.h \
+ datpacker-th02.c \
+ datpacker-th06.c \
+ datpacker-th08.c \
+ datpacker-th75.c \
+ datpacker-th95.c \
+ datpacker.c \
+ datpacker.h \
+ thcrypt.c \
+ thcrypt.h \
+ thdat.c \
+ thdat.h \
+ thlzss.c \
+ thlzss.h \
+ thrle.c \
+ thrle.h \
+ $(util_SOURCES)
+
+eclc_SOURCES = \
+ parser.h \
+ lexer.l \
+ parser.y \
+ $(util_SOURCES)
+
+ecldump_LDADD = $(LDADD) -lm
+ecldump_SOURCES = \
+ ecldump.c \
+ instr.c \
+ instr.h \
+ $(util_SOURCES)
+
+thanm_CFLAGS = $(libpng_CFAGS)
+thanm_LDADD = $(LDADD) $(libpng_LIBS)
+thanm_SOURCES = \
+ thanm.c \
+ thanm.h \
+ $(util_SOURCES)
diff --git a/bits.c b/bits.c
new file mode 100644
index 00000000..6cb36408
--- /dev/null
+++ b/bits.c
@@ -0,0 +1,84 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+#include
+#include
+#include
+#include "bits.h"
+
+void
+bitstream_init(bitstream_t* b, unsigned int size)
+{
+ b->used_bytes = 0;
+ b->buffer = NULL;
+ b->buffer_size = size;
+ b->next_byte = 0;
+ b->used_bits = 0;
+ bitstream_grow(b);
+}
+
+void
+bitstream_finish(bitstream_t* b)
+{
+ while (b->used_bits)
+ bitstream_write1(b, 0);
+}
+
+void
+bitstream_grow(bitstream_t* b)
+{
+ b->buffer_size <<= 1;
+ b->buffer = realloc(b->buffer, b->buffer_size);
+}
+
+void
+bitstream_write(bitstream_t* b, unsigned int bits, uint32_t data)
+{
+ int i;
+ if (bits > 32)
+ bits = 32;
+ for (i = bits - 1; i >= 0; --i) {
+ const unsigned int bit = (data >> i) & 1;
+ bitstream_write1(b, bit);
+ }
+}
+
+void
+bitstream_write1(bitstream_t* b, unsigned int bit)
+{
+ b->next_byte |= bit << (7 - b->used_bits);
+ b->used_bits++;
+ if (b->used_bits >= 8) {
+ b->buffer[b->used_bytes] = b->next_byte;
+ b->used_bytes++;
+ if (b->used_bytes >= b->buffer_size)
+ bitstream_grow(b);
+ b->next_byte = 0;
+ b->used_bits = 0;
+ }
+}
diff --git a/bits.h b/bits.h
new file mode 100644
index 00000000..adb0906b
--- /dev/null
+++ b/bits.h
@@ -0,0 +1,51 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+#ifndef BITS_H_
+#define BITS_H_
+
+#include
+#include
+
+typedef struct {
+ unsigned char* buffer;
+ unsigned int buffer_size;
+ unsigned int used_bytes;
+
+ unsigned char next_byte;
+ unsigned char used_bits;
+} bitstream_t;
+
+/* Size is multiplied by two right away. */
+void bitstream_init(bitstream_t* b, unsigned int size);
+void bitstream_finish(bitstream_t* b);
+void bitstream_grow(bitstream_t* b);
+void bitstream_write(bitstream_t* b, unsigned int bits, uint32_t data);
+void bitstream_write1(bitstream_t* b, unsigned int bit);
+
+#endif
diff --git a/configure.ac b/configure.ac
new file mode 100644
index 00000000..d73f912d
--- /dev/null
+++ b/configure.ac
@@ -0,0 +1,92 @@
+dnl 2.62 for AC_OPENMP
+AC_PREREQ(2.62)
+AC_INIT([Touhou Toolkit], [0], [xarnonymous@gmail.com], [thtk], [http://code.google.com/p/thtk/])
+AM_INIT_AUTOMAKE([silent-rules foreign])
+AM_SILENT_RULES
+AC_CONFIG_SRCDIR([datpacker.c])
+AC_CONFIG_HEADERS([config.h])
+
+AC_DEFINE([PACKAGE_THDAT_VERSION], ["6"], [Define to the version of this package.])
+AC_DEFINE([PACKAGE_THECL_VERSION], ["1"], [Define to the version of this package.])
+AC_DEFINE([PACKAGE_THANM_VERSION], ["0"], [Define to the version of this package.])
+
+AH_TEMPLATE([HAVE_LIBPNG], [Define to 1 if libpng is available.])
+AH_TEMPLATE([PACK_PRAGMA], [Define to 1 if your C compiler supports the packing pragma.])
+AC_DEFINE([PACK_ATTRIBUTE], [], [Define to __attribute__((packed)) if supported by your C compiler.])
+
+dnl check for programs
+
+dnl AC_PROG_CC
+AC_PROG_LEX
+AC_PROG_YACC
+
+dnl check for libraries
+
+PKG_CHECK_MODULES([libpng], [libpng12], [AC_DEFINE([HAVE_LIBPNG], [1])])
+
+dnl check for header files
+
+AC_CHECK_HEADERS([iconv.h io.h libgen.h sys/stat.h windows.h])
+
+dnl check for types
+
+AC_TYPE_SSIZE_T
+AC_TYPE_UINT8_T
+AC_TYPE_UINT16_T
+AC_TYPE_UINT32_T
+AC_TYPE_INT32_T
+
+dnl check for structures
+
+dnl check for compiler characteristics
+
+AC_C_BIGENDIAN
+AC_C_INLINE
+AC_OPENMP
+
+AC_CHECK_DECL([WIN32],
+ [AC_SUBST([iconv_LIBS], [-liconv])])
+
+AC_MSG_CHECKING([whether CC supports -fwhole-program -combine])
+enable_combine="no"
+ac_save_CFLAGS=$CFLAGS
+CFLAGS="$CFLAGS -fwhole-program -combine"
+AC_COMPILE_IFELSE(AC_LANG_PROGRAM([], []),
+ [AC_SUBST([combine_CFLAGS],
+ ["-fwhole-program -combine"])
+ enable_combine="yes"])
+CFLAGS=$ac_save_CFLAGS
+AM_CONDITIONAL([COMBINE], [test "$enable_combine" = "yes"])
+AC_MSG_RESULT([$enable_combine])
+
+dnl XXX: Maybe offsetof isn't all that portable.
+AC_MSG_CHECKING([whether structure packing can be controlled])
+structpacking="unsupported"
+AC_RUN_IFELSE(AC_LANG_PROGRAM([dnl
+ #include
+ struct test_t { char a;
+ #pragma pack(push,1)
+ int b; };
+ #pragma pack(pop)],
+ [return offsetof(struct test_t, b) == 1 ? 0 : 1;],),
+ [AC_DEFINE([PACK_PRAGMA], [1])
+ structpacking="pragma pack(push,1)"],
+ [],
+ [AC_DEFINE([PACK_PRAGMA], [1])
+ structpacking="(assumed) pragma pack(push,1)"])
+if test "$structpacking" = "unsupported"; then
+AC_RUN_IFELSE(AC_LANG_PROGRAM([dnl
+ #include
+ struct test_t { char a; int b; } __attribute__((packed));],
+ [return offsetof(struct test_t, b) == 1 ? 0 : 1;],),
+ [AC_DEFINE([PACK_ATTRIBUTE], [__attribute__((packed))])
+ structpacking="__attribute__((packed))"])
+fi
+AC_MSG_RESULT([$structpacking])
+
+dnl check for library functions
+
+AC_CHECK_FUNCS([basename iconv_open mempcpy mkdir snprintf _snprintf _splitpath strdup strtof])
+
+AC_CONFIG_FILES([Makefile])
+AC_OUTPUT
diff --git a/datpacker-th02.c b/datpacker-th02.c
new file mode 100644
index 00000000..12957468
--- /dev/null
+++ b/datpacker-th02.c
@@ -0,0 +1,117 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+#include
+#include
+#include
+#include
+#include
+#include "datpacker.h"
+#include "thdat.h"
+#include "util.h"
+
+static archive_t*
+th02_open(FILE* stream, unsigned int version, unsigned int count)
+{
+ return archive_open(stream, version, (count + 1) * 32, count);
+}
+
+/* TODO: Check that filenames are 8.3, make it a THDAT_ flag. */
+/* TODO: Find out if lowercase filenames are supported. */
+static int
+th02_write(archive_t* archive, entry_t* entry, FILE* stream)
+{
+ unsigned int i;
+ unsigned char* data;
+
+ for (i = 0; i < 13; ++i)
+ if (entry->name[i])
+ entry->name[i] ^= 0xff;
+
+ data = thdat_read_file(entry, stream);
+ if (!data)
+ return -1;
+
+ data = thdat_rle(entry, data);
+
+ for (i = 0; i < entry->zsize; ++i)
+ data[i] ^= 0x12;
+
+ return thdat_write_entry(archive, entry, data);
+}
+
+static int
+th02_close(archive_t* archive)
+{
+ unsigned char* buffer;
+ unsigned char* buffer_ptr;
+ unsigned int i;
+ unsigned int list_size = 32 * (archive->count + 1);
+
+ if (!util_seek(archive->fd, 0, NULL))
+ return -1;
+
+ buffer = malloc(list_size);
+ memset(buffer, 0, list_size);
+
+ buffer_ptr = buffer;
+ for (i = 0; i < archive->count; ++i) {
+ const uint16_t magic1 = 0xf388;
+ const uint16_t magic2 = 0x9595;
+ entry_t* entry = &archive->entries[i];
+
+ if (entry->zsize == entry->size)
+ buffer_ptr = mempcpy(buffer_ptr, &magic1, sizeof(uint16_t));
+ else
+ buffer_ptr = mempcpy(buffer_ptr, &magic2, sizeof(uint16_t));
+
+ *buffer_ptr++ = 3;
+
+ buffer_ptr = mempcpy(buffer_ptr, entry->name, 13);
+ buffer_ptr = mempcpy(buffer_ptr, &entry->zsize, sizeof(uint32_t));
+ buffer_ptr = mempcpy(buffer_ptr, &entry->size, sizeof(uint32_t));
+ buffer_ptr = mempcpy(buffer_ptr, &entry->offset, sizeof(uint32_t));
+ buffer_ptr += 4;
+ }
+
+ if (fwrite(buffer, list_size, 1, archive->fd) != 1) {
+ snprintf(library_error, LIBRARY_ERROR_SIZE, "couldn't write: %s", strerror(errno));
+ free(buffer);
+ return -1;
+ }
+ free(buffer);
+
+ return 0;
+}
+
+const archive_module_t archive_th02 = {
+ th02_open,
+ th02_write,
+ th02_close,
+ THDAT_BASENAME
+};
diff --git a/datpacker-th06.c b/datpacker-th06.c
new file mode 100644
index 00000000..43703e3c
--- /dev/null
+++ b/datpacker-th06.c
@@ -0,0 +1,209 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+#include
+#include
+#include
+#include
+#include
+#include
+#include "datpacker.h"
+#include "util.h"
+#include "bits.h"
+#include "thdat.h"
+#include "thlzss.h"
+
+static void
+th06_write_uint32(bitstream_t* b, uint32_t value)
+{
+ unsigned int size = 1;
+ if (value & 0xffffff00) {
+ size = 2;
+ if (value & 0xffff0000) {
+ size = 3;
+ if (value & 0xff000000) {
+ size = 4;
+ }
+ }
+ }
+
+ bitstream_write(b, 2, size - 1);
+ bitstream_write(b, size * 8, value);
+}
+
+static void
+th06_write_string(bitstream_t* b, unsigned int length, unsigned char* data)
+{
+ unsigned int i;
+ for (i = 0; i < length; ++i)
+ bitstream_write(b, 8, data[i]);
+}
+
+static archive_t*
+th06_open(FILE* stream, unsigned int version, unsigned int count)
+{
+ /* 13 is the largest size the header can have, so some bytes might be
+ * wasted. */
+ return archive_open(stream, version, version == 6 ? 13 : 16, count);
+}
+
+static int
+th06_write(archive_t* archive, entry_t* entry, FILE* stream)
+{
+ unsigned char* data;
+
+ /* There is a chance that one of the games support uncompressed data. */
+
+ data = thdat_read_file_lzss(entry, stream);
+
+ if (archive->version == 6) {
+ unsigned int i;
+ for (i = 0; i < entry->zsize; ++i)
+ entry->extra += data[i];
+ }
+
+ return thdat_write_entry(archive, entry, data);
+}
+
+static int
+th06_close(archive_t* archive)
+{
+ const char* magic = archive->version == 6 ? "PBG3" : "PBG4";
+ unsigned int i;
+ uint32_t header[3];
+ unsigned char* buffer = NULL;
+ unsigned char* buffer_ptr = NULL;
+ unsigned char* zbuffer;
+ unsigned int list_size = 0;
+ unsigned int list_zsize = 0;
+ bitstream_t b;
+
+ thdat_sort(archive);
+
+ if (archive->version == 6) {
+ bitstream_init(&b, 16);
+ } else {
+ for (i = 0; i < archive->count; ++i)
+ list_size += strlen(archive->entries[i].name) + 1 + (sizeof(uint32_t) * 3);
+
+ list_size += 4;
+ buffer = malloc(list_size);
+ memset(buffer, 0, list_size);
+
+ buffer_ptr = buffer;
+ }
+
+ for (i = 0; i < archive->count; ++i) {
+ const uint32_t zero = 0;
+ entry_t* entry = &archive->entries[i];
+ /* These values are unknown, but it seems they can be ignored. */
+ uint32_t unknown1 = 0; /* The same for all entries in an archive. */
+ uint32_t unknown2 = 0; /* Starts at a high value. Increases by a random multiple of a thousand per entry. */
+
+ if (archive->version == 6) {
+ th06_write_uint32(&b, unknown1);
+ th06_write_uint32(&b, unknown2);
+ th06_write_uint32(&b, entry->extra);
+ th06_write_uint32(&b, entry->offset);
+ th06_write_uint32(&b, entry->size);
+ th06_write_string(&b, strlen(entry->name) + 1, (unsigned char*)entry->name);
+ } else {
+ buffer_ptr = mempcpy(buffer_ptr, entry->name, strlen(entry->name) + 1);
+ buffer_ptr = mempcpy(buffer_ptr, &entry->offset, sizeof(uint32_t));
+ buffer_ptr = mempcpy(buffer_ptr, &entry->size, sizeof(uint32_t));
+ buffer_ptr = mempcpy(buffer_ptr, &zero, sizeof(uint32_t));
+ }
+ }
+
+ if (archive->version == 6) {
+ bitstream_finish(&b);
+
+ if (fwrite(b.buffer, b.used_bytes, 1, archive->fd) != 1) {
+ snprintf(library_error, LIBRARY_ERROR_SIZE, "couldn't write: %s", strerror(errno));
+ free(b.buffer);
+ return -1;
+ }
+
+ free(b.buffer);
+ } else {
+ memset(buffer_ptr, 0, sizeof(uint32_t));
+
+ zbuffer = th_lz_mem(buffer, list_size, &list_zsize);
+ free(buffer);
+
+ if (fwrite(zbuffer, list_zsize, 1, archive->fd) != 1) {
+ snprintf(library_error, LIBRARY_ERROR_SIZE, "couldn't write: %s", strerror(errno));
+ free(zbuffer);
+ return -1;
+ }
+ free(zbuffer);
+ }
+
+ if (!util_seek(archive->fd, 0, NULL))
+ return -1;
+
+ if (fwrite(magic, 4, 1, archive->fd) != 1) {
+ snprintf(library_error, LIBRARY_ERROR_SIZE, "couldn't write: %s", strerror(errno));
+ return -1;
+ }
+
+ if (archive->version == 6) {
+ bitstream_init(&b, 8);
+
+ th06_write_uint32(&b, archive->count);
+ th06_write_uint32(&b, archive->offset);
+
+ bitstream_finish(&b);
+
+ if (fwrite(b.buffer, b.used_bytes, 1, archive->fd) != 1) {
+ snprintf(library_error, LIBRARY_ERROR_SIZE, "couldn't write: %s", strerror(errno));
+ free(b.buffer);
+ return -1;
+ }
+
+ free(b.buffer);
+ } else {
+ header[0] = archive->count;
+ header[1] = archive->offset;
+ header[2] = list_size;
+
+ if (fwrite(&header, sizeof(header), 1, archive->fd) != 1) {
+ snprintf(library_error, LIBRARY_ERROR_SIZE, "couldn't write: %s", strerror(errno));
+ return -1;
+ }
+ }
+
+ return 0;
+}
+
+const archive_module_t archive_th06 = {
+ th06_open,
+ th06_write,
+ th06_close,
+ THDAT_BASENAME
+};
diff --git a/datpacker-th08.c b/datpacker-th08.c
new file mode 100644
index 00000000..58f0b399
--- /dev/null
+++ b/datpacker-th08.c
@@ -0,0 +1,260 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include "datpacker.h"
+#include "thcrypt.h"
+#include "thlzss.h"
+#include "util.h"
+#include "thdat.h"
+
+typedef struct {
+ unsigned char type;
+ unsigned char key;
+ unsigned char step;
+ unsigned int block;
+ unsigned int limit;
+} crypt_params;
+
+/* Indices into th??_crypt_params. */
+#define TYPE_ETC 0
+#define TYPE_ANM 1
+#define TYPE_ECL 2
+#define TYPE_JPG 3
+#define TYPE_TXT 4
+#define TYPE_WAV 5
+#define TYPE_MSG 6
+
+static const crypt_params
+th08_crypt_params[] = {
+ { 0x2d, 0x35, 0x97, 0x80, 0x2800 }, /* .rpy, .end, .mid, .dat, .sht, .png, .std, .ver, .fmt */
+ { 0x41, 0xc1, 0x51, 0x1400, 0x2000 }, /* .anm */
+ { 0x45, 0xab, 0xcd, 0x200, 0x1000 }, /* .ecl */
+ { 0x4a, 0x03, 0x19, 0x1400, 0x7800 }, /* .jpg */
+ { 0x54, 0x51, 0xe9, 0x40, 0x3000 }, /* .txt */
+ { 0x57, 0x12, 0x34, 0x400, 0x2800 }, /* .wav */
+ { 0x2d, 0x35, 0x97, 0x80, 0x2800 }, /* TYPE_MSG, same as TYPE_MSG here. */
+};
+
+static const crypt_params
+th09_crypt_params[] = {
+ { 0x2a, 0x99, 0x37, 0x400, 0x1000 }, /* .rpy, .end, .mid, .sht, .png, .std, .ver, .fmt */
+ { 0x41, 0xc1, 0x51, 0x400, 0x400 }, /* .anm */
+ { 0x45, 0xab, 0xcd, 0x200, 0x1000 }, /* .ecl */
+ { 0x4a, 0x03, 0x19, 0x400, 0x400 }, /* .jpg */
+ { 0x54, 0x51, 0xe9, 0x40, 0x3000 }, /* .txt */
+ { 0x57, 0x12, 0x34, 0x400, 0x400 }, /* .wav */
+ { 0x4d, 0x1b, 0x37, 0x40, 0x2800 }, /* .msg */
+};
+
+static void
+tolowerstr(char* str)
+{
+ while (*str)
+ *str++ = tolower(*str);
+}
+
+static archive_t*
+th08_open(FILE* stream, unsigned int version, unsigned int count)
+{
+ return archive_open(stream, version, 16, count);
+}
+
+static unsigned char*
+th08_read_file(entry_t* entry, FILE* stream)
+{
+ unsigned char* data = malloc(entry->size + 4);
+
+ if (fread(data + 4, entry->size, 1, stream) != 1) {
+ snprintf(library_error, LIBRARY_ERROR_SIZE, "couldn't read: %s", strerror(errno));
+ free(data);
+ return NULL;
+ }
+
+ return data;
+}
+
+static int
+th08_encrypt(archive_t* archive, entry_t* entry, unsigned char* data)
+{
+ const crypt_params* current_crypt_params = archive->version == 8 ? th08_crypt_params : th09_crypt_params;
+ unsigned int type;
+ char ext[5];
+
+ /* TODO: Move all of this to one or two functions. */
+ if (strlen(entry->name) < 4) {
+ strcpy(ext, "");
+ } else {
+ strcpy(ext, entry->name + strlen(entry->name) - 4);
+ }
+ tolowerstr(ext);
+
+ if (strcmp(ext, ".anm") == 0) {
+ type = TYPE_ANM;
+ } else if (strcmp(ext, ".ecl") == 0) {
+ type = TYPE_ECL;
+ } else if (strcmp(ext, ".jpg") == 0) {
+ type = TYPE_JPG;
+ } else if (strcmp(ext, ".msg") == 0) {
+ type = TYPE_MSG;
+ } else if (strcmp(ext, ".txt") == 0) {
+ type = TYPE_TXT;
+ } else if (strcmp(ext, ".wav") == 0) {
+ type = TYPE_WAV;
+ } else {
+ type = TYPE_ETC;
+ }
+
+ data[0] = 'e';
+ data[1] = 'd';
+ data[2] = 'z';
+ data[3] = current_crypt_params[type].type;
+
+ if (th_encrypt(data + 4,
+ entry->size,
+ current_crypt_params[type].key,
+ current_crypt_params[type].step,
+ current_crypt_params[type].block,
+ current_crypt_params[type].limit) == -1) {
+ free(data);
+ return -1;
+ }
+
+ return 0;
+}
+
+static unsigned char*
+th08_lzss(entry_t* entry, unsigned char* data)
+{
+ unsigned char* zdata;
+ zdata = th_lz_mem(data, entry->size + 4, &entry->zsize);
+ free(data);
+ return zdata;
+}
+
+static int
+th08_write(archive_t* archive, entry_t* entry, FILE* stream)
+{
+ unsigned char* data;
+
+ data = th08_read_file(entry, stream);
+ if (!data)
+ return -1;
+
+ if (th08_encrypt(archive, entry, data) == -1)
+ return -1;
+
+ data = th08_lzss(entry, data);
+
+ /* XXX: There doesn't seem to be an option for non-compressed data here. */
+
+ return thdat_write_entry(archive, entry, data);
+}
+
+static int
+th08_close(archive_t* archive)
+{
+ unsigned char* buffer;
+ unsigned char* buffer_ptr;
+ unsigned int i;
+ unsigned char* zbuffer;
+ uint32_t header[4];
+ unsigned int list_size = 0;
+ unsigned int list_zsize = 0;
+ const uint32_t zero = 0;
+
+ thdat_sort(archive);
+
+ for (i = 0; i < archive->count; ++i)
+ list_size += strlen(archive->entries[i].name) + 1 + (sizeof(uint32_t) * 3);
+
+ /* XXX: I'm adding some padding here to satisfy pbgzmlt; th08 has 1248 zero
+ * bytes and th09 1200 bytes. The games work fine without it. */
+ list_size += 4;
+ buffer = malloc(list_size);
+ memset(buffer, 0, list_size);
+
+ buffer_ptr = buffer;
+ for (i = 0; i < archive->count; ++i) {
+ entry_t* entry = &archive->entries[i];
+ buffer_ptr = mempcpy(buffer_ptr, entry->name, strlen(entry->name) + 1);
+ buffer_ptr = mempcpy(buffer_ptr, &entry->offset, sizeof(uint32_t));
+ entry->size += 4; /* XXX: Only OK if I don't use it later on. */
+ buffer_ptr = mempcpy(buffer_ptr, &entry->size, sizeof(uint32_t));
+ buffer_ptr = mempcpy(buffer_ptr, &zero, sizeof(uint32_t));
+ }
+
+ buffer_ptr = mempcpy(buffer_ptr, &zero, sizeof(uint32_t));
+
+ zbuffer = th_lz_mem(buffer, list_size, &list_zsize);
+ free(buffer);
+
+ if (th_encrypt(zbuffer, list_zsize, 0x3e, 0x9b, 0x80, 0x400) == -1) {
+ free(zbuffer);
+ return -1;
+ }
+
+ if (fwrite(zbuffer, list_zsize, 1, archive->fd) != 1) {
+ snprintf(library_error, LIBRARY_ERROR_SIZE, "couldn't write: %s", strerror(errno));
+ free(zbuffer);
+ return -1;
+ }
+ free(zbuffer);
+
+ header[0] = 0x5a474250; /* ZGBP */
+ header[1] = archive->count + 123456;
+ header[2] = archive->offset + 345678;
+ header[3] = list_size + 567891;
+
+ if (th_encrypt((unsigned char*)&header[1], sizeof(uint32_t) * 3, 0x1b, 0x37, sizeof(uint32_t) * 3, 0x400) == -1) {
+ return -1;
+ }
+
+ if (!util_seek(archive->fd, 0, NULL))
+ return -1;
+
+ if (fwrite(&header, sizeof(header), 1, archive->fd) != 1) {
+ snprintf(library_error, LIBRARY_ERROR_SIZE, "couldn't write: %s", strerror(errno));
+ return -1;
+ }
+
+ return 0;
+}
+
+const archive_module_t archive_th08 = {
+ th08_open,
+ th08_write,
+ th08_close,
+ THDAT_BASENAME
+};
diff --git a/datpacker-th75.c b/datpacker-th75.c
new file mode 100644
index 00000000..91cdfa77
--- /dev/null
+++ b/datpacker-th75.c
@@ -0,0 +1,127 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+#include
+#include
+#include
+#include
+#include
+#include
+#include "datpacker.h"
+#include "util.h"
+#include "thdat.h"
+
+static archive_t*
+th75_open(FILE* stream, unsigned int version, unsigned int count)
+{
+ return archive_open(stream, version, 2 + 108 * count, count);
+}
+
+static void
+th75_process_name(entry_t* entry)
+{
+ char* temp;
+
+ entry->name[100] = 0;
+
+ /* XXX: This is only needed on some platforms, and should be done in
+ * archive_add_entry. */
+ temp = entry->name;
+ while (*temp) {
+ if (*temp == '/') *temp = '\\';
+ temp++;
+ }
+}
+
+/* TODO: Figure out something nice for relative paths. */
+static int
+th75_write(archive_t* archive, entry_t* entry, FILE* stream)
+{
+ unsigned char* data;
+
+ th75_process_name(entry);
+
+ data = thdat_read_file(entry, stream);
+ if (!data)
+ return -1;
+
+ entry->zsize = entry->size;
+
+ return thdat_write_entry(archive, entry, data);
+}
+
+static int
+th75_close(archive_t* archive)
+{
+ unsigned char* buffer;
+ unsigned char* buffer_ptr;
+ unsigned int i;
+ const unsigned int list_size = archive->count * 108;
+ uint16_t count = archive->count;
+ unsigned char k = 100, t = 100;
+
+ if (!util_seek(archive->fd, 0, NULL))
+ return -1;
+
+ if (fwrite(&count, sizeof(uint16_t), 1, archive->fd) != 1) {
+ snprintf(library_error, LIBRARY_ERROR_SIZE, "couldn't write: %s", strerror(errno));
+ return -1;
+ }
+
+ buffer = malloc(list_size);
+
+ buffer_ptr = buffer;
+ for (i = 0; i < archive->count; ++i) {
+ const entry_t* entry = &archive->entries[i];
+ buffer_ptr = mempcpy(buffer_ptr, entry->name, 100);
+ buffer_ptr = mempcpy(buffer_ptr, &entry->size, sizeof(uint32_t));
+ buffer_ptr = mempcpy(buffer_ptr, &entry->offset, sizeof(uint32_t));
+ }
+
+ for (i = 0; i < list_size; ++i) {
+ buffer[i] ^= k;
+ k += t;
+ t += 77;
+ }
+
+ if (fwrite(buffer, list_size, 1, archive->fd) != 1) {
+ snprintf(library_error, LIBRARY_ERROR_SIZE, "couldn't write: %s", strerror(errno));
+ free(buffer);
+ return -1;
+ }
+ free(buffer);
+
+ return 0;
+}
+
+const archive_module_t archive_th75 = {
+ th75_open,
+ th75_write,
+ th75_close,
+ 0
+};
diff --git a/datpacker-th95.c b/datpacker-th95.c
new file mode 100644
index 00000000..53fb95fa
--- /dev/null
+++ b/datpacker-th95.c
@@ -0,0 +1,197 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+#include
+#include
+#include
+#include
+#include
+#include
+#include "datpacker.h"
+#include "thcrypt.h"
+#include "thlzss.h"
+#include "util.h"
+#include "thdat.h"
+
+static unsigned int
+th95_get_crypt_param_index(const char *name)
+{
+ char index = 0;
+ while (*name) index += *name++;
+ return index & 7;
+}
+
+typedef struct {
+ unsigned char key;
+ unsigned char step;
+ unsigned int block;
+ unsigned int limit;
+} crypt_params_t;
+
+static const crypt_params_t th95_crypt_params[] = {
+ /* key step block limit */
+ { 0x1b, 0x37, 0x40, 0x2800 },
+ { 0x51, 0xe9, 0x40, 0x3000 },
+ { 0xc1, 0x51, 0x80, 0x3200 },
+ { 0x03, 0x19, 0x400, 0x7800 },
+ { 0xab, 0xcd, 0x200, 0x2800 },
+ { 0x12, 0x34, 0x80, 0x3200 },
+ { 0x35, 0x97, 0x80, 0x2800 },
+ { 0x99, 0x37, 0x400, 0x2000 }
+};
+
+static const crypt_params_t th12_crypt_params[] = {
+ /* key step block limit */
+ { 0x1b, 0x73, 0x40, 0x3800 },
+ { 0x51, 0x9e, 0x40, 0x4000 },
+ { 0xc1, 0x15, 0x400, 0x2C00 },
+ { 0x03, 0x91, 0x80, 0x6400 },
+ { 0xab, 0xdc, 0x80, 0x6e00 },
+ { 0x12, 0x43, 0x200, 0x3c00 },
+ { 0x35, 0x79, 0x400, 0x3c00 },
+ { 0x99, 0x7d, 0x80, 0x2800 }
+};
+
+static archive_t*
+th95_open(FILE* stream, unsigned int version, unsigned int count)
+{
+ return archive_open(stream, version, 16, count);
+}
+
+static int
+th95_encrypt_data(archive_t* archive, entry_t* entry, unsigned char* data)
+{
+ const unsigned int i = th95_get_crypt_param_index(entry->name);
+ const crypt_params_t* crypt_params = archive->version == 12 ? th12_crypt_params : th95_crypt_params;
+
+ if (th_encrypt(data, entry->zsize, crypt_params[i].key, crypt_params[i].step, crypt_params[i].block, crypt_params[i].limit) == -1) {
+ free(data);
+ return -1;
+ }
+
+ return 0;
+}
+
+static int
+th95_write(archive_t* archive, entry_t* entry, FILE* stream)
+{
+ unsigned char* data;
+
+ data = thdat_read_file_lzss(entry, stream);
+
+ if (entry->zsize >= entry->size) {
+ free(data);
+
+ if (!util_seek(stream, 0, NULL))
+ return -1;
+
+ data = thdat_read_file(entry, stream);
+ if (!data)
+ return -1;
+
+ entry->zsize = entry->size;
+ }
+
+ if (th95_encrypt_data(archive, entry, data) == -1)
+ return -1;
+
+ return thdat_write_entry(archive, entry, data);
+}
+
+static int
+th95_close(archive_t* archive)
+{
+ unsigned char* buffer;
+ unsigned char* buffer_ptr;
+ unsigned int i;
+ unsigned char* zbuffer;
+ uint32_t header[4];
+ unsigned int list_size = 0;
+ unsigned int list_zsize = 0;
+
+ thdat_sort(archive);
+
+ for (i = 0; i < archive->count; ++i) {
+ const size_t namelen = strlen(archive->entries[i].name);
+ list_size += (sizeof(uint32_t) * 3) + namelen + (4 - namelen % 4);
+ }
+
+ buffer = malloc(list_size);
+
+ buffer_ptr = buffer;
+ for (i = 0; i < archive->count; ++i) {
+ const uint32_t zero = 0;
+ const entry_t* entry = &archive->entries[i];
+ const size_t namelen = strlen(entry->name);
+ buffer_ptr = mempcpy(buffer_ptr, entry->name, namelen + (4 - namelen % 4));
+ buffer_ptr = mempcpy(buffer_ptr, &entry->offset, sizeof(uint32_t));
+ buffer_ptr = mempcpy(buffer_ptr, &entry->size, sizeof(uint32_t));
+ buffer_ptr = mempcpy(buffer_ptr, &zero, sizeof(uint32_t));
+ }
+
+ zbuffer = th_lz_mem(buffer, list_size, &list_zsize);
+ free(buffer);
+
+ if (th_encrypt(zbuffer, list_zsize, 0x3e, 0x9b, 0x80, list_size) == -1) {
+ free(zbuffer);
+ return -1;
+ }
+
+ if (fwrite(zbuffer, list_zsize, 1, archive->fd) != 1) {
+ snprintf(library_error, LIBRARY_ERROR_SIZE, "couldn't write: %s", strerror(errno));
+ free(zbuffer);
+ return -1;
+ }
+ free(zbuffer);
+
+ if (!util_seek(archive->fd, 0, NULL))
+ return -1;
+
+ header[0] = 0x31414854;
+ header[1] = list_size + 123456789;
+ header[2] = list_zsize + 987654321;
+ header[3] = archive->count - 0xf7e7f8ac;
+
+ if (th_encrypt((unsigned char*)&header, sizeof(header), 0x1b, 0x37, sizeof(header), sizeof(header)) == -1) {
+ return -1;
+ }
+
+ if (fwrite(&header, sizeof(header), 1, archive->fd) != 1) {
+ snprintf(library_error, LIBRARY_ERROR_SIZE, "couldn't write: %s", strerror(errno));
+ return -1;
+ }
+
+ return 0;
+}
+
+const archive_module_t archive_th95 = {
+ th95_open,
+ th95_write,
+ th95_close,
+ THDAT_BASENAME
+};
diff --git a/datpacker.c b/datpacker.c
new file mode 100644
index 00000000..fcef477e
--- /dev/null
+++ b/datpacker.c
@@ -0,0 +1,232 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+#include
+#include
+#include
+#include
+#include
+#include "datpacker.h"
+#include "thdat.h"
+#include "program.h"
+#include "util.h"
+
+extern const archive_module_t archive_th02;
+extern const archive_module_t archive_th06;
+extern const archive_module_t archive_th75;
+extern const archive_module_t archive_th08;
+extern const archive_module_t archive_th95;
+
+char library_error[LIBRARY_ERROR_SIZE] = { 0 };
+
+static void
+print_usage()
+{
+ printf("Usage: %s [OPTION]... ARCHIVE FILE...\n"
+ "OPTION can be:\n"
+ " -v VERSION can be 2, 6, 7, 75, 8, 9, 95, 10, 11, or 12, defaults to 12\n"
+ " -h display this help and exit\n"
+ " -V display version information and exit\n\n"
+ "Additional documentation might be available at <" PACKAGE_URL ">.\n"
+ "Report bugs to <" PACKAGE_BUGREPORT ">.\n", argv0);
+}
+
+static entry_t*
+archive_add_entry(archive_t* archive, FILE* fd, const char* filename, unsigned int flags)
+{
+ entry_t* e;
+
+ /* TODO: Pass the index to add_entry instead. */
+#pragma omp critical
+ e = &archive->entries[archive->count++];
+
+ memset(e->name, 0, 256);
+ e->size = util_fsize(fd);
+ e->zsize = 0;
+ e->offset = 0;
+ e->extra = 0;
+
+ if (flags & THDAT_BASENAME)
+ util_basename(e->name, 255, filename);
+ else
+ strncpy(e->name, filename, 255);
+
+ if ((int)e->size == -1)
+ return NULL;
+
+ return e;
+}
+
+static int
+archive_check_duplicates(archive_t* archive)
+{
+ unsigned int i, j;
+
+ for (i = 0; i < archive->count; ++i) {
+ for (j = 0; j < archive->count; ++j) {
+ if (i == j)
+ continue;
+ if (strcmp(archive->entries[i].name, archive->entries[j].name) == 0) {
+ snprintf(library_error, LIBRARY_ERROR_SIZE, "duplicate filename ``%s''", archive->entries[i].name);
+ return 1;
+ }
+ }
+ }
+
+ return 0;
+}
+
+static void
+archive_free(archive_t* archive)
+{
+ if (archive) {
+ free(archive->entries);
+ free(archive);
+ }
+}
+
+int
+main(int argc, char* argv[])
+{
+ FILE* archive;
+ archive_t* private;
+ const archive_module_t* archive_module = NULL;
+ unsigned int version = 12;
+ int i;
+ int argstart;
+
+ argv0 = util_shortname(argv[0]);
+
+ for (i = 1; i < argc; ++i) {
+ if (strcmp(argv[i], "-h") == 0) {
+ print_usage();
+ return 0;
+ } else if (strcmp(argv[i], "-V") == 0) {
+ util_print_version("datpacker", PACKAGE_THDAT_VERSION);
+ return 0;
+ } else if (strcmp(argv[i], "-v") == 0) {
+ ++i;
+ if (i == argc) {
+ print_usage();
+ return 1;
+ }
+ version = strtol(argv[i], NULL, 10);
+ } else {
+ break;
+ }
+ }
+
+ if ((i + 2) > argc) {
+ print_usage();
+ return 1;
+ }
+
+ switch (version) {
+ case 2:
+ archive_module = &archive_th02;
+ break;
+ case 6:
+ case 7:
+ archive_module = &archive_th06;
+ break;
+ case 75:
+ archive_module = &archive_th75;
+ break;
+ case 8:
+ case 9:
+ archive_module = &archive_th08;
+ break;
+ case 95:
+ case 10:
+ case 11:
+ case 12:
+ archive_module = &archive_th95;
+ break;
+ }
+
+ if (!archive_module) {
+ fprintf(stderr, "%s: unsupported version\n", argv0);
+ return 1;
+ }
+
+ archive = fopen(argv[i], "wb");
+ if (!archive) {
+ fprintf(stderr, "%s: couldn't open %s for writing\n", argv0, argv[i]);
+ return 1;
+ }
+ current_output = argv[i];
+
+ private = archive_module->open(archive, version, argc - i - 1);
+ if (!private) {
+ fprintf(stderr, "%s: %s\n", argv0, library_error);
+ fclose(archive);
+ return 1;
+ }
+
+ argstart = i + 1;
+#pragma omp parallel shared(private)
+#pragma omp for
+ for (i = argstart; i < argc; ++i) {
+ entry_t* entry;
+ FILE* fd = fopen(argv[i], "rb");
+ if (!fd) {
+ fprintf(stderr, "%s: couldn't open %s for reading\n", argv0, argv[i]);
+ fclose(archive);
+ exit(1);
+ }
+ current_input = argv[i];
+#pragma omp critical
+{
+ printf("%s...\n", argv[i]);
+ fflush(stdout);
+}
+
+ entry = archive_add_entry(private, fd, argv[i], archive_module->flags);
+ if (!entry || archive_module->write(private, entry, fd) == -1) {
+ fprintf(stderr, "%s: %s\n", argv0, library_error);
+ fclose(fd);
+ fclose(archive);
+ archive_free(private);
+ exit(1);
+ }
+
+ fclose(fd);
+ }
+
+ if (archive_check_duplicates(private) || archive_module->close(private) == -1) {
+ fprintf(stderr, "%s: %s\n", argv0, library_error);
+ fclose(archive);
+ archive_free(private);
+ return 1;
+ }
+
+ archive_free(private);
+ fclose(archive);
+
+ return 0;
+}
diff --git a/datpacker.h b/datpacker.h
new file mode 100644
index 00000000..2eee6aa2
--- /dev/null
+++ b/datpacker.h
@@ -0,0 +1,73 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+#ifndef DATPACKER_H_
+#define DATPACKER_H_
+
+#include
+#include
+#include
+
+extern char library_error[];
+#define LIBRARY_ERROR_SIZE 512
+
+typedef struct {
+ char name[256];
+ uint32_t size;
+ uint32_t zsize;
+ uint32_t offset;
+
+ uint32_t extra;
+} entry_t;
+
+typedef struct {
+ unsigned int version;
+ FILE* fd;
+ uint32_t offset;
+ uint32_t count;
+ entry_t* entries;
+} archive_t;
+
+/* Strip path names. */
+#define THDAT_BASENAME 1
+
+typedef struct {
+ /* Takes a truncated file opened for writing, archive version, and an
+ * estimated file count. Returns private data, or NULL upon error. */
+ archive_t* (*open)(FILE*, unsigned int, unsigned int);
+ /* Takes private data, a file opened for reading, and a filename. Returns
+ * 0, or -1 upon error. Private data is freed upon error. */
+ int (*write)(archive_t*, entry_t*, FILE*);
+ /* Takes private data. Returns 0, or -1 upon error. Private data is always
+ * freed. */
+ int (*close)(archive_t*);
+ /* THDAT_ flags. */
+ uint32_t flags;
+} archive_module_t;
+
+#endif
diff --git a/ecl.h b/ecl.h
new file mode 100644
index 00000000..6c924809
--- /dev/null
+++ b/ecl.h
@@ -0,0 +1,160 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+#ifndef ECL_H_
+#define ECL_H_
+
+#include
+#include
+
+#define RANK_EASY (1 << 0)
+#define RANK_NORMAL (1 << 1)
+#define RANK_HARD (1 << 2)
+#define RANK_LUNATIC (1 << 3)
+
+#define ECLDUMP_MODE_NORMAL 0
+#define ECLDUMP_MODE_RAWDUMP 1
+#define ECLDUMP_MODE_PARAMETERS 2
+
+typedef struct {
+#ifdef PACK_PRAGMA
+#pragma pack(push,1)
+#endif
+ uint16_t unknown1; /* 1 */
+ uint16_t include_length; /* include_offset + ANIM+ECLI length */
+ uint32_t include_offset; /* 0x24 */
+ uint32_t zero1;
+ uint32_t sub_cnt;
+ uint32_t zero2[4];
+#ifdef PACK_PRAGMA
+#pragma pack(pop)
+#endif
+} PACK_ATTRIBUTE header_scpt_t;
+
+typedef struct {
+#ifdef PACK_PRAGMA
+#pragma pack(push,1)
+#endif
+ /* Size of an instruction header? */
+ uint32_t unknown1; /* 0x10 */
+ uint32_t zero[2];
+#ifdef PACK_PRAGMA
+#pragma pack(pop)
+#endif
+} PACK_ATTRIBUTE header_eclh_t;
+
+typedef struct {
+ /* Same as instruction format strings. */
+ unsigned char type;
+ /* Reference to stack value or not. */
+ unsigned int stack;
+ union {
+ int32_t i;
+ float f;
+ int32_t D[2];
+ struct {
+ uint32_t length;
+ char* data;
+ } s;
+ } value;
+} param_t;
+
+typedef struct {
+#ifdef PACK_PRAGMA
+#pragma pack(push,1)
+#endif
+ uint32_t time;
+ uint16_t id;
+ /* Size of the entire instruction. */
+ uint16_t size;
+ uint16_t param_mask;
+ uint8_t rank_mask;
+ uint8_t param_cnt;
+ /* uint32_t zero; */
+#ifdef PACK_PRAGMA
+#pragma pack(pop)
+#endif
+ param_t* params;
+ uint32_t offset;
+} PACK_ATTRIBUTE instr_t;
+
+typedef struct {
+#ifdef PACK_PRAGMA
+#pragma pack(push,1)
+#endif
+ uint32_t time;
+ uint16_t id;
+ uint16_t size;
+ uint16_t param_mask;
+ /* The rank bitmask.
+ * 1111LHNE
+ * Bits mean: easy, normal, hard, lunatic. The rest are always set to 1. */
+ uint8_t rank_mask;
+ /* There doesn't seem to be a way of telling how many parameters there are
+ * from the additional data. */
+ uint8_t param_cnt;
+ uint32_t zero;
+#ifdef PACK_PRAGMA
+#pragma pack(pop)
+#endif
+ /* extra: */
+ uint16_t data_size;
+ char* data;
+ uint32_t offset;
+} PACK_ATTRIBUTE raw_instr_t;
+
+typedef struct {
+ char* name;
+ int32_t offset;
+} label_t;
+
+typedef struct {
+ char* name;
+ /* Used both for the file offset and instruction offsets. */
+ uint32_t offset;
+ unsigned int instr_cnt;
+ raw_instr_t* raw_instrs;
+ instr_t* instrs;
+ unsigned int label_cnt;
+ label_t* labels;
+} sub_t;
+
+typedef struct {
+ header_scpt_t scpt;
+
+ uint32_t anim_cnt;
+ char** anim;
+
+ uint32_t ecli_cnt;
+ char** ecli;
+
+ uint16_t sub_cnt;
+ sub_t* subs;
+} ecl_t;
+
+#endif
diff --git a/ecldump.c b/ecldump.c
new file mode 100644
index 00000000..82887ee3
--- /dev/null
+++ b/ecldump.c
@@ -0,0 +1,625 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include "program.h"
+#include "ecl.h"
+#include "instr.h"
+#include "util.h"
+
+static FILE* out;
+
+static void
+print_usage()
+{
+ printf("Usage: %s -v {10,11,12} [OPTION]... FILE\n"
+ "OPTION can be:\n"
+ " -o FILE write output to the specified file\n"
+ " -r raw dump\n"
+ " -p generate parameter table\n"
+ " -h display this help and exit\n"
+ " -V display version information and exit\n\n"
+ "Additional documentation might be available at <" PACKAGE_URL ">.\n"
+ "Report bugs to <" PACKAGE_BUGREPORT ">.\n", argv0);
+}
+
+/* It would probably be easier to read the entire file at once and use pointers
+ * to access the data. */
+static int
+open_ecl(ecl_t* ecl, FILE* f)
+{
+ unsigned int i;
+ char magic[5] = { 0 };
+
+ if (fread(magic, 4, 1, f) != 1) {
+ fprintf(stderr, "%s:%s: couldn't read: %s\n",
+ argv0, current_input, strerror(errno));
+ return -1;
+ }
+ if (strncmp(magic, "SCPT", 4) != 0) {
+ fprintf(stderr, "%s:%s: SCPT signature missing\n", argv0, current_input);
+ return -1;
+ }
+
+ if (fread(&ecl->scpt, sizeof(header_scpt_t), 1, f) != 1) {
+ fprintf(stderr, "%s:%s: couldn't read: %s\n",
+ argv0, current_input, strerror(errno));
+ return -1;
+ }
+ assert(ecl->scpt.unknown1 == 1);
+ assert(ecl->scpt.include_offset == 0x24);
+ assert(ecl->scpt.zero1 == 0);
+ assert(ecl->scpt.zero2[0] == 0);
+ assert(ecl->scpt.zero2[1] == 0);
+ assert(ecl->scpt.zero2[2] == 0);
+ assert(ecl->scpt.zero2[3] == 0);
+
+ if (fseek(f, ecl->scpt.include_offset, SEEK_SET) == -1) {
+ fprintf(stderr, "%s:%s: couldn't seek: %s\n",
+ argv0, current_input, strerror(errno));
+ return -1;
+ }
+
+ if (fread(magic, 4, 1, f) != 1) {
+ fprintf(stderr, "%s:%s: couldn't read: %s\n",
+ argv0, current_input, strerror(errno));
+ return -1;
+ }
+ if (strncmp(magic, "ANIM", 4) != 0) {
+ fprintf(stderr, "%s:%s: ANIM signature missing\n", argv0, current_input);
+ return -1;
+ }
+
+ if (fread(&ecl->anim_cnt, sizeof(uint32_t), 1, f) != 1) {
+ fprintf(stderr, "%s:%s: couldn't read: %s\n",
+ argv0, current_input, strerror(errno));
+ return -1;
+ }
+ ecl->anim = malloc(sizeof(char*) * ecl->anim_cnt);
+ for (i = 0; i < ecl->anim_cnt; ++i) {
+ char buffer[256];
+ util_read_asciiz(buffer, 256, f);
+ ecl->anim[i] = strdup(buffer);
+ }
+
+ while (ftell(f) % 4 != 0) {
+ if (fgetc(f) == EOF)
+ break;
+ }
+
+ if (fread(magic, 4, 1, f) != 1) {
+ fprintf(stderr, "%s:%s: couldn't read: %s\n",
+ argv0, current_input, strerror(errno));
+ return -1;
+ }
+ if (strncmp(magic, "ECLI", 4) != 0) {
+ fprintf(stderr, "%s:%s: ECLI signature missing\n", argv0, current_input);
+ return -1;
+ }
+
+ if (fread(&ecl->ecli_cnt, sizeof(uint32_t), 1, f) != 1) {
+ fprintf(stderr, "%s:%s: couldn't read: %s\n",
+ argv0, current_input, strerror(errno));
+ return -1;
+ }
+ ecl->ecli = malloc(sizeof(char*) * ecl->ecli_cnt);
+ for (i = 0; i < ecl->ecli_cnt; ++i) {
+ char buffer[256];
+ util_read_asciiz(buffer, 256, f);
+ ecl->ecli[i] = strdup(buffer);
+ }
+
+ ecl->sub_cnt = ecl->scpt.sub_cnt;
+ ecl->subs = calloc(ecl->sub_cnt, sizeof(sub_t));
+
+ while (ftell(f) % 4 != 0) {
+ if (fgetc(f) == EOF)
+ break;
+ }
+
+ for (i = 0; i < ecl->sub_cnt; ++i) {
+ if (fread(&ecl->subs[i].offset, sizeof(uint32_t), 1, f) != 1) {
+ fprintf(stderr, "%s:%s: couldn't read: %s\n",
+ argv0, current_input, strerror(errno));
+ return -1;
+ }
+ }
+
+ for (i = 0; i < ecl->sub_cnt; ++i) {
+ /* XXX: Maximum length? */
+ char buffer[256];
+ util_read_asciiz(buffer, 256, f);
+ ecl->subs[i].name = strdup(buffer);
+ }
+
+ for (i = 0; i < ecl->sub_cnt; ++i) {
+ header_eclh_t eclh;
+ sub_t* sub;
+
+ if (fseek(f, ecl->subs[i].offset, SEEK_SET) == -1) {
+ fprintf(stderr, "%s:%s: couldn't seek: %s\n",
+ argv0, current_input, strerror(errno));
+ return -1;
+ }
+
+ if (fread(magic, 4, 1, f) != 1) {
+ fprintf(stderr, "%s:%s: couldn't read: %s\n",
+ argv0, current_input, strerror(errno));
+ return -1;
+ }
+ if (strncmp(magic, "ECLH", 4) != 0) {
+ fprintf(stderr, "%s:%s: ECLH signature missing\n",
+ argv0, current_input);
+ return -1;
+ }
+
+ if (fread(&eclh, sizeof(header_eclh_t), 1, f) != 1) {
+ fprintf(stderr, "%s:%s: couldn't read: %s\n",
+ argv0, current_input, strerror(errno));
+ return -1;
+ }
+
+ assert(eclh.unknown1 == 0x10);
+ assert(eclh.zero[0] == 0);
+ assert(eclh.zero[1] == 0);
+
+ sub = &ecl->subs[i];
+ sub->instr_cnt = 0;
+ sub->raw_instrs = NULL;
+
+ /* Unfortunately the instruction count is not provided. */
+ while (1) {
+ raw_instr_t rins;
+ memset(&rins, 0, sizeof(raw_instr_t));
+ rins.offset = ftell(f);
+
+ /* EOF is expected for the last instruction. */
+ if (fread(&rins, 16, 1, f) != 1)
+ break;
+
+ if (i + 1 != ecl->sub_cnt && ftell(f) > ecl->subs[i + 1].offset)
+ break;
+
+ assert(rins.zero == 0);
+ assert(rins.size >= 16);
+
+ rins.data_size = rins.size - 16;
+ if (rins.data_size) {
+ rins.data = malloc(rins.data_size);
+ if (fread(rins.data, rins.data_size, 1, f) != 1) {
+ fprintf(stderr, "%s:%s: couldn't read: %s\n",
+ argv0, current_input, strerror(errno));
+ return -1;
+ }
+ }
+
+ sub->instr_cnt++;
+ sub->raw_instrs = realloc(sub->raw_instrs,
+ sub->instr_cnt * sizeof(raw_instr_t));
+ sub->raw_instrs[sub->instr_cnt - 1] = rins;
+ }
+ }
+
+ return 0;
+}
+
+static void
+ecldump_list_params(ecl_t* ecl)
+{
+ unsigned int i;
+
+ char instr_params[1024][16];
+ memset(instr_params, -1, 1024 * 16);
+
+ for (i = 0; i < ecl->sub_cnt; ++i) {
+ unsigned int j;
+ for (j = 0; j < ecl->subs[i].instr_cnt; ++j) {
+ unsigned int k;
+ raw_instr_t* rins = &ecl->subs[i].raw_instrs[j];
+
+ assert(rins->id <= 1024);
+ assert(rins->param_cnt <= 15);
+
+ switch (instr_params[rins->id][0]) {
+ case -2: /* Broken. */
+ break;
+ case -1: /* New. */
+ memset(instr_params[rins->id], 0, 16);
+ if (rins->param_cnt != rins->data_size >> 2) {
+ instr_params[rins->id][0] = -2;
+ break;
+ } else {
+ instr_params[rins->id][0] = rins->param_cnt;
+ }
+ default:
+ for (k = 0; k < rins->param_cnt; ++k) {
+ if (instr_params[rins->id][k + 1] != 'i') {
+ /* TODO: This float checking stuff is pretty ugly,
+ * move it to util.c so it can be fixed more easily. */
+ int32_t integer = 0;
+ float floating = 0;
+ char buffer[128];
+ memcpy(&integer,
+ rins->data + k * sizeof(int32_t),
+ sizeof(int32_t));
+ memcpy(&floating,
+ rins->data + k * sizeof(int32_t),
+ sizeof(int32_t));
+ sprintf(buffer, "%.9f", floating);
+ if (((integer & 0xfffff000) == 0xfffff000) ||
+ (strcmp("nan", buffer) == 0) ||
+ (integer != 0 && strcmp("0.000000000", buffer) == 0))
+ instr_params[rins->id][k + 1] = 'i';
+ else
+ instr_params[rins->id][k + 1] = 'f';
+ }
+ }
+ break;
+ }
+ }
+ }
+
+ for (i = 0; i < 1024; ++i) {
+ if (instr_params[i][0] >= 0) {
+ fprintf(out, " { %u, \"%s\" },\n", i, instr_params[i] + 1);
+ } else if (instr_params[i][0] == -2) {
+ fprintf(out, " /*{ %u, \"%s\" },*/\n", i, instr_params[i] + 1);
+ }
+ }
+}
+
+static void
+ecldump_rawdump(ecl_t* ecl)
+{
+ unsigned int i;
+
+ for (i = 0; i < ecl->sub_cnt; ++i) {
+ unsigned int j;
+
+ fprintf(out, "Subroutine %s\n", ecl->subs[i].name);
+
+ for (j = 0; j < ecl->subs[i].instr_cnt; ++j) {
+ raw_instr_t* rins = &ecl->subs[i].raw_instrs[j];
+ unsigned int k;
+
+ fprintf(out, "Instruction %u %u %u 0x%02x %u %u: ",
+ rins->time, rins->id, rins->size, rins->param_mask,
+ rins->rank_mask, rins->param_cnt);
+
+ for (k = 0; k < rins->data_size; k += sizeof(uint32_t)) {
+ fprintf(out, "0x%08x ", *(uint32_t*)(rins->data + k));
+ }
+
+ fprintf(out, "\n");
+ }
+
+ fprintf(out, "\n");
+ }
+}
+
+static void
+ecldump_translate(ecl_t* ecl, unsigned int version)
+{
+ unsigned int i;
+
+ for (i = 0; i < ecl->sub_cnt; ++i) {
+ unsigned int j;
+
+ ecl->subs[i].instrs = malloc(sizeof(instr_t) * ecl->subs[i].instr_cnt);
+
+ for (j = 0; j < ecl->subs[i].instr_cnt; ++j) {
+ if (!instr_parse(&ecl->subs[i].raw_instrs[j],
+ &ecl->subs[i].instrs[j], version)) {
+ exit(1);
+ }
+ ecl->subs[i].instrs[j].offset =
+ ecl->subs[i].raw_instrs[j].offset - ecl->subs[i].offset;
+ }
+ }
+}
+
+static void
+ecldump_translate_print(ecl_t* ecl)
+{
+ unsigned int i;
+
+ if (ecl->anim_cnt) {
+ fprintf(out, "anim { ");
+ for (i = 0; i < ecl->anim_cnt; ++i)
+ fprintf(out, "\"%s\"; ", ecl->anim[i]);
+ fprintf(out, "}\n");
+ }
+
+ if (ecl->ecli_cnt) {
+ fprintf(out, "ecli { ");
+ for (i = 0; i < ecl->ecli_cnt; ++i)
+ fprintf(out, "\"%s\"; ", ecl->ecli[i]);
+ fprintf(out, "}\n");
+ }
+
+ for (i = 0; i < ecl->sub_cnt; ++i) {
+ unsigned int j, k, m;
+ unsigned int time = 0;
+
+ fprintf(out, "\nsub %s\n{\n", ecl->subs[i].name);
+
+ for (j = 0; j < ecl->subs[i].instr_cnt; ++j) {
+ instr_t* instr = &ecl->subs[i].instrs[j];
+
+ if (instr->time != time) {
+ time = instr->time;
+ fprintf(out, "%u:\n", time);
+ }
+
+ /* If there is a reference to this instruction, print a label. */
+ for (k = 0; k < ecl->subs[i].instr_cnt; ++k) {
+ for (m = 0; m < ecl->subs[i].instrs[k].param_cnt; ++m) {
+ if (ecl->subs[i].instrs[k].params[m].type == 'o' &&
+ instr->offset == ecl->subs[i].instrs[k].offset + ecl->subs[i].instrs[k].params[m].value.i) {
+ fprintf(out, "%s_%u:\n",
+ ecl->subs[i].name, instr->offset);
+ k = ecl->subs[i].instr_cnt;
+ break;
+ }
+ }
+ }
+
+ fprintf(out, " %u", instr->id);
+
+ if (instr->rank_mask != 0xff) {
+ fprintf(out, " +");
+ if (instr->rank_mask & RANK_EASY) fputc('E', out);
+ if (instr->rank_mask & RANK_NORMAL) fputc('N', out);
+ if (instr->rank_mask & RANK_HARD) fputc('H', out);
+ if (instr->rank_mask & RANK_LUNATIC) fputc('L', out);
+ }
+
+ for (k = 0; k < instr->param_cnt; ++k) {
+ const char* floatb;
+retry:
+ switch (instr->params[k].type) {
+ case 'i':
+ if (instr->param_mask & (1 << k)) {
+ if (instr->params[k].value.i >= 0 &&
+ instr->params[k].value.i % 4) {
+ fprintf(stderr, "%s:%s: strange stack offset: %d\n",
+ argv0, current_input, instr->params[k].value.i);
+ abort();
+ }
+ fprintf(out, " [%d]", instr->params[k].value.i);
+ } else
+ fprintf(out, " %d", instr->params[k].value.i);
+ break;
+ case 'o':
+ fprintf(out, " %s_%u",
+ ecl->subs[i].name, instr->offset + instr->params[k].value.i);
+ break;
+ case 'f':
+ floatb = util_printfloat(&instr->params[k].value.f);
+ if (instr->param_mask & (1 << k)) {
+ int tempint = instr->params[k].value.f;
+ /* XXX: Exactly how well does this work? */
+ if (floor(instr->params[k].value.f) != instr->params[k].value.f)
+ fprintf(stderr, "%s:%s: non-integral float: %s\n",
+ argv0, current_input, floatb);
+ if (tempint >= 0 && tempint % 4) {
+ fprintf(stderr, "%s:%s: strange stack offset: %s\n",
+ argv0, current_input, floatb);
+ }
+ fprintf(out, " [%sf]", floatb);
+ } else
+ fprintf(out, " %sf", floatb);
+ break;
+ case 's':
+ fprintf(out, " \"%s\"", instr->params[k].value.s.data);
+ break;
+ case 'c':
+ fprintf(out, " C\"%s\"", instr->params[k].value.s.data);
+ break;
+ case 'D':
+ if (instr->params[k].value.D[0] == 0x6666) {
+ fprintf(out, " (float)");
+ instr->params[k].type = 'f';
+ memcpy(&instr->params[k].value.f,
+ &instr->params[k].value.D[1],
+ sizeof(float));
+ } else if (instr->params[k].value.D[0] == 0x6669) {
+ fprintf(out, " (float)");
+ instr->params[k].type = 'i';
+ memcpy(&instr->params[k].value.i,
+ &instr->params[k].value.D[1],
+ sizeof(int32_t));
+ } else if (instr->params[k].value.D[0] == 0x6969) {
+ fprintf(out, " (int)");
+ instr->params[k].type = 'i';
+ memcpy(&instr->params[k].value.i,
+ &instr->params[k].value.D[1],
+ sizeof(int32_t));
+ } else {
+ fprintf(stderr, "%s:%s: unknown value: %u\n",
+ argv0, current_input, instr->params[k].value.D[0]);
+ abort();
+ }
+ goto retry;
+ break;
+ default:
+ break;
+ }
+ }
+ fprintf(out, ";\n");
+ }
+ fprintf(out, "}\n");
+ }
+}
+
+static void
+ecldump_free(ecl_t* ecl)
+{
+ unsigned int i, j, k;
+
+ if (ecl->anim) {
+ for (i = 0; i < ecl->anim_cnt; ++i)
+ free(ecl->anim[i]);
+ free(ecl->anim);
+ }
+
+ if (ecl->ecli) {
+ for (i = 0; i < ecl->ecli_cnt; ++i)
+ free(ecl->ecli[i]);
+ free(ecl->ecli);
+ }
+
+ if (ecl->subs) {
+ for (i = 0; i < ecl->sub_cnt; ++i) {
+ free(ecl->subs[i].name);
+
+ for (j = 0; j < ecl->subs[i].instr_cnt; ++j) {
+ free(ecl->subs[i].raw_instrs[j].data);
+
+ if (ecl->subs[i].instrs && ecl->subs[i].instrs[j].params) {
+ for (k = 0; k < ecl->subs[i].instrs[j].param_cnt; ++k) {
+ /* 'o' might store data, but not in ecldump. */
+ if (ecl->subs[i].instrs[j].params[k].type == 's' ||
+ ecl->subs[i].instrs[j].params[k].type == 'c') {
+ free(ecl->subs[i].instrs[j].params[k].value.s.data);
+ }
+ }
+
+ free(ecl->subs[i].instrs[j].params);
+ }
+ }
+
+ free(ecl->subs[i].raw_instrs);
+ free(ecl->subs[i].instrs);
+
+ for (j = 0; j < ecl->subs[i].label_cnt; ++j)
+ free(ecl->subs[i].labels[j].name);
+
+ free(ecl->subs[i].labels);
+ }
+ free(ecl->subs);
+ }
+}
+
+int
+main(int argc, char* argv[])
+{
+ int i;
+ FILE* f;
+ unsigned int mode = ECLDUMP_MODE_NORMAL;
+ unsigned int version = 0;
+ ecl_t ecl;
+
+ f = stdin;
+ current_input = "(stdin)";
+ out = stdout;
+
+ argv0 = util_shortname(argv[0]);
+ memset(&ecl, 0, sizeof(ecl_t));
+
+ for (i = 1; i < argc; ++i) {
+ if (strcmp(argv[i], "-h") == 0) {
+ print_usage();
+ exit(0);
+ } else if (strcmp(argv[i], "-V") == 0) {
+ util_print_version("ecldump", PACKAGE_THECL_VERSION);
+ exit(0);
+ } else if (strcmp(argv[i], "-v") == 0) {
+ ++i;
+ if (i == argc) {
+ print_usage();
+ exit(1);
+ }
+ version = strtol(argv[i], NULL, 10);
+ } else if (strcmp(argv[i], "-r") == 0) {
+ mode = ECLDUMP_MODE_RAWDUMP;
+ } else if (strcmp(argv[i], "-p") == 0) {
+ mode = ECLDUMP_MODE_PARAMETERS;
+ } else if (strcmp(argv[i], "-o") == 0) {
+ ++i;
+ if (i == argc) {
+ print_usage();
+ exit(1);
+ }
+ out = fopen(argv[i], "w");
+ if (!out) {
+ fprintf(stderr, "%s: couldn't open %s for writing: %s\n",
+ argv0, argv[i], strerror(errno));
+ exit(1);
+ }
+ } else {
+ break;
+ }
+ }
+
+ if (version != 10 && version != 11 && version != 12) {
+ print_usage();
+ exit(1);
+ }
+
+ if (i != argc) {
+ f = fopen(argv[i], "rb");
+ if (!f) {
+ fprintf(stderr, "%s: couldn't open %s for reading: %s\n",
+ argv0, argv[i], strerror(errno));
+ exit(1);
+ }
+
+ current_input = argv[i];
+ }
+
+ if (open_ecl(&ecl, f) != 0)
+ exit(1);
+
+ fclose(f);
+
+ switch (mode) {
+ case ECLDUMP_MODE_NORMAL:
+ ecldump_translate(&ecl, version);
+ ecldump_translate_print(&ecl);
+ break;
+ case ECLDUMP_MODE_PARAMETERS:
+ ecldump_list_params(&ecl);
+ break;
+ /* This mode might be useful for newer formats. */
+ case ECLDUMP_MODE_RAWDUMP:
+ ecldump_rawdump(&ecl);
+ break;
+ }
+
+ ecldump_free(&ecl);
+
+ return 0;
+}
diff --git a/instr.c b/instr.c
new file mode 100644
index 00000000..4f980363
--- /dev/null
+++ b/instr.c
@@ -0,0 +1,614 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+#include
+#include
+#include
+#include
+#include
+#include
+#include "program.h"
+#include "ecl.h"
+#include "util.h"
+
+typedef struct {
+ int id;
+ const char* format;
+} instr_fmt_t;
+
+static const instr_fmt_t th10_fmts[] = {
+ { 0, "" },
+ { 1, "" },
+ { 10, "" },
+ { 11, "s?D*" },
+ { 12, "oi" },
+ { 13, "oi" },
+ { 14, "oi" },
+ { 15, "s?D*" },
+ { 16, "si" },
+ { 17, "i" },
+ { 21, "" },
+ { 40, "i" },
+ { 42, "i" },
+ { 43, "i" },
+ { 44, "f" },
+ { 45, "f" },
+ { 50, "" },
+ { 51, "" },
+ { 52, "" },
+ { 53, "" },
+ { 54, "" },
+ { 55, "" },
+ { 56, "" },
+ { 57, "" },
+ { 58, "" },
+ { 59, "" },
+ { 61, "" },
+ { 63, "" },
+ { 64, "" },
+ { 65, "" },
+ { 67, "" },
+ { 68, "" },
+ { 69, "" },
+ { 70, "" },
+ { 71, "" },
+ { 73, "" },
+ { 74, "" },
+ { 78, "i" },
+ { 79, "" },
+ { 81, "ffff" },
+ { 82, "f" },
+ { 83, "i" },
+ { 85, "" },
+ { 256, "sffiii" },
+ { 257, "sffiii" },
+ { 258, "i" },
+ { 259, "ii" },
+ { 260, "sffiii" },
+ { 261, "sffiii" },
+ { 262, "ii" },
+ { 263, "ii" },
+ { 264, "ii" },
+ { 265, "sffiii" },
+ { 266, "sffiii" },
+ { 267, "sffiii" },
+ { 268, "sffiii" },
+ { 269, "i" },
+ { 270, "sfffiii" },
+ { 272, "ii" },
+ { 273, "iif" },
+ { 280, "ff" },
+ { 281, "iiff" },
+ { 282, "ff" },
+ { 283, "iifi" },
+ { 284, "ff" },
+ { 285, "iiff" },
+ { 286, "ff" },
+ { 287, "iiff" },
+ { 288, "ffff" },
+ { 290, "ffff" },
+ { 291, "iifffi" },
+ { 292, "iif" },
+ { 294, "" },
+ { 296, "iif" },
+ { 298, "ff" },
+ { 299, "ff" },
+ { 320, "ff" },
+ { 321, "ff" },
+ { 322, "i" },
+ { 323, "i" },
+ { 324, "ifff" },
+ { 325, "" },
+ { 326, "" },
+ { 327, "ii" },
+ { 328, "ff" },
+ { 329, "" },
+ { 330, "i" },
+ { 331, "i" },
+ { 332, "i" },
+ { 333, "" },
+ { 334, "iiis" },
+ { 335, "i" },
+ { 336, "i" },
+ { 337, "iii" },
+ { 338, "i" },
+ { 339, "" },
+ { 340, "" },
+ { 341, "is" },
+ { 342, "iiic" },
+ { 343, "" },
+ { 344, "i" },
+ { 345, "" },
+ { 346, "f" },
+ { 347, "ifi" },
+ { 355, "iiiii" },
+ { 356, "fffff" },
+ { 357, "iiic" },
+ { 359, "iiic" },
+ { 360, "i" },
+ { 361, "i" },
+ { 362, "" },
+ { 363, "" },
+ { 364, "i" },
+ { 365, "" },
+ { 366, "ii" },
+ { 367, "f" },
+ { 368, "iiii" },
+ { 400, "i" },
+ { 401, "i" },
+ { 402, "iii" },
+ { 403, "iff" },
+ { 404, "iff" },
+ { 405, "iff" },
+ { 406, "iii" },
+ { 407, "ii" },
+ { 409, "iiiiiiff" },
+ { 410, "" },
+ { 411, "ii" },
+ { 412, "iiffffif" },
+ { 413, "iiifffiiiifi" },
+ { 420, "f" },
+ { 421, "f" },
+ { 425, "iiiiiii" },
+ { 428, "iiffifif" },
+ { 431, "iiffifff" },
+ { 433, "iiffifff" },
+ { 435, "iffffffff" },
+ { 436, "iiiiiiiii" },
+ { -1, NULL }
+};
+
+static const instr_fmt_t th11_fmts[] = {
+ { 0, "" },
+ { 1, "" },
+ { 10, "" },
+ { 11, "s?D*" },
+ { 12, "oi" },
+ { 13, "oi" },
+ { 14, "oi" },
+ { 15, "s?D*" },
+ { 16, "si" },
+ { 17, "i" },
+ { 21, "" },
+ { 40, "i" },
+ { 42, "i" },
+ { 43, "i" },
+ { 44, "f" },
+ { 45, "f" },
+ { 50, "" },
+ { 51, "" },
+ { 52, "" },
+ { 53, "" },
+ { 54, "" },
+ { 55, "" },
+ { 57, "" },
+ { 58, "" },
+ { 59, "" },
+ { 61, "" },
+ { 63, "" },
+ { 64, "" },
+ { 65, "" },
+ { 66, "" },
+ { 67, "" },
+ { 68, "" },
+ { 69, "" },
+ { 70, "" },
+ { 73, "" },
+ { 78, "i" },
+ { 81, "ffff" },
+ { 82, "f" },
+ { 83, "i" },
+ { 85, "" },
+ { 256, "sffiii" },
+ { 257, "sffiii" },
+ { 258, "i" },
+ { 259, "ii" },
+ { 260, "sffiii" },
+ { 261, "sffiii" },
+ { 262, "ii" },
+ { 263, "ii" },
+ { 264, "ii" },
+ { 265, "sffiii" },
+ { 266, "sffiii" },
+ { 267, "sffiii" },
+ { 268, "sffiii" },
+ { 269, "i" },
+ { 275, "fi" },
+ { 276, "" },
+ { 277, "ff" },
+ { 278, "i" },
+ { 280, "ff" },
+ { 281, "iiff" },
+ { 282, "ff" },
+ { 283, "iifi" },
+ { 284, "ff" },
+ { 285, "iiff" },
+ { 286, "ff" },
+ { 287, "iiff" },
+ { 288, "ffff" },
+ { 289, "iifff" },
+ { 290, "ffff" },
+ { 291, "iiffi" },
+ { 292, "iif" },
+ { 300, "ffffff" },
+ { 302, "ffffff" },
+ { 305, "iffffff" },
+ { 307, "" },
+ { 320, "ff" },
+ { 321, "ff" },
+ { 322, "i" },
+ { 323, "i" },
+ { 324, "ifff" },
+ { 325, "" },
+ { 326, "" },
+ { 327, "ii" },
+ { 328, "ff" },
+ { 329, "" },
+ { 330, "i" },
+ { 331, "i" },
+ { 332, "i" },
+ { 333, "" },
+ { 334, "iiis" },
+ { 335, "i" },
+ { 336, "i" },
+ { 337, "iii" },
+ { 338, "i" },
+ { 339, "" },
+ { 340, "" },
+ { 341, "is" },
+ { 342, "iiic" },
+ { 343, "" },
+ { 344, "i" },
+ { 345, "" },
+ { 346, "f" },
+ { 347, "ifi" },
+ { 355, "iiiii" },
+ { 356, "fffff" },
+ { 357, "iiic" },
+ { 359, "iiic" },
+ { 360, "i" },
+ { 362, "" },
+ { 363, "" },
+ { 365, "" },
+ { 367, "f" },
+ { 368, "iiii" },
+ { 369, "i" },
+ { 370, "i" },
+ { 371, "i" },
+ { 400, "i" },
+ { 401, "i" },
+ { 402, "iii" },
+ { 403, "iff" },
+ { 404, "iff" },
+ { 405, "iff" },
+ { 406, "iii" },
+ { 407, "ii" },
+ { 408, "iii" },
+ { 409, "iiiiiiff" },
+ { 410, "" },
+ { 411, "ii" },
+ { 412, "iiffffif" },
+ { 413, "iiifffiiiifi" },
+ { 420, "f" },
+ { 421, "f" },
+ { 431, "iiffifff" },
+ { 433, "iiffifff" },
+ { 435, "iffffffff" },
+ { 436, "iiiiiiiii" },
+ { 437, "iff" },
+ { 438, "if" },
+ { 439, "iff" },
+ { 440, "fi" },
+ { 441, "i" },
+ { 442, "i" },
+ { 443, "i" },
+ { 444, "i" },
+ { 445, "i" },
+ { 447, "f" },
+ { 448, "i" },
+ { 449, "i" },
+ { 450, "i" },
+ { 500, "i" },
+ { -1, NULL }
+};
+
+static const instr_fmt_t th12_fmts[] = {
+ { 0, "" },
+ { 1, "" },
+ { 10, "" },
+ { 11, "s?D*" },
+ { 12, "oi" },
+ { 13, "oi" },
+ { 14, "oi" },
+ { 15, "s?D*" },
+ { 16, "si?D*" },
+ { 17, "f" },
+ { 21, "" },
+ { 40, "i" },
+ { 42, "i" },
+ { 43, "i" },
+ { 44, "f" },
+ { 45, "f" },
+ { 50, "" },
+ { 51, "" },
+ { 52, "" },
+ { 53, "" },
+ { 54, "" },
+ { 55, "" },
+ { 56, "" },
+ { 57, "" },
+ { 58, "" },
+ { 59, "" },
+ { 60, "" },
+ { 61, "" },
+ { 63, "" },
+ { 64, "" },
+ { 65, "" },
+ { 66, "" },
+ { 67, "" },
+ { 68, "" },
+ { 69, "" },
+ { 70, "" },
+ { 73, "" },
+ { 74, "" },
+ { 78, "i" },
+ { 81, "ffff" },
+ { 82, "f" },
+ { 83, "i" },
+ { 86, "fff" },
+ { 87, "fffff" },
+ { 89, "fff" },
+ { 256, "sffiii" },
+ { 257, "sffiii" },
+ { 258, "i" },
+ { 259, "ii" },
+ { 260, "sffiii" },
+ { 261, "sffiii" },
+ { 262, "ii" },
+ { 263, "ii" },
+ { 264, "fi" },
+ { 265, "sffiii" },
+ { 267, "sffiii" },
+ { 269, "f" },
+ { 270, "sffiiii" },
+ { 273, "iif" },
+ { 274, "fi" },
+ { 275, "fi" },
+ { 276, "" },
+ { 277, "ff" },
+ { 278, "iff" },
+ { 279, "iff" },
+ { 280, "siiiii" },
+ { 281, "if" },
+ { 300, "ff" },
+ { 301, "iiff" },
+ { 304, "ff" },
+ { 305, "iiff" },
+ { 308, "ffff" },
+ { 309, "iifff" },
+ { 310, "ffff" },
+ { 311, "iifff" },
+ { 312, "iif" },
+ { 320, "ffffff" },
+ { 321, "iifffff" },
+ { 325, "iffffff" },
+ { 328, "ff" },
+ { 329, "ifff" },
+ { 400, "ff" },
+ { 401, "ff" },
+ { 402, "i" },
+ { 403, "i" },
+ { 404, "ffff" },
+ { 405, "" },
+ { 406, "" },
+ { 407, "ii" },
+ { 408, "ff" },
+ { 409, "" },
+ { 410, "i" },
+ { 411, "i" },
+ { 412, "i" },
+ { 413, "" },
+ { 414, "iiis" },
+ { 415, "i" },
+ { 416, "i" },
+ { 417, "iii" },
+ { 418, "i" },
+ { 419, "" },
+ { 420, "" },
+ { 421, "is" },
+ { 422, "iiic" },
+ { 423, "" },
+ { 424, "i" },
+ { 425, "" },
+ { 427, "ifi" },
+ { 435, "iiiii" },
+ { 436, "fffff" },
+ { 437, "iiic" },
+ { 438, "iiic" },
+ { 439, "iiic" },
+ { 440, "i" },
+ { 442, "" },
+ { 443, "" },
+ { 444, "i" },
+ { 445, "" },
+ { 446, "if" },
+ { 447, "f" },
+ { 448, "iiii" },
+ { 452, "f" },
+ { 453, "i" },
+ { 454, "" },
+ { 455, "ii" },
+ { 500, "i" },
+ { 501, "i" },
+ { 502, "iii" },
+ { 503, "iff" },
+ { 504, "iff" },
+ { 505, "iff" },
+ { 506, "iii" },
+ { 507, "ii" },
+ { 508, "iii" },
+ { 509, "iiiiiiff" },
+ { 510, "" },
+ { 511, "ii" },
+ { 512, "f" },
+ { 513, "f" },
+ { 520, "fff" },
+ { 521, "iffffffff" },
+ { 522, "iiiiiiiii" },
+ { 523, "iff" },
+ { 524, "if" },
+ { 525, "iff" },
+ { 526, "fi" },
+ { 529, "i" },
+ { 531, "i" },
+ { 534, "i" },
+ { 600, "iffff" },
+ { 601, "iiiiii" },
+ { 602, "i" },
+ { 603, "fi" },
+ { 608, "if" },
+ { 610, "i" },
+ { 611, "i" },
+ { 700, "i" },
+ { -1, NULL }
+};
+
+static const char*
+format_find(unsigned int version, int id)
+{
+ unsigned int i;
+ const instr_fmt_t* fmts = NULL;
+
+ switch (version) {
+ case 10: fmts = th10_fmts; break;
+ case 11: fmts = th11_fmts; break;
+ case 12: fmts = th12_fmts; break;
+ default:
+ fprintf(stderr, "%s: unsupported version: %u\n", argv0, version);
+ return NULL;
+ }
+
+ for (i = 0; fmts[i].id != -1; ++i)
+ if (fmts[i].id == id)
+ return fmts[i].format;
+
+ fprintf(stderr, "%s: instruction %u is not defined for version %u\n", argv0, id, version);
+ return NULL;
+}
+
+instr_t*
+instr_parse(raw_instr_t* rinstr, instr_t* instr, unsigned int version)
+{
+ unsigned int param_cnt;
+ unsigned int i;
+ unsigned int data_offset = 0;
+ const char* format = NULL;
+
+ format = format_find(version, rinstr->id);
+ if (!format)
+ return NULL;
+
+ param_cnt = strlen(format);
+
+ memset(instr, 0, sizeof(instr_t));
+ instr->time = rinstr->time;
+ instr->id = rinstr->id;
+ instr->size = rinstr->size;
+ instr->param_mask = rinstr->param_mask;
+ instr->rank_mask = rinstr->rank_mask;
+ instr->param_cnt = 0;
+ instr->params = NULL;
+ instr->offset = 0;
+
+ for (i = 0; i < param_cnt; ++i) {
+ if (format[i] == '?') {
+ if (data_offset == rinstr->data_size)
+ i = param_cnt;
+ } else if (format[i] == '*') {
+ if (data_offset == rinstr->data_size)
+ i = param_cnt;
+ else
+ i -= 2;
+ } else {
+ param_t* param;
+
+ if (data_offset == rinstr->data_size) {
+ fprintf(stderr, "%s:%s: data size too small for instruction %u of version %u\n",
+ argv0, current_input, rinstr->id, version);
+ return NULL;
+ }
+
+ instr->param_cnt++;
+ instr->params = realloc(instr->params, instr->param_cnt * sizeof(param_t));
+ param = &instr->params[instr->param_cnt - 1];
+ param->type = format[i];
+
+ if (data_offset + 4 > rinstr->data_size) {
+ fprintf(stderr, "%s:%s: format size larger than data size for instruction %u of version %u\n",
+ argv0, current_input, rinstr->id, version);
+ return NULL;
+ }
+
+ memcpy(¶m->value, rinstr->data + data_offset, 4);
+ data_offset += 4;
+
+ if (format[i] == 's' || format[i] == 'c') {
+ if (data_offset + param->value.s.length > rinstr->data_size) {
+ fprintf(stderr, "%s:%s: format size larger than data size for instruction %u of version %u\n",
+ argv0, current_input, rinstr->id, version);
+ return NULL;
+ }
+ param->value.s.data = malloc(param->value.s.length);
+ memcpy(param->value.s.data, rinstr->data + data_offset, param->value.s.length);
+ data_offset += param->value.s.length;
+
+ if (format[i] == 'c') {
+ size_t outsize;
+ util_sillyxor((unsigned char*)param->value.s.data, (unsigned char*)param->value.s.data, param->value.s.length, 119, 7, 16);
+ param->value.s.data = (char*)util_iconv("UTF-8", "SHIFT_JIS", (unsigned char*)param->value.s.data, param->value.s.length, &outsize);
+ }
+ } else if (format[i] == 'D') {
+ if (data_offset + 4 > rinstr->data_size) {
+ fprintf(stderr, "%s:%s: format size larger than data size for instruction %u of version %u\n",
+ argv0, current_input, rinstr->id, version);
+ return NULL;
+ }
+ memcpy(¶m->value.D[1], rinstr->data + data_offset, 4);
+ data_offset += 4;
+ }
+ }
+ }
+
+ if (data_offset != rinstr->data_size) {
+ fprintf(stderr, "%s:%s: data size differs from format size for instruction %u of version %u\n",
+ argv0, current_input, rinstr->id, version);
+ return NULL;
+ }
+
+ return instr;
+}
diff --git a/instr.h b/instr.h
new file mode 100644
index 00000000..a6f785f4
--- /dev/null
+++ b/instr.h
@@ -0,0 +1,37 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+#ifndef INSTR_H_
+#define INSTR_H_
+
+#include
+#include "ecl.h"
+
+instr_t* instr_parse(raw_instr_t* rinstr, instr_t* instr, unsigned int version);
+
+#endif
diff --git a/lexer.l b/lexer.l
new file mode 100644
index 00000000..b67e8c64
--- /dev/null
+++ b/lexer.l
@@ -0,0 +1,154 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+%option noinput nounput noyywrap
+ #include
+ #include
+ #include
+ #include
+ #include "parser.h"
+ #include "ecl.h"
+ #include "util.h"
+ void yyerror(const char*);
+ /* yylloc.first_column is initialized to 1. */
+ #define YY_USER_INIT yylloc.first_column = 0
+ /* XXX: The builtin YY_INPUT does something with the newlines that doesn't
+ * work well with Wine/Windows. */
+ /* TODO: Add some \r handling to the line counting stuff. */
+ #define YY_INPUT(buf,result,max_size) \
+ { \
+ int c = fgetc(yyin); \
+ if (c == EOF) { \
+ result = YY_NULL; \
+ } else { \
+ if (c == '\n') { \
+ yylloc.first_line++; \
+ yylloc.last_line = yylloc.first_line; \
+ yylloc.first_column = 0; \
+ } else { \
+ yylloc.first_column++; \
+ } \
+ yylloc.last_column = yylloc.first_column; \
+ buf[0] = c; \
+ result = 1; \
+ } \
+ }
+%x COMMENT CSTRING STRING
+%%
+":" return COLON;
+";" return SEMICOLON;
+"[" return SQUARE_OPEN;
+"]" return SQUARE_CLOSE;
+"{" return BRACE_OPEN;
+"}" return BRACE_CLOSE;
+"(float)" return CAST_FLOAT;
+"(int)" return CAST_INT;
+"anim" return ANIM;
+"ecli" return ECLI;
+"sub" return SUB;
+-?[0-9]+(\.[0-9]+)?f {
+ yylval.floating = strtof(yytext, NULL);
+ return FLOATING;
+}
+-?[0-9]+ {
+ yylval.integer = strtol(yytext, NULL, 10);
+ return INTEGER;
+}
+-?0x[0-9a-fA-F]+ {
+ yylval.integer = strtol(yytext, NULL, 16);
+ return INTEGER;
+}
+\+[ENHL]+ {
+ yylval.integer = 0xf0;
+ if (strchr(yytext, 'E')) yylval.integer |= RANK_EASY;
+ if (strchr(yytext, 'N')) yylval.integer |= RANK_NORMAL;
+ if (strchr(yytext, 'H')) yylval.integer |= RANK_HARD;
+ if (strchr(yytext, 'L')) yylval.integer |= RANK_LUNATIC;
+ return RANK;
+}
+[a-zA-Z][a-zA-Z0-9\._]* {
+ yylval.string = strdup(yytext);
+ return IDENTIFIER;
+}
+
+C\" {
+ BEGIN(CSTRING);
+ yylval.bytes.length = 0;
+ yylval.bytes.data = NULL;
+}
+[^\\\"\n]+ {
+ yylval.bytes.length += yyleng;
+ yylval.bytes.data = realloc(yylval.bytes.data, yylval.bytes.length);
+ memcpy(yylval.bytes.data + yylval.bytes.length - yyleng, yytext, yyleng);
+}
+\\. {
+ yylval.bytes.length++;
+ yylval.bytes.data = realloc(yylval.bytes.data, yylval.bytes.length);
+ yylval.bytes.data[yylval.bytes.length - 1] = yytext[1];
+}
+\n {
+ free(yylval.bytes.data);
+ yyerror("unterminated string");
+ BEGIN(INITIAL);
+ yyterminate();
+}
+\" {
+ size_t length = 0;
+ yylval.bytes.data = util_iconv("SHIFT_JIS", "UTF-8", (unsigned char*)yylval.bytes.data, yylval.bytes.length, &length);
+ yylval.bytes.length = length;
+ yylval.bytes.data = realloc(yylval.bytes.data, yylval.bytes.length + 4);
+ memset(yylval.bytes.data + yylval.bytes.length, 0, 4);
+ util_sillyxor(yylval.bytes.data, yylval.bytes.data, yylval.bytes.length + 4, 119, 7, 16);
+ BEGIN(INITIAL);
+ return CTEXT;
+}
+
+\" BEGIN(STRING); yylval.string = strdup("");
+[^\\\"\n]+ {
+ yylval.string = realloc(yylval.string, strlen(yylval.string) + yyleng + 1);
+ strcat(yylval.string, yytext);
+}
+\\. {
+ yylval.string = realloc(yylval.string, strlen(yylval.string) + 1 + 1);
+ strncat(yylval.string, yytext + 1, 1);
+}
+\n free(yylval.string); yyerror("unterminated string"); BEGIN(INITIAL); yyterminate();
+\" BEGIN(INITIAL); return TEXT;
+
+"/*" BEGIN(COMMENT);
+[^*]* |
+\*[^*/]* ;
+"*/" BEGIN(INITIAL);
+
+[\t\n ]+ ;
+. {
+ char buf[256];
+ snprintf(buf, 256, "illegal token: %s", yytext);
+ yyerror(buf);
+ return ILLEGAL_TOKEN;
+}
diff --git a/parser.y b/parser.y
new file mode 100644
index 00000000..90d50c4d
--- /dev/null
+++ b/parser.y
@@ -0,0 +1,693 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+%{
+#include
+#include
+#include
+#include
+#include
+#include
+#include "ecl.h"
+#include "program.h"
+#include "util.h"
+
+static int timer = 0;
+
+static void add_anim(char*);
+static void add_ecli(char*);
+static void add_eclh(char*);
+
+void yyerror(const char*);
+
+typedef struct list_t {
+ struct list_t* next;
+ param_t* param;
+} list_t;
+
+static void instr_add(int id, int rank_mask, list_t* list);
+static void label_create(char* label);
+static int32_t label_find(sub_t* sub, const char* label);
+
+int yylex();
+extern FILE* yyin;
+
+%}
+
+%error-verbose
+%locations
+
+%union {
+ int integer;
+ float floating;
+ char* string;
+ struct {
+ unsigned int length;
+ unsigned char* data;
+ } bytes;
+ struct list_t* list;
+}
+
+%token IDENTIFIER "identifier"
+%token TEXT "text"
+%token CTEXT "encrypted text"
+%token INTEGER "integer"
+%token FLOATING "float"
+%token RANK "rank"
+%token COLON ":"
+%token SEMICOLON ";"
+%token SQUARE_OPEN "["
+%token SQUARE_CLOSE "]"
+%token CAST_FLOAT "(float)"
+%token CAST_INT "(int)"
+%token ANIM "anim"
+%token ECLI "ecli"
+%token SUB "sub"
+%token BRACE_OPEN "{"
+%token BRACE_CLOSE "}"
+%token ILLEGAL_TOKEN "illegal token"
+%token END_OF_FILE 0 "end of file"
+
+%type params
+%type cast_params
+%type square_params
+%type param
+%type includes
+
+%%
+
+input:
+ | input statement
+ ;
+
+statement:
+ "sub" IDENTIFIER "{" { add_eclh($2); } instructions "}" { add_eclh(NULL); }
+ | "anim" "{" includes "}" {
+ list_t* list = $3;
+
+ while (list) {
+ list_t* temp = list;
+ add_anim(list->param->value.s.data);
+ list = list->next;
+ free(temp->param);
+ free(temp);
+ }
+ }
+ | "ecli" "{" includes "}" {
+ list_t* list = $3;
+
+ while (list) {
+ list_t* temp = list;
+ add_ecli(list->param->value.s.data);
+ list = list->next;
+ free(temp->param);
+ free(temp);
+ }
+ }
+ ;
+
+includes:
+ { $$ = NULL; }
+ | TEXT SEMICOLON includes {
+ $$ = malloc(sizeof(list_t));
+ $$->param = malloc(sizeof(param_t));
+ $$->param->type = 's';
+ $$->param->value.s.data = $1;
+ $$->next = $3;
+ }
+ ;
+
+instructions:
+ | instructions instruction
+ ;
+
+/* TODO: Check the given parameters against the parameters expected for the
+ * instruction. This requires passing a version parameter to eclc. */
+instruction:
+ IDENTIFIER ":" { label_create($1); }
+ | INTEGER ":" {
+ if ($1 == timer || (timer > 0 && $1 < timer)) {
+ char buf[256];
+ snprintf(buf, 256, "illegal timer change: %d to %d", timer, $1);
+ yyerror(buf);
+ }
+ timer = $1;
+ }
+ | INTEGER RANK params SEMICOLON { instr_add($1, $2, $3); }
+ | INTEGER params SEMICOLON { instr_add($1, 0xff, $2); }
+ ;
+
+params:
+ { $$ = NULL; }
+ | cast_params params { $$ = $1; $$->next = $2; }
+ ;
+
+cast_params:
+ "(int)" square_params {
+ int integer;
+ $$ = $2;
+ if ($$->param->type != 'i') {
+ yyerror("integer expected");
+ YYABORT;
+ }
+ $$->param->type = 'D';
+ integer = $$->param->value.i;
+ $$->param->value.D[0] = 0x6969;
+ $$->param->value.D[1] = integer;
+ }
+ | "(float)" square_params {
+ float floating;
+ int integer;
+ $$ = $2;
+ if ($$->param->type == 'f') {
+ floating = $$->param->value.f;
+ $$->param->value.D[0] = 0x6666;
+ memcpy(&$$->param->value.D[1], &floating, sizeof(float));
+ } else if ($$->param->type == 'i') {
+ integer = $$->param->value.i;
+ $$->param->value.D[0] = 0x6669;
+ $$->param->value.D[1] = integer;
+ } else {
+ yyerror("integer or float expected");
+ YYABORT;
+ }
+ $$->param->type = 'D';
+ }
+ | square_params
+ ;
+
+square_params:
+ "[" param "]" {
+ $$ = $2;
+ $$->param->stack = 1;
+ }
+ | param {
+ $$ = $1;
+ $$->param->stack = 0;
+ }
+ ;
+
+param:
+ CTEXT {
+ $$ = malloc(sizeof(list_t));
+ $$->param = malloc(sizeof(param_t));
+ $$->param->type = 'c';
+ $$->param->value.s.length = $1.length;
+ $$->param->value.s.data = (char*)$1.data;
+ }
+ | TEXT {
+ $$ = malloc(sizeof(list_t));
+ $$->param = malloc(sizeof(param_t));
+ $$->param->type = 's';
+ $$->param->value.s.length = strlen($1);
+ $$->param->value.s.data = $1;
+ }
+ | INTEGER {
+ $$ = malloc(sizeof(list_t));
+ $$->param = malloc(sizeof(param_t));
+ $$->param->type = 'i';
+ $$->param->value.i = $1;
+ }
+ | FLOATING {
+ $$ = malloc(sizeof(list_t));
+ $$->param = malloc(sizeof(param_t));
+ $$->param->type = 'f';
+ $$->param->value.f = $1;
+ }
+ | IDENTIFIER {
+ $$ = malloc(sizeof(list_t));
+ $$->param = malloc(sizeof(param_t));
+ $$->param->type = 'o';
+ $$->param->value.s.length = strlen($1);
+ $$->param->value.s.data = $1;
+ }
+ ;
+
+%%
+
+static uint32_t anim_cnt;
+static char** anim_list;
+static uint32_t ecli_cnt;
+static char** ecli_list;
+
+static unsigned int sub_cnt;
+static sub_t* subs;
+static sub_t* current_sub;
+
+static void
+add_eclh(char* arg)
+{
+ if (!arg) {
+ current_sub = NULL;
+ return;
+ }
+
+ sub_cnt++;
+ subs = realloc(subs, sub_cnt * sizeof(sub_t));
+ current_sub = &subs[sub_cnt - 1];
+
+ timer = 0;
+ current_sub->name = arg;
+ current_sub->instr_cnt = 0;
+ current_sub->instrs = NULL;
+ current_sub->offset = 0;
+ current_sub->label_cnt = 0;
+ current_sub->labels = NULL;
+}
+
+static void
+label_create(char* name)
+{
+ current_sub->label_cnt++;
+ current_sub->labels = realloc(current_sub->labels, sizeof(label_t) * current_sub->label_cnt);
+ current_sub->labels[current_sub->label_cnt - 1].name = name;
+ current_sub->labels[current_sub->label_cnt - 1].offset = current_sub->offset;
+}
+
+static int32_t
+label_find(sub_t* sub, const char* name)
+{
+ unsigned int i;
+ for (i = 0; i < sub->label_cnt; ++i) {
+ if (strcmp(sub->labels[i].name, name) == 0)
+ return sub->labels[i].offset;
+ }
+ return 0;
+}
+
+static void
+instr_create(instr_t* instr, uint32_t time, uint16_t id, uint16_t param_mask, uint8_t rank_mask, uint8_t param_cnt, param_t* params)
+{
+ unsigned int i;
+
+ instr->time = time;
+ instr->id = id;
+ instr->size = 16;
+ instr->param_mask = param_mask;
+ instr->rank_mask = rank_mask;
+ instr->param_cnt = param_cnt;
+ instr->params = params;
+ instr->offset = 0;
+
+ for (i = 0; i < param_cnt; ++i) {
+ unsigned int padded_length;
+ switch (params[i].type) {
+ case 'i':
+ case 'o':
+ case 'f':
+ instr->size += sizeof(int32_t);
+ break;
+ case 'D':
+ instr->size += sizeof(uint32_t) * 2;
+ break;
+ case 's':
+ case 'c':
+ padded_length = instr->params[i].value.s.length + (4 - instr->params[i].value.s.length % 4);
+ instr->size += sizeof(uint32_t);
+ instr->size += padded_length;
+ break;
+ }
+ }
+}
+
+static char*
+instr_serialize(sub_t* sub, instr_t* op)
+{
+ unsigned int i;
+ char* data = calloc(op->size, 1);
+ unsigned int offset = 0;
+
+ memcpy(data, op, 12);
+ offset += 16;
+
+ for (i = 0; i < op->param_cnt; ++i) {
+ unsigned int padded_length;
+ int32_t label;
+ switch (op->params[i].type) {
+ case 'i':
+ memcpy(data + offset, &op->params[i].value.i, sizeof(int32_t));
+ offset += sizeof(int32_t);
+ break;
+ case 'f':
+ memcpy(data + offset, &op->params[i].value.f, sizeof(float));
+ offset += sizeof(float);
+ break;
+ case 'D':
+ memcpy(data + offset, &op->params[i].value.D, sizeof(int32_t) * 2);
+ offset += sizeof(int32_t) * 2;
+ break;
+ case 'o':
+ label = label_find(sub, op->params[i].value.s.data) - op->offset;
+ memcpy(data + offset, &label, sizeof(int32_t));
+ offset += sizeof(int32_t);
+
+ free(op->params[i].value.s.data);
+
+ break;
+ case 's':
+ case 'c':
+ padded_length = op->params[i].value.s.length + (4 - op->params[i].value.s.length % 4);
+
+ memcpy(data + offset, &padded_length, sizeof(uint32_t));
+ offset += sizeof(uint32_t);
+
+ if (op->params[i].type == 'c') {
+ memcpy(data + offset, op->params[i].value.s.data, padded_length);
+ } else {
+ memcpy(data + offset, op->params[i].value.s.data, op->params[i].value.s.length);
+ }
+ offset += padded_length;
+
+ free(op->params[i].value.s.data);
+
+ break;
+ }
+ }
+
+ free(op->params);
+
+ return data;
+}
+
+static void
+instr_add(int id, int rank_mask, list_t* list)
+{
+ int param_mask = 0;
+ uint8_t param_cnt = 0;
+ param_t* params = NULL;
+ instr_t* op;
+
+ current_sub->instr_cnt++;
+ current_sub->instrs = realloc(current_sub->instrs, current_sub->instr_cnt * sizeof(instr_t));
+
+ op = ¤t_sub->instrs[current_sub->instr_cnt - 1];
+
+ while (list) {
+ list_t* temp;
+ param_cnt++;
+
+ params = realloc(params, param_cnt * sizeof(param_t));
+ params[param_cnt - 1] = *list->param;
+
+ if (list->param->stack)
+ param_mask |= 1 << (param_cnt - 1);
+
+ temp = list;
+ list = list->next;
+ free(temp->param);
+ free(temp);
+ }
+
+ instr_create(op, timer, id, param_mask, rank_mask, param_cnt, params);
+
+ op->offset = current_sub->offset;
+ current_sub->offset += op->size;
+}
+
+static void
+add_anim(char* arg)
+{
+ anim_cnt++;
+ anim_list = realloc(anim_list, sizeof(char*) * anim_cnt);
+ anim_list[anim_cnt - 1] = arg;
+}
+
+static void
+add_ecli(char* arg)
+{
+ ecli_cnt++;
+ ecli_list = realloc(ecli_list, sizeof(char*) * ecli_cnt);
+ ecli_list[ecli_cnt - 1] = arg;
+}
+
+void
+yyerror(const char* str)
+{
+ /* TODO: Research standard row and column range formats. */
+ if (yylloc.first_line == yylloc.last_line) {
+ if (yylloc.first_column == yylloc.last_column) {
+ fprintf(stderr,
+ "%s:%s:%d,%d: %s\n",
+ argv0, current_input, yylloc.first_line, yylloc.first_column, str);
+ } else {
+ fprintf(stderr,
+ "%s:%s:%d,%d-%d: %s\n",
+ argv0, current_input, yylloc.first_line, yylloc.first_column, yylloc.last_column, str);
+ }
+ } else {
+ fprintf(stderr,
+ "%s:%s:%d,%d-%d,%d: %s\n",
+ argv0, current_input, yylloc.first_line, yylloc.first_column, yylloc.last_line, yylloc.last_column, str);
+ }
+}
+
+static void
+print_usage()
+{
+ printf("Usage: %s [OPTION]... [FILE]\n"
+ "OPTION can be:\n"
+ " -o write output to the specified file\n"
+ " -h display this help and exit\n"
+ " -V display version information and exit\n\n"
+ "Additional documentation might be available at <" PACKAGE_URL ">.\n"
+ "Report bugs to <" PACKAGE_BUGREPORT ">.\n", argv0);
+}
+
+int
+main(int argc, char* argv[])
+{
+ long pos;
+ int ret;
+ unsigned int i;
+ int j;
+ FILE* out;
+ const uint32_t zero = 0;
+ header_scpt_t header;
+ header_eclh_t eclh;
+
+ yyin = stdin;
+ argv0 = "eclc";
+ current_input = "(stdin)";
+ out = stdout;
+
+ for (j = 1; j < argc; ++j) {
+ if (strcmp(argv[j], "-h") == 0) {
+ print_usage();
+ return 0;
+ } else if (strcmp(argv[j], "-V") == 0) {
+ util_print_version("eclc", PACKAGE_THECL_VERSION);
+ return 0;
+ } else if (strcmp(argv[j], "-o") == 0) {
+ ++j;
+ if (j == argc) {
+ print_usage();
+ return 1;
+ }
+ out = fopen(argv[j], "wb");
+ if (!out) {
+ fprintf(stderr, "%s: couldn't open %s for writing: %s\n", argv0, argv[j], strerror(errno));
+ return 1;
+ }
+ } else {
+ break;
+ }
+ }
+
+ if (j != argc) {
+ yyin = fopen(argv[j], "r");
+ current_input = argv[j];
+ if (!yyin) {
+ fprintf(stderr, "%s: couldn't open %s for reading: %s\n", argv0, argv[j], strerror(errno));
+ return 1;
+ }
+ }
+
+ ret = yyparse();
+
+ if (ret != 0)
+ return ret;
+
+ /* TODO: Use a memory buffer instead of writing many small chunks. */
+
+ header.unknown1 = 1;
+ header.include_length = 0;
+ header.include_offset = 0;
+ header.zero1 = 0;
+ header.sub_cnt = sub_cnt;
+ header.zero2[0] = 0;
+ header.zero2[1] = 0;
+ header.zero2[2] = 0;
+ header.zero2[3] = 0;
+
+ if (fputs("SCPT", out) == EOF) {
+ fprintf(stderr, "%s: couldn't write: %s\n", argv0, strerror(errno));
+ return 1;
+ }
+ if (fwrite(&header, sizeof(header_scpt_t), 1, out) != 1) {
+ fprintf(stderr, "%s: couldn't write: %s\n", argv0, strerror(errno));
+ return 1;
+ }
+
+ header.include_offset = util_tell(out);
+ if (header.include_offset == (uint32_t)-1) {
+ return 1;
+ }
+ if (fputs("ANIM", out) == EOF) {
+ fprintf(stderr, "%s: couldn't write: %s\n", argv0, strerror(errno));
+ return 1;
+ }
+ if (fwrite(&anim_cnt, sizeof(uint32_t), 1, out) != 1) {
+ fprintf(stderr, "%s: couldn't write: %s\n", argv0, strerror(errno));
+ return 1;
+ }
+ for (i = 0; i < anim_cnt; ++i) {
+ if (fwrite(anim_list[i], strlen(anim_list[i]) + 1, 1, out) != 1) {
+ fprintf(stderr, "%s: couldn't write: %s\n", argv0, strerror(errno));
+ return 1;
+ }
+ free(anim_list[i]);
+ }
+ free(anim_list);
+
+ pos = util_tell(out);
+ if (pos == -1)
+ return 1;
+ if (pos % 4 != 0) {
+ if (fwrite(&zero, 4 - pos % 4, 1, out) != 1) {
+ fprintf(stderr, "%s: couldn't write: %s\n", argv0, strerror(errno));
+ return 1;
+ }
+ }
+
+ if (fputs("ECLI", out) == EOF) {
+ fprintf(stderr, "%s: couldn't write: %s\n", argv0, strerror(errno));
+ return 1;
+ }
+ if (fwrite(&ecli_cnt, sizeof(uint32_t), 1, out) != 1) {
+ fprintf(stderr, "%s: couldn't write: %s\n", argv0, strerror(errno));
+ return 1;
+ }
+ for (i = 0; i < ecli_cnt; ++i) {
+ if (fwrite(ecli_list[i], strlen(ecli_list[i]) + 1, 1, out) != 1) {
+ fprintf(stderr, "%s: couldn't write: %s\n", argv0, strerror(errno));
+ return 1;
+ }
+ free(ecli_list[i]);
+ }
+ free(ecli_list);
+
+ pos = util_tell(out);
+ if (pos == -1)
+ return 1;
+ if (pos % 4 != 0) {
+ if (fwrite(&zero, 4 - pos % 4, 1, out) != 1) {
+ fprintf(stderr, "%s: couldn't write: %s\n", argv0, strerror(errno));
+ return 1;
+ }
+ }
+ pos = util_tell(out);
+ if (pos == -1)
+ return 1;
+ header.include_length = pos - header.include_offset;
+
+ for (i = 0; i < sub_cnt; ++i) {
+ if (fwrite(&zero, sizeof(uint32_t), 1, out) != 1) {
+ fprintf(stderr, "%s: couldn't write: %s\n", argv0, strerror(errno));
+ return 1;
+ }
+ }
+
+ for (i = 0; i < sub_cnt; ++i) {
+ if (fwrite(subs[i].name, strlen(subs[i].name) + 1, 1, out) != 1) {
+ fprintf(stderr, "%s: couldn't write: %s\n", argv0, strerror(errno));
+ return 1;
+ }
+ free(subs[i].name);
+ }
+
+ pos = util_tell(out);
+ if (pos == -1)
+ return 1;
+ if (pos % 4 != 0) {
+ if (fwrite(&zero, 4 - pos % 4, 1, out) != 1) {
+ fprintf(stderr, "%s: couldn't write: %s\n", argv0, strerror(errno));
+ return 1;
+ }
+ }
+
+ eclh.unknown1 = 0x10;
+ eclh.zero[0] = 0;
+ eclh.zero[1] = 0;
+
+ for (i = 0; i < sub_cnt; ++i) {
+ unsigned int j;
+ subs[i].offset = util_tell(out);
+ if (subs[i].offset == (uint32_t)-1)
+ return 1;
+ if (fputs("ECLH", out) == EOF) {
+ fprintf(stderr, "%s: couldn't write: %s\n", argv0, strerror(errno));
+ return 1;
+ }
+ if (fwrite(&eclh, sizeof(header_eclh_t), 1, out) != 1) {
+ fprintf(stderr, "%s: couldn't write: %s\n", argv0, strerror(errno));
+ return 1;
+ }
+ for (j = 0; j < subs[i].instr_cnt; ++j) {
+ char* data = instr_serialize(&subs[i], &subs[i].instrs[j]);
+ if (fwrite(data, subs[i].instrs[j].size, 1, out) != 1) {
+ fprintf(stderr, "%s: couldn't write: %s\n", argv0, strerror(errno));
+ return 1;
+ }
+ free(data);
+ }
+ for (j = 0; j < subs[i].label_cnt; ++j) {
+ free(subs[i].labels[j].name);
+ }
+ free(subs[i].labels);
+ free(subs[i].instrs);
+ }
+
+ util_seek(out, 4, NULL);
+ if (fwrite(&header, sizeof(header_scpt_t), 1, out) != 1) {
+ fprintf(stderr, "%s: couldn't write: %s\n", argv0, strerror(errno));
+ return 1;
+ }
+
+ util_seek(out, header.include_offset + header.include_length, NULL);
+ for (i = 0; i < sub_cnt; ++i) {
+ if (fwrite(&subs[i].offset, sizeof(uint32_t), 1, out) != 1) {
+ fprintf(stderr, "%s: couldn't write: %s\n", argv0, strerror(errno));
+ return 1;
+ }
+ }
+ free(subs);
+
+ fclose(yyin);
+ fclose(out);
+ return 0;
+}
diff --git a/program.c b/program.c
new file mode 100644
index 00000000..fdcafad2
--- /dev/null
+++ b/program.c
@@ -0,0 +1,6 @@
+#include
+#include "program.h"
+
+const char* argv0 = NULL;
+const char* current_input = NULL;
+const char* current_output = NULL;
diff --git a/program.h b/program.h
new file mode 100644
index 00000000..1cfd8f07
--- /dev/null
+++ b/program.h
@@ -0,0 +1,36 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+#ifndef PROGRAM_H_
+#define PROGRAM_H_
+
+extern const char* argv0;
+extern const char* current_input;
+extern const char* current_output;
+
+#endif
diff --git a/thanm.c b/thanm.c
new file mode 100644
index 00000000..c7411add
--- /dev/null
+++ b/thanm.c
@@ -0,0 +1,1151 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+#include
+#include
+#include
+#include
+#include
+#include
+#ifdef HAVE_LIBPNG
+#include
+#endif
+#include "thanm.h"
+#include "program.h"
+#include "util.h"
+
+static unsigned int option_force;
+static unsigned int option_verbose;
+static unsigned int option_filemap;
+filemap_t filemap;
+
+static unsigned int
+format_Bpp(format_t format)
+{
+ switch (format) {
+ case FORMAT_BGRA8888:
+ return 4;
+ case FORMAT_BGRA4444:
+ case FORMAT_BGR565:
+ return 2;
+ case FORMAT_GRAY8:
+ return 1;
+ default:
+ fprintf(stderr, "%s: unknown format: %u\n", argv0, format);
+ if (!option_force) abort();
+ return 1;
+ }
+}
+
+static char*
+rgba_to_fmt(const uint32_t* data, unsigned int pixels, format_t format)
+{
+ unsigned int i;
+ char* out = NULL;
+
+ if (format == FORMAT_GRAY8) {
+ out = malloc(pixels);
+ for (i = 0; i < pixels; ++i) {
+ out[i] = data[i] & 0xff;
+ }
+ } else if (format == FORMAT_BGRA8888) {
+ const char* data8 = (const char*)data;
+ out = malloc(sizeof(uint32_t) * pixels);
+ memcpy(out, data, pixels * sizeof(uint32_t));
+ for (i = 0; i < pixels; ++i) {
+ out[i * sizeof(uint32_t) + 0] = data8[i * sizeof(uint32_t) + 2];
+ out[i * sizeof(uint32_t) + 2] = data8[i * sizeof(uint32_t) + 0];
+ }
+ } else if (format == FORMAT_BGRA4444) {
+ out = malloc(sizeof(uint16_t) * pixels);
+ for (i = 0; i < pixels; ++i) {
+ out[i * sizeof(uint16_t) + 0] = ((data[i] & 0xf00000) >> 20)
+ | ((data[i] & 0xf000) >> 8);
+ out[i * sizeof(uint16_t) + 1] = ((data[i] & 0xf0) >> 4)
+ | ((data[i] & 0xf0000000) >> 24);
+ }
+ } else if (format == FORMAT_BGR565) {
+ uint16_t* out16;
+ out = malloc(sizeof(uint16_t) * pixels);
+ out16 = (uint16_t*)out;
+ for (i = 0; i < pixels; ++i) {
+ out16[i] = ((data[i] & 0xf8) << 8) /* 00000000 00000000 11111000 -> 00000000 00011111 */
+ | ((data[i] & 0xfc00) >> 5) /* 00000000 11111100 00000000 -> 00000111 11100000 */
+ | ((data[i] & 0xf80000) >>19); /* 11111000 00000000 00000000 -> 11111000 00000000 */
+ }
+ } else {
+ fprintf(stderr, "%s: unknown format: %u\n", argv0, format);
+ abort();
+ }
+
+ return out;
+}
+
+static char*
+fmt_to_rgba(const char* data, unsigned int pixels, format_t format)
+{
+ unsigned int i;
+ uint32_t* out = malloc(sizeof(uint32_t) * pixels);
+
+ if (format == FORMAT_GRAY8) {
+ for (i = 0; i < pixels; ++i) {
+ out[i] = 0xff000000
+ | (data[i] << 16 & 0xff0000)
+ | (data[i] << 8 & 0xff00)
+ | (data[i] << 0 & 0xff);
+ }
+ } else if (format == FORMAT_BGRA8888) {
+ char* out8 = (char*)out;
+ memcpy(out, data, pixels * sizeof(uint32_t));
+ for (i = 0; i < pixels; ++i) {
+ out8[i * sizeof(uint32_t) + 0] = data[i * sizeof(uint32_t) + 2];
+ out8[i * sizeof(uint32_t) + 2] = data[i * sizeof(uint32_t) + 0];
+ }
+ } else if (format == FORMAT_BGRA4444) {
+ for (i = 0; i < pixels; ++i) {
+ /* Extends like this: 0x0 -> 0x00, 0x3 -> 0x33, 0xf -> 0xff.
+ * It's required for proper alpha. */
+ out[i] = ((data[i * sizeof(uint16_t) + 1] & 0xf0) << 24 & 0xf0000000)
+ | ((data[i * sizeof(uint16_t) + 1] & 0xf0) << 20 & 0x0f000000)
+ | ((data[i * sizeof(uint16_t) + 0] & 0x0f) << 20 & 0xf00000)
+ | ((data[i * sizeof(uint16_t) + 0] & 0x0f) << 16 & 0x0f0000)
+ | ((data[i * sizeof(uint16_t) + 0] & 0xf0) << 8 & 0xf000)
+ | ((data[i * sizeof(uint16_t) + 0] & 0xf0) << 4 & 0x0f00)
+ | ((data[i * sizeof(uint16_t) + 1] & 0x0f) << 4 & 0xf0)
+ | ((data[i * sizeof(uint16_t) + 1] & 0x0f) << 0 & 0x0f);
+ }
+ } else if (format == FORMAT_BGR565) {
+ uint16_t* u16 = (uint16_t*)data;
+ for (i = 0; i < pixels; ++i) {
+ /* Bit-extends channels: 00001b -> 00001111b. */
+ out[i] = 0xff000000
+ | ((u16[i] & 0x001f) << 19 & 0xf80000)
+ | ((u16[i] & 0x0001) << 16 & 0x040000)
+ | ((u16[i] & 0x0001) << 16 & 0x020000)
+ | ((u16[i] & 0x0001) << 16 & 0x010000)
+
+ | ((u16[i] & 0x07e0) << 5 & 0x00fc00)
+ | ((u16[i] & 0x0020) << 4 & 0x000200)
+ | ((u16[i] & 0x0020) << 3 & 0x000100)
+
+ | ((u16[i] & 0xf800) >> 8 & 0x0000f8)
+ | ((u16[i] & 0x0800) >> 9 & 0x000004)
+ | ((u16[i] & 0x0800) >> 10 & 0x000002)
+ | ((u16[i] & 0x0800) >> 11 & 0x000001);
+ }
+ } else {
+ fprintf(stderr, "%s: unknown format: %u\n", argv0, format);
+ abort();
+ }
+
+ return (char*)out;
+}
+
+static const opcode_fmt_t formats_v2[] = {
+ { 0, "" },
+ { 1, "" },
+ { 2, "" },
+ { 3, "i" },
+ { 4, "ii" },
+ { 5, "iii" },
+ { 6, "fff" },
+ { 7, "ff" },
+ { 8, "i" },
+ { 9, "i" },
+ { 10, "" },
+ { 12, "fff" },
+ { 13, "fff" },
+ { 14, "ff" },
+ { 15, "ii" },
+ { 16, "i" },
+ { 17, "ffii" },
+ { 18, "ffii" },
+ { 19, "fffi" },
+ { 20, "" },
+ { 21, "i" },
+ { 22, "" },
+ { 23, "" },
+ { 24, "i" },
+ { 25, "i" },
+ { 26, "f" },
+ { 27, "f" },
+ { 28, "i" },
+ { 29, "ffi" },
+ { 30, "i" },
+ { 31, "i" },
+ { 32, "iiffi" },
+ { 33, "iii" },
+ { 34, "iii" },
+ { 35, "iiiif" },
+ { 36, "iiff" },
+ { 37, "ii" },
+ { 38, "ff" },
+ { 42, "ff" },
+ { 50, "fff" },
+ { 52, "fff" },
+ { 55, "iii" },
+ { 59, "ii" },
+ { 60, "ff" },
+ { 69, "iiii" },
+ { 79, "i" },
+ { 80, "i" }
+};
+
+static const opcode_fmt_t formats_v3[] = {
+ { 0, "" },
+ { 1, "" },
+ { 2, "" },
+ { 3, "i" },
+ { 4, "ii" },
+ { 5, "iii" },
+ { 6, "fff" },
+ { 7, "ff" },
+ { 8, "i" },
+ { 9, "iii" },
+ { 10, "" },
+ { 12, "fff" },
+ { 13, "fff" },
+ { 14, "ff" },
+ { 15, "ii" },
+ { 16, "i" },
+ { 17, "ffii" },
+ { 18, "ffii" },
+ { 20, "" },
+ { 21, "i" },
+ { 22, "" },
+ { 23, "" },
+ { 24, "i" },
+ { 25, "i" },
+ { 26, "f" },
+ { 27, "f" },
+ { 28, "i" },
+ { 30, "i" },
+ { 31, "i" },
+ { 32, "iifff" },
+ { 33, "iiiii" },
+ { 34, "iii" },
+ { 35, "iiiif" },
+ { 36, "iiff" },
+ { 37, "ii" },
+ { 38, "ff" },
+ { 40, "ff" },
+ { 42, "ff" },
+ { 44, "ff" },
+ { 49, "iii" },
+ { 50, "fff" },
+ { 52, "fff" },
+ { 54, "fff" },
+ { 55, "iii" },
+ { 56, "fff" },
+ { 59, "ii" },
+ { 60, "ff" },
+ { 69, "iiii" },
+ { 79, "i" },
+ { 80, "f" },
+ { 81, "f" },
+ { 82, "i" },
+ { 83, "i" },
+ { 85, "i" },
+ { 86, "iiiii" },
+ { 87, "iii" },
+ { 89, "" }
+};
+
+static const opcode_fmt_t formats_v4p[] = {
+ { 0, "" },
+ { 1, "" },
+ { 2, "" },
+ { 3, "i" },
+ { 4, "ii" },
+ { 5, "iii" },
+ { 6, "ii" },
+ { 7, "ff" },
+ { 8, "ii" },
+ { 9, "ff" },
+ { 11, "ff" },
+ { 13, "ff" },
+ { 18, "iii" },
+ { 19, "fff" },
+ { 21, "fff" },
+ { 22, "iii" },
+ { 23, "fff" },
+ { 24, "iii" },
+ { 25, "fff" },
+ { 26, "iii" },
+ { 27, "fff" },
+ { 30, "iiii" },
+ { 40, "ii" },
+ { 42, "ff" },
+ { 43, "ff" },
+ { 48, "fff" },
+ { 49, "fff" },
+ { 50, "ff" },
+ { 51, "i" },
+ { 52, "iii" },
+ { 53, "fff" },
+ { 56, "iifff" },
+ { 57, "iiiii" },
+ { 58, "iii" },
+ { 59, "iifif" },
+ { 60, "iiff" },
+ { 61, "" },
+ { 63, "" },
+ { 64, "i" },
+ { 65, "i" },
+ { 66, "i" },
+ { 67, "i" },
+ { 68, "i" },
+ { 69, "" },
+ { 70, "f" },
+ { 71, "f" },
+ { 73, "i" },
+ { 74, "i" },
+ { 75, "i" },
+ { 76, "iii" },
+ { 77, "i" },
+ { 78, "iiiii" },
+ { 79, "iii" },
+ { 81, "" },
+ { 82, "i" },
+ { 83, "" },
+ { 84, "i" },
+ { 85, "i" },
+ { 86, "i" },
+ { 87, "i" },
+ { 88, "i" },
+ { 89, "i" },
+ { 90, "i" },
+ { 91, "i" },
+ { 92, "i" },
+ { 93, "iif" },
+ { 94, "iif" },
+ { 95, "i" },
+ { 96, "iff" },
+ { 100, "ifffffiffi" },
+ { 101, "i" },
+ { 102, "ii" },
+ { 103, "ff" },
+ { 104, "fi" },
+ { 105, "fi" },
+ { 106, "fi" },
+ { 107, "iiff" },
+ { 108, "ff" },
+ { 110, "ff" }
+};
+
+/* The order and sizes of fields changed for TH11. */
+static void
+convert_header(anm_header_t* header)
+{
+ anm_header11_t th11 = *(anm_header11_t*)header;
+ header->sprites = th11.sprites;
+ header->animations = th11.animations;
+ header->zero1 = th11.zero1;
+ header->w = th11.w;
+ header->h = th11.h;
+ header->format = th11.format;
+ header->zero2 = 0;
+ header->nameoffset = th11.nameoffset;
+ header->x = th11.x;
+ header->y = th11.y;
+ header->version = th11.version;
+ header->unknown1 = th11.unknown1;
+ header->thtxoffset = th11.thtxoffset;
+ header->unknown2 = th11.unknown2;
+ header->nextoffset = th11.nextoffset;
+ header->zero3 = 0;
+}
+
+static anm_t*
+anm_read_file(const char* filename)
+{
+ anm_t* anm;
+ uint32_t offset = 0;
+ long filesize;
+ FILE* f = fopen(filename, "rb");
+ if (!f) {
+ fprintf(stderr, "%s: couldn't open %s: %s\n", argv0, filename, strerror(errno));
+ exit(1);
+ }
+
+ filesize = util_fsize(f);
+
+ anm = malloc(sizeof(anm_t));
+ anm->name_count = 0;
+ anm->names = NULL;
+ anm->entry_count = 0;
+ anm->entries = NULL;
+
+ if (option_filemap) {
+ filemap.data = malloc(filesize);
+ memset(filemap.data, ' ', filesize);
+ filemap.pos = 0;
+ }
+
+ for (;;) {
+ entry_t* entry;
+ char name[256];
+ unsigned int i;
+
+ ++anm->entry_count;
+ anm->entries = realloc(anm->entries, sizeof(entry_t) * anm->entry_count);
+
+ entry = &anm->entries[anm->entry_count - 1];
+ memset(&entry->thtx, 0, sizeof(thtx_header_t));
+ entry->name = NULL;
+ entry->sprite_count = 0;
+ entry->sprites = NULL;
+ entry->animation_count = 0;
+ entry->animations = NULL;
+ entry->data_size = 0;
+ entry->data = NULL;
+
+ util_seek(f, offset, NULL);
+ util_read(f, &entry->header, sizeof(anm_header_t), 'H', NULL);
+
+ /* XXX: This is not a particularly good way of detecting this. */
+ if (entry->header.zero1 != 0)
+ convert_header(&entry->header);
+
+ if (entry->header.version != 0 &&
+ entry->header.version != 2 &&
+ entry->header.version != 3 &&
+ entry->header.version != 4 &&
+ entry->header.version != 7) {
+ fprintf(stderr, "%s:%s:%u: unknown version: %u\n",
+ argv0, current_input, anm->entry_count, entry->header.version);
+ if (!option_force) abort();
+ }
+ if (entry->header.format != FORMAT_BGRA8888 &&
+ entry->header.format != FORMAT_BGR565 &&
+ entry->header.format != FORMAT_BGRA4444 &&
+ entry->header.format != FORMAT_GRAY8) {
+ if (!(entry->header.format == 0 && entry->header.thtxoffset == 0)) {
+ fprintf(stderr, "%s:%s:%u: unknown format: %u\n",
+ argv0, current_input, anm->entry_count, entry->header.format);
+ if (!option_force) abort();
+ }
+ }
+ if (entry->header.unknown1 != 0 &&
+ entry->header.unknown1 != 1 &&
+ entry->header.unknown1 != 10 &&
+ entry->header.unknown1 != 11) {
+ fprintf(stderr, "%s:%s:%u: unknown value for unknown1: %u\n",
+ argv0, current_input, anm->entry_count, entry->header.unknown1);
+ if (!option_force) abort();
+ }
+ if (entry->header.unknown2 == 0 &&
+ entry->header.thtxoffset != 0) {
+ fprintf(stderr, "%s:%s:%u: unknown2 and thtxoffset do not match: %u, %u\n",
+ argv0, current_input, anm->entry_count, entry->header.unknown2, entry->header.thtxoffset);
+ if (!option_force) abort();
+ }
+ if (entry->header.unknown2 != 0 &&
+ entry->header.unknown2 != 1) {
+ fprintf(stderr, "%s:%s:%u: unknown value for unknown2: %u\n",
+ argv0, current_input, anm->entry_count, entry->header.unknown2);
+ if (!option_force) abort();
+ }
+ if (entry->header.zero1 != 0) {
+ fprintf(stderr, "%s:%s:%u: unknown value for zero1: %u\n",
+ argv0, current_input, anm->entry_count, entry->header.zero1);
+ if (!option_force) abort();
+ }
+ if (entry->header.zero3 != 0) {
+ fprintf(stderr, "%s:%s:%u: unknown value for zero3: %u\n",
+ argv0, current_input, anm->entry_count, entry->header.zero3);
+ if (!option_force) abort();
+ }
+
+ /* Lengths, including padding, observed are: 16, 32, 48. */
+ entry->name = NULL;
+ util_seek(f, offset + entry->header.nameoffset, NULL);
+ fgets(name, 256, f);
+ if (option_filemap) {
+ memset(filemap.data + filemap.pos, 'N', strlen(name) + 1);
+ filemap.pos += strlen(name) + 1;
+ }
+
+ for (i = 0; i < anm->name_count; ++i) {
+ if (strcmp(name, anm->names[i]) == 0) {
+ entry->name = anm->names[i];
+ }
+ }
+
+ if (!entry->name) {
+ ++anm->name_count;
+ anm->names = realloc(anm->names, sizeof(char*) * anm->name_count);
+ anm->names[anm->name_count - 1] = strdup(name);
+ entry->name = anm->names[anm->name_count - 1];
+ }
+
+ util_seek(f, offset + sizeof(anm_header_t), NULL);
+
+ /* Parse any sprites in the entry. */
+ if (entry->header.sprites) {
+ uint32_t* offsets = NULL;
+ int sequential = 1;
+
+ offsets = malloc(sizeof(uint32_t) * entry->header.sprites);
+ entry->sprites = malloc(sizeof(sprite_t) * entry->header.sprites);
+ entry->sprite_count = entry->header.sprites;
+ util_read(f, offsets, sizeof(uint32_t) * entry->header.sprites, 'o', NULL);
+
+ /* Check that the sprites are stored packed. */
+ for (i = 1; i < entry->header.sprites; ++i) {
+ if (offsets[i] - offsets[i - 1] != sizeof(sprite_t)) {
+ /* This should never happen. */
+ sequential = 0;
+ break;
+ }
+ }
+
+ if (sequential) {
+ util_seek(f, offset + offsets[0], NULL);
+ util_read(f, entry->sprites, sizeof(sprite_t) * entry->header.sprites, 'R', NULL);
+ } else {
+ for (i = 0; i < entry->header.sprites; ++i) {
+ util_seek(f, offset + offsets[i], NULL);
+ util_read(f, &entry->sprites[i], sizeof(sprite_t), 'R', NULL);
+ }
+ }
+
+ free(offsets);
+ }
+
+ /* Read in all the animation scripts. */
+ /* XXX: TH06 is not yet supported. */
+ if (entry->header.animations && entry->header.version != 0) {
+ animation_offset_t* offsets;
+
+ util_seek(f, offset + sizeof(anm_header_t) + sizeof(uint32_t) * entry->header.sprites, NULL);
+
+ offsets = malloc(sizeof(animation_offset_t) * entry->header.animations);
+ util_read(f, offsets, sizeof(animation_offset_t) * entry->header.animations, 'O', NULL);
+
+ entry->animations = NULL;
+ entry->animation_count = 0;
+
+ for (i = 0; i < entry->header.animations; ++i) {
+ long limit = 0;
+
+ util_seek(f, offset + offsets[i].offset, NULL);
+
+ if (i < entry->header.animations - 1)
+ limit = offset + offsets[i + 1].offset;
+ else if (entry->header.thtxoffset)
+ limit = offset + entry->header.thtxoffset;
+ else
+ limit = filesize;
+
+ for (;;) {
+ animation_t anim;
+
+ if (ftell(f) + 8 > limit) {
+ fprintf(stderr, "%s:%s:%s:%d: would read past limit\n",
+ argv0, current_input, entry->name, offsets[i].id);
+ break;
+ }
+
+ util_read(f, &anim, 8, 'a', NULL);
+
+ /* End of this animation. */
+ if (anim.type == 0xffff)
+ break;
+
+ /* This shouldn't happen. */
+ if (anim.length == 0) {
+ fprintf(stderr, "%s:%s:%s:%d: animation length is zero: %u %u %u\n",
+ argv0, current_input, entry->name, offsets[i].id, anim.type, anim.length, anim.time);
+ if (!option_force) abort();
+ break;
+ }
+
+ if (anim.length % 4 != 0) {
+ fprintf(stderr, "%s:%s:%s:%d: length is not a multiple of four: %u\n",
+ argv0, current_input, entry->name, offsets[i].id, anim.length);
+ if (!option_force) abort();
+ break;
+ }
+
+ ++entry->animation_count;
+ entry->animations = realloc(entry->animations, sizeof(animation_t) * entry->animation_count);
+
+ entry->animations[entry->animation_count - 1].time = anim.time;
+ entry->animations[entry->animation_count - 1].type = anim.type;
+ entry->animations[entry->animation_count - 1].length = anim.length;
+ entry->animations[entry->animation_count - 1].id = offsets[i].id;
+ if (anim.length > 8) {
+ entry->animations[entry->animation_count - 1].data = malloc(anim.length - 8);
+ util_read(f, entry->animations[entry->animation_count - 1].data, anim.length - 8, 'A', NULL);
+ } else {
+ entry->animations[entry->animation_count - 1].data = NULL;
+ }
+ }
+ }
+
+ free(offsets);
+ }
+
+ /* TH06 doesn't store entry data. */
+ if (entry->header.thtxoffset) {
+ char* data = NULL;
+ char magic[5] = { 0 };
+
+ util_seek(f, offset + entry->header.thtxoffset, NULL);
+
+ util_read(f, magic, 4, 't', NULL);
+ if (strcmp(magic, "THTX") != 0) {
+ fprintf(stderr, "%s:%s:%s: unknown thtx magic: %s\n",
+ argv0, current_input, entry->name, magic);
+ if (!option_force) abort();
+ }
+
+ util_read(f, &entry->thtx, sizeof(thtx_header_t), 'T', NULL);
+ if (entry->thtx.zero != 0) {
+ fprintf(stderr, "%s:%s:%s: unknown value for zero: %u\n",
+ argv0, current_input, entry->name, entry->thtx.zero);
+ if (!option_force) abort();
+ }
+ if (entry->header.format != entry->thtx.format) {
+ fprintf(stderr, "%s:%s:%s: header format does not match thtx header format: %u != %u\n",
+ argv0, current_input, entry->name,
+ entry->header.format, entry->thtx.format);
+ if (!option_force) abort();
+ }
+
+ if (entry->thtx.w * entry->thtx.h * format_Bpp(entry->thtx.format) > entry->thtx.size) {
+ fprintf(stderr, "%s:%s:%s: w*h*Bpp is greater than data size: %u*%u*%u > %u\n",
+ argv0, current_input, entry->name,
+ entry->thtx.w, entry->thtx.h, format_Bpp(entry->thtx.format), entry->thtx.size);
+ if (!option_force) abort();
+ }
+
+ /* XXX: There are three known entries which don't match.
+ * What they have in common is that their sizes are odd,
+ * and that the extra data makes up two rows.
+ *
+ * 09/pl02.anm : data/pl/pl02/eff09sk1.png
+ * 09/pl02b.anm : data/pl/pl02b/eff09sk1.png
+ * 95/enm11.anm : data/cdbg/cdbg11.png
+ *
+ * The extra lines are neither written out nor read in. */
+ /* if (entry->thtx.w * entry->thtx.h * format_Bpp(entry->thtx.format) != entry->thtx.size)
+ fprintf(stderr, "%s: %u != %u\n", entry->name, entry->thtx.w * entry->thtx.h * format_Bpp(entry->thtx.format), entry->thtx.size); */
+
+ data = malloc(entry->thtx.size);
+ util_read(f, data, entry->thtx.w * entry->thtx.h * format_Bpp(entry->thtx.format), 'D', NULL);
+
+ entry->data_size = entry->thtx.w * entry->thtx.h * 4;
+ entry->data = fmt_to_rgba(data, entry->thtx.w * entry->thtx.h, entry->thtx.format);
+
+ free(data);
+ }
+
+ if (!entry->header.nextoffset)
+ break;
+
+ offset += entry->header.nextoffset;
+ }
+
+ fclose(f);
+
+ if (option_filemap) {
+ f = fopen("filemap.dat", "wb");
+ if (!f) {
+ fprintf(stderr, "%s: couldn't open filemap.dat for writing: %s\n", argv0, strerror(errno));
+ } else {
+ if (fwrite(filemap.data, filesize, 1, f) != 1)
+ fprintf(stderr, "%s: couldn't write to filemap.dat: %s\n", argv0, strerror(errno));
+ free(filemap.data);
+ fclose(f);
+ }
+ }
+
+ qsort(anm->names, anm->name_count, sizeof(char*), util_strpcmp);
+
+ return anm;
+}
+
+static void
+anm_list(const anm_t* anm)
+{
+ unsigned int i;
+
+ if (!option_verbose) {
+ for (i = 0; i < anm->name_count; ++i)
+ puts(anm->names[i]);
+ } else {
+ for (i = 0; i < anm->entry_count; ++i) {
+ unsigned int j;
+ printf("%s %ux%u+%u+%u %ux%u %u %u %u %u\n",
+ anm->entries[i].name,
+ anm->entries[i].header.w, anm->entries[i].header.h, anm->entries[i].header.x, anm->entries[i].header.y,
+ anm->entries[i].thtx.w, anm->entries[i].thtx.h,
+ anm->entries[i].header.version,
+ anm->entries[i].header.format,
+ anm->entries[i].header.unknown1,
+ anm->entries[i].header.unknown2);
+
+ if (option_verbose > 1) {
+ for (j = 0; j < anm->entries[i].sprite_count; ++j) {
+ printf(" Sprite #%u: %.fx%.f+%.f+%.f\n",
+ anm->entries[i].sprites[j].id,
+ anm->entries[i].sprites[j].w,
+ anm->entries[i].sprites[j].h,
+ anm->entries[i].sprites[j].x,
+ anm->entries[i].sprites[j].y);
+ }
+ }
+
+ if (option_verbose > 2 && anm->entries[i].animation_count) {
+ const opcode_fmt_t* formats = NULL;
+ unsigned int format_count = 0;
+ if (anm->entries[i].header.version == 2) {
+ formats = formats_v2;
+ format_count = sizeof(formats_v2) / sizeof(formats_v2[0]);
+ } else if (anm->entries[i].header.version == 3) {
+ formats = formats_v3;
+ format_count = sizeof(formats_v3) / sizeof(formats_v3[0]);
+ } else if (anm->entries[i].header.version == 4 || anm->entries[i].header.version == 7) {
+ formats = formats_v4p;
+ format_count = sizeof(formats_v4p) / sizeof(formats_v4p[0]);
+ } else {
+ fprintf(stderr, "%s:%s:%s: unknown version: %u\n", argv0, current_input, anm->entries[i].name, anm->entries[i].header.version);
+ continue;
+ }
+ for (j = 0; j < anm->entries[i].animation_count; ++j) {
+ unsigned int k;
+ unsigned int done = 0;
+
+ if (j == 0 || anm->entries[i].animations[j].id != anm->entries[i].animations[j - 1].id)
+ printf(" Animation #%i:\n", anm->entries[i].animations[j].id);
+
+ printf(" @%-5u %-5u", anm->entries[i].animations[j].time, anm->entries[i].animations[j].type);
+
+ for (k = 0; k < format_count; ++k) {
+ if (formats[k].type == anm->entries[i].animations[j].type && formats[k].format) {
+ unsigned int m;
+ if ((int)strlen(formats[k].format) * 4 != anm->entries[i].animations[j].length - 8) {
+ fprintf(stderr, "%s:%s:%d: animation format length does not match data length: %lu != %u\n",
+ argv0,
+ current_input,
+ anm->entries[i].animations[j].id,
+ (long unsigned int)strlen(formats[k].format) * 4,
+ anm->entries[i].animations[j].length - 8);
+ break;
+ }
+ for (m = 0; m < strlen(formats[k].format); ++m) {
+ int32_t int1;
+ switch (formats[k].format[m]) {
+ case 'i':
+ memcpy(&int1, anm->entries[i].animations[j].data + m * sizeof(int32_t), sizeof(int32_t));
+ printf(" %i", int1);
+ break;
+ case 'f':
+ printf(" %sf", util_printfloat(anm->entries[i].animations[j].data + m * sizeof(int32_t)));
+ break;
+ default:
+ fprintf(stderr, "%s: unknown animation format specifier: %c\n", argv0, formats[k].format[m]);
+ if (!option_force) abort();
+ break;
+ }
+ }
+ printf("\n");
+ done = 1;
+ break;
+ }
+ }
+ if (!done) {
+ uint32_t* array = (uint32_t*)anm->entries[i].animations[j].data;
+ printf(" ? @%-5u %-5u", anm->entries[i].animations[j].time, anm->entries[i].animations[j].type);
+ for (k = 0; (int)k < (anm->entries[i].animations[j].length - 8) >> 2; ++k) {
+ printf(" 0x%08x", *(array + k));
+ }
+ printf("\n");
+ }
+ }
+ }
+ }
+ }
+}
+
+static void
+util_total_entry_size(const anm_t* anm, const char* name, unsigned int* widthptr, unsigned int* heightptr)
+{
+ unsigned int i;
+ unsigned int width = 0;
+ unsigned int height = 0;
+
+ for (i = 0; i < anm->entry_count; ++i) {
+ if (anm->entries[i].name == name) {
+ if (!anm->entries[i].header.thtxoffset)
+ return;
+ if (anm->entries[i].header.x + anm->entries[i].thtx.w > width)
+ width = anm->entries[i].header.x + anm->entries[i].thtx.w;
+ if (anm->entries[i].header.y + anm->entries[i].thtx.h > height)
+ height = anm->entries[i].header.y + anm->entries[i].thtx.h;
+ }
+ }
+
+ *widthptr = width;
+ *heightptr = height;
+}
+
+#ifdef HAVE_LIBPNG
+static void
+anm_replace(const anm_t* anm, const char* name, const char* filename)
+{
+ unsigned int i;
+ unsigned int y;
+ unsigned int width = 0, height = 0;
+ char* image_data = NULL;
+
+ unsigned char header[8] = { 0 };
+ FILE* pngfd;
+ png_structp png_ptr;
+ png_infop info_ptr;
+ png_bytepp row_pointers;
+
+ pngfd = fopen(filename, "rb");
+ if (!pngfd) {
+ fprintf(stderr, "%s: couldn't open %s for reading: %s\n", argv0, filename, strerror(errno));
+ exit(1);
+ }
+
+ if (fread(header, 8, 1, pngfd) != 1) {
+ fprintf(stderr, "%s: couldn't read from %s: %s\n", argv0, filename, strerror(errno));
+ exit(1);
+ }
+
+ if (png_sig_cmp(header, 0, 8) != 0) {
+ fprintf(stderr, "%s: invalid PNG signature for %s\n", argv0, filename);
+ exit(1);
+ }
+
+ png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+ info_ptr = png_create_info_struct(png_ptr);
+
+ png_init_io(png_ptr, pngfd);
+ png_set_sig_bytes(png_ptr, 8);
+
+ png_read_png(png_ptr, info_ptr, 0, NULL);
+
+ row_pointers = png_get_rows(png_ptr, info_ptr);
+ fclose(pngfd);
+
+ util_total_entry_size(anm, name, &width, &height);
+
+ if (width != info_ptr->width || height != info_ptr->height) {
+ fprintf(stderr, "%s:%s:%s: wrong image dimensions for %s: %u, %u instead of %u, %u\n", argv0, current_input, name, filename, (unsigned int)info_ptr->width, (unsigned int)info_ptr->height, width, height);
+ exit(1);
+ }
+
+ if (info_ptr->color_type != PNG_COLOR_TYPE_RGB_ALPHA) {
+ fprintf(stderr, "%s: %s must be RGBA\n", argv0, filename);
+ exit(1);
+ }
+
+ image_data = malloc(width * height * 4);
+ for (y = 0; y < info_ptr->height; ++y)
+ memcpy(image_data + y * info_ptr->width * 4, row_pointers[y], info_ptr->width * 4);
+
+ for (i = 0; i < anm->entry_count; ++i) {
+ /* XXX: Try to avoid doing the conversion for every part. */
+ if (anm->entries[i].name == name && anm->entries[i].header.thtxoffset) {
+ char* converted_data = rgba_to_fmt((uint32_t*)image_data, width * height, anm->entries[i].header.format);
+
+ for (y = anm->entries[i].header.y; y < anm->entries[i].header.y + anm->entries[i].thtx.h; ++y) {
+ memcpy(anm->entries[i].data + (y - anm->entries[i].header.y) * anm->entries[i].thtx.w * format_Bpp(anm->entries[i].header.format),
+ converted_data + y * width * format_Bpp(anm->entries[i].header.format) + anm->entries[i].header.x * format_Bpp(anm->entries[i].header.format),
+ anm->entries[i].thtx.w * format_Bpp(anm->entries[i].header.format));
+ }
+
+ free(converted_data);
+ }
+ }
+
+ free(image_data);
+
+ png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
+}
+
+static void
+anm_extract(const anm_t* anm, const char* name)
+{
+ png_structp png_ptr;
+ png_infop info_ptr;
+ png_byte** imagep;
+ unsigned int i;
+ unsigned int y;
+ char* data;
+ FILE* fp;
+ unsigned int width = 0;
+ unsigned int height = 0;
+
+ util_total_entry_size(anm, name, &width, &height);
+
+ if (width == 0 || height == 0) {
+ fprintf(stderr, "%s: calculated width or height is zero\n", argv0);
+ if (!option_force) abort();
+ return;
+ }
+
+ util_makepath(name);
+
+ fp = fopen(name, "wb");
+ if (!fp) {
+ fprintf(stderr, "%s: couldn't open %s for writing: %s\n", argv0, name, strerror(errno));
+ return;
+ }
+
+ png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
+ if (!png_ptr) {
+ fprintf(stderr, "%s: png_create_write_struct failed\n", argv0);
+ abort();
+ }
+ info_ptr = png_create_info_struct(png_ptr);
+ if (!info_ptr) {
+ fprintf(stderr, "%s: png_create_info_struct failed\n", argv0);
+ abort();
+ }
+ png_init_io(png_ptr, fp);
+
+ data = malloc(width * height * 4);
+ memset(data, 0xff, width * height * 4);
+
+ for (i = 0; i < anm->entry_count; ++i) {
+ if (anm->entries[i].name == name) {
+ for (y = anm->entries[i].header.y; y < anm->entries[i].header.y + anm->entries[i].thtx.h; ++y) {
+ memcpy(data + y * width * 4 + anm->entries[i].header.x * 4,
+ anm->entries[i].data + (y - anm->entries[i].header.y) * anm->entries[i].thtx.w * 4,
+ anm->entries[i].thtx.w * 4);
+ }
+ }
+ }
+
+ png_set_IHDR(png_ptr, info_ptr,
+ width, height, 8, PNG_COLOR_TYPE_RGB_ALPHA,
+ PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
+ png_write_info(png_ptr, info_ptr);
+
+ imagep = malloc(sizeof(png_byte*) * height);
+ for (y = 0; y < height; ++y)
+ imagep[y] = (png_byte*)(data + y * width * 4);
+
+ png_write_image(png_ptr, imagep);
+ png_write_end(png_ptr, info_ptr);
+ png_destroy_write_struct(&png_ptr, &info_ptr);
+
+ free(imagep);
+ free(data);
+
+ fclose(fp);
+}
+#endif
+
+static void
+anm_free(anm_t* anm)
+{
+ unsigned int i, j;
+
+ if (!anm)
+ return;
+
+ if (anm->entries) {
+ for (i = 0; i < anm->entry_count; ++i) {
+ if (anm->entries[i].sprites)
+ free(anm->entries[i].sprites);
+ if (anm->entries[i].animations) {
+ for (j = 0; j < anm->entries[i].animation_count; ++j) {
+ if (anm->entries[i].animations[j].data)
+ free(anm->entries[i].animations[j].data);
+ }
+ free(anm->entries[i].animations);
+ }
+ if (anm->entries[i].data)
+ free(anm->entries[i].data);
+ }
+ free(anm->entries);
+ }
+
+ if (anm->names) {
+ for (i = 0; i < anm->name_count; ++i)
+ if (anm->names[i])
+ free(anm->names[i]);
+ free(anm->names);
+ }
+
+ free(anm);
+}
+
+static void
+print_usage()
+{
+ printf("Usage: %s [OPTION]... MODE ARCHIVE [FILE]...\n"
+ "MODE can be:\n"
+ " -l list archive\n"
+#ifdef HAVE_LIBPNG
+ " -x extract archive\n"
+ " -r replace entries in archive\n"
+#endif
+ "OPTION can be:\n"
+ " -v N verbosity level\n"
+ " -f ignore errors when possible\n"
+ " -d write filemap.dat\n"
+ " -h display this help and exit\n"
+ " -V display version information and exit\n\n"
+ "Additional documentation might be available at <" PACKAGE_URL ">.\n"
+ "Report bugs to <" PACKAGE_BUGREPORT ">.\n"
+ , argv0);
+}
+
+int
+main(int argc, char* argv[])
+{
+ int i;
+ int filestart = 0;
+ int mode = 0;
+
+ argv0 = util_shortname(argv[0]);
+ if (!argv0)
+ argv0 = PACKAGE;
+
+ for (i = 1; i < argc; ++i) {
+ if (strcmp(argv[i], "-h") == 0) {
+ print_usage();
+ exit(0);
+ } else if (strcmp(argv[i], "-V") == 0) {
+ util_print_version("thanm", PACKAGE_THANM_VERSION);
+ exit(0);
+ } else if (strcmp(argv[i], "-v") == 0) {
+ ++i;
+ if (i >= argc) {
+ print_usage();
+ exit(1);
+ }
+ option_verbose = strtol(argv[i], NULL, 10);
+ } else if (strcmp(argv[i], "-d") == 0) {
+ option_filemap = 1;
+ } else if (strcmp(argv[i], "-f") == 0) {
+ option_force = 1;
+#ifdef HAVE_LIBPNG
+ } else if (strcmp(argv[i], "-x") == 0) {
+ mode = MODE_EXTRACT;
+ } else if (strcmp(argv[i], "-r") == 0) {
+ mode = MODE_REPLACE;
+#endif
+ } else if (strcmp(argv[i], "-l") == 0) {
+ mode = MODE_LIST;
+ } else {
+ filestart = i;
+ break;
+ }
+ }
+
+ if (!filestart) {
+ print_usage();
+ exit(1);
+ }
+
+ current_input = util_shortname(argv[filestart]);
+
+ if (mode == MODE_LIST) {
+ anm_t* anm = anm_read_file(argv[filestart]);
+ anm_list(anm);
+ anm_free(anm);
+#ifdef HAVE_LIBPNG
+ } else if (mode == MODE_EXTRACT) {
+ anm_t* anm = anm_read_file(argv[filestart]);
+
+ if (argc == filestart + 1) {
+ /* Extract all files. */
+ for (i = 0; i < (int)anm->name_count; ++i) {
+ puts(anm->names[i]);
+ anm_extract(anm, anm->names[i]);
+ }
+ } else {
+ /* Extract all listed files. */
+ for (i = filestart + 1; i < argc; ++i) {
+ unsigned int j;
+ for (j = 0; j < anm->name_count; ++j) {
+ if (strcmp(argv[i], anm->names[j]) == 0) {
+ anm_extract(anm, anm->names[j]);
+ goto extract_next;
+ }
+ }
+ fprintf(stderr, "%s:%s: %s not found in archive\n", argv0, current_input, argv[i]);
+extract_next:
+ ;
+ }
+ }
+
+ anm_free(anm);
+ } else if (mode == MODE_REPLACE) {
+ FILE* anmfp;
+ unsigned int offset = 0;
+ anm_t* anm = anm_read_file(argv[filestart]);
+
+ for (i = 0; i < (int)anm->name_count; ++i) {
+ if (strcmp(argv[filestart + 1], anm->names[i]) == 0) {
+ anm_replace(anm, anm->names[i], argv[filestart + 2]);
+ goto replace_done;
+ }
+ }
+
+ fprintf(stderr, "%s:%s: %s not found in archive\n", argv0, current_input, argv[filestart + 1]);
+
+replace_done:
+
+ anmfp = fopen(argv[filestart], "rb+");
+ if (!anmfp) {
+ fprintf(stderr, "%s:%s: couldn't open for writing: %s\n", argv0, current_input, strerror(errno));
+ abort();
+ }
+
+ for (i = 0; i < (int)anm->entry_count; ++i) {
+ unsigned int nextoffset = anm->entries[i].header.nextoffset;
+ if (strcmp(argv[filestart + 1], anm->entries[i].name) == 0 && anm->entries[i].header.thtxoffset) {
+ fseek(anmfp, offset + anm->entries[i].header.thtxoffset + 4 + sizeof(thtx_header_t), SEEK_SET);
+ fwrite(anm->entries[i].data, anm->entries[i].thtx.w * anm->entries[i].thtx.h * format_Bpp(anm->entries[i].thtx.format), 1, anmfp);
+ }
+ offset += nextoffset;
+ }
+
+ fclose(anmfp);
+
+ anm_free(anm);
+#endif
+ } else {
+ fprintf(stderr, "%s: no MODE specified\n", argv0);
+ print_usage();
+ exit(1);
+ }
+
+ exit(0);
+}
diff --git a/thanm.h b/thanm.h
new file mode 100644
index 00000000..139a2126
--- /dev/null
+++ b/thanm.h
@@ -0,0 +1,178 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+#ifndef THANM_H_
+#define THANM_H_
+
+#include
+#include
+
+enum {
+ MODE_EXTRACT = 1,
+ MODE_REPLACE = 2,
+ MODE_LIST = 3
+};
+
+typedef enum {
+ FORMAT_BGRA8888 = 1,
+ FORMAT_BGR565 = 3,
+ FORMAT_BGRA4444 = 5,
+ FORMAT_GRAY8 = 7
+} format_t;
+
+typedef struct {
+#ifdef PACK_PRAGMA
+#pragma pack(push,1)
+#endif
+ uint32_t id;
+ float x, y, w, h;
+#ifdef PACK_PRAGMA
+#pragma pack(pop)
+#endif
+} PACK_ATTRIBUTE sprite_t;
+
+typedef struct {
+#ifdef PACK_PRAGMA
+#pragma pack(push,1)
+#endif
+ int32_t id;
+ uint32_t offset;
+#ifdef PACK_PRAGMA
+#pragma pack(pop)
+#endif
+} PACK_ATTRIBUTE animation_offset_t;
+
+typedef struct {
+#ifdef PACK_PRAGMA
+#pragma pack(push,1)
+#endif
+ uint16_t type;
+ uint16_t length;
+ uint32_t time;
+ int32_t id;
+#ifdef PACK_PRAGMA
+#pragma pack(pop)
+#endif
+ char* data;
+} PACK_ATTRIBUTE animation_t;
+
+typedef struct {
+ uint16_t type;
+ const char* format;
+} opcode_fmt_t;
+
+typedef struct {
+#ifdef PACK_PRAGMA
+#pragma pack(push,1)
+#endif
+ uint32_t sprites;
+ uint32_t animations;
+ uint32_t zero1;
+ uint32_t w, h;
+ uint32_t format;
+ uint32_t zero2; /* XXX: A few are 0xff000000. */
+ uint32_t nameoffset;
+ uint32_t x, y; /* Not used until TH11. */
+ /* 0: TH06
+ * 2: TH07
+ * 3: TH08, TH09
+ * 4: TH95, TH10
+ * 7: TH11, TH12 */
+ uint32_t version;
+ /* 0 - Random things, everything in TH06. */
+ /* 1 - data/ascii/loading.png for TH08 and TH09. */
+ /* 10 - Mostly sprites? */
+ /* 11 - Used mainly for backgrounds and ascii.png. */
+ uint32_t unknown1;
+ uint32_t thtxoffset;
+ /* Always zero when thtxoffset is zero. Otherwise 1. */
+ uint32_t unknown2;
+ uint32_t nextoffset;
+ uint32_t zero3;
+#ifdef PACK_PRAGMA
+#pragma pack(pop)
+#endif
+} PACK_ATTRIBUTE anm_header_t;
+
+typedef struct {
+#ifdef PACK_PRAGMA
+#pragma pack(push,1)
+#endif
+ uint32_t version;
+ uint16_t sprites;
+ uint16_t animations;
+ uint16_t zero1;
+ uint16_t w, h;
+ uint16_t format;
+ uint32_t nameoffset;
+ uint16_t x, y;
+ uint32_t unknown1;
+ uint32_t thtxoffset;
+ uint32_t unknown2;
+ uint32_t nextoffset;
+ uint32_t zero2[6];
+#ifdef PACK_PRAGMA
+#pragma pack(pop)
+#endif
+} PACK_ATTRIBUTE anm_header11_t;
+
+typedef struct {
+#ifdef PACK_PRAGMA
+#pragma pack(push,1)
+#endif
+ uint16_t zero;
+ uint16_t format;
+ /* These may be different from the parent entry. */
+ uint16_t w, h;
+ uint32_t size;
+#ifdef PACK_PRAGMA
+#pragma pack(pop)
+#endif
+} PACK_ATTRIBUTE thtx_header_t;
+
+typedef struct {
+ anm_header_t header;
+ thtx_header_t thtx;
+ char* name;
+ unsigned int sprite_count;
+ sprite_t* sprites;
+ /* TODO: Rename this, or enclose it. */
+ unsigned int animation_count;
+ animation_t* animations;
+ unsigned int data_size;
+ char* data;
+} entry_t;
+
+typedef struct {
+ unsigned int name_count;
+ char** names;
+ unsigned int entry_count;
+ entry_t* entries;
+} anm_t;
+
+#endif
diff --git a/thcrypt.c b/thcrypt.c
new file mode 100644
index 00000000..57517123
--- /dev/null
+++ b/thcrypt.c
@@ -0,0 +1,84 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+#include
+#include
+#include
+#include
+#include "thcrypt.h"
+
+int
+th_encrypt(unsigned char* data, unsigned int size, unsigned char key, const unsigned char step, unsigned int block, unsigned int limit)
+{
+ const unsigned char* end;
+ unsigned char* ip;
+ unsigned char* mblock;
+ unsigned char* temp;
+ unsigned int increment = (block >> 1) + (block & 1);
+
+ mblock = malloc(block);
+
+ if (size < block >> 2)
+ size = 0;
+ else
+ size -= (size % block < block >> 2) * size % block + size % 2;
+
+ if (limit % block != 0)
+ limit = limit + (block - (limit % block));
+
+ end = data + (size < limit ? size : limit);
+
+ while (data < end) {
+ if (end - data < block) {
+ block = end - data;
+ increment = (block >> 1) + (block & 1);
+ }
+
+ temp = mblock;
+
+ for (ip = data + block - 1; ip > data; ip -= 2) {
+ *temp = *ip ^ key;
+ *(temp + increment) = *(ip - 1) ^ (key + step * increment);
+ temp++;
+ key += step;
+ }
+
+ if (block & 1) {
+ *temp = *ip ^ key;
+ key += step;
+ }
+ key += step * increment;
+
+ memcpy(data, mblock, block);
+ data += block;
+ }
+
+ free(mblock);
+
+ return 0;
+}
diff --git a/thcrypt.h b/thcrypt.h
new file mode 100644
index 00000000..ef2c22e2
--- /dev/null
+++ b/thcrypt.h
@@ -0,0 +1,42 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+#ifndef THCRYPT_H_
+#define THCRYPT_H_
+
+#include
+
+int th_encrypt(
+ unsigned char* data,
+ unsigned int size,
+ unsigned char key,
+ unsigned char step,
+ unsigned int block,
+ unsigned int limit);
+
+#endif
diff --git a/thdat.c b/thdat.c
new file mode 100644
index 00000000..990a9115
--- /dev/null
+++ b/thdat.c
@@ -0,0 +1,138 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+#include
+#include
+#include
+#include
+#include
+#include "datpacker.h"
+#include "util.h"
+#include "thdat.h"
+#include "thrle.h"
+#include "thlzss.h"
+
+archive_t*
+archive_open(FILE* fd, uint32_t version, uint32_t offset, unsigned int count)
+{
+ archive_t* archive;
+
+ /* Reserve some space for the header. */
+ /* TODO: Use a seek wrapper. */
+ if (fseek(fd, offset, SEEK_SET) == -1) {
+ snprintf(library_error, LIBRARY_ERROR_SIZE, "couldn't seek: %s", strerror(errno));
+ return NULL;
+ }
+
+ archive = malloc(sizeof(archive_t));
+ archive->entries = malloc(count * sizeof(entry_t));
+
+ archive->version = version;
+ archive->fd = fd;
+ archive->offset = offset;
+ archive->count = 0;
+
+ return archive;
+}
+
+unsigned char*
+thdat_read_file(entry_t* entry, FILE* fd)
+{
+ unsigned char* data = malloc(entry->size);
+
+ if (fread(data, entry->size, 1, fd) != 1) {
+ snprintf(library_error, LIBRARY_ERROR_SIZE, "couldn't read: %s", strerror(errno));
+ free(data);
+ return NULL;
+ }
+
+ return data;
+}
+
+unsigned char*
+thdat_read_file_lzss(entry_t* entry, FILE* fd)
+{
+ return th_lz_fd(fd, &entry->zsize);
+}
+
+unsigned char*
+thdat_rle(entry_t* entry, unsigned char* data)
+{
+ unsigned char* zdata = th_rle(data, entry->size, &entry->zsize);
+
+ if (entry->zsize >= entry->size) {
+ entry->zsize = entry->size;
+ free(zdata);
+ zdata = data;
+ } else {
+ free(data);
+ }
+
+ return zdata;
+}
+
+int
+thdat_write_entry(archive_t* archive, entry_t* entry, unsigned char* data)
+{
+ size_t ret;
+
+#pragma omp critical
+{
+ ret = fwrite(data, entry->zsize, 1, archive->fd);
+ entry->offset = archive->offset;
+ archive->offset += entry->zsize;
+}
+
+ free(data);
+
+ if (ret != 1) {
+ snprintf(library_error, LIBRARY_ERROR_SIZE, "couldn't write: %s", strerror(errno));
+ free(data);
+ return -1;
+ } else
+ return 0;
+}
+
+static int
+entry_compar(const void* a, const void* b)
+{
+ entry_t* ea = (entry_t*)a;
+ entry_t* eb = (entry_t*)b;
+ if (ea->offset < eb->offset)
+ return -1;
+ else if (ea->offset > eb->offset)
+ return 1;
+ else
+ return 0;
+}
+
+void
+thdat_sort(archive_t* archive)
+{
+ qsort(archive->entries, archive->count, sizeof(entry_t), entry_compar);
+}
diff --git a/thdat.h b/thdat.h
new file mode 100644
index 00000000..4229d07c
--- /dev/null
+++ b/thdat.h
@@ -0,0 +1,59 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+#ifndef THDAT_H_
+#define THDAT_H_
+
+#include
+#include
+#include
+#include "datpacker.h"
+
+/* TODO: Rename functions. */
+archive_t* archive_open(FILE* fd, uint32_t version, uint32_t offset, unsigned int count);
+
+/* All of these functions set library_error on error. */
+
+/* Reads entry->size bytes from the passed file descriptor. Returns a newly
+ * allocated buffer with the read data. Returns NULL on error. */
+unsigned char* thdat_read_file(entry_t* entry, FILE* fd);
+/* Reads entry->size bytes from the passed file descriptor. Returns a newly
+ * allocated buffer containing an LZSS compressed version of the read data.
+ * Returns NULL on error. */
+unsigned char* thdat_read_file_lzss(entry_t* entry, FILE* fd);
+/* Performs RLE on the passed buffer. Frees the passed buffer. Updates
+ * entry->zsize. Returns a newly allocated buffer containing the RLE data.
+ * Returns NULL on error. */
+unsigned char* thdat_rle(entry_t* entry, unsigned char* data);
+/* Writes the entry->zsize bytes from the passed buffer to the archive. Frees
+ * the passed buffer. Updates the archive offset. Returns -1 on error. */
+int thdat_write_entry(archive_t* archive, entry_t* entry, unsigned char* data);
+/* Sorts the entries in the archive according to their offsets. */
+void thdat_sort(archive_t* archive);
+
+#endif
diff --git a/thlzss.c b/thlzss.c
new file mode 100644
index 00000000..f5eca4e0
--- /dev/null
+++ b/thlzss.c
@@ -0,0 +1,223 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+#include
+#include
+#include
+#include
+#include
+
+#include "bits.h"
+
+/* Compression specification:
+ *
+ * The LZSS algorithm is used. A single bit is used in front of every entry:
+ * 1 means that a byte follows, and 0 that an (offset, length) follows. The
+ * size of the offset is 13 bits, and the length is 4 bits. The minimum match
+ * length is 3, allowing for a maximum match length of 18. Offsets cannot
+ * refer to entry 0 in the dictionary.
+ *
+ * The dictionary is 8192 bytes large, writing must start at index 1. The last
+ * 18 bytes (the dictionary pointer and in front of it) in the dictionary
+ * cannot be read. The dictionary is initialized to 0.
+ *
+ * Forward-looking matches are possible for repeating data. A zero-offset,
+ * zero-size entry is used to terminate the data. This information is only
+ * verified for TH10 and TH11. */
+
+#define LZSS_DICTSIZE 0x2000
+#define LZSS_DICTSIZE_MASK 0x1fff
+#define LZSS_MIN_MATCH 3
+/* LZSS_MIN_MATCH + 4 bits. */
+#define LZSS_MAX_MATCH 18
+
+/* Higher values seem to give both better speed and compression. */
+#define HASH_SIZE 0x10000
+#define HASH_NULL 0
+
+/* This structure contains a hash for the dictionary and a special linked list
+ * which is used for the entries in the hash. */
+typedef struct {
+ unsigned int hash[HASH_SIZE];
+ unsigned int prev[LZSS_DICTSIZE];
+ unsigned int next[LZSS_DICTSIZE];
+} hash_t;
+
+static inline unsigned int
+generate_key(const unsigned char* array, unsigned int base)
+{
+ return ((array[(base + 1) & LZSS_DICTSIZE_MASK] << 8) | array[(base + 2) & LZSS_DICTSIZE_MASK]) ^ (array[base] << 4);
+}
+
+static inline void
+list_remove(hash_t* hash, const unsigned int key, const unsigned int offset)
+{
+ /* XXX: It is always the last entry in the list that is removed.
+ * hash->next[offset] will always be HASH_NULL. */
+
+ hash->next[hash->prev[offset]] = HASH_NULL;
+
+ if (hash->prev[offset] == HASH_NULL)
+ hash->hash[key] = HASH_NULL;
+}
+
+static inline void
+list_add(hash_t* hash, const unsigned int key, const unsigned int offset)
+{
+ hash->next[offset] = hash->hash[key];
+ hash->prev[offset] = HASH_NULL;
+ hash->prev[hash->hash[key]] = offset;
+ hash->hash[key] = offset;
+}
+
+/* TODO: Make this read blocks for improved performance. */
+typedef int (*read_byte_fptr)(void*);
+
+static unsigned char*
+th_lz(unsigned int* outsize, read_byte_fptr read_byte, void* data)
+{
+ bitstream_t bs;
+ hash_t hash;
+ unsigned char dict[LZSS_DICTSIZE];
+ unsigned int dict_head = 1;
+ unsigned int waiting_bytes = 0;
+ unsigned int i;
+ int c;
+
+ bitstream_init(&bs, 2048);
+ memset(&hash, 0, sizeof(hash));
+ memset(dict, 0, sizeof(dict));
+
+ /* Fill the forward-looking buffer. */
+ for (i = 0; i < LZSS_MAX_MATCH; ++i) {
+ c = read_byte(data);
+ if (c == -1)
+ break;
+ dict[dict_head + i] = c;
+ waiting_bytes++;
+ }
+
+ while (waiting_bytes) {
+ unsigned int match_len = LZSS_MIN_MATCH - 1;
+ unsigned int match_offset = 0;
+ unsigned int offset;
+
+ /* Find a good match. */
+ for (offset = hash.hash[generate_key(dict, dict_head)]; offset != HASH_NULL; offset = hash.next[offset]) {
+ unsigned int match_tmp = 0;
+ for (i = 0; i < waiting_bytes; ++i) {
+ if (dict[(dict_head + i) & LZSS_DICTSIZE_MASK] != dict[(offset + i) & LZSS_DICTSIZE_MASK])
+ break;
+ match_tmp++;
+ }
+ if (match_tmp > match_len) {
+ match_len = match_tmp;
+ match_offset = offset;
+ if (match_len == waiting_bytes)
+ break;
+ }
+ }
+
+ /* Write data to the output buffer. */
+ if (match_len < LZSS_MIN_MATCH) {
+ match_len = 1;
+ bitstream_write1(&bs, 1);
+ bitstream_write(&bs, 8, dict[dict_head]);
+ } else {
+ bitstream_write1(&bs, 0);
+ bitstream_write(&bs, 13, match_offset);
+ bitstream_write(&bs, 4, match_len - LZSS_MIN_MATCH);
+ }
+
+ /* Add bytes to the dictionary. */
+ for (i = 0; i < match_len; ++i) {
+ unsigned int offset = (dict_head + LZSS_MAX_MATCH) & LZSS_DICTSIZE_MASK;
+ /* TODO: See if it is possible to combine list_add and list_remove
+ * instead. The cases where they are not called at the same time
+ * must be identified first. */
+ if (offset != 0)
+ list_remove(&hash, generate_key(dict, offset), offset);
+
+ c = read_byte(data);
+ if (c != -1) {
+ dict[offset] = c;
+ waiting_bytes++;
+ }
+
+ if (dict_head != 0)
+ list_add(&hash, generate_key(dict, dict_head), dict_head);
+
+ dict_head = (dict_head + 1) & LZSS_DICTSIZE_MASK;
+ waiting_bytes--;
+ }
+ }
+
+ bitstream_write1(&bs, 0);
+ bitstream_write(&bs, 13, 0);
+ bitstream_write(&bs, 4, 0);
+
+ bitstream_finish(&bs);
+
+ *outsize = bs.used_bytes;
+ return bs.buffer;
+}
+
+static int
+read_byte_mem(const unsigned char** ptrs)
+{
+ if (ptrs[0] >= ptrs[1])
+ return -1;
+ else
+ return *ptrs[0]++;
+}
+
+unsigned char*
+th_lz_mem(const unsigned char* in, unsigned int insize, unsigned int* outsize)
+{
+ unsigned char* ptrs[2];
+ ptrs[0] = (unsigned char*)in;
+ ptrs[1] = (unsigned char*)in + insize;
+ return th_lz(outsize, (read_byte_fptr)read_byte_mem, &ptrs);
+}
+
+static int
+read_byte_fd(FILE* stream)
+{
+ int c = fgetc(stream);
+
+ if (c == EOF)
+ return -1;
+ else
+ return c;
+}
+
+unsigned char*
+th_lz_fd(FILE* stream, unsigned int* outsize)
+{
+ return th_lz(outsize, (read_byte_fptr)read_byte_fd, stream);
+}
diff --git a/thlzss.h b/thlzss.h
new file mode 100644
index 00000000..7ba9f068
--- /dev/null
+++ b/thlzss.h
@@ -0,0 +1,46 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+#ifndef THLZSS_H_
+#define THLZSS_H_
+
+#include
+#include
+
+unsigned char* th_lz_fd(
+ FILE* stream,
+ unsigned int* outsize);
+
+/* Compress insize bytes from in and return a pointer to allocated data outsize
+ * large. */
+unsigned char* th_lz_mem(
+ const unsigned char* in,
+ unsigned int insize,
+ unsigned int* outsize);
+
+#endif
diff --git a/thrle.c b/thrle.c
new file mode 100644
index 00000000..29ba11a8
--- /dev/null
+++ b/thrle.c
@@ -0,0 +1,78 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+#include
+#include
+#include
+
+static unsigned char*
+buffer_add(unsigned char* buffer, unsigned int* buffer_pos, unsigned int* buffer_size, unsigned char b)
+{
+ if (*buffer_pos >= *buffer_size) {
+ *buffer_size *= 2;
+ buffer = realloc(buffer, *buffer_size);
+ }
+ buffer[(*buffer_pos)++] = b;
+ return buffer;
+}
+
+unsigned char*
+th_rle(const unsigned char* in, unsigned int insize, unsigned int* outsize)
+{
+ const unsigned char* orig_in = in;
+ unsigned char* buffer = NULL;
+ unsigned int buffer_size = 1024;
+ int prevc = -1;
+ int c = -1;
+ int rl = 0;
+
+ buffer = malloc(buffer_size);
+
+ while ((in - orig_in) < insize) {
+ c = *in++;
+ if (rl) {
+ if (c != prevc || rl == 0x100) {
+ buffer = buffer_add(buffer, outsize, &buffer_size, rl - 1);
+ rl = 0;
+ buffer = buffer_add(buffer, outsize, &buffer_size, c);
+ }
+ } else {
+ buffer = buffer_add(buffer, outsize, &buffer_size, c);
+ }
+
+ if (c == prevc)
+ rl++;
+
+ prevc = c;
+ }
+
+ if (rl)
+ buffer = buffer_add(buffer, outsize, &buffer_size, rl - 1);
+
+ return buffer;
+}
diff --git a/thrle.h b/thrle.h
new file mode 100644
index 00000000..9b904407
--- /dev/null
+++ b/thrle.h
@@ -0,0 +1,34 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+#ifndef THRLE_H_
+#define THRLE_H_
+
+unsigned char* th_rle(const unsigned char* in, unsigned int insize, unsigned int* outsize);
+
+#endif
diff --git a/util.c b/util.c
new file mode 100644
index 00000000..748d05ea
--- /dev/null
+++ b/util.c
@@ -0,0 +1,315 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+
+#include
+#include
+#include
+#include
+#include
+#include
+#ifdef HAVE_SYS_STAT_H
+#include
+#endif
+#ifdef HAVE_IO_H
+#include
+#endif
+#ifdef HAVE_ICONV_H
+#include
+#endif
+#ifdef HAVE_LIBGEN_H
+#include
+#endif
+#include "program.h"
+#include "util.h"
+
+void
+util_print_version(const char* name, const char* version)
+{
+ printf("%s %s (part of " PACKAGE_NAME " release " PACKAGE_VERSION ")\n"
+ "The latest version is available at <" PACKAGE_URL ">\n",
+ name, version);
+}
+
+int
+util_seek(FILE* stream, long offset, filemap_t* filemap)
+{
+ if (filemap)
+ filemap->pos = offset;
+ if (fseek(stream, offset, SEEK_SET) != 0) {
+ fprintf(stderr, "%s: failed seeking to %lu: %s\n", argv0, offset, strerror(errno));
+ return 0;
+ } else
+ return 1;
+}
+
+long
+util_tell(FILE* stream)
+{
+ long pos = ftell(stream);
+ if (pos == -1)
+ fprintf(stderr, "argv0: ftell failed: %s\n", strerror(errno));
+ return pos;
+}
+
+/* Might be better to use stat, but it's probably less cross-platform. */
+/* This will fail for 2GB+ files, but who cares. */
+/* TODO: Implement stat() version. */
+long
+util_fsize(FILE* stream)
+{
+ long prev, end;
+
+ if ((prev = util_tell(stream)) == -1)
+ return -1;
+
+ if (fseek(stream, 0, SEEK_END) == -1) {
+ fprintf(stderr, "%s: failed seeking to end: %s\n", argv0, strerror(errno));
+ return -1;
+ }
+
+ if ((end = util_tell(stream)) == -1)
+ return -1;
+
+ if (!util_seek(stream, prev, NULL))
+ return -1;
+
+ return end;
+}
+
+int
+util_read(FILE* stream, void* buffa, size_t size, int c, filemap_t* filemap)
+{
+ if (size == 0) {
+ fprintf(stderr, "%s: util_read called with size 0\n", argv0);
+ abort();
+ }
+ if (filemap) {
+ memset(filemap->data + filemap->pos, c, size);
+ filemap->pos += size;
+ }
+ if (fread(buffa, size, 1, stream) != 1) {
+ fprintf(stderr, "%s: failed reading %lu bytes: %s\n", argv0, (long unsigned int)size, strerror(errno));
+ return 0;
+ } else
+ return 1;
+}
+
+ssize_t
+util_read_asciiz(char* buffer, size_t buffersize, FILE* stream)
+{
+ unsigned int i;
+ int c;
+ for (i = 0; i < buffersize; ++i) {
+ if (i == buffersize - 1) {
+ return -1;
+ }
+ c = fgetc(stream);
+ if (c == EOF) {
+ return -1;
+ }
+ buffer[i] = c;
+ if (!c)
+ break;
+ }
+ return i + 1;
+}
+
+void
+util_basename(char* dst, size_t dstlen, const char* src)
+{
+#ifdef WIN32
+ char filename[_MAX_FNAME];
+ char ext[_MAX_EXT];
+ _splitpath(src, NULL, NULL, filename, ext);
+ _snprintf(dst, dstlen, "%s%s", filename, ext);
+#else
+ char* tmp = strdup(src);
+ tmp = basename(tmp);
+ strncpy(dst, tmp, dstlen);
+ free(tmp);
+#endif
+}
+
+/* TODO: Use util_basename if it can be made to return a pointer to inside of
+ * path. */
+const char*
+util_shortname(const char* path)
+{
+ const char* ret;
+ if (!path) {
+ fprintf(stderr, "%s: NULL path passed to short_name\n", argv0);
+ abort();
+ }
+#ifdef WIN32
+ ret = MAX(strrchr(path, '/'), strrchr(path, '\\'));
+#else
+ ret = strrchr(path, '/');
+#endif
+ return ret ? ret + 1 : path;
+}
+
+/* XXX: Win32 has MakeSureDirectoryPathExists in dbghelp.dll. */
+void
+util_makepath(const char* path)
+{
+ char* name;
+ char* filename;
+
+ /* TODO: Implement something like this for Windows. */
+ if (!path || path[0] == '/') {
+ fprintf(stderr, "%s: invalid path: %s\n", argv0, path);
+ abort();
+ }
+
+ name = strdup(path);
+ filename = name;
+
+ for (;;) {
+ filename = strchr(filename, '/');
+ if (!filename)
+ break;
+ *filename = '\0';
+ if (
+#ifdef WIN32
+ mkdir(name)
+#else
+ mkdir(name, 0755)
+#endif
+ == -1
+#ifdef EEXIST
+ && errno != EEXIST
+#endif
+ ) {
+ fprintf(stderr, "%s: couldn't create directory %s: %s\n", argv0, name, strerror(errno));
+ abort();
+ }
+
+ *filename = '/';
+ ++filename;
+ }
+
+ free(name);
+}
+
+#ifndef HAVE_MEMPCPY
+void*
+mempcpy(void* dest, const void* src, size_t n)
+{
+ memcpy(dest, src, n);
+ return (void*)((size_t)dest + n);
+}
+#endif
+
+void
+util_sillyxor(const unsigned char* in, unsigned char* out, int size, unsigned char key, unsigned char step, const unsigned char step2)
+{
+ const unsigned char* iend = in + size;
+ while (in < iend) {
+ *out = *in ^ key;
+ key += step;
+ step += step2;
+ in++;
+ out++;
+ }
+}
+
+#ifdef HAVE_ICONV_H
+unsigned char*
+util_iconv(const char* to, const char* from, unsigned char* in, size_t insize, size_t* outsize)
+{
+ size_t ret;
+ char* inp;
+ char* outp;
+ size_t outsize2 = insize * 2;
+ size_t outsize3 = outsize2;
+ char* out = malloc(outsize2);
+ iconv_t ic = iconv_open(to, from);
+
+ if (ic == (iconv_t)-1) {
+ fprintf(stderr, "%s:iconv_open: %s\n", argv0, strerror(errno));
+ return NULL;
+ }
+
+ memset(out, 0, outsize2);
+
+ inp = (char*)in;
+ outp = out;
+
+ ret = iconv(ic, &inp, &insize, &outp, &outsize2);
+ if (ret == (size_t)-1) {
+ fprintf(stderr, "%s:iconv: %s\n", argv0, strerror(errno));
+ return NULL;
+ }
+
+ *outsize = (size_t)(outsize3 - outsize2);
+ free(in);
+
+ if (iconv_close(ic) == -1) {
+ fprintf(stderr, "%s:iconv_close: %s\n", argv0, strerror(errno));
+ return NULL;
+ }
+
+ return (unsigned char*)out;
+}
+#endif
+
+const char*
+util_printfloat(const void* data)
+{
+ static char buf[256];
+ float f;
+ unsigned int i;
+
+ memcpy(&f, data, sizeof(float));
+
+ for (i = 1; i < 50; ++i) {
+ snprintf(buf, 256, "%.*f", i, f);
+
+#ifdef HAVE_STRTOF
+ if (f == strtof(buf, NULL))
+ break;
+#else
+ {
+ float g = 0.0f;
+ if (sscanf(buf, "%f", &g) == 1 && f == g)
+ break;
+ }
+#endif
+ }
+
+ /* XXX: What about errors? */
+
+ return buf;
+}
+
+int
+util_strpcmp(const void* a, const void* b)
+{
+ return strcmp(*(const char**)a, *(const char**)b);
+}
diff --git a/util.h b/util.h
new file mode 100644
index 00000000..53aadef4
--- /dev/null
+++ b/util.h
@@ -0,0 +1,90 @@
+/*
+ * Redistribution and use in source and binary forms, with
+ * or without modification, are permitted provided that the
+ * following conditions are met:
+ *
+ * 1. Redistributions of source code must retain this list
+ * of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce this
+ * list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the
+ * distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+ * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
+ * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
+ * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
+ * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
+ * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
+ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
+ * DAMAGE.
+ */
+#ifndef UTIL_H_
+#define UTIL_H_
+
+#include
+#include
+#include
+#include
+
+#ifndef MIN
+# define MIN(a, b) ((a) < (b) ? (a) : (b))
+#endif
+#ifndef MAX
+# define MAX(a, b) ((a) > (b) ? (a) : (b))
+#endif
+
+typedef struct {
+ char* data;
+ long pos;
+} filemap_t;
+
+void util_print_version(const char* name, const char* version);
+
+/* Returns an unique string representation of a float. */
+const char* util_printfloat(const void* data);
+
+/* Returns a pointer to after the last directory separator in path. */
+const char* util_shortname(const char* path);
+
+/* Creates all components of the path. */
+void util_makepath(const char* path);
+
+/* Writes the basename of src to dst. */
+void util_basename(char* dst, size_t dstlen, const char* src);
+
+/* A wrapper for fseek with SEEK_SET which prints an error message upon error.
+ * The position in the filemap is updated. */
+int util_seek(FILE* stream, long offset, filemap_t* filemap);
+/* A wrapper for ftell which prints an error message upon error. */
+long util_tell(FILE* stream);
+/* A wrapper for fread which prints an error message upon error. The bytes
+ * read are filled with c in the filemap. */
+int util_read(FILE* stream, void* buffa, size_t size, int c, filemap_t* filemap);
+/* Returns the filesize of the passed file stream, or -1 and an error message
+ * upon error. */
+long util_fsize(FILE* stream);
+/* Reads a stream until '\0'. */
+ssize_t util_read_asciiz(char* buffer, size_t buffersize, FILE* stream);
+
+/* Compares two strings. Can be used by qsort. */
+int util_strpcmp(const void* a, const void* b);
+
+/* "Writes" a value to a buffer and returns a pointer to the memory location
+ * after the written value. */
+void* mempcpy(void* dest, const void* src, size_t n);
+
+/* Performs simple encryption. */
+void util_sillyxor(const unsigned char* in, unsigned char* out, int size, unsigned char key, unsigned char step, const unsigned char step2);
+
+/* Converts a string from one character set to another. */
+unsigned char* util_iconv(const char* to, const char* from, unsigned char* in, size_t insize, size_t* outsize);
+
+#endif