From ddfc1cdee41a1732f22f2b7a54902d91e91b7110 Mon Sep 17 00:00:00 2001 From: cvs Date: Tue, 12 Sep 2000 13:09:29 +0000 Subject: [PATCH] DM. 12-Sep-2000. Add MakeMemPrintable entry point. --- hardsup/makeprint.c | 213 +++++++++++++++++++++++++++++--------------- 1 file changed, 143 insertions(+), 70 deletions(-) diff --git a/hardsup/makeprint.c b/hardsup/makeprint.c index d155498c..a5e6be6b 100644 --- a/hardsup/makeprint.c +++ b/hardsup/makeprint.c @@ -49,8 +49,11 @@ ** ** #include ** -** MakePrint - ensure all characters in a buffer are printable. -** MakePrintable - extended version of MakePrint. +** MakeCharPrintable - routine used by MakePrintable and MakeMemPrintable. +** 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) ** --------- @@ -69,6 +72,44 @@ ** and then replaces any non-printing character with a ".". A trailing ** "\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) ** ------------- ** Input Args: @@ -83,14 +124,27 @@ ** Routines called: ** none ** Description: -** The routine copies all the characters in "in" to "out", ensuring that -** "out" only contains printable characters. -** If the parity bit of a char is set, the char is prefixed with "^", -** any "^" chars get replaced by "\^". -** Then, if the result is non-printable, it is replaced by "\" and its -** octal value. Standard C-language control chars get replaced with -** the corresponding printable string, e.g. is "\n". Any "\" is -** replaced by "\\". +** The routine converts characters in the "in" string to a printable +** representation using MakeCharPrintable and copies them to "out" until +** a null is detected. +**--------------------------------------------------------------------------- +** char *MakeMemPrintable (*out, out_size, *in, in_len) +** ---------------- +** Input Args: +** 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 #include #include +#ifdef FORTIFY + #include +#endif #include @@ -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. */ char *MakePrint (char *chr) { @@ -134,70 +255,22 @@ **-------------------------------------------------------------------------- ** MakePrintable: improved version of MakePrint. */ - char *MakePrintable (char *out, int out_size, char *in) { + char *MakePrintable ( /* ============= -*/ - int len, i, j; - char *my_tmp1, *my_tmp2; +*/ char *out, + int out_size, + char *in) { - len = strlen (in); + int i; + char *pntr; - my_tmp1 = malloc (5 * len + 1); /* Get plenty of working space */ - if (my_tmp1 == NULL) { - out[0] = NIL; return out; + if (out_size <= 0) return out; + + while ((out_size > 1) && (*in != NIL)) { + i = MakeCharPrintable (out, (out_size - 1), *in); + in++; out += i; out_size -= i; } - - 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); + *out = NIL; return out; } /*-------------------------------------------- End of MakePrint.C =======*/