2005-11-17 09:05:55 +00:00
|
|
|
/**
|
2006-10-31 12:27:59 +00:00
|
|
|
* @file chunk.c
|
|
|
|
*
|
|
|
|
* @brief Pointer/lenght abstraction and its functions.
|
|
|
|
*
|
2005-11-17 09:05:55 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
2006-07-07 08:49:06 +00:00
|
|
|
* Copyright (C) 2005-2006 Martin Willi
|
|
|
|
* Copyright (C) 2005 Jan Hutter
|
2005-11-17 09:05:55 +00:00
|
|
|
* Hochschule fuer Technik Rapperswil
|
|
|
|
*
|
|
|
|
* 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. See <http://www.fsf.org/copyleft/gpl.txt>.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2006-04-10 08:07:38 +00:00
|
|
|
|
2006-07-05 10:09:42 +00:00
|
|
|
#include <stdio.h>
|
2005-11-17 09:05:55 +00:00
|
|
|
|
2006-10-31 12:27:59 +00:00
|
|
|
#include "chunk.h"
|
2005-11-17 09:05:55 +00:00
|
|
|
|
2006-10-31 12:27:59 +00:00
|
|
|
#include <printf_hook.h>
|
2005-11-17 09:05:55 +00:00
|
|
|
|
2005-12-06 13:07:06 +00:00
|
|
|
/**
|
|
|
|
* Empty chunk.
|
|
|
|
*/
|
2006-10-31 12:27:59 +00:00
|
|
|
chunk_t chunk_empty = { NULL, 0 };
|
2006-04-10 08:07:38 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Described in header.
|
|
|
|
*/
|
2007-02-12 15:56:47 +00:00
|
|
|
chunk_t chunk_create(u_char *ptr, size_t len)
|
|
|
|
{
|
|
|
|
chunk_t chunk = {ptr, len};
|
|
|
|
return chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Described in header.
|
|
|
|
*/
|
|
|
|
chunk_t chunk_create_clone(u_char *ptr, chunk_t chunk)
|
2006-04-10 08:07:38 +00:00
|
|
|
{
|
2006-10-31 12:27:59 +00:00
|
|
|
chunk_t clone = chunk_empty;
|
2006-04-10 08:07:38 +00:00
|
|
|
|
|
|
|
if (chunk.ptr && chunk.len > 0)
|
|
|
|
{
|
2007-02-12 15:56:47 +00:00
|
|
|
clone.ptr = ptr;
|
2006-04-10 08:07:38 +00:00
|
|
|
clone.len = chunk.len;
|
|
|
|
memcpy(clone.ptr, chunk.ptr, chunk.len);
|
|
|
|
}
|
|
|
|
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
2006-07-27 12:18:40 +00:00
|
|
|
/**
|
|
|
|
* Decribed in header.
|
|
|
|
*/
|
2007-02-12 15:56:47 +00:00
|
|
|
size_t chunk_length(const char* mode, ...)
|
2006-07-27 12:18:40 +00:00
|
|
|
{
|
|
|
|
va_list chunks;
|
2007-02-12 15:56:47 +00:00
|
|
|
size_t length = 0;
|
|
|
|
|
2006-07-27 12:18:40 +00:00
|
|
|
va_start(chunks, mode);
|
2007-02-12 15:56:47 +00:00
|
|
|
while (TRUE)
|
2006-07-27 12:18:40 +00:00
|
|
|
{
|
2007-02-12 15:56:47 +00:00
|
|
|
switch (*mode++)
|
|
|
|
{
|
|
|
|
case 'm':
|
|
|
|
case 'c':
|
|
|
|
{
|
|
|
|
chunk_t ch = va_arg(chunks, chunk_t);
|
|
|
|
length += ch.len;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
2006-07-27 12:18:40 +00:00
|
|
|
}
|
|
|
|
va_end(chunks);
|
2007-02-12 15:56:47 +00:00
|
|
|
return length;
|
|
|
|
}
|
2006-07-27 12:18:40 +00:00
|
|
|
|
2007-02-12 15:56:47 +00:00
|
|
|
/**
|
|
|
|
* Decribed in header.
|
|
|
|
*/
|
|
|
|
chunk_t chunk_create_cat(u_char *ptr, const char* mode, ...)
|
|
|
|
{
|
|
|
|
va_list chunks;
|
|
|
|
chunk_t construct = chunk_create(ptr, 0);
|
|
|
|
|
2006-07-27 12:18:40 +00:00
|
|
|
va_start(chunks, mode);
|
2007-02-12 15:56:47 +00:00
|
|
|
while (TRUE)
|
2006-07-27 12:18:40 +00:00
|
|
|
{
|
2007-02-12 15:56:47 +00:00
|
|
|
bool free_chunk = FALSE;
|
2006-07-27 12:18:40 +00:00
|
|
|
switch (*mode++)
|
|
|
|
{
|
|
|
|
case 'm':
|
2007-02-12 15:56:47 +00:00
|
|
|
{
|
|
|
|
free_chunk = TRUE;
|
|
|
|
}
|
2006-07-27 12:18:40 +00:00
|
|
|
case 'c':
|
2007-02-12 15:56:47 +00:00
|
|
|
{
|
|
|
|
chunk_t ch = va_arg(chunks, chunk_t);
|
|
|
|
memcpy(ptr, ch.ptr, ch.len);
|
|
|
|
ptr += ch.len;
|
|
|
|
construct.len += ch.len;
|
|
|
|
if (free_chunk)
|
|
|
|
{
|
|
|
|
free(ch.ptr);
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2006-07-27 12:18:40 +00:00
|
|
|
default:
|
2007-02-12 15:56:47 +00:00
|
|
|
break;
|
2006-07-27 12:18:40 +00:00
|
|
|
}
|
2007-02-12 15:56:47 +00:00
|
|
|
break;
|
2006-07-27 12:18:40 +00:00
|
|
|
}
|
|
|
|
va_end(chunks);
|
|
|
|
|
|
|
|
return construct;
|
|
|
|
}
|
|
|
|
|
2007-02-12 15:56:47 +00:00
|
|
|
/**
|
|
|
|
* Decribed in header.
|
|
|
|
*/
|
|
|
|
void chunk_split(chunk_t chunk, const char *mode, ...)
|
|
|
|
{
|
|
|
|
va_list chunks;
|
|
|
|
size_t len;
|
|
|
|
chunk_t *ch;
|
|
|
|
|
|
|
|
va_start(chunks, mode);
|
|
|
|
while (TRUE)
|
|
|
|
{
|
|
|
|
if (*mode == '\0')
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
len = va_arg(chunks, size_t);
|
|
|
|
ch = va_arg(chunks, chunk_t*);
|
|
|
|
/* a null chunk means skip len bytes */
|
|
|
|
if (ch == NULL)
|
|
|
|
{
|
|
|
|
chunk = chunk_skip(chunk, len);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
switch (*mode++)
|
|
|
|
{
|
|
|
|
case 'm':
|
|
|
|
{
|
|
|
|
ch->len = min(chunk.len, len);
|
|
|
|
if (ch->len)
|
|
|
|
{
|
|
|
|
ch->ptr = chunk.ptr;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ch->ptr = NULL;
|
|
|
|
}
|
|
|
|
chunk = chunk_skip(chunk, ch->len);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case 'a':
|
|
|
|
{
|
|
|
|
ch->len = min(chunk.len, len);
|
|
|
|
if (ch->len)
|
|
|
|
{
|
|
|
|
ch->ptr = malloc(ch->len);
|
|
|
|
memcpy(ch->ptr, chunk.ptr, ch->len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ch->ptr = NULL;
|
|
|
|
}
|
|
|
|
chunk = chunk_skip(chunk, ch->len);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
case 'c':
|
|
|
|
{
|
|
|
|
ch->len = min(ch->len, chunk.len);
|
|
|
|
ch->len = min(ch->len, len);
|
|
|
|
if (ch->len)
|
|
|
|
{
|
|
|
|
memcpy(ch->ptr, chunk.ptr, ch->len);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ch->ptr = NULL;
|
|
|
|
}
|
|
|
|
chunk = chunk_skip(chunk, ch->len);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
va_end(chunks);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-04-10 08:07:38 +00:00
|
|
|
/**
|
|
|
|
* Described in header.
|
|
|
|
*/
|
|
|
|
void chunk_free(chunk_t *chunk)
|
|
|
|
{
|
|
|
|
free(chunk->ptr);
|
|
|
|
chunk->ptr = NULL;
|
|
|
|
chunk->len = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Described in header.
|
|
|
|
*/
|
2007-02-12 15:56:47 +00:00
|
|
|
chunk_t chunk_skip(chunk_t chunk, size_t bytes)
|
2006-04-10 08:07:38 +00:00
|
|
|
{
|
2007-02-12 15:56:47 +00:00
|
|
|
if (chunk.len > bytes)
|
|
|
|
{
|
|
|
|
chunk.ptr += bytes;
|
|
|
|
chunk.len -= bytes;
|
|
|
|
return chunk;
|
|
|
|
}
|
|
|
|
return chunk_empty;
|
2006-04-10 08:07:38 +00:00
|
|
|
}
|
|
|
|
|
2007-03-27 04:37:15 +00:00
|
|
|
/**
|
|
|
|
* Described in header.
|
|
|
|
*/
|
|
|
|
int chunk_compare(chunk_t a, chunk_t b)
|
|
|
|
{
|
|
|
|
int compare_len = a.len - b.len;
|
|
|
|
int len = (compare_len < 0)? a.len : b.len;
|
|
|
|
|
|
|
|
if (compare_len != 0 || len == 0)
|
|
|
|
{
|
|
|
|
return compare_len;
|
|
|
|
}
|
|
|
|
return memcmp(a.ptr, b.ptr, len);
|
|
|
|
};
|
|
|
|
|
2006-04-19 11:40:48 +00:00
|
|
|
/**
|
|
|
|
* Described in header.
|
|
|
|
*/
|
|
|
|
bool chunk_equals(chunk_t a, chunk_t b)
|
|
|
|
{
|
2006-06-16 05:53:47 +00:00
|
|
|
return a.ptr != NULL && b.ptr != NULL &&
|
2006-10-31 12:27:59 +00:00
|
|
|
a.len == b.len && memeq(a.ptr, b.ptr, a.len);
|
2006-06-16 05:53:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Described in header.
|
|
|
|
*/
|
|
|
|
bool chunk_equals_or_null(chunk_t a, chunk_t b)
|
|
|
|
{
|
|
|
|
if (a.ptr == NULL || b.ptr == NULL)
|
|
|
|
return TRUE;
|
|
|
|
return a.len == b.len && memeq(a.ptr, b.ptr, a.len);
|
2006-04-19 11:40:48 +00:00
|
|
|
}
|
2006-04-10 08:07:38 +00:00
|
|
|
|
2006-09-27 14:15:49 +00:00
|
|
|
/**
|
|
|
|
* Number of bytes per line to dump raw data
|
|
|
|
*/
|
|
|
|
#define BYTES_PER_LINE 16
|
|
|
|
|
|
|
|
/**
|
|
|
|
* output handler in printf() for byte ranges
|
|
|
|
*/
|
|
|
|
static int print_bytes(FILE *stream, const struct printf_info *info,
|
|
|
|
const void *const *args)
|
|
|
|
{
|
|
|
|
char *bytes = *((void**)(args[0]));
|
|
|
|
int len = *((size_t*)(args[1]));
|
|
|
|
|
|
|
|
char buffer[BYTES_PER_LINE * 3];
|
|
|
|
char ascii_buffer[BYTES_PER_LINE + 1];
|
|
|
|
char *buffer_pos = buffer;
|
|
|
|
char *bytes_pos = bytes;
|
|
|
|
char *bytes_roof = bytes + len;
|
|
|
|
int line_start = 0;
|
|
|
|
int i = 0;
|
2006-10-18 11:46:13 +00:00
|
|
|
int written = 0;
|
2006-09-27 14:15:49 +00:00
|
|
|
|
2006-10-18 11:46:13 +00:00
|
|
|
written += fprintf(stream, "=> %d bytes @ %p", len, bytes);
|
2006-09-27 14:15:49 +00:00
|
|
|
|
|
|
|
while (bytes_pos < bytes_roof)
|
|
|
|
{
|
|
|
|
static char hexdig[] = "0123456789ABCDEF";
|
|
|
|
|
|
|
|
*buffer_pos++ = hexdig[(*bytes_pos >> 4) & 0xF];
|
|
|
|
*buffer_pos++ = hexdig[ *bytes_pos & 0xF];
|
|
|
|
|
|
|
|
ascii_buffer[i++] =
|
|
|
|
(*bytes_pos > 31 && *bytes_pos < 127) ? *bytes_pos : '.';
|
|
|
|
|
|
|
|
if (++bytes_pos == bytes_roof || i == BYTES_PER_LINE)
|
|
|
|
{
|
|
|
|
int padding = 3 * (BYTES_PER_LINE - i);
|
|
|
|
int written;
|
|
|
|
|
|
|
|
while (padding--)
|
|
|
|
{
|
|
|
|
*buffer_pos++ = ' ';
|
|
|
|
}
|
|
|
|
*buffer_pos++ = '\0';
|
|
|
|
ascii_buffer[i] = '\0';
|
|
|
|
|
2006-10-18 11:46:13 +00:00
|
|
|
written += fprintf(stream, "\n%4d: %s %s",
|
2006-10-31 12:27:59 +00:00
|
|
|
line_start, buffer, ascii_buffer);
|
2006-10-18 11:46:13 +00:00
|
|
|
|
2006-09-27 14:15:49 +00:00
|
|
|
|
|
|
|
buffer_pos = buffer;
|
|
|
|
line_start += BYTES_PER_LINE;
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*buffer_pos++ = ' ';
|
|
|
|
}
|
|
|
|
}
|
2006-10-18 11:46:13 +00:00
|
|
|
return written;
|
2006-09-27 14:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* output handler in printf() for chunks
|
|
|
|
*/
|
|
|
|
static int print_chunk(FILE *stream, const struct printf_info *info,
|
|
|
|
const void *const *args)
|
|
|
|
{
|
|
|
|
chunk_t *chunk = *((chunk_t**)(args[0]));
|
2006-10-18 11:46:13 +00:00
|
|
|
bool first = TRUE;
|
|
|
|
chunk_t copy = *chunk;
|
|
|
|
int written = 0;
|
2006-09-27 14:15:49 +00:00
|
|
|
|
2006-10-18 11:46:13 +00:00
|
|
|
if (!info->alt)
|
|
|
|
{
|
|
|
|
const void *new_args[] = {&chunk->ptr, &chunk->len};
|
|
|
|
return print_bytes(stream, info, new_args);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (copy.len > 0)
|
|
|
|
{
|
|
|
|
if (first)
|
|
|
|
{
|
|
|
|
first = FALSE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
written += fprintf(stream, ":");
|
|
|
|
}
|
2006-10-25 08:36:28 +00:00
|
|
|
written += fprintf(stream, "%02x", *copy.ptr++);
|
2006-10-18 11:46:13 +00:00
|
|
|
copy.len--;
|
|
|
|
}
|
|
|
|
return written;
|
2006-09-27 14:15:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2006-10-31 12:27:59 +00:00
|
|
|
* register printf() handlers
|
2006-05-29 07:02:12 +00:00
|
|
|
*/
|
2006-10-18 11:46:13 +00:00
|
|
|
static void __attribute__ ((constructor))print_register()
|
2006-05-29 07:02:12 +00:00
|
|
|
{
|
2006-10-31 12:27:59 +00:00
|
|
|
register_printf_function(PRINTF_CHUNK, print_chunk, arginfo_ptr);
|
|
|
|
register_printf_function(PRINTF_BYTES, print_bytes, arginfo_ptr_int);
|
2006-05-29 07:02:12 +00:00
|
|
|
}
|