Bug Summary

File:Plugins/Bonjour/libezv/Private Classes/AWEzvXMLStream.m
Location:line 192, column 3
Description:dead store

Annotated Source Code

1/*
2 * Project: Libezv
3 * File: AWEzvXMLStream.m
4 *
5 * Version: 1.0
6 * Author: Andrew Wellington <proton[at]wiretapped.net>
7 *
8 * License:
9 * Copyright (C) 2004-2005 Andrew Wellington.
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions are met:
14 *
15 * 1. Redistributions of source code must retain the above copyright notice,
16 * this list of conditions and the following disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright notice,
18 * this list of conditions and the following disclaimer in the documentation
19 * and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
22 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
24 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33
34#import "AWEzvXMLStream.h"
35#import "AWEzvXMLNode.h"
36#import "AWEzvStack.h"
37
38#import "AWEzvSupportRoutines.h"
39
40#define XMLCALL
41#include <expat.h>
42
43/* XML Function prototypes */
44void xml_start_element (void *userData,
45 const XML_Char *name,
46 const XML_Char **atts);
47void xml_end_element (void *userData,
48 const XML_Char *name);
49void xml_char_data (void *userData,
50 const XML_Char *s,
51 int len);
52
53
54@implementation AWEzvXMLStream
55
56- (id) initWithFileHandle:(NSFileHandle *)myConnection initiator:(int)myInitiator
57{
58 if ((self = [super init])) {
59 connection = [myConnection retain];
60 delegate = nil0;
61 nodeStack = [[AWEzvStack alloc] init];
62 initiator = myInitiator;
63 negotiated = 0;
64 }
65
66 return self;
67}
68
69- (void)dealloc
70{
71 if (connection != nil0) {
72 [connection closeFile];
73 [connection release];
74 }
75
76 [[NSNotificationCenter defaultCenter] removeObserver:self];
77 [nodeStack release];
78
79 [super dealloc];
80}
81
82- (NSFileHandle *)fileHandle {
83 return connection;
84}
85
86- (void) readAndParse {
87 [[NSNotificationCenter defaultCenter] addObserver:self
88 selector:@selector(dataReceived:)
89 name:NSFileHandleReadCompletionNotification
90 object:connection];
91 [[NSNotificationCenter defaultCenter] addObserver:self
92 selector:@selector(dataAvailable:)
93 name:NSFileHandleDataAvailableNotification
94 object:connection];
95
96 parser = XML_ParserCreate(NULL( ( void * ) 0 ));
97 XML_SetUserData(parser, self);
98 XML_SetElementHandler(parser, &xml_start_element, &xml_end_element);
99 XML_SetCharacterDataHandler(parser, &xml_char_data);
100 XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_NEVER);
101
102 if (!negotiated && initiator) {
103 [self sendNegotiationInitiator:initiator];
104 }
105
106 [connection waitForDataInBackgroundAndNotify];
107
108}
109
110- (void) sendData:(NSData *)data {
111 [connection writeData:data];
112}
113
114- (void) sendString:(NSString *)string {
115 [connection writeData:[string dataUsingEncoding:NSUTF8StringEncoding]];
116}
117
118- (void) dataReceived:(NSNotification *)aNotification {
119 NSData *data = [[aNotification userInfo] objectForKey:NSFileHandleNotificationDataItem];
120 int status;
121
122 if ([data length] == 0) {
123 if (connection != nil0)
124 [[aNotification object] autorelease];
125 connection = nil0;
126 [delegate XMLConnectionClosed];
127 }
128
129 status = XML_Parse(parser, [data bytes], [data length], [data length] == 0 ? 1 : 0);
130
131 if (connection != nil0)
132 [[aNotification object] waitForDataInBackgroundAndNotify];
133}
134
135- (void)dataAvailable:(NSNotification *)aNotification {
136 [[aNotification object] readInBackgroundAndNotify];
137}
138
139- (void) closeFileHandle {
140 [connection closeFile];
141 connection = nil0;
142}
143
144- (void) setDelegate:(id)myDelegate {
145 delegate = myDelegate;
146}
147- (id) delegate {
148 return delegate;
149}
150
151- (void) xmlStartElement:(const XML_Char *)name attributes:(const XML_Char **)attributes {
152 AWEzvXMLNode *node;
153 NSString *attribute, *value, *nodeName;
154
155 nodeName = [NSString stringWithUTF8String:name];
156
157 node = [[[AWEzvXMLNode alloc] initWithType:AWEzvXMLElement name:nodeName] autorelease];
158
159 while (*attributes != NULL( ( void * ) 0 )) {
160 attribute = [NSString stringWithUTF8String:*attributes++];
161 value = [NSString stringWithUTF8String:*attributes++];
162 [node addAttribute:attribute withValue:value];
163 }
164
165 if ([nodeStack size] > 0 && [(AWEzvXMLNode *)[nodeStack top] type] == AWEzvXMLText)
166 [nodeStack pop];
167
168 if ([nodeStack size] > 0) {
169 [(AWEzvXMLNode *)[nodeStack top] addChild:node];
170 }
171
172 [nodeStack push:node];
173
174 if (([nodeName isEqualToString:@"stream:stream"]) && !negotiated) {
175 if (initiator) {
176 negotiated = 1;
177 } else {
178 [self sendNegotiationInitiator:0];
179 }
180 node = [nodeStack pop];
181 }
182
183
184}
185
186- (void) xmlEndElement:(const XML_Char *)name {
187 NSString *nodeName;
188 AWEzvXMLNode *node;
189
190 nodeName = [NSString stringWithUTF8String:name];
191 if (([nodeStack size] > 0) && ([(AWEzvXMLNode *)[nodeStack top] type] == AWEzvXMLText)) {
Value stored to 'node' is never read
192 node = [nodeStack pop];
193 }
194 else if ([nodeStack size] == 0 && [nodeName isEqualToString:@"stream:stream"]) {
195 /* We have no stack but were sent stream:stream to end, therefore end connection */
196 [self endConnection];
197 return;
198 }
199 node = [nodeStack top];
200
201 if (node != nil0 && [[node name] isEqualToString:nodeName]) {
202 [nodeStack pop];
203 } else if ([[node name] isEqualToString:@"stream:stream"]) {
204 // Wow, end of connection!
205 [self endConnection];
206 return;
207 } else {
208 AWEzvLog(@"Ending node that is not at top of stack");
209 }
210
211 if ([nodeStack size] == 0 && node != nil0) {
212 if (delegate != nil0)
213 [delegate XMLReceivedMessage:node];
214 else
215 AWEzvLog(@"Received message but no delegate to send it to");
216 }
217
218}
219- (void) endConnection{
220 [self sendString:@"</stream:stream>"];
221 [connection closeFile];
222 [connection release];
223 connection = nil0;
224 [delegate XMLConnectionClosed];
225
226}
227
228- (void) xmlCharData:(const XML_Char *)data length:(int)len {
229 AWEzvXMLNode *node;
230 NSString *newData;
231
232 if ((len == 1) && (*data == '\n'))
233 return;
234
235 newData = [[[NSString alloc] initWithData:[NSData dataWithBytes:data length:len] encoding:NSUTF8StringEncoding] autorelease];
236
237 if ([nodeStack size] > 0 && [(AWEzvXMLNode *)[nodeStack top] type] == AWEzvXMLText) {
238 node = [nodeStack top];
239 if ([node name] != nil0)
240 [node setName:([[node name] stringByAppendingString:newData])];
241 else
242 [node setName:newData];
243 } else {
244 node = [[[AWEzvXMLNode alloc] initWithType:AWEzvXMLText name:newData] autorelease];
245 if ([nodeStack top] != nil0)
246 [(AWEzvXMLNode *)[nodeStack top] addChild:node];
247 [nodeStack push:node];
248 }
249}
250
251- (void) sendNegotiationInitiator:(int)myInitiator {
252 NSString *string;
253 NSMutableString *mutableString;
254 NSMutableDictionary *dict;
255 NSMutableArray *array;
256
257 CFXMLNodeRef xmlNode;
258 CFXMLElementInfo xmlElementInfo;
259 CFXMLTreeRef xmlTree;
260 NSData *data;
261
262 /* spit out an XML header */
263 string = @"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";
264 [connection writeData:[NSData dataWithBytes:[string UTF8String] length:[string length]]];
265
266 /* now make the handshake initialisation output */
267 string = @"stream:stream";
268
269 /* create elements for handshake */
270 dict = [NSMutableDictionary dictionary];
271 if ([delegate uniqueID]) {
272 [dict setObject:[delegate uniqueID] forKey:@"to"];
273 } else {
274 [dict setObject:@"127.0.0.1" forKey:@"to"];
275 }
276 [dict setObject:[[delegate manager] myInstanceName] forKey:@"from"];
277 [dict setObject:@"jabber:client" forKey:@"xmlns"];
278 [dict setObject:@"http://etherx.jabber.org/streams" forKey:@"xmlns:stream"];
279 array = [NSMutableArray array];
280 [array insertObject:@"xmlns:stream" atIndex:0];
281 [array insertObject:@"xmlns" atIndex:0];
282 [array insertObject:@"from" atIndex:0];
283 [array insertObject:@"to" atIndex:0];
284
285 /* and make an element info structure */
286 xmlElementInfo.attributes = (CFDictionaryRef)[[dict copy] autorelease];
287 xmlElementInfo.attributeOrder = (CFArrayRef)[[array copy] autorelease];
288 xmlElementInfo.isEmpty = YES( BOOL ) 1;
289
290 /* create node and tree, then convert to XML text */
291 xmlNode = CFXMLNodeCreate(NULL( ( void * ) 0 ), kCFXMLNodeTypeElement, (CFStringRef)string, &xmlElementInfo, kCFXMLNodeCurrentVersion);
292 xmlTree = CFXMLTreeCreateWithNode(NULL( ( void * ) 0 ), xmlNode);
293 data = [(NSData *)CFXMLTreeCreateXMLData(NULL( ( void * ) 0 ), xmlTree) autorelease];
294
295 /* now we create an NSString with our data */
296 mutableString = [[[NSMutableString alloc] initWithCString:[data bytes] length:[data length]] autorelease];
297 [mutableString deleteCharactersInRange:NSMakeRange([mutableString length] - 2, 1)];
298 /* and we send it to the connection */
299 [connection writeData:[NSData dataWithBytes:[mutableString UTF8String] length:[mutableString length]]];
300
301 /* and set negoiated if we didn't initiate */
302 if (!myInitiator)
303 negotiated = 1;
304}
305
306@end
307
308/* XML function handlers */
309void xml_start_element (void *userData,
310 const XML_Char *name,
311 const XML_Char **atts) {
312 AWEzvXMLStream *self = userData;
313 [self xmlStartElement:name attributes:atts];
314}
315
316void xml_end_element (void *userData,
317 const XML_Char *name) {
318 AWEzvXMLStream *self = userData;
319 [self xmlEndElement:name];
320}
321
322void xml_char_data (void *userData,
323 const XML_Char *s,
324 int len) {
325 AWEzvXMLStream *self = userData;
326 [self xmlCharData:s length:len];
327}