"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Christian Weisgerber <naddy@mips.inka.de>
Subject:
Use POSIX endian functions
To:
gameoftrees@openbsd.org
Date:
Wed, 9 Sep 2020 22:36:18 +0200

Download raw body.

Thread
Use the POSIX standard endian functions and include <endian.h>

Let's use the POSIX beXXtoh() functions instead of the old betohXX()
ones and explicitly include <endian.h> where they are used.  The
htobeXX() ones already have the right name.
https://www.opengroup.org/austin/docs/austin_514.txt

OK?

diff 81a12da58651e79380d81dea7020bf6af20bb28b /home/naddy/got
blob - 86971081d57bb312c6571c4a5ae79e576a5643aa
file + lib/fetch.c
--- lib/fetch.c
+++ lib/fetch.c
@@ -24,6 +24,7 @@
 #include <sys/resource.h>
 #include <sys/socket.h>
 
+#include <endian.h>
 #include <errno.h>
 #include <err.h>
 #include <fcntl.h>
@@ -680,7 +681,7 @@ got_fetch_pack(struct got_object_id **pack_hash, struc
 			    "bad pack file version");
 			goto done;
 		}
-		nobj = betoh32(pack_hdr.nobjects);
+		nobj = be32toh(pack_hdr.nobjects);
 		if (nobj == 0 &&
 		    packfile_size > sizeof(pack_hdr) + SHA1_DIGEST_LENGTH)
 			return got_error_msg(GOT_ERR_BAD_PACKFILE,
blob - 6a48cf6389fbe45e5c317c0e02cd53a91867cd4c
file + lib/pack.c
--- lib/pack.c
+++ lib/pack.c
@@ -106,7 +106,7 @@ got_packidx_init_hdr(struct got_packidx *p, int verify
 			goto done;
 		}
 	}
-	if (betoh32(*h->magic) != GOT_PACKIDX_V2_MAGIC) {
+	if (be32toh(*h->magic) != GOT_PACKIDX_V2_MAGIC) {
 		err = got_error(GOT_ERR_BAD_PACKIDX);
 		goto done;
 	}
@@ -137,7 +137,7 @@ got_packidx_init_hdr(struct got_packidx *p, int verify
 			goto done;
 		}
 	}
