alts_counter.cc
Go to the documentation of this file.
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 
20 
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_counter_create(bool is_client, size_t counter_size,
35  size_t overflow_size,
36  alts_counter** crypter_counter,
37  char** error_details) {
38  /* Perform input sanity check. */
39  if (counter_size == 0) {
40  const char error_msg[] = "counter_size is invalid.";
41  maybe_copy_error_msg(error_msg, error_details);
43  }
44  if (overflow_size == 0 || overflow_size >= counter_size) {
45  const char error_msg[] = "overflow_size is invalid.";
46  maybe_copy_error_msg(error_msg, error_details);
48  }
49  if (crypter_counter == nullptr) {
50  const char error_msg[] = "crypter_counter is nullptr.";
51  maybe_copy_error_msg(error_msg, error_details);
53  }
54  *crypter_counter =
55  static_cast<alts_counter*>(gpr_malloc(sizeof(**crypter_counter)));
56  (*crypter_counter)->size = counter_size;
57  (*crypter_counter)->overflow_size = overflow_size;
58  (*crypter_counter)->counter =
59  static_cast<unsigned char*>(gpr_zalloc(counter_size));
60  if (is_client) {
61  ((*crypter_counter)->counter)[counter_size - 1] = 0x80;
62  }
63  return GRPC_STATUS_OK;
64 }
65 
67  bool* is_overflow,
68  char** error_details) {
69  /* Perform input sanity check. */
70  if (crypter_counter == nullptr) {
71  const char error_msg[] = "crypter_counter is nullptr.";
72  maybe_copy_error_msg(error_msg, error_details);
74  }
75  if (is_overflow == nullptr) {
76  const char error_msg[] = "is_overflow is nullptr.";
77  maybe_copy_error_msg(error_msg, error_details);
79  }
80  /* Increment the internal counter. */
81  size_t i = 0;
82  for (; i < crypter_counter->overflow_size; i++) {
83  (crypter_counter->counter)[i]++;
84  if ((crypter_counter->counter)[i] != 0x00) {
85  break;
86  }
87  }
91  if (i == crypter_counter->overflow_size) {
92  *is_overflow = true;
94  }
95  *is_overflow = false;
96  return GRPC_STATUS_OK;
97 }
98 
99 size_t alts_counter_get_size(alts_counter* crypter_counter) {
100  if (crypter_counter == nullptr) {
101  return 0;
102  }
103  return crypter_counter->size;
104 }
105 
106 unsigned char* alts_counter_get_counter(alts_counter* crypter_counter) {
107  if (crypter_counter == nullptr) {
108  return nullptr;
109  }
110  return crypter_counter->counter;
111 }
112 
113 void alts_counter_destroy(alts_counter* crypter_counter) {
114  if (crypter_counter != nullptr) {
115  gpr_free(crypter_counter->counter);
116  gpr_free(crypter_counter);
117  }
118 }
dst
static const char dst[]
Definition: test-fs-copyfile.c:37
alts_counter_get_counter
unsigned char * alts_counter_get_counter(alts_counter *crypter_counter)
Definition: alts_counter.cc:106
alts_counter.h
string.h
alts_counter::size
size_t size
Definition: alts_counter.h:31
gpr_free
GPRAPI void gpr_free(void *ptr)
Definition: alloc.cc:51
grpc_status_code
grpc_status_code
Definition: include/grpc/impl/codegen/status.h:28
gpr_malloc
GPRAPI void * gpr_malloc(size_t size)
Definition: alloc.cc:29
alts_counter_increment
grpc_status_code alts_counter_increment(alts_counter *crypter_counter, bool *is_overflow, char **error_details)
Definition: alts_counter.cc:66
GRPC_STATUS_INVALID_ARGUMENT
@ GRPC_STATUS_INVALID_ARGUMENT
Definition: include/grpc/impl/codegen/status.h:46
alts_counter_get_size
size_t alts_counter_get_size(alts_counter *crypter_counter)
Definition: alts_counter.cc:99
gpr_zalloc
GPRAPI void * gpr_zalloc(size_t size)
Definition: alloc.cc:40
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
GRPC_STATUS_OK
@ GRPC_STATUS_OK
Definition: include/grpc/impl/codegen/status.h:30
alts_counter::counter
unsigned char * counter
Definition: alts_counter.h:33
alts_counter::overflow_size
size_t overflow_size
Definition: alts_counter.h:32
maybe_copy_error_msg
static void maybe_copy_error_msg(const char *src, char **dst)
Definition: alts_counter.cc:27
alloc.h
GRPC_STATUS_FAILED_PRECONDITION
@ GRPC_STATUS_FAILED_PRECONDITION
Definition: include/grpc/impl/codegen/status.h:97
alts_counter_create
grpc_status_code alts_counter_create(bool is_client, size_t counter_size, size_t overflow_size, alts_counter **crypter_counter, char **error_details)
Definition: alts_counter.cc:34
alts_counter
Definition: alts_counter.h:30
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
alts_counter_destroy
void alts_counter_destroy(alts_counter *crypter_counter)
Definition: alts_counter.cc:113
port_platform.h


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:40