commit 3fe2daf1bfa78990045d9613e9b797437ac7f361 from: Stefan Sperling date: Mon Dec 24 16:03:21 2018 UTC remove pointless memcopies from fileindex code commit - 7426bbfd0325eb69b703734636662a9968c36888 commit + 3fe2daf1bfa78990045d9613e9b797437ac7f361 blob - e860a4dace549dfb77ad350dc3fce155b64dfdc1 blob + 86250c11fe57924c5ca80e01cac4cd1188d8e075 --- lib/fileindex.c +++ lib/fileindex.c @@ -116,13 +116,11 @@ got_fileindex_free(struct got_fileindex *fileindex) static const struct got_error * write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FILE *outfile) { - uint8_t buf[sizeof(uint64_t)]; size_t n; val = htobe64(val); - memcpy(buf, &val, sizeof(val)); - SHA1Update(ctx, buf, sizeof(val)); - n = fwrite(buf, 1, sizeof(val), outfile); + SHA1Update(ctx, (uint8_t *)&val, sizeof(val)); + n = fwrite(&val, 1, sizeof(val), outfile); if (n != sizeof(val)) return got_ferror(outfile, GOT_ERR_IO); return NULL; @@ -131,13 +129,11 @@ write_fileindex_val64(SHA1_CTX *ctx, uint64_t val, FIL static const struct got_error * write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FILE *outfile) { - uint8_t buf[sizeof(uint32_t)]; size_t n; val = htobe32(val); - memcpy(buf, &val, sizeof(val)); - SHA1Update(ctx, buf, sizeof(val)); - n = fwrite(buf, 1, sizeof(val), outfile); + SHA1Update(ctx, (uint8_t *)&val, sizeof(val)); + n = fwrite(&val, 1, sizeof(val), outfile); if (n != sizeof(val)) return got_ferror(outfile, GOT_ERR_IO); return NULL; @@ -146,13 +142,11 @@ write_fileindex_val32(SHA1_CTX *ctx, uint32_t val, FIL static const struct got_error * write_fileindex_val16(SHA1_CTX *ctx, uint16_t val, FILE *outfile) { - uint8_t buf[sizeof(uint16_t)]; size_t n; val = htobe16(val); - memcpy(buf, &val, sizeof(val)); - SHA1Update(ctx, buf, sizeof(val)); - n = fwrite(buf, 1, sizeof(val), outfile); + SHA1Update(ctx, (uint8_t *)&val, sizeof(val)); + n = fwrite(&val, 1, sizeof(val), outfile); if (n != sizeof(val)) return got_ferror(outfile, GOT_ERR_IO); return NULL; @@ -269,14 +263,12 @@ got_fileindex_write(struct got_fileindex *fileindex, F static const struct got_error * read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FILE *infile) { - uint8_t buf[sizeof(uint64_t)]; size_t n; - n = fread(buf, 1, sizeof(buf), infile); - if (n != sizeof(buf)) + n = fread(val, 1, sizeof(*val), infile); + if (n != sizeof(*val)) return got_ferror(infile, GOT_ERR_IO); - SHA1Update(ctx, buf, sizeof(buf)); - memcpy(val, buf, sizeof(*val)); + SHA1Update(ctx, (uint8_t *)val, sizeof(*val)); *val = be64toh(*val); return NULL; } @@ -284,14 +276,12 @@ read_fileindex_val64(uint64_t *val, SHA1_CTX *ctx, FIL static const struct got_error * read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FILE *infile) { - uint8_t buf[sizeof(uint32_t)]; size_t n; - n = fread(buf, 1, sizeof(buf), infile); - if (n != sizeof(buf)) + n = fread(val, 1, sizeof(*val), infile); + if (n != sizeof(*val)) return got_ferror(infile, GOT_ERR_IO); - SHA1Update(ctx, buf, sizeof(buf)); - memcpy(val, buf, sizeof(*val)); + SHA1Update(ctx, (uint8_t *)val, sizeof(*val)); *val = be32toh(*val); return NULL; } @@ -299,14 +289,12 @@ read_fileindex_val32(uint32_t *val, SHA1_CTX *ctx, FIL static const struct got_error * read_fileindex_val16(uint16_t *val, SHA1_CTX *ctx, FILE *infile) { - uint8_t buf[sizeof(uint16_t)]; size_t n; - n = fread(buf, 1, sizeof(buf), infile); - if (n != sizeof(buf)) + n = fread(val, 1, sizeof(*val), infile); + if (n != sizeof(*val)) return got_ferror(infile, GOT_ERR_IO); - SHA1Update(ctx, buf, sizeof(buf)); - memcpy(val, buf, sizeof(*val)); + SHA1Update(ctx, (uint8_t *)val, sizeof(*val)); *val = be16toh(*val); return NULL; }