Skip to content

Commit

Permalink
fix openssl headers conflicting with custom SHA1 implementations
Browse files Browse the repository at this point in the history
On ARM I have the following compilation errors:

    CC fast-import.o
In file included from cache.h:8,
                 from builtin.h:6,
                 from fast-import.c:142:
arm/sha1.h:14: error: conflicting types for 'SHA_CTX'
/usr/include/openssl/sha.h:105: error: previous declaration of 'SHA_CTX' was here
arm/sha1.h:16: error: conflicting types for 'SHA1_Init'
/usr/include/openssl/sha.h:115: error: previous declaration of 'SHA1_Init' was here
arm/sha1.h:17: error: conflicting types for 'SHA1_Update'
/usr/include/openssl/sha.h:116: error: previous declaration of 'SHA1_Update' was here
arm/sha1.h:18: error: conflicting types for 'SHA1_Final'
/usr/include/openssl/sha.h:117: error: previous declaration of 'SHA1_Final' was here
make: *** [fast-import.o] Error 1

This is because openssl header files are always included in
git-compat-util.h since commit 684ec6c whenever NO_OPENSSL is not
set, which somehow brings in <openssl/sha1.h> clashing with the custom
ARM version.  Compilation of git is probably broken on PPC too for the
same reason.

Turns out that the only file requiring openssl/ssl.h and openssl/err.h
is imap-send.c.  But only moving those problematic includes there
doesn't solve the issue as it also includes cache.h which brings in the
conflicting local SHA1 header file.

As suggested by Jeff King, the best solution is to rename our references
to SHA1 functions and structure to something git specific, and define those
according to the implementation used.

Signed-off-by: Nicolas Pitre <nico@cam.org>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
  • Loading branch information
Nicolas Pitre authored and spearce committed Oct 3, 2008
1 parent 120a385 commit 9126f00
Show file tree
Hide file tree
Showing 24 changed files with 162 additions and 141 deletions.
16 changes: 8 additions & 8 deletions arm/sha1.c
Expand Up @@ -8,9 +8,9 @@
#include <string.h>
#include "sha1.h"

extern void sha_transform(uint32_t *hash, const unsigned char *data, uint32_t *W);
extern void arm_sha_transform(uint32_t *hash, const unsigned char *data, uint32_t *W);

void SHA1_Init(SHA_CTX *c)
void arm_SHA1_Init(arm_SHA_CTX *c)
{
c->len = 0;
c->hash[0] = 0x67452301;
Expand All @@ -20,7 +20,7 @@ void SHA1_Init(SHA_CTX *c)
c->hash[4] = 0xc3d2e1f0;
}

void SHA1_Update(SHA_CTX *c, const void *p, unsigned long n)
void arm_SHA1_Update(arm_SHA_CTX *c, const void *p, unsigned long n)
{
uint32_t workspace[80];
unsigned int partial;
Expand All @@ -32,12 +32,12 @@ void SHA1_Update(SHA_CTX *c, const void *p, unsigned long n)
if (partial) {
done = 64 - partial;
memcpy(c->buffer + partial, p, done);
sha_transform(c->hash, c->buffer, workspace);
arm_sha_transform(c->hash, c->buffer, workspace);
partial = 0;
} else
done = 0;
while (n >= done + 64) {
sha_transform(c->hash, p + done, workspace);
arm_sha_transform(c->hash, p + done, workspace);
done += 64;
}
} else
Expand All @@ -46,7 +46,7 @@ void SHA1_Update(SHA_CTX *c, const void *p, unsigned long n)
memcpy(c->buffer + partial, p + done, n - done);
}

