use std::io::{self, Write};
pub fn char_to_wchar(c: char) -> u16 {
if (c as u32) <= 0xFFFF {
c as u16
} else {
0xFFFD
}
}
pub(crate) trait BinaryProducer {
fn write<W: Write>(&self, writer: &mut ByteWriter<W>) -> io::Result<()>;
}
pub struct ByteWriter<W = Vec<u8>> {
buf: W,
position: usize,
}
impl ByteWriter {
pub fn new() -> Self {
Self::with_writer(Vec::new())
}
pub fn into_bytes(self) -> Vec<u8> {
self.buf
}
pub fn as_bytes(&self) -> &[u8] {
&self.buf
}
}
impl<W: Write> ByteWriter<W> {
pub(crate) fn with_writer(buf: W) -> Self {
Self { buf, position: 0 }
}
pub fn position(&self) -> usize {
self.position
}
pub fn write_u8(&mut self, v: u8) -> io::Result<()> {
self.write_bytes(&[v])
}
pub fn write_u16(&mut self, v: u16) -> io::Result<()> {
self.write_bytes(&v.to_le_bytes())
}
pub fn write_u32(&mut self, v: u32) -> io::Result<()> {
self.write_bytes(&v.to_le_bytes())
}
pub fn write_i8(&mut self, v: i8) -> io::Result<()> {
self.write_bytes(&v.to_le_bytes())
}
pub fn write_i16(&mut self, v: i16) -> io::Result<()> {
self.write_bytes(&v.to_le_bytes())
}
pub fn write_i32(&mut self, v: i32) -> io::Result<()> {
self.write_bytes(&v.to_le_bytes())
}
pub fn write_f64(&mut self, v: f64) -> io::Result<()> {
self.write_bytes(&v.to_le_bytes())
}
pub fn write_bytes(&mut self, data: &[u8]) -> io::Result<()> {
let end = self
.position
.checked_add(data.len())
.ok_or_else(|| io::Error::other("binary size overflow"))?;
self.buf.write_all(data)?;
self.position = end;
Ok(())
}
pub fn write_hwp_string(&mut self, text: &str) -> io::Result<()> {
let mut units = 0usize;
let mut last = 0u16;
for unit in text.encode_utf16().take(u16::MAX as usize) {
units += 1;
last = unit;
}
if (0xD800..=0xDBFF).contains(&last) {
units -= 1;
}
self.write_u16(units as u16)?;
for unit in text.encode_utf16().take(units) {
self.write_u16(unit)?;
}
Ok(())
}
pub fn write_color_ref(&mut self, color: u32) -> io::Result<()> {
self.write_u32(color)
}
pub fn write_zeros(&mut self, mut count: usize) -> io::Result<()> {
const ZERO: [u8; 256] = [0; 256];
while count != 0 {
let n = count.min(ZERO.len());
self.write_bytes(&ZERO[..n])?;
count -= n;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::parser::byte_reader::ByteReader;
#[test]
fn test_char_to_wchar_bmp_passthrough() {
assert_eq!(char_to_wchar('A'), 0x0041);
assert_eq!(char_to_wchar('가'), 0xAC00);
assert_eq!(char_to_wchar('\u{FFFF}'), 0xFFFF);
}
#[test]
fn test_char_to_wchar_astral_replaces_instead_of_truncating() {
let c = '\u{1F536}';
assert_ne!(c as u32 as u16, 0xFFFD); assert_eq!(char_to_wchar(c), 0xFFFD);
}
#[test]
fn test_write_u8() {
let mut w = ByteWriter::new();
w.write_u8(0x42).unwrap();
assert_eq!(w.into_bytes(), [0x42]);
}
#[test]
fn test_write_u16_le() {
let mut w = ByteWriter::new();
w.write_u16(0x1234).unwrap();
assert_eq!(w.into_bytes(), [0x34, 0x12]);
}
#[test]
fn test_write_u32_le() {
let mut w = ByteWriter::new();
w.write_u32(0x12345678).unwrap();
assert_eq!(w.into_bytes(), [0x78, 0x56, 0x34, 0x12]);
}
#[test]
fn test_write_i8() {
let mut w = ByteWriter::new();
w.write_i8(-1).unwrap();
assert_eq!(w.into_bytes(), [0xFF]);
}
#[test]
fn test_write_i16_negative() {
let mut w = ByteWriter::new();
w.write_i16(-100).unwrap();
let bytes = w.into_bytes();
let mut reader = ByteReader::new(&bytes);
assert_eq!(reader.read_i16().unwrap(), -100);
}
#[test]
fn test_write_i32_negative() {
let mut w = ByteWriter::new();
w.write_i32(-7200).unwrap();
let bytes = w.into_bytes();
let mut reader = ByteReader::new(&bytes);
assert_eq!(reader.read_i32().unwrap(), -7200);
}
#[test]
fn test_write_bytes() {
let mut w = ByteWriter::new();
w.write_bytes(&[0x01, 0x02, 0x03]).unwrap();
assert_eq!(w.into_bytes(), [0x01, 0x02, 0x03]);
}
#[test]
fn test_write_hwp_string_korean() {
let mut w = ByteWriter::new();
w.write_hwp_string("한글").unwrap();
let bytes = w.into_bytes();
let mut reader = ByteReader::new(&bytes);
assert_eq!(reader.read_hwp_string().unwrap(), "한글");
}
#[test]
fn test_write_hwp_string_ascii() {
let mut w = ByteWriter::new();
w.write_hwp_string("ABC").unwrap();
let bytes = w.into_bytes();
let mut reader = ByteReader::new(&bytes);
assert_eq!(reader.read_hwp_string().unwrap(), "ABC");
}
#[test]
fn test_write_hwp_string_empty() {
let mut w = ByteWriter::new();
w.write_hwp_string("").unwrap();
let bytes = w.into_bytes();
let mut reader = ByteReader::new(&bytes);
assert_eq!(reader.read_hwp_string().unwrap(), "");
}
#[test]
fn test_write_hwp_string_mixed() {
let mut w = ByteWriter::new();
w.write_hwp_string("Hello 세계!").unwrap();
let bytes = w.into_bytes();
let mut reader = ByteReader::new(&bytes);
assert_eq!(reader.read_hwp_string().unwrap(), "Hello 세계!");
}
#[test]
fn test_write_hwp_string_overlong_truncates_instead_of_wrapping() {
let s: String = "A".repeat(u16::MAX as usize + 10);
let mut w = ByteWriter::new();
w.write_hwp_string(&s).unwrap();
let bytes = w.into_bytes();
let mut reader = ByteReader::new(&bytes);
let read_back = reader.read_hwp_string().unwrap();
assert_eq!(read_back.encode_utf16().count(), u16::MAX as usize);
}
#[test]
fn test_write_color_ref() {
let mut w = ByteWriter::new();
w.write_color_ref(0x00FF8040).unwrap();
let bytes = w.into_bytes();
let mut reader = ByteReader::new(&bytes);
assert_eq!(reader.read_color_ref().unwrap(), 0x00FF8040);
}
#[test]
fn test_write_zeros() {
let mut w = ByteWriter::new();
w.write_zeros(5).unwrap();
assert_eq!(w.into_bytes(), [0, 0, 0, 0, 0]);
}
#[test]
fn test_position() {
let mut w = ByteWriter::new();
assert_eq!(w.position(), 0);
w.write_u16(0x1234).unwrap();
assert_eq!(w.position(), 2);
w.write_u32(0).unwrap();
assert_eq!(w.position(), 6);
}
#[test]
fn test_sequential_writes_roundtrip() {
let mut w = ByteWriter::new();
w.write_u8(42).unwrap();
w.write_u16(1000).unwrap();
w.write_i32(-500).unwrap();
let bytes = w.into_bytes();
let mut reader = ByteReader::new(&bytes);
assert_eq!(reader.read_u8().unwrap(), 42);
assert_eq!(reader.read_u16().unwrap(), 1000);
assert_eq!(reader.read_i32().unwrap(), -500);
assert!(reader.is_empty());
}
}