Loading...
Searching...
No Matches
bstraux.h File Reference

C example that implements trivial additional functions. More...

`#include <time.h>`
`#include <bstrlib.h>`

Go to the source code of this file.

## Macros

#define | bstrDeclare(b)
#define | bstrFree(b)
#define | bAssign(a, b)
#define | bSubs(b, pos, len, a, c)
#define | bStrchr(b, c)
#define | bStrchrFast(b, c)
#define | bCatCstr(b, s)
#define | bCatBlk(b, s, len)
#define | bCatStatic(b, s)
#define | bTrunc(b, n)
#define | bReplaceAll(b, find, repl, pos)
#define | bUppercase(b)
#define | bLowercase(b)
#define | bCaselessCmp(a, b)
#define | bCaselessNCmp(a, b, n)
#define | bBase64Decode(b)
#define | bUuDecode(b)
#define | bAscTime(t)
#define | bCTime(t)
#define | bSecureDestroy(b)
#define | bSecureWriteProtect(t)

## Typedefs

typedef int(* | bNwrite) (const void *buf, size_t elsize, size_t nelem, void *parm)

## Functions

BSTR_PUBLIC struct bStream * | bsFromBstr (const bstring b)
BSTR_PUBLIC bstring | bTail (bstring b, int n)
BSTR_PUBLIC bstring | bHead (bstring b, int n)
BSTR_PUBLIC int | bSetCstrChar (bstring a, int pos, char c)
BSTR_PUBLIC int | bSetChar (bstring b, int pos, char c)
BSTR_PUBLIC int | bFill (bstring a, char c, int len)
BSTR_PUBLIC int | bReplicate (bstring b, int n)
BSTR_PUBLIC int | bReverse (bstring b)
BSTR_PUBLIC int | bInsertChrs (bstring b, int pos, int len, unsigned char c, unsigned char fill)
BSTR_PUBLIC bstring | bStrfTime (const char *fmt, const struct tm *timeptr)
BSTR_PUBLIC int | bJustifyLeft (bstring b, int space)
BSTR_PUBLIC int | bJustifyRight (bstring b, int width, int space)
BSTR_PUBLIC int | bJustifyMargin (bstring b, int width, int space)
BSTR_PUBLIC int | bJustifyCenter (bstring b, int width, int space)
BSTR_PUBLIC char * | bStr2NetStr (const bstring b)
BSTR_PUBLIC bstring | bNetStr2Bstr (const char *buf)
BSTR_PUBLIC bstring | bBase64Encode (const bstring b)
BSTR_PUBLIC bstring | bBase64DecodeEx (const bstring b, int *boolTruncError)
BSTR_PUBLIC struct bStream * | bsUuDecode (struct bStream *sInp, int *badlines)
BSTR_PUBLIC bstring | bUuDecodeEx (const bstring src, int *badlines)
BSTR_PUBLIC bstring | bUuEncode (const bstring src)
BSTR_PUBLIC bstring | bYEncode (const bstring src)
BSTR_PUBLIC bstring | bYDecode (const bstring src)
BSTR_PUBLIC int | bSGMLEncode (bstring b)
BSTR_PUBLIC struct bwriteStream * | bwsOpen (bNwrite writeFn, void *parm)
BSTR_PUBLIC int | bwsWriteBstr (struct bwriteStream *stream, const bstring b)
BSTR_PUBLIC int | bwsWriteBlk (struct bwriteStream *stream, void *blk, int len)
BSTR_PUBLIC int | bwsWriteFlush (struct bwriteStream *stream)
BSTR_PUBLIC int | bwsIsEOF (const struct bwriteStream *stream)
BSTR_PUBLIC int | bwsBuffLength (struct bwriteStream *stream, int sz)
BSTR_PUBLIC void * | bwsClose (struct bwriteStream *stream)
BSTR_PUBLIC bstring | bSecureInput (int maxlen, int termchar, bNgetc vgetchar, void *vgcCtx)

## Detailed Description

C example that implements trivial additional functions.

This file is not a necessary part of the core bstring library itself, but is just an auxilliary module which includes miscellaneous or trivial functions.

## Macro Definition Documentation

## bAscTime

#define bAscTime | ( |  | t | ) |

Value:
(bStrfTime ("%c\n", (t)))
bStrfTime
BSTR_PUBLIC bstring bStrfTime(const char *fmt, const struct tm *timeptr)

