]> git.saurik.com Git - apple/xnu.git/blame - libkern/c++/OSNumber.cpp
xnu-344.49.tar.gz
[apple/xnu.git] / libkern / c++ / OSNumber.cpp
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
43866e37 6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
1c79356b 7 *
43866e37
A
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
1c79356b
A
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
43866e37
A
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.
1c79356b
A
22 *
23 * @APPLE_LICENSE_HEADER_END@
24 */
25/* IOOffset.m created by rsulack on Wed 17-Sep-1997 */
26
9bccf70c
A
27#include <sys/cdefs.h>
28
29__BEGIN_DECLS
30extern int sscanf(const char *input, const char *fmt, ...);
31__END_DECLS
32
1c79356b
A
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
0b4e3aa0 38#define sizeMask (~0ULL >> (64 - size))
1c79356b
A
39
40#define super OSObject
41
42OSDefineMetaClassAndStructors(OSNumber, OSObject)
9bccf70c 43
1c79356b
A
44OSMetaClassDefineReservedUnused(OSNumber, 0);
45OSMetaClassDefineReservedUnused(OSNumber, 1);
46OSMetaClassDefineReservedUnused(OSNumber, 2);
47OSMetaClassDefineReservedUnused(OSNumber, 3);
48OSMetaClassDefineReservedUnused(OSNumber, 4);
49OSMetaClassDefineReservedUnused(OSNumber, 5);
50OSMetaClassDefineReservedUnused(OSNumber, 6);
51OSMetaClassDefineReservedUnused(OSNumber, 7);
52
53bool 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
64bool 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
80void OSNumber::free() { super::free(); }
81
82OSNumber *OSNumber::withNumber(unsigned long long value,
83 unsigned int numberOfBits)
84{
85 OSNumber *me = new OSNumber;
86
87 if (me && !me->init(value, numberOfBits)) {
de355530 88 me->free();
1c79356b
A
89 return 0;
90 }
91
92 return me;
93}
94
95OSNumber *OSNumber::withNumber(const char *value, unsigned int numberOfBits)
96{
97 OSNumber *me = new OSNumber;
98
99 if (me && !me->init(value, numberOfBits)) {
de355530 100 me->free();
1c79356b
A
101 return 0;
102 }
103
104 return me;
105}
106
107unsigned int OSNumber::numberOfBits() const { return size; }
108
109unsigned int OSNumber::numberOfBytes() const { return (size + 7) / 8; }
110
111
112unsigned char OSNumber::unsigned8BitValue() const
113{
114 return (unsigned char) value;
115}
116
117unsigned short OSNumber::unsigned16BitValue() const
118{
119 return (unsigned short) value;
120}
121
122unsigned int OSNumber::unsigned32BitValue() const
123{
124 return (unsigned int) value;
125}
126
127unsigned long long OSNumber::unsigned64BitValue() const
128{
129 return value;
130}
131
132void OSNumber::addValue(signed long long inValue)
133{
134 value = ((value + inValue) & sizeMask);
135}
136
137void OSNumber::setValue(unsigned long long inValue)
138{
139 value = (inValue & sizeMask);
140}
141
142bool OSNumber::isEqualTo(const OSNumber *integer) const
143{
144 return((value == integer->value));
145}
146
147bool 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
156bool 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}