OpenRadioss 2025.1.11
OpenRadioss project
Loading...
Searching...
No Matches
checksum.cpp
Go to the documentation of this file.
1//Copyright> OpenRadioss
2//Copyright> Copyright (C) 1986-2025 Altair Engineering Inc.
3//Copyright>
4//Copyright> This program is free software: you can redistribute it and/or modify
5//Copyright> it under the terms of the GNU Affero General Public License as published by
6//Copyright> the Free Software Foundation, either version 3 of the License, or
7//Copyright> (at your option) any later version.
8//Copyright>
9//Copyright> This program is distributed in the hope that it will be useful,
10//Copyright> but WITHOUT ANY WARRANTY; without even the implied warranty of
11//Copyright> MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12//Copyright> GNU Affero General Public License for more details.
13//Copyright>
14//Copyright> You should have received a copy of the GNU Affero General Public License
15//Copyright> along with this program. If not, see <https://www.gnu.org/licenses/>.
16//Copyright>
17//Copyright>
18//Copyright> Commercial Alternative: Altair Radioss Software
19//Copyright>
20//Copyright> As an alternative to this open-source version, Altair also offers Altair Radioss
21//Copyright> software under a commercial license. Contact Altair to discuss further if the
22//Copyright> commercial version may interest you: https://www.altair.com/radioss/.
23#include <checksum.h>
24
25std::string checksum::compute_checksum(std::string file){
26 std::ifstream input_file(file, std::ios::binary);
27
28 if (!input_file) {
29 std::cerr << "Error opening file: " << file << std::endl;
30 checksum_list.push_back(std::make_tuple(file, "XXXXXXXXXXXXXXXX"));
31 return "XXXXXXXXXXXXXXXX";
32 }
33
34 md5_state_t state;
35 md5_init(&state);
36
37 char buffer[BUFFERSIZE];
38 int cont=1;
39
40 while ( cont == 1){
41 input_file.read(buffer, BUFFERSIZE);
42 int count = input_file.gcount();
43 md5_append(&state, ( md5_byte_t *)buffer, count);
44 if ( count < BUFFERSIZE){
45 cont=0;
46 }
47 }
48 input_file.close();
49
50 unsigned char digest[16];
51 md5_finish(&state, digest);
52
53 std::ostringstream formatted_line;
54 for (int i = 0; i < 16; ++i) {
55 formatted_line << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(digest[i]);
56 }
57 checksum_list.push_back(std::make_tuple(file, formatted_line.str()));
58 return formatted_line.str();
59}
60
61
62std::list <std::tuple< std::string, std::string>> checksum::dump_list() {
63 std::list <std::tuple< std::string, std::string>> checksum_list_tmp;
64 for (const auto& entry : checksum_list) {
65 checksum_list_tmp.push_back(entry);
66 }
67 return checksum_list_tmp;
68}
69
70
71// C/FORTRAN Engine interface
72extern "C" {
74 checksum* cs_output_files = new checksum();
75 return cs_output_files;
76 }
77
78 void compute_binary_checksum(checksum* cs_output_files,char *file, int len , int izip) {
79 char *file_c;
80 file_c=(char*)malloc(sizeof(char)* len+1);
81 for (int i=0; i<len; i++){
82 file_c[i]=file[i];
83 }
84 file_c[len]='\0';
85
86 std::string file_str(file_c);
87 if (izip > 0) {
88 file_str = file_str + ".gz";
89 }
90
91 std::string checksum_str=cs_output_files->compute_checksum(file_str);
92 // std::cout << "Checksum for file " << file_str << " is: " << checksum_str << std::endl;
93 }
94
95 void print_checksum_list( checksum* cs_output_files,int fd) {
96 std::list<std::tuple<std::string, std::string>> checksum_list = cs_output_files->dump_list();
97 for (const auto& entry : checksum_list) {
98 std::string str_line=" " + std::get<0>(entry) + " " + std::get<1>(entry);
99 int len_line=str_line.length();
100 const char * c_line = str_line.c_str();
101 write_out_file(&fd,c_line,&len_line);
102 }
103 }
104
105
106}
107
108#ifdef MAIN
109
110int main(int argc, char *argv[]) {
111 checksum cs;
112 std::string file=std::string(argv[1]);
113 std::string checksum = cs.compute_checksum(file);
114 std::cout << "Checksum for file " << file << " is: " << checksum << std::endl;
115 return 0;
116}
117#endif
int main()
checksum * new_file_checksum_list()
Definition checksum.cpp:73
void print_checksum_list(checksum *cs_output_files, int fd)
Definition checksum.cpp:95
void compute_binary_checksum(checksum *cs_output_files, char *file, int len, int izip)
Definition checksum.cpp:78
#define write_out_file
Definition checksum.h:53
#define BUFFERSIZE
Definition checksum.h:39
std::string compute_checksum(std::string file)
Definition checksum.cpp:25
std::list< std::tuple< std::string, std::string > > dump_list()
Definition checksum.cpp:62
std::list< std::tuple< std::string, std::string > > checksum_list
Definition checksum.h:43