-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidusutil.h
170 lines (149 loc) · 5.76 KB
/
validusutil.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/**
* @file validusutil.h
* @brief Definitions of the Validus utility functions.
*
* Defines the functions utilized by the Validus CLI application.
*
* @author Ryan M. Lederman \<lederman@gmail.com\>
* @date 2004-2023
* @version 1.0.4
* @copyright The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef _VALIDUS_UTIL_H_INCLUDED
# define _VALIDUS_UTIL_H_INCLUDED
# include "validus.h"
# include <stdio.h>
# include <stdlib.h>
# include <inttypes.h>
# include <string.h>
# include <errno.h>
# include <time.h>
# if defined(_WIN32)
# define _CRT_SECURE_NO_WARNINGS
# define WIN32_LEAN_AND_MEAN
# define WINVER 0x0A00
# define _WIN32_WINNT 0x0A00
# include <Windows.h>
# define __WIN__
# endif
/**
* @defgroup util Utility
*
* Utility wrappers around the core Validus implementation. Provides functions
* for hashing files, strings, and blocks of memory, etc.
*
* @addtogroup util
* @{
*/
///////////////////////////////// macros ///////////////////////////////////////
/** The size, in octets used to read blocks of data from a file. */
# define VALIDUS_FILE_BLOCKSIZE 8192UL
/** The maximum size, in octets of a string to hash. */
# define VALIDUS_MAX_STRING 2048UL
/** Format specifier string for a Validus fingerprint. */
# define VALIDUS_FP_FMT_SPEC \
"%08" PRIx32 "%08" PRIx32 "%08" PRIx32 "%08" PRIx32 "%08" PRIx32 "%08" PRIx32
//////////////////////////// function exports //////////////////////////////////
# ifdef __cplusplus
extern "C" {
# endif
/**
* @brief Hashes a string.
*
* @param state Pointer to a validus_state object which will contain the
* results of the operation upon success.
* @param string Pointer to the string to hash. Will be read up until the first
* null terminator, or VALIDUS_MAX_STRING; whichever comes first.
* @returns bool `true` if input parameters are valid, `false` otherwise.
*/
bool validus_hash_string(validus_state* state, const char* string);
/**
* @brief Hashes a block of memory.
*
* @param state Pointer to a validus_state object which will contain the
* results of the operation upon success.
* @param mem Pointer to the area of memory to hash.
* @param len Length of `mem` in octets.
* @returns bool `true` if input parameters are valid, `false` otherwise.
*/
bool validus_hash_mem(validus_state* state, const void* mem, size_t len);
/**
* @brief Hashes a file.
*
* @note If a relative pathname is supplied, it shall be treated as relative
* to the current working directory. This may or may not be the same directory
* that this program resides in.
*
* @note The preprocessor macro VALIDUS_FILE_BLOCKSIZE may be modified at compile
* time to suit your needs if the default value (8 KiB) is insufficient.
*
* @param state Pointer to a validus_state object which will contain the
* results of the operation upon success.
* @param file Absolute or relative pathname to the file to hash.
* @returns bool `true` if the file is opened and read successfully, `false`
* otherwise.
*/
bool validus_hash_file(validus_state *state, const char *file);
/**
* @brief Converts a validus_state to hexadecimal string form.
*
* @param state Pointer to the validus_state to convert.
* @param out Pointer to a buffer to receive the formatted string.
* @param len The length of `out` in octets. Must be at minimum 49.
* @returns bool `true` if input parameters are valid, and conversion succeeds,
* `false` otherwise.
*/
bool validus_state_to_string(const validus_state *state, char *out, size_t len);
/** @} */
////////////////////////// internal functions //////////////////////////////////
/** A platform-dependent timer used for performance measurement. */
typedef struct {
# if defined(__WIN__)
FILETIME ft; /**< The timer type on Windows. */
# else
struct timespec ts; /**< The timer type on *nix */
# endif
} validus_timer;
/**
* @brief Starts a validus_timer.
*
* @param timer Pointer to the validus_timer which should be initialized to the
* chosen clock's current value.
*/
void validus_timer_start(validus_timer* timer);
/**
* @brief Returns the milliseconds that have elapsed since `timer` was started.
*
* @param timer Pointer to the validus_timer which should be examined
* for its elapsed time.
* @returns double The number of milliseconds that have elapsed since
* ::validus_timer_start was called for `timer`.
*/
double validus_timer_elapsed(const validus_timer* timer);
/**
* @brief Retrieves the local time.
*
* @returns const char* A formatted time stamp for the current local time.
*/
const char* validus_get_local_time(void);
# ifdef __cplusplus
}
# endif
#endif /* !_VALIDUS_UTIL_H_INCLUDED */