summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorH. Peter Anvin <hpa@zytor.com>2016-03-08 02:06:39 -0800
committerH. Peter Anvin <hpa@zytor.com>2016-03-08 02:08:44 -0800
commitcc147f79e44e06ab6dc81449f66e581fe9a1cf80 (patch)
treef4b4fa6a3b3ce3d270a86761fb560443e2b69fcb /lib
parentbdae10bee2ec613e8765235ba17f3383693e3d73 (diff)
downloadnasm-cc147f79e44e06ab6dc81449f66e581fe9a1cf80.tar.gz
Makefiles: use actual archive managers
There is no reason to not use an archive manager to build our executables. If there really are systems which don't have any kind of archive manager, we can simply link all the objects. This also drops any use of configure to detect library objects. Instead just use HAVE_* and let the archive manager delete them. A lot of additional functions could be declared library functions and reorganized. ***FIX*** Mkfiles/*.mak have not yet been updated. Signed-off-by: H. Peter Anvin <hpa@zytor.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/crc64.c189
-rw-r--r--lib/file.c140
-rw-r--r--lib/ilog2.c161
-rw-r--r--lib/md5c.c247
-rw-r--r--lib/nasmlib.c669
-rw-r--r--lib/realpath.c135
-rw-r--r--lib/snprintf.c4
-rw-r--r--lib/ver.c51
-rw-r--r--lib/vsnprintf.c4
9 files changed, 1600 insertions, 0 deletions
diff --git a/lib/crc64.c b/lib/crc64.c
new file mode 100644
index 00000000..338e0be4
--- /dev/null
+++ b/lib/crc64.c
@@ -0,0 +1,189 @@
+/* ----------------------------------------------------------------------- *
+ *
+ * Copyright 1996-2014 The NASM Authors - All Rights Reserved
+ * See the file AUTHORS included with the NASM distribution for
+ * the specific copyright holders.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * ----------------------------------------------------------------------- */
+
+#include "compiler.h"
+#include "nasmlib.h"
+#include "hashtbl.h"
+
+static const uint64_t crc64_tab[256] = {
+ UINT64_C(0x0000000000000000), UINT64_C(0x7ad870c830358979),
+ UINT64_C(0xf5b0e190606b12f2), UINT64_C(0x8f689158505e9b8b),
+ UINT64_C(0xc038e5739841b68f), UINT64_C(0xbae095bba8743ff6),
+ UINT64_C(0x358804e3f82aa47d), UINT64_C(0x4f50742bc81f2d04),
+ UINT64_C(0xab28ecb46814fe75), UINT64_C(0xd1f09c7c5821770c),
+ UINT64_C(0x5e980d24087fec87), UINT64_C(0x24407dec384a65fe),
+ UINT64_C(0x6b1009c7f05548fa), UINT64_C(0x11c8790fc060c183),
+ UINT64_C(0x9ea0e857903e5a08), UINT64_C(0xe478989fa00bd371),
+ UINT64_C(0x7d08ff3b88be6f81), UINT64_C(0x07d08ff3b88be6f8),
+ UINT64_C(0x88b81eabe8d57d73), UINT64_C(0xf2606e63d8e0f40a),
+ UINT64_C(0xbd301a4810ffd90e), UINT64_C(0xc7e86a8020ca5077),
+ UINT64_C(0x4880fbd87094cbfc), UINT64_C(0x32588b1040a14285),
+ UINT64_C(0xd620138fe0aa91f4), UINT64_C(0xacf86347d09f188d),
+ UINT64_C(0x2390f21f80c18306), UINT64_C(0x594882d7b0f40a7f),
+ UINT64_C(0x1618f6fc78eb277b), UINT64_C(0x6cc0863448deae02),
+ UINT64_C(0xe3a8176c18803589), UINT64_C(0x997067a428b5bcf0),
+ UINT64_C(0xfa11fe77117cdf02), UINT64_C(0x80c98ebf2149567b),
+ UINT64_C(0x0fa11fe77117cdf0), UINT64_C(0x75796f2f41224489),
+ UINT64_C(0x3a291b04893d698d), UINT64_C(0x40f16bccb908e0f4),
+ UINT64_C(0xcf99fa94e9567b7f), UINT64_C(0xb5418a5cd963f206),
+ UINT64_C(0x513912c379682177), UINT64_C(0x2be1620b495da80e),
+ UINT64_C(0xa489f35319033385), UINT64_C(0xde51839b2936bafc),
+ UINT64_C(0x9101f7b0e12997f8), UINT64_C(0xebd98778d11c1e81),
+ UINT64_C(0x64b116208142850a), UINT64_C(0x1e6966e8b1770c73),
+ UINT64_C(0x8719014c99c2b083), UINT64_C(0xfdc17184a9f739fa),
+ UINT64_C(0x72a9e0dcf9a9a271), UINT64_C(0x08719014c99c2b08),
+ UINT64_C(0x4721e43f0183060c), UINT64_C(0x3df994f731b68f75),
+ UINT64_C(0xb29105af61e814fe), UINT64_C(0xc849756751dd9d87),
+ UINT64_C(0x2c31edf8f1d64ef6), UINT64_C(0x56e99d30c1e3c78f),
+ UINT64_C(0xd9810c6891bd5c04), UINT64_C(0xa3597ca0a188d57d),
+ UINT64_C(0xec09088b6997f879), UINT64_C(0x96d1784359a27100),
+ UINT64_C(0x19b9e91b09fcea8b), UINT64_C(0x636199d339c963f2),
+ UINT64_C(0xdf7adabd7a6e2d6f), UINT64_C(0xa5a2aa754a5ba416),
+ UINT64_C(0x2aca3b2d1a053f9d), UINT64_C(0x50124be52a30b6e4),
+ UINT64_C(0x1f423fcee22f9be0), UINT64_C(0x659a4f06d21a1299),
+ UINT64_C(0xeaf2de5e82448912), UINT64_C(0x902aae96b271006b),
+ UINT64_C(0x74523609127ad31a), UINT64_C(0x0e8a46c1224f5a63),
+ UINT64_C(0x81e2d7997211c1e8), UINT64_C(0xfb3aa75142244891),
+ UINT64_C(0xb46ad37a8a3b6595), UINT64_C(0xceb2a3b2ba0eecec),
+ UINT64_C(0x41da32eaea507767), UINT64_C(0x3b024222da65fe1e),
+ UINT64_C(0xa2722586f2d042ee), UINT64_C(0xd8aa554ec2e5cb97),
+ UINT64_C(0x57c2c41692bb501c), UINT64_C(0x2d1ab4dea28ed965),
+ UINT64_C(0x624ac0f56a91f461), UINT64_C(0x1892b03d5aa47d18),
+ UINT64_C(0x97fa21650afae693), UINT64_C(0xed2251ad3acf6fea),
+ UINT64_C(0x095ac9329ac4bc9b), UINT64_C(0x7382b9faaaf135e2),
+ UINT64_C(0xfcea28a2faafae69), UINT64_C(0x8632586aca9a2710),
+ UINT64_C(0xc9622c4102850a14), UINT64_C(0xb3ba5c8932b0836d),
+ UINT64_C(0x3cd2cdd162ee18e6), UINT64_C(0x460abd1952db919f),
+ UINT64_C(0x256b24ca6b12f26d), UINT64_C(0x5fb354025b277b14),
+ UINT64_C(0xd0dbc55a0b79e09f), UINT64_C(0xaa03b5923b4c69e6),
+ UINT64_C(0xe553c1b9f35344e2), UINT64_C(0x9f8bb171c366cd9b),
+ UINT64_C(0x10e3202993385610), UINT64_C(0x6a3b50e1a30ddf69),
+ UINT64_C(0x8e43c87e03060c18), UINT64_C(0xf49bb8b633338561),
+ UINT64_C(0x7bf329ee636d1eea), UINT64_C(0x012b592653589793),
+ UINT64_C(0x4e7b2d0d9b47ba97), UINT64_C(0x34a35dc5ab7233ee),
+ UINT64_C(0xbbcbcc9dfb2ca865), UINT64_C(0xc113bc55cb19211c),
+ UINT64_C(0x5863dbf1e3ac9dec), UINT64_C(0x22bbab39d3991495),
+ UINT64_C(0xadd33a6183c78f1e), UINT64_C(0xd70b4aa9b3f20667),
+ UINT64_C(0x985b3e827bed2b63), UINT64_C(0xe2834e4a4bd8a21a),
+ UINT64_C(0x6debdf121b863991), UINT64_C(0x1733afda2bb3b0e8),
+ UINT64_C(0xf34b37458bb86399), UINT64_C(0x8993478dbb8deae0),
+ UINT64_C(0x06fbd6d5ebd3716b), UINT64_C(0x7c23a61ddbe6f812),
+ UINT64_C(0x3373d23613f9d516), UINT64_C(0x49aba2fe23cc5c6f),
+ UINT64_C(0xc6c333a67392c7e4), UINT64_C(0xbc1b436e43a74e9d),
+ UINT64_C(0x95ac9329ac4bc9b5), UINT64_C(0xef74e3e19c7e40cc),
+ UINT64_C(0x601c72b9cc20db47), UINT64_C(0x1ac40271fc15523e),
+ UINT64_C(0x5594765a340a7f3a), UINT64_C(0x2f4c0692043ff643),
+ UINT64_C(0xa02497ca54616dc8), UINT64_C(0xdafce7026454e4b1),
+ UINT64_C(0x3e847f9dc45f37c0), UINT64_C(0x445c0f55f46abeb9),
+ UINT64_C(0xcb349e0da4342532), UINT64_C(0xb1eceec59401ac4b),
+ UINT64_C(0xfebc9aee5c1e814f), UINT64_C(0x8464ea266c2b0836),
+ UINT64_C(0x0b0c7b7e3c7593bd), UINT64_C(0x71d40bb60c401ac4),
+ UINT64_C(0xe8a46c1224f5a634), UINT64_C(0x927c1cda14c02f4d),
+ UINT64_C(0x1d148d82449eb4c6), UINT64_C(0x67ccfd4a74ab3dbf),
+ UINT64_C(0x289c8961bcb410bb), UINT64_C(0x5244f9a98c8199c2),
+ UINT64_C(0xdd2c68f1dcdf0249), UINT64_C(0xa7f41839ecea8b30),
+ UINT64_C(0x438c80a64ce15841), UINT64_C(0x3954f06e7cd4d138),
+ UINT64_C(0xb63c61362c8a4ab3), UINT64_C(0xcce411fe1cbfc3ca),
+ UINT64_C(0x83b465d5d4a0eece), UINT64_C(0xf96c151de49567b7),
+ UINT64_C(0x76048445b4cbfc3c), UINT64_C(0x0cdcf48d84fe7545),
+ UINT64_C(0x6fbd6d5ebd3716b7), UINT64_C(0x15651d968d029fce),
+ UINT64_C(0x9a0d8ccedd5c0445), UINT64_C(0xe0d5fc06ed698d3c),
+ UINT64_C(0xaf85882d2576a038), UINT64_C(0xd55df8e515432941),
+ UINT64_C(0x5a3569bd451db2ca), UINT64_C(0x20ed197575283bb3),
+ UINT64_C(0xc49581ead523e8c2), UINT64_C(0xbe4df122e51661bb),
+ UINT64_C(0x3125607ab548fa30), UINT64_C(0x4bfd10b2857d7349),
+ UINT64_C(0x04ad64994d625e4d), UINT64_C(0x7e7514517d57d734),
+ UINT64_C(0xf11d85092d094cbf), UINT64_C(0x8bc5f5c11d3cc5c6),
+ UINT64_C(0x12b5926535897936), UINT64_C(0x686de2ad05bcf04f),
+ UINT64_C(0xe70573f555e26bc4), UINT64_C(0x9ddd033d65d7e2bd),
+ UINT64_C(0xd28d7716adc8cfb9), UINT64_C(0xa85507de9dfd46c0),
+ UINT64_C(0x273d9686cda3dd4b), UINT64_C(0x5de5e64efd965432),
+ UINT64_C(0xb99d7ed15d9d8743), UINT64_C(0xc3450e196da80e3a),
+ UINT64_C(0x4c2d9f413df695b1), UINT64_C(0x36f5ef890dc31cc8),
+ UINT64_C(0x79a59ba2c5dc31cc), UINT64_C(0x037deb6af5e9b8b5),
+ UINT64_C(0x8c157a32a5b7233e), UINT64_C(0xf6cd0afa9582aa47),
+ UINT64_C(0x4ad64994d625e4da), UINT64_C(0x300e395ce6106da3),
+ UINT64_C(0xbf66a804b64ef628), UINT64_C(0xc5bed8cc867b7f51),
+ UINT64_C(0x8aeeace74e645255), UINT64_C(0xf036dc2f7e51db2c),
+ UINT64_C(0x7f5e4d772e0f40a7), UINT64_C(0x05863dbf1e3ac9de),
+ UINT64_C(0xe1fea520be311aaf), UINT64_C(0x9b26d5e88e0493d6),
+ UINT64_C(0x144e44b0de5a085d), UINT64_C(0x6e963478ee6f8124),
+ UINT64_C(0x21c640532670ac20), UINT64_C(0x5b1e309b16452559),
+ UINT64_C(0xd476a1c3461bbed2), UINT64_C(0xaeaed10b762e37ab),
+ UINT64_C(0x37deb6af5e9b8b5b), UINT64_C(0x4d06c6676eae0222),
+ UINT64_C(0xc26e573f3ef099a9), UINT64_C(0xb8b627f70ec510d0),
+ UINT64_C(0xf7e653dcc6da3dd4), UINT64_C(0x8d3e2314f6efb4ad),
+ UINT64_C(0x0256b24ca6b12f26), UINT64_C(0x788ec2849684a65f),
+ UINT64_C(0x9cf65a1b368f752e), UINT64_C(0xe62e2ad306bafc57),
+ UINT64_C(0x6946bb8b56e467dc), UINT64_C(0x139ecb4366d1eea5),
+ UINT64_C(0x5ccebf68aecec3a1), UINT64_C(0x2616cfa09efb4ad8),
+ UINT64_C(0xa97e5ef8cea5d153), UINT64_C(0xd3a62e30fe90582a),
+ UINT64_C(0xb0c7b7e3c7593bd8), UINT64_C(0xca1fc72bf76cb2a1),
+ UINT64_C(0x45775673a732292a), UINT64_C(0x3faf26bb9707a053),
+ UINT64_C(0x70ff52905f188d57), UINT64_C(0x0a2722586f2d042e),
+ UINT64_C(0x854fb3003f739fa5), UINT64_C(0xff97c3c80f4616dc),
+ UINT64_C(0x1bef5b57af4dc5ad), UINT64_C(0x61372b9f9f784cd4),
+ UINT64_C(0xee5fbac7cf26d75f), UINT64_C(0x9487ca0fff135e26),
+ UINT64_C(0xdbd7be24370c7322), UINT64_C(0xa10fceec0739fa5b),
+ UINT64_C(0x2e675fb4576761d0), UINT64_C(0x54bf2f7c6752e8a9),
+ UINT64_C(0xcdcf48d84fe75459), UINT64_C(0xb71738107fd2dd20),
+ UINT64_C(0x387fa9482f8c46ab), UINT64_C(0x42a7d9801fb9cfd2),
+ UINT64_C(0x0df7adabd7a6e2d6), UINT64_C(0x772fdd63e7936baf),
+ UINT64_C(0xf8474c3bb7cdf024), UINT64_C(0x829f3cf387f8795d),
+ UINT64_C(0x66e7a46c27f3aa2c), UINT64_C(0x1c3fd4a417c62355),
+ UINT64_C(0x935745fc4798b8de), UINT64_C(0xe98f353477ad31a7),
+ UINT64_C(0xa6df411fbfb21ca3), UINT64_C(0xdc0731d78f8795da),
+ UINT64_C(0x536fa08fdfd90e51), UINT64_C(0x29b7d047efec8728),
+};
+
+uint64_t crc64(uint64_t crc, const char *str)
+{
+ uint8_t c;
+
+ while ((c = *str++) != 0) {
+ crc = crc64_tab[(uint8_t)crc ^ c] ^ (crc >> 8);
+ }
+
+ return crc;
+}
+
+uint64_t crc64i(uint64_t crc, const char *str)
+{
+ uint8_t c;
+
+ while ((c = *str++) != 0) {
+ crc = crc64_tab[(uint8_t)crc ^ nasm_tolower(c)] ^ (crc >> 8);
+ }
+
+ return crc;
+}
diff --git a/lib/file.c b/lib/file.c
new file mode 100644
index 00000000..a5a97a00
--- /dev/null
+++ b/lib/file.c
@@ -0,0 +1,140 @@
+/* ----------------------------------------------------------------------- *
+ *
+ * Copyright 1996-2016 The NASM Authors - All Rights Reserved
+ * See the file AUTHORS included with the NASM distribution for
+ * the specific copyright holders.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * ----------------------------------------------------------------------- */
+
+#include "compiler.h"
+#include "nasmlib.h"
+
+#include <errno.h>
+
+#ifdef HAVE_IO_H
+# include <io.h>
+#endif
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+
+void nasm_write(const void *ptr, size_t size, FILE *f)
+{
+ size_t n = fwrite(ptr, 1, size, f);
+ if (n != size || ferror(f) || feof(f))
+ nasm_fatal(0, "unable to write output: %s", strerror(errno));
+}
+
+#ifdef WORDS_LITTLEENDIAN
+
+void fwriteint16_t(uint16_t data, FILE * fp)
+{
+ nasm_write(&data, 2, fp);
+}
+
+void fwriteint32_t(uint32_t data, FILE * fp)
+{
+ nasm_write(&data, 4, fp);
+}
+
+void fwriteint64_t(uint64_t data, FILE * fp)
+{
+ nasm_write(&data, 8, fp);
+}
+
+void fwriteaddr(uint64_t data, int size, FILE * fp)
+{
+ nasm_write(&data, size, fp);
+}
+
+#else /* not WORDS_LITTLEENDIAN */
+
+void fwriteint16_t(uint16_t data, FILE * fp)
+{
+ char buffer[2], *p = buffer;
+ WRITESHORT(p, data);
+ nasm_write(buffer, 2, fp);
+}
+
+void fwriteint32_t(uint32_t data, FILE * fp)
+{
+ char buffer[4], *p = buffer;
+ WRITELONG(p, data);
+ nasm_write(buffer, 4, fp);
+}
+
+void fwriteint64_t(uint64_t data, FILE * fp)
+{
+ char buffer[8], *p = buffer;
+ WRITEDLONG(p, data);
+ nasm_write(buffer, 8, fp);
+}
+
+void fwriteaddr(uint64_t data, int size, FILE * fp)
+{
+ char buffer[8], *p = buffer;
+ WRITEADDR(p, data, size);
+ nasm_write(buffer, size, fp);
+}
+
+#endif
+
+
+#ifdef HAVE_FILENO /* Useless without fileno() */
+# ifdef HAVE__CHSIZE_S
+# define nasm_ftruncate(fd,size) _chsize_s(fd,size)
+# elif defined(HAVE__CHSIZE)
+# define nasm_ftruncate(fd,size) _chsize(fd,size)
+# elif defined(HAVE_FTRUNCATE)
+# define nasm_ftruncate(fd,size) ftruncate(fd,size)
+# endif
+#endif
+
+void fwritezero(size_t bytes, FILE *fp)
+{
+ size_t blksize;
+
+#ifdef nasm_ftruncate
+ if (bytes >= BUFSIZ && !ferror(fp) && !feof(fp)) {
+ off_t pos = ftello(fp);
+ if (pos >= 0) {
+ if (!fflush(fp) &&
+ !nasm_ftruncate(fileno(fp), pos + bytes) &&
+ !fseeko(fp, pos+bytes, SEEK_SET))
+ return;
+ }
+ }
+#endif
+
+ while (bytes) {
+ blksize = (bytes < ZERO_BUF_SIZE) ? bytes : ZERO_BUF_SIZE;
+
+ nasm_write(zero_buffer, blksize, fp);
+ bytes -= blksize;
+ }
+}
diff --git a/lib/ilog2.c b/lib/ilog2.c
new file mode 100644
index 00000000..a6066221
--- /dev/null
+++ b/lib/ilog2.c
@@ -0,0 +1,161 @@
+/* ----------------------------------------------------------------------- *
+ *
+ * Copyright 1996-2016 The NASM Authors - All Rights Reserved
+ * See the file AUTHORS included with the NASM distribution for
+ * the specific copyright holders.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * ----------------------------------------------------------------------- */
+
+#include "compiler.h"
+#include "nasmlib.h"
+
+#define ROUND(v, a, w) \
+ do { \
+ if (v & (((1 << w) - 1) << w)) { \
+ a += w; \
+ v >>= w; \
+ } \
+ } while (0)
+
+
+#if defined(__GNUC__) && defined(__x86_64__)
+
+int ilog2_32(uint32_t v)
+{
+ int n;
+
+ __asm__("bsrl %1,%0"
+ : "=r" (n)
+ : "rm" (v), "0" (0));
+ return n;
+}
+
+#elif defined(__GNUC__) && defined(__i386__)
+
+int ilog2_32(uint32_t v)
+{
+ int n;
+
+ __asm__("bsrl %1,%0 ; jnz 1f ; xorl %0,%0\n"
+ "1:"
+ : "=&r" (n)
+ : "rm" (v));
+ return n;
+}
+
+#elif defined(HAVE___BUILTIN_CTZ) && INT_MAX >= 2147483647
+
+int ilog2_32(uint32_t v)
+{
+ if (!v)
+ return 0;
+
+ return __builtin_clz(v) ^ 31;
+}
+
+#else
+
+int ilog2_32(uint32_t v)
+{
+ int p = 0;
+
+ ROUND(v, p, 16);
+ ROUND(v, p, 8);
+ ROUND(v, p, 4);
+ ROUND(v, p, 2);
+ ROUND(v, p, 1);
+
+ return p;
+}
+
+#endif
+
+#if defined(__GNUC__) && defined(__x86_64__)
+
+int ilog2_64(uint64_t v)
+{
+ uint64_t n;
+
+ __asm__("bsrq %1,%0"
+ : "=r" (n)
+ : "rm" (v), "0" (UINT64_C(0)));
+ return n;
+}
+
+#elif defined(HAVE__BUILTIN_CTZLL) && LLONG_MAX >= 9223372036854775807L
+
+int ilog2_64(uint64_t v)
+{
+ if (!v)
+ return 0;
+
+ return __builtin_clzll(v) ^ 63;
+}
+
+#else
+
+int ilog2_64(uint64_t vv)
+{
+ int p = 0;
+ uint32_t v;
+
+ v = vv >> 32;
+ if (v)
+ p += 32;
+ else
+ v = vv;
+
+ ROUND(v, p, 16);
+ ROUND(v, p, 8);
+ ROUND(v, p, 4);
+ ROUND(v, p, 2);
+ ROUND(v, p, 1);
+
+ return p;
+}
+
+#endif
+
+/*
+ * v == 0 ? 0 : is_power2(x) ? ilog2_X(v) : -1
+ */
+int alignlog2_32(uint32_t v)
+{
+ if (unlikely(v & (v-1)))
+ return -1; /* invalid alignment */
+
+ return ilog2_32(v);
+}
+
+int alignlog2_64(uint64_t v)
+{
+ if (unlikely(v & (v-1)))
+ return -1; /* invalid alignment */
+
+ return ilog2_64(v);
+}
diff --git a/lib/md5c.c b/lib/md5c.c
new file mode 100644
index 00000000..5d5c91a3
--- /dev/null
+++ b/lib/md5c.c
@@ -0,0 +1,247 @@
+/*
+ * This code implements the MD5 message-digest algorithm.
+ * The algorithm is due to Ron Rivest. This code was
+ * written by Colin Plumb in 1993, no copyright is claimed.
+ * This code is in the public domain; do with it what you wish.
+ *
+ * Equivalent code is available from RSA Data Security, Inc.
+ * This code has been tested against that, and is equivalent,
+ * except that you don't need to include two pages of legalese
+ * with every copy.
+ *
+ * To compute the message digest of a chunk of bytes, declare an
+ * MD5Context structure, pass it to MD5Init, call MD5Update as
+ * needed on buffers full of bytes, and then call MD5Final, which
+ * will fill a supplied 16-byte array with the digest.
+ */
+
+#include "md5.h"
+#include <string.h> /* for memcpy() */
+
+#ifdef WORDS_LITTEENDIAN
+#define byteReverse(buf, len) /* Nothing */
+#else
+static void byteReverse(unsigned char *buf, unsigned longs);
+
+/*
+ * Note: this code is harmless on little-endian machines.
+ */
+static void byteReverse(unsigned char *buf, unsigned longs)
+{
+ uint32_t t;
+ do {
+ t = (uint32_t) ((unsigned) buf[3] << 8 | buf[2]) << 16 |
+ ((unsigned) buf[1] << 8 | buf[0]);
+ *(uint32_t *) buf = t;
+ buf += 4;
+ } while (--longs);
+}
+#endif
+
+/*
+ * Start MD5 accumulation. Set bit count to 0 and buffer to mysterious
+ * initialization constants.
+ */
+void MD5Init(MD5_CTX *ctx)
+{
+ ctx->buf[0] = 0x67452301;
+ ctx->buf[1] = 0xefcdab89;
+ ctx->buf[2] = 0x98badcfe;
+ ctx->buf[3] = 0x10325476;
+
+ ctx->bits[0] = 0;
+ ctx->bits[1] = 0;
+}
+
+/*
+ * Update context to reflect the concatenation of another buffer full
+ * of bytes.
+ */
+void MD5Update(MD5_CTX *ctx, unsigned char const *buf, unsigned len)
+{
+ uint32_t t;
+
+ /* Update bitcount */
+
+ t = ctx->bits[0];
+ if ((ctx->bits[0] = t + ((uint32_t) len << 3)) < t)
+ ctx->bits[1]++; /* Carry from low to high */
+ ctx->bits[1] += len >> 29;
+
+ t = (t >> 3) & 0x3f; /* Bytes already in shsInfo->data */
+
+ /* Handle any leading odd-sized chunks */
+
+ if (t) {
+ unsigned char *p = (unsigned char *) ctx->in + t;
+
+ t = 64 - t;
+ if (len < t) {
+ memcpy(p, buf, len);
+ return;
+ }
+ memcpy(p, buf, t);
+ byteReverse(ctx->in, 16);
+ MD5Transform(ctx->buf, (uint32_t *) ctx->in);
+ buf += t;
+ len -= t;
+ }
+ /* Process data in 64-byte chunks */
+
+ while (len >= 64) {
+ memcpy(ctx->in, buf, 64);
+ byteReverse(ctx->in, 16);
+ MD5Transform(ctx->buf, (uint32_t *) ctx->in);
+ buf += 64;
+ len -= 64;
+ }
+
+ /* Handle any remaining bytes of data. */
+
+ memcpy(ctx->in, buf, len);
+}
+
+/*
+ * Final wrapup - pad to 64-byte boundary with the bit pattern
+ * 1 0* (64-bit count of bits processed, MSB-first)
+ */
+void MD5Final(unsigned char digest[16], MD5_CTX *ctx)
+{
+ unsigned count;
+ unsigned char *p;
+
+ /* Compute number of bytes mod 64 */
+ count = (ctx->bits[0] >> 3) & 0x3F;
+
+ /* Set the first char of padding to 0x80. This is safe since there is
+ always at least one byte free */
+ p = ctx->in + count;
+ *p++ = 0x80;
+
+ /* Bytes of padding needed to make 64 bytes */
+ count = 64 - 1 - count;
+
+ /* Pad out to 56 mod 64 */
+ if (count < 8) {
+ /* Two lots of padding: Pad the first block to 64 bytes */
+ memset(p, 0, count);
+ byteReverse(ctx->in, 16);
+ MD5Transform(ctx->buf, (uint32_t *) ctx->in);
+
+ /* Now fill the next block with 56 bytes */
+ memset(ctx->in, 0, 56);
+ } else {
+ /* Pad block to 56 bytes */
+ memset(p, 0, count - 8);
+ }
+ byteReverse(ctx->in, 14);
+
+ /* Append length in bits and transform */
+ ((uint32_t *) ctx->in)[14] = ctx->bits[0];
+ ((uint32_t *) ctx->in)[15] = ctx->bits[1];
+
+ MD5Transform(ctx->buf, (uint32_t *) ctx->in);
+ byteReverse((unsigned char *) ctx->buf, 4);
+ memcpy(digest, ctx->buf, 16);
+ memset((char *) ctx, 0, sizeof(ctx)); /* In case it's sensitive */
+}
+
+/* The four core functions - F1 is optimized somewhat */
+
+/* #define F1(x, y, z) (x & y | ~x & z) */
+#define F1(x, y, z) (z ^ (x & (y ^ z)))
+#define F2(x, y, z) F1(z, x, y)
+#define F3(x, y, z) (x ^ y ^ z)
+#define F4(x, y, z) (y ^ (x | ~z))
+
+/* This is the central step in the MD5 algorithm. */
+#define MD5STEP(f, w, x, y, z, data, s) \
+ ( w += f(x, y, z) + data, w = w<<s | w>>(32-s), w += x )
+
+/*
+ * The core of the MD5 algorithm, this alters an existing MD5 hash to
+ * reflect the addition of 16 longwords of new data. MD5Update blocks
+ * the data and converts bytes into longwords for this routine.
+ */
+void MD5Transform(uint32_t buf[4], uint32_t const in[16])
+{
+ register uint32_t a, b, c, d;
+
+ a = buf[0];
+ b = buf[1];
+ c = buf[2];
+ d = buf[3];
+
+ MD5STEP(F1, a, b, c, d, in[0] + 0xd76aa478, 7);
+ MD5STEP(F1, d, a, b, c, in[1] + 0xe8c7b756, 12);
+ MD5STEP(F1, c, d, a, b, in[2] + 0x242070db, 17);
+ MD5STEP(F1, b, c, d, a, in[3] + 0xc1bdceee, 22);
+ MD5STEP(F1, a, b, c, d, in[4] + 0xf57c0faf, 7);
+ MD5STEP(F1, d, a, b, c, in[5] + 0x4787c62a, 12);
+ MD5STEP(F1, c, d, a, b, in[6] + 0xa8304613, 17);
+ MD5STEP(F1, b, c, d, a, in[7] + 0xfd469501, 22);
+ MD5STEP(F1, a, b, c, d, in[8] + 0x698098d8, 7);
+ MD5STEP(F1, d, a, b, c, in[9] + 0x8b44f7af, 12);
+ MD5STEP(F1, c, d, a, b, in[10] + 0xffff5bb1, 17);
+ MD5STEP(F1, b, c, d, a, in[11] + 0x895cd7be, 22);
+ MD5STEP(F1, a, b, c, d, in[12] + 0x6b901122, 7);
+ MD5STEP(F1, d, a, b, c, in[13] + 0xfd987193, 12);
+ MD5STEP(F1, c, d, a, b, in[14] + 0xa679438e, 17);
+ MD5STEP(F1, b, c, d, a, in[15] + 0x49b40821, 22);
+
+ MD5STEP(F2, a, b, c, d, in[1] + 0xf61e2562, 5);
+ MD5STEP(F2, d, a, b, c, in[6] + 0xc040b340, 9);
+ MD5STEP(F2, c, d, a, b, in[11] + 0x265e5a51, 14);
+ MD5STEP(F2, b, c, d, a, in[0] + 0xe9b6c7aa, 20);
+ MD5STEP(F2, a, b, c, d, in[5] + 0xd62f105d, 5);
+ MD5STEP(F2, d, a, b, c, in[10] + 0x02441453, 9);
+ MD5STEP(F2, c, d, a, b, in[15] + 0xd8a1e681, 14);
+ MD5STEP(F2, b, c, d, a, in[4] + 0xe7d3fbc8, 20);
+ MD5STEP(F2, a, b, c, d, in[9] + 0x21e1cde6, 5);
+ MD5STEP(F2, d, a, b, c, in[14] + 0xc33707d6, 9);
+ MD5STEP(F2, c, d, a, b, in[3] + 0xf4d50d87, 14);
+ MD5STEP(F2, b, c, d, a, in[8] + 0x455a14ed, 20);
+ MD5STEP(F2, a, b, c, d, in[13] + 0xa9e3e905, 5);
+ MD5STEP(F2, d, a, b, c, in[2] + 0xfcefa3f8, 9);
+ MD5STEP(F2, c, d, a, b, in[7] + 0x676f02d9, 14);
+ MD5STEP(F2, b, c, d, a, in[12] + 0x8d2a4c8a, 20);
+
+ MD5STEP(F3, a, b, c, d, in[5] + 0xfffa3942, 4);
+ MD5STEP(F3, d, a, b, c, in[8] + 0x8771f681, 11);
+ MD5STEP(F3, c, d, a, b, in[11] + 0x6d9d6122, 16);
+ MD5STEP(F3, b, c, d, a, in[14] + 0xfde5380c, 23);
+ MD5STEP(F3, a, b, c, d, in[1] + 0xa4beea44, 4);
+ MD5STEP(F3, d, a, b, c, in[4] + 0x4bdecfa9, 11);
+ MD5STEP(F3, c, d, a, b, in[7] + 0xf6bb4b60, 16);
+ MD5STEP(F3, b, c, d, a, in[10] + 0xbebfbc70, 23);
+ MD5STEP(F3, a, b, c, d, in[13] + 0x289b7ec6, 4);
+ MD5STEP(F3, d, a, b, c, in[0] + 0xeaa127fa, 11);
+ MD5STEP(F3, c, d, a, b, in[3] + 0xd4ef3085, 16);
+ MD5STEP(F3, b, c, d, a, in[6] + 0x04881d05, 23);
+ MD5STEP(F3, a, b, c, d, in[9] + 0xd9d4d039, 4);
+ MD5STEP(F3, d, a, b, c, in[12] + 0xe6db99e5, 11);
+ MD5STEP(F3, c, d, a, b, in[15] + 0x1fa27cf8, 16);
+ MD5STEP(F3, b, c, d, a, in[2] + 0xc4ac5665, 23);
+
+ MD5STEP(F4, a, b, c, d, in[0] + 0xf4292244, 6);
+ MD5STEP(F4, d, a, b, c, in[7] + 0x432aff97, 10);
+ MD5STEP(F4, c, d, a, b, in[14] + 0xab9423a7, 15);
+ MD5STEP(F4, b, c, d, a, in[5] + 0xfc93a039, 21);
+ MD5STEP(F4, a, b, c, d, in[12] + 0x655b59c3, 6);
+ MD5STEP(F4, d, a, b, c, in[3] + 0x8f0ccc92, 10);
+ MD5STEP(F4, c, d, a, b, in[10] + 0xffeff47d, 15);
+ MD5STEP(F4, b, c, d, a, in[1] + 0x85845dd1, 21);
+ MD5STEP(F4, a, b, c, d, in[8] + 0x6fa87e4f, 6);
+ MD5STEP(F4, d, a, b, c, in[15] + 0xfe2ce6e0, 10);
+ MD5STEP(F4, c, d, a, b, in[6] + 0xa3014314, 15);
+ MD5STEP(F4, b, c, d, a, in[13] + 0x4e0811a1, 21);
+ MD5STEP(F4, a, b, c, d, in[4] + 0xf7537e82, 6);
+ MD5STEP(F4, d, a, b, c, in[11] + 0xbd3af235, 10);
+ MD5STEP(F4, c, d, a, b, in[2] + 0x2ad7d2bb, 15);
+ MD5STEP(F4, b, c, d, a, in[9] + 0xeb86d391, 21);
+
+ buf[0] += a;
+ buf[1] += b;
+ buf[2] += c;
+ buf[3] += d;
+}
diff --git a/lib/nasmlib.c b/lib/nasmlib.c
new file mode 100644
index 00000000..7bc1ffe5
--- /dev/null
+++ b/lib/nasmlib.c
@@ -0,0 +1,669 @@
+/* ----------------------------------------------------------------------- *
+ *
+ * Copyright 1996-2016 The NASM Authors - All Rights Reserved
+ * See the file AUTHORS included with the NASM distribution for
+ * the specific copyright holders.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * nasmlib.c library routines for the Netwide Assembler
+ */
+
+#include "compiler.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <ctype.h>
+#include <errno.h>
+#include <inttypes.h>
+
+#include "nasm.h"
+#include "nasmlib.h"
+#include "insns.h"
+
+int globalbits = 0; /* defined in nasm.h, works better here for ASM+DISASM */
+vefunc nasm_verror; /* Global error handling function */
+
+/* Uninitialized -> all zero by C spec */
+const uint8_t zero_buffer[ZERO_BUF_SIZE];
+
+/*
+ * Prepare a table of tolower() results. This avoids function calls
+ * on some platforms.
+ */
+
+unsigned char nasm_tolower_tab[256];
+
+void tolower_init(void)
+{
+ int i;
+
+ for (i = 0; i < 256; i++)
+ nasm_tolower_tab[i] = tolower(i);
+}
+
+void nasm_error(int severity, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ nasm_verror(severity, fmt, ap);
+ va_end(ap);
+}
+
+no_return nasm_fatal(int flags, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ nasm_verror(flags | ERR_FATAL, fmt, ap);
+ abort(); /* We should never get here */
+}
+
+no_return nasm_panic(int flags, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ nasm_verror(flags | ERR_PANIC, fmt, ap);
+ abort(); /* We should never get here */
+}
+
+no_return nasm_panic_from_macro(const char *file, int line)
+{
+ nasm_panic(ERR_NOFILE, "Internal error at %s:%d\n", file, line);
+}
+
+void *nasm_malloc(size_t size)
+{
+ void *p = malloc(size);
+ if (!p)
+ nasm_fatal(ERR_NOFILE, "out of memory");
+ return p;
+}
+
+void *nasm_zalloc(size_t size)
+{
+ void *p = calloc(size, 1);
+ if (!p)
+ nasm_fatal(ERR_NOFILE, "out of memory");
+ return p;
+}
+
+void *nasm_realloc(void *q, size_t size)
+{
+ void *p = q ? realloc(q, size) : malloc(size);
+ if (!p)
+ nasm_fatal(ERR_NOFILE, "out of memory");
+ return p;
+}
+
+void nasm_free(void *q)
+{
+ if (q)
+ free(q);
+}
+
+char *nasm_strdup(const char *s)
+{
+ char *p;
+ int size = strlen(s) + 1;
+
+ p = malloc(size);
+ if (!p)
+ nasm_fatal(ERR_NOFILE, "out of memory");
+ strcpy(p, s);
+ return p;
+}
+
+char *nasm_strndup(const char *s, size_t len)
+{
+ char *p;
+ int size = len + 1;
+
+ p = malloc(size);
+ if (!p)
+ nasm_fatal(ERR_NOFILE, "out of memory");
+ strncpy(p, s, len);
+ p[len] = '\0';
+ return p;
+}
+
+no_return nasm_assert_failed(const char *file, int line, const char *msg)
+{
+ nasm_fatal(0, "assertion %s failed at %s:%d", msg, file, line);
+ exit(1);
+}
+
+#ifndef nasm_stricmp
+int nasm_stricmp(const char *s1, const char *s2)
+{
+ unsigned char c1, c2;
+ int d;
+
+ while (1) {
+ c1 = nasm_tolower(*s1++);
+ c2 = nasm_tolower(*s2++);
+ d = c1-c2;
+
+ if (d)
+ return d;
+ if (!c1)
+ break;
+ }
+ return 0;
+}
+#endif
+
+#ifndef nasm_strnicmp
+int nasm_strnicmp(const char *s1, const char *s2, size_t n)
+{
+ unsigned char c1, c2;
+ int d;
+
+ while (n--) {
+ c1 = nasm_tolower(*s1++);
+ c2 = nasm_tolower(*s2++);
+ d = c1-c2;
+
+ if (d)
+ return d;
+ if (!c1)
+ break;
+ }
+ return 0;
+}
+#endif
+
+int nasm_memicmp(const char *s1, const char *s2, size_t n)
+{
+ unsigned char c1, c2;
+ int d;
+
+ while (n--) {
+ c1 = nasm_tolower(*s1++);
+ c2 = nasm_tolower(*s2++);
+ d = c1-c2;
+ if (d)
+ return d;
+ }
+ return 0;
+}
+
+#ifndef nasm_strsep
+char *nasm_strsep(char **stringp, const char *delim)
+{
+ char *s = *stringp;
+ char *e;
+
+ if (!s)
+ return NULL;
+
+ e = strpbrk(s, delim);
+ if (e)
+ *e++ = '\0';
+
+ *stringp = e;
+ return s;
+}
+#endif
+
+
+#define lib_isnumchar(c) (nasm_isalnum(c) || (c) == '$' || (c) == '_')
+
+static int radix_letter(char c)
+{
+ switch (c) {
+ case 'b': case 'B':
+ case 'y': case 'Y':
+ return 2; /* Binary */
+ case 'o': case 'O':
+ case 'q': case 'Q':
+ return 8; /* Octal */
+ case 'h': case 'H':
+ case 'x': case 'X':
+ return 16; /* Hexadecimal */
+ case 'd': case 'D':
+ case 't': case 'T':
+ return 10; /* Decimal */
+ default:
+ return 0; /* Not a known radix letter */
+ }
+}
+
+int64_t readnum(char *str, bool *error)
+{
+ char *r = str, *q;
+ int32_t pradix, sradix, radix;
+ int plen, slen, len;
+ uint64_t result, checklimit;
+ int digit, last;
+ bool warn = false;
+ int sign = 1;
+
+ *error = false;
+
+ while (nasm_isspace(*r))
+ r++; /* find start of number */
+
+ /*
+ * If the number came from make_tok_num (as a result of an %assign), it
+ * might have a '-' built into it (rather than in a preceeding token).
+ */
+ if (*r == '-') {
+ r++;
+ sign = -1;
+ }
+
+ q = r;
+
+ while (lib_isnumchar(*q))
+ q++; /* find end of number */
+
+ len = q-r;
+ if (!len) {
+ /* Not numeric */
+ *error = true;
+ return 0;
+ }
+
+ /*
+ * Handle radix formats:
+ *
+ * 0<radix-letter><string>
+ * $<string> (hexadecimal)
+ * <string><radix-letter>
+ */
+ pradix = sradix = 0;
+ plen = slen = 0;
+
+ if (len > 2 && *r == '0' && (pradix = radix_letter(r[1])) != 0)
+ plen = 2;
+ else if (len > 1 && *r == '$')
+ pradix = 16, plen = 1;
+
+ if (len > 1 && (sradix = radix_letter(q[-1])) != 0)
+ slen = 1;
+
+ if (pradix > sradix) {
+ radix = pradix;
+ r += plen;
+ } else if (sradix > pradix) {
+ radix = sradix;
+ q -= slen;
+ } else {
+ /* Either decimal, or invalid -- if invalid, we'll trip up
+ further down. */
+ radix = 10;
+ }
+
+ /*
+ * `checklimit' must be 2**64 / radix. We can't do that in
+ * 64-bit arithmetic, which we're (probably) using, so we
+ * cheat: since we know that all radices we use are even, we
+ * can divide 2**63 by radix/2 instead.
+ */
+ checklimit = UINT64_C(0x8000000000000000) / (radix >> 1);
+
+ /*
+ * Calculate the highest allowable value for the last digit of a
+ * 64-bit constant... in radix 10, it is 6, otherwise it is 0
+ */
+ last = (radix == 10 ? 6 : 0);
+
+ result = 0;
+ while (*r && r < q) {
+ if (*r != '_') {
+ if (*r < '0' || (*r > '9' && *r < 'A')
+ || (digit = numvalue(*r)) >= radix) {
+ *error = true;
+ return 0;
+ }
+ if (result > checklimit ||
+ (result == checklimit && digit >= last)) {
+ warn = true;
+ }
+
+ result = radix * result + digit;
+ }
+ r++;
+ }
+
+ if (warn)
+ nasm_error(ERR_WARNING | ERR_PASS1 | ERR_WARN_NOV,
+ "numeric constant %s does not fit in 64 bits",
+ str);
+
+ return result * sign;
+}
+
+int64_t readstrnum(char *str, int length, bool *warn)
+{
+ int64_t charconst = 0;
+ int i;
+
+ *warn = false;
+
+ str += length;
+ if (globalbits == 64) {
+ for (i = 0; i < length; i++) {
+ if (charconst & UINT64_C(0xFF00000000000000))
+ *warn = true;
+ charconst = (charconst << 8) + (uint8_t)*--str;
+ }
+ } else {
+ for (i = 0; i < length; i++) {
+ if (charconst & 0xFF000000UL)
+ *warn = true;
+ charconst = (charconst << 8) + (uint8_t)*--str;
+ }
+ }
+ return charconst;
+}
+
+int32_t seg_alloc(void)
+{
+ static int32_t next_seg = 0;
+ int32_t this_seg = next_seg;
+
+ next_seg += 2;
+
+ return this_seg;
+}
+
+void standard_extension(char *inname, char *outname, char *extension)
+{
+ char *p, *q;
+
+ if (*outname) /* file name already exists, */
+ return; /* so do nothing */
+ q = inname;
+ p = outname;
+ while (*q)
+ *p++ = *q++; /* copy, and find end of string */
+ *p = '\0'; /* terminate it */
+ while (p > outname && *--p != '.') ; /* find final period (or whatever) */
+ if (*p != '.')
+ while (*p)
+ p++; /* go back to end if none found */
+ if (!strcmp(p, extension)) { /* is the extension already there? */
+ if (*extension)
+ nasm_error(ERR_WARNING | ERR_NOFILE,
+ "file name already ends in `%s': "
+ "output will be in `nasm.out'", extension);
+ else
+ nasm_error(ERR_WARNING | ERR_NOFILE,
+ "file name already has no extension: "
+ "output will be in `nasm.out'");
+ strcpy(outname, "nasm.out");
+ } else
+ strcpy(p, extension);
+}
+
+/*
+ * Common list of prefix names
+ */
+static const char *prefix_names[] = {
+ "a16", "a32", "a64", "asp", "lock", "o16", "o32", "o64", "osp",
+ "rep", "repe", "repne", "repnz", "repz", "times", "wait",
+ "xacquire", "xrelease", "bnd"
+};
+
+const char *prefix_name(int token)
+{
+ unsigned int prefix = token-PREFIX_ENUM_START;
+ if (prefix >= ARRAY_SIZE(prefix_names))
+ return NULL;
+
+ return prefix_names[prefix];
+}
+
+/*
+ * Binary search.
+ */
+int bsi(const char *string, const char **array, int size)
+{
+ int i = -1, j = size; /* always, i < index < j */
+ while (j - i >= 2) {
+ int k = (i + j) / 2;
+ int l = strcmp(string, array[k]);
+ if (l < 0) /* it's in the first half */
+ j = k;
+ else if (l > 0) /* it's in the second half */
+ i = k;
+ else /* we've got it :) */
+ return k;
+ }
+ return -1; /* we haven't got it :( */
+}
+
+int bsii(const char *string, const char **array, int size)
+{
+ int i = -1, j = size; /* always, i < index < j */
+ while (j - i >= 2) {
+ int k = (i + j) / 2;
+ int l = nasm_stricmp(string, array[k]);
+ if (l < 0) /* it's in the first half */
+ j = k;
+ else if (l > 0) /* it's in the second half */
+ i = k;
+ else /* we've got it :) */
+ return k;
+ }
+ return -1; /* we haven't got it :( */
+}
+
+static char *file_name = NULL;
+static int32_t line_number = 0;
+
+char *src_set_fname(char *newname)
+{
+ char *oldname = file_name;
+ file_name = newname;
+ return oldname;
+}
+
+int32_t src_set_linnum(int32_t newline)
+{
+ int32_t oldline = line_number;
+ line_number = newline;
+ return oldline;
+}
+
+int32_t src_get_linnum(void)
+{
+ return line_number;
+}
+
+int src_get(int32_t *xline, char **xname)
+{
+ if (!file_name || !*xname || strcmp(*xname, file_name)) {
+ nasm_free(*xname);
+ *xname = file_name ? nasm_strdup(file_name) : NULL;
+ *xline = line_number;
+ return -2;
+ }
+ if (*xline != line_number) {
+ int32_t tmp = line_number - *xline;
+ *xline = line_number;
+ return tmp;
+ }
+ return 0;
+}
+
+char *nasm_strcat(const char *one, const char *two)
+{
+ char *rslt;
+ int l1 = strlen(one);
+ rslt = nasm_malloc(l1 + strlen(two) + 1);
+ strcpy(rslt, one);
+ strcpy(rslt + l1, two);
+ return rslt;
+}
+
+/* skip leading spaces */
+char *nasm_skip_spaces(const char *p)
+{
+ if (p)
+ while (*p && nasm_isspace(*p))
+ p++;
+ return (char *)p;
+}
+
+/* skip leading non-spaces */
+char *nasm_skip_word(const char *p)
+{
+ if (p)
+ while (*p && !nasm_isspace(*p))
+ p++;
+ return (char *)p;
+}
+
+/* zap leading spaces with zero */
+char *nasm_zap_spaces_fwd(char *p)
+{
+ if (p)
+ while (*p && nasm_isspace(*p))
+ *p++ = 0x0;
+ return p;
+}
+
+/* zap spaces with zero in reverse order */
+char *nasm_zap_spaces_rev(char *p)
+{
+ if (p)
+ while (*p && nasm_isspace(*p))
+ *p-- = 0x0;
+ return p;
+}
+
+/* zap leading and trailing spaces */
+char *nasm_trim_spaces(char *p)
+{
+ p = nasm_zap_spaces_fwd(p);
+ nasm_zap_spaces_fwd(nasm_skip_word(p));
+
+ return p;
+}
+
+/*
+ * return the word extracted from a stream
+ * or NULL if nothing left
+ */
+char *nasm_get_word(char *p, char **tail)
+{
+ char *word = nasm_skip_spaces(p);
+ char *next = nasm_skip_word(word);
+
+ if (word && *word) {
+ if (*next)
+ *next++ = '\0';
+ } else
+ word = next = NULL;
+
+ /* NOTE: the tail may start with spaces */
+ *tail = next;
+
+ return word;
+}
+
+/*
+ * Extract "opt=val" values from the stream and
+ * returns "opt"
+ *
+ * Exceptions:
+ * 1) If "=val" passed the NULL returned though
+ * you may continue handling the tail via "next"
+ * 2) If "=" passed the NULL is returned and "val"
+ * is set to NULL as well
+ */
+char *nasm_opt_val(char *p, char **val, char **next)
+{
+ char *q, *nxt;
+
+ *val = *next = NULL;
+
+ p = nasm_get_word(p, &nxt);
+ if (!p)
+ return NULL;
+
+ q = strchr(p, '=');
+ if (q) {
+ if (q == p)
+ p = NULL;
+ *q++='\0';
+ if (*q) {
+ *val = q;
+ } else {
+ q = nasm_get_word(q + 1, &nxt);
+ if (q)
+ *val = q;
+ }
+ } else {
+ q = nasm_skip_spaces(nxt);
+ if (q && *q == '=') {
+ q = nasm_get_word(q + 1, &nxt);
+ if (q)
+ *val = q;
+ }
+ }
+
+ *next = nxt;
+ return p;
+}
+
+/*
+ * initialized data bytes length from opcode
+ */
+int idata_bytes(int opcode)
+{
+ switch (opcode) {
+ case I_DB:
+ return 1;
+ case I_DW:
+ return 2;
+ case I_DD:
+ return 4;
+ case I_DQ:
+ return 8;
+ case I_DT:
+ return 10;
+ case I_DO:
+ return 16;
+ case I_DY:
+ return 32;
+ case I_DZ:
+ return 64;
+ case I_none:
+ return -1;
+ default:
+ return 0;
+ }
+}
diff --git a/lib/realpath.c b/lib/realpath.c
new file mode 100644
index 00000000..d93dc15f
--- /dev/null
+++ b/lib/realpath.c
@@ -0,0 +1,135 @@
+/* ----------------------------------------------------------------------- *
+ *
+ * Copyright 2016 The NASM Authors - All Rights Reserved
+ * See the file AUTHORS included with the NASM distribution for
+ * the specific copyright holders.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * ----------------------------------------------------------------------- */
+
+/*
+ * realpath.c As system-independent as possible implementation of realpath()
+ */
+
+#include "compiler.h"
+
+#include <stdlib.h>
+#include <errno.h>
+#include <limits.h>
+#ifdef HAVE_UNISTD_H
+# include <unistd.h>
+#endif
+#ifdef HAVE_SYS_PARAM_H
+# include <sys/param.h>
+#endif
+
+#include "nasmlib.h"
+
+#ifdef HAVE_CANONICALIZE_FILE_NAME
+
+/*
+ * GNU-specific, but avoids the realpath(..., NULL)
+ * portability problem if it exists.
+ */
+char *nasm_realpath(const char *rel_path)
+{
+ char *rp = canonicalize_file_name(rel_path);
+ return rp ? rp : nasm_strdup(rel_path);
+}
+
+#elif defined(HAVE_REALPATH)
+
+/*
+ * POSIX.1-2008 defines realpath(..., NULL); POSIX.1-2001 doesn't guarantee
+ * that a NULL second argument is supported.
+ */
+
+char *nasm_realpath(const char *rel_path)
+{
+ char *rp;
+
+ rp = realpath(rel_path, NULL);
+
+ /* Not all implemetations of realpath() support a NULL second argument */
+ if (!rp && errno == EINVAL) {
+ long path_max = -1;
+ char *rp;
+
+#if defined(HAVE_PATHCONF) && defined(_PC_PATH_MAX)
+ path_max = pathconf(rel_path, _PC_PATH_MAX); /* POSIX */
+#endif
+
+ if (path_max < 0) {
+#ifdef PATH_MAX
+ path_max = PATH_MAX; /* SUSv2 */
+#elif defined(MAXPATHLEN)
+ path_max = MAXPATHLEN; /* Solaris */
+#else
+ path_max = 65536; /* Crazily high, we hope */
+#endif
+ }
+
+ rp = nasm_malloc(path_max);
+
+ if (!realpath(rel_path, rp)) {
+ nasm_free(rp);
+ rp = NULL;
+ } else {
+ /* On some systems, pathconf() can return a very large value */
+
+ rp[path_max - 1] = '\0'; /* Just in case overrun is possible */
+ rp = nasm_realloc(rp, strlen(rp) + 1);
+ }
+ }
+
+ return rp ? rp : nasm_strdup(rel_path);
+}
+
+#elif defined(HAVE__FULLPATH)
+
+/*
+ * win32/win64 API
+ */
+
+char *nasm_realpath(const char *rel_path)
+{
+ char *rp = _fullpath(NULL, rel_path, 0);
+ return rp ? rp : nasm_strdup(rel_path);
+}
+
+#else
+
+/*
+ * There is nothing we know how to do here, so hope it just works anyway.
+ */
+
+char *nasm_realpath(const char *rel_path)
+{
+ return nasm_strdup(rel_path);
+}
+
+#endif
diff --git a/lib/snprintf.c b/lib/snprintf.c
index 01734dcf..95bfc314 100644
--- a/lib/snprintf.c
+++ b/lib/snprintf.c
@@ -12,6 +12,8 @@
#include "nasmlib.h"
+#if !defined(HAVE_SNPRINTF) && !defined(HAVE__SNPRINTF)
+
int snprintf(char *str, size_t size, const char *format, ...)
{
va_list ap;
@@ -23,3 +25,5 @@ int snprintf(char *str, size_t size, const char *format, ...)
return rv;
}
+
+#endif
diff --git a/lib/ver.c b/lib/ver.c
new file mode 100644
index 00000000..a7c4608e
--- /dev/null
+++ b/lib/ver.c
@@ -0,0 +1,51 @@
+/* ----------------------------------------------------------------------- *
+ *
+ * Copyright 1996-2009 The NASM Authors - All Rights Reserved
+ * See the file AUTHORS included with the NASM distribution for
+ * the specific copyright holders.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following
+ * conditions are met:
+ *
+ * * Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * * Redistributions in binary form must reproduce the above
+ * copyright notice, this list of conditions and the following
+ * disclaimer in the documentation and/or other materials provided
+ * with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
+ * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
+ * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
+ * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
+ * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * ----------------------------------------------------------------------- */
+
+#include "nasm.h"
+#include "version.h"
+
+/* This is printed when entering nasm -v */
+const char nasm_version[] = NASM_VER;
+const char nasm_date[] = __DATE__;
+const char nasm_compile_options[] = ""
+#ifdef DEBUG
+ " with -DDEBUG"
+#endif
+ ;
+
+/* These are used by some backends. */
+const char nasm_comment[] =
+ "The Netwide Assembler " NASM_VER;
+
+const char nasm_signature[] =
+ "NASM " NASM_VER;
diff --git a/lib/vsnprintf.c b/lib/vsnprintf.c
index ecca3e66..b8fd082a 100644
--- a/lib/vsnprintf.c
+++ b/lib/vsnprintf.c
@@ -14,6 +14,8 @@
#include "nasmlib.h"
+#if !defined(HAVE_VSNPRINTF) && !defined(HAVE__VSNPRINTF)
+
#define BUFFER_SIZE 65536 /* Bigger than any string we might print... */
static char snprintf_buffer[BUFFER_SIZE];
@@ -44,3 +46,5 @@ int vsnprintf(char *str, size_t size, const char *format, va_list ap)
return rv;
}
+
+#endif