/* strutil.c * String utility routines * * $Id: strutil.c,v 1.2 2000/09/11 20:05:13 gram Exp $ * * Ethereal - Network traffic analyzer * By Gerald Combs * Copyright 1998 Gerald Combs * * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #ifdef HAVE_CONFIG_H # include "config.h" #endif #include #include #include #include #include "strutil.h" /* * Given a pointer into a data buffer, and to the end of the buffer, * find the end of the (putative) line at that position in the data * buffer. * Return a pointer to the EOL character(s) in "*eol". */ const u_char * find_line_end(const u_char *data, const u_char *dataend, const u_char **eol) { const u_char *lineend; lineend = memchr(data, '\n', dataend - data); if (lineend == NULL) { /* * No LF - line is probably continued in next TCP segment. */ lineend = dataend; *eol = dataend; } else { /* * Is the LF at the beginning of the line? */ if (lineend > data) { /* * No - is it preceded by a carriage return? * (Perhaps it's supposed to be, but that's not guaranteed....) */ if (*(lineend - 1) == '\r') { /* * Yes. The EOL starts with the CR. */ *eol = lineend - 1; } else { /* * No. The EOL starts with the LF. */ *eol = lineend; /* * I seem to remember that we once saw lines ending with LF-CR * in an HTTP request or response, so check if it's *followed* * by a carriage return. */ if (lineend < (dataend - 1) && *(lineend + 1) == '\r') { /* * It's ; say it ends with the CR. */ lineend++; } } } /* * Point to the character after the last character. */ lineend++; } return lineend; } /* * Get the length of the next token in a line, and the beginning of the * next token after that (if any). * Return 0 if there is no next token. */ int get_token_len(const u_char *linep, const u_char *lineend, const u_char **next_token) { const u_char *tokenp; int token_len; tokenp = linep; /* * Search for a blank, a CR or an LF, or the end of the buffer. */ while (linep < lineend && *linep != ' ' && *linep != '\r' && *linep != '\n') linep++; token_len = linep - tokenp; /* * Skip trailing blanks. */ while (linep < lineend && *linep == ' ') linep++; *next_token = linep; return token_len; } #define MAX_COLUMNS_LINE_DETAIL 62 /* * Given a string, generate a string from it that shows non-printable * characters as C-style escapes, and return a pointer to it. */ gchar * format_text(const u_char *string, int len) { static gchar fmtbuf[MAX_COLUMNS_LINE_DETAIL + 3 + 4 + 1]; gchar *fmtbufp; int column; const u_char *stringend = string + len; u_char c; int i; column = 0; fmtbufp = &fmtbuf[0]; while (string < stringend) { if (column >= MAX_COLUMNS_LINE_DETAIL) { /* * Put "..." and quit. */ strcpy(fmtbufp, " ..."); fmtbufp += 4; break; } c = *string++; if (isprint(c)) { *fmtbufp++ = c; column++; } else { *fmtbufp++ = '\\'; column++; switch (c) { case '\\': *fmtbufp++ = '\\'; column++; break; case '\a': *fmtbufp++ = 'a'; column++; break; case '\b': *fmtbufp++ = 'b'; column++; break; case '\f': *fmtbufp++ = 'f'; column++; break; case '\n': *fmtbufp++ = 'n'; column++; break; case '\r': *fmtbufp++ = 'r'; column++; break; case '\t': *fmtbufp++ = 't'; column++; break; case '\v': *fmtbufp++ = 'v'; column++; break; default: i = (c>>6)&03; *fmtbufp++ = i + '0'; column++; i = (c>>3)&07; *fmtbufp++ = i + '0'; column++; i = (c>>0)&07; *fmtbufp++ = i + '0'; column++; break; } } } *fmtbufp = '\0'; return fmtbuf; }