]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/chksum.c
Apply fix from Martin Simmons to clear structure before
[bacula/bacula] / bacula / src / filed / chksum.c
1 /*
2  * General routines for handling the various checksum supported.
3  *
4  *  Written by Preben 'Peppe' Guldberg, December MMIV
5  */
6 /*
7    Copyright (C) 2004-2005 Kern Sibbald
8
9    This program is free software; you can redistribute it and/or
10    modify it under the terms of the GNU General Public License
11    version 2 as amended with additional clauses defined in the
12    file LICENSE in the main source directory.
13
14    This program is distributed in the hope that it will be useful,
15    but WITHOUT ANY WARRANTY; without even the implied warranty of
16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
17    the file LICENSE for additional details.
18
19  */
20
21 #include "bacula.h"
22 #include "filed.h"
23
24 /* return 0 on success, otherwise some handler specific error code. */
25 int chksum_init(CHKSUM *chksum, int flags)
26 {
27    int status = 0;
28
29    chksum->type = CHKSUM_NONE;
30    bstrncpy(chksum->name, "NONE", sizeof(chksum->name));
31    chksum->updated = false;
32    if (flags & CHKSUM_MD5) {
33       chksum->length = 16;
34       MD5Init(&chksum->context.md5);
35       chksum->type = CHKSUM_MD5;
36       bstrncpy(chksum->name, "MD5", sizeof(chksum->name));
37    } else if (flags & CHKSUM_SHA1) {
38       chksum->length = 20;
39       status = SHA1Init(&chksum->context.sha1);
40       if (status == 0) {
41          chksum->type = CHKSUM_SHA1;
42          bstrncpy(chksum->name, "SHA1", sizeof(chksum->name));
43       }
44    }
45    return status;
46 }
47
48 /* return 0 on success, otherwise some handler specific error code. */
49 int chksum_update(CHKSUM *chksum, void *buf, unsigned len)
50 {
51    int status;
52    switch (chksum->type) {
53    case CHKSUM_NONE:
54       return 0;
55    case CHKSUM_MD5:
56       MD5Update(&chksum->context.md5, (unsigned char *)buf, len);
57       chksum->updated = true;
58       return 0;
59    case CHKSUM_SHA1:
60       status = SHA1Update(&chksum->context.sha1, (uint8_t *)buf, len);
61       if (status == 0) {
62          chksum->updated = true;
63       }
64       return status;
65    default:
66       return -1;
67    }
68 }
69
70 /* return 0 on success, otherwise some handler specific error code. */
71 int chksum_final(CHKSUM *chksum)
72 {
73    switch (chksum->type) {
74    case CHKSUM_NONE:
75       return 0;
76    case CHKSUM_MD5:
77       MD5Final(chksum->signature, &chksum->context.md5);
78       return 0;
79    case CHKSUM_SHA1:
80       return SHA1Final(&chksum->context.sha1, chksum->signature);
81    default:
82       return -1;
83    }
84 }