]> git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSNumber.cpp
xnu-7195.101.1.tar.gz
[apple/xnu.git] / libkern / c++ / OSNumber.cpp
1 /*
2 * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
14 *
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
28 /* IOOffset.m created by rsulack on Wed 17-Sep-1997 */
29
30 #define IOKIT_ENABLE_SHARED_PTR
31
32 #include <sys/cdefs.h>
33
34 #include <libkern/c++/OSNumber.h>
35 #include <libkern/c++/OSString.h>
36 #include <libkern/c++/OSSerialize.h>
37 #include <libkern/c++/OSSharedPtr.h>
38 #include <libkern/c++/OSLib.h>
39
40 #define sizeMask (~0ULL >> (64 - size))
41
42 #define super OSObject
43
44 OSDefineMetaClassAndStructorsWithZone(OSNumber, OSObject,
45 (zone_create_flags_t) (ZC_CACHING | ZC_ZFREE_CLEARMEM))
46
47 OSMetaClassDefineReservedUnused(OSNumber, 0);
48 OSMetaClassDefineReservedUnused(OSNumber, 1);
49 OSMetaClassDefineReservedUnused(OSNumber, 2);
50 OSMetaClassDefineReservedUnused(OSNumber, 3);
51 OSMetaClassDefineReservedUnused(OSNumber, 4);
52 OSMetaClassDefineReservedUnused(OSNumber, 5);
53 OSMetaClassDefineReservedUnused(OSNumber, 6);
54 OSMetaClassDefineReservedUnused(OSNumber, 7);
55
56 bool
57 OSNumber::init(unsigned long long inValue, unsigned int newNumberOfBits)
58 {
59 if (!super::init()) {
60 return false;
61 }
62 if (newNumberOfBits > 64) {
63 return false;
64 }
65
66 size = newNumberOfBits;
67 value = (inValue & sizeMask);
68
69 return true;
70 }
71
72 bool
73 OSNumber::init(const char *newValue, unsigned int newNumberOfBits)
74 {
75 return init((unsigned long long)strtoul(newValue, NULL, 0), newNumberOfBits);
76 }
77
78 void
79 OSNumber::free()
80 {
81 super::free();
82 }
83
84 OSSharedPtr<OSNumber>
85 OSNumber::withNumber(unsigned long long value,
86 unsigned int newNumberOfBits)
87 {
88 OSSharedPtr<OSNumber> me = OSMakeShared<OSNumber>();
89
90 if (me && !me->init(value, newNumberOfBits)) {
91 return nullptr;
92 }
93
94 return me;
95 }
96
97 OSSharedPtr<OSNumber>
98 OSNumber::withNumber(const char *value, unsigned int newNumberOfBits)
99 {
100 OSSharedPtr<OSNumber> me = OSMakeShared<OSNumber>();
101
102 if (me && !me->init(value, newNumberOfBits)) {
103 return nullptr;
104 }
105
106 return me;
107 }
108
109 unsigned int
110 OSNumber::numberOfBits() const
111 {
112 return size;
113 }
114
115 unsigned int
116 OSNumber::numberOfBytes() const
117 {
118 return (size + 7) / 8;
119 }
120
121
122 unsigned char
123 OSNumber::unsigned8BitValue() const
124 {
125 return (unsigned char) value;
126 }
127
128 unsigned short
129 OSNumber::unsigned16BitValue() const
130 {
131 return (unsigned short) value;
132 }
133
134 unsigned int
135 OSNumber::unsigned32BitValue() const
136 {
137 return (unsigned int) value;
138 }
139
140 unsigned long long
141 OSNumber::unsigned64BitValue() const
142 {
143 return value;
144 }
145
146 void
147 OSNumber::addValue(signed long long inValue)
148 {
149 value = ((value + inValue) & sizeMask);
150 }
151
152 void
153 OSNumber::setValue(unsigned long long inValue)
154 {
155 value = (inValue & sizeMask);
156 }
157
158 bool
159 OSNumber::isEqualTo(const OSNumber *integer) const
160 {
161 return value == integer->value;
162 }
163
164 bool
165 OSNumber::isEqualTo(const OSMetaClassBase *obj) const
166 {
167 OSNumber * offset;
168 if ((offset = OSDynamicCast(OSNumber, obj))) {
169 return isEqualTo(offset);
170 } else {
171 return false;
172 }
173 }
174
175 bool
176 OSNumber::serialize(OSSerialize *s) const
177 {
178 char temp[32];
179
180 if (s->previouslySerialized(this)) {
181 return true;
182 }
183
184 snprintf(temp, sizeof(temp), "integer size=\"%d\"", size);
185 if (!s->addXMLStartTag(this, temp)) {
186 return false;
187 }
188
189 //XXX sprintf(temp, "0x%qx", value);
190 if ((value >> 32)) {
191 snprintf(temp, sizeof(temp), "0x%lx%08lx", (unsigned long)(value >> 32),
192 (unsigned long)(value & 0xFFFFFFFF));
193 } else {
194 snprintf(temp, sizeof(temp), "0x%lx", (unsigned long)value);
195 }
196 if (!s->addString(temp)) {
197 return false;
198 }
199
200 return s->addXMLEndTag("integer");
201 }