xref: /titanic_50/usr/src/lib/libbsm/common/adrf.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
31*7c478bd9Sstevel@tonic-gate #include <bsm/audit.h>
32*7c478bd9Sstevel@tonic-gate #include <bsm/libbsm.h>
33*7c478bd9Sstevel@tonic-gate #include <bsm/audit_record.h>
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate /*
37*7c478bd9Sstevel@tonic-gate  * adr_struct.now is used to calculate the record length so
38*7c478bd9Sstevel@tonic-gate  * end of record will be recognized.
39*7c478bd9Sstevel@tonic-gate  */
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate 
42*7c478bd9Sstevel@tonic-gate void
43*7c478bd9Sstevel@tonic-gate adrf_start(adrf_t *adrf, adr_t *adr, FILE *fp)
44*7c478bd9Sstevel@tonic-gate {
45*7c478bd9Sstevel@tonic-gate 	adrf->adrf_fp = fp;
46*7c478bd9Sstevel@tonic-gate 	adrf->adrf_adr = adr;
47*7c478bd9Sstevel@tonic-gate 	adrf->adrf_adr->adr_now = NULL;
48*7c478bd9Sstevel@tonic-gate }
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate /*
51*7c478bd9Sstevel@tonic-gate  * adrf_char - pull out characters
52*7c478bd9Sstevel@tonic-gate  */
53*7c478bd9Sstevel@tonic-gate int
54*7c478bd9Sstevel@tonic-gate adrf_char(adrf_t *adrf, char *cp, int count)
55*7c478bd9Sstevel@tonic-gate {
56*7c478bd9Sstevel@tonic-gate 	int c;	/* read character in here */
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate 	while (count--) {
59*7c478bd9Sstevel@tonic-gate 		if ((c = fgetc(adrf->adrf_fp)) == EOF)
60*7c478bd9Sstevel@tonic-gate 			return (-1);
61*7c478bd9Sstevel@tonic-gate 		*cp++ = c;
62*7c478bd9Sstevel@tonic-gate 		adrf->adrf_adr->adr_now += sizeof (char);
63*7c478bd9Sstevel@tonic-gate 	}
64*7c478bd9Sstevel@tonic-gate 	return (0);
65*7c478bd9Sstevel@tonic-gate }
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate /*
68*7c478bd9Sstevel@tonic-gate  * adrf_short - pull out shorts
69*7c478bd9Sstevel@tonic-gate  */
70*7c478bd9Sstevel@tonic-gate int
71*7c478bd9Sstevel@tonic-gate adrf_short(adrf_t *adrf, short *sp, int count)
72*7c478bd9Sstevel@tonic-gate {
73*7c478bd9Sstevel@tonic-gate 	int c;	/* read character in here */
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate 	while (count--) {
76*7c478bd9Sstevel@tonic-gate 		if ((c = fgetc(adrf->adrf_fp)) == EOF)
77*7c478bd9Sstevel@tonic-gate 			return (-1);
78*7c478bd9Sstevel@tonic-gate 		*sp = c << 8;
79*7c478bd9Sstevel@tonic-gate 		if ((c = fgetc(adrf->adrf_fp)) == EOF)
80*7c478bd9Sstevel@tonic-gate 			return (-1);
81*7c478bd9Sstevel@tonic-gate 		*sp++ |= c & 0x00ff;
82*7c478bd9Sstevel@tonic-gate 		adrf->adrf_adr->adr_now += sizeof (short);
83*7c478bd9Sstevel@tonic-gate 	}
84*7c478bd9Sstevel@tonic-gate 	return (0);
85*7c478bd9Sstevel@tonic-gate }
86*7c478bd9Sstevel@tonic-gate 
87*7c478bd9Sstevel@tonic-gate /*
88*7c478bd9Sstevel@tonic-gate  * adrf_int32 - pull out int32
89*7c478bd9Sstevel@tonic-gate  */
90*7c478bd9Sstevel@tonic-gate int adrf_int(adrf_t *adrf, int32_t *lp, int count);
91*7c478bd9Sstevel@tonic-gate int adrf_long(adrf_t *adrf, int32_t *lp, int count);
92*7c478bd9Sstevel@tonic-gate 
93*7c478bd9Sstevel@tonic-gate #pragma weak adrf_int = adrf_int32
94*7c478bd9Sstevel@tonic-gate #pragma weak adrf_long = adrf_int32
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate int
97*7c478bd9Sstevel@tonic-gate adrf_int32(adrf_t *adrf, int32_t *lp, int count)
98*7c478bd9Sstevel@tonic-gate {
99*7c478bd9Sstevel@tonic-gate 	int i;
100*7c478bd9Sstevel@tonic-gate 	int c;	/* read character in here */
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate 	for (; count--; lp++) {
103*7c478bd9Sstevel@tonic-gate 		*lp = 0;
104*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < 4; i++) {
105*7c478bd9Sstevel@tonic-gate 			if ((c = fgetc(adrf->adrf_fp)) == EOF)
106*7c478bd9Sstevel@tonic-gate 				return (-1);
107*7c478bd9Sstevel@tonic-gate 			*lp <<= 8;
108*7c478bd9Sstevel@tonic-gate 			*lp |= c & 0x000000ff;
109*7c478bd9Sstevel@tonic-gate 		}
110*7c478bd9Sstevel@tonic-gate 		adrf->adrf_adr->adr_now += sizeof (int32_t);
111*7c478bd9Sstevel@tonic-gate 	}
112*7c478bd9Sstevel@tonic-gate 	return (0);
113*7c478bd9Sstevel@tonic-gate }
114*7c478bd9Sstevel@tonic-gate 
115*7c478bd9Sstevel@tonic-gate int
116*7c478bd9Sstevel@tonic-gate adrf_int64(adrf_t *adrf, int64_t *lp, int count)
117*7c478bd9Sstevel@tonic-gate {
118*7c478bd9Sstevel@tonic-gate 	int i;
119*7c478bd9Sstevel@tonic-gate 	int c;	/* read character in here */
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 	for (; count--; lp++) {
122*7c478bd9Sstevel@tonic-gate 		*lp = 0;
123*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < 8; i++) {
124*7c478bd9Sstevel@tonic-gate 			if ((c = fgetc(adrf->adrf_fp)) == EOF)
125*7c478bd9Sstevel@tonic-gate 				return (-1);
126*7c478bd9Sstevel@tonic-gate 			*lp <<= 8;
127*7c478bd9Sstevel@tonic-gate 			*lp |= c & 0x00000000000000ff;
128*7c478bd9Sstevel@tonic-gate 		}
129*7c478bd9Sstevel@tonic-gate 		adrf->adrf_adr->adr_now += sizeof (int64_t);
130*7c478bd9Sstevel@tonic-gate 	}
131*7c478bd9Sstevel@tonic-gate 	return (0);
132*7c478bd9Sstevel@tonic-gate }
133*7c478bd9Sstevel@tonic-gate 
134*7c478bd9Sstevel@tonic-gate int adrf_u_int(adrf_t *adrf, uint32_t *cp, int count);
135*7c478bd9Sstevel@tonic-gate int adrf_u_long(adrf_t *adrf, uint32_t *cp, int count);
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate #pragma weak adrf_u_int = adrf_u_int32
138*7c478bd9Sstevel@tonic-gate #pragma weak adrf_u_long = adrf_u_int32
139*7c478bd9Sstevel@tonic-gate 
140*7c478bd9Sstevel@tonic-gate int
141*7c478bd9Sstevel@tonic-gate adrf_u_int32(adrf_t *adrf, uint32_t *cp, int count)
142*7c478bd9Sstevel@tonic-gate {
143*7c478bd9Sstevel@tonic-gate 	return (adrf_int32(adrf, (int32_t *)cp, count));
144*7c478bd9Sstevel@tonic-gate }
145*7c478bd9Sstevel@tonic-gate 
146*7c478bd9Sstevel@tonic-gate int
147*7c478bd9Sstevel@tonic-gate adrf_u_char(adrf_t *adrf, uchar_t *cp, int count)
148*7c478bd9Sstevel@tonic-gate {
149*7c478bd9Sstevel@tonic-gate 	return (adrf_char(adrf, (char *)cp, count));
150*7c478bd9Sstevel@tonic-gate }
151*7c478bd9Sstevel@tonic-gate 
152*7c478bd9Sstevel@tonic-gate int
153*7c478bd9Sstevel@tonic-gate adrf_u_int64(adrf_t *adrf, uint64_t *lp, int count)
154*7c478bd9Sstevel@tonic-gate {
155*7c478bd9Sstevel@tonic-gate 	return (adrf_int64(adrf, (int64_t *)lp, count));
156*7c478bd9Sstevel@tonic-gate }
157*7c478bd9Sstevel@tonic-gate 
158*7c478bd9Sstevel@tonic-gate int
159*7c478bd9Sstevel@tonic-gate adrf_u_short(adrf_t *adrf, ushort_t *sp, int count)
160*7c478bd9Sstevel@tonic-gate {
161*7c478bd9Sstevel@tonic-gate 	return (adrf_short(adrf, (short *)sp, count));
162*7c478bd9Sstevel@tonic-gate }
163*7c478bd9Sstevel@tonic-gate 
164*7c478bd9Sstevel@tonic-gate int
165*7c478bd9Sstevel@tonic-gate adrf_peek(adrf_t *adrf)
166*7c478bd9Sstevel@tonic-gate {
167*7c478bd9Sstevel@tonic-gate 	return (ungetc(fgetc(adrf->adrf_fp), adrf->adrf_fp));
168*7c478bd9Sstevel@tonic-gate }
169