use snafu::Snafu;
use std::io::{self, Cursor};
#[derive(Debug, Snafu)]
pub enum Hwp3SpecialCharError {
#[snafu(display("잘못된 특수 문자 코드입니다: {code}"))]
InvalidCode { code: u16 },
#[snafu(display("입출력 오류가 발생했습니다: {source}"))]
IoError { source: io::Error },
}
impl From<io::Error> for Hwp3SpecialCharError {
fn from(error: io::Error) -> Self {
Hwp3SpecialCharError::IoError { source: error }
}
}
#[derive(Debug)]
pub enum Hwp3SpecialChar {
FieldCode {
length: u32,
data: Vec<u8>,
}, Bookmark {
length: u32,
data: Vec<u8>,
}, DateFormat {
length: u32,
data: Vec<u8>,
}, DateCode {
length: u32,
data: Vec<u8>,
}, Tab {
length: u32,
data: Vec<u8>,
}, TableBoxEqButtonHypertext {
length: u32,
data: Vec<u8>,
}, Picture {
length: u32,
picture_type: u8,
frame_header: Option<crate::parser::hwp3::drawing::Hwp3DrawingObjectFrameHeader>,
drawing_objects: Vec<crate::parser::hwp3::drawing::Hwp3DrawingObject>,
ole_info: Option<crate::parser::hwp3::ole::Hwp3OleInfo>,
raw_data: Vec<u8>,
}, Line {
length: u32,
data: Vec<u8>,
}, HiddenComment {
length: u32,
data: Vec<u8>,
}, HeaderFooter {
length: u32,
data: Vec<u8>,
}, FootnoteEndnote {
length: u32,
data: Vec<u8>,
}, NumberCode {
length: u32,
data: Vec<u8>,
}, NewNumber {
length: u32,
data: Vec<u8>,
}, PageNumber {
length: u32,
data: Vec<u8>,
}, OddEvenPage {
length: u32,
data: Vec<u8>,
}, MailMerge {
length: u32,
data: Vec<u8>,
}, CharOverlap {
length: u32,
data: Vec<u8>,
}, Hyphen {
length: u32,
data: Vec<u8>,
}, TitleTableFigureMark {
length: u32,
data: Vec<u8>,
}, IndexMark {
length: u32,
data: Vec<u8>,
}, OutlineShapeNumber {
length: u32,
data: Vec<u8>,
}, CrossReference {
length: u32,
data: Vec<u8>,
}, BundleBlank {
length: u32,
data: Vec<u8>,
}, FixedBlank {
length: u32,
data: Vec<u8>,
}, Unknown {
code: u16,
length: u32,
data: Vec<u8>,
},
}
impl Hwp3SpecialChar {
pub fn parse(code: u16, length: u32, data: Vec<u8>) -> Result<Self, Hwp3SpecialCharError> {
match code {
5 => Ok(Hwp3SpecialChar::FieldCode { length, data }),
6 => Ok(Hwp3SpecialChar::Bookmark { length, data }),
7 => Ok(Hwp3SpecialChar::DateFormat { length, data }),
8 => Ok(Hwp3SpecialChar::DateCode { length, data }),
9 => Ok(Hwp3SpecialChar::Tab { length, data }),
10 => Ok(Hwp3SpecialChar::TableBoxEqButtonHypertext { length, data }),
11 => {
let mut picture_type = 0;
let mut frame_header = None;
let mut drawing_objects = Vec::new();
let mut ole_info = None;
if data.len() >= 348 {
picture_type = data[74];
if picture_type == 3 {
let mut cursor = Cursor::new(&data[348..]);
if let Ok(frame) =
crate::parser::hwp3::drawing::Hwp3DrawingObjectFrameHeader::read(
&mut cursor,
)
{
for _ in 0..frame.object_count {
if let Ok(obj) =
crate::parser::hwp3::drawing::Hwp3DrawingObject::read(
&mut cursor,
)
{
drawing_objects.push(obj);
} else {
break;
}
}
frame_header = Some(frame);
}
} else if picture_type == 1 {
let mut cursor = Cursor::new(&data[348..]);
let ext_len = (data.len() - 348) as u32;
if let Ok(ole) =
crate::parser::hwp3::ole::Hwp3OleInfo::read(&mut cursor, ext_len)
{
ole_info = Some(ole);
}
}
}
Ok(Hwp3SpecialChar::Picture {
length,
picture_type,
frame_header,
drawing_objects,
ole_info,
raw_data: data,
})
}
14 => Ok(Hwp3SpecialChar::Line { length, data }),
15 => Ok(Hwp3SpecialChar::HiddenComment { length, data }),
16 => Ok(Hwp3SpecialChar::HeaderFooter { length, data }),
17 => Ok(Hwp3SpecialChar::FootnoteEndnote { length, data }),
18 => Ok(Hwp3SpecialChar::NumberCode { length, data }),
19 => Ok(Hwp3SpecialChar::NewNumber { length, data }),
20 => Ok(Hwp3SpecialChar::PageNumber { length, data }),
21 => Ok(Hwp3SpecialChar::OddEvenPage { length, data }),
22 => Ok(Hwp3SpecialChar::MailMerge { length, data }),
23 => Ok(Hwp3SpecialChar::CharOverlap { length, data }),
24 => Ok(Hwp3SpecialChar::Hyphen { length, data }),
25 => Ok(Hwp3SpecialChar::TitleTableFigureMark { length, data }),
26 => Ok(Hwp3SpecialChar::IndexMark { length, data }),
28 => Ok(Hwp3SpecialChar::OutlineShapeNumber { length, data }),
29 => Ok(Hwp3SpecialChar::CrossReference { length, data }),
30 => Ok(Hwp3SpecialChar::BundleBlank { length, data }),
31 => Ok(Hwp3SpecialChar::FixedBlank { length, data }),
_ => Ok(Hwp3SpecialChar::Unknown { code, length, data }),
}
}
}