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