operation_table.cpp 1.9 KB

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