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