UTF7Encoding.cs 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801
  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. #if ECMA_COMPAT
  29. internal
  30. #else
  31. public
  32. #endif
  33. class UTF7Encoding : Encoding
  34. {
  35. // Magic number used by Windows for UTF-7.
  36. internal const int UTF7_CODE_PAGE = 65000;
  37. // Internal state.
  38. private bool allowOptionals;
  39. // Encoding rule table for 0x00-0x7F.
  40. // 0 - full encode, 1 - direct, 2 - optional, 3 - encode plus.
  41. private static readonly byte[] encodingRules = {
  42. 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, // 00
  43. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 10
  44. 1, 2, 2, 2, 2, 2, 2, 1, 1, 1, 2, 3, 1, 1, 1, 1, // 20
  45. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 1, // 30
  46. 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 40
  47. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 0, 2, 2, 2, // 50
  48. 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 60
  49. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 0, 0, // 70
  50. };
  51. // Characters to use to encode 6-bit values in base64.
  52. private const String base64Chars =
  53. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  54. // Map bytes in base64 to 6-bit values.
  55. private static readonly sbyte[] base64Values = {
  56. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 00
  57. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 10
  58. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, 63, // 20
  59. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, // 30
  60. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, // 40
  61. 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, // 50
  62. 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, // 60
  63. 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1, -1, // 70
  64. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 80
  65. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // 90
  66. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // A0
  67. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // B0
  68. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // C0
  69. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // D0
  70. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // E0
  71. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, // F0
  72. };
  73. // Constructors.
  74. public UTF7Encoding()
  75. : base(UTF7_CODE_PAGE)
  76. {
  77. allowOptionals = false;
  78. }
  79. public UTF7Encoding(bool allowOptionals)
  80. : base(UTF7_CODE_PAGE)
  81. {
  82. this.allowOptionals = allowOptionals;
  83. }
  84. // Internal version of "GetByteCount" that can handle
  85. // a rolling state between calls.
  86. private static int InternalGetByteCount
  87. (char[] chars, int index, int count, bool flush,
  88. int leftOver, bool allowOptionals)
  89. {
  90. // Validate the parameters.
  91. if(chars == null)
  92. {
  93. throw new ArgumentNullException("chars");
  94. }
  95. if(index < 0 || index > chars.Length)
  96. {
  97. throw new ArgumentOutOfRangeException
  98. ("index", _("ArgRange_Array"));
  99. }
  100. if(count < 0 || count > (chars.Length - index))
  101. {
  102. throw new ArgumentOutOfRangeException
  103. ("count", _("ArgRange_Array"));
  104. }
  105. // Determine the length of the output.
  106. int length = 0;
  107. int leftOverSize = (leftOver >> 8);
  108. byte[] rules = encodingRules;
  109. int ch, rule;
  110. while(count > 0)
  111. {
  112. ch = (int)(chars[index++]);
  113. --count;
  114. if(ch < 0x0080)
  115. {
  116. rule = rules[ch];
  117. }
  118. else
  119. {
  120. rule = 0;
  121. }
  122. switch(rule)
  123. {
  124. case 0:
  125. {
  126. // Handle characters that must be fully encoded.
  127. if(leftOverSize == 0)
  128. {
  129. ++length;
  130. }
  131. leftOverSize += 16;
  132. while(leftOverSize >= 6)
  133. {
  134. ++length;
  135. leftOverSize -= 6;
  136. }
  137. }
  138. break;
  139. case 1:
  140. {
  141. // The character is encoded as itself.
  142. if(leftOverSize != 0)
  143. {
  144. // Flush the previous encoded sequence.
  145. length += 2;
  146. leftOverSize = 0;
  147. }
  148. ++length;
  149. }
  150. break;
  151. case 2:
  152. {
  153. // The character may need to be encoded.
  154. if(allowOptionals)
  155. {
  156. goto case 1;
  157. }
  158. else
  159. {
  160. goto case 0;
  161. }
  162. }
  163. // Not reached.
  164. case 3:
  165. {
  166. // Encode the plus sign as "+-".
  167. if(leftOverSize != 0)
  168. {
  169. // Flush the previous encoded sequence.
  170. length += 2;
  171. leftOverSize = 0;
  172. }
  173. length += 2;
  174. }
  175. break;
  176. }
  177. }
  178. if(leftOverSize != 0 && flush)
  179. {
  180. length += 2;
  181. }
  182. // Return the length to the caller.
  183. return length;
  184. }
  185. // Get the number of bytes needed to encode a character buffer.
  186. public override int GetByteCount(char[] chars, int index, int count)
  187. {
  188. return InternalGetByteCount(chars, index, count,
  189. true, 0, 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, bool allowOptionals)
  197. {
  198. // Validate the parameters.
  199. if(chars == null)
  200. {
  201. throw new ArgumentNullException("chars");
  202. }
  203. if(bytes == null)
  204. {
  205. throw new ArgumentNullException("bytes");
  206. }
  207. if(charIndex < 0 || charIndex > chars.Length)
  208. {
  209. throw new ArgumentOutOfRangeException
  210. ("charIndex", _("ArgRange_Array"));
  211. }
  212. if(charCount < 0 || charCount > (chars.Length - charIndex))
  213. {
  214. throw new ArgumentOutOfRangeException
  215. ("charCount", _("ArgRange_Array"));
  216. }
  217. if(byteIndex < 0 || byteIndex > bytes.Length)
  218. {
  219. throw new ArgumentOutOfRangeException
  220. ("byteIndex", _("ArgRange_Array"));
  221. }
  222. // Convert the characters.
  223. int posn = byteIndex;
  224. int byteLength = bytes.Length;
  225. int leftOverSize = (leftOver >> 8);
  226. int leftOverBits = (leftOver & 0xFF);
  227. byte[] rules = encodingRules;
  228. String base64 = base64Chars;
  229. int ch, rule;
  230. while(charCount > 0)
  231. {
  232. ch = (int)(chars[charIndex++]);
  233. --charCount;
  234. if(ch < 0x0080)
  235. {
  236. rule = rules[ch];
  237. }
  238. else
  239. {
  240. rule = 0;
  241. }
  242. switch(rule)
  243. {
  244. case 0:
  245. {
  246. // Handle characters that must be fully encoded.
  247. if(leftOverSize == 0)
  248. {
  249. if(posn >= byteLength)
  250. {
  251. throw new ArgumentException
  252. (_("Arg_InsufficientSpace"), "bytes");
  253. }
  254. bytes[posn++] = (byte)'+';
  255. }
  256. leftOverBits = ((leftOverBits << 16) | ch);
  257. leftOverSize += 16;
  258. while(leftOverSize >= 6)
  259. {
  260. if(posn >= byteLength)
  261. {
  262. throw new ArgumentException
  263. (_("Arg_InsufficientSpace"), "bytes");
  264. }
  265. leftOverSize -= 6;
  266. bytes[posn++] = (byte)(base64
  267. [leftOverBits >> leftOverSize]);
  268. leftOverBits &= ((1 << leftOverSize) - 1);
  269. }
  270. }
  271. break;
  272. case 1:
  273. {
  274. // The character is encoded as itself.
  275. if(leftOverSize != 0)
  276. {
  277. // Flush the previous encoded sequence.
  278. if((posn + 2) > byteLength)
  279. {
  280. throw new ArgumentException
  281. (_("Arg_InsufficientSpace"), "bytes");
  282. }
  283. bytes[posn++] = (byte)(base64
  284. [leftOverBits << (6 - leftOverSize)]);
  285. bytes[posn++] = (byte)'-';
  286. leftOverSize = 0;
  287. leftOverBits = 0;
  288. }
  289. if(posn >= byteLength)
  290. {
  291. throw new ArgumentException
  292. (_("Arg_InsufficientSpace"), "bytes");
  293. }
  294. bytes[posn++] = (byte)ch;
  295. }
  296. break;
  297. case 2:
  298. {
  299. // The character may need to be encoded.
  300. if(allowOptionals)
  301. {
  302. goto case 1;
  303. }
  304. else
  305. {
  306. goto case 0;
  307. }
  308. }
  309. // Not reached.
  310. case 3:
  311. {
  312. // Encode the plus sign as "+-".
  313. if(leftOverSize != 0)
  314. {
  315. // Flush the previous encoded sequence.
  316. if((posn + 2) > byteLength)
  317. {
  318. throw new ArgumentException
  319. (_("Arg_InsufficientSpace"), "bytes");
  320. }
  321. bytes[posn++] = (byte)(base64
  322. [leftOverBits << (6 - leftOverSize)]);
  323. bytes[posn++] = (byte)'-';
  324. leftOverSize = 0;
  325. leftOverBits = 0;
  326. }
  327. if((posn + 2) > byteLength)
  328. {
  329. throw new ArgumentException
  330. (_("Arg_InsufficientSpace"), "bytes");
  331. }
  332. bytes[posn++] = (byte)'+';
  333. bytes[posn++] = (byte)'-';
  334. }
  335. break;
  336. }
  337. }
  338. if(leftOverSize != 0 && flush)
  339. {
  340. if((posn + 2) > byteLength)
  341. {
  342. throw new ArgumentException
  343. (_("Arg_InsufficientSpace"), "bytes");
  344. }
  345. bytes[posn++] = (byte)(base64
  346. [leftOverBits << (6 - leftOverSize)]);
  347. bytes[posn++] = (byte)'-';
  348. leftOverSize = 0;
  349. leftOverBits = 0;
  350. }
  351. leftOver = ((leftOverSize << 8) | leftOverBits);
  352. // Return the length to the caller.
  353. return posn - byteIndex;
  354. }
  355. // Get the bytes that result from encoding a character buffer.
  356. public override int GetBytes(char[] chars, int charIndex, int charCount,
  357. byte[] bytes, int byteIndex)
  358. {
  359. int leftOver = 0;
  360. return InternalGetBytes(chars, charIndex, charCount,
  361. bytes, byteIndex, true,
  362. ref leftOver, allowOptionals);
  363. }
  364. // Internal version of "GetCharCount" that can handle
  365. // a rolling state between call.s
  366. private static int InternalGetCharCount
  367. (byte[] bytes, int index, int count, int leftOver)
  368. {
  369. // Validate the parameters.
  370. if(bytes == null)
  371. {
  372. throw new ArgumentNullException("bytes");
  373. }
  374. if(index < 0 || index > bytes.Length)
  375. {
  376. throw new ArgumentOutOfRangeException
  377. ("index", _("ArgRange_Array"));
  378. }
  379. if(count < 0 || count > (bytes.Length - index))
  380. {
  381. throw new ArgumentOutOfRangeException
  382. ("count", _("ArgRange_Array"));
  383. }
  384. // Determine the length of the result.
  385. int length = 0;
  386. int byteval, b64value;
  387. bool normal = ((leftOver & 0x01000000) == 0);
  388. bool prevIsPlus = ((leftOver & 0x02000000) != 0);
  389. int leftOverSize = ((leftOver >> 16) & 0xFF);
  390. sbyte[] base64 = base64Values;
  391. while(count > 0)
  392. {
  393. byteval = (int)(bytes[index++]);
  394. --count;
  395. if(normal)
  396. {
  397. if(byteval != '+')
  398. {
  399. // Directly-encoded character.
  400. ++length;
  401. }
  402. else
  403. {
  404. // Start of a base64-encoded character.
  405. normal = false;
  406. prevIsPlus = true;
  407. }
  408. }
  409. else
  410. {
  411. // Process the next byte in a base64 sequence.
  412. if(byteval == (int)'-')
  413. {
  414. // End of a base64 sequence.
  415. if(prevIsPlus || leftOverSize > 0)
  416. {
  417. ++length;
  418. leftOverSize = 0;
  419. }
  420. normal = true;
  421. }
  422. else if((b64value = base64[byteval]) != -1)
  423. {
  424. // Extra character in a base64 sequence.
  425. leftOverSize += 6;
  426. if(leftOverSize >= 16)
  427. {
  428. ++length;
  429. leftOverSize -= 16;
  430. }
  431. }
  432. else
  433. {
  434. // Normal character terminating a base64 sequence.
  435. if(leftOverSize > 0)
  436. {
  437. ++length;
  438. leftOverSize = 0;
  439. }
  440. ++length;
  441. normal = true;
  442. }
  443. prevIsPlus = false;
  444. }
  445. }
  446. // Return the final length to the caller.
  447. return length;
  448. }
  449. // Get the number of characters needed to decode a byte buffer.
  450. public override int GetCharCount(byte[] bytes, int index, int count)
  451. {
  452. return InternalGetCharCount(bytes, index, count, 0);
  453. }
  454. // Internal version of "GetChars" that can handle a
  455. // rolling state between calls.
  456. private static int InternalGetChars
  457. (byte[] bytes, int byteIndex, int byteCount,
  458. char[] chars, int charIndex, ref int leftOver)
  459. {
  460. // Validate the parameters.
  461. if(bytes == null)
  462. {
  463. throw new ArgumentNullException("bytes");
  464. }
  465. if(chars == null)
  466. {
  467. throw new ArgumentNullException("chars");
  468. }
  469. if(byteIndex < 0 || byteIndex > bytes.Length)
  470. {
  471. throw new ArgumentOutOfRangeException
  472. ("byteIndex", _("ArgRange_Array"));
  473. }
  474. if(byteCount < 0 || byteCount > (bytes.Length - byteIndex))
  475. {
  476. throw new ArgumentOutOfRangeException
  477. ("byteCount", _("ArgRange_Array"));
  478. }
  479. if(charIndex < 0 || charIndex > chars.Length)
  480. {
  481. throw new ArgumentOutOfRangeException
  482. ("charIndex", _("ArgRange_Array"));
  483. }
  484. // Convert the bytes into characters.
  485. int posn = charIndex;
  486. int charLength = chars.Length;
  487. int byteval, b64value;
  488. bool normal = ((leftOver & 0x01000000) == 0);
  489. bool prevIsPlus = ((leftOver & 0x02000000) != 0);
  490. int leftOverSize = ((leftOver >> 16) & 0xFF);
  491. int leftOverBits = (leftOver & 0xFFFF);
  492. sbyte[] base64 = base64Values;
  493. while(byteCount > 0)
  494. {
  495. byteval = (int)(bytes[byteIndex++]);
  496. --byteCount;
  497. if(normal)
  498. {
  499. if(byteval != '+')
  500. {
  501. // Directly-encoded character.
  502. if(posn >= charLength)
  503. {
  504. throw new ArgumentException
  505. (_("Arg_InsufficientSpace"), "chars");
  506. }
  507. chars[posn++] = (char)byteval;
  508. }
  509. else
  510. {
  511. // Start of a base64-encoded character.
  512. normal = false;
  513. prevIsPlus = true;
  514. }
  515. }
  516. else
  517. {
  518. // Process the next byte in a base64 sequence.
  519. if(byteval == (int)'-')
  520. {
  521. // End of a base64 sequence.
  522. if(prevIsPlus)
  523. {
  524. if(posn >= charLength)
  525. {
  526. throw new ArgumentException
  527. (_("Arg_InsufficientSpace"), "chars");
  528. }
  529. chars[posn++] = '+';
  530. }
  531. else if(leftOverSize > 0)
  532. {
  533. if(posn >= charLength)
  534. {
  535. throw new ArgumentException
  536. (_("Arg_InsufficientSpace"), "chars");
  537. }
  538. chars[posn++] =
  539. (char)(leftOverBits << (16 - leftOverSize));
  540. leftOverSize = 0;
  541. leftOverBits = 0;
  542. }
  543. normal = true;
  544. }
  545. else if((b64value = base64[byteval]) != -1)
  546. {
  547. // Extra character in a base64 sequence.
  548. leftOverBits = (leftOverBits << 6) | b64value;
  549. leftOverSize += 6;
  550. if(leftOverSize >= 16)
  551. {
  552. if(posn >= charLength)
  553. {
  554. throw new ArgumentException
  555. (_("Arg_InsufficientSpace"), "chars");
  556. }
  557. leftOverSize -= 16;
  558. chars[posn++] =
  559. (char)(leftOverBits >> leftOverSize);
  560. leftOverBits &= ((1 << leftOverSize) - 1);
  561. }
  562. }
  563. else
  564. {
  565. // Normal character terminating a base64 sequence.
  566. if(leftOverSize > 0)
  567. {
  568. if(posn >= charLength)
  569. {
  570. throw new ArgumentException
  571. (_("Arg_InsufficientSpace"), "chars");
  572. }
  573. chars[posn++] =
  574. (char)(leftOverBits << (16 - leftOverSize));
  575. leftOverSize = 0;
  576. leftOverBits = 0;
  577. }
  578. if(posn >= charLength)
  579. {
  580. throw new ArgumentException
  581. (_("Arg_InsufficientSpace"), "chars");
  582. }
  583. chars[posn++] = (char)byteval;
  584. normal = true;
  585. }
  586. prevIsPlus = false;
  587. }
  588. }
  589. leftOver = (leftOverBits | (leftOverSize << 16) |
  590. (normal ? 0 : 0x01000000) |
  591. (prevIsPlus ? 0x02000000 : 0));
  592. // Return the final length to the caller.
  593. return posn - charIndex;
  594. }
  595. // Get the characters that result from decoding a byte buffer.
  596. public override int GetChars(byte[] bytes, int byteIndex, int byteCount,
  597. char[] chars, int charIndex)
  598. {
  599. int leftOver = 0;
  600. return InternalGetChars(bytes, byteIndex, byteCount,
  601. chars, charIndex, ref leftOver);
  602. }
  603. // Get the maximum number of bytes needed to encode a
  604. // specified number of characters.
  605. public override int GetMaxByteCount(int charCount)
  606. {
  607. if(charCount < 0)
  608. {
  609. throw new ArgumentOutOfRangeException
  610. ("charCount", _("ArgRange_NonNegative"));
  611. }
  612. return charCount * 5;
  613. }
  614. // Get the maximum number of characters needed to decode a
  615. // specified number of bytes.
  616. public override int GetMaxCharCount(int byteCount)
  617. {
  618. if(byteCount < 0)
  619. {
  620. throw new ArgumentOutOfRangeException
  621. ("byteCount", _("ArgRange_NonNegative"));
  622. }
  623. return byteCount;
  624. }
  625. // Get a UTF7-specific decoder that is attached to this instance.
  626. public override Decoder GetDecoder()
  627. {
  628. return new UTF7Decoder();
  629. }
  630. // Get a UTF7-specific encoder that is attached to this instance.
  631. public override Encoder GetEncoder()
  632. {
  633. return new UTF7Encoder(allowOptionals);
  634. }
  635. #if !ECMA_COMPAT
  636. // Get the mail body name for this encoding.
  637. public override String BodyName
  638. {
  639. get
  640. {
  641. return "utf-7";
  642. }
  643. }
  644. // Get the human-readable name for this encoding.
  645. public override String EncodingName
  646. {
  647. get
  648. {
  649. return "Unicode (UTF-7)";
  650. }
  651. }
  652. // Get the mail agent header name for this encoding.
  653. public override String HeaderName
  654. {
  655. get
  656. {
  657. return "utf-7";
  658. }
  659. }
  660. // Determine if this encoding can be displayed in a mail/news agent.
  661. public override bool IsMailNewsDisplay
  662. {
  663. get
  664. {
  665. return true;
  666. }
  667. }
  668. // Determine if this encoding can be saved from a mail/news agent.
  669. public override bool IsMailNewsSave
  670. {
  671. get
  672. {
  673. return true;
  674. }
  675. }
  676. // Get the IANA-preferred Web name for this encoding.
  677. public override String WebName
  678. {
  679. get
  680. {
  681. return "utf-7";
  682. }
  683. }
  684. // Get the Windows code page represented by this object.
  685. public override int WindowsCodePage
  686. {
  687. get
  688. {
  689. return UnicodeEncoding.UNICODE_CODE_PAGE;
  690. }
  691. }
  692. #endif // !ECMA_COMPAT
  693. // UTF-7 decoder implementation.
  694. private sealed class UTF7Decoder : Decoder
  695. {
  696. // Internal state.
  697. private int leftOver;
  698. // Constructor.
  699. public UTF7Decoder()
  700. {
  701. leftOver = 0;
  702. }
  703. // Override inherited methods.
  704. public override int GetCharCount(byte[] bytes, int index, int count)
  705. {
  706. return InternalGetCharCount(bytes, index, count, leftOver);
  707. }
  708. public override int GetChars(byte[] bytes, int byteIndex,
  709. int byteCount, char[] chars,
  710. int charIndex)
  711. {
  712. return InternalGetChars(bytes, byteIndex, byteCount,
  713. chars, charIndex, ref leftOver);
  714. }
  715. } // class UTF7Decoder
  716. // UTF-7 encoder implementation.
  717. private sealed class UTF7Encoder : Encoder
  718. {
  719. private bool allowOptionals;
  720. private int leftOver;
  721. // Constructor.
  722. public UTF7Encoder(bool allowOptionals)
  723. {
  724. this.allowOptionals = allowOptionals;
  725. this.leftOver = 0;
  726. }
  727. // Override inherited methods.
  728. public override int GetByteCount(char[] chars, int index,
  729. int count, bool flush)
  730. {
  731. return InternalGetByteCount
  732. (chars, index, count, flush, leftOver, allowOptionals);
  733. }
  734. public override int GetBytes(char[] chars, int charIndex,
  735. int charCount, byte[] bytes,
  736. int byteIndex, bool flush)
  737. {
  738. return InternalGetBytes(chars, charIndex, charCount,
  739. bytes, byteIndex, flush,
  740. ref leftOver, allowOptionals);
  741. }
  742. } // class UTF7Encoder
  743. }; // class UTF7Encoding
  744. }; // namespace System.Text