1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-28 19:47:45 +00:00

Merge pull request #1659 from sylvestre/base-non-utf8

base32/base64: tolerate non-utf8 encoded inputs
This commit is contained in:
Sylvestre Ledru 2020-12-16 08:46:48 +01:00 committed by GitHub
commit 1e9820a7c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 14 additions and 4 deletions

View file

@ -8,7 +8,7 @@
// that was distributed with this source code.
use std::fs::File;
use std::io::{stdin, BufReader, Read};
use std::io::{stdin, stdout, BufReader, Read, Write};
use std::path::Path;
use uucore::encoding::{wrap_print, Data, Format};
@ -85,7 +85,12 @@ fn handle_input<R: Read>(
wrap_print(&data, encoded);
} else {
match data.decode() {
Ok(s) => print!("{}", String::from_utf8(s).unwrap()),
Ok(s) => {
if stdout().write_all(&s).is_err() {
// on windows console, writing invalid utf8 returns an error
crash!(1, "Cannot write non-utf8 data");
}
}
Err(_) => crash!(1, "invalid input"),
}
}

View file

@ -8,7 +8,7 @@
// that was distributed with this source code.
use std::fs::File;
use std::io::{stdin, BufReader, Read};
use std::io::{stdin, stdout, BufReader, Read, Write};
use std::path::Path;
use uucore::encoding::{wrap_print, Data, Format};
@ -85,7 +85,12 @@ fn handle_input<R: Read>(
wrap_print(&data, encoded);
} else {
match data.decode() {
Ok(s) => print!("{}", String::from_utf8(s).unwrap()),
Ok(s) => {
if stdout().write_all(&s).is_err() {
// on windows console, writing invalid utf8 returns an error
crash!(1, "Cannot write non-utf8 data");
}
}
Err(_) => crash!(1, "invalid input"),
}
}