• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "src/core/lib/security/credentials/alts/check_gcp_environment.h"
20 
21 #include <ctype.h>
22 #include <grpc/support/alloc.h>
23 #include <grpc/support/port_platform.h>
24 #include <stdio.h>
25 #include <string.h>
26 
27 #include "absl/log/log.h"
28 
29 const size_t kBiosDataBufferSize = 256;
30 
trim(const char * src)31 static char* trim(const char* src) {
32   if (src == nullptr || *src == '\0') {
33     return nullptr;
34   }
35   char* des = nullptr;
36   size_t start = 0, end = strlen(src) - 1;
37   // find the last character that is not a whitespace.
38   while (end != 0 && isspace(src[end])) {
39     end--;
40   }
41   // find the first character that is not a whitespace.
42   while (start < strlen(src) && isspace(src[start])) {
43     start++;
44   }
45   if (start <= end) {
46     des = static_cast<char*>(
47         gpr_zalloc(sizeof(char) * (end - start + 2 /* '\0' */)));
48     memcpy(des, src + start, end - start + 1);
49   }
50   return des;
51 }
52 
53 namespace grpc_core {
54 namespace internal {
55 
read_bios_file(const char * bios_file)56 char* read_bios_file(const char* bios_file) {
57   FILE* fp = fopen(bios_file, "r");
58   if (!fp) {
59     VLOG(2) << "BIOS data file does not exist or cannot be opened.";
60     return nullptr;
61   }
62   char buf[kBiosDataBufferSize + 1];
63   size_t ret = fread(buf, sizeof(char), kBiosDataBufferSize, fp);
64   buf[ret] = '\0';
65   char* trimmed_buf = trim(buf);
66   fclose(fp);
67   return trimmed_buf;
68 }
69 
70 }  // namespace internal
71 }  // namespace grpc_core
72