DM. 12-Sep-2000. Add MakeMemPrintable entry point.
This commit is contained in:
@ -49,8 +49,11 @@
|
|||||||
**
|
**
|
||||||
** #include <sinq_prototypes.h>
|
** #include <sinq_prototypes.h>
|
||||||
**
|
**
|
||||||
** MakePrint - ensure all characters in a buffer are printable.
|
** MakeCharPrintable - routine used by MakePrintable and MakeMemPrintable.
|
||||||
** MakePrintable - extended version of MakePrint.
|
** MakeMemPrintable - version of MakePrintable which will handle
|
||||||
|
** buffers containing a NUL character.
|
||||||
|
** MakePrint - ensure all characters in a buffer are printable.
|
||||||
|
** MakePrintable - extended version of MakePrint.
|
||||||
**---------------------------------------------------------------------
|
**---------------------------------------------------------------------
|
||||||
** char *MakePrint (*text)
|
** char *MakePrint (*text)
|
||||||
** ---------
|
** ---------
|
||||||
@ -69,6 +72,44 @@
|
|||||||
** and then replaces any non-printing character with a ".". A trailing
|
** and then replaces any non-printing character with a ".". A trailing
|
||||||
** "\n" or "\r" is removed.
|
** "\n" or "\r" is removed.
|
||||||
**---------------------------------------------------------------------------
|
**---------------------------------------------------------------------------
|
||||||
|
** int *MakeCharPrintable (*out, out_size, in)
|
||||||
|
** -----------------
|
||||||
|
** Input Args:
|
||||||
|
** char in -- the character to be converted.
|
||||||
|
** int out_size -- the size of the out buffer.
|
||||||
|
** Output Args:
|
||||||
|
** char *out -- buffer to hold the converted text.
|
||||||
|
** Modified Args:
|
||||||
|
** none
|
||||||
|
** Return status:
|
||||||
|
** The number of characters put into the output buffer.
|
||||||
|
** Routines called:
|
||||||
|
** none
|
||||||
|
** Description:
|
||||||
|
** The routine puts a printable version of the character "in" into the
|
||||||
|
** "out" buffer. The printable version is generated as follows:
|
||||||
|
**
|
||||||
|
** a) If the parity bit of a char is set, a "^" is inserted into the
|
||||||
|
** output buffer, the parity bit of the char is cleared and processed
|
||||||
|
** further.
|
||||||
|
** b) If the char is "^", "\^" is inserted into the output buffer.
|
||||||
|
** c) If the char is "\", "\\" is inserted into the output buffer.
|
||||||
|
** d) If the char is a standard C-language control char, it gets replaced
|
||||||
|
** by a recognised backslash escape sequence. The following are
|
||||||
|
** recognised:
|
||||||
|
** NUL 0x00 --> \0
|
||||||
|
** BEL 0x07 --> \a
|
||||||
|
** BS 0x08 --> \b
|
||||||
|
** HT 0x09 --> \t
|
||||||
|
** LF 0x0a --> \n
|
||||||
|
** VT 0x0b --> \v
|
||||||
|
** FF 0x0c --> \f
|
||||||
|
** CR 0x0d --> \r
|
||||||
|
** e) If the character is printable (i.e. between " "/0x20 and "~"/0x7e
|
||||||
|
** inclusive), it is inserted into the output buffer as is.
|
||||||
|
** f) Anything else gets inserted as "\xxx", where xxx is the octal
|
||||||
|
** representation of the character.
|
||||||
|
**---------------------------------------------------------------------------
|
||||||
** char *MakePrintable (*out, out_size, *in)
|
** char *MakePrintable (*out, out_size, *in)
|
||||||
** -------------
|
** -------------
|
||||||
** Input Args:
|
** Input Args:
|
||||||
@ -83,14 +124,27 @@
|
|||||||
** Routines called:
|
** Routines called:
|
||||||
** none
|
** none
|
||||||
** Description:
|
** Description:
|
||||||
** The routine copies all the characters in "in" to "out", ensuring that
|
** The routine converts characters in the "in" string to a printable
|
||||||
** "out" only contains printable characters.
|
** representation using MakeCharPrintable and copies them to "out" until
|
||||||
** If the parity bit of a char is set, the char is prefixed with "^",
|
** a null is detected.
|
||||||
** any "^" chars get replaced by "\^".
|
**---------------------------------------------------------------------------
|
||||||
** Then, if the result is non-printable, it is replaced by "\" and its
|
** char *MakeMemPrintable (*out, out_size, *in, in_len)
|
||||||
** octal value. Standard C-language control chars get replaced with
|
** ----------------
|
||||||
** the corresponding printable string, e.g. <LF> is "\n". Any "\" is
|
** Input Args:
|
||||||
** replaced by "\\".
|
** int out_size -- the size of the out buffer.
|
||||||
|
** char *in -- the text to be converted.
|
||||||
|
** int in_len -- the number of characters to be converted.
|
||||||
|
** Output Args:
|
||||||
|
** char *out -- buffer to hold the converted text.
|
||||||
|
** Modified Args:
|
||||||
|
** none
|
||||||
|
** Return status:
|
||||||
|
** A pointer to "out".
|
||||||
|
** Routines called:
|
||||||
|
** none
|
||||||
|
** Description:
|
||||||
|
** The routine is the same as MakePrintable, except that it converts
|
||||||
|
** a given number of characters rather than a null terminated string.
|
||||||
**============================================================================*/
|
**============================================================================*/
|
||||||
/*
|
/*
|
||||||
**---------------------------------------------------------------------------
|
**---------------------------------------------------------------------------
|
||||||
@ -99,6 +153,9 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#ifdef FORTIFY
|
||||||
|
#include <fortify/fortify.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
#include <sinq_prototypes.h>
|
#include <sinq_prototypes.h>
|
||||||
|
|
||||||
@ -108,6 +165,70 @@
|
|||||||
*/
|
*/
|
||||||
/*
|
/*
|
||||||
**--------------------------------------------------------------------------
|
**--------------------------------------------------------------------------
|
||||||
|
** MakeCharPrintable: makes a single character printable.
|
||||||
|
*/
|
||||||
|
int MakeCharPrintable (char *out, int out_size, char in) {
|
||||||
|
/* =================
|
||||||
|
**
|
||||||
|
** Return value is number of chars put into *out.
|
||||||
|
*/
|
||||||
|
char buff[8], *pntr;
|
||||||
|
|
||||||
|
pntr = buff;
|
||||||
|
|
||||||
|
if ((in & 0x80) != 0) { /* Parity bit set? */
|
||||||
|
*pntr++ = '^'; /* Yes. Put a '^' in the buffer .. */
|
||||||
|
in = in & 0x7f; /* .. and remove the parity bit. */
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (in) {
|
||||||
|
case '^': *pntr++ = '\\'; *pntr++ = '^'; break;
|
||||||
|
case '\\': *pntr++ = '\\'; *pntr++ = '\\'; break;
|
||||||
|
case '\000': *pntr++ = '\\'; *pntr++ = '0'; break;
|
||||||
|
case '\007': *pntr++ = '\\'; *pntr++ = 'a'; break;
|
||||||
|
case '\010': *pntr++ = '\\'; *pntr++ = 'b'; break;
|
||||||
|
case '\011': *pntr++ = '\\'; *pntr++ = 't'; break;
|
||||||
|
case '\012': *pntr++ = '\\'; *pntr++ = 'n'; break;
|
||||||
|
case '\013': *pntr++ = '\\'; *pntr++ = 'v'; break;
|
||||||
|
case '\014': *pntr++ = '\\'; *pntr++ = 'f'; break;
|
||||||
|
case '\015': *pntr++ = '\\'; *pntr++ = 'r'; break;
|
||||||
|
default:
|
||||||
|
if ((in < ' ') || (in > '~')) {
|
||||||
|
pntr += sprintf (pntr, "\\%03.3o", in);
|
||||||
|
}else {
|
||||||
|
*pntr++ = in;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
out_size = (out_size > (pntr - buff)) ? (pntr - buff) : out_size;
|
||||||
|
memcpy (out, buff, out_size);
|
||||||
|
return out_size;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
**--------------------------------------------------------------------------
|
||||||
|
** MakeMemPrintable: alternative version of MakePrintable.
|
||||||
|
*/
|
||||||
|
char *MakeMemPrintable (
|
||||||
|
/* ================
|
||||||
|
*/ char *out,
|
||||||
|
int out_size,
|
||||||
|
char *in,
|
||||||
|
int in_len) {
|
||||||
|
|
||||||
|
int i;
|
||||||
|
char *pntr;
|
||||||
|
|
||||||
|
if (out_size <= 0) return out;
|
||||||
|
|
||||||
|
while ((out_size > 1) && (in_len > 0)) {
|
||||||
|
i = MakeCharPrintable (out, (out_size - 1), *in);
|
||||||
|
out += i; out_size -= i;
|
||||||
|
in++; in_len--;
|
||||||
|
}
|
||||||
|
*out = NIL;
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
/*
|
||||||
|
**--------------------------------------------------------------------------
|
||||||
** MakePrint: Make all characters in a buffer printable.
|
** MakePrint: Make all characters in a buffer printable.
|
||||||
*/
|
*/
|
||||||
char *MakePrint (char *chr) {
|
char *MakePrint (char *chr) {
|
||||||
@ -134,70 +255,22 @@
|
|||||||
**--------------------------------------------------------------------------
|
**--------------------------------------------------------------------------
|
||||||
** MakePrintable: improved version of MakePrint.
|
** MakePrintable: improved version of MakePrint.
|
||||||
*/
|
*/
|
||||||
char *MakePrintable (char *out, int out_size, char *in) {
|
char *MakePrintable (
|
||||||
/* =============
|
/* =============
|
||||||
*/
|
*/ char *out,
|
||||||
int len, i, j;
|
int out_size,
|
||||||
char *my_tmp1, *my_tmp2;
|
char *in) {
|
||||||
|
|
||||||
len = strlen (in);
|
int i;
|
||||||
|
char *pntr;
|
||||||
|
|
||||||
my_tmp1 = malloc (5 * len + 1); /* Get plenty of working space */
|
if (out_size <= 0) return out;
|
||||||
if (my_tmp1 == NULL) {
|
|
||||||
out[0] = NIL; return out;
|
while ((out_size > 1) && (*in != NIL)) {
|
||||||
|
i = MakeCharPrintable (out, (out_size - 1), *in);
|
||||||
|
in++; out += i; out_size -= i;
|
||||||
}
|
}
|
||||||
|
*out = NIL;
|
||||||
my_tmp2 = malloc (5 * len + 1);
|
|
||||||
if (my_tmp2 == NULL) {
|
|
||||||
free (my_tmp1); out[0] = NIL; return out;
|
|
||||||
}
|
|
||||||
/*
|
|
||||||
** Process the parity bits, "\" and "^" first.
|
|
||||||
*/
|
|
||||||
i = 0; j = 0;
|
|
||||||
while (in[i] != NIL) {
|
|
||||||
if ((in[i] & 0x80) != 0) {
|
|
||||||
my_tmp1[j] = '^'; j++;
|
|
||||||
my_tmp1[j] = in[i] & 0x7f;
|
|
||||||
}else if (in[i] == '\\') {
|
|
||||||
my_tmp1[j] = '\\'; j++;
|
|
||||||
my_tmp1[j] = '\\';
|
|
||||||
}else if (in[i] == '^') {
|
|
||||||
my_tmp1[j] = '\\'; j++;
|
|
||||||
my_tmp1[j] = '^';
|
|
||||||
}else {
|
|
||||||
my_tmp1[j] = in[i];
|
|
||||||
}
|
|
||||||
i++; j++;
|
|
||||||
}
|
|
||||||
my_tmp1[j] = NIL;
|
|
||||||
/*
|
|
||||||
** Now do the non-printing stuff.
|
|
||||||
*/
|
|
||||||
i = 0; j = 0;
|
|
||||||
while (my_tmp1[i] != NIL) {
|
|
||||||
if ((my_tmp1[i] < ' ') || (my_tmp1[i] == 0x7f)) {
|
|
||||||
my_tmp2[j] = '\\', j++;
|
|
||||||
switch (my_tmp1[i]) {
|
|
||||||
case '\007': my_tmp2[j] = 'a'; break;
|
|
||||||
case '\010': my_tmp2[j] = 'b'; break;
|
|
||||||
case '\011': my_tmp2[j] = 't'; break;
|
|
||||||
case '\012': my_tmp2[j] = 'n'; break;
|
|
||||||
case '\013': my_tmp2[j] = 'v'; break;
|
|
||||||
case '\014': my_tmp2[j] = 'f'; break;
|
|
||||||
case '\015': my_tmp2[j] = 'r'; break;
|
|
||||||
default: sprintf (&my_tmp2[j], "%3o", my_tmp1[i]); j += 2; break;
|
|
||||||
}
|
|
||||||
}else {
|
|
||||||
my_tmp2[j] = my_tmp1[i];
|
|
||||||
}
|
|
||||||
i++; j++;
|
|
||||||
}
|
|
||||||
my_tmp2[j] = NIL;
|
|
||||||
|
|
||||||
StrJoin (out, out_size, my_tmp2, "");
|
|
||||||
|
|
||||||
free (my_tmp1); free (my_tmp2);
|
|
||||||
return out;
|
return out;
|
||||||
}
|
}
|
||||||
/*-------------------------------------------- End of MakePrint.C =======*/
|
/*-------------------------------------------- End of MakePrint.C =======*/
|
||||||
|
Reference in New Issue
Block a user