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