@@ -108,18 +108,14 @@ where
108
108
HashT : BlockSizeUser + Default + FixedOutput + HashMarker ,
109
109
HashT :: OutputSize : IsLessOrEqual < HashT :: BlockSize , Output = True > ,
110
110
{
111
- fn fill_bytes ( & mut self , okm : & mut [ u8 ] ) -> Result < usize > {
112
- let mut read_bytes = 0 ;
111
+ fn fill_bytes ( & mut self , mut okm : & mut [ u8 ] ) -> Result < usize > {
112
+ if self . remaining == 0 {
113
+ return Err ( Error ) ;
114
+ }
113
115
114
- for b in okm {
115
- if self . remaining == 0 {
116
- if read_bytes == 0 {
117
- return Err ( Error ) ;
118
- } else {
119
- return Ok ( read_bytes) ;
120
- }
121
- }
116
+ let mut read_bytes = 0 ;
122
117
118
+ while self . remaining != 0 {
123
119
if self . offset == self . b_vals . len ( ) {
124
120
self . index += 1 ;
125
121
self . offset = 0 ;
@@ -138,10 +134,27 @@ where
138
134
self . b_vals = b_vals. finalize_fixed ( ) ;
139
135
}
140
136
141
- * b = self . b_vals [ self . offset ] ;
142
- self . offset += 1 ;
143
- self . remaining -= 1 ;
144
- read_bytes += 1 ;
137
+ let bytes_to_read = self
138
+ . remaining
139
+ . min ( okm. len ( ) . try_into ( ) . unwrap_or ( u16:: MAX ) )
140
+ . min (
141
+ ( self . b_vals . len ( ) - self . offset )
142
+ . try_into ( )
143
+ . unwrap_or ( u16:: MAX ) ,
144
+ ) ;
145
+
146
+ if bytes_to_read == 0 {
147
+ return Ok ( read_bytes) ;
148
+ }
149
+
150
+ okm[ ..bytes_to_read. into ( ) ] . copy_from_slice (
151
+ & self . b_vals [ self . offset ..self . offset + usize:: from ( bytes_to_read) ] ,
152
+ ) ;
153
+ okm = & mut okm[ bytes_to_read. into ( ) ..] ;
154
+
155
+ self . offset += usize:: from ( bytes_to_read) ;
156
+ self . remaining -= bytes_to_read;
157
+ read_bytes += usize:: from ( bytes_to_read) ;
145
158
}
146
159
147
160
Ok ( read_bytes)
0 commit comments