DecoderBestFitFallback.cs 8.1 KB

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