ivila commented on code in PR #190:
URL: 
https://github.com/apache/incubator-teaclave-trustzone-sdk/pull/190#discussion_r2101475130


##########
optee-utee/src/property.rs:
##########
@@ -0,0 +1,483 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+use crate::{Error, ErrorKind, Result};
+use crate::{Identity, Uuid};
+use alloc::{ffi::CString, string::{String, ToString}, vec::Vec};
+use optee_utee_sys as raw;
+
+/// Represents a TEE property set according to the TEE Internal API.
+/// The property set is a collection of properties that can be
+/// queried from the TEE. The property set is identified by a
+/// handle, which is a pointer to a TEE_PropSetHandle structure.
+pub enum PropertySet {
+    TeeImplementation,
+    CurrentClient,
+    CurrentTa,
+}
+
+impl PropertySet {
+    fn as_raw(&self) -> raw::TEE_PropSetHandle {
+        match self {
+            PropertySet::TeeImplementation => 
raw::TEE_PROPSET_TEE_IMPLEMENTATION,
+            PropertySet::CurrentClient => raw::TEE_PROPSET_CURRENT_CLIENT,
+            PropertySet::CurrentTa => raw::TEE_PROPSET_CURRENT_TA,
+        }
+    }
+}
+
+/// Represents a TEE property value.
+/// The property value can be of different types, such as
+/// string, bool, u32, TEE_UUID, TEE_Identity, etc.
+/// The property value is obtained from the TEE
+/// property set using the TEE_GetPropertyAs* functions.
+pub trait PropertyValue: Sized {
+    fn from_raw(set: raw::TEE_PropSetHandle, key: CString) -> Result<Self>;
+}
+
+/// Implements the PropertyValue trait for all return types:
+/// String, Bool, u32, u64, BinaryBlock, UUID, Identity.
+impl PropertyValue for String {
+    fn from_raw(set: raw::TEE_PropSetHandle, key: CString) -> Result<Self> {
+        let mut out_size = 0;
+
+        // The first call is to get the size of the string
+        // So we pass a null pointer and a size of 0
+        let res = unsafe {
+            raw::TEE_GetPropertyAsString(
+                set,
+                key.as_ptr() as *const core::ffi::c_char,
+                core::ptr::null_mut(),
+                &mut out_size,
+            )
+        };
+        match res {
+            raw::TEE_SUCCESS => {
+                if out_size == 0 {
+                    // return an empty string
+                    return Ok(String::new());
+                }
+                else {
+                    return Err(Error::new(ErrorKind::Generic));
+                }
+            }
+            raw::TEE_ERROR_SHORT_BUFFER => {
+                // Resize the string to the actual size
+                let mut out_buffer = vec![0; out_size as usize];
+                let res = unsafe {
+                    raw::TEE_GetPropertyAsString(
+                        set,
+                        key.as_ptr() as *const core::ffi::c_char,
+                        out_buffer.as_mut_ptr() as *mut core::ffi::c_char,
+                        &mut out_size,
+                    )
+                };
+                if res != raw::TEE_SUCCESS {
+                    return Err(Error::from_raw_error(res));
+                }
+
+                // For C raw strings, the last byte is a null terminator
+                // So we need to trim it
+                
Ok(String::from_utf8_lossy(&out_buffer).trim_end_matches('\0').to_string())

Review Comment:
   I would suggest using `core::ffi::CStr` instead of handling the null 
terminator ourself.
   Maybe 
[CStr::from_bytes_with_nul](https://doc.rust-lang.org/std/ffi/struct.CStr.html#method.from_bytes_with_nul)?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@teaclave.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@teaclave.apache.org
For additional commands, e-mail: dev-h...@teaclave.apache.org

Reply via email to