]> git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSNumber.cpp
e327230f9a584d25432adf899a6b0c5a9e3fde5e
[apple/xnu.git] / libkern / c++ / OSNumber.cpp
1 /*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25 /* IOOffset.m created by rsulack on Wed 17-Sep-1997 */
26
27 #include <sys/cdefs.h>
28
29 __BEGIN_DECLS
30 extern int sscanf(const char *input, const char *fmt, ...);
31 __END_DECLS
32
33 #include <libkern/c++/OSNumber.h>
34 #include <libkern/c++/OSString.h>
35 #include <libkern/c++/OSSerialize.h>
36 #include <libkern/c++/OSLib.h>
37
38 #define sizeMask (~0ULL >> (64 - size))
39
40 #define super OSObject
41
42 OSDefineMetaClassAndStructors(OSNumber, OSObject)
43
44 OSMetaClassDefineReservedUnused(OSNumber, 0);
45 OSMetaClassDefineReservedUnused(OSNumber, 1);
46 OSMetaClassDefineReservedUnused(OSNumber, 2);
47 OSMetaClassDefineReservedUnused(OSNumber, 3);
48 OSMetaClassDefineReservedUnused(OSNumber, 4);
49 OSMetaClassDefineReservedUnused(OSNumber, 5);
50 OSMetaClassDefineReservedUnused(OSNumber, 6);
51 OSMetaClassDefineReservedUnused(OSNumber, 7);
52
53 bool OSNumber::init(unsigned long long inValue, unsigned int numberOfBits)
54 {
55 if (!super::init())
56 return false;
57
58 size = numberOfBits;
59 value = (inValue & sizeMask);
60
61 return true;
62 }
63
64 bool OSNumber::init(const char *value, unsigned int numberOfBits)
65 {
66 unsigned long long thisOffset;
67
68 #ifdef q_works
69 sscanf(value, "%qi", thisOffset);
70 #else
71 unsigned int smallOffset;
72
73 sscanf(value, "%i", &smallOffset);
74 thisOffset = smallOffset;
75 #endif
76
77 return init(thisOffset, numberOfBits);
78 }
79
80 void OSNumber::free() { super::free(); }
81
82 OSNumber *OSNumber::withNumber(unsigned long long value,
83 unsigned int numberOfBits)
84 {
85 OSNumber *me = new OSNumber;
86
87 if (me && !me->init(value, numberOfBits)) {
88 me->release();
89 return 0;
90 }
91
92 return me;
93 }
94
95 OSNumber *OSNumber::withNumber(const char *value, unsigned int numberOfBits)
96 {
97 OSNumber *me = new OSNumber;
98
99 if (me && !me->init(value, numberOfBits)) {
100 me->release();
101 return 0;
102 }
103
104 return me;
105 }
106
107 unsigned int OSNumber::numberOfBits() const { return size; }
108
109 unsigned int OSNumber::numberOfBytes() const { return (size + 7) / 8; }
110
111
112 unsigned char OSNumber::unsigned8BitValue() const
113 {
114 return (unsigned char) value;
115 }
116
117 unsigned short OSNumber::unsigned16BitValue() const
118 {
119 return (unsigned short) value;
120 }
121
122 unsigned int OSNumber::unsigned32BitValue() const
123 {
124 return (unsigned int) value;
125 }
126
127 unsigned long long OSNumber::unsigned64BitValue() const
128 {
129 return value;
130 }
131
132 void OSNumber::addValue(signed long long inValue)
133 {
134 value = ((value + inValue) & sizeMask);
135 }
136
137 void OSNumber::setValue(unsigned long long inValue)
138 {
139 value = (inValue & sizeMask);
140 }
141
142 bool OSNumber::isEqualTo(const OSNumber *integer) const
143 {
144 return((value == integer->value));
145 }
146
147 bool OSNumber::isEqualTo(const OSMetaClassBase *obj) const
148 {
149 OSNumber * offset;
150 if ((offset = OSDynamicCast(OSNumber, obj)))
151 return isEqualTo(offset);
152 else
153 return false;
154 }
155
156 bool OSNumber::serialize(OSSerialize *s) const
157 {
158 char temp[32];
159
160 if (s->previouslySerialized(this)) return true;
161
162 sprintf(temp, "integer size=\"%d\"", size);
163 if (!s->addXMLStartTag(this, temp)) return false;
164
165 //XXX sprintf(temp, "0x%qx", value);
166 if ((value >> 32)) {
167 sprintf(temp, "0x%lx%08lx", (unsigned long)(value >> 32),
168 (unsigned long)(value & 0xFFFFFFFF));
169 } else {
170 sprintf(temp, "0x%lx", (unsigned long)value);
171 }
172 if (!s->addString(temp)) return false;
173
174 return s->addXMLEndTag("integer");
175 }