| 1 |
/* |
| 2 |
|
| 3 |
Copyright (C) 2020 David Anderson. All Rights Reserved. |
| 4 |
|
| 5 |
This program is free software; you can redistribute it |
| 6 |
and/or modify it under the terms of version 2.1 of the |
| 7 |
GNU Lesser General Public License as published by the Free |
| 8 |
Software Foundation. |
| 9 |
|
| 10 |
This program is distributed in the hope that it would be |
| 11 |
useful, but WITHOUT ANY WARRANTY; without even the implied |
| 12 |
warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 13 |
PURPOSE. |
| 14 |
|
| 15 |
Further, this software is distributed without any warranty |
| 16 |
that it is free of the rightful claim of any third person |
| 17 |
regarding infringement or the like. Any license provided |
| 18 |
herein, whether implied or otherwise, applies only to this |
| 19 |
software file. Patent licenses, if any, provided herein |
| 20 |
do not apply to combinations of this program with other |
| 21 |
software, or any other product whatsoever. |
| 22 |
|
| 23 |
You should have received a copy of the GNU Lesser General |
| 24 |
Public License along with this program; if not, write |
| 25 |
the Free Software Foundation, Inc., 51 Franklin Street - |
| 26 |
Fifth Floor, Boston MA 02110-1301, USA. |
| 27 |
*/ |
| 28 |
|
| 29 |
#include <config.h> |
| 30 |
|
| 31 |
#include <stddef.h> /* size_t */ |
| 32 |
#include <stdio.h> /* SEEK_END SEEK_SET */ |
| 33 |
#include <stdlib.h> /* free() malloc() */ |
| 34 |
#include <string.h> /* memcpy() */ |
| 35 |
|
| 36 |
#include "dwarf.h" |
| 37 |
#include "libdwarf.h" |
| 38 |
#include "libdwarf_private.h" |
| 39 |
#include "dwarf_base_types.h" |
| 40 |
#include "dwarf_util.h" |
| 41 |
#include "dwarf_opaque.h" |
| 42 |
#include "dwarf_error.h" |
| 43 |
|
| 44 |
/* Returns DW_DLV_OK DW_DLV_NO_ENTRY or DW_DLV_ERROR |
| 45 |
crc32 used for debuglink crc calculation. |
| 46 |
Caller passes pointer to an |
| 47 |
uninitialized array of 4 unsigned char |
| 48 |
and if this returns DW_DLV_OK that is filled in. |
| 49 |
The crc is calculated based on reading |
| 50 |
the entire current open |
| 51 |
Dwarf_Debug dbg object file and all bytes in |
| 52 |
the file are read to create the crc. |
| 53 |
|
| 54 |
In Windows, where unsigned int is 16 bits, this |
| 55 |
produces different output than on 32bit ints. |
| 56 |
|
| 57 |
Since lseek() (all systems/versions) returns |
| 58 |
a signed value, we check for < 0 as error |
| 59 |
rather than just check for -1. So it is clear |
| 60 |
to symbolic exectution that conversion to |
| 61 |
unsigned does not lose bits. |
| 62 |
*/ |
| 63 |
int |
| 64 |
dwarf_crc32 (Dwarf_Debug dbg,unsigned char *crcbuf, |
| 65 |
Dwarf_Error *error) |
| 66 |
{ |
| 67 |
/* off_t is signed, defined by POSIX */ |
| 68 |
/* ssize_t is signed, defined in POSIX */ |
| 69 |
|
| 70 |
/* size_t is unsigned, defined in C89. */ |
| 71 |
Dwarf_Unsigned fsize = 0; |
| 72 |
/* Named with u to remind the reader that this is |
| 73 |
an unsigned value. */ |
| 74 |
Dwarf_Unsigned readlenu = 10000; |
| 75 |
Dwarf_Unsigned size_left = 0; |
| 76 |
const unsigned char *readbuf = 0; |
| 77 |
unsigned int tcrc = 0; |
| 78 |
unsigned int init = 0; |
| 79 |
int fd = -1; |
| 80 |
Dwarf_Unsigned sz = 0; |
| 81 |
int res = 0; |
| 82 |
|
| 83 |
CHECK_DBG(dbg,error,"dwarf_crc32()"); |
| 84 |
if (!crcbuf) { |
| 85 |
return DW_DLV_NO_ENTRY; |
| 86 |
} |
| 87 |
if (!dbg->de_owns_fd) { |
| 88 |
return DW_DLV_NO_ENTRY; |
| 89 |
} |
| 90 |
fd = dbg->de_fd; |
| 91 |
if (fd < 0) { |
| 92 |
return DW_DLV_NO_ENTRY; |
| 93 |
} |
| 94 |
fd = dbg->de_fd; |
| 95 |
if (dbg->de_filesize) { |
| 96 |
fsize = (size_t)dbg->de_filesize; |
| 97 |
} else { |
| 98 |
res = _dwarf_seekr(fd,0,SEEK_END,&sz); |
| 99 |
if (res != DW_DLV_OK) { |
| 100 |
_dwarf_error_string(dbg,error,DW_DLE_SEEK_ERROR, |
| 101 |
"DW_DLE_SEEK_ERROR: dwarf_crc32 seek " |
| 102 |
"to end fails"); |
| 103 |
return DW_DLV_ERROR; |
| 104 |
} |
| 105 |
fsize = sz; |
| 106 |
} |
| 107 |
if (fsize <= (Dwarf_Unsigned)500) { |
| 108 |
/* Not a real object file. |
| 109 |
A random length check. */ |
| 110 |
return DW_DLV_NO_ENTRY; |
| 111 |
} |
| 112 |
size_left = fsize; |
| 113 |
res = _dwarf_seekr(fd,0,SEEK_SET,0); |
| 114 |
if (res != DW_DLV_OK) { |
| 115 |
_dwarf_error_string(dbg,error,DW_DLE_SEEK_ERROR, |
| 116 |
"DW_DLE_SEEK_ERROR: dwarf_crc32 seek " |
| 117 |
"to start fails"); |
| 118 |
return DW_DLV_ERROR; |
| 119 |
} |
| 120 |
readbuf = (unsigned char *)malloc(readlenu); |
| 121 |
if (!readbuf) { |
| 122 |
_dwarf_error_string(dbg,error,DW_DLE_ALLOC_FAIL, |
| 123 |
"DW_DLE_ALLOC_FAIL: dwarf_crc32 read buffer" |
| 124 |
" alloc fails"); |
| 125 |
return DW_DLV_ERROR; |
| 126 |
} |
| 127 |
while (size_left > 0) { |
| 128 |
if (size_left < readlenu) { |
| 129 |
readlenu = size_left; |
| 130 |
} |
| 131 |
res = _dwarf_readr(fd,(char *)readbuf,readlenu,0); |
| 132 |
if (res != DW_DLV_OK) { |
| 133 |
_dwarf_error_string(dbg,error,DW_DLE_READ_ERROR, |
| 134 |
"DW_DLE_READ_ERROR: dwarf_crc32 read fails "); |
| 135 |
free((unsigned char*)readbuf); |
| 136 |
return DW_DLV_ERROR; |
| 137 |
} |
| 138 |
/* Call the public API function so it gets tested too. */ |
| 139 |
tcrc = (unsigned int)dwarf_basic_crc32(readbuf, |
| 140 |
(unsigned long)readlenu, |
| 141 |
(unsigned long)init); |
| 142 |
init = tcrc; |
| 143 |
size_left -= readlenu; |
| 144 |
} |
| 145 |
/* endianness issues? */ |
| 146 |
free((unsigned char*)readbuf); |
| 147 |
memcpy(crcbuf,(void *)&tcrc,4); |
| 148 |
return DW_DLV_OK; |
| 149 |
} |