UTF7Encoding.cs 21 KB

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