UTF7Encoding.cs 21 KB

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