added a chunk_printable() function (replaces sanitize_chunk)
This commit is contained in:
@@ -19,6 +19,7 @@
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
#include <errno.h>
|
||||
#include <ctype.h>
|
||||
|
||||
#include "chunk.h"
|
||||
|
||||
@@ -441,6 +442,32 @@ int chunk_compare(chunk_t a, chunk_t b)
|
||||
return memcmp(a.ptr, b.ptr, len);
|
||||
};
|
||||
|
||||
/**
|
||||
* Remove non-printable characters from a chunk.
|
||||
*/
|
||||
bool chunk_printable(chunk_t chunk, chunk_t *sane, char replace)
|
||||
{
|
||||
bool printable = TRUE;
|
||||
int i;
|
||||
|
||||
if (sane)
|
||||
{
|
||||
*sane = chunk_clone(chunk);
|
||||
}
|
||||
for (i = 0; i < chunk.len; i++)
|
||||
{
|
||||
if (!isprint(chunk.ptr[i]))
|
||||
{
|
||||
if (sane)
|
||||
{
|
||||
sane->ptr[i] = replace;
|
||||
}
|
||||
printable = FALSE;
|
||||
}
|
||||
}
|
||||
return printable;
|
||||
}
|
||||
|
||||
/**
|
||||
* Described in header.
|
||||
*
|
||||
|
||||
@@ -231,6 +231,19 @@ static inline bool chunk_equals(chunk_t a, chunk_t b)
|
||||
a.len == b.len && memeq(a.ptr, b.ptr, a.len);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a chunk has printable characters only.
|
||||
*
|
||||
* If sane is given, chunk is cloned into sane and all non printable characters
|
||||
* get replaced by "replace".
|
||||
*
|
||||
* @param chunk chunk to check for printability
|
||||
* @param sane pointer where sane version is allocated, or NULL
|
||||
* @param replace character to use for replaceing unprintable characters
|
||||
* @return TRUE if all characters in chunk are printable
|
||||
*/
|
||||
bool chunk_printable(chunk_t chunk, chunk_t *sane, char replace);
|
||||
|
||||
/**
|
||||
* Computes a 32 bit hash of the given chunk.
|
||||
* Note: This hash is only intended for hash tables not for cryptographic purposes.
|
||||
|
||||
Reference in New Issue
Block a user