Nicer function names for endian encode/decode

This commit is contained in:
Kovid Goyal
2023-07-19 12:45:56 +05:30
parent b03fa08884
commit 7a9c425c66
2 changed files with 100 additions and 52 deletions

View File

@@ -230,11 +230,11 @@ signature_header(Patcher *self, PyObject *a2) {
PyErr_SetString(RsyncError, "Output buffer is too small");
}
uint8_t *o = dest.buf;
le16b(o, 0); // version
le16b(o + 2, 0); // checksum type
le16b(o + 4, 0); // strong hash type
le16b(o + 6, 0); // weak hash type
le32b(o + 8, self->rsync.block_size); // block size
le16enc(o, 0); // version
le16enc(o + 2, 0); // checksum type
le16enc(o + 4, 0); // strong hash type
le16enc(o + 6, 0); // weak hash type
le32enc(o + 8, self->rsync.block_size); // block size
return PyLong_FromSsize_t(header_size);
}
@@ -251,12 +251,12 @@ sign_block(Patcher *self, PyObject *args) {
}
self->rsync.hasher.reset(self->rsync.hasher.state);
if (!self->rsync.hasher.update(self->rsync.hasher.state, src.buf, src.len)) { PyErr_SetString(PyExc_ValueError, "String hashing failed"); return NULL; }
uint64_t strong_hash = self->rsync.hasher.digest64(self->rsync.hasher.state);
uint64_t strong_hash = self->rsync.hasher.oneshot64(src.buf, src.len);
uint32_t weak_hash = rolling_checksum_full(&self->rc, src.buf, src.len);
uint8_t *o = dest.buf;
le64b(o, self->signature_idx++);
le32b(o + 8, weak_hash);
le64b(o + 12, strong_hash);
le64enc(o, self->signature_idx++);
le32enc(o + 8, weak_hash);
le64enc(o + 12, strong_hash);
return PyLong_FromSize_t(signature_block_size);
}
@@ -275,18 +275,18 @@ unserialize_op(uint8_t *data, size_t len, Operation *op) {
case OpBlock:
consumed = 9;
if (len < consumed) return 0;
op->block_index = le64(data + 1);
op->block_index = le64dec(data + 1);
break;
case OpBlockRange:
consumed = 13;
if (len < consumed) return 0;
op->block_index = le64(data + 1);
op->block_index_end = op->block_index + le32(data + 9);
op->block_index = le64dec(data + 1);
op->block_index_end = op->block_index + le32dec(data + 9);
break;
case OpHash:
consumed = 3;
if (len < consumed) return 0;
op->data.len = le16(data + 1);
op->data.len = le16dec(data + 1);
if (len < consumed + op->data.len) return 0;
op->data.buf = data + 3;
consumed += op->data.len;
@@ -294,7 +294,7 @@ unserialize_op(uint8_t *data, size_t len, Operation *op) {
case OpData:
consumed = 5;
if (len < consumed) return 0;
op->data.len = le32(data + 1);
op->data.len = le32dec(data + 1);
if (len < consumed + op->data.len) return 0;
op->data.buf = data + 5;
consumed += op->data.len;
@@ -468,19 +468,19 @@ parse_signature_header(Differ *self) {
if (self->buf.len < 12) return;
uint8_t *p = self->buf.data;
uint32_t x;
if ((x = le16(p)) != 0) {
if ((x = le16dec(p)) != 0) {
PyErr_Format(RsyncError, "Invalid version in signature header: %u", x); return;
} p += 2;
if ((x = le16(p)) != 0) {
if ((x = le16dec(p)) != 0) {
PyErr_Format(RsyncError, "Invalid checksum type in signature header: %u", x); return;
} p += 2;
if ((x = le16(p)) != 0) {
if ((x = le16dec(p)) != 0) {
PyErr_Format(RsyncError, "Invalid strong hash type in signature header: %u", x); return;
} p += 2;
if ((x = le16(p)) != 0) {
if ((x = le16dec(p)) != 0) {
PyErr_Format(RsyncError, "Invalid weak hash type in signature header: %u", x); return;
} p += 2;
const char *err = init_rsync(&self->rsync, le32(p), 0, 0);
const char *err = init_rsync(&self->rsync, le32dec(p), 0, 0);
if (err != NULL) { PyErr_SetString(RsyncError, err); return; }
p += 4;
shift_left(&self->buf, p - self->buf.data);
@@ -502,18 +502,18 @@ add_collision(SignatureMap *sm, Signature s) {
static size_t
parse_signature_block(Differ *self, uint8_t *data, size_t len) {
if (len < 20) return 0;
int weak_hash = le32(data + 8);
int weak_hash = le32dec(data + 8);
SignatureMap *sm;
HASH_FIND_INT(self->signature_map, &weak_hash, sm);
if (sm == NULL) {
sm = calloc(1, sizeof(SignatureMap));
if (sm == NULL) { PyErr_NoMemory(); return 0; }
sm->weak_hash = weak_hash;
sm->sig.index = le64(data);
sm->sig.strong_hash = le64(data+12);
sm->sig.index = le64dec(data);
sm->sig.strong_hash = le64dec(data+12);
HASH_ADD_INT(self->signature_map, weak_hash, sm);
} else {
if (!add_collision(sm, (Signature){.index=le64(data), .strong_hash=le64(data+12)})) return 0;
if (!add_collision(sm, (Signature){.index=le64dec(data), .strong_hash=le64dec(data+12)})) return 0;
}
return 20;
}
@@ -556,21 +556,21 @@ send_op(Differ *self, Operation *op) {
metadata[0] = op->type;
switch (op->type) {
case OpBlock:
le64b(metadata + 1, op->block_index);
le64enc(metadata + 1, op->block_index);
len = 9;
break;
case OpBlockRange:
le64b(metadata + 1, op->block_index);
le32b(metadata + 9, op->block_index_end - op->block_index);
le64enc(metadata + 1, op->block_index);
le32enc(metadata + 9, op->block_index_end - op->block_index);
len = 13;
break;
case OpHash:
le16b(metadata + 1, op->data.len);
le16enc(metadata + 1, op->data.len);
memcpy(metadata + 3, op->data.buf, op->data.len);
len = 3 + op->data.len;
break;
case OpData:
le32b(metadata + 1, op->data.len);
le32enc(metadata + 1, op->data.len);
len = 5;
break;
}

View File

@@ -8,38 +8,86 @@
#include <stdint.h>
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
inline static uint16_t le16(const uint8_t b[sizeof(uint16_t)]) { return *((uint16_t*)b); }
inline static uint16_t le32(const uint8_t b[sizeof(uint32_t)]) { return *((uint32_t*)b); }
inline static uint16_t le64(const uint8_t b[sizeof(uint64_t)]) { return *((uint64_t*)b); }
inline static void le16b(uint8_t b[sizeof(uint16_t)], const uint16_t n) { *((uint16_t*)b) = n; }
inline static void le32b(uint8_t b[sizeof(uint32_t)], const uint32_t n) { *((uint32_t*)b) = n; }
inline static void le64b(uint8_t b[sizeof(uint64_t)], const uint64_t n) { *((uint64_t*)b) = n; }
#else
inline static uint16_t le16(const uint8_t b[sizeof(uint16_t)]) {
return b[0]|(uint16_t)b[1]<<8;
static inline uint16_t
be16dec(const void *pp) {
uint8_t const *p = (uint8_t const *)pp;
return (((unsigned)p[0] << 8) | p[1]);
}
inline static uint32_t le32(const uint8_t b[sizeof(uint32_t)]) {
return le16(b)|(uint32_t)le16(b+2)<<16;
static inline uint32_t
be32dec(const void *pp) {
uint8_t const *p = (uint8_t const *)pp;
return (((uint32_t)p[0] << 24) | ((uint32_t)p[1] << 16) |
((uint32_t)p[2] << 8) | p[3]);
}
inline static uint64_t le64(const uint8_t b[sizeof(uint64_t)]) {
return le32(b)|(uint64_t)le32(b+4)<<32;
static inline uint64_t
be64dec(const void *pp) {
uint8_t const *p = (uint8_t const *)pp;
return (((uint64_t)be32dec(p) << 32) | be32dec(p + 4));
}
inline static void le16b(uint8_t b[sizeof(uint16_t)], const uint16_t n) {
b[0] = n;
b[1] = n>>8;
static inline uint16_t
le16dec(const void *pp) {
uint8_t const *p = (uint8_t const *)pp;
return (((unsigned)p[1] << 8) | p[0]);
}
inline static void le32b(uint8_t b[sizeof(uint32_t)], const uint32_t n) {
le16b(b, n);
le16b(b+2, n>>16);
static inline uint32_t
le32dec(const void *pp) {
uint8_t const *p = (uint8_t const *)pp;
return (((uint32_t)p[3] << 24) | ((uint32_t)p[2] << 16) |
((uint32_t)p[1] << 8) | p[0]);
}
inline static void le64b(uint8_t b[sizeof(uint64_t)], const uint64_t n) {
le32b(b, n);
le32b(b+4, n>>32);
static inline uint64_t
le64dec(const void *pp) {
uint8_t const *p = (uint8_t const *)pp;
return (((uint64_t)le32dec(p + 4) << 32) | le32dec(p));
}
static inline void
be16enc(void *pp, uint16_t u) {
uint8_t *p = (uint8_t *)pp;
p[0] = (u >> 8) & 0xff;
p[1] = u & 0xff;
}
static inline void
be32enc(void *pp, uint32_t u) {
uint8_t *p = (uint8_t *)pp;
p[0] = (u >> 24) & 0xff;
p[1] = (u >> 16) & 0xff;
p[2] = (u >> 8) & 0xff;
p[3] = u & 0xff;
}
static inline void
be64enc(void *pp, uint64_t u) {
uint8_t *p = (uint8_t *)pp;
be32enc(p, (uint32_t)(u >> 32));
be32enc(p + 4, (uint32_t)(u & 0xffffffffU));
}
static inline void
le16enc(void *pp, uint16_t u) {
uint8_t *p = (uint8_t *)pp;
p[0] = u & 0xff;
p[1] = (u >> 8) & 0xff;
}
static inline void
le32enc(void *pp, uint32_t u) {
uint8_t *p = (uint8_t *)pp;
p[0] = u & 0xff;
p[1] = (u >> 8) & 0xff;
p[2] = (u >> 16) & 0xff;
p[3] = (u >> 24) & 0xff;
}
static inline void
le64enc(void *pp, uint64_t u) {
uint8_t *p = (uint8_t *)pp;
le32enc(p, (uint32_t)(u & 0xffffffffU));
le32enc(p + 4, (uint32_t)(u >> 32));
}
#endif