AspTokenizer.cs 9.3 KB

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