xref: /netbsd-src/usr.bin/vndcompress/offtab.h (revision 9719b6dc165fc55f7e9481c97d1a3ecd08bd4d3d)
1*9719b6dcSriastradh /*	$NetBSD: offtab.h,v 1.3 2017/04/16 23:50:40 riastradh Exp $	*/
2735c2397Sriastradh 
3735c2397Sriastradh /*-
4735c2397Sriastradh  * Copyright (c) 2014 The NetBSD Foundation, Inc.
5735c2397Sriastradh  * All rights reserved.
6735c2397Sriastradh  *
7735c2397Sriastradh  * This code is derived from software contributed to The NetBSD Foundation
8735c2397Sriastradh  * by Taylor R. Campbell.
9735c2397Sriastradh  *
10735c2397Sriastradh  * Redistribution and use in source and binary forms, with or without
11735c2397Sriastradh  * modification, are permitted provided that the following conditions
12735c2397Sriastradh  * are met:
13735c2397Sriastradh  * 1. Redistributions of source code must retain the above copyright
14735c2397Sriastradh  *    notice, this list of conditions and the following disclaimer.
15735c2397Sriastradh  * 2. Redistributions in binary form must reproduce the above copyright
16735c2397Sriastradh  *    notice, this list of conditions and the following disclaimer in the
17735c2397Sriastradh  *    documentation and/or other materials provided with the distribution.
18735c2397Sriastradh  *
19735c2397Sriastradh  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20735c2397Sriastradh  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21735c2397Sriastradh  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22735c2397Sriastradh  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23735c2397Sriastradh  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24735c2397Sriastradh  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25735c2397Sriastradh  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26735c2397Sriastradh  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27735c2397Sriastradh  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28735c2397Sriastradh  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29735c2397Sriastradh  * POSSIBILITY OF SUCH DAMAGE.
30735c2397Sriastradh  */
31735c2397Sriastradh 
32735c2397Sriastradh #ifndef	VNDCOMPRESS_OFFTAB_H
33735c2397Sriastradh #define	VNDCOMPRESS_OFFTAB_H
34735c2397Sriastradh 
35735c2397Sriastradh #include <sys/cdefs.h>
36735c2397Sriastradh 
37735c2397Sriastradh #include <stdbool.h>
38735c2397Sriastradh #include <stdint.h>
39735c2397Sriastradh 
40*9719b6dcSriastradh #include "common.h"
41*9719b6dcSriastradh 
42735c2397Sriastradh struct offtab {
43735c2397Sriastradh 	uint32_t	ot_n_offsets;
443e40e9d7Sriastradh 	uint32_t	ot_window_size;
453e40e9d7Sriastradh 	uint32_t	ot_window_start;
463e40e9d7Sriastradh 	uint64_t	*ot_window;
47735c2397Sriastradh 	uint32_t	ot_blkno;
48735c2397Sriastradh 	int		ot_fd;
49735c2397Sriastradh 	off_t		ot_fdpos;
50735c2397Sriastradh 	void		(*ot_report)(const char *, ...) __printflike(1,2);
51735c2397Sriastradh 	void		(*ot_reportx)(const char *, ...) __printflike(1,2);
52735c2397Sriastradh 	enum offtab_mode {
53735c2397Sriastradh 		OFFTAB_MODE_NONE,
54735c2397Sriastradh 		OFFTAB_MODE_READ,
55735c2397Sriastradh 		OFFTAB_MODE_WRITE,
56735c2397Sriastradh 	}		ot_mode;
57735c2397Sriastradh };
58735c2397Sriastradh 
59*9719b6dcSriastradh #define	OFFTAB_MAX_FDPOS						      \
60*9719b6dcSriastradh 	((off_t)(MIN(OFF_MAX, UINT64_MAX) -				      \
61*9719b6dcSriastradh 	    (off_t)MAX_N_OFFSETS*sizeof(uint64_t)))
62*9719b6dcSriastradh 
633e40e9d7Sriastradh void		offtab_init(struct offtab *, uint32_t, uint32_t, int, off_t);
64735c2397Sriastradh void		offtab_destroy(struct offtab *);
65735c2397Sriastradh 
663e40e9d7Sriastradh bool		offtab_transmogrify_read_to_write(struct offtab *, uint32_t);
67735c2397Sriastradh 
68735c2397Sriastradh bool		offtab_reset_read(struct offtab *,
69735c2397Sriastradh 		    void (*)(const char *, ...) __printflike(1,2),
70735c2397Sriastradh 		    void (*)(const char *, ...) __printflike(1,2));
71735c2397Sriastradh bool		offtab_prepare_get(struct offtab *, uint32_t);
72735c2397Sriastradh uint64_t	offtab_get(struct offtab *, uint32_t);
73735c2397Sriastradh 
74735c2397Sriastradh void		offtab_reset_write(struct offtab *);
75735c2397Sriastradh void		offtab_checkpoint(struct offtab *, uint32_t, int);
76735c2397Sriastradh #define	OFFTAB_CHECKPOINT_SYNC	1
77735c2397Sriastradh void		offtab_prepare_put(struct offtab *, uint32_t);
78735c2397Sriastradh void		offtab_put(struct offtab *, uint32_t, uint64_t);
79735c2397Sriastradh 
80735c2397Sriastradh #endif	/* VNDCOMPRESS_OFFTAB_H */
81