• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2023 Huawei Device Co., Ltd.
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 //     http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13 
14 use std::ffi::c_char;
15 use std::slice;
16 
17 use crate::utils::filter::{CommonFilter, Filter};
18 use crate::utils::form_item::{FileSpec, FormItem};
19 
20 #[repr(C)]
21 pub(crate) struct CStringWrapper {
22     c_str: *const c_char,
23     len: u32,
24 }
25 
26 impl From<&str> for CStringWrapper {
from(value: &str) -> Self27     fn from(value: &str) -> Self {
28         let c_str = value.as_ptr() as *const c_char;
29         let len = value.len() as u32;
30         CStringWrapper { c_str, len }
31     }
32 }
33 
34 impl From<&String> for CStringWrapper {
from(value: &String) -> Self35     fn from(value: &String) -> Self {
36         Self::from(value.as_str())
37     }
38 }
39 
40 impl ToString for CStringWrapper {
to_string(&self) -> String41     fn to_string(&self) -> String {
42         if self.c_str.is_null() || self.len == 0 {
43             unsafe { DeleteChar(self.c_str) };
44             return String::new();
45         }
46         let bytes = unsafe { slice::from_raw_parts(self.c_str as *const u8, self.len as usize) };
47         let str = unsafe { String::from_utf8_unchecked(bytes.to_vec()) };
48         unsafe { DeleteChar(self.c_str) };
49         str
50     }
51 }
52 
53 #[repr(C)]
54 pub(crate) struct CFilter {
55     bundle: CStringWrapper,
56     common_data: CommonFilter,
57 }
58 
59 impl Filter {
to_c_struct(&self) -> CFilter60     pub(crate) fn to_c_struct(&self) -> CFilter {
61         CFilter {
62             bundle: CStringWrapper::from(&self.bundle),
63             common_data: self.common_data,
64         }
65     }
66 }
67 
68 #[repr(C)]
69 pub(crate) struct CVectorWrapper {
70     pub(crate) ptr: *const u32,
71     pub(crate) len: u64,
72 }
73 
74 #[repr(C)]
75 pub(crate) struct CFileSpec {
76     pub(crate) name: CStringWrapper,
77     pub(crate) path: CStringWrapper,
78     pub(crate) file_name: CStringWrapper,
79     pub(crate) mime_type: CStringWrapper,
80 }
81 
82 impl FileSpec {
to_c_struct(&self) -> CFileSpec83     pub(crate) fn to_c_struct(&self) -> CFileSpec {
84         CFileSpec {
85             name: CStringWrapper::from(&self.name),
86             path: CStringWrapper::from(&self.path),
87             file_name: CStringWrapper::from(&self.file_name),
88             mime_type: CStringWrapper::from(&self.mime_type),
89         }
90     }
91 
from_c_struct(c_struct: &CFileSpec) -> Self92     pub(crate) fn from_c_struct(c_struct: &CFileSpec) -> Self {
93         FileSpec {
94             name: c_struct.name.to_string(),
95             path: c_struct.path.to_string(),
96             file_name: c_struct.file_name.to_string(),
97             mime_type: c_struct.mime_type.to_string(),
98         }
99     }
100 }
101 
102 #[repr(C)]
103 pub(crate) struct CFormItem {
104     pub(crate) name: CStringWrapper,
105     pub(crate) value: CStringWrapper,
106 }
107 
108 impl FormItem {
to_c_struct(&self) -> CFormItem109     pub(crate) fn to_c_struct(&self) -> CFormItem {
110         CFormItem {
111             name: CStringWrapper::from(&self.name),
112             value: CStringWrapper::from(&self.value),
113         }
114     }
115 
from_c_struct(c_struct: &CFormItem) -> Self116     pub(crate) fn from_c_struct(c_struct: &CFormItem) -> Self {
117         FormItem {
118             name: c_struct.name.to_string(),
119             value: c_struct.value.to_string(),
120         }
121     }
122 }
123 
124 #[cfg(feature = "oh")]
125 #[link(name = "request_service_c")]
126 extern "C" {
DeleteChar(ptr: *const c_char)127     pub(crate) fn DeleteChar(ptr: *const c_char);
DeleteCFormItem(ptr: *const CFormItem)128     pub(crate) fn DeleteCFormItem(ptr: *const CFormItem);
DeleteCFileSpec(ptr: *const CFileSpec)129     pub(crate) fn DeleteCFileSpec(ptr: *const CFileSpec);
DeleteCVectorWrapper(ptr: *const u32)130     pub(crate) fn DeleteCVectorWrapper(ptr: *const u32);
DeleteCStringPtr(ptr: *const CStringWrapper)131     pub(crate) fn DeleteCStringPtr(ptr: *const CStringWrapper);
132 }
133