]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/filed/chksum.c
4453d7d6af1964b0feaaff75d670d0a0d7618e49
[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 /*
8    Copyright (C) 2004 Kern Sibbald
9
10    This program is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License as
12    published by the Free Software Foundation; either version 2 of
13    the License, or (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18    General Public License for more details.
19
20    You should have received a copy of the GNU General Public
21    License along with this program; if not, write to the Free
22    Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
23    MA 02111-1307, USA.
24
25  */
26
27 #include "bacula.h"
28 #include "filed.h"
29
30 /* return 0 on success, otherwise some handler specific error code. */
31 int chksum_init(CHKSUM *chksum, int flags)
32 {
33    int status = 0;
34
35    chksum->type = CHKSUM_NONE;
36    bstrncpy(chksum->name, "NONE", sizeof(chksum->name));
37    chksum->updated = false;
38    if (flags & CHKSUM_MD5) {
39       chksum->length = 16;
40       MD5Init(&chksum->context.md5);
41       chksum->type = CHKSUM_MD5;
42       bstrncpy(chksum->name, "MD5", sizeof(chksum->name));
43    } else if (flags * CHKSUM_SHA1) {
44       chksum->length = 20;
45       status = SHA1Init(&chksum->context.sha1);
46       if (status == 0) {
47          chksum->type = CHKSUM_SHA1;
48          bstrncpy(chksum->name, "SHA1", sizeof(chksum->name));
49       }
50    }
51    return status;
52 }
53
54 /* return 0 on success, otherwise some handler specific error code. */
55 int chksum_update(CHKSUM *chksum, void *buf, unsigned len)
56 {
57    int status;
58    switch (chksum->type) {
59    case CHKSUM_NONE:
60       return 0;
61    case CHKSUM_MD5:
62       MD5Update(&chksum->context.md5, (unsigned char *)buf, len);
63       chksum->updated = true;
64       return 0;
65    case CHKSUM_SHA1:
66       status = SHA1Update(&chksum->context.sha1, (uint8_t *)buf, len);
67       if (status == 0) {
68          chksum->updated = true;
69       }
70       return status;
71    default:
72       return -1;
73    }
74 }
75
76 /* return 0 on success, otherwise some handler specific error code. */
77 int chksum_final(CHKSUM *chksum)
78 {
79    switch (chksum->type) {
80    case CHKSUM_NONE:
81       return 0;
82    case CHKSUM_MD5:
83       MD5Final(chksum->signature, &chksum->context.md5);
84       return 0;
85    case CHKSUM_SHA1:
86       return SHA1Final(&chksum->context.sha1, chksum->signature);
87    default:
88       return -1;
89    }
90 }