-	if (betoh32(*h->version) != GOT_PACKIDX_VERSION) {
+	if (be32toh(*h->version) != GOT_PACKIDX_VERSION) {
 		err = got_error(GOT_ERR_BAD_PACKIDX);
 		goto done;
 	}
@@ -178,7 +178,7 @@ got_packidx_init_hdr(struct got_packidx *p, int verify
 	offset += len_fanout;
 	remain -= len_fanout;
 
-	nobj = betoh32(h->fanout_table[0xff]);
+	nobj = be32toh(h->fanout_table[0xff]);
 	len_ids = nobj * sizeof(*h->sorted_ids);
 	if (len_ids <= nobj || len_ids > remain) {
 		err = got_error(GOT_ERR_BAD_PACKIDX);
@@ -259,7 +259,7 @@ got_packidx_init_hdr(struct got_packidx *p, int verify
 
 	/* Large file offsets are contained only in files > 2GB. */
 	for (i = 0; i < nobj; i++) {
-		uint32_t o = betoh32(h->offsets[i]);
+		uint32_t o = be32toh(h->offsets[i]);
 		if (o & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX)
 			p->nlargeobj++;
 	}
@@ -417,14 +417,14 @@ got_packidx_close(struct got_packidx *packidx)
 static off_t
 get_object_offset(struct got_packidx *packidx, int idx)
 {
-	uint32_t offset = betoh32(packidx->hdr.offsets[idx]);
+	uint32_t offset = be32toh(packidx->hdr.offsets[idx]);
 	if (offset & GOT_PACKIDX_OFFSET_VAL_IS_LARGE_IDX) {
 		uint64_t loffset;
 		idx = offset & GOT_PACKIDX_OFFSET_VAL_MASK;
 		if (idx < 0 || idx >= packidx->nlargeobj ||
 		    packidx->hdr.large_offsets == NULL)
 			return -1;
-		loffset = betoh64(packidx->hdr.large_offsets[idx]);
+		loffset = be64toh(packidx->hdr.large_offsets[idx]);
 		return (loffset > INT64_MAX ? -1 : (off_t)loffset);
 	}
 	return (off_t)(offset & GOT_PACKIDX_OFFSET_VAL_MASK);
@@ -434,11 +434,11 @@ int
 got_packidx_get_object_idx(struct got_packidx *packidx, struct got_object_id *id)
 {
 	u_int8_t id0 = id->sha1[0];
-	uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
+	uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
 	int left = 0, right = totobj - 1;
 
 	if (id0 > 0)
-		left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
+		left = be32toh(packidx->hdr.fanout_table[id0 - 1]);
 
 	while (left <= right) {
 		struct got_packidx_object_id *oid;
@@ -464,7 +464,7 @@ got_packidx_match_id_str_prefix(struct got_object_id_q
 {
 	const struct got_error *err = NULL;
 	u_int8_t id0;
-	uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
+	uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
 	char hex[3];
 	size_t prefix_len = strlen(id_str_prefix);
 	struct got_packidx_object_id *oid;
@@ -481,7 +481,7 @@ got_packidx_match_id_str_prefix(struct got_object_id_q
 	if (!got_parse_xdigit(&id0, hex))
 		return got_error_path(id_str_prefix, GOT_ERR_BAD_OBJ_ID_STR);
 
-	i = betoh32(packidx->hdr.fanout_table[id0 - 1]);
+	i = be32toh(packidx->hdr.fanout_table[id0 - 1]);
 	if (i == 0)
 		return NULL;
 
blob - 8860e1831b9a9021198957db53b2d3632a242df4
file + lib/repository.c
--- lib/repository.c
+++ lib/repository.c
@@ -23,6 +23,7 @@
 #include <sys/syslimits.h>
 
 #include <ctype.h>
+#include <endian.h>
 #include <fcntl.h>
 #include <fnmatch.h>
 #include <limits.h>
@@ -912,7 +913,7 @@ static const struct got_error *
 read_packfile_hdr(int fd, struct got_packidx *packidx)
 {
 	const struct got_error *err = NULL;
-	uint32_t totobj = betoh32(packidx->hdr.fanout_table[0xff]);
+	uint32_t totobj = be32toh(packidx->hdr.fanout_table[0xff]);
 	struct got_packfile_hdr hdr;
 	ssize_t n;
 
@@ -922,9 +923,9 @@ read_packfile_hdr(int fd, struct got_packidx *packidx)
 	if (n != sizeof(hdr))
 		return got_error(GOT_ERR_BAD_PACKFILE);
 
-	if (betoh32(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
-	    betoh32(hdr.version) != GOT_PACKFILE_VERSION ||
-	    betoh32(hdr.nobjects) != totobj)
+	if (be32toh(hdr.signature) != GOT_PACKFILE_SIGNATURE ||
+	    be32toh(hdr.version) != GOT_PACKFILE_VERSION ||
+	    be32toh(hdr.nobjects) != totobj)
 		err = got_error(GOT_ERR_BAD_PACKFILE);
 
 	return err;
blob - 10955c887d40ead647e9bdb923b82fcc3bce37d4
file + libexec/got-index-pack/got-index-pack.c
--- libexec/got-index-pack/got-index-pack.c
+++ libexec/got-index-pack/got-index-pack.c
@@ -33,6 +33,7 @@
 #include <string.h>
 #include <ctype.h>
 #include <sha1.h>
+#include <endian.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <zlib.h>
@@ -447,12 +448,12 @@ static int
 find_object_idx(struct got_packidx *packidx, uint8_t *sha1)
 {
 	u_int8_t id0 = sha1[0];
-	uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
+	uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
 	int left = 0, right = nindexed - 1;
 	int cmp = 0, i = 0;
 
 	if (id0 > 0)
-		left = betoh32(packidx->hdr.fanout_table[id0 - 1]);
+		left = be32toh(packidx->hdr.fanout_table[id0 - 1]);
 
 	while (left <= right) {
 		struct got_packidx_object_id *oid;
@@ -476,7 +477,7 @@ find_object_idx(struct got_packidx *packidx, uint8_t *
 static void
 print_packidx(struct got_packidx *packidx)
 {
-	uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
+	uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
 	int i;
 
 	fprintf(stderr, "object IDs:\n");
@@ -569,7 +570,7 @@ update_packidx(struct got_packidx *packidx, int nobj,
     struct got_indexed_object *obj)
 {
 	uint32_t idx;
-	uint32_t nindexed = betoh32(packidx->hdr.fanout_table[0xff]);
+	uint32_t nindexed = be32toh(packidx->hdr.fanout_table[0xff]);
 
 	idx = find_object_idx(packidx, obj->id.sha1);
 	if (idx == -1) {
@@ -657,7 +658,7 @@ index_pack(struct got_pack *pack, int idxfd, FILE *tmp
 	if (hdr.version != htobe32(GOT_PACKFILE_VERSION))
 		return got_error_msg(GOT_ERR_BAD_PACKFILE,
 		    "bad packfile version");
-	nobj = betoh32(hdr.nobjects);
+	nobj = be32toh(hdr.nobjects);
 	if (nobj == 0)
 		return got_error_msg(GOT_ERR_BAD_PACKFILE,
 		    "bad packfile with zero objects");
-- 
Christian "naddy" Weisgerber                          naddy@mips.inka.de