diff options
Diffstat (limited to 'src/common/packed.c')
-rw-r--r-- | src/common/packed.c | 106 |
1 files changed, 80 insertions, 26 deletions
diff --git a/src/common/packed.c b/src/common/packed.c index b8638b3..117b557 100644 --- a/src/common/packed.c +++ b/src/common/packed.c @@ -51,7 +51,7 @@ * * ******************************************************************************/ -void init_packed_buffer(packed_buffer *pbuf) +void init_packed_buffer(packed_buffer_t *pbuf) { pbuf->allocated = PACKET_BLOCK_SIZE; pbuf->data = malloc(pbuf->allocated * sizeof(uint8_t)); @@ -65,6 +65,27 @@ void init_packed_buffer(packed_buffer *pbuf) * * * Paramètres : pbuf = paquet de données à réinitialiser. [OUT] * * * +* Description : Rembobine le paquet de données à son départ. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +void rewind_packed_buffer(packed_buffer_t *pbuf) +{ + pbuf->pos = sizeof(uint32_t); + + assert(pbuf->pos <= pbuf->allocated); + +} + + +/****************************************************************************** +* * +* Paramètres : pbuf = paquet de données à réinitialiser. [OUT] * +* * * Description : Réinitialise un paquet réseau pour une constitution. * * * * Retour : - * @@ -73,10 +94,11 @@ void init_packed_buffer(packed_buffer *pbuf) * * ******************************************************************************/ -void reset_packed_buffer(packed_buffer *pbuf) +void reset_packed_buffer(packed_buffer_t *pbuf) { pbuf->used = 0; - pbuf->pos = sizeof(uint32_t); + + rewind_packed_buffer(pbuf); assert(pbuf->pos <= pbuf->allocated); @@ -95,17 +117,49 @@ void reset_packed_buffer(packed_buffer *pbuf) * * ******************************************************************************/ -void exit_packed_buffer(packed_buffer *pbuf) +void exit_packed_buffer(packed_buffer_t *pbuf) { #ifndef NDEBUG assert(pbuf->data != NULL); #endif - free(pbuf->data); + if (pbuf->data) + { + free(pbuf->data); + pbuf->data = NULL; + } + +} + -#ifndef NDEBUG - pbuf->data = NULL; -#endif +/****************************************************************************** +* * +* Paramètres : dest = tampon de données à constituer. * +* src = tampon de données à copier. * +* * +* Description : Copie les données d'un tampon dans un autre. * +* * +* Retour : - * +* * +* Remarques : - * +* * +******************************************************************************/ + +void copy_packed_buffer(packed_buffer_t *dest, const packed_buffer_t *src) +{ + size_t len; /* Taille des données à copier */ + + exit_packed_buffer(dest); + + len = dest->allocated * sizeof(uint8_t); + + dest->allocated = src->allocated; + dest->data = malloc(len); + + memcpy(dest->data, src->data, len); + + dest->used = src->used; + dest->pos = src->pos; } @@ -122,7 +176,7 @@ void exit_packed_buffer(packed_buffer *pbuf) * * ******************************************************************************/ -size_t get_packed_buffer_payload_length(const packed_buffer *pbuf) +size_t get_packed_buffer_payload_length(const packed_buffer_t *pbuf) { size_t result; /* Quantité à renvoyer */ @@ -145,7 +199,7 @@ size_t get_packed_buffer_payload_length(const packed_buffer *pbuf) * * ******************************************************************************/ -bool has_more_data_in_packed_buffer(const packed_buffer *pbuf) +bool has_more_data_in_packed_buffer(const packed_buffer_t *pbuf) { bool result; /* Bilan à retourner */ @@ -159,7 +213,7 @@ bool has_more_data_in_packed_buffer(const packed_buffer *pbuf) /****************************************************************************** * * * Paramètres : pbuf = paquet de données à compléter. * -* buf = nouvelles données à ajouter. * +* data = nouvelles données à ajouter. * * len = quantité de ces données. * * hton = indique si une conversion est à réaliser. * * * @@ -171,7 +225,7 @@ bool has_more_data_in_packed_buffer(const packed_buffer *pbuf) * * ******************************************************************************/ -bool extend_packed_buffer(packed_buffer *pbuf, const void *buf, size_t len, bool hton) +bool extend_packed_buffer(packed_buffer_t *pbuf, const void *data, size_t len, bool hton) { uint16_t tmp16; /* Valeur intermédiaire 16b */ uint32_t tmp32; /* Valeur intermédiaire 32b */ @@ -193,21 +247,21 @@ bool extend_packed_buffer(packed_buffer *pbuf, const void *buf, size_t len, bool switch (len) { case 1: - *((uint8_t *)(pbuf->data + pbuf->pos)) = *((uint8_t *)buf); + *((uint8_t *)(pbuf->data + pbuf->pos)) = *((uint8_t *)data); break; case 2: - tmp16 = htobe16(*(uint16_t *)buf); + tmp16 = htobe16(*(uint16_t *)data); *((uint16_t *)(pbuf->data + pbuf->pos)) = tmp16; break; case 4: - tmp32 = htobe32(*(uint32_t *)buf); + tmp32 = htobe32(*(uint32_t *)data); *((uint32_t *)(pbuf->data + pbuf->pos)) = tmp32; break; case 8: - tmp64 = htobe64(*(uint64_t *)buf); + tmp64 = htobe64(*(uint64_t *)data); *((uint64_t *)(pbuf->data + pbuf->pos)) = tmp64; break; @@ -221,7 +275,7 @@ bool extend_packed_buffer(packed_buffer *pbuf, const void *buf, size_t len, bool */ assert(!hton); - memcpy(pbuf->data + pbuf->pos, buf, len); + memcpy(pbuf->data + pbuf->pos, data, len); break; } @@ -249,7 +303,7 @@ bool extend_packed_buffer(packed_buffer *pbuf, const void *buf, size_t len, bool * * ******************************************************************************/ -bool peek_packed_buffer(packed_buffer *pbuf, void *buf, size_t len, bool ntoh) +bool peek_packed_buffer(packed_buffer_t *pbuf, void *buf, size_t len, bool ntoh) { bool result; /* Bilan à retourner */ uint16_t tmp16; /* Valeur intermédiaire 16b */ @@ -322,7 +376,7 @@ bool peek_packed_buffer(packed_buffer *pbuf, void *buf, size_t len, bool ntoh) * * ******************************************************************************/ -void advance_packed_buffer(packed_buffer *pbuf, size_t len) +void advance_packed_buffer(packed_buffer_t *pbuf, size_t len) { pbuf->pos += len; @@ -346,7 +400,7 @@ void advance_packed_buffer(packed_buffer *pbuf, size_t len) * * ******************************************************************************/ -bool extract_packed_buffer(packed_buffer *pbuf, void *buf, size_t len, bool ntoh) +bool extract_packed_buffer(packed_buffer_t *pbuf, void *buf, size_t len, bool ntoh) { bool result; /* Bilan à retourner */ @@ -373,7 +427,7 @@ bool extract_packed_buffer(packed_buffer *pbuf, void *buf, size_t len, bool ntoh * * ******************************************************************************/ -bool read_packed_buffer(packed_buffer *pbuf, int fd) +bool read_packed_buffer(packed_buffer_t *pbuf, int fd) { bool result; /* Bilan à retourner */ uint32_t used; /* Taille de charge utile */ @@ -414,7 +468,7 @@ bool read_packed_buffer(packed_buffer *pbuf, int fd) * * ******************************************************************************/ -bool write_packed_buffer(packed_buffer *pbuf, int fd) +bool write_packed_buffer(packed_buffer_t *pbuf, int fd) { bool result; /* Bilan à retourner */ @@ -440,7 +494,7 @@ bool write_packed_buffer(packed_buffer *pbuf, int fd) * * ******************************************************************************/ -bool recv_packed_buffer(packed_buffer *pbuf, int fd) +bool recv_packed_buffer(packed_buffer_t *pbuf, int fd) { bool result; /* Bilan à retourner */ uint32_t used; /* Taille de charge utile */ @@ -481,7 +535,7 @@ bool recv_packed_buffer(packed_buffer *pbuf, int fd) * * ******************************************************************************/ -bool send_packed_buffer(packed_buffer *pbuf, int fd) +bool send_packed_buffer(packed_buffer_t *pbuf, int fd) { bool result; /* Bilan à retourner */ @@ -507,7 +561,7 @@ bool send_packed_buffer(packed_buffer *pbuf, int fd) * * ******************************************************************************/ -bool ssl_recv_packed_buffer(packed_buffer *pbuf, SSL *fd) +bool ssl_recv_packed_buffer(packed_buffer_t *pbuf, SSL *fd) { bool result; /* Bilan à retourner */ uint32_t used; /* Taille de charge utile */ @@ -555,7 +609,7 @@ bool ssl_recv_packed_buffer(packed_buffer *pbuf, SSL *fd) * * ******************************************************************************/ -bool ssl_send_packed_buffer(packed_buffer *pbuf, SSL *fd) +bool ssl_send_packed_buffer(packed_buffer_t *pbuf, SSL *fd) { bool result; /* Bilan à retourner */ int quantity; /* Nombre de données à traiter */ |