void SHA1_Final(unsigned char *hash, SHA_CTX *c)
void arm_SHA1_Final(unsigned char *hash, arm_SHA_CTX *c)
{
uint64_t bitlen;
uint32_t bitlen_hi, bitlen_lo;
Expand All @@ -57,7 +57,7 @@ void SHA1_Final(unsigned char *hash, SHA_CTX *c)
bitlen = c->len << 3;
offset = c->len & 0x3f;
padlen = ((offset < 56) ? 56 : (64 + 56)) - offset;
SHA1_Update(c, padding, padlen);
arm_SHA1_Update(c, padding, padlen);

bitlen_hi = bitlen >> 32;
bitlen_lo = bitlen & 0xffffffff;
Expand All @@ -69,7 +69,7 @@ void SHA1_Final(unsigned char *hash, SHA_CTX *c)
bits[5] = bitlen_lo >> 16;
bits[6] = bitlen_lo >> 8;
bits[7] = bitlen_lo;
SHA1_Update(c, bits, 8);
arm_SHA1_Update(c, bits, 8);

for (i = 0; i < 5; i++) {
uint32_t v = c->hash[i];
Expand Down
15 changes: 10 additions & 5 deletions arm/sha1.h
Expand Up @@ -7,12 +7,17 @@

#include <stdint.h>

typedef struct sha_context {
typedef struct {
uint64_t len;
uint32_t hash[5];
unsigned char buffer[64];
} SHA_CTX;
} arm_SHA_CTX;

void SHA1_Init(SHA_CTX *c);
void SHA1_Update(SHA_CTX *c, const void *p, unsigned long n);
void SHA1_Final(unsigned char *hash, SHA_CTX *c);
void arm_SHA1_Init(arm_SHA_CTX *c);
void arm_SHA1_Update(arm_SHA_CTX *c, const void *p, unsigned long n);
void arm_SHA1_Final(unsigned char *hash, arm_SHA_CTX *c);

#define git_SHA_CTX arm_SHA_CTX
#define git_SHA1_Init arm_SHA1_Init
#define git_SHA1_Update arm_SHA1_Update
#define git_SHA1_Final arm_SHA1_Final
4 changes: 2 additions & 2 deletions arm/sha1_arm.S
Expand Up @@ -10,15 +10,15 @@
*/

.text
.globl sha_transform
.globl arm_sha_transform

/*
* void sha_transform(uint32_t *hash, const unsigned char *data, uint32_t *W);
*
* note: the "data" pointer may be unaligned.
*/

sha_transform:
arm_sha_transform:

stmfd sp!, {r4 - r8, lr}

Expand Down
10 changes: 5 additions & 5 deletions builtin-unpack-objects.c
Expand Up @@ -19,7 +19,7 @@ static const char unpack_usage[] = "git unpack-objects [-n] [-q] [-r] [--strict]
static unsigned char buffer[4096];
static unsigned int offset, len;
static off_t consumed_bytes;
static SHA_CTX ctx;
static git_SHA_CTX ctx;

/*
* When running under --strict mode, objects whose reachability are
Expand Down Expand Up @@ -59,7 +59,7 @@ static void *fill(int min)
if (min > sizeof(buffer))
die("cannot fill %d bytes", min);
if (offset) {
SHA1_Update(&ctx, buffer, offset);
git_SHA1_Update(&ctx, buffer, offset);
memmove(buffer, buffer + offset, len);
offset = 0;
}
Expand Down Expand Up @@ -539,10 +539,10 @@ int cmd_unpack_objects(int argc, const char **argv, const char *prefix)
/* We don't take any non-flag arguments now.. Maybe some day */
usage(unpack_usage);
}
SHA1_Init(&ctx);
git_SHA1_Init(&ctx);
unpack_all();
SHA1_Update(&ctx, buffer, offset);
SHA1_Final(sha1, &ctx);
git_SHA1_Update(&ctx, buffer, offset);
git_SHA1_Final(sha1, &ctx);
if (strict)
write_rest();
if (hashcmp(fill(20), sha1))
Expand Down
8 changes: 7 additions & 1 deletion cache.h
Expand Up @@ -6,8 +6,14 @@
#include "hash.h"

#include SHA1_HEADER
#include <zlib.h>
#ifndef git_SHA_CTX
#define git_SHA_CTX SHA_CTX
#define git_SHA1_Init SHA1_Init
#define git_SHA1_Update SHA1_Update
#define git_SHA1_Final SHA1_Final
#endif

#include <zlib.h>
#if defined(NO_DEFLATE_BOUND) || ZLIB_VERNUM < 0x1200
#define deflateBound(c,s) ((s) + (((s) + 7) >> 3) + (((s) + 63) >> 6) + 11)
#endif
Expand Down
8 changes: 4 additions & 4 deletions csum-file.c
Expand Up @@ -36,11 +36,11 @@ int sha1close(struct sha1file *f, unsigned char *result, unsigned int flags)
unsigned offset = f->offset;

if (offset) {
SHA1_Update(&f->ctx, f->buffer, offset);
git_SHA1_Update(&f->ctx, f->buffer, offset);
sha1flush(f, f->buffer, offset);
f->offset = 0;
}
SHA1_Final(f->buffer, &f->ctx);
git_SHA1_Final(f->buffer, &f->ctx);
if (result)
hashcpy(result, f->buffer);
if (flags & (CSUM_CLOSE | CSUM_FSYNC)) {
Expand Down Expand Up @@ -82,7 +82,7 @@ int sha1write(struct sha1file *f, void *buf, unsigned int count)
buf = (char *) buf + nr;
left -= nr;
if (!left) {
SHA1_Update(&f->ctx, data, offset);
git_SHA1_Update(&f->ctx, data, offset);
sha1flush(f, data, offset);
offset = 0;
}
Expand All @@ -105,7 +105,7 @@ struct sha1file *sha1fd_throughput(int fd, const char *name, struct progress *tp
f->tp = tp;
f->name = name;
f->do_crc = 0;
SHA1_Init(&f->ctx);
git_SHA1_Init(&f->ctx);
return f;
}

Expand Down
2 changes: 1 addition & 1 deletion csum-file.h
Expand Up @@ -7,7 +7,7 @@ struct progress;
struct sha1file {
int fd;
unsigned int offset;
SHA_CTX ctx;
git_SHA_CTX ctx;
off_t total;
struct progress *tp;
const char *name;
Expand Down
12 changes: 6 additions & 6 deletions diff.c
Expand Up @@ -3087,7 +3087,7 @@ static void diff_summary(FILE *file, struct diff_filepair *p)
}

struct patch_id_t {
SHA_CTX *ctx;
git_SHA_CTX *ctx;
int patchlen;
};

Expand Down Expand Up @@ -3115,7 +3115,7 @@ static void patch_id_consume(void *priv, char *line, unsigned long len)

new_len = remove_space(line, len);

SHA1_Update(data->ctx, line, new_len);
git_SHA1_Update(data->ctx, line, new_len);
data->patchlen += new_len;
}

Expand All @@ -3124,11 +3124,11 @@ static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
{
struct diff_queue_struct *q = &diff_queued_diff;
int i;
SHA_CTX ctx;
git_SHA_CTX ctx;
struct patch_id_t data;
char buffer[PATH_MAX * 4 + 20];

SHA1_Init(&ctx);
git_SHA1_Init(&ctx);
memset(&data, 0, sizeof(struct patch_id_t));
data.ctx = &ctx;

Expand Down Expand Up @@ -3190,7 +3190,7 @@ static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
len2, p->two->path,
len1, p->one->path,
len2, p->two->path);
SHA1_Update(&ctx, buffer, len1);
git_SHA1_Update(&ctx, buffer, len1);

xpp.flags = XDF_NEED_MINIMAL;
xecfg.ctxlen = 3;
Expand All @@ -3199,7 +3199,7 @@ static int diff_get_patch_id(struct diff_options *options, unsigned char *sha1)
&xpp, &xecfg, &ecb);
}

