summaryrefslogtreecommitdiff
path: root/third_party/heimdal/lib/roken/dumpdata.c
blob: a8b3efefcff47a6c28bad3056dee635fd987034e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/*
 * Copyright (c) 2005 Kungliga Tekniska Högskolan
 * (Royal Institute of Technology, Stockholm, Sweden).
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. 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.
 *
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE 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 INSTITUTE 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 <config.h>

#include "roken.h"

/*
 * Write datablob to a filename, don't care about errors.
 */

ROKEN_LIB_FUNCTION void ROKEN_LIB_CALL
rk_dumpdata (const char *filename, const void *buf, size_t size)
{
    int fd;

    fd = open(filename, O_WRONLY|O_TRUNC|O_CREAT, 0640);
    if (fd < 0)
	return;
    net_write(fd, buf, size);
    close(fd);
}

/* For not-regular files */
static int
undump_not_file(int fd, char **out, size_t *size, int nul_terminate)
{
    size_t lim = 10 * 1024 * 1024;
    size_t bufsz = 0;
    size_t sz = 0;
    char *buf = NULL;
    char *tmp;

    *out = NULL;
    if (size && *size != 0 && *size < lim)
        lim = *size;
    if (size)
        *size = 0;

    /*
     * We can't use net_read() if we're on WIN32 because that really wants a
     * socket FD, which is in a distinct FD namespace from those returned by
     * open() on Windows.
     */
    do {
        ssize_t bytes;

        if (sz == bufsz) {
            if (bufsz == 0)
                bufsz = 1024;
            else
                bufsz += bufsz >> 1;

            tmp = realloc(buf, bufsz);
            if (tmp == NULL) {
                free(buf);
                return ENOMEM;
            }
            buf = tmp;
        }

        bytes = read(fd, buf + sz, bufsz - sz);
        if (bytes == 0)
            break;
        if (bytes < 0 &&
            (errno == EAGAIN || errno == EWOULDBLOCK))
            continue;
        if (bytes < 0) {
            free(buf);
            return errno;
        }
        sz += bytes;
    } while (sz < lim);

    *out = buf;
    if (size)
        *size = sz;

    if (!nul_terminate)
        return 0;

    if (bufsz > sz) {
        buf[sz] = '\0';
        return 0;
    }

    *out = tmp = realloc(buf, bufsz + 1);
    if (tmp == NULL) {
        free(buf);
        return ENOMEM;
    }
    buf = tmp;
    buf[sz] = '\0';
    return 0;
}

/*
 * Read all data from a file, care about errors.
 *
 * If `*size' is not zero and the file is not a regular file, then up to that
 * many bytes will be read.
 *
 * Returns zero on success or a system error code on failure.
 */

ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
rk_undumpdata(const char *filename, void **buf, size_t *size)
{
    struct stat sb;
    int fd, ret;
    ssize_t sret;

    *buf = NULL;

    fd = open(filename, O_RDONLY, 0);
    if (fd < 0)
	return errno;
    if (fstat(fd, &sb) != 0){
	ret = errno;
	goto out;
    }
    if (!S_ISREG(sb.st_mode)) {
        char *char_buf;

        ret = undump_not_file(fd, &char_buf, size, 0);
        (void) close(fd);
        *buf = char_buf;
        return ret;
    }

    if (sb.st_size < 0)
        sb.st_size = 0;
    *buf = malloc(sb.st_size);
    if (*buf == NULL) {
	ret = ENOMEM;
	goto out;
    }
    *size = sb.st_size;

    sret = read(fd, *buf, *size);
    if (sret < 0)
	ret = errno;
    else if (sret != (ssize_t)*size)
	ret = EINVAL;
    else
	ret = 0;

  out:
    if (ret) {
	free(*buf);
	*buf = NULL;
    }
    close(fd);
    return ret;
}

/*
 * Read all text from a file.
 *
 * Outputs a C string.  It is up to the caller to check for embedded NULs.
 * The number of bytes read will be stored in `*size' if `size' is not NULL.
 *
 * If `size' is not NULL and `*size' is not zero and the file is not a regular
 * file, then up to that many bytes will be read.
 *
 * Returns zero on success or a system error code on failure.
 */

ROKEN_LIB_FUNCTION int ROKEN_LIB_CALL
rk_undumptext(const char *filename, char **out, size_t *size)
{
    struct stat sb;
    int fd, ret;
    ssize_t sret;
    char *buf;

    *out = NULL;

    fd = open(filename, O_RDONLY, 0);
    if (fd < 0)
        return errno;
    if (fstat(fd, &sb) != 0) {
        (void) close(fd);
	return errno;
    }
    if (!S_ISREG(sb.st_mode)) {
        ret = undump_not_file(fd, out, size, 1);
        (void) close(fd);
        return ret;
    }

    if (sb.st_size < 0)
        sb.st_size = 0;
    buf = malloc(sb.st_size + 1);
    if (buf == NULL) {
	ret = ENOMEM;
	goto out;
    }
    if (size)
        *size = sb.st_size;

    sret = read(fd, buf, sb.st_size);
    if (sret < 0)
	ret = errno;
    else if (sret != (ssize_t)sb.st_size)
	ret = EINVAL;
    else
	ret = 0;

out:
    if (ret) {
	free(buf);
    } else {
        buf[sb.st_size] = '\0';
        *out = buf;
    }
    close(fd);
    return ret;
}