xref: /illumos-gate/usr/src/lib/libbsm/common/adrm.c (revision 1da57d55)
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 /*
27  * Adr memory based translations
28  */
29 
30 #include <stdio.h>
31 #include <sys/types.h>
32 #include <bsm/audit.h>
33 #include <bsm/audit_record.h>
34 
35 void
adrm_start(adr_t * adr,char * p)36 adrm_start(adr_t *adr, char *p)
37 {
38 	adr->adr_stream = p;
39 	adr->adr_now = p;
40 }
41 
42 /*
43  * adrm_char - pull out characters
44  */
45 void
adrm_char(adr_t * adr,char * cp,int count)46 adrm_char(adr_t *adr, char *cp, int count)
47 {
48 	while (count-- > 0)
49 		*cp++ = *adr->adr_now++;
50 }
51 
52 /*
53  * adrm_short - pull out shorts
54  */
55 void
adrm_short(adr_t * adr,short * sp,int count)56 adrm_short(adr_t *adr, short *sp, int count)
57 {
58 	while (count-- > 0) {
59 		*sp = *adr->adr_now++ << 8;
60 		*sp++ += ((short)*adr->adr_now++) & 0x00ff;
61 	}
62 }
63 
64 /*
65  * adrm_int32 - pull out int
66  */
67 void adrm_int(adr_t *adr, int32_t *lp, int count);
68 void adrm_long(adr_t *adr, int32_t *lp, int count);
69 #pragma weak adrm_int = adrm_int32
70 #pragma weak adrm_long = adrm_int32
71 
72 void
adrm_int32(adr_t * adr,int32_t * lp,int count)73 adrm_int32(adr_t *adr, int32_t *lp, int count)
74 {
75 	int i;
76 
77 	for (; count-- > 0; lp++) {
78 		*lp = 0;
79 		for (i = 0; i < 4; i++) {
80 			*lp <<= 8;
81 			*lp += ((int32_t)*adr->adr_now++) & 0x000000ff;
82 		}
83 	}
84 }
85 
86 void
adrm_uid(adr_t * adr,uid_t * up,int count)87 adrm_uid(adr_t *adr, uid_t *up, int count)
88 {
89 	int i;
90 
91 	for (; count-- > 0; up++) {
92 		*up = 0;
93 		for (i = 0; i < 4; i++) {
94 			*up <<= 8;
95 			*up += ((uid_t)*adr->adr_now++) & 0x000000ff;
96 		}
97 	}
98 }
99 
100 void
adrm_int64(adr_t * adr,int64_t * lp,int count)101 adrm_int64(adr_t *adr, int64_t *lp, int count)
102 {
103 	int i;
104 
105 	for (; count-- > 0; lp++) {
106 		*lp = 0;
107 		for (i = 0; i < 8; i++) {
108 			*lp <<= 8;
109 			*lp += ((int64_t)*adr->adr_now++) & 0x00000000000000ff;
110 		}
111 	}
112 }
113 
114 void adrm_u_int(adr_t *adr, uint32_t *cp, int count);
115 void adrm_u_long(adr_t *adr, uint32_t *cp, int count);
116 #pragma weak adrm_u_int = adrm_u_int32
117 #pragma weak adrm_u_long = adrm_u_int32
118 
119 void
adrm_u_int32(adr_t * adr,uint32_t * cp,int count)120 adrm_u_int32(adr_t *adr, uint32_t *cp, int count)
121 {
122 	adrm_int32(adr, (int32_t *)cp, count);
123 }
124 
125 void
adrm_u_char(adr_t * adr,uchar_t * cp,int count)126 adrm_u_char(adr_t *adr, uchar_t *cp, int count)
127 {
128 	adrm_char(adr, (char *)cp, count);
129 }
130 
131 void
adrm_u_int64(adr_t * adr,uint64_t * lp,int count)132 adrm_u_int64(adr_t *adr, uint64_t *lp, int count)
133 {
134 	adrm_int64(adr, (int64_t *)lp, count);
135 }
136 
137 void
adrm_u_short(adr_t * adr,ushort_t * sp,int count)138 adrm_u_short(adr_t *adr, ushort_t *sp, int count)
139 {
140 	adrm_short(adr, (short *)sp, count);
141 }
142 
143 /*
144  * adrm_putint32 - pack in int32
145  */
146 #pragma weak adrm_putint = adrm_putint32
147 #pragma weak adrm_putlong = adrm_putint32
148 void
adrm_putint32(adr_t * adr,int32_t * lp,int count)149 adrm_putint32(adr_t *adr, int32_t *lp, int count)
150 {
151 	int i;		/* index for counting */
152 	int32_t l;	/* value for shifting */
153 
154 	for (; count-- > 0; lp++) {
155 		for (i = 0, l = *lp; i < 4; i++) {
156 			*adr->adr_now++ = (char)((l & (int32_t)0xff000000) >>
157 			    (int)24);
158 			l <<= (int)8;
159 		}
160 	}
161 }
162