## bAssign

#define bAssign | ( |  | a,
|  |  | b )

Value:
((bassign)((a), (b)))
bassign
BSTR_PUBLIC int bassign(bstring a, const bstring b)

## bBase64Decode

#define bBase64Decode | ( |  | b | ) |

Value:
(bBase64DecodeEx((b), NULL))
bBase64DecodeEx
BSTR_PUBLIC bstring bBase64DecodeEx(const bstring b, int *boolTruncError)

## bCaselessCmp

#define bCaselessCmp | ( |  | a,
|  |  | b )

Value:
((bstricmp)((a), (b)))
bstricmp
BSTR_PUBLIC int bstricmp(const bstring b0, const bstring b1)

## bCaselessNCmp

#define bCaselessNCmp | ( |  | a,
|  |  | b,
|  |  | n )

Value:
((bstrnicmp)((a), (b), (n)))
bstrnicmp
BSTR_PUBLIC int bstrnicmp(const bstring b0, const bstring b1, int n)

## bCatBlk

#define bCatBlk | ( |  | b,
|  |  | s,
|  |  | len )

Value:
((bcatblk)((b), (s), (len)))
bcatblk
BSTR_PUBLIC int bcatblk(bstring b, const void *s, int len)

## bCatCstr

#define bCatCstr | ( |  | b,
|  |  | s )

Value:
((bcatcstr)((b), (s)))
bcatcstr
BSTR_PUBLIC int bcatcstr(bstring b, const char *s)

## bCatStatic

#define bCatStatic | ( |  | b,
|  |  | s )

Value:
bcatStatic(b, s)

## bCTime

#define bCTime | ( |  | t | ) |

Value:
((t) ? bAscTime (localtime (t)) : NULL)

## bLowercase

#define bLowercase | ( |  | b | ) |

Value:
((btolower)(b))
btolower
BSTR_PUBLIC int btolower(bstring b)

## bReplaceAll

#define bReplaceAll | ( |  | b,
|  |  | find,
|  |  | repl,
|  |  | pos )

Value:
((bfindreplace)((b),(find),(repl),(pos)))
bfindreplace
BSTR_PUBLIC int bfindreplace(bstring b, const bstring find, const bstring repl, int pos)

## bSecureDestroy

#define bSecureDestroy | ( |  | b | ) |

Value:
do { \
if ((b) && (b)->mlen > 0 && (b)->data) { \
(void)memset((b)->data, 0, (size_t)(b)->mlen); \
} \
(void)bdestroy((b)); \
} while (0)
bdestroy
BSTR_PUBLIC int bdestroy(bstring b)

## bSecureWriteProtect

#define bSecureWriteProtect | ( |  | t | ) |

Value:
do { \
if ((t).mlen >= 0) { \
if ((t).mlen > (t).slen)) { \
(void)memset((t).data + (t).slen, 0, \
(size_t)(t).mlen - (t).slen); \
} \
(t).mlen = -1; \
} \
} while (0)

## bStrchr

#define bStrchr | ( |  | b,
|  |  | c )

Value:
((bstrchr)((b), (c)))
bstrchr
#define bstrchr(b, c)
Definition bstrlib.h:676

## bStrchrFast

#define bStrchrFast | ( |  | b,
|  |  | c )

Value:
((bstrchr)((b), (c)))

## bstrDeclare

#define bstrDeclare | ( |  | b | ) |

Value:
bstring(b) = NULL

## bstrFree

#define bstrFree | ( |  | b | ) |

Value:
do { \
if ((b) != NULL && (b)->slen >= 0 && (b)->mlen >= (b)->slen) { \
bdestroy(b); \
(b) = NULL; \
} \
} while (0)

## bSubs

#define bSubs | ( |  | b,
|  |  | pos,
|  |  | len,
|  |  | a,
|  |  | c )

Value:
((breplace)((b),(pos),(len),(a),(unsigned char)(c)))
breplace
BSTR_PUBLIC int breplace(bstring b1, int pos, int len, const bstring b2, unsigned char fill)

## bTrunc

#define bTrunc | ( |  | b,
|  |  | n )

Value:
((btrunc)((b), (n)))
btrunc
BSTR_PUBLIC int btrunc(bstring b, int n)

## bUppercase

#define bUppercase | ( |  | b | ) |

Value:
((btoupper)(b))
btoupper
BSTR_PUBLIC int btoupper(bstring b)

