X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/mLib/blobdiff_plain/800d4c590fc75ba21ae7b742fe11472cdc71d4a1..8f45ff6cb9a269fc6f8f631908f93b4c8123419c:/buf.h diff --git a/buf.h b/buf.h index 84c4121..8eb931a 100644 --- a/buf.h +++ b/buf.h @@ -7,28 +7,28 @@ * (c) 2001 Straylight/Edgeware */ -/*----- Licensing notice --------------------------------------------------* +/*----- Licensing notice --------------------------------------------------* * - * This file is part of Catacomb. + * This file is part of the mLib utilities library. * - * Catacomb is free software; you can redistribute it and/or modify + * mLib is free software; you can redistribute it and/or modify * it under the terms of the GNU Library General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. - * - * Catacomb is distributed in the hope that it will be useful, + * + * mLib is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. - * + * * You should have received a copy of the GNU Library General Public - * License along with Catacomb; if not, write to the Free + * License along with mLib; if not, write to the Free * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, * MA 02111-1307, USA. */ -#ifndef CATACOMB_BUF_H -#define CATACOMB_BUF_H +#ifndef MLIB_BUF_H +#define MLIB_BUF_H #ifdef __cplusplus extern "C" { @@ -38,14 +38,12 @@ #include -#include - -#ifndef CATACOMB_MP_H -# include "mp.h" +#ifndef MLIB_BITS_H +# include "bits.h" #endif -#ifndef CATACOMB_EC_H -# include "ec.h" +#ifndef MLIB_DSTR_H +# include "dstr.h" #endif /*----- Data structures ---------------------------------------------------*/ @@ -78,6 +76,8 @@ typedef struct buf { #define BENSURE(b, sz) \ (BBAD(b) ? -1 : (sz) > BLEFT(b) ? (b)->f |= BF_BROKEN, -1 : 0) +#define BUF_DOSUFFIXES(_) DOUINTCONV(_) _(z, z, z) + /*----- Functions provided ------------------------------------------------*/ /* --- @buf_init@ --- * @@ -177,71 +177,51 @@ extern int buf_getbyte(buf */*b*/); extern int buf_putbyte(buf */*b*/, int /*ch*/); -/* --- @buf_getu16@ --- * +/* --- @buf_getu{8,{16,24,32,64}{,l,b}}@ --- * * * Arguments: @buf *b@ = pointer to a buffer block - * @uint16 *w@ = where to put the word + * @uintSZ *w@ = where to put the word * * Returns: Zero if OK, or nonzero if there wasn't a word there. * - * Use: Gets a 16-bit word from a buffer. + * Use: Gets a word of appropriate size and order from a buffer. */ -extern int buf_getu16(buf */*b*/, uint16 */*w*/); +#define BUF_DECL_GETU_(n, W, w) \ + extern int buf_getu##w(buf */*b*/, uint##n */*w*/); +DOUINTCONV(BUF_DECL_GETU_) -/* --- @buf_putu16@ --- * +/* --- @buf_putu{8,{16,24,32,64}{,l,b}}@ --- * * * Arguments: @buf *b@ = pointer to a buffer block - * @uint16 w@ = word to write + * @uintSZ w@ = word to write * - * Returns: Zero if OK, nonzero if there wasn't enough space. + * Returns: Zero if OK, or nonzero if there wasn't enough space * - * Use: Puts a 16-but word in a buffer. + * Use: Puts a word into a buffer with appropriate size and order. */ -extern int buf_putu16(buf */*b*/, uint16 /*w*/); +#define BUF_DECL_PUTU_(n, W, w) \ + extern int buf_putu##w(buf */*b*/, uint##n /*w*/); +DOUINTCONV(BUF_DECL_PUTU_) -/* --- @buf_getu32@ --- * - * - * Arguments: @buf *b@ = pointer to a buffer block - * @uint32 *w@ = where to put the word - * - * Returns: Zero if OK, or nonzero if there wasn't a word there. - * - * Use: Gets a 32-bit word from a buffer. - */ - -extern int buf_getu32(buf */*b*/, uint32 */*w*/); - -/* --- @buf_putu32@ --- * - * - * Arguments: @buf *b@ = pointer to a buffer block - * @uint32 w@ = word to write - * - * Returns: Zero if OK, nonzero if there wasn't enough space. - * - * Use: Puts a 32-but word in a buffer. - */ - -extern int buf_putu32(buf */*b*/, uint32 /*w*/); - -/* --- @buf_getmem{8,16,32,z} --- * +/* --- @buf_getmem{8,{16,24,32,64}{,l,b},z} --- * * * Arguments: @buf *b@ = pointer to a buffer block * @size_t *nn@ = where to put the length * * Returns: Pointer to the buffer data, or null. * - * Use: Gets a chunk of memory from a buffer. The number, @16@ or - * @32@, is the width of the length; @z@ means null-terminated. + * Use: Gets a chunk of memory from a buffer. The suffix is the + * width and byte order of the length; @z@ means null- + * terminated. */ -extern void *buf_getmem8(buf */*b*/, size_t */*nn*/); -extern void *buf_getmem16(buf */*b*/, size_t */*nn*/); -extern void *buf_getmem32(buf */*b*/, size_t */*nn*/); -extern void *buf_getmemz(buf */*b*/, size_t */*nn*/); +#define BUF_DECL_GETMEM_(n, W, w) \ + extern void *buf_getmem##w(buf */*b*/, size_t */*nn*/); +BUF_DOSUFFIXES(BUF_DECL_GETMEM_) -/* --- @buf_putmem{8,16,32,z} --- * +/* --- @buf_putmem{8,{16,24,32,64}{,l,b},z} --- * * * Arguments: @buf *b@ = pointer to a buffer block * @const void *p@ = pointer to data to write @@ -249,16 +229,16 @@ extern void *buf_getmemz(buf */*b*/, size_t */*nn*/); * * Returns: Zero if OK, nonzero if there wasn't enough space. * - * Use: Writes a chunk of data to a buffer. The number, @16@ or - * @32@, is the width of the length; @z@ means null-terminated. + * Use: Writes a chunk of data to a buffer. The suffix is the + * width and byte order of the length; @z@ means null- + * terminated. */ -extern int buf_putmem8(buf */*b*/, const void */*p*/, size_t /*n*/); -extern int buf_putmem16(buf */*b*/, const void */*p*/, size_t /*n*/); -extern int buf_putmem32(buf */*b*/, const void */*p*/, size_t /*n*/); -extern int buf_putmemz(buf */*b*/, const void */*p*/, size_t /*n*/); +#define BUF_DECL_PUTMEM_(n, W, w) \ + extern int buf_putmem##w(buf */*b*/, const void */*p*/, size_t /*nn*/); +BUF_DOSUFFIXES(BUF_DECL_PUTMEM_) -/* --- @buf_getbuf{8,16,32,z}@ --- * +/* --- @buf_getbuf{8,{16,24,32,64}{,l,b},z} --- * * * Arguments: @buf *b@ = pointer to a buffer block * @buf *bb@ = where to put the result @@ -266,33 +246,28 @@ extern int buf_putmemz(buf */*b*/, const void */*p*/, size_t /*n*/); * Returns: Zero if it worked, nonzero if there wasn't enough space. * * Use: Gets a block of data from a buffer, and writes its bounds to - * another buffer. The number, @16@ or @32@, is the width of - * the length; @z@ means null-terminated. + * another buffer. */ -extern int buf_getbuf8(buf */*b*/, buf */*bb*/); -extern int buf_getbuf16(buf */*b*/, buf */*bb*/); -extern int buf_getbuf32(buf */*b*/, buf */*bb*/); -extern int buf_getbufz(buf */*b*/, buf */*bb*/); +#define BUF_DECL_GETBUF_(n, W, w) \ + extern int buf_getbuf##w(buf */*b*/, buf */*bb*/); +BUF_DOSUFFIXES(BUF_DECL_GETBUF_) -/* --- @buf_putbuf{8,16,32,z}@ --- * +/* --- @buf_putbuf{8,{16,24,32,64}{,l,b},z} --- * * * Arguments: @buf *b@ = pointer to a buffer block * @buf *bb@ = buffer to write * * Returns: Zero if it worked, nonzero if there wasn't enough space. * - * Use: Puts the contents of a buffer to a buffer. The number, @16@ - * or @32@, is the width of the length; @z@ means null- - * terminated. + * Use: Puts the contents of a buffer to a buffer. */ -extern int buf_putbuf8(buf */*b*/, buf */*bb*/); -extern int buf_putbuf16(buf */*b*/, buf */*bb*/); -extern int buf_putbuf32(buf */*b*/, buf */*bb*/); -extern int buf_putbufz(buf */*b*/, buf */*bb*/); +#define BUF_DECL_PUTBUF_(n, W, w) \ + extern int buf_putbuf##w(buf */*b*/, buf */*bb*/); +BUF_DOSUFFIXES(BUF_DECL_PUTBUF_) -/* --- @buf_getstr{8,16,32,z}@ --- * +/* --- @buf_getdstr{8,{16,24,32,64}{,l,b},z} --- * * * Arguments: @buf *b@ = pointer to a buffer block * @dstr *d@ = where to put the result @@ -300,79 +275,40 @@ extern int buf_putbufz(buf */*b*/, buf */*bb*/); * Returns: Zero if it worked, nonzero if there wasn't enough space. * * Use: Gets a block of data from a buffer, and writes its contents - * to a string. The number, @16@ or @32@, is the width of the - * length; @z@ means null-terminated. + * to a string. */ -extern int buf_getstr8(buf */*b*/, dstr */*d*/); -extern int buf_getstr16(buf */*b*/, dstr */*d*/); -extern int buf_getstr32(buf */*b*/, dstr */*d*/); -extern int buf_getstrz(buf */*b*/, dstr */*d*/); +#define BUF_DECL_GETDSTR_(n, W, w) \ + extern int buf_getdstr##w(buf */*b*/, dstr */*d*/); +BUF_DOSUFFIXES(BUF_DECL_GETDSTR_) -/* --- @buf_putstr{8,16,32,z}@ --- * +/* --- @buf_putdstr{8,{16,24,32,64}{,l,b},z} --- * * * Arguments: @buf *b@ = pointer to a buffer block * @dstr *d@ = string to write * * Returns: Zero if it worked, nonzero if there wasn't enough space. * - * Use: Puts a string to a buffer, and writes its bounds to - * another buffer. The number, @16@ or @32@, is the width of - * the length; @z@ means null-terminated. - */ - -extern int buf_putstr8(buf */*b*/, dstr */*d*/); -extern int buf_putstr16(buf */*b*/, dstr */*d*/); -extern int buf_putstr32(buf */*b*/, dstr */*d*/); -extern int buf_putstrz(buf */*b*/, dstr */*d*/); - -/* --- @buf_getmp@ --- * - * - * Arguments: @buf *b@ = pointer to a buffer block - * - * Returns: A multiprecision integer, or null if there wasn't one there. - * - * Use: Gets a multiprecision integer from a buffer. - */ - -extern mp *buf_getmp(buf */*b*/); - -/* --- @buf_putmp@ --- * - * - * Arguments: @buf *b@ = pointer to a buffer block - * @mp *m@ = a multiprecision integer - * - * Returns: Zero if it worked, nonzero if there wasn't enough space. - * - * Use: Puts a multiprecision integer to a buffer. - */ - -extern int buf_putmp(buf */*b*/, mp */*m*/); - -/* --- @buf_getec@ --- * - * - * Arguments: @buf *b@ = pointer to a buffer block - * @ec *p@ = where to put the point - * - * Returns: Zero if it worked, nonzero if it failed. - * - * Use: Gets a multiprecision integer from a buffer. The point must - * be initialized. + * Use: Puts a dynamic string to a buffer. */ -extern int buf_getec(buf */*b*/, ec */*p*/); +#define BUF_DECL_PUTDSTR_(n, W, w) \ + extern int buf_putdstr##w(buf */*b*/, dstr */*d*/); +BUF_DOSUFFIXES(BUF_DECL_PUTDSTR_) -/* --- @buf_putec@ --- * +/* --- @buf_putstr{8,{16,24,32,64}{,l,b},z} --- * * * Arguments: @buf *b@ = pointer to a buffer block - * @ec *p@ = an elliptic curve point + * @const char *p@ = string to write * * Returns: Zero if it worked, nonzero if there wasn't enough space. * - * Use: Puts an elliptic curve point to a buffer. + * Use: Puts a null-terminated string to a buffer. */ -extern int buf_putec(buf */*b*/, ec */*p*/); +#define BUF_DECL_PUTSTR_(n, W, w) \ + extern int buf_putstr##w(buf */*b*/, const char */*p*/); \ +BUF_DOSUFFIXES(BUF_DECL_PUTSTR_) /*----- That's all, folks -------------------------------------------------*/