@@ -507,7 +507,7 @@ PHP_FUNCTION(file_get_contents)
507
507
char * contents ;
508
508
zend_bool use_include_path = 0 ;
509
509
php_stream * stream ;
510
- int len , newlen ;
510
+ int len ;
511
511
long offset = -1 ;
512
512
long maxlen = PHP_STREAM_COPY_ALL ;
513
513
zval * zcontext = NULL ;
@@ -555,7 +555,7 @@ PHP_FUNCTION(file_put_contents)
555
555
char * filename ;
556
556
int filename_len ;
557
557
zval * data ;
558
- int numbytes = 0 ;
558
+ int numchars = 0 ;
559
559
long flags = 0 ;
560
560
zval * zcontext = NULL ;
561
561
php_stream_context * context = NULL ;
@@ -592,52 +592,53 @@ PHP_FUNCTION(file_put_contents)
592
592
php_stream * srcstream ;
593
593
php_stream_from_zval (srcstream , & data );
594
594
595
- numbytes = php_stream_copy_to_stream (srcstream , stream , PHP_STREAM_COPY_ALL );
595
+ numchars = php_stream_copy_to_stream (srcstream , stream , PHP_STREAM_COPY_ALL );
596
596
597
597
break ;
598
598
}
599
599
case IS_ARRAY :
600
600
if (zend_hash_num_elements (Z_ARRVAL_P (data ))) {
601
- int bytes_written ;
602
601
zval * * tmp ;
603
602
HashPosition pos ;
604
603
605
604
zend_hash_internal_pointer_reset_ex (Z_ARRVAL_P (data ), & pos );
606
605
while (zend_hash_get_current_data_ex (Z_ARRVAL_P (data ), (void * * ) & tmp , & pos ) == SUCCESS ) {
607
606
if (Z_TYPE_PP (tmp ) == IS_UNICODE ) {
608
- int wrote_bytes = php_stream_u_write (stream , Z_USTRVAL_PP (tmp ), Z_USTRLEN_PP (tmp ));
609
- if (wrote_bytes < 0 ) {
610
- php_error_docref (NULL TSRMLS_CC , E_WARNING , "Failed to write %d characters to %s" , Z_USTRLEN_PP (tmp ), filename );
611
- numbytes = -1 ;
607
+ int ustrlen = u_countChar32 (Z_USTRVAL_PP (tmp ), Z_USTRLEN_PP (tmp ));
608
+ int wrote_u16 = php_stream_write_unicode (stream , Z_USTRVAL_PP (tmp ), Z_USTRLEN_PP (tmp ));
609
+ if (wrote_u16 < 0 ) {
610
+ php_error_docref (NULL TSRMLS_CC , E_WARNING , "Failed to write %d characters to %s" , ustrlen , filename );
611
+ numchars = -1 ;
612
612
break ;
613
- } else if (wrote_bytes != UBYTES (Z_USTRLEN_PP (tmp ))) {
614
- int ustrlen = u_countChar32 (Z_USTRVAL_PP (tmp ), Z_USTRLEN_PP (tmp ));
615
- int numchars = u_countChar32 (Z_USTRVAL_PP (tmp ), wrote_bytes / UBYTES (1 ));
613
+ } else if (wrote_u16 != Z_USTRLEN_PP (tmp )) {
614
+ int numchars = u_countChar32 (Z_USTRVAL_PP (tmp ), wrote_u16 );
616
615
617
616
php_error_docref (NULL TSRMLS_CC , E_WARNING , "Only %d of %d characters written, possibly out of free disk space" , numchars , ustrlen );
618
- numbytes = -1 ;
617
+ numchars = -1 ;
619
618
break ;
620
619
}
621
- numbytes += wrote_bytes ;
620
+ numchars += ustrlen ;
622
621
} else { /* non-unicode */
623
622
int free_val = 0 ;
624
623
zval strval = * * tmp ;
624
+ int wrote_bytes ;
625
625
626
626
if (Z_TYPE (strval ) != IS_STRING ) {
627
627
zval_copy_ctor (& strval );
628
628
convert_to_string (& strval );
629
629
free_val = 1 ;
630
630
}
631
631
if (Z_STRLEN (strval )) {
632
- numbytes += Z_STRLEN (strval );
633
- bytes_written = php_stream_write (stream , Z_STRVAL (strval ), Z_STRLEN (strval ));
634
- if (bytes_written < 0 || bytes_written != Z_STRLEN (strval )) {
635
- if (bytes_written < 0 ) {
636
- php_error_docref (NULL TSRMLS_CC , E_WARNING , "Failed to write %d bytes to %s" , Z_STRLEN (strval ), filename );
637
- } else {
638
- php_error_docref (NULL TSRMLS_CC , E_WARNING , "Only %d of %d bytes written, possibly out of free disk space" , bytes_written , Z_STRLEN (strval ));
639
- }
640
- numbytes = -1 ;
632
+ numchars += Z_STRLEN (strval );
633
+ wrote_bytes = php_stream_write (stream , Z_STRVAL (strval ), Z_STRLEN (strval ));
634
+ if (wrote_bytes < 0 ) {
635
+ php_error_docref (NULL TSRMLS_CC , E_WARNING , "Failed to write %d bytes to %s" , Z_STRLEN (strval ), filename );
636
+ numchars = -1 ;
637
+ break ;
638
+ }
639
+ if (wrote_bytes != Z_STRLEN (strval )) {
640
+ php_error_docref (NULL TSRMLS_CC , E_WARNING , "Only %d of %d bytes written, possibly out of free disk space" , wrote_bytes , Z_STRLEN (strval ));
641
+ numchars = -1 ;
641
642
break ;
642
643
}
643
644
}
@@ -652,20 +653,20 @@ PHP_FUNCTION(file_put_contents)
652
653
case IS_OBJECT :
653
654
/* TODO */
654
655
php_error_docref (NULL TSRMLS_CC , E_WARNING , "2nd parameter must be non-object (for now)" );
655
- numbytes = -1 ;
656
+ numchars = -1 ;
656
657
break ;
657
658
case IS_UNICODE :
658
659
if (Z_USTRLEN_P (data )) {
659
- numbytes = php_stream_u_write ( stream , Z_USTRVAL_P (data ), Z_USTRLEN_P (data ));
660
- if ( numbytes < 0 ) {
661
- php_error_docref ( NULL TSRMLS_CC , E_WARNING , "Failed to write %d characters to %s" , Z_USTRLEN_P ( data ), filename );
662
- numbytes = -1 ;
663
- } else if ( numbytes != UBYTES ( Z_USTRLEN_P ( data ))) {
664
- int ustrlen = u_countChar32 ( Z_USTRVAL_P ( data ), Z_USTRLEN_P (data ));
665
- int numchars = u_countChar32 (Z_USTRVAL_P (data ), numbytes / UBYTES ( 1 ) );
660
+ int ustrlen = u_countChar32 ( Z_USTRVAL_P (data ), Z_USTRLEN_P (data ));
661
+ numchars = php_stream_write_unicode ( stream , Z_USTRVAL_P ( data ), Z_USTRLEN_P ( data ));
662
+ if ( numchars < 0 ) {
663
+ php_error_docref ( NULL TSRMLS_CC , E_WARNING , "Failed to write %d characters to %s" , ustrlen , filename ) ;
664
+ numchars = -1 ;
665
+ } else if ( numchars != UBYTES ( Z_USTRLEN_P (data ))) {
666
+ int numchars = u_countChar32 (Z_USTRVAL_P (data ), numchars );
666
667
667
668
php_error_docref (NULL TSRMLS_CC , E_WARNING , "Only %d of %d characters written, possibly out of free disk space" , numchars , ustrlen );
668
- numbytes = -1 ;
669
+ numchars = -1 ;
669
670
}
670
671
}
671
672
break ;
@@ -680,22 +681,22 @@ PHP_FUNCTION(file_put_contents)
680
681
convert_to_string_ex (& data );
681
682
}
682
683
if (Z_STRLEN_P (data )) {
683
- numbytes = php_stream_write (stream , Z_STRVAL_P (data ), Z_STRLEN_P (data ));
684
- if (numbytes != Z_STRLEN_P (data )) {
685
- php_error_docref (NULL TSRMLS_CC , E_WARNING , "Only %d of %d bytes written, possibly out of free disk space" , numbytes , Z_STRLEN_P (data ));
686
- numbytes = -1 ;
684
+ numchars = php_stream_write (stream , Z_STRVAL_P (data ), Z_STRLEN_P (data ));
685
+ if (numchars != Z_STRLEN_P (data )) {
686
+ php_error_docref (NULL TSRMLS_CC , E_WARNING , "Only %d of %d bytes written, possibly out of free disk space" , numchars , Z_STRLEN_P (data ));
687
+ numchars = -1 ;
687
688
}
688
689
}
689
690
break ;
690
691
691
692
}
692
693
php_stream_close (stream );
693
694
694
- if (numbytes < 0 ) {
695
+ if (numchars < 0 ) {
695
696
RETURN_FALSE ;
696
697
}
697
698
698
- RETURN_LONG (numbytes );
699
+ RETURN_LONG (numchars );
699
700
}
700
701
/* }}} */
701
702
@@ -709,9 +710,9 @@ PHP_FUNCTION(file)
709
710
{
710
711
char * filename ;
711
712
int filename_len ;
712
- char * slashed , * target_buf = NULL , * p , * s , * e ;
713
+ char * target_buf = NULL , * p , * s , * e ;
713
714
register int i = 0 ;
714
- int target_len , len ;
715
+ int target_len ;
715
716
char eol_marker = '\n' ;
716
717
long flags = 0 ;
717
718
zend_bool use_include_path ;
@@ -748,7 +749,7 @@ PHP_FUNCTION(file)
748
749
s = target_buf ;
749
750
e = target_buf + target_len ;
750
751
751
- if (!(p = php_stream_locate_eol (stream , target_buf , target_len TSRMLS_CC ))) {
752
+ if (!(p = php_stream_locate_eol (stream , ( zstr ) target_buf , target_len TSRMLS_CC ))) {
752
753
p = e ;
753
754
goto parse_eol ;
754
755
}
@@ -1024,29 +1025,28 @@ PHPAPI PHP_FUNCTION(fgets)
1024
1025
PHPAPI PHP_FUNCTION (fgetc )
1025
1026
{
1026
1027
zval * * arg1 ;
1027
- char buf [2 * sizeof (UChar )];
1028
- int is_unicode ;
1029
1028
php_stream * stream ;
1030
- int32_t num_bytes = UBYTES (2 ), num_chars = 1 ;
1031
1029
1032
1030
if (ZEND_NUM_ARGS () != 1 || zend_get_parameters_ex (1 , & arg1 ) == FAILURE ) {
1033
1031
WRONG_PARAM_COUNT ;
1034
1032
}
1035
1033
1036
1034
PHP_STREAM_TO_ZVAL (stream , arg1 );
1037
1035
1038
- if (!php_stream_u_read (stream , buf , & num_bytes , & num_chars , & is_unicode )) {
1039
- RETVAL_FALSE ;
1040
- } else {
1041
- if (is_unicode ) {
1042
- UChar * ubuf = (UChar * )buf ;
1043
- int num_u16 = num_bytes >> 1 ;
1044
- ubuf [num_u16 ] = 0 ;
1045
- RETURN_UNICODEL (ubuf , num_u16 , 1 );
1046
- } else {
1047
- buf [1 ] = 0 ;
1048
- RETURN_STRINGL (buf , 1 , 1 );
1036
+ if (php_stream_reads_unicode (stream )) {
1037
+ int buflen = 1 ;
1038
+ UChar * buf = php_stream_read_unicode_chars (stream , & buflen );
1039
+
1040
+ if (!buf ) {
1041
+ RETURN_FALSE ;
1049
1042
}
1043
+ RETURN_UNICODEL (buf , buflen , 0 );
1044
+ } else {
1045
+ char buf [2 ];
1046
+
1047
+ buf [0 ] = php_stream_getc (stream );
1048
+ buf [1 ] = 0 ;
1049
+ RETURN_STRINGL (buf , 1 , 1 );
1050
1050
}
1051
1051
}
1052
1052
/* }}} */
@@ -1213,26 +1213,19 @@ PHPAPI PHP_FUNCTION(fwrite)
1213
1213
if (write_len < 0 || write_len > Z_USTRLEN_P (zstring )) {
1214
1214
write_len = Z_USTRLEN_P (zstring );
1215
1215
}
1216
- ret = php_stream_u_write (stream , Z_USTRVAL_P (zstring ), write_len );
1216
+ ret = php_stream_write_unicode (stream , Z_USTRVAL_P (zstring ), write_len );
1217
1217
1218
1218
/* Convert data points back to code units */
1219
1219
if (ret > 0 ) {
1220
1220
ret = u_countChar32 (Z_USTRVAL_P (zstring ), ret );
1221
1221
}
1222
1222
} else {
1223
- char * buffer = NULL ;
1224
- int num_bytes ;
1225
-
1226
1223
convert_to_string (zstring );
1227
1224
if (write_len < 0 || write_len > Z_STRLEN_P (zstring )) {
1228
1225
write_len = Z_STRLEN_P (zstring );
1229
1226
}
1230
1227
1231
- num_bytes = write_len ;
1232
- ret = php_stream_write (stream , buffer ? buffer : Z_STRVAL_P (zstring ), num_bytes );
1233
- if (buffer ) {
1234
- efree (buffer );
1235
- }
1228
+ ret = php_stream_write (stream , Z_STRVAL_P (zstring ), write_len );
1236
1229
}
1237
1230
1238
1231
RETURN_LONG (ret );
@@ -1759,11 +1752,8 @@ PHPAPI int php_copy_file(char *src, char *dest TSRMLS_DC)
1759
1752
PHPAPI PHP_FUNCTION (fread )
1760
1753
{
1761
1754
zval * zstream ;
1762
- char * buf ;
1763
1755
long len ;
1764
1756
php_stream * stream ;
1765
- int is_unicode ;
1766
- int32_t num_bytes , num_chars ;
1767
1757
1768
1758
if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC , "rl" , & zstream , & len ) == FAILURE ) {
1769
1759
RETURN_NULL ();
@@ -1776,22 +1766,25 @@ PHPAPI PHP_FUNCTION(fread)
1776
1766
RETURN_FALSE ;
1777
1767
}
1778
1768
1779
- num_chars = len ;
1780
- num_bytes = UBYTES ( len ) ;
1781
- buf = emalloc ( num_bytes + UBYTES ( 1 ) );
1769
+ if ( php_stream_reads_unicode ( stream )) {
1770
+ int buflen = len ;
1771
+ UChar * buf = php_stream_read_unicode_chars ( stream , & buflen );
1782
1772
1783
- if (!php_stream_u_read (stream , buf , & num_bytes , & num_chars , & is_unicode )) {
1784
- efree (buf );
1785
- RETURN_FALSE ;
1786
- }
1773
+ if (!buf ) {
1774
+ RETURN_FALSE ;
1775
+ }
1787
1776
1788
- if (is_unicode ) {
1789
- buf [num_bytes ] = 0 ;
1790
- buf [num_bytes + 1 ] = 0 ;
1791
- RETURN_UNICODEL ((UChar * )buf , num_bytes >> 1 , 0 );
1777
+ RETURN_UNICODEL (buf , buflen , 0 );
1792
1778
} else {
1793
- buf [num_bytes ] = 0 ;
1794
- RETURN_STRINGL (buf , num_bytes , 0 );
1779
+ char * buf = emalloc (len + 1 );
1780
+ int buflen = php_stream_read (stream , buf , len );
1781
+
1782
+ if (!buflen ) {
1783
+ efree (buf );
1784
+ RETURN_FALSE ;
1785
+ }
1786
+ buf [buflen ] = 0 ;
1787
+ RETURN_STRINGL (buf , buflen , 0 );
1795
1788
}
1796
1789
}
1797
1790
/* }}} */
0 commit comments