Skip to content

Commit b60f41e

Browse files
committed
[FIX] Accept iconv charset names like CP949 in CEA-708 output
1 parent 604dad8 commit b60f41e

3 files changed

Lines changed: 124 additions & 3 deletions

File tree

‎mac/libccx_rust.a‎

26.5 MB
Binary file not shown.

‎src/rust/src/decoder/output.rs‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::os::windows::io::{FromRawHandle, IntoRawHandle};
77
use std::{fs::File, io::Write};
88

99
use crate::{bindings::*, utils::is_true};
10+
use encoding_rs::{Encoding, BIG5, EUC_JP, EUC_KR, GBK};
1011

1112
use log::{debug, warn};
1213

@@ -124,6 +125,23 @@ pub fn is_utf16_charset(charset: &str) -> bool {
124125
|| upper.contains("UCS_2")
125126
}
126127

128+
/// Find the encoding for a CEA-708 service charset name (`--service "1[name]"`).
129+
///
130+
/// The name was already accepted by `iconv_open()` in `dtvcc_writer_init()`, so it
131+
/// can be an iconv name that encoding_rs does not know as a label. Map the iconv
132+
/// names of the encodings encoding_rs does support to that encoding.
133+
pub fn find_charset_encoding(charset: &str) -> Option<&'static Encoding> {
134+
Encoding::for_label(charset.as_bytes()).or_else(|| {
135+
match charset.to_ascii_uppercase().as_str() {
136+
"CP949" | "UHC" | "EUCKR" => Some(EUC_KR),
137+
"CP936" => Some(GBK),
138+
"CP950" => Some(BIG5),
139+
"EUCJP" => Some(EUC_JP),
140+
_ => None,
141+
}
142+
})
143+
}
144+
127145
/// Convert from CEA-708 color representation to hex code
128146
///
129147
/// Two bits are specified for each red, green, and blue color value which defines the
@@ -142,6 +160,26 @@ pub fn color_to_hex(color: u8) -> (u8, u8, u8) {
142160
mod tests {
143161
use super::*;
144162

163+
#[test]
164+
fn test_find_charset_encoding_iconv_names() {
165+
// Names iconv accepts that are not encoding_rs labels
166+
assert_eq!(find_charset_encoding("CP949"), Some(EUC_KR));
167+
assert_eq!(find_charset_encoding("cp949"), Some(EUC_KR));
168+
assert_eq!(find_charset_encoding("UHC"), Some(EUC_KR));
169+
assert_eq!(find_charset_encoding("EUCKR"), Some(EUC_KR));
170+
assert_eq!(find_charset_encoding("CP936"), Some(GBK));
171+
assert_eq!(find_charset_encoding("CP950"), Some(BIG5));
172+
assert_eq!(find_charset_encoding("EUCJP"), Some(EUC_JP));
173+
// Labels encoding_rs already knows are unchanged
174+
assert_eq!(find_charset_encoding("EUC-KR"), Some(EUC_KR));
175+
assert_eq!(
176+
find_charset_encoding("SHIFT_JIS"),
177+
Some(encoding_rs::SHIFT_JIS)
178+
);
179+
// An encoding encoding_rs doesn't have
180+
assert_eq!(find_charset_encoding("JOHAB"), None);
181+
}
182+
145183
#[test]
146184
fn test_write_char_utf16_mode() {
147185
let mut buf = Vec::new();

‎src/rust/src/decoder/tv_screen.rs‎

Lines changed: 86 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,14 @@ use std::{ffi::CStr, fs::File};
1313
#[cfg(windows)]
1414
use crate::bindings::_get_osfhandle;
1515

16-
use super::output::{color_to_hex, is_utf16_charset, write_char, Writer};
16+
use super::output::{color_to_hex, find_charset_encoding, is_utf16_charset, write_char, Writer};
1717
use super::timing::{get_scc_time_str, get_time_str};
1818
use super::{CCX_DTVCC_SCREENGRID_COLUMNS, CCX_DTVCC_SCREENGRID_ROWS};
1919
use crate::{
2020
bindings::*,
2121
current_fps,
2222
utils::{is_false, is_true},
2323
};
24-
use encoding_rs::Encoding;
2524

2625
use log::{debug, warn};
2726

@@ -273,7 +272,7 @@ impl dtvcc_tv_screen {
273272
debug!("Charset: {charset}");
274273

275274
// Look up the encoding by label (name)
276-
if let Some(encoding) = Encoding::for_label(charset.as_bytes()) {
275+
if let Some(encoding) = find_charset_encoding(charset) {
277276
let (cow, _encoding_used, had_errors) = encoding.decode(&buf);
278277
if had_errors {
279278
println!("Warning: Had errors during encoding from {charset} to UTF-8");
@@ -282,6 +281,18 @@ impl dtvcc_tv_screen {
282281
println!("Warning: Encoding specified ({}) does not match encoding detected ({})",charset,_encoding_used.name());
283282
}
284283
writer.write_to_file(cow.as_bytes())?;
284+
} else {
285+
// iconv accepted the name but encoding_rs can't convert from it:
286+
// write the text unconverted instead of dropping it.
287+
static WARNED: std::sync::Once = std::sync::Once::new();
288+
WARNED.call_once(|| {
289+
if let Some(logger) = lib_ccxr::util::log::logger() {
290+
logger.log_info(&format_args!(
291+
"[CEA-708] Can't convert from charset \"{charset}\", writing the text unconverted\n"
292+
));
293+
}
294+
});
295+
writer.write_to_file(&buf)?;
285296
}
286297
}
287298
} else {
@@ -833,6 +844,78 @@ mod test {
833844
);
834845
}
835846

847+
/// Runs write_row on row 0 of `screen` with a --service charset and
848+
/// returns what was written.
849+
#[cfg(unix)]
850+
fn write_row_with_charset(screen: &dtvcc_tv_screen, charset: &str) -> Vec<u8> {
851+
use std::ffi::CString;
852+
use std::io::{Read, Seek, SeekFrom};
853+
use std::os::unix::io::AsRawFd;
854+
use tempfile::tempfile;
855+
856+
let charset = CString::new(charset).unwrap();
857+
let mut writer_ctx = get_zero_allocated_obj::<dtvcc_writer_ctx>();
858+
let transcript_settings = get_zero_allocated_obj::<ccx_encoders_transcript_format>();
859+
let mut temp_file = tempfile().expect("Failed to create temp file");
860+
writer_ctx.fd = temp_file.as_raw_fd();
861+
writer_ctx.filename = std::ptr::null_mut();
862+
writer_ctx.charset = charset.as_ptr() as *mut _;
863+
// dtvcc_writer_init() opened an iconv descriptor for the charset
864+
writer_ctx.cd = 1 as iconv_t;
865+
866+
let mut counter = 0u32;
867+
let mut writer = Writer::new(
868+
&mut counter,
869+
0,
870+
ccx_output_format::CCX_OF_SRT,
871+
&mut writer_ctx,
872+
1,
873+
&transcript_settings,
874+
0,
875+
b"\r\n",
876+
);
877+
screen
878+
.write_row(&mut writer, 0, false)
879+
.expect("write_row should succeed");
880+
881+
temp_file.seek(SeekFrom::Start(0)).unwrap();
882+
let mut output = Vec::new();
883+
temp_file.read_to_end(&mut output).unwrap();
884+
output
885+
}
886+
887+
#[test]
888+
#[cfg(unix)]
889+
fn test_write_row_iconv_charset_name() {
890+
// "Hi 가" in EUC-KR / CP949: 가 is 0xB0A1
891+
let mut screen = get_zero_allocated_obj::<dtvcc_tv_screen>();
892+
screen.chars[0][0] = dtvcc_symbol::new(b'H' as u16);
893+
screen.chars[0][1] = dtvcc_symbol::new(b'i' as u16);
894+
screen.chars[0][2] = dtvcc_symbol::new(b' ' as u16);
895+
screen.chars[0][3] = dtvcc_symbol::new(0xB0A1);
896+
897+
for charset in ["EUC-KR", "CP949", "UHC", "EUCKR"] {
898+
let output = write_row_with_charset(&screen, charset);
899+
assert_eq!(
900+
String::from_utf8_lossy(&output),
901+
"Hi 가",
902+
"charset {charset}"
903+
);
904+
}
905+
}
906+
907+
#[test]
908+
#[cfg(unix)]
909+
fn test_write_row_unknown_charset_keeps_text() {
910+
let mut screen = get_zero_allocated_obj::<dtvcc_tv_screen>();
911+
screen.chars[0][0] = dtvcc_symbol::new(b'H' as u16);
912+
screen.chars[0][1] = dtvcc_symbol::new(b'i' as u16);
913+
914+
// iconv knows JOHAB, encoding_rs doesn't
915+
let output = write_row_with_charset(&screen, "JOHAB");
916+
assert_eq!(output, b"Hi");
917+
}
918+
836919
#[test]
837920
#[cfg(unix)]
838921
fn test_writer_output_with_valid_fd() {

0 commit comments

Comments
 (0)