@@ -7083,15 +7083,21 @@ decode_code_page_strict(UINT code_page,
70837083 const char * in ,
70847084 int insize )
70857085{
7086- const DWORD flags = decode_code_page_flags ( code_page ) ;
7086+ DWORD flags = MB_ERR_INVALID_CHARS ;
70877087 wchar_t * out ;
70887088 DWORD outsize ;
70897089
70907090 /* First get the size of the result */
70917091 assert (insize > 0 );
7092- outsize = MultiByteToWideChar (code_page , flags , in , insize , NULL , 0 );
7093- if (outsize <= 0 )
7094- goto error ;
7092+ while ((outsize = MultiByteToWideChar (code_page , flags ,
7093+ in , insize , NULL , 0 )) <= 0 )
7094+ {
7095+ if (!flags || GetLastError () != ERROR_INVALID_FLAGS ) {
7096+ goto error ;
7097+ }
7098+ /* For some code pages (e.g. UTF-7) flags must be set to 0. */
7099+ flags = 0 ;
7100+ }
70957101
70967102 /* Extend a wchar_t* buffer */
70977103 Py_ssize_t n = * bufsize ; /* Get the current length */
@@ -7129,7 +7135,7 @@ decode_code_page_errors(UINT code_page,
71297135{
71307136 const char * startin = in ;
71317137 const char * endin = in + size ;
7132- const DWORD flags = decode_code_page_flags ( code_page ) ;
7138+ DWORD flags = MB_ERR_INVALID_CHARS ;
71337139 /* Ideally, we should get reason from FormatMessage. This is the Windows
71347140 2000 English version of the message. */
71357141 const char * reason = "No mapping for the Unicode character exists "
@@ -7187,6 +7193,11 @@ decode_code_page_errors(UINT code_page,
71877193 if (outsize > 0 )
71887194 break ;
71897195 err = GetLastError ();
7196+ if (err == ERROR_INVALID_FLAGS && flags ) {
7197+ /* For some code pages (e.g. UTF-7) flags must be set to 0. */
7198+ flags = 0 ;
7199+ continue ;
7200+ }
71907201 if (err != ERROR_NO_UNICODE_TRANSLATION
71917202 && err != ERROR_INSUFFICIENT_BUFFER )
71927203 {
0 commit comments