Beispiel #1
0
 internal void Config_ScriptCompile(object sender, ScriptSecurityEventArgs e)
 {
     if (Config.EnableScriptSecurity)
     {
         e.IsValid = ScriptChecker.IsValid(e.ReportLanguage, e.ReportScript, e.References, e.Report);
     }
 }
Beispiel #2
0
        /// <summary>
        /// Returns true, if compilation is successful
        /// </summary>
        private bool InternalCompile(CompilerParameters cp, out CompilerResults cr)
        {
            // find assembly in cache
            string   assemblyHash   = GetAssemblyHash(cp);
            Assembly cachedAssembly = null;

            if (FAssemblyCache.TryGetValue(assemblyHash, out cachedAssembly))
            {
                Assembly = cachedAssembly;
                InitInstance(Assembly.CreateInstance("FastReport.ReportScript"));
                cr = null;
                return(true);
            }


            // compile report scripts
            using (CodeDomProvider provider = Report.CodeHelper.GetCodeProvider())
            {
                ScriptSecurityEventArgs ssea = new ScriptSecurityEventArgs(Report, scriptText.ToString(), Report.ReferencedAssemblies);
                Config.OnScriptCompile(ssea);

#if NETSTANDARD || NETCOREAPP
                provider.BeforeEmitCompilation += Config.OnBeforeScriptCompilation;
#endif

                cr       = provider.CompileAssemblyFromSource(cp, scriptText.ToString());
                Assembly = null;
                Instance = null;

                if (cr.Errors.Count != 0)   // Compile errors
                {
                    return(false);
                }

                FAssemblyCache.TryAdd(assemblyHash, cr.CompiledAssembly);

                Assembly = cr.CompiledAssembly;
                InitInstance(Assembly.CreateInstance("FastReport.ReportScript"));
                return(true);
            }
        }
Beispiel #3
0
        private void InternalCompile()
        {
            // set the current folder
            string currentFolder = Config.ApplicationFolder;

            if (Config.WebMode)
            {
                try
                {
                    if (Directory.Exists(currentFolder + @"Bin\"))
                    {
                        currentFolder += @"Bin\";
                    }
                }
                catch
                {
                }
            }
            // Commented by Samuray
            //Directory.SetCurrentDirectory(currentFolder);

            // configure compiler options
            CompilerParameters cp = new CompilerParameters();

            AddFastReportAssemblies(cp.ReferencedAssemblies);
#if NETCOREAPP
            cp.ReferencedAssemblies.Add("System.Drawing.Primitives");
#endif
            AddReferencedAssemblies(cp.ReferencedAssemblies, currentFolder);
            ReviewReferencedAssemblies(cp.ReferencedAssemblies);
            cp.GenerateInMemory = true;
            // sometimes the system temp folder is not accessible...
            if (Config.TempFolder != null)
            {
                cp.TempFiles = new TempFileCollection(Config.TempFolder, false);
            }

            // find assembly in cache
            StringBuilder assemblyHashSB = new StringBuilder();
            foreach (string a in cp.ReferencedAssemblies)
            {
                assemblyHashSB.Append(a);
            }
            assemblyHashSB.Append(scriptText.ToString());
            byte[] hash = null;
            using (HMACSHA1 hMACSHA1 = new HMACSHA1(Encoding.ASCII.GetBytes(shaKey)))
            {
                hash = hMACSHA1.ComputeHash(Encoding.Unicode.GetBytes(assemblyHashSB.ToString()));
            }
            string   assemblyHash   = Convert.ToBase64String(hash);
            Assembly cachedAssembly = null;
            if (FAssemblyCache.TryGetValue(assemblyHash, out cachedAssembly))
            {
                assembly = cachedAssembly;
                InitInstance(assembly.CreateInstance("FastReport.ReportScript"));
                return;
            }

            // compile report scripts
            using (CodeDomProvider provider = Report.CodeHelper.GetCodeProvider())
            {
                ScriptSecurityEventArgs ssea = new ScriptSecurityEventArgs(Report, scriptText.ToString(), Report.ReferencedAssemblies);
                Config.OnScriptCompile(ssea);

                CompilerResults cr = provider.CompileAssemblyFromSource(cp, scriptText.ToString());
                assembly = null;
                instance = null;
                bool needException = true; // shows need to throw exception or errors can be handled without exception
                if (cr.Errors.Count > 0)
                {
                    string errors = "";
                    foreach (CompilerError ce in cr.Errors)
                    {
                        int line = GetScriptLine(ce.Line);
                        // error is inside own items
                        if (line == -1)
                        {
                            string errObjName = GetErrorObjectName(ce.Line);

                            // handle division by zero errors
                            if (ce.ErrorNumber == "CS0020")
                            {
                                TextObjectBase text = Report.FindObject(errObjName) as TextObjectBase;
                                text.CanGrow   = true;
                                text.FillColor = Color.Red;
                                text.Text      = "DIVISION BY ZERO!";
                                if (cr.Errors.Count == 1) // there are only division by zero errors, exception does't needed
                                {
                                    needException = false;
                                }
                            }
                            else
                            {
                                errors += String.Format("({0}): " + Res.Get("Messages,Error") + " {1}: {2}", new object[] { errObjName, ce.ErrorNumber, ce.ErrorText }) + "\r\n";
                                ErrorMsg(errObjName, ce);
                            }
                        }
                        else
                        {
                            errors += String.Format("({0},{1}): " + Res.Get("Messages,Error") + " {2}: {3}", new object[] { line, ce.Column, ce.ErrorNumber, ce.ErrorText }) + "\r\n";
                            ErrorMsg(ce, line);
                        }
                    }
                    if (needException) // throw exception if errors were not handled
                    {
                        throw new CompilerException(errors);
                    }
                }
                else
                {
#if DOTNET_4
                    FAssemblyCache.TryAdd(assemblyHash, cr.CompiledAssembly);
#else
                    FAssemblyCache.Add(assemblyHash, cr.CompiledAssembly);
#endif
                    assembly = cr.CompiledAssembly;
                    InitInstance(assembly.CreateInstance("FastReport.ReportScript"));
                }
            }
        }