ObjFW  Diff

Differences From Artifact [0bbf55df10]:

To Artifact [826cbee6d8]:


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
- copy
{
	return [self retain];
}

- (BOOL)isAtEndOfStream
{
	if (_cacheLength > 0)
		return NO;

	return [self lowlevelIsAtEndOfStream];
}

- (size_t)readIntoBuffer: (void*)buffer
		  length: (size_t)length
{
	if (_cacheLength == 0)
		return [self lowlevelReadIntoBuffer: buffer
					     length: length];

	if (length >= _cacheLength) {
		size_t ret = _cacheLength;
		memcpy(buffer, _cache, _cacheLength);

		[self freeMemory: _cache];
		_cache = NULL;
		_cacheLength = 0;

		return ret;
	} else {


		char *tmp = [self allocMemoryWithSize: _cacheLength - length];
		memcpy(tmp, _cache + length, _cacheLength - length);

		memcpy(buffer, _cache, length);

		[self freeMemory: _cache];
		_cache = tmp;
		_cacheLength -= length;

		return length;
	}
}

- (void)readIntoBuffer: (void*)buffer
	   exactLength: (size_t)length







|








|



|
|
|

|
|
|



>
>
|
|

|

|
|
|







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
- copy
{
	return [self retain];
}

- (BOOL)isAtEndOfStream
{
	if (_readBufferLength > 0)
		return NO;

	return [self lowlevelIsAtEndOfStream];
}

- (size_t)readIntoBuffer: (void*)buffer
		  length: (size_t)length
{
	if (_readBufferLength == 0)
		return [self lowlevelReadIntoBuffer: buffer
					     length: length];

	if (length >= _readBufferLength) {
		size_t ret = _readBufferLength;
		memcpy(buffer, _readBuffer, _readBufferLength);

		[self freeMemory: _readBuffer];
		_readBuffer = NULL;
		_readBufferLength = 0;

		return ret;
	} else {
		char *tmp;

		tmp = [self allocMemoryWithSize: _readBufferLength - length];
		memcpy(tmp, _readBuffer + length, _readBufferLength - length);

		memcpy(buffer, _readBuffer, length);

		[self freeMemory: _readBuffer];
		_readBuffer = tmp;
		_readBufferLength -= length;

		return length;
	}
}

- (void)readIntoBuffer: (void*)buffer
	   exactLength: (size_t)length
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

	return ret;
}

