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