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
*sha1create(const char *fmt
, ...)
83 f
= xmalloc(sizeof(*f
));
86 len
= vsnprintf(f
->name
, sizeof(f
->name
), fmt
, arg
);
89 die("you wascally wabbit, you");
92 fd
= open(f
->name
, O_CREAT
| O_EXCL
| O_WRONLY
, 0666);
94 die("unable to open %s (%s)", f
->name
, strerror(errno
));
103 struct sha1file
*sha1fd(int fd
, const char *name
)
108 f
= xmalloc(sizeof(*f
));
112 die("you wascally wabbit, you");
114 memcpy(f
->name
, name
, len
+1);
124 int sha1write_compressed(struct sha1file
*f
, void *in
, unsigned int size
, int level
)
127 unsigned long maxsize
;
130 memset(&stream
, 0, sizeof(stream
));
131 deflateInit(&stream
, level
);
132 maxsize
= deflateBound(&stream
, size
);
133 out
= xmalloc(maxsize
);
137 stream
.avail_in
= size
;
139 stream
.next_out
= out
;
140 stream
.avail_out
= maxsize
;
142 while (deflate(&stream
, Z_FINISH
) == Z_OK
)
146 size
= stream
.total_out
;
147 sha1write(f
, out
, size
);
152 void crc32_begin(struct sha1file
*f
)
154 f
->crc32
= crc32(0, Z_NULL
, 0);
158 uint32_t crc32_end(struct sha1file
*f
)