- (OFString*)tryReadLineWithEncoding: (of_string_encoding_t)encoding
{
	size_t i, pageSize, bufferLength, retLength;
	char *retCString, *buffer, *newCache;
	OFString *ret;

	/* Look if there's a line or \0 in our cache */
	if (!_waitingForDelimiter && _cache != NULL) {
		for (i = 0; i < _cacheLength; i++) {
			if OF_UNLIKELY (_cache[i] == '\n' ||
			    _cache[i] == '\0') {
				retLength = i;

				if (i > 0 && _cache[i - 1] == '\r')
					retLength--;

				ret = [OFString stringWithCString: _cache
							 encoding: encoding
							   length: retLength];

				newCache = [self
				    allocMemoryWithSize: _cacheLength - i - 1];
				if (newCache != NULL)
					memcpy(newCache, _cache + i + 1,
					    _cacheLength - i - 1);

				[self freeMemory: _cache];
				_cache = newCache;
				_cacheLength -= i + 1;

				_waitingForDelimiter = NO;
				return ret;
			}
		}
	}

	/* Read and see if we got a newline or \0 */
	pageSize = [OFSystemInfo pageSize];
	buffer = [self allocMemoryWithSize: pageSize];

	@try {
		if ([self lowlevelIsAtEndOfStream]) {
			if (_cache == NULL) {
				_waitingForDelimiter = NO;
				return nil;
			}

			retLength = _cacheLength;

			if (retLength > 0 && _cache[retLength - 1] == '\r')
				retLength--;

			ret = [OFString stringWithCString: _cache
						 encoding: encoding
						   length: retLength];

			[self freeMemory: _cache];
			_cache = NULL;
			_cacheLength = 0;

			_waitingForDelimiter = NO;
			return ret;
		}

		bufferLength = [self lowlevelReadIntoBuffer: buffer
						     length: pageSize];

		/* Look if there's a newline or \0 */
		for (i = 0; i < bufferLength; i++) {
			if OF_UNLIKELY (buffer[i] == '\n' ||
			    buffer[i] == '\0') {
				retLength = _cacheLength + i;
				retCString = [self
				    allocMemoryWithSize: retLength];

				if (_cache != NULL)
					memcpy(retCString, _cache,
					    _cacheLength);
				memcpy(retCString + _cacheLength, buffer, i);


				if (retLength > 0 &&
				    retCString[retLength - 1] == '\r')
					retLength--;

				@try {
					char *rcs = retCString;
					size_t rl = retLength;

					ret = [OFString
					    stringWithCString: rcs
						     encoding: encoding
						       length: rl];
				} @catch (id e) {
					/*
					 * Append data to cache to prevent loss
					 * of data due to wrong encoding.
					 */
					_cache = [self
					    resizeMemory: _cache
						    size: _cacheLength +
							  bufferLength];

					if (_cache != NULL)
						memcpy(_cache + _cacheLength,

						    buffer, bufferLength);

					_cacheLength += bufferLength;

					@throw e;
				} @finally {
					[self freeMemory: retCString];
				}

				newCache = [self allocMemoryWithSize:
				    bufferLength - i - 1];
				if (newCache != NULL)
					memcpy(newCache, buffer + i + 1,
					    bufferLength - i - 1);

				[self freeMemory: _cache];
				_cache = newCache;
				_cacheLength = bufferLength - i - 1;

				_waitingForDelimiter = NO;
				return ret;
			}
		}

		/* There was no newline or \0 */
		_cache = [self resizeMemory: _cache

				       size: _cacheLength + bufferLength];

		/*
		 * It's possible that _cacheLength + bufferLength is 0 and thus
		 * _cache was set to NULL by resizeMemory:size:.
		 */
		if (_cache != NULL)

			memcpy(_cache + _cacheLength, buffer, bufferLength);

		_cacheLength += bufferLength;
	} @finally {
		[self freeMemory: buffer];
	}

	_waitingForDelimiter = YES;
	return nil;
}







|


|
|
|
|
|


|


|



|
|
|
|
|

|
|
|













|




|

|


|



|
|
|












|



|
|
|
|
>















|
|

|
|
|


|
|
>


|






|
|
|
|


|
|
|







|
>
|


|
|

|
>
|

|







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

	return ret;
}

