DecoderBestFitFallback.cs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the MIT license.
  3. // See the LICENSE file in the project root for more information.
  4. //
  5. // This is used internally to create best fit behavior as per the original windows best fit behavior.
  6. //
  7. using System.Diagnostics;
  8. using System.Threading;
  9. namespace System.Text
  10. {
  11. internal sealed class InternalDecoderBestFitFallback : DecoderFallback
  12. {
  13. // Our variables
  14. internal Encoding _encoding = null;
  15. internal char[] _arrayBestFit = null;
  16. internal char _cReplacement = '?';
  17. internal InternalDecoderBestFitFallback(Encoding encoding)
  18. {
  19. // Need to load our replacement characters table.
  20. _encoding = encoding;
  21. }
  22. public override DecoderFallbackBuffer CreateFallbackBuffer()
  23. {
  24. return new InternalDecoderBestFitFallbackBuffer(this);
  25. }
  26. // Maximum number of characters that this instance of this fallback could return
  27. public override int MaxCharCount
  28. {
  29. get
  30. {
  31. return 1;
  32. }
  33. }
  34. public override bool Equals(object value)
  35. {
  36. InternalDecoderBestFitFallback that = value as InternalDecoderBestFitFallback;
  37. if (that != null)
  38. {
  39. return (_encoding.CodePage == that._encoding.CodePage);
  40. }
  41. return (false);
  42. }
  43. public override int GetHashCode()
  44. {
  45. return _encoding.CodePage;
  46. }
  47. }
  48. internal sealed class InternalDecoderBestFitFallbackBuffer : DecoderFallbackBuffer
  49. {
  50. // Our variables
  51. private char _cBestFit = '\0';
  52. private int _iCount = -1;
  53. private int _iSize;
  54. private InternalDecoderBestFitFallback _oFallback;
  55. // Private object for locking instead of locking on a public type for SQL reliability work.
  56. private static object s_InternalSyncObject;
  57. private static object InternalSyncObject
  58. {
  59. get
  60. {
  61. if (s_InternalSyncObject == null)
  62. {
  63. object o = new object();
  64. Interlocked.CompareExchange<object>(ref s_InternalSyncObject, o, null);
  65. }
  66. return s_InternalSyncObject;
  67. }
  68. }
  69. // Constructor
  70. public InternalDecoderBestFitFallbackBuffer(InternalDecoderBestFitFallback fallback)
  71. {
  72. _oFallback = fallback;
  73. if (_oFallback._arrayBestFit == null)
  74. {
  75. // Lock so we don't confuse ourselves.
  76. lock (InternalSyncObject)
  77. {
  78. // Double check before we do it again.
  79. if (_oFallback._arrayBestFit == null)
  80. _oFallback._arrayBestFit = fallback._encoding.GetBestFitBytesToUnicodeData();
  81. }
  82. }
  83. }
  84. // Fallback methods
  85. public override bool Fallback(byte[] bytesUnknown, int index)
  86. {
  87. // We expect no previous fallback in our buffer
  88. Debug.Assert(_iCount < 1, "[DecoderReplacementFallbackBuffer.Fallback] Calling fallback without a previously empty buffer");
  89. _cBestFit = TryBestFit(bytesUnknown);
  90. if (_cBestFit == '\0')
  91. _cBestFit = _oFallback._cReplacement;
  92. _iCount = _iSize = 1;
  93. return true;
  94. }
  95. // Default version is overridden in DecoderReplacementFallback.cs
  96. public override char GetNextChar()
  97. {
  98. // We want it to get < 0 because == 0 means that the current/last character is a fallback
  99. // and we need to detect recursion. We could have a flag but we already have this counter.
  100. _iCount--;
  101. // Do we have anything left? 0 is now last fallback char, negative is nothing left
  102. if (_iCount < 0)
  103. return '\0';
  104. // Need to get it out of the buffer.
  105. // Make sure it didn't wrap from the fast count-- path
  106. if (_iCount == int.MaxValue)
  107. {
  108. _iCount = -1;
  109. return '\0';
  110. }
  111. // Return the best fit character
  112. return _cBestFit;
  113. }
  114. public override bool MovePrevious()
  115. {
  116. // Exception fallback doesn't have anywhere to back up to.
  117. if (_iCount >= 0)
  118. _iCount++;
  119. // Return true if we could do it.
  120. return (_iCount >= 0 && _iCount <= _iSize);
  121. }
  122. // How many characters left to output?
  123. public override int Remaining
  124. {
  125. get
  126. {
  127. return (_iCount > 0) ? _iCount : 0;
  128. }
  129. }
  130. // Clear the buffer
  131. public override unsafe void Reset()
  132. {
  133. _iCount = -1;
  134. byteStart = null;
  135. }
  136. // This version just counts the fallback and doesn't actually copy anything.
  137. internal unsafe override int InternalFallback(byte[] bytes, byte* pBytes)
  138. // Right now this has both bytes and bytes[], since we might have extra bytes, hence the
  139. // array, and we might need the index, hence the byte*
  140. {
  141. // return our replacement string Length (always 1 for InternalDecoderBestFitFallback, either
  142. // a best fit char or ?
  143. return 1;
  144. }
  145. // private helper methods
  146. private char TryBestFit(byte[] bytesCheck)
  147. {
  148. // Need to figure out our best fit character, low is beginning of array, high is 1 AFTER end of array
  149. int lowBound = 0;
  150. int highBound = _oFallback._arrayBestFit.Length;
  151. int index;
  152. char cCheck;
  153. // Check trivial case first (no best fit)
  154. if (highBound == 0)
  155. return '\0';
  156. // If our array is too small or too big we can't check
  157. if (bytesCheck.Length == 0 || bytesCheck.Length > 2)
  158. return '\0';
  159. if (bytesCheck.Length == 1)
  160. cCheck = unchecked((char)bytesCheck[0]);
  161. else
  162. cCheck = unchecked((char)((bytesCheck[0] << 8) + bytesCheck[1]));
  163. // Check trivial out of range case
  164. if (cCheck < _oFallback._arrayBestFit[0] || cCheck > _oFallback._arrayBestFit[highBound - 2])
  165. return '\0';
  166. // Binary search the array
  167. int iDiff;
  168. while ((iDiff = (highBound - lowBound)) > 6)
  169. {
  170. // Look in the middle, which is complicated by the fact that we have 2 #s for each pair,
  171. // so we don't want index to be odd because it must be word aligned.
  172. // Also note that index can never == highBound (because diff is rounded down)
  173. index = ((iDiff / 2) + lowBound) & 0xFFFE;
  174. char cTest = _oFallback._arrayBestFit[index];
  175. if (cTest == cCheck)
  176. {
  177. // We found it
  178. Debug.Assert(index + 1 < _oFallback._arrayBestFit.Length,
  179. "[InternalDecoderBestFitFallbackBuffer.TryBestFit]Expected replacement character at end of array");
  180. return _oFallback._arrayBestFit[index + 1];
  181. }
  182. else if (cTest < cCheck)
  183. {
  184. // We weren't high enough
  185. lowBound = index;
  186. }
  187. else
  188. {
  189. // We weren't low enough
  190. highBound = index;
  191. }
  192. }
  193. for (index = lowBound; index < highBound; index += 2)
  194. {
  195. if (_oFallback._arrayBestFit[index] == cCheck)
  196. {
  197. // We found it
  198. Debug.Assert(index + 1 < _oFallback._arrayBestFit.Length,
  199. "[InternalDecoderBestFitFallbackBuffer.TryBestFit]Expected replacement character at end of array");
  200. return _oFallback._arrayBestFit[index + 1];
  201. }
  202. }
  203. // Char wasn't in our table
  204. return '\0';
  205. }
  206. }
  207. }