]>
Commit | Line | Data |
---|---|---|
1c79356b A |
1 | /* |
2 | * Copyright (c) 2000 Apple Computer, Inc. All rights reserved. | |
3 | * | |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
e5568f75 A |
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. | |
1c79356b | 11 | * |
e5568f75 A |
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 | |
1c79356b A |
14 | * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, |
15 | * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, | |
e5568f75 A |
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. | |
1c79356b A |
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 | |
91447636 | 27 | extern unsigned long strtoul(const char *, char **, int); |
9bccf70c A |
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 | ||
39 | OSDefineMetaClassAndStructors(OSNumber, OSObject) | |
9bccf70c | 40 | |
1c79356b A |
41 | OSMetaClassDefineReservedUnused(OSNumber, 0); |
42 | OSMetaClassDefineReservedUnused(OSNumber, 1); | |
43 | OSMetaClassDefineReservedUnused(OSNumber, 2); | |
44 | OSMetaClassDefineReservedUnused(OSNumber, 3); | |
45 | OSMetaClassDefineReservedUnused(OSNumber, 4); | |
46 | OSMetaClassDefineReservedUnused(OSNumber, 5); | |
47 | OSMetaClassDefineReservedUnused(OSNumber, 6); | |
48 | OSMetaClassDefineReservedUnused(OSNumber, 7); | |
49 | ||
50 | bool 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 | ||
61 | bool OSNumber::init(const char *value, unsigned int numberOfBits) | |
62 | { | |
91447636 | 63 | return init((unsigned long long)strtoul(value, NULL, 0), numberOfBits); |
1c79356b A |
64 | } |
65 | ||
66 | void OSNumber::free() { super::free(); } | |
67 | ||
68 | OSNumber *OSNumber::withNumber(unsigned long long value, | |
69 | unsigned int numberOfBits) | |
70 | { | |
71 | OSNumber *me = new OSNumber; | |
72 | ||
73 | if (me && !me->init(value, numberOfBits)) { | |
55e303ae | 74 | me->release(); |
1c79356b A |
75 | return 0; |
76 | } | |
77 | ||
78 | return me; | |
79 | } | |
80 | ||
81 | OSNumber *OSNumber::withNumber(const char *value, unsigned int numberOfBits) | |
82 | { | |
83 | OSNumber *me = new OSNumber; | |
84 | ||
85 | if (me && !me->init(value, numberOfBits)) { | |
55e303ae | 86 | me->release(); |
1c79356b A |
87 | return 0; |
88 | } | |
89 | ||
90 | return me; | |
91 | } | |
92 | ||
93 | unsigned int OSNumber::numberOfBits() const { return size; } | |
94 | ||
95 | unsigned int OSNumber::numberOfBytes() const { return (size + 7) / 8; } | |
96 | ||
97 | ||
98 | unsigned char OSNumber::unsigned8BitValue() const | |
99 | { | |
100 | return (unsigned char) value; | |
101 | } | |
102 | ||
103 | unsigned short OSNumber::unsigned16BitValue() const | |
104 | { | |
105 | return (unsigned short) value; | |
106 | } | |
107 | ||
108 | unsigned int OSNumber::unsigned32BitValue() const | |
109 | { | |
110 | return (unsigned int) value; | |
111 | } | |
112 | ||
113 | unsigned long long OSNumber::unsigned64BitValue() const | |
114 | { | |
115 | return value; | |
116 | } | |
117 | ||
118 | void OSNumber::addValue(signed long long inValue) | |
119 | { | |
120 | value = ((value + inValue) & sizeMask); | |
121 | } | |
122 | ||
123 | void OSNumber::setValue(unsigned long long inValue) | |
124 | { | |
125 | value = (inValue & sizeMask); | |
126 | } | |
127 | ||
128 | bool OSNumber::isEqualTo(const OSNumber *integer) const | |
129 | { | |
130 | return((value == integer->value)); | |
131 | } | |
132 | ||
133 | bool OSNumber::isEqualTo(const OSMetaClassBase *obj) const | |
134 | { | |
135 | OSNumber * offset; | |
136 | if ((offset = OSDynamicCast(OSNumber, obj))) | |
137 | return isEqualTo(offset); | |
138 | else | |
139 | return false; | |
140 | } | |
141 | ||
142 | bool OSNumber::serialize(OSSerialize *s) const | |
143 | { | |
144 | char temp[32]; | |
145 | ||
146 | if (s->previouslySerialized(this)) return true; | |
147 | ||
148 | sprintf(temp, "integer size=\"%d\"", size); | |
149 | if (!s->addXMLStartTag(this, temp)) return false; | |
150 | ||
151 | //XXX sprintf(temp, "0x%qx", value); | |
152 | if ((value >> 32)) { | |
153 | sprintf(temp, "0x%lx%08lx", (unsigned long)(value >> 32), | |
154 | (unsigned long)(value & 0xFFFFFFFF)); | |
155 | } else { | |
156 | sprintf(temp, "0x%lx", (unsigned long)value); | |
157 | } | |
158 | if (!s->addString(temp)) return false; | |
159 | ||
160 | return s->addXMLEndTag("integer"); | |
161 | } |