SHA1_Final(sha1, &ctx);
git_SHA1_Final(sha1, &ctx);
return 0;
}

Expand Down
18 changes: 9 additions & 9 deletions fast-import.c
Expand Up @@ -845,7 +845,7 @@ static int oecmp (const void *a_, const void *b_)
static char *create_index(void)
{
static char tmpfile[PATH_MAX];
SHA_CTX ctx;
git_SHA_CTX ctx;
struct sha1file *f;
struct object_entry **idx, **c, **last, *e;
struct object_entry_pool *o;
Expand Down Expand Up @@ -882,17 +882,17 @@ static char *create_index(void)
idx_fd = xmkstemp(tmpfile);
f = sha1fd(idx_fd, tmpfile);
sha1write(f, array, 256 * sizeof(int));
SHA1_Init(&ctx);
git_SHA1_Init(&ctx);
for (c = idx; c != last; c++) {
uint32_t offset = htonl((*c)->offset);
sha1write(f, &offset, 4);
sha1write(f, (*c)->sha1, sizeof((*c)->sha1));
SHA1_Update(&ctx, (*c)->sha1, 20);
git_SHA1_Update(&ctx, (*c)->sha1, 20);
}
sha1write(f, pack_data->sha1, sizeof(pack_data->sha1));
sha1close(f, NULL, CSUM_FSYNC);
free(idx);
SHA1_Final(pack_data->sha1, &ctx);
git_SHA1_Final(pack_data->sha1, &ctx);
return tmpfile;
}

