From ddbecf288240dd8ad2568df57415fed8fa3e25a8 Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Mon, 8 Sep 2014 14:52:14 -0400 Subject: [PATCH 1/6] use rust-encoding to correctly(?) set utf16 strings for cmdline args as expected --- ports/cef/lib.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ports/cef/lib.rs b/ports/cef/lib.rs index bf7aeec80e6..00c952c005d 100644 --- a/ports/cef/lib.rs +++ b/ports/cef/lib.rs @@ -37,6 +37,8 @@ extern crate native; extern crate libc; extern crate "url" as std_url; +extern crate encoding; + #[cfg(target_os="macos")] extern crate core_graphics; #[cfg(target_os="macos")] From e794f856cfe34d279acfa50385d8355320651a8f Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Mon, 8 Sep 2014 15:52:48 -0400 Subject: [PATCH 2/6] implement cef_string_utf8_to_utf16() --- ports/cef/string.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/ports/cef/string.rs b/ports/cef/string.rs index b878defb8b5..db4a61abac5 100644 --- a/ports/cef/string.rs +++ b/ports/cef/string.rs @@ -3,12 +3,15 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ +use encoding::{Encoding, EncodeStrict}; +use encoding::all::{UTF_16LE}; use eutil::fptr_is_null; use libc::{size_t, c_int, c_ushort,c_void}; use libc::types::os::arch::c95::wchar_t; use mem::{new0,newarray0,delete,deletearray}; use std::mem; use std::ptr; +use std::string; use types::{cef_string_utf16_t, cef_string_utf8_t, cef_string_wide_t}; use types::{cef_string_userfree_utf16_t, cef_string_userfree_utf8_t, cef_string_userfree_wide_t}; @@ -90,6 +93,16 @@ pub extern "C" fn cef_string_utf8_set(src: *const u8, src_len: size_t, output: * return 1; } +#[no_mangle] +pub extern "C" fn cef_string_utf8_to_utf16(src: *const u8, src_len: size_t, output: *mut cef_string_utf16_t) -> c_int { + unsafe { + let result = UTF_16LE.encode(string::raw::from_buf_len(src, src_len as uint).as_slice(), EncodeStrict); + if result.is_err() { return 0; } + cef_string_utf16_set(mem::transmute(result.unwrap().as_slice().as_ptr()), (src_len * 2) as size_t, output, 1); + } + 1 +} + #[no_mangle] pub extern "C" fn cef_string_utf16_clear(cs: *mut cef_string_utf16_t) { unsafe { From f657e76e1357bed27aeca83617f32a4c9a04168b Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Mon, 8 Sep 2014 15:53:04 -0400 Subject: [PATCH 3/6] implement cef_string_utf16_to_utf8() --- ports/cef/string.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/ports/cef/string.rs b/ports/cef/string.rs index db4a61abac5..2c3be54243e 100644 --- a/ports/cef/string.rs +++ b/ports/cef/string.rs @@ -11,6 +11,7 @@ use libc::types::os::arch::c95::wchar_t; use mem::{new0,newarray0,delete,deletearray}; use std::mem; use std::ptr; +use std::slice; use std::string; use types::{cef_string_utf16_t, cef_string_utf8_t, cef_string_wide_t}; use types::{cef_string_userfree_utf16_t, cef_string_userfree_utf8_t, cef_string_userfree_wide_t}; @@ -103,6 +104,24 @@ pub extern "C" fn cef_string_utf8_to_utf16(src: *const u8, src_len: size_t, outp 1 } +#[no_mangle] +pub extern "C" fn cef_string_utf16_to_utf8(src: *const u16, src_len: size_t, output: *mut cef_string_utf8_t) -> c_int { + unsafe { + slice::raw::buf_as_slice(src, src_len as uint, |ustr| { + match string::String::from_utf16(ustr) { + Some(str) => { + cef_string_utf8_set(str.as_bytes().as_ptr(), str.len() as size_t, output, 1); + return 1 as c_int; + }, + None => { + return 0 as c_int; + } + } + }); + } + 1 +} + #[no_mangle] pub extern "C" fn cef_string_utf16_clear(cs: *mut cef_string_utf16_t) { unsafe { From cb5d7b98bf38f648fcacec7901262003b2316c88 Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Sun, 12 Oct 2014 23:35:47 -0400 Subject: [PATCH 4/6] remove rust-encoding usage from cef --- ports/cef/lib.rs | 2 -- ports/cef/string.rs | 10 +++++----- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/ports/cef/lib.rs b/ports/cef/lib.rs index 00c952c005d..bf7aeec80e6 100644 --- a/ports/cef/lib.rs +++ b/ports/cef/lib.rs @@ -37,8 +37,6 @@ extern crate native; extern crate libc; extern crate "url" as std_url; -extern crate encoding; - #[cfg(target_os="macos")] extern crate core_graphics; #[cfg(target_os="macos")] diff --git a/ports/cef/string.rs b/ports/cef/string.rs index 2c3be54243e..47505f2a038 100644 --- a/ports/cef/string.rs +++ b/ports/cef/string.rs @@ -3,8 +3,6 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -use encoding::{Encoding, EncodeStrict}; -use encoding::all::{UTF_16LE}; use eutil::fptr_is_null; use libc::{size_t, c_int, c_ushort,c_void}; use libc::types::os::arch::c95::wchar_t; @@ -13,6 +11,7 @@ use std::mem; use std::ptr; use std::slice; use std::string; +use std::str; use types::{cef_string_utf16_t, cef_string_utf8_t, cef_string_wide_t}; use types::{cef_string_userfree_utf16_t, cef_string_userfree_utf8_t, cef_string_userfree_wide_t}; @@ -97,9 +96,10 @@ pub extern "C" fn cef_string_utf8_set(src: *const u8, src_len: size_t, output: * #[no_mangle] pub extern "C" fn cef_string_utf8_to_utf16(src: *const u8, src_len: size_t, output: *mut cef_string_utf16_t) -> c_int { unsafe { - let result = UTF_16LE.encode(string::raw::from_buf_len(src, src_len as uint).as_slice(), EncodeStrict); - if result.is_err() { return 0; } - cef_string_utf16_set(mem::transmute(result.unwrap().as_slice().as_ptr()), (src_len * 2) as size_t, output, 1); + slice::raw::buf_as_slice(src, src_len as uint, |result| { + let enc = str::from_utf8(result).unwrap().utf16_units().collect::>(); + cef_string_utf16_set(enc.as_ptr(), (src_len * 2) as size_t, output, 1); + }); } 1 } From 62deac93461b822547ed2712af0ca267941d1b46 Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Mon, 13 Oct 2014 01:32:32 -0400 Subject: [PATCH 5/6] use enc.len() for cef_string_utf8_to_utf16 conversion size --- ports/cef/string.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ports/cef/string.rs b/ports/cef/string.rs index 47505f2a038..dd1e7ae5c51 100644 --- a/ports/cef/string.rs +++ b/ports/cef/string.rs @@ -98,7 +98,7 @@ pub extern "C" fn cef_string_utf8_to_utf16(src: *const u8, src_len: size_t, outp unsafe { slice::raw::buf_as_slice(src, src_len as uint, |result| { let enc = str::from_utf8(result).unwrap().utf16_units().collect::>(); - cef_string_utf16_set(enc.as_ptr(), (src_len * 2) as size_t, output, 1); + cef_string_utf16_set(enc.as_ptr(), enc.len() as size_t, output, 1); }); } 1 From 5bbce40efe5e6e05bb2d6b5a805ee04be19d0484 Mon Sep 17 00:00:00 2001 From: Mike Blumenkrantz Date: Mon, 13 Oct 2014 01:46:00 -0400 Subject: [PATCH 6/6] handle str::from_utf8() failure, improve return calls --- ports/cef/string.rs | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/ports/cef/string.rs b/ports/cef/string.rs index dd1e7ae5c51..855fa1226ec 100644 --- a/ports/cef/string.rs +++ b/ports/cef/string.rs @@ -97,11 +97,16 @@ pub extern "C" fn cef_string_utf8_set(src: *const u8, src_len: size_t, output: * pub extern "C" fn cef_string_utf8_to_utf16(src: *const u8, src_len: size_t, output: *mut cef_string_utf16_t) -> c_int { unsafe { slice::raw::buf_as_slice(src, src_len as uint, |result| { - let enc = str::from_utf8(result).unwrap().utf16_units().collect::>(); - cef_string_utf16_set(enc.as_ptr(), enc.len() as size_t, output, 1); - }); + match str::from_utf8(result) { + Some(enc) => { + let conv = enc.utf16_units().collect::>(); + cef_string_utf16_set(conv.as_ptr(), conv.len() as size_t, output, 1); + 1 + }, + None => 0 + } + }) } - 1 } #[no_mangle] @@ -111,15 +116,12 @@ pub extern "C" fn cef_string_utf16_to_utf8(src: *const u16, src_len: size_t, out match string::String::from_utf16(ustr) { Some(str) => { cef_string_utf8_set(str.as_bytes().as_ptr(), str.len() as size_t, output, 1); - return 1 as c_int; + 1 as c_int }, - None => { - return 0 as c_int; - } + None => 0 as c_int } - }); + }) } - 1 } #[no_mangle]