| 1 |
/* |
| 2 |
Copyright (c) 2018-2024, David Anderson All rights reserved. |
| 3 |
|
| 4 |
Redistribution and use in source and binary forms, with |
| 5 |
or without modification, are permitted provided that the |
| 6 |
following conditions are met: |
| 7 |
|
| 8 |
Redistributions of source code must retain the above |
| 9 |
copyright notice, this list of conditions and the following |
| 10 |
disclaimer. |
| 11 |
|
| 12 |
Redistributions in binary form must reproduce the above |
| 13 |
copyright notice, this list of conditions and the following |
| 14 |
disclaimer in the documentation and/or other materials |
| 15 |
provided with the distribution. |
| 16 |
|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND |
| 18 |
CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, |
| 19 |
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 20 |
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 |
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 22 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 24 |
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 26 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 27 |
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 28 |
OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 29 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 30 |
*/ |
| 31 |
|
| 32 |
#include <config.h> |
| 33 |
|
| 34 |
#include <stdlib.h> /* free() */ |
| 35 |
#include <stdio.h> /* SEEK_END SEEK_SET */ |
| 36 |
#include <string.h> /* memset() strlen() */ |
| 37 |
|
| 38 |
#ifdef _WIN32 |
| 39 |
#ifdef HAVE_STDAFX_H |
| 40 |
#include "stdafx.h" |
| 41 |
#endif /* HAVE_STDAFX_H */ |
| 42 |
#include <io.h> /* lseek() off_t ssize_t */ |
| 43 |
#endif /* _WIN32 */ |
| 44 |
|
| 45 |
#ifdef HAVE_UNISTD_H |
| 46 |
#include <unistd.h> /* close() */ |
| 47 |
#endif /* HAVE_UNISTD_H */ |
| 48 |
|
| 49 |
#ifdef HAVE_FCNTL_H |
| 50 |
#include <fcntl.h> /* open() O_RDONLY */ |
| 51 |
#endif /* HAVE_FCNTL_H */ |
| 52 |
|
| 53 |
#ifdef _WIN64 |
| 54 |
#ifdef lseek /* defined in msys2 in an io.h */ |
| 55 |
#undef lseek |
| 56 |
#endif /* lseek */ |
| 57 |
#define lseek _lseeki64 |
| 58 |
#endif /* _WIN64 */ |
| 59 |
|
| 60 |
#ifdef HAVE_UNISTD_H |
| 61 |
#include <unistd.h> /* lseek() off_t */ |
| 62 |
#endif /* HAVE_UNISTD_H */ |
| 63 |
|
| 64 |
#ifdef HAVE_FCNTL_H |
| 65 |
#include <fcntl.h> /* open() O_RDONLY */ |
| 66 |
#endif /* HAVE_FCNTL_H */ |
| 67 |
|
| 68 |
#include "dwarf.h" |
| 69 |
#include "libdwarf.h" |
| 70 |
#include "libdwarf_private.h" |
| 71 |
#include "dwarf_base_types.h" |
| 72 |
#include "dwarf_opaque.h" |
| 73 |
|
| 74 |
#ifndef O_BINARY |
| 75 |
#define O_BINARY 0 |
| 76 |
#endif /* O_BINARY */ |
| 77 |
#ifndef O_RDONLY |
| 78 |
#define O_RDONLY 0 |
| 79 |
#endif /* O_RDONLY */ |
| 80 |
#ifndef O_CLOEXEC |
| 81 |
#define O_CLOEXEC 0 |
| 82 |
#endif /* O_CLOEXEC */ |
| 83 |
|
| 84 |
#if 0 /* debugging only */ |
| 85 |
static void |
| 86 |
dump_bytes(char * msg,Dwarf_Small * start, long len) |
| 87 |
{ |
| 88 |
Dwarf_Small *end = start + len; |
| 89 |
Dwarf_Small *cur = start; |
| 90 |
|
| 91 |
printf("%s ",msg); |
| 92 |
for (; cur < end; cur++) { |
| 93 |
printf("%02x ", *cur); |
| 94 |
} |
| 95 |
printf("\n"); |
| 96 |
} |
| 97 |
#endif |
| 98 |
|
| 99 |
int |
| 100 |
_dwarf_readr(int fd, |
| 101 |
char *buf, |
| 102 |
Dwarf_Unsigned size, |
| 103 |
Dwarf_Unsigned *sizeread_out) |
| 104 |
{ |
| 105 |
|
| 106 |
Dwarf_Signed rcode = 0; |
| 107 |
#ifdef _WIN64 |
| 108 |
Dwarf_Unsigned max_single_read = 0x1ffff000; |
| 109 |
#elif defined(_WIN32) |
| 110 |
Dwarf_Unsigned max_single_read = 0xffff; |
| 111 |
#else |
| 112 |
Dwarf_Unsigned max_single_read = 0x1ffff000; |
| 113 |
#endif |
| 114 |
Dwarf_Unsigned remaining_bytes = 0; |
| 115 |
Dwarf_Unsigned totalsize = size; |
| 116 |
|
| 117 |
remaining_bytes = size; |
| 118 |
while(remaining_bytes > 0) { |
| 119 |
if (remaining_bytes > max_single_read) { |
| 120 |
size = max_single_read; |
| 121 |
} |
| 122 |
#ifdef _WIN64 |
| 123 |
rcode = (Dwarf_Signed)_read(fd,buf,(unsigned const)size); |
| 124 |
#elif defined(_WIN32) |
| 125 |
rcode = (Dwarf_Signed)_read(fd,buf,(unsigned const)size); |
| 126 |
#else /* linux */ |
| 127 |
rcode = (Dwarf_Signed)read(fd,buf,(size_t)size); |
| 128 |
#endif |
| 129 |
if (rcode < 0 || rcode != (Dwarf_Signed)size) { |
| 130 |
return DW_DLV_ERROR; |
| 131 |
} |
| 132 |
remaining_bytes -= size; |
| 133 |
buf += size; |
| 134 |
size = remaining_bytes; |
| 135 |
} |
| 136 |
if (sizeread_out) { |
| 137 |
*sizeread_out = totalsize; |
| 138 |
} |
| 139 |
return DW_DLV_OK; |
| 140 |
} |
| 141 |
|
| 142 |
int |
| 143 |
_dwarf_seekr(int fd, |
| 144 |
Dwarf_Unsigned loc, |
| 145 |
int seektype, |
| 146 |
Dwarf_Unsigned *out_loc) |
| 147 |
{ |
| 148 |
Dwarf_Signed fsize = 0; |
| 149 |
Dwarf_Signed sloc = 0; |
| 150 |
|
| 151 |
sloc = (Dwarf_Signed)loc; |
| 152 |
if (sloc < 0) { |
| 153 |
return DW_DLV_ERROR; |
| 154 |
} |
| 155 |
#ifdef _WIN64 |
| 156 |
fsize = (Dwarf_Signed)lseek(fd,(__int64)loc,seektype); |
| 157 |
#elif defined(_WIN32) |
| 158 |
fsize = (Dwarf_Signed)lseek(fd,(off_t)loc,seektype); |
| 159 |
#else /* linux */ |
| 160 |
fsize = (Dwarf_Signed)lseek(fd,(off_t)loc,seektype); |
| 161 |
#endif |
| 162 |
if (fsize < 0) { |
| 163 |
return DW_DLV_ERROR; |
| 164 |
} |
| 165 |
if (out_loc) { |
| 166 |
*out_loc = (Dwarf_Unsigned)fsize; |
| 167 |
} |
| 168 |
return DW_DLV_OK; |
| 169 |
} |
| 170 |
|
| 171 |
void |
| 172 |
_dwarf_closer( int fd) |
| 173 |
{ |
| 174 |
#ifdef _WIN64 |
| 175 |
_close(fd); |
| 176 |
#elif defined(_WIN32) |
| 177 |
_close(fd); |
| 178 |
#else /* linux */ |
| 179 |
close(fd); |
| 180 |
#endif |
| 181 |
} |
| 182 |
|
| 183 |
int |
| 184 |
_dwarf_openr( const char *name) |
| 185 |
{ |
| 186 |
|
| 187 |
int fd = -1; |
| 188 |
#ifdef _WIN64 |
| 189 |
fd = _open(name, O_RDONLY | O_BINARY|O_CLOEXEC); |
| 190 |
#elif defined(_WIN32) |
| 191 |
fd = _open(name, O_RDONLY | O_BINARY|O_CLOEXEC); |
| 192 |
#else /* linux */ |
| 193 |
fd = open(name, O_RDONLY | O_BINARY|O_CLOEXEC); |
| 194 |
#endif |
| 195 |
return fd; |
| 196 |
|
| 197 |
} |