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