UTF7Encoding.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630
  1. /*
  2. * UTF7Encoding.cs - Implementation of the
  3. * "System.Text.UTF7Encoding" class.
  4. *
  5. * Copyright (c) 2002 Southern Storm Software, Pty Ltd
  6. * Copyright (c) 2003, 2004, Novell, Inc.
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining
  9. * a copy of this software and associated documentation files (the "Software"),
  10. * to deal in the Software without restriction, including without limitation
  11. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  12. * and/or sell copies of the Software, and to permit persons to whom the
  13. * Software is furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included
  16. * in all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  19. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  21. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  22. * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  23. * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  24. * OTHER DEALINGS IN THE SOFTWARE.
  25. */
  26. namespace System.Text
  27. {
  28. using System;
  29. [Serializable]
  30. [MonoTODO ("Fix serialization compatibility with MS.NET")]
  31. #if ECMA_COMPAT
  32. internal
  33. #else
  34. public
  35. #endif
  36. class UTF7Encoding : Encoding
  37. {
  38. // Magic number used by Windows for UTF-7.
  39. internal const int UTF7_CODE_PAGE = 65000;
  40. // Internal state.
  41. private bool allowOptionals;
  42. // Encoding rule table for 0x00-0x7F.
  43. // 0 - full encode, 1 - direct, 2 - optional, 3 - encode plus.
  44. private static readonly byte[] encodingRules = {
  45. 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, // 00
  46. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10
  47. 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 2, 3, 1, 1, 1, 1, // 20
  48. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 1, // 30
  49. 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 40
  50. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 0, 2, 2, 2, // 50
  51. 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 60
  52. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 0, 0, // 70
  53. };
  54. // Characters to use to encode 6-bit values in base64.
  55. private const String base64Chars =
  56. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  57. // Map bytes in base64 to 6-bit values.
  58. private static readonly sbyte[] base64Values = {
  59. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 00
  60. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 10
  61. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, 63, // 20
  62. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, // 30
  63. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, // 40
  64. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, // 50
  65. -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, // 60
  66. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, // 70
  67. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 80
  68. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 90
  69. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // A0
  70. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // B0
  71. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // C0
  72. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // D0
  73. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // E0
  74. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // F0
  75. };
  76. // Constructors.
  77. public UTF7Encoding ()
  78. : this (false)
  79. {
  80. }
  81. public UTF7Encoding (bool allowOptionals)
  82. : base (UTF7_CODE_PAGE)
  83. {
  84. this.allowOptionals = allowOptionals;
  85. body_name = "utf-7";
  86. encoding_name = "Unicode (UTF-7)";
  87. header_name = "utf-7";
  88. is_mail_news_display = true;
  89. is_mail_news_save = true;
  90. web_name = "utf-7";
  91. windows_code_page = UnicodeEncoding.UNICODE_CODE_PAGE;
  92. }
  93. // Internal version of "GetByteCount" that can handle
  94. // a rolling state between calls.
  95. private static int InternalGetByteCount
  96. (char[] chars, int index, int count, bool flush,
  97. int leftOver, bool isInShifted, bool allowOptionals)
  98. {
  99. // Validate the parameters.
  100. if (chars == null) {
  101. throw new ArgumentNullException ("chars");
  102. }
  103. if (index < 0 || index > chars.Length) {
  104. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  105. }
  106. if (count < 0 || count > (chars.Length - index)) {
  107. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  108. }
  109. // Determine the length of the output.
  110. int length = 0;
  111. int leftOverSize = (leftOver >> 8);
  112. byte[] rules = encodingRules;
  113. int ch, rule;
  114. while (count > 0) {
  115. ch = (int)(chars[index++]);
  116. --count;
  117. if (ch < 0x0080) {
  118. rule = rules[ch];
  119. } else {
  120. rule = 0;
  121. }
  122. switch (rule) {
  123. case 0:
  124. // Handle characters that must be fully encoded.
  125. if ( !isInShifted ) {
  126. ++length;
  127. leftOverSize = 0;
  128. isInShifted = true;
  129. }
  130. leftOverSize += 16;
  131. while (leftOverSize >= 6) {
  132. ++length;
  133. leftOverSize -= 6;
  134. }
  135. break;
  136. case 1:
  137. // The character is encoded as itself.
  138. if (isInShifted) {
  139. if (leftOverSize != 0) {
  140. // Flush the previous encoded sequence.
  141. ++length;
  142. leftOverSize = 0;
  143. }
  144. // Count the "-" (sequence terminator)
  145. ++length;
  146. isInShifted = false;
  147. }
  148. ++length;
  149. break;
  150. case 2:
  151. // The character may need to be encoded.
  152. if (allowOptionals) {
  153. goto case 1;
  154. } else {
  155. goto case 0;
  156. }
  157. // Not reached.
  158. case 3:
  159. // Encode the plus sign as "+-".
  160. if (isInShifted) {
  161. if (leftOverSize != 0) {
  162. // Flush the previous encoded sequence.
  163. ++length;
  164. leftOverSize = 0;
  165. }
  166. // Count the "-" (sequence terminator)
  167. ++length;
  168. isInShifted = false;
  169. }
  170. length += 2;
  171. break;
  172. }
  173. }
  174. if (isInShifted && flush) {
  175. if (leftOverSize != 0)
  176. {
  177. // Flush the previous encoded sequence.
  178. ++length;
  179. }
  180. // Count the "-" (sequence terminator)
  181. ++length;
  182. }
  183. // Return the length to the caller.
  184. return length;
  185. }
  186. // Get the number of bytes needed to encode a character buffer.
  187. public override int GetByteCount (char[] chars, int index, int count)
  188. {
  189. return InternalGetByteCount (chars, index, count, true, 0, false, allowOptionals);
  190. }
  191. // Internal version of "GetBytes" that can handle a
  192. // rolling state between calls.
  193. private static int InternalGetBytes
  194. (char[] chars, int charIndex, int charCount,
  195. byte[] bytes, int byteIndex, bool flush,
  196. ref int leftOver, ref bool isInShifted, bool allowOptionals)
  197. {
  198. // Validate the parameters.
  199. if (chars == null) {
  200. throw new ArgumentNullException ("chars");
  201. }
  202. if (bytes == null) {
  203. throw new ArgumentNullException ("bytes");
  204. }
  205. if (charIndex < 0 || charIndex > chars.Length) {
  206. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
  207. }
  208. if (charCount < 0 || charCount > (chars.Length - charIndex)) {
  209. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_Array"));
  210. }
  211. if (byteIndex < 0 || byteIndex > bytes.Length) {
  212. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  213. }
  214. // Convert the characters.
  215. int posn = byteIndex;
  216. int byteLength = bytes.Length;
  217. int leftOverSize = (leftOver >> 8);
  218. int leftOverBits = (leftOver & 0xFF);
  219. byte[] rules = encodingRules;
  220. String base64 = base64Chars;
  221. int ch, rule;
  222. while (charCount > 0) {
  223. ch = (int)(chars[charIndex++]);
  224. --charCount;
  225. if (ch < 0x0080) {
  226. rule = rules[ch];
  227. } else {
  228. rule = 0;
  229. }
  230. switch (rule) {
  231. case 0:
  232. // Handle characters that must be fully encoded.
  233. if (!isInShifted) {
  234. if (posn >= byteLength) {
  235. throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
  236. }
  237. // Start the sequence
  238. bytes[posn++] = (byte)'+';
  239. isInShifted = true;
  240. leftOverSize = 0;
  241. }
  242. leftOverBits = ((leftOverBits << 16) | ch);
  243. leftOverSize += 16;
  244. while (leftOverSize >= 6) {
  245. if (posn >= byteLength) {
  246. throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
  247. }
  248. leftOverSize -= 6;
  249. bytes[posn++] = (byte)(base64 [leftOverBits >> leftOverSize]);
  250. leftOverBits &= ((1 << leftOverSize) - 1);
  251. }
  252. break;
  253. case 1:
  254. // The character is encoded as itself.
  255. if (isInShifted) {
  256. if (leftOverSize != 0) {
  257. // Flush the previous encoded sequence.
  258. if ((posn + 1) > byteLength) {
  259. throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
  260. }
  261. bytes[posn++] = (byte)(base64 [leftOverBits << (6 - leftOverSize)]);
  262. }
  263. if ((posn + 1) > byteLength) {
  264. throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
  265. }
  266. // Terminate the sequence
  267. bytes[posn++] = (byte)'-';
  268. isInShifted = false;
  269. leftOverSize = 0;
  270. leftOverBits = 0;
  271. }
  272. if (posn >= byteLength) {
  273. throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
  274. }
  275. bytes[posn++] = (byte)ch;
  276. break;
  277. case 2:
  278. // The character may need to be encoded.
  279. if (allowOptionals) {
  280. goto case 1;
  281. } else {
  282. goto case 0;
  283. }
  284. // Not reached.
  285. case 3:
  286. // Encode the plus sign as "+-".
  287. if (isInShifted) {
  288. if (leftOverSize != 0) {
  289. // Flush the previous encoded sequence.
  290. if ((posn + 1) > byteLength) {
  291. throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
  292. }
  293. bytes[posn++] = (byte)(base64 [leftOverBits << (6 - leftOverSize)]);
  294. }
  295. if ((posn + 1) > byteLength) {
  296. throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
  297. }
  298. // Terminate the sequence
  299. bytes[posn++] = (byte)'-';
  300. isInShifted = false;
  301. leftOverSize = 0;
  302. leftOverBits = 0;
  303. }
  304. if ((posn + 2) > byteLength) {
  305. throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
  306. }
  307. bytes[posn++] = (byte)'+';
  308. bytes[posn++] = (byte)'-';
  309. break;
  310. }
  311. }
  312. if (isInShifted && flush) {
  313. // Flush the previous encoded sequence.
  314. if (leftOverSize != 0) {
  315. if ((posn + 1) > byteLength) {
  316. throw new ArgumentException (_("Arg_InsufficientSpace"), "bytes");
  317. }
  318. bytes[posn++] = (byte)(base64 [leftOverBits << (6 - leftOverSize)]);
  319. }
  320. // Terminate the sequence
  321. bytes[posn++] = (byte)'-';
  322. leftOverSize = 0;
  323. leftOverBits = 0;
  324. isInShifted = false;
  325. }
  326. leftOver = ((leftOverSize << 8) | leftOverBits);
  327. // Return the length to the caller.
  328. return posn - byteIndex;
  329. }
  330. // Get the bytes that result from encoding a character buffer.
  331. public override int GetBytes (char[] chars, int charIndex, int charCount,
  332. byte[] bytes, int byteIndex)
  333. {
  334. int leftOver = 0;
  335. bool isInShifted = false;
  336. return InternalGetBytes (chars, charIndex, charCount, bytes, byteIndex, true,
  337. ref leftOver, ref isInShifted, allowOptionals);
  338. }
  339. // Internal version of "GetCharCount" that can handle
  340. // a rolling state between call.s
  341. private static int InternalGetCharCount
  342. (byte[] bytes, int index, int count, int leftOver)
  343. {
  344. // Validate the parameters.
  345. if (bytes == null) {
  346. throw new ArgumentNullException ("bytes");
  347. }
  348. if (index < 0 || index > bytes.Length) {
  349. throw new ArgumentOutOfRangeException ("index", _("ArgRange_Array"));
  350. }
  351. if (count < 0 || count > (bytes.Length - index)) {
  352. throw new ArgumentOutOfRangeException ("count", _("ArgRange_Array"));
  353. }
  354. // Determine the length of the result.
  355. int length = 0;
  356. int byteval;
  357. bool normal = ((leftOver & 0x01000000) == 0);
  358. bool prevIsPlus = ((leftOver & 0x02000000) != 0);
  359. int leftOverSize = ((leftOver >> 16) & 0xFF);
  360. sbyte[] base64 = base64Values;
  361. while (count > 0) {
  362. byteval = (int)(bytes[index++]);
  363. --count;
  364. if (normal) {
  365. if (byteval != '+') {
  366. // Directly-encoded character.
  367. ++length;
  368. } else {
  369. // Start of a base64-encoded character.
  370. normal = false;
  371. prevIsPlus = true;
  372. }
  373. } else {
  374. // Process the next byte in a base64 sequence.
  375. if (byteval == (int)'-') {
  376. // End of a base64 sequence.
  377. if (prevIsPlus) {
  378. ++length;
  379. leftOverSize = 0;
  380. }
  381. normal = true;
  382. } else if (base64 [byteval] != -1) {
  383. // Extra character in a base64 sequence.
  384. leftOverSize += 6;
  385. if (leftOverSize >= 16) {
  386. ++length;
  387. leftOverSize -= 16;
  388. }
  389. } else {
  390. ++length;
  391. normal = true;
  392. leftOverSize = 0;
  393. }
  394. prevIsPlus = false;
  395. }
  396. }
  397. // Return the final length to the caller.
  398. return length;
  399. }
  400. // Get the number of characters needed to decode a byte buffer.
  401. public override int GetCharCount (byte[] bytes, int index, int count)
  402. {
  403. return InternalGetCharCount (bytes, index, count, 0);
  404. }
  405. // Internal version of "GetChars" that can handle a
  406. // rolling state between calls.
  407. private static int InternalGetChars (byte[] bytes, int byteIndex, int byteCount,
  408. char[] chars, int charIndex, ref int leftOver)
  409. {
  410. // Validate the parameters.
  411. if (bytes == null) {
  412. throw new ArgumentNullException ("bytes");
  413. }
  414. if (chars == null) {
  415. throw new ArgumentNullException ("chars");
  416. }
  417. if (byteIndex < 0 || byteIndex > bytes.Length) {
  418. throw new ArgumentOutOfRangeException ("byteIndex", _("ArgRange_Array"));
  419. }
  420. if (byteCount < 0 || byteCount > (bytes.Length - byteIndex)) {
  421. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_Array"));
  422. }
  423. if (charIndex < 0 || charIndex > chars.Length) {
  424. throw new ArgumentOutOfRangeException ("charIndex", _("ArgRange_Array"));
  425. }
  426. // Convert the bytes into characters.
  427. int posn = charIndex;
  428. int charLength = chars.Length;
  429. int byteval, b64value;
  430. bool normal = ((leftOver & 0x01000000) == 0);
  431. bool prevIsPlus = ((leftOver & 0x02000000) != 0);
  432. int leftOverSize = ((leftOver >> 16) & 0xFF);
  433. int leftOverBits = (leftOver & 0xFFFF);
  434. sbyte[] base64 = base64Values;
  435. while (byteCount > 0) {
  436. byteval = (int)(bytes[byteIndex++]);
  437. --byteCount;
  438. if (normal) {
  439. if (byteval != '+') {
  440. // Directly-encoded character.
  441. if (posn >= charLength) {
  442. throw new ArgumentException (_("Arg_InsufficientSpace"), "chars");
  443. }
  444. chars[posn++] = (char)byteval;
  445. } else {
  446. // Start of a base64-encoded character.
  447. normal = false;
  448. prevIsPlus = true;
  449. }
  450. } else {
  451. // Process the next byte in a base64 sequence.
  452. if (byteval == (int)'-') {
  453. // End of a base64 sequence.
  454. if (prevIsPlus) {
  455. if (posn >= charLength) {
  456. throw new ArgumentException (_("Arg_InsufficientSpace"), "chars");
  457. }
  458. chars[posn++] = '+';
  459. }
  460. // RFC1642 Rule #2
  461. // When decoding, any bits at the end of the Modified Base64 sequence that
  462. // do not constitute a complete 16-bit Unicode character are discarded.
  463. // If such discarded bits are non-zero the sequence is ill-formed.
  464. normal = true;
  465. leftOverSize = 0;
  466. leftOverBits = 0;
  467. }
  468. else if ((b64value = base64[byteval]) != -1)
  469. {
  470. // Extra character in a base64 sequence.
  471. leftOverBits = (leftOverBits << 6) | b64value;
  472. leftOverSize += 6;
  473. if (leftOverSize >= 16) {
  474. if (posn >= charLength) {
  475. throw new ArgumentException (_("Arg_InsufficientSpace"), "chars");
  476. }
  477. leftOverSize -= 16;
  478. chars[posn++] = (char)(leftOverBits >> leftOverSize);
  479. leftOverBits &= ((1 << leftOverSize) - 1);
  480. }
  481. } else {
  482. if (posn >= charLength) {
  483. throw new ArgumentException (_("Arg_InsufficientSpace"), "chars");
  484. }
  485. chars[posn++] = (char)byteval;
  486. normal = true;
  487. leftOverSize = 0;
  488. leftOverBits = 0;
  489. }
  490. prevIsPlus = false;
  491. }
  492. }
  493. leftOver = (leftOverBits | (leftOverSize << 16) |
  494. (normal ? 0 : 0x01000000) |
  495. (prevIsPlus ? 0x02000000 : 0));
  496. // Return the final length to the caller.
  497. return posn - charIndex;
  498. }
  499. // Get the characters that result from decoding a byte buffer.
  500. public override int GetChars (byte[] bytes, int byteIndex, int byteCount,
  501. char[] chars, int charIndex)
  502. {
  503. int leftOver = 0;
  504. return InternalGetChars (bytes, byteIndex, byteCount, chars, charIndex, ref leftOver);
  505. }
  506. // Get the maximum number of bytes needed to encode a
  507. // specified number of characters.
  508. public override int GetMaxByteCount (int charCount)
  509. {
  510. if (charCount < 0) {
  511. throw new ArgumentOutOfRangeException ("charCount", _("ArgRange_NonNegative"));
  512. }
  513. if (charCount == 0)
  514. return 0;
  515. return 8 * (int) (charCount / 3) + (charCount % 3) * 3 + 2;
  516. }
  517. // Get the maximum number of characters needed to decode a
  518. // specified number of bytes.
  519. public override int GetMaxCharCount (int byteCount)
  520. {
  521. if (byteCount < 0) {
  522. throw new ArgumentOutOfRangeException ("byteCount", _("ArgRange_NonNegative"));
  523. }
  524. return byteCount;
  525. }
  526. // Get a UTF7-specific decoder that is attached to this instance.
  527. public override Decoder GetDecoder ()
  528. {
  529. return new UTF7Decoder ();
  530. }
  531. // Get a UTF7-specific encoder that is attached to this instance.
  532. public override Encoder GetEncoder ()
  533. {
  534. return new UTF7Encoder (allowOptionals);
  535. }
  536. // UTF-7 decoder implementation.
  537. private sealed class UTF7Decoder : Decoder
  538. {
  539. // Internal state.
  540. private int leftOver;
  541. // Constructor.
  542. public UTF7Decoder ()
  543. {
  544. leftOver = 0;
  545. }
  546. // Override inherited methods.
  547. public override int GetCharCount (byte[] bytes, int index, int count)
  548. {
  549. return InternalGetCharCount (bytes, index, count, leftOver);
  550. }
  551. public override int GetChars (byte[] bytes, int byteIndex,
  552. int byteCount, char[] chars,
  553. int charIndex)
  554. {
  555. return InternalGetChars (bytes, byteIndex, byteCount, chars, charIndex, ref leftOver);
  556. }
  557. } // class UTF7Decoder
  558. // UTF-7 encoder implementation.
  559. private sealed class UTF7Encoder : Encoder
  560. {
  561. private bool allowOptionals;
  562. private int leftOver = 0;
  563. private bool isInShifted = false;
  564. // Constructor.
  565. public UTF7Encoder (bool allowOptionals)
  566. {
  567. this.allowOptionals = allowOptionals;
  568. }
  569. // Override inherited methods.
  570. public override int GetByteCount (char[] chars, int index,
  571. int count, bool flush)
  572. {
  573. return InternalGetByteCount
  574. (chars, index, count, flush, leftOver, isInShifted, allowOptionals);
  575. }
  576. public override int GetBytes (char[] chars, int charIndex,
  577. int charCount, byte[] bytes,
  578. int byteIndex, bool flush)
  579. {
  580. return InternalGetBytes (chars, charIndex, charCount,
  581. bytes, byteIndex, flush,
  582. ref leftOver, ref isInShifted, allowOptionals);
  583. }
  584. } // class UTF7Encoder
  585. }; // class UTF7Encoding
  586. }; // namespace System.Text