| 485 | |
| 486 | #[pyfunction] |
| 487 | fn b2a_qp(args: B2aQpArgs) -> PyResult<Vec<u8>> { |
| 488 | let s = args.data; |
| 489 | let quotetabs = args.quotetabs; |
| 490 | let istext = args.istext; |
| 491 | let header = args.header; |
| 492 | s.with_ref(|buf| { |
| 493 | let buflen = buf.len(); |
| 494 | let mut line_len = 0; |
| 495 | let mut out_data_len = 0; |
| 496 | let mut crlf = false; |
| 497 | let mut ch; |
| 498 | |
| 499 | let mut in_idx; |
| 500 | let mut out_idx; |
| 501 | |
| 502 | in_idx = 0; |
| 503 | while in_idx < buflen { |
| 504 | if buf[in_idx] == b'\n' { |
| 505 | break; |
| 506 | } |
| 507 | in_idx += 1; |
| 508 | } |
| 509 | if buflen > 0 && in_idx < buflen && buf[in_idx - 1] == b'\r' { |
| 510 | crlf = true; |
| 511 | } |
| 512 | |
| 513 | in_idx = 0; |
| 514 | while in_idx < buflen { |
| 515 | let mut delta = 0; |
| 516 | if (buf[in_idx] > 126) |
| 517 | || (buf[in_idx] == b'=') |
| 518 | || (header && buf[in_idx] == b'_') |
| 519 | || (buf[in_idx] == b'.' |
| 520 | && line_len == 0 |
| 521 | && (in_idx + 1 == buflen |
| 522 | || buf[in_idx + 1] == b'\n' |
| 523 | || buf[in_idx + 1] == b'\r' |
| 524 | || buf[in_idx + 1] == 0)) |
| 525 | || (!istext && ((buf[in_idx] == b'\r') || (buf[in_idx] == b'\n'))) |
| 526 | || ((buf[in_idx] == b'\t' || buf[in_idx] == b' ') && (in_idx + 1 == buflen)) |
| 527 | || ((buf[in_idx] < 33) |
| 528 | && (buf[in_idx] != b'\r') |
| 529 | && (buf[in_idx] != b'\n') |
| 530 | && (quotetabs || ((buf[in_idx] != b'\t') && (buf[in_idx] != b' ')))) |
| 531 | { |
| 532 | if (line_len + 3) >= MAXLINESIZE { |
| 533 | line_len = 0; |
| 534 | delta += if crlf { 3 } else { 2 }; |
| 535 | } |
| 536 | line_len += 3; |
| 537 | delta += 3; |
| 538 | in_idx += 1; |
| 539 | } else if istext |
| 540 | && ((buf[in_idx] == b'\n') |
| 541 | || ((in_idx + 1 < buflen) |
| 542 | && (buf[in_idx] == b'\r') |
| 543 | && (buf[in_idx + 1] == b'\n'))) |
| 544 | { |