fix: less dumb zlib decoder

there are a bunch of zlib headers, so letting the zlib library deal with it is probably safer
This commit is contained in:
beerpiss 2023-11-15 19:02:34 +07:00
parent a445d87c6e
commit 07028d9dc7
2 changed files with 30 additions and 19 deletions

View File

@ -125,17 +125,26 @@ pub fn read_hinternet_url(handle: HINTERNET) -> Result<String> {
return Err(anyhow!("Could not get URL from HINTERNET handle: {ec}"));
}
pub unsafe fn read_potentially_deflated_buffer(buf: *const u8, len: usize) -> Result<String> {
let mut slice = std::slice::from_raw_parts(buf, len);
pub fn read_potentially_deflated_buffer(buf: *const u8, len: usize) -> Result<String> {
let mut slice = unsafe { std::slice::from_raw_parts(buf, len) };
let mut ret = String::new();
let _ = if slice[0] == 120 && slice[1] == 156 {
// Just a really dumb check if the request is sent over zlib or not
let mut decoder = ZlibDecoder::new(slice);
decoder.read_to_string(&mut ret)
} else {
slice.read_to_string(&mut ret)
}?;
let mut decoder = ZlibDecoder::new(slice);
let zlib_result = decoder.read_to_string(&mut ret);
if zlib_result.is_ok() {
return Ok(ret);
}
Ok(ret)
ret.clear();
let result = slice.read_to_string(&mut ret);
if result.is_ok() {
return Ok(ret);
}
// Unwrapping here is fine, if result was Ok we wouldn't reach this place.
Err(anyhow!(
"Could not decode contents of buffer as both DEFLATE-compressed ({:#}) and plaintext ({:#}) UTF-8 string.",
zlib_result.err().expect("This shouldn't happen, if Result was Ok the string should have been returned earlier."),
result.err().expect("This shouldn't happen, if Result was Ok the string should have been returned earlier."),
))
}

View File

@ -17,7 +17,7 @@ use crate::{
helpers::{call_tachi, read_hinternet_url, read_potentially_deflated_buffer, request_tachi},
types::{
game::UpsertUserAllRequest,
tachi::{ClassEmblem, Import, ImportClasses, ImportScore, Difficulty},
tachi::{ClassEmblem, Difficulty, Import, ImportClasses, ImportScore},
},
CONFIGURATION, TACHI_IMPORT_URL, TACHI_STATUS_URL,
};
@ -93,12 +93,10 @@ fn winhttpwritedata_hook(
};
debug!("winhttpwritedata URL: {url}");
let request_body = match unsafe {
read_potentially_deflated_buffer(
lp_buffer as *const u8,
dw_number_of_bytes_to_write as usize,
)
} {
let request_body = match read_potentially_deflated_buffer(
lp_buffer as *const u8,
dw_number_of_bytes_to_write as usize,
) {
Ok(data) => data,
Err(err) => {
error!("There was an error reading the request body: {:#}", err);
@ -144,8 +142,12 @@ fn winhttpwritedata_hook(
.user_playlog_list
.into_iter()
.filter_map(|playlog| {
let result = ImportScore::try_from_playlog(playlog, CONFIGURATION.general.fail_over_lamp);
if result.as_ref().is_ok_and(|v| v.difficulty != Difficulty::WorldsEnd) {
let result =
ImportScore::try_from_playlog(playlog, CONFIGURATION.general.fail_over_lamp);
if result
.as_ref()
.is_ok_and(|v| v.difficulty != Difficulty::WorldsEnd)
{
result.ok()
} else {
None