Bug 1617369 - Reformat rsdparsa_capi/ using rustfmt r=dminor

Differential Revision: https://phabricator.services.mozilla.com/D65897

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Sylvestre Ledru 2020-03-09 11:32:26 +00:00
parent cad36d87f0
commit 558c5bec85
5 changed files with 700 additions and 480 deletions

File diff suppressed because it is too large Load Diff

View File

@ -2,13 +2,13 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
extern crate rsdparsa;
extern crate libc;
extern crate nserror;
extern crate rsdparsa;
use std::ffi::CString;
use std::ptr;
use std::os::raw::c_char;
use std::ptr;
use libc::size_t;
@ -16,29 +16,32 @@ use std::rc::Rc;
use std::convert::{TryFrom, TryInto};
use nserror::{nsresult, NS_OK, NS_ERROR_INVALID_ARG};
use rsdparsa::{SdpTiming, SdpBandwidth, SdpSession};
use nserror::{nsresult, NS_ERROR_INVALID_ARG, NS_OK};
use rsdparsa::address::ExplicitlyTypedAddress;
use rsdparsa::anonymizer::{AnonymizingClone, StatefulSdpAnonymizer};
use rsdparsa::attribute_type::SdpAttribute;
use rsdparsa::error::SdpParserError;
use rsdparsa::media_type::{SdpMediaValue, SdpProtocolValue};
use rsdparsa::attribute_type::{SdpAttribute};
use rsdparsa::anonymizer::{StatefulSdpAnonymizer, AnonymizingClone};
use rsdparsa::address::ExplicitlyTypedAddress;
use rsdparsa::{SdpBandwidth, SdpSession, SdpTiming};
pub mod types;
pub mod network;
pub mod attribute;
pub mod media_section;
pub mod network;
pub mod types;
use network::{
get_bandwidth, origin_view_helper, RustAddressType, RustSdpConnection, RustSdpOrigin,
};
pub use types::{StringView, NULL_STRING};
use network::{RustSdpOrigin, origin_view_helper, RustSdpConnection,
get_bandwidth, RustAddressType};
#[no_mangle]
pub unsafe extern "C" fn parse_sdp(sdp: StringView,
fail_on_warning: bool,
session: *mut *const SdpSession,
error: *mut *const SdpParserError) -> nsresult {
let sdp_str:String = match sdp.try_into() {
pub unsafe extern "C" fn parse_sdp(
sdp: StringView,
fail_on_warning: bool,
session: *mut *const SdpSession,
error: *mut *const SdpParserError,
) -> nsresult {
let sdp_str: String = match sdp.try_into() {
Ok(string) => string,
Err(boxed_error) => {
*session = ptr::null();
@ -53,13 +56,13 @@ pub unsafe extern "C" fn parse_sdp(sdp: StringView,
let parser_result = rsdparsa::parse_sdp(&sdp_str, fail_on_warning);
match parser_result {
Ok(mut parsed) => {
*error = match parsed.warnings.len(){
*error = match parsed.warnings.len() {
0 => ptr::null(),
_ => Box::into_raw(Box::new(parsed.warnings.remove(0))),
};
*session = Rc::into_raw(Rc::new(parsed));
NS_OK
},
}
Err(e) => {
*session = ptr::null();
println!("Error parsing SDP in rust: {}", e);
@ -70,8 +73,12 @@ pub unsafe extern "C" fn parse_sdp(sdp: StringView,
}
#[no_mangle]
pub unsafe extern "C" fn create_anonymized_sdp_clone(session: *const SdpSession) -> *const SdpSession {
Rc::into_raw(Rc::new((*session).masked_clone(&mut StatefulSdpAnonymizer::new())))
pub unsafe extern "C" fn create_anonymized_sdp_clone(
session: *const SdpSession,
) -> *const SdpSession {
Rc::into_raw(Rc::new(
(*session).masked_clone(&mut StatefulSdpAnonymizer::new()),
))
}
#[no_mangle]
@ -91,20 +98,20 @@ pub unsafe extern "C" fn sdp_new_reference(session: *mut SdpSession) -> *const S
#[no_mangle]
pub unsafe extern "C" fn sdp_get_error_line_num(error: *mut SdpParserError) -> size_t {
match *error {
SdpParserError::Line {line_number, ..} |
SdpParserError::Unsupported { line_number, ..} |
SdpParserError::Sequence {line_number, ..} => line_number
SdpParserError::Line { line_number, .. }
| SdpParserError::Unsupported { line_number, .. }
| SdpParserError::Sequence { line_number, .. } => line_number,
}
}
#[no_mangle]
// Callee must check that a nullptr is not returned
pub unsafe extern "C" fn sdp_get_error_message(error: *mut SdpParserError) -> *mut c_char {
let message = format!("{}", *error);
return match CString::new(message.as_str()) {
let message = format!("{}", *error);
return match CString::new(message.as_str()) {
Ok(c_char_ptr) => c_char_ptr.into_raw(),
Err(_) => 0 as *mut c_char,
}
};
}
#[no_mangle]
@ -121,12 +128,12 @@ pub unsafe extern "C" fn sdp_free_error(error: *mut SdpParserError) {
}
#[no_mangle]
pub unsafe extern "C" fn get_version(session: *const SdpSession) -> u64 {
pub unsafe extern "C" fn get_version(session: *const SdpSession) -> u64 {
(*session).get_version()
}
#[no_mangle]
pub unsafe extern "C" fn sdp_get_origin(session: *const SdpSession) -> RustSdpOrigin {
pub unsafe extern "C" fn sdp_get_origin(session: *const SdpSession) -> RustSdpOrigin {
origin_view_helper((*session).get_origin())
}
@ -141,36 +148,47 @@ pub unsafe extern "C" fn sdp_session_has_connection(session: *const SdpSession)
}
#[no_mangle]
pub unsafe extern "C" fn sdp_get_session_connection(session: *const SdpSession,
connection: *mut RustSdpConnection) -> nsresult {
pub unsafe extern "C" fn sdp_get_session_connection(
session: *const SdpSession,
connection: *mut RustSdpConnection,
) -> nsresult {
match (*session).connection {
Some(ref c) => {
*connection = RustSdpConnection::from(c);
NS_OK
},
None => NS_ERROR_INVALID_ARG
}
None => NS_ERROR_INVALID_ARG,
}
}
#[no_mangle]
pub unsafe extern "C" fn sdp_add_media_section(session: *mut SdpSession,
media_type: u32, direction: u32,
port: u16, protocol: u32,
addr_type: u32, address: StringView) -> nsresult {
pub unsafe extern "C" fn sdp_add_media_section(
session: *mut SdpSession,
media_type: u32,
direction: u32,
port: u16,
protocol: u32,
addr_type: u32,
address: StringView,
) -> nsresult {
let addr_type = match RustAddressType::try_from(addr_type) {
Ok(a) => a.into(),
Err(e) => { return e;},
Err(e) => {
return e;
}
};
let address_string:String = match address.try_into(){
Ok(x) => x,
Err(boxed_error) => {
println!("Error while parsing string, description: {}", boxed_error);
return NS_ERROR_INVALID_ARG;
}
let address_string: String = match address.try_into() {
Ok(x) => x,
Err(boxed_error) => {
println!("Error while parsing string, description: {}", boxed_error);
return NS_ERROR_INVALID_ARG;
}
};
let address = match ExplicitlyTypedAddress::try_from((addr_type, address_string.as_str())) {
Ok(a) => a,
Err(_) => {return NS_ERROR_INVALID_ARG;},
Err(_) => {
return NS_ERROR_INVALID_ARG;
}
};
let media_type = match media_type {
@ -178,8 +196,8 @@ pub unsafe extern "C" fn sdp_add_media_section(session: *mut SdpSession,
1 => SdpMediaValue::Video, // MediaType::kVideo
3 => SdpMediaValue::Application, // MediaType::kApplication
_ => {
return NS_ERROR_INVALID_ARG;
}
return NS_ERROR_INVALID_ARG;
}
};
let protocol = match protocol {
20 => SdpProtocolValue::RtpSavpf, // Protocol::kRtpSavpf
@ -191,21 +209,21 @@ pub unsafe extern "C" fn sdp_add_media_section(session: *mut SdpSession,
38 => SdpProtocolValue::UdpDtlsSctp, // Protocol::kUdpDtlsSctp
39 => SdpProtocolValue::TcpDtlsSctp, // Protocol::kTcpDtlsSctp
_ => {
return NS_ERROR_INVALID_ARG;
}
return NS_ERROR_INVALID_ARG;
}
};
let direction = match direction {
1 => SdpAttribute::Sendonly,
2 => SdpAttribute::Recvonly,
3 => SdpAttribute::Sendrecv,
_ => {
return NS_ERROR_INVALID_ARG;
}
return NS_ERROR_INVALID_ARG;
}
};
match (*session).add_media(media_type, direction, port as u32, protocol, address) {
Ok(_) => NS_OK,
Err(_) => NS_ERROR_INVALID_ARG
Err(_) => NS_ERROR_INVALID_ARG,
}
}
@ -218,7 +236,10 @@ pub struct RustSdpTiming {
impl<'a> From<&'a SdpTiming> for RustSdpTiming {
fn from(timing: &SdpTiming) -> Self {
RustSdpTiming {start: timing.start, stop: timing.stop}
RustSdpTiming {
start: timing.start,
stop: timing.stop,
}
}
}
@ -228,14 +249,16 @@ pub unsafe extern "C" fn sdp_session_has_timing(session: *const SdpSession) -> b
}
#[no_mangle]
pub unsafe extern "C" fn sdp_session_timing(session: *const SdpSession,
timing: *mut RustSdpTiming) -> nsresult {
pub unsafe extern "C" fn sdp_session_timing(
session: *const SdpSession,
timing: *mut RustSdpTiming,
) -> nsresult {
match (*session).timing {
Some(ref t) => {
*timing = RustSdpTiming::from(t);
NS_OK
},
None => NS_ERROR_INVALID_ARG
}
None => NS_ERROR_INVALID_ARG,
}
}
@ -244,19 +267,24 @@ pub unsafe extern "C" fn sdp_media_section_count(session: *const SdpSession) ->
(*session).media.len()
}
#[no_mangle]
pub unsafe extern "C" fn get_sdp_bandwidth(session: *const SdpSession,
bandwidth_type: *const c_char) -> u32 {
pub unsafe extern "C" fn get_sdp_bandwidth(
session: *const SdpSession,
bandwidth_type: *const c_char,
) -> u32 {
get_bandwidth(&(*session).bandwidth, bandwidth_type)
}
#[no_mangle]
pub unsafe extern "C" fn sdp_get_session_bandwidth_vec(session: *const SdpSession) -> *const Vec<SdpBandwidth> {
pub unsafe extern "C" fn sdp_get_session_bandwidth_vec(
session: *const SdpSession,
) -> *const Vec<SdpBandwidth> {
&(*session).bandwidth
}
#[no_mangle]
pub unsafe extern "C" fn get_sdp_session_attributes(session: *const SdpSession) -> *const Vec<SdpAttribute> {
pub unsafe extern "C" fn get_sdp_session_attributes(
session: *const SdpSession,
) -> *const Vec<SdpAttribute> {
&(*session).attribute
}

View File

@ -2,27 +2,28 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use std::ptr;
use std::os::raw::c_char;
use std::convert::TryInto;
use std::os::raw::c_char;
use std::ptr;
use libc::size_t;
use nserror::{nsresult, NS_OK, NS_ERROR_INVALID_ARG};
use rsdparsa::{SdpBandwidth, SdpSession};
use rsdparsa::media_type::{SdpMedia, SdpMediaValue, SdpProtocolValue,
SdpFormatList};
use nserror::{nsresult, NS_ERROR_INVALID_ARG, NS_OK};
use rsdparsa::attribute_type::{SdpAttribute, SdpAttributeRtpmap};
use rsdparsa::media_type::{SdpFormatList, SdpMedia, SdpMediaValue, SdpProtocolValue};
use rsdparsa::{SdpBandwidth, SdpSession};
use network::{get_bandwidth, RustSdpConnection};
use types::StringView;
#[no_mangle]
pub unsafe extern "C" fn sdp_get_media_section(session: *const SdpSession,
index: size_t) -> *const SdpMedia {
pub unsafe extern "C" fn sdp_get_media_section(
session: *const SdpSession,
index: size_t,
) -> *const SdpMedia {
return match (*session).media.get(index) {
Some(m) => m,
None => ptr::null()
None => ptr::null(),
};
}
@ -39,7 +40,7 @@ impl<'a> From<&'a SdpMediaValue> for RustSdpMediaValue {
match *val {
SdpMediaValue::Audio => RustSdpMediaValue::Audio,
SdpMediaValue::Video => RustSdpMediaValue::Video,
SdpMediaValue::Application => RustSdpMediaValue::Application
SdpMediaValue::Application => RustSdpMediaValue::Application,
}
}
}
@ -84,7 +85,9 @@ impl<'a> From<&'a SdpProtocolValue> for RustSdpProtocolValue {
}
#[no_mangle]
pub unsafe extern "C" fn sdp_get_media_protocol(sdp_media: *const SdpMedia) -> RustSdpProtocolValue {
pub unsafe extern "C" fn sdp_get_media_protocol(
sdp_media: *const SdpMedia,
) -> RustSdpProtocolValue {
RustSdpProtocolValue::from((*sdp_media).get_proto())
}
@ -92,19 +95,21 @@ pub unsafe extern "C" fn sdp_get_media_protocol(sdp_media: *const SdpMedia) ->
#[derive(Clone, Copy)]
pub enum RustSdpFormatType {
Integers,
Strings
Strings,
}
#[no_mangle]
pub unsafe extern "C" fn sdp_get_format_type(sdp_media: *const SdpMedia) -> RustSdpFormatType {
pub unsafe extern "C" fn sdp_get_format_type(sdp_media: *const SdpMedia) -> RustSdpFormatType {
match *(*sdp_media).get_formats() {
SdpFormatList::Integers(_) => RustSdpFormatType::Integers,
SdpFormatList::Strings(_) => RustSdpFormatType::Strings
SdpFormatList::Strings(_) => RustSdpFormatType::Strings,
}
}
#[no_mangle]
pub unsafe extern "C" fn sdp_get_format_string_vec(sdp_media: *const SdpMedia) -> *const Vec<String> {
pub unsafe extern "C" fn sdp_get_format_string_vec(
sdp_media: *const SdpMedia,
) -> *const Vec<String> {
if let SdpFormatList::Strings(ref formats) = *(*sdp_media).get_formats() {
formats
} else {
@ -137,13 +142,17 @@ pub unsafe extern "C" fn sdp_get_media_port_count(sdp_media: *const SdpMedia) ->
}
#[no_mangle]
pub unsafe extern "C" fn sdp_get_media_bandwidth(sdp_media: *const SdpMedia,
bandwidth_type: *const c_char) -> u32 {
pub unsafe extern "C" fn sdp_get_media_bandwidth(
sdp_media: *const SdpMedia,
bandwidth_type: *const c_char,
) -> u32 {
get_bandwidth((*sdp_media).get_bandwidth(), bandwidth_type)
}
#[no_mangle]
pub unsafe extern "C" fn sdp_get_media_bandwidth_vec(sdp_media: *const SdpMedia) -> *const Vec<SdpBandwidth> {
pub unsafe extern "C" fn sdp_get_media_bandwidth_vec(
sdp_media: *const SdpMedia,
) -> *const Vec<SdpBandwidth> {
(*sdp_media).get_bandwidth()
}
@ -153,7 +162,10 @@ pub unsafe extern "C" fn sdp_media_has_connection(sdp_media: *const SdpMedia) ->
}
#[no_mangle]
pub unsafe extern "C" fn sdp_get_media_connection(sdp_media: *const SdpMedia, ret: *mut RustSdpConnection) -> nsresult {
pub unsafe extern "C" fn sdp_get_media_connection(
sdp_media: *const SdpMedia,
ret: *mut RustSdpConnection,
) -> nsresult {
if let &Some(ref connection) = (*sdp_media).get_connection() {
*ret = RustSdpConnection::from(connection);
return NS_OK;
@ -162,7 +174,9 @@ pub unsafe extern "C" fn sdp_get_media_connection(sdp_media: *const SdpMedia, re
}
#[no_mangle]
pub unsafe extern "C" fn sdp_get_media_attribute_list(sdp_media: *const SdpMedia) -> *const Vec<SdpAttribute> {
pub unsafe extern "C" fn sdp_get_media_attribute_list(
sdp_media: *const SdpMedia,
) -> *const Vec<SdpAttribute> {
(*sdp_media).get_attributes()
}
@ -172,21 +186,25 @@ pub unsafe extern "C" fn sdp_media_clear_codecs(sdp_media: *mut SdpMedia) {
}
#[no_mangle]
pub unsafe extern "C" fn sdp_media_add_codec(sdp_media: *mut SdpMedia, pt: u8,
codec_name: StringView, clockrate: u32,
channels: u16) -> nsresult {
let rtpmap = SdpAttributeRtpmap {
payload_type: pt,
codec_name: match codec_name.try_into() {
Ok(x) => x,
Err(boxed_error) => {
println!("Error while parsing string, description: {}", boxed_error);
return NS_ERROR_INVALID_ARG;
}
},
frequency: clockrate,
channels: Some(channels as u32),
};
pub unsafe extern "C" fn sdp_media_add_codec(
sdp_media: *mut SdpMedia,
pt: u8,
codec_name: StringView,
clockrate: u32,
channels: u16,
) -> nsresult {
let rtpmap = SdpAttributeRtpmap {
payload_type: pt,
codec_name: match codec_name.try_into() {
Ok(x) => x,
Err(boxed_error) => {
println!("Error while parsing string, description: {}", boxed_error);
return NS_ERROR_INVALID_ARG;
}
},
frequency: clockrate,
channels: Some(channels as u32),
};
match (*sdp_media).add_codec(rtpmap) {
Ok(_) => NS_OK,
@ -195,17 +213,21 @@ pub unsafe extern "C" fn sdp_media_add_codec(sdp_media: *mut SdpMedia, pt: u8,
}
#[no_mangle]
pub unsafe extern "C" fn sdp_media_add_datachannel(sdp_media: *mut SdpMedia, name: StringView,
port: u16, streams: u16, message_size: u32)
-> nsresult {
pub unsafe extern "C" fn sdp_media_add_datachannel(
sdp_media: *mut SdpMedia,
name: StringView,
port: u16,
streams: u16,
message_size: u32,
) -> nsresult {
let name_str = match name.try_into() {
Ok(x) => x,
Err(_) => {
return NS_ERROR_INVALID_ARG;
}
};
match (*sdp_media).add_datachannel(name_str, port, streams, message_size){
match (*sdp_media).add_datachannel(name_str, port, streams, message_size) {
Ok(_) => NS_OK,
Err(_) => NS_ERROR_INVALID_ARG
Err(_) => NS_ERROR_INVALID_ARG,
}
}

View File

@ -4,20 +4,20 @@
extern crate nserror;
use std::ffi::{CStr, CString};
use std::net::IpAddr;
use std::os::raw::c_char;
use std::ffi::{CStr, CString};
use rsdparsa::{SdpOrigin, SdpConnection, SdpBandwidth};
use rsdparsa::address::{Address, AddressType, AddressTyped, ExplicitlyTypedAddress};
use types::{StringView, NULL_STRING};
use rsdparsa::{SdpBandwidth, SdpConnection, SdpOrigin};
use std::convert::TryFrom;
use types::{StringView, NULL_STRING};
#[repr(C)]
#[derive(Clone, Copy, PartialEq)]
pub enum RustAddressType {
IP4,
IP6
IP6,
}
impl TryFrom<u32> for RustAddressType {
@ -61,7 +61,7 @@ pub fn get_octets(addr: &IpAddr) -> [u8; 16] {
IpAddr::V4(v4_addr) => {
let v4_octets = v4_addr.octets();
(&mut octets[0..4]).copy_from_slice(&v4_octets);
},
}
IpAddr::V6(v6_addr) => {
let v6_octets = v6_addr.octets();
octets.copy_from_slice(&v6_octets);
@ -81,13 +81,11 @@ impl<'a> From<&'a Address> for RustAddress {
fn from(address: &Address) -> Self {
match address {
Address::Ip(ip) => Self::from(ip),
Address::Fqdn(fqdn) => {
Self {
ip_address: [0; 50],
fqdn: fqdn.as_str().into(),
is_fqdn: true,
}
}
Address::Fqdn(fqdn) => Self {
ip_address: [0; 50],
fqdn: fqdn.as_str().into(),
is_fqdn: true,
},
}
}
}
@ -100,7 +98,11 @@ impl<'a> From<&'a IpAddr> for RustAddress {
if str_bytes.len() < 50 {
c_addr[..str_bytes.len()].copy_from_slice(&str_bytes);
}
Self {ip_address: c_addr, fqdn:NULL_STRING, is_fqdn:false }
Self {
ip_address: c_addr,
fqdn: NULL_STRING,
is_fqdn: false,
}
}
}
@ -118,7 +120,7 @@ impl Default for RustExplicitlyTypedAddress {
ip_address: [0; 50],
fqdn: NULL_STRING,
is_fqdn: false,
}
},
}
}
}
@ -126,21 +128,17 @@ impl Default for RustExplicitlyTypedAddress {
impl<'a> From<&'a ExplicitlyTypedAddress> for RustExplicitlyTypedAddress {
fn from(address: &ExplicitlyTypedAddress) -> Self {
match address {
ExplicitlyTypedAddress::Fqdn {domain, ..} => {
Self {
address_type: address.address_type().into(),
address: RustAddress {
ip_address: [0; 50],
fqdn: StringView::from(domain.as_str()),
is_fqdn: true,
},
}
ExplicitlyTypedAddress::Fqdn { domain, .. } => Self {
address_type: address.address_type().into(),
address: RustAddress {
ip_address: [0; 50],
fqdn: StringView::from(domain.as_str()),
is_fqdn: true,
},
},
ExplicitlyTypedAddress::Ip(ip_address) => {
Self {
address_type: ip_address.address_type().into(),
address: ip_address.into(),
}
ExplicitlyTypedAddress::Ip(ip_address) => Self {
address_type: ip_address.address_type().into(),
address: ip_address.into(),
},
}
}
@ -150,40 +148,37 @@ impl<'a> From<&'a ExplicitlyTypedAddress> for RustExplicitlyTypedAddress {
impl<'a> From<&'a Option<IpAddr>> for RustExplicitlyTypedAddress {
fn from(addr: &Option<IpAddr>) -> Self {
match *addr {
Some(ref x) => {
Self {
address_type: RustAddressType::from(x.address_type()),
address: RustAddress::from(x),
}
},
None => {
Self::default()
Some(ref x) => Self {
address_type: RustAddressType::from(x.address_type()),
address: RustAddress::from(x),
},
None => Self::default(),
}
}
}
#[repr(C)]
pub struct RustSdpConnection {
pub addr: RustExplicitlyTypedAddress
,
pub addr: RustExplicitlyTypedAddress,
pub ttl: u8,
pub amount: u64
pub amount: u64,
}
impl<'a> From<&'a SdpConnection> for RustSdpConnection {
fn from(sdp_connection: &SdpConnection) -> Self {
let ttl = match sdp_connection.ttl {
Some(x) => x as u8,
None => 0
None => 0,
};
let amount = match sdp_connection.amount {
Some(x) => x as u64,
None => 0
None => 0,
};
RustSdpConnection { addr: RustExplicitlyTypedAddress
::from(&sdp_connection.address),
ttl: ttl, amount: amount }
RustSdpConnection {
addr: RustExplicitlyTypedAddress::from(&sdp_connection.address),
ttl: ttl,
amount: amount,
}
}
}
@ -192,11 +187,9 @@ pub struct RustSdpOrigin {
username: StringView,
session_id: u64,
session_version: u64,
addr: RustExplicitlyTypedAddress
,
addr: RustExplicitlyTypedAddress,
}
fn bandwidth_match(str_bw: &str, enum_bw: &SdpBandwidth) -> bool {
match *enum_bw {
SdpBandwidth::As(_) => str_bw == "AS",
@ -208,17 +201,15 @@ fn bandwidth_match(str_bw: &str, enum_bw: &SdpBandwidth) -> bool {
fn bandwidth_value(bandwidth: &SdpBandwidth) -> u32 {
match *bandwidth {
SdpBandwidth::As(x) | SdpBandwidth::Ct(x) |
SdpBandwidth::Tias(x) => x,
SdpBandwidth::Unknown(_, _) => 0
SdpBandwidth::As(x) | SdpBandwidth::Ct(x) | SdpBandwidth::Tias(x) => x,
SdpBandwidth::Unknown(_, _) => 0,
}
}
pub unsafe fn get_bandwidth(bandwidths: &Vec<SdpBandwidth>,
bandwidth_type: *const c_char) -> u32 {
pub unsafe fn get_bandwidth(bandwidths: &Vec<SdpBandwidth>, bandwidth_type: *const c_char) -> u32 {
let bw_type = match CStr::from_ptr(bandwidth_type).to_str() {
Ok(string) => string,
Err(_) => return 0
Err(_) => return 0,
};
for bandwidth in bandwidths.iter() {
if bandwidth_match(bw_type, bandwidth) {
@ -237,24 +228,24 @@ pub unsafe extern "C" fn sdp_serialize_bandwidth(bw: *const Vec<SdpBandwidth>) -
builder.push_str("b=AS:");
builder.push_str(&val.to_string());
builder.push_str("\r\n");
},
}
SdpBandwidth::Ct(val) => {
builder.push_str("b=CT:");
builder.push_str(&val.to_string());
builder.push_str("\r\n");
},
}
SdpBandwidth::Tias(val) => {
builder.push_str("b=TIAS:");
builder.push_str(&val.to_string());
builder.push_str("\r\n");
},
}
SdpBandwidth::Unknown(ref name, val) => {
builder.push_str("b=");
builder.push_str(name.as_str());
builder.push(':');
builder.push_str(&val.to_string());
builder.push_str("\r\n");
},
}
}
}
CString::from_vec_unchecked(builder.into_bytes()).into_raw()
@ -270,7 +261,6 @@ pub unsafe fn origin_view_helper(origin: &SdpOrigin) -> RustSdpOrigin {
username: StringView::from(origin.username.as_str()),
session_id: origin.session_id,
session_version: origin.session_version,
addr: RustExplicitlyTypedAddress
::from(&origin.unicast_addr)
addr: RustExplicitlyTypedAddress::from(&origin.unicast_addr),
}
}

View File

@ -3,63 +3,73 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use libc::size_t;
use std::ffi::CStr;
use std::{str, slice};
use std::error::Error;
use std::boxed::Box;
use std::convert::TryInto;
use std::error::Error;
use std::ffi::CStr;
use std::{slice, str};
use nserror::{nsresult, NS_OK, NS_ERROR_INVALID_ARG};
use nserror::{nsresult, NS_ERROR_INVALID_ARG, NS_OK};
#[repr(C)]
#[derive(Clone, Copy)]
pub struct StringView {
buffer: *const u8,
len: size_t
len: size_t,
}
pub const NULL_STRING: StringView = StringView { buffer: 0 as *const u8,
len: 0 };
pub const NULL_STRING: StringView = StringView {
buffer: 0 as *const u8,
len: 0,
};
impl<'a> From<&'a str> for StringView {
fn from(input: &str) -> StringView {
StringView { buffer: input.as_ptr(), len: input.len()}
StringView {
buffer: input.as_ptr(),
len: input.len(),
}
}
}
impl TryInto<String> for StringView {
type Error = Box<dyn Error>;
fn try_into(self) -> Result<String, Box<dyn Error>> {
// This block must be unsafe as it converts a StringView, most likly provided from the
// C++ code, into a rust String and thus needs to operate with raw pointers.
let string_slice: &[u8];
unsafe {
// Add one to the length as the length passed in the StringView is the length of
// the string and is missing the null terminator
string_slice = slice::from_raw_parts(self.buffer, self.len+1 as usize);
string_slice = slice::from_raw_parts(self.buffer, self.len + 1 as usize);
}
let c_str = match CStr::from_bytes_with_nul(string_slice) {
Ok(string) => string,
Err(x) => { return Err(Box::new(x)); },
};
let c_str = match CStr::from_bytes_with_nul(string_slice) {
Ok(string) => string,
Err(x) => {
return Err(Box::new(x));
}
};
let str_slice: &str = match str::from_utf8(c_str.to_bytes()) {
Ok(string) => string,
Err(x) => { return Err(Box::new(x)); },
};
let str_slice: &str = match str::from_utf8(c_str.to_bytes()) {
Ok(string) => string,
Err(x) => {
return Err(Box::new(x));
}
};
Ok(str_slice.to_string())
Ok(str_slice.to_string())
}
}
impl<'a, T: AsRef<str>> From<&'a Option<T>> for StringView {
fn from(input: &Option<T>) -> StringView {
match *input {
Some(ref x) => StringView { buffer: x.as_ref().as_ptr(),
len: x.as_ref().len()},
None => NULL_STRING
Some(ref x) => StringView {
buffer: x.as_ref().as_ptr(),
len: x.as_ref().len(),
},
None => NULL_STRING,
}
}
}
@ -70,15 +80,17 @@ pub unsafe extern "C" fn string_vec_len(vec: *const Vec<String>) -> size_t {
}
#[no_mangle]
pub unsafe extern "C" fn string_vec_get_view(vec: *const Vec<String>,
index: size_t,
ret: *mut StringView) -> nsresult {
pub unsafe extern "C" fn string_vec_get_view(
vec: *const Vec<String>,
index: size_t,
ret: *mut StringView,
) -> nsresult {
match (*vec).get(index) {
Some(ref string) => {
*ret = StringView::from(string.as_str());
NS_OK
},
None => NS_ERROR_INVALID_ARG
}
None => NS_ERROR_INVALID_ARG,
}
}
@ -94,15 +106,17 @@ pub unsafe extern "C" fn f32_vec_len(vec: *const Vec<f32>) -> size_t {
}
#[no_mangle]
pub unsafe extern "C" fn f32_vec_get(vec: *const Vec<f32>,
index: size_t,
ret: *mut f32) -> nsresult {
pub unsafe extern "C" fn f32_vec_get(
vec: *const Vec<f32>,
index: size_t,
ret: *mut f32,
) -> nsresult {
match (*vec).get(index) {
Some(val) => {
*ret = *val;
NS_OK
},
None => NS_ERROR_INVALID_ARG
}
None => NS_ERROR_INVALID_ARG,
}
}
@ -112,15 +126,17 @@ pub unsafe extern "C" fn u32_vec_len(vec: *const Vec<u32>) -> size_t {
}
#[no_mangle]
pub unsafe extern "C" fn u32_vec_get(vec: *const Vec<u32>,
index: size_t,
ret: *mut u32) -> nsresult {
pub unsafe extern "C" fn u32_vec_get(
vec: *const Vec<u32>,
index: size_t,
ret: *mut u32,
) -> nsresult {
match (*vec).get(index) {
Some(val) => {
*ret = *val;
NS_OK
},
None => NS_ERROR_INVALID_ARG
}
None => NS_ERROR_INVALID_ARG,
}
}
@ -130,15 +146,17 @@ pub unsafe extern "C" fn u16_vec_len(vec: *const Vec<u16>) -> size_t {
}
#[no_mangle]
pub unsafe extern "C" fn u16_vec_get(vec: *const Vec<u16>,
index: size_t,
ret: *mut u16) -> nsresult {
pub unsafe extern "C" fn u16_vec_get(
vec: *const Vec<u16>,
index: size_t,
ret: *mut u16,
) -> nsresult {
match (*vec).get(index) {
Some(val) => {
*ret = *val;
NS_OK
},
None => NS_ERROR_INVALID_ARG
}
None => NS_ERROR_INVALID_ARG,
}
}
@ -148,14 +166,12 @@ pub unsafe extern "C" fn u8_vec_len(vec: *const Vec<u8>) -> size_t {
}
#[no_mangle]
pub unsafe extern "C" fn u8_vec_get(vec: *const Vec<u8>,
index: size_t,
ret: *mut u8) -> nsresult {
pub unsafe extern "C" fn u8_vec_get(vec: *const Vec<u8>, index: size_t, ret: *mut u8) -> nsresult {
match (*vec).get(index) {
Some(val) => {
*ret = *val;
NS_OK
},
None => NS_ERROR_INVALID_ARG
}
None => NS_ERROR_INVALID_ARG,
}
}