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