* @param OutBuf the output buffer
* @param In Buffer to encode
* @param PlainIn way in from plain old c strings
+ * @param PlainInLen way in from plain old c strings; maybe you've got binary data or know the length?
*/
-void StrBufHexescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
+void StrBufHexEscAppend(StrBuf *OutBuf, const StrBuf *In, const unsigned char *PlainIn, long PlainInLen)
{
- const char *pch, *pche;
+ const unsigned char *pch, *pche;
char *pt, *pte;
int len;
if (((In == NULL) && (PlainIn == NULL)) || (OutBuf == NULL) )
return;
if (PlainIn != NULL) {
- len = strlen(PlainIn);
+ if (PlainInLen < 0)
+ len = strlen((const char*)PlainIn);
+ else
+ len = PlainInLen;
pch = PlainIn;
pche = pch + len;
}
else {
- pch = In->buf;
+ pch = (const unsigned char*)In->buf;
pche = pch + In->BufUsed;
len = In->BufUsed;
}
pt = OutBuf->buf + OutBuf->BufUsed;
}
- *pt = HexList[(unsigned char)*pch][0];
+ *pt = HexList[*pch][0];
pt ++;
- *pt = HexList[(unsigned char)*pch][1];
+ *pt = HexList[*pch][1];
pt ++; pch ++; OutBuf->BufUsed += 2;
}
*pt = '\0';
}
+/**
+ * @ingroup StrBuf_DeEnCoder
+ * @brief append a string in hex encoding to the buffer
+ * @param OutBuf the output buffer
+ * @param In Buffer to encode
+ * @param PlainIn way in from plain old c strings
+ */
+void StrBufHexescAppend(StrBuf *OutBuf, const StrBuf *In, const char *PlainIn)
+{
+ StrBufHexEscAppend(OutBuf, In, (const unsigned char*) PlainIn, -1);
+}
+
/**
* @ingroup StrBuf_DeEnCoder
* @brief Append a string, escaping characters which have meaning in HTML.
return -1;
bptr = Target->buf + Target->BufUsed;
- eptr = Target->buf + Target->BufSize - 3; /* our biggest unit to put in... */
+ eptr = Target->buf + Target->BufSize - 7; /* our biggest unit to put in... */
while (aptr < eiptr){
if(bptr >= eptr) {
IncreaseBuf(Target, 1, -1);
- eptr = Target->buf + Target->BufSize - 3;
+ eptr = Target->buf + Target->BufSize - 7; /* our biggest unit to put in... */
bptr = Target->buf + Target->BufUsed;
}
- if (*aptr == '"') {
+ switch (*aptr) {
+ case '\n':
+ memcpy(bptr, HKEY("\\n"));
+ bptr += 2;
+ Target->BufUsed += 2;
+ break;
+ case '\r':
+ memcpy(bptr, HKEY("\\r"));
+ bptr += 2;
+ Target->BufUsed += 2;
+ break;
+ case '"':
*bptr = '\\';
bptr ++;
*bptr = '"';
bptr ++;
Target->BufUsed += 2;
- } else if (*aptr == '\\') {
+ break;
+ case '\\':
+ if ((*(aptr + 1) == 'u') &&
+ isxdigit(*(aptr + 2)) &&
+ isxdigit(*(aptr + 3)) &&
+ isxdigit(*(aptr + 4)) &&
+ isxdigit(*(aptr + 5)))
+ { /* oh, a unicode escaper. let it pass through. */
+ memcpy(bptr, aptr, 6);
+ aptr += 5;
+ bptr +=6;
+ Target->BufUsed += 6;
+ }
+ else
+ {
+ *bptr = '\\';
+ bptr ++;
+ *bptr = '\\';
+ bptr ++;
+ Target->BufUsed += 2;
+ }
+ break;
+ case '\b':
*bptr = '\\';
bptr ++;
+ *bptr = 'b';
+ bptr ++;
+ Target->BufUsed += 2;
+ break;
+ case '\f':
*bptr = '\\';
bptr ++;
+ *bptr = 'f';
+ bptr ++;
Target->BufUsed += 2;
- }
- else{
- *bptr = *aptr;
- bptr++;
- Target->BufUsed ++;
+ break;
+ case '\t':
+ *bptr = '\\';
+ bptr ++;
+ *bptr = 't';
+ bptr ++;
+ Target->BufUsed += 2;
+ break;
+ default:
+ IsUtf8Sequence = Ctdl_GetUtf8SequenceLength(aptr, eiptr);
+ while (IsUtf8Sequence > 0){
+ *bptr = *aptr;
+ Target->BufUsed ++;
+ if (--IsUtf8Sequence)
+ aptr++;
+ bptr++;
+ }
}
aptr ++;
}
eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in... */
bptr = Target->buf + Target->BufUsed;
}
- if (*aptr == '<') {
- memcpy(bptr, "<", 4);
+ switch (*aptr) {
+ case '<':
+ memcpy(bptr, HKEY("<"));
bptr += 4;
Target->BufUsed += 4;
- }
- else if (*aptr == '>') {
- memcpy(bptr, ">", 4);
+ break;
+ case '>':
+ memcpy(bptr, HKEY(">"));
bptr += 4;
Target->BufUsed += 4;
- }
- else if (*aptr == '&') {
- memcpy(bptr, "&", 5);
+ break;
+ case '&':
+ memcpy(bptr, HKEY("&"));
bptr += 5;
Target->BufUsed += 5;
- }
- else if (*aptr == LB) {
+ break;
+ case LB:
*bptr = '<';
bptr ++;
Target->BufUsed ++;
- }
- else if (*aptr == RB) {
+ break;
+ case RB:
*bptr = '>';
bptr ++;
Target->BufUsed ++;
- }
- else if ((*aptr == 32) && (nbsp == 1)) {
- memcpy(bptr, " ", 6);
- bptr += 6;
- Target->BufUsed += 6;
- }
- else if ((*aptr == '\n') && (nolinebreaks == 1)) {
- *bptr='\0'; /* nothing */
- }
- else if ((*aptr == '\n') && (nolinebreaks == 2)) {
- memcpy(bptr, "<br/>", 11);
- bptr += 11;
- Target->BufUsed += 11;
- }
-
- else if ((*aptr == '\r') && (nolinebreaks != 0)) {
- *bptr='\0'; /* nothing */
- }
-
- else if ((*aptr == '"') || (*aptr == QU)) {
+ break;
+ case '\n':
+ switch (nolinebreaks) {
+ case 1:
+ *bptr='\0'; /* nothing */
+ break;
+ case 2:
+ memcpy(bptr, HKEY("<br/>"));
+ bptr += 11;
+ Target->BufUsed += 11;
+ break;
+ default:
+ memcpy(bptr, HKEY("\\n"));
+ bptr += 2;
+ Target->BufUsed += 2;
+ }
+ break;
+ case '\r':
+ switch (nolinebreaks) {
+ case 1:
+ case 2:
+ *bptr='\0'; /* nothing */
+ break;
+ default:
+ memcpy(bptr, HKEY("\\r"));
+ bptr += 2;
+ Target->BufUsed += 2;
+ break;
+ }
+ break;
+ case '"':
+ case QU:
*bptr = '\\';
bptr ++;
*bptr = '"';
bptr ++;
Target->BufUsed += 2;
- } else if (*aptr == '\\') {
+ break;
+ case '\\':
+ if ((*(aptr + 1) == 'u') &&
+ isxdigit(*(aptr + 2)) &&
+ isxdigit(*(aptr + 3)) &&
+ isxdigit(*(aptr + 4)) &&
+ isxdigit(*(aptr + 5)))
+ { /* oh, a unicode escaper. let it pass through. */
+ memcpy(bptr, aptr, 6);
+ aptr += 5;
+ bptr +=6;
+ Target->BufUsed += 6;
+ }
+ else
+ {
+ *bptr = '\\';
+ bptr ++;
+ *bptr = '\\';
+ bptr ++;
+ Target->BufUsed += 2;
+ }
+ break;
+ case '\b':
*bptr = '\\';
bptr ++;
+ *bptr = 'b';
+ bptr ++;
+ Target->BufUsed += 2;
+ break;
+ case '\f':
*bptr = '\\';
bptr ++;
+ *bptr = 'f';
+ bptr ++;
Target->BufUsed += 2;
- }
- else {
- if (((unsigned char)*aptr) >= 0x20)
- {
- IsUtf8Sequence = Ctdl_GetUtf8SequenceLength(aptr, eiptr);
-
+ break;
+ case '\t':
+ *bptr = '\\';
+ bptr ++;
+ *bptr = 't';
+ bptr ++;
+ Target->BufUsed += 2;
+ break;
+ case 32:
+ if (nbsp == 1) {
+ memcpy(bptr, HKEY(" "));
+ bptr += 6;
+ Target->BufUsed += 6;
+ break;
+ }
+ default:
+ IsUtf8Sequence = Ctdl_GetUtf8SequenceLength(aptr, eiptr);
+ while (IsUtf8Sequence > 0){
*bptr = *aptr;
Target->BufUsed ++;
- while (IsUtf8Sequence > 1){
- if(bptr + IsUtf8Sequence >= eptr) {
- IncreaseBuf(Target, 1, -1);
- eptr = Target->buf + Target->BufSize - 11; /* our biggest unit to put in... */
- bptr = Target->buf + Target->BufUsed - 1;
- }
- bptr++; aptr++;
- IsUtf8Sequence --;
- *bptr = *aptr;
- Target->BufUsed ++;
- }
+ if (--IsUtf8Sequence)
+ aptr++;
bptr++;
}
-
}
aptr ++;
}
if ((*target)->BufUsed + 4 >= (*target)->BufSize)
IncreaseBuf(*target, 1, 0);
ch = (unsigned char) source->buf[i];
- if ((ch < 32) || (ch > 126) || (ch == 61)) {
+ if ((ch < 32) ||
+ (ch > 126) ||
+ (ch == 61) ||
+ (ch == '=') ||
+ (ch == '[') ||
+ (ch == ']') )
+ {
sprintf(&(*target)->buf[(*target)->BufUsed], "=%02X", ch);
(*target)->BufUsed += 3;
}
StrBuf *EncBuf)
{
StrBuf *Target;
- int need_to_encode;
-
const char *pch, *pche;
const char *UserStart, *UserEnd, *EmailStart, *EmailEnd, *At;
if ((Recp == NULL) || (StrLength(Recp) == 0))
return NULL;
- need_to_encode = 0;
pch = ChrPtr(Recp);
pche = pch + StrLength(Recp);
while ((pch != NULL) && (pch < pche))
{
- int ColonOk = 0;
-
while (isspace(*pch)) pch++;
UserStart = UserEnd = EmailStart = EmailEnd = NULL;
if (EmailEnd == NULL)
EmailEnd = pche;
pch = EmailEnd + 1;
- ColonOk = 1;
}
else {
int gt = 0;
* @ingroup StrBuf
* @brief evaluate the length of an utf8 special character sequence
* @param Char the character to examine
- * @returns width of utf8 chars in bytes
+ * @returns width of utf8 chars in bytes; if the sequence is broken 0 is returned; 1 if its simply ASCII.
*/
static inline int Ctdl_GetUtf8SequenceLength(const char *CharS, const char *CharE)
{
- int n = 1;
- char test = (1<<7);
-
- while ((n < 8) && ((test & *CharS) != 0)) {
- test = test << 1;
+ int n = 0;
+ unsigned char test = (1<<7);
+
+ if ((*CharS & 0xC0) == 0)
+ return 1;
+
+ while ((n < 8) &&
+ ((test & ((unsigned char)*CharS)) != 0))
+ {
+ test = test >> 1;
n ++;
}
if ((n > 6) || ((CharE - CharS) < n))
- n = 1;
+ n = 0;
return n;
}
int StrBufReadBLOB(StrBuf *Buf, int *fd, int append, long nBytes, const char **Error)
{
int fdflags;
- int len, rlen, slen;
+ int rlen;
int nSuccessLess;
int nRead = 0;
char *ptr;
ptr = Buf->buf + Buf->BufUsed;
- slen = len = Buf->BufUsed;
-
fdflags = fcntl(*fd, F_GETFL);
IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
nSuccessLess = 0;
int check,
const char **Error)
{
- const char *pche;
const char *pos;
int fdflags;
int len = 0;
- int rlen, slen;
+ int rlen;
int nRead = 0;
int nAlreadyRead = 0;
int IsNonBlock;
char *ptr;
fd_set rfds;
- const char *pch;
struct timeval tv;
int nSuccessLess = 0;
int MaxTries;
(pos != NULL) &&
(pos < IOBuf->buf + IOBuf->BufUsed))
{
- pche = IOBuf->buf + IOBuf->BufUsed;
- pch = pos;
-
if (rlen < nBytes) {
memcpy(Blob->buf + Blob->BufUsed, pos, rlen);
Blob->BufUsed += rlen;
IncreaseBuf(IOBuf, 0, nBytes - nRead);
ptr = IOBuf->buf;
- slen = len = Blob->BufUsed;
+ len = Blob->BufUsed;
fdflags = fcntl(*fd, F_GETFL);
IsNonBlock = (fdflags & O_NONBLOCK) == O_NONBLOCK;
* @param Buf BLOB with lines of text...
* @param Ptr moved arround to keep the next-line across several iterations
* has to be &NULL on start; will be &NotNULL on end of buffer
- * @returns size of copied buffer
+ * @returns size of remaining buffer
*/
-int StrBufSipLine(StrBuf *LineBuf, StrBuf *Buf, const char **Ptr)
+int StrBufSipLine(StrBuf *LineBuf, const StrBuf *Buf, const char **Ptr)
{
const char *aptr, *ptr, *eptr;
char *optr, *xptr;