diff options
47 files changed, 3660 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a980bc5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,13 @@ +/*.a +/*.so +/src/config.h +/src/*.o +/src/*.lo +/src/*.to +/src/*.xa +/src/*.bin +/src/*.c.txt +/src/*.cat +/doc/*.[0-9] +/tests/slurp.o +/tests/fuzz/corpus/ @@ -0,0 +1 @@ +TD_USE_BUILTIN_HOOKS='git' diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,661 @@ + GNU AFFERO GENERAL PUBLIC LICENSE + Version 3, 19 November 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/> + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU Affero General Public License is a free, copyleft license for +software and other kinds of works, specifically designed to ensure +cooperation with the community in the case of network server software. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +our General Public Licenses are 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. + + 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. + + Developers that use our General Public Licenses protect your rights +with two steps: (1) assert copyright on the software, and (2) offer +you this License which gives you legal permission to copy, distribute +and/or modify the software. + + A secondary benefit of defending all users' freedom is that +improvements made in alternate versions of the program, if they +receive widespread use, become available for other developers to +incorporate. Many developers of free software are heartened and +encouraged by the resulting cooperation. However, in the case of +software used on network servers, this result may fail to come about. +The GNU General Public License permits making a modified version and +letting the public access it on a server without ever releasing its +source code to the public. + + The GNU Affero General Public License is designed specifically to +ensure that, in such cases, the modified source code becomes available +to the community. It requires the operator of a network server to +provide the source code of the modified version running there to the +users of that server. Therefore, public use of a modified version, on +a publicly accessible server, gives the public access to the source +code of the modified version. + + An older license, called the Affero General Public License and +published by Affero, was designed to accomplish similar goals. This is +a different license, not a version of the Affero GPL, but Affero has +released a new version of the Affero GPL which permits relicensing under +this license. + + 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 Affero 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. Remote Network Interaction; Use with the GNU General Public License. + + Notwithstanding any other provision of this License, if you modify the +Program, your modified version must prominently offer all users +interacting with it remotely through a computer network (if your version +supports such interaction) an opportunity to receive the Corresponding +Source of your version by providing access to the Corresponding Source +from a network server at no charge, through some standard or customary +means of facilitating copying of software. This Corresponding Source +shall include the Corresponding Source for any work covered by version 3 +of the GNU General Public License that is incorporated pursuant to the +following paragraph. + + 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 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 work with which it is combined will remain governed by version +3 of the GNU General Public License. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU Affero 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 Affero 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 Affero 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 Affero General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + <one line to give the program's name and a brief idea of what it does.> + Copyright (C) <year> <name of author> + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero 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 Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +Also add information on how to contact you by electronic and paper mail. + + If your software can interact with users remotely through a computer +network, you should also make sure that it provides a way for users to +get its source. For example, if your program is a web application, its +interface could display a "Source" link that leads users to an archive +of the code. There are many ways you could offer source, and different +solutions will be better for different programs; see section 13 for the +specific requirements. + + 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 AGPL, see +<https://www.gnu.org/licenses/>. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..26919ba --- /dev/null +++ b/Makefile @@ -0,0 +1,199 @@ +.POSIX: +DATE = 1970-01-01 +VERSION = 0.1.0 +NAME = pindaiba +NAME_UC = PindaÃba +MAILING_LIST = public-inbox +TRANSLATIONS = +## Installation prefix. Defaults to "/usr". +PREFIX = /usr +BINDIR = $(PREFIX)/bin +LIBDIR = $(PREFIX)/lib +INCLUDEDIR = $(PREFIX)/include +SRCDIR = $(PREFIX)/src/$(NAME) +SHAREDIR = $(PREFIX)/share +LOCALEDIR = $(SHAREDIR)/locale +MANDIR = $(SHAREDIR)/man +CFLAGS.a = $(CFLAGS) +CFLAGS.so = $(CFLAGS) -fPIC +LDFLAGS.a = $(LDFLAGS) +LDFLAGS.so = $(LDFLAGS) --shared +LDLIBS.a = $(LDLIBS) +LDLIBS.so = $(LDLIBS) +EXT.so = .so +ILIBS = lib$(NAME).a lib$(NAME)$(EXT.so) # no ABI stability whatsoever +EXEC = ./ +## Where to store the installation. Empty by default. +DESTDIR = +LDLIBS = + + + +.SUFFIXES: +.SUFFIXES: .in .c .o .lo .to .xa .bin .msg .cat + +.in: + sed \ + -e 's:@VERSION@:$(VERSION):g' \ + -e 's:@DATE@:$(DATE):g' \ + -e 's:@NAME@:$(NAME):g' \ + < $< > $@ + if [ -x $< ]; then chmod +x $@; fi + +.c.o: + $(CC) $(CFLAGS.a) -o $@ -c $< + +.c.lo: + $(CC) $(CFLAGS.so) -o $@ -c $< + +.c.to: + $(CC) $(CFLAGS.a) -DTEST -o $@ -c $< + +.xa.bin: + $(CC) $(LDFLAGS.a) -o $@ $< $(LDLIBS.a) + + + +all: +include deps.mk + +manpages.in = $(manpages.en.in) +manpages = $(manpages.in:.in=) + +catalogs.en.msg = src/$(NAME).en.msg +catalogs.msg = $(catalogs.en.msg) +catalogs.cat = $(catalogs.msg:.msg=.cat) + +sources.o = $(sources.c:.c=.o) +sources.lo = $(sources.c:.c=.lo) +sources.to = $(sources.c:.c=.to) +sources.xa = $(sources.c:.c=.xa) +sources.bin = $(sources.c:.c=.bin) + +sources = \ + $(sources.c) \ + $(sources.c:.c=.h) \ + src/config.h.in \ + src/config.h \ + src/main.c \ + src/$(NAME).h \ + $(catalogs.msg) \ + + +derived-assets = \ + $(ILIBS) \ + src/config.h \ + $(manpages) \ + $(catalogs.cat) \ + $(sources.o) \ + $(sources.lo) \ + $(sources.to) \ + $(sources.xa) \ + $(sources.bin) \ + tests/slurp.o \ + src/main.o \ + src/main.xa \ + src/main.bin \ + +side-assets = \ + src/logerr.c.txt \ + src/catalog.c.txt \ + + + +## Default target. Builds all artifacts required for testing +## and installation. +all: $(derived-assets) + + +lib$(NAME).a: $(sources.o) +src/main.xa: $(sources.o) src/main.o +$(manpages) src/config.h: Makefile deps.mk +$(sources.o) $(sources.lo) $(sources.to): src/config.h Makefile deps.mk +tests/slurp.o: src/config.h Makefile deps.mk + + +lib$(NAME)$(EXT.so): $(sources.lo) + $(CC) $(LDFLAGS.so) -o $@ $(sources.lo) $(LDLIBS.so) + +lib$(NAME).a $(sources.xa) src/main.xa: + $(AR) $(ARFLAGS) $@ $? + +src/$(NAME).en.cat: src/i18n.bin + env DUMP_TRANSLATABLE_STRINGS=1 $(EXEC)src/i18n.bin > $*.msg.new + cmp -s $*.msg.new $*.msg || mv $*.msg.new $*.msg + rm -f $*.msg.new + gencat $@ $*.msg + touch $@ + + + +src/catalog.bin-check: src/$(NAME).en.cat + + +.SUFFIXES: .bin-check +sources.bin-check = $(sources.c:.c=.bin-check) +$(sources.bin-check): + $(EXEC)$*.bin + +check-unit: $(sources.bin-check) + + +## Run all tests. Each test suite is isolated, so that a parallel +## build can run tests at the same time. The required artifacts +## are created if missing. +check: check-unit + + +## Remove *all* derived artifacts produced during the build. +## A dedicated test asserts that this is always true. +clean: + rm -rf $(derived-assets) $(side-assets) + + +## Installs into $(DESTDIR)$(PREFIX). Its dependency target +## ensures that all installable artifacts are crafted beforehand. +install: all + mkdir -p \ + '$(DESTDIR)$(BINDIR)' \ + '$(DESTDIR)$(LIBDIR)' \ + '$(DESTDIR)$(INCLUDEDIR)' + cp src/main.bin '$(DESTDIR)$(BINDIR)'/$(NAME) + cp $(ILIBS) '$(DESTDIR)$(LIBDIR)' + cp src/$(NAME).h '$(DESTDIR)$(INCLUDEDIR)' + for f in $(sources); do \ + dir='$(DESTDIR)$(SRCDIR)'/"`dirname "$${f#src/}"`"; \ + mkdir -p "$$dir"; \ + cp -P "$$f" "$$dir"; \ + done + for l in en $(TRANSLATIONS); do \ + dir='$(DESTDIR)$(LOCALEDIR)'/"$$l"/LC_MESSAGES; \ + mkdir -p "$$dir"; \ + cp src/$(NAME)."$$l".cat "$$dir"/$(NAME).cat; \ + done + sh tools/manpages.sh -ip '$(DESTDIR)$(MANDIR)' $(manpages) + +## Uninstalls from $(DESTDIR)$(PREFIX). This is a perfect mirror +## of the "install" target, and removes *all* that was installed. +## A dedicated test asserts that this is always true. +uninstall: + rm -rf \ + '$(DESTDIR)$(BINDIR)'/$(NAME) \ + '$(DESTDIR)$(INCLUDEDIR)'/$(NAME).h \ + '$(DESTDIR)$(SRCDIR)' + for lib in $(ILIBS); do \ + rm -f '$(DESTDIR)$(LIBDIR)'/"$$lib"; \ + done + for l in en $(TRANSLATIONS); do \ + rm -f '$(DESTDIR)$(LOCALEDIR)'/"$$l"/LC_MESSAGES/$(NAME).cat; \ + done + sh tools/manpages.sh -up '$(DESTDIR)$(MANDIR)' $(manpages) + + +MAKEFILE = Makefile +## Show this help. +help: + cat $(MAKEFILE) | sh tools/makehelp.sh + + +ALWAYS: diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/README.md diff --git a/TODOs.md b/TODOs.md new file mode 100644 index 0000000..9ed59c6 --- /dev/null +++ b/TODOs.md @@ -0,0 +1,28 @@ +# Tasks + + +# Bugs + + +# Improvements + + +# Questions + + +# Decisions + + +# Ideas + + +# Proposals + + +# Resources + +- <https://github.com/mcinglis/libbase> +- <https://github.com/fragglet/c-algorithms> + + +# Scratch @@ -0,0 +1,58 @@ +manpages.en.in = \ + doc/pindaiba.CHANGELOG.en.7.in \ + doc/pindaiba.README.en.7.in \ + doc/pindaiba.TODOs.en.7.in \ + doc/pindaiba.en.1.in \ + doc/pindaiba.recipes.en.7.in \ + doc/pindaiba.tutorial.en.7.in \ + doc/pindaiba.why.en.7.in \ + +sources.c = \ + src/catalog.c \ + src/hash.c \ + src/i18n.c \ + src/lib.c \ + src/logerr.c \ + src/random.c \ + src/testing.c \ + +src/catalog.o src/catalog.lo src/catalog.to: src/catalog.h +src/hash.o src/hash.lo src/hash.to: src/hash.h +src/i18n.o src/i18n.lo src/i18n.to: src/i18n.h +src/lib.o src/lib.lo src/lib.to: src/lib.h +src/logerr.o src/logerr.lo src/logerr.to: src/logerr.h +src/random.o src/random.lo src/random.to: src/random.h +src/testing.o src/testing.lo src/testing.to: src/testing.h + +src/catalog.xa: src/catalog.to +src/hash.xa: src/hash.to +src/i18n.xa: src/i18n.to +src/lib.xa: src/lib.to +src/logerr.xa: src/logerr.to +src/random.xa: src/random.to +src/testing.xa: src/testing.to + +src/catalog.bin-check: src/catalog.bin +src/hash.bin-check: src/hash.bin +src/i18n.bin-check: src/i18n.bin +src/lib.bin-check: src/lib.bin +src/logerr.bin-check: src/logerr.bin +src/random.bin-check: src/random.bin +src/testing.bin-check: src/testing.bin + + +src/catalog.o src/catalog.lo src/catalog.to: src/logerr.h src/testing.h src/../tests/slurp.h +src/hash.o src/hash.lo src/hash.to: src/logerr.h src/random.h src/testing.h +src/i18n.o src/i18n.lo src/i18n.to: src/logerr.h src/catalog.h +src/lib.o src/lib.lo src/lib.to: +src/logerr.o src/logerr.lo src/logerr.to: src/testing.h src/../tests/slurp.h +src/random.o src/random.lo src/random.to: src/logerr.h src/testing.h +src/testing.o src/testing.lo src/testing.to: + +src/catalog.xa: src/logerr.o src/testing.o src/../tests/slurp.o +src/hash.xa: src/logerr.o src/random.o src/testing.o +src/i18n.xa: src/logerr.o src/catalog.o +src/lib.xa: +src/logerr.xa: src/testing.o src/../tests/slurp.o +src/random.xa: src/logerr.o src/testing.o +src/testing.xa: diff --git a/description b/description new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/description diff --git a/doc/pindaiba.CHANGELOG.en.7.in b/doc/pindaiba.CHANGELOG.en.7.in new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/doc/pindaiba.CHANGELOG.en.7.in diff --git a/doc/pindaiba.README.en.7.in b/doc/pindaiba.README.en.7.in new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/doc/pindaiba.README.en.7.in diff --git a/doc/pindaiba.TODOs.en.7.in b/doc/pindaiba.TODOs.en.7.in new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/doc/pindaiba.TODOs.en.7.in diff --git a/doc/pindaiba.en.1.in b/doc/pindaiba.en.1.in new file mode 100644 index 0000000..8ff1d8c --- /dev/null +++ b/doc/pindaiba.en.1.in @@ -0,0 +1,5 @@ +.TH XABLAU 1 DATE "OIOJI" "OIJOIJ" + +.SH NAME + +woeifj diff --git a/doc/pindaiba.recipes.en.7.in b/doc/pindaiba.recipes.en.7.in new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/doc/pindaiba.recipes.en.7.in diff --git a/doc/pindaiba.tutorial.en.7.in b/doc/pindaiba.tutorial.en.7.in new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/doc/pindaiba.tutorial.en.7.in diff --git a/doc/pindaiba.why.en.7.in b/doc/pindaiba.why.en.7.in new file mode 100644 index 0000000..6cd923d --- /dev/null +++ b/doc/pindaiba.why.en.7.in @@ -0,0 +1,14 @@ +.TH WOEIJF + +My quality standard for a C library is: +- it is C, not C/C++; +- it has test; +- it uses sanitizers and fuzzing; +- it works with more than one C compiler (notably, gcc, clang, tcc, scc and + cproc); +- it has `.POSIX` as a rule in its Makefile; +- it uses make, and atmost, a ./configure script; +- it works with more than one C implementation (notably, bmake, gnumake and ?); +- it has an `install` and an `uninstall` target; + +8cc, c, chibicc, lacc, lcc diff --git a/long-description b/long-description new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/long-description diff --git a/meta.capim b/meta.capim new file mode 100644 index 0000000..0967ef4 --- /dev/null +++ b/meta.capim @@ -0,0 +1 @@ +{} diff --git a/mkdeps.sh b/mkdeps.sh new file mode 100755 index 0000000..dc1bfe7 --- /dev/null +++ b/mkdeps.sh @@ -0,0 +1,17 @@ +#!/bin/sh +set -eu + +varlist() { + printf '%s = \\\n' "$1" + sed 's|^\(.*\)$|\t\1 \\|' + printf '\n' +} + +cfiles() { + find src/*.c -not -name main.c | sort +} + +export LANG=POSIX.UTF-8 +find doc/*.en.*.in | sort | varlist 'manpages.en.in' +cfiles | varlist 'sources.c' +sh tools/cdeps.sh $(cfiles) diff --git a/src/catalog.c b/src/catalog.c new file mode 100644 index 0000000..31aef9a --- /dev/null +++ b/src/catalog.c @@ -0,0 +1,507 @@ +#include "config.h" + +#include <assert.h> +#include <errno.h> +#include <nl_types.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> + +#include "logerr.h" +#include "catalog.h" + +#ifdef TEST +#include "testing.h" +#include "../tests/slurp.h" +#endif + + +#ifdef TEST +static const char *const +FNAME = __FILE__ ".txt"; + +enum TEST_MSGCATALOG_ID { + MSG_X_FIRST = 1, + MSG_X_1, + MSG_X_2, + MSG_X_LAST, + MSG_STANDALONE, +}; + +static const char *const +TEST_MSGS[] = { + "", + [MSG_X_FIRST]="First line\n", + [MSG_X_1]="a second\n", + [MSG_X_2]="a third\n", + [MSG_X_LAST]="and the last one\n", + [MSG_STANDALONE]="single line message\n", + NULL +}; +#endif + + + +static const char *const +CATALOG_NAME = NAME; + +static nl_catd +catalog_descriptor = NULL; + +static const char *const +NLSPATH = LOCALEDIR "/%l_%t/LC_MESSAGES/%N.cat" ":" + LOCALEDIR "/%l/LC_MESSAGES/%N.cat"; + +static const char *const +NLSPATH_KEY = "NLSPATH"; + + +int +i18n_init(void) { + int rc = 0; + + static const int should_overwrite = 0; + if (setenv(NLSPATH_KEY, NLSPATH, should_overwrite)) { + logerr("setenv(\"%s\", \"%s\", 0): %s\n", NLSPATH_KEY, + NLSPATH, strerror(errno)); + rc = -1; + goto out; + } + + catalog_descriptor = catopen(CATALOG_NAME, 0); + if (catalog_descriptor && catalog_descriptor == (nl_catd)-1) { + logerr("catopen(\"%s\", 0): %s\n", CATALOG_NAME, strerror(errno)); + catalog_descriptor = NULL; + rc = -1; + goto out; + } + +out: + return rc; +} + +#ifdef TEST +static int +test_i18n_init(void) { + int rc = 0; + + test_start("i18n_init()"); + + { + testing("simple call without touching the environment"); + + const int should_overwrite = 1; + if (setenv(NLSPATH_KEY, "src/%N.en.cat", should_overwrite)) { + logerr("setenv(\"%s\", \"src/%%N.en.cat\", 1): %s\n", + NLSPATH_KEY, strerror(errno)); + rc = -1; + goto out; + } + + if (i18n_init()) { + logerr("i18n_init()\n"); + rc = -1; + goto out; + } + + test_ok(); + } + +out: + if (i18n_destroy()) { + logerr("i18n_destroy()\n"); + rc = -1; + } + return rc; +} +#endif + + +int +i18n_destroy(void) { + int rc = 0; + + if (catalog_descriptor) { + if (catclose(catalog_descriptor)) { + logerr("catclose(...): %s\n", strerror(errno)); + rc = -1; + goto out; + } + } + +out: + if (catalog_descriptor) { + catalog_descriptor = NULL; + } + return rc; +} + +#ifdef TEST +static int +test_i18n_destroy(void) { + int rc = 0; + + test_start("i18n_destroy()"); + + { + testing("simple call without init first"); + + if (i18n_destroy()) { + logerr("i18n_destroy()\n"); + rc = -1; + goto out; + } + + test_ok(); + } + +out: + return rc; +} +#endif + + +/** + * Infallible: always returns a valid string, no matter what. + */ +const char * +s(const char* const MSGS[], const int msg_id) { + assert(msg_id > 0); + // FIXME: assert within bounds! + // printf("sizeof(MSGS): %ld\n", sizeof(MSGS)); + if (!catalog_descriptor) { + return MSGS[msg_id]; + } + + errno = 0; + const char *const ret = + catgets(catalog_descriptor, NL_SETD, msg_id, MSGS[msg_id]); + if (errno) { + logerr("catgets(%d): %s\n", msg_id, strerror(errno)); + } + + return ret; +} + +#ifdef TEST +static int +test_s(void) { + int rc = 0; + + test_start("_()"); + FILE *file = NULL; + char *str = NULL; + + { + testing("empty string"); + + file = fopen(FNAME, "w"); + if (!file) { + perror("fopen(FNAME, \"w\")"); + rc = -1; + goto out; + } + + // FIXME: implement correct test + + + + test_ok(); + } + +out: + if (str) { + free(str); + } + if (file) { + if (fclose(file)) { + logerr("fclose(file): %s\n", strerror(errno)); + rc = -1; + } + } + return rc; +} +#endif + + +int +s_print_msgs( + const char *const MSGS[], + FILE *restrict stream, + const int msg_begin, + const int msg_end +) { + int rc = 0; + + for (int i = msg_begin; i <= msg_end; i++) { + if (fprintf(stream, "%s", s(MSGS, i)) < 0) { + logerr("fprintf(stream, \"%%s\", _(%d)): %s\n", i, + strerror(errno)); + rc = -1; + goto out; + } + } + +out: + return rc; +} + +#ifdef TEST +static int +test_s_print_msgs(void) { + int rc = 0; + + test_start("s_print_msgs()"); + FILE *file = NULL; + char *str = NULL; + + { + testing("message in range"); + + file = fopen(FNAME, "w"); + if (!file) { + perror("fopen(FNAME, \"w\")"); + rc = -1; + goto out; + } + + if (s_print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_LAST)) { + logerr("print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_LAST)\n"); + rc = -1; + goto out; + } + + const int ret = fclose(file); + file = NULL; + if (ret) { + logerr("fclose(file): %s\n", strerror(errno)); + rc = -1; + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + logerr("slurp_for_tests(FNAME, &str)\n"); + rc = -1; + goto out; + } + + const char *const expected = + "First line\n" + "a second\n" + "a third\n" + "and the last one\n" + ; + + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; + + test_ok(); + } + { + testing("range begin and end is the same"); + + file = fopen(FNAME, "w"); + if (!file) { + logerr("fopen(FNAME, \"w\"): %s\n", strerror(errno)); + rc = -1; + goto out; + } + + if (s_print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_FIRST)) { + logerr("s_print_msgs(TEST_MSGS, file, MSG_X_FIRST, MSG_X_FIRST)\n"); + rc = -1; + goto out; + } + + const int ret = fclose(file); + file = NULL; + if (ret) { + logerr("fclose(file): %s\n", strerror(errno)); + rc = -1; + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + logerr("slurp_for_tests(FNAME, &str)\n"); + rc = -1; + goto out; + } + + const char *const expected = + "First line\n"; + + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; + + test_ok(); + } + +out: + if (str) { + free(str); + } + if (file) { + if (fclose(file)) { + logerr("fclose(file): %s\n", strerror(errno)); + rc = -1; + } + } + return rc; +} +#endif + + +int +s_print_msg(const char *const MSGS[], FILE *const fd, const int msg_id) { + return s_print_msgs(MSGS, fd, msg_id, msg_id); +} + +#ifdef TEST +static int +test_s_print_msg(void) { + int rc = 0; + + test_start("s_print_msg()"); + FILE *file = NULL; + char *str = NULL; + + { + testing("simple individual message"); + + file = fopen(FNAME, "w"); + if (!file) { + logerr("fopen(FNAME, \"w\"): %s\n"); + rc = -1; + goto out; + } + + if (s_print_msg(TEST_MSGS, file, MSG_STANDALONE)) { + logerr("s_print_msg(TEST_MSGS, file, MSG_STANDALONE)\n"); + rc = -1; + goto out; + } + + const int ret = fclose(file); + file = NULL; + if (ret) { + logerr("fopen(file): %s\n", strerror(errno)); + rc = -1; + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + logerr("slurp_for_tests(FNAME, &str)\n"); + rc = -1; + goto out; + } + + const char *const expected = + "single line message\n"; + + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; + + test_ok(); + } + +out: + if (str) { + free(str); + } + if (file) { + if (fclose(file)) { + logerr("fclose(file): %s\n", strerror(errno)); + rc = -1; + } + } + return rc; +} +#endif + + +int +dump_translatable_strings(const char *const MSGS[]) { + int rc = 0; + + for (size_t i = 1; MSGS[i]; i++) { + if (printf("%ld ", i) < 0) { + logerr("printf(\"%%ld\", %d): %s\n", i); + rc = -1; + goto out; + } + + for (size_t j = 0; MSGS[i][j]; j++) { + if (MSGS[i][j] == '\n') { + if (printf("\\n") < 0) { + logerr("printf(\"\\\\n\"): %s\n", + strerror(errno)); + rc = -1; + goto out; + } + } else { + if (printf("%c", MSGS[i][j]) < 0) { + logerr("printf(\"%%c\", " + "MSGS[%ld][%ld]): %s\n", + i, j, strerror(errno)); + rc = -1; + goto out; + } + } + } + + if (printf("\n\n") < 0) { + logerr("printf(\"\\n\\n\"): %s\n", strerror(errno)); + rc = -1; + goto out; + } + } + +out: + return rc; +} + +#ifdef TEST +int +main(void) { + int rc = 0; + + if (test_i18n_init()) { + logerr("test_i18n_init()\n"); + rc = -1; + goto out; + } + + if (test_i18n_destroy()) { + logerr("test_i18n_destroy()\n"); + rc = -1; + goto out; + } + + if (test_s()) { + logerr("test_s()\n"); + rc = -1; + goto out; + } + + if (test_s_print_msgs()) { + logerr("test_s_print_msgs()\n"); + rc = -1; + goto out; + } + + if (test_s_print_msg()) { + logerr("test_s_print_msg()\n"); + rc = -1; + goto out; + } + +out: + return !!rc; +} +#endif diff --git a/src/catalog.h b/src/catalog.h new file mode 100644 index 0000000..25adbeb --- /dev/null +++ b/src/catalog.h @@ -0,0 +1,22 @@ +int +i18n_init(void); + +int +i18n_destroy(void); + +const char * +s(const char *const MSGS[], const int msg_id); + +int +s_print_msgs( + const char *const MSGS[], + FILE *restrict stream, + const int msg_begin, + const int msg_end +); + +int +s_print_msg(const char *const MSGS[], FILE *restrict stream, const int msg_id); + +int +dump_translatable_strings(const char *const MSGS[]); diff --git a/src/config.h.in b/src/config.h.in new file mode 100644 index 0000000..ab2256e --- /dev/null +++ b/src/config.h.in @@ -0,0 +1,6 @@ +#define _XOPEN_SOURCE 700 +#define _POSIX_C_SOURCE 200809L +#define VERSION "@VERSION@" +#define DATE "@DATE@" +#define NAME "@NAME@" +#define LOCALEDIR "@LOCALEDIR@" diff --git a/src/hash.c b/src/hash.c new file mode 100644 index 0000000..c8fd5d6 --- /dev/null +++ b/src/hash.c @@ -0,0 +1,644 @@ +#include "config.h" + +#include <assert.h> +#include <stdbool.h> +#include <stdio.h> +#include <stdint.h> +#include <string.h> +#include <pthread.h> + +#include "logerr.h" +#include "random.h" +#include "hash.h" + +#ifdef TEST +#include "testing.h" +#endif + + +// FIXME: verify that this is the correct type of mutex for the use case +static pthread_mutex_t +SEED_INIT_MUTEX = PTHREAD_MUTEX_INITIALIZER; + +#define PRIV_SEED_LENGTH 16 + +struct Seed { + bool initialized; + uint8_t bytes[PRIV_SEED_LENGTH]; +}; + +static struct Seed +SEED = { + .initialized = false, + .bytes = { 0 }, +}; + + +int +hash_init(void) { + int rc = 0; + + int ret = 0; + struct Seed seed = { 0 }; + + if (SEED.initialized) { + goto out; + } + + if (urandom_bytes(sizeof(seed.bytes), &seed.bytes)) { + logerr("urandom_bytes(sizeof(seed.bytes), &seed.bytes);\n"); + rc = -1; + goto out; + } + + ret = pthread_mutex_lock(&SEED_INIT_MUTEX); + if (ret) { + logerr( + "pthread_mutex_lock(&SEED_INIT_MUTEX): %s;\n", + strerror(ret) + ); + rc = -1; + goto out; + } + + /* + In case where the seed was initialized by another thread while the + current was enterinig hash_init() and locking the mutex. The + previous check is only a best-effort one to avoid all threads that + start at the same time to serially acquire the mutex and do nothing + with. In order to sometimes avoid the extra overhead, the previous + check exists, but only the following check that has any correctness + guarantee, and instead of being best-effort it blocks others so it + can make the correct decision. + */ + if (SEED.initialized) { + goto out; + } + + // FIXME: memcpy? + const uint8_t length = sizeof(SEED.bytes) / sizeof(SEED.bytes[0]); + for (uint8_t i = 0; i < length; i++) { + SEED.bytes[i] = seed.bytes[i]; + } + + SEED.initialized = true; + +out: + // FIXME: verify that unlocking an unlocked mutex is fine + ret = pthread_mutex_unlock(&SEED_INIT_MUTEX); + if (ret) { + logerr( + "pthread_mutex_unlock(&SEED_INIT_MUTEX): %s\n", + strerror(ret) + ); + rc = -1; + } + return rc; +} + +#ifdef TEST +static void * +hash_init_thread(void *data) { + int *ret = (int *)data; + *ret = hash_init(); + pthread_exit(NULL); + return NULL; +} + +static int +test_hash_init(void) { + int rc = 0; + + test_start("hash_init()"); + + const uint8_t length = sizeof(SEED.bytes) / sizeof(SEED.bytes[0]); + + assert(length == 16); + for (uint8_t i = 0; i < length; i++) { + assert(SEED.bytes[i] == 0); + } + + pthread_mutex_t test_mutex = PTHREAD_MUTEX_INITIALIZER; + int ret; + + { + testing("when is initialized we do nothing"); + + SEED.initialized = true; + assert(hash_init() == 0); + + for (uint8_t i = 0; i < length; i++) { + assert(SEED.bytes[i] == 0); + } + + test_ok(); + } + + { + testing("do nothing when initialized on the second check"); + + /* + 0. start with an unitialized seed + 1. grab the lock + 2. start the other thread + 3. set SEED_INITIALIZED + 4. release the lock + 5. join the other thread + 6. assert HASH_SEED stilll empty + */ + + SEED.initialized = false; // 0 + ret = pthread_mutex_lock(&test_mutex); // 1 + if (ret) { + logerr( + "pthread_mutex_lock(&test_mutex): %s;\n", + strerror(ret) + ); + rc = -1; + goto out; + } + + pthread_t t; + int t_rc = -1; + ret = pthread_create(&t, NULL, hash_init_thread, &t_rc); // 2 + if (ret) { + logerr("pthread_create(%s, %s, %s, %s): %s\n", + "&t", + "NULL", + "hash_init", + "&t_rc", + strerror(ret)); + rc = -1; + goto out; + } + + SEED.initialized = true; // 3 + ret = pthread_mutex_unlock(&test_mutex); // 4 + if (ret) { + logerr( + "pthread_mutex_unlock(&test_mutex): %s\n", + strerror(ret) + ); + rc = -1; + goto out; + } + + ret = pthread_join(t, NULL); // 5 + if (ret) { + logerr("pthread_join(t, &rc): %s\n", strerror(ret)); + rc = -1; + goto out; + } + + if (t_rc) { + logerr("hash_init() in pthread_t\n"); + rc = -1; + goto out; + } + + for (size_t i = 0; i < length; i++) { + assert(SEED.bytes[i] == 0); // 6 + } + + test_ok(); + } + + { + testing("competing threads can init the seed besides us"); + + /* + 0. start with an unitialized seed + 1. grab the lock + 2. start the other 2 threads + 3. release the lock + 4. join the other threads + 5. assert(SEED_INITIALIZED == true); + */ + + SEED.initialized = false; // 0 + ret = pthread_mutex_lock(&test_mutex); // 1 + if (ret) { + logerr( + "pthread_mutex_lock(&SEED_INIT_MUTEX): %s;\n", + strerror(ret) + ); + rc = -1; + goto out; + } + + pthread_t t1; + int t1_rc = -1; + ret = pthread_create(&t1, NULL, hash_init_thread, &t1_rc); // 2 + if (ret) { + logerr("pthread_create(%s, %s, %s, %s): %s\n", + "&t1", + "NULL", + "hash_init", + "&t1_rc", + strerror(ret)); + rc = -1; + goto out; + } + + pthread_t t2; + int t2_rc = -1; + ret = pthread_create(&t2, NULL, hash_init_thread, &t2_rc); // 2 + if (ret) { + logerr("pthread_create(%s, %s, %s, %s): %s\n", + "&t2", + "NULL", + "hash_init", + "&t2_rc", + strerror(ret)); + rc = -1; + goto out; + } + + ret = pthread_mutex_unlock(&test_mutex); // 3 + if (ret) { + logerr( + "pthread_mutex_unlock(&test_mutex): %s\n", + strerror(ret) + ); + rc = -1; + goto out; + } + + ret = pthread_join(t1, NULL); // 4 + if (ret) { + logerr("pthread_join(t1, &rc): %s\n", strerror(ret)); + rc = -1; + goto out; + } + + if (t1_rc) { + logerr("hash_init() in pthread_t t1\n"); + rc = -1; + goto out; + } + + ret = pthread_join(t2, NULL); // 4 + if (ret) { + logerr("pthread_join(t2, &rc): %s\n", strerror(ret)); + rc = -1; + goto out; + } + + if (t2_rc) { + logerr("hash_init() in pthread_t t2\n"); + rc = -1; + goto out; + } + + assert(SEED.initialized == true); // 5 + + test_ok(); + } + +out: + ret = pthread_mutex_unlock(&test_mutex); + if (ret) { + logerr( + "pthread_mutex_unlock(&test_mutex): %s\n", + strerror(ret) + ); + rc = -1; + } + return rc; +} +#endif + + +#define ROTL(x, b) (uint64_t)(((x) << (b)) | ((x) >> (64 - (b)))) + +#define U32TO8_LE(p, v) \ + (p)[0] = (uint8_t)((v)); \ + (p)[1] = (uint8_t)((v) >> 8); \ + (p)[2] = (uint8_t)((v) >> 16); \ + (p)[3] = (uint8_t)((v) >> 24); + +#define U64TO8_LE(p, v) \ + U32TO8_LE((p), (uint32_t)((v))); \ + U32TO8_LE((p) + 4, (uint32_t)((v) >> 32)); + +#define U8TO64_LE(p) \ + (((uint64_t)((p)[0])) | ((uint64_t)((p)[1]) << 8) | \ + ((uint64_t)((p)[2]) << 16) | ((uint64_t)((p)[3]) << 24) | \ + ((uint64_t)((p)[4]) << 32) | ((uint64_t)((p)[5]) << 40) | \ + ((uint64_t)((p)[6]) << 48) | ((uint64_t)((p)[7]) << 56)) + +#define SIPROUND \ + do { \ + v0 += v1; \ + v1 = ROTL(v1, 13); \ + v1 ^= v0; \ + v0 = ROTL(v0, 32); \ + v2 += v3; \ + v3 = ROTL(v3, 16); \ + v3 ^= v2; \ + v0 += v3; \ + v3 = ROTL(v3, 21); \ + v3 ^= v0; \ + v2 += v1; \ + v1 = ROTL(v1, 17); \ + v1 ^= v2; \ + v2 = ROTL(v2, 32); \ + } while (0) + +/** + * Computes a SipHash value + * [in] key: pointer to the key data (read-only), must be 16 bytes + * [in] input: pointer to input data (read-only) + * [in] inlen: input data length in bytes (any size_t value) + * [out] out: pointer to output data (write-only), 16 bytes must be allocated + * + * @tags infallible +*/ +static void +sip_hash( + const uint8_t *const key, + const size_t inlen, + const void *const input, + uint8_t out[OUTPUT_LENGTH] +) { + static const int cROUNDS = 2; + static const int dROUNDS = 4; + + const unsigned char *ni = (const unsigned char *)input; + const unsigned char *kk = (const unsigned char *)key; + + uint64_t v0 = UINT64_C(0x736f6d6570736575); + uint64_t v1 = UINT64_C(0x646f72616e646f6d); + uint64_t v2 = UINT64_C(0x6c7967656e657261); + uint64_t v3 = UINT64_C(0x7465646279746573); + uint64_t k0 = U8TO64_LE(kk); /// Here we are relying on `key` to + uint64_t k1 = U8TO64_LE(kk + 8); /// be 16 bytes long. + uint64_t m; + int i; + const unsigned char *end = ni + inlen - (inlen % sizeof(uint64_t)); + const int left = inlen & 7; + uint64_t b = ((uint64_t)inlen) << 56; + v3 ^= k1; + v2 ^= k0; + v1 ^= k1; + v0 ^= k0; + + v1 ^= 0xee; + + for (; ni != end; ni += 8) { + m = U8TO64_LE(ni); + v3 ^= m; + + for (i = 0; i < cROUNDS; ++i) { + SIPROUND; + } + + v0 ^= m; + } + + switch (left) { + case 7: + b |= ((uint64_t)ni[6]) << 48; + /* fall-through */ + case 6: + b |= ((uint64_t)ni[5]) << 40; + /* fall-through */ + case 5: + b |= ((uint64_t)ni[4]) << 32; + /* fall-through */ + case 4: + b |= ((uint64_t)ni[3]) << 24; + /* fall-through */ + case 3: + b |= ((uint64_t)ni[2]) << 16; + /* fall-through */ + case 2: + b |= ((uint64_t)ni[1]) << 8; + /* fall-through */ + case 1: + b |= ((uint64_t)ni[0]); + break; + case 0: + break; + } + + v3 ^= b; + + for (i = 0; i < cROUNDS; ++i) { + SIPROUND; + } + + v0 ^= b; + + v2 ^= 0xee; + + for (i = 0; i < dROUNDS; ++i) { + SIPROUND; + } + + b = v0 ^ v1 ^ v2 ^ v3; + U64TO8_LE(out, b); + + v1 ^= 0xdd; + + for (i = 0; i < dROUNDS; ++i) { + SIPROUND; + } + + b = v0 ^ v1 ^ v2 ^ v3; + U64TO8_LE(out + 8, b); + + return; +} + +#ifdef TEST +static const uint8_t +TEST_VECTORS[64][OUTPUT_LENGTH] = { + { 0xa3, 0x81, 0x7f, 0x04, 0xba, 0x25, 0xa8, 0xe6, 0x6d, 0xf6, 0x72, 0x14, 0xc7, 0x55, 0x02, 0x93 }, + { 0xda, 0x87, 0xc1, 0xd8, 0x6b, 0x99, 0xaf, 0x44, 0x34, 0x76, 0x59, 0x11, 0x9b, 0x22, 0xfc, 0x45 }, + { 0x81, 0x77, 0x22, 0x8d, 0xa4, 0xa4, 0x5d, 0xc7, 0xfc, 0xa3, 0x8b, 0xde, 0xf6, 0x0a, 0xff, 0xe4 }, + { 0x9c, 0x70, 0xb6, 0x0c, 0x52, 0x67, 0xa9, 0x4e, 0x5f, 0x33, 0xb6, 0xb0, 0x29, 0x85, 0xed, 0x51 }, + { 0xf8, 0x81, 0x64, 0xc1, 0x2d, 0x9c, 0x8f, 0xaf, 0x7d, 0x0f, 0x6e, 0x7c, 0x7b, 0xcd, 0x55, 0x79 }, + { 0x13, 0x68, 0x87, 0x59, 0x80, 0x77, 0x6f, 0x88, 0x54, 0x52, 0x7a, 0x07, 0x69, 0x0e, 0x96, 0x27 }, + { 0x14, 0xee, 0xca, 0x33, 0x8b, 0x20, 0x86, 0x13, 0x48, 0x5e, 0xa0, 0x30, 0x8f, 0xd7, 0xa1, 0x5e }, + { 0xa1, 0xf1, 0xeb, 0xbe, 0xd8, 0xdb, 0xc1, 0x53, 0xc0, 0xb8, 0x4a, 0xa6, 0x1f, 0xf0, 0x82, 0x39 }, + { 0x3b, 0x62, 0xa9, 0xba, 0x62, 0x58, 0xf5, 0x61, 0x0f, 0x83, 0xe2, 0x64, 0xf3, 0x14, 0x97, 0xb4 }, + { 0x26, 0x44, 0x99, 0x06, 0x0a, 0xd9, 0xba, 0xab, 0xc4, 0x7f, 0x8b, 0x02, 0xbb, 0x6d, 0x71, 0xed }, + { 0x00, 0x11, 0x0d, 0xc3, 0x78, 0x14, 0x69, 0x56, 0xc9, 0x54, 0x47, 0xd3, 0xf3, 0xd0, 0xfb, 0xba }, + { 0x01, 0x51, 0xc5, 0x68, 0x38, 0x6b, 0x66, 0x77, 0xa2, 0xb4, 0xdc, 0x6f, 0x81, 0xe5, 0xdc, 0x18 }, + { 0xd6, 0x26, 0xb2, 0x66, 0x90, 0x5e, 0xf3, 0x58, 0x82, 0x63, 0x4d, 0xf6, 0x85, 0x32, 0xc1, 0x25 }, + { 0x98, 0x69, 0xe2, 0x47, 0xe9, 0xc0, 0x8b, 0x10, 0xd0, 0x29, 0x93, 0x4f, 0xc4, 0xb9, 0x52, 0xf7 }, + { 0x31, 0xfc, 0xef, 0xac, 0x66, 0xd7, 0xde, 0x9c, 0x7e, 0xc7, 0x48, 0x5f, 0xe4, 0x49, 0x49, 0x02 }, + { 0x54, 0x93, 0xe9, 0x99, 0x33, 0xb0, 0xa8, 0x11, 0x7e, 0x08, 0xec, 0x0f, 0x97, 0xcf, 0xc3, 0xd9 }, + { 0x6e, 0xe2, 0xa4, 0xca, 0x67, 0xb0, 0x54, 0xbb, 0xfd, 0x33, 0x15, 0xbf, 0x85, 0x23, 0x05, 0x77 }, + { 0x47, 0x3d, 0x06, 0xe8, 0x73, 0x8d, 0xb8, 0x98, 0x54, 0xc0, 0x66, 0xc4, 0x7a, 0xe4, 0x77, 0x40 }, + { 0xa4, 0x26, 0xe5, 0xe4, 0x23, 0xbf, 0x48, 0x85, 0x29, 0x4d, 0xa4, 0x81, 0xfe, 0xae, 0xf7, 0x23 }, + { 0x78, 0x01, 0x77, 0x31, 0xcf, 0x65, 0xfa, 0xb0, 0x74, 0xd5, 0x20, 0x89, 0x52, 0x51, 0x2e, 0xb1 }, + { 0x9e, 0x25, 0xfc, 0x83, 0x3f, 0x22, 0x90, 0x73, 0x3e, 0x93, 0x44, 0xa5, 0xe8, 0x38, 0x39, 0xeb }, + { 0x56, 0x8e, 0x49, 0x5a, 0xbe, 0x52, 0x5a, 0x21, 0x8a, 0x22, 0x14, 0xcd, 0x3e, 0x07, 0x1d, 0x12 }, + { 0x4a, 0x29, 0xb5, 0x45, 0x52, 0xd1, 0x6b, 0x9a, 0x46, 0x9c, 0x10, 0x52, 0x8e, 0xff, 0x0a, 0xae }, + { 0xc9, 0xd1, 0x84, 0xdd, 0xd5, 0xa9, 0xf5, 0xe0, 0xcf, 0x8c, 0xe2, 0x9a, 0x9a, 0xbf, 0x69, 0x1c }, + { 0x2d, 0xb4, 0x79, 0xae, 0x78, 0xbd, 0x50, 0xd8, 0x88, 0x2a, 0x8a, 0x17, 0x8a, 0x61, 0x32, 0xad }, + { 0x8e, 0xce, 0x5f, 0x04, 0x2d, 0x5e, 0x44, 0x7b, 0x50, 0x51, 0xb9, 0xea, 0xcb, 0x8d, 0x8f, 0x6f }, + { 0x9c, 0x0b, 0x53, 0xb4, 0xb3, 0xc3, 0x07, 0xe8, 0x7e, 0xae, 0xe0, 0x86, 0x78, 0x14, 0x1f, 0x66 }, + { 0xab, 0xf2, 0x48, 0xaf, 0x69, 0xa6, 0xea, 0xe4, 0xbf, 0xd3, 0xeb, 0x2f, 0x12, 0x9e, 0xeb, 0x94 }, + { 0x06, 0x64, 0xda, 0x16, 0x68, 0x57, 0x4b, 0x88, 0xb9, 0x35, 0xf3, 0x02, 0x73, 0x58, 0xae, 0xf4 }, + { 0xaa, 0x4b, 0x9d, 0xc4, 0xbf, 0x33, 0x7d, 0xe9, 0x0c, 0xd4, 0xfd, 0x3c, 0x46, 0x7c, 0x6a, 0xb7 }, + { 0xea, 0x5c, 0x7f, 0x47, 0x1f, 0xaf, 0x6b, 0xde, 0x2b, 0x1a, 0xd7, 0xd4, 0x68, 0x6d, 0x22, 0x87 }, + { 0x29, 0x39, 0xb0, 0x18, 0x32, 0x23, 0xfa, 0xfc, 0x17, 0x23, 0xde, 0x4f, 0x52, 0xc4, 0x3d, 0x35 }, + { 0x7c, 0x39, 0x56, 0xca, 0x5e, 0xea, 0xfc, 0x3e, 0x36, 0x3e, 0x9d, 0x55, 0x65, 0x46, 0xeb, 0x68 }, + { 0x77, 0xc6, 0x07, 0x71, 0x46, 0xf0, 0x1c, 0x32, 0xb6, 0xb6, 0x9d, 0x5f, 0x4e, 0xa9, 0xff, 0xcf }, + { 0x37, 0xa6, 0x98, 0x6c, 0xb8, 0x84, 0x7e, 0xdf, 0x09, 0x25, 0xf0, 0xf1, 0x30, 0x9b, 0x54, 0xde }, + { 0xa7, 0x05, 0xf0, 0xe6, 0x9d, 0xa9, 0xa8, 0xf9, 0x07, 0x24, 0x1a, 0x2e, 0x92, 0x3c, 0x8c, 0xc8 }, + { 0x3d, 0xc4, 0x7d, 0x1f, 0x29, 0xc4, 0x48, 0x46, 0x1e, 0x9e, 0x76, 0xed, 0x90, 0x4f, 0x67, 0x11 }, + { 0x0d, 0x62, 0xbf, 0x01, 0xe6, 0xfc, 0x0e, 0x1a, 0x0d, 0x3c, 0x47, 0x51, 0xc5, 0xd3, 0x69, 0x2b }, + { 0x8c, 0x03, 0x46, 0x8b, 0xca, 0x7c, 0x66, 0x9e, 0xe4, 0xfd, 0x5e, 0x08, 0x4b, 0xbe, 0xe7, 0xb5 }, + { 0x52, 0x8a, 0x5b, 0xb9, 0x3b, 0xaf, 0x2c, 0x9c, 0x44, 0x73, 0xcc, 0xe5, 0xd0, 0xd2, 0x2b, 0xd9 }, + { 0xdf, 0x6a, 0x30, 0x1e, 0x95, 0xc9, 0x5d, 0xad, 0x97, 0xae, 0x0c, 0xc8, 0xc6, 0x91, 0x3b, 0xd8 }, + { 0x80, 0x11, 0x89, 0x90, 0x2c, 0x85, 0x7f, 0x39, 0xe7, 0x35, 0x91, 0x28, 0x5e, 0x70, 0xb6, 0xdb }, + { 0xe6, 0x17, 0x34, 0x6a, 0xc9, 0xc2, 0x31, 0xbb, 0x36, 0x50, 0xae, 0x34, 0xcc, 0xca, 0x0c, 0x5b }, + { 0x27, 0xd9, 0x34, 0x37, 0xef, 0xb7, 0x21, 0xaa, 0x40, 0x18, 0x21, 0xdc, 0xec, 0x5a, 0xdf, 0x89 }, + { 0x89, 0x23, 0x7d, 0x9d, 0xed, 0x9c, 0x5e, 0x78, 0xd8, 0xb1, 0xc9, 0xb1, 0x66, 0xcc, 0x73, 0x42 }, + { 0x4a, 0x6d, 0x80, 0x91, 0xbf, 0x5e, 0x7d, 0x65, 0x11, 0x89, 0xfa, 0x94, 0xa2, 0x50, 0xb1, 0x4c }, + { 0x0e, 0x33, 0xf9, 0x60, 0x55, 0xe7, 0xae, 0x89, 0x3f, 0xfc, 0x0e, 0x3d, 0xcf, 0x49, 0x29, 0x02 }, + { 0xe6, 0x1c, 0x43, 0x2b, 0x72, 0x0b, 0x19, 0xd1, 0x8e, 0xc8, 0xd8, 0x4b, 0xdc, 0x63, 0x15, 0x1b }, + { 0xf7, 0xe5, 0xae, 0xf5, 0x49, 0xf7, 0x82, 0xcf, 0x37, 0x90, 0x55, 0xa6, 0x08, 0x26, 0x9b, 0x16 }, + { 0x43, 0x8d, 0x03, 0x0f, 0xd0, 0xb7, 0xa5, 0x4f, 0xa8, 0x37, 0xf2, 0xad, 0x20, 0x1a, 0x64, 0x03 }, + { 0xa5, 0x90, 0xd3, 0xee, 0x4f, 0xbf, 0x04, 0xe3, 0x24, 0x7e, 0x0d, 0x27, 0xf2, 0x86, 0x42, 0x3f }, + { 0x5f, 0xe2, 0xc1, 0xa1, 0x72, 0xfe, 0x93, 0xc4, 0xb1, 0x5c, 0xd3, 0x7c, 0xae, 0xf9, 0xf5, 0x38 }, + { 0x2c, 0x97, 0x32, 0x5c, 0xbd, 0x06, 0xb3, 0x6e, 0xb2, 0x13, 0x3d, 0xd0, 0x8b, 0x3a, 0x01, 0x7c }, + { 0x92, 0xc8, 0x14, 0x22, 0x7a, 0x6b, 0xca, 0x94, 0x9f, 0xf0, 0x65, 0x9f, 0x00, 0x2a, 0xd3, 0x9e }, + { 0xdc, 0xe8, 0x50, 0x11, 0x0b, 0xd8, 0x32, 0x8c, 0xfb, 0xd5, 0x08, 0x41, 0xd6, 0x91, 0x1d, 0x87 }, + { 0x67, 0xf1, 0x49, 0x84, 0xc7, 0xda, 0x79, 0x12, 0x48, 0xe3, 0x2b, 0xb5, 0x92, 0x25, 0x83, 0xda }, + { 0x19, 0x38, 0xf2, 0xcf, 0x72, 0xd5, 0x4e, 0xe9, 0x7e, 0x94, 0x16, 0x6f, 0xa9, 0x1d, 0x2a, 0x36 }, + { 0x74, 0x48, 0x1e, 0x96, 0x46, 0xed, 0x49, 0xfe, 0x0f, 0x62, 0x24, 0x30, 0x16, 0x04, 0x69, 0x8e }, + { 0x57, 0xfc, 0xa5, 0xde, 0x98, 0xa9, 0xd6, 0xd8, 0x00, 0x64, 0x38, 0xd0, 0x58, 0x3d, 0x8a, 0x1d }, + { 0x9f, 0xec, 0xde, 0x1c, 0xef, 0xdc, 0x1c, 0xbe, 0xd4, 0x76, 0x36, 0x74, 0xd9, 0x57, 0x53, 0x59 }, + { 0xe3, 0x04, 0x0c, 0x00, 0xeb, 0x28, 0xf1, 0x53, 0x66, 0xca, 0x73, 0xcb, 0xd8, 0x72, 0xe7, 0x40 }, + { 0x76, 0x97, 0x00, 0x9a, 0x6a, 0x83, 0x1d, 0xfe, 0xcc, 0xa9, 0x1c, 0x59, 0x93, 0x67, 0x0f, 0x7a }, + { 0x58, 0x53, 0x54, 0x23, 0x21, 0xf5, 0x67, 0xa0, 0x05, 0xd5, 0x47, 0xa4, 0xf0, 0x47, 0x59, 0xbd }, + { 0x51, 0x50, 0xd1, 0x77, 0x2f, 0x50, 0x83, 0x4a, 0x50, 0x3e, 0x06, 0x9a, 0x97, 0x3f, 0xbd, 0x7c }, +}; + +/** + * @tags infallible +*/ +static void +test_sip_hash(void) { + test_start("sip_hash()"); + + { + testing("upstream test vectors"); + + const size_t LEN = 64; + uint8_t in[64 /* LEN */]; + + uint8_t out[OUTPUT_LENGTH]; + struct Seed key; + + const uint8_t length = sizeof(SEED.bytes) / sizeof(SEED.bytes[0]); + for (uint8_t i = 0; i < length; ++i) { + key.bytes[i] = i; + } + + for (uint8_t i = 0; i < LEN; ++i) { + in[i] = i; + } + + for (uint8_t i = 0; i < LEN; ++i) { + sip_hash(key.bytes, i, in, out); + for (uint8_t j = 0; j < OUTPUT_LENGTH; j++) { + assert(out[j] == TEST_VECTORS[i][j]); + } + } + + test_ok(); + } + + return; +} +#endif + + +/** + * @tags infallible +*/ +void +hash(const size_t len, const void *const in, uint8_t out[OUTPUT_LENGTH]) { + assert(SEED.initialized && "Missing call to hash_init() before using hash()."); + sip_hash(SEED.bytes, len, in, out); + return; +} + +#ifdef TEST +/** + * @tags infallible +*/ +static void +test_hash(void) { + test_start("hash()"); + + { + testing("we get the same hash given the same input"); + + uint8_t out1[OUTPUT_LENGTH] = { 0 }; + uint8_t out2[OUTPUT_LENGTH] = { 0 }; + + const char *const input_str = "the input"; + const uint8_t *const input = (uint8_t *)input_str; + const size_t input_length = strlen(input_str); + + hash(input_length, input, out1); + hash(input_length, input, out2); + + for (int i = 0; i < OUTPUT_LENGTH; i++) { + assert(out1[i] == out2[i]); + } + + test_ok(); + } + + { + testing("hashing twice overwrites whatever was in `out`"); + + uint8_t out1[OUTPUT_LENGTH]; + uint8_t out2[OUTPUT_LENGTH]; + + const char *const input_str = "another input"; + const uint8_t *const input = (uint8_t *)input_str; + const size_t input_length = strlen(input_str); + + hash(6, (uint8_t *)"before", out1); + hash(input_length, input, out1); + hash(input_length, input, out2); + + for (int i = 0; i < OUTPUT_LENGTH; i++) { + assert(out1[i] == out2[i]); + } + + test_ok(); + } + + return; +} +#endif + + +#ifdef TEST +int +main(void) { + int rc = 0; + + if (test_hash_init()) { + logerr("test_hash_init();\n"); + rc = -1; + goto out; + } + + test_sip_hash(); + test_hash(); + +out: + return !!rc; +} +#endif diff --git a/src/hash.h b/src/hash.h new file mode 100644 index 0000000..bdc6cbd --- /dev/null +++ b/src/hash.h @@ -0,0 +1,8 @@ +#define OUTPUT_LENGTH 16 + + +int +hash_init(void); + +void +hash(const size_t in_len, const void *const in, uint8_t out[OUTPUT_LENGTH]); diff --git a/src/i18n.c b/src/i18n.c new file mode 100644 index 0000000..60d900a --- /dev/null +++ b/src/i18n.c @@ -0,0 +1,74 @@ +#include "config.h" + +#include <stdio.h> + +#include "i18n.h" + +#ifdef TEST +#include <stdlib.h> + +#include "logerr.h" +#include "catalog.h" +#endif + +const char *const +MSGS[] = { + "", + [MSG_USAGE_FIRST]="Usage:\n", + [MSG_USAGE_1]=" " NAME " -p FILE [-o DIRECTORY]\n", + [MSG_USAGE_2]=" " NAME " -l FILE [-o DIRECTORY]\n", + [MSG_USAGE_LAST]=" " NAME " [-hV]\n", + [MSG_HELP_FIRST]="\n", + [MSG_HELP_1]="\n", + [MSG_HELP_2]="Options:\n", + [MSG_HELP_3]=" -p FILE parser file to be processed\n", + [MSG_HELP_4]=" -l FILE lexer file to be processed\n", + [MSG_HELP_5]=" -o DIRECTORY output where to place the\n", + [MSG_HELP_6]=" generated files (default .)\n", + [MSG_HELP_7]=" -h, --help show this help message\n", + [MSG_HELP_8]=" -V, --version print the version number\n", + [MSG_HELP_9]="\n", + [MSG_HELP_10]="\n", + [MSG_HELP_11]="Run the " NAME "(1) parser program.\n", + [MSG_HELP_12]="\n", + [MSG_HELP_13]="Here is the explanation for what it does, and the synopsis\n", + [MSG_HELP_14]="of its usage.\n", + [MSG_HELP_15]="\n", + [MSG_HELP_16]="See \"man " NAME "\" for usage information and\n", + [MSG_HELP_17]="\"man " NAME ".tutorial\" for a beginner introduction.\n", + [MSG_HELP_18]="\n", + [MSG_HELP_19]="\n", + [MSG_HELP_20]="Examples:\n", + [MSG_HELP_21]="\n", + [MSG_HELP_22]=" Do a one-line parser:\n", + [MSG_HELP_23]="\n", + [MSG_HELP_24]=" $ " NAME " run md.grammar < README.md\n", + [MSG_HELP_25]="\n", + [MSG_HELP_26]="\n", + [MSG_HELP_27]=" Compile the grammer:\n", + [MSG_HELP_28]="\n", + [MSG_HELP_LAST]=" $ " NAME " build csv.grammar > dunno.alsodunno\n", + [MSG_VERSION]= NAME " " VERSION " " DATE "\n", + [MSG_ERR_VECTOR_MAX_CAPACITY]= "Already at max capacity (%ld): %s\n", + [MSG_ERR_VECTOR_OUT_OF_BOUNDS]= "idx (%ld) is beyond bounds (%ld)\n", + NULL +}; + + +#ifdef TEST +int +main(void) { + int rc = 0; + + if (getenv("DUMP_TRANSLATABLE_STRINGS")) { + if (dump_translatable_strings(MSGS)) { + logerr("dump_translatable_strings(MSGS)\n"); + rc = -1; + goto out; + } + } + +out: + return !!rc; +} +#endif diff --git a/src/i18n.h b/src/i18n.h new file mode 100644 index 0000000..2cf992e --- /dev/null +++ b/src/i18n.h @@ -0,0 +1,45 @@ +enum MSGCATALOG_ID { + MSG_USAGE_FIRST = 1, + MSG_USAGE_1, + MSG_USAGE_2, + MSG_USAGE_LAST, + MSG_HELP_FIRST, + MSG_HELP_1, + MSG_HELP_2, + MSG_HELP_3, + MSG_HELP_4, + MSG_HELP_5, + MSG_HELP_6, + MSG_HELP_7, + MSG_HELP_8, + MSG_HELP_9, + MSG_HELP_10, + MSG_HELP_11, + MSG_HELP_12, + MSG_HELP_13, + MSG_HELP_14, + MSG_HELP_15, + MSG_HELP_16, + MSG_HELP_17, + MSG_HELP_18, + MSG_HELP_19, + MSG_HELP_20, + MSG_HELP_21, + MSG_HELP_22, + MSG_HELP_23, + MSG_HELP_24, + MSG_HELP_25, + MSG_HELP_26, + MSG_HELP_27, + MSG_HELP_28, + MSG_HELP_LAST, + MSG_VERSION, + MSG_ERR_VECTOR_MAX_CAPACITY, + MSG_ERR_VECTOR_OUT_OF_BOUNDS, +}; + + +extern const char *const +MSGS[]; + +#define _(msg_id) s(MSGS, msg_id) diff --git a/src/lib.c b/src/lib.c new file mode 100644 index 0000000..8a8bff9 --- /dev/null +++ b/src/lib.c @@ -0,0 +1,20 @@ +#include <stdio.h> + + +int +re_main(int argc, char **argv) { + int rc = 0; + for (int i = 0; i < argc; i++) { + printf("argv[%d]: %s\n", i, argv[i]); + } + return rc; +} + + +#ifdef TEST +int +main(void) { + int rc = 0; + return rc; +} +#endif diff --git a/src/lib.h b/src/lib.h new file mode 120000 index 0000000..e823a5d --- /dev/null +++ b/src/lib.h @@ -0,0 +1 @@ +pindaiba.h
\ No newline at end of file diff --git a/src/logerr.c b/src/logerr.c new file mode 100644 index 0000000..eb160d2 --- /dev/null +++ b/src/logerr.c @@ -0,0 +1,314 @@ +#include "config.h" + +#include <stdarg.h> +#include <stdlib.h> +#include <stdio.h> + +#include "logerr.h" + +#ifdef TEST +#include <assert.h> +#include <errno.h> +#include <string.h> + +#include "testing.h" +#include "../tests/slurp.h" +#endif + + +/** + * @tags infallible + */ +void +vlogerr( + const char *const file, + const char *const function, + const int lineno, + FILE *restrict stream, + const char *restrict format, + ... +) { + (void)fprintf(stream, "%s:%s:%d: ", file, function, lineno); + + va_list args; + va_start(args, format); + (void)vfprintf(stream, format, args); + va_end(args); + + return; +} + +#ifdef TEST +static const char *const +FNAME = __FILE__ ".txt"; + +static int +test_vlogerr(void) { + int rc = 0; + + test_start("vlogerr()"); + FILE *file = NULL; + char *str = NULL; + + { + testing("empty varargs"); + + file = fopen(FNAME, "w"); + if (!file) { + perror("fopen(FNAME, \"w\")"); + rc = -1; + goto out; + } + + #line 100 + vlogerr(__FILE__, __func__, __LINE__, file, + ""); + + const int ret = fclose(file); + file = NULL; + if (ret) { + perror("fclose(file)"); + rc = -1; + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + perror("slurp_for_tests(FNAME, &str)"); + rc = -1; + goto out; + } + + const char *const expected = + "src/logerr.c:test_vlogerr:100: "; + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; + + test_ok(); + } + { + testing("a newline only"); + + file = fopen(FNAME, "w"); + if (!file) { + perror("fopen(FNAME, \"w\")"); + rc = -1; + goto out; + } + + #line 200 + vlogerr(__FILE__, __func__, __LINE__, file, + "\n"); + + const int ret = fclose(file); + file = NULL; + if (ret) { + perror("fclose(file)"); + rc = -1; + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + perror("slurp_for_tests(FNAME, &str)"); + rc = -1; + goto out; + } + + const char *const expected = + "src/logerr.c:test_vlogerr:200: \n"; + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; + + test_ok(); + } + { + testing("static format string"); + + file = fopen(FNAME, "w"); + if (!file) { + perror("fopen(FNAME, \"w\")"); + rc = -1; + goto out; + } + + #line 300 + vlogerr(__FILE__, __func__, __LINE__, file, + "some static string\n"); + + const int ret = fclose(file); + file = NULL; + if (ret) { + perror("fclose(file)"); + rc = -1; + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + perror("slurp_for_tests(FNAME, &str)"); + rc = -1; + goto out; + } + + const char *const expected = + "src/logerr.c:test_vlogerr:300: some static string\n"; + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; + + test_ok(); + } + { + testing("single arg format string"); + + file = fopen(FNAME, "w"); + if (!file) { + perror("fopen(FNAME, \"w\")"); + rc = -1; + goto out; + } + + #line 400 + vlogerr(__FILE__, __func__, __LINE__, file, + "fn(%s)\n", "an-arg"); + + const int ret = fclose(file); + file = NULL; + if (ret) { + perror("fclose(file)"); + rc = -1; + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + perror("slurp_for_tests(FNAME, &str)"); + rc = -1; + goto out; + } + + const char *const expected = + "src/logerr.c:test_vlogerr:400: fn(an-arg)\n"; + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; + + test_ok(); + } + { + testing("multiple format strings"); + + file = fopen(FNAME, "w"); + if (!file) { + perror("fopen(FNAME, \"w\")"); + rc = -1; + goto out; + } + + #line 500 + vlogerr(__FILE__, __func__, __LINE__, file, + "int (%d), string (%s) and char (%c)\n", + 123, + "another-str", + 'z'); + + const int ret = fclose(file); + file = NULL; + if (ret) { + perror("fclose(file)"); + rc = -1; + goto out; + } + + if (slurp_for_tests(FNAME, &str)) { + perror("slurp_for_tests(FNAME, &str)"); + rc = -1; + goto out; + } + + const char *const expected = + "src/logerr.c:test_vlogerr:500: " + "int (123), string (another-str) and char (z)\n"; + assert(strcmp(expected, str) == 0); + + free(str); + str = NULL; + + test_ok(); + } + +out: + if (str) { + free(str); + } + if (file) { + if (fclose(file)) { + perror("fclose(file)"); + rc = -1; + } + } + return rc; +} + +static int +test_logerr(void) { + int rc = 0; + + test_start("logerr()"); + + { + testing("can be called with an empty string"); + + logerr(""); + + test_ok(); + } + { + testing("can be called with a static string"); + + logerr("some err\n"); + + test_ok(); + } + { + testing("can be called with a formatted string"); + + logerr("some err: %s\n", strerror(errno)); + + test_ok(); + } + { + testing("can be called with formatting arguments"); + + logerr("int: %d\nstr: %s\n", 123, "an example string"); + + test_ok(); + } + + return rc; +} + + +int +main(void) { + int rc = 0; + + if (test_vlogerr()) { + perror("test_vlogerr()"); + rc = -1; + goto out; + } + + if (test_logerr()) { + perror("test_logerr()"); + rc = -1; + goto out; + } + +out: + return !!rc; +} +#endif diff --git a/src/logerr.h b/src/logerr.h new file mode 100644 index 0000000..56b2432 --- /dev/null +++ b/src/logerr.h @@ -0,0 +1,12 @@ +void +vlogerr( + + const char *const file, + const char *const function, + const int lineno, + FILE *restrict stream, + const char *restrict format, + ... +); + +#define logerr(...) vlogerr(__FILE__, __func__, __LINE__, stderr, __VA_ARGS__) diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..ac9a1bd --- /dev/null +++ b/src/main.c @@ -0,0 +1,8 @@ +#include "config.h" + +#include "pindaiba.h" + +int +main(int argc, char **argv) { + return re_main(argc, argv); +} diff --git a/src/pindaiba.en.msg b/src/pindaiba.en.msg new file mode 100644 index 0000000..1fbb6f2 --- /dev/null +++ b/src/pindaiba.en.msg @@ -0,0 +1,74 @@ +1 Usage:\n + +2 pindaiba -p FILE [-o DIRECTORY]\n + +3 pindaiba -l FILE [-o DIRECTORY]\n + +4 pindaiba [-hV]\n + +5 \n + +6 \n + +7 Options:\n + +8 -p FILE parser file to be processed\n + +9 -l FILE lexer file to be processed\n + +10 -o DIRECTORY output where to place the\n + +11 generated files (default .)\n + +12 -h, --help show this help message\n + +13 -V, --version print the version number\n + +14 \n + +15 \n + +16 Run the pindaiba(1) parser program.\n + +17 \n + +18 Here is the explanation for what it does, and the synopsis\n + +19 of its usage.\n + +20 \n + +21 See "man pindaiba" for usage information and\n + +22 "man pindaiba.tutorial" for a beginner introduction.\n + +23 \n + +24 \n + +25 Examples:\n + +26 \n + +27 Do a one-line parser:\n + +28 \n + +29 $ pindaiba run md.grammar < README.md\n + +30 \n + +31 \n + +32 Compile the grammer:\n + +33 \n + +34 $ pindaiba build csv.grammar > dunno.alsodunno\n + +35 pindaiba 0.1.0 1970-01-01\n + +36 Already at max capacity (%ld): %s\n + +37 idx (%ld) is beyond bounds (%ld)\n + diff --git a/src/pindaiba.h b/src/pindaiba.h new file mode 100644 index 0000000..39c9f2d --- /dev/null +++ b/src/pindaiba.h @@ -0,0 +1,14 @@ +/** + * # Xa blau + * + * Xupli xablau xubliu, xubliu: + * + * ``` + * int + * main(int argc, char **argv) { + * return re_main(argc, argv); + * } + * ``` + */ +int +re_main(int argc, char **argv); diff --git a/src/random.c b/src/random.c new file mode 100644 index 0000000..d92d533 --- /dev/null +++ b/src/random.c @@ -0,0 +1,122 @@ +#include "config.h" + +#include <assert.h> +#include <errno.h> +#include <stddef.h> +#include <stdio.h> +#include <stdint.h> +#include <string.h> + +#include "logerr.h" +#include "random.h" + +#ifdef TEST +#include "testing.h" +#endif + + +int +urandom_bytes(const size_t n, uint8_t (*const addr)[]) { + int rc = 0; + + FILE *f = NULL; + + f = fopen("/dev/urandom", "r"); + if (!f) { + logerr("fopen(\"/dev/urandom\", \"r\"): %s\n", strerror(errno)); + rc = -1; + goto out; + } + + const size_t read_count = fread(addr, 1, n, f); + if (ferror(f)) { + logerr("fread(addr, 1, n, f): %s\n", strerror(errno)); + rc = -1; + goto out; + } + assert(read_count == n); + +out: + if (f) { + if (fclose(f)) { + logerr("fclose(f): %s\n", strerror(errno)); + rc = -1; + } + } + return rc; +} + +#ifdef TEST +static int +test_urandom_bytes(void) { + int rc = 0; + + test_start("urandom_bytes()"); + + { + testing("we get to pick the size that comes out"); + + const size_t LEN = 256; + uint8_t arr[256 /* LEN */] = { 0 }; + + for (size_t n = 0; n < LEN; n++) { + if (urandom_bytes(n, &arr)) { + logerr("urandom_bytes(n, &arr);\n"); + rc = -1; + goto out; + } + for (size_t i = n; i < LEN; i++) { + assert(arr[i] == 0); + } + } + + test_ok(); + } + + { + testing("we always get a new value as a result"); + + const size_t LEN = 64; + uint8_t arr1[64 /* LEN */] = { 0 }; + uint8_t arr2[64 /* LEN */] = { 0 }; + + if (urandom_bytes(LEN, &arr1)) { + logerr("urandom_bytes(LEN, &arr1);\n"); + rc = -1; + goto out; + } + + const size_t attempts = 10; + for (size_t n = 0; n < attempts; n++) { + if (urandom_bytes(LEN, &arr2)) { + logerr("urandom_bytes(LEN, &arr2);\n"); + rc = -1; + goto out; + } + assert(memcmp(arr1, arr2, LEN) != 0); + } + + test_ok(); + } + +out: + return rc; +} +#endif + + +#ifdef TEST +int +main(void) { + int rc = 0; + + if (test_urandom_bytes()) { + logerr("test_urandom_bytes();\n"); + rc = -1; + goto out; + } + +out: + return !!rc; +} +#endif diff --git a/src/random.h b/src/random.h new file mode 100644 index 0000000..ffaa69e --- /dev/null +++ b/src/random.h @@ -0,0 +1,2 @@ +int +urandom_bytes(const size_t n, uint8_t (*const addr)[]); diff --git a/src/testing.c b/src/testing.c new file mode 100644 index 0000000..028e48f --- /dev/null +++ b/src/testing.c @@ -0,0 +1,114 @@ +#include "config.h" + +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> + +#include "testing.h" + + +#define COLOUR_RESET "\033[0m" +#define COLOUR_GREEN "\033[0;32m" +#define COLOUR_YELLOW "\033[0;33m" + +static const char *const +ENVVAR_NAME = "NO_COLOR"; + + +static bool +show_colour(void) { + const char *const no_colour = getenv(ENVVAR_NAME); + return !no_colour || no_colour[0] == '\0'; +} + + +/** + * @tags infallible + */ +void +test_start(const char *const name) { + (void)fprintf(stderr, "%s:\n", name); + + return; +} + + +/** + * @tags infallible + */ +void +testing(const char *const message) { + if (show_colour()) { + (void)fprintf( + stderr, + COLOUR_YELLOW "testing" COLOUR_RESET ": %s...", + message + ); + } else { + (void)fprintf( + stderr, + "testing: %s...", + message + ); + } + + return; +} + + +/** + * @tags infallible + */ +void +test_ok(void) { + if (show_colour()) { + (void)fprintf(stderr, " " COLOUR_GREEN "OK" COLOUR_RESET ".\n"); + } else { + (void)fprintf(stderr, " OK.\n"); + } + + return; +} + +#ifdef TEST +int +main(void) { + int rc = 0; + + test_start("testing.c"); + const int should_overwrite = 1; + + if (unsetenv(ENVVAR_NAME)) { + perror("unsetenv(\"NO_COLOR\")"); + rc = -1; + goto out; + } + { + testing("unset NO_COLOR"); + test_ok(); + } + + if (setenv(ENVVAR_NAME, "", should_overwrite)) { + perror("setenv(\"NO_COLOR\", \"\", 1)"); + rc = -1; + goto out; + } + { + testing("empty NO_COLOR"); + test_ok(); + } + + if (setenv(ENVVAR_NAME, "something", should_overwrite)) { + perror("setenv(\"NO_COLOR\", \"something\", 1)"); + rc = -1; + goto out; + } + { + testing("defined NO_COLOR"); + test_ok(); + } + +out: + return !!rc; +} +#endif diff --git a/src/testing.h b/src/testing.h new file mode 100644 index 0000000..9a1bb2e --- /dev/null +++ b/src/testing.h @@ -0,0 +1,8 @@ +void +test_start(const char *const name); + +void +testing(const char *const message); + +void +test_ok(void); diff --git a/tests/c/resources/a-file-to-be-read-in-the-test.txt b/tests/c/resources/a-file-to-be-read-in-the-test.txt new file mode 100644 index 0000000..762cf0f --- /dev/null +++ b/tests/c/resources/a-file-to-be-read-in-the-test.txt @@ -0,0 +1,3 @@ +A file with some content +that should be read when +running the unit tests. diff --git a/tests/c/resources/an-empty-file.txt b/tests/c/resources/an-empty-file.txt new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/tests/c/resources/an-empty-file.txt diff --git a/tests/fuzz/another.c b/tests/fuzz/another.c new file mode 100644 index 0000000..7fbfe07 --- /dev/null +++ b/tests/fuzz/another.c @@ -0,0 +1,17 @@ +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> + +static bool +fuzz_me(const uint8_t *const data, const size_t size) { + (void)data; + (void)size; + return true; +} + +int +LLVMFuzzerTestOneInput(const uint8_t *const data, const size_t size) { + int rc = 0; + fuzz_me(data, size); + return rc; +} diff --git a/tests/fuzz/hello.c b/tests/fuzz/hello.c new file mode 100644 index 0000000..7fbfe07 --- /dev/null +++ b/tests/fuzz/hello.c @@ -0,0 +1,17 @@ +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> + +static bool +fuzz_me(const uint8_t *const data, const size_t size) { + (void)data; + (void)size; + return true; +} + +int +LLVMFuzzerTestOneInput(const uint8_t *const data, const size_t size) { + int rc = 0; + fuzz_me(data, size); + return rc; +} diff --git a/tests/slurp.c b/tests/slurp.c new file mode 100644 index 0000000..7b5936f --- /dev/null +++ b/tests/slurp.c @@ -0,0 +1,71 @@ +#include "../src/config.h" + +#include <stdio.h> +#include <stdlib.h> + +#include "slurp.h" + +int +slurp_for_tests(const char *const FNAME, char **strref) { + int rc = 0; + + FILE *file = NULL; + char *str = NULL; + + file = fopen(FNAME, "r"); + if (!file) { + perror("fopen(FNAME, \"r\")"); + rc = -1; + goto out; + } + + if (fseek(file, 0L, SEEK_END)) { + perror("fseek(file, 0L, SEEK_END)"); + rc = -1; + goto out; + } + + const long lsize = ftell(file); + if (lsize == -1) { + perror("ftell(file)"); + rc = -1; + goto out; + } + const size_t size = (size_t)lsize + sizeof(char); + + if (fseek(file, 0L, SEEK_SET)) { + perror("fseek(file, 0L, SEEK_SET)"); + rc = -1; + goto out; + } + + str = malloc(size); + if (!str) { + perror("malloc(...)"); + rc = -1; + goto out; + } + + if (fread(str, sizeof(char), size - 1, file) != size - 1) { + perror("fread(...)"); + rc = -1; + goto out; + } + str[size - 1] = '\0'; + *strref = str; + +out: + if (file) { + if (fclose(file)) { + perror("flcose(file"); + rc = -1; + } + } + + if (rc) { + if (str) { + free(str); + } + } + return rc; +} diff --git a/tests/slurp.h b/tests/slurp.h new file mode 100644 index 0000000..fd6c082 --- /dev/null +++ b/tests/slurp.h @@ -0,0 +1,2 @@ +int +slurp_for_tests(const char *const FNAME, char **strref); diff --git a/tools/cdeps.sh b/tools/cdeps.sh new file mode 100755 index 0000000..689c82d --- /dev/null +++ b/tools/cdeps.sh @@ -0,0 +1,234 @@ +#!/bin/sh +set -eu + +. tools/lib.sh + +usage() { + cat <<-'EOF' + Usage: + tools/cdeps.sh FILE... + tools/cdeps.sh -h + EOF +} + +help() { + cat <<-'EOF' + + + Options: + -h, --help show this message + + FILE toplevel entrypoint file + + + Given a list of C FILEs, generate the Makefile dependencies + between them. + + We have 3 types of object files: + - .o: plain object files; + - .lo: object files compiled as relocatable so it can be + included in a shared library; + - .to: compiled with -DTEST so it can expose its embedded unit + tests; + + We also have 1 aggregate file: + - .xa: an e**x**ecutable **a**rchive, made with ar(1), that + includes all the .o dependencies required for linking, so that + one can have only the archive as the linker input, i.e. + `cc -o example.bin example.xa`. This way we don't need + separate targets for each executable, and we can instead deal + separately with dependencies and linking, but without specific + file listing for each case. We use an ar-chive to exploit the + fact that a) it can replace existing files with the same name + and b) the $? macro in make(1) gives us all the out-of-date + dependencies, so our rule in the Makefile is usually a simple + `$(AR) $(ARFLAGS) $@ $?`. This way each .xa file lists its + dependency separately, and the building of the .xa file is + taken care of, in the same way that the linkage into an + executable is also taken care of. For running unit tests, we + include as a dependency of "$NAME.xa" the "$NAME.to" file, + which is the object code of "$NAME.c" compiled with the -DTEST + flag. This exposes a main() function for running unit tests. + + Also in order to run the unit tests without having to relink + them on each run, we have: + - .bin-check: a dedicated virtual target that does nothing but + execute the tests. In order to assert the binaries exist, + each "$NAME.bin-check" virtual target depends on the + equivalent "$NAME.check" physical target. + + There are 2 types of dependencies that are generated: + 1. self dependencies; + 2. inter dependencies. + + The self dependencies are the ones across different + manifestations of the same file so all derived assets are + correctly kept up-to-date: + - $NAME.o $NAME.lo $NAME.to: $NAME.h + + As the .SUFFIXES rule already covers the dependency to the + orinal $NAME.c file, all we do is say that whenever the public + interface of these binaries change, they need to be + recompiled; + + - $NAME.xa: $NAME.to + + We make sure to include in each executable archive (.xa) file + its own binary with unit tests. We include the "depN.o" + dependencies later; + + - $NAME.bin-check: $NAME.bin + + Enforce that the binary exists before we run them. + + After we establish the self dependencies, we scrub each file's + content looking for `#include "..."` lines that denote + dependency to other C file. Once we do that we'll have: + - $NAME.o $NAME.lo $NAME.to: dep1.h dep2.h ... depN.h + + We'll recompile our file when its public header changes. When + only the body of the code changes we don't recompile, only + later relink; + + - $NAME.xa: dep1.o dep2.o ... depN.o + + Make sure to include all required dependencies in the + $NAME.bin binary so that the later linking works properly. + + So if we have file1.c, file2.c and file3.c with their respective + headers, where file2.c and file3.c depend of file1.c, i.e. they + have `#include "file.h"` in their code, and file3.c depend of + file2.c, the expected output is: + + file1.o file1.lo file1.to: file1.h + file2.o file2.lo file2.to: file2.h + file3.o file3.lo file3.to: file3.h + + file1.xa: file1.to + file2.xa: file2.to + file3.xa: file3.to + + file1.bin-check: file1.bin + file2.bin-check: file2.bin + file3.bin-check: file3.bin + + + file1.o file1.lo file1.to: + file2.o file2.lo file2.to: file1.h + file3.o file3.lo file3.to: file1.h file2.h + + file1.xa: + file2.xa: file1.o + file3.xa: file1.o file2.o + + This ensures that only the minimal amount of files need to get + recompiled, but no less. + + + Examples: + + Get deps for all files in 'src/' but 'src/main.c': + + $ sh tools/cdeps.sh `find src/*.c -not -name 'main.c'` + + + Emit dependencies for all C files in a Git repository: + + $ sh tools/cdeps.sh `git ls-files | grep '\.c$'` + EOF +} + + +for flag in "$@"; do + case "$flag" in + (--) + break + ;; + (--help) + usage + help + exit + ;; + (*) + ;; + esac +done + +while getopts 'h' flag; do + case "$flag" in + (h) + usage + help + exit + ;; + (*) + usage >&2 + exit 2 + ;; + esac +done +shift $((OPTIND - 1)) + +FILE="${1:-}" +eval "$(assert_arg "$FILE" 'FILE')" + + + +each_f() { + fn="$1" + shift + for file in "$@"; do + f="${file%.c}" + "$fn" "$f" + done + printf '\n' +} + +self_header_deps() { + printf '%s.o\t%s.lo\t%s.to:\t%s.h\n' "$1" "$1" "$1" "$1" +} + +self_xa_deps() { + printf '%s.xa:\t%s.to\n' "$1" "$1" +} + +self_bincheck_deps() { + printf '%s.bin-check:\t%s.bin\n' "$1" "$1" +} + +deps_for() { + ext="$2" + for file in $(awk -F'"' '/^#include "/ { print $2 }' "$1.c"); do + if [ "$file" = 'config.h' ]; then + continue + fi + if [ "$(basename "$file")" = 'tests-lib.h' ]; then + continue + fi + f="$(dirname "$1")/$file" + if [ "$f" = "$1.h" ]; then + continue + fi + printf '%s\n' "${f%.h}$2" + done +} + +rebuild_deps() { + printf '\n' + printf '%s.o\t%s.lo\t%s.to:' "$1" "$1" "$1" + printf ' %s' $(deps_for "$1" .h) | sed 's| *$||' +} + +archive_deps() { + printf '\n' + printf '%s.xa:' "$1" + printf ' %s' $(deps_for "$1" .o) | sed 's| *$||' +} + + +each_f self_header_deps "$@" +each_f self_xa_deps "$@" +each_f self_bincheck_deps "$@" + +each_f rebuild_deps "$@" +each_f archive_deps "$@" diff --git a/tools/lib.sh b/tools/lib.sh new file mode 100644 index 0000000..0412d7c --- /dev/null +++ b/tools/lib.sh @@ -0,0 +1,49 @@ +#!/bin/sh + +assert_arg() { + if [ -z "$1" ]; then + printf 'Missing %s.\n\n' "$2" >&2 + cat <<-'EOF' + usage >&2 + exit 2 + EOF + fi +} + +uuid() { + od -xN20 /dev/urandom | + head -n1 | + awk '{OFS="-"; print $2$3,$4,$5,$6,$7$8$9}' +} + +tmpname() { + echo "${TMPDIR:-/tmp}/uuid-tmpname with spaces.$(uuid)" +} + +mkstemp() { + name="$(tmpname)" + touch "$name" + echo "$name" +} + +mkdtemp() { + name="$(tmpname)" + mkdir "$name" + echo "$name" +} + +END="\033[0m" +yellow() { + YELLOW="\033[0;33m" + printf "${YELLOW}${1}${END}" +} + +green() { + GREEN="\033[0;32m" + printf "${GREEN}${1}${END}" +} + +red() { + RED="\033[0;31m" + printf "${RED}${1}${END}" +} diff --git a/tools/makehelp.sh b/tools/makehelp.sh new file mode 100755 index 0000000..e6118de --- /dev/null +++ b/tools/makehelp.sh @@ -0,0 +1,149 @@ +#!/bin/sh +set -eu + +. tools/lib.sh + + +usage() { + cat <<-'EOF' + Usage: + makehelp.sh < MAKEFILE + makehelp.sh -h + EOF +} + +help() { + cat <<-'EOF' + + + Options: + -h, --help show this message + + + Generate a help message from the given Makefile. + + Any target or variable commented with two "#" characters gets + picked up. Multi-line comments are supported: + + VAR1 = 1 + # a comment + VAR2 = 2 + ## another comment -> this one is included in the docs + VAR3 = 3 + + ## with a big + ## comment, which is also included + a-target: + + + Examples: + + Generate help messages from "Makefile": + + $ aux/makehelp.sh < Makefile + + + Generate help messages for all targets: + + $ cat Makefile dev.mk | aux/makehelp.sh + EOF +} + + +for flag in "$@"; do + case "$flag" in + (--) + break + ;; + (--help) + usage + help + exit + ;; + (*) + ;; + esac +done + +while getopts 'h' flag; do + case "$flag" in + (h) + usage + help + exit + ;; + (*) + usage >&2 + exit 2 + ;; + esac +done +shift $((OPTIND - 1)) + + +TARGETS="$(mkstemp)" +VARIABLES="$(mkstemp)" +trap 'rm -f "$TARGETS" "$VARIABLES"' EXIT + +awk -vCOLUMN=15 -vTARGETS="$TARGETS" -vVARIABLES="$VARIABLES" ' +function indent(n, where) { + for (INDENT = 0; INDENT < n; INDENT++) { + printf " " > where + } +} + +/^## / { doc[len++] = substr($0, 4) } + +/^[-_a-zA-Z]+:/ && len { + printf "\033[36m%s\033[0m", substr($1, 1, length($1) - 1) > TARGETS + for (i = 0; i < len; i++) { + n = COLUMN - (i == 0 ? length($1) - 1 : 0) + indent(n, TARGETS) + printf "%s\n", doc[i] > TARGETS + } + len = 0 +} + +/^.++=/ && len { + printf "\033[36m%s\033[0m", $1 > VARIABLES + for (i = 0; i < len; i++) { + n = COLUMN - (i == 0 ? length($1) : 0) + indent(n, VARIABLES) + printf "%s\n", doc[i] > VARIABLES + } + len = 0 +}' + + + +indent() { + sed 's|^| |' +} + +cat <<-EOF + Usage: + + make [VARIABLE=value...] [target...] + + + Targets: + + $(indent < "$TARGETS") + + + Variables: + + $(indent < "$VARIABLES") + + + Examples: + + Build "all", the default target: + + $ make + + + Test and install, with \$(DESTDIR) set to "tmp/": + + $ make DESTDIR=tmp check install +EOF diff --git a/tools/manpages.sh b/tools/manpages.sh new file mode 100755 index 0000000..755ff77 --- /dev/null +++ b/tools/manpages.sh @@ -0,0 +1,126 @@ +#!/bin/sh +set -eu + +. tools/lib.sh + + +usage() { + cat <<-'EOF' + Usage: + sh doc/manpages.sh -i|-u -p MANDIR FILE... + sh doc/manpages.sh -h + EOF +} + +help() { + cat <<-'EOF' + + + Options: + -i set ACTION=install + -u set ACTION=uninstall + -p MANDIR use $MANDIR as the prefix directory + -h, --help show this message + + + Installs/uninstalls manpage files into MANDIR using the original + filename as a hint towards which subdirectory to pick: the + `prog.ru.1` file is installed under the `man1/` directory + hierarchy under the `ru/` language folder. When then language + is `en`, a the toplevel `man1/` gets a symlink to the `en/` + language folder: + + man/ + en/ + man1/ + prog.1 + ru/ + man1/ + prog.1 + man1/ + prog.1 -> ../en/man1/prog.1 + + + Examples: + + Install prog.en.1 under `/usr/man`: + + $ sh doc/manpages.sh -ip/usr/man prog.en.1 + + + Uninstall all files listed under doc/ from `$PREFIX`: + + sh doc/manpages.sh -up "$PREFIX" doc/*.[0-9] + EOF +} + + +for flag in "$@"; do + case "$flag" in + (--) + break + ;; + (--help) + usage + help + exit + ;; + (*) + ;; + esac +done + +while getopts 'iup:h' flag; do + case "$flag" in + (i) + ACTION=install + ;; + (u) + ACTION=uninstall + ;; + (p) + MANDIR="$OPTARG" + ;; + (h) + usage + help + exit + ;; + (*) + usage >&2 + exit 2 + ;; + esac +done +shift $((OPTIND - 1)) + +eval "$(assert_arg "${ACTION:-}" '-[iu] for choosing action')" +eval "$(assert_arg "${MANDIR:-}" '-p MANDIR')" +eval "$(assert_arg "${1:-}" 'FILE...')" + + +for f in "$@"; do + l="$(echo "$f" | awk -F. '{print $(NF-1)}')" + n="$(echo "$f" | awk -F. '{print $NF}')" + case "$ACTION" in + (install) + to_name="$(basename "${f%."$l"."$n"}.$n")" + mkdir -p "$MANDIR/$l/man$n" "$MANDIR/man$n" + cp "$f" "$MANDIR/$l/man$n/$to_name" + if [ "$l" = 'en' ]; then + ln -fs "../en/man$n/$to_name" \ + "$MANDIR/man$n/$to_name" + fi + ;; + (uninstall) + to_name="$(basename "${f%."$l"."$n"}.$n")" + rm -f \ + "$MANDIR/$l/man$n/$to_name" \ + "$MANDIR/man$n/$to_name" + ;; + (*) + echo "Bad ACTION: $ACTION" + exit 1 + ;; + esac +done |