- (OFString*)tryReadLineWithEncoding: (of_string_encoding_t)encoding
{
	size_t i, pageSize, bufferLength, retLength;
	char *retCString, *buffer, *readBuffer;
	OFString *ret;

	/* Look if there's a line or \0 in our buffer */
	if (!_waitingForDelimiter && _readBuffer != NULL) {
		for (i = 0; i < _readBufferLength; i++) {
			if OF_UNLIKELY (_readBuffer[i] == '\n' ||
			    _readBuffer[i] == '\0') {
				retLength = i;

				if (i > 0 && _readBuffer[i - 1] == '\r')
					retLength--;

				ret = [OFString stringWithCString: _readBuffer
							 encoding: encoding
							   length: retLength];

				readBuffer = [self allocMemoryWithSize:
				    _readBufferLength - i - 1];
				if (readBuffer != NULL)
					memcpy(readBuffer, _readBuffer + i + 1,
					    _readBufferLength - i - 1);

				[self freeMemory: _readBuffer];
				_readBuffer = readBuffer;
				_readBufferLength -= i + 1;

				_waitingForDelimiter = NO;
				return ret;
			}
		}
	}

	/* Read and see if we got a newline or \0 */
	pageSize = [OFSystemInfo pageSize];
	buffer = [self allocMemoryWithSize: pageSize];

	@try {
		if ([self lowlevelIsAtEndOfStream]) {
			if (_readBuffer == NULL) {
				_waitingForDelimiter = NO;
				return nil;
			}

			retLength = _readBufferLength;

			if (retLength > 0 && _readBuffer[retLength - 1] == '\r')
				retLength--;

			ret = [OFString stringWithCString: _readBuffer
						 encoding: encoding
						   length: retLength];

			[self freeMemory: _readBuffer];
			_readBuffer = NULL;
			_readBufferLength = 0;

			_waitingForDelimiter = NO;
			return ret;
		}

		bufferLength = [self lowlevelReadIntoBuffer: buffer
						     length: pageSize];

		/* Look if there's a newline or \0 */
		for (i = 0; i < bufferLength; i++) {
			if OF_UNLIKELY (buffer[i] == '\n' ||
			    buffer[i] == '\0') {
				retLength = _readBufferLength + i;
				retCString = [self
				    allocMemoryWithSize: retLength];

				if (_readBuffer != NULL)
					memcpy(retCString, _readBuffer,
					    _readBufferLength);
				memcpy(retCString + _readBufferLength,
				    buffer, i);

				if (retLength > 0 &&
				    retCString[retLength - 1] == '\r')
					retLength--;

				@try {
					char *rcs = retCString;
					size_t rl = retLength;

					ret = [OFString
					    stringWithCString: rcs
						     encoding: encoding
						       length: rl];
				} @catch (id e) {
					/*
					 * Append data to readBuffer to prevent
					 * loss of data due to wrong encoding.
					 */
					_readBuffer = [self
					    resizeMemory: _readBuffer
						    size: _readBufferLength +
							  bufferLength];

					if (_readBuffer != NULL)
						memcpy(_readBuffer +
						    _readBufferLength,
						    buffer, bufferLength);

					_readBufferLength += bufferLength;

					@throw e;
				} @finally {
					[self freeMemory: retCString];
				}

				readBuffer = [self
				    allocMemoryWithSize: bufferLength - i - 1];
				if (readBuffer != NULL)
					memcpy(readBuffer, buffer + i + 1,
					    bufferLength - i - 1);

				[self freeMemory: _readBuffer];
				_readBuffer = readBuffer;
				_readBufferLength = bufferLength - i - 1;

				_waitingForDelimiter = NO;
				return ret;
			}
		}

		/* There was no newline or \0 */
		_readBuffer = [self resizeMemory: _readBuffer
					    size: _readBufferLength +
						  bufferLength];

		/*
		 * It's possible that _readBufferLength + bufferLength is 0 and
		 * thus _readBuffer was set to NULL by resizeMemory:size:.
		 */
		if (_readBuffer != NULL)
			memcpy(_readBuffer + _readBufferLength,
			    buffer, bufferLength);

		_readBufferLength += bufferLength;
	} @finally {
		[self freeMemory: buffer];
	}

	_waitingForDelimiter = YES;
	return nil;
}
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856

857
858
859
860
861

862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894

895
896
897
898
899
900
901

902
903
904
905
906
907
908
909
910
911
}

