~ubuntu-branches/debian/stretch/ccache/stretch

« back to all changes in this revision

Viewing changes to hash.c

  • Committer: Bazaar Package Importer
  • Author(s): Joel Rosdahl
  • Date: 2010-12-02 21:05:17 UTC
  • mfrom: (5.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20101202210517-ji5owl2qa3s5c1rg
New upstream version.

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
 
20
20
#include "ccache.h"
21
21
 
22
 
#include <sys/types.h>
23
 
#include <sys/stat.h>
24
 
#include <fcntl.h>
25
 
#include <stdio.h>
26
 
#include <string.h>
27
 
#include <unistd.h>
28
 
 
29
22
#define HASH_DELIMITER "\000cCaChE"
30
23
 
31
 
void hash_buffer(struct mdfour *md, const void *s, size_t len)
 
24
void
 
25
hash_start(struct mdfour *md)
 
26
{
 
27
        mdfour_begin(md);
 
28
}
 
29
 
 
30
void
 
31
hash_buffer(struct mdfour *md, const void *s, size_t len)
32
32
{
33
33
        mdfour_update(md, (unsigned char *)s, len);
34
34
}
35
35
 
36
 
void hash_start(struct mdfour *md)
37
 
{
38
 
        mdfour_begin(md);
 
36
/* Return the hash result as a hex string. Caller frees. */
 
37
char *
 
38
hash_result(struct mdfour *md)
 
39
{
 
40
        unsigned char sum[16];
 
41
 
 
42
        hash_result_as_bytes(md, sum);
 
43
        return format_hash_as_string(sum, (unsigned) md->totalN);
 
44
}
 
45
 
 
46
/* return the hash result as 16 binary bytes */
 
47
void
 
48
hash_result_as_bytes(struct mdfour *md, unsigned char *out)
 
49
{
 
50
        hash_buffer(md, NULL, 0);
 
51
        mdfour_result(md, out);
 
52
}
 
53
 
 
54
bool
 
55
hash_equal(struct mdfour *md1, struct mdfour *md2)
 
56
{
 
57
        unsigned char sum1[16], sum2[16];
 
58
        hash_result_as_bytes(md1, sum1);
 
59
        hash_result_as_bytes(md2, sum2);
 
60
        return memcmp(sum1, sum2, sizeof(sum1)) == 0;
39
61
}
40
62
 
41
63
/*
48
70
 *   information X if CCACHE_A is set and information Y if CCACHE_B is set,
49
71
 *   there should never be a hash collision risk).
50
72
 */
51
 
void hash_delimiter(struct mdfour *md, const char *type)
 
73
void
 
74
hash_delimiter(struct mdfour *md, const char *type)
52
75
{
53
76
        hash_buffer(md, HASH_DELIMITER, sizeof(HASH_DELIMITER));
54
77
        hash_buffer(md, type, strlen(type) + 1); /* Include NUL. */
55
78
}
56
79
 
57
 
void hash_string(struct mdfour *md, const char *s)
 
80
void
 
81
hash_string(struct mdfour *md, const char *s)
58
82
{
59
83
        hash_buffer(md, s, strlen(s));
60
84
}
61
85
 
62
 
void hash_int(struct mdfour *md, int x)
 
86
void
 
87
hash_int(struct mdfour *md, int x)
63
88
{
64
89
        hash_buffer(md, (char *)&x, sizeof(x));
65
90
}
66
91
 
67
92
/*
68
 
 * Add contents of an open file to the hash. Returns 1 on success, otherwise 0.
 
93
 * Add contents of an open file to the hash. Returns true on success, otherwise
 
94
 * false.
69
95
 */
70
 
int hash_fd(struct mdfour *md, int fd)
 
96
bool
 
97
hash_fd(struct mdfour *md, int fd)
71
98
{
72
 
        char buf[1024];
73
 
        size_t n;
 
99
        char buf[16384];
 
100
        ssize_t n;
74
101
 
75
 
        while ((n = read(fd, buf, sizeof(buf))) > 0) {
76
 
                hash_buffer(md, buf, n);
77
 
        }
78
 
        if (n == 0) {
79
 
                return 1;
80
 
        } else {
81
 
                return 0;
82
 
        }
 
102
        while ((n = read(fd, buf, sizeof(buf))) != 0) {
 
103
                if (n == -1 && errno != EINTR) {
 
104
                        break;
 
105
                }
 
106
                if (n > 0) {
 
107
                        hash_buffer(md, buf, n);
 
108
                }
 
109
        }
 
110
        return n == 0;
83
111
}
84
112
 
85
113
/*
86
 
 * Add contents of a file to the hash. Returns 1 on success, otherwise 0.
 
114
 * Add contents of a file to the hash. Returns true on success, otherwise
 
115
 * false.
87
116
 */
88
 
int hash_file(struct mdfour *md, const char *fname)
 
117
bool
 
118
hash_file(struct mdfour *md, const char *fname)
89
119
{
90
120
        int fd;
91
 
        int ret;
 
121
        bool ret;
92
122
 
93
123
        fd = open(fname, O_RDONLY|O_BINARY);
94
124
        if (fd == -1) {
95
 
                return 0;
 
125
                return false;
96
126
        }
97
127
 
98
128
        ret = hash_fd(md, fd);
99
129
        close(fd);
100
130
        return ret;
101
131
}
102
 
 
103
 
/* Return the hash result as a hex string. Caller frees. */
104
 
char *hash_result(struct mdfour *md)
105
 
{
106
 
        unsigned char sum[16];
107
 
 
108
 
        hash_result_as_bytes(md, sum);
109
 
        return format_hash_as_string(sum, (unsigned) md->totalN);
110
 
}
111
 
 
112
 
/* return the hash result as 16 binary bytes */
113
 
void hash_result_as_bytes(struct mdfour *md, unsigned char *out)
114
 
{
115
 
        hash_buffer(md, NULL, 0);
116
 
        mdfour_result(md, out);
117
 
}