Support username/password authentication in SOCKS 5.
[sgt/putty] / misc.c
diff --git a/misc.c b/misc.c
index cb894b3..34e613e 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -51,6 +51,35 @@ char *dupcat(char *s1, ...)
 }
 
 /* ----------------------------------------------------------------------
+ * Base64 encoding routine. This is required in public-key writing
+ * but also in HTTP proxy handling, so it's centralised here.
+ */
+
+void base64_encode_atom(unsigned char *data, int n, char *out)
+{
+    static const char base64_chars[] =
+       "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+
+    unsigned word;
+
+    word = data[0] << 16;
+    if (n > 1)
+       word |= data[1] << 8;
+    if (n > 2)
+       word |= data[2];
+    out[0] = base64_chars[(word >> 18) & 0x3F];
+    out[1] = base64_chars[(word >> 12) & 0x3F];
+    if (n > 1)
+       out[2] = base64_chars[(word >> 6) & 0x3F];
+    else
+       out[2] = '=';
+    if (n > 2)
+       out[3] = base64_chars[word & 0x3F];
+    else
+       out[3] = '=';
+}
+
+/* ----------------------------------------------------------------------
  * Generic routines to deal with send buffers: a linked list of
  * smallish blocks, with the operations
  * 
@@ -294,7 +323,7 @@ static void *minefield_alloc(int size)
     /*
      * Update the admin region.
      */
-    for (i = start + 2; i < start + npages - 1; i++)
+    for (i = start + 2; i < start + npages + 1; i++)
        minefield_admin[i] = 0xFFFE;   /* used but no region starts here */
     minefield_admin[start + 1] = region_start % PAGESIZE;
 
@@ -408,12 +437,14 @@ void *safemalloc(size_t size)
 #ifdef MALLOC_LOG
        sprintf(str, "Out of memory! (%s:%d, size=%d)",
                mlog_file, mlog_line, size);
+       fprintf(fp, "*** %s\n", str);
+       fclose(fp);
 #else
        strcpy(str, "Out of memory!");
 #endif
        MessageBox(NULL, str, "PuTTY Fatal Error",
                   MB_SYSTEMMODAL | MB_ICONERROR | MB_OK);
-       exit(1);
+       cleanup_exit(1);
     }
 #ifdef MALLOC_LOG
     if (fp)
@@ -443,12 +474,14 @@ void *saferealloc(void *ptr, size_t size)
 #ifdef MALLOC_LOG
        sprintf(str, "Out of memory! (%s:%d, size=%d)",
                mlog_file, mlog_line, size);
+       fprintf(fp, "*** %s\n", str);
+       fclose(fp);
 #else
        strcpy(str, "Out of memory!");
 #endif
        MessageBox(NULL, str, "PuTTY Fatal Error",
                   MB_SYSTEMMODAL | MB_ICONERROR | MB_OK);
-       exit(1);
+       cleanup_exit(1);
     }
 #ifdef MALLOC_LOG
     if (fp)