Mercurial > octave-nkf
annotate liboctave/oct-md5.cc @ 11936:e896fba7e3d4 release-3-0-x
include cstdio where needed
author | Rafael Laboissiere <rafael@debian.org> |
---|---|
date | Mon, 23 Feb 2009 21:06:42 +0100 |
parents | 93c65f2a5668 |
children |
rev | line source |
---|---|
6375 | 1 /* |
2 | |
3 Copyright (C) 2007 David Bateman | |
4 | |
5 This file is part of Octave. | |
6 | |
7 Octave is free software; you can redistribute it and/or modify it | |
8 under the terms of the GNU General Public License as published by the | |
7016 | 9 Free Software Foundation; either version 3 of the License, or (at your |
10 option) any later version. | |
6375 | 11 |
12 Octave is distributed in the hope that it will be useful, but WITHOUT | |
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 for more details. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
7016 | 18 along with Octave; see the file COPYING. If not, see |
19 <http://www.gnu.org/licenses/>. | |
6375 | 20 |
21 */ | |
22 | |
23 #ifdef HAVE_CONFIG_H | |
24 #include "config.h" | |
25 #endif | |
26 | |
11936
e896fba7e3d4
include cstdio where needed
Rafael Laboissiere <rafael@debian.org>
parents:
7016
diff
changeset
|
27 #include <cstdio> |
e896fba7e3d4
include cstdio where needed
Rafael Laboissiere <rafael@debian.org>
parents:
7016
diff
changeset
|
28 |
e896fba7e3d4
include cstdio where needed
Rafael Laboissiere <rafael@debian.org>
parents:
7016
diff
changeset
|
29 #include <string> |
e896fba7e3d4
include cstdio where needed
Rafael Laboissiere <rafael@debian.org>
parents:
7016
diff
changeset
|
30 #include <vector> |
e896fba7e3d4
include cstdio where needed
Rafael Laboissiere <rafael@debian.org>
parents:
7016
diff
changeset
|
31 |
6383 | 32 #include "lo-error.h" |
6375 | 33 #include "oct-md5.h" |
34 #include "md5.h" | |
35 | |
36 std::string | |
37 oct_md5 (const std::string str) | |
38 { | |
39 md5_state_t state; | |
40 | |
41 OCTAVE_LOCAL_BUFFER (md5_byte_t, digest, 16); | |
42 md5_init (&state); | |
43 md5_append (&state, reinterpret_cast<const md5_byte_t *>(str.c_str()), | |
44 str.length()); | |
45 md5_finish (&state, digest); | |
46 | |
47 OCTAVE_LOCAL_BUFFER (char, tmp, 33); | |
48 for (octave_idx_type i = 0; i < 16; i++) | |
49 sprintf (&tmp[2*i], "%02x", digest[i]); | |
50 tmp[32] = 0; | |
51 return std::string (tmp); | |
52 } | |
53 | |
6383 | 54 std::string |
55 oct_md5_file (const std::string file) | |
56 { | |
57 FILE *ifile = fopen (file.c_str (), "rb"); | |
58 | |
59 if (! ifile) | |
60 { | |
61 (*current_liboctave_error_handler) ("unable to open file `%s' for writing", | |
62 file.c_str()); | |
63 return std::string(); | |
64 } | |
65 else | |
66 { | |
67 md5_state_t state; | |
68 size_t nel; | |
69 | |
70 OCTAVE_LOCAL_BUFFER (md5_byte_t, digest, 16); | |
71 OCTAVE_LOCAL_BUFFER (md5_byte_t, buf, 1024); | |
72 | |
73 md5_init (&state); | |
74 | |
75 while ((nel = fread (buf, 1, 1024, ifile))) | |
76 md5_append (&state, buf, nel); | |
77 | |
78 fclose (ifile); | |
79 | |
80 md5_finish (&state, digest); | |
81 | |
82 OCTAVE_LOCAL_BUFFER (char, tmp, 33); | |
83 for (octave_idx_type i = 0; i < 16; i++) | |
84 sprintf (&tmp[2*i], "%02x", digest[i]); | |
85 tmp[32] = 0; | |
86 return std::string (tmp); | |
87 } | |
88 } | |
89 | |
6375 | 90 /* |
91 ;;; Local Variables: *** | |
92 ;;; mode: C++ *** | |
93 ;;; End: *** | |
94 */ |