Debian Codegraph @

Framework/Pantomime/CWMIMEUtility.m

raw
1 /*
2 ** CWMIMEUtility.m
3 **
4 ** Copyright (c) 2001-2007 Ludovic Marcotte
5 ** Copyright (C) 2014-2018 Riccardo Mottola
6 **
7 ** Author: Ludovic Marcotte <[email protected]>
8 ** Riccardo Mottola <[email protected]>
9 ** Sebastian Reitenbach
10 **
11 ** This library is free software; you can redistribute it and/or
12 ** modify it under the terms of the GNU Lesser General Public
13 ** License as published by the Free Software Foundation; either
14 ** version 2.1 of the License, or (at your option) any later version.
15 **
16 ** This library is distributed in the hope that it will be useful,
17 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 ** Lesser General Public License for more details.
20 **
21 ** You should have received a copy of the GNU General Public License
22 ** along with this program. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #import <Pantomime/CWMIMEUtility.h>
26
27 #import <Pantomime/CWConstants.h>
28 #import <Pantomime/CWMessage.h>
29 #import <Pantomime/CWMIMEMultipart.h>
30 #import <Pantomime/NSString+Extensions.h>
31 #import <Pantomime/NSData+Extensions.h>
32 #import <Pantomime/CWPart.h>
33 #import <Pantomime/CWMD5.h>
34 #import <Pantomime/CWUUFile.h>
35
36 #import <Foundation/NSAutoreleasePool.h>
37 #import <Foundation/NSBundle.h>
38 #import <Foundation/NSCharacterSet.h>
39 #import <Foundation/NSFileManager.h>
40 #import <Foundation/NSHost.h>
41 #import <Foundation/NSScanner.h>
42 #import <Foundation/NSValue.h>
43
44 #include <ctype.h>
45 #include <unistd.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <time.h>
49
50 //
51 // C functions
52 //
53 char ent(char **ref);
54 char *striphtml(char *s, int encoding);
55 NSString *unique_id();
56
57 static const char *hexDigit = "0123456789ABCDEF";
58 static unsigned seed_count = 1;
59
60 @implementation CWMIMEUtility
61
62 //
63 // See RFC2047.
64 // It supports:
65 //
66 // Abcd =?ISO-8859-1?Q?=E8fgh?=
67 // =?iso-8859-1?Q?ab=E7de?= =?iso-8859-1?Q?_?= =?iso-8859-1?Q?oo=F4oo?=
68 // Abd =?ISO-8859-1?Q?=E8?= fghijklmn
69 //
70 //
71 + (NSString *) decodeHeader: (NSData *) theData
72 charset: (NSString *) theCharset
73 {
74 NSMutableString *aMutableString;
75 NSData *charset, *encoded_text;
76 NSString *aString;
77
78 NSUInteger i, length, start, i_charset, i_encoding, end;
79 const char *bytes;
80
81 BOOL ignore_span;
82 char encoding;
83
84 // We first verify for a nil value
85 if (theData == nil || (length = [theData length]) == 0)
86 {
87 return @"";
88 }
89
90 // We check for the ISO-2022-JP announcer sequence
91 if ([theData hasCPrefix: "\e$B"])
92 {
93 return AUTORELEASE([[NSString alloc] initWithData: theData encoding: NSISO2022JPStringEncoding]);
94 }
95
96 bytes = [theData bytes];
97
98 aMutableString = [[NSMutableString alloc] initWithCapacity: length];
99
100 start = i = 0;
101 ignore_span = NO;
102
103 while (i < (length - 1))
104 {
105 if (bytes[i] != '=' || bytes[i+1] != '?')
106 {
107 if (bytes[i] > 32)
108 {
109 ignore_span = NO;
110 }
111
112 i++;
113 continue;
114 }
115
116 if (i != start && !ignore_span)
117 {
118 aString = nil;
119
120 if (theCharset)
121 {
122 aString = [NSString stringWithData: [NSData dataWithBytes: bytes+start length: i-start]
123 charset: [theCharset dataUsingEncoding: NSASCIIStringEncoding]];
124 RETAIN(aString);
125 }
126
127 if (!aString)
128 {
129 aString = [[NSString alloc] initWithCString: bytes+start length: i-start];
130 }
131
132 [aMutableString appendString: aString];
133 DESTROY(aString);
134 }
135
136 start = i;
137
138 // skip the =? and one character (or it isn't valid)
139 i += 3;
140 while (i < length && bytes[i] != '?')
141 {
142 i++;
143 }
144
145 if ( i == length)
146 {
147 break;
148 }
149
150 i_charset = i;
151
152 // encoding is a single character
153 if (i+2 >= length)
154 {
155 break;
156 }
157
158 encoding = bytes[i+1];
159
160 if (bytes[i+2] != '?')
161 {
162 break;
163 }
164
165 i += 3;
166 i_encoding = i;
167
168 while (i <length && bytes[i] != '?')
169 {
170 i++;
171 }
172
173 if (i+1 >= length)
174 {
175 break;
176 }
177
178 if (bytes[i+1] != '=')
179 {
180 break;
181 }
182
183 end = i;
184 i += 2;
185
186 if (theCharset)
187 {
188 charset = [theCharset dataUsingEncoding: NSASCIIStringEncoding];
189 }
190 else
191 {
192 NSRange charsetRange;
193 charsetRange = NSMakeRange(start+2,i_charset-start-2);
194 charset = [theData subdataWithRange: charsetRange];
195 }
196
197 encoded_text = [theData subdataWithRange: NSMakeRange(i_encoding,end-i_encoding)];
198
199 if (encoding == 'q' || encoding == 'Q')
200 {
201 aString = [NSString stringWithData: [encoded_text decodeQuotedPrintableInHeader: YES]
202 charset: charset];
203 }
204 else if (encoding == 'b' || encoding== 'B')
205 {
206 NSData *decodedData;
207
208 decodedData = [encoded_text decodeBase64];
209 aString = [NSString stringWithData: decodedData
210 charset: charset];
211 }
212 else
213 {
214 continue;
215 }
216
217 if (aString)
218 {
219 [aMutableString appendString: aString];
220 }
221 aString = nil;
222 start = i;
223 ignore_span = YES;
224 }
225
226 // always append end of string, even if there was a space
227 i = length;
228 if (i > start)
229 {
230 aString = nil;
231
232 if (theCharset)
233 {
234 aString = [NSString stringWithData: [NSData dataWithBytes: bytes+start length: i-start]
235 charset: [theCharset dataUsingEncoding: NSASCIIStringEncoding]];
236 RETAIN(aString);
237 }
238
239 if (!aString)
240 {
241 aString = [[NSString alloc] initWithCString: bytes+start length: i-start];
242 }
243
244 [aMutableString appendString: aString];
245 DESTROY(aString);
246 }
247
248 return AUTORELEASE(aMutableString);
249 }
250
251
252 //
253 //
254 //
255 + (NSData *) globallyUniqueBoundary
256 {
257 NSMutableData *aMutableData;
258
259 aMutableData = [[NSMutableData alloc] init];
260 [aMutableData appendBytes: "=_" length: 2];
261 [aMutableData appendCFormat: @"%@", unique_id()];
262
263 return AUTORELEASE(aMutableData);
264 }
265
266
267 //
268 // Instead of using [[NSHost currentHost] name],
269 // we use gethostname(). This is due to the fact
270 // that NSHost's -name method will block for a good
271 // amount of time if DNS resolutions can't be made.
272 //
273 + (NSData *) globallyUniqueID
274 {
275 NSMutableData *aMutableData;
276 char s[65];
277
278
279 aMutableData = [[NSMutableData alloc] init];
280 [aMutableData appendCFormat: @"%@", unique_id()];
281
282 memset(s, 0, sizeof(s));
283 gethostname(s, sizeof(s)-1);
284 [aMutableData appendCFormat: @"@%s", s];
285
286 return AUTORELEASE(aMutableData);
287 }
288
289
290 //
291 // FIXME: Should we really use NSUTF8StringEncoding in base64?
292 //
293 + (NSData *) encodeHeader: (NSString *) theText
294 charset: (NSString *) theCharset
295 encoding: (PantomimeEncoding) theEncoding
296 {
297 NSData *aData;
298
299 // Initial verification
300 if (!theText || [theText length] == 0)
301 {
302 return [NSData data];
303 }
304
305 aData = [theText dataUsingEncoding: [NSString encodingForCharset:
306 [theCharset dataUsingEncoding: NSASCIIStringEncoding]]];
307
308 if (theEncoding == PantomimeEncodingQuotedPrintable)
309 {
310 return [aData encodeQuotedPrintableWithLineLength: 0 inHeader: YES];
311 }
312 else if (theEncoding == PantomimeEncodingBase64)
313 {
314 return [aData encodeBase64WithLineLength: 0];
315 }
316 //
317 // FIXME: What should we do here, should we just return the 'aData' w/o
318 // encoding it or should we generate an exception?
319 else
320 {
321 return aData;
322 }
323 }
324
325
326 //
327 // The format returned is for example:
328 //
329 // =?ISO-8859-1?B?....?=
330 //
331 // This format is known as an encoded-word defined in RFC2047.
332 // If the word doesn't need to be encoded, it's just returned as is.
333 //
334 // FIXME: We should verify that the length doesn't exceed 75 chars.
335 //
336 #warning remove/combine or leave and document
337 + (NSData *) encodeWordUsingBase64: (NSString *) theWord
338 prefixLength: (int) thePrefixLength
339 {
340 // Initial verification
341 if (!theWord || [theWord length] == 0)
342 {
343 return [NSData data];
344 }
345 else if ([theWord is7bitSafe])
346 {
347 return [theWord dataUsingEncoding: NSASCIIStringEncoding];
348 }
349 else
350 {
351 NSMutableData *aMutableData;
352 NSString *aCharset;
353
354 aMutableData = [[NSMutableData alloc] init];
355 aCharset = [theWord charset];
356
357 [aMutableData appendCFormat: @"=?%@?b?", aCharset];
358 [aMutableData appendData: [CWMIMEUtility encodeHeader: theWord
359 charset: aCharset
360 encoding: PantomimeEncodingBase64]];
361 [aMutableData appendCString: "?="];
362
363 return AUTORELEASE(aMutableData);
364 }
365
366 // Never reached.
367 return nil;
368 }
369
370
371 //
372 // The format returned is for example:
373 //
374 // =?ISO-8859-1?Q?=E8fgh?=
375 //
376 // This format is known as an encoded-word defined in RFC2047.
377 // If the word doesn't need to be encoded, it's just returned as it.
378 //
379 // The string returned will NOT be more than 75 characters long
380 // for each folded part of the original string.
381 //
382 #warning remove/combine or leave and document
383 + (NSData *) encodeWordUsingQuotedPrintable: (NSString *) theWord
384 prefixLength: (NSUInteger) thePrefixLength
385 {
386 NSMutableString *aMutableString;
387 NSMutableArray *aMutableArray;
388 NSString *aCharset, *aString;
389 NSMutableData *aMutableData;
390 NSScanner *aScanner;
391
392 NSUInteger i, count;
393 NSUInteger previousLocation, currentLocation;
394 BOOL mustUseEncoding;
395
396 if (!theWord || [theWord length] == 0 )
397 {
398 return [NSData data];
399 }
400
401 // If it's not an ASCII string, we must use the encoding!
402 mustUseEncoding = ![theWord is7bitSafe];
403
404 aCharset = nil;
405
406 if (mustUseEncoding)
407 {
408 aCharset = [theWord charset];
409 }
410
411 aMutableString = [[NSMutableString alloc] init];
412
413 aMutableArray = [[NSMutableArray alloc] init];
414 AUTORELEASE(aMutableArray);
415
416 // We initialize our scanner with the content of our word
417 aScanner = [[NSScanner alloc] initWithString: theWord];
418
419 previousLocation = 0;
420
421 while ([aScanner scanUpToCharactersFromSet: [NSCharacterSet whitespaceCharacterSet] intoString: NULL])
422 {
423 NSUInteger length;
424
425 currentLocation = [aScanner scanLocation];
426
427 // aString contains the substring WITH the spaces present BEFORE the word and AFTER the last aString.
428 //
429 // For example, the result of "this is a test" will be
430 //
431 // aString = "this"
432 // aString = " is"
433 // aString = " a"
434 // aString = " test"
435 //
436 aString = [theWord substringWithRange: NSMakeRange(previousLocation, currentLocation - previousLocation)];
437
438 if (mustUseEncoding)
439 {
440 // Our 'initial' length contains =?iso-8859-x?q? and ?=
441 length = 18;
442 length += [[CWMIMEUtility encodeHeader: [NSString stringWithFormat: @"%@%@", aMutableString, aString]
443 charset: aCharset
444 encoding: PantomimeEncodingQuotedPrintable] length];
445 }
446 else
447 {
448 length = [aMutableString length] + [aString length];
449 }
450
451 // If we are on the first line, we must consider the prefix length.
452 // For example, the prefix length might be the length of the string "Subject: "
453 if ([aMutableArray count] == 0)
454 {
455 length += thePrefixLength;
456 }
457
458 if (length > 75)
459 {
460 [aMutableArray addObject: aMutableString];
461
462 RELEASE(aMutableString);
463 aMutableString = [[NSMutableString alloc] init];
464 }
465
466 [aMutableString appendString: aString];
467 previousLocation = currentLocation;
468 }
469
470 // We add our last string to the array.
471 [aMutableArray addObject: aMutableString];
472 RELEASE(aMutableString);
473
474 RELEASE(aScanner);
475
476 aMutableData = [[NSMutableData alloc] init];
477
478 count = [aMutableArray count];
479 for (i = 0; i < count; i++)
480 {
481 aString = [aMutableArray objectAtIndex: i];
482
483 // We must append a space (' ') before each folded line.
484 if (i > 0)
485 {
486 [aMutableData appendCString: " "];
487 }
488
489 if (mustUseEncoding)
490 {
491 [aMutableData appendCFormat: @"=?%@?q?", aCharset];
492 [aMutableData appendData: [CWMIMEUtility encodeHeader: aString
493 charset: aCharset
494 encoding: PantomimeEncodingQuotedPrintable] ];
495 [aMutableData appendCString: "?="];
496 }
497 else
498 {
499 [aMutableData appendData: [aString dataUsingEncoding: NSASCIIStringEncoding]];
500 }
501
502 // We if it is our last string, we must NOT append the \n
503 if (!(i == (count-1)))
504 {
505 [aMutableData appendCString: "\n"];
506 }
507 }
508
509 return AUTORELEASE(aMutableData);
510 }
511
512
513 //
514 //
515 //
516 + (CWMessage *) compositeMessageContentFromRawSource: (NSData *) theData
517 {
518 return AUTORELEASE([[CWMessage alloc] initWithData: theData]);
519 }
520
521
522 //
523 // FIXME: whitespace after boundary markers
524 //
525 + (CWMIMEMultipart *) compositeMultipartContentFromRawSource: (NSData *) theData
526 boundary: (NSData *) theBoundary
527 {
528 CWMIMEMultipart *aMimeMultipart;
529
530 NSMutableData *aMutableData;
531 NSArray *allParts;
532 NSRange aRange;
533 NSUInteger i, count;
534
535 aMimeMultipart = [[CWMIMEMultipart alloc] init];
536
537 aMutableData = [[NSMutableData alloc] init];
538 [aMutableData appendBytes: "--" length: 2];
539 [aMutableData appendData: theBoundary];
540
541 // We first skip everything before the first boundary
542 aRange = [theData rangeOfData: aMutableData];
543
544 if (aRange.length && aRange.location)
545 {
546 theData = [theData subdataFromIndex: (aRange.location + aRange.length)];
547 }
548
549 [aMutableData setLength: 0];
550 [aMutableData appendBytes: "\n--" length: 3];
551 [aMutableData appendData: theBoundary];
552
553 // Add terminating 0 so we can use it as a C string below
554 [aMutableData appendBytes: "\0" length: 1];
555
556 // We split this mime body part into multiple parts since we have various representations
557 // of the actual body part.
558 allParts = [theData componentsSeparatedByCString: [aMutableData bytes]];
559 count = [allParts count];
560 RELEASE(aMutableData);
561
562 for (i = 0; i < count; i++)
563 {
564 CWPart *aPart;
565 NSData *aData;
566
567 aData = [allParts objectAtIndex: i];
568
569 if (aData && [aData length] > 0)
570 {
571 // This is the last part. Ignore everything past the end marker
572 if ([aData hasCPrefix: "--\n"] ||
573 ([aData length] == 2 && [aData hasCPrefix: "--"]))
574 {
575 break;
576 }
577
578 // We then skip the first character since it's a \n (the one at the end of the boundary)
579 if ([aData hasCPrefix: "\n"])
580 {
581 aData = [aData subdataFromIndex: 1];
582 }
583
584 aPart = [[CWPart alloc] initWithData: aData];
585 [aPart setSize: [aData length]];
586 [aMimeMultipart addPart: aPart];
587 RELEASE(aPart);
588 }
589 }
590
591 return AUTORELEASE(aMimeMultipart);
592 }
593
594
595 //
596 //
597 //
598 + (id) discreteContentFromRawSource: (NSData *) theData
599 encoding: (PantomimeEncoding) theEncoding
600 {
601 if (theEncoding == PantomimeEncodingQuotedPrintable)
602 {
603 return [theData decodeQuotedPrintableInHeader: NO];
604 }
605 else if (theEncoding == PantomimeEncodingBase64)
606 {
607 return [[theData dataByRemovingLineFeedCharacters] decodeBase64];
608 }
609
610 return theData;
611 }
612
613
614 //
615 //
616 //
617 + (void) setContentFromRawSource: (NSData *) theData
618 inPart: (CWPart *) thePart
619 {
620 NSAutoreleasePool *pool;
621
622 // We create a temporary autorelease pool since this method can be
623 // memory consuming on our default autorelease pool.
624 pool = [[NSAutoreleasePool alloc] init];
625
626 //
627 // Composite types (message/multipart).
628 //
629 if ([thePart isMIMEType: @"message" subType: @"rfc822"])
630 {
631 NSData *aData;
632
633 aData = theData;
634
635 // We verify the Content-Transfer-Encoding, this part could be base64 encoded.
636 if ([thePart contentTransferEncoding] == PantomimeEncodingBase64)
637 {
638 NSMutableData *aMutableData;
639
640 aData = [[theData dataByRemovingLineFeedCharacters] decodeBase64];
641
642 aMutableData = [NSMutableData dataWithData: aData];
643 [aMutableData replaceCRLFWithLF];
644 aData = aMutableData;
645 }
646
647 [thePart setContent: [CWMIMEUtility compositeMessageContentFromRawSource: aData]];
648 }
649 else if ([thePart isMIMEType: @"multipart" subType: @"*"])
650 {
651 [thePart setContent: [CWMIMEUtility compositeMultipartContentFromRawSource: theData
652 boundary: [thePart boundary]]];
653 }
654 //
655 // Discrete types (text/application/audio/image/video) or any "unsupported Content-Type:s"
656 //
657 // text/*
658 // image/*
659 // application/*
660 // audio/*
661 // video/*
662 //
663 // We also treat those composite type as discrete types:
664 //
665 #warning test extensively those
666 // message/delivery-status
667 // message/disposition-notification
668 //
669 else
670 {
671 [thePart setContent: [CWMIMEUtility discreteContentFromRawSource: theData
672 encoding: [thePart contentTransferEncoding]]];
673 }
674
675 RELEASE(pool);
676 }
677
678
679 //
680 //
681 //
682 + (NSData *) plainTextContentFromPart: (CWPart *) thePart
683 {
684 NSData *aContent;
685
686 aContent = (NSData *)[thePart content];
687
688 //
689 // If it's a text/html part, we must remove all the formatting codes and
690 // keep only the 'real' text contained in that part. We should do the
691 // same for text/rtf or text/enriched parts.
692 //
693 if ([thePart isMIMEType: @"text" subType: @"html"])
694 {
695 char *buf, *bytes;
696
697 buf = (char *)malloc(([aContent length]+1)*sizeof(char));
698 memset(buf, 0, [aContent length]+1);
699 memcpy(buf, [aContent bytes], [aContent length]);
700
701 bytes = striphtml(buf, [NSString encodingForPart: thePart]);
702 free(buf);
703
704 aContent = [NSData dataWithBytesNoCopy: bytes length: strlen(bytes) freeWhenDone: YES];
705 }
706
707 return aContent;
708 }
709
710 @end
711
712
713 //
714 // This C function has been written by Abhijit Menon-Sen <[email protected]>
715 // This code is in the public domain.
716 //
717 char *striphtml(char *s, int encoding)
718 {
719 char c, *t, *text;
720
721 if ((t = text = malloc(strlen(s)+1)) == NULL)
722 {
723 return NULL;
724 }
725
726 while ((c = *s++))
727 {
728 if (c == '<')
729 {
730 /*
731 * Ignore everything to the end of this tag or sgml comment.
732 */
733 if (s[0] == '!' && s[1] == '-' && s[2] == '-')
734 {
735 s += 3;
736 while ((c = *s++))
737 {
738 if (c == '-' && s[0] == '-' && s[1] == '>')
739 {
740 s += 2;
741 break;
742 }
743 }
744 }
745 else
746 {
747 while ((c = *s++))
748 {
749 if (c == '>')
750 {
751 break;
752 }
753 }
754 }
755 }
756 else if (c == '&')
757 {
758 NSString *aString;
759
760 c = ent(&s);
761 aString = AUTORELEASE([[NSString alloc] initWithBytes: &c length: 1 encoding: NSISOLatin1StringEncoding]);
762
763 if ([aString length])
764 {
765 NSData *aData;
766
767 aData = [aString dataUsingEncoding: encoding];
768
769 if (aData)
770 {
771 char *bytes;
772 NSUInteger len;
773
774 bytes = (char *)[aData bytes];
775 len = [aData length];
776
777 while (len--)
778 {
779 *t++ = *bytes;
780 bytes++;
781 }
782 }
783 }
784 }
785 else
786 {
787 *t++ = c;
788 }
789 }
790
791 *t = '\0';
792
793 return text;
794 }
795
796
797 //
798 // This C function has been written by Abhijit Menon-Sen <[email protected]>
799 // This code is in the public domain.
800 //
801 char ent(char **ref)
802 {
803 unsigned i;
804 char c = ' ', *s = *ref, *t = s;
805
806 struct {
807 char *name;
808 char chr;
809 } refs[] = {
810 { "lt" , '<' },
811 { "gt" , '>' },
812 { "amp" , '&' },
813 { "quot" , '"' },
814 { "nbsp" , (char)160 },
815 { "iexcl" , (char)161 },
816 { "cent" , (char)162 },
817 { "pound" , (char)163 },
818 { "curren", (char)164 },
819 { "yen" , (char)165 },
820 { "brvbar", (char)166 },
821 { "sect" , (char)167 },
822 { "uml" , (char)168 },
823 { "copy" , (char)169 },
824 { "ordf" , (char)170 },
825 { "laquo" , (char)171 },
826 { "not" , (char)172 },
827 { "shy" , (char)173 },
828 { "reg" , (char)174 },
829 { "macr" , (char)175 },
830 { "deg" , (char)176 },
831 { "plusmn", (char)177 },
832 { "sup2" , (char)178 },
833 { "sup3" , (char)179 },
834 { "acute" , (char)180 },
835 { "micro" , (char)181 },
836 { "para" , (char)182 },
837 { "middot", (char)183 },
838 { "cedil" , (char)184 },
839 { "sup1" , (char)185 },
840 { "ordm" , (char)186 },
841 { "raquo" , (char)187 },
842 { "frac14", (char)188 },
843 { "frac12", (char)189 },
844 { "frac34", (char)190 },
845 { "iquest", (char)191 },
846 { "Agrave", (char)192 },
847 { "Aacute", (char)193 },
848 { "Acirc" , (char)194 },
849 { "Atilde", (char)195 },
850 { "Auml" , (char)196 },
851 { "Aring" , (char)197 },
852 { "AElig" , (char)198 },
853 { "Ccedil", (char)199 },
854 { "Egrave", (char)200 },
855 { "Eacute", (char)201 },
856 { "Ecirc" , (char)202 },
857 { "Euml" , (char)203 },
858 { "Igrave", (char)204 },
859 { "Iacute", (char)205 },
860 { "Icirc" , (char)206 },
861 { "Iuml" , (char)207 },
862 { "ETH" , (char)208 },
863 { "Ntilde", (char)209 },
864 { "Ograve", (char)210 },
865 { "Oacute", (char)211 },
866 { "Ocirc" , (char)212 },
867 { "Otilde", (char)213 },
868 { "Ouml" , (char)214 },
869 { "times" , (char)215 },
870 { "Oslash", (char)216 },
871 { "Ugrave", (char)217 },
872 { "Uacute", (char)218 },
873 { "Ucirc" , (char)219 },
874 { "Uuml" , (char)220 },
875 { "Yacute", (char)221 },
876 { "THORN" , (char)222 },
877 { "szlig" , (char)223 },
878 { "agrave", (char)224 },
879 { "aacute", (char)225 },
880 { "acirc" , (char)226 },
881 { "atilde", (char)227 },
882 { "auml" , (char)228 },
883 { "aring" , (char)229 },
884 { "aelig" , (char)230 },
885 { "ccedil", (char)231 },
886 { "egrave", (char)232 },
887 { "eacute", (char)233 },
888 { "ecirc" , (char)234 },
889 { "euml" , (char)235 },
890 { "igrave", (char)236 },
891 { "iacute", (char)237 },
892 { "icirc" , (char)238 },
893 { "iuml" , (char)239 },
894 { "eth" , (char)240 },
895 { "ntilde", (char)241 },
896 { "ograve", (char)242 },
897 { "oacute", (char)243 },
898 { "ocirc" , (char)244 },
899 { "otilde", (char)245 },
900 { "ouml" , (char)246 },
901 { "divide", (char)247 },
902 { "oslash", (char)248 },
903 { "ugrave", (char)249 },
904 { "uacute", (char)250 },
905 { "ucirc" , (char)251 },
906 { "uuml" , (char)252 },
907 { "yacute", (char)253 },
908 { "thorn" , (char)254 },
909 { "yuml" , (char)255 }
910 };
911
912 while (isalpha((int)(unsigned char)*s) || isdigit((int)(unsigned char)*s) || *s == '#')
913 s++;
914
915 for (i = 0; i < sizeof(refs)/sizeof(refs[0]); i++) {
916 if (strncmp(refs[i].name, t, s-t) == 0) {
917 c = refs[i].chr;
918 break;
919 }
920 }
921
922 if (*s == ';')
923 s++;
924
925 *ref = s;
926 return c;
927 }
928
929
930 //
931 //
932 //
933 NSString *unique_id()
934 {
935 NSMutableData *aMutableData;
936 CWMD5 *aMD5;
937
938 char random_data[9];
939 time_t curtime;
940 int i, pid;
941
942 pid = getpid();
943 time(&curtime);
944
945 for (i = 0; i < 8; i++)
946 {
947 srand(seed_count++);
948 random_data[i] = hexDigit[rand()&0xf];
949 }
950 random_data[8] = '\0';
951
952 aMutableData = [[NSMutableData alloc] init];
953 [aMutableData appendCFormat: @"%d.%ld%s", pid, (long)curtime, random_data];
954 aMD5 = [[CWMD5 alloc] initWithData: aMutableData];
955 RELEASE(aMutableData);
956 AUTORELEASE(aMD5);
957
958 [aMD5 computeDigest];
959
960 return [aMD5 digestAsString];
961 }