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