xref: /xnu-8796.141.3/libkern/c++/OSString.cpp (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions  * Copyright (c) 2019 Apple Inc. All rights reserved.
3*1b191cb5SApple OSS Distributions  *
4*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1b191cb5SApple OSS Distributions  *
6*1b191cb5SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*1b191cb5SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*1b191cb5SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*1b191cb5SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*1b191cb5SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*1b191cb5SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*1b191cb5SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*1b191cb5SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*1b191cb5SApple OSS Distributions  *
15*1b191cb5SApple OSS Distributions  * Please obtain a copy of the License at
16*1b191cb5SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1b191cb5SApple OSS Distributions  *
18*1b191cb5SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*1b191cb5SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1b191cb5SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1b191cb5SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1b191cb5SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1b191cb5SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*1b191cb5SApple OSS Distributions  * limitations under the License.
25*1b191cb5SApple OSS Distributions  *
26*1b191cb5SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1b191cb5SApple OSS Distributions  */
28*1b191cb5SApple OSS Distributions /* IOString.m created by rsulack on Wed 17-Sep-1997 */
29*1b191cb5SApple OSS Distributions /* IOString.cpp converted to C++ on Tue 1998-9-22 */
30*1b191cb5SApple OSS Distributions 
31*1b191cb5SApple OSS Distributions #define IOKIT_ENABLE_SHARED_PTR
32*1b191cb5SApple OSS Distributions 
33*1b191cb5SApple OSS Distributions #include <string.h>
34*1b191cb5SApple OSS Distributions 
35*1b191cb5SApple OSS Distributions #include <libkern/c++/OSString.h>
36*1b191cb5SApple OSS Distributions #include <libkern/c++/OSSerialize.h>
37*1b191cb5SApple OSS Distributions #include <libkern/c++/OSSharedPtr.h>
38*1b191cb5SApple OSS Distributions #include <libkern/c++/OSLib.h>
39*1b191cb5SApple OSS Distributions #include <libkern/c++/OSData.h>
40*1b191cb5SApple OSS Distributions #include <string.h>
41*1b191cb5SApple OSS Distributions 
42*1b191cb5SApple OSS Distributions #define super OSObject
43*1b191cb5SApple OSS Distributions 
44*1b191cb5SApple OSS Distributions OSDefineMetaClassAndStructorsWithZone(OSString, OSObject,
45*1b191cb5SApple OSS Distributions     (zone_create_flags_t) (ZC_CACHING | ZC_ZFREE_CLEARMEM))
46*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 0);
47*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 1);
48*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 2);
49*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 3);
50*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 4);
51*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 5);
52*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 6);
53*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 7);
54*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 8);
55*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 9);
56*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 10);
57*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 11);
58*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 12);
59*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 13);
60*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 14);
61*1b191cb5SApple OSS Distributions OSMetaClassDefineReservedUnused(OSString, 15);
62*1b191cb5SApple OSS Distributions 
63*1b191cb5SApple OSS Distributions bool
initWithString(const OSString * aString)64*1b191cb5SApple OSS Distributions OSString::initWithString(const OSString *aString)
65*1b191cb5SApple OSS Distributions {
66*1b191cb5SApple OSS Distributions 	return initWithCString(aString->string);
67*1b191cb5SApple OSS Distributions }
68*1b191cb5SApple OSS Distributions 
69*1b191cb5SApple OSS Distributions bool
initWithCString(const char * cString)70*1b191cb5SApple OSS Distributions OSString::initWithCString(const char *cString)
71*1b191cb5SApple OSS Distributions {
72*1b191cb5SApple OSS Distributions 	unsigned int   newLength;
73*1b191cb5SApple OSS Distributions 	char         * newString;
74*1b191cb5SApple OSS Distributions 
75*1b191cb5SApple OSS Distributions 	if (!cString || !super::init()) {
76*1b191cb5SApple OSS Distributions 		return false;
77*1b191cb5SApple OSS Distributions 	}
78*1b191cb5SApple OSS Distributions 
79*1b191cb5SApple OSS Distributions 	newLength = (unsigned int) strnlen(cString, kMaxStringLength);
80*1b191cb5SApple OSS Distributions 	if (newLength >= kMaxStringLength) {
81*1b191cb5SApple OSS Distributions 		return false;
82*1b191cb5SApple OSS Distributions 	}
83*1b191cb5SApple OSS Distributions 
84*1b191cb5SApple OSS Distributions 	newLength++;
85*1b191cb5SApple OSS Distributions 	newString = (char *)kalloc_data(newLength,
86*1b191cb5SApple OSS Distributions 	    Z_VM_TAG_BT(Z_WAITOK, VM_KERN_MEMORY_LIBKERN));
87*1b191cb5SApple OSS Distributions 	if (!newString) {
88*1b191cb5SApple OSS Distributions 		return false;
89*1b191cb5SApple OSS Distributions 	}
90*1b191cb5SApple OSS Distributions 
91*1b191cb5SApple OSS Distributions 	bcopy(cString, newString, newLength);
92*1b191cb5SApple OSS Distributions 
93*1b191cb5SApple OSS Distributions 	if (!(flags & kOSStringNoCopy) && string) {
94*1b191cb5SApple OSS Distributions 		kfree_data(string, length);
95*1b191cb5SApple OSS Distributions 		OSCONTAINER_ACCUMSIZE(-((size_t)length));
96*1b191cb5SApple OSS Distributions 	}
97*1b191cb5SApple OSS Distributions 	string = newString;
98*1b191cb5SApple OSS Distributions 	length = newLength;
99*1b191cb5SApple OSS Distributions 	flags &= ~kOSStringNoCopy;
100*1b191cb5SApple OSS Distributions 
101*1b191cb5SApple OSS Distributions 	OSCONTAINER_ACCUMSIZE(length);
102*1b191cb5SApple OSS Distributions 
103*1b191cb5SApple OSS Distributions 	return true;
104*1b191cb5SApple OSS Distributions }
105*1b191cb5SApple OSS Distributions 
106*1b191cb5SApple OSS Distributions bool
initWithStringOfLength(const char * cString,size_t inlength)107*1b191cb5SApple OSS Distributions OSString::initWithStringOfLength(const char *cString, size_t inlength)
108*1b191cb5SApple OSS Distributions {
109*1b191cb5SApple OSS Distributions 	unsigned int   newLength;
110*1b191cb5SApple OSS Distributions 	unsigned int   cStringLength;
111*1b191cb5SApple OSS Distributions 	char         * newString;
112*1b191cb5SApple OSS Distributions 
113*1b191cb5SApple OSS Distributions 	if (!cString || !super::init()) {
114*1b191cb5SApple OSS Distributions 		return false;
115*1b191cb5SApple OSS Distributions 	}
116*1b191cb5SApple OSS Distributions 
117*1b191cb5SApple OSS Distributions 	if (inlength >= kMaxStringLength) {
118*1b191cb5SApple OSS Distributions 		return false;
119*1b191cb5SApple OSS Distributions 	}
120*1b191cb5SApple OSS Distributions 
121*1b191cb5SApple OSS Distributions 	cStringLength = (unsigned int)strnlen(cString, inlength);
122*1b191cb5SApple OSS Distributions 
123*1b191cb5SApple OSS Distributions 	if (cStringLength < inlength) {
124*1b191cb5SApple OSS Distributions 		inlength = cStringLength;
125*1b191cb5SApple OSS Distributions 	}
126*1b191cb5SApple OSS Distributions 
127*1b191cb5SApple OSS Distributions 	newLength = (unsigned int) (inlength + 1);
128*1b191cb5SApple OSS Distributions 	newString = (char *)kalloc_data(newLength,
129*1b191cb5SApple OSS Distributions 	    Z_VM_TAG_BT(Z_WAITOK, VM_KERN_MEMORY_LIBKERN));
130*1b191cb5SApple OSS Distributions 	if (!newString) {
131*1b191cb5SApple OSS Distributions 		return false;
132*1b191cb5SApple OSS Distributions 	}
133*1b191cb5SApple OSS Distributions 
134*1b191cb5SApple OSS Distributions 	bcopy(cString, newString, inlength);
135*1b191cb5SApple OSS Distributions 	newString[inlength] = 0;
136*1b191cb5SApple OSS Distributions 
137*1b191cb5SApple OSS Distributions 	if (!(flags & kOSStringNoCopy) && string) {
138*1b191cb5SApple OSS Distributions 		kfree_data(string, length);
139*1b191cb5SApple OSS Distributions 		OSCONTAINER_ACCUMSIZE(-((size_t)length));
140*1b191cb5SApple OSS Distributions 	}
141*1b191cb5SApple OSS Distributions 
142*1b191cb5SApple OSS Distributions 	string = newString;
143*1b191cb5SApple OSS Distributions 	length = newLength;
144*1b191cb5SApple OSS Distributions 	flags &= ~kOSStringNoCopy;
145*1b191cb5SApple OSS Distributions 
146*1b191cb5SApple OSS Distributions 	OSCONTAINER_ACCUMSIZE(length);
147*1b191cb5SApple OSS Distributions 
148*1b191cb5SApple OSS Distributions 	return true;
149*1b191cb5SApple OSS Distributions }
150*1b191cb5SApple OSS Distributions 
151*1b191cb5SApple OSS Distributions bool
initWithCStringNoCopy(const char * cString)152*1b191cb5SApple OSS Distributions OSString::initWithCStringNoCopy(const char *cString)
153*1b191cb5SApple OSS Distributions {
154*1b191cb5SApple OSS Distributions 	if (!cString || !super::init()) {
155*1b191cb5SApple OSS Distributions 		return false;
156*1b191cb5SApple OSS Distributions 	}
157*1b191cb5SApple OSS Distributions 
158*1b191cb5SApple OSS Distributions 	length = (unsigned int) strnlen(cString, kMaxStringLength);
159*1b191cb5SApple OSS Distributions 	if (length >= kMaxStringLength) {
160*1b191cb5SApple OSS Distributions 		return false;
161*1b191cb5SApple OSS Distributions 	}
162*1b191cb5SApple OSS Distributions 
163*1b191cb5SApple OSS Distributions 	length++;
164*1b191cb5SApple OSS Distributions 	flags |= kOSStringNoCopy;
165*1b191cb5SApple OSS Distributions 	string = const_cast<char *>(cString);
166*1b191cb5SApple OSS Distributions 
167*1b191cb5SApple OSS Distributions 	return true;
168*1b191cb5SApple OSS Distributions }
169*1b191cb5SApple OSS Distributions 
170*1b191cb5SApple OSS Distributions OSSharedPtr<OSString>
withString(const OSString * aString)171*1b191cb5SApple OSS Distributions OSString::withString(const OSString *aString)
172*1b191cb5SApple OSS Distributions {
173*1b191cb5SApple OSS Distributions 	OSSharedPtr<OSString> me = OSMakeShared<OSString>();
174*1b191cb5SApple OSS Distributions 
175*1b191cb5SApple OSS Distributions 	if (me && !me->initWithString(aString)) {
176*1b191cb5SApple OSS Distributions 		return nullptr;
177*1b191cb5SApple OSS Distributions 	}
178*1b191cb5SApple OSS Distributions 
179*1b191cb5SApple OSS Distributions 	return me;
180*1b191cb5SApple OSS Distributions }
181*1b191cb5SApple OSS Distributions 
182*1b191cb5SApple OSS Distributions OSSharedPtr<OSString>
withCString(const char * cString)183*1b191cb5SApple OSS Distributions OSString::withCString(const char *cString)
184*1b191cb5SApple OSS Distributions {
185*1b191cb5SApple OSS Distributions 	OSSharedPtr<OSString> me = OSMakeShared<OSString>();
186*1b191cb5SApple OSS Distributions 
187*1b191cb5SApple OSS Distributions 	if (me && !me->initWithCString(cString)) {
188*1b191cb5SApple OSS Distributions 		return nullptr;
189*1b191cb5SApple OSS Distributions 	}
190*1b191cb5SApple OSS Distributions 
191*1b191cb5SApple OSS Distributions 	return me;
192*1b191cb5SApple OSS Distributions }
193*1b191cb5SApple OSS Distributions 
194*1b191cb5SApple OSS Distributions OSSharedPtr<OSString>
withCStringNoCopy(const char * cString)195*1b191cb5SApple OSS Distributions OSString::withCStringNoCopy(const char *cString)
196*1b191cb5SApple OSS Distributions {
197*1b191cb5SApple OSS Distributions 	OSSharedPtr<OSString> me = OSMakeShared<OSString>();
198*1b191cb5SApple OSS Distributions 
199*1b191cb5SApple OSS Distributions 	if (me && !me->initWithCStringNoCopy(cString)) {
200*1b191cb5SApple OSS Distributions 		return nullptr;
201*1b191cb5SApple OSS Distributions 	}
202*1b191cb5SApple OSS Distributions 
203*1b191cb5SApple OSS Distributions 	return me;
204*1b191cb5SApple OSS Distributions }
205*1b191cb5SApple OSS Distributions 
206*1b191cb5SApple OSS Distributions OSSharedPtr<OSString>
withCString(const char * cString,size_t length)207*1b191cb5SApple OSS Distributions OSString::withCString(const char *cString, size_t length)
208*1b191cb5SApple OSS Distributions {
209*1b191cb5SApple OSS Distributions 	OSSharedPtr<OSString> me = OSMakeShared<OSString>();
210*1b191cb5SApple OSS Distributions 
211*1b191cb5SApple OSS Distributions 	if (me && !me->initWithStringOfLength(cString, length)) {
212*1b191cb5SApple OSS Distributions 		return nullptr;
213*1b191cb5SApple OSS Distributions 	}
214*1b191cb5SApple OSS Distributions 
215*1b191cb5SApple OSS Distributions 	return me;
216*1b191cb5SApple OSS Distributions }
217*1b191cb5SApple OSS Distributions 
218*1b191cb5SApple OSS Distributions 
219*1b191cb5SApple OSS Distributions 
220*1b191cb5SApple OSS Distributions /* @@@ gvdl */
221*1b191cb5SApple OSS Distributions #if 0
222*1b191cb5SApple OSS Distributions OSString *
223*1b191cb5SApple OSS Distributions OSString::stringWithFormat(const char *format, ...)
224*1b191cb5SApple OSS Distributions {
225*1b191cb5SApple OSS Distributions #ifndef KERNEL                  // mach3xxx
226*1b191cb5SApple OSS Distributions 	OSString *me;
227*1b191cb5SApple OSS Distributions 	va_list argList;
228*1b191cb5SApple OSS Distributions 
229*1b191cb5SApple OSS Distributions 	if (!format) {
230*1b191cb5SApple OSS Distributions 		return 0;
231*1b191cb5SApple OSS Distributions 	}
232*1b191cb5SApple OSS Distributions 
233*1b191cb5SApple OSS Distributions 	va_start(argList, format);
234*1b191cb5SApple OSS Distributions 	me = stringWithCapacity(256);
235*1b191cb5SApple OSS Distributions 	me->length = vsnprintf(me->string, 256, format, argList);
236*1b191cb5SApple OSS Distributions 	me->length++;   // we include the null in the length
237*1b191cb5SApple OSS Distributions 	if (me->Length > 256) {
238*1b191cb5SApple OSS Distributions 		me->Length = 256;
239*1b191cb5SApple OSS Distributions 	}
240*1b191cb5SApple OSS Distributions 	va_end(argList);
241*1b191cb5SApple OSS Distributions 
242*1b191cb5SApple OSS Distributions 	return me;
243*1b191cb5SApple OSS Distributions #else
244*1b191cb5SApple OSS Distributions 	return 0;
245*1b191cb5SApple OSS Distributions #endif
246*1b191cb5SApple OSS Distributions }
247*1b191cb5SApple OSS Distributions #endif /* 0 */
248*1b191cb5SApple OSS Distributions 
249*1b191cb5SApple OSS Distributions void
free()250*1b191cb5SApple OSS Distributions OSString::free()
251*1b191cb5SApple OSS Distributions {
252*1b191cb5SApple OSS Distributions 	if (!(flags & kOSStringNoCopy) && string) {
253*1b191cb5SApple OSS Distributions 		kfree_data(string, length);
254*1b191cb5SApple OSS Distributions 		OSCONTAINER_ACCUMSIZE(-((size_t)length));
255*1b191cb5SApple OSS Distributions 	}
256*1b191cb5SApple OSS Distributions 
257*1b191cb5SApple OSS Distributions 	super::free();
258*1b191cb5SApple OSS Distributions }
259*1b191cb5SApple OSS Distributions 
260*1b191cb5SApple OSS Distributions unsigned int
getLength() const261*1b191cb5SApple OSS Distributions OSString::getLength()  const
262*1b191cb5SApple OSS Distributions {
263*1b191cb5SApple OSS Distributions 	return length - 1;
264*1b191cb5SApple OSS Distributions }
265*1b191cb5SApple OSS Distributions 
266*1b191cb5SApple OSS Distributions const char *
getCStringNoCopy() const267*1b191cb5SApple OSS Distributions OSString::getCStringNoCopy() const
268*1b191cb5SApple OSS Distributions {
269*1b191cb5SApple OSS Distributions 	return string;
270*1b191cb5SApple OSS Distributions }
271*1b191cb5SApple OSS Distributions 
272*1b191cb5SApple OSS Distributions bool
setChar(char aChar,unsigned int index)273*1b191cb5SApple OSS Distributions OSString::setChar(char aChar, unsigned int index)
274*1b191cb5SApple OSS Distributions {
275*1b191cb5SApple OSS Distributions 	if (!(flags & kOSStringNoCopy) && index < length - 1) {
276*1b191cb5SApple OSS Distributions 		string[index] = aChar;
277*1b191cb5SApple OSS Distributions 
278*1b191cb5SApple OSS Distributions 		return true;
279*1b191cb5SApple OSS Distributions 	} else {
280*1b191cb5SApple OSS Distributions 		return false;
281*1b191cb5SApple OSS Distributions 	}
282*1b191cb5SApple OSS Distributions }
283*1b191cb5SApple OSS Distributions 
284*1b191cb5SApple OSS Distributions char
getChar(unsigned int index) const285*1b191cb5SApple OSS Distributions OSString::getChar(unsigned int index) const
286*1b191cb5SApple OSS Distributions {
287*1b191cb5SApple OSS Distributions 	if (index < length) {
288*1b191cb5SApple OSS Distributions 		return string[index];
289*1b191cb5SApple OSS Distributions 	} else {
290*1b191cb5SApple OSS Distributions 		return '\0';
291*1b191cb5SApple OSS Distributions 	}
292*1b191cb5SApple OSS Distributions }
293*1b191cb5SApple OSS Distributions 
294*1b191cb5SApple OSS Distributions 
295*1b191cb5SApple OSS Distributions bool
isEqualTo(const OSString * aString) const296*1b191cb5SApple OSS Distributions OSString::isEqualTo(const OSString *aString) const
297*1b191cb5SApple OSS Distributions {
298*1b191cb5SApple OSS Distributions 	if (length != aString->length) {
299*1b191cb5SApple OSS Distributions 		return false;
300*1b191cb5SApple OSS Distributions 	} else {
301*1b191cb5SApple OSS Distributions 		return isEqualTo((const char *) aString->string);
302*1b191cb5SApple OSS Distributions 	}
303*1b191cb5SApple OSS Distributions }
304*1b191cb5SApple OSS Distributions 
305*1b191cb5SApple OSS Distributions bool
isEqualTo(const char * aCString) const306*1b191cb5SApple OSS Distributions OSString::isEqualTo(const char *aCString) const
307*1b191cb5SApple OSS Distributions {
308*1b191cb5SApple OSS Distributions 	return strncmp(string, aCString, length) == 0;
309*1b191cb5SApple OSS Distributions }
310*1b191cb5SApple OSS Distributions 
311*1b191cb5SApple OSS Distributions bool
isEqualTo(const OSMetaClassBase * obj) const312*1b191cb5SApple OSS Distributions OSString::isEqualTo(const OSMetaClassBase *obj) const
313*1b191cb5SApple OSS Distributions {
314*1b191cb5SApple OSS Distributions 	OSString *  str;
315*1b191cb5SApple OSS Distributions 	OSData *    data;
316*1b191cb5SApple OSS Distributions 
317*1b191cb5SApple OSS Distributions 	if ((str = OSDynamicCast(OSString, obj))) {
318*1b191cb5SApple OSS Distributions 		return isEqualTo(str);
319*1b191cb5SApple OSS Distributions 	} else if ((data = OSDynamicCast(OSData, obj))) {
320*1b191cb5SApple OSS Distributions 		return isEqualTo(data);
321*1b191cb5SApple OSS Distributions 	} else {
322*1b191cb5SApple OSS Distributions 		return false;
323*1b191cb5SApple OSS Distributions 	}
324*1b191cb5SApple OSS Distributions }
325*1b191cb5SApple OSS Distributions 
326*1b191cb5SApple OSS Distributions bool
isEqualTo(const OSData * obj) const327*1b191cb5SApple OSS Distributions OSString::isEqualTo(const OSData *obj) const
328*1b191cb5SApple OSS Distributions {
329*1b191cb5SApple OSS Distributions 	if (NULL == obj) {
330*1b191cb5SApple OSS Distributions 		return false;
331*1b191cb5SApple OSS Distributions 	}
332*1b191cb5SApple OSS Distributions 
333*1b191cb5SApple OSS Distributions 	unsigned int dataLen = obj->getLength();
334*1b191cb5SApple OSS Distributions 	const char * dataPtr = (const char *) obj->getBytesNoCopy();
335*1b191cb5SApple OSS Distributions 
336*1b191cb5SApple OSS Distributions 	if (dataLen != length) {
337*1b191cb5SApple OSS Distributions 		// check for the fact that OSData may be a buffer that
338*1b191cb5SApple OSS Distributions 		// that includes a termination byte and will thus have
339*1b191cb5SApple OSS Distributions 		// a length of the actual string length PLUS 1. In this
340*1b191cb5SApple OSS Distributions 		// case we verify that the additional byte is a terminator
341*1b191cb5SApple OSS Distributions 		// and if so count the two lengths as being the same.
342*1b191cb5SApple OSS Distributions 
343*1b191cb5SApple OSS Distributions 		if ((dataLen - length) == 1) {
344*1b191cb5SApple OSS Distributions 			if (dataPtr[dataLen - 1] != 0) {
345*1b191cb5SApple OSS Distributions 				return false;
346*1b191cb5SApple OSS Distributions 			}
347*1b191cb5SApple OSS Distributions 			dataLen--;
348*1b191cb5SApple OSS Distributions 		} else {
349*1b191cb5SApple OSS Distributions 			return false;
350*1b191cb5SApple OSS Distributions 		}
351*1b191cb5SApple OSS Distributions 	}
352*1b191cb5SApple OSS Distributions 
353*1b191cb5SApple OSS Distributions 	for (unsigned int i = 0; i < dataLen; i++) {
354*1b191cb5SApple OSS Distributions 		if (*dataPtr++ != string[i]) {
355*1b191cb5SApple OSS Distributions 			return false;
356*1b191cb5SApple OSS Distributions 		}
357*1b191cb5SApple OSS Distributions 	}
358*1b191cb5SApple OSS Distributions 
359*1b191cb5SApple OSS Distributions 	return true;
360*1b191cb5SApple OSS Distributions }
361*1b191cb5SApple OSS Distributions 
362*1b191cb5SApple OSS Distributions bool
serialize(OSSerialize * s) const363*1b191cb5SApple OSS Distributions OSString::serialize(OSSerialize *s) const
364*1b191cb5SApple OSS Distributions {
365*1b191cb5SApple OSS Distributions 	char *c = string;
366*1b191cb5SApple OSS Distributions 
367*1b191cb5SApple OSS Distributions 	if (s->previouslySerialized(this)) {
368*1b191cb5SApple OSS Distributions 		return true;
369*1b191cb5SApple OSS Distributions 	}
370*1b191cb5SApple OSS Distributions 
371*1b191cb5SApple OSS Distributions 	if (!s->addXMLStartTag(this, "string")) {
372*1b191cb5SApple OSS Distributions 		return false;
373*1b191cb5SApple OSS Distributions 	}
374*1b191cb5SApple OSS Distributions 	while (*c) {
375*1b191cb5SApple OSS Distributions 		if (*c == '<') {
376*1b191cb5SApple OSS Distributions 			if (!s->addString("&lt;")) {
377*1b191cb5SApple OSS Distributions 				return false;
378*1b191cb5SApple OSS Distributions 			}
379*1b191cb5SApple OSS Distributions 		} else if (*c == '>') {
380*1b191cb5SApple OSS Distributions 			if (!s->addString("&gt;")) {
381*1b191cb5SApple OSS Distributions 				return false;
382*1b191cb5SApple OSS Distributions 			}
383*1b191cb5SApple OSS Distributions 		} else if (*c == '&') {
384*1b191cb5SApple OSS Distributions 			if (!s->addString("&amp;")) {
385*1b191cb5SApple OSS Distributions 				return false;
386*1b191cb5SApple OSS Distributions 			}
387*1b191cb5SApple OSS Distributions 		} else {
388*1b191cb5SApple OSS Distributions 			if (!s->addChar(*c)) {
389*1b191cb5SApple OSS Distributions 				return false;
390*1b191cb5SApple OSS Distributions 			}
391*1b191cb5SApple OSS Distributions 		}
392*1b191cb5SApple OSS Distributions 		c++;
393*1b191cb5SApple OSS Distributions 	}
394*1b191cb5SApple OSS Distributions 
395*1b191cb5SApple OSS Distributions 	return s->addXMLEndTag("string");
396*1b191cb5SApple OSS Distributions }
397