changeset 2245:4e0124f5aee2 vim73

Optimize the blowfish crypt/decrypt code a bit more.
author Bram Moolenaar <bram@vim.org>
date Wed, 02 Jun 2010 20:32:23 +0200
parents caca0ddd789b
children 1e48f569b03d
files runtime/doc/todo.txt src/blowfish.c src/integration.c src/misc2.c src/proto/blowfish.pro src/undo.c src/workshop.c
diffstat 7 files changed, 117 insertions(+), 69 deletions(-) [+]
line wrap: on
line diff
--- a/runtime/doc/todo.txt
+++ b/runtime/doc/todo.txt
@@ -1082,6 +1082,8 @@ restored. (Luc St-Louis)
 
 
 Vim 7.3:
+- undofile: keep markers where the file was written/read, so that it's easy to
+  go back to a saved version of the file  ":earlier 1file"?
 - using NSIS 2.46: install on Windows 7 works, but no "Edit with Vim" menu.
    Use register_shell_extension()? (George Reilly, 2010 May 26)
    Ron's version: http://dev.ronware.org/p/vim/finfo?name=gvim.nsi
@@ -1094,11 +1096,6 @@ Vim 7.3:
     Verify recovery works.
 - Update for crypt code to use salt. (Mohsin May 30)
     Make the strengthen_key value configurable and store it in the header.
-- Do profiling on crypt code to find obvious bottlenecks.
-    bf_ranbyte() and bf_ofb_init() are called for each byte, can they be done
-    inline somehow?
-    -> Add a function in blowfish.c to process an array, called once from
-       crypt_decode() and crypt_encode().
 Patches to include:
 - Include conceal patch?
   http://vince.negri.googlepages.com/
--- a/src/blowfish.c
+++ b/src/blowfish.c
@@ -317,17 +317,17 @@ static UINT32_T sbi[4][256] = {
 
 #define F1(i) \
     xl ^= pax[i]; \
-    xr ^= ((sbx[0][xl>>24] + \
-    sbx[1][(xl&0xFF0000)>>16]) ^ \
-    sbx[2][(xl&0xFF00)>>8]) + \
-    sbx[3][xl&0xFF];
+    xr ^= ((sbx[0][xl >> 24] + \
+    sbx[1][(xl & 0xFF0000) >> 16]) ^ \
+    sbx[2][(xl & 0xFF00) >> 8]) + \
+    sbx[3][xl & 0xFF];
 
 #define F2(i) \
     xr ^= pax[i]; \
-    xl ^= ((sbx[0][xr>>24] + \
-    sbx[1][(xr&0xFF0000)>>16]) ^ \
-    sbx[2][(xr&0xFF00)>>8]) + \
-    sbx[3][xr&0xFF];
+    xl ^= ((sbx[0][xr >> 24] + \
+    sbx[1][(xr & 0xFF0000) >> 16]) ^ \
+    sbx[2][(xr & 0xFF00) >> 8]) + \
+    sbx[3][xr & 0xFF];
 
 
     static void
@@ -339,9 +339,13 @@ bf_e_block(p_xl, p_xr)
 
     F1(0) F2(1) F1(2) F2(3) F1(4) F2(5) F1(6) F2(7)
     F1(8) F2(9) F1(10) F2(11) F1(12) F2(13) F1(14) F2(15)
-    xl ^= pax[16]; xr ^= pax[17];
-    temp = xl; xl = xr; xr = temp;
-    *p_xl = xl; *p_xr = xr;
+    xl ^= pax[16];
+    xr ^= pax[17];
+    temp = xl;
+    xl = xr;
+    xr = temp;
+    *p_xl = xl;
+    *p_xr = xr;
 }
 
 #if 0  /* not used */
@@ -373,7 +377,8 @@ bf_d_block(p_xl, p_xr)
 bf_e_cblock(block)
     char_u *block;
 {
-    block8 bk;
+    block8	bk;
+
     memcpy(bk.uc, block, 8);
     htonl2(bk.ul[0]);
     htonl2(bk.ul[1]);
@@ -552,26 +557,75 @@ bf_ofb_init(iv, iv_len)
     }
 }
 
