18
18
*
19
19
*/
20
20
21
+ #include <errno.h>
22
+ #include <stdio.h>
21
23
#include <mqueue.h>
24
+ #include <pthread.h>
22
25
23
26
#ifndef BS_COMMON_H
24
27
#define BS_COMMON_H
@@ -42,15 +45,39 @@ typedef struct {
42
45
43
46
#ifdef USE_OPENSSL
44
47
#include <openssl/sha.h>
48
+ static inline void bs_sha1 (void * hash , const void * message , size_t length ) {
49
+ SHA1 (message , length , hash );
50
+ }
45
51
#elif USE_GCRYPT
46
52
#include <gcrypt.h>
53
+ static inline void bs_sha1 (void * hash , const void * message , size_t length ) {
54
+ gcry_md_hash_buffer (GCRY_MD_SHA1 , hash , message , length );
55
+ }
47
56
#else
48
57
#error No cryptographic library specififed.
49
58
#endif
50
59
51
- void bs_sha1 (void * hash , const void * message , size_t length );
60
+ /**
61
+ * Print out a sha1 hash
62
+ */
63
+ static inline void bs_print_hash (unsigned char * hash , int length ) {
64
+ int i ;
65
+ for (i = 0 ; i < length ; i ++ ) {
66
+ printf ("%02x" , hash [i ]);
67
+ }
68
+ }
52
69
53
- void bs_print_hash (unsigned char * hash , int length );
70
+ /**
71
+ * Safely open a file for read/write if it does not exist, without wiping
72
+ * the contents of the file.
73
+ */
74
+ static inline FILE * bs_open_rw (char * path ) {
75
+ FILE * fp ;
54
76
55
- FILE * bs_open_rw (char * path );
77
+ fp = fopen (path , "r+" );
78
+ if (fp == NULL && errno == ENOENT ) {
79
+ fp = fopen (path , "w+" );
80
+ }
81
+ return fp ;
82
+ }
56
83
#endif
0 commit comments