Skip to content

Commit 81b9543

Browse files
committed
Improve invalid codepage handling
1 parent 5da1f12 commit 81b9543

1 file changed

Lines changed: 21 additions & 14 deletions

File tree

src/Zip/ZipConstants.cs

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public enum EncryptionAlgorithm
124124
/// </summary>
125125
Des = 0x6601,
126126
/// <summary>
127-
/// RCS encryption has been used for encryption.
127+
/// RC2 encryption has been used for encryption.
128128
/// </summary>
129129
RC2 = 0x6602,
130130
/// <summary>
@@ -473,16 +473,18 @@ public sealed class ZipConstants
473473
// 850 is a good default for english speakers particularly in Europe.
474474
static int defaultCodePage = CultureInfo.CurrentCulture.TextInfo.ANSICodePage;
475475
#else
476-
/// <remarks>
477-
/// Get OEM codepage from NetFX, which parses the NLP file with culture info table etc etc.
478-
/// But sometimes it yields the special value of 1 which is nicknamed <c>CodePageNoOEM</c> in <see cref="Encoding"/> sources (might also mean <c>CP_OEMCP</c>, but Encoding puts it so).
479-
/// This was observed on Ukranian and Hindu systems.
480-
/// Given this value, <see cref="Encoding.GetEncoding(int)"/> throws an <see cref="ArgumentException"/>.
481-
/// So replace it with some fallback, e.g. 437 which is the default cpcp in a console in a default Windows installation.
482-
/// </remarks>
483-
static int defaultCodePage =
484-
Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage != 1 /* CodePageNoOEM constant, causes ArgumentException in subsequent calls to Encoding::GetEncoding() */ ?
485-
Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage : 437 /* The default OEM encoding in a console in a default Windows installation, as a fallback. */;
476+
/// <remarks>
477+
/// Get OEM codepage from NetFX, which parses the NLP file with culture info table etc etc.
478+
/// But sometimes it yields the special value of 1 which is nicknamed <c>CodePageNoOEM</c> in <see cref="Encoding"/> sources (might also mean <c>CP_OEMCP</c>, but Encoding puts it so).
479+
/// This was observed on Ukranian and Hindu systems.
480+
/// Given this value, <see cref="Encoding.GetEncoding(int)"/> throws an <see cref="ArgumentException"/>.
481+
/// So replace it with some fallback, e.g. 437 which is the default cpcp in a console in a default Windows installation.
482+
/// </remarks>
483+
static int defaultCodePage =
484+
// these values cause ArgumentException in subsequent calls to Encoding::GetEncoding()
485+
((Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage == 1) || (Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage == 2) || (Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage == 3) || (Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage == 42))
486+
? 437 // The default OEM encoding in a console in a default Windows installation, as a fallback.
487+
: Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage;
486488
#endif
487489

488490
/// <summary>
@@ -497,11 +499,16 @@ public static int DefaultCodePage {
497499
return defaultCodePage;
498500
}
499501
set {
500-
defaultCodePage = value;
502+
if ((value < 0) || (value > 65535) ||
503+
(value == 1) || (value == 2) || (value == 3) || (value == 42)) {
504+
throw new ArgumentOutOfRangeException("value");
505+
}
506+
507+
defaultCodePage = value;
501508
}
502509
}
503510

504-
/// <summary>
511+
/// <summary>
505512
/// Convert a portion of a byte array to a string.
506513
/// </summary>
507514
/// <param name="data">
@@ -511,7 +518,7 @@ public static int DefaultCodePage {
511518
/// Number of bytes to convert starting from index 0
512519
/// </param>
513520
/// <returns>
514-
/// data[0]..data[length - 1] converted to a string
521+
/// data[0]..data[count - 1] converted to a string
515522
/// </returns>
516523
public static string ConvertToString(byte[] data, int count)
517524
{

0 commit comments

Comments
 (0)