-    void
-bf_ofb_update(c)
-    int c;
-{
-    ofb_buffer[update_offset++] ^= (char_u)c;
-    if (update_offset == BF_OFB_LEN)
-	update_offset = 0;
+#define BF_OFB_UPDATE(c) { \
+    ofb_buffer[update_offset] ^= (char_u)c; \
+    if (++update_offset == BF_OFB_LEN) \
+	update_offset = 0; \
+}
+
+#define BF_RANBYTE(t) { \
+    if ((randbyte_offset & BF_BLOCK_MASK) == 0) \
+	bf_e_cblock(&ofb_buffer[randbyte_offset]); \
+    t = ofb_buffer[randbyte_offset]; \
+    if (++randbyte_offset == BF_OFB_LEN) \
+	randbyte_offset = 0; \
 }
 
-    int
-bf_ranbyte()
+/*
+ * Encrypt "from[len]" into "to[len]".
+ * "from" and "to" can be equal to encrypt in place.
+ */
+    void
+bf_crypt_encode(from, len, to)
+    char_u	*from;
+    size_t	len;
+    char_u	*to;
 {
-    int b;
+    size_t	i;
+    int		ztemp, t;
+
+    for (i = 0; i < len; ++i)
+    {
+	ztemp = from[i];
+	BF_RANBYTE(t);
+	BF_OFB_UPDATE(ztemp);
+	to[i] = t ^ ztemp;
+    }
+}
 
-    if ((randbyte_offset & BF_BLOCK_MASK) == 0)
-	bf_e_cblock(&ofb_buffer[randbyte_offset]);
-    b = ofb_buffer[randbyte_offset];
-    if (++randbyte_offset == BF_OFB_LEN)
-	randbyte_offset = 0;
-    return b;
+/*
+ * Decrypt "ptr[len]" in place.
+ */
+    void
+bf_crypt_decode(ptr, len)
+    char_u	*ptr;
+    long	len;
+{
+    char_u	*p;
+    int		t;
+
+    for (p = ptr; p < ptr + len; ++p)
+    {
+	BF_RANBYTE(t);
+	*p ^= t;
+	BF_OFB_UPDATE(*p);
+    }
+}
+
+/*
+ * Initialize the encryption keys and the random header according to
+ * the given password.
+ */
+    void
+bf_crypt_init_keys(passwd)
+    char_u *passwd;		/* password string with which to modify keys */
+{
+    char_u *p;
+
+    for (p = passwd; *p != NUL; ++p)
+    {
+	BF_OFB_UPDATE(*p);
+    }
 }
 
 /*
--- a/src/integration.c
+++ b/src/integration.c
@@ -183,7 +183,7 @@ messageFromEserve(XtPointer clientData U
 			ackNum = atoi(&cmd[4]);
 			vim_snprintf(buf, sizeof(buf),
 					       NOCATGETS("ack %d\n"), ackNum);
-			write(sd, buf, strlen(buf));
+			(void)write(sd, buf, strlen(buf));
 		} else if (strncmp(cmd,
 		    NOCATGETS("addMarkType "), 12) == 0) {
 			int idx;
@@ -280,7 +280,7 @@ messageFromEserve(XtPointer clientData U
 			vim_snprintf(buf, sizeof(buf),
 					     NOCATGETS("markLine %s %d %d\n"),
 			    file, markid, line);
-			write(sd, buf, strlen(buf));
+			(void)write(sd, buf, strlen(buf));
 		} else if (cmd[1] == 'o' && cmd[4] == 'L' &&
 		    strncmp(cmd, NOCATGETS("gotoLine "), 9) == 0) {
 			char *file;
@@ -729,10 +729,10 @@ void	workshop_connect(XtAppContext conte
 		workshop_get_editor_name(),
 		PROTOCOL_VERSION,
 		workshop_get_editor_version());
-	write(sd, buf, strlen(buf));
+	(void)write(sd, buf, strlen(buf));
 
 	vim_snprintf(buf, sizeof(buf), NOCATGETS("ack 1\n"));
-	write(sd, buf, strlen(buf));
+	(void)write(sd, buf, strlen(buf));
 }
 
 void	workshop_disconnect()
@@ -1059,7 +1059,7 @@ void workshop_file_closed(char *filename
 	char buffer[2*MAXPATHLEN];
 	vim_snprintf(buffer, sizeof(buffer),
 			NOCATGETS("deletedFile %s\n"), filename);
-	write(sd, buffer, strlen(buffer));
+	(void)write(sd, buffer, strlen(buffer));
 }
 #endif
 
@@ -1068,7 +1068,7 @@ void workshop_file_closed_lineno(char *f
 	char buffer[2*MAXPATHLEN];
 	vim_snprintf(buffer, sizeof(buffer),
 			NOCATGETS("deletedFile %s %d\n"), filename, lineno);
-	write(sd, buffer, strlen(buffer));
+	(void)write(sd, buffer, strlen(buffer));
 }
 
 void workshop_file_opened(char *filename, int readOnly)
@@ -1076,7 +1076,7 @@ void workshop_file_opened(char *filename
 	char buffer[2*MAXPATHLEN];
 	vim_snprintf(buffer, sizeof(buffer),
 			NOCATGETS("loadedFile %s %d\n"), filename, readOnly);
-	write(sd, buffer, strlen(buffer));
+	(void)write(sd, buffer, strlen(buffer));
 }
 
 
@@ -1085,7 +1085,7 @@ void workshop_file_saved(char *filename)
 	char buffer[2*MAXPATHLEN];
 	vim_snprintf(buffer, sizeof(buffer),
 			NOCATGETS("savedFile %s\n"), filename);
-	write(sd, buffer, strlen(buffer));
+	(void)write(sd, buffer, strlen(buffer));
 
 	/* Let editor report any moved marks that the eserve client
 	 * should deal with (for example, moving location-based breakpoints) */
