use std::collections::BTreeMap;
use std::path::Path;
use crate::utils::{
Reader, decode_fixed_utf16, ensure_even, read_file, read_u32_at, slice_at, validate_count,
};
use crate::{Dictionary, Entry, Error, Format, Metadata, Result};
const HEADER_LEN: usize = 12;
const RECORD_COUNT_OFFSET: usize = 0x120;
const TOTAL_WORDS_OFFSET: usize = 0x124;
const NAME_RANGE: std::ops::Range<usize> = 0x130..0x338;
const CATEGORY_RANGE: std::ops::Range<usize> = 0x338..0x540;
const DESCRIPTION_RANGE: std::ops::Range<usize> = 0x540..0x0d40;
const EXAMPLE_RANGE: std::ops::Range<usize> = 0x0d40..0x1540;
const PINYIN_COUNT_OFFSET: usize = 0x1540;
const PINYIN_HEADER_LEN: usize = 4;
const PINYIN_TABLE_OFFSET: usize = 0x1544;
const ENGLISH_CODE_COUNT: u16 = 26;
const FORMAT: Format = Format::Scel;
type CodeTable = Vec<Option<String>>;
pub fn parse(data: &[u8]) -> Result<Dictionary> {
validate_header(data)?;
let metadata = parse_metadata(data)?;
let record_count = read_u32_at(data, RECORD_COUNT_OFFSET, FORMAT)?;
let total_words = read_u32_at(data, TOTAL_WORDS_OFFSET, FORMAT)?;
let pinyin_count = read_pinyin_count(data)?;
let mut reader = Reader::at(data, PINYIN_TABLE_OFFSET, FORMAT);
let code_table = parse_pinyin_table(&mut reader, pinyin_count)?;
let entries = parse_word_table(
&mut reader,
record_count,
total_words,
pinyin_count,
&code_table,
)?;
Ok(Dictionary { metadata, entries })
}
pub fn parse_file(path: impl AsRef<Path>) -> Result<Dictionary> {
let data = read_file(path, FORMAT)?;
parse(&data)
}
fn validate_header(data: &[u8]) -> Result<()> {
let header = slice_at(data, 0, HEADER_LEN, FORMAT)?;
let magic = [header[0], header[1], header[2], header[3]];
if magic != [0x40, 0x15, 0x00, 0x00] {
return Err(Error::InvalidMagic {
format: FORMAT,
found: magic.to_vec(),
});
}
let variant = [header[4], header[5], header[6]];
if variant != *b"DCS" && variant != *b"ECS" {
return Err(Error::UnsupportedVariant {
format: FORMAT,
found: variant.to_vec(),
});
}
let version = [header[7], header[8], header[9], header[10], header[11]];
if version != [0x01, 0x01, 0x00, 0x00, 0x00] {
return Err(Error::UnsupportedVersion {
format: FORMAT,
found: version.to_vec(),
});
}
Ok(())
}
fn parse_metadata(data: &[u8]) -> Result<Metadata> {
let name = decode_fixed_utf16(data, NAME_RANGE, "dictionary name", FORMAT)?;
let category = decode_fixed_utf16(data, CATEGORY_RANGE, "dictionary category", FORMAT)?;
let description =
decode_fixed_utf16(data, DESCRIPTION_RANGE, "dictionary description", FORMAT)?;
let example = decode_fixed_utf16(data, EXAMPLE_RANGE, "dictionary example", FORMAT)?;
let mut extra = BTreeMap::new();
if let Some(example) = example {
extra.insert("example".to_owned(), example);
}
Ok(Metadata {
name,
category,
description,
extra,
})
}
fn parse_pinyin_table(reader: &mut Reader<'_>, pinyin_count: u16) -> Result<CodeTable> {
validate_count(
"pinyin table",
u64::from(pinyin_count),
reader.remaining() / 4,
FORMAT,
)?;
let mut code_table = vec![None; usize::from(pinyin_count)];
for _ in 0..pinyin_count {
let index = reader.read_u16()?;
let byte_len = reader.read_u16()? as usize;
let text = reader.read_utf16(byte_len, "pinyin table entry")?;
let table_index = usize::from(index);
if table_index >= code_table.len() {
code_table.resize_with(table_index + 1, || None);
}
if code_table[table_index].replace(text).is_some() {
return Err(Error::DuplicateCodeIndex {
format: FORMAT,
index: u64::from(index),
});
}
}
Ok(code_table)
}
fn parse_word_table(
reader: &mut Reader<'_>,
record_count: u32,
total_words: u32,
pinyin_count: u16,
code_table: &CodeTable,
) -> Result<Vec<Entry>> {
validate_count(
"word group",
u64::from(record_count),
reader.remaining() / 4,
FORMAT,
)?;
let mut entries = Vec::new();
for _ in 0..record_count {
let word_count = reader.read_u16()?;
let pinyin_byte_len = reader.read_u16()? as usize;
ensure_even("pinyin indices", reader.position(), pinyin_byte_len, FORMAT)?;
let indices_offset = reader.position();
let indices = reader.read_bytes(pinyin_byte_len)?;
let mut codes = indices
.chunks_exact(2)
.enumerate()
.map(|(index_position, pair)| {
let index = u16::from_le_bytes([pair[0], pair[1]]);
resolve_code_index(
code_table,
pinyin_count,
index,
indices_offset + index_position * 2,
)
})
.collect::<Result<Vec<_>>>()?;
for word_index in 0..word_count {
let word_byte_len = reader.read_u16()? as usize;
let word = reader.read_utf16(word_byte_len, "dictionary word")?;
let extension_len = reader.read_u16()? as usize;
let extension = reader.read_bytes(extension_len)?;
let weight = extension
.get(..2)
.map(|bytes| u16::from_le_bytes([bytes[0], bytes[1]]) as u32);
let code = if word_index + 1 == word_count {
std::mem::take(&mut codes)
} else {
codes.clone()
};
entries.push(Entry { word, code, weight });
}
}
if entries.len() != total_words as usize {
return Err(Error::CountMismatch {
format: FORMAT,
field: "word",
expected: u64::from(total_words),
actual: entries.len() as u64,
});
}
Ok(entries)
}
fn resolve_code_index(
code_table: &CodeTable,
pinyin_count: u16,
index: u16,
offset: usize,
) -> Result<String> {
if let Some(code) = code_table.get(usize::from(index)).and_then(Option::as_ref) {
return Ok(code.clone());
}
if pinyin_count != 0 {
if let Some(english_offset) = index.checked_sub(pinyin_count) {
if english_offset < ENGLISH_CODE_COUNT {
return Ok(char::from(b'a' + english_offset as u8).to_string());
}
}
}
Err(Error::InvalidCodeIndex {
format: FORMAT,
index: u64::from(index),
offset,
})
}
fn read_pinyin_count(data: &[u8]) -> Result<u16> {
let bytes = slice_at(data, PINYIN_COUNT_OFFSET, PINYIN_HEADER_LEN, FORMAT)?;
Ok(u16::from_le_bytes([bytes[0], bytes[1]]))
}