- (OFString*)tryReadTillDelimiter: (OFString*)delimiter
			 encoding: (of_string_encoding_t)encoding
{
	const char *delimiterCString;
	size_t i, j, delimiterLength, pageSize, bufferLength, retLength;
	char *retCString, *buffer, *newCache;
	OFString *ret;

	delimiterCString = [delimiter cStringWithEncoding: encoding];
	delimiterLength = [delimiter cStringLengthWithEncoding: encoding];
	j = 0;

	if (delimiterLength == 0)
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	/* Look if there's something in our cache */
	if (!_waitingForDelimiter && _cache != NULL) {
		for (i = 0; i < _cacheLength; i++) {
			if (_cache[i] != delimiterCString[j++])
				j = 0;

			if (j == delimiterLength || _cache[i] == '\0') {
				if (_cache[i] == '\0')
					delimiterLength = 1;

				ret = [OFString
				    stringWithCString: _cache
					     encoding: encoding
					      length: i + 1 - delimiterLength];

				newCache = [self allocMemoryWithSize:
				    _cacheLength - i - 1];
				if (newCache != NULL)
					memcpy(newCache, _cache + i + 1,
					    _cacheLength - i - 1);

				[self freeMemory: _cache];
				_cache = newCache;
				_cacheLength -= i + 1;

				_waitingForDelimiter = NO;
				return ret;
			}
		}
	}

	/* Read and see if we got a delimiter or \0 */
	pageSize = [OFSystemInfo pageSize];
	buffer = [self allocMemoryWithSize: pageSize];

	@try {
		if ([self lowlevelIsAtEndOfStream]) {
			if (_cache == NULL) {
				_waitingForDelimiter = NO;
				return nil;
			}

			ret = [OFString stringWithCString: _cache
						 encoding: encoding
						   length: _cacheLength];

			[self freeMemory: _cache];
			_cache = NULL;
			_cacheLength = 0;

			_waitingForDelimiter = NO;
			return ret;
		}

		bufferLength = [self lowlevelReadIntoBuffer: buffer
						     length: pageSize];

		/* Look if there's a delimiter or \0 */
		for (i = 0; i < bufferLength; i++) {
			if (buffer[i] != delimiterCString[j++])
				j = 0;

			if (j == delimiterLength || buffer[i] == '\0') {
				if (buffer[i] == '\0')
					delimiterLength = 1;

				retLength = _cacheLength + i + 1 -
				    delimiterLength;
				retCString = [self
				    allocMemoryWithSize: retLength];


				if (_cache != NULL && _cacheLength <= retLength)
					memcpy(retCString, _cache,
					    _cacheLength);
				else if (_cache != NULL)
					memcpy(retCString, _cache, retLength);

				if (i >= delimiterLength)
					memcpy(retCString + _cacheLength,
					    buffer, i + 1 - delimiterLength);

				@try {
					char *rcs = retCString;
					size_t rl = retLength;

					ret = [OFString
					    stringWithCString: rcs
						     encoding: encoding
						       length: rl];
				} @finally {
					[self freeMemory: retCString];
				}

				newCache = [self allocMemoryWithSize:
				    bufferLength - i - 1];
				if (newCache != NULL)
					memcpy(newCache, buffer + i + 1,
					    bufferLength - i - 1);

				[self freeMemory: _cache];
				_cache = newCache;
				_cacheLength = bufferLength - i - 1;

				_waitingForDelimiter = NO;
				return ret;
			}
		}

		/* Neither the delimiter nor \0 was found */
		_cache = [self resizeMemory: _cache

				       size: _cacheLength + bufferLength];

		/*
		 * It's possible that _cacheLength + bufferLength is 0 and thus
		 * _cache was set to NULL by resizeMemory:size:.
		 */
		if (_cache != NULL)

			memcpy(_cache + _cacheLength, buffer, bufferLength);

		_cacheLength += bufferLength;
	} @finally {
		[self freeMemory: buffer];
	}

	_waitingForDelimiter = YES;
	return nil;
}







|











|
|
|
|


|
|



|



|
|
|
|
|

|
|
|













|




|

|

|
|
|

















|




>
|
|
|
|
|
>

|














|

|
|


|
|
|







|
>
|


|
|

|
>
|

|







773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
}

