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