H. Peter Anvin | bdae10b | 2016-03-08 01:22:13 -0800 | [diff] [blame] | 1 | /* ----------------------------------------------------------------------- * |
| 2 | * |
| 3 | * Copyright 1996-2016 The NASM Authors - All Rights Reserved |
| 4 | * See the file AUTHORS included with the NASM distribution for |
| 5 | * the specific copyright holders. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following |
| 9 | * conditions are met: |
| 10 | * |
| 11 | * * Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * * Redistributions in binary form must reproduce the above |
| 14 | * copyright notice, this list of conditions and the following |
| 15 | * disclaimer in the documentation and/or other materials provided |
| 16 | * with the distribution. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 19 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 20 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
| 23 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 25 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 26 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 27 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 29 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 30 | * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | * |
| 32 | * ----------------------------------------------------------------------- */ |
| 33 | |
| 34 | #include "compiler.h" |
| 35 | #include "nasmlib.h" |
| 36 | |
| 37 | #include <errno.h> |
| 38 | |
| 39 | #ifdef HAVE_IO_H |
| 40 | # include <io.h> |
| 41 | #endif |
| 42 | #ifdef HAVE_UNISTD_H |
| 43 | # include <unistd.h> |
| 44 | #endif |
| 45 | |
| 46 | void nasm_write(const void *ptr, size_t size, FILE *f) |
| 47 | { |
| 48 | size_t n = fwrite(ptr, 1, size, f); |
| 49 | if (n != size || ferror(f) || feof(f)) |
| 50 | nasm_fatal(0, "unable to write output: %s", strerror(errno)); |
| 51 | } |
| 52 | |
| 53 | #ifdef WORDS_LITTLEENDIAN |
| 54 | |
| 55 | void fwriteint16_t(uint16_t data, FILE * fp) |
| 56 | { |
| 57 | nasm_write(&data, 2, fp); |
| 58 | } |
| 59 | |
| 60 | void fwriteint32_t(uint32_t data, FILE * fp) |
| 61 | { |
| 62 | nasm_write(&data, 4, fp); |
| 63 | } |
| 64 | |
| 65 | void fwriteint64_t(uint64_t data, FILE * fp) |
| 66 | { |
| 67 | nasm_write(&data, 8, fp); |
| 68 | } |
| 69 | |
| 70 | void fwriteaddr(uint64_t data, int size, FILE * fp) |
| 71 | { |
| 72 | nasm_write(&data, size, fp); |
| 73 | } |
| 74 | |
| 75 | #else /* not WORDS_LITTLEENDIAN */ |
| 76 | |
| 77 | void fwriteint16_t(uint16_t data, FILE * fp) |
| 78 | { |
| 79 | char buffer[2], *p = buffer; |
| 80 | WRITESHORT(p, data); |
| 81 | nasm_write(buffer, 2, fp); |
| 82 | } |
| 83 | |
| 84 | void fwriteint32_t(uint32_t data, FILE * fp) |
| 85 | { |
| 86 | char buffer[4], *p = buffer; |
| 87 | WRITELONG(p, data); |
| 88 | nasm_write(buffer, 4, fp); |
| 89 | } |
| 90 | |
| 91 | void fwriteint64_t(uint64_t data, FILE * fp) |
| 92 | { |
| 93 | char buffer[8], *p = buffer; |
| 94 | WRITEDLONG(p, data); |
| 95 | nasm_write(buffer, 8, fp); |
| 96 | } |
| 97 | |
| 98 | void fwriteaddr(uint64_t data, int size, FILE * fp) |
| 99 | { |
| 100 | char buffer[8], *p = buffer; |
| 101 | WRITEADDR(p, data, size); |
| 102 | nasm_write(buffer, size, fp); |
| 103 | } |
| 104 | |
| 105 | #endif |
| 106 | |
| 107 | |
| 108 | #ifdef HAVE_FILENO /* Useless without fileno() */ |
| 109 | # ifdef HAVE__CHSIZE_S |
| 110 | # define nasm_ftruncate(fd,size) _chsize_s(fd,size) |
| 111 | # elif defined(HAVE__CHSIZE) |
| 112 | # define nasm_ftruncate(fd,size) _chsize(fd,size) |
| 113 | # elif defined(HAVE_FTRUNCATE) |
| 114 | # define nasm_ftruncate(fd,size) ftruncate(fd,size) |
| 115 | # endif |
| 116 | #endif |
| 117 | |
| 118 | void fwritezero(size_t bytes, FILE *fp) |
| 119 | { |
| 120 | size_t blksize; |
| 121 | |
| 122 | #ifdef nasm_ftruncate |
| 123 | if (bytes >= BUFSIZ && !ferror(fp) && !feof(fp)) { |
| 124 | off_t pos = ftello(fp); |
| 125 | if (pos >= 0) { |
| 126 | if (!fflush(fp) && |
| 127 | !nasm_ftruncate(fileno(fp), pos + bytes) && |
| 128 | !fseeko(fp, pos+bytes, SEEK_SET)) |
| 129 | return; |
| 130 | } |
| 131 | } |
| 132 | #endif |
| 133 | |
| 134 | while (bytes) { |
| 135 | blksize = (bytes < ZERO_BUF_SIZE) ? bytes : ZERO_BUF_SIZE; |
| 136 | |
| 137 | nasm_write(zero_buffer, blksize, fp); |
| 138 | bytes -= blksize; |
| 139 | } |
| 140 | } |