]> git.saurik.com Git - apple/securityd.git/blob - src/flippers.cpp
securityd-32597.tar.gz
[apple/securityd.git] / src / flippers.cpp
1 /*
2 * Copyright (c) 2003-2004 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25 //
26 // process - track a single client process and its belongings
27 //
28 #include "flippers.h"
29 #include <security_utilities/memutils.h>
30
31 using namespace LowLevelMemoryUtilities;
32
33
34 namespace Flippers {
35
36 //
37 // The raw byte reversal flipper
38 //
39 void flip(void *addr, size_t size)
40 {
41 assert(size > 1 && (size % 2 == 0));
42 Byte *word = reinterpret_cast<uint8 *>(addr);
43 for (size_t n = 0; n < size/2; n++) {
44 Byte b = word[n];
45 word[n] = word[size-1-n];
46 word[size-1-n] = b;
47 }
48 }
49
50
51 //
52 // Basic flippers
53 //
54 void flip(uint32 &obj) { flip(&obj, sizeof(obj)); }
55 void flip(uint16 &obj) { flip(&obj, sizeof(obj)); }
56 void flip(sint32 &obj) { flip(&obj, sizeof(obj)); }
57 void flip(sint16 &obj) { flip(&obj, sizeof(obj)); }
58
59
60 //
61 // Flip a context attribute. This is heavily polymorphic.
62 //
63 void flip(CSSM_CONTEXT_ATTRIBUTE &obj)
64 {
65 flip(obj.AttributeType);
66 flip(obj.AttributeLength);
67 switch (obj.AttributeType & CSSM_ATTRIBUTE_TYPE_MASK) {
68 case CSSM_ATTRIBUTE_DATA_UINT32:
69 flip(obj.Attribute.Uint32);
70 break;
71 // all other alternatives are handled by CSSM_CONTEXT_ATTRIBUTE's walker
72 default:
73 break;
74 }
75 }
76
77
78 //
79 // Flip a CSSM_DB_ATTRIBUTE_INFO, also very polymorphic
80 //
81 void flip(CSSM_DB_ATTRIBUTE_INFO &obj)
82 {
83 bool flippedAttributeNameFormat = false;
84 // check and see if obj is in host byte order. If not, flip it now
85 if (obj.AttributeNameFormat > CSSM_DB_ATTRIBUTE_NAME_AS_INTEGER)
86 {
87 flip(obj.AttributeNameFormat);
88 flippedAttributeNameFormat = true;
89 }
90
91 switch (obj.AttributeNameFormat)
92 {
93 case CSSM_DB_ATTRIBUTE_NAME_AS_INTEGER:
94 {
95 flip(obj.Label.AttributeID);
96 }
97 break;
98 }
99
100 flip (obj.AttributeFormat);
101
102 if (!flippedAttributeNameFormat)
103 {
104 flip(obj.AttributeNameFormat);
105 }
106
107 }
108
109 //
110 // Automatically generated flippers
111 //
112 #include "flip_gen.cpp"
113
114
115 } // end namespace Flippers