Apollo 10.0
自动驾驶开放平台
file_utility.cc
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2019 The Apollo Authors. All Rights Reserved.
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
18
19#include <dirent.h>
20
21#include <algorithm>
22#include <cerrno>
23#include <iostream>
24#include <limits>
25
26#define BOOST_NO_CXX11_SCOPED_ENUMS
27#include <boost/filesystem.hpp>
28#undef BOOST_NO_CXX11_SCOPED_ENUMS
29
30#include "cyber/common/log.h"
31#include "fastrtps/TopicDataType.h"
32
33namespace apollo {
34namespace localization {
35namespace msf {
36const size_t kBufferSize = 20480000;
37
38void FileUtility::ComputeFileMd5(const std::string &file_path,
39 unsigned char res[kUcharMd5Length]) {
40 std::vector<unsigned char> buf(kBufferSize);
41 unsigned char *buf_pt = &buf[0];
42
43 FILE *file = fopen(file_path.c_str(), "rb");
44 size_t total_size = 0;
45 if (file) {
46 int count = 1;
47 while (!feof(file)) {
48 if (count > 1) {
49 buf.resize(kBufferSize * count);
50 buf_pt = &buf[count - 1];
51 }
52
53 size_t size = fread(buf_pt, sizeof(unsigned char), kBufferSize, file);
54 total_size += size;
55
56 ++count;
57 }
58 } else {
59 AERROR << "Can't find the file: " << file_path;
60 return;
61 }
62
63 ComputeBinaryMd5(&buf[0], total_size, res);
64 fclose(file);
65}
66
67void FileUtility::ComputeFileMd5(const std::string &file_path,
68 char res[kCharMd5Lenth]) {
69 std::vector<unsigned char> buf(kBufferSize);
70 unsigned char *buf_pt = &buf[0];
71
72 FILE *file = fopen(file_path.c_str(), "rb");
73 size_t total_size = 0;
74 if (file) {
75 int count = 1;
76 while (!feof(file)) {
77 if (count > 1) {
78 buf.resize(kBufferSize * count);
79 buf_pt = &buf[count - 1];
80 }
81
82 size_t size = fread(buf_pt, sizeof(unsigned char), kBufferSize, file);
83 total_size += size;
84
85 ++count;
86 }
87 } else {
88 AERROR << "Can't find the file: " << file_path;
89 return;
90 }
91
92 ComputeBinaryMd5(&buf[0], total_size, res);
93 fclose(file);
94}
95
96void FileUtility::ComputeBinaryMd5(const unsigned char *binary, size_t size,
97 unsigned char res[kUcharMd5Length]) {
98 MD5 md5;
99 md5.init();
100 md5.update(binary, static_cast<unsigned int>(size));
101 md5.finalize();
102 for (uint8_t i = 0; i < kUcharMd5Length; ++i) {
103 res[i] = md5.digest[i];
104 }
105}
106
107void FileUtility::ComputeBinaryMd5(const unsigned char *binary, size_t size,
108 char res[kCharMd5Lenth]) {
109 unsigned char md[kUcharMd5Length] = {"\0"};
110 char buf[kCharMd5Lenth] = {'\0'};
111 char tmp[3] = {'\0'};
112
113 ComputeBinaryMd5(binary, size, md);
114
115 for (unsigned int i = 0; i < kUcharMd5Length; i++) {
116 snprintf(tmp, sizeof(tmp), "%02X", md[i]);
117 strncat(buf, tmp, sizeof(tmp));
118 }
119
120 memcpy(res, buf, sizeof(buf));
121}
122
123} // namespace msf
124} // namespace localization
125} // namespace apollo
static void ComputeFileMd5(const std::string &file_path, unsigned char res[kUcharMd5Length])
Compute file md5 given a file path.
static void ComputeBinaryMd5(const unsigned char *binary, size_t size, unsigned char res[kUcharMd5Length])
Compute file md5 given a binary chunk.
#define AERROR
Definition log.h:44
class register implement
Definition arena_queue.h:37