]>
Commit | Line | Data |
---|---|---|
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 | |
27 | extern 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 | ||
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 | { | |
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 | ||
77 | void OSNumber::free() { super::free(); } | |
78 | ||
79 | OSNumber *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 | ||
92 | OSNumber *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 | ||
104 | unsigned int OSNumber::numberOfBits() const { return size; } | |
105 | ||
106 | unsigned int OSNumber::numberOfBytes() const { return (size + 7) / 8; } | |
107 | ||
108 | ||
109 | unsigned char OSNumber::unsigned8BitValue() const | |
110 | { | |
111 | return (unsigned char) value; | |
112 | } | |
113 | ||
114 | unsigned short OSNumber::unsigned16BitValue() const | |
115 | { | |
116 | return (unsigned short) value; | |
117 | } | |
118 | ||
119 | unsigned int OSNumber::unsigned32BitValue() const | |
120 | { | |
121 | return (unsigned int) value; | |
122 | } | |
123 | ||
124 | unsigned long long OSNumber::unsigned64BitValue() const | |
125 | { | |
126 | return value; | |
127 | } | |
128 | ||
129 | void OSNumber::addValue(signed long long inValue) | |
130 | { | |
131 | value = ((value + inValue) & sizeMask); | |
132 | } | |
133 | ||
134 | void OSNumber::setValue(unsigned long long inValue) | |
135 | { | |
136 | value = (inValue & sizeMask); | |
137 | } | |
138 | ||
139 | bool OSNumber::isEqualTo(const OSNumber *integer) const | |
140 | { | |
141 | return((value == integer->value)); | |
142 | } | |
143 | ||
144 | bool 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 | ||
153 | bool 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 | } |