UTF7Encoding.cs 21 KB

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