fix: clipboard data, decompress, buf too small (#8556)
* fix: clipboard data, decompress, buf too small Signed-off-by: fufesou <linlong1266@gmail.com> * fix: compress image Signed-off-by: fufesou <linlong1266@gmail.com> * decompress image, use default level Signed-off-by: fufesou <linlong1266@gmail.com> * chore Signed-off-by: fufesou <linlong1266@gmail.com> * decompress, zstd::decode_all Signed-off-by: fufesou <linlong1266@gmail.com> --------- Signed-off-by: fufesou <linlong1266@gmail.com>
This commit is contained in:
parent
0ab500c27c
commit
62a8349739
@ -1,5 +1,5 @@
|
||||
use std::{cell::RefCell, io};
|
||||
use zstd::bulk::{Compressor, Decompressor};
|
||||
use zstd::bulk::Compressor;
|
||||
|
||||
// The library supports regular compression levels from 1 up to ZSTD_maxCLevel(),
|
||||
// which is currently 22. Levels >= 20
|
||||
@ -7,7 +7,6 @@ use zstd::bulk::{Compressor, Decompressor};
|
||||
// value 0 means default, which is controlled by ZSTD_CLEVEL_DEFAULT
|
||||
thread_local! {
|
||||
static COMPRESSOR: RefCell<io::Result<Compressor<'static>>> = RefCell::new(Compressor::new(crate::config::COMPRESS_LEVEL));
|
||||
static DECOMPRESSOR: RefCell<io::Result<Decompressor<'static>>> = RefCell::new(Decompressor::new());
|
||||
}
|
||||
|
||||
pub fn compress(data: &[u8]) -> Vec<u8> {
|
||||
@ -31,27 +30,5 @@ pub fn compress(data: &[u8]) -> Vec<u8> {
|
||||
}
|
||||
|
||||
pub fn decompress(data: &[u8]) -> Vec<u8> {
|
||||
let mut out = Vec::new();
|
||||
DECOMPRESSOR.with(|d| {
|
||||
if let Ok(mut d) = d.try_borrow_mut() {
|
||||
match &mut *d {
|
||||
Ok(d) => {
|
||||
const MAX: usize = 1024 * 1024 * 64;
|
||||
const MIN: usize = 1024 * 1024;
|
||||
let mut n = 30 * data.len();
|
||||
n = n.clamp(MIN, MAX);
|
||||
match d.decompress(data, n) {
|
||||
Ok(res) => out = res,
|
||||
Err(err) => {
|
||||
crate::log::debug!("Failed to decompress: {}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(err) => {
|
||||
crate::log::debug!("Failed to get decompressor: {}", err);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
out
|
||||
zstd::decode_all(data).unwrap_or_default()
|
||||
}
|
||||
|
@ -5,11 +5,11 @@ use std::sync::{
|
||||
|
||||
use clipboard_master::{CallbackResult, ClipboardHandler, Master, Shutdown};
|
||||
use hbb_common::{
|
||||
ResultType,
|
||||
allow_err,
|
||||
compress::{compress as compress_func, decompress},
|
||||
log,
|
||||
message_proto::*,
|
||||
ResultType,
|
||||
};
|
||||
|
||||
pub const CLIPBOARD_NAME: &'static str = "clipboard";
|
||||
@ -218,12 +218,13 @@ impl ClipboardData {
|
||||
}
|
||||
|
||||
fn from_msg(clipboard: Clipboard) -> Self {
|
||||
let is_image = clipboard.width > 0 && clipboard.height > 0;
|
||||
let data = if clipboard.compress {
|
||||
decompress(&clipboard.content)
|
||||
} else {
|
||||
clipboard.content.into()
|
||||
};
|
||||
if clipboard.width > 0 && clipboard.height > 0 {
|
||||
if is_image {
|
||||
ClipboardData::Image(
|
||||
arboard::ImageData {
|
||||
bytes: data.into(),
|
||||
|
Loading…
Reference in New Issue
Block a user