summaryrefslogtreecommitdiff
path: root/Objects
diff options
context:
space:
mode:
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2019-03-20 21:31:57 -0700
committerGitHub <noreply@github.com>2019-03-20 21:31:57 -0700
commit74829b7323642739cdc439c2c88d406daf92075b (patch)
tree32058009ab61da1ff92cad6edac38bdf94e57c07 /Objects
parent65b9849f0f07a000d751c96d9d711aeb24c95224 (diff)
downloadcpython-git-74829b7323642739cdc439c2c88d406daf92075b.tar.gz
bpo-36312: Fix decoders for some code pages. (GH-12369)
(cherry picked from commit c1e2c288f41cdc1c6e6e09d9a5277a58232ceb03) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
Diffstat (limited to 'Objects')
-rw-r--r--Objects/unicodeobject.c21
1 files changed, 16 insertions, 5 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
index b67ffac4e9..adcf69d4e5 100644
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -7123,15 +7123,21 @@ decode_code_page_strict(UINT code_page,
const char *in,
int insize)
{
- const DWORD flags = decode_code_page_flags(code_page);
+ DWORD flags = MB_ERR_INVALID_CHARS;
wchar_t *out;
DWORD outsize;
/* First get the size of the result */
assert(insize > 0);
- outsize = MultiByteToWideChar(code_page, flags, in, insize, NULL, 0);
- if (outsize <= 0)
- goto error;
+ while ((outsize = MultiByteToWideChar(code_page, flags,
+ in, insize, NULL, 0)) <= 0)
+ {
+ if (!flags || GetLastError() != ERROR_INVALID_FLAGS) {
+ goto error;
+ }
+ /* For some code pages (e.g. UTF-7) flags must be set to 0. */
+ flags = 0;
+ }
if (*v == NULL) {
/* Create unicode object */
@@ -7177,7 +7183,7 @@ decode_code_page_errors(UINT code_page,
{
const char *startin = in;
const char *endin = in + size;
- const DWORD flags = decode_code_page_flags(code_page);
+ DWORD flags = MB_ERR_INVALID_CHARS;
/* Ideally, we should get reason from FormatMessage. This is the Windows
2000 English version of the message. */
const char *reason = "No mapping for the Unicode character exists "
@@ -7248,6 +7254,11 @@ decode_code_page_errors(UINT code_page,
if (outsize > 0)
break;
err = GetLastError();
+ if (err == ERROR_INVALID_FLAGS && flags) {
+ /* For some code pages (e.g. UTF-7) flags must be set to 0. */
+ flags = 0;
+ continue;
+ }
if (err != ERROR_NO_UNICODE_TRANSLATION
&& err != ERROR_INSUFFICIENT_BUFFER)
{