Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Improve invalid codepage handling
  • Loading branch information
jfreilly committed Jan 10, 2015
1 parent 5da1f12 commit 81b9543
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/Zip/ZipConstants.cs
Expand Up @@ -124,7 +124,7 @@ public enum EncryptionAlgorithm
/// </summary>
Des = 0x6601,
/// <summary>
/// RCS encryption has been used for encryption.
/// RC2 encryption has been used for encryption.
/// </summary>
RC2 = 0x6602,
/// <summary>
Expand Down Expand Up @@ -473,16 +473,18 @@ public sealed class ZipConstants
// 850 is a good default for english speakers particularly in Europe.
static int defaultCodePage = CultureInfo.CurrentCulture.TextInfo.ANSICodePage;
#else
/// <remarks>
/// Get OEM codepage from NetFX, which parses the NLP file with culture info table etc etc.
/// 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).
/// This was observed on Ukranian and Hindu systems.
/// Given this value, <see cref="Encoding.GetEncoding(int)"/> throws an <see cref="ArgumentException"/>.
/// So replace it with some fallback, e.g. 437 which is the default cpcp in a console in a default Windows installation.
/// </remarks>
static int defaultCodePage =
Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage != 1 /* CodePageNoOEM constant, causes ArgumentException in subsequent calls to Encoding::GetEncoding() */ ?
Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage : 437 /* The default OEM encoding in a console in a default Windows installation, as a fallback. */;
/// <remarks>
/// Get OEM codepage from NetFX, which parses the NLP file with culture info table etc etc.
/// 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).
/// This was observed on Ukranian and Hindu systems.
/// Given this value, <see cref="Encoding.GetEncoding(int)"/> throws an <see cref="ArgumentException"/>.
/// So replace it with some fallback, e.g. 437 which is the default cpcp in a console in a default Windows installation.
/// </remarks>
static int defaultCodePage =
// these values cause ArgumentException in subsequent calls to Encoding::GetEncoding()
((Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage == 1) || (Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage == 2) || (Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage == 3) || (Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage == 42))
? 437 // The default OEM encoding in a console in a default Windows installation, as a fallback.
: Thread.CurrentThread.CurrentCulture.TextInfo.OEMCodePage;
#endif

/// <summary>
Expand All @@ -497,11 +499,16 @@ public sealed class ZipConstants
return defaultCodePage;
}
set {
defaultCodePage = value;
if ((value < 0) || (value > 65535) ||
(value == 1) || (value == 2) || (value == 3) || (value == 42)) {
throw new ArgumentOutOfRangeException("value");
}

defaultCodePage = value;
}
}

/// <summary>
/// <summary>
/// Convert a portion of a byte array to a string.
/// </summary>
/// <param name="data">
Expand All @@ -511,7 +518,7 @@ public sealed class ZipConstants
/// Number of bytes to convert starting from index 0
/// </param>
/// <returns>
/// data[0]..data[length - 1] converted to a string
/// data[0]..data[count - 1] converted to a string
/// </returns>
public static string ConvertToString(byte[] data, int count)
{
Expand Down

0 comments on commit 81b9543

Please sign in to comment.