~ubuntu-branches/ubuntu/intrepid/git-core/intrepid-security

« back to all changes in this revision

Viewing changes to csum-file.c

  • Committer: Package Import Robot
  • Author(s): Gerrit Pape
  • Date: 2007-10-04 08:27:01 UTC
  • mfrom: (1.1.23)
  • Revision ID: package-import@ubuntu.com-20071004082701-rsd058ontoqz4i30
Tags: 1:1.5.3.4-1
new upstream point release (closes: #445188).

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
        }
30
30
}
31
31
 
32
 
int sha1close(struct sha1file *f, unsigned char *result, int update)
 
32
int sha1close(struct sha1file *f, unsigned char *result, int final)
33
33
{
34
34
        unsigned offset = f->offset;
35
35
        if (offset) {
36
36
                SHA1_Update(&f->ctx, f->buffer, offset);
37
37
                sha1flush(f, offset);
 
38
                f->offset = 0;
38
39
        }
 
40
        if (!final)
 
41
                return 0;       /* only want to flush (no checksum write, no close) */
39
42
        SHA1_Final(f->buffer, &f->ctx);
40
43
        if (result)
41
44
                hashcpy(result, f->buffer);
42
 
        if (update)
43
 
                sha1flush(f, 20);
 
45
        sha1flush(f, 20);
44
46
        if (close(f->fd))
45
47
                die("%s: sha1 file error on close (%s)", f->name, strerror(errno));
46
48
        free(f);
71
73
        return 0;
72
74
}
73
75
 
74
 
struct sha1file *sha1create(const char *fmt, ...)
75
 
{
76
 
        struct sha1file *f;
77
 
        unsigned len;
78
 
        va_list arg;
79
 
        int fd;
80
 
 
81
 
        f = xmalloc(sizeof(*f));
82
 
 
83
 
        va_start(arg, fmt);
84
 
        len = vsnprintf(f->name, sizeof(f->name), fmt, arg);
85
 
        va_end(arg);
86
 
        if (len >= PATH_MAX)
87
 
                die("you wascally wabbit, you");
88
 
        f->namelen = len;
89
 
 
90
 
        fd = open(f->name, O_CREAT | O_EXCL | O_WRONLY, 0666);
91
 
        if (fd < 0)
92
 
                die("unable to open %s (%s)", f->name, strerror(errno));
93
 
        f->fd = fd;
94
 
        f->error = 0;
95
 
        f->offset = 0;
96
 
        f->do_crc = 0;
97
 
        SHA1_Init(&f->ctx);
98
 
        return f;
99
 
}
100
 
 
101
76
struct sha1file *sha1fd(int fd, const char *name)
102
77
{
103
78
        struct sha1file *f;
119
94
        return f;
120
95
}
121
96
 
122
 
int sha1write_compressed(struct sha1file *f, void *in, unsigned int size)
123
 
{
124
 
        z_stream stream;
125
 
        unsigned long maxsize;
126
 
        void *out;
127
 
 
128
 
        memset(&stream, 0, sizeof(stream));
129
 
        deflateInit(&stream, zlib_compression_level);
130
 
        maxsize = deflateBound(&stream, size);
131
 
        out = xmalloc(maxsize);
132
 
 
133
 
        /* Compress it */
134
 
        stream.next_in = in;
135
 
        stream.avail_in = size;
136
 
 
137
 
        stream.next_out = out;
138
 
        stream.avail_out = maxsize;
139
 
 
140
 
        while (deflate(&stream, Z_FINISH) == Z_OK)
141
 
                /* nothing */;
142
 
        deflateEnd(&stream);
143
 
 
144
 
        size = stream.total_out;
145
 
        sha1write(f, out, size);
146
 
        free(out);
147
 
        return size;
148
 
}
149
 
 
150
97
void crc32_begin(struct sha1file *f)
151
98
{
152
99
        f->crc32 = crc32(0, Z_NULL, 0);