## bUuDecode

#define bUuDecode | ( |  | b | ) |

Value:
(bUuDecodeEx((b), NULL))
bUuDecodeEx
BSTR_PUBLIC bstring bUuDecodeEx(const bstring src, int *badlines)

## Function Documentation

## bBase64DecodeEx()

BSTR_PUBLIC bstring bBase64DecodeEx | ( | const bstring | b,
|  | int * | boolTruncError )

Decode a base64 block of data.

All MIME headers are assumed to have been removed.

See: RFC1341

## bBase64Encode()

BSTR_PUBLIC bstring bBase64Encode | ( | const bstring | b | ) |

Generate a base64 encoding.

See: RFC1341

## bFill()

BSTR_PUBLIC int bFill | ( | bstring | a,
|  | char | c,
|  | int | len )

Fill a given bstring with the character in parameter c, for a length n.

## bHead()

BSTR_PUBLIC bstring bHead | ( | bstring | b,
|  | int | n )

Return with a string of the first n characters of b.

## bInsertChrs()

BSTR_PUBLIC int bInsertChrs | ( | bstring | b,
|  | int | pos,
|  | int | len,
|  | unsigned char | c,
|  | unsigned char | fill )

Insert a repeated sequence of a given character into the string at position pos for a length len.

## bJustifyCenter()

BSTR_PUBLIC int bJustifyCenter | ( | bstring | b,
|  | int | width,
|  | int | space )

Center a string's non-white space characters to within a given width by inserting whitespaces at the beginning.

## bJustifyLeft()

BSTR_PUBLIC int bJustifyLeft | ( | bstring | b,
|  | int | space )

Left justify a string.

## bJustifyMargin()

BSTR_PUBLIC int bJustifyMargin | ( | bstring | b,
|  | int | width,
|  | int | space )

Stretch a string to flush against left and right margins by evenly distributing additional white space between words.

If the line is too long to be margin justified, it is left justified.

## bJustifyRight()

BSTR_PUBLIC int bJustifyRight | ( | bstring | b,
|  | int | width,
|  | int | space )

Right justify a string to within a given width.

## bNetStr2Bstr()

BSTR_PUBLIC bstring bNetStr2Bstr | ( | const char * | buf | ) |

Convert a netstring to a bstring.

See http://cr.yp.to/proto/netstrings.txt for a description of netstrings.

Note that the terminating "," must be present, however a following '\0' is not required.

## bReplicate()

BSTR_PUBLIC int bReplicate | ( | bstring | b,
|  | int | n )

Replicate the contents of b end to end n times and replace it in b.

## bReverse()

BSTR_PUBLIC int bReverse | ( | bstring | b | ) |

Reverse the contents of b in place.

## bSecureInput()

BSTR_PUBLIC bstring bSecureInput | ( | int | maxlen,
|  | int | termchar,
|  | bNgetc | vgetchar,
|  | void * | vgcCtx )

Read input from an abstracted input interface, for a length of at most maxlen characters.

If maxlen <= 0, then there is no length limit put on the input. The result is terminated early if vgetchar() return EOF or the user specified value termchar.

## bSetChar()

BSTR_PUBLIC int bSetChar | ( | bstring | b,
|  | int | pos,
|  | char | c )

Sets the character at position pos to the character c in the bstring a.

The string is not truncated if the character c is NUL ('\0'). pos must be in the position between 0 and b->slen inclusive, otherwise BSTR_ERR will be returned.

## bSetCstrChar()

BSTR_PUBLIC int bSetCstrChar | ( | bstring | a,
|  | int | pos,
|  | char | c )

Sets the character at position pos to the character c in the bstring a.

If the character c is NUL ('\0') then the string is truncated at this point. Note: this does not enable any other '\0' character in the bstring as terminator indicator for the string. pos must be in the position between 0 and b->slen inclusive, otherwise BSTR_ERR will be returned.

## bsFromBstr()

BSTR_PUBLIC struct bStream * bsFromBstr | ( | const bstring | b | ) |

Create a bStream whose contents are a copy of the bstring passed in.

This allows the use of all the bStream APIs with bstrings.

## bSGMLEncode()

BSTR_PUBLIC int bSGMLEncode | ( | bstring | b | ) |

Change the string into a version that is quotable in SGML (HTML, XML). Replaces &, ", <, > with their SGML entity equivalents.

Parameters

  b | the bstring to encode in-place

