]>
git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSNumber.cpp
e327230f9a584d25432adf899a6b0c5a9e3fde5e
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
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
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.
23 * @APPLE_LICENSE_HEADER_END@
25 /* IOOffset.m created by rsulack on Wed 17-Sep-1997 */
27 #include <sys/cdefs.h>
30 extern int sscanf(const char *input
, const char *fmt
, ...);
33 #include <libkern/c++/OSNumber.h>
34 #include <libkern/c++/OSString.h>
35 #include <libkern/c++/OSSerialize.h>
36 #include <libkern/c++/OSLib.h>
38 #define sizeMask (~0ULL >> (64 - size))
40 #define super OSObject
42 OSDefineMetaClassAndStructors(OSNumber
, OSObject
)
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);
53 bool OSNumber::init(unsigned long long inValue
, unsigned int numberOfBits
)
59 value
= (inValue
& sizeMask
);
64 bool OSNumber::init(const char *value
, unsigned int numberOfBits
)
66 unsigned long long thisOffset
;
69 sscanf(value
, "%qi", thisOffset
);
71 unsigned int smallOffset
;
73 sscanf(value
, "%i", &smallOffset
);
74 thisOffset
= smallOffset
;
77 return init(thisOffset
, numberOfBits
);
80 void OSNumber::free() { super::free(); }
82 OSNumber
*OSNumber::withNumber(unsigned long long value
,
83 unsigned int numberOfBits
)
85 OSNumber
*me
= new OSNumber
;
87 if (me
&& !me
->init(value
, numberOfBits
)) {
95 OSNumber
*OSNumber::withNumber(const char *value
, unsigned int numberOfBits
)
97 OSNumber
*me
= new OSNumber
;
99 if (me
&& !me
->init(value
, numberOfBits
)) {
107 unsigned int OSNumber::numberOfBits() const { return size
; }
109 unsigned int OSNumber::numberOfBytes() const { return (size
+ 7) / 8; }
112 unsigned char OSNumber::unsigned8BitValue() const
114 return (unsigned char) value
;
117 unsigned short OSNumber::unsigned16BitValue() const
119 return (unsigned short) value
;
122 unsigned int OSNumber::unsigned32BitValue() const
124 return (unsigned int) value
;
127 unsigned long long OSNumber::unsigned64BitValue() const
132 void OSNumber::addValue(signed long long inValue
)
134 value
= ((value
+ inValue
) & sizeMask
);
137 void OSNumber::setValue(unsigned long long inValue
)
139 value
= (inValue
& sizeMask
);
142 bool OSNumber::isEqualTo(const OSNumber
*integer
) const
144 return((value
== integer
->value
));
147 bool OSNumber::isEqualTo(const OSMetaClassBase
*obj
) const
150 if ((offset
= OSDynamicCast(OSNumber
, obj
)))
151 return isEqualTo(offset
);
156 bool OSNumber::serialize(OSSerialize
*s
) const
160 if (s
->previouslySerialized(this)) return true;
162 sprintf(temp
, "integer size=\"%d\"", size
);
163 if (!s
->addXMLStartTag(this, temp
)) return false;
165 //XXX sprintf(temp, "0x%qx", value);
167 sprintf(temp
, "0x%lx%08lx", (unsigned long)(value
>> 32),
168 (unsigned long)(value
& 0xFFFFFFFF));
170 sprintf(temp
, "0x%lx", (unsigned long)value
);
172 if (!s
->addString(temp
)) return false;
174 return s
->addXMLEndTag("integer");