-// Copyright (c) 1987-2022 by the citadel.org team
+// Copyright (c) 1987-2023 by the citadel.org team
//
-// This program is open source software; you can redistribute it and/or modify
-// it under the terms of the GNU General Public License as published by
-// the Free Software Foundation; either version 3 of the License, or
-// (at your option) any later version.
-//
-// This program is distributed in the hope that it will be useful,
-// but WITHOUT ANY WARRANTY; without even the implied warranty of
-// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-// GNU General Public License for more details.
-//
-// You should have received a copy of the GNU General Public License
-// along with this program; if not, write to the Free Software
-// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+// This program is open source software. Use, duplication, or disclosure
+// is subject to the terms of the GNU General Public License, version 3.
#define _GNU_SOURCE
#include "sysdep.h"
#ifdef HAVE_ZLIB
#include <zlib.h>
-int ZEXPORT compress_gzip(Bytef * dest, size_t * destLen,
- const Bytef * source, uLong sourceLen, int level);
+int ZEXPORT compress_gzip(Bytef * dest, size_t * destLen, const Bytef * source, uLong sourceLen, int level);
#endif
int BaseStrBufSize = 64;
int EnableSplice = 0;
"F0","F1","F2","F3","F4","F5","F6","F7","F8","F9","FA","FB","FC","FD","FE","FF"};
-/*
- * Private Structure for the Stringbuffer
- */
+// Private Structure for the Stringbuffer
struct StrBuf {
- char *buf; /**< the pointer to the dynamic buffer */
- long BufSize; /**< how many spcae do we optain */
- long BufUsed; /**< StNumber of Chars used excluding the trailing \\0 */
- int ConstBuf; /**< are we just a wrapper arround a static buffer and musn't we be changed? */
+ char *buf; // the pointer to the dynamic buffer
+ long BufSize; // how many spcae do we optain
+ long BufUsed; // Number of Chars used excluding the trailing \\0
+ int ConstBuf; // are we just a wrapper arround a static buffer and musn't we be changed?
#ifdef SIZE_DEBUG
- long nIncreases; /**< for profiling; cound how many times we needed more */
- char bt [SIZ]; /**< Stacktrace of last increase */
- char bt_lastinc [SIZ]; /**< How much did we increase last time? */
+ long nIncreases; // for profiling; cound how many times we needed more
+ char bt [SIZ]; // Stacktrace of last increase
+ char bt_lastinc[SIZ]; // How much did we increase last time?
#endif
};
size_t size, i;
char **strings;
- if (which)
+ if (which) {
pstart = pch = Buf->bt;
- else
+ }
+ else {
pstart = pch = Buf->bt_lastinc;
+ }
size = backtrace(stack_frames, sizeof(stack_frames) / sizeof(void*));
strings = backtrace_symbols(stack_frames, size);
for (i = 0; i < size; i++) {
- if (strings != NULL)
+ if (strings != NULL) {
n = snprintf(pch, SIZ - (pch - pstart), "%s\\n", strings[i]);
- else
+ }
+ else {
n = snprintf(pch, SIZ - (pch - pstart), "%p\\n", stack_frames[i]);
+ }
pch += n;
}
free(strings);
-
-
}
#endif
}
-/**
+/*
* use strbuf as wrapper for a string constant for easy handling
* StringConstant a string to wrap
* SizeOfStrConstant should be sizeof(StringConstant)-1
}
-/**
+/*
* flush the content of a Buf; keep its struct
* buf Buffer to flush
*/
return 0;
}
-/**
+/*
* wipe the content of a Buf thoroughly (overwrite it -> expensive); keep its struct
* buf Buffer to wipe
*/
#ifdef SIZE_DEBUG
int hFreeDbglog = -1;
#endif
-/**
+/*
* Release a Buffer
* Its a double pointer, so it can NULL your pointer
* so fancy SIG11 appear instead of random results
* Simple string transformations *
*******************************************************************************/
-/**
- * Wrapper around atol
- */
-long StrTol(const StrBuf *Buf)
-{
+// Wrapper around atol
+long StrTol(const StrBuf *Buf) {
if (Buf == NULL)
return 0;
if(Buf->BufUsed > 0)
return 0;
}
-/**
- * Wrapper around atoi
- */
-int StrToi(const StrBuf *Buf)
-{
+
+// Wrapper around atoi
+int StrToi(const StrBuf *Buf) {
if (Buf == NULL)
return 0;
if (Buf->BufUsed > 0)
return 0;
}
-/**
- * Checks to see if the string is a pure number
- * Buf The buffer to inspect
- * @returns 1 if its a pure number, 0, if not.
- */
+
+// Checks to see if the string is a pure number
+// Buf The buffer to inspect
+// returns 1 if its a pure number, 0, if not.
int StrBufIsNumber(const StrBuf *Buf) {
char * pEnd;
if ((Buf == NULL) || (Buf->BufUsed == 0)) {
return 0;
}
-/**
- * modifies a Single char of the Buf
- * You can point to it via char* or a zero-based integer
- * Buf The buffer to manipulate
- * ptr char* to zero; use NULL if unused
- * nThChar zero based pointer into the string; use -1 if unused
- * PeekValue The Character to place into the position
- */
-long StrBufPeek(StrBuf *Buf, const char* ptr, long nThChar, char PeekValue)
-{
+
+// modifies a Single char of the Buf
+// You can point to it via char* or a zero-based integer
+// Buf The buffer to manipulate
+// ptr char* to zero; use NULL if unused
+// nThChar zero based pointer into the string; use -1 if unused
+// PeekValue The Character to place into the position
+long StrBufPeek(StrBuf *Buf, const char* ptr, long nThChar, char PeekValue) {
if (Buf == NULL)
return -1;
if (ptr != NULL)
return nThChar;
}
-/**
- * modifies a range of chars of the Buf
- * You can point to it via char* or a zero-based integer
- * Buf The buffer to manipulate
- * ptr char* to zero; use NULL if unused
- * nThChar zero based pointer into the string; use -1 if unused
- * nChars how many chars are to be flushed?
- * PookValue The Character to place into that area
- */
-long StrBufPook(StrBuf *Buf, const char* ptr, long nThChar, long nChars, char PookValue)
-{
+
+// modifies a range of chars of the Buf
+// You can point to it via char* or a zero-based integer
+// Buf The buffer to manipulate
+// ptr char* to zero; use NULL if unused
+// nThChar zero based pointer into the string; use -1 if unused
+// nChars how many chars are to be flushed?
+// PookValue The Character to place into that area
+long StrBufPook(StrBuf *Buf, const char* ptr, long nThChar, long nChars, char PookValue) {
if (Buf == NULL)
return -1;
if (ptr != NULL)
nChars = Buf->BufUsed - nThChar;
memset(Buf->buf + nThChar, PookValue, nChars);
- /* just to be shure... */
+ // just to be sure...
Buf->buf[Buf->BufUsed] = 0;
return nChars;
}
-/**
- * Append a StringBuffer to the buffer
- * Buf Buffer to modify
- * AppendBuf Buffer to copy at the end of our buffer
- * Offset Should we start copying from an offset?
- */
-void StrBufAppendBuf(StrBuf *Buf, const StrBuf *AppendBuf, unsigned long Offset)
-{
+
+// Append a StringBuffer to the buffer
+// Buf Buffer to modify
+// AppendBuf Buffer to copy at the end of our buffer
+// Offset Should we start copying from an offset?
+void StrBufAppendBuf(StrBuf *Buf, const StrBuf *AppendBuf, unsigned long Offset) {
if ((AppendBuf == NULL) || (AppendBuf->buf == NULL) ||
(Buf == NULL) || (Buf->buf == NULL))
return;
Buf->buf[Buf->BufUsed] = '\0';
}
-/*
- * sprintf like function appending the formated string to the buffer
- * vsnprintf version to wrap into own calls
- * Buf Buffer to extend by format and Params
- * format printf alike format to add
- * ap va_list containing the items for format
- */
-void StrBufVAppendPrintf(StrBuf *Buf, const char *format, va_list ap)
-{
+
+// sprintf like function appending the formated string to the buffer
+// vsnprintf version to wrap into own calls
+// Buf Buffer to extend by format and Params
+// format printf alike format to add
+// ap va_list containing the items for format
+void StrBufVAppendPrintf(StrBuf *Buf, const char *format, va_list ap) {
va_list apl;
size_t BufSize;
size_t nWritten;
while (newused >= BufSize) {
va_copy(apl, ap);
- nWritten = vsnprintf(Buf->buf + Offset,
- Buf->BufSize - Offset,
- format, apl);
+ nWritten = vsnprintf(Buf->buf + Offset, Buf->BufSize - Offset, format, apl);
va_end(apl);
newused = Offset + nWritten;
if (newused >= Buf->BufSize) {
if (IncreaseBuf(Buf, 1, newused) == -1)
- return; /* TODO: error handling? */
+ return; // TODO: error handling?
newused = Buf->BufSize + 1;
}
else {
}
}
-/**
- * sprintf like function appending the formated string to the buffer
- * Buf Buffer to extend by format and Params
- * format printf alike format to add
- */
-void StrBufAppendPrintf(StrBuf *Buf, const char *format, ...)
-{
+
+// sprintf like function appending the formated string to the buffer
+// Buf Buffer to extend by format and Params
+// format printf alike format to add
+void StrBufAppendPrintf(StrBuf *Buf, const char *format, ...) {
size_t BufSize;
size_t nWritten;
size_t Offset;
while (newused >= BufSize) {
va_start(arg_ptr, format);
- nWritten = vsnprintf(Buf->buf + Buf->BufUsed,
- Buf->BufSize - Buf->BufUsed,
- format, arg_ptr);
+ nWritten = vsnprintf(Buf->buf + Buf->BufUsed, Buf->BufSize - Buf->BufUsed, format, arg_ptr);
va_end(arg_ptr);
newused = Buf->BufUsed + nWritten;
if (newused >= Buf->BufSize) {
if (IncreaseBuf(Buf, 1, newused) == -1)
- return; /* TODO: error handling? */
+ return; // TODO: error handling?
newused = Buf->BufSize + 1;
}
else {
}
}
-/**
- * sprintf like function putting the formated string into the buffer
- * Buf Buffer to extend by format and Parameters
- * format printf alike format to add
- */
-void StrBufPrintf(StrBuf *Buf, const char *format, ...)
-{
+
+// sprintf like function putting the formated string into the buffer
+// Buf Buffer to extend by format and Parameters
+// format printf alike format to add
+void StrBufPrintf(StrBuf *Buf, const char *format, ...) {
size_t nWritten;
va_list arg_ptr;
va_end(arg_ptr);
if (nWritten >= Buf->BufSize) {
if (IncreaseBuf(Buf, 0, 0) == -1)
- return; /* TODO: error handling? */
+ return; // TODO: error handling?
nWritten = Buf->BufSize + 1;
continue;
}
}
}
-/**
- * Callback for cURL to append the webserver reply to a buffer
- * ptr pre-defined by the cURL API; see man 3 curl for mre info
- * size pre-defined by the cURL API; see man 3 curl for mre info
- * nmemb pre-defined by the cURL API; see man 3 curl for mre info
- * stream pre-defined by the cURL API; see man 3 curl for mre info
- */
-size_t CurlFillStrBuf_callback(void *ptr, size_t size, size_t nmemb, void *stream)
-{
+
+// Callback for cURL to append the webserver reply to a buffer
+// ptr, size, nmemb, and stream are pre-defined by the cURL API; see man 3 curl for more info
+size_t CurlFillStrBuf_callback(void *ptr, size_t size, size_t nmemb, void *stream) {
StrBuf *Target;
}
-/**
- * extracts a substring from Source into dest
- * dest buffer to place substring into
- * Source string to copy substring from
- * Offset chars to skip from start
- * nChars number of chars to copy
- * @returns the number of chars copied; may be different from nChars due to the size of Source
- */
-int StrBufSub(StrBuf *dest, const StrBuf *Source, unsigned long Offset, size_t nChars)
-{
+// extracts a substring from Source into dest
+// dest buffer to place substring into
+// Source string to copy substring from
+// Offset chars to skip from start
+// nChars number of chars to copy
+// returns the number of chars copied; may be different from nChars due to the size of Source
+int StrBufSub(StrBuf *dest, const StrBuf *Source, unsigned long Offset, size_t nChars) {
size_t NCharsRemain;
- if (Offset > Source->BufUsed)
- {
+ if (Offset > Source->BufUsed) {
if (dest != NULL)
FlushStrBuf(dest);
return 0;
}
- if (Offset + nChars < Source->BufUsed)
- {
+ if (Offset + nChars < Source->BufUsed) {
if ((nChars >= dest->BufSize) &&
(IncreaseBuf(dest, 0, nChars + 1) == -1))
return 0;
return (*target)->BufUsed;;
}
-/**
- * Quoted-Printable encode a message; make it < 80 columns width.
- * source Source string to be encoded.
- * @returns buffer with encoded message.
- */
-StrBuf *StrBufRFC2047encodeMessage(const StrBuf *EncodeMe)
-{
+// Quoted-Printable encode a message; make it < 80 columns width.
+// source Source string to be encoded.
+// returns buffer with encoded message.
+StrBuf *StrBufQuotedPrintableEncode(const StrBuf *EncodeMe) {
StrBuf *OutBuf;
char *Optr, *OEptr;
const char *ptr, *eptr;
eptr = EncodeMe->buf + EncodeMe->BufUsed;
LinePos = 0;
- while (ptr < eptr)
- {
- if (Optr + 4 >= OEptr)
- {
+ while (ptr < eptr) {
+ if (Optr + 4 >= OEptr) {
long Offset;
Offset = Optr - OutBuf->buf;
OutBuf->BufUsed = Optr - OutBuf->buf;
Optr = OutBuf->buf + Offset;
OEptr = OutBuf->buf + OutBuf->BufSize;
}
- if (*ptr == '\r')
- {
- /* ignore carriage returns */
+ if (*ptr == '\r') { // ignore carriage returns
ptr ++;
}
- else if (*ptr == '\n') {
- /* hard line break */
+ else if (*ptr == '\n') { // hard line break
memcpy(Optr, HKEY("=0A"));
Optr += 3;
LinePos += 3;
ptr ++;
}
- else if (( (*ptr >= 32) && (*ptr <= 60) ) ||
- ( (*ptr >= 62) && (*ptr <= 126) ))
- {
+ else if (( (*ptr >= 32) && (*ptr <= 60) ) || ( (*ptr >= 62) && (*ptr <= 126) )) {
*Optr = *ptr;
Optr ++;
ptr ++;
ptr ++;
}
- if (LinePos > 72) {
- /* soft line break */
+ if (LinePos > 72) { // soft line break
if (isspace(*(Optr - 1))) {
ch = *(Optr - 1);
Optr --;
}
-static void AddRecipient(StrBuf *Target,
- StrBuf *UserName,
- StrBuf *EmailAddress,
- StrBuf *EncBuf)
-{
+static void AddRecipient(StrBuf *Target, StrBuf *UserName, StrBuf *EmailAddress, StrBuf *EncBuf) {
int QuoteMe = 0;
if (StrLength(Target) > 0) StrBufAppendBufPlain(Target, HKEY(", "), 0);
* \param EncBuf Temporary buffer for internal use; Please provide valid buffer.
* \returns encoded & sanitized buffer with the contents of Recp; Caller owns this memory.
*/
-StrBuf *StrBufSanitizeEmailRecipientVector(const StrBuf *Recp,
- StrBuf *UserName,
- StrBuf *EmailAddress,
- StrBuf *EncBuf)
-{
+StrBuf *StrBufSanitizeEmailRecipientVector(const StrBuf *Recp, StrBuf *UserName, StrBuf *EmailAddress, StrBuf *EncBuf) {
StrBuf *Target;
const char *pch, *pche;
const char *UserStart, *UserEnd, *EmailStart, *EmailEnd, *At;
* search character to search
* replace character to replace search by
*/
-void StrBufReplaceChars(StrBuf *buf, char search, char replace)
-{
+void StrBufReplaceChars(StrBuf *buf, char search, char replace) {
long i;
if (buf == NULL)
return;
int nSuccessLess = 0;
int MaxTries;
- if ((Blob == NULL) ||
- (*fd == -1) ||
- (IOBuf == NULL) ||
- (Pos == NULL))
- {
+ if ((Blob == NULL) || (*fd == -1) || (IOBuf == NULL) || (Pos == NULL)) {
if (Pos != NULL)
*Pos = NULL;
*Error = ErrRBB_BLOBFPreConditionFailed;
rlen = IOBuf->BufUsed - rlen;
- if ((IOBuf->BufUsed > 0) && (pos != NULL) && (pos < IOBuf->buf + IOBuf->BufUsed))
- {
+ if ((IOBuf->BufUsed > 0) && (pos != NULL) && (pos < IOBuf->buf + IOBuf->BufUsed)) {
if (rlen < nBytes) {
memcpy(Blob->buf + Blob->BufUsed, pos, rlen);
Blob->BufUsed += rlen;
}
}
rlen = read(*fd, ptr, IOBuf->BufSize - (ptr - IOBuf->buf));
- // if (rlen == -1) { 2021feb27 ajc changed this, apparently we will always get at least 1 byte unless the connection is broken
- if (rlen < 1) {
+ if (rlen < 1) { // We will always get at least 1 byte unless the connection is broken
close(*fd);
*fd = -1;
*Error = strerror(errno);
return nRead + nAlreadyRead;
}
-/**
- * extract a "next line" from Buf; Ptr to persist across several iterations
- * LineBuf your line will be copied here.
- * Buf BLOB with lines of text...
- * 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 remaining buffer
- */
-int StrBufSipLine(StrBuf *LineBuf, const StrBuf *Buf, const char **Ptr)
-{
+
+// extract a "next line" from Buf; Ptr to persist across several iterations
+// LineBuf your line will be copied here.
+// Buf BLOB with lines of text...
+// 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 remaining buffer
+int StrBufSipLine(StrBuf *LineBuf, const StrBuf *Buf, const char **Ptr) {
const char *aptr, *ptr, *eptr;
char *optr, *xptr;
- if ((Buf == NULL) ||
- (*Ptr == StrBufNOTNULL) ||
- (LineBuf == NULL)||
- (LineBuf->buf == NULL))
- {
+ if ((Buf == NULL) || (*Ptr == StrBufNOTNULL) || (LineBuf == NULL)|| (LineBuf->buf == NULL)) {
*Ptr = StrBufNOTNULL;
return 0;
}
eptr = Buf->buf + Buf->BufUsed;
xptr = LineBuf->buf + LineBuf->BufSize - 1;
- while ((ptr <= eptr) &&
- (*ptr != '\n') &&
- (*ptr != '\r') )
- {
+ while ((ptr <= eptr) && (*ptr != '\n') && (*ptr != '\r') ) {
*optr = *ptr;
optr++; ptr++;
if (optr == xptr) {
}
-/**
- * removes double slashes from pathnames
- * Dir directory string to filter
- * RemoveTrailingSlash allows / disallows trailing slashes
- */
-void StrBufStripSlashes(StrBuf *Dir, int RemoveTrailingSlash)
-{
+// removes double slashes from pathnames
+// Dir directory string to filter
+// RemoveTrailingSlash allows / disallows trailing slashes
+void StrBufStripSlashes(StrBuf *Dir, int RemoveTrailingSlash) {
char *a, *b;
a = b = Dir->buf;
}
-/*
- * Decode a quoted-printable encoded StrBuf buffer "in place"
- * This is possible because the decoded will always be shorter than the encoded
- * so we don't have to worry about the buffer being to small.
- */
-void StrBufDecodeQP(StrBuf *Buf)
-{
+// Decode a quoted-printable encoded StrBuf buffer "in place"
+// This is possible because the decoded will always be shorter than the encoded
+// so we don't have to worry about the buffer being to small.
+void StrBufDecodeQP(StrBuf *Buf) {
if (!Buf) { // sanity check #1
return;
}