Expand Down Expand Up @@ -1033,15 +1033,15 @@ static int store_object(
unsigned char hdr[96];
unsigned char sha1[20];
unsigned long hdrlen, deltalen;
SHA_CTX c;
git_SHA_CTX c;
z_stream s;

hdrlen = sprintf((char*)hdr,"%s %lu", typename(type),
(unsigned long)dat->len) + 1;
SHA1_Init(&c);
SHA1_Update(&c, hdr, hdrlen);
SHA1_Update(&c, dat->buf, dat->len);
SHA1_Final(sha1, &c);
git_SHA1_Init(&c);
git_SHA1_Update(&c, hdr, hdrlen);
git_SHA1_Update(&c, dat->buf, dat->len);
git_SHA1_Final(sha1, &c);
if (sha1out)
hashcpy(sha1out, sha1);

Expand Down
10 changes: 5 additions & 5 deletions http-push.c
Expand Up @@ -126,7 +126,7 @@ struct transfer_request
char errorstr[CURL_ERROR_SIZE];
long http_code;
unsigned char real_sha1[20];
SHA_CTX c;
git_SHA_CTX c;
z_stream stream;
int zret;
int rename;
Expand Down Expand Up @@ -209,7 +209,7 @@ static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
request->stream.next_out = expn;
request->stream.avail_out = sizeof(expn);
request->zret = inflate(&request->stream, Z_SYNC_FLUSH);
SHA1_Update(&request->c, expn,
git_SHA1_Update(&request->c, expn,
sizeof(expn) - request->stream.avail_out);
} while (request->stream.avail_in && request->zret == Z_OK);
data_received++;
Expand Down Expand Up @@ -270,7 +270,7 @@ static void start_fetch_loose(struct transfer_request *request)

inflateInit(&request->stream);

SHA1_Init(&request->c);
git_SHA1_Init(&request->c);

url = xmalloc(strlen(remote->url) + 50);
request->url = xmalloc(strlen(remote->url) + 50);
Expand Down Expand Up @@ -310,7 +310,7 @@ static void start_fetch_loose(struct transfer_request *request)
if (prev_read == -1) {
memset(&request->stream, 0, sizeof(request->stream));
inflateInit(&request->stream);
SHA1_Init(&request->c);
git_SHA1_Init(&request->c);
if (prev_posn>0) {
prev_posn = 0;
lseek(request->local_fileno, 0, SEEK_SET);
Expand Down Expand Up @@ -742,7 +742,7 @@ static void finish_request(struct transfer_request *request)
fprintf(stderr, "Warning: requested range invalid; we may already have all the data.\n");

inflateEnd(&request->stream);
SHA1_Final(request->real_sha1, &request->c);
git_SHA1_Final(request->real_sha1, &request->c);
if (request->zret != Z_STREAM_END) {
unlink(request->tmpfile);
} else if (hashcmp(request->obj->sha1, request->real_sha1)) {
Expand Down
10 changes: 5 additions & 5 deletions http-walker.c
Expand Up @@ -36,7 +36,7 @@ struct object_request
char errorstr[CURL_ERROR_SIZE];
long http_code;
unsigned char real_sha1[20];
SHA_CTX c;
git_SHA_CTX c;
z_stream stream;
int zret;
int rename;
Expand Down Expand Up @@ -83,7 +83,7 @@ static size_t fwrite_sha1_file(void *ptr, size_t eltsize, size_t nmemb,
obj_req->stream.next_out = expn;
obj_req->stream.avail_out = sizeof(expn);
obj_req->zret = inflate(&obj_req->stream, Z_SYNC_FLUSH);
SHA1_Update(&obj_req->c, expn,
git_SHA1_Update(&obj_req->c, expn,
sizeof(expn) - obj_req->stream.avail_out);
} while (obj_req->stream.avail_in && obj_req->zret == Z_OK);
data_received++;
Expand Down Expand Up @@ -144,7 +144,7 @@ static void start_object_request(struct walker *walker,

inflateInit(&obj_req->stream);

SHA1_Init(&obj_req->c);
git_SHA1_Init(&obj_req->c);

url = xmalloc(strlen(obj_req->repo->base) + 51);
obj_req->url = xmalloc(strlen(obj_req->repo->base) + 51);
Expand Down Expand Up @@ -184,7 +184,7 @@ static void start_object_request(struct walker *walker,
if (prev_read == -1) {
memset(&obj_req->stream, 0, sizeof(obj_req->stream));
inflateInit(&obj_req->stream);
SHA1_Init(&obj_req->c);
git_SHA1_Init(&obj_req->c);
if (prev_posn>0) {
prev_posn = 0;
lseek(obj_req->local, 0, SEEK_SET);
Expand Down Expand Up @@ -244,7 +244,7 @@ static void finish_object_request(struct object_request *obj_req)
}

inflateEnd(&obj_req->stream);
SHA1_Final(obj_req->real_sha1, &obj_req->c);
git_SHA1_Final(obj_req->real_sha1, &obj_req->c);
if (obj_req->zret != Z_STREAM_END) {
unlink(obj_req->tmpfile);
return;
Expand Down

0 comments on commit 9126f00

Please sign in to comment.