checksums.h 6.06 KB
Newer Older
1 2 3 4 5 6 7
#ifndef REPREPRO_CHECKSUMS_H
#define REPREPRO_CHECKSUMS_H

#ifndef REPREPRO_ERROR_H
#include "error.h"
#warning "What's hapening here?"
#endif
8 9 10
#ifndef REPREPRO_STRLIST_H
#include "strlist.h"
#endif
11

12 13 14 15 16 17
enum checksumtype {
		/* must be first */
		cs_md5sum,
		/* additionall hashes: */
#define cs_firstEXTENDED cs_sha1sum
		cs_sha1sum,
Bernhard Link's avatar
Bernhard Link committed
18
		cs_sha256sum,
19 20 21 22 23
#define cs_hashCOUNT cs_length
		/* must be last but one */
		cs_length,
		/* must be last */
		cs_COUNT };
24 25 26

struct checksums;

27 28
extern const char * const changes_checksum_names[];
extern const char * const source_checksum_names[];
29
extern const char * const release_checksum_names[];
Bernhard Link's avatar
Bernhard Link committed
30
extern const struct constant *hashnames;
31

32 33 34 35 36 37 38
struct hashes {
	struct hash_data {
		const char *start; size_t len;
	} hashes[cs_COUNT];
};


39 40
void checksums_free(/*@only@*//*@null@*/struct checksums *);

41
/* duplicate a checksum record, NULL means OOM */
42
/*@null@*/struct checksums *checksums_dup(const struct checksums *);
43

44
retvalue checksums_setall(/*@out@*/struct checksums **checksums_p, const char *combinedchecksum, size_t len);
45

46 47
retvalue checksums_initialize(/*@out@*/struct checksums **checksums_p, const struct hash_data *);
/* hashes[*] is free'd: */
48
retvalue checksums_init(/*@out@*/struct checksums **, char *hashes[cs_COUNT]);
49
retvalue checksums_parse(/*@out@*/struct checksums **, const char *);
50

51
off_t checksums_getfilesize(const struct checksums *);
52 53 54

/* get 0-terminated combined textual representation of the checksums,
 * including the size (including the trailing '\0'): */
55
retvalue checksums_getcombined(const struct checksums *, /*@out@*/const char **, /*@out@*/size_t *);
56 57

/* get a static pointer to a specific part of a checksum (wihtout size) */
58 59 60
bool checksums_getpart(const struct checksums *, enum checksumtype, /*@out@*/const char **, /*@out@*/size_t *);
/* extract a single checksum from the combined data: */
bool checksums_gethashpart(const struct checksums *, enum checksumtype, /*@out@*/const char **hash_p, /*@out@*/size_t *hashlen_p, /*@out@*/const char **size_p, /*@out@*/size_t *sizelen_p);
61 62

/* check if a single checksum fits */
63
bool checksums_matches(const struct checksums *, enum checksumtype, const char *);
64 65

/* Copy file <origin> to file <destination>, calculating checksums */
66 67
retvalue checksums_copyfile(const char * /*destination*/, const char * /*origin*/, bool /*deletetarget*/, /*@out@*/struct checksums **);
retvalue checksums_hardlink(const char * /*directory*/, const char * /*filekey*/, const char * /*sourcefilename*/, const struct checksums *);
68

69
retvalue checksums_linkorcopyfile(const char * /*destination*/, const char * /*origin*/, /*@out@*/struct checksums **);
70

71
/* calculare checksums of a file: */
72
retvalue checksums_read(const char * /*fullfilename*/, /*@out@*/struct checksums **);
73

74
/* replace the contents of a file with data and calculate the new checksums */
75
retvalue checksums_replace(const char * /*filename*/, const char *, size_t, /*@out@*//*@null@*/struct checksums **);
76

77
/* check if the file has the given md5sum (only cheap tests like size),
78 79 80
 * RET_NOTHING means file does not exist,
 * RET_ERROR_WRONG_MD5 means wrong size */
