xref: /titanic_41/usr/src/lib/libbsm/common/adr.c (revision d29f5a711240f866521445b1656d114da090335e)
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 adr_start(adr_t *adr, char *p)
39 {
40 	adr->adr_stream = p;
41 	adr->adr_now = p;
42 }
43 
44 int
45 adr_count(adr_t *adr)
46 {
47 	return (((intptr_t)adr->adr_now) - ((intptr_t)adr->adr_stream));
48 }
49 
50 
51 /*
52  * adr_char - pull out characters
53  */
54 void
55 adr_char(adr_t *adr, char *cp, int count)
56 {
57 	while (count-- > 0)
58 		*adr->adr_now++ = *cp++;
59 }
60 
61 /*
62  * adr_short - pull out shorts
63  */
64 void
65 adr_short(adr_t *adr, short *sp, int count)
66 {
67 
68 	for (; count-- > 0; sp++) {
69 		*adr->adr_now++ = (char)((*sp >> 8) & 0x00ff);
70 		*adr->adr_now++ = (char)(*sp & 0x00ff);
71 	}
72 }
73 
74 /*
75  * adr_int32 - pull out uint32
76  */
77 #pragma weak adr_long = adr_int32
78 void
79 adr_long(adr_t *adr, int32_t *lp, int count);
80 void
81 adr_int32(adr_t *adr, int32_t *lp, int count)
82 {
83 	int i;		/* index for counting */
84 	uint32_t l;	/* value for shifting */
85 
86 	for (; count-- > 0; lp++) {
87 		for (i = 0, l = *(uint32_t *)lp; i < 4; i++) {
88 			*adr->adr_now++ =
89 			    (char)((uint32_t)(l & 0xff000000) >> 24);
90 			l <<= 8;
91 		}
92 	}
93 }
94 
95 /*
96  * adr_uid
97  */
98 
99 void
100 adr_uid(adr_t *adr, uid_t *up, int count)
101 {
102 	int i;		/* index for counting */
103 	uid_t l;	/* value for shifting */
104 
105 	for (; count-- > 0; up++) {
106 		for (i = 0, l = *(uint32_t *)up; i < 4; i++) {
107 			*adr->adr_now++ =
108 			    (char)((uint32_t)(l & 0xff000000) >> 24);
109 			l <<= 8;
110 		}
111 	}
112 }
113 
114 /*
115  * adr_int64 - pull out uint64_t
116  */
117 void
118 adr_int64(adr_t *adr, int64_t *lp, int count)
119 {
120 	int i;		/* index for counting */
121 	uint64_t l;	/* value for shifting */
122 
123 	for (; count-- > 0; lp++) {
124 		for (i = 0, l = *(uint64_t *)lp; i < 8; i++) {
125 			*adr->adr_now++ = (char)
126 			    ((uint64_t)(l & 0xff00000000000000ULL) >> 56);
127 			l <<= 8;
128 		}
129 	}
130 }
131