1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <keymaster/android_keymaster_utils.h>
18 #include <keymaster/operation.h>
19 #include <keymaster/operation_table.h>
20 
21 namespace keymaster {
22 
Add(OperationPtr && operation)23 keymaster_error_t OperationTable::Add(OperationPtr&& operation) {
24     if (!table_) {
25         table_.reset(new (std::nothrow) OperationPtr[table_size_]);
26         if (!table_) return KM_ERROR_MEMORY_ALLOCATION_FAILED;
27     }
28     for (size_t i = 0; i < table_size_; ++i) {
29         if (!table_[i]) {
30             table_[i] = move(operation);
31             return KM_ERROR_OK;
32         }
33     }
34     return KM_ERROR_TOO_MANY_OPERATIONS;
35 }
36 
Find(keymaster_operation_handle_t op_handle)37 Operation* OperationTable::Find(keymaster_operation_handle_t op_handle) {
38     if (op_handle == 0) return nullptr;
39 
40     if (!table_.get()) return nullptr;
41 
42     for (size_t i = 0; i < table_size_; ++i) {
43         if (table_[i] && table_[i]->operation_handle() == op_handle) return table_[i].get();
44     }
45     return nullptr;
46 }
47 
Delete(keymaster_operation_handle_t op_handle)48 bool OperationTable::Delete(keymaster_operation_handle_t op_handle) {
49     if (!table_.get()) return false;
50 
51     for (size_t i = 0; i < table_size_; ++i) {
52         if (table_[i] && table_[i]->operation_handle() == op_handle) {
53             table_[i].reset();
54             return true;
55         }
56     }
57     return false;
58 }
59 
60 }  // namespace keymaster
61