xref: /illumos-gate/usr/src/lib/libdwarf/common/dwarf_object_read_common.c (revision 4d9fdb46b215739778ebc12079842c9905586999)
1 /*
2 Copyright (c) 2018, David Anderson
3 All rights reserved.
4 
5 Redistribution and use in source and binary forms, with
6 or without modification, are permitted provided that the
7 following conditions are met:
8 
9     Redistributions of source code must retain the above
10     copyright notice, this list of conditions and the following
11     disclaimer.
12 
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
16     provided 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
21 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER 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 #ifdef _WIN32
34 #define _CRT_SECURE_NO_WARNINGS
35 #endif /* _WIN32 */
36 
37 #include "config.h"
38 #include <stdio.h>
39 #include <string.h> /* memcpy */
40 #include <sys/types.h> /* lseek and read */
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h> /* lseek read close */
43 #elif defined(_WIN32) && defined(_MSC_VER)
44 #include <io.h>
45 #include <basetsd.h>
46 typedef SSIZE_T ssize_t; /* MSVC does not have POSIX ssize_t */
47 #endif /* HAVE_UNISTD_H */
48 
49 /* Windows specific header files */
50 #if defined(_WIN32) && defined(HAVE_STDAFX_H)
51 #include "stdafx.h"
52 #endif /* HAVE_STDAFX_H */
53 
54 #include "libdwarf.h" /* For error codes. */
55 #include "dwarf_object_read_common.h"
56 
57 /*  Neither off_t nor ssize_t is in C90.
58     However, both are in Posix:
59     IEEE Std 1003.1-1990, aka
60     ISO/IEC 9954-1:1990. */
61 int
_dwarf_object_read_random(int fd,char * buf,off_t loc,size_t size,off_t filesize,int * errc)62 _dwarf_object_read_random(int fd, char *buf, off_t loc,
63     size_t size, off_t filesize, int *errc)
64 {
65     off_t scode = 0;
66     ssize_t rcode = 0;
67     off_t endpoint = 0;
68 
69     if (loc >= filesize) {
70         /*  Seek can seek off the end. Lets not allow that.
71             The object is corrupt. */
72         *errc = DW_DLE_SEEK_OFF_END;
73         return DW_DLV_ERROR;
74     }
75     endpoint = loc+size;
76     if (endpoint > filesize) {
77         /*  Let us -not- try to read past end of object.
78             The object is corrupt. */
79         *errc = DW_DLE_READ_OFF_END;
80         return DW_DLV_ERROR;
81     }
82     scode = lseek(fd,loc,SEEK_SET);
83     if (scode == (off_t)-1) {
84         *errc = DW_DLE_SEEK_ERROR;
85         return DW_DLV_ERROR;
86     }
87     rcode = read(fd,buf,size);
88     if (rcode == -1 ||
89         (size_t)rcode != size) {
90         *errc = DW_DLE_READ_ERROR;
91         return DW_DLV_ERROR;
92     }
93     return DW_DLV_OK;
94 }
95 
96 void
_dwarf_safe_strcpy(char * out,long outlen,const char * in,long inlen)97 _dwarf_safe_strcpy(char *out, long outlen, const char *in, long inlen)
98 {
99     if (inlen >= (outlen - 1)) {
100         strncpy(out, in, outlen - 1);
101         out[outlen - 1] = 0;
102     } else {
103         strcpy(out, in);
104     }
105 }
106