UTF7Encoding.cs 18 KB

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