xref: /netbsd-src/external/bsd/file/dist/src/buffer.c (revision e15daa8be9575f7ad2ca804c7c7c2d7f8e182d98)
1 /*	$NetBSD: buffer.c,v 1.1.1.5 2023/08/18 18:36:49 christos Exp $	*/
2 
3 /*
4  * Copyright (c) Christos Zoulas 2017.
5  * All Rights Reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice immediately at the beginning of the file, without modification,
12  *    this list of conditions, and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 #include "file.h"
30 
31 #ifndef	lint
32 #if 0
33 FILE_RCSID("@(#)$File: buffer.c,v 1.13 2023/07/02 12:48:39 christos Exp $")
34 #else
35 __RCSID("$NetBSD: buffer.c,v 1.1.1.5 2023/08/18 18:36:49 christos Exp $");
36 #endif
37 #endif	/* lint */
38 
39 #include "magic.h"
40 #include <unistd.h>
41 #include <string.h>
42 #include <stdlib.h>
43 #include <sys/stat.h>
44 
45 void
buffer_init(struct buffer * b,int fd,const struct stat * st,const void * data,size_t len)46 buffer_init(struct buffer *b, int fd, const struct stat *st, const void *data,
47     size_t len)
48 {
49 	b->fd = fd;
50 	if (st)
51 		memcpy(&b->st, st, sizeof(b->st));
52 	else if (b->fd == -1 || fstat(b->fd, &b->st) == -1)
53 		memset(&b->st, 0, sizeof(b->st));
54 	b->fbuf = data;
55 	b->flen = len;
56 	b->eoff = 0;
57 	b->ebuf = NULL;
58 	b->elen = 0;
59 }
60 
61 void
buffer_fini(struct buffer * b)62 buffer_fini(struct buffer *b)
63 {
64 	free(b->ebuf);
65 	b->ebuf = NULL;
66 	b->elen = 0;
67 }
68 
69 int
buffer_fill(const struct buffer * bb)70 buffer_fill(const struct buffer *bb)
71 {
72 	struct buffer *b = CCAST(struct buffer *, bb);
73 
74 	if (b->elen != 0)
75 		return b->elen == FILE_BADSIZE ? -1 : 0;
76 
77 	if (!S_ISREG(b->st.st_mode))
78 		goto out;
79 
80 	b->elen = CAST(size_t, b->st.st_size) < b->flen ?
81 	    CAST(size_t, b->st.st_size) : b->flen;
82 	if (b->elen == 0) {
83 		free(b->ebuf);
84 		b->ebuf = NULL;
85 		return 0;
86 	}
87 	if ((b->ebuf = malloc(b->elen)) == NULL)
88 		goto out;
89 
90 	b->eoff = b->st.st_size - b->elen;
91 	if (pread(b->fd, b->ebuf, b->elen, b->eoff) == -1) {
92 		free(b->ebuf);
93 		b->ebuf = NULL;
94 		goto out;
95 	}
96 
97 	return 0;
98 out:
99 	b->elen = FILE_BADSIZE;
100 	return -1;
101 }
102