]> granicus.if.org Git - vim/commitdiff
Optimize the blowfish crypt/decrypt code a bit more.
authorBram Moolenaar <Bram@vim.org>
Wed, 2 Jun 2010 18:32:23 +0000 (20:32 +0200)
committerBram Moolenaar <Bram@vim.org>
Wed, 2 Jun 2010 18:32:23 +0000 (20:32 +0200)
runtime/doc/todo.txt
src/blowfish.c
src/integration.c
src/misc2.c
src/proto/blowfish.pro
src/undo.c
src/workshop.c

index 41326a9bf1123d650df116b27ed95a4d0a1c9977..daec1bc6804d4593bb35fed8c746d340f7609676 100644 (file)
@@ -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/
index f88cbfdeac6089a158fa1aee3f1a7232ffecd7fd..1ab8eca8def4cb54ce679a5f5a2ab2433561b86f 100644 (file)
@@ -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)
     }
 }
 
+#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; \
+}
+
+/*
+ * Encrypt "from[len]" into "to[len]".
+ * "from" and "to" can be equal to encrypt in place.
+ */
     void
-bf_ofb_update(c)
-    int c;
+bf_crypt_encode(from, len, to)
+    char_u     *from;
+    size_t     len;
+    char_u     *to;
 {
-    ofb_buffer[update_offset++] ^= (char_u)c;
-    if (update_offset == BF_OFB_LEN)
-       update_offset = 0;
+    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;
+    }
 }
 
-    int
-bf_ranbyte()
+/*
+ * 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 */
 {
-    int b;
-
-    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;
+    char_u *p;
+
+    for (p = passwd; *p != NUL; ++p)
+    {
+       BF_OFB_UPDATE(*p);
+    }
 }
 
 /*
index 9da6e2faa0237d0159e99c3a7932f439f513e492..4b1492c63cb78123c4c0a06b4fceb9c5486a0de0 100644 (file)
@@ -183,7 +183,7 @@ messageFromEserve(XtPointer clientData UNUSED,
                        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 UNUSED,
                        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 context)
                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 *filename, int lineno)
        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, int readOnly)
        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 *filename)
        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, int markId, int newLineno)
        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 new_y, int new_w, int new_h)
                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, void *clientData)
                        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, void *clientData)
 #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
 
index 7a0a0512e01ef79a1e1cfa3cd8371a238c07cfbc..e0eea68b5215f35239d1823d0e67fbb64bf1478d 100644 (file)
@@ -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);
        }
index 6b6628742c6690d8d9671d8d0291c276a9873984..da3560e78f91a12fd6b24ed16752fa046d69eee1 100644 (file)
@@ -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 : */
index 39f53414ccb7f79740946ae5c32280c0be4ec2bd..232dbac48d016eb6f136c01dbae183b4c264dc12 100644 (file)
@@ -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;
index 7d4e891f3612ca1bd4def04ea855392a828c5f30..393e3d151c98430d2568490bbaa31f63bcc44fe0 100644 (file)
@@ -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);
     }