xref: /titanic_41/usr/src/lib/libbsm/common/adr.c (revision 2654012f83cec5dc15b61dfe3e4a4915f186e7a6)
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 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 
27 /*
28  * Adr memory based encoding
29  */
30 
31 #include <sys/types.h>
32 #include <bsm/audit.h>
33 #include <bsm/libbsm.h>
34 #include <bsm/audit_record.h>
35 
36 void
37 adr_start(adr_t *adr, char *p)
38 {
39 	adr->adr_stream = p;
40 	adr->adr_now = p;
41 }
42 
43 int
44 adr_count(adr_t *adr)
45 {
46 	return (((intptr_t)adr->adr_now) - ((intptr_t)adr->adr_stream));
47 }
48 
49 
50 /*
51  * adr_char - pull out characters
52  */
53 void
54 adr_char(adr_t *adr, char *cp, int count)
55 {
56 	while (count-- > 0)
57 		*adr->adr_now++ = *cp++;
58 }
59 
60 /*
61  * adr_short - pull out shorts
62  */
63 void
64 adr_short(adr_t *adr, short *sp, int count)
65 {
66 
67 	for (; count-- > 0; sp++) {
68 		*adr->adr_now++ = (char)((*sp >> 8) & 0x00ff);
69 		*adr->adr_now++ = (char)(*sp & 0x00ff);
70 	}
71 }
72 
73 /*
74  * adr_ushort - pull out ushorts
75  */
76 void
77 adr_ushort(adr_t *adr, ushort_t *sp, int count)
78 {
79 
80 	for (; count-- > 0; sp++) {
81 		*adr->adr_now++ = (char)((*sp >> 8) & 0x00ff);
82 		*adr->adr_now++ = (char)(*sp & 0x00ff);
83 	}
84 }
85 
86 /*
87  * adr_int32 - pull out uint32
88  */
89 #pragma weak adr_long = adr_int32
90 void
91 adr_long(adr_t *adr, int32_t *lp, int count);
92 void
93 adr_int32(adr_t *adr, int32_t *lp, int count)
94 {
95 	int i;		/* index for counting */
96 	uint32_t l;	/* value for shifting */
97 
98 	for (; count-- > 0; lp++) {
99 		for (i = 0, l = *(uint32_t *)lp; i < 4; i++) {
100 			*adr->adr_now++ =
101 			    (char)((uint32_t)(l & 0xff000000) >> 24);
102 			l <<= 8;
103 		}
104 	}
105 }
106 
107 /*
108  * adr_uid
109  */
110 
111 void
112 adr_uid(adr_t *adr, uid_t *up, int count)
113 {
114 	int i;		/* index for counting */
115 	uid_t l;	/* value for shifting */
116 
117 	for (; count-- > 0; up++) {
118 		for (i = 0, l = *(uint32_t *)up; i < 4; i++) {
119 			*adr->adr_now++ =
120 			    (char)((uint32_t)(l & 0xff000000) >> 24);
121 			l <<= 8;
122 		}
123 	}
124 }
125 
126 /*
127  * adr_int64 - pull out uint64_t
128  */
129 void
130 adr_int64(adr_t *adr, int64_t *lp, int count)
131 {
132 	int i;		/* index for counting */
133 	uint64_t l;	/* value for shifting */
134 
135 	for (; count-- > 0; lp++) {
136 		for (i = 0, l = *(uint64_t *)lp; i < 8; i++) {
137 			*adr->adr_now++ = (char)
138 			    ((uint64_t)(l & 0xff00000000000000ULL) >> 56);
139 			l <<= 8;
140 		}
141 	}
142 }
143