1 /*
2  * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
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 License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 #include <libkern/OSBase.h>
29 
30 __BEGIN_DECLS
31 #include <mach/mach_types.h>
32 #include <mach/vm_types.h>
33 #include <mach/kmod.h>
34 
35 kmod_start_func_t test2_start;
36 kmod_stop_func_t test2_stop;
37 __END_DECLS
38 
39 #include <libkern/c++/OSContainers.h>
40 #include <iokit/IOLib.h>
41 
42 const char *testBuffer =
43     " <?xml version=\"1.0\" encoding=\"UTF-8\"?> \n"
44     " <!DOCTYPE plist SYSTEM \"file://localhost/System/Library/DTDs/PropertyList.dtd\"> \n"
45     " <plist version=\"1.0\"> \n"
46     " <!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" \"http://www.apple.com/DTDs/PropertyList-1.0.dtd\"> \n"
47     " <plist version=\"0.9\"> \n"
48     " <!-- this is a comment --> \n"
49     " <!-- this is a comment with embedded XML statements \n"
50     " <key>ignore me</key>	<true/> \n"
51     " --> \n"
52 
53     " <dict> \n"
54 
55     " <key>key true</key>	<true/> \n"
56     " <key>key false</key>	<false/> \n"
57 
58     " <key>key d0</key>	<data> </data> \n"
59     " <key>key d1</key>	<data>AQ==</data> \n"
60     " <key>key d2</key>	<data>ASM=</data> \n"
61     " <key>key d3</key>	<data>ASNF</data> \n"
62     " <key>key d4</key>	<data ID=\"1\">ASNFZw==</data> \n"
63 
64     " <key>key i0</key>	<integer></integer> \n"
65     " <key>key i1</key>	<integer>123456789</integer> \n"
66     " <key>key i2</key>	<integer>-123456789</integer> \n"
67     " <key>key i3</key>	<integer size=\"32\" ID=\"2\">0x12345678</integer> \n"
68 
69     " <key>key s0</key>	<string></string> \n"
70     " <key>key s1</key>	<string>string 1</string> \n"
71     " <key>key s2</key>	<string ID=\"3\">string 2</string> \n"
72     " <key>key mr �</key>	<string>mac roman copyright �</string> \n"
73     " <key>key uft8 \xc2\xa9</key>	<string>utf-8 copyright \xc2\xa9</string> \n"
74     " <key>key &lt;&amp;&gt;</key>	<string>&lt;&amp;&gt;</string> \n"
75 
76     " <key>key D0</key>	<dict ID=\"4\"> \n"
77     "                        </dict> \n"
78 
79     " <key>key a0</key>	<array> \n"
80     "                        </array> \n"
81 
82     " <key>key a1</key>	<array ID=\"5\"> \n"
83     "                            <string>array string 1</string> \n"
84     "                            <string>array string 2</string> \n"
85     "                        </array> \n"
86 
87     " <key>key r1</key>	<ref IDREF=\"1\"/> \n"
88     " <key>key r2</key>	<ref IDREF=\"2\"/> \n"
89     " <key>key r3</key>	<ref IDREF=\"3\"/> \n"
90     " <key>key r4</key>	<ref IDREF=\"4\"/> \n"
91     " <key>key r5</key>	<ref IDREF=\"5\"/> \n"
92 
93     " <key>key e1</key>	<array/> \n"
94     " <key>key e2</key>	<dict/> \n"
95     " <key>key e4</key>	<integer/> \n"
96     " <key>key e5</key>	<string/> \n"
97     " <key>key e6</key>	<data/> \n"
98 
99     " <key>key S0</key>	<set> \n"
100     "                        </set> \n"
101     " <key>key S1</key>	<set ID=\"6\"> \n"
102     "                             <string>set string 1</string> \n"
103     "                             <string>set string 2</string> \n"
104     "                         </set> \n"
105     " <key>key r6</key>	<ref IDREF=\"6\"/> \n"
106     " <key>key e3</key>	<set/> \n"
107 
108     " </dict> \n"
109     " </plist> \n"
110 ;
111 
112 /*
113  *  this causes the parser to return an empty string? it doesn't look like yyerror gets called
114  *  char *testBuffer = "<array ID=1><array IDREF=\"1\"/></array>"
115  *
116  */
117 
118 kern_return_t
test2_start(struct kmod_info * ki,void * data)119 test2_start(struct kmod_info *ki, void *data)
120 {
121 	IOLog("test buffer start:\n%s\n:test buffer end.\n", testBuffer);
122 
123 	// test unserialize
124 	OSString *errmsg = 0;
125 	OSObject *d = OSUnserializeXML(testBuffer, &errmsg);
126 	if (!d) {
127 		if (errmsg) {
128 			IOLog("%s\n", errmsg->getCStringNoCopy());
129 		} else {
130 			IOLog("bogus error message\n");
131 		}
132 
133 		return KMOD_RETURN_SUCCESS;
134 	}
135 
136 	// test serialize
137 	OSSerialize *s = OSSerialize::withCapacity(5);
138 	if (!d->serialize(s)) {
139 		IOLog("serialization failed\n");
140 		return KMOD_RETURN_SUCCESS;
141 	}
142 
143 	IOLog("serialized object's length = %d, capacity = %d\n", s->getLength(), s->getCapacity());
144 	IOLog("object unformatted = %s\n", s->text());
145 
146 	// try second time
147 	OSObject *d2 = OSUnserializeXML(s->text(), &errmsg);
148 	if (!d2) {
149 		IOLog("%s\n", errmsg->getCStringNoCopy());
150 		return KMOD_RETURN_SUCCESS;
151 	}
152 	OSSerialize *s2 = OSSerialize::withCapacity(5);
153 	if (!d2->serialize(s2)) {
154 		IOLog("serialization #2 failed\n");
155 		return KMOD_RETURN_SUCCESS;
156 	}
157 
158 	IOLog("serialized object's length = %d, capacity = %d\n",
159 	    s2->getLength(), s2->getCapacity());
160 	IOLog("object unformatted = %s\n", s2->text());
161 
162 	IOLog("\nserialized objects compared %ssuccessfully textually\n\n",
163 	    strcmp(s->text(), s2->text()) ? "un":"");
164 
165 	IOLog("\nserialized objects compared %ssuccessfully objectwise\n\n",
166 	    d->isEqualTo(d2) ? "":"un");
167 
168 	s2->release();
169 	if (d2) {
170 		d2->release();
171 	}
172 	s->release();
173 	if (d) {
174 		d->release();
175 	}
176 
177 	return KMOD_RETURN_SUCCESS;
178 }
179 
180 kern_return_t
test2_stop(struct kmod_info * ki,void * data)181 test2_stop(struct kmod_info *ki, void *data)
182 {
183 	return KMOD_RETURN_SUCCESS;
184 }
185