@@ -202,24 +202,28 @@ def readable(self):
202
202
# Open streams are currently always readable.
203
203
return not self .closed
204
204
205
- def read (self , b = None ):
205
+ def read (self , b = - 1 ):
206
206
"""Returns either the requested number of bytes or the entire stream.
207
207
208
208
:param int b: Number of bytes to read
209
209
:returns: Processed (encrypted or decrypted) bytes from source stream
210
210
:rtype: bytes
211
211
"""
212
212
# Any negative value for b is interpreted as a full read
213
- if b is not None and b < 0 :
214
- b = None
213
+ # None is also accepted for legacy compatibility
214
+ if b is None or b < 0 :
215
+ b = - 1
215
216
216
217
_LOGGER .debug ("Stream read called, requesting %s bytes" , b )
217
218
output = io .BytesIO ()
219
+
218
220
if not self ._message_prepped :
219
221
self ._prep_message ()
222
+
220
223
if self .closed :
221
224
raise ValueError ("I/O operation on closed file" )
222
- if b :
225
+
226
+ if b >= 0 :
223
227
self ._read_bytes (b )
224
228
output .write (self .output_buffer [:b ])
225
229
self .output_buffer = self .output_buffer [b :]
@@ -228,6 +232,7 @@ def read(self, b=None):
228
232
self ._read_bytes (LINE_LENGTH )
229
233
output .write (self .output_buffer )
230
234
self .output_buffer = b""
235
+
231
236
self .bytes_read += output .tell ()
232
237
_LOGGER .debug ("Returning %s bytes of %s bytes requested" , output .tell (), b )
233
238
return output .getvalue ()
@@ -511,14 +516,18 @@ def _read_bytes_to_non_framed_body(self, b):
511
516
_LOGGER .debug ("Closing encryptor after receiving only %s bytes of %s bytes requested" , plaintext , b )
512
517
self .source_stream .close ()
513
518
closing = self .encryptor .finalize ()
519
+
514
520
if self .signer is not None :
515
521
self .signer .update (closing )
522
+
516
523
closing += aws_encryption_sdk .internal .formatting .serialize .serialize_non_framed_close (
517
524
tag = self .encryptor .tag , signer = self .signer
518
525
)
526
+
519
527
if self .signer is not None :
520
528
closing += aws_encryption_sdk .internal .formatting .serialize .serialize_footer (self .signer )
521
529
return ciphertext + closing
530
+
522
531
return ciphertext
523
532
524
533
def _read_bytes_to_framed_body (self , b ):
@@ -530,14 +539,22 @@ def _read_bytes_to_framed_body(self, b):
530
539
"""
531
540
_LOGGER .debug ("collecting %s bytes" , b )
532
541
_b = b
533
- b = int (math .ceil (b / float (self .config .frame_length )) * self .config .frame_length )
534
- _LOGGER .debug ("%s bytes requested; reading %s bytes after normalizing to frame length" , _b , b )
542
+
543
+ if b > 0 :
544
+ _frames_to_read = math .ceil (b / float (self .config .frame_length ))
545
+ b = int (_frames_to_read * self .config .frame_length )
546
+ _LOGGER .debug ("%d bytes requested; reading %d bytes after normalizing to frame length" , _b , b )
547
+
535
548
plaintext = self .source_stream .read (b )
536
- _LOGGER .debug ("%s bytes read from source" , len (plaintext ))
549
+ plaintext_length = len (plaintext )
550
+ _LOGGER .debug ("%d bytes read from source" , plaintext_length )
551
+
537
552
finalize = False
538
- if len (plaintext ) < b :
553
+
554
+ if b < 0 or plaintext_length < b :
539
555
_LOGGER .debug ("Final plaintext read from source" )
540
556
finalize = True
557
+
541
558
output = b""
542
559
final_frame_written = False
543
560
@@ -583,8 +600,8 @@ def _read_bytes(self, b):
583
600
:param int b: Number of bytes to read
584
601
:raises NotSupportedError: if content type is not supported
585
602
"""
586
- _LOGGER .debug ("%s bytes requested from stream with content type: %s" , b , self .content_type )
587
- if b <= len (self .output_buffer ) or self .source_stream .closed :
603
+ _LOGGER .debug ("%d bytes requested from stream with content type: %s" , b , self .content_type )
604
+ if 0 <= b <= len (self .output_buffer ) or self .source_stream .closed :
588
605
_LOGGER .debug ("No need to read from source stream or source stream closed" )
589
606
return
590
607
@@ -776,10 +793,13 @@ def _read_bytes_from_non_framed_body(self, b):
776
793
bytes_to_read = self .body_end - self .source_stream .tell ()
777
794
_LOGGER .debug ("%s bytes requested; reading %s bytes" , b , bytes_to_read )
778
795
ciphertext = self .source_stream .read (bytes_to_read )
796
+
779
797
if len (self .output_buffer ) + len (ciphertext ) < self .body_length :
780
798
raise SerializationError ("Total message body contents less than specified in body description" )
799
+
781
800
if self .verifier is not None :
782
801
self .verifier .update (ciphertext )
802
+
783
803
plaintext = self .decryptor .update (ciphertext )
784
804
plaintext += self .decryptor .finalize ()
785
805
aws_encryption_sdk .internal .formatting .deserialize .update_verifier_with_tag (
@@ -844,10 +864,9 @@ def _read_bytes(self, b):
844
864
_LOGGER .debug ("Source stream closed" )
845
865
return
846
866
847
- if b <= len (self .output_buffer ):
848
- _LOGGER .debug (
849
- "%s bytes requested less than or equal to current output buffer size %s" , b , len (self .output_buffer )
850
- )
867
+ buffer_length = len (self .output_buffer )
868
+ if 0 <= b <= buffer_length :
869
+ _LOGGER .debug ("%d bytes requested less than or equal to current output buffer size %d" , b , buffer_length )
851
870
return
852
871
853
872
if self ._header .content_type == ContentType .FRAMED_DATA :
0 commit comments