1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
use crate::{ args::{DecodeArgs, EncodeArgs, RemoveArgs}, Chunk, ChunkType, Error, Png, Result, }; use std::{ convert::TryInto, fs::OpenOptions, io::{Read, Write}, path::Path, str::FromStr, }; fn take_png<P: AsRef<Path>>(input: P) -> Result<Png> { let mut file = OpenOptions::new().write(true).read(true).open(input)?; let mut buffer = Vec::with_capacity(1000_000); file.read_to_end(&mut buffer)?; Ok(buffer.as_slice().try_into()?) } pub fn encode<S: AsRef<Path>>(input: S, args: EncodeArgs) -> Result<()> { let mut png = take_png(&input)?; png.append_chunk(Chunk::new( ChunkType::from_str(&args.chunk_type)?, args.message.into_bytes(), )); let mut file = std::fs::File::create(input)?; file.write_all(&png.as_bytes())?; Ok(()) } pub fn decode<S: AsRef<Path>>(input: S, args: DecodeArgs) -> Result<()> { let png = take_png(&input)?; if let Some(chunk) = png.chunk_by_type(&args.chunk_type) { println!( "Hidden message in the chunk {}: '{}'", chunk.chunk_type().to_string(), chunk.data_as_string()? ); } else { return Err(Error::Custom("Unable to decode chunk")); } Ok(()) } pub fn remove<S: AsRef<Path>>(input: S, args: RemoveArgs) -> Result<()> { let mut png = take_png(&input)?; png.remove_chunk(&args.chunk_type)?; let mut file = std::fs::File::create(input)?; file.write_all(&png.as_bytes())?; Ok(()) } pub fn print(input: &Path) -> Result<()> { let png = take_png(&input)?; println!("File: {}, Size: {}", input.display(), png.as_bytes().len()); for (i, chunk) in png.chunks().iter().enumerate() { print!("\n({})", i + 1); print!("{}", chunk); } Ok(()) } #[cfg(test)] mod tests { use super::*; use std::{fs::remove_file, path::PathBuf}; fn make_copy_of_file(input: &str) -> PathBuf { let input = PathBuf::new().join("assets").join(input); std::fs::File::create(&input).unwrap(); std::fs::copy("assets/pic.png", &input).unwrap(); input } #[test] fn test_encode() { let input = make_copy_of_file("encode.png"); let chunk_type = "RuST".to_string(); let message = "Message".to_string(); let args = EncodeArgs { chunk_type, message, }; let res = encode(&input, args); assert!(res.is_ok()); remove_file(input).unwrap(); } #[test] fn test_decode() { let input = make_copy_of_file("decode.png"); let chunk_type = "RuST".to_string(); let message = "Message".to_string(); let args = EncodeArgs { chunk_type, message, }; let res = encode(&input, args); assert!(res.is_ok()); let chunk_type = "RuST".to_string(); let args = DecodeArgs { chunk_type }; let res = decode(&input, args); assert!(res.is_ok()); remove_file(input).unwrap(); } #[test] fn test_remove() { let input = make_copy_of_file("remove.png"); let chunk_type = "RuST".to_string(); let message = "Message".to_string(); let args = EncodeArgs { chunk_type, message, }; let res = encode(&input, args); assert!(res.is_ok()); let chunk_type = "RuST".to_string(); let args = RemoveArgs { chunk_type }; let res = remove(&input, args); assert!(res.is_ok()); remove_file(input).unwrap(); } #[test] fn test_print() { let input = make_copy_of_file("print.png"); let res = print(&input); assert!(res.is_ok()); remove_file(input).unwrap(); } #[test] fn test_all_one() { let input = make_copy_of_file("all_in.png"); let chunk_type = "RuST".to_string(); let message = "Message".to_string(); let args = EncodeArgs { chunk_type, message, }; let res = encode(&input, args); assert!(res.is_ok()); let chunk_type = "RuST".to_string(); let args = DecodeArgs { chunk_type }; let res = decode(&input, args); assert!(res.is_ok()); let chunk_type = "RuST".to_string(); let args = RemoveArgs { chunk_type }; let res = remove(&input, args); assert!(res.is_ok()); let res = print(&input); assert!(res.is_ok()); remove_file(input).unwrap(); } }