use the POSIX standard endian functions and explicitly include <endian.h> ok stsp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
diff --git a/lib/fetch.c b/lib/fetch.c
index 8697108..057287f 100644
--- a/lib/fetch.c
+++ b/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, struct got_pathlist_head *refs,
"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,
diff --git a/lib/pack.c b/lib/pack.c
index 6a48cf6..ad61697 100644
--- a/lib/pack.c
+++ b/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_queue *matched_ids,
{
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_queue *matched_ids,
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;
diff --git a/lib/repository.c b/lib/repository.c
index 8860e18..c2ee66c 100644
--- a/lib/repository.c
+++ b/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;
diff --git a/libexec/got-index-pack/got-index-pack.c b/libexec/got-index-pack/got-index-pack.c
index 10955c8..60075c0 100644
--- a/libexec/got-index-pack/got-index-pack.c
+++ b/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 *sha1)
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 *tmpfile,
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");