1*27b03b36SApple OSS Distributions /*
2*27b03b36SApple OSS Distributions * Copyright (c) 2000-2006 Apple Computer, Inc. All rights reserved.
3*27b03b36SApple OSS Distributions *
4*27b03b36SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*27b03b36SApple OSS Distributions *
6*27b03b36SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*27b03b36SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*27b03b36SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*27b03b36SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*27b03b36SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*27b03b36SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*27b03b36SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*27b03b36SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*27b03b36SApple OSS Distributions *
15*27b03b36SApple OSS Distributions * Please obtain a copy of the License at
16*27b03b36SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*27b03b36SApple OSS Distributions *
18*27b03b36SApple OSS Distributions * The Original Code and all software distributed under the License are
19*27b03b36SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*27b03b36SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*27b03b36SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*27b03b36SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*27b03b36SApple OSS Distributions * Please see the License for the specific language governing rights and
24*27b03b36SApple OSS Distributions * limitations under the License.
25*27b03b36SApple OSS Distributions *
26*27b03b36SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*27b03b36SApple OSS Distributions */
28*27b03b36SApple OSS Distributions /* IOOffset.m created by rsulack on Wed 17-Sep-1997 */
29*27b03b36SApple OSS Distributions
30*27b03b36SApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
31*27b03b36SApple OSS Distributions
32*27b03b36SApple OSS Distributions #include <sys/cdefs.h>
33*27b03b36SApple OSS Distributions
34*27b03b36SApple OSS Distributions #include <libkern/c++/OSNumber.h>
35*27b03b36SApple OSS Distributions #include <libkern/c++/OSString.h>
36*27b03b36SApple OSS Distributions #include <libkern/c++/OSSerialize.h>
37*27b03b36SApple OSS Distributions #include <libkern/c++/OSSharedPtr.h>
38*27b03b36SApple OSS Distributions #include <libkern/c++/OSLib.h>
39*27b03b36SApple OSS Distributions
40*27b03b36SApple OSS Distributions #define sizeMask (~0ULL >> (64 - size))
41*27b03b36SApple OSS Distributions
42*27b03b36SApple OSS Distributions #define super OSObject
43*27b03b36SApple OSS Distributions
44*27b03b36SApple OSS Distributions OSDefineMetaClassAndStructorsWithZone(OSNumber, OSObject,
45*27b03b36SApple OSS Distributions (zone_create_flags_t) (ZC_CACHING | ZC_ZFREE_CLEARMEM))
46*27b03b36SApple OSS Distributions
47*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSNumber, 0);
48*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSNumber, 1);
49*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSNumber, 2);
50*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSNumber, 3);
51*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSNumber, 4);
52*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSNumber, 5);
53*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSNumber, 6);
54*27b03b36SApple OSS Distributions OSMetaClassDefineReservedUnused(OSNumber, 7);
55*27b03b36SApple OSS Distributions
56*27b03b36SApple OSS Distributions bool
init(unsigned long long inValue,unsigned int newNumberOfBits)57*27b03b36SApple OSS Distributions OSNumber::init(unsigned long long inValue, unsigned int newNumberOfBits)
58*27b03b36SApple OSS Distributions {
59*27b03b36SApple OSS Distributions if (!super::init()) {
60*27b03b36SApple OSS Distributions return false;
61*27b03b36SApple OSS Distributions }
62*27b03b36SApple OSS Distributions if (newNumberOfBits > 64) {
63*27b03b36SApple OSS Distributions return false;
64*27b03b36SApple OSS Distributions }
65*27b03b36SApple OSS Distributions
66*27b03b36SApple OSS Distributions size = newNumberOfBits;
67*27b03b36SApple OSS Distributions value = (inValue & sizeMask);
68*27b03b36SApple OSS Distributions
69*27b03b36SApple OSS Distributions return true;
70*27b03b36SApple OSS Distributions }
71*27b03b36SApple OSS Distributions
72*27b03b36SApple OSS Distributions bool
init(const char * newValue,unsigned int newNumberOfBits)73*27b03b36SApple OSS Distributions OSNumber::init(const char *newValue, unsigned int newNumberOfBits)
74*27b03b36SApple OSS Distributions {
75*27b03b36SApple OSS Distributions return init((unsigned long long)strtoul(newValue, NULL, 0), newNumberOfBits);
76*27b03b36SApple OSS Distributions }
77*27b03b36SApple OSS Distributions
78*27b03b36SApple OSS Distributions void
free()79*27b03b36SApple OSS Distributions OSNumber::free()
80*27b03b36SApple OSS Distributions {
81*27b03b36SApple OSS Distributions super::free();
82*27b03b36SApple OSS Distributions }
83*27b03b36SApple OSS Distributions
84*27b03b36SApple OSS Distributions OSSharedPtr<OSNumber>
withNumber(unsigned long long value,unsigned int newNumberOfBits)85*27b03b36SApple OSS Distributions OSNumber::withNumber(unsigned long long value,
86*27b03b36SApple OSS Distributions unsigned int newNumberOfBits)
87*27b03b36SApple OSS Distributions {
88*27b03b36SApple OSS Distributions OSSharedPtr<OSNumber> me = OSMakeShared<OSNumber>();
89*27b03b36SApple OSS Distributions
90*27b03b36SApple OSS Distributions if (me && !me->init(value, newNumberOfBits)) {
91*27b03b36SApple OSS Distributions return nullptr;
92*27b03b36SApple OSS Distributions }
93*27b03b36SApple OSS Distributions
94*27b03b36SApple OSS Distributions return me;
95*27b03b36SApple OSS Distributions }
96*27b03b36SApple OSS Distributions
97*27b03b36SApple OSS Distributions OSSharedPtr<OSNumber>
withNumber(const char * value,unsigned int newNumberOfBits)98*27b03b36SApple OSS Distributions OSNumber::withNumber(const char *value, unsigned int newNumberOfBits)
99*27b03b36SApple OSS Distributions {
100*27b03b36SApple OSS Distributions OSSharedPtr<OSNumber> me = OSMakeShared<OSNumber>();
101*27b03b36SApple OSS Distributions
102*27b03b36SApple OSS Distributions if (me && !me->init(value, newNumberOfBits)) {
103*27b03b36SApple OSS Distributions return nullptr;
104*27b03b36SApple OSS Distributions }
105*27b03b36SApple OSS Distributions
106*27b03b36SApple OSS Distributions return me;
107*27b03b36SApple OSS Distributions }
108*27b03b36SApple OSS Distributions
109*27b03b36SApple OSS Distributions unsigned int
numberOfBits() const110*27b03b36SApple OSS Distributions OSNumber::numberOfBits() const
111*27b03b36SApple OSS Distributions {
112*27b03b36SApple OSS Distributions return size;
113*27b03b36SApple OSS Distributions }
114*27b03b36SApple OSS Distributions
115*27b03b36SApple OSS Distributions unsigned int
numberOfBytes() const116*27b03b36SApple OSS Distributions OSNumber::numberOfBytes() const
117*27b03b36SApple OSS Distributions {
118*27b03b36SApple OSS Distributions return (size + 7) / 8;
119*27b03b36SApple OSS Distributions }
120*27b03b36SApple OSS Distributions
121*27b03b36SApple OSS Distributions
122*27b03b36SApple OSS Distributions unsigned char
unsigned8BitValue() const123*27b03b36SApple OSS Distributions OSNumber::unsigned8BitValue() const
124*27b03b36SApple OSS Distributions {
125*27b03b36SApple OSS Distributions return (unsigned char) value;
126*27b03b36SApple OSS Distributions }
127*27b03b36SApple OSS Distributions
128*27b03b36SApple OSS Distributions unsigned short
unsigned16BitValue() const129*27b03b36SApple OSS Distributions OSNumber::unsigned16BitValue() const
130*27b03b36SApple OSS Distributions {
131*27b03b36SApple OSS Distributions return (unsigned short) value;
132*27b03b36SApple OSS Distributions }
133*27b03b36SApple OSS Distributions
134*27b03b36SApple OSS Distributions unsigned int
unsigned32BitValue() const135*27b03b36SApple OSS Distributions OSNumber::unsigned32BitValue() const
136*27b03b36SApple OSS Distributions {
137*27b03b36SApple OSS Distributions return (unsigned int) value;
138*27b03b36SApple OSS Distributions }
139*27b03b36SApple OSS Distributions
140*27b03b36SApple OSS Distributions unsigned long long
unsigned64BitValue() const141*27b03b36SApple OSS Distributions OSNumber::unsigned64BitValue() const
142*27b03b36SApple OSS Distributions {
143*27b03b36SApple OSS Distributions return value;
144*27b03b36SApple OSS Distributions }
145*27b03b36SApple OSS Distributions
146*27b03b36SApple OSS Distributions void
addValue(signed long long inValue)147*27b03b36SApple OSS Distributions OSNumber::addValue(signed long long inValue)
148*27b03b36SApple OSS Distributions {
149*27b03b36SApple OSS Distributions value = ((value + inValue) & sizeMask);
150*27b03b36SApple OSS Distributions }
151*27b03b36SApple OSS Distributions
152*27b03b36SApple OSS Distributions void
setValue(unsigned long long inValue)153*27b03b36SApple OSS Distributions OSNumber::setValue(unsigned long long inValue)
154*27b03b36SApple OSS Distributions {
155*27b03b36SApple OSS Distributions value = (inValue & sizeMask);
156*27b03b36SApple OSS Distributions }
157*27b03b36SApple OSS Distributions
158*27b03b36SApple OSS Distributions bool
isEqualTo(const OSNumber * integer) const159*27b03b36SApple OSS Distributions OSNumber::isEqualTo(const OSNumber *integer) const
160*27b03b36SApple OSS Distributions {
161*27b03b36SApple OSS Distributions return value == integer->value;
162*27b03b36SApple OSS Distributions }
163*27b03b36SApple OSS Distributions
164*27b03b36SApple OSS Distributions bool
isEqualTo(const OSMetaClassBase * obj) const165*27b03b36SApple OSS Distributions OSNumber::isEqualTo(const OSMetaClassBase *obj) const
166*27b03b36SApple OSS Distributions {
167*27b03b36SApple OSS Distributions OSNumber * offset;
168*27b03b36SApple OSS Distributions if ((offset = OSDynamicCast(OSNumber, obj))) {
169*27b03b36SApple OSS Distributions return isEqualTo(offset);
170*27b03b36SApple OSS Distributions } else {
171*27b03b36SApple OSS Distributions return false;
172*27b03b36SApple OSS Distributions }
173*27b03b36SApple OSS Distributions }
174*27b03b36SApple OSS Distributions
175*27b03b36SApple OSS Distributions bool
serialize(OSSerialize * s) const176*27b03b36SApple OSS Distributions OSNumber::serialize(OSSerialize *s) const
177*27b03b36SApple OSS Distributions {
178*27b03b36SApple OSS Distributions char temp[32];
179*27b03b36SApple OSS Distributions
180*27b03b36SApple OSS Distributions if (s->previouslySerialized(this)) {
181*27b03b36SApple OSS Distributions return true;
182*27b03b36SApple OSS Distributions }
183*27b03b36SApple OSS Distributions
184*27b03b36SApple OSS Distributions snprintf(temp, sizeof(temp), "integer size=\"%d\"", size);
185*27b03b36SApple OSS Distributions if (!s->addXMLStartTag(this, temp)) {
186*27b03b36SApple OSS Distributions return false;
187*27b03b36SApple OSS Distributions }
188*27b03b36SApple OSS Distributions
189*27b03b36SApple OSS Distributions //XXX sprintf(temp, "0x%qx", value);
190*27b03b36SApple OSS Distributions if ((value >> 32)) {
191*27b03b36SApple OSS Distributions snprintf(temp, sizeof(temp), "0x%lx%08lx", (unsigned long)(value >> 32),
192*27b03b36SApple OSS Distributions (unsigned long)(value & 0xFFFFFFFF));
193*27b03b36SApple OSS Distributions } else {
194*27b03b36SApple OSS Distributions snprintf(temp, sizeof(temp), "0x%lx", (unsigned long)value);
195*27b03b36SApple OSS Distributions }
196*27b03b36SApple OSS Distributions if (!s->addString(temp)) {
197*27b03b36SApple OSS Distributions return false;
198*27b03b36SApple OSS Distributions }
199*27b03b36SApple OSS Distributions
200*27b03b36SApple OSS Distributions return s->addXMLEndTag("integer");
201*27b03b36SApple OSS Distributions }
202