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