@@ -268,27 +268,29 @@ - (id) initWithData: (NSData *)data
268268      if  (self)
269269	{
270270	  NSStringEncoding 	enc;
271+           NSString 		*tmp;
271272
272273	  _parser = [GSXMLParserIvars new ];
273- 	  /*  Determine character encoding and convert to utf-8 if needed. 
274+ 	  /*  Determine character encoding and convert to utf-8
274275	   */  
275276	  enc = [GSMimeDocument encodingFromCharset: 
276277	    [GSMimeDocument charsetForXml:  data]];
277- 	  if  (enc == NSUTF8StringEncoding
278- 	    || enc == NSASCIIStringEncoding
279- 	    || enc == GSUndefinedEncoding)
278+ 	  if  (GSUndefinedEncoding == enc)
280279	    {
281- 	      this-> data  = [data  copy ]; 
280+ 	      enc  = NSUTF8StringEncoding;	 //  Guess at UTF8 
282281	    }
283- 	  else 
284- 	    {
285- 	      NSString 	*tmp;
286282
283+           tmp = [[NSString  alloc ] initWithData:  data encoding:  enc];
284+ 	  if  (nil  == tmp)
285+ 	    {
286+ 	      /*  Bad encoding... fall back to latin1, guaranteed to work.
287+ 	       */  
288+ 	      enc = NSISOLatin1StringEncoding;
287289	      tmp = [[NSString  alloc ] initWithData:  data encoding:  enc];
288- 	      this-> data 
289- 		= [[ tmp dataUsingEncoding:  NSUTF8StringEncoding]  retain ] ;
290- 	       RELEASE (tmp);
291- 	    } 
290+ 	    }   
291+ 	  this-> data  =  RETAIN ([ tmp dataUsingEncoding:  NSUTF8StringEncoding]) ;
292+ 	  RELEASE (tmp);
293+ 
292294	  this->tagPath  = [[NSMutableArray  alloc ] init ];
293295	  this->namespaces  = [[NSMutableArray  alloc ] init ];
294296	  this->bytes  = [this->data bytes ];
@@ -1357,24 +1359,22 @@ - (BOOL) parse
13571359            {
13581360              if  ([this->tagPath count ] != 0 )
13591361                {
1360-                   if  (!this->acceptHTML )
1362+                   if  (this->acceptHTML )
1363+ 		    {
1364+ 		      /*  Implicitly closes all open tags.
1365+ 		       */  
1366+ 		      while  ([this->tagPath count ] > 0 )
1367+ 			{
1368+ 			  [self  _closeLastTag ];
1369+ 			}
1370+ 		    }
1371+                   else 
13611372                    {
13621373                      /*  strict XML nesting error
13631374                       */  
13641375                      return  [self  _parseError:  @" unexpected end of file" 
13651376			code:  NSXMLParserNotWellBalancedError];
13661377                    }
1367-                 while  ([this->tagPath count ] > 0 )
1368-                   {
1369-                     //  lazily close all open tags
1370-                     if  (this->didEndElement  != 0 )
1371-                       {
1372-                         (*this->didEndElement )(_del,
1373- 			  didEndElementSel, self,
1374-                           [this->tagPath lastObject ], nil , nil );
1375-                       }
1376-                     [this->tagPath removeLastObject ];  //  pop from stack
1377-                   }
13781378                }
13791379#if  EXTRA_DEBUG
13801380              NSLog (@" parserDidEndDocument: "  );
0 commit comments