]> arthur.barton.de Git - netatalk.git/blobdiff - libatalk/bstring/bstrlib.c
commit dircache rewrite.
[netatalk.git] / libatalk / bstring / bstrlib.c
diff --git a/libatalk/bstring/bstrlib.c b/libatalk/bstring/bstrlib.c
new file mode 100755 (executable)
index 0000000..93c481c
--- /dev/null
@@ -0,0 +1,2956 @@
+/*\r
+ * This source file is part of the bstring string library.  This code was\r
+ * written by Paul Hsieh in 2002-2008, and is covered by the BSD open source \r
+ * license and the GPL. Refer to the accompanying documentation for details \r
+ * on usage and license.\r
+ */\r
+\r
+/*\r
+ * bstrlib.c\r
+ *\r
+ * This file is the core module for implementing the bstring functions.\r
+ */\r
+\r
+#include <stdio.h>\r
+#include <stddef.h>\r
+#include <stdarg.h>\r
+#include <stdlib.h>\r
+#include <string.h>\r
+#include <ctype.h>\r
+\r
+#include <atalk/bstrlib.h>\r
+\r
+/* Optionally include a mechanism for debugging memory */\r
+\r
+#if defined(MEMORY_DEBUG) || defined(BSTRLIB_MEMORY_DEBUG)\r
+#include "memdbg.h"\r
+#endif\r
+\r
+#ifndef bstr__alloc\r
+#define bstr__alloc(x) malloc (x)\r
+#endif\r
+\r
+#ifndef bstr__free\r
+#define bstr__free(p) free (p)\r
+#endif\r
+\r
+#ifndef bstr__realloc\r
+#define bstr__realloc(p,x) realloc ((p), (x))\r
+#endif\r
+\r
+#ifndef bstr__memcpy\r
+#define bstr__memcpy(d,s,l) memcpy ((d), (s), (l))\r
+#endif\r
+\r
+#ifndef bstr__memmove\r
+#define bstr__memmove(d,s,l) memmove ((d), (s), (l))\r
+#endif\r
+\r
+#ifndef bstr__memset\r
+#define bstr__memset(d,c,l) memset ((d), (c), (l))\r
+#endif\r
+\r
+#ifndef bstr__memcmp\r
+#define bstr__memcmp(d,c,l) memcmp ((d), (c), (l))\r
+#endif\r
+\r
+#ifndef bstr__memchr\r
+#define bstr__memchr(s,c,l) memchr ((s), (c), (l))\r
+#endif\r
+\r
+/* Just a length safe wrapper for memmove. */\r
+\r
+#define bBlockCopy(D,S,L) { if ((L) > 0) bstr__memmove ((D),(S),(L)); }\r
+\r
+/* Compute the snapped size for a given requested size.  By snapping to powers\r
+   of 2 like this, repeated reallocations are avoided. */\r
+static int snapUpSize (int i) {\r
+       if (i < 8) {\r
+               i = 8;\r
+       } else {\r
+               unsigned int j;\r
+               j = (unsigned int) i;\r
+\r
+               j |= (j >>  1);\r
+               j |= (j >>  2);\r
+               j |= (j >>  4);\r
+               j |= (j >>  8);         /* Ok, since int >= 16 bits */\r
+#if (UINT_MAX != 0xffff)\r
+               j |= (j >> 16);         /* For 32 bit int systems */\r
+#if (UINT_MAX > 0xffffffffUL)\r
+               j |= (j >> 32);         /* For 64 bit int systems */\r
+#endif\r
+#endif\r
+               /* Least power of two greater than i */\r
+               j++;\r
+               if ((int) j >= i) i = (int) j;\r
+       }\r
+       return i;\r
+}\r
+\r
+/*  int balloc (bstring b, int len)\r
+ *\r
+ *  Increase the size of the memory backing the bstring b to at least len.\r
+ */\r
+int balloc (bstring b, int olen) {\r
+       int len;\r
+       if (b == NULL || b->data == NULL || b->slen < 0 || b->mlen <= 0 || \r
+           b->mlen < b->slen || olen <= 0) {\r
+               return BSTR_ERR;\r
+       }\r
+\r
+       if (olen >= b->mlen) {\r
+               unsigned char * x;\r
+\r
+               if ((len = snapUpSize (olen)) <= b->mlen) return BSTR_OK;\r
+\r
+               /* Assume probability of a non-moving realloc is 0.125 */\r
+               if (7 * b->mlen < 8 * b->slen) {\r
+\r
+                       /* If slen is close to mlen in size then use realloc to reduce\r
+                          the memory defragmentation */\r
+\r
+                       reallocStrategy:;\r
+\r
+                       x = (unsigned char *) bstr__realloc (b->data, (size_t) len);\r
+                       if (x == NULL) {\r
+\r
+                               /* Since we failed, try allocating the tighest possible \r
+                                  allocation */\r
+\r
+                               if (NULL == (x = (unsigned char *) bstr__realloc (b->data, (size_t) (len = olen)))) {\r
+                                       return BSTR_ERR;\r
+                               }\r
+                       }\r
+               } else {\r
+\r
+                       /* If slen is not close to mlen then avoid the penalty of copying\r
+                          the extra bytes that are allocated, but not considered part of\r
+                          the string */\r
+\r
+                       if (NULL == (x = (unsigned char *) bstr__alloc ((size_t) len))) {\r
+\r
+                               /* Perhaps there is no available memory for the two \r
+                                  allocations to be in memory at once */\r
+\r
+                               goto reallocStrategy;\r
+\r
+                       } else {\r
+                               if (b->slen) bstr__memcpy ((char *) x, (char *) b->data, (size_t) b->slen);\r
+                               bstr__free (b->data);\r
+                       }\r
+               }\r
+               b->data = x;\r
+               b->mlen = len;\r
+               b->data[b->slen] = (unsigned char) '\0';\r
+       }\r
+\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  int ballocmin (bstring b, int len)\r
+ *\r
+ *  Set the size of the memory backing the bstring b to len or b->slen+1,\r
+ *  whichever is larger.  Note that repeated use of this function can degrade\r
+ *  performance.\r
+ */\r
+int ballocmin (bstring b, int len) {\r
+       unsigned char * s;\r
+\r
+       if (b == NULL || b->data == NULL || (b->slen+1) < 0 || b->mlen <= 0 || \r
+           b->mlen < b->slen || len <= 0) {\r
+               return BSTR_ERR;\r
+       }\r
+\r
+       if (len < b->slen + 1) len = b->slen + 1;\r
+\r
+       if (len != b->mlen) {\r
+               s = (unsigned char *) bstr__realloc (b->data, (size_t) len);\r
+               if (NULL == s) return BSTR_ERR;\r
+               s[b->slen] = (unsigned char) '\0';\r
+               b->data = s;\r
+               b->mlen = len;\r
+       }\r
+\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  bstring bfromcstr (const char * str)\r
+ *\r
+ *  Create a bstring which contains the contents of the '\0' terminated char *\r
+ *  buffer str.\r
+ */\r
+bstring bfromcstr (const char * str) {\r
+bstring b;\r
+int i;\r
+size_t j;\r
+\r
+       if (str == NULL) return NULL;\r
+       j = (strlen) (str);\r
+       i = snapUpSize ((int) (j + (2 - (j != 0))));\r
+       if (i <= (int) j) return NULL;\r
+\r
+       b = (bstring) bstr__alloc (sizeof (struct tagbstring));\r
+       if (NULL == b) return NULL;\r
+       b->slen = (int) j;\r
+       if (NULL == (b->data = (unsigned char *) bstr__alloc (b->mlen = i))) {\r
+               bstr__free (b);\r
+               return NULL;\r
+       }\r
+\r
+       bstr__memcpy (b->data, str, j+1);\r
+       return b;\r
+}\r
+\r
+/*  bstring bfromcstralloc (int mlen, const char * str)\r
+ *\r
+ *  Create a bstring which contains the contents of the '\0' terminated char *\r
+ *  buffer str.  The memory buffer backing the string is at least len \r
+ *  characters in length.\r
+ */\r
+bstring bfromcstralloc (int mlen, const char * str) {\r
+bstring b;\r
+int i;\r
+size_t j;\r
+\r
+       if (str == NULL) return NULL;\r
+       j = (strlen) (str);\r
+       i = snapUpSize ((int) (j + (2 - (j != 0))));\r
+       if (i <= (int) j) return NULL;\r
+\r
+       b = (bstring) bstr__alloc (sizeof (struct tagbstring));\r
+       if (b == NULL) return NULL;\r
+       b->slen = (int) j;\r
+       if (i < mlen) i = mlen;\r
+\r
+       if (NULL == (b->data = (unsigned char *) bstr__alloc (b->mlen = i))) {\r
+               bstr__free (b);\r
+               return NULL;\r
+       }\r
+\r
+       bstr__memcpy (b->data, str, j+1);\r
+       return b;\r
+}\r
+\r
+/*  bstring blk2bstr (const void * blk, int len)\r
+ *\r
+ *  Create a bstring which contains the content of the block blk of length \r
+ *  len.\r
+ */\r
+bstring blk2bstr (const void * blk, int len) {\r
+bstring b;\r
+int i;\r
+\r
+       if (blk == NULL || len < 0) return NULL;\r
+       b = (bstring) bstr__alloc (sizeof (struct tagbstring));\r
+       if (b == NULL) return NULL;\r
+       b->slen = len;\r
+\r
+       i = len + (2 - (len != 0));\r
+       i = snapUpSize (i);\r
+\r
+       b->mlen = i;\r
+\r
+       b->data = (unsigned char *) bstr__alloc ((size_t) b->mlen);\r
+       if (b->data == NULL) {\r
+               bstr__free (b);\r
+               return NULL;\r
+       }\r
+\r
+       if (len > 0) bstr__memcpy (b->data, blk, (size_t) len);\r
+       b->data[len] = (unsigned char) '\0';\r
+\r
+       return b;\r
+}\r
+\r
+/*  char * bstr2cstr (const_bstring s, char z)\r
+ *\r
+ *  Create a '\0' terminated char * buffer which is equal to the contents of \r
+ *  the bstring s, except that any contained '\0' characters are converted \r
+ *  to the character in z. This returned value should be freed with a \r
+ *  bcstrfree () call, by the calling application.\r
+ */\r
+char * bstr2cstr (const_bstring b, char z) {\r
+int i, l;\r
+char * r;\r
+\r
+       if (b == NULL || b->slen < 0 || b->data == NULL) return NULL;\r
+       l = b->slen;\r
+       r = (char *) bstr__alloc ((size_t) (l + 1));\r
+       if (r == NULL) return r;\r
+\r
+       for (i=0; i < l; i ++) {\r
+               r[i] = (char) ((b->data[i] == '\0') ? z : (char) (b->data[i]));\r
+       }\r
+\r
+       r[l] = (unsigned char) '\0';\r
+\r
+       return r;\r
+}\r
+\r
+/*  int bcstrfree (char * s)\r
+ *\r
+ *  Frees a C-string generated by bstr2cstr ().  This is normally unnecessary\r
+ *  since it just wraps a call to bstr__free (), however, if bstr__alloc () \r
+ *  and bstr__free () have been redefined as a macros within the bstrlib \r
+ *  module (via defining them in memdbg.h after defining \r
+ *  BSTRLIB_MEMORY_DEBUG) with some difference in behaviour from the std \r
+ *  library functions, then this allows a correct way of freeing the memory \r
+ *  that allows higher level code to be independent from these macro \r
+ *  redefinitions.\r
+ */\r
+int bcstrfree (char * s) {\r
+       if (s) {\r
+               bstr__free (s);\r
+               return BSTR_OK;\r
+       }\r
+       return BSTR_ERR;\r
+}\r
+\r
+/*  int bconcat (bstring b0, const_bstring b1)\r
+ *\r
+ *  Concatenate the bstring b1 to the bstring b0.\r
+ */\r
+int bconcat (bstring b0, const_bstring b1) {\r
+int len, d;\r
+bstring aux = (bstring) b1;\r
+\r
+       if (b0 == NULL || b1 == NULL || b0->data == NULL || b1->data == NULL) return BSTR_ERR;\r
+\r
+       d = b0->slen;\r
+       len = b1->slen;\r
+       if ((d | (b0->mlen - d) | len | (d + len)) < 0) return BSTR_ERR;\r
+\r
+       if (b0->mlen <= d + len + 1) {\r
+               ptrdiff_t pd = b1->data - b0->data;\r
+               if (0 <= pd && pd < b0->mlen) {\r
+                       if (NULL == (aux = bstrcpy (b1))) return BSTR_ERR;\r
+               }\r
+               if (balloc (b0, d + len + 1) != BSTR_OK) {\r
+                       if (aux != b1) bdestroy (aux);\r
+                       return BSTR_ERR;\r
+               }\r
+       }\r
+\r
+       bBlockCopy (&b0->data[d], &aux->data[0], (size_t) len);\r
+       b0->data[d + len] = (unsigned char) '\0';\r
+       b0->slen = d + len;\r
+       if (aux != b1) bdestroy (aux);\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  int bconchar (bstring b, char c)\r
+/ *\r
+ *  Concatenate the single character c to the bstring b.\r
+ */\r
+int bconchar (bstring b, char c) {\r
+int d;\r
+\r
+       if (b == NULL) return BSTR_ERR;\r
+       d = b->slen;\r
+       if ((d | (b->mlen - d)) < 0 || balloc (b, d + 2) != BSTR_OK) return BSTR_ERR;\r
+       b->data[d] = (unsigned char) c;\r
+       b->data[d + 1] = (unsigned char) '\0';\r
+       b->slen++;\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  int bcatcstr (bstring b, const char * s)\r
+ *\r
+ *  Concatenate a char * string to a bstring.\r
+ */\r
+int bcatcstr (bstring b, const char * s) {\r
+char * d;\r
+int i, l;\r
+\r
+       if (b == NULL || b->data == NULL || b->slen < 0 || b->mlen < b->slen\r
+        || b->mlen <= 0 || s == NULL) return BSTR_ERR;\r
+\r
+       /* Optimistically concatenate directly */\r
+       l = b->mlen - b->slen;\r
+       d = (char *) &b->data[b->slen];\r
+       for (i=0; i < l; i++) {\r
+               if ((*d++ = *s++) == '\0') {\r
+                       b->slen += i;\r
+                       return BSTR_OK;\r
+               }\r
+       }\r
+       b->slen += i;\r
+\r
+       /* Need to explicitely resize and concatenate tail */\r
+       return bcatblk (b, (const void *) s, (int) strlen (s));\r
+}\r
+\r
+/*  int bcatblk (bstring b, const void * s, int len)\r
+ *\r
+ *  Concatenate a fixed length buffer to a bstring.\r
+ */\r
+int bcatblk (bstring b, const void * s, int len) {\r
+int nl;\r
+\r
+       if (b == NULL || b->data == NULL || b->slen < 0 || b->mlen < b->slen\r
+        || b->mlen <= 0 || s == NULL || len < 0) return BSTR_ERR;\r
+\r
+       if (0 > (nl = b->slen + len)) return BSTR_ERR; /* Overflow? */\r
+       if (b->mlen <= nl && 0 > balloc (b, nl + 1)) return BSTR_ERR;\r
+\r
+       bBlockCopy (&b->data[b->slen], s, (size_t) len);\r
+       b->slen = nl;\r
+       b->data[nl] = (unsigned char) '\0';\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  bstring bstrcpy (const_bstring b)\r
+ *\r
+ *  Create a copy of the bstring b.\r
+ */\r
+bstring bstrcpy (const_bstring b) {\r
+bstring b0;\r
+int i,j;\r
+\r
+       /* Attempted to copy an invalid string? */\r
+       if (b == NULL || b->slen < 0 || b->data == NULL) return NULL;\r
+\r
+       b0 = (bstring) bstr__alloc (sizeof (struct tagbstring));\r
+       if (b0 == NULL) {\r
+               /* Unable to allocate memory for string header */\r
+               return NULL;\r
+       }\r
+\r
+       i = b->slen;\r
+       j = snapUpSize (i + 1);\r
+\r
+       b0->data = (unsigned char *) bstr__alloc (j);\r
+       if (b0->data == NULL) {\r
+               j = i + 1;\r
+               b0->data = (unsigned char *) bstr__alloc (j);\r
+               if (b0->data == NULL) {\r
+                       /* Unable to allocate memory for string data */\r
+                       bstr__free (b0);\r
+                       return NULL;\r
+               }\r
+       }\r
+\r
+       b0->mlen = j;\r
+       b0->slen = i;\r
+\r
+       if (i) bstr__memcpy ((char *) b0->data, (char *) b->data, i);\r
+       b0->data[b0->slen] = (unsigned char) '\0';\r
+\r
+       return b0;\r
+}\r
+\r
+/*  int bassign (bstring a, const_bstring b)\r
+ *\r
+ *  Overwrite the string a with the contents of string b.\r
+ */\r
+int bassign (bstring a, const_bstring b) {\r
+       if (b == NULL || b->data == NULL || b->slen < 0)\r
+               return BSTR_ERR;\r
+       if (b->slen != 0) {\r
+               if (balloc (a, b->slen) != BSTR_OK) return BSTR_ERR;\r
+               bstr__memmove (a->data, b->data, b->slen);\r
+       } else {\r
+               if (a == NULL || a->data == NULL || a->mlen < a->slen || \r
+                   a->slen < 0 || a->mlen == 0) \r
+                       return BSTR_ERR;\r
+       }\r
+       a->data[b->slen] = (unsigned char) '\0';\r
+       a->slen = b->slen;\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  int bassignmidstr (bstring a, const_bstring b, int left, int len)\r
+ *\r
+ *  Overwrite the string a with the middle of contents of string b \r
+ *  starting from position left and running for a length len.  left and \r
+ *  len are clamped to the ends of b as with the function bmidstr.\r
+ */\r
+int bassignmidstr (bstring a, const_bstring b, int left, int len) {\r
+       if (b == NULL || b->data == NULL || b->slen < 0)\r
+               return BSTR_ERR;\r
+\r
+       if (left < 0) {\r
+               len += left;\r
+               left = 0;\r
+       }\r
+\r
+       if (len > b->slen - left) len = b->slen - left;\r
+\r
+       if (a == NULL || a->data == NULL || a->mlen < a->slen ||\r
+           a->slen < 0 || a->mlen == 0)\r
+               return BSTR_ERR;\r
+\r
+       if (len > 0) {\r
+               if (balloc (a, len) != BSTR_OK) return BSTR_ERR;\r
+               bstr__memmove (a->data, b->data + left, len);\r
+               a->slen = len;\r
+       } else {\r
+               a->slen = 0;\r
+       }\r
+       a->data[a->slen] = (unsigned char) '\0';\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  int bassigncstr (bstring a, const char * str)\r
+ *\r
+ *  Overwrite the string a with the contents of char * string str.  Note that \r
+ *  the bstring a must be a well defined and writable bstring.  If an error \r
+ *  occurs BSTR_ERR is returned however a may be partially overwritten.\r
+ */\r
+int bassigncstr (bstring a, const char * str) {\r
+int i;\r
+size_t len;\r
+       if (a == NULL || a->data == NULL || a->mlen < a->slen ||\r
+           a->slen < 0 || a->mlen == 0 || NULL == str) \r
+               return BSTR_ERR;\r
+\r
+       for (i=0; i < a->mlen; i++) {\r
+               if ('\0' == (a->data[i] = str[i])) {\r
+                       a->slen = i;\r
+                       return BSTR_OK;\r
+               }\r
+       }\r
+\r
+       a->slen = i;\r
+       len = strlen (str + i);\r
+       if (len > INT_MAX || i + len + 1 > INT_MAX ||\r
+           0 > balloc (a, (int) (i + len + 1))) return BSTR_ERR;\r
+       bBlockCopy (a->data + i, str + i, (size_t) len + 1);\r
+       a->slen += (int) len;\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  int bassignblk (bstring a, const void * s, int len)\r
+ *\r
+ *  Overwrite the string a with the contents of the block (s, len).  Note that \r
+ *  the bstring a must be a well defined and writable bstring.  If an error \r
+ *  occurs BSTR_ERR is returned and a is not overwritten.\r
+ */\r
+int bassignblk (bstring a, const void * s, int len) {\r
+       if (a == NULL || a->data == NULL || a->mlen < a->slen ||\r
+           a->slen < 0 || a->mlen == 0 || NULL == s || len + 1 < 1) \r
+               return BSTR_ERR;\r
+       if (len + 1 > a->mlen && 0 > balloc (a, len + 1)) return BSTR_ERR;\r
+       bBlockCopy (a->data, s, (size_t) len);\r
+       a->data[len] = (unsigned char) '\0';\r
+       a->slen = len;\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  int btrunc (bstring b, int n)\r
+ *\r
+ *  Truncate the bstring to at most n characters.\r
+ */\r
+int btrunc (bstring b, int n) {\r
+       if (n < 0 || b == NULL || b->data == NULL || b->mlen < b->slen ||\r
+           b->slen < 0 || b->mlen <= 0) return BSTR_ERR;\r
+       if (b->slen > n) {\r
+               b->slen = n;\r
+               b->data[n] = (unsigned char) '\0';\r
+       }\r
+       return BSTR_OK;\r
+}\r
+\r
+#define   upcase(c) (toupper ((unsigned char) c))\r
+#define downcase(c) (tolower ((unsigned char) c))\r
+#define   wspace(c) (isspace ((unsigned char) c))\r
+\r
+/*  int btoupper (bstring b)\r
+ *\r
+ *  Convert contents of bstring to upper case.\r
+ */\r
+int btoupper (bstring b) {\r
+int i, len;\r
+       if (b == NULL || b->data == NULL || b->mlen < b->slen ||\r
+           b->slen < 0 || b->mlen <= 0) return BSTR_ERR;\r
+       for (i=0, len = b->slen; i < len; i++) {\r
+               b->data[i] = (unsigned char) upcase (b->data[i]);\r
+       }\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  int btolower (bstring b)\r
+ *\r
+ *  Convert contents of bstring to lower case.\r
+ */\r
+int btolower (bstring b) {\r
+int i, len;\r
+       if (b == NULL || b->data == NULL || b->mlen < b->slen ||\r
+           b->slen < 0 || b->mlen <= 0) return BSTR_ERR;\r
+       for (i=0, len = b->slen; i < len; i++) {\r
+               b->data[i] = (unsigned char) downcase (b->data[i]);\r
+       }\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  int bstricmp (const_bstring b0, const_bstring b1)\r
+ *\r
+ *  Compare two strings without differentiating between case.  The return \r
+ *  value is the difference of the values of the characters where the two \r
+ *  strings first differ after lower case transformation, otherwise 0 is \r
+ *  returned indicating that the strings are equal.  If the lengths are \r
+ *  different, then a difference from 0 is given, but if the first extra \r
+ *  character is '\0', then it is taken to be the value UCHAR_MAX+1.\r
+ */\r
+int bstricmp (const_bstring b0, const_bstring b1) {\r
+int i, v, n;\r
+\r
+       if (bdata (b0) == NULL || b0->slen < 0 || \r
+           bdata (b1) == NULL || b1->slen < 0) return SHRT_MIN;\r
+       if ((n = b0->slen) > b1->slen) n = b1->slen;\r
+       else if (b0->slen == b1->slen && b0->data == b1->data) return BSTR_OK;\r
+\r
+       for (i = 0; i < n; i ++) {\r
+               v  = (char) downcase (b0->data[i])\r
+                  - (char) downcase (b1->data[i]);\r
+               if (0 != v) return v;\r
+       }\r
+\r
+       if (b0->slen > n) {\r
+               v = (char) downcase (b0->data[n]);\r
+               if (v) return v;\r
+               return UCHAR_MAX + 1;\r
+       }\r
+       if (b1->slen > n) {\r
+               v = - (char) downcase (b1->data[n]);\r
+               if (v) return v;\r
+               return - (int) (UCHAR_MAX + 1);\r
+       }\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  int bstrnicmp (const_bstring b0, const_bstring b1, int n)\r
+ *\r
+ *  Compare two strings without differentiating between case for at most n\r
+ *  characters.  If the position where the two strings first differ is\r
+ *  before the nth position, the return value is the difference of the values\r
+ *  of the characters, otherwise 0 is returned.  If the lengths are different\r
+ *  and less than n characters, then a difference from 0 is given, but if the \r
+ *  first extra character is '\0', then it is taken to be the value \r
+ *  UCHAR_MAX+1.\r
+ */\r
+int bstrnicmp (const_bstring b0, const_bstring b1, int n) {\r
+int i, v, m;\r
+\r
+       if (bdata (b0) == NULL || b0->slen < 0 || \r
+           bdata (b1) == NULL || b1->slen < 0 || n < 0) return SHRT_MIN;\r
+       m = n;\r
+       if (m > b0->slen) m = b0->slen;\r
+       if (m > b1->slen) m = b1->slen;\r
+\r
+       if (b0->data != b1->data) {\r
+               for (i = 0; i < m; i ++) {\r
+                       v  = (char) downcase (b0->data[i]);\r
+                       v -= (char) downcase (b1->data[i]);\r
+                       if (v != 0) return b0->data[i] - b1->data[i];\r
+               }\r
+       }\r
+\r
+       if (n == m || b0->slen == b1->slen) return BSTR_OK;\r
+\r
+       if (b0->slen > m) {\r
+               v = (char) downcase (b0->data[m]);\r
+               if (v) return v;\r
+               return UCHAR_MAX + 1;\r
+       }\r
+\r
+       v = - (char) downcase (b1->data[m]);\r
+       if (v) return v;\r
+       return - (int) (UCHAR_MAX + 1);\r
+}\r
+\r
+/*  int biseqcaseless (const_bstring b0, const_bstring b1)\r
+ *\r
+ *  Compare two strings for equality without differentiating between case.  \r
+ *  If the strings differ other than in case, 0 is returned, if the strings \r
+ *  are the same, 1 is returned, if there is an error, -1 is returned.  If \r
+ *  the length of the strings are different, this function is O(1).  '\0' \r
+ *  termination characters are not treated in any special way.\r
+ */\r
+int biseqcaseless (const_bstring b0, const_bstring b1) {\r
+int i, n;\r
+\r
+       if (bdata (b0) == NULL || b0->slen < 0 || \r
+           bdata (b1) == NULL || b1->slen < 0) return BSTR_ERR;\r
+       if (b0->slen != b1->slen) return BSTR_OK;\r
+       if (b0->data == b1->data || b0->slen == 0) return 1;\r
+       for (i=0, n=b0->slen; i < n; i++) {\r
+               if (b0->data[i] != b1->data[i]) {\r
+                       unsigned char c = (unsigned char) downcase (b0->data[i]);\r
+                       if (c != (unsigned char) downcase (b1->data[i])) return 0;\r
+               }\r
+       }\r
+       return 1;\r
+}\r
+\r
+/*  int bisstemeqcaselessblk (const_bstring b0, const void * blk, int len)\r
+ *\r
+ *  Compare beginning of string b0 with a block of memory of length len \r
+ *  without differentiating between case for equality.  If the beginning of b0\r
+ *  differs from the memory block other than in case (or if b0 is too short), \r
+ *  0 is returned, if the strings are the same, 1 is returned, if there is an \r
+ *  error, -1 is returned.  '\0' characters are not treated in any special \r
+ *  way.\r
+ */\r
+int bisstemeqcaselessblk (const_bstring b0, const void * blk, int len) {\r
+int i;\r
+\r
+       if (bdata (b0) == NULL || b0->slen < 0 || NULL == blk || len < 0)\r
+               return BSTR_ERR;\r
+       if (b0->slen < len) return BSTR_OK;\r
+       if (b0->data == (const unsigned char *) blk || len == 0) return 1;\r
+\r
+       for (i = 0; i < len; i ++) {\r
+               if (b0->data[i] != ((const unsigned char *) blk)[i]) {\r
+                       if (downcase (b0->data[i]) != \r
+                           downcase (((const unsigned char *) blk)[i])) return 0;\r
+               }\r
+       }\r
+       return 1;\r
+}\r
+\r
+/*\r
+ * int bltrimws (bstring b)\r
+ *\r
+ * Delete whitespace contiguous from the left end of the string.\r
+ */\r
+int bltrimws (bstring b) {\r
+int i, len;\r
+\r
+       if (b == NULL || b->data == NULL || b->mlen < b->slen ||\r
+           b->slen < 0 || b->mlen <= 0) return BSTR_ERR;\r
+\r
+       for (len = b->slen, i = 0; i < len; i++) {\r
+               if (!wspace (b->data[i])) {\r
+                       return bdelete (b, 0, i);\r
+               }\r
+       }\r
+\r
+       b->data[0] = (unsigned char) '\0';\r
+       b->slen = 0;\r
+       return BSTR_OK;\r
+}\r
+\r
+/*\r
+ * int brtrimws (bstring b)\r
+ *\r
+ * Delete whitespace contiguous from the right end of the string.\r
+ */\r
+int brtrimws (bstring b) {\r
+int i;\r
+\r
+       if (b == NULL || b->data == NULL || b->mlen < b->slen ||\r
+           b->slen < 0 || b->mlen <= 0) return BSTR_ERR;\r
+\r
+       for (i = b->slen - 1; i >= 0; i--) {\r
+               if (!wspace (b->data[i])) {\r
+                       if (b->mlen > i) b->data[i+1] = (unsigned char) '\0';\r
+                       b->slen = i + 1;\r
+                       return BSTR_OK;\r
+               }\r
+       }\r
+\r
+       b->data[0] = (unsigned char) '\0';\r
+       b->slen = 0;\r
+       return BSTR_OK;\r
+}\r
+\r
+/*\r
+ * int btrimws (bstring b)\r
+ *\r
+ * Delete whitespace contiguous from both ends of the string.\r
+ */\r
+int btrimws (bstring b) {\r
+int i, j;\r
+\r
+       if (b == NULL || b->data == NULL || b->mlen < b->slen ||\r
+           b->slen < 0 || b->mlen <= 0) return BSTR_ERR;\r
+\r
+       for (i = b->slen - 1; i >= 0; i--) {\r
+               if (!wspace (b->data[i])) {\r
+                       if (b->mlen > i) b->data[i+1] = (unsigned char) '\0';\r
+                       b->slen = i + 1;\r
+                       for (j = 0; wspace (b->data[j]); j++) {}\r
+                       return bdelete (b, 0, j);\r
+               }\r
+       }\r
+\r
+       b->data[0] = (unsigned char) '\0';\r
+       b->slen = 0;\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  int biseq (const_bstring b0, const_bstring b1)\r
+ *\r
+ *  Compare the string b0 and b1.  If the strings differ, 0 is returned, if \r
+ *  the strings are the same, 1 is returned, if there is an error, -1 is \r
+ *  returned.  If the length of the strings are different, this function is\r
+ *  O(1).  '\0' termination characters are not treated in any special way.\r
+ */\r
+int biseq (const_bstring b0, const_bstring b1) {\r
+       if (b0 == NULL || b1 == NULL || b0->data == NULL || b1->data == NULL ||\r
+               b0->slen < 0 || b1->slen < 0) return BSTR_ERR;\r
+       if (b0->slen != b1->slen) return BSTR_OK;\r
+       if (b0->data == b1->data || b0->slen == 0) return 1;\r
+       return !bstr__memcmp (b0->data, b1->data, b0->slen);\r
+}\r
+\r
+/*  int bisstemeqblk (const_bstring b0, const void * blk, int len)\r
+ *\r
+ *  Compare beginning of string b0 with a block of memory of length len for \r
+ *  equality.  If the beginning of b0 differs from the memory block (or if b0 \r
+ *  is too short), 0 is returned, if the strings are the same, 1 is returned, \r
+ *  if there is an error, -1 is returned.  '\0' characters are not treated in \r
+ *  any special way.\r
+ */\r
+int bisstemeqblk (const_bstring b0, const void * blk, int len) {\r
+int i;\r
+\r
+       if (bdata (b0) == NULL || b0->slen < 0 || NULL == blk || len < 0)\r
+               return BSTR_ERR;\r
+       if (b0->slen < len) return BSTR_OK;\r
+       if (b0->data == (const unsigned char *) blk || len == 0) return 1;\r
+\r
+       for (i = 0; i < len; i ++) {\r
+               if (b0->data[i] != ((const unsigned char *) blk)[i]) return BSTR_OK;\r
+       }\r
+       return 1;\r
+}\r
+\r
+/*  int biseqcstr (const_bstring b, const char *s)\r
+ *\r
+ *  Compare the bstring b and char * string s.  The C string s must be '\0' \r
+ *  terminated at exactly the length of the bstring b, and the contents \r
+ *  between the two must be identical with the bstring b with no '\0' \r
+ *  characters for the two contents to be considered equal.  This is \r
+ *  equivalent to the condition that their current contents will be always be \r
+ *  equal when comparing them in the same format after converting one or the \r
+ *  other.  If the strings are equal 1 is returned, if they are unequal 0 is \r
+ *  returned and if there is a detectable error BSTR_ERR is returned.\r
+ */\r
+int biseqcstr (const_bstring b, const char * s) {\r
+int i;\r
+       if (b == NULL || s == NULL || b->data == NULL || b->slen < 0) return BSTR_ERR;\r
+       for (i=0; i < b->slen; i++) {\r
+               if (s[i] == '\0' || b->data[i] != (unsigned char) s[i]) return BSTR_OK;\r
+       }\r
+       return s[i] == '\0';\r
+}\r
+\r
+/*  int biseqcstrcaseless (const_bstring b, const char *s)\r
+ *\r
+ *  Compare the bstring b and char * string s.  The C string s must be '\0' \r
+ *  terminated at exactly the length of the bstring b, and the contents \r
+ *  between the two must be identical except for case with the bstring b with \r
+ *  no '\0' characters for the two contents to be considered equal.  This is \r
+ *  equivalent to the condition that their current contents will be always be \r
+ *  equal ignoring case when comparing them in the same format after \r
+ *  converting one or the other.  If the strings are equal, except for case, \r
+ *  1 is returned, if they are unequal regardless of case 0 is returned and \r
+ *  if there is a detectable error BSTR_ERR is returned.\r
+ */\r
+int biseqcstrcaseless (const_bstring b, const char * s) {\r
+int i;\r
+       if (b == NULL || s == NULL || b->data == NULL || b->slen < 0) return BSTR_ERR;\r
+       for (i=0; i < b->slen; i++) {\r
+               if (s[i] == '\0' || \r
+                   (b->data[i] != (unsigned char) s[i] && \r
+                    downcase (b->data[i]) != (unsigned char) downcase (s[i])))\r
+                       return BSTR_OK;\r
+       }\r
+       return s[i] == '\0';\r
+}\r
+\r
+/*  int bstrcmp (const_bstring b0, const_bstring b1)\r
+ *\r
+ *  Compare the string b0 and b1.  If there is an error, SHRT_MIN is returned, \r
+ *  otherwise a value less than or greater than zero, indicating that the \r
+ *  string pointed to by b0 is lexicographically less than or greater than \r
+ *  the string pointed to by b1 is returned.  If the the string lengths are \r
+ *  unequal but the characters up until the length of the shorter are equal \r
+ *  then a value less than, or greater than zero, indicating that the string \r
+ *  pointed to by b0 is shorter or longer than the string pointed to by b1 is \r
+ *  returned.  0 is returned if and only if the two strings are the same.  If \r
+ *  the length of the strings are different, this function is O(n).  Like its\r
+ *  standard C library counter part strcmp, the comparison does not proceed \r
+ *  past any '\0' termination characters encountered.\r
+ */\r
+int bstrcmp (const_bstring b0, const_bstring b1) {\r
+int i, v, n;\r
+\r
+       if (b0 == NULL || b1 == NULL || b0->data == NULL || b1->data == NULL ||\r
+               b0->slen < 0 || b1->slen < 0) return SHRT_MIN;\r
+       n = b0->slen; if (n > b1->slen) n = b1->slen;\r
+       if (b0->slen == b1->slen && (b0->data == b1->data || b0->slen == 0))\r
+               return BSTR_OK;\r
+\r
+       for (i = 0; i < n; i ++) {\r
+               v = ((char) b0->data[i]) - ((char) b1->data[i]);\r
+               if (v != 0) return v;\r
+               if (b0->data[i] == (unsigned char) '\0') return BSTR_OK;\r
+       }\r
+\r
+       if (b0->slen > n) return 1;\r
+       if (b1->slen > n) return -1;\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  int bstrncmp (const_bstring b0, const_bstring b1, int n)\r
+ *\r
+ *  Compare the string b0 and b1 for at most n characters.  If there is an \r
+ *  error, SHRT_MIN is returned, otherwise a value is returned as if b0 and \r
+ *  b1 were first truncated to at most n characters then bstrcmp was called\r
+ *  with these new strings are paremeters.  If the length of the strings are \r
+ *  different, this function is O(n).  Like its standard C library counter \r
+ *  part strcmp, the comparison does not proceed past any '\0' termination \r
+ *  characters encountered.\r
+ */\r
+int bstrncmp (const_bstring b0, const_bstring b1, int n) {\r
+int i, v, m;\r
+\r
+       if (b0 == NULL || b1 == NULL || b0->data == NULL || b1->data == NULL ||\r
+               b0->slen < 0 || b1->slen < 0) return SHRT_MIN;\r
+       m = n;\r
+       if (m > b0->slen) m = b0->slen;\r
+       if (m > b1->slen) m = b1->slen;\r
+\r
+       if (b0->data != b1->data) {\r
+               for (i = 0; i < m; i ++) {\r
+                       v = ((char) b0->data[i]) - ((char) b1->data[i]);\r
+                       if (v != 0) return v;\r
+                       if (b0->data[i] == (unsigned char) '\0') return BSTR_OK;\r
+               }\r
+       }\r
+\r
+       if (n == m || b0->slen == b1->slen) return BSTR_OK;\r
+\r
+       if (b0->slen > m) return 1;\r
+       return -1;\r
+}\r
+\r
+/*  bstring bmidstr (const_bstring b, int left, int len)\r
+ *\r
+ *  Create a bstring which is the substring of b starting from position left\r
+ *  and running for a length len (clamped by the end of the bstring b.)  If\r
+ *  b is detectably invalid, then NULL is returned.  The section described \r
+ *  by (left, len) is clamped to the boundaries of b.\r
+ */\r
+bstring bmidstr (const_bstring b, int left, int len) {\r
+\r
+       if (b == NULL || b->slen < 0 || b->data == NULL) return NULL;\r
+\r
+       if (left < 0) {\r
+               len += left;\r
+               left = 0;\r
+       }\r
+\r
+       if (len > b->slen - left) len = b->slen - left;\r
+\r
+       if (len <= 0) return bfromcstr ("");\r
+       return blk2bstr (b->data + left, len);\r
+}\r
+\r
+/*  int bdelete (bstring b, int pos, int len)\r
+ *\r
+ *  Removes characters from pos to pos+len-1 inclusive and shifts the tail of \r
+ *  the bstring starting from pos+len to pos.  len must be positive for this \r
+ *  call to have any effect.  The section of the string described by (pos, \r
+ *  len) is clamped to boundaries of the bstring b.\r
+ */\r
+int bdelete (bstring b, int pos, int len) {\r
+       /* Clamp to left side of bstring */\r
+       if (pos < 0) {\r
+               len += pos;\r
+               pos = 0;\r
+       }\r
+\r
+       if (len < 0 || b == NULL || b->data == NULL || b->slen < 0 || \r
+           b->mlen < b->slen || b->mlen <= 0) \r
+               return BSTR_ERR;\r
+       if (len > 0 && pos < b->slen) {\r
+               if (pos + len >= b->slen) {\r
+                       b->slen = pos;\r
+               } else {\r
+                       bBlockCopy ((char *) (b->data + pos),\r
+                                   (char *) (b->data + pos + len), \r
+                                   b->slen - (pos+len));\r
+                       b->slen -= len;\r
+               }\r
+               b->data[b->slen] = (unsigned char) '\0';\r
+       }\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  int bdestroy (bstring b)\r
+ *\r
+ *  Free up the bstring.  Note that if b is detectably invalid or not writable\r
+ *  then no action is performed and BSTR_ERR is returned.  Like a freed memory\r
+ *  allocation, dereferences, writes or any other action on b after it has \r
+ *  been bdestroyed is undefined.\r
+ */\r
+int bdestroy (bstring b) {\r
+       if (b == NULL || b->slen < 0 || b->mlen <= 0 || b->mlen < b->slen ||\r
+           b->data == NULL)\r
+               return BSTR_ERR;\r
+\r
+       bstr__free (b->data);\r
+\r
+       /* In case there is any stale usage, there is one more chance to \r
+          notice this error. */\r
+\r
+       b->slen = -1;\r
+       b->mlen = -__LINE__;\r
+       b->data = NULL;\r
+\r
+       bstr__free (b);\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  int binstr (const_bstring b1, int pos, const_bstring b2)\r
+ *\r
+ *  Search for the bstring b2 in b1 starting from position pos, and searching \r
+ *  forward.  If it is found then return with the first position where it is \r
+ *  found, otherwise return BSTR_ERR.  Note that this is just a brute force \r
+ *  string searcher that does not attempt clever things like the Boyer-Moore \r
+ *  search algorithm.  Because of this there are many degenerate cases where \r
+ *  this can take much longer than it needs to.\r
+ */\r
+int binstr (const_bstring b1, int pos, const_bstring b2) {\r
+int j, ii, ll, lf;\r
+unsigned char * d0;\r
+unsigned char c0;\r
+register unsigned char * d1;\r
+register unsigned char c1;\r
+register int i;\r
+\r
+       if (b1 == NULL || b1->data == NULL || b1->slen < 0 ||\r
+           b2 == NULL || b2->data == NULL || b2->slen < 0) return BSTR_ERR;\r
+       if (b1->slen == pos) return (b2->slen == 0)?pos:BSTR_ERR;\r
+       if (b1->slen < pos || pos < 0) return BSTR_ERR;\r
+       if (b2->slen == 0) return pos;\r
+\r
+       /* No space to find such a string? */\r
+       if ((lf = b1->slen - b2->slen + 1) <= pos) return BSTR_ERR;\r
+\r
+       /* An obvious alias case */\r
+       if (b1->data == b2->data && pos == 0) return 0;\r
+\r
+       i = pos;\r
+\r
+       d0 = b2->data;\r
+       d1 = b1->data;\r
+       ll = b2->slen;\r
+\r
+       /* Peel off the b2->slen == 1 case */\r
+       c0 = d0[0];\r
+       if (1 == ll) {\r
+               for (;i < lf; i++) if (c0 == d1[i]) return i;\r
+               return BSTR_ERR;\r
+       }\r
+\r
+       c1 = c0;\r
+       j = 0;\r
+       lf = b1->slen - 1;\r
+\r
+       ii = -1;\r
+       if (i < lf) do {\r
+               /* Unrolled current character test */\r
+               if (c1 != d1[i]) {\r
+                       if (c1 != d1[1+i]) {\r
+                               i += 2;\r
+                               continue;\r
+                       }\r
+                       i++;\r
+               }\r
+\r
+               /* Take note if this is the start of a potential match */\r
+               if (0 == j) ii = i;\r
+\r
+               /* Shift the test character down by one */\r
+               j++;\r
+               i++;\r
+\r
+               /* If this isn't past the last character continue */\r
+               if (j < ll) {\r
+                       c1 = d0[j];\r
+                       continue;\r
+               }\r
+\r
+               N0:;\r
+\r
+               /* If no characters mismatched, then we matched */\r
+               if (i == ii+j) return ii;\r
+\r
+               /* Shift back to the beginning */\r
+               i -= j;\r
+               j  = 0;\r
+               c1 = c0;\r
+       } while (i < lf);\r
+\r
+       /* Deal with last case if unrolling caused a misalignment */\r
+       if (i == lf && ll == j+1 && c1 == d1[i]) goto N0;\r
+\r
+       return BSTR_ERR;\r
+}\r
+\r
+/*  int binstrr (const_bstring b1, int pos, const_bstring b2)\r
+ *\r
+ *  Search for the bstring b2 in b1 starting from position pos, and searching \r
+ *  backward.  If it is found then return with the first position where it is \r
+ *  found, otherwise return BSTR_ERR.  Note that this is just a brute force \r
+ *  string searcher that does not attempt clever things like the Boyer-Moore \r
+ *  search algorithm.  Because of this there are many degenerate cases where \r
+ *  this can take much longer than it needs to.\r
+ */\r
+int binstrr (const_bstring b1, int pos, const_bstring b2) {\r
+int j, i, l;\r
+unsigned char * d0, * d1;\r
+\r
+       if (b1 == NULL || b1->data == NULL || b1->slen < 0 ||\r
+           b2 == NULL || b2->data == NULL || b2->slen < 0) return BSTR_ERR;\r
+       if (b1->slen == pos && b2->slen == 0) return pos;\r
+       if (b1->slen < pos || pos < 0) return BSTR_ERR;\r
+       if (b2->slen == 0) return pos;\r
+\r
+       /* Obvious alias case */\r
+       if (b1->data == b2->data && pos == 0 && b2->slen <= b1->slen) return 0;\r
+\r
+       i = pos;\r
+       if ((l = b1->slen - b2->slen) < 0) return BSTR_ERR;\r
+\r
+       /* If no space to find such a string then snap back */\r
+       if (l + 1 <= i) i = l;\r
+       j = 0;\r
+\r
+       d0 = b2->data;\r
+       d1 = b1->data;\r
+       l  = b2->slen;\r
+\r
+       for (;;) {\r
+               if (d0[j] == d1[i + j]) {\r
+                       j ++;\r
+                       if (j >= l) return i;\r
+               } else {\r
+                       i --;\r
+                       if (i < 0) break;\r
+                       j=0;\r
+               }\r
+       }\r
+\r
+       return BSTR_ERR;\r
+}\r
+\r
+/*  int binstrcaseless (const_bstring b1, int pos, const_bstring b2)\r
+ *\r
+ *  Search for the bstring b2 in b1 starting from position pos, and searching \r
+ *  forward but without regard to case.  If it is found then return with the \r
+ *  first position where it is found, otherwise return BSTR_ERR.  Note that \r
+ *  this is just a brute force string searcher that does not attempt clever \r
+ *  things like the Boyer-Moore search algorithm.  Because of this there are \r
+ *  many degenerate cases where this can take much longer than it needs to.\r
+ */\r
+int binstrcaseless (const_bstring b1, int pos, const_bstring b2) {\r
+int j, i, l, ll;\r
+unsigned char * d0, * d1;\r
+\r
+       if (b1 == NULL || b1->data == NULL || b1->slen < 0 ||\r
+           b2 == NULL || b2->data == NULL || b2->slen < 0) return BSTR_ERR;\r
+       if (b1->slen == pos) return (b2->slen == 0)?pos:BSTR_ERR;\r
+       if (b1->slen < pos || pos < 0) return BSTR_ERR;\r
+       if (b2->slen == 0) return pos;\r
+\r
+       l = b1->slen - b2->slen + 1;\r
+\r
+       /* No space to find such a string? */\r
+       if (l <= pos) return BSTR_ERR;\r
+\r
+       /* An obvious alias case */\r
+       if (b1->data == b2->data && pos == 0) return BSTR_OK;\r
+\r
+       i = pos;\r
+       j = 0;\r
+\r
+       d0 = b2->data;\r
+       d1 = b1->data;\r
+       ll = b2->slen;\r
+\r
+       for (;;) {\r
+               if (d0[j] == d1[i + j] || downcase (d0[j]) == downcase (d1[i + j])) {\r
+                       j ++;\r
+                       if (j >= ll) return i;\r
+               } else {\r
+                       i ++;\r
+                       if (i >= l) break;\r
+                       j=0;\r
+               }\r
+       }\r
+\r
+       return BSTR_ERR;\r
+}\r
+\r
+/*  int binstrrcaseless (const_bstring b1, int pos, const_bstring b2)\r
+ *\r
+ *  Search for the bstring b2 in b1 starting from position pos, and searching \r
+ *  backward but without regard to case.  If it is found then return with the \r
+ *  first position where it is found, otherwise return BSTR_ERR.  Note that \r
+ *  this is just a brute force string searcher that does not attempt clever \r
+ *  things like the Boyer-Moore search algorithm.  Because of this there are \r
+ *  many degenerate cases where this can take much longer than it needs to.\r
+ */\r
+int binstrrcaseless (const_bstring b1, int pos, const_bstring b2) {\r
+int j, i, l;\r
+unsigned char * d0, * d1;\r
+\r
+       if (b1 == NULL || b1->data == NULL || b1->slen < 0 ||\r
+           b2 == NULL || b2->data == NULL || b2->slen < 0) return BSTR_ERR;\r
+       if (b1->slen == pos && b2->slen == 0) return pos;\r
+       if (b1->slen < pos || pos < 0) return BSTR_ERR;\r
+       if (b2->slen == 0) return pos;\r
+\r
+       /* Obvious alias case */\r
+       if (b1->data == b2->data && pos == 0 && b2->slen <= b1->slen) return BSTR_OK;\r
+\r
+       i = pos;\r
+       if ((l = b1->slen - b2->slen) < 0) return BSTR_ERR;\r
+\r
+       /* If no space to find such a string then snap back */\r
+       if (l + 1 <= i) i = l;\r
+       j = 0;\r
+\r
+       d0 = b2->data;\r
+       d1 = b1->data;\r
+       l  = b2->slen;\r
+\r
+       for (;;) {\r
+               if (d0[j] == d1[i + j] || downcase (d0[j]) == downcase (d1[i + j])) {\r
+                       j ++;\r
+                       if (j >= l) return i;\r
+               } else {\r
+                       i --;\r
+                       if (i < 0) break;\r
+                       j=0;\r
+               }\r
+       }\r
+\r
+       return BSTR_ERR;\r
+}\r
+\r
+\r
+/*  int bstrchrp (const_bstring b, int c, int pos)\r
+ *\r
+ *  Search for the character c in b forwards from the position pos \r
+ *  (inclusive).\r
+ */\r
+int bstrchrp (const_bstring b, int c, int pos) {\r
+unsigned char * p;\r
+\r
+       if (b == NULL || b->data == NULL || b->slen <= pos || pos < 0) return BSTR_ERR;\r
+       p = (unsigned char *) bstr__memchr ((b->data + pos), (unsigned char) c, (b->slen - pos));\r
+       if (p) return (int) (p - b->data);\r
+       return BSTR_ERR;\r
+}\r
+\r
+/*  int bstrrchrp (const_bstring b, int c, int pos)\r
+ *\r
+ *  Search for the character c in b backwards from the position pos in string \r
+ *  (inclusive).\r
+ */\r
+int bstrrchrp (const_bstring b, int c, int pos) {\r
+int i;\r
\r
+       if (b == NULL || b->data == NULL || b->slen <= pos || pos < 0) return BSTR_ERR;\r
+       for (i=pos; i >= 0; i--) {\r
+               if (b->data[i] == (unsigned char) c) return i;\r
+       }\r
+       return BSTR_ERR;\r
+}\r
+\r
+#if !defined (BSTRLIB_AGGRESSIVE_MEMORY_FOR_SPEED_TRADEOFF)\r
+#define LONG_LOG_BITS_QTY (3)\r
+#define LONG_BITS_QTY (1 << LONG_LOG_BITS_QTY)\r
+#define LONG_TYPE unsigned char\r
+\r
+#define CFCLEN ((1 << CHAR_BIT) / LONG_BITS_QTY)\r
+struct charField { LONG_TYPE content[CFCLEN]; };\r
+#define testInCharField(cf,c) ((cf)->content[(c) >> LONG_LOG_BITS_QTY] & (((long)1) << ((c) & (LONG_BITS_QTY-1))))\r
+#define setInCharField(cf,idx) { \\r
+       unsigned int c = (unsigned int) (idx); \\r
+       (cf)->content[c >> LONG_LOG_BITS_QTY] |= (LONG_TYPE) (1ul << (c & (LONG_BITS_QTY-1))); \\r
+}\r
+\r
+#else\r
+\r
+#define CFCLEN (1 << CHAR_BIT)\r
+struct charField { unsigned char content[CFCLEN]; };\r
+#define testInCharField(cf,c) ((cf)->content[(unsigned char) (c)])\r
+#define setInCharField(cf,idx) (cf)->content[(unsigned int) (idx)] = ~0\r
+\r
+#endif\r
+\r
+/* Convert a bstring to charField */\r
+static int buildCharField (struct charField * cf, const_bstring b) {\r
+int i;\r
+       if (b == NULL || b->data == NULL || b->slen <= 0) return BSTR_ERR;\r
+       memset ((void *) cf->content, 0, sizeof (struct charField));\r
+       for (i=0; i < b->slen; i++) {\r
+               setInCharField (cf, b->data[i]);\r
+       }\r
+       return BSTR_OK;\r
+}\r
+\r
+static void invertCharField (struct charField * cf) {\r
+int i;\r
+       for (i=0; i < CFCLEN; i++) cf->content[i] = ~cf->content[i];\r
+}\r
+\r
+/* Inner engine for binchr */\r
+static int binchrCF (const unsigned char * data, int len, int pos, const struct charField * cf) {\r
+int i;\r
+       for (i=pos; i < len; i++) {\r
+               unsigned char c = (unsigned char) data[i];\r
+               if (testInCharField (cf, c)) return i;\r
+       }\r
+       return BSTR_ERR;\r
+}\r
+\r
+/*  int binchr (const_bstring b0, int pos, const_bstring b1);\r
+ *\r
+ *  Search for the first position in b0 starting from pos or after, in which \r
+ *  one of the characters in b1 is found and return it.  If such a position \r
+ *  does not exist in b0, then BSTR_ERR is returned.\r
+ */\r
+int binchr (const_bstring b0, int pos, const_bstring b1) {\r
+struct charField chrs;\r
+       if (pos < 0 || b0 == NULL || b0->data == NULL ||\r
+           b0->slen <= pos) return BSTR_ERR;\r
+       if (1 == b1->slen) return bstrchrp (b0, b1->data[0], pos);\r
+       if (0 > buildCharField (&chrs, b1)) return BSTR_ERR;\r
+       return binchrCF (b0->data, b0->slen, pos, &chrs);\r
+}\r
+\r
+/* Inner engine for binchrr */\r
+static int binchrrCF (const unsigned char * data, int pos, const struct charField * cf) {\r
+int i;\r
+       for (i=pos; i >= 0; i--) {\r
+               unsigned int c = (unsigned int) data[i];\r
+               if (testInCharField (cf, c)) return i;\r
+       }\r
+       return BSTR_ERR;\r
+}\r
+\r
+/*  int binchrr (const_bstring b0, int pos, const_bstring b1);\r
+ *\r
+ *  Search for the last position in b0 no greater than pos, in which one of \r
+ *  the characters in b1 is found and return it.  If such a position does not \r
+ *  exist in b0, then BSTR_ERR is returned.\r
+ */\r
+int binchrr (const_bstring b0, int pos, const_bstring b1) {\r
+struct charField chrs;\r
+       if (pos < 0 || b0 == NULL || b0->data == NULL || b1 == NULL ||\r
+           b0->slen < pos) return BSTR_ERR;\r
+       if (pos == b0->slen) pos--;\r
+       if (1 == b1->slen) return bstrrchrp (b0, b1->data[0], pos);\r
+       if (0 > buildCharField (&chrs, b1)) return BSTR_ERR;\r
+       return binchrrCF (b0->data, pos, &chrs);\r
+}\r
+\r
+/*  int bninchr (const_bstring b0, int pos, const_bstring b1);\r
+ *\r
+ *  Search for the first position in b0 starting from pos or after, in which \r
+ *  none of the characters in b1 is found and return it.  If such a position \r
+ *  does not exist in b0, then BSTR_ERR is returned.\r
+ */\r
+int bninchr (const_bstring b0, int pos, const_bstring b1) {\r
+struct charField chrs;\r
+       if (pos < 0 || b0 == NULL || b0->data == NULL || \r
+           b0->slen <= pos) return BSTR_ERR;\r
+       if (buildCharField (&chrs, b1) < 0) return BSTR_ERR;\r
+       invertCharField (&chrs);\r
+       return binchrCF (b0->data, b0->slen, pos, &chrs);\r
+}\r
+\r
+/*  int bninchrr (const_bstring b0, int pos, const_bstring b1);\r
+ *\r
+ *  Search for the last position in b0 no greater than pos, in which none of \r
+ *  the characters in b1 is found and return it.  If such a position does not \r
+ *  exist in b0, then BSTR_ERR is returned.\r
+ */\r
+int bninchrr (const_bstring b0, int pos, const_bstring b1) {\r
+struct charField chrs;\r
+       if (pos < 0 || b0 == NULL || b0->data == NULL || \r
+           b0->slen < pos) return BSTR_ERR;\r
+       if (pos == b0->slen) pos--;\r
+       if (buildCharField (&chrs, b1) < 0) return BSTR_ERR;\r
+       invertCharField (&chrs);\r
+       return binchrrCF (b0->data, pos, &chrs);\r
+}\r
+\r
+/*  int bsetstr (bstring b0, int pos, bstring b1, unsigned char fill)\r
+ *\r
+ *  Overwrite the string b0 starting at position pos with the string b1. If \r
+ *  the position pos is past the end of b0, then the character "fill" is \r
+ *  appended as necessary to make up the gap between the end of b0 and pos.\r
+ *  If b1 is NULL, it behaves as if it were a 0-length string.\r
+ */\r
+int bsetstr (bstring b0, int pos, const_bstring b1, unsigned char fill) {\r
+int d, newlen;\r
+ptrdiff_t pd;\r
+bstring aux = (bstring) b1;\r
+\r
+       if (pos < 0 || b0 == NULL || b0->slen < 0 || NULL == b0->data || \r
+           b0->mlen < b0->slen || b0->mlen <= 0) return BSTR_ERR;\r
+       if (b1 != NULL && (b1->slen < 0 || b1->data == NULL)) return BSTR_ERR;\r
+\r
+       d = pos;\r
+\r
+       /* Aliasing case */\r
+       if (NULL != aux) {\r
+               if ((pd = (ptrdiff_t) (b1->data - b0->data)) >= 0 && pd < (ptrdiff_t) b0->mlen) {\r
+                       if (NULL == (aux = bstrcpy (b1))) return BSTR_ERR;\r
+               }\r
+               d += aux->slen;\r
+       }\r
+\r
+       /* Increase memory size if necessary */\r
+       if (balloc (b0, d + 1) != BSTR_OK) {\r
+               if (aux != b1) bdestroy (aux);\r
+               return BSTR_ERR;\r
+       }\r
+\r
+       newlen = b0->slen;\r
+\r
+       /* Fill in "fill" character as necessary */\r
+       if (pos > newlen) {\r
+               bstr__memset (b0->data + b0->slen, (int) fill, (size_t) (pos - b0->slen));\r
+               newlen = pos;\r
+       }\r
+\r
+       /* Copy b1 to position pos in b0. */\r
+       if (aux != NULL) {\r
+               bBlockCopy ((char *) (b0->data + pos), (char *) aux->data, aux->slen);\r
+               if (aux != b1) bdestroy (aux);\r
+       }\r
+\r
+       /* Indicate the potentially increased size of b0 */\r
+       if (d > newlen) newlen = d;\r
+\r
+       b0->slen = newlen;\r
+       b0->data[newlen] = (unsigned char) '\0';\r
+\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  int binsert (bstring b1, int pos, bstring b2, unsigned char fill)\r
+ *\r
+ *  Inserts the string b2 into b1 at position pos.  If the position pos is \r
+ *  past the end of b1, then the character "fill" is appended as necessary to \r
+ *  make up the gap between the end of b1 and pos.  Unlike bsetstr, binsert\r
+ *  does not allow b2 to be NULL.\r
+ */\r
+int binsert (bstring b1, int pos, const_bstring b2, unsigned char fill) {\r
+int d, l;\r
+ptrdiff_t pd;\r
+bstring aux = (bstring) b2;\r
+\r
+       if (pos < 0 || b1 == NULL || b2 == NULL || b1->slen < 0 || \r
+           b2->slen < 0 || b1->mlen < b1->slen || b1->mlen <= 0) return BSTR_ERR;\r
+\r
+       /* Aliasing case */\r
+       if ((pd = (ptrdiff_t) (b2->data - b1->data)) >= 0 && pd < (ptrdiff_t) b1->mlen) {\r
+               if (NULL == (aux = bstrcpy (b2))) return BSTR_ERR;\r
+       }\r
+\r
+       /* Compute the two possible end pointers */\r
+       d = b1->slen + aux->slen;\r
+       l = pos + aux->slen;\r
+       if ((d|l) < 0) return BSTR_ERR;\r
+\r
+       if (l > d) {\r
+               /* Inserting past the end of the string */\r
+               if (balloc (b1, l + 1) != BSTR_OK) {\r
+                       if (aux != b2) bdestroy (aux);\r
+                       return BSTR_ERR;\r
+               }\r
+               bstr__memset (b1->data + b1->slen, (int) fill, (size_t) (pos - b1->slen));\r
+               b1->slen = l;\r
+       } else {\r
+               /* Inserting in the middle of the string */\r
+               if (balloc (b1, d + 1) != BSTR_OK) {\r
+                       if (aux != b2) bdestroy (aux);\r
+                       return BSTR_ERR;\r
+               }\r
+               bBlockCopy (b1->data + l, b1->data + pos, d - l);\r
+               b1->slen = d;\r
+       }\r
+       bBlockCopy (b1->data + pos, aux->data, aux->slen);\r
+       b1->data[b1->slen] = (unsigned char) '\0';\r
+       if (aux != b2) bdestroy (aux);\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  int breplace (bstring b1, int pos, int len, bstring b2, \r
+ *                unsigned char fill)\r
+ *\r
+ *  Replace a section of a string from pos for a length len with the string b2.\r
+ *  fill is used is pos > b1->slen.\r
+ */\r
+int breplace (bstring b1, int pos, int len, const_bstring b2, \r
+                         unsigned char fill) {\r
+int pl, ret;\r
+ptrdiff_t pd;\r
+bstring aux = (bstring) b2;\r
+\r
+       if (pos < 0 || len < 0 || (pl = pos + len) < 0 || b1 == NULL || \r
+           b2 == NULL || b1->data == NULL || b2->data == NULL || \r
+           b1->slen < 0 || b2->slen < 0 || b1->mlen < b1->slen ||\r
+           b1->mlen <= 0) return BSTR_ERR;\r
+\r
+       /* Straddles the end? */\r
+       if (pl >= b1->slen) {\r
+               if ((ret = bsetstr (b1, pos, b2, fill)) < 0) return ret;\r
+               if (pos + b2->slen < b1->slen) {\r
+                       b1->slen = pos + b2->slen;\r
+                       b1->data[b1->slen] = (unsigned char) '\0';\r
+               }\r
+               return ret;\r
+       }\r
+\r
+       /* Aliasing case */\r
+       if ((pd = (ptrdiff_t) (b2->data - b1->data)) >= 0 && pd < (ptrdiff_t) b1->slen) {\r
+               if (NULL == (aux = bstrcpy (b2))) return BSTR_ERR;\r
+       }\r
+\r
+       if (aux->slen > len) {\r
+               if (balloc (b1, b1->slen + aux->slen - len) != BSTR_OK) {\r
+                       if (aux != b2) bdestroy (aux);\r
+                       return BSTR_ERR;\r
+               }\r
+       }\r
+\r
+       if (aux->slen != len) bstr__memmove (b1->data + pos + aux->slen, b1->data + pos + len, b1->slen - (pos + len));\r
+       bstr__memcpy (b1->data + pos, aux->data, aux->slen);\r
+       b1->slen += aux->slen - len;\r
+       b1->data[b1->slen] = (unsigned char) '\0';\r
+       if (aux != b2) bdestroy (aux);\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  int bfindreplace (bstring b, const_bstring find, const_bstring repl, \r
+ *                    int pos)\r
+ *\r
+ *  Replace all occurrences of a find string with a replace string after a\r
+ *  given point in a bstring.\r
+ */\r
+\r
+typedef int (*instr_fnptr) (const_bstring s1, int pos, const_bstring s2);\r
+\r
+static int findreplaceengine (bstring b, const_bstring find, const_bstring repl, int pos, instr_fnptr instr) {\r
+int i, ret, slen, mlen, delta, acc;\r
+int * d;\r
+int static_d[32];\r
+ptrdiff_t pd;\r
+bstring auxf = (bstring) find;\r
+bstring auxr = (bstring) repl;\r
+\r
+       if (b == NULL || b->data == NULL || find == NULL ||\r
+           find->data == NULL || repl == NULL || repl->data == NULL || \r
+           pos < 0 || find->slen <= 0 || b->mlen < 0 || b->slen > b->mlen || \r
+           b->mlen <= 0 || b->slen < 0 || repl->slen < 0) return BSTR_ERR;\r
+       if (pos > b->slen - find->slen) return BSTR_OK;\r
+\r
+       /* Alias with find string */\r
+       pd = (ptrdiff_t) (find->data - b->data);\r
+       if ((ptrdiff_t) (pos - find->slen) < pd && pd < (ptrdiff_t) b->slen) {\r
+               if (NULL == (auxf = bstrcpy (find))) return BSTR_ERR;\r
+       }\r
+\r
+       /* Alias with repl string */\r
+       pd = (ptrdiff_t) (repl->data - b->data);\r
+       if ((ptrdiff_t) (pos - repl->slen) < pd && pd < (ptrdiff_t) b->slen) {\r
+               if (NULL == (auxr = bstrcpy (repl))) {\r
+                       if (auxf != find) bdestroy (auxf);\r
+                       return BSTR_ERR;\r
+               }\r
+       }\r
+\r
+       delta = auxf->slen - auxr->slen;\r
+\r
+       /* in-place replacement since find and replace strings are of equal \r
+          length */\r
+       if (delta == 0) {\r
+               while ((pos = instr (b, pos, auxf)) >= 0) {\r
+                       bstr__memcpy (b->data + pos, auxr->data, auxr->slen);\r
+                       pos += auxf->slen;\r
+               }\r
+               if (auxf != find) bdestroy (auxf);\r
+               if (auxr != repl) bdestroy (auxr);\r
+               return BSTR_OK;\r
+       }\r
+\r
+       /* shrinking replacement since auxf->slen > auxr->slen */\r
+       if (delta > 0) {\r
+               acc = 0;\r
+\r
+               while ((i = instr (b, pos, auxf)) >= 0) {\r
+                       if (acc && i > pos)\r
+                               bstr__memmove (b->data + pos - acc, b->data + pos, i - pos);\r
+                       if (auxr->slen)\r
+                               bstr__memcpy (b->data + i - acc, auxr->data, auxr->slen);\r
+                       acc += delta;\r
+                       pos = i + auxf->slen;\r
+               }\r
+\r
+               if (acc) {\r
+                       i = b->slen;\r
+                       if (i > pos)\r
+                               bstr__memmove (b->data + pos - acc, b->data + pos, i - pos);\r
+                       b->slen -= acc;\r
+                       b->data[b->slen] = (unsigned char) '\0';\r
+               }\r
+\r
+               if (auxf != find) bdestroy (auxf);\r
+               if (auxr != repl) bdestroy (auxr);\r
+               return BSTR_OK;\r
+       }\r
+\r
+       /* expanding replacement since find->slen < repl->slen.  Its a lot \r
+          more complicated. */\r
+\r
+       mlen = 32;\r
+       d = (int *) static_d; /* Avoid malloc for trivial cases */\r
+       acc = slen = 0;\r
+\r
+       while ((pos = instr (b, pos, auxf)) >= 0) {\r
+               if (slen + 1 >= mlen) {\r
+                       int sl;\r
+                       int * t;\r
+                       mlen += mlen;\r
+                       sl = sizeof (int *) * mlen;\r
+                       if (static_d == d) d = NULL;\r
+                       if (sl < mlen || NULL == (t = (int *) bstr__realloc (d, sl))) {\r
+                               ret = BSTR_ERR;\r
+                               goto done;\r
+                       }\r
+                       if (NULL == d) bstr__memcpy (t, static_d, sizeof (static_d));\r
+                       d = t;\r
+               }\r
+               d[slen] = pos;\r
+               slen++;\r
+               acc -= delta;\r
+               pos += auxf->slen;\r
+               if (pos < 0 || acc < 0) {\r
+                       ret = BSTR_ERR;\r
+                       goto done;\r
+               }\r
+       }\r
+       d[slen] = b->slen;\r
+\r
+       if (BSTR_OK == (ret = balloc (b, b->slen + acc + 1))) {\r
+               b->slen += acc;\r
+               for (i = slen-1; i >= 0; i--) {\r
+                       int s, l;\r
+                       s = d[i] + auxf->slen;\r
+                       l = d[i+1] - s;\r
+                       if (l) {\r
+                               bstr__memmove (b->data + s + acc, b->data + s, l);\r
+                       }\r
+                       if (auxr->slen) {\r
+                               bstr__memmove (b->data + s + acc - auxr->slen, \r
+                                        auxr->data, auxr->slen);\r
+                       }\r
+                       acc += delta;           \r
+               }\r
+               b->data[b->slen] = (unsigned char) '\0';\r
+       }\r
+\r
+       done:;\r
+       if (static_d == d) d = NULL;\r
+       bstr__free (d);\r
+       if (auxf != find) bdestroy (auxf);\r
+       if (auxr != repl) bdestroy (auxr);\r
+       return ret;\r
+}\r
+\r
+/*  int bfindreplace (bstring b, const_bstring find, const_bstring repl, \r
+ *                    int pos)\r
+ *\r
+ *  Replace all occurrences of a find string with a replace string after a\r
+ *  given point in a bstring.\r
+ */\r
+int bfindreplace (bstring b, const_bstring find, const_bstring repl, int pos) {\r
+       return findreplaceengine (b, find, repl, pos, binstr);\r
+}\r
+\r
+/*  int bfindreplacecaseless (bstring b, const_bstring find, const_bstring repl, \r
+ *                    int pos)\r
+ *\r
+ *  Replace all occurrences of a find string, ignoring case, with a replace \r
+ *  string after a given point in a bstring.\r
+ */\r
+int bfindreplacecaseless (bstring b, const_bstring find, const_bstring repl, int pos) {\r
+       return findreplaceengine (b, find, repl, pos, binstrcaseless);\r
+}\r
+\r
+/*  int binsertch (bstring b, int pos, int len, unsigned char fill)\r
+ *\r
+ *  Inserts the character fill repeatedly into b at position pos for a \r
+ *  length len.  If the position pos is past the end of b, then the \r
+ *  character "fill" is appended as necessary to make up the gap between the \r
+ *  end of b and the position pos + len.\r
+ */\r
+int binsertch (bstring b, int pos, int len, unsigned char fill) {\r
+int d, l, i;\r
+\r
+       if (pos < 0 || b == NULL || b->slen < 0 || b->mlen < b->slen ||\r
+           b->mlen <= 0 || len < 0) return BSTR_ERR;\r
+\r
+       /* Compute the two possible end pointers */\r
+       d = b->slen + len;\r
+       l = pos + len;\r
+       if ((d|l) < 0) return BSTR_ERR;\r
+\r
+       if (l > d) {\r
+               /* Inserting past the end of the string */\r
+               if (balloc (b, l + 1) != BSTR_OK) return BSTR_ERR;\r
+               pos = b->slen;\r
+               b->slen = l;\r
+       } else {\r
+               /* Inserting in the middle of the string */\r
+               if (balloc (b, d + 1) != BSTR_OK) return BSTR_ERR;\r
+               for (i = d - 1; i >= l; i--) {\r
+                       b->data[i] = b->data[i - len];\r
+               }\r
+               b->slen = d;\r
+       }\r
+\r
+       for (i=pos; i < l; i++) b->data[i] = fill;\r
+       b->data[b->slen] = (unsigned char) '\0';\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  int bpattern (bstring b, int len)\r
+ *\r
+ *  Replicate the bstring, b in place, end to end repeatedly until it \r
+ *  surpasses len characters, then chop the result to exactly len characters. \r
+ *  This function operates in-place.  The function will return with BSTR_ERR \r
+ *  if b is NULL or of length 0, otherwise BSTR_OK is returned.\r
+ */\r
+int bpattern (bstring b, int len) {\r
+int i, d;\r
+\r
+       d = blength (b);\r
+       if (d <= 0 || len < 0 || balloc (b, len + 1) != BSTR_OK) return BSTR_ERR;\r
+       if (len > 0) {\r
+               if (d == 1) return bsetstr (b, len, NULL, b->data[0]);\r
+               for (i = d; i < len; i++) b->data[i] = b->data[i - d];\r
+       }\r
+       b->data[len] = (unsigned char) '\0';\r
+       b->slen = len;\r
+       return BSTR_OK;\r
+}\r
+\r
+#define BS_BUFF_SZ (1024)\r
+\r
+/*  int breada (bstring b, bNread readPtr, void * parm)\r
+ *\r
+ *  Use a finite buffer fread-like function readPtr to concatenate to the \r
+ *  bstring b the entire contents of file-like source data in a roughly \r
+ *  efficient way.\r
+ */\r
+int breada (bstring b, bNread readPtr, void * parm) {\r
+int i, l, n;\r
+\r
+       if (b == NULL || b->mlen <= 0 || b->slen < 0 || b->mlen < b->slen ||\r
+           b->mlen <= 0 || readPtr == NULL) return BSTR_ERR;\r
+\r
+       i = b->slen;\r
+       for (n=i+16; ; n += ((n < BS_BUFF_SZ) ? n : BS_BUFF_SZ)) {\r
+               if (BSTR_OK != balloc (b, n + 1)) return BSTR_ERR;\r
+               l = (int) readPtr ((void *) (b->data + i), 1, n - i, parm);\r
+               i += l;\r
+               b->slen = i;\r
+               if (i < n) break;\r
+       }\r
+\r
+       b->data[i] = (unsigned char) '\0';\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  bstring bread (bNread readPtr, void * parm)\r
+ *\r
+ *  Use a finite buffer fread-like function readPtr to create a bstring \r
+ *  filled with the entire contents of file-like source data in a roughly \r
+ *  efficient way.\r
+ */\r
+bstring bread (bNread readPtr, void * parm) {\r
+bstring buff;\r
+\r
+       if (0 > breada (buff = bfromcstr (""), readPtr, parm)) {\r
+               bdestroy (buff);\r
+               return NULL;\r
+       }\r
+       return buff;\r
+}\r
+\r
+/*  int bassigngets (bstring b, bNgetc getcPtr, void * parm, char terminator)\r
+ *\r
+ *  Use an fgetc-like single character stream reading function (getcPtr) to \r
+ *  obtain a sequence of characters which are concatenated to the end of the\r
+ *  bstring b.  The stream read is terminated by the passed in terminator \r
+ *  parameter.\r
+ *\r
+ *  If getcPtr returns with a negative number, or the terminator character \r
+ *  (which is appended) is read, then the stream reading is halted and the \r
+ *  function returns with a partial result in b.  If there is an empty partial\r
+ *  result, 1 is returned.  If no characters are read, or there is some other \r
+ *  detectable error, BSTR_ERR is returned.\r
+ */\r
+int bassigngets (bstring b, bNgetc getcPtr, void * parm, char terminator) {\r
+int c, d, e;\r
+\r
+       if (b == NULL || b->mlen <= 0 || b->slen < 0 || b->mlen < b->slen ||\r
+           b->mlen <= 0 || getcPtr == NULL) return BSTR_ERR;\r
+       d = 0;\r
+       e = b->mlen - 2;\r
+\r
+       while ((c = getcPtr (parm)) >= 0) {\r
+               if (d > e) {\r
+                       b->slen = d;\r
+                       if (balloc (b, d + 2) != BSTR_OK) return BSTR_ERR;\r
+                       e = b->mlen - 2;\r
+               }\r
+               b->data[d] = (unsigned char) c;\r
+               d++;\r
+               if (c == terminator) break;\r
+       }\r
+\r
+       b->data[d] = (unsigned char) '\0';\r
+       b->slen = d;\r
+\r
+       return d == 0 && c < 0;\r
+}\r
+\r
+/*  int bgetsa (bstring b, bNgetc getcPtr, void * parm, char terminator)\r
+ *\r
+ *  Use an fgetc-like single character stream reading function (getcPtr) to \r
+ *  obtain a sequence of characters which are concatenated to the end of the\r
+ *  bstring b.  The stream read is terminated by the passed in terminator \r
+ *  parameter.\r
+ *\r
+ *  If getcPtr returns with a negative number, or the terminator character \r
+ *  (which is appended) is read, then the stream reading is halted and the \r
+ *  function returns with a partial result concatentated to b.  If there is \r
+ *  an empty partial result, 1 is returned.  If no characters are read, or \r
+ *  there is some other detectable error, BSTR_ERR is returned.\r
+ */\r
+int bgetsa (bstring b, bNgetc getcPtr, void * parm, char terminator) {\r
+int c, d, e;\r
+\r
+       if (b == NULL || b->mlen <= 0 || b->slen < 0 || b->mlen < b->slen ||\r
+           b->mlen <= 0 || getcPtr == NULL) return BSTR_ERR;\r
+       d = b->slen;\r
+       e = b->mlen - 2;\r
+\r
+       while ((c = getcPtr (parm)) >= 0) {\r
+               if (d > e) {\r
+                       b->slen = d;\r
+                       if (balloc (b, d + 2) != BSTR_OK) return BSTR_ERR;\r
+                       e = b->mlen - 2;\r
+               }\r
+               b->data[d] = (unsigned char) c;\r
+               d++;\r
+               if (c == terminator) break;\r
+       }\r
+\r
+       b->data[d] = (unsigned char) '\0';\r
+       b->slen = d;\r
+\r
+       return d == 0 && c < 0;\r
+}\r
+\r
+/*  bstring bgets (bNgetc getcPtr, void * parm, char terminator)\r
+ *\r
+ *  Use an fgetc-like single character stream reading function (getcPtr) to \r
+ *  obtain a sequence of characters which are concatenated into a bstring.  \r
+ *  The stream read is terminated by the passed in terminator function.\r
+ *\r
+ *  If getcPtr returns with a negative number, or the terminator character \r
+ *  (which is appended) is read, then the stream reading is halted and the \r
+ *  result obtained thus far is returned.  If no characters are read, or \r
+ *  there is some other detectable error, NULL is returned.\r
+ */\r
+bstring bgets (bNgetc getcPtr, void * parm, char terminator) {\r
+bstring buff;\r
+\r
+       if (0 > bgetsa (buff = bfromcstr (""), getcPtr, parm, terminator) || 0 >= buff->slen) {\r
+               bdestroy (buff);\r
+               buff = NULL;\r
+       }\r
+       return buff;\r
+}\r
+\r
+struct bStream {\r
+       bstring buff;           /* Buffer for over-reads */\r
+       void * parm;            /* The stream handle for core stream */\r
+       bNread readFnPtr;       /* fread compatible fnptr for core stream */\r
+       int isEOF;              /* track file's EOF state */\r
+       int maxBuffSz;\r
+};\r
+\r
+/*  struct bStream * bsopen (bNread readPtr, void * parm)\r
+ *\r
+ *  Wrap a given open stream (described by a fread compatible function \r
+ *  pointer and stream handle) into an open bStream suitable for the bstring \r
+ *  library streaming functions.\r
+ */\r
+struct bStream * bsopen (bNread readPtr, void * parm) {\r
+struct bStream * s;\r
+\r
+       if (readPtr == NULL) return NULL;\r
+       s = (struct bStream *) bstr__alloc (sizeof (struct bStream));\r
+       if (s == NULL) return NULL;\r
+       s->parm = parm;\r
+       s->buff = bfromcstr ("");\r
+       s->readFnPtr = readPtr;\r
+       s->maxBuffSz = BS_BUFF_SZ;\r
+       s->isEOF = 0;\r
+       return s;\r
+}\r
+\r
+/*  int bsbufflength (struct bStream * s, int sz)\r
+ *\r
+ *  Set the length of the buffer used by the bStream.  If sz is zero, the \r
+ *  length is not set.  This function returns with the previous length.\r
+ */\r
+int bsbufflength (struct bStream * s, int sz) {\r
+int oldSz;\r
+       if (s == NULL || sz < 0) return BSTR_ERR;\r
+       oldSz = s->maxBuffSz;\r
+       if (sz > 0) s->maxBuffSz = sz;\r
+       return oldSz;\r
+}\r
+\r
+int bseof (const struct bStream * s) {\r
+       if (s == NULL || s->readFnPtr == NULL) return BSTR_ERR;\r
+       return s->isEOF && (s->buff->slen == 0);\r
+}\r
+\r
+/*  void * bsclose (struct bStream * s)\r
+ *\r
+ *  Close the bStream, and return the handle to the stream that was originally\r
+ *  used to open the given stream.\r
+ */\r
+void * bsclose (struct bStream * s) {\r
+void * parm;\r
+       if (s == NULL) return NULL;\r
+       s->readFnPtr = NULL;\r
+       if (s->buff) bdestroy (s->buff);\r
+       s->buff = NULL;\r
+       parm = s->parm;\r
+       s->parm = NULL;\r
+       s->isEOF = 1;\r
+       bstr__free (s);\r
+       return parm;\r
+}\r
+\r
+/*  int bsreadlna (bstring r, struct bStream * s, char terminator)\r
+ *\r
+ *  Read a bstring terminated by the terminator character or the end of the\r
+ *  stream from the bStream (s) and return it into the parameter r.  This \r
+ *  function may read additional characters from the core stream that are not \r
+ *  returned, but will be retained for subsequent read operations.\r
+ */\r
+int bsreadlna (bstring r, struct bStream * s, char terminator) {\r
+int i, l, ret, rlo;\r
+char * b;\r
+struct tagbstring x;\r
+\r
+       if (s == NULL || s->buff == NULL || r == NULL || r->mlen <= 0 ||\r
+           r->slen < 0 || r->mlen < r->slen) return BSTR_ERR;\r
+       l = s->buff->slen;\r
+       if (BSTR_OK != balloc (s->buff, s->maxBuffSz + 1)) return BSTR_ERR;\r
+       b = (char *) s->buff->data;\r
+       x.data = (unsigned char *) b;\r
+\r
+       /* First check if the current buffer holds the terminator */\r
+       b[l] = terminator; /* Set sentinel */\r
+       for (i=0; b[i] != terminator; i++) ;\r
+       if (i < l) {\r
+               x.slen = i + 1;\r
+               ret = bconcat (r, &x);\r
+               s->buff->slen = l;\r
+               if (BSTR_OK == ret) bdelete (s->buff, 0, i + 1);\r
+               return BSTR_OK;\r
+       }\r
+\r
+       rlo = r->slen;\r
+\r
+       /* If not then just concatenate the entire buffer to the output */\r
+       x.slen = l;\r
+       if (BSTR_OK != bconcat (r, &x)) return BSTR_ERR;\r
+\r
+       /* Perform direct in-place reads into the destination to allow for\r
+          the minimum of data-copies */\r
+       for (;;) {\r
+               if (BSTR_OK != balloc (r, r->slen + s->maxBuffSz + 1)) return BSTR_ERR;\r
+               b = (char *) (r->data + r->slen);\r
+               l = (int) s->readFnPtr (b, 1, s->maxBuffSz, s->parm);\r
+               if (l <= 0) {\r
+                       r->data[r->slen] = (unsigned char) '\0';\r
+                       s->buff->slen = 0;\r
+                       s->isEOF = 1;\r
+                       /* If nothing was read return with an error message */\r
+                       return BSTR_ERR & -(r->slen == rlo);\r
+               }\r
+               b[l] = terminator; /* Set sentinel */\r
+               for (i=0; b[i] != terminator; i++) ;\r
+               if (i < l) break;\r
+               r->slen += l;\r
+       }\r
+\r
+       /* Terminator found, push over-read back to buffer */\r
+       i++;\r
+       r->slen += i;\r
+       s->buff->slen = l - i;\r
+       bstr__memcpy (s->buff->data, b + i, l - i);\r
+       r->data[r->slen] = (unsigned char) '\0';\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  int bsreadlnsa (bstring r, struct bStream * s, bstring term)\r
+ *\r
+ *  Read a bstring terminated by any character in the term string or the end \r
+ *  of the stream from the bStream (s) and return it into the parameter r.  \r
+ *  This function may read additional characters from the core stream that \r
+ *  are not returned, but will be retained for subsequent read operations.\r
+ */\r
+int bsreadlnsa (bstring r, struct bStream * s, const_bstring term) {\r
+int i, l, ret, rlo;\r
+unsigned char * b;\r
+struct tagbstring x;\r
+struct charField cf;\r
+\r
+       if (s == NULL || s->buff == NULL || r == NULL || term == NULL ||\r
+           term->data == NULL || r->mlen <= 0 || r->slen < 0 ||\r
+           r->mlen < r->slen) return BSTR_ERR;\r
+       if (term->slen == 1) return bsreadlna (r, s, term->data[0]);\r
+       if (term->slen < 1 || buildCharField (&cf, term)) return BSTR_ERR;\r
+\r
+       l = s->buff->slen;\r
+       if (BSTR_OK != balloc (s->buff, s->maxBuffSz + 1)) return BSTR_ERR;\r
+       b = (unsigned char *) s->buff->data;\r
+       x.data = b;\r
+\r
+       /* First check if the current buffer holds the terminator */\r
+       b[l] = term->data[0]; /* Set sentinel */\r
+       for (i=0; !testInCharField (&cf, b[i]); i++) ;\r
+       if (i < l) {\r
+               x.slen = i + 1;\r
+               ret = bconcat (r, &x);\r
+               s->buff->slen = l;\r
+               if (BSTR_OK == ret) bdelete (s->buff, 0, i + 1);\r
+               return BSTR_OK;\r
+       }\r
+\r
+       rlo = r->slen;\r
+\r
+       /* If not then just concatenate the entire buffer to the output */\r
+       x.slen = l;\r
+       if (BSTR_OK != bconcat (r, &x)) return BSTR_ERR;\r
+\r
+       /* Perform direct in-place reads into the destination to allow for\r
+          the minimum of data-copies */\r
+       for (;;) {\r
+               if (BSTR_OK != balloc (r, r->slen + s->maxBuffSz + 1)) return BSTR_ERR;\r
+               b = (unsigned char *) (r->data + r->slen);\r
+               l = (int) s->readFnPtr (b, 1, s->maxBuffSz, s->parm);\r
+               if (l <= 0) {\r
+                       r->data[r->slen] = (unsigned char) '\0';\r
+                       s->buff->slen = 0;\r
+                       s->isEOF = 1;\r
+                       /* If nothing was read return with an error message */\r
+                       return BSTR_ERR & -(r->slen == rlo);\r
+               }\r
+\r
+               b[l] = term->data[0]; /* Set sentinel */\r
+               for (i=0; !testInCharField (&cf, b[i]); i++) ;\r
+               if (i < l) break;\r
+               r->slen += l;\r
+       }\r
+\r
+       /* Terminator found, push over-read back to buffer */\r
+       i++;\r
+       r->slen += i;\r
+       s->buff->slen = l - i;\r
+       bstr__memcpy (s->buff->data, b + i, l - i);\r
+       r->data[r->slen] = (unsigned char) '\0';\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  int bsreada (bstring r, struct bStream * s, int n)\r
+ *\r
+ *  Read a bstring of length n (or, if it is fewer, as many bytes as is \r
+ *  remaining) from the bStream.  This function may read additional \r
+ *  characters from the core stream that are not returned, but will be \r
+ *  retained for subsequent read operations.  This function will not read\r
+ *  additional characters from the core stream beyond virtual stream pointer.\r
+ */\r
+int bsreada (bstring r, struct bStream * s, int n) {\r
+int l, ret, orslen;\r
+char * b;\r
+struct tagbstring x;\r
+\r
+       if (s == NULL || s->buff == NULL || r == NULL || r->mlen <= 0\r
+        || r->slen < 0 || r->mlen < r->slen || n <= 0) return BSTR_ERR;\r
+\r
+       n += r->slen;\r
+       if (n <= 0) return BSTR_ERR;\r
+\r
+       l = s->buff->slen;\r
+\r
+       orslen = r->slen;\r
+\r
+       if (0 == l) {\r
+               if (s->isEOF) return BSTR_ERR;\r
+               if (r->mlen > n) {\r
+                       l = (int) s->readFnPtr (r->data + r->slen, 1, n - r->slen, s->parm);\r
+                       if (0 >= l || l > n - r->slen) {\r
+                               s->isEOF = 1;\r
+                               return BSTR_ERR;\r
+                       }\r
+                       r->slen += l;\r
+                       r->data[r->slen] = (unsigned char) '\0';\r
+                       return 0;\r
+               }\r
+       }\r
+\r
+       if (BSTR_OK != balloc (s->buff, s->maxBuffSz + 1)) return BSTR_ERR;\r
+       b = (char *) s->buff->data;\r
+       x.data = (unsigned char *) b;\r
+\r
+       do {\r
+               if (l + r->slen >= n) {\r
+                       x.slen = n - r->slen;\r
+                       ret = bconcat (r, &x);\r
+                       s->buff->slen = l;\r
+                       if (BSTR_OK == ret) bdelete (s->buff, 0, x.slen);\r
+                       return BSTR_ERR & -(r->slen == orslen);\r
+               }\r
+\r
+               x.slen = l;\r
+               if (BSTR_OK != bconcat (r, &x)) break;\r
+\r
+               l = n - r->slen;\r
+               if (l > s->maxBuffSz) l = s->maxBuffSz;\r
+\r
+               l = (int) s->readFnPtr (b, 1, l, s->parm);\r
+\r
+       } while (l > 0);\r
+       if (l < 0) l = 0;\r
+       if (l == 0) s->isEOF = 1;\r
+       s->buff->slen = l;\r
+       return BSTR_ERR & -(r->slen == orslen);\r
+}\r
+\r
+/*  int bsreadln (bstring r, struct bStream * s, char terminator)\r
+ *\r
+ *  Read a bstring terminated by the terminator character or the end of the\r
+ *  stream from the bStream (s) and return it into the parameter r.  This \r
+ *  function may read additional characters from the core stream that are not \r
+ *  returned, but will be retained for subsequent read operations.\r
+ */\r
+int bsreadln (bstring r, struct bStream * s, char terminator) {\r
+       if (s == NULL || s->buff == NULL || r == NULL || r->mlen <= 0)\r
+               return BSTR_ERR;\r
+       if (BSTR_OK != balloc (s->buff, s->maxBuffSz + 1)) return BSTR_ERR;\r
+       r->slen = 0;\r
+       return bsreadlna (r, s, terminator);\r
+}\r
+\r
+/*  int bsreadlns (bstring r, struct bStream * s, bstring term)\r
+ *\r
+ *  Read a bstring terminated by any character in the term string or the end \r
+ *  of the stream from the bStream (s) and return it into the parameter r.  \r
+ *  This function may read additional characters from the core stream that \r
+ *  are not returned, but will be retained for subsequent read operations.\r
+ */\r
+int bsreadlns (bstring r, struct bStream * s, const_bstring term) {\r
+       if (s == NULL || s->buff == NULL || r == NULL || term == NULL \r
+        || term->data == NULL || r->mlen <= 0) return BSTR_ERR;\r
+       if (term->slen == 1) return bsreadln (r, s, term->data[0]);\r
+       if (term->slen < 1) return BSTR_ERR;\r
+       if (BSTR_OK != balloc (s->buff, s->maxBuffSz + 1)) return BSTR_ERR;\r
+       r->slen = 0;\r
+       return bsreadlnsa (r, s, term);\r
+}\r
+\r
+/*  int bsread (bstring r, struct bStream * s, int n)\r
+ *\r
+ *  Read a bstring of length n (or, if it is fewer, as many bytes as is \r
+ *  remaining) from the bStream.  This function may read additional \r
+ *  characters from the core stream that are not returned, but will be \r
+ *  retained for subsequent read operations.  This function will not read\r
+ *  additional characters from the core stream beyond virtual stream pointer.\r
+ */\r
+int bsread (bstring r, struct bStream * s, int n) {\r
+       if (s == NULL || s->buff == NULL || r == NULL || r->mlen <= 0\r
+        || n <= 0) return BSTR_ERR;\r
+       if (BSTR_OK != balloc (s->buff, s->maxBuffSz + 1)) return BSTR_ERR;\r
+       r->slen = 0;\r
+       return bsreada (r, s, n);\r
+}\r
+\r
+/*  int bsunread (struct bStream * s, const_bstring b)\r
+ *\r
+ *  Insert a bstring into the bStream at the current position.  These \r
+ *  characters will be read prior to those that actually come from the core \r
+ *  stream.\r
+ */\r
+int bsunread (struct bStream * s, const_bstring b) {\r
+       if (s == NULL || s->buff == NULL) return BSTR_ERR;\r
+       return binsert (s->buff, 0, b, (unsigned char) '?');\r
+}\r
+\r
+/*  int bspeek (bstring r, const struct bStream * s)\r
+ *\r
+ *  Return the currently buffered characters from the bStream that will be \r
+ *  read prior to reads from the core stream.\r
+ */\r
+int bspeek (bstring r, const struct bStream * s) {\r
+       if (s == NULL || s->buff == NULL) return BSTR_ERR;\r
+       return bassign (r, s->buff);\r
+}\r
+\r
+/*  bstring bjoin (const struct bstrList * bl, const_bstring sep);\r
+ *\r
+ *  Join the entries of a bstrList into one bstring by sequentially \r
+ *  concatenating them with the sep string in between.  If there is an error \r
+ *  NULL is returned, otherwise a bstring with the correct result is returned.\r
+ */\r
+bstring bjoin (const struct bstrList * bl, const_bstring sep) {\r
+bstring b;\r
+int i, c, v;\r
+\r
+       if (bl == NULL || bl->qty < 0) return NULL;\r
+       if (sep != NULL && (sep->slen < 0 || sep->data == NULL)) return NULL;\r
+\r
+       for (i = 0, c = 1; i < bl->qty; i++) {\r
+               v = bl->entry[i]->slen;\r
+               if (v < 0) return NULL; /* Invalid input */\r
+               c += v;\r
+               if (c < 0) return NULL; /* Wrap around ?? */\r
+       }\r
+\r
+       if (sep != NULL) c += (bl->qty - 1) * sep->slen;\r
+\r
+       b = (bstring) bstr__alloc (sizeof (struct tagbstring));\r
+       if (NULL == b) return NULL; /* Out of memory */\r
+       b->data = (unsigned char *) bstr__alloc (c);\r
+       if (b->data == NULL) {\r
+               bstr__free (b);\r
+               return NULL;\r
+       }\r
+\r
+       b->mlen = c;\r
+       b->slen = c-1;\r
+\r
+       for (i = 0, c = 0; i < bl->qty; i++) {\r
+               if (i > 0 && sep != NULL) {\r
+                       bstr__memcpy (b->data + c, sep->data, sep->slen);\r
+                       c += sep->slen;\r
+               }\r
+               v = bl->entry[i]->slen;\r
+               bstr__memcpy (b->data + c, bl->entry[i]->data, v);\r
+               c += v;\r
+       }\r
+       b->data[c] = (unsigned char) '\0';\r
+       return b;\r
+}\r
+\r
+#define BSSSC_BUFF_LEN (256)\r
+\r
+/*  int bssplitscb (struct bStream * s, const_bstring splitStr, \r
+ *     int (* cb) (void * parm, int ofs, const_bstring entry), void * parm)\r
+ *\r
+ *  Iterate the set of disjoint sequential substrings read from a stream \r
+ *  divided by any of the characters in splitStr.  An empty splitStr causes \r
+ *  the whole stream to be iterated once.\r
+ *\r
+ *  Note: At the point of calling the cb function, the bStream pointer is \r
+ *  pointed exactly at the position right after having read the split \r
+ *  character.  The cb function can act on the stream by causing the bStream\r
+ *  pointer to move, and bssplitscb will continue by starting the next split\r
+ *  at the position of the pointer after the return from cb.\r
+ *\r
+ *  However, if the cb causes the bStream s to be destroyed then the cb must\r
+ *  return with a negative value, otherwise bssplitscb will continue in an \r
+ *  undefined manner.\r
+ */\r
+int bssplitscb (struct bStream * s, const_bstring splitStr, \r
+       int (* cb) (void * parm, int ofs, const_bstring entry), void * parm) {\r
+struct charField chrs;\r
+bstring buff;\r
+int i, p, ret;\r
+\r
+       if (cb == NULL || s == NULL || s->readFnPtr == NULL \r
+        || splitStr == NULL || splitStr->slen < 0) return BSTR_ERR;\r
+\r
+       if (NULL == (buff = bfromcstr (""))) return BSTR_ERR;\r
+\r
+       if (splitStr->slen == 0) {\r
+               while (bsreada (buff, s, BSSSC_BUFF_LEN) >= 0) ;\r
+               if ((ret = cb (parm, 0, buff)) > 0) \r
+                       ret = 0;\r
+       } else {\r
+               buildCharField (&chrs, splitStr);\r
+               ret = p = i = 0;\r
+               for (;;) {\r
+                       if (i >= buff->slen) {\r
+                               bsreada (buff, s, BSSSC_BUFF_LEN);\r
+                               if (i >= buff->slen) {\r
+                                       if (0 < (ret = cb (parm, p, buff))) ret = 0;\r
+                                       break;\r
+                               }\r
+                       }\r
+                       if (testInCharField (&chrs, buff->data[i])) {\r
+                               struct tagbstring t;\r
+                               unsigned char c;\r
+\r
+                               blk2tbstr (t, buff->data + i + 1, buff->slen - (i + 1));\r
+                               if ((ret = bsunread (s, &t)) < 0) break;\r
+                               buff->slen = i;\r
+                               c = buff->data[i];\r
+                               buff->data[i] = (unsigned char) '\0';\r
+                               if ((ret = cb (parm, p, buff)) < 0) break;\r
+                               buff->data[i] = c;\r
+                               buff->slen = 0;\r
+                               p += i + 1;\r
+                               i = -1;\r
+                       }\r
+                       i++;\r
+               }\r
+       }\r
+\r
+       bdestroy (buff);\r
+       return ret;\r
+}\r
+\r
+/*  int bssplitstrcb (struct bStream * s, const_bstring splitStr, \r
+ *     int (* cb) (void * parm, int ofs, const_bstring entry), void * parm)\r
+ *\r
+ *  Iterate the set of disjoint sequential substrings read from a stream \r
+ *  divided by the entire substring splitStr.  An empty splitStr causes \r
+ *  each character of the stream to be iterated.\r
+ *\r
+ *  Note: At the point of calling the cb function, the bStream pointer is \r
+ *  pointed exactly at the position right after having read the split \r
+ *  character.  The cb function can act on the stream by causing the bStream\r
+ *  pointer to move, and bssplitscb will continue by starting the next split\r
+ *  at the position of the pointer after the return from cb.\r
+ *\r
+ *  However, if the cb causes the bStream s to be destroyed then the cb must\r
+ *  return with a negative value, otherwise bssplitscb will continue in an \r
+ *  undefined manner.\r
+ */\r
+int bssplitstrcb (struct bStream * s, const_bstring splitStr, \r
+       int (* cb) (void * parm, int ofs, const_bstring entry), void * parm) {\r
+bstring buff;\r
+int i, p, ret;\r
+\r
+       if (cb == NULL || s == NULL || s->readFnPtr == NULL \r
+        || splitStr == NULL || splitStr->slen < 0) return BSTR_ERR;\r
+\r
+       if (splitStr->slen == 1) return bssplitscb (s, splitStr, cb, parm);\r
+\r
+       if (NULL == (buff = bfromcstr (""))) return BSTR_ERR;\r
+\r
+       if (splitStr->slen == 0) {\r
+               for (i=0; bsreada (buff, s, BSSSC_BUFF_LEN) >= 0; i++) {\r
+                       if ((ret = cb (parm, 0, buff)) < 0) {\r
+                               bdestroy (buff);\r
+                               return ret;\r
+                       }\r
+                       buff->slen = 0;\r
+               }\r
+               return BSTR_OK;\r
+       } else {\r
+               ret = p = i = 0;\r
+               for (i=p=0;;) {\r
+                       if ((ret = binstr (buff, 0, splitStr)) >= 0) {\r
+                               struct tagbstring t;\r
+                               blk2tbstr (t, buff->data, ret);\r
+                               i = ret + splitStr->slen;\r
+                               if ((ret = cb (parm, p, &t)) < 0) break;\r
+                               p += i;\r
+                               bdelete (buff, 0, i);\r
+                       } else {\r
+                               bsreada (buff, s, BSSSC_BUFF_LEN);\r
+                               if (bseof (s)) {\r
+                                       if ((ret = cb (parm, p, buff)) > 0) ret = 0;\r
+                                       break;\r
+                               }\r
+                       }\r
+               }\r
+       }\r
+\r
+       bdestroy (buff);\r
+       return ret;\r
+}\r
+\r
+/*  int bstrListCreate (void)\r
+ *\r
+ *  Create a bstrList.\r
+ */\r
+struct bstrList * bstrListCreate (void) {\r
+struct bstrList * sl = (struct bstrList *) bstr__alloc (sizeof (struct bstrList));\r
+       if (sl) {\r
+               sl->entry = (bstring *) bstr__alloc (1*sizeof (bstring));\r
+               if (!sl->entry) {\r
+                       bstr__free (sl);\r
+                       sl = NULL;\r
+               } else {\r
+                       sl->qty = 0;\r
+                       sl->mlen = 1;\r
+               }\r
+       }\r
+       return sl;\r
+}\r
+\r
+/*  int bstrListDestroy (struct bstrList * sl)\r
+ *\r
+ *  Destroy a bstrList that has been created by bsplit, bsplits or bstrListCreate.\r
+ */\r
+int bstrListDestroy (struct bstrList * sl) {\r
+int i;\r
+       if (sl == NULL || sl->qty < 0) return BSTR_ERR;\r
+       for (i=0; i < sl->qty; i++) {\r
+               if (sl->entry[i]) {\r
+                       bdestroy (sl->entry[i]);\r
+                       sl->entry[i] = NULL;\r
+               }\r
+       }\r
+       sl->qty  = -1;\r
+       sl->mlen = -1;\r
+       bstr__free (sl->entry);\r
+       sl->entry = NULL;\r
+       bstr__free (sl);\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  int bstrListAlloc (struct bstrList * sl, int msz)\r
+ *\r
+ *  Ensure that there is memory for at least msz number of entries for the\r
+ *  list.\r
+ */\r
+int bstrListAlloc (struct bstrList * sl, int msz) {\r
+bstring * l;\r
+int smsz;\r
+size_t nsz;\r
+       if (!sl || msz <= 0 || !sl->entry || sl->qty < 0 || sl->mlen <= 0 || sl->qty > sl->mlen) return BSTR_ERR;\r
+       if (sl->mlen >= msz) return BSTR_OK;\r
+       smsz = snapUpSize (msz);\r
+       nsz = ((size_t) smsz) * sizeof (bstring);\r
+       if (nsz < (size_t) smsz) return BSTR_ERR;\r
+       l = (bstring *) bstr__realloc (sl->entry, nsz);\r
+       if (!l) {\r
+               smsz = msz;\r
+               nsz = ((size_t) smsz) * sizeof (bstring);\r
+               l = (bstring *) bstr__realloc (sl->entry, nsz);\r
+               if (!l) return BSTR_ERR;\r
+       }\r
+       sl->mlen = smsz;\r
+       sl->entry = l;\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  int bstrListAllocMin (struct bstrList * sl, int msz)\r
+ *\r
+ *  Try to allocate the minimum amount of memory for the list to include at\r
+ *  least msz entries or sl->qty whichever is greater.\r
+ */\r
+int bstrListAllocMin (struct bstrList * sl, int msz) {\r
+bstring * l;\r
+size_t nsz;\r
+       if (!sl || msz <= 0 || !sl->entry || sl->qty < 0 || sl->mlen <= 0 || sl->qty > sl->mlen) return BSTR_ERR;\r
+       if (msz < sl->qty) msz = sl->qty;\r
+       if (sl->mlen == msz) return BSTR_OK;\r
+       nsz = ((size_t) msz) * sizeof (bstring);\r
+       if (nsz < (size_t) msz) return BSTR_ERR;\r
+       l = (bstring *) bstr__realloc (sl->entry, nsz);\r
+       if (!l) return BSTR_ERR;\r
+       sl->mlen = msz;\r
+       sl->entry = l;\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  int bsplitcb (const_bstring str, unsigned char splitChar, int pos,\r
+ *     int (* cb) (void * parm, int ofs, int len), void * parm)\r
+ *\r
+ *  Iterate the set of disjoint sequential substrings over str divided by the\r
+ *  character in splitChar.\r
+ *\r
+ *  Note: Non-destructive modification of str from within the cb function \r
+ *  while performing this split is not undefined.  bsplitcb behaves in \r
+ *  sequential lock step with calls to cb.  I.e., after returning from a cb \r
+ *  that return a non-negative integer, bsplitcb continues from the position \r
+ *  1 character after the last detected split character and it will halt \r
+ *  immediately if the length of str falls below this point.  However, if the \r
+ *  cb function destroys str, then it *must* return with a negative value, \r
+ *  otherwise bsplitcb will continue in an undefined manner.\r
+ */\r
+int bsplitcb (const_bstring str, unsigned char splitChar, int pos,\r
+       int (* cb) (void * parm, int ofs, int len), void * parm) {\r
+int i, p, ret;\r
+\r
+       if (cb == NULL || str == NULL || pos < 0 || pos > str->slen) \r
+               return BSTR_ERR;\r
+\r
+       p = pos;\r
+       do {\r
+               for (i=p; i < str->slen; i++) {\r
+                       if (str->data[i] == splitChar) break;\r
+               }\r
+               if ((ret = cb (parm, p, i - p)) < 0) return ret;\r
+               p = i + 1;\r
+       } while (p <= str->slen);\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  int bsplitscb (const_bstring str, const_bstring splitStr, int pos,\r
+ *     int (* cb) (void * parm, int ofs, int len), void * parm)\r
+ *\r
+ *  Iterate the set of disjoint sequential substrings over str divided by any \r
+ *  of the characters in splitStr.  An empty splitStr causes the whole str to\r
+ *  be iterated once.\r
+ *\r
+ *  Note: Non-destructive modification of str from within the cb function \r
+ *  while performing this split is not undefined.  bsplitscb behaves in \r
+ *  sequential lock step with calls to cb.  I.e., after returning from a cb \r
+ *  that return a non-negative integer, bsplitscb continues from the position \r
+ *  1 character after the last detected split character and it will halt \r
+ *  immediately if the length of str falls below this point.  However, if the \r
+ *  cb function destroys str, then it *must* return with a negative value, \r
+ *  otherwise bsplitscb will continue in an undefined manner.\r
+ */\r
+int bsplitscb (const_bstring str, const_bstring splitStr, int pos,\r
+       int (* cb) (void * parm, int ofs, int len), void * parm) {\r
+struct charField chrs;\r
+int i, p, ret;\r
+\r
+       if (cb == NULL || str == NULL || pos < 0 || pos > str->slen \r
+        || splitStr == NULL || splitStr->slen < 0) return BSTR_ERR;\r
+       if (splitStr->slen == 0) {\r
+               if ((ret = cb (parm, 0, str->slen)) > 0) ret = 0;\r
+               return ret;\r
+       }\r
+\r
+       if (splitStr->slen == 1) \r
+               return bsplitcb (str, splitStr->data[0], pos, cb, parm);\r
+\r
+       buildCharField (&chrs, splitStr);\r
+\r
+       p = pos;\r
+       do {\r
+               for (i=p; i < str->slen; i++) {\r
+                       if (testInCharField (&chrs, str->data[i])) break;\r
+               }\r
+               if ((ret = cb (parm, p, i - p)) < 0) return ret;\r
+               p = i + 1;\r
+       } while (p <= str->slen);\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  int bsplitstrcb (const_bstring str, const_bstring splitStr, int pos,\r
+ *     int (* cb) (void * parm, int ofs, int len), void * parm)\r
+ *\r
+ *  Iterate the set of disjoint sequential substrings over str divided by the \r
+ *  substring splitStr.  An empty splitStr causes the whole str to be \r
+ *  iterated once.\r
+ *\r
+ *  Note: Non-destructive modification of str from within the cb function \r
+ *  while performing this split is not undefined.  bsplitstrcb behaves in \r
+ *  sequential lock step with calls to cb.  I.e., after returning from a cb \r
+ *  that return a non-negative integer, bsplitscb continues from the position \r
+ *  1 character after the last detected split character and it will halt \r
+ *  immediately if the length of str falls below this point.  However, if the \r
+ *  cb function destroys str, then it *must* return with a negative value, \r
+ *  otherwise bsplitscb will continue in an undefined manner.\r
+ */\r
+int bsplitstrcb (const_bstring str, const_bstring splitStr, int pos,\r
+       int (* cb) (void * parm, int ofs, int len), void * parm) {\r
+int i, p, ret;\r
+\r
+       if (cb == NULL || str == NULL || pos < 0 || pos > str->slen \r
+        || splitStr == NULL || splitStr->slen < 0) return BSTR_ERR;\r
+\r
+       if (0 == splitStr->slen) {\r
+               for (i=pos; i < str->slen; i++) {\r
+                       if ((ret = cb (parm, i, 1)) < 0) return ret;\r
+               }\r
+               return BSTR_OK;\r
+       }\r
+\r
+       if (splitStr->slen == 1) \r
+               return bsplitcb (str, splitStr->data[0], pos, cb, parm);\r
+\r
+       for (i=p=pos; i <= str->slen - splitStr->slen; i++) {\r
+               if (0 == bstr__memcmp (splitStr->data, str->data + i, splitStr->slen)) {\r
+                       if ((ret = cb (parm, p, i - p)) < 0) return ret;\r
+                       i += splitStr->slen;\r
+                       p = i;\r
+               }\r
+       }\r
+       if ((ret = cb (parm, p, str->slen - p)) < 0) return ret;\r
+       return BSTR_OK;\r
+}\r
+\r
+struct genBstrList {\r
+       bstring b;\r
+       struct bstrList * bl;\r
+};\r
+\r
+static int bscb (void * parm, int ofs, int len) {\r
+struct genBstrList * g = (struct genBstrList *) parm;\r
+       if (g->bl->qty >= g->bl->mlen) {\r
+               int mlen = g->bl->mlen * 2;\r
+               bstring * tbl;\r
+\r
+               while (g->bl->qty >= mlen) {\r
+                       if (mlen < g->bl->mlen) return BSTR_ERR;\r
+                       mlen += mlen;\r
+               }\r
+\r
+               tbl = (bstring *) bstr__realloc (g->bl->entry, sizeof (bstring) * mlen);\r
+               if (tbl == NULL) return BSTR_ERR;\r
+\r
+               g->bl->entry = tbl;\r
+               g->bl->mlen = mlen;\r
+       }\r
+\r
+       g->bl->entry[g->bl->qty] = bmidstr (g->b, ofs, len);\r
+       g->bl->qty++;\r
+       return BSTR_OK;\r
+}\r
+\r
+/*  struct bstrList * bsplit (const_bstring str, unsigned char splitChar)\r
+ *\r
+ *  Create an array of sequential substrings from str divided by the character\r
+ *  splitChar.  \r
+ */\r
+struct bstrList * bsplit (const_bstring str, unsigned char splitChar) {\r
+struct genBstrList g;\r
+\r
+       if (str == NULL || str->data == NULL || str->slen < 0) return NULL;\r
+\r
+       g.bl = (struct bstrList *) bstr__alloc (sizeof (struct bstrList));\r
+       if (g.bl == NULL) return NULL;\r
+       g.bl->mlen = 4;\r
+       g.bl->entry = (bstring *) bstr__alloc (g.bl->mlen * sizeof (bstring));\r
+       if (NULL == g.bl->entry) {\r
+               bstr__free (g.bl);\r
+               return NULL;\r
+       }\r
+\r
+       g.b = (bstring) str;\r
+       g.bl->qty = 0;\r
+       if (bsplitcb (str, splitChar, 0, bscb, &g) < 0) {\r
+               bstrListDestroy (g.bl);\r
+               return NULL;\r
+       }\r
+       return g.bl;\r
+}\r
+\r
+/*  struct bstrList * bsplitstr (const_bstring str, const_bstring splitStr)\r
+ *\r
+ *  Create an array of sequential substrings from str divided by the entire\r
+ *  substring splitStr.\r
+ */\r
+struct bstrList * bsplitstr (const_bstring str, const_bstring splitStr) {\r
+struct genBstrList g;\r
+\r
+       if (str == NULL || str->data == NULL || str->slen < 0) return NULL;\r
+\r
+       g.bl = (struct bstrList *) bstr__alloc (sizeof (struct bstrList));\r
+       if (g.bl == NULL) return NULL;\r
+       g.bl->mlen = 4;\r
+       g.bl->entry = (bstring *) bstr__alloc (g.bl->mlen * sizeof (bstring));\r
+       if (NULL == g.bl->entry) {\r
+               bstr__free (g.bl);\r
+               return NULL;\r
+       }\r
+\r
+       g.b = (bstring) str;\r
+       g.bl->qty = 0;\r
+       if (bsplitstrcb (str, splitStr, 0, bscb, &g) < 0) {\r
+               bstrListDestroy (g.bl);\r
+               return NULL;\r
+       }\r
+       return g.bl;\r
+}\r
+\r
+/*  struct bstrList * bsplits (const_bstring str, bstring splitStr)\r
+ *\r
+ *  Create an array of sequential substrings from str divided by any of the \r
+ *  characters in splitStr.  An empty splitStr causes a single entry bstrList\r
+ *  containing a copy of str to be returned.\r
+ */\r
+struct bstrList * bsplits (const_bstring str, const_bstring splitStr) {\r
+struct genBstrList g;\r
+\r
+       if (     str == NULL ||      str->slen < 0 ||      str->data == NULL ||\r
+           splitStr == NULL || splitStr->slen < 0 || splitStr->data == NULL)\r
+               return NULL;\r
+\r
+       g.bl = (struct bstrList *) bstr__alloc (sizeof (struct bstrList));\r
+       if (g.bl == NULL) return NULL;\r
+       g.bl->mlen = 4;\r
+       g.bl->entry = (bstring *) bstr__alloc (g.bl->mlen * sizeof (bstring));\r
+       if (NULL == g.bl->entry) {\r
+               bstr__free (g.bl);\r
+               return NULL;\r
+       }\r
+       g.b = (bstring) str;\r
+       g.bl->qty = 0;\r
+\r
+       if (bsplitscb (str, splitStr, 0, bscb, &g) < 0) {\r
+               bstrListDestroy (g.bl);\r
+               return NULL;\r
+       }\r
+       return g.bl;\r
+}\r
+\r
+#if defined (__TURBOC__) && !defined (__BORLANDC__)\r
+# ifndef BSTRLIB_NOVSNP\r
+#  define BSTRLIB_NOVSNP\r
+# endif\r
+#endif\r
+\r
+/* Give WATCOM C/C++, MSVC some latitude for their non-support of vsnprintf */\r
+#if defined(__WATCOMC__) || defined(_MSC_VER)\r
+#define exvsnprintf(r,b,n,f,a) {r = _vsnprintf (b,n,f,a);}\r
+#else\r
+#ifdef BSTRLIB_NOVSNP\r
+/* This is just a hack.  If you are using a system without a vsnprintf, it is \r
+   not recommended that bformat be used at all. */\r
+#define exvsnprintf(r,b,n,f,a) {vsprintf (b,f,a); r = -1;}\r
+#define START_VSNBUFF (256)\r
+#else\r
+\r
+#ifdef __GNUC__\r
+/* Something is making gcc complain about this prototype not being here, so \r
+   I've just gone ahead and put it in. */\r
+extern int vsnprintf (char *buf, size_t count, const char *format, va_list arg);\r
+#endif\r
+\r
+#define exvsnprintf(r,b,n,f,a) {r = vsnprintf (b,n,f,a);}\r
+#endif\r
+#endif\r
+\r
+#if !defined (BSTRLIB_NOVSNP)\r
+\r
+#ifndef START_VSNBUFF\r
+#define START_VSNBUFF (16)\r
+#endif\r
+\r
+/* On IRIX vsnprintf returns n-1 when the operation would overflow the target \r
+   buffer, WATCOM and MSVC both return -1, while C99 requires that the \r
+   returned value be exactly what the length would be if the buffer would be\r
+   large enough.  This leads to the idea that if the return value is larger \r
+   than n, then changing n to the return value will reduce the number of\r
+   iterations required. */\r
+\r
+/*  int bformata (bstring b, const char * fmt, ...)\r
+ *\r
+ *  After the first parameter, it takes the same parameters as printf (), but \r
+ *  rather than outputting results to stdio, it appends the results to \r
+ *  a bstring which contains what would have been output. Note that if there \r
+ *  is an early generation of a '\0' character, the bstring will be truncated \r
+ *  to this end point.\r
+ */\r
+int bformata (bstring b, const char * fmt, ...) {\r
+va_list arglist;\r
+bstring buff;\r
+int n, r;\r
+\r
+       if (b == NULL || fmt == NULL || b->data == NULL || b->mlen <= 0 \r
+        || b->slen < 0 || b->slen > b->mlen) return BSTR_ERR;\r
+\r
+       /* Since the length is not determinable beforehand, a search is\r
+          performed using the truncating "vsnprintf" call (to avoid buffer\r
+          overflows) on increasing potential sizes for the output result. */\r
+\r
+       if ((n = (int) (2*strlen (fmt))) < START_VSNBUFF) n = START_VSNBUFF;\r
+       if (NULL == (buff = bfromcstralloc (n + 2, ""))) {\r
+               n = 1;\r
+               if (NULL == (buff = bfromcstralloc (n + 2, ""))) return BSTR_ERR;\r
+       }\r
+\r
+       for (;;) {\r
+               va_start (arglist, fmt);\r
+               exvsnprintf (r, (char *) buff->data, n + 1, fmt, arglist);\r
+               va_end (arglist);\r
+\r
+               buff->data[n] = (unsigned char) '\0';\r
+               buff->slen = (int) (strlen) ((char *) buff->data);\r
+\r
+               if (buff->slen < n) break;\r
+\r
+               if (r > n) n = r; else n += n;\r
+\r
+               if (BSTR_OK != balloc (buff, n + 2)) {\r
+                       bdestroy (buff);\r
+                       return BSTR_ERR;\r
+               }\r
+       }\r
+\r
+       r = bconcat (b, buff);\r
+       bdestroy (buff);\r
+       return r;\r
+}\r
+\r
+/*  int bassignformat (bstring b, const char * fmt, ...)\r
+ *\r
+ *  After the first parameter, it takes the same parameters as printf (), but \r
+ *  rather than outputting results to stdio, it outputs the results to \r
+ *  the bstring parameter b. Note that if there is an early generation of a \r
+ *  '\0' character, the bstring will be truncated to this end point.\r
+ */\r
+int bassignformat (bstring b, const char * fmt, ...) {\r
+va_list arglist;\r
+bstring buff;\r
+int n, r;\r
+\r
+       if (b == NULL || fmt == NULL || b->data == NULL || b->mlen <= 0 \r
+        || b->slen < 0 || b->slen > b->mlen) return BSTR_ERR;\r
+\r
+       /* Since the length is not determinable beforehand, a search is\r
+          performed using the truncating "vsnprintf" call (to avoid buffer\r
+          overflows) on increasing potential sizes for the output result. */\r
+\r
+       if ((n = (int) (2*strlen (fmt))) < START_VSNBUFF) n = START_VSNBUFF;\r
+       if (NULL == (buff = bfromcstralloc (n + 2, ""))) {\r
+               n = 1;\r
+               if (NULL == (buff = bfromcstralloc (n + 2, ""))) return BSTR_ERR;\r
+       }\r
+\r
+       for (;;) {\r
+               va_start (arglist, fmt);\r
+               exvsnprintf (r, (char *) buff->data, n + 1, fmt, arglist);\r
+               va_end (arglist);\r
+\r
+               buff->data[n] = (unsigned char) '\0';\r
+               buff->slen = (int) (strlen) ((char *) buff->data);\r
+\r
+               if (buff->slen < n) break;\r
+\r
+               if (r > n) n = r; else n += n;\r
+\r
+               if (BSTR_OK != balloc (buff, n + 2)) {\r
+                       bdestroy (buff);\r
+                       return BSTR_ERR;\r
+               }\r
+       }\r
+\r
+       r = bassign (b, buff);\r
+       bdestroy (buff);\r
+       return r;\r
+}\r
+\r
+/*  bstring bformat (const char * fmt, ...)\r
+ *\r
+ *  Takes the same parameters as printf (), but rather than outputting results\r
+ *  to stdio, it forms a bstring which contains what would have been output.\r
+ *  Note that if there is an early generation of a '\0' character, the \r
+ *  bstring will be truncated to this end point.\r
+ */\r
+bstring bformat (const char * fmt, ...) {\r
+va_list arglist;\r
+bstring buff;\r
+int n, r;\r
+\r
+       if (fmt == NULL) return NULL;\r
+\r
+       /* Since the length is not determinable beforehand, a search is\r
+          performed using the truncating "vsnprintf" call (to avoid buffer\r
+          overflows) on increasing potential sizes for the output result. */\r
+\r
+       if ((n = (int) (2*strlen (fmt))) < START_VSNBUFF) n = START_VSNBUFF;\r
+       if (NULL == (buff = bfromcstralloc (n + 2, ""))) {\r
+               n = 1;\r
+               if (NULL == (buff = bfromcstralloc (n + 2, ""))) return NULL;\r
+       }\r
+\r
+       for (;;) {\r
+               va_start (arglist, fmt);\r
+               exvsnprintf (r, (char *) buff->data, n + 1, fmt, arglist);\r
+               va_end (arglist);\r
+\r
+               buff->data[n] = (unsigned char) '\0';\r
+               buff->slen = (int) (strlen) ((char *) buff->data);\r
+\r
+               if (buff->slen < n) break;\r
+\r
+               if (r > n) n = r; else n += n;\r
+\r
+               if (BSTR_OK != balloc (buff, n + 2)) {\r
+                       bdestroy (buff);\r
+                       return NULL;\r
+               }\r
+       }\r
+\r
+       return buff;\r
+}\r
+\r
+/*  int bvcformata (bstring b, int count, const char * fmt, va_list arglist)\r
+ *\r
+ *  The bvcformata function formats data under control of the format control \r
+ *  string fmt and attempts to append the result to b.  The fmt parameter is \r
+ *  the same as that of the printf function.  The variable argument list is \r
+ *  replaced with arglist, which has been initialized by the va_start macro.\r
+ *  The size of the output is upper bounded by count.  If the required output\r
+ *  exceeds count, the string b is not augmented with any contents and a value\r
+ *  below BSTR_ERR is returned.  If a value below -count is returned then it\r
+ *  is recommended that the negative of this value be used as an update to the\r
+ *  count in a subsequent pass.  On other errors, such as running out of \r
+ *  memory, parameter errors or numeric wrap around BSTR_ERR is returned.  \r
+ *  BSTR_OK is returned when the output is successfully generated and \r
+ *  appended to b.\r
+ *\r
+ *  Note: There is no sanity checking of arglist, and this function is\r
+ *  destructive of the contents of b from the b->slen point onward.  If there \r
+ *  is an early generation of a '\0' character, the bstring will be truncated \r
+ *  to this end point.\r
+ */\r
+int bvcformata (bstring b, int count, const char * fmt, va_list arg) {\r
+int n, r, l;\r
+\r
+       if (b == NULL || fmt == NULL || count <= 0 || b->data == NULL\r
+        || b->mlen <= 0 || b->slen < 0 || b->slen > b->mlen) return BSTR_ERR;\r
+\r
+       if (count > (n = b->slen + count) + 2) return BSTR_ERR;\r
+       if (BSTR_OK != balloc (b, n + 2)) return BSTR_ERR;\r
+\r
+       exvsnprintf (r, (char *) b->data + b->slen, count + 2, fmt, arg);\r
+\r
+       /* Did the operation complete successfully within bounds? */\r
+\r
+       if (n >= (l = b->slen + (int) (strlen) ((const char *) b->data + b->slen))) {\r
+               b->slen = l;\r
+               return BSTR_OK;\r
+       }\r
+\r
+       /* Abort, since the buffer was not large enough.  The return value \r
+          tries to help set what the retry length should be. */\r
+\r
+       b->data[b->slen] = '\0';\r
+       if (r > count+1) l = r; else {\r
+               l = count+count;\r
+               if (count > l) l = INT_MAX;\r
+       }\r
+       n = -l;\r
+       if (n > BSTR_ERR-1) n = BSTR_ERR-1;\r
+       return n;\r
+}\r
+\r
+#endif\r