@@ -1098,7 +1098,7 @@ void workshop_file_modified(char *filena
 	char buffer[2*MAXPATHLEN];
 	vim_snprintf(buffer, sizeof(buffer),
 			NOCATGETS("modifiedFile %s\n"), filename);
-	write(sd, buffer, strlen(buffer));
+	(void)write(sd, buffer, strlen(buffer));
 }
 
 void workshop_move_mark(char *filename, int markId, int newLineno)
@@ -1106,7 +1106,7 @@ void workshop_move_mark(char *filename, 
 	char buffer[2*MAXPATHLEN];
 	vim_snprintf(buffer, sizeof(buffer),
 	       NOCATGETS("moveMark %s %d %d\n"), filename, markId, newLineno);
-	write(sd, buffer, strlen(buffer));
+	(void)write(sd, buffer, strlen(buffer));
 }
 #endif
 
@@ -1119,7 +1119,7 @@ void workshop_frame_moved(int new_x, int
 		vim_snprintf(buffer, sizeof(buffer),
 				NOCATGETS("frameAt %d %d %d %d\n"),
 				new_x, new_y, new_w, new_h);
-		write(sd, buffer, strlen(buffer));
+		(void)write(sd, buffer, strlen(buffer));
 	}
 }
 
@@ -1179,7 +1179,7 @@ void workshop_perform_verb(char *verb, v
 			selEndLine, selEndCol,
 			selLength,
 			selection);
-		write(sd, buf, strlen(buf));
+		(void)write(sd, buf, strlen(buf));
 		if (*selection) {
 			free(selection);
 		}
@@ -1190,7 +1190,7 @@ void workshop_perform_verb(char *verb, v
 #if defined(NOHANDS_SUPPORT_FUNCTIONS) || defined(FEAT_BEVAL)
 void workshop_send_message(char *buf)
 {
-	write(sd, buf, strlen(buf));
+	(void)write(sd, buf, strlen(buf));
 }
 #endif
 
--- a/src/misc2.c
+++ b/src/misc2.c
@@ -3768,13 +3768,7 @@ crypt_encode(from, len, to)
 	    to[i] = t ^ ztemp;
 	}
     else
