UTF8Encoding.cs 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740
  1. /*
  2. * UTF8Encoding.cs - Implementation of the "System.Text.UTF8Encoding" class.
  3. *
  4. * Copyright (c) 2001, 2002 Southern Storm Software, Pty Ltd
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining
  7. * a copy of this software and associated documentation files (the "Software"),
  8. * to deal in the Software without restriction, including without limitation
  9. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  10. * and/or sell copies of the Software, and to permit persons to whom the
  11. * Software is furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included
  14. * in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  20. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  21. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  22. * OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. namespace System.Text
  25. {
  26. using System;
  27. [Serializable]
  28. public class UTF8Encoding : Encoding
  29. {
  30. // Magic number used by Windows for UTF-8.
  31. internal const int UTF8_CODE_PAGE = 65001;
  32. // Internal state.
  33. private bool emitIdentifier;
  34. private bool throwOnInvalid;
  35. // Constructors.
  36. public UTF8Encoding () : this (false, false) {}
  37. public UTF8Encoding (bool encoderShouldEmitUTF8Identifier)
  38. : this (encoderShouldEmitUTF8Identifier, false) {}
  39. public UTF8Encoding (bool encoderShouldEmitUTF8Identifier, bool throwOnInvalidBytes)
  40. : base (UTF8_CODE_PAGE)
  41. {
  42. emitIdentifier = encoderShouldEmitUTF8Identifier;
  43. throwOnInvalid = throwOnInvalidBytes;
  44. web_name = body_name = header_name = "utf-8";
  45. encoding_name = "Unicode (UTF-8)";
  46. is_browser_save = true;
  47. is_browser_display = true;
  48. is_mail_news_display = true;
  49. windows_code_page = UnicodeEncoding.UNICODE_CODE_PAGE;
  50. }
  51. // Internal version of "GetByteCount" which can handle a rolling
  52. // state between multiple calls to this method.
  53. private static int InternalGetByteCount (char[] chars, int index, int count, uint leftOver, bool flush)
  54. {
  55. // Validate the parameters.
  56. if (chars == null) {
  57. throw new ArgumentNullException ("chars");
  58. }
  59. if (index < 0 || index > chars.Length) {
  60. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  61. }
  62. if (count < 0 || count > (chars.Length - index)) {
  63. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  64. }
  65. // Determine the lengths of all characters.
  66. char ch;
  67. int length = 0;
  68. uint pair = leftOver;
  69. while (count > 0) {
  70. ch = chars[index];
  71. if (pair == 0) {
  72. if (ch < '\u0080') {
  73. ++length;
  74. } else if (ch < '\u0800') {
  75. length += 2;
  76. } else if (ch >= '\uD800' && ch <= '\uDBFF') {
  77. // This is the start of a surrogate pair.
  78. pair = (uint)ch;
  79. } else {
  80. length += 3;
  81. }
  82. } else if (ch >= '\uDC00' && ch <= '\uDFFF') {
  83. // We have a surrogate pair.
  84. length += 4;
  85. pair = 0;
  86. } else {
  87. // We have a surrogate start followed by a
  88. // regular character. Technically, this is
  89. // invalid, but we have to do something.
  90. // We write out the surrogate start and then
  91. // re-visit the current character again.
  92. length += 3;
  93. pair = 0;
  94. continue;
  95. }
  96. ++index;
  97. --count;
  98. }
  99. if (flush && pair != 0) {
  100. // Flush the left-over surrogate pair start.
  101. length += 3;
  102. }
  103. // Return the final length to the caller.
  104. return length;
  105. }
  106. // Get the number of bytes needed to encode a character buffer.
  107. public override int GetByteCount (char[] chars, int index, int count)
  108. {
  109. return InternalGetByteCount (chars, index, count, 0, true);
  110. }
  111. // Convenience wrappers for "GetByteCount".
  112. public override int GetByteCount (String s)
  113. {
  114. // Validate the parameters.
  115. if (s == null) {
  116. throw new ArgumentNullException ("s");
  117. }
  118. // Determine the lengths of all characters.
  119. char ch;
  120. int index = 0;
  121. int count = s.Length;
  122. int length = 0;
  123. uint pair;
  124. while (count > 0) {
  125. ch = s[index++];
  126. if (ch < '\u0080') {
  127. ++length;
  128. } else if (ch < '\u0800') {
  129. length += 2;
  130. } else if (ch >= '\uD800' && ch <= '\uDBFF' && count > 1) {
  131. // This may be the start of a surrogate pair.
  132. pair = (uint)(s[index]);
  133. if (pair >= (uint)0xDC00 && pair <= (uint)0xDFFF) {
  134. length += 4;
  135. ++index;
  136. --count;
  137. } else {
  138. length += 3;
  139. }
  140. } else {
  141. length += 3;
  142. }
  143. --count;
  144. }
  145. // Return the final length to the caller.
  146. return length;
  147. }
  148. // Internal version of "GetBytes" which can handle a rolling
  149. // state between multiple calls to this method.
  150. private static int InternalGetBytes (char[] chars, int charIndex,
  151. int charCount, byte[] bytes,
  152. int byteIndex, ref uint leftOver,
  153. bool flush)
  154. {
  155. // Validate the parameters.
  156. if (chars == null) {
  157. throw new ArgumentNullException ("chars");
  158. }
  159. if (bytes == null) {
  160. throw new ArgumentNullException ("bytes");
  161. }
  162. if (charIndex < 0 || charIndex > chars.Length) {
  163. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
  164. }
  165. if (charCount < 0 || charCount > (chars.Length - charIndex)) {
  166. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
  167. }
  168. if (byteIndex < 0 || byteIndex > bytes.Length) {
  169. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  170. }
  171. // Convert the characters into bytes.
  172. char ch;
  173. int length = bytes.Length;
  174. uint pair;
  175. uint left = leftOver;
  176. int posn = byteIndex;
  177. while (charCount > 0) {
  178. // Fetch the next UTF-16 character pair value.
  179. ch = chars[charIndex++];
  180. --charCount;
  181. if (left == 0) {
  182. if (ch >= '\uD800' && ch <= '\uDBFF') {
  183. // This is the start of a surrogate pair.
  184. left = (uint)ch;
  185. continue;
  186. } else {
  187. // This is a regular character.
  188. pair = (uint)ch;
  189. }
  190. } else if (ch >= '\uDC00' && ch <= '\uDFFF') {
  191. // We have a surrogate pair.
  192. pair = ((left - (uint)0xD800) << 10) +
  193. (((uint)ch) - (uint)0xDC00) +
  194. (uint)0x10000;
  195. left = 0;
  196. } else {
  197. // We have a surrogate start followed by a
  198. // regular character. Technically, this is
  199. // invalid, but we have to do something.
  200. // We write out the surrogate start and then
  201. // re-visit the current character again.
  202. pair = (uint)left;
  203. left = 0;
  204. --charIndex;
  205. ++charCount;
  206. }
  207. // Encode the character pair value.
  208. if (pair < (uint)0x0080) {
  209. if (posn >= length) {
  210. throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
  211. }
  212. bytes[posn++] = (byte)pair;
  213. } else if (pair < (uint)0x0800) {
  214. if ((posn + 2) > length) {
  215. throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
  216. }
  217. bytes[posn++] = (byte)(0xC0 | (pair >> 6));
  218. bytes[posn++] = (byte)(0x80 | (pair & 0x3F));
  219. } else if (pair < (uint)0x10000) {
  220. if ((posn + 3) > length) {
  221. throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
  222. }
  223. bytes[posn++] = (byte)(0xE0 | (pair >> 12));
  224. bytes[posn++] = (byte)(0x80 | ((pair >> 6) & 0x3F));
  225. bytes[posn++] = (byte)(0x80 | (pair & 0x3F));
  226. } else {
  227. if ((posn + 4) > length) {
  228. throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
  229. }
  230. bytes[posn++] = (byte)(0xF0 | (pair >> 18));
  231. bytes[posn++] = (byte)(0x80 | ((pair >> 12) & 0x3F));
  232. bytes[posn++] = (byte)(0x80 | ((pair >> 6) & 0x3F));
  233. bytes[posn++] = (byte)(0x80 | (pair & 0x3F));
  234. }
  235. }
  236. if (flush && left != 0) {
  237. // Flush the left-over surrogate pair start.
  238. if ((posn + 3) > length) {
  239. throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
  240. }
  241. bytes[posn++] = (byte)(0xE0 | (left >> 12));
  242. bytes[posn++] = (byte)(0x80 | ((left >> 6) & 0x3F));
  243. bytes[posn++] = (byte)(0x80 | (left & 0x3F));
  244. left = 0;
  245. }
  246. leftOver = left;
  247. // Return the final count to the caller.
  248. return posn - byteIndex;
  249. }
  250. // Get the bytes that result from encoding a character buffer.
  251. public override int GetBytes (char[] chars, int charIndex, int charCount,
  252. byte[] bytes, int byteIndex)
  253. {
  254. uint leftOver = 0;
  255. return InternalGetBytes (chars, charIndex, charCount, bytes, byteIndex, ref leftOver, true);
  256. }
  257. // Convenience wrappers for "GetBytes".
  258. public override int GetBytes (String s, int charIndex, int charCount,
  259. byte[] bytes, int byteIndex)
  260. {
  261. // Validate the parameters.
  262. if (s == null) {
  263. throw new ArgumentNullException ("s");
  264. }
  265. if (bytes == null) {
  266. throw new ArgumentNullException ("bytes");
  267. }
  268. if (charIndex < 0 || charIndex > s.Length) {
  269. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_StringIndex"));
  270. }
  271. if (charCount < 0 || charCount > (s.Length - charIndex)) {
  272. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_StringRange"));
  273. }
  274. if (byteIndex < 0 || byteIndex > bytes.Length) {
  275. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  276. }
  277. // Convert the characters into bytes.
  278. char ch;
  279. int length = bytes.Length;
  280. uint pair;
  281. int posn = byteIndex;
  282. while (charCount > 0) {
  283. // Fetch the next UTF-16 character pair value.
  284. ch = s[charIndex++];
  285. --charCount;
  286. if (ch >= '\uD800' && ch <= '\uDBFF' && charCount > 1) {
  287. // This may be the start of a surrogate pair.
  288. pair = (uint)(s[charIndex]);
  289. if (pair >= (uint)0xDC00 && pair <= (uint)0xDFFF) {
  290. pair = (pair - (uint)0xDC00) +
  291. ((((uint)ch) - (uint)0xD800) << 10) +
  292. (uint)0x10000;
  293. ++charIndex;
  294. --charCount;
  295. } else {
  296. pair = (uint)ch;
  297. }
  298. } else {
  299. pair = (uint)ch;
  300. }
  301. // Encode the character pair value.
  302. if (pair < (uint)0x0080) {
  303. if (posn >= length) {
  304. throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
  305. }
  306. bytes[posn++] = (byte)pair;
  307. } else if (pair < (uint)0x0800) {
  308. if ((posn + 2) > length) {
  309. throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
  310. }
  311. bytes[posn++] = (byte)(0xC0 | (pair >> 6));
  312. bytes[posn++] = (byte)(0x80 | (pair & 0x3F));
  313. } else if (pair < (uint)0x10000) {
  314. if ((posn + 3) > length) {
  315. throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
  316. }
  317. bytes[posn++] = (byte)(0xE0 | (pair >> 12));
  318. bytes[posn++] = (byte)(0x80 | ((pair >> 6) & 0x3F));
  319. bytes[posn++] = (byte)(0x80 | (pair & 0x3F));
  320. } else {
  321. if ((posn + 4) > length) {
  322. throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
  323. }
  324. bytes[posn++] = (byte)(0xF0 | (pair >> 18));
  325. bytes[posn++] = (byte)(0x80 | ((pair >> 12) & 0x3F));
  326. bytes[posn++] = (byte)(0x80 | ((pair >> 6) & 0x3F));
  327. bytes[posn++] = (byte)(0x80 | (pair & 0x3F));
  328. }
  329. }
  330. // Return the final count to the caller.
  331. return posn - byteIndex;
  332. }
  333. // Internal version of "GetCharCount" which can handle a rolling
  334. // state between multiple calls to this method.
  335. private static int InternalGetCharCount (byte[] bytes, int index, int count,
  336. uint leftOverBits,
  337. uint leftOverCount,
  338. bool throwOnInvalid, bool flush)
  339. {
  340. // Validate the parameters.
  341. if (bytes == null) {
  342. throw new ArgumentNullException ("bytes");
  343. }
  344. if (index < 0 || index > bytes.Length) {
  345. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  346. }
  347. if (count < 0 || count > (bytes.Length - index)) {
  348. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  349. }
  350. // Determine the number of characters that we have.
  351. uint ch;
  352. int length = 0;
  353. uint leftBits = leftOverBits;
  354. uint leftSoFar = (leftOverCount & (uint)0x0F);
  355. uint leftSize = ((leftOverCount >> 4) & (uint)0x0F);
  356. while (count > 0) {
  357. ch = (uint)(bytes[index++]);
  358. --count;
  359. if (leftSize == 0) {
  360. // Process a UTF-8 start character.
  361. if (ch < (uint)0x0080) {
  362. // Single-byte UTF-8 character.
  363. ++length;
  364. } else if ((ch & (uint)0xE0) == (uint)0xC0) {
  365. // Double-byte UTF-8 character.
  366. leftBits = (ch & (uint)0x1F);
  367. leftSoFar = 1;
  368. leftSize = 2;
  369. } else if ((ch & (uint)0xF0) == (uint)0xE0) {
  370. // Three-byte UTF-8 character.
  371. leftBits = (ch & (uint)0x0F);
  372. leftSoFar = 1;
  373. leftSize = 3;
  374. } else if ((ch & (uint)0xF8) == (uint)0xF0) {
  375. // Four-byte UTF-8 character.
  376. leftBits = (ch & (uint)0x07);
  377. leftSoFar = 1;
  378. leftSize = 4;
  379. } else if ((ch & (uint)0xFC) == (uint)0xF8) {
  380. // Five-byte UTF-8 character.
  381. leftBits = (ch & (uint)0x03);
  382. leftSoFar = 1;
  383. leftSize = 5;
  384. } else if ((ch & (uint)0xFC) == (uint)0xFC) {
  385. // Six-byte UTF-8 character.
  386. leftBits = (ch & (uint)0x03);
  387. leftSoFar = 1;
  388. leftSize = 6;
  389. } else {
  390. // Invalid UTF-8 start character.
  391. if (throwOnInvalid) {
  392. throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
  393. }
  394. }
  395. } else {
  396. // Process an extra byte in a multi-byte sequence.
  397. if ((ch & (uint)0xC0) == (uint)0x80) {
  398. leftBits = ((leftBits << 6) | (ch & (uint)0x3F));
  399. if (++leftSoFar >= leftSize) {
  400. // We have a complete character now.
  401. if (leftBits < (uint)0x10000) {
  402. if (leftBits != (uint)0xFEFF) {
  403. ++length;
  404. }
  405. } else if (leftBits < (uint)0x110000) {
  406. length += 2;
  407. } else if (throwOnInvalid) {
  408. throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
  409. }
  410. leftSize = 0;
  411. }
  412. } else {
  413. // Invalid UTF-8 sequence: clear and restart.
  414. if (throwOnInvalid) {
  415. throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
  416. }
  417. leftSize = 0;
  418. --index;
  419. ++count;
  420. }
  421. }
  422. }
  423. if (flush && leftSize != 0 && throwOnInvalid) {
  424. // We had left-over bytes that didn't make up
  425. // a complete UTF-8 character sequence.
  426. throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
  427. }
  428. // Return the final length to the caller.
  429. return length;
  430. }
  431. // Get the number of characters needed to decode a byte buffer.
  432. public override int GetCharCount (byte[] bytes, int index, int count)
  433. {
  434. return InternalGetCharCount (bytes, index, count, 0, 0, throwOnInvalid, true);
  435. }
  436. // Get the characters that result from decoding a byte buffer.
  437. private static int InternalGetChars (byte[] bytes, int byteIndex,
  438. int byteCount, char[] chars,
  439. int charIndex, ref uint leftOverBits,
  440. ref uint leftOverCount,
  441. bool throwOnInvalid, bool flush)
  442. {
  443. // Validate the parameters.
  444. if (bytes == null) {
  445. throw new ArgumentNullException ("bytes");
  446. }
  447. if (chars == null) {
  448. throw new ArgumentNullException ("chars");
  449. }
  450. if (byteIndex < 0 || byteIndex > bytes.Length) {
  451. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  452. }
  453. if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
  454. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
  455. }
  456. if (charIndex < 0 || charIndex > chars.Length) {
  457. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
  458. }
  459. // Convert the bytes into the output buffer.
  460. uint ch;
  461. int length = chars.Length;
  462. int posn = charIndex;
  463. uint leftBits = leftOverBits;
  464. uint leftSoFar = (leftOverCount & (uint)0x0F);
  465. uint leftSize = ((leftOverCount >> 4) & (uint)0x0F);
  466. while (byteCount > 0) {
  467. // Fetch the next character from the byte buffer.
  468. ch = (uint)(bytes[byteIndex++]);
  469. --byteCount;
  470. if (leftSize == 0) {
  471. // Process a UTF-8 start character.
  472. if (ch < (uint)0x0080) {
  473. // Single-byte UTF-8 character.
  474. if (posn >= length) {
  475. throw new ArgumentException (_("Arg_InsufficientSpace"), "chars");
  476. }
  477. chars[posn++] = (char)ch;
  478. } else if ((ch & (uint)0xE0) == (uint)0xC0) {
  479. // Double-byte UTF-8 character.
  480. leftBits = (ch & (uint)0x1F);
  481. leftSoFar = 1;
  482. leftSize = 2;
  483. } else if ((ch & (uint)0xF0) == (uint)0xE0) {
  484. // Three-byte UTF-8 character.
  485. leftBits = (ch & (uint)0x0F);
  486. leftSoFar = 1;
  487. leftSize = 3;
  488. } else if ((ch & (uint)0xF8) == (uint)0xF0) {
  489. // Four-byte UTF-8 character.
  490. leftBits = (ch & (uint)0x07);
  491. leftSoFar = 1;
  492. leftSize = 4;
  493. } else if ((ch & (uint)0xFC) == (uint)0xF8) {
  494. // Five-byte UTF-8 character.
  495. leftBits = (ch & (uint)0x03);
  496. leftSoFar = 1;
  497. leftSize = 5;
  498. } else if ((ch & (uint)0xFC) == (uint)0xFC) {
  499. // Six-byte UTF-8 character.
  500. leftBits = (ch & (uint)0x03);
  501. leftSoFar = 1;
  502. leftSize = 6;
  503. } else {
  504. // Invalid UTF-8 start character.
  505. if (throwOnInvalid) {
  506. throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
  507. }
  508. }
  509. } else {
  510. // Process an extra byte in a multi-byte sequence.
  511. if ((ch & (uint)0xC0) == (uint)0x80) {
  512. leftBits = ((leftBits << 6) | (ch & (uint)0x3F));
  513. if (++leftSoFar >= leftSize) {
  514. // We have a complete character now.
  515. if (leftBits < (uint)0x10000) {
  516. if (leftBits != (uint)0xFEFF) {
  517. if (posn >= length) {
  518. throw new ArgumentException
  519. (_("Arg_InsufficientSpace"), "chars");
  520. }
  521. chars[posn++] = (char)leftBits;
  522. }
  523. } else if (leftBits < (uint)0x110000) {
  524. if ((posn + 2) > length) {
  525. throw new ArgumentException
  526. (_("Arg_InsufficientSpace"), "chars");
  527. }
  528. leftBits -= (uint)0x10000;
  529. chars[posn++] = (char)((leftBits >> 10) +
  530. (uint)0xD800);
  531. chars[posn++] =
  532. (char)((leftBits & (uint)0x3FF) + (uint)0xDC00);
  533. } else if (throwOnInvalid) {
  534. throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
  535. }
  536. leftSize = 0;
  537. }
  538. } else {
  539. // Invalid UTF-8 sequence: clear and restart.
  540. if (throwOnInvalid) {
  541. throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
  542. }
  543. leftSize = 0;
  544. --byteIndex;
  545. ++byteCount;
  546. }
  547. }
  548. }
  549. if (flush && leftSize != 0 && throwOnInvalid) {
  550. // We had left-over bytes that didn't make up
  551. // a complete UTF-8 character sequence.
  552. throw new ArgumentException (_("Arg_InvalidUTF8"), "bytes");
  553. }
  554. leftOverBits = leftBits;
  555. leftOverCount = (leftSoFar | (leftSize << 4));
  556. // Return the final length to the caller.
  557. return posn - charIndex;
  558. }
  559. // Get the characters that result from decoding a byte buffer.
  560. public override int GetChars (byte[] bytes, int byteIndex, int byteCount,
  561. char[] chars, int charIndex)
  562. {
  563. uint leftOverBits = 0;
  564. uint leftOverCount = 0;
  565. return InternalGetChars (bytes, byteIndex, byteCount, chars,
  566. charIndex, ref leftOverBits, ref leftOverCount, throwOnInvalid, true);
  567. }
  568. // Get the maximum number of bytes needed to encode a
  569. // specified number of characters.
  570. public override int GetMaxByteCount (int charCount)
  571. {
  572. if (charCount < 0) {
  573. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
  574. }
  575. return charCount * 4;
  576. }
  577. // Get the maximum number of characters needed to decode a
  578. // specified number of bytes.
  579. public override int GetMaxCharCount (int byteCount)
  580. {
  581. if (byteCount < 0) {
  582. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
  583. }
  584. return byteCount;
  585. }
  586. // Get a UTF8-specific decoder that is attached to this instance.
  587. public override Decoder GetDecoder ()
  588. {
  589. return new UTF8Decoder (throwOnInvalid);
  590. }
  591. // Get a UTF8-specific encoder that is attached to this instance.
  592. public override Encoder GetEncoder ()
  593. {
  594. return new UTF8Encoder (emitIdentifier);
  595. }
  596. // Get the UTF8 preamble.
  597. public override byte[] GetPreamble ()
  598. {
  599. if (emitIdentifier) {
  600. byte[] pre = new byte [3];
  601. pre[0] = (byte)0xEF;
  602. pre[1] = (byte)0xBB;
  603. pre[2] = (byte)0xBF;
  604. return pre;
  605. } else {
  606. return new byte [0];
  607. }
  608. }
  609. // Determine if this object is equal to another.
  610. public override bool Equals (Object value)
  611. {
  612. UTF8Encoding enc = (value as UTF8Encoding);
  613. if (enc != null) {
  614. return (codePage == enc.codePage &&
  615. emitIdentifier == enc.emitIdentifier &&
  616. throwOnInvalid == enc.throwOnInvalid);
  617. } else {
  618. return false;
  619. }
  620. }
  621. // Get the hash code for this object.
  622. public override int GetHashCode ()
  623. {
  624. return base.GetHashCode ();
  625. }
  626. public override byte [] GetBytes (String s)
  627. {
  628. if (s == null)
  629. throw new ArgumentNullException ("s");
  630. int length = GetByteCount (s);
  631. byte [] bytes = new byte [length];
  632. GetBytes (s, 0, s.Length, bytes, 0);
  633. return bytes;
  634. }
  635. // UTF-8 decoder implementation.
  636. [Serializable]
  637. private class UTF8Decoder : Decoder
  638. {
  639. private bool throwOnInvalid;
  640. private uint leftOverBits;
  641. private uint leftOverCount;
  642. // Constructor.
  643. public UTF8Decoder (bool throwOnInvalid)
  644. {
  645. this.throwOnInvalid = throwOnInvalid;
  646. leftOverBits = 0;
  647. leftOverCount = 0;
  648. }
  649. // Override inherited methods.
  650. public override int GetCharCount (byte[] bytes, int index, int count)
  651. {
  652. return InternalGetCharCount (bytes, index, count,
  653. leftOverBits, leftOverCount, throwOnInvalid, false);
  654. }
  655. public override int GetChars (byte[] bytes, int byteIndex,
  656. int byteCount, char[] chars, int charIndex)
  657. {
  658. return InternalGetChars (bytes, byteIndex, byteCount,
  659. chars, charIndex, ref leftOverBits, ref leftOverCount, throwOnInvalid, false);
  660. }
  661. } // class UTF8Decoder
  662. // UTF-8 encoder implementation.
  663. [Serializable]
  664. private class UTF8Encoder : Encoder
  665. {
  666. private bool emitIdentifier;
  667. private uint leftOver;
  668. // Constructor.
  669. public UTF8Encoder (bool emitIdentifier)
  670. {
  671. this.emitIdentifier = emitIdentifier;
  672. leftOver = 0;
  673. }
  674. // Override inherited methods.
  675. public override int GetByteCount (char[] chars, int index,
  676. int count, bool flush)
  677. {
  678. return InternalGetByteCount (chars, index, count, leftOver, flush);
  679. }
  680. public override int GetBytes (char[] chars, int charIndex,
  681. int charCount, byte[] bytes, int byteCount, bool flush)
  682. {
  683. int result;
  684. result = InternalGetBytes (chars, charIndex, charCount, bytes, byteCount, ref leftOver, flush);
  685. emitIdentifier = false;
  686. return result;
  687. }
  688. } // class UTF8Encoder
  689. }; // class UTF8Encoding
  690. }; // namespace System.Text