1*2c2f96dcSApple OSS Distributions /*
2*2c2f96dcSApple OSS Distributions * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3*2c2f96dcSApple OSS Distributions *
4*2c2f96dcSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*2c2f96dcSApple OSS Distributions *
6*2c2f96dcSApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*2c2f96dcSApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*2c2f96dcSApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*2c2f96dcSApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*2c2f96dcSApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*2c2f96dcSApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*2c2f96dcSApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*2c2f96dcSApple OSS Distributions * terms of an Apple operating system software license agreement.
14*2c2f96dcSApple OSS Distributions *
15*2c2f96dcSApple OSS Distributions * Please obtain a copy of the License at
16*2c2f96dcSApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*2c2f96dcSApple OSS Distributions *
18*2c2f96dcSApple OSS Distributions * The Original Code and all software distributed under the License are
19*2c2f96dcSApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*2c2f96dcSApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*2c2f96dcSApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*2c2f96dcSApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*2c2f96dcSApple OSS Distributions * Please see the License for the specific language governing rights and
24*2c2f96dcSApple OSS Distributions * limitations under the License.
25*2c2f96dcSApple OSS Distributions *
26*2c2f96dcSApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*2c2f96dcSApple OSS Distributions */
28*2c2f96dcSApple OSS Distributions /* IOOffset.m created by rsulack on Wed 17-Sep-1997 */
29*2c2f96dcSApple OSS Distributions
30*2c2f96dcSApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
31*2c2f96dcSApple OSS Distributions
32*2c2f96dcSApple OSS Distributions #include <sys/cdefs.h>
33*2c2f96dcSApple OSS Distributions
34*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSNumber.h>
35*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSString.h>
36*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSSerialize.h>
37*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSSharedPtr.h>
38*2c2f96dcSApple OSS Distributions #include <libkern/c++/OSLib.h>
39*2c2f96dcSApple OSS Distributions
40*2c2f96dcSApple OSS Distributions #define sizeMask (~0ULL >> (64 - size))
41*2c2f96dcSApple OSS Distributions
42*2c2f96dcSApple OSS Distributions #define super OSObject
43*2c2f96dcSApple OSS Distributions
44*2c2f96dcSApple OSS Distributions OSDefineMetaClassAndStructorsWithZone(OSNumber, OSObject,
45*2c2f96dcSApple OSS Distributions (zone_create_flags_t) (ZC_CACHING | ZC_ZFREE_CLEARMEM))
46*2c2f96dcSApple OSS Distributions
47*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(OSNumber, 0);
48*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(OSNumber, 1);
49*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(OSNumber, 2);
50*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(OSNumber, 3);
51*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(OSNumber, 4);
52*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(OSNumber, 5);
53*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(OSNumber, 6);
54*2c2f96dcSApple OSS Distributions OSMetaClassDefineReservedUnused(OSNumber, 7);
55*2c2f96dcSApple OSS Distributions
56*2c2f96dcSApple OSS Distributions bool
init(unsigned long long inValue,unsigned int newNumberOfBits)57*2c2f96dcSApple OSS Distributions OSNumber::init(unsigned long long inValue, unsigned int newNumberOfBits)
58*2c2f96dcSApple OSS Distributions {
59*2c2f96dcSApple OSS Distributions if (!super::init()) {
60*2c2f96dcSApple OSS Distributions return false;
61*2c2f96dcSApple OSS Distributions }
62*2c2f96dcSApple OSS Distributions if (newNumberOfBits > 64) {
63*2c2f96dcSApple OSS Distributions return false;
64*2c2f96dcSApple OSS Distributions }
65*2c2f96dcSApple OSS Distributions
66*2c2f96dcSApple OSS Distributions size = newNumberOfBits;
67*2c2f96dcSApple OSS Distributions value = (inValue & sizeMask);
68*2c2f96dcSApple OSS Distributions
69*2c2f96dcSApple OSS Distributions return true;
70*2c2f96dcSApple OSS Distributions }
71*2c2f96dcSApple OSS Distributions
72*2c2f96dcSApple OSS Distributions bool
init(const char * newValue,unsigned int newNumberOfBits)73*2c2f96dcSApple OSS Distributions OSNumber::init(const char *newValue, unsigned int newNumberOfBits)
74*2c2f96dcSApple OSS Distributions {
75*2c2f96dcSApple OSS Distributions return init((unsigned long long)strtoul(newValue, NULL, 0), newNumberOfBits);
76*2c2f96dcSApple OSS Distributions }
77*2c2f96dcSApple OSS Distributions
78*2c2f96dcSApple OSS Distributions void
free()79*2c2f96dcSApple OSS Distributions OSNumber::free()
80*2c2f96dcSApple OSS Distributions {
81*2c2f96dcSApple OSS Distributions super::free();
82*2c2f96dcSApple OSS Distributions }
83*2c2f96dcSApple OSS Distributions
84*2c2f96dcSApple OSS Distributions OSSharedPtr<OSNumber>
withNumber(unsigned long long value,unsigned int newNumberOfBits)85*2c2f96dcSApple OSS Distributions OSNumber::withNumber(unsigned long long value,
86*2c2f96dcSApple OSS Distributions unsigned int newNumberOfBits)
87*2c2f96dcSApple OSS Distributions {
88*2c2f96dcSApple OSS Distributions OSSharedPtr<OSNumber> me = OSMakeShared<OSNumber>();
89*2c2f96dcSApple OSS Distributions
90*2c2f96dcSApple OSS Distributions if (me && !me->init(value, newNumberOfBits)) {
91*2c2f96dcSApple OSS Distributions return nullptr;
92*2c2f96dcSApple OSS Distributions }
93*2c2f96dcSApple OSS Distributions
94*2c2f96dcSApple OSS Distributions return me;
95*2c2f96dcSApple OSS Distributions }
96*2c2f96dcSApple OSS Distributions
97*2c2f96dcSApple OSS Distributions OSSharedPtr<OSNumber>
withNumber(const char * value,unsigned int newNumberOfBits)98*2c2f96dcSApple OSS Distributions OSNumber::withNumber(const char *value, unsigned int newNumberOfBits)
99*2c2f96dcSApple OSS Distributions {
100*2c2f96dcSApple OSS Distributions OSSharedPtr<OSNumber> me = OSMakeShared<OSNumber>();
101*2c2f96dcSApple OSS Distributions
102*2c2f96dcSApple OSS Distributions if (me && !me->init(value, newNumberOfBits)) {
103*2c2f96dcSApple OSS Distributions return nullptr;
104*2c2f96dcSApple OSS Distributions }
105*2c2f96dcSApple OSS Distributions
106*2c2f96dcSApple OSS Distributions return me;
107*2c2f96dcSApple OSS Distributions }
108*2c2f96dcSApple OSS Distributions
109*2c2f96dcSApple OSS Distributions OSSharedPtr<OSNumber>
withDouble(double value)110*2c2f96dcSApple OSS Distributions OSNumber::withDouble(
111*2c2f96dcSApple OSS Distributions double value)
112*2c2f96dcSApple OSS Distributions {
113*2c2f96dcSApple OSS Distributions OSSharedPtr<OSNumber> me = OSMakeShared<OSNumber>();
114*2c2f96dcSApple OSS Distributions
115*2c2f96dcSApple OSS Distributions if (me && !me->OSObject::init()) {
116*2c2f96dcSApple OSS Distributions return nullptr;
117*2c2f96dcSApple OSS Distributions }
118*2c2f96dcSApple OSS Distributions me->size = 63;
119*2c2f96dcSApple OSS Distributions me->fpValue = value;
120*2c2f96dcSApple OSS Distributions
121*2c2f96dcSApple OSS Distributions return me;
122*2c2f96dcSApple OSS Distributions }
123*2c2f96dcSApple OSS Distributions
124*2c2f96dcSApple OSS Distributions OSSharedPtr<OSNumber>
withFloat(float value)125*2c2f96dcSApple OSS Distributions OSNumber::withFloat(
126*2c2f96dcSApple OSS Distributions float value)
127*2c2f96dcSApple OSS Distributions {
128*2c2f96dcSApple OSS Distributions OSSharedPtr<OSNumber> me = OSMakeShared<OSNumber>();
129*2c2f96dcSApple OSS Distributions
130*2c2f96dcSApple OSS Distributions if (me && !me->OSObject::init()) {
131*2c2f96dcSApple OSS Distributions return nullptr;
132*2c2f96dcSApple OSS Distributions }
133*2c2f96dcSApple OSS Distributions me->size = 31;
134*2c2f96dcSApple OSS Distributions me->fpValue = (double) value;
135*2c2f96dcSApple OSS Distributions
136*2c2f96dcSApple OSS Distributions return me;
137*2c2f96dcSApple OSS Distributions }
138*2c2f96dcSApple OSS Distributions
139*2c2f96dcSApple OSS Distributions double
doubleValue() const140*2c2f96dcSApple OSS Distributions OSNumber::doubleValue() const
141*2c2f96dcSApple OSS Distributions {
142*2c2f96dcSApple OSS Distributions if ((size != 63) && (size != 31)) {
143*2c2f96dcSApple OSS Distributions return (double) value;
144*2c2f96dcSApple OSS Distributions }
145*2c2f96dcSApple OSS Distributions return fpValue;
146*2c2f96dcSApple OSS Distributions }
147*2c2f96dcSApple OSS Distributions
148*2c2f96dcSApple OSS Distributions float
floatValue() const149*2c2f96dcSApple OSS Distributions OSNumber::floatValue() const
150*2c2f96dcSApple OSS Distributions {
151*2c2f96dcSApple OSS Distributions if ((size != 63) && (size != 31)) {
152*2c2f96dcSApple OSS Distributions return (float) value;
153*2c2f96dcSApple OSS Distributions }
154*2c2f96dcSApple OSS Distributions return (float) fpValue;
155*2c2f96dcSApple OSS Distributions }
156*2c2f96dcSApple OSS Distributions
157*2c2f96dcSApple OSS Distributions unsigned int
numberOfBits() const158*2c2f96dcSApple OSS Distributions OSNumber::numberOfBits() const
159*2c2f96dcSApple OSS Distributions {
160*2c2f96dcSApple OSS Distributions return size;
161*2c2f96dcSApple OSS Distributions }
162*2c2f96dcSApple OSS Distributions
163*2c2f96dcSApple OSS Distributions unsigned int
numberOfBytes() const164*2c2f96dcSApple OSS Distributions OSNumber::numberOfBytes() const
165*2c2f96dcSApple OSS Distributions {
166*2c2f96dcSApple OSS Distributions return (size + 7) / 8;
167*2c2f96dcSApple OSS Distributions }
168*2c2f96dcSApple OSS Distributions
169*2c2f96dcSApple OSS Distributions
170*2c2f96dcSApple OSS Distributions unsigned char
unsigned8BitValue() const171*2c2f96dcSApple OSS Distributions OSNumber::unsigned8BitValue() const
172*2c2f96dcSApple OSS Distributions {
173*2c2f96dcSApple OSS Distributions if ((size == 63) || (size == 31)) {
174*2c2f96dcSApple OSS Distributions return (unsigned char) fpValue;
175*2c2f96dcSApple OSS Distributions }
176*2c2f96dcSApple OSS Distributions return (unsigned char) value;
177*2c2f96dcSApple OSS Distributions }
178*2c2f96dcSApple OSS Distributions
179*2c2f96dcSApple OSS Distributions unsigned short
unsigned16BitValue() const180*2c2f96dcSApple OSS Distributions OSNumber::unsigned16BitValue() const
181*2c2f96dcSApple OSS Distributions {
182*2c2f96dcSApple OSS Distributions if ((size == 63) || (size == 31)) {
183*2c2f96dcSApple OSS Distributions return (unsigned short) fpValue;
184*2c2f96dcSApple OSS Distributions }
185*2c2f96dcSApple OSS Distributions return (unsigned short) value;
186*2c2f96dcSApple OSS Distributions }
187*2c2f96dcSApple OSS Distributions
188*2c2f96dcSApple OSS Distributions unsigned int
unsigned32BitValue() const189*2c2f96dcSApple OSS Distributions OSNumber::unsigned32BitValue() const
190*2c2f96dcSApple OSS Distributions {
191*2c2f96dcSApple OSS Distributions if ((size == 63) || (size == 31)) {
192*2c2f96dcSApple OSS Distributions return (unsigned int) fpValue;
193*2c2f96dcSApple OSS Distributions }
194*2c2f96dcSApple OSS Distributions return (unsigned int) value;
195*2c2f96dcSApple OSS Distributions }
196*2c2f96dcSApple OSS Distributions
197*2c2f96dcSApple OSS Distributions unsigned long long
unsigned64BitValue() const198*2c2f96dcSApple OSS Distributions OSNumber::unsigned64BitValue() const
199*2c2f96dcSApple OSS Distributions {
200*2c2f96dcSApple OSS Distributions if ((size == 63) || (size == 31)) {
201*2c2f96dcSApple OSS Distributions return (unsigned long long) fpValue;
202*2c2f96dcSApple OSS Distributions }
203*2c2f96dcSApple OSS Distributions return value;
204*2c2f96dcSApple OSS Distributions }
205*2c2f96dcSApple OSS Distributions
206*2c2f96dcSApple OSS Distributions void
addValue(signed long long inValue)207*2c2f96dcSApple OSS Distributions OSNumber::addValue(signed long long inValue)
208*2c2f96dcSApple OSS Distributions {
209*2c2f96dcSApple OSS Distributions if ((size == 63) || (size == 31)) {
210*2c2f96dcSApple OSS Distributions fpValue += inValue;
211*2c2f96dcSApple OSS Distributions } else {
212*2c2f96dcSApple OSS Distributions value = ((value + inValue) & sizeMask);
213*2c2f96dcSApple OSS Distributions }
214*2c2f96dcSApple OSS Distributions }
215*2c2f96dcSApple OSS Distributions
216*2c2f96dcSApple OSS Distributions void
setValue(unsigned long long inValue)217*2c2f96dcSApple OSS Distributions OSNumber::setValue(unsigned long long inValue)
218*2c2f96dcSApple OSS Distributions {
219*2c2f96dcSApple OSS Distributions if ((size == 63) || (size == 31)) {
220*2c2f96dcSApple OSS Distributions fpValue = (double) inValue;
221*2c2f96dcSApple OSS Distributions } else {
222*2c2f96dcSApple OSS Distributions value = (inValue & sizeMask);
223*2c2f96dcSApple OSS Distributions }
224*2c2f96dcSApple OSS Distributions }
225*2c2f96dcSApple OSS Distributions
226*2c2f96dcSApple OSS Distributions bool
isEqualTo(const OSNumber * integer) const227*2c2f96dcSApple OSS Distributions OSNumber::isEqualTo(const OSNumber *integer) const
228*2c2f96dcSApple OSS Distributions {
229*2c2f96dcSApple OSS Distributions return unsigned64BitValue() == integer->unsigned64BitValue();
230*2c2f96dcSApple OSS Distributions }
231*2c2f96dcSApple OSS Distributions
232*2c2f96dcSApple OSS Distributions bool
isEqualTo(const OSMetaClassBase * obj) const233*2c2f96dcSApple OSS Distributions OSNumber::isEqualTo(const OSMetaClassBase *obj) const
234*2c2f96dcSApple OSS Distributions {
235*2c2f96dcSApple OSS Distributions OSNumber * offset;
236*2c2f96dcSApple OSS Distributions if ((offset = OSDynamicCast(OSNumber, obj))) {
237*2c2f96dcSApple OSS Distributions return isEqualTo(offset);
238*2c2f96dcSApple OSS Distributions } else {
239*2c2f96dcSApple OSS Distributions return false;
240*2c2f96dcSApple OSS Distributions }
241*2c2f96dcSApple OSS Distributions }
242*2c2f96dcSApple OSS Distributions
243*2c2f96dcSApple OSS Distributions bool
serialize(OSSerialize * s) const244*2c2f96dcSApple OSS Distributions OSNumber::serialize(OSSerialize *s) const
245*2c2f96dcSApple OSS Distributions {
246*2c2f96dcSApple OSS Distributions char temp[32];
247*2c2f96dcSApple OSS Distributions
248*2c2f96dcSApple OSS Distributions if (s->previouslySerialized(this)) {
249*2c2f96dcSApple OSS Distributions return true;
250*2c2f96dcSApple OSS Distributions }
251*2c2f96dcSApple OSS Distributions
252*2c2f96dcSApple OSS Distributions snprintf(temp, sizeof(temp), "integer size=\"%d\"", size);
253*2c2f96dcSApple OSS Distributions if (!s->addXMLStartTag(this, temp)) {
254*2c2f96dcSApple OSS Distributions return false;
255*2c2f96dcSApple OSS Distributions }
256*2c2f96dcSApple OSS Distributions
257*2c2f96dcSApple OSS Distributions //XXX sprintf(temp, "0x%qx", value);
258*2c2f96dcSApple OSS Distributions if ((value >> 32)) {
259*2c2f96dcSApple OSS Distributions snprintf(temp, sizeof(temp), "0x%lx%08lx", (unsigned long)(value >> 32),
260*2c2f96dcSApple OSS Distributions (unsigned long)(value & 0xFFFFFFFF));
261*2c2f96dcSApple OSS Distributions } else {
262*2c2f96dcSApple OSS Distributions snprintf(temp, sizeof(temp), "0x%lx", (unsigned long)value);
263*2c2f96dcSApple OSS Distributions }
264*2c2f96dcSApple OSS Distributions if (!s->addString(temp)) {
265*2c2f96dcSApple OSS Distributions return false;
266*2c2f96dcSApple OSS Distributions }
267*2c2f96dcSApple OSS Distributions
268*2c2f96dcSApple OSS Distributions return s->addXMLEndTag("integer");
269*2c2f96dcSApple OSS Distributions }
270