2 * Copyright (c) 2017 Tilman Sauerbeck (tilman at code-monkey de)
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 pub struct Yencode<'a> {
29 buffer: &'a mut Buffer,
33 impl<'a> Yencode<'a> {
34 pub fn new(buffer: &mut Buffer) -> Yencode {
41 pub fn start(&mut self, filename: &[u8]) {
42 self.buffer.write(b"=ybegin name=");
43 self.buffer.write(filename);
44 self.buffer.write(b"\n");
47 fn encode_line(&mut self, input_buf: &[u8], output_buf: &mut [u8]) {
48 let mut output_buf_offset = 0;
50 for input in input_buf {
51 let mut c : u8 = *input;
55 b'\0' | b'\n' | b'\r' | b'=' => {
56 output_buf[output_buf_offset] = b'=';
57 output_buf_offset += 1;
65 output_buf[output_buf_offset] = c;
66 output_buf_offset += 1;
69 output_buf[output_buf_offset] = b'\n';
70 output_buf_offset += 1;
72 if output_buf[0] == b'.' {
73 self.buffer.write(b".");
76 self.buffer.write(&output_buf[0..output_buf_offset]);
79 pub fn data(&mut self, buf: &[u8]) {
82 const INPUT_BYTES_PER_LINE : usize = 128;
84 for chunk in buf.chunks(INPUT_BYTES_PER_LINE) {
85 let mut linebuf = [0u8; INPUT_BYTES_PER_LINE << 1];
87 self.encode_line(chunk, &mut linebuf);
91 pub fn finish(&mut self) {
92 let mut yend = [b' '; 23];
94 yend[0..].copy_from_slice(b"=yend crc32=xxxxxxxx\n.\n");
96 fmt_x32(&mut yend[12..], self.crc.finish());
98 self.buffer.write(¥d);