To: vim-dev@vim.org Subject: Patch 6.1.420 (extra) Fcc: outbox From: Bram Moolenaar Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 8bit ------------ Patch 6.1.420 (extra) Problem: convert_input() has an unnecessary STRLEN(). Conversion from UCS-2 to a codepage uses word count instead of byte count. Solution: Remove the STRLEN() call. (Daniel Elstner) Always use byte count for string_convert(). Files: src/gui_w32.c, src/mbyte.c *** ../vim61.419/src/gui_w32.c Sun Mar 9 16:21:47 2003 --- src/gui_w32.c Sun Mar 23 14:09:28 2003 *************** *** 1411,1424 **** } /* ! * get the current composition string, in UCS-2; len is the number of * Unicode characters */ static unsigned short * ! GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *len) { ! LONG ret; ! unsigned short *wbuf = NULL; if (!pImmGetContext) return NULL; /* no imm32.dll */ --- 1413,1428 ---- } /* ! * get the current composition string, in UCS-2; *lenp is the number of * Unicode characters */ static unsigned short * ! GetCompositionString_inUCS2(HIMC hIMC, DWORD GCS, int *lenp) { ! LONG ret; ! unsigned short *wbuf = NULL; ! char_u *buf; ! int len; if (!pImmGetContext) return NULL; /* no imm32.dll */ *************** *** 1433,1461 **** wbuf = (unsigned short *) alloc(ret * sizeof(unsigned short)); if(!wbuf) return NULL; pImmGetCompositionStringW(hIMC, GCS, wbuf, ret); ! *len = ret / sizeof(unsigned short); /* char -> wchar */ return wbuf; } /* ret < 0; we got an error, so try the ANSI version. This'll work * on 9x/ME, but only if the codepage happens to be set to whatever * we're inputting. */ ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0); if (ret <= 0) return NULL; /* empty or error */ - else - { - char_u *buf; ! buf = alloc(ret); ! if (buf == NULL) ! return NULL; ! pImmGetCompositionStringA(hIMC, GCS, buf, ret); ! ! /* convert from codepage to UCS-2 */ ! wbuf = (unsigned short *)string_convert(&ime_conv_cp, buf, &ret); ! vim_free(buf); ! *len = ret / sizeof(unsigned short); /* char_u -> wchar */ ! } return wbuf; } --- 1437,1464 ---- wbuf = (unsigned short *) alloc(ret * sizeof(unsigned short)); if(!wbuf) return NULL; pImmGetCompositionStringW(hIMC, GCS, wbuf, ret); ! *lenp = ret / sizeof(unsigned short); /* char -> wchar */ return wbuf; } + /* ret < 0; we got an error, so try the ANSI version. This'll work * on 9x/ME, but only if the codepage happens to be set to whatever * we're inputting. */ ret = pImmGetCompositionStringA(hIMC, GCS, NULL, 0); if (ret <= 0) return NULL; /* empty or error */ ! buf = alloc(ret); ! if (buf == NULL) ! return NULL; ! pImmGetCompositionStringA(hIMC, GCS, buf, ret); ! ! /* convert from codepage to UCS-2 */ ! len = ret; ! wbuf = (unsigned short *)string_convert(&ime_conv_cp, buf, &len); ! vim_free(buf); ! *lenp = len / sizeof(unsigned short); /* char_u -> wchar */ ! return wbuf; } *************** *** 1469,1474 **** --- 1472,1478 ---- GetResultStr(HWND hwnd, int GCS) { DWORD dwBufLen; /* Stogare for len. of composition str. */ + int buflen; HIMC hIMC; /* Input context handle. */ unsigned short *buf = NULL; char *convbuf = NULL; *************** *** 1481,1487 **** if (buf == NULL) return NULL; ! convbuf = string_convert(&ime_conv, (unsigned char *) buf, &dwBufLen); pImmReleaseContext(hwnd, hIMC); vim_free(buf); return convbuf; --- 1485,1492 ---- if (buf == NULL) return NULL; ! buflen = dwBufLen * 2; /* length in words -> length in bytes */ ! convbuf = string_convert(&ime_conv, (unsigned char *)buf, &buflen); pImmReleaseContext(hwnd, hIMC); vim_free(buf); return convbuf; *** ../vim61.419/src/mbyte.c Wed Mar 26 21:48:04 2003 --- src/mbyte.c Wed Mar 26 19:53:37 2003 *************** *** 4118,4123 **** --- 4153,4159 ---- /* * Do conversion on typed input characters in-place. + * The input and output are not NUL terminated! * Returns the length after conversion. */ int *************** *** 4127,4153 **** int maxlen; { char_u *d; ! int l; ! d = string_convert(&input_conv, ptr, &len); if (d != NULL) { ! l = (int)STRLEN(d); ! if (l <= maxlen) ! { ! mch_memmove(ptr, d, l); ! len = l; ! } vim_free(d); } ! return len; } /* * Convert text "ptr[*lenp]" according to "vcp". * Returns the result in allocated memory and sets "*lenp". * When "lenp" is NULL, use NUL terminated strings. ! * When something goes wrong, NULL is returned. */ char_u * string_convert(vcp, ptr, lenp) --- 4163,4187 ---- int maxlen; { char_u *d; ! int dlen = len; ! d = string_convert(&input_conv, ptr, &dlen); if (d != NULL) { ! if (dlen <= maxlen) ! mch_memmove(ptr, d, dlen); ! else ! dlen = len; /* result is too long, keep the unconverted text */ vim_free(d); } ! return dlen; } /* * Convert text "ptr[*lenp]" according to "vcp". * Returns the result in allocated memory and sets "*lenp". * When "lenp" is NULL, use NUL terminated strings. ! * When something goes wrong, NULL is returned and "*lenp" is unchanged. */ char_u * string_convert(vcp, ptr, lenp) *************** *** 4238,4245 **** { int retlen; ! if (!lenp) ! len /= sizeof(unsigned short); retlen = WideCharToMultiByte(vcp->vc_dbcs, 0, (const unsigned short *)ptr, len, 0, 0, 0, 0); retval = alloc(retlen + 1); --- 4272,4279 ---- { int retlen; ! /* buffer size -> number of shorts */ ! len /= sizeof(unsigned short); retlen = WideCharToMultiByte(vcp->vc_dbcs, 0, (const unsigned short *)ptr, len, 0, 0, 0, 0); retval = alloc(retlen + 1); *************** *** 4263,4269 **** MultiByteToWideChar(GetACP(), 0, ptr, len, (unsigned short *) retval, retlen); if (lenp != NULL) ! *lenp = retlen * sizeof(unsigned short); /* number of shorts -> buffer size */ } # endif } --- 4297,4304 ---- MultiByteToWideChar(GetACP(), 0, ptr, len, (unsigned short *) retval, retlen); if (lenp != NULL) ! /* number of shorts -> buffer size */ ! *lenp = retlen * sizeof(unsigned short); } # endif } *** ../vim61.419/src/version.c Wed Mar 26 22:12:58 2003 --- src/version.c Wed Mar 26 22:14:54 2003 *************** *** 613,614 **** --- 613,616 ---- { /* Add new patch number below this line */ + /**/ + 420, /**/ -- A computer without Windows is like a fish without a bicycle. /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net \\\ /// Creator of Vim - Vi IMproved -- http://www.Vim.org \\\ \\\ Project leader for A-A-P -- http://www.A-A-P.org /// \\\ Help AIDS victims, buy at Amazon -- http://ICCF.nl/click1.html ///