@@ -11,6 +11,8 @@ mod decl {
1111 } ;
1212 use itertools:: Itertools ;
1313
14+ const MAXLINESIZE : usize = 76 ;
15+
1416 #[ pyattr( name = "Error" , once) ]
1517 fn error_type ( vm : & VirtualMachine ) -> PyTypeRef {
1618 vm. ctx . new_exception_type (
@@ -173,13 +175,13 @@ mod decl {
173175 // The 64 instead of the expected 63 is because
174176 // there are a few uuencodes out there that use
175177 // '`' as zero instead of space.
176- if !( 0x20 ..=0x60 ) . contains ( c) {
178+ if !( b' ' ..=( b' ' + 64 ) ) . contains ( c) {
177179 if [ b'\r' , b'\n' ] . contains ( c) {
178180 return Ok ( 0 ) ;
179181 }
180182 return Err ( vm. new_value_error ( "Illegal char" . to_string ( ) ) ) ;
181183 }
182- Ok ( ( * c - 0x20 ) & 0x3f )
184+ Ok ( ( * c - b' ' ) & 0x3f )
183185 }
184186
185187 #[ derive( FromArgs ) ]
@@ -200,43 +202,45 @@ mod decl {
200202 let mut idx = 0 ;
201203
202204 while idx < len {
203- if buffer[ idx] == 0x3d {
205+ if buffer[ idx] == b'=' {
204206 idx += 1 ;
205207 if idx >= len {
206208 break ;
207209 }
208- /* Soft line breaks */
209- if ( buffer[ idx] == 0x0a ) || ( buffer[ idx] == 0x0d ) {
210- if buffer[ idx] != 0x0a {
211- while idx < len && buffer[ idx] != 0x0a {
210+ // Soft line breaks
211+ if ( buffer[ idx] == b'\n' ) || ( buffer[ idx] == b'\r' ) {
212+ if buffer[ idx] != b'\n' {
213+ while idx < len && buffer[ idx] != b'\n' {
212214 idx += 1 ;
213215 }
214216 }
215217 if idx < len {
216218 idx += 1 ;
217219 }
218- } else if buffer[ idx] == 0x3d {
219- out_data. push ( 0x3d ) ;
220+ } else if buffer[ idx] == b'=' {
221+ // roken case from broken python qp
222+ out_data. push ( b'=' ) ;
220223 idx += 1 ;
221224 } else if idx + 1 < len
222- && ( ( buffer[ idx] >= 0x41 && buffer[ idx] <= 0x46 )
223- || ( buffer[ idx] >= 0x61 && buffer[ idx] <= 0x66 )
224- || ( buffer[ idx] >= 0x30 && buffer[ idx] <= 0x39 ) )
225- && ( ( buffer[ idx + 1 ] >= 0x41 && buffer[ idx + 1 ] <= 0x46 )
226- || ( buffer[ idx + 1 ] >= 0x61 && buffer[ idx + 1 ] <= 0x66 )
227- || ( buffer[ idx + 1 ] >= 0x30 && buffer[ idx + 1 ] <= 0x39 ) )
225+ && ( ( buffer[ idx] >= b'A' && buffer[ idx] <= b'F' )
226+ || ( buffer[ idx] >= b'a' && buffer[ idx] <= b'f' )
227+ || ( buffer[ idx] >= b'0' && buffer[ idx] <= b'9' ) )
228+ && ( ( buffer[ idx + 1 ] >= b'A' && buffer[ idx + 1 ] <= b'F' )
229+ || ( buffer[ idx + 1 ] >= b'a' && buffer[ idx + 1 ] <= b'f' )
230+ || ( buffer[ idx + 1 ] >= b'0' && buffer[ idx + 1 ] <= b'9' ) )
228231 {
232+ // hexval
229233 if let ( Some ( ch1) , Some ( ch2) ) =
230234 ( unhex_nibble ( buffer[ idx] ) , unhex_nibble ( buffer[ idx + 1 ] ) )
231235 {
232236 out_data. push ( ch1 << 4 | ch2) ;
233237 }
234238 idx += 2 ;
235239 } else {
236- out_data. push ( 0x3d ) ;
240+ out_data. push ( b'=' ) ;
237241 }
238- } else if header && buffer[ idx] == 0x5f {
239- out_data. push ( 0x20 ) ;
242+ } else if header && buffer[ idx] == b'_' {
243+ out_data. push ( b' ' ) ;
240244 idx += 1 ;
241245 } else {
242246 out_data. push ( buffer[ idx] ) ;
@@ -278,57 +282,59 @@ mod decl {
278282
279283 inidx = 0 ;
280284 while inidx < buflen {
281- if buf[ inidx] == 0x0a {
285+ if buf[ inidx] == b'\n' {
282286 break ;
283287 }
284288 inidx += 1 ;
285289 }
286- if buflen > 0 && inidx < buflen && buf[ inidx - 1 ] == 0x0d {
290+ if buflen > 0 && inidx < buflen && buf[ inidx - 1 ] == b'\r' {
287291 crlf = true ;
288292 }
289293
290294 inidx = 0 ;
291295 while inidx < buflen {
292296 let mut delta = 0 ;
293297 if ( buf[ inidx] > 126 )
294- || ( buf[ inidx] == 0x3d )
295- || ( header && buf[ inidx] == 0x5f )
296- || ( buf[ inidx] == 0x2e
298+ || ( buf[ inidx] == b'=' )
299+ || ( header && buf[ inidx] == b'_' )
300+ || ( buf[ inidx] == b'.'
297301 && linelen == 0
298302 && ( inidx + 1 == buflen
299- || buf[ inidx + 1 ] == 0x0a
300- || buf[ inidx + 1 ] == 0x0d
303+ || buf[ inidx + 1 ] == b'\n'
304+ || buf[ inidx + 1 ] == b'\r'
301305 || buf[ inidx + 1 ] == 0 ) )
302- || ( !istext && ( ( buf[ inidx] == 0x0d ) || ( buf[ inidx] == 0x0a ) ) )
303- || ( ( buf[ inidx] == 0x09 || buf[ inidx] == 0x20 ) && ( inidx + 1 == buflen) )
306+ || ( !istext && ( ( buf[ inidx] == b'\r' ) || ( buf[ inidx] == b'\n' ) ) )
307+ || ( ( buf[ inidx] == b'\t' || buf[ inidx] == b' ' ) && ( inidx + 1 == buflen) )
304308 || ( ( buf[ inidx] < 33 )
305- && ( buf[ inidx] != 0x0d )
306- && ( buf[ inidx] != 0x0a )
307- && ( quotetabs || ( ( buf[ inidx] != 0x09 ) && ( buf[ inidx] != 0x20 ) ) ) )
309+ && ( buf[ inidx] != b'\r' )
310+ && ( buf[ inidx] != b'\n' )
311+ && ( quotetabs || ( ( buf[ inidx] != b'\t' ) && ( buf[ inidx] != b' ' ) ) ) )
308312 {
309- if ( linelen + 3 ) >= 76 {
310- // MAXLINESIZE = 76
313+ if ( linelen + 3 ) >= MAXLINESIZE {
311314 linelen = 0 ;
312315 delta += if crlf { 3 } else { 2 } ;
313316 }
314317 linelen += 3 ;
315318 delta += 3 ;
316319 inidx += 1 ;
317320 } else if istext
318- && ( ( buf[ inidx] == 0x0a )
321+ && ( ( buf[ inidx] == b'\n' )
319322 || ( ( inidx + 1 < buflen)
320- && ( buf[ inidx] == 0x0d )
321- && ( buf[ inidx + 1 ] == 0x0a ) ) )
323+ && ( buf[ inidx] == b'\r' )
324+ && ( buf[ inidx + 1 ] == b'\n' ) ) )
322325 {
323326 linelen = 0 ;
324- if ( inidx != 0 ) && ( ( buf[ inidx - 1 ] == 0x20 ) || ( buf[ inidx - 1 ] == 0x09 ) ) {
327+ // Protect against whitespace on end of line
328+ if ( inidx != 0 ) && ( ( buf[ inidx - 1 ] == b' ' ) || ( buf[ inidx - 1 ] == b'\t' ) ) {
325329 delta += 2 ;
326330 }
327331 delta += if crlf { 2 } else { 1 } ;
328- inidx += if buf[ inidx] == 0x0d { 2 } else { 1 } ;
332+ inidx += if buf[ inidx] == b'\r' { 2 } else { 1 } ;
329333 } else {
330- if ( inidx + 1 != buflen) && ( buf[ inidx + 1 ] != 0x0a ) && ( linelen + 1 ) >= 76 {
331- // MAXLINESIZE
334+ if ( inidx + 1 != buflen)
335+ && ( buf[ inidx + 1 ] != b'\n' )
336+ && ( linelen + 1 ) >= MAXLINESIZE
337+ {
332338 linelen = 0 ;
333339 delta += if crlf { 3 } else { 2 } ;
334340 }
@@ -346,97 +352,97 @@ mod decl {
346352
347353 while inidx < buflen {
348354 if ( buf[ inidx] > 126 )
349- || ( buf[ inidx] == 0x3d )
350- || ( header && buf[ inidx] == 0x5f )
351- || ( ( buf[ inidx] == 0x2e )
355+ || ( buf[ inidx] == b'=' )
356+ || ( header && buf[ inidx] == b'_' )
357+ || ( ( buf[ inidx] == b'.' )
352358 && ( linelen == 0 )
353359 && ( inidx + 1 == buflen
354- || buf[ inidx + 1 ] == 0x0a
355- || buf[ inidx + 1 ] == 0x0d
360+ || buf[ inidx + 1 ] == b'\n'
361+ || buf[ inidx + 1 ] == b'\r'
356362 || buf[ inidx + 1 ] == 0 ) )
357- || ( !istext && ( ( buf[ inidx] == 0x0d ) || ( buf[ inidx] == 0x0a ) ) )
358- || ( ( buf[ inidx] == 0x09 || buf[ inidx] == 0x20 ) && ( inidx + 1 == buflen) )
363+ || ( !istext && ( ( buf[ inidx] == b'\r' ) || ( buf[ inidx] == b'\n' ) ) )
364+ || ( ( buf[ inidx] == b'\t' || buf[ inidx] == b' ' ) && ( inidx + 1 == buflen) )
359365 || ( ( buf[ inidx] < 33 )
360- && ( buf[ inidx] != 0x0d )
361- && ( buf[ inidx] != 0x0a )
362- && ( quotetabs || ( ( buf[ inidx] != 0x09 ) && ( buf[ inidx] != 0x20 ) ) ) )
366+ && ( buf[ inidx] != b'\r' )
367+ && ( buf[ inidx] != b'\n' )
368+ && ( quotetabs || ( ( buf[ inidx] != b'\t' ) && ( buf[ inidx] != b' ' ) ) ) )
363369 {
364- if ( linelen + 3 ) >= 76 {
370+ if ( linelen + 3 ) >= MAXLINESIZE {
365371 // MAXLINESIZE = 76
366- out_data. push ( 0x3d ) ;
372+ out_data. push ( b'=' ) ;
367373 outidx += 1 ;
368374 if crlf {
369- out_data. push ( 0x0d ) ;
375+ out_data. push ( b'\r' ) ;
370376 outidx += 1 ;
371377 }
372- out_data. push ( 0x0a ) ;
378+ out_data. push ( b'\n' ) ;
373379 outidx += 1 ;
374380 linelen = 0 ;
375381 }
376- out_data. push ( 0x3d ) ;
382+ out_data. push ( b'=' ) ;
377383 outidx += 1 ;
378384
379385 ch = hex_nibble ( buf[ inidx] >> 4 ) ;
380- if ( 0x61 ..=0x66 ) . contains ( & ch) {
381- ch -= 0x20 ;
386+ if ( b'a' ..=b'f' ) . contains ( & ch) {
387+ ch -= b' ' ;
382388 }
383389 out_data. push ( ch) ;
384390 ch = hex_nibble ( buf[ inidx] & 0xf ) ;
385- if ( 0x61 ..=0x66 ) . contains ( & ch) {
386- ch -= 0x20 ;
391+ if ( b'a' ..=b'f' ) . contains ( & ch) {
392+ ch -= b' ' ;
387393 }
388394 out_data. push ( ch) ;
389395
390396 outidx += 2 ;
391397 inidx += 1 ;
392398 linelen += 3 ;
393399 } else if istext
394- && ( ( buf[ inidx] == 0x0a )
400+ && ( ( buf[ inidx] == b'\n' )
395401 || ( ( inidx + 1 < buflen)
396- && ( buf[ inidx] == 0x0d )
397- && ( buf[ inidx + 1 ] == 0x0a ) ) )
402+ && ( buf[ inidx] == b'\r' )
403+ && ( buf[ inidx + 1 ] == b'\n' ) ) )
398404 {
399405 linelen = 0 ;
400406 if ( outidx != 0 )
401- && ( ( out_data[ outidx - 1 ] == 0x20 ) || ( out_data[ outidx - 1 ] == 0x09 ) )
407+ && ( ( out_data[ outidx - 1 ] == b' ' ) || ( out_data[ outidx - 1 ] == b'\t' ) )
402408 {
403409 ch = hex_nibble ( out_data[ outidx - 1 ] >> 4 ) ;
404- if ( 0x61 ..=0x66 ) . contains ( & ch) {
405- ch -= 0x20 ;
410+ if ( b'a' ..=b'f' ) . contains ( & ch) {
411+ ch -= b' ' ;
406412 }
407413 out_data. push ( ch) ;
408414 ch = hex_nibble ( out_data[ outidx - 1 ] & 0xf ) ;
409- if ( 0x61 ..=0x66 ) . contains ( & ch) {
410- ch -= 0x20 ;
415+ if ( b'a' ..=b'f' ) . contains ( & ch) {
416+ ch -= b' ' ;
411417 }
412418 out_data. push ( ch) ;
413- out_data[ outidx - 1 ] = 0x3d ;
419+ out_data[ outidx - 1 ] = b'=' ;
414420 outidx += 2 ;
415421 }
416422
417423 if crlf {
418- out_data. push ( 0x0d ) ;
424+ out_data. push ( b'\r' ) ;
419425 outidx += 1 ;
420426 }
421- out_data. push ( 0x0a ) ;
427+ out_data. push ( b'\n' ) ;
422428 outidx += 1 ;
423- inidx += if buf[ inidx] == 0x0d { 2 } else { 1 } ;
429+ inidx += if buf[ inidx] == b'\r' { 2 } else { 1 } ;
424430 } else {
425- if ( inidx + 1 != buflen) && ( buf[ inidx + 1 ] != 0x0a ) && ( linelen + 1 ) >= 76 {
431+ if ( inidx + 1 != buflen) && ( buf[ inidx + 1 ] != b'\n' ) && ( linelen + 1 ) >= 76 {
426432 // MAXLINESIZE = 76
427- out_data. push ( 0x3d ) ;
433+ out_data. push ( b'=' ) ;
428434 outidx += 1 ;
429435 if crlf {
430- out_data. push ( 0x0d ) ;
436+ out_data. push ( b'\r' ) ;
431437 outidx += 1 ;
432438 }
433- out_data. push ( 0x0a ) ;
439+ out_data. push ( b'\n' ) ;
434440 outidx += 1 ;
435441 linelen = 0 ;
436442 }
437443 linelen += 1 ;
438- if header && buf[ inidx] == 0x20 {
439- out_data. push ( 0x5f ) ;
444+ if header && buf[ inidx] == b' ' {
445+ out_data. push ( b'_' ) ;
440446 outidx += 1 ;
441447 inidx += 1 ;
442448 } else {
@@ -524,7 +530,7 @@ mod decl {
524530 let length = if b. is_empty ( ) {
525531 ( ( -0x20i32 ) & 0x3fi32 ) as usize
526532 } else {
527- ( ( b[ 0 ] - 0x20 ) & 0x3f ) as usize
533+ ( ( b[ 0 ] - b' ' ) & 0x3f ) as usize
528534 } ;
529535
530536 // Allocate the buffer
@@ -581,7 +587,7 @@ mod decl {
581587 if backtick && num != 0 {
582588 0x60
583589 } else {
584- 0x20 + num
590+ b' ' + num
585591 }
586592 }
587593
0 commit comments