/* $Header: /CVSROOT/mon/mondump.c,v 1.2 2004/09/13 01:21:17 tino Exp $ * * mondump: Transform protocol into cleartext * * This is a rough dumper just to show that the protocol works. * It has memory leaks etc., it's just a quick hack. * * Copyright (C)2004 Valentin Hilbig, webmaster@scylla-charybdis.com * * 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 * * $Log: mondump.c,v $ * Revision 1.2 2004/09/13 01:21:17 tino * preparing dist * * Revision 1.1 2004/09/13 01:14:43 tino * first version without network monitoring */ #define _GNU_SOURCE #include #include #include #include #include #include "mon_version.h" #define INTERNAL end("internal error %s:%d", __FILE__, __LINE__) static void end(const char *s, ...) { va_list list; printf("[[["); va_start(list, s); vprintf(s, list); printf("]]]\n"); exit(0); } static const char * my_strdup(const char *s) { char *p; p = strdup(s); if (!p) end("out of memory"); return p; } static void * my_realloc(void *old, size_t len) { return (old ? realloc(old, len) : malloc(len)); } static int get(void) { int c; if ((c=getchar())==EOF) end("EOF"); #if 0 printf("[%02x]", c); fflush(stdout); #endif return c&0xff; } static unsigned long long get_num_sub(int len) { unsigned long long u; int i; u=0; for (i=len; i>=0; i--) { u <<= 8; u |= get(); } return u; } #define MAX_INDEXES 10 static int index_pos; static unsigned long long index_val[MAX_INDEXES]; static unsigned long long get_num(unsigned long long last, int prefix) { for (;;) { unsigned long long u; int c, i; c = get(); if (c<0xe0) return last+c; switch (c) { default: u = get_num_sub(c&0x7); switch (c&0xf8) { default: INTERNAL; case 0xe0: return last+u+0xe0; case 0xe8: return last-u; case 0xf0: return u; } break; case 0xfa: if (prefix) { i = index_pos; break; } case 0xfb: if (prefix) { i = index_pos-1; break; } case 0xfc: if (prefix) { i = index_pos-2; break; } case 0xfd: if (prefix) { index_pos = 0; continue; } end("0x%02x not allowed here", c); case 0xf8: case 0xf9: case 0xfe: case 0xff: end("0x%02x not implemented", c); continue; } if (i>=MAX_INDEXES) end("index nesting too deep: %d", i); index_val[i] = get_num(index_val[i], 0); index_pos = i+1; } } static unsigned get_token(void) { static unsigned long long u; return u=get_num(u, 1); } static int get_str(char **s) { unsigned long long u; int len; u = get_num(0, 0); len = u; if (u!=len) end("string length out of bounds"); *s = my_realloc(*s, len+1); (*s)[len] = 0; if (fread(*s, len, 1, stdin)!=1) end("fread EOF %d", len); return len; } static void dump_str(const char *s, int len) { int tic, i; printf("%d'", len); tic = 1; for (i=0; iMAXTOKENS || !token[tok].name) end("wrong define token %d!\n", tok); preset.name = token[tok].name; preset.type = token[tok].type; printf("{reuse %u %d}", tok, index_pos); continue; } printf("{idx %d}", index_pos); } define |= 1; len = get_str(&s); if (len<2) end("oops, token name too short: %d", len); if (strlen(s)!=len) end("oops, funny string: '%s'", s); printf("{define %s}", s); preset.type = *s++; preset.name = s; s = 0; /* s was used */ continue; } if (tok>MAXTOKENS) end("more than %d tokens!\n", MAXTOKENS); if (define) { if (token[tok].name) end("token %d multiple defined: was '%s', now '%s'\n", tok, token[tok].name, preset.name); token[tok] = preset; define = 0; } if (!token[tok].name) end("token %d used undefined\n", tok); printf("%s", token[tok].name); if (index_pos || token[tok].idx) { putchar('<'); for (i=0; i'); } putchar('='); switch (token[tok].type) { default: end("illegal token type: %c", token[tok].type); case 's': len = get_str(&s); dump_str(s, len); printf("\n"); break; case 'u': u = get_num(token[tok].last, 0); token[tok].last = u; printf("%Lu\n", u); break; } fflush(stdout); } return 0; }