xref: /onnv-gate/usr/src/lib/libtnf/access.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  *	Copyright (c) 1994, by Sun Microsytems, Inc.
24*0Sstevel@tonic-gate  */
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate #pragma	ident	"%Z%%M%	%I%	%E% SMI"
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #include "libtnf.h"
29*0Sstevel@tonic-gate 
30*0Sstevel@tonic-gate /*
31*0Sstevel@tonic-gate  * File header operations
32*0Sstevel@tonic-gate  */
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate tnf_datum_t
tnf_get_file_header(TNF * tnf)35*0Sstevel@tonic-gate tnf_get_file_header(TNF *tnf)
36*0Sstevel@tonic-gate {
37*0Sstevel@tonic-gate 	return (DATUM(tnf->file_header_info, (caddr_t)tnf->file_header));
38*0Sstevel@tonic-gate }
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate /*
41*0Sstevel@tonic-gate  * Block access operations
42*0Sstevel@tonic-gate  */
43*0Sstevel@tonic-gate 
44*0Sstevel@tonic-gate unsigned
tnf_get_block_count(TNF * tnf)45*0Sstevel@tonic-gate tnf_get_block_count(TNF *tnf)
46*0Sstevel@tonic-gate {
47*0Sstevel@tonic-gate 	return (tnf->block_count);
48*0Sstevel@tonic-gate }
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate tnf_datum_t
tnf_get_block_absolute(TNF * tnf,unsigned index)51*0Sstevel@tonic-gate tnf_get_block_absolute(TNF *tnf, unsigned index)
52*0Sstevel@tonic-gate {
53*0Sstevel@tonic-gate 	if (index >= tnf->block_count)
54*0Sstevel@tonic-gate 		/*
55*0Sstevel@tonic-gate 		 * access to non-existent block:
56*0Sstevel@tonic-gate 		 * no error as per spec
57*0Sstevel@tonic-gate 		 */
58*0Sstevel@tonic-gate 		return (TNF_DATUM_NULL);
59*0Sstevel@tonic-gate 	else
60*0Sstevel@tonic-gate 		/*
61*0Sstevel@tonic-gate 		 * XXX Require a single block header tag
62*0Sstevel@tonic-gate 		 */
63*0Sstevel@tonic-gate 		/* LINTED pointer cast may result in improper alignment */
64*0Sstevel@tonic-gate 		return (DATUM(tnf->block_header_info,
65*0Sstevel@tonic-gate 			    (caddr_t)_GET_INDEX_BLOCK(tnf, index)));
66*0Sstevel@tonic-gate }
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate tnf_datum_t
tnf_get_block_relative(tnf_datum_t datum,int adjust)69*0Sstevel@tonic-gate tnf_get_block_relative(tnf_datum_t datum, int adjust)
70*0Sstevel@tonic-gate {
71*0Sstevel@tonic-gate 	TNF		*tnf;
72*0Sstevel@tonic-gate 	tnf_ref32_t	*bhdr;
73*0Sstevel@tonic-gate 	unsigned	index;
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate 	CHECK_DATUM(datum);
76*0Sstevel@tonic-gate 
77*0Sstevel@tonic-gate 	tnf 	= DATUM_TNF(datum);
78*0Sstevel@tonic-gate 	bhdr 	= _GET_BLOCK(tnf, DATUM_VAL(datum));
79*0Sstevel@tonic-gate 	index	= _GET_BLOCK_INDEX(tnf, bhdr);
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate 	return (tnf_get_block_absolute(tnf, index + adjust));
82*0Sstevel@tonic-gate }
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate int
tnf_is_block_header(tnf_datum_t datum)85*0Sstevel@tonic-gate tnf_is_block_header(tnf_datum_t datum)
86*0Sstevel@tonic-gate {
87*0Sstevel@tonic-gate 	struct taginfo	*info;
88*0Sstevel@tonic-gate 	caddr_t		val;
89*0Sstevel@tonic-gate 	tnf_ref32_t	*bhdr;
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 	CHECK_DATUM(datum);
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	info	= DATUM_INFO(datum);
94*0Sstevel@tonic-gate 	val	= DATUM_VAL(datum);
95*0Sstevel@tonic-gate 	bhdr	= _GET_BLOCK(info->tnf, val);
96*0Sstevel@tonic-gate 
97*0Sstevel@tonic-gate 	return (((caddr_t)bhdr == val) &&
98*0Sstevel@tonic-gate 		(info == info->tnf->block_header_info));
99*0Sstevel@tonic-gate }
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate tnf_datum_t
tnf_get_block_header(tnf_datum_t datum)102*0Sstevel@tonic-gate tnf_get_block_header(tnf_datum_t datum)
103*0Sstevel@tonic-gate {
104*0Sstevel@tonic-gate 	TNF	*tnf;
105*0Sstevel@tonic-gate 	caddr_t	val;
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	CHECK_DATUM(datum);
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 	tnf 	= DATUM_TNF(datum);
110*0Sstevel@tonic-gate 	val	= DATUM_VAL(datum);
111*0Sstevel@tonic-gate 	/*
112*0Sstevel@tonic-gate 	 * XXX Require a single block header tag
113*0Sstevel@tonic-gate 	 */
114*0Sstevel@tonic-gate 	return (DATUM(tnf->block_header_info, (caddr_t)_GET_BLOCK(tnf, val)));
115*0Sstevel@tonic-gate }
116*0Sstevel@tonic-gate 
117*0Sstevel@tonic-gate /*
118*0Sstevel@tonic-gate  * Sequential record access
119*0Sstevel@tonic-gate  */
120*0Sstevel@tonic-gate 
121*0Sstevel@tonic-gate tnf_datum_t
tnf_get_next_record(tnf_datum_t datum)122*0Sstevel@tonic-gate tnf_get_next_record(tnf_datum_t datum)
123*0Sstevel@tonic-gate {
124*0Sstevel@tonic-gate 	TNF		*tnf;
125*0Sstevel@tonic-gate 	tnf_ref32_t	*bhdr, *cell, ref32;
126*0Sstevel@tonic-gate 	caddr_t		val, nval, bval, blim;
127*0Sstevel@tonic-gate 	size_t		size, bytes;
128*0Sstevel@tonic-gate 
129*0Sstevel@tonic-gate 	CHECK_RECORD(datum);
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate 	tnf	= DATUM_TNF(datum);
132*0Sstevel@tonic-gate 	val	= DATUM_VAL(datum);
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate 	size	= tnf_get_size(datum);
135*0Sstevel@tonic-gate 	nval	= val + size;
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 	/* Check file bounds */
138*0Sstevel@tonic-gate 	if (nval < tnf->data_start)
139*0Sstevel@tonic-gate 		return (tnf_get_block_absolute(tnf, 0));
140*0Sstevel@tonic-gate 	else if (nval >= tnf->file_end)
141*0Sstevel@tonic-gate 		return (TNF_DATUM_NULL);
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	/*
144*0Sstevel@tonic-gate 	 * OK, nval is in data area, start looking in block
145*0Sstevel@tonic-gate 	 */
146*0Sstevel@tonic-gate 	bhdr 	= _GET_BLOCK(tnf, nval);
147*0Sstevel@tonic-gate 	/* LINTED pointer cast may result in improper alignment */
148*0Sstevel@tonic-gate 	bytes	= _GET_BLOCK_BYTES_VALID(tnf, bhdr);
149*0Sstevel@tonic-gate 	bval 	= (caddr_t)bhdr;
150*0Sstevel@tonic-gate 	blim  	= bval + bytes;
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	/* sequentially examine valid cells in block from nval onwards */
153*0Sstevel@tonic-gate 	while (nval < blim) {
154*0Sstevel@tonic-gate 		/* LINTED pointer cast may result in improper alignment */
155*0Sstevel@tonic-gate 		cell 	= (tnf_ref32_t *)nval;
156*0Sstevel@tonic-gate 		ref32 	= _GET_INT32(tnf, cell);
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate 		switch (TNF_REF32_TYPE(ref32)) {
159*0Sstevel@tonic-gate 		case TNF_REF32_T_FWD: /* skip forwarding cells */
160*0Sstevel@tonic-gate 			nval += sizeof (tnf_ref32_t);
161*0Sstevel@tonic-gate 			break;
162*0Sstevel@tonic-gate 		case TNF_REF32_T_RSVD: /* catch bogus cells */
163*0Sstevel@tonic-gate 			_tnf_error(tnf, TNF_ERR_BADTNF);
164*0Sstevel@tonic-gate 			return (TNF_DATUM_NULL);
165*0Sstevel@tonic-gate 		default:	/* PAIR or TAG: record header */
166*0Sstevel@tonic-gate 			return (RECORD_DATUM(tnf, cell));
167*0Sstevel@tonic-gate 		}
168*0Sstevel@tonic-gate 	}
169*0Sstevel@tonic-gate 
170*0Sstevel@tonic-gate 	/*
171*0Sstevel@tonic-gate 	 * Couldn't find it: return next non-zero block header
172*0Sstevel@tonic-gate 	 */
173*0Sstevel@tonic-gate 	while ((bval += tnf->block_size) < tnf->file_end)
174*0Sstevel@tonic-gate 		/* Gotta check that there is a real bhdr here */
175*0Sstevel@tonic-gate 		/* LINTED pointer cast may result in improper alignment */
176*0Sstevel@tonic-gate 		if (*(tnf_ref32_t *)bval != TNF_NULL)
177*0Sstevel@tonic-gate 		/* LINTED pointer cast may result in improper alignment */
178*0Sstevel@tonic-gate 			return (RECORD_DATUM(tnf, (tnf_ref32_t *)bval));
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate 	/*
181*0Sstevel@tonic-gate 	 * default: we're off the end of the file
182*0Sstevel@tonic-gate 	 */
183*0Sstevel@tonic-gate 	return (TNF_DATUM_NULL);
184*0Sstevel@tonic-gate }
185