-	for (i = 0; i < len; ++i)
-	{
-	    ztemp = from[i];
-	    t = bf_ranbyte();
-	    bf_ofb_update(ztemp);
-	    to[i] = t ^ ztemp;
-	}
+	bf_crypt_encode(from, len, to);
 }
 
 /*
@@ -3797,8 +3791,7 @@ crypt_decode(ptr, len)
 	    UPDATE_KEYS_ZIP(*p ^= temp);
 	}
     else
-	for (p = ptr; p < ptr + len; ++p)
-	    bf_ofb_update(*p ^= bf_ranbyte());
+	bf_crypt_decode(ptr, len);
 }
 
 /*
@@ -3812,18 +3805,21 @@ crypt_init_keys(passwd)
 {
     if (passwd != NULL && *passwd != NUL)
     {
-	make_crc_tab();
-	keys[0] = 305419896L;
-	keys[1] = 591751049L;
-	keys[2] = 878082192L;
 	if (use_crypt_method == 0)
-	    while (*passwd != '\0')
+	{
+	    char_u *p;
+
+	    make_crc_tab();
+	    keys[0] = 305419896L;
+	    keys[1] = 591751049L;
+	    keys[2] = 878082192L;
+	    for (p = passwd; *p!= NUL; ++p)
 	    {
-		UPDATE_KEYS_ZIP((int)*passwd++);
+		UPDATE_KEYS_ZIP((int)*p);
 	    }
+	}
 	else
-	    while (*passwd != '\0')
-		bf_ofb_update((int)*passwd++);
+	    bf_crypt_init_keys(passwd);
     }
 }
 
@@ -6320,9 +6316,9 @@ put_time(fd, the_time)
 	else
 	{
 #if defined(SIZEOF_TIME_T) && SIZEOF_TIME_T > 4
-	    c = wtime >> (i * 8);
+	    c = (int)(wtime >> (i * 8));
 #else
-	    c = (long_u)wtime >> (i * 8);
+	    c = (int)((long_u)wtime >> (i * 8));
 #endif
 	    putc(c, fd);
 	}
--- a/src/proto/blowfish.pro
+++ b/src/proto/blowfish.pro
@@ -1,7 +1,8 @@
 /* blowfish.c */
 void bf_key_init __ARGS((char_u *password));
 void bf_ofb_init __ARGS((char_u *iv, int iv_len));
-void bf_ofb_update __ARGS((int c));
-int bf_ranbyte __ARGS((void));
+void bf_crypt_encode __ARGS((char_u *from, size_t len, char_u *to));
+void bf_crypt_decode __ARGS((char_u *ptr, long len));
+void bf_crypt_init_keys __ARGS((char_u *passwd));
 int blowfish_self_test __ARGS((void));
 /* vim: set ft=c : */
--- a/src/undo.c
+++ b/src/undo.c
@@ -825,7 +825,7 @@ serialize_header(fp, buf, hash)
 	header = prepare_crypt_write(buf, &header_len);
 	if (header == NULL)
 	    return FAIL;
-	len = fwrite(header, (size_t)header_len, (size_t)1, fp);
+	len = (int)fwrite(header, (size_t)header_len, (size_t)1, fp);
 	vim_free(header);
 	if (len != 1)
 	    return FAIL;
--- a/src/workshop.c
+++ b/src/workshop.c
@@ -1826,7 +1826,7 @@ findYourself(
     else if (*argv0 == '.' || strchr(argv0, '/'))
     {
 	runpath = (char *) malloc(MAXPATHLEN);
-	getcwd(runpath, MAXPATHLEN);
+	(void)getcwd(runpath, MAXPATHLEN);
 	strcat(runpath, "/");
 	strcat(runpath, argv0);
     }