Built motion from commit 6a09e18b.|2.6.11
[motion2.git] / legacy-libs / grpc / deps / grpc / src / core / tsi / alts / frame_protector / alts_crypter.cc
1 /*
2  *
3  * Copyright 2018 gRPC authors.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */
18
19 #include <grpc/support/port_platform.h>
20
21 #include "src/core/tsi/alts/frame_protector/alts_crypter.h"
22
23 #include <string.h>
24
25 #include <grpc/support/alloc.h>
26
27 static void maybe_copy_error_msg(const char* src, char** dst) {
28   if (dst != nullptr && src != nullptr) {
29     *dst = static_cast<char*>(gpr_malloc(strlen(src) + 1));
30     memcpy(*dst, src, strlen(src) + 1);
31   }
32 }
33
34 grpc_status_code alts_crypter_process_in_place(
35     alts_crypter* crypter, unsigned char* data, size_t data_allocated_size,
36     size_t data_size, size_t* output_size, char** error_details) {
37   if (crypter != nullptr && crypter->vtable != nullptr &&
38       crypter->vtable->process_in_place != nullptr) {
39     return crypter->vtable->process_in_place(crypter, data, data_allocated_size,
40                                              data_size, output_size,
41                                              error_details);
42   }
43   /* An error occurred. */
44   const char error_msg[] =
45       "crypter or crypter->vtable has not been initialized properly.";
46   maybe_copy_error_msg(error_msg, error_details);
47   return GRPC_STATUS_INVALID_ARGUMENT;
48 }
49
50 size_t alts_crypter_num_overhead_bytes(const alts_crypter* crypter) {
51   if (crypter != nullptr && crypter->vtable != nullptr &&
52       crypter->vtable->num_overhead_bytes != nullptr) {
53     return crypter->vtable->num_overhead_bytes(crypter);
54   }
55   /* An error occurred. */
56   return 0;
57 }
58
59 void alts_crypter_destroy(alts_crypter* crypter) {
60   if (crypter != nullptr) {
61     if (crypter->vtable != nullptr && crypter->vtable->destruct != nullptr) {
62       crypter->vtable->destruct(crypter);
63     }
64     gpr_free(crypter);
65   }
66 }