UTF7Encoding.cs 21 KB

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