Skip to content

Commit

Permalink
Merge pull request #20 from Holzhaus/housekeeping
Browse files Browse the repository at this point in the history
Housekeeping
  • Loading branch information
Holzhaus authored Nov 30, 2023
2 parents dedb3be + 6dbd1a0 commit 4e296af
Show file tree
Hide file tree
Showing 9 changed files with 37 additions and 17 deletions.
1 change: 1 addition & 0 deletions .codespellignorelines
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
b"siz" => Field::FileSize(text),
b"mis" => Field::Missing(value),
10 changes: 5 additions & 5 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ default_language_version:
python: python3
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.4.0
rev: v4.5.0
hooks:
- id: check-case-conflict
- id: check-json
Expand All @@ -21,7 +21,7 @@ repos:
- id: mixed-line-ending
- id: trailing-whitespace
- repo: https://github.com/codespell-project/codespell
rev: v2.2.2
rev: v2.2.6
hooks:
- id: codespell
args:
Expand All @@ -48,11 +48,11 @@ repos:
warnings,
]
- repo: https://github.com/DavidAnson/markdownlint-cli2
rev: v0.6.0
rev: v0.11.0
hooks:
- id: markdownlint-cli2
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v3.0.0-alpha.4
rev: v3.1.0
hooks:
- id: prettier
# We use markdowncli
Expand All @@ -66,6 +66,6 @@ repos:
hooks:
- id: sourceheaders
- repo: https://github.com/jorisroovers/gitlint
rev: v0.19.0dev
rev: v0.19.1
hooks:
- id: gitlint
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ edition = "2021"

[dependencies]
nom = "7"
base64 = "0.13"
base64 = "0.21"
thiserror = "1"

[dev-dependencies]
id3 = "1"
textwrap = "0.14"
textwrap = "0.16"
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ pub enum Error {
errors: Vec<(Vec<u8>, nom::error::VerboseErrorKind)>,
},

/// Represents decode error.
#[error("Failed to encode base64 data")]
Base64EncodeError { source: base64::EncodeSliceError },

/// Represents decode error.
#[error("Malformed base64 data")]
Base64DecodeError { source: base64::DecodeError },
Expand Down
2 changes: 1 addition & 1 deletion src/library/database.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ fn take_u16_bytes(input: &[u8]) -> Res<&[u8], Vec<u16>> {

fn parse_u16_text(input: &[u8]) -> Res<&[u8], String> {
let (input, bytes) = nom::combinator::all_consuming(take_u16_bytes)(input)?;
let text = std::char::decode_utf16(bytes.into_iter())
let text = std::char::decode_utf16(bytes)
.map(|r| r.unwrap_or(std::char::REPLACEMENT_CHARACTER))
.collect::<String>();
Ok((input, text))
Expand Down
2 changes: 1 addition & 1 deletion src/library/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@

//! Parsers for the Serato library database and crates
pub mod database;
pub(self) mod parser;
mod parser;

pub use parser::{Library, Track};
19 changes: 14 additions & 5 deletions src/tag/format/enveloped.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use super::Tag;
use crate::error::Error;
use crate::util::{take_utf8, Res};
use base64::Engine;
use std::io;
use std::io::Cursor;

Expand Down Expand Up @@ -49,14 +50,21 @@ pub fn take_base64_with_newline(input: &[u8]) -> Res<&[u8], &[u8]> {
nom::bytes::complete::take_while(|b| is_base64(b) || is_newline(b))(input)
}

const BASE64_FORGIVING: base64::Config = base64::STANDARD_NO_PAD.decode_allow_trailing_bits(true);
const BASE64_FORGIVING: base64::engine::general_purpose::GeneralPurpose =
base64::engine::general_purpose::GeneralPurpose::new(
&base64::alphabet::STANDARD,
base64::engine::general_purpose::GeneralPurposeConfig::new()
.with_encode_padding(false)
.with_decode_allow_trailing_bits(true)
.with_decode_padding_mode(base64::engine::DecodePaddingMode::RequireNone),
);

pub fn base64_decode(input: &[u8]) -> Result<Vec<u8>, Error> {
let mut encoded: Vec<u8> = input.iter().filter(|&b| !is_newline(*b)).copied().collect();
if encoded.len() % 4 != 2 {
encoded.pop();
}
let decoded = base64::decode_config(encoded, BASE64_FORGIVING);
let decoded = BASE64_FORGIVING.decode(encoded);
match decoded {
Ok(data) => Ok(data),
Err(e) => Err(Error::Base64DecodeError { source: e }),
Expand All @@ -68,9 +76,10 @@ pub fn base64_encode(writer: &mut impl io::Write, input: &[u8]) -> Result<usize,
let chunks = input.chunks(54);
let last_chunk_index = chunks.len() - 1;
for (i, chunk) in chunks.enumerate() {
let mut buf = Vec::new();
buf.resize(72, 0);
let bytes_encoded = base64::encode_config_slice(chunk, BASE64_FORGIVING, &mut buf);
let mut buf = vec![0; 72];
let bytes_encoded = BASE64_FORGIVING
.encode_slice(chunk, &mut buf)
.map_err(|e| Error::Base64EncodeError { source: e })?;
bytes_written += writer.write(&buf[..bytes_encoded])?;
if i == last_chunk_index {
if bytes_encoded % 4 != 2 {
Expand Down
10 changes: 8 additions & 2 deletions src/tag/markers2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use super::generic::{
use super::util::{take_color, take_version, write_color, write_version};
use crate::error::Error;
use crate::util::{take_utf8, Res, NULL};
use base64::Engine;
use nom::error::ParseError;
use std::io;
use std::io::Cursor;
Expand Down Expand Up @@ -263,12 +264,17 @@ fn decode_base64_chunks(
}
let mut buf = [0; 54];
// TODO: Add proper error handling here
let mut res = base64::decode_config_slice(chunk, base64::STANDARD, &mut buf);
// We can use the unchecked decoding variant here, because we already ensured that the
// chunk is not longer than 72 characters (which means that the result can not exceed 54
// bytes.
let mut res = base64::engine::general_purpose::STANDARD_NO_PAD
.decode_slice_unchecked(chunk, &mut buf);
if let Err(base64::DecodeError::InvalidLength) = res {
let mut v = Vec::new();
v.extend_from_slice(chunk);
v.push(b'A');
res = base64::decode_config_slice(v.as_slice(), base64::STANDARD, &mut buf);
res = base64::engine::general_purpose::STANDARD_NO_PAD
.decode_slice_unchecked(v.as_slice(), &mut buf);
}
let num_bytes = res.unwrap();
decoded_data.extend_from_slice(&buf[..num_bytes]);
Expand Down
2 changes: 1 addition & 1 deletion src/tag/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub mod color;
pub mod format;
pub mod generic;
pub mod serato32;
pub(self) mod util;
mod util;

pub mod container;
pub use container::TagContainer;
Expand Down

0 comments on commit 4e296af

Please sign in to comment.