+ byte read_hex (string arg, int pos, out bool error)
+ {
+ error = false;
+
+ int total;
+ char c = arg [pos];
+
+ if ((c >= '0') && (c <= '9'))
+ total = (int) c - (int) '0';
+ else if ((c >= 'A') && (c <= 'F'))
+ total = (int) c - (int) 'A' + 10;
+ else if ((c >= 'a') && (c <= 'f'))
+ total = (int) c - (int) 'a' + 10;
+ else {
+ error = true;
+ return 0;
+ }
+
+ total *= 16;
+ c = arg [pos+1];
+
+ if ((c >= '0') && (c <= '9'))
+ total += (int) c - (int) '0';
+ else if ((c >= 'A') && (c <= 'F'))
+ total += (int) c - (int) 'A' + 10;
+ else if ((c >= 'a') && (c <= 'f'))
+ total += (int) c - (int) 'a' + 10;
+ else {
+ error = true;
+ return 0;
+ }
+
+ return (byte) total;
+ }
+
+ /// <summary>
+ /// Handles #pragma checksum
+ /// </summary>
+ bool PreProcessPragmaChecksum (string arg)
+ {
+ if ((arg [0] != ' ') && (arg [0] != '\t'))
+ return false;
+
+ arg = arg.Trim (simple_whitespaces);
+ if ((arg.Length < 2) || (arg [0] != '"'))
+ return false;
+
+ StringBuilder file_sb = new StringBuilder ();
+
+ int pos = 1;
+ char ch;
+ while ((ch = arg [pos++]) != '"') {
+ if (pos >= arg.Length)
+ return false;
+
+ if (ch == '\\') {
+ if (pos+1 >= arg.Length)
+ return false;
+ ch = arg [pos++];
+ }
+
+ file_sb.Append (ch);
+ }
+
+ if ((pos+2 >= arg.Length) || ((arg [pos] != ' ') && (arg [pos] != '\t')))
+ return false;
+
+ arg = arg.Substring (pos).Trim (simple_whitespaces);
+ if ((arg.Length < 42) || (arg [0] != '"') || (arg [1] != '{') ||
+ (arg [10] != '-') || (arg [15] != '-') || (arg [20] != '-') ||
+ (arg [25] != '-') || (arg [38] != '}') || (arg [39] != '"'))
+ return false;
+
+ bool error;
+ byte[] guid_bytes = new byte [16];
+
+ for (int i = 0; i < 4; i++) {
+ guid_bytes [i] = read_hex (arg, 2+2*i, out error);
+ if (error)
+ return false;
+ }
+ for (int i = 0; i < 2; i++) {
+ guid_bytes [i+4] = read_hex (arg, 11+2*i, out error);
+ if (error)
+ return false;
+ guid_bytes [i+6] = read_hex (arg, 16+2*i, out error);
+ if (error)
+ return false;
+ guid_bytes [i+8] = read_hex (arg, 21+2*i, out error);
+ if (error)
+ return false;
+ }
+
+ for (int i = 0; i < 6; i++) {
+ guid_bytes [i+10] = read_hex (arg, 26+2*i, out error);
+ if (error)
+ return false;
+ }
+
+ arg = arg.Substring (40).Trim (simple_whitespaces);
+ if ((arg.Length < 34) || (arg [0] != '"') || (arg [33] != '"'))
+ return false;
+
+ byte[] checksum_bytes = new byte [16];
+ for (int i = 0; i < 16; i++) {
+ checksum_bytes [i] = read_hex (arg, 1+2*i, out error);
+ if (error)
+ return false;
+ }
+
+ arg = arg.Substring (34).Trim (simple_whitespaces);
+ if (arg.Length > 0)
+ return false;
+
+ SourceFile file = Location.LookupFile (file_sb.ToString ());
+ file.SetChecksum (guid_bytes, checksum_bytes);
+ ref_name.AutoGenerated = true;
+ return true;
+ }
+