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