lyric_decoder/
lib.rs

1use std::{
2    ffi::{c_char, CStr},
3    ptr::NonNull,
4};
5mod c_str;
6use c_str::COwnedString;
7
8extern "C" {
9    fn krcdecode(
10        src: *mut ::std::os::raw::c_char,
11        src_len: ::std::os::raw::c_int,
12    ) -> *mut ::std::os::raw::c_char;
13    fn qrcdecode(
14        src: *mut ::std::os::raw::c_char,
15        src_len: ::std::os::raw::c_int,
16    ) -> *mut ::std::os::raw::c_char;
17}
18
19pub fn krc_decode(resp: &mut [c_char]) -> Option<COwnedString> {
20    unsafe {
21        NonNull::new(krcdecode(resp.as_mut_ptr(), resp.len() as _)).map(|ptr| {
22            let len = CStr::from_ptr(ptr.as_ptr() as _).to_bytes().len() as isize;
23            COwnedString::from_raw_parts(ptr, len)
24        })
25    }
26}
27
28pub fn qrc_decode(resp: &mut [c_char]) -> Option<COwnedString> {
29    unsafe {
30        NonNull::new(qrcdecode(resp.as_mut_ptr(), resp.len() as _)).map(|ptr| {
31            let len = CStr::from_ptr(ptr.as_ptr() as _).to_bytes().len() as isize;
32            COwnedString::from_raw_parts(ptr, len)
33        })
34    }
35}