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/operation_table.h>
18 #include <keymaster/operation.h>
19 #include <keymaster/android_keymaster_utils.h>
20 
21 #include <keymaster/new.h>
22 
23 namespace keymaster {
24 
Add(OperationPtr && operation)25 keymaster_error_t OperationTable::Add(OperationPtr&& operation) {
26     if (!table_) {
27         table_.reset(new (std::nothrow) OperationPtr[table_size_]);
28         if (!table_)
29             return KM_ERROR_MEMORY_ALLOCATION_FAILED;
30     }
31     for (size_t i = 0; i < table_size_; ++i) {
32         if (!table_[i]) {
33             table_[i] = move(operation);
34             return KM_ERROR_OK;
35         }
36     }
37     return KM_ERROR_TOO_MANY_OPERATIONS;
38 }
39 
Find(keymaster_operation_handle_t op_handle)40 Operation* OperationTable::Find(keymaster_operation_handle_t op_handle) {
41     if (op_handle == 0)
42         return nullptr;
43 
44     if (!table_.get())
45         return nullptr;
46 
47     for (size_t i = 0; i < table_size_; ++i) {
48         if (table_[i] && table_[i]->operation_handle() == op_handle)
49             return table_[i].get();
50     }
51     return nullptr;
52 }
53 
Delete(keymaster_operation_handle_t op_handle)54 bool OperationTable::Delete(keymaster_operation_handle_t op_handle) {
55     if (!table_.get())
56         return false;
57 
58     for (size_t i = 0; i < table_size_; ++i) {
59         if (table_[i] && table_[i]->operation_handle() == op_handle) {
60             table_[i].reset();
61             return true;
62         }
63     }
64     return false;
65 }
66 
67 }  // namespace keymaster
68