/*
* Copyright (c) 2006 Sendmail, Inc. and its suppliers.
* All rights reserved.
*
* By using this file, you agree to the terms and conditions set
* forth in the LICENSE file which can be found at the top level of
* the sendmail distribution.
*
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <sm/sendmail.h>
#include <ctype.h>
/*
** STR2PRT -- convert "unprintable" characters in a string to \oct
**
** Parameters:
** s -- string to convert
**
** Returns:
** converted string.
** This is a static local buffer, string must be copied
** before this function is called again!
*/
char *
str2prt(s)
char *s;
{
int l;
char c, *h;
bool ok;
static int len = 0;
if (s == NULL)
return NULL;
ok = true;
for (h = s, l = 1; *h != '\0'; h++, l++)
{
if (*h == '\\')
{
++l;
ok = false;
}
{
l += 3;
ok = false;
}
}
if (ok)
return s;
if (l > len)
{
len = l;
}
for (h = buf; *s != '\0' && l > 0; s++, l--)
{
c = *s;
{
*h++ = c;
}
else
{
*h++ = '\\';
--l;
switch (c)
{
case '\\':
*h++ = '\\';
break;
case '\t':
*h++ = 't';
break;
case '\n':
*h++ = 'n';
break;
case '\r':
*h++ = 'r';
break;
default:
SM_ASSERT(l >= 2);
(void) sm_snprintf(h, l, "%03o",
(unsigned int)((unsigned char) c));
/*
** XXX since l is unsigned this may
** wrap around if the calculation is screwed
** up...
*/
l -= 2;
h += 3;
break;
}
}
}
*h = '\0';
return buf;
}
/*
** QUOTE_INTERNAL_CHARS -- do quoting of internal characters
**
** Necessary to make sure that we don't have metacharacters such
** as the internal versions of "$*" or "$&" in a string.
** The input and output pointers can be the same.
**
** Parameters:
** ibp -- a pointer to the string to translate
** obp -- a pointer to an output buffer
** bsp -- pointer to the length of the output buffer
**
** Returns:
** A possibly new bp (if the buffer needed to grow); if
** it is different, *bsp will updated to the size of
** the new buffer and the caller is responsible for
** freeing the memory.
*/
char *
char *ibp;
char *obp;
int *bsp;
{
needs_quoting = false;
/* determine length of output string (starts at 1 for trailing '\0') */
{
if (SM_MM_QUOTE(*ip))
{
olen++;
needs_quoting = true;
}
}
/* is the output buffer big enough? */
{
buffer_same = false;
}
/*
** shortcut: no change needed?
** Note: we don't check this first as some bozo may use the same
** buffers but restrict the size of the output buffer to less
** than the length of the input buffer in which case we need to
** allocate a new buffer.
*/
if (!needs_quoting)
{
if (!buffer_same)
{
}
return obp;
}
if (buffer_same)
{
buffer_same = false;
}
{
if (SM_MM_QUOTE(*ip))
{
}
}
return obp;
}
/*
** DEQUOTE_INTERNAL_CHARS -- undo the effect of quote_internal_chars
**
** Parameters:
** ibp -- a pointer to the string to be translated.
** obp -- a pointer to the output buffer. Can be the
** same as ibp.
** obs -- the size of the output buffer.
**
** Returns:
** number of character added to obp
*/
int
char *ibp;
char *obp;
int obs;
{
int len;
bool quoted;
quoted = false;
len = 0;
{
{
quoted = true;
continue;
}
{
++len;
}
quoted = false;
}
*op = '\0';
return len;
}