Skip to content

Commit

Permalink
buf.[ch]: clang-format
Browse files Browse the repository at this point in the history
  • Loading branch information
janmojzis committed Dec 8, 2024
1 parent 4cf1fa2 commit 6cae8c7
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 68 deletions.
105 changes: 58 additions & 47 deletions buf.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
20140108
20241208 - reformated using clang-format
Jan Mojzis
Public domain.
Expand All @@ -21,8 +22,8 @@ immediately exits with 111 status code.
/*
Initialize 'b' structure.
*/
void buf_init_(const char *fn, unsigned long long line,
struct buf *b, unsigned char *buf, long long alloc) {
void buf_init_(const char *fn, unsigned long long line, struct buf *b,
unsigned char *buf, long long alloc) {

if (!b || !buf || alloc <= 0 || alloc > 1073741824) bug_inval_(fn, line);

Expand All @@ -35,10 +36,11 @@ void buf_init_(const char *fn, unsigned long long line,
/*
Remove content of 'b'.
*/
void buf_purge_(const char *fn, unsigned long long line,
struct buf *b) {
void buf_purge_(const char *fn, unsigned long long line, struct buf *b) {

if (!b || b->len < 0 || b->len > 1073741824 || b->alloc <= 0 || b->alloc > 1073741824 || !b->buf) bug_inval_(fn, line);
if (!b || b->len < 0 || b->len > 1073741824 || b->alloc <= 0 ||
b->alloc > 1073741824 || !b->buf)
bug_inval_(fn, line);

purge(b->buf, b->len);
b->len = 0;
Expand All @@ -47,21 +49,24 @@ void buf_purge_(const char *fn, unsigned long long line,
/*
Retun if 'b' has available space for string of length 'len'.
*/
int buf_ready_(const char *fn, unsigned long long line,
struct buf *b, long long len) {
int buf_ready_(const char *fn, unsigned long long line, struct buf *b,
long long len) {

if (!b || b->len < 0 || b->len > 1073741824 || b->alloc <= 0 || b->alloc > 1073741824 || !b->buf || len < 0 || len > 1073741824) bug_inval_(fn, line);
if (!b || b->len < 0 || b->len > 1073741824 || b->alloc <= 0 ||
b->alloc > 1073741824 || !b->buf || len < 0 || len > 1073741824)
bug_inval_(fn, line);
return (b->len + len < b->alloc);
}


/*
Put string 'x' of length 'len'.
*/
int buf_put_(const char *fn, unsigned long long line,
struct buf *b, const unsigned char *x, long long len) {
int buf_put_(const char *fn, unsigned long long line, struct buf *b,
const unsigned char *x, long long len) {

if (!b || b->len < 0 || b->len > 1073741824 || b->alloc <= 0 || b->alloc > 1073741824 || !b->buf || !x || len < 0 || len > 1073741824) bug_inval_(fn, line);
if (!b || b->len < 0 || b->len > 1073741824 || b->alloc <= 0 ||
b->alloc > 1073741824 || !b->buf || !x || len < 0 || len > 1073741824)
bug_inval_(fn, line);
if (b->len + len >= b->alloc) bug_nomem_(fn, line);

byte_copy(b->buf + b->len, len, x);
Expand All @@ -72,18 +77,20 @@ int buf_put_(const char *fn, unsigned long long line,
/*
Put 0-terminated string 'x'.
*/
int buf_puts_(const char *fn, unsigned long long line,
struct buf *b, const char *x) {
return buf_put_(fn, line, b, (const unsigned char *)x, str_len(x));
int buf_puts_(const char *fn, unsigned long long line, struct buf *b,
const char *x) {
return buf_put_(fn, line, b, (const unsigned char *) x, str_len(x));
}

/*
Put zero bytes of length 'len'.
*/
int buf_putzerobytes_(const char *fn, unsigned long long line,
struct buf *b, long long len) {
int buf_putzerobytes_(const char *fn, unsigned long long line, struct buf *b,
long long len) {

if (!b || b->len < 0 || b->len > 1073741824 || b->alloc <= 0 || b->alloc > 1073741824 || !b->buf || len < 0 || len > 1073741824) bug_inval_(fn, line);
if (!b || b->len < 0 || b->len > 1073741824 || b->alloc <= 0 ||
b->alloc > 1073741824 || !b->buf || len < 0 || len > 1073741824)
bug_inval_(fn, line);
if (b->len + len >= b->alloc) bug_nomem_(fn, line);

byte_zero(b->buf + b->len, len);
Expand All @@ -94,10 +101,12 @@ int buf_putzerobytes_(const char *fn, unsigned long long line,
/*
Put random bytes of length 'len'.
*/
int buf_putrandombytes_(const char *fn, unsigned long long line,
struct buf *b, long long len) {
int buf_putrandombytes_(const char *fn, unsigned long long line, struct buf *b,
long long len) {

if (!b || b->len < 0 || b->len > 1073741824 || b->alloc <= 0 || b->alloc > 1073741824 || !b->buf || len < 0 || len > 1073741824) bug_inval_(fn, line);
if (!b || b->len < 0 || b->len > 1073741824 || b->alloc <= 0 ||
b->alloc > 1073741824 || !b->buf || len < 0 || len > 1073741824)
bug_inval_(fn, line);
if (b->len + len >= b->alloc) bug_nomem_(fn, line);

randombytes(b->buf + b->len, len);
Expand All @@ -108,10 +117,12 @@ int buf_putrandombytes_(const char *fn, unsigned long long line,
/*
Put padding of length 'len'.
*/
int buf_putpadding_(const char *fn, unsigned long long line,
struct buf *b, long long len) {
int buf_putpadding_(const char *fn, unsigned long long line, struct buf *b,
long long len) {

if (!b || b->len < 0 || b->len > 1073741824 || b->alloc <= 0 || b->alloc > 1073741824 || !b->buf || len < 0 || len > 1073741824) bug_inval_(fn, line);
if (!b || b->len < 0 || b->len > 1073741824 || b->alloc <= 0 ||
b->alloc > 1073741824 || !b->buf || len < 0 || len > 1073741824)
bug_inval_(fn, line);
if (b->len + len >= b->alloc) bug_nomem_(fn, line);

purge(b->buf + b->len, len);
Expand All @@ -122,8 +133,8 @@ int buf_putpadding_(const char *fn, unsigned long long line,
/*
Put 32-bit unsigned integer in big-endian format.
*/
int buf_putnum32_(const char *fn, unsigned long long line,
struct buf *b, crypto_uint32 x) {
int buf_putnum32_(const char *fn, unsigned long long line, struct buf *b,
crypto_uint32 x) {

unsigned char s[4];

Expand All @@ -134,17 +145,17 @@ int buf_putnum32_(const char *fn, unsigned long long line,
/*
Put 8-bit unsigned integer.
*/
int buf_putnum8_(const char *fn, unsigned long long line,
struct buf *b, crypto_uint8 u) {
int buf_putnum8_(const char *fn, unsigned long long line, struct buf *b,
crypto_uint8 u) {

return buf_put_(fn, line, b, &u, 1);
}

/*
Put string 'x' of length 'len' and format it as a SSH-string.
*/
int buf_putstringlen_(const char *fn, unsigned long long line,
struct buf *b, const unsigned char *x, long long len) {
int buf_putstringlen_(const char *fn, unsigned long long line, struct buf *b,
const unsigned char *x, long long len) {

if (!buf_putnum32_(fn, line, b, len)) return 0;
return buf_put_(fn, line, b, x, len);
Expand All @@ -153,41 +164,41 @@ int buf_putstringlen_(const char *fn, unsigned long long line,
/*
Put 0-terminated string 'x' and format it as a SSH-string.
*/
int buf_putstring_(const char *fn, unsigned long long line,
struct buf *b, const char *x) {
int buf_putstring_(const char *fn, unsigned long long line, struct buf *b,
const char *x) {

return buf_putstringlen_(fn, line, b, (const unsigned char *)x, str_len(x));
return buf_putstringlen_(fn, line, b, (const unsigned char *) x,
str_len(x));
}

/*
Put SSH shared secret (bignum formated into wire format)
*/
int buf_putsharedsecret_(const char *fn, unsigned long long line,
struct buf *b, const unsigned char *x, long long len) {
int buf_putsharedsecret_(const char *fn, unsigned long long line, struct buf *b,
const unsigned char *x, long long len) {

long long pos;

if (len < 0 || len > 1073741824 || !b || !x) bug_inval_(fn, line);

for (pos = 0; pos < len; ++pos) if (x[pos]) break;
for (pos = 0; pos < len; ++pos)
if (x[pos]) break;

if (x[pos] & 0x80) {
buf_putnum32_(fn, line, b, len - pos + 1);
buf_putnum8_(fn, line, b, 0);
}
else {
buf_putnum32_(fn, line, b, len - pos + 0);
}
else { buf_putnum32_(fn, line, b, len - pos + 0); }
return buf_put_(fn, line, b, x + pos, len - pos);
}


/*
Put string 'x' of length 'len' and encode it into base64.
*/
static const unsigned char b64chars[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int buf_putbase64_(const char *fn, unsigned long long line,
struct buf *b, const unsigned char *x, long long len) {
static const unsigned char b64chars[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
int buf_putbase64_(const char *fn, unsigned long long line, struct buf *b,
const unsigned char *x, long long len) {

long long i;
unsigned long long bits = 0, v = 0;
Expand All @@ -196,7 +207,9 @@ int buf_putbase64_(const char *fn, unsigned long long line,
if (len < 0 || len > 1073741824 || !b || !x) bug_inval_(fn, line);

for (i = 0; i < len; ++i) {
v <<= 8; v += x[i]; bits += 8;
v <<= 8;
v += x[i];
bits += 8;
while (bits > 6) {
ch = b64chars[((v >> (bits - 6)) & 63)];
buf_putnum8_(fn, line, b, ch);
Expand All @@ -209,8 +222,6 @@ int buf_putbase64_(const char *fn, unsigned long long line,
ch = b64chars[v & 63];
buf_putnum8_(fn, line, b, ch);
}
while (b->len & 3) {
buf_putnum8_(fn, line, b, '=');
}
while (b->len & 3) { buf_putnum8_(fn, line, b, '='); }
return 1;
}
59 changes: 38 additions & 21 deletions buf.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/*
20140108
20241208 - reformated using clang-format
Jan Mojzis
Public domain.
*/
Expand All @@ -16,37 +17,53 @@ struct buf {
long long alloc;
};

extern void buf_init_(const char *, unsigned long long, struct buf *, unsigned char *, long long);
extern void buf_init_(const char *, unsigned long long, struct buf *,
unsigned char *, long long);
extern void buf_purge_(const char *, unsigned long long, struct buf *);
extern int buf_ready_(const char *, unsigned long long, struct buf *, long long);

extern int buf_put_(const char *, unsigned long long, struct buf *, const unsigned char *, long long);
extern int buf_puts_(const char *, unsigned long long, struct buf *, const char *);
extern int buf_putzerobytes_(const char *, unsigned long long, struct buf *, long long);
extern int buf_putrandombytes_(const char *, unsigned long long, struct buf *, long long);
extern int buf_putpadding_(const char *, unsigned long long, struct buf *, long long);
extern int buf_putnum32_(const char *, unsigned long long, struct buf *, crypto_uint32);
extern int buf_putnum8_(const char *, unsigned long long, struct buf *, crypto_uint8);
extern int buf_putstringlen_(const char *, unsigned long long, struct buf *, const unsigned char *, long long);
extern int buf_putstring_(const char *, unsigned long long, struct buf *, const char *);
extern int buf_putsharedsecret_(const char *, unsigned long long, struct buf *, const unsigned char *, long long);
extern int buf_putbase64_(const char *, unsigned long long, struct buf *, const unsigned char *, long long);
extern int buf_ready_(const char *, unsigned long long, struct buf *,
long long);

extern int buf_put_(const char *, unsigned long long, struct buf *,
const unsigned char *, long long);
extern int buf_puts_(const char *, unsigned long long, struct buf *,
const char *);
extern int buf_putzerobytes_(const char *, unsigned long long, struct buf *,
long long);
extern int buf_putrandombytes_(const char *, unsigned long long, struct buf *,
long long);
extern int buf_putpadding_(const char *, unsigned long long, struct buf *,
long long);
extern int buf_putnum32_(const char *, unsigned long long, struct buf *,
crypto_uint32);
extern int buf_putnum8_(const char *, unsigned long long, struct buf *,
crypto_uint8);
extern int buf_putstringlen_(const char *, unsigned long long, struct buf *,
const unsigned char *, long long);
extern int buf_putstring_(const char *, unsigned long long, struct buf *,
const char *);
extern int buf_putsharedsecret_(const char *, unsigned long long, struct buf *,
const unsigned char *, long long);
extern int buf_putbase64_(const char *, unsigned long long, struct buf *,
const unsigned char *, long long);

#define buf_init(a, b, cc) buf_init_(__FILE__, __LINE__, (a), (b), (cc))
#define buf_purge(a) buf_purge_(__FILE__, __LINE__, (a))
#define buf_ready(a, b) buf_ready_(__FILE__, __LINE__, (a), (b))

#define buf_put(a, b, cc) buf_put_(__FILE__, __LINE__, (a), (b), (cc))
#define buf_puts(a, b) buf_puts_(__FILE__, __LINE__, (a), (b))
#define buf_putzerobytes(a, b) buf_putzerobytes_(__FILE__, __LINE__, (a), (b))
#define buf_putrandombytes(a, b) buf_putrandombytes_(__FILE__, __LINE__, (a), (b))
#define buf_putpadding(a, b) buf_putpadding_(__FILE__, __LINE__, (a), (b))
#define buf_putzerobytes(a, b) buf_putzerobytes_(__FILE__, __LINE__, (a), (b))
#define buf_putrandombytes(a, b) \
buf_putrandombytes_(__FILE__, __LINE__, (a), (b))
#define buf_putpadding(a, b) buf_putpadding_(__FILE__, __LINE__, (a), (b))
#define buf_putnum32(a, b) buf_putnum32_(__FILE__, __LINE__, (a), (b))
#define buf_putnum8(a, b) buf_putnum8_(__FILE__, __LINE__, (a), (b))
#define buf_putstringlen(a, b, cc) buf_putstringlen_(__FILE__, __LINE__, (a), (b), (cc))
#define buf_putstringlen(a, b, cc) \
buf_putstringlen_(__FILE__, __LINE__, (a), (b), (cc))
#define buf_putstring(a, b) buf_putstring_(__FILE__, __LINE__, (a), (b))
#define buf_putsharedsecret(a, b, cc) buf_putsharedsecret_(__FILE__, __LINE__, (a), (b), (cc))
#define buf_putbase64(a, b, cc) buf_putbase64_(__FILE__, __LINE__, (a), (b), (cc))
#define buf_putsharedsecret(a, b, cc) \
buf_putsharedsecret_(__FILE__, __LINE__, (a), (b), (cc))
#define buf_putbase64(a, b, cc) \
buf_putbase64_(__FILE__, __LINE__, (a), (b), (cc))

#endif

0 comments on commit 6cae8c7

Please sign in to comment.