]> git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSNumber.cpp
ffbc9e79359d00bfee6eb82d524769103ff8cca0
[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 #include <sys/cdefs.h>
31
32 #include <libkern/c++/OSNumber.h>
33 #include <libkern/c++/OSString.h>
34 #include <libkern/c++/OSSerialize.h>
35 #include <libkern/c++/OSLib.h>
36
37 #define sizeMask (~0ULL >> (64 - size))
38
39 #define super OSObject
40
41 OSDefineMetaClassAndStructors(OSNumber, OSObject)
42
43 OSMetaClassDefineReservedUnused(OSNumber, 0);
44 OSMetaClassDefineReservedUnused(OSNumber, 1);
45 OSMetaClassDefineReservedUnused(OSNumber, 2);
46 OSMetaClassDefineReservedUnused(OSNumber, 3);
47 OSMetaClassDefineReservedUnused(OSNumber, 4);
48 OSMetaClassDefineReservedUnused(OSNumber, 5);
49 OSMetaClassDefineReservedUnused(OSNumber, 6);
50 OSMetaClassDefineReservedUnused(OSNumber, 7);
51
52 bool
53 OSNumber::init(unsigned long long inValue, unsigned int newNumberOfBits)
54 {
55 if (!super::init()) {
56 return false;
57 }
58 if (newNumberOfBits > 64) {
59 return false;
60 }
61
62 size = newNumberOfBits;
63 value = (inValue & sizeMask);
64
65 return true;
66 }
67
68 bool
69 OSNumber::init(const char *newValue, unsigned int newNumberOfBits)
70 {
71 return init((unsigned long long)strtoul(newValue, NULL, 0), newNumberOfBits);
72 }
73
74 void
75 OSNumber::free()
76 {
77 super::free();
78 }
79
80 OSNumber *
81 OSNumber::withNumber(unsigned long long value,
82 unsigned int newNumberOfBits)
83 {
84 OSNumber *me = new OSNumber;
85
86 if (me && !me->init(value, newNumberOfBits)) {
87 me->release();
88 return 0;
89 }
90
91 return me;
92 }
93
94 OSNumber *
95 OSNumber::withNumber(const char *value, unsigned int newNumberOfBits)
96 {
97 OSNumber *me = new OSNumber;
98
99 if (me && !me->init(value, newNumberOfBits)) {
100 me->release();
101 return 0;
102 }
103
104 return me;
105 }
106
107 unsigned int
108 OSNumber::numberOfBits() const
109 {
110 return size;
111 }
112
113 unsigned int
114 OSNumber::numberOfBytes() const
115 {
116 return (size + 7) / 8;
117 }
118
119
120 unsigned char
121 OSNumber::unsigned8BitValue() const
122 {
123 return (unsigned char) value;
124 }
125
126 unsigned short
127 OSNumber::unsigned16BitValue() const
128 {
129 return (unsigned short) value;
130 }
131
132 unsigned int
133 OSNumber::unsigned32BitValue() const
134 {
135 return (unsigned int) value;
136 }
137
138 unsigned long long
139 OSNumber::unsigned64BitValue() const
140 {
141 return value;
142 }
143
144 void
145 OSNumber::addValue(signed long long inValue)
146 {
147 value = ((value + inValue) & sizeMask);
148 }
149
150 void
151 OSNumber::setValue(unsigned long long inValue)
152 {
153 value = (inValue & sizeMask);
154 }
155
156 bool
157 OSNumber::isEqualTo(const OSNumber *integer) const
158 {
159 return value == integer->value;
160 }
161
162 bool
163 OSNumber::isEqualTo(const OSMetaClassBase *obj) const
164 {
165 OSNumber * offset;
166 if ((offset = OSDynamicCast(OSNumber, obj))) {
167 return isEqualTo(offset);
168 } else {
169 return false;
170 }
171 }
172
173 bool
174 OSNumber::serialize(OSSerialize *s) const
175 {
176 char temp[32];
177
178 if (s->previouslySerialized(this)) {
179 return true;
180 }
181
182 snprintf(temp, sizeof(temp), "integer size=\"%d\"", size);
183 if (!s->addXMLStartTag(this, temp)) {
184 return false;
185 }
186
187 //XXX sprintf(temp, "0x%qx", value);
188 if ((value >> 32)) {
189 snprintf(temp, sizeof(temp), "0x%lx%08lx", (unsigned long)(value >> 32),
190 (unsigned long)(value & 0xFFFFFFFF));
191 } else {
192 snprintf(temp, sizeof(temp), "0x%lx", (unsigned long)value);
193 }
194 if (!s->addString(temp)) {
195 return false;
196 }
197
198 return s->addXMLEndTag("integer");
199 }