#include "structures/buffer.h" // Inline extern prototypes bool LSUP_buffer_eq(const LSUP_Buffer *buf1, const LSUP_Buffer *buf2); LSUP_Buffer *LSUP_buffer_new(size_t size) { LSUP_Buffer *buf; CRITICAL(buf = malloc(sizeof(LSUP_Buffer))); LSUP_buffer_init(buf, size); return buf; } int LSUP_buffer_init(LSUP_Buffer *buf, size_t size) { TRACE("Buffer Size: %lu\n", size); CRITICAL(buf->addr = malloc(size * sizeof(char))); buf->size = size; return 0; } /** * Print a byte string of a given length in a human-readable format. * * The string is printed in Python style: printable characters are output * literally, and non-printable ones as hex sequences. */ void LSUP_buffer_print(const LSUP_Buffer *buf) { for (size_t i = 0; i < buf->size; i++) { char chr = ((char*)buf->addr)[i]; if (isprint(chr)) { fputc(chr, stdout); } else { printf("\\x%02x", chr); } } printf("\n"); } void LSUP_buffer_done(LSUP_Buffer *buf){ if (buf->addr != NULL) { TRACE(STR, "Freeing buffer."); free(buf->addr); } }