- (OFString*)tryReadTillDelimiter: (OFString*)delimiter
			 encoding: (of_string_encoding_t)encoding
{
	const char *delimiterCString;
	size_t i, j, delimiterLength, pageSize, bufferLength, retLength;
	char *retCString, *buffer, *readBuffer;
	OFString *ret;

	delimiterCString = [delimiter cStringWithEncoding: encoding];
	delimiterLength = [delimiter cStringLengthWithEncoding: encoding];
	j = 0;

	if (delimiterLength == 0)
		@throw [OFInvalidArgumentException
		    exceptionWithClass: [self class]
			      selector: _cmd];

	/* Look if there's something in our buffer */
	if (!_waitingForDelimiter && _readBuffer != NULL) {
		for (i = 0; i < _readBufferLength; i++) {
			if (_readBuffer[i] != delimiterCString[j++])
				j = 0;

			if (j == delimiterLength || _readBuffer[i] == '\0') {
				if (_readBuffer[i] == '\0')
					delimiterLength = 1;

				ret = [OFString
				    stringWithCString: _readBuffer
					     encoding: encoding
					      length: i + 1 - delimiterLength];

				readBuffer = [self allocMemoryWithSize:
				    _readBufferLength - i - 1];
				if (readBuffer != NULL)
					memcpy(readBuffer, _readBuffer + i + 1,
					    _readBufferLength - i - 1);

				[self freeMemory: _readBuffer];
				_readBuffer = readBuffer;
				_readBufferLength -= i + 1;

				_waitingForDelimiter = NO;
				return ret;
			}
		}
	}

	/* Read and see if we got a delimiter or \0 */
	pageSize = [OFSystemInfo pageSize];
	buffer = [self allocMemoryWithSize: pageSize];

	@try {
		if ([self lowlevelIsAtEndOfStream]) {
			if (_readBuffer == NULL) {
				_waitingForDelimiter = NO;
				return nil;
			}

			ret = [OFString stringWithCString: _readBuffer
						 encoding: encoding
						   length: _readBufferLength];

			[self freeMemory: _readBuffer];
			_readBuffer = NULL;
			_readBufferLength = 0;

			_waitingForDelimiter = NO;
			return ret;
		}

		bufferLength = [self lowlevelReadIntoBuffer: buffer
						     length: pageSize];

		/* Look if there's a delimiter or \0 */
		for (i = 0; i < bufferLength; i++) {
			if (buffer[i] != delimiterCString[j++])
				j = 0;

			if (j == delimiterLength || buffer[i] == '\0') {
				if (buffer[i] == '\0')
					delimiterLength = 1;

				retLength = _readBufferLength + i + 1 -
				    delimiterLength;
				retCString = [self
				    allocMemoryWithSize: retLength];

				if (_readBuffer != NULL &&
				    _readBufferLength <= retLength)
					memcpy(retCString, _readBuffer,
					    _readBufferLength);
				else if (_readBuffer != NULL)
					memcpy(retCString, _readBuffer,
					    retLength);
				if (i >= delimiterLength)
					memcpy(retCString + _readBufferLength,
					    buffer, i + 1 - delimiterLength);

				@try {
					char *rcs = retCString;
					size_t rl = retLength;

					ret = [OFString
					    stringWithCString: rcs
						     encoding: encoding
						       length: rl];
				} @finally {
					[self freeMemory: retCString];
				}

				readBuffer = [self allocMemoryWithSize:
				    bufferLength - i - 1];
				if (readBuffer != NULL)
					memcpy(readBuffer, buffer + i + 1,
					    bufferLength - i - 1);

				[self freeMemory: _readBuffer];
				_readBuffer = readBuffer;
				_readBufferLength = bufferLength - i - 1;

				_waitingForDelimiter = NO;
				return ret;
			}
		}

		/* Neither the delimiter nor \0 was found */
		_readBuffer = [self resizeMemory: _readBuffer
					    size: _readBufferLength +
						  bufferLength];

		/*
		 * It's possible that _readBufferLength + bufferLength is 0 and
		 * thus _readBuffer was set to NULL by resizeMemory:size:.
		 */
		if (_readBuffer != NULL)
			memcpy(_readBuffer + _readBufferLength,
			    buffer, bufferLength);

		_readBufferLength += bufferLength;
	} @finally {
		[self freeMemory: buffer];
	}

	_waitingForDelimiter = YES;
	return nil;
}
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
	} @finally {
		free(UTF8String);
	}

	return length;
}

- (size_t)pendingBytes
{
	return _cacheLength;
}

- (BOOL)isBlocking
{
	return _blocking;
}








|

|







1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
	} @finally {
		free(UTF8String);
	}

	return length;
}

- (size_t)numberOfBytesInReadBuffer
{
	return _readBufferLength;
}

- (BOOL)isBlocking
{
	return _blocking;
}