Bug Summary

File:Plugins/Bonjour/libezv/Private Classes/AWEzvXMLStream.m
Location:line 175, column 9
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) setDelegate:(id)myDelegate {
140 delegate = myDelegate;
141}
142- (id) delegate {
143 return delegate;
144}
145
146- (void) xmlStartElement:(const XML_Char *)name attributes:(const XML_Char **)attributes {
147 AWEzvXMLNode *node;
148 NSString *attribute, *value, *nodeName;
149
150 nodeName = [NSString stringWithUTF8String:name];
151
152 node = [[[AWEzvXMLNode alloc] initWithType:AWEzvXMLElement name:nodeName] autorelease];
153
154 while (*attributes != NULL( ( void * ) 0 )) {
155 attribute = [NSString stringWithUTF8String:*attributes++];
156 value = [NSString stringWithUTF8String:*attributes++];
157 [node addAttribute:attribute withValue:value];
158 }
159
160 if ([nodeStack size] > 0 && [(AWEzvXMLNode *)[nodeStack top] type] == AWEzvXMLText)
161 [nodeStack pop];
162
163 if ([nodeStack size] > 0) {
164 [(AWEzvXMLNode *)[nodeStack top] addChild:node];
165 }
166
167 [nodeStack push:node];
168
169 if (([nodeName isEqualToString:@"stream:stream"]) && !negotiated) {
170 if (initiator) {
171 negotiated = 1;
172 } else {
173 [self sendNegotiationInitiator:0];
174 }
Value stored to 'node' is never read
175 node = [nodeStack pop];
176 }
177
178
179}
180
181- (void) xmlEndElement:(const XML_Char *)name {
182 NSString *nodeName = [NSString stringWithUTF8String:name];
183 if (([nodeStack size] > 0) && ([(AWEzvXMLNode *)[nodeStack top] type] == AWEzvXMLText)) {
184 [nodeStack pop];
185 }
186 else if ([nodeStack size] == 0 && [nodeName isEqualToString:@"stream:stream"]) {
187 /* We have no stack but were sent stream:stream to end, therefore end connection */
188 [self endConnection];
189 return;
190 }
191 AWEzvXMLNode *node = [nodeStack top];
192
193 if (node != nil0 && [[node name] isEqualToString:nodeName]) {
194 [nodeStack pop];
195 } else if ([[node name] isEqualToString:@"stream:stream"]) {
196 // Wow, end of connection!
197 [self endConnection];
198 return;
199 } else {
200 AWEzvLog(@"Ending node that is not at top of stack");
201 }
202
203 if ([nodeStack size] == 0 && node != nil0) {
204 if (delegate != nil0)
205 [delegate XMLReceivedMessage:node];
206 else
207 AWEzvLog(@"Received message but no delegate to send it to");
208 }
209
210}
211- (void) endConnection{
212 [self sendString:@"</stream:stream>"];
213 [connection closeFile];
214 [connection release];
215 connection = nil0;
216 [delegate XMLConnectionClosed];
217
218}
219
220- (void) xmlCharData:(const XML_Char *)data length:(int)len {
221 AWEzvXMLNode *node;
222 NSString *newData;
223
224 if ((len == 1) && (*data == '\n'))
225 return;
226
227 newData = [[[NSString alloc] initWithData:[NSData dataWithBytes:data length:len] encoding:NSUTF8StringEncoding] autorelease];
228
229 if ([nodeStack size] > 0 && [(AWEzvXMLNode *)[nodeStack top] type] == AWEzvXMLText) {
230 node = [nodeStack top];
231 if ([node name] != nil0)
232 [node setName:([[node name] stringByAppendingString:newData])];
233 else
234 [node setName:newData];
235 } else {
236 node = [[[AWEzvXMLNode alloc] initWithType:AWEzvXMLText name:newData] autorelease];
237 if ([nodeStack top] != nil0)
238 [(AWEzvXMLNode *)[nodeStack top] addChild:node];
239 [nodeStack push:node];
240 }
241}
242
243- (void) sendNegotiationInitiator:(int)myInitiator {
244 /* spit out an XML header */
245 [self sendString:@"<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"];
246
247 /* create elements for handshake */
248 NSDictionary *handshakeElements = [NSDictionary dictionaryWithObjectsAndKeys:
249 ([delegate uniqueID] ? [delegate uniqueID] : @"127.0.0.1"), @"to",
250 [[delegate manager] myInstanceName], @"from",
251 @"jabber:client", @"xmlns",
252 @"http://etherx.jabber.org/streams", @"xmlns:stream",
253 nil0];
254
255 /* and make an element info structure */
256 CFXMLElementInfo xmlElementInfo;
257 xmlElementInfo.attributes = (CFDictionaryRef)handshakeElements;
258 xmlElementInfo.attributeOrder = (CFArrayRef)[NSArray arrayWithObjects:@"to", @"from", @"xmlns", @"xmlns:stream", nil0];
259 xmlElementInfo.isEmpty = YES( BOOL ) 1;
260
261 /* create node and tree, then convert to XML text */
262 CFXMLNodeRef xmlNode = CFXMLNodeCreate(NULL( ( void * ) 0 ), kCFXMLNodeTypeElement, (CFStringRef)@"stream:stream", &xmlElementInfo, kCFXMLNodeCurrentVersion);
263 CFXMLTreeRef xmlTree = CFXMLTreeCreateWithNode(NULL( ( void * ) 0 ), xmlNode);
264 NSData *data = [(NSData *)CFXMLTreeCreateXMLData(NULL( ( void * ) 0 ), xmlTree) autorelease];
265 CFRelease(xmlNode);
266 CFRelease(xmlTree);
267
268 /* Unfortunately CFXML* gives us <stream ... /> and we need <stream ...>, so we remove the / here */
269 NSMutableString *mutableString = [NSMutableString stringWithData:data encoding:NSUTF8StringEncoding];
270 [mutableString deleteCharactersInRange:NSMakeRange([mutableString length] - 2, 1)];
271
272 /* and we send it to the connection */
273 [self sendString:mutableString];
274
275 /* and set negoiated if we didn't initiate */
276 if (!myInitiator)
277 negotiated = 1;
278}
279
280@end
281
282/* XML function handlers */
283void xml_start_element (void *userData,
284 const XML_Char *name,
285 const XML_Char **atts) {
286 AWEzvXMLStream *self = userData;
287 [self xmlStartElement:name attributes:atts];
288}
289
290void xml_end_element (void *userData,
291 const XML_Char *name) {
292 AWEzvXMLStream *self = userData;
293 [self xmlEndElement:name];
294}
295
296void xml_char_data (void *userData,
297 const XML_Char *s,
298 int len) {
299 AWEzvXMLStream *self = userData;
300 [self xmlCharData:s length:len];
301}