- Char/ShiftBuffer: Fixed multi-char string search
- UtilCore: Always pass output buffer & remove own buffer for conversions
- UtilCore: Remove optional parameters to force passing of buffer
- LogCore: Add TempBuffer for output conversion (using UtilCore)
- ApplicationCore: Allow setting size of TempBuffer via config
- Optimize use of {}
252 lines
7.3 KiB
C++
252 lines
7.3 KiB
C++
/*
|
|
* UtilCore.cpp
|
|
*
|
|
* Created on: 14 April 2019
|
|
* Author: wentzelc
|
|
*/
|
|
|
|
// Standard C/C++ Libraries
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <ctype.h>
|
|
|
|
// redA Libraries
|
|
#include "UtilCore.h"
|
|
|
|
//---------------------------------------------------------------------------
|
|
|
|
char * BytesToSafeStr( const char * Bytes, const int Len, const bool NoCrLf, const char SpecChar, char * OutBuf )
|
|
{
|
|
char * BufPos;
|
|
|
|
// Validate output buffer
|
|
if (!OutBuf)
|
|
return NULL;
|
|
else
|
|
BufPos = OutBuf;
|
|
|
|
// Remove special char
|
|
for (int i=0; i<Len; i++) {
|
|
if ((Bytes[i] == '\r') || (Bytes[i] == '\n')) {
|
|
*BufPos = (NoCrLf)? SpecChar : Bytes[i];
|
|
}
|
|
else if (((Bytes[i] < 32) || (Bytes[i] > 126)) ||
|
|
(NoCrLf && ((Bytes[i] == '\r') || (Bytes[i] == '\n')))) {
|
|
*BufPos = SpecChar;
|
|
}
|
|
else {
|
|
*BufPos = Bytes[i];
|
|
}
|
|
BufPos++;
|
|
}
|
|
*BufPos = 0;
|
|
return OutBuf;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
char * BytesToHexStr( const char * Bytes, const int Len, const char * Separator, char * OutBuf )
|
|
{
|
|
char * BufPos;
|
|
bool First = true;
|
|
char SepLen = (Separator)? strlen(Separator) : 0;
|
|
|
|
// Validate output buffer
|
|
if (!OutBuf)
|
|
return NULL;
|
|
else
|
|
BufPos = OutBuf;
|
|
|
|
// Print Hex values of individual bytes
|
|
for (int i=0; i<Len; i++) {
|
|
sprintf( BufPos, "%s%02X", ((First || !Separator)? "" : Separator), (unsigned char)Bytes[i] );
|
|
BufPos += (First)? 2 : 2+SepLen;
|
|
First = false;
|
|
}
|
|
*BufPos = 0;
|
|
return OutBuf;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
char * BytesToBinStr( const char * Bytes, const int Len, const char * Separator, char * OutBuf )
|
|
{
|
|
char * BufPos;
|
|
bool First = true;
|
|
char SepLen = (Separator)? strlen(Separator) : 0;
|
|
|
|
// Validate output buffer
|
|
if (!OutBuf)
|
|
return NULL;
|
|
else
|
|
BufPos = OutBuf;
|
|
|
|
// Print each byte as 8-bit binary
|
|
for (int i=0; i<Len; i++) {
|
|
sprintf( BufPos, "%s%c%c%c%c%c%c%c%c", ((First || !Separator)? "" : Separator),
|
|
(Bytes[i] & 0x80)?'1':'0', (Bytes[i] & 0x40)?'1':'0', (Bytes[i] & 0x20)?'1':'0', (Bytes[i] & 0x10)?'1':'0',
|
|
(Bytes[i] & 0x08)?'1':'0', (Bytes[i] & 0x04)?'1':'0', (Bytes[i] & 0x02)?'1':'0', (Bytes[i] & 0x01)?'1':'0' );
|
|
BufPos += (First)? 8 : 8+SepLen;
|
|
First = false;
|
|
}
|
|
*BufPos = 0;
|
|
return OutBuf;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
char * IntToBinStr( const int Bytes, const int Len, const char * Separator, char * OutBuf )
|
|
{
|
|
char * BufPos;
|
|
bool First = true;
|
|
char SepLen = (Separator)? strlen(Separator) : 0;
|
|
u_int8_t Byte;
|
|
|
|
// Validate output buffer
|
|
if (!OutBuf)
|
|
return NULL;
|
|
else
|
|
BufPos = OutBuf;
|
|
|
|
// Print each byte as 8-bit binary
|
|
for (int i=Len-1; i>=0; i--) {
|
|
Byte = (Bytes >> (i*8)) & 0xFF;
|
|
sprintf( BufPos, "%s%c%c%c%c%c%c%c%c", ((First || !Separator)? "" : Separator),
|
|
(Byte & 0x80)?'1':'0', (Byte & 0x40)?'1':'0', (Byte & 0x20)?'1':'0', (Byte & 0x10)?'1':'0',
|
|
(Byte & 0x08)?'1':'0', (Byte & 0x04)?'1':'0', (Byte & 0x02)?'1':'0', (Byte & 0x01)?'1':'0' );
|
|
BufPos += (First)? 8 : 8+SepLen;
|
|
First = false;
|
|
}
|
|
*BufPos = 0;
|
|
return OutBuf;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
char * HexStrToBytes( const char * Str, const int Len, const char * Separator, char * OutBuf )
|
|
{
|
|
u_int8_t * BufPos;
|
|
char * StrPos = (char*)Str;
|
|
char SepLen = (Separator)? strlen(Separator) : 0;
|
|
|
|
// Validate output buffer
|
|
if (!OutBuf)
|
|
return NULL;
|
|
else
|
|
BufPos = (u_int8_t*)OutBuf;
|
|
|
|
// Print Hex values of individual bytes
|
|
while ((StrPos - Str < Len) && isxdigit(StrPos[0]) && isxdigit(StrPos[1])) {
|
|
*BufPos = (HexToInt(StrPos[0]) << 4) + HexToInt(StrPos[1]);
|
|
BufPos++;
|
|
StrPos += 2;
|
|
if (Separator) {
|
|
if (memcmp( StrPos, Separator, SepLen ))
|
|
break;
|
|
else
|
|
StrPos += SepLen;
|
|
}
|
|
}
|
|
*BufPos = 0;
|
|
return OutBuf;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
char * StrSearch( const char * Haystack, const char * Needle, const int HaystackLen, const int NeedleLen )
|
|
{
|
|
if ((Haystack == NULL) || (Needle == NULL))
|
|
return NULL;
|
|
|
|
char * hPos = (char*)Haystack;
|
|
char * nPos = (char*)Needle;
|
|
int hLen = (!HaystackLen)? strlen(Haystack) : HaystackLen;
|
|
int nLen = (!NeedleLen)? strlen(Needle) : NeedleLen;
|
|
char * Mark = NULL;
|
|
|
|
while ((hPos = (char*)memchr( hPos, *nPos, (hLen-(hPos-Haystack)) ))) { // Check if first char match
|
|
Mark = hPos;
|
|
hPos++; nPos++;
|
|
while ((hPos - Haystack < hLen) && (nPos - Needle < nLen) && (*hPos == *nPos)) { // Check remain char match
|
|
hPos++; nPos++;
|
|
}
|
|
if (nPos - Needle == nLen) return Mark; // Found it!
|
|
hPos = Mark+1; // Continue search
|
|
nPos = (char*)Needle;
|
|
}
|
|
return NULL;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
bool UrlEncode( const char * Input, char * Output, char ** EndPos )
|
|
{
|
|
int c;
|
|
char h[] = "0123456789abcdef";
|
|
char * Ipos = (char*)Input;
|
|
char * Opos = Output;
|
|
|
|
if (!Ipos || !Opos) {
|
|
if (*EndPos) *EndPos = NULL;
|
|
return false;
|
|
}
|
|
|
|
while ((c = *Ipos)) {
|
|
if ((('a' <= c) && (c <= 'z')) ||
|
|
(('A' <= c) && (c <= 'Z')) ||
|
|
(('0' <= c) && (c <= '9')) ||
|
|
(c == '-') || (c == '_') || (c == '.')) {
|
|
*Opos = *Ipos;
|
|
Opos++;
|
|
}
|
|
else if (c == ' ') {
|
|
*Opos = '+';
|
|
Opos++;
|
|
}
|
|
else {
|
|
Opos[0] = '%';
|
|
Opos[1] = h[c >> 4];
|
|
Opos[2] = h[c & 0x0f];
|
|
Opos += 3;
|
|
}
|
|
Ipos++;
|
|
}
|
|
*Opos = 0;
|
|
if (EndPos) *EndPos = Opos;
|
|
return true;
|
|
}
|
|
//---------------------------------------------------------------------------
|
|
|
|
bool UrlDecode( const char * Input, char * Output, char ** EndPos )
|
|
{
|
|
char c, c1, c2;
|
|
char * Ipos = (char*)Input;
|
|
char * Opos = Output;
|
|
|
|
if (!Ipos || !Opos) {
|
|
if (EndPos) *EndPos = Output;
|
|
return false;
|
|
}
|
|
|
|
while ((c = *Ipos)) {
|
|
if (c == '%') {
|
|
if (!(c1 = tolower(Ipos[1])) ||
|
|
!(c2 = tolower(Ipos[2])) ||
|
|
!isxdigit(c1) || !isxdigit(c2))
|
|
return false;
|
|
c1 = (c1 <= '9')? (c1 - '0') : (c1 - 'a' + 10);
|
|
c2 = (c2 <= '9')? (c2 - '0') : (c2 - 'a' + 10);
|
|
*Opos = 16*c1 + c2;
|
|
Ipos += 3;
|
|
}
|
|
else if (c == '+') {
|
|
*Opos = ' ';
|
|
Ipos++;
|
|
}
|
|
else {
|
|
*Opos = c;
|
|
Ipos++;
|
|
}
|
|
Opos++;
|
|
}
|
|
*Opos = 0;
|
|
if (EndPos) *EndPos = Opos;
|
|
return true;
|
|
}
|
|
//---------------------------------------------------------------------------
|