retvalue checksums_cheaptest(const char * /*fullfilename*/, const struct checksums *, bool);
81

82
/* check if filename has specified checksums, if not return RET_ERROR_WRONG_MD5,
83 84
 * if it has, and checksums_p put the improved checksum there
 * (*checksums_p should either be NULL or checksums) */
85 86
retvalue checksums_test(const char *, const struct checksums *, /*@null@*/struct checksums **);

87
/* check if checksum of filekey in database and checksum of actual file, set improve if some new has is in the last */
88
bool checksums_check(const struct checksums *, const struct checksums *, /*@out@*/bool * /*improves_p*/);
89

90 91 92
/* return true, iff all supported checksums are available */
bool checksums_iscomplete(const struct checksums *);

93 94 95
/* Collect missing checksums (if all are there always RET_OK without checking).
 * if the file is not there, return RET_NOTHING,
 * if it is but not matches, return RET_ERROR_WRONG_MD5 */
96
retvalue checksums_complete(struct checksums **, const char * /*fullfilename*/);
97

98
void checksums_printdifferences(FILE *, const struct checksums * /*expected*/, const struct checksums * /*got*/);
99

100
retvalue checksums_combine(struct checksums **, const struct checksums *, /*@null@*/bool[cs_hashCOUNT]);
101

102
typedef /*@only@*/ struct checksums *ownedchecksums;
103 104
struct checksumsarray {
	struct strlist names;
105
	/*@null@*/ownedchecksums *checksums;
106
};
107 108
void checksumsarray_move(/*@out@*/struct checksumsarray *, /*@special@*/struct checksumsarray *array)/*@requires maxSet(array->names.values) >= array->names.count /\ maxSet(array->checksums) >= array->names.count @*/ /*@releases array->checksums, array->names.values @*/;
void checksumsarray_done(/*@special@*/struct checksumsarray *array) /*@requires maxSet(array->names.values) >= array->names.count /\ maxSet(array->checksums) >= array->names.count @*/ /*@releases array->checksums, array->names.values @*/;
109
retvalue checksumsarray_parse(/*@out@*/struct checksumsarray *, const struct strlist [cs_hashCOUNT], const char * /*filenametoshow*/);
110
retvalue checksumsarray_genfilelist(const struct checksumsarray *, /*@out@*/char **, /*@out@*/char **, /*@out@*/char **);
111
retvalue checksumsarray_include(struct checksumsarray *, /*@only@*/char *, const struct checksums *);
112
void checksumsarray_resetunsupported(const struct checksumsarray *, bool[cs_hashCOUNT]);
113

114
retvalue hashline_parse(const char * /*filenametoshow*/, const char * /*line*/, enum checksumtype, /*@out@*/const char ** /*basename_p*/, /*@out@*/struct hash_data *, /*@out@*/struct hash_data *);
115

Bernhard Link's avatar
Bernhard Link committed
116 117
struct configiterator;

118 119 120 121 122 123 124
#ifdef CHECKSUMS_CONTEXT
#ifndef MD5_H
#include "md5.h"
#endif
#ifndef REPREPRO_SHA1_H
#include "sha1.h"
#endif
Bernhard Link's avatar
Bernhard Link committed
125 126 127
#ifndef REPREPRO_SHA256_H
#include "sha256.h"
#endif
128 129 130 131

struct checksumscontext {
	struct MD5Context md5;
	struct SHA1_Context sha1;
Bernhard Link's avatar
Bernhard Link committed
132
	struct SHA256_Context sha256;
133 134 135 136 137 138
};

void checksumscontext_init(/*@out@*/struct checksumscontext *);
void checksumscontext_update(struct checksumscontext *, const unsigned char *, size_t);
retvalue checksums_from_context(/*@out@*/struct checksums **, struct checksumscontext *);
#endif
139

140
#endif