Returns
  BSTR_OK on success, BSTR_ERR on error

## bStr2NetStr()

BSTR_PUBLIC char * bStr2NetStr | ( | const bstring | b | ) |

Convert a bstring to a netstring.

See http://cr.yp.to/proto/netstrings.txt for a description of netstrings.

Note: 1) The value returned should be freed with a call to bcstrfree() at the point when it will no longer be referenced to avoid a memory leak. 2) If the returned value is non-NULL, then it also '\0' terminated in the character position one past the "," terminator.

## bStrfTime()

BSTR_PUBLIC bstring bStrfTime | ( | const char * | fmt,
|  | const struct tm * | timeptr )

Takes a format string that is compatible with strftime and a struct tm pointer, formats the time according to the format string and outputs the bstring as a result.

Note that if there is an early generation of a '\0' character, the bstring will be truncated to this end point.

## bsUuDecode()

BSTR_PUBLIC struct bStream * bsUuDecode | ( | struct bStream * | sInp,
|  | int * | badlines )

Creates a bStream which performs the UUDecode of an an input stream.

If there are errors in the decoding, they are counted up and returned in "badlines", if badlines is not NULL. It is assumed that the "begin" and "end" lines have already been stripped off. The potential security problem of writing the filename in the begin line is something that is beyond the scope of a portable library.

## bTail()

BSTR_PUBLIC bstring bTail | ( | bstring | b,
|  | int | n )

Return with a string of the last n characters of b.

## bUuDecodeEx()

BSTR_PUBLIC bstring bUuDecodeEx | ( | const bstring | src,
|  | int * | badlines )

Performs a UUDecode of a block of data.

If there are errors in the decoding, they are counted up and returned in "badlines", if badlines is not NULL. It is assumed that the "begin" and "end" lines have already been stripped off. The potential security problem of writing the filename in the begin line is something that is beyond the scope of a portable library.

## bUuEncode()

BSTR_PUBLIC bstring bUuEncode | ( | const bstring | src | ) |

Performs a UUEncode of a block of data.

The "begin" and "end" lines are not appended.

## bwsBuffLength()

BSTR_PUBLIC int bwsBuffLength | ( | struct bwriteStream * | stream,
|  | int | sz )

Set the length of the buffer used by the bwsStream.

If sz is zero, the length is not set. This function returns with the previous length.

## bwsClose()

BSTR_PUBLIC void * bwsClose | ( | struct bwriteStream * | stream | ) |

Close the bwriteStream, and return the handle to the stream that was originally used to open the given stream.

Note that even if the stream is at EOF it still needs to be closed with a call to bwsClose.

## bwsIsEOF()

BSTR_PUBLIC int bwsIsEOF | ( | const struct bwriteStream * | stream | ) |

Returns 0 if the stream is currently writable, 1 if the core stream has responded by not accepting the previous attempted write.

## bwsOpen()

BSTR_PUBLIC struct bwriteStream * bwsOpen | ( | bNwrite | writeFn,
|  | void * | parm )

Wrap a given open stream (described by a fwrite work-a-like function pointer and stream handle) into an open bwriteStream suitable for write streaming functions.

## bwsWriteBlk()

BSTR_PUBLIC int bwsWriteBlk | ( | struct bwriteStream * | stream,
|  | void * | blk,
|  | int | len )

Send a block of data a bwriteStream.

If the stream is at EOF BSTR_ERR is returned.

## bwsWriteBstr()

BSTR_PUBLIC int bwsWriteBstr | ( | struct bwriteStream * | stream,
|  | const bstring | b )

Send a bstring to a bwriteStream.

If the stream is at EOF BSTR_ERR is returned. Note that there is no deterministic way to determine the exact cut off point where the core stream stopped accepting data.

## bwsWriteFlush()

BSTR_PUBLIC int bwsWriteFlush | ( | struct bwriteStream * | stream | ) |

Force any pending data to be written to the core stream.

## bYDecode()

BSTR_PUBLIC bstring bYDecode | ( | const bstring | src | ) |

Performs a YDecode of a block of data.

See: https://yence.sourceforge.net/docs/protocol/version1_3_draft.html

## bYEncode()

BSTR_PUBLIC bstring bYEncode | ( | const bstring | src | ) |

Performs a YEncode of a block of data.

No header or tail info is appended.

See: https://yence.sourceforge.net/docs/protocol/version1_3_draft.html