Skip to content

Commit

Permalink
Implement decoding of strings generated by WA 2.18+
Browse files Browse the repository at this point in the history
  • Loading branch information
Zireael-N committed Sep 20, 2020
1 parent 639062a commit af0dd1a
Show file tree
Hide file tree
Showing 9 changed files with 586 additions and 11 deletions.
2 changes: 1 addition & 1 deletion native/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion native/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "node-weakauras-parser"
version = "3.0.8"
version = "3.0.9"
authors = ["Velithris"]
edition = "2018"
license = "MIT"
Expand Down
29 changes: 21 additions & 8 deletions native/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ use std::borrow::Cow;
use super::base64;
use super::huffman;

use super::ace_serialize::{Deserializer, Serializer};
use super::ace_serialize::{Deserializer as LegacyDeserializer, Serializer};
use super::lib_serialize::Deserializer;

enum StringVersion {
Huffman, // base64
Deflate, // '!' + base64
BinarySerialization, // !WA:\d+! + base64
}

pub fn transform_max_size<'a>(v: Handle<'a, JsValue>, cx: &'a mut FunctionContext) -> NeonResult<Option<usize>> {
if v.downcast::<JsUndefined>().is_ok() {
Expand All @@ -25,16 +32,18 @@ pub fn transform_max_size<'a>(v: Handle<'a, JsValue>, cx: &'a mut FunctionContex
}

pub fn decode_weakaura(src: &str, max_size: Option<usize>) -> Result<String, &'static str> {
let (weakaura, legacy) = if src.starts_with('!') {
(&src[1..], false)
let (weakaura, version) = if src.starts_with("!WA:2!") {
(&src[6..], StringVersion::BinarySerialization)
} else if src.starts_with('!') {
(&src[1..], StringVersion::Deflate)
} else {
(&src[..], true)
(&src[..], StringVersion::Huffman)
};

let decoded = base64::decode(weakaura)?;

let max_size = max_size.unwrap_or(usize::MAX);
let decompressed = if legacy {
let decompressed = if let StringVersion::Huffman = version {
huffman::decompress(&decoded, max_size)
} else {
use flate2::read::DeflateDecoder;
Expand All @@ -59,9 +68,13 @@ pub fn decode_weakaura(src: &str, max_size: Option<usize>) -> Result<String, &'s
.map(|_| Cow::from(result))
}?;

Deserializer::from_str(&String::from_utf8_lossy(&decompressed))
.deserialize_first()
.and_then(|deserialized| serde_json::to_string(&deserialized).map_err(|_| "Failed to convert to JSON"))
let deserialized = if let StringVersion::BinarySerialization = version {
Deserializer::from_slice(&decompressed).deserialize_first()
} else {
LegacyDeserializer::from_str(&String::from_utf8_lossy(&decompressed)).deserialize_first()
}?;

serde_json::to_string(&deserialized).map_err(|_| "Failed to convert to JSON")
}

pub fn encode_weakaura(json: &str) -> Result<String, &'static str> {
Expand Down
1 change: 1 addition & 0 deletions native/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use neon::prelude::*;
mod ace_serialize;
mod base64;
mod huffman;
mod lib_serialize;

mod asynchronous;
mod common;
Expand Down

0 comments on commit af0dd1a

Please sign in to comment.