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