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