AspTokenizer.cs 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  1. //
  2. // System.Web.Compilation.AspTokenizer
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. //
  7. // (C) 2002,2003 Ximian, Inc (http://www.ximian.com)
  8. //
  9. //
  10. // Permission is hereby granted, free of charge, to any person obtaining
  11. // a copy of this software and associated documentation files (the
  12. // "Software"), to deal in the Software without restriction, including
  13. // without limitation the rights to use, copy, modify, merge, publish,
  14. // distribute, sublicense, and/or sell copies of the Software, and to
  15. // permit persons to whom the Software is furnished to do so, subject to
  16. // the following conditions:
  17. //
  18. // The above copyright notice and this permission notice shall be
  19. // included in all copies or substantial portions of the Software.
  20. //
  21. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  22. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  23. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  24. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  25. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  26. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  27. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  28. //
  29. using System;
  30. using System.Collections;
  31. using System.IO;
  32. using System.Text;
  33. using System.Security.Cryptography;
  34. namespace System.Web.Compilation
  35. {
  36. class Token
  37. {
  38. public const int EOF = 0x0200000;
  39. public const int IDENTIFIER = 0x0200001;
  40. public const int DIRECTIVE = 0x0200002;
  41. public const int ATTVALUE = 0x0200003;
  42. public const int TEXT = 0x0200004;
  43. public const int DOUBLEDASH = 0x0200005;
  44. public const int CLOSING = 0x0200006;
  45. }
  46. class AspTokenizer
  47. {
  48. #if NET_2_0
  49. const int CHECKSUM_BUF_SIZE = 8192;
  50. #endif
  51. class PutBackItem
  52. {
  53. public readonly string Value;
  54. public readonly int Position;
  55. public readonly int CurrentToken;
  56. public readonly bool InTag;
  57. public PutBackItem (string value, int position, int currentToken, bool inTag)
  58. {
  59. Value = value;
  60. Position = position;
  61. CurrentToken = currentToken;
  62. InTag = inTag;
  63. }
  64. }
  65. static char [] lfcr = new char [] { '\n', '\r' };
  66. TextReader sr;
  67. int current_token;
  68. StringBuilder sb, odds;
  69. int col, line;
  70. int begcol, begline;
  71. int position;
  72. bool inTag;
  73. bool expectAttrValue;
  74. bool alternatingQuotes;
  75. bool hasPutBack;
  76. bool verbatim;
  77. bool have_value;
  78. bool have_unget;
  79. int unget_value;
  80. string val;
  81. Stack putBackBuffer;
  82. #if NET_2_0
  83. MD5 checksum;
  84. char[] checksum_buf = new char [CHECKSUM_BUF_SIZE];
  85. int checksum_buf_pos = -1;
  86. public MD5 Checksum {
  87. get { return checksum; }
  88. }
  89. #endif
  90. public AspTokenizer (TextReader reader)
  91. {
  92. this.sr = reader;
  93. sb = new StringBuilder ();
  94. odds= new StringBuilder();
  95. col = line = 1;
  96. hasPutBack = inTag = false;
  97. }
  98. public bool Verbatim
  99. {
  100. get { return verbatim; }
  101. set { verbatim = value; }
  102. }
  103. public void put_back ()
  104. {
  105. if (hasPutBack && !inTag)
  106. throw new HttpException ("put_back called twice!");
  107. hasPutBack = true;
  108. if (putBackBuffer == null)
  109. putBackBuffer = new Stack ();
  110. string val = Value;
  111. putBackBuffer.Push (new PutBackItem (val, position, current_token, inTag));
  112. position -= val.Length;
  113. }
  114. public int get_token ()
  115. {
  116. if (hasPutBack) {
  117. PutBackItem pbi = putBackBuffer.Pop () as PutBackItem;
  118. hasPutBack = putBackBuffer.Count > 0;
  119. position = pbi.Position;
  120. have_value = false;
  121. val = null;
  122. sb = new StringBuilder (pbi.Value);
  123. current_token = pbi.CurrentToken;
  124. inTag = pbi.InTag;
  125. return current_token;
  126. }
  127. begline = line;
  128. begcol = col;
  129. have_value = false;
  130. current_token = NextToken ();
  131. return current_token;
  132. }
  133. bool is_identifier_start_character (char c)
  134. {
  135. return (Char.IsLetter (c) || c == '_' );
  136. }
  137. bool is_identifier_part_character (char c)
  138. {
  139. return (Char.IsLetterOrDigit (c) || c == '_' || c == '-');
  140. }
  141. void ungetc (int value)
  142. {
  143. have_unget = true;
  144. unget_value = value;
  145. // Only '/' passes through here now.
  146. // If we ever let \n here, update 'line'
  147. position--;
  148. col--;
  149. }
  150. #if NET_2_0
  151. void TransformNextBlock (int count, bool final)
  152. {
  153. byte[] input = Encoding.UTF8.GetBytes (checksum_buf, 0, count);
  154. if (checksum == null)
  155. checksum = MD5.Create ();
  156. if (final)
  157. checksum.TransformFinalBlock (input, 0, input.Length);
  158. else
  159. checksum.TransformBlock (input, 0, input.Length, input, 0);
  160. input = null;
  161. checksum_buf_pos = -1;
  162. }
  163. void UpdateChecksum (int c)
  164. {
  165. bool final = c == -1;
  166. if (!final) {
  167. if (checksum_buf_pos + 1 >= CHECKSUM_BUF_SIZE)
  168. TransformNextBlock (checksum_buf_pos + 1, false);
  169. checksum_buf [++checksum_buf_pos] = (char)c;
  170. } else
  171. TransformNextBlock (checksum_buf_pos + 1, true);
  172. }
  173. #endif
  174. int read_char ()
  175. {
  176. int c;
  177. if (have_unget) {
  178. c = unget_value;
  179. have_unget = false;
  180. } else {
  181. c = sr.Read ();
  182. #if NET_2_0
  183. UpdateChecksum (c);
  184. #endif
  185. }
  186. if (c == '\r' && sr.Peek () == '\n') {
  187. c = sr.Read ();
  188. #if NET_2_0
  189. UpdateChecksum (c);
  190. #endif
  191. position++;
  192. }
  193. if (c == '\n'){
  194. col = -1;
  195. line++;
  196. }
  197. if (c != -1) {
  198. col++;
  199. position++;
  200. }
  201. return c;
  202. }
  203. int ReadAttValue (int start)
  204. {
  205. int quoteChar = 0;
  206. bool quoted = false;
  207. if (start == '"' || start == '\'') {
  208. quoteChar = start;
  209. quoted = true;
  210. } else {
  211. sb.Append ((char) start);
  212. }
  213. int c;
  214. int last = 0;
  215. bool inServerTag = false;
  216. alternatingQuotes = true;
  217. while ((c = sr.Peek ()) != -1) {
  218. if (c == '%' && last == '<') {
  219. inServerTag = true;
  220. } else if (inServerTag && c == '>' && last == '%') {
  221. inServerTag = false;
  222. } else if (!inServerTag) {
  223. if (!quoted && c == '/') {
  224. read_char ();
  225. c = sr.Peek ();
  226. if (c == -1) {
  227. c = '/';
  228. } else if (c == '>') {
  229. ungetc ('/');
  230. break;
  231. }
  232. } else if (!quoted && (c == '>' || Char.IsWhiteSpace ((char) c))) {
  233. break;
  234. } else if (quoted && c == quoteChar && last != '\\') {
  235. read_char ();
  236. break;
  237. }
  238. } else if (quoted && c == quoteChar) {
  239. alternatingQuotes = false;
  240. }
  241. sb.Append ((char) c);
  242. read_char ();
  243. last = c;
  244. }
  245. return Token.ATTVALUE;
  246. }
  247. int NextToken ()
  248. {
  249. int c;
  250. sb.Length = 0;
  251. odds.Length=0;
  252. while ((c = read_char ()) != -1){
  253. if (verbatim){
  254. inTag = false;
  255. sb.Append ((char) c);
  256. return c;
  257. }
  258. if (inTag && expectAttrValue && (c == '"' || c == '\''))
  259. return ReadAttValue (c);
  260. if (c == '<'){
  261. inTag = true;
  262. sb.Append ((char) c);
  263. return c;
  264. }
  265. if (c == '>'){
  266. inTag = false;
  267. sb.Append ((char) c);
  268. return c;
  269. }
  270. if (current_token == '<' && "%/!".IndexOf ((char) c) != -1){
  271. sb.Append ((char) c);
  272. return c;
  273. }
  274. if (inTag && current_token == '%' && "@#=".IndexOf ((char) c) != -1){
  275. if (odds.Length == 0 || odds.ToString ().IndexOfAny (lfcr) < 0) {
  276. sb.Append ((char) c);
  277. return c;
  278. }
  279. sb.Append ((char) c);
  280. continue;
  281. }
  282. if (inTag && c == '-' && sr.Peek () == '-'){
  283. sb.Append ("--");
  284. read_char ();
  285. return Token.DOUBLEDASH;
  286. }
  287. if (!inTag){
  288. sb.Append ((char) c);
  289. while ((c = sr.Peek ()) != -1 && c != '<')
  290. sb.Append ((char) read_char ());
  291. return (c != -1 || sb.Length > 0) ? Token.TEXT : Token.EOF;
  292. }
  293. if (inTag && current_token == '=' && !Char.IsWhiteSpace ((char) c))
  294. return ReadAttValue (c);
  295. if (inTag && is_identifier_start_character ((char) c)){
  296. sb.Append ((char) c);
  297. while ((c = sr.Peek ()) != -1) {
  298. if (!is_identifier_part_character ((char) c) && c != ':')
  299. break;
  300. sb.Append ((char) read_char ());
  301. }
  302. if (current_token == '@' && Directive.IsDirective (sb.ToString ()))
  303. return Token.DIRECTIVE;
  304. return Token.IDENTIFIER;
  305. }
  306. if (!Char.IsWhiteSpace ((char) c)) {
  307. sb.Append ((char) c);
  308. return c;
  309. }
  310. // keep otherwise discarded characters in case we need.
  311. odds.Append((char) c);
  312. }
  313. return Token.EOF;
  314. }
  315. public string Value {
  316. get {
  317. if (have_value)
  318. return val;
  319. have_value = true;
  320. val = sb.ToString ();
  321. return val;
  322. }
  323. }
  324. public string Odds {
  325. get {
  326. return odds.ToString();
  327. }
  328. }
  329. public bool InTag {
  330. get { return inTag; }
  331. set { inTag = value; }
  332. }
  333. // Hack for preventing confusion with VB comments (see bug #63451)
  334. public bool ExpectAttrValue {
  335. get { return expectAttrValue; }
  336. set { expectAttrValue = value; }
  337. }
  338. public bool AlternatingQuotes {
  339. get { return alternatingQuotes; }
  340. }
  341. public int BeginLine {
  342. get { return begline; }
  343. }
  344. public int BeginColumn {
  345. get { return begcol; }
  346. }
  347. public int EndLine {
  348. get { return line; }
  349. }
  350. public int EndColumn {
  351. get { return col; }
  352. }
  353. public int Position {
  354. get { return position; }
  355. }
  356. }
  357. }