4 * Copyright (C) 2005 Linus Torvalds
6 * Simple file write infrastructure for writing SHA1-summed
7 * files. Useful when you write a file that you want to be
8 * able to verify hasn't been messed with afterwards.
11 #include "csum-file.h"
13 static void sha1flush(struct sha1file
*f
, unsigned int count
)
15 void *buf
= f
->buffer
;
18 int ret
= xwrite(f
->fd
, buf
, count
);
20 buf
= (char *) buf
+ ret
;
27 die("sha1 file '%s' write error. Out of diskspace", f
->name
);
28 die("sha1 file '%s' write error (%s)", f
->name
, strerror(errno
));
32 int sha1close(struct sha1file
*f
, unsigned char *result
, int final
)
34 unsigned offset
= f
->offset
;
36 SHA1_Update(&f
->ctx
, f
->buffer
, offset
);
41 return 0; /* only want to flush (no checksum write, no close) */
42 SHA1_Final(f
->buffer
, &f
->ctx
);
44 hashcpy(result
, f
->buffer
);
47 die("%s: sha1 file error on close (%s)", f
->name
, strerror(errno
));
52 int sha1write(struct sha1file
*f
, void *buf
, unsigned int count
)
55 f
->crc32
= crc32(f
->crc32
, buf
, count
);
57 unsigned offset
= f
->offset
;
58 unsigned left
= sizeof(f
->buffer
) - offset
;
59 unsigned nr
= count
> left
? left
: count
;
61 memcpy(f
->buffer
+ offset
, buf
, nr
);
64 buf
= (char *) buf
+ nr
;
67 SHA1_Update(&f
->ctx
, f
->buffer
, offset
);
76 struct sha1file
*sha1fd(int fd
, const char *name
)
81 f
= xmalloc(sizeof(*f
));
85 die("you wascally wabbit, you");
87 memcpy(f
->name
, name
, len
+1);
97 void crc32_begin(struct sha1file
*f
)
99 f
->crc32
= crc32(0, Z_NULL
, 0);
103 uint32_t crc32_end(struct sha1file
*f
)