static FILE *debug_fp = NULL;
static int debug_got_console = 0;
-void dprintf(char *fmt, ...) {
- char buf[2048];
+static void dputs (char *buf) {
DWORD dw;
- va_list ap;
if (!debug_got_console) {
AllocConsole();
debug_fp = fopen("debug.log", "w");
}
- va_start(ap, fmt);
- vsprintf(buf, fmt, ap);
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf, strlen(buf), &dw, NULL);
fputs(buf, debug_fp);
fflush(debug_fp);
+}
+
+
+void dprintf(char *fmt, ...) {
+ char buf[2048];
+ va_list ap;
+
+ va_start(ap, fmt);
+ vsprintf(buf, fmt, ap);
+ dputs (buf);
va_end(ap);
}
-#endif
+
+
+void debug_memdump (void *buf, int len, int L) {
+ int i;
+ unsigned char *p = buf;
+ if (L) {
+ int delta;
+ dprintf ("\t%d (0x%x) bytes:\n", len, len);
+ delta = 15 & (int) p;
+ p -= delta;
+ len += delta;
+ }
+ for (; 0 < len; p += 16, len -= 16) {
+ dputs ("\t");
+ if (L) dprintf ("%p: ", p);
+ for (i = 0; i < 16 && i < len; ++i) {
+ if (&p[i] < (unsigned char *) buf) {
+ dputs (" "); /* 3 spaces */
+ } else {
+ dprintf (
+ "%c%02.2x",
+ &p[i] != (unsigned char *) buf && i % 4 ? '.' : ' ',
+ p[i]
+ );
+ }
+ }
+ dputs ("\n");
+ }
+}
+
+#endif /* def DEBUG */
+
--- /dev/null
+#ifndef PUTTY_MISC_H
+#define PUTTY_MISC_H
+
+#include "puttymem.h"
+
+
+/*
+ * Debugging functions.
+ *
+ * Output goes to debug.log
+ *
+ * debug(()) (note the double brackets) is like printf().
+ *
+ * dmemdump() and dmemdumpl() both do memory dumps. The difference
+ * is that dmemdumpl() is more suited for when where the memory is is
+ * important (say because you'll be recording pointer values later
+ * on). dmemdump() is more concise.
+ */
+
+#ifdef DEBUG
+void dprintf(char *fmt, ...);
+void debug_memdump (void *buf, int len, int L);
+#define debug(x) (dprintf x)
+#define dmemdump(buf,len) debug_memdump (buf, len, 0);
+#define dmemdumpl(buf,len) debug_memdump (buf, len, 1);
+#else
+#define debug(x)
+#define dmemdump(buf,len)
+#define dmemdumpl(buf,len)
+#endif
+
+
+#ifndef lenof
+#define lenof(x) ( (sizeof((x))) / (sizeof(*(x))))
+#endif
+
+
+#endif
* Exports from misc.c.
*/
-#include "puttymem.h"
+#include "misc.h"
/*
* Exports from version.c.
void agent_query(void *in, int inlen, void **out, int *outlen);
int agent_exists(void);
-#ifdef DEBUG
-void dprintf(char *fmt, ...);
-#define debug(x) (dprintf x)
-#else
-#define debug(x)
-#endif
#endif
#ifndef PUTTY_PUTTYMEM_H
#define PUTTY_PUTTYMEM_H
+#include <stddef.h> /* for size_t */
+#include <string.h> /* for memcpy() */
+
+
/* #define MALLOC_LOG do this if you suspect putty of leaking memory */
#ifdef MALLOC_LOG
#define smalloc(z) (mlog(__FILE__,__LINE__), safemalloc(z))
void *saferealloc(void *, size_t);
void safefree(void *);
+
+/* smalloc a thing */
+#define smalloca(type) ((type *) smalloc (sizeof (type)))
+/* smalloc a copy of a thing */
+#define smallocc(ptr) memcpy (smalloc (sizeof (*ptr)), ptr, sizeof (*ptr))
+/* smalloc n things */
+#define smallocn(n,type) ((type *) smalloc ((n) * sizeof (type)))
+
+
#endif