xref: /titanic_41/usr/src/lib/libbsm/common/adrf.c (revision 40cb5e5daa7b80bb70fcf8dadfb20f9281566331)
1 /*
2  * CDDL HEADER START
3  *
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.
8  *
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.
13  *
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]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <sys/types.h>
31 #include <bsm/audit.h>
32 #include <bsm/libbsm.h>
33 #include <bsm/audit_record.h>
34 
35 
36 /*
37  * adr_struct.now is used to calculate the record length so
38  * end of record will be recognized.
39  */
40 
41 
42 void
43 adrf_start(adrf_t *adrf, adr_t *adr, FILE *fp)
44 {
45 	adrf->adrf_fp = fp;
46 	adrf->adrf_adr = adr;
47 	adrf->adrf_adr->adr_now = NULL;
48 }
49 
50 /*
51  * adrf_char - pull out characters
52  */
53 int
54 adrf_char(adrf_t *adrf, char *cp, int count)
55 {
56 	int c;	/* read character in here */
57 
58 	while (count--) {
59 		if ((c = fgetc(adrf->adrf_fp)) == EOF)
60 			return (-1);
61 		*cp++ = c;
62 		adrf->adrf_adr->adr_now += sizeof (char);
63 	}
64 	return (0);
65 }
66 
67 /*
68  * adrf_short - pull out shorts
69  */
70 int
71 adrf_short(adrf_t *adrf, short *sp, int count)
72 {
73 	int c;	/* read character in here */
74 
75 	while (count--) {
76 		if ((c = fgetc(adrf->adrf_fp)) == EOF)
77 			return (-1);
78 		*sp = c << 8;
79 		if ((c = fgetc(adrf->adrf_fp)) == EOF)
80 			return (-1);
81 		*sp++ |= c & 0x00ff;
82 		adrf->adrf_adr->adr_now += sizeof (short);
83 	}
84 	return (0);
85 }
86 
87 /*
88  * adrf_int32 - pull out int32
89  */
90 int adrf_int(adrf_t *adrf, int32_t *lp, int count);
91 int adrf_long(adrf_t *adrf, int32_t *lp, int count);
92 
93 #pragma weak adrf_int = adrf_int32
94 #pragma weak adrf_long = adrf_int32
95 
96 int
97 adrf_int32(adrf_t *adrf, int32_t *lp, int count)
98 {
99 	int i;
100 	int c;	/* read character in here */
101 
102 	for (; count--; lp++) {
103 		*lp = 0;
104 		for (i = 0; i < 4; i++) {
105 			if ((c = fgetc(adrf->adrf_fp)) == EOF)
106 				return (-1);
107 			*lp <<= 8;
108 			*lp |= c & 0x000000ff;
109 		}
110 		adrf->adrf_adr->adr_now += sizeof (int32_t);
111 	}
112 	return (0);
113 }
114 
115 int
116 adrf_int64(adrf_t *adrf, int64_t *lp, int count)
117 {
118 	int i;
119 	int c;	/* read character in here */
120 
121 	for (; count--; lp++) {
122 		*lp = 0;
123 		for (i = 0; i < 8; i++) {
124 			if ((c = fgetc(adrf->adrf_fp)) == EOF)
125 				return (-1);
126 			*lp <<= 8;
127 			*lp |= c & 0x00000000000000ff;
128 		}
129 		adrf->adrf_adr->adr_now += sizeof (int64_t);
130 	}
131 	return (0);
132 }
133 
134 int adrf_u_int(adrf_t *adrf, uint32_t *cp, int count);
135 int adrf_u_long(adrf_t *adrf, uint32_t *cp, int count);
136 
137 #pragma weak adrf_u_int = adrf_u_int32
138 #pragma weak adrf_u_long = adrf_u_int32
139 
140 int
141 adrf_u_int32(adrf_t *adrf, uint32_t *cp, int count)
142 {
143 	return (adrf_int32(adrf, (int32_t *)cp, count));
144 }
145 
146 int
147 adrf_u_char(adrf_t *adrf, uchar_t *cp, int count)
148 {
149 	return (adrf_char(adrf, (char *)cp, count));
150 }
151 
152 int
153 adrf_u_int64(adrf_t *adrf, uint64_t *lp, int count)
154 {
155 	return (adrf_int64(adrf, (int64_t *)lp, count));
156 }
157 
158 int
159 adrf_u_short(adrf_t *adrf, ushort_t *sp, int count)
160 {
161 	return (adrf_short(adrf, (short *)sp, count));
162 }
163 
164 int
165 adrf_peek(adrf_t *adrf)
166 {
167 	return (ungetc(fgetc(adrf->adrf_fp), adrf->adrf_fp));
168 }
169