File: Parser.m

package info (click to toggle)
pantomime 1.1.2-3
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 2,224 kB
  • ctags: 389
  • sloc: objc: 15,433; ansic: 2,843; makefile: 75; sh: 4
file content (762 lines) | stat: -rw-r--r-- 19,481 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/*
**  Parser.m
**
**  Copyright (c) 2001, 2002, 2003
**
**  Author: Ludovic Marcotte <ludovic@Sophos.ca>
**
**  This library is free software; you can redistribute it and/or
**  modify it under the terms of the GNU Lesser General Public
**  License as published by the Free Software Foundation; either
**  version 2.1 of the License, or (at your option) any later version.
**  
**  This library is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
**  Lesser General Public License for more details.
**  
**  You should have received a copy of the GNU Lesser General Public
**  License along with this library; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <Pantomime/Parser.h>

#include <Pantomime/Constants.h>
#include <Pantomime/Flags.h>
#include <Pantomime/InternetAddress.h>
#include <Pantomime/Message.h>
#include <Pantomime/MimeUtility.h>
#include <Pantomime/NSData+Extensions.h>
#include <Pantomime/NSString+Extensions.h>

#include <Pantomime/elm_defs.h>

#include <Foundation/NSTimeZone.h>

@implementation Parser

//
// This method is used to parse the Content-Description: header value.
//
+ (void) parseContentDescription: (NSData *) theLine
                          inPart: (Part *) thePart
{
  NSData *aData;

  aData = [[theLine subdataFromIndex: 20] dataByTrimmingWhiteSpaces];

  if (aData && [aData length])
    {
      [thePart setContentDescription: [[aData dataFromQuotedData] asciiString] ];
    }
}


//
// This method is used to parse the Content-Disposition: header value.
// It supports the following parameters:  "filename" ; case-insensitive
//
+ (void) parseContentDisposition: (NSData *) theLine
                          inPart: (Part *) thePart
{  
  if ( [theLine length] > 21 )
    {
      NSData *aData;
      NSRange aRange;

      aData = [theLine subdataFromIndex: 21];
      aRange = [aData rangeOfCString: ";"];
      
      if ( aRange.length > 0 )
	{
	  NSRange filenameRange;
	  NSData *aFilename;
	  
	  // We set the content disposition to this part
	  [thePart setContentDisposition: [[aData subdataWithRange: NSMakeRange(0, aRange.location)] asciiString] ];
	  
	  // We now decode our filename
	  filenameRange = [aData rangeOfCString: "filename"];

	  if ( filenameRange.length > 0)
	    {
	      aFilename = [Parser _parameterValueUsingLine: aData
				  range: filenameRange];
	      [thePart setFilename: [MimeUtility decodeHeader: [aFilename dataFromQuotedData]
	      					 charset: [thePart defaultCharset]] ];
	    }
	}
      else
	{
	  [thePart setContentDisposition: [[aData dataByTrimmingWhiteSpaces] asciiString] ];
	}
    }
  else
    {
      [thePart setContentDisposition: @""];
    }
}


//
// This method is used to parse the Content-ID: header value.
//
+ (void) parseContentID: (NSData *) theLine
		 inPart: (Part *) thePart
{
  if ( [theLine length] > 12 )
    {
      NSData *aData;
      
      aData = [theLine subdataFromIndex: 12];
      
      if ( [aData hasCPrefix: "<"] && [aData hasCSuffix: ">"] )
	{
	  [thePart setContentID: [[aData subdataWithRange: NSMakeRange(1, [aData length] -2)] asciiString] ];
	}
      else
	{
	  [thePart setContentID: [aData asciiString] ];
	}
    }
  else
    {
      [thePart setContentID: @""];
    }
}


//
// This method is used to parse the Content-Transfer-Encoding: header value.
//
// It supports: "7bit" (or none) ;  case-insensitive
//              "quoted-printable"
//              "base64"
//              "8bit"
//              "binary"
//
+ (void) parseContentTransferEncoding: (NSData *) theLine
                               inPart: (Part *) thePart
{
  if ( [theLine length] > 26 )
    {
      NSData *aData;
      
      aData = [[theLine subdataFromIndex: 26] dataByTrimmingWhiteSpaces];
      
      if ([aData caseInsensitiveCCompare: "quoted-printable"] == NSOrderedSame)
	{
	  [thePart setContentTransferEncoding: QUOTEDPRINTABLE];
	}
      else if ([aData caseInsensitiveCCompare: "base64"] == NSOrderedSame)
	{
	  [thePart setContentTransferEncoding: BASE64];
	}
      else if ([aData caseInsensitiveCCompare: "8bit"] == NSOrderedSame)
	{
	  [thePart setContentTransferEncoding: EIGHTBIT];
	}
      else if ([aData caseInsensitiveCCompare: "binary"] == NSOrderedSame)
	{
	  [thePart setContentTransferEncoding: BINARY];
	}
      else
	{
	  [thePart setContentTransferEncoding: NONE];
	}
    }
  else
    {
      [thePart setContentTransferEncoding: NONE];
    }
}


//
// This method must parse:  
// - Content-Type: text/plain
// - Content-Type: Text/plain;
// - Content-Type: text/plain; charset="iso-8859-1"
// - Content-Type: text
// - Content-Type:    text/plain
//
// This method also parse (if we need too) the following parameters: 
//  - boundary (if Content-Type is multipart/*)
//  - charset  (if Content-Type is text/plain)
//  - name
//
+ (void) parseContentType: (NSData *) theLine
		   inPart: (Part *) thePart
{
  NSRange aRange;
  NSData *aData;
  int x;

  if ( [theLine length] <= 14 )
    {
      [thePart setContentType: @"text/plain"];
      return;
    }

  aData = [[theLine subdataFromIndex: 13] dataByTrimmingWhiteSpaces];

  // We first skip the parameters, if we need to
  x = [aData indexOfCharacter: ';'];
  if (x > 0)
    {
      aData = [aData subdataToIndex: x];
    } 
  
  // We see if there's a subtype specified for text, if none was specified, we append "/plain"
  x = [aData indexOfCharacter: '/'];

  if (x < 0 && [aData hasCaseInsensitiveCPrefix: "text"])
    {
      [thePart setContentType: [[[aData asciiString] stringByAppendingString: @"/plain"] lowercaseString] ];
    }
  else
    {
      [thePart setContentType: [[aData asciiString] lowercaseString] ];
    }

  //
  // We decode our boundary (if we need to)
  //
  aRange = [theLine rangeOfCString: "boundary"
		    options: NSCaseInsensitiveSearch];
  
  if (aRange.length > 0)
    {
      [thePart setBoundary: [Parser _parameterValueUsingLine: theLine
				    range: aRange] ];
    }

  //
  // We decode our charset (if we need to)
  //
  aRange = [theLine rangeOfCString: "charset"
		    options: NSCaseInsensitiveSearch];
  
  if (aRange.length > 0)
    {
      [thePart setCharset: [[Parser _parameterValueUsingLine: theLine
				   range: aRange] asciiString] ];
    }
  
  
  //
  // We decode our format (if we need to). See RFC2646.
  //
  aRange = [theLine rangeOfCString: "format"
		    options: NSCaseInsensitiveSearch];
  
  if (aRange.length > 0)
    {
      NSData *aFormat;
      
      aFormat = [Parser _parameterValueUsingLine: theLine
			range: aRange];

      if ([aFormat caseInsensitiveCCompare: "flowed"] == NSOrderedSame)
	{
	  [thePart setFormat: FORMAT_FLOWED];
	}
      else
	{
	  [thePart setFormat: FORMAT_UNKNOWN];
	}
    }
  else
    {
      [thePart setFormat: FORMAT_UNKNOWN];
    }

  //
  // We decode the parameter "name" iif the thePart is an instance of Part
  //
  if ( [thePart isKindOfClass: [Part class]] )
  {
    aRange = [theLine rangeOfCString: "name"
		      options: NSCaseInsensitiveSearch];

    if (aRange.length > 0)
      {
	NSData *aFilename;

	aFilename = [Parser _parameterValueUsingLine: theLine
			    range: aRange];
	
	[thePart setFilename: [MimeUtility decodeHeader: aFilename
					   charset: [thePart defaultCharset]]];
      }
  }
}


//
// This method is used to parse the Date: header value.
//
+ (void) parseDate: (NSData *) theLine
	 inMessage: (Message *) theMessage
{
  if ( [theLine length] > 6 )
    {
      struct header_rec hdr;
      NSCalendarDate *aDate;
      NSData *aData;
      
      aData = [theLine subdataFromIndex: 6];
      
      if ( parse_arpa_date([aData cString], &hdr) )
	{
	  aDate = [NSCalendarDate dateWithTimeIntervalSince1970: hdr.time_sent];
	  [aDate setTimeZone: [NSTimeZone timeZoneForSecondsFromGMT: hdr.tz_offset]];
	  [theMessage setReceivedDate: aDate];
	}
    }
}


//
// This method is used to parse the To: Cc: Bcc: headers value.
//
+ (void) parseDestination: (NSData *) theLine
		  forType: (int) theType
		inMessage: (Message *) theMessage
{  
  InternetAddress *anInternetAddress;
  char abuf[128], nbuf[128], *cf = "", *nf;
  int rc;

  if (theType == BCC && ([theLine length] > 5))
    {
      [theMessage addHeader:@"Bcc" withValue:@""];
      cf = (char*)[[theLine subdataFromIndex: 5] cString];
    }
  else if (theType == CC && ([theLine length] > 4))
    {
      [theMessage addHeader:@"Cc" withValue:@""];
      cf = (char*)[[theLine subdataFromIndex: 4] cString];
    }
  else if (theType == TO && ([theLine length] > 4))
    {
      [theMessage addHeader:@"To" withValue:@""];
      cf = (char*)[[theLine subdataFromIndex: 4] cString];
    }
  else if (theType == RESENT_BCC && ([theLine length] > 12))
    {
      [theMessage addHeader:@"Resent-Bcc" withValue:@""];
      cf = (char*)[[theLine subdataFromIndex: 12] cString];
    }
  else if (theType == RESENT_CC && ([theLine length] > 11))
    {
      [theMessage addHeader:@"Resent-Cc" withValue:@""];
      cf = (char*)[[theLine subdataFromIndex: 11] cString];
    }
  else if (theType == RESENT_TO && ([theLine length] > 11))
    {
      [theMessage addHeader:@"Resent-To" withValue:@""];
      cf = (char*)[[theLine subdataFromIndex: 11] cString];
    }

  while (*cf != '\0')
    {
      rc = parse_arpa_mailbox(cf, abuf, sizeof(abuf), nbuf, sizeof(nbuf), &nf);
      if (rc < 0)
	{
	  anInternetAddress = [[InternetAddress alloc] init];

	  [anInternetAddress setPersonal: [MimeUtility decodeHeader: [NSData dataWithCString: cf]
						       charset: [theMessage defaultCharset]]];
	  [anInternetAddress setType: theType];
	  [theMessage addToRecipients: anInternetAddress];

	  RELEASE(anInternetAddress);
	}
      else
	{
	  anInternetAddress = [[InternetAddress alloc] init];

	  [anInternetAddress setPersonal: [MimeUtility decodeHeader: [NSData dataWithCString: nbuf]
						       charset: [theMessage defaultCharset]]];
	  [anInternetAddress setAddress: [NSString stringWithCString: abuf]];
	  [anInternetAddress setType: theType];
	  [theMessage addToRecipients: anInternetAddress];

	  RELEASE(anInternetAddress);
	}
      cf = nf;
    }
}

//
// This method is used to parse the From: header value.
//
+ (void) parseFrom: (NSData *) theLine
	 inMessage: (Message *) theMessage
{
  InternetAddress *anInternetAddress;
  char abuf[128], nbuf[128], *cf, *nf;
  int rc;
  
  if ( !([theLine length] > 6) )
    {
      return;
    }
 
  cf = (char*)[[theLine subdataFromIndex: 6] cString];
  rc = parse_arpa_mailbox(cf, abuf, sizeof(abuf), nbuf, sizeof(nbuf), &nf);

  anInternetAddress = [[InternetAddress alloc] init];

  if (rc < 0)
    {
      [anInternetAddress setPersonal: [MimeUtility decodeHeader: [NSData dataWithCString: cf]
						   charset: [theMessage defaultCharset]]];
    }
  else
    {
      [anInternetAddress setPersonal: [MimeUtility decodeHeader: [NSData dataWithCString: nbuf]
						   charset: [theMessage defaultCharset]]];
      [anInternetAddress setAddress: [NSString stringWithCString: abuf] ];
    }
  
  [theMessage setFrom: anInternetAddress];

  RELEASE(anInternetAddress);
}


//
// This method is used to parse the In-Reply-To: header value.
//
+ (void) parseInReplyTo: (NSData *) theLine
              inMessage: (Message *) theMessage
{
  NSData *aData;

  if ( !([theLine length] > 13) )
    {
      return;
    }

  aData = [theLine subdataFromIndex: 13];
  
  [theMessage setInReplyTo: [aData asciiString]];    
}


//
// This method is used to parse the Message-ID: header value.
//
+ (void) parseMessageID: (NSData *) theLine
	      inMessage: (Message *) theMessage
{
  NSData *aData;

  if ( !([theLine length] > 12) )
    {
      return;
    }

  aData = [theLine subdataFromIndex: 12];
  
  [theMessage setMessageID: [aData asciiString]];    
}


//
// This method is used to parse the MIME-Version: header value.
//
+ (void) parseMimeVersion: (NSData *) theLine
		inMessage: (Message *) theMessage
{
  if ( [theLine length] > 14 )
    {
      [theMessage setMimeVersion: [[theLine subdataFromIndex: 14] asciiString] ];
    }
}


//
// This method is used to parse the References: header value.
//
+ (void) parseReferences: (NSData *) theLine
               inMessage: (Message *) theMessage
{
  if ( [theLine length] > 12 )
    {
      NSMutableArray *aMutableArray;
      NSArray *allReferences;
      int i;

      allReferences = [[theLine subdataFromIndex: 12] componentsSeparatedByCString: " "];

      aMutableArray = [[NSMutableArray alloc] initWithCapacity: [allReferences count]];
      
      for (i = 0; i < [allReferences count]; i++)
	{
	  [aMutableArray addObject: [[allReferences objectAtIndex: i] asciiString]];
	}

      [theMessage setReferences: aMutableArray];
      RELEASE(aMutableArray);
    }
}


//
// This method is used to parse the Reply-To: header value.
//
+ (void) parseReplyTo: (NSData *) theLine
	    inMessage: (Message *) theMessage
{
  InternetAddress *anInternetAddress;
  char abuf[128], nbuf[128], *cf, *nf;
  int rc;
  
  if ( !([theLine length] > 10) )
    {
      return;
    }

  cf = (char*)[[theLine subdataFromIndex: 10] cString];
  rc = parse_arpa_mailbox(cf, abuf, sizeof(abuf), nbuf, sizeof(nbuf), &nf);

  anInternetAddress = [[InternetAddress alloc] init];
  
  if (rc < 0)
    {
      [anInternetAddress setPersonal: [MimeUtility decodeHeader: [NSData dataWithCString: cf]
						   charset: [theMessage defaultCharset]]];
    }
  else
    {
      [anInternetAddress setPersonal: [MimeUtility decodeHeader: [NSData dataWithCString: nbuf]
						   charset: [theMessage defaultCharset]]];
      [anInternetAddress setAddress: [NSString stringWithCString: abuf]];
    }

  [theMessage setReplyTo: anInternetAddress];
  RELEASE(anInternetAddress);
}


//
// This method is used to parse the Resent-From: header value.
//
+ (void) parseResentFrom: (NSData *) theLine
	 inMessage: (Message *) theMessage
{
  InternetAddress *anInternetAddress;
  char abuf[128], nbuf[128], *cf, *nf;
  int rc;
  
  if ( !([theLine length] > 13) )
    {
      return;
    }
  
  cf = (char*)[[theLine subdataFromIndex: 13] cString];
  rc = parse_arpa_mailbox(cf, abuf, sizeof(abuf), nbuf, sizeof(nbuf), &nf);

  anInternetAddress = [[InternetAddress alloc] init];
  
  if (rc < 0)
    {
      [anInternetAddress setPersonal: [MimeUtility decodeHeader: [NSData dataWithCString: cf]
						   charset: [theMessage defaultCharset]]];
    }
  else
    {
      [anInternetAddress setPersonal: [MimeUtility decodeHeader: [NSData dataWithCString: nbuf]
						   charset: [theMessage defaultCharset]]];
      [anInternetAddress setAddress: [NSString stringWithCString: abuf]];
    }
  
  [theMessage setResentFrom: anInternetAddress];

  RELEASE(anInternetAddress);
}


//
// This method is used to parse the Status: header value.
//
+ (void) parseStatus: (NSData *) theLine
	   inMessage: (Message *) theMessage
{
  if ( [theLine length] > 8 )
    {
      [[theMessage flags] addFlagsFromData: [theLine subdataFromIndex: 8]];
      [theMessage addHeader: @"Status"  withValue: [[theLine subdataFromIndex: 8] asciiString]];
    }
}


//
// This method is used to parse the X-Status: header value.
//
+ (void) parseXStatus: (NSData *) theLine
	    inMessage: (Message *) theMessage
{
  if ( [theLine length] > 10 )
    {
      [[theMessage flags] addFlagsFromData: [theLine subdataFromIndex: 10]];
      [theMessage addHeader: @"X-Status"  withValue: [[theLine subdataFromIndex: 10] asciiString]];
    }
}


//
// This method is used to parse the Subject: header value.
//
+ (void) parseSubject: (NSData *) theLine
	    inMessage: (Message *) theMessage
{
  NSString *subject;

  if ( [theLine length] > 9 )
    {
      subject = [MimeUtility decodeHeader: [[theLine subdataFromIndex: 8] dataByTrimmingWhiteSpaces]
			     charset: [theMessage defaultCharset]];
    }
  else
    {
      subject = @"";
    }
  
  [theMessage setSubject: subject];
}


//
// This method is used to parse the headers that we
// don't "support natively".
//
+ (void) parseUnknownHeader: (NSData *) theLine
		  inMessage: (Message *) theMessage
{
  NSData *aName, *aValue;
  NSRange range;

  range = [theLine rangeOfCString: ":"];
  
  if (range.location != NSNotFound)
    {
      aName = [theLine subdataWithRange: NSMakeRange(0, range.location) ];
      
      // we keep only the headers that have a value
      if ( ([theLine length] - range.location - 1) > 0)
	{
	  aValue = [theLine subdataWithRange: NSMakeRange(range.location + 2, [theLine length] -
							    range.location - 2) ];
	  
	  [theMessage addHeader: [aName asciiString] 
		      withValue: [aValue asciiString] ];
	}
    }
}


//
// This method is used to parse the Organization: header value.
//
+ (void) parseOrganization: (NSData *) theLine
		 inMessage: (Message *) theMessage
{
  NSString *organization;

  if ( [theLine length] > 14 )
    {
      organization = [MimeUtility decodeHeader: [[theLine subdataFromIndex: 13] dataByTrimmingWhiteSpaces]
				  charset: [theMessage defaultCharset]];
    }
  else
    {
      organization = @"";
    }
  
  [theMessage setOrganization: organization];    
}


//
// private methods
//
+ (NSData *) _parameterValueUsingLine: (NSData *) theLine
                                range: (NSRange) theRange
{
  NSData *aData;
  NSRange r;
  
  int valueStart, valueEnd;
  
  // The parameter can be quoted or not like this (for example, with a charset):
  // charset="us-ascii"
  // charset = "us-ascii"
  // charset=us-ascii
  // charset = us-ascii
  // It can be terminated by ';' or end of line.
  
  // Look the the first occurrence of ';'.
  // That marks the end of this key value pair.
  // If we don't find one, we set it to the end of the line.
  r = [theLine rangeOfCString: ";"
	       options: 0
	       range: NSMakeRange(theRange.location + theRange.length,
				  [theLine length] - theRange.location - theRange.length)];

  if (r.length > 0)
    {
      valueEnd = r.location - 1;
    }
  else
    {
      valueEnd = [theLine length] - 1;
    }
  
  // Look for the first occurance of '=' before the valueEnd
  // That markes the beginning of the value.
  // If we don't find one, we set the beggining right after the end of the key tag
  r = [theLine rangeOfCString: "=" options: 0
	       range: NSMakeRange(theRange.location + theRange.length,
				  [theLine length] - theRange.location - theRange.length)];

  if (r.length > 0)
    {
      // If "=" was found, but after ";", something is very broken
      // and we just return nil. That can happen if we have a Content-Type like:
      //
      // Content-Type: text/x-patch; name=mpg321-format-string.diff; charset=ISO-8859-1
      //
      // "format" is part of the _name_ parameter. It has nothing to do with format=flowed.
      //
      if ( r.location > valueEnd )
	{
	  return nil;
	}
      valueStart = r.location + r.length;
    }
  else
    {
      valueStart = theRange.location + theRange.length;
    }  

  // We now have a range that should contain our value.
  // Build a NSRange out of it.
  r = NSMakeRange(valueStart, valueEnd - valueStart + 1);
  
  // Grab our substring
  aData = [theLine subdataWithRange: r];
  
  // Trim white spaces
  aData = [aData dataByTrimmingWhiteSpaces];
  
  // If it quoted, grab the stuff in-between.
  aData = [aData dataFromQuotedData];

  return aData;
}

@end