text:
% \iffalse meta-comment
%
% Copyright 1993-2016
% The LaTeX3 Project and any individual authors listed elsewhere
% in this file.
%
% This file is part of the LaTeX base system.
% -------------------------------------------
%
% It may be distributed and/or modified under the
% conditions of the LaTeX Project Public License, either version 1.3c
% of this license or (at your option) any later version.
% The latest version of this license is in
%    http://www.latex-project.org/lppl.txt
% and version 1.3c or later is part of all distributions of LaTeX
% version 2005/12/01 or later.
%
% This file has the LPPL maintenance status "maintained".
%
% The list of all files belonging to the LaTeX base distribution is
% given in the file `manifest.txt'. See also `legal.txt' for additional
% information.
%
% The list of derived (unpacked) files belonging to the distribution
% and covered by LPPL is defined by the unpacking scripts (with
% extension .ins) which are part of the distribution.
%
% \fi

% This document will typeset the LaTeX sources as a single document.
% This will produce quite a large file (roughly 555 pages) and may
% take a long time.
%
% Some notes on processing this document are contained at the end
% of this document, after \end{document}
%
% DPC 1997/11/17
%
% First a special index style for makeindex
%

\begin{filecontents}{source2e.ist}
actual '='
quote '!'
level '>'
preamble
"\n \\begin{theindex} \n \\makeatletter\\scan@allowedfalse\n"
postamble
"\n\n \\end{theindex}\n"
item_x1   "\\efill \n \\subitem "
item_x2   "\\efill \n \\subsubitem "
delim_0   "\\pfill "
delim_1   "\\pfill "
delim_2   "\\pfill "
% The next lines will produce some warnings when
% running Makeindex as they try to cover two different
% versions of the program:
lethead_prefix   "{\\bfseries\\hfil "
lethead_suffix   "\\hfil}\\nopagebreak\n"
lethead_flag       1
heading_prefix   "{\\bfseries\\hfil "
heading_suffix   "\\hfil}\\nopagebreak\n"
headings_flag       1

% and just for source2e:
% Remove R so I is treated in sequence I J K not I II III
page_precedence "rnaA"
\end{filecontents}

\documentclass{ltxdoc}

\listfiles

% Do not index some TeX primitives, and some common plain TeX commands.

\DoNotIndex{\def,\long,\edef,\xdef,\gdef,\let,\global}
\DoNotIndex{\if,\ifnum,\ifdim,\ifcat,\ifmmode,\ifvmode,\ifhmode,%
            \iftrue,\iffalse,\ifvoid,\ifx,\ifeof,\ifcase,\else,\or,\fi}
\DoNotIndex{\box,\copy,\setbox,\unvbox,\unhbox,\hbox,%
            \vbox,\vtop,\vcenter}
\DoNotIndex{\@empty,\immediate,\write}
\DoNotIndex{\egroup,\bgroup,\expandafter,\begingroup,\endgroup}
\DoNotIndex{\divide,\advance,\multiply,\count,\dimen}
\DoNotIndex{\relax,\space,\string}
\DoNotIndex{\csname,\endcsname,\@spaces,\openin,\openout,%
            \closein,\closeout}
\DoNotIndex{\catcode,\endinput}
\DoNotIndex{\jobname,\message,\read,\the,\m@ne,\noexpand}
\DoNotIndex{\hsize,\vsize,\hskip,\vskip,\kern,\hfil,\hfill,\hss}
\DoNotIndex{\m@ne,\z@,\z@skip,\@ne,\tw@,\p@}
\DoNotIndex{\dp,\wd,\ht,\vss,\unskip}

% Set up the Index and Change History to use \part
\IndexPrologue{\part*{Index}%
                 \markboth{Index}{Index}%
                 \addcontentsline{toc}{part}{Index}%
                 The italic numbers denote the pages where the
                 corresponding entry is described,
                 numbers underlined point to the definition,
                 all others indicate the places where it is used.}

\GlossaryPrologue{\part*{Change History}%
%                Allow control names to be hyphenated here...
                 {\GlossaryParms\ttfamily\hyphenchar\font=`\-}%
                 \markboth{Change History}{Change History}%
                 \addcontentsline{toc}{part}{Change History}}

% The standard \changes command modified slightly to better cope with
% this multiple file document.
\makeatletter
\def\changes@#1#2#3{%
  \let\protect\@unexpandable@protect
  \edef\@tempa{\noexpand\glossary{#2\space\currentfile\space#1\levelchar
                                 \ifx\saved@macroname\@empty
                                   \space
                                   \actualchar
                                   \generalname
                                 \else
                                   \expandafter\@gobble
                                   \saved@macroname
                                   \actualchar
                                   \string\verb\quotechar*%
                                   \verbatimchar\saved@macroname
                                   \verbatimchar
                                 \fi
                                 :\levelchar #3}}%
  \@tempa\endgroup\@esphack}
\makeatother

% Produce a Change Log and (2 column) Index.
\RecordChanges
\CodelineIndex
\EnableCrossrefs
\setcounter{IndexColumns}{2}

% Needed for documentation in ltoutenc.dtx
\usepackage{textcomp}

\begin{document}
 \title{The \LaTeXe\ Sources}
 \author{%
  Johannes Braams\\
  David Carlisle\\
  Alan Jeffrey\\
  Leslie Lamport\\
  Frank Mittelbach\\
  Chris Rowley\\
  Rainer Sch\"opf}

% This command will be used to input the patch file
% if that file exists.
\newcommand{\includeltpatch}{%
  \def\currentfile{ltpatch.ltx}
  \part{ltpatch}
  {\let\ttfamily\relax
    \xdef\filekey{\filekey, \thepart={\ttfamily\currentfile}}}%
  Things we did wrong\ldots
  \IndexInput{ltpatch.ltx}}

% Get the date and patch level from ltvers.dtx
\makeatletter
\let\patchdate=\@empty
\begingroup
   \def\ProvidesFile#1\fmtversion#2#3\patch@level#4{%
      \date{#2}\xdef\patchdate{#4}\endinput}
   \input{ltvers.dtx}
\global\let\X@date=\@date

% Add the patch version if available.
   \long\def\Xdef#1#2#3\def#4#5{%
    \xdef\X@date{#2}%
    \xdef\patchdate{#5}%
    \endinput}%
   \InputIfFileExists{ltpatch.ltx}
    {\let\def\Xdef}{\global\let\includeltpatch\relax}
\endgroup

\ifx\@date\X@date
   \def\Xpatch{0}
   \ifx\patchdate\Xpatch\else
     \edef\@date{\@date\space Patch level \patchdate}
   \fi
\else
   \@warning{ltpatch.ltx does not match ltvers.dtx!}
   \let\includeltpatch\relax
\fi
\makeatother

 \pagenumbering{roman}
 \MaintainedByLaTeXTeam{latex}
 \maketitle
 \renewcommand\maketitle{}

 \tableofcontents

 \clearpage

 \pagenumbering{arabic}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Each of the following \DocInclude lines includes a file with extension
% .dtx. Each of these files may be typeset separately. For instance
% latex ltboxes.dtx
% will typeset the source of the LaTeX box commands.
%
% If this file is processed, each of these separate dtx files will be
% contained as a part of a single document. Using ltxdoc.cfg you can
% then optionally produce a combined index and/or change history for
% the entire source of the format file. Note that such a document will
% be quite large (about 555 pages).
%

 \DocInclude{ltdirchk} % System dependent initialisation

 \DocInclude{ltplain}  % LaTeX version of Knuth's plain.tex

 \DocInclude{ltvers}   % Current version date

 \DocInclude{ltdefns}  % Initial definitions.

 \DocInclude{ltalloc}  % Allocation of counters and others.

 \DocInclude{ltcntrl}  % Program control macros.

 \DocInclude{lterror}  % Error handling.

 \DocInclude{ltpar}    % Paragraphs.

 \DocInclude{ltspace}  % Spacing, line and page breaking.

 \DocInclude{ltlogos}  % Logos.

 \DocInclude{ltfiles}  % \input files and related commands

 \DocInclude{ltoutenc} % Output encoding interface

 \DocInclude{ltcounts} % Counters

 \DocInclude{ltlength} % Lengths

 \DocInclude{ltfssbas} % NFSS Base macros

 \DocInclude{ltfsstrc} % NFSS Tracing (and tracefnt.sty)

 \DocInclude{ltfsscmp} % NFSS1 Compatibility

 \DocInclude{ltfssdcl} % NFSS Declarative interface

 \DocInclude{ltfssini} % NFSS Initialisation

 \DocInclude{fontdef}  % fonttext.ltx/fontmath.ltx

 \DocInclude{preload}  % preload.ltx

 \DocInclude{ltfntcmd} % \textrm etc

 \DocInclude{ltpageno} % Page numbering

 \DocInclude{ltxref}   % Cross referencing

 \DocInclude{ltmiscen} % Miscellaneous environment definitions.

 \DocInclude{ltmath}   % Mathematics set up.

 \DocInclude{ltlists}  % List and related environments

 \DocInclude{ltboxes}  % Parbox and friends

 \DocInclude{lttab}    % Tabbing tabular and array

 \DocInclude{ltpictur} % Picture mode

 \DocInclude{ltthm}    % Theorem environments

 \DocInclude{ltsect}   % Sectioning

 \DocInclude{ltfloat}  % Floats

 \DocInclude{ltidxglo} % Index and Glossary

 \DocInclude{ltbibl}   % Bibliography

 \DocInclude{ltpage}   % \pagestyle \raggedbottom \sloppy

 \DocInclude{ltoutput} % Output routine

 \DocInclude{ltclass}  % Package & Class interface

 \DocInclude{lthyphen} % Hyphenation (hyphen.ltx).

 \DocInclude{ltluatex} % Luatex support

 \DocInclude{ltfinal}  % Last minute initialisations and dump

 \includeltpatch       % Corrections distributed after the full release

% Stop here if ltxdoc.cfg says \AtEndOfClass{\OnlyDescription}
\StopEventually{\end{document}}

\clearpage
\pagestyle{headings}

% Make TeX shut up.
\hbadness=10000
\newcount\hbadness
\hfuzz=\maxdimen

\typeout{%
  \string # Produce change log with^^J%
  makeindex -s gglo.ist -o source2e.gls source2e.glo}

\PrintChanges

\clearpage

% makeindex needs a symbol between the parts of composite page numbers
% but we dont want one, so:
\typeout{%
  \string # Produce index with^^J%
  makeindex -s source2e.ist source2e.idx}

\begingroup
\def\endash{--}
\catcode`\-\active
\def-{\futurelet\temp\indexdash}
\def\indexdash{\ifx\temp-\endash\fi}

\PrintIndex
\endgroup

% Make sure that the index is not printed twice
% (ltxdoc.cfg might have a second \PrintIndex command)
\let\PrintChanges\relax
\let\PrintIndex\relax

\end{document}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

To use this file to produce a fully indexed source code
you need to execute the following (or equivalent) commands:

   latex source2e.tex

   makeindex -s source2e.ist source2e.idx
   makeindex -s gglo.ist -o source2e.gls source2e.glo

   latex source2e.tex
   latex source2e.tex

The makeindex style source2e.ist is used in place of the usual
doc gind.ist to ensure that I is used in the sequence I J K
not I II II, which would be the default makeindex behaviour.

The third run with latex is only required to get the table of
contents entries for the change log and index. You may speed things up
by using the \includeonly mechanism so as not to typeset the source
files on the second run. This involves changing the file
ltxdoc.cfg
between the latex runs.

The following unix script automates this.
  (It could easily be ported to scripts for DOS or VMS,
   rm is ReMove a file, and echo "..." > file writes ... to "file".)

After this script (after the second ==============) is a similar script
that will produce the documentation for all the files in the base
distribution that are *not* included in source2e.dvi. This second script
was requested, but before using it, beware it will take a long time!
It may however be modified as required, eg to not typeset the fdd files
or whatever...

==============
#!/bin/sh

rm  -f source2e.gls source2e.ind source2e.toc

# First run:
# Create new standard ltxdoc.cfg file
# Pass the (possibly empty) list of arguments supplied on the
# command line to article class.
#
# If you use A4 paper, running this script with argument
#    a4paper
# may save about 30 pages.
#
echo "\PassOptionsToClass{$*}{article}" > ltxdoc.cfg

# Now LaTeX the file with this cfg file.
#
latex source2e.tex

# Make the Change log and Glossary.
#
makeindex -s source2e.ist source2e.idx
makeindex -s gglo.ist -o source2e.gls source2e.glo

# Second run: append \includeonly{} to ltxdoc.cfg to speed up things
# (this run needed only to get changes and index listed in .toc file)
#
# Note that the index will not be made incorrect by the insertion
# of the table of contents as the front matter uses a different page
# numbering scheme.
#
echo "\includeonly{}" >> ltxdoc.cfg

latex source2e.tex

# Third and final run, to put everything together.
# First restore the cfg file:
#
echo "\PassOptionsToClass{$*}{article}" > ltxdoc.cfg
latex source2e.tex

==============
#!/bin/sh

# Running this script will process all the dtx fdd and *guide.tex
# and ltnews*.tex files in the LaTeX distribution, except the dtx
# files included in source2e.tex.
# (The shell first script in the comments of source2e.tex will
#  process those.)

# Any command line arguments (eg a4paper) are taken as options to the
# article class.

# This script is likely to take ages!

echo "\PassOptionsToClass{$*}{article}"                  > ltxdoc.cfg
echo "\batchmode"                                       >> ltxdoc.cfg

# The next four lines produce full indexes and change logs
# you may not want those.
echo "\AtBeginDocument{\RecordChanges}"                 >> ltxdoc.cfg
echo "\AtEndDocument{\PrintChanges}"                    >> ltxdoc.cfg
echo "\AtBeginDocument{\CodelineIndex\EnableCrossrefs}" >> ltxdoc.cfg
echo "\AtEndDocument{\PrintIndex}"                      >> ltxdoc.cfg

# If you do not want any code listings, just documentation, then instead
# of the above four lines, uncomment the following:
# echo "\AtBeginDocument{\OnlyDescription}"                >> ltxdoc.cfg

echo "\PassOptionsToClass{$*}{article}"                  > ltxguide.cfg
echo "\batchmode"                                       >> ltxguide.cfg

cp ltxguide.cfg ltnews.cfg

for i in *dtx *fdd *guide.tex ltnews*.tex
do
B=`basename $i .dtx`

if (grep "Include{$B}" source2e.tex >/dev/null ; )
then
echo In source2e: $i
else
echo latex $i
  if (latex $i > /dev/null)
  then
    echo latex $i
    latex $i > /dev/null
    echo makeindex -s gind.ist $B.idx
    makeindex -s gind.ist $B.idx > /dev/null 2> /dev/null
    echo makeindex -s gglo.ist -o $B.gls $B.glo
    makeindex -s gglo.ist -o $B.gls $B.glo > /dev/null 2> /dev/null
    echo latex $i
    latex $i > /dev/null
  else
    echo "!!! LaTeX ERROR: $i. (See $B.log.)"
  fi
fi

done


text:
using System.Threading.Tasks;
using Microsoft.AspNet.Identity;

namespace NetMud.Authentication
{
    /// <summary>
    /// Sends a message via sms to a user
    /// </summary>
    public class SmsService : IIdentityMessageService
    {
        /// <summary>
        /// Send a message via sms to a user
        /// </summary>
        /// <param name="message">the message to send</param>
        /// <returns>this function's results</returns>
        public Task SendAsync(IdentityMessage message)
        {
            // Plug in your SMS service here to send a text message.
            return Task.FromResult(0);
        }
    }
}

redpajama_set_name: RedPajamaGithub


text:
var app = require('../lib/snowf');
var appsetting = require('./appsetting');

//初始化应用
app.init(appsetting.snowf);
//添加控制器
app.addController('home', require('./controllers/homeController'));
//启动应用
app.start();


text:
<?php namespace ZN\Authorization;
/**
 * ZN PHP Web Framework
 *
 * "Simplicity is the ultimate sophistication." ~ Da Vinci
 *
 * @package ZN
 * @license MIT [http://opensource.org/licenses/MIT]
 * @author  Ozan UYKUN [ozan@znframework.com]
 */

/**
 * Default Configuration
 *
 * Enabled when the configuration file can not be accessed.
 */
class AuthorizationDefaultConfiguration
{
    /*
    |--------------------------------------------------------------------------
    | Permission
    |--------------------------------------------------------------------------
    |
    | Includes configurations for the Permission library.
    |
    | method : It is used to set which id value will use which method of sending.
    | page   : It is used to set which id value will use which page.
    | process: It is used to set which id value will use which object.
    |
    | Example Usage
    |
    | [
    |     '1' => 'any',
    |     '2' => ['noperm'  => ['delete', 'update']],
    |     '3' => ['perm'    => ['delete', 'update']],
    |     '4' => ['noperm'  => ['delete', 'update', 'add']],
    |     '5' => 'all'
    | ]
    |
    */

    public $method  = [];
    public $page    = [];
    public $process = [];
}


text:
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using System.Linq;
using Newtonsoft.Json;

namespace Chutzpah.Models
{
    public enum TestHarnessReferenceMode
    {
        Normal,
        AMD
    }

    public enum TestHarnessLocationMode
    {
        TestFileAdjacent,
        SettingsFileAdjacent,
        Custom
    }

    public enum RootReferencePathMode
    {
        DriveRoot,
        SettingsFileDirectory
    }

    public enum CodeCoverageExecutionMode
    {
        /// <summary>
        /// Run code coverage when the user asks
        /// </summary>
        Manual,

        /// <summary>
        /// Always run code coverage
        /// </summary>
        Always,

        /// <summary>
        /// Never run code coverage
        /// </summary>
        Never
    }

    /// <summary>
    /// Represents the Chutzpah Test Settings file (chutzpah.json)
    /// Applies to all test files in its directory and below.
    /// </summary>
    public class ChutzpahTestSettingsFile
    {
        public static ChutzpahTestSettingsFile Default = new ChutzpahTestSettingsFile(true);
        private Regex testPatternRegex;

        public ChutzpahTestSettingsFile()
        {
            CodeCoverageIncludes = new List<string>();
            CodeCoverageExcludes = new List<string>();
            CodeCoverageIgnores = new List<string>();
            BrowserArguments = new Dictionary<string, string>();
            References = new List<SettingsFileReference>();
            Tests = new List<SettingsFileTestPath>();
            Transforms = new List<TransformConfig>();
        }

        private ChutzpahTestSettingsFile(bool isDefaultSetings) : this()
        {
            IsDefaultSettings = true;

            CodeCoverageSuccessPercentage = Constants.DefaultCodeCoverageSuccessPercentage;
            TestHarnessReferenceMode = Chutzpah.Models.TestHarnessReferenceMode.Normal;
            TestHarnessLocationMode = Chutzpah.Models.TestHarnessLocationMode.TestFileAdjacent;
            RootReferencePathMode = Chutzpah.Models.RootReferencePathMode.DriveRoot;
            EnableTestFileBatching = false;
            IgnoreResourceLoadingErrors = false;
        }

        public bool IsDefaultSettings { get; set; }

        /// <summary>
        /// Determines if this settings file should inherit and merge with the settings of its
        /// parent settings file.
        /// </summary>
        public bool InheritFromParent { get; set; }

        /// <summary>
        /// Determines if this settings file should inherit and merge with the settings of a given file
        /// </summary>
        public string InheritFromPath { get; set; }

        /// <summary>
        /// Suppress errors that are reported when a script request to load a url (e.g. xhr/script/image) fails
        /// </summary>
        public bool? IgnoreResourceLoadingErrors { get; set; }

        /// <summary>
        /// Determines if Chutzpah should try to batch all test files for this chutzpah.json file in one test harness
        /// </summary>
        public bool? EnableTestFileBatching { get; set; }

        /// <summary>
        /// The time to wait for the tests to complete in milliseconds
        /// </summary>
        public int? TestFileTimeout { get; set; }

        /// <summary>
        /// If not null tells Chutzpah which framework to use instead of detecting automatically
        /// </summary>
        public string Framework { get; set; }

        /// <summary>
        /// Indicates which version of a framework Chutzah should use.
        /// This is only useful when Chutzpah supports more than one version which is usually a temporary situation.
        /// If this is null chutzpah will use its default version of a framework.
        /// If an unknown version is set Chutzpah will use its default version
        /// </summary>
        public string FrameworkVersion { get; set; }

        /// <summary>
        /// The name of the Mocha interface the tests use
        /// This will override the detection mechanism for tests
        /// </summary>
        public string MochaInterface { get; set; }

        /// <summary>
        /// Determines how Chutzpah handles referenced files in the test harness
        /// Normal - Sets the test harness for normal test running
        /// AMD - Sets the test harness to running tests using AMD
        /// </summary>
        public TestHarnessReferenceMode? TestHarnessReferenceMode { get; set; }

        /// <summary>
        /// Tells Chutzpah where it should place the generated test harness html.
        /// TestFileAdjacent - Places the harness next to the file under test. This is the default.
        /// SettingsFileAdjacent - Places the harness next to the first chutzpah.json file found up the directory tree from the file under test
        /// Custom - Lets you specify the TestHarnessDirectory property to give a custom folder to place the test harness. If folder is not found it will revert to the default.
        /// </summary>
        public TestHarnessLocationMode? TestHarnessLocationMode { get; set; }

        /// <summary>
        /// If TestHarnessLocationMode is set to Custom then this will be the path to the folder to place the generated test harness file
        /// </summary>
        public string TestHarnessDirectory { get; set; }

        /// <summary>
        /// A Regualr Expression which tells Chutpah where to find the names of your tests in the test file.
        /// The regex must contain a capture group named TestName like (?<TestName>) that contains the test name (inside of the quotes)
        /// </summary>
        public string TestPattern { get; set; }

        /// <summary>
        /// The path to your own test harness for Chutzpah to use.
        /// This is an *advanced* scenario since Chutzpah has specific requirements on the test harness
        /// If you deploy your own then you must copy from Chutzpah's and if you upgrade Chutzpah
        /// you must keep parity
        /// There are no guarantees about anything working once you deploy your own.
        /// </summary>
        public string CustomTestHarnessPath { get; set; }

        /// <summary>
        /// Set the baseurl for Chutzpah to use when generating the test harness.
        ///  Defaults to the test harness directory if not set.
        /// </summary>
        public string AMDBaseUrl { get; set; }

        /// <summary>
        /// Tells Chutzpah the root path for your AMD paths. This is only needed if your baseUrl is a different location than your source directory.
        /// This is common if you are compiling from another language to JavaScript and copying those compiled files to a different folder.
        /// For example if you have all your .ts files in /src and you compile them to a /out directry then your AMDBaseUrl is /out and AMDAppDirectory is /src
        ///
        /// Defaults to the test harness directory if not set
        /// </summary>
        public string AMDAppDirectory { get; set; }

        /// <summary>
        /// OBSOLETE: This would set the base url and try to map the files relative to the test harness. This is deprecated in favor of AMDBaseUrl.
        /// </summary>
        public string AMDBasePath { get; set; }

        /// <summary>
        /// Determines what a reference path that starts with / or \  (.e.g <reference path="/" />) is relative to
        /// DriveRoot - Make it relative to the root of the drive (e.g. C:\). This is default.
        /// SettingsFileDirectory - Makes root path relative to the directory of the settings file
        /// </summary>
        public RootReferencePathMode? RootReferencePathMode { get; set; }

        /// <summary>
        /// Manual - Run code coverage when user asks
        /// Always - Run code coverage always
        /// Never  - Never run code coverage, ignore if user asks
        /// </summary>
        public CodeCoverageExecutionMode? CodeCoverageExecutionMode { get; set; }

        /// <summary>
        /// The percentage of lines should be covered to show the coverage output as success or failure. By default, this is 60.
        /// </summary>
        public double? CodeCoverageSuccessPercentage { get; set; }

        /// <summary>
        /// The collection code coverage file patterns to include in coverage. These are in glob format. If you specify none all files are included.
        /// </summary>
        public ICollection<string> CodeCoverageIncludes { get; set; }

        /// <summary>
        /// The collection code coverage file patterns to exclude in coverage. These are in glob format. If you specify none no files are excluded.
        /// </summary>
        public ICollection<string> CodeCoverageExcludes { get; set; }

        /// <summary>
        /// The collection code coverage file patterns to ignore in coverage. These are in glob format. If you specify none no files are excluded.
        /// </summary>
        public ICollection<string> CodeCoverageIgnores { get; set; }

        /// <summary>
        /// The dictionary of browser name (keys) to corresponding browser arguments (values), i.e.; { 'chrome': '--allow-file-access-from-files' }.
        /// </summary>
        public IDictionary<string, string> BrowserArguments { get; set; }

        /// <summary>
        /// The collection of test files. These can list individual tests or folders scanned recursively. This setting can work in two ways:
        /// 1. If you run tests normally by specifying folders/files then this settings will filter the sets of those files.
        /// 2. If you run tests by running a specific chutzpah.json file then this settings will select the test files you choose.
        ///
        /// This settings will not get inherited from parent settings file
        /// </summary>
        public ICollection<SettingsFileTestPath> Tests { get; set; }

        /// <summary>
        /// The collection of reference settings. These can list individual reference files or folders scanned recursively.
        /// </summary>
        public ICollection<SettingsFileReference> References { get; set; }

        /// <summary>
        /// The path to the settings file
        /// </summary>
        [JsonIgnore]
        public string SettingsFileDirectory { get; set; }

        /// <summary>
        /// The compile settings which tell Chutzpah how to perform the batch compile
        /// </summary>
        public BatchCompileConfiguration Compile { get; set; }

        /// <summary>
        /// The user agent to tell PhantomJS to use when making web requests
        /// </summary>
        public string UserAgent { get; set; }

        /// <summary>
        /// Maps the names of transforms to run after testing with their corresponding output paths.
        /// </summary>
        public ICollection<TransformConfig> Transforms { get; set; }

        /// <summary>
        /// Max degree of parallelism for Chutzpah. Defaults to number of CPUs + 1
        ///  If you specify more than 1 the test output may be a bit jumbled
        /// </summary>
        public int? Parallelism { get; set; }

        /// <summary>
        /// Logs tracing information to a chutzpah.log file
        /// </summary>
        public bool? EnableTracing { get; set; }

        /// <summary>
        /// The path to write the trace file to. Defaults tp %temp%/chutzpah.log
        /// </summary>
        public string TraceFilePath { get; set; }

        /// <summary>
        /// This is deprecated and only here for back compat for now
        /// If True, forces code coverage to run always
        /// If Null or not not set, allows code coverage to run if invoked using test adapter, command line or context menu options (default)
        /// If False, forces code coverage to never run.
        /// </summary>
        public bool? EnableCodeCoverage
        {
            set
            {
                if (!CodeCoverageExecutionMode.HasValue)
                {
                    if (value.HasValue && value.Value)
                    {
                        CodeCoverageExecutionMode = Chutzpah.Models.CodeCoverageExecutionMode.Always;
                    }
                    else if (value.HasValue && !value.Value)
                    {
                        CodeCoverageExecutionMode = Chutzpah.Models.CodeCoverageExecutionMode.Never;
                    }
                }
            }
        }

        public string SettingsFileName
        {
            get
            {
                return Path.Combine(SettingsFileDirectory, Constants.SettingsFileName);
            }
        }

        public Regex TestPatternRegex
        {
            get
            {
                if (TestPattern == null)
                {
                    return null;
                }

                return testPatternRegex ?? (testPatternRegex = new Regex(TestPattern));
            }
        }

        public override int GetHashCode()
        {
            if (SettingsFileDirectory == null)
            {
                return "".GetHashCode();
            }

            return SettingsFileDirectory.ToLowerInvariant().GetHashCode();
        }

        public override bool Equals(object obj)
        {
            var settings = obj as ChutzpahTestSettingsFile;
            if (settings == null)
            {
                return false;
            }

            if (SettingsFileDirectory == null && settings.SettingsFileDirectory == null)
            {
                return true;
            }

            return SettingsFileDirectory != null && SettingsFileDirectory.Equals(settings.SettingsFileDirectory, StringComparison.OrdinalIgnoreCase);

        }

        public ChutzpahTestSettingsFile InheritFromDefault()
        {
            return this.InheritFrom(Default);
        }

        /// <summary>
        /// Merge a selection of settings from a parent file into the current one.
        /// This merge will work as follows
        /// 1. For basic properties the child's property wins if it is different than the default
        /// 2. For complex objects the child's property wins if it is not null
        /// 3. For lists the childs items get added to the parents
        /// </summary>
        /// <param name="parent"></param>
        public ChutzpahTestSettingsFile InheritFrom(ChutzpahTestSettingsFile parent)
        {
            if (parent == null || this.IsDefaultSettings)
            {
                return this;
            }

            this.References = parent.References.Concat(this.References).ToList();
            this.CodeCoverageIncludes = parent.CodeCoverageIncludes.Concat(this.CodeCoverageIncludes).ToList();
            this.CodeCoverageExcludes = parent.CodeCoverageExcludes.Concat(this.CodeCoverageExcludes).ToList();
            this.CodeCoverageIgnores = parent.CodeCoverageIgnores.Concat(this.CodeCoverageIgnores).ToList();
            this.Transforms = parent.Transforms.Concat(this.Transforms).ToList();

            foreach (var browserArgument in parent.BrowserArguments)
            {
                // We should only override the child if the child does not already define a value for a key
                if (this.BrowserArguments.ContainsKey(browserArgument.Key))
                {
                    continue;
                }

                this.BrowserArguments[browserArgument.Key] = browserArgument.Value;
            }

            if (this.Compile == null)
            {
                this.Compile = parent.Compile;
            }
            else if(this.Compile != null && parent.Compile != null)
            {
                this.Compile.Arguments = this.Compile.Arguments ?? parent.Compile.Arguments;
                this.Compile.Executable = this.Compile.Executable ?? parent.Compile.Executable;
                this.Compile.Extensions = this.Compile.Extensions.Any() ? this.Compile.Extensions : parent.Compile.Extensions;
                this.Compile.ExtensionsWithNoOutput = this.Compile.ExtensionsWithNoOutput.Any() ? this.Compile.ExtensionsWithNoOutput : parent.Compile.ExtensionsWithNoOutput;
                this.Compile.Mode = this.Compile.Mode ?? parent.Compile.Mode;
                this.Compile.IgnoreMissingFiles = this.Compile.IgnoreMissingFiles ?? parent.Compile.IgnoreMissingFiles;
                this.Compile.UseSourceMaps = this.Compile.UseSourceMaps ?? parent.Compile.UseSourceMaps;
                this.Compile.SkipIfUnchanged = this.Compile.SkipIfUnchanged ?? parent.Compile.SkipIfUnchanged;
                this.Compile.WorkingDirectory = this.Compile.WorkingDirectory ?? parent.Compile.WorkingDirectory;
                this.Compile.Timeout = this.Compile.Timeout ?? parent.Compile.Timeout;
                this.Compile.Paths = this.Compile.Paths.Concat(parent.Compile.Paths).ToList();
            }

            this.AMDBaseUrl = this.AMDBaseUrl ?? parent.AMDBaseUrl;
            this.AMDAppDirectory = this.AMDAppDirectory ?? parent.AMDAppDirectory;
            this.CodeCoverageSuccessPercentage = this.CodeCoverageSuccessPercentage ?? parent.CodeCoverageSuccessPercentage;
            this.CustomTestHarnessPath = this.CustomTestHarnessPath ?? parent.CustomTestHarnessPath;
            this.CodeCoverageExecutionMode = this.CodeCoverageExecutionMode ?? parent.CodeCoverageExecutionMode;
            this.Framework = this.Framework ?? parent.Framework;
            this.FrameworkVersion = this.FrameworkVersion ?? parent.FrameworkVersion;
            this.MochaInterface = this.MochaInterface ?? parent.MochaInterface;
            this.RootReferencePathMode = this.RootReferencePathMode ?? parent.RootReferencePathMode;
            this.TestFileTimeout = this.TestFileTimeout ?? parent.TestFileTimeout;
            this.TestHarnessReferenceMode = this.TestHarnessReferenceMode ?? parent.TestHarnessReferenceMode;
            this.TestPattern = this.TestPattern ?? parent.TestPattern;
            this.UserAgent = this.UserAgent ?? parent.UserAgent;
            this.EnableTestFileBatching = this.EnableTestFileBatching ?? parent.EnableTestFileBatching;
            this.IgnoreResourceLoadingErrors = this.IgnoreResourceLoadingErrors ?? parent.IgnoreResourceLoadingErrors;
            this.Parallelism = this.Parallelism ?? parent.Parallelism;
            this.EnableTracing = this.EnableTracing ?? parent.EnableTracing;
            this.TraceFilePath = this.TraceFilePath ?? parent.TraceFilePath;

            // Deprecated
            this.AMDBasePath = this.AMDBasePath ?? parent.AMDBasePath;

            // We need to handle an inherited test harness location mode specially
            // If the parent set their mode to SettingsFileAdjacent and the current file has it set to null
            // Then we make the current file have a Custom mode with the parent files settings directory
            if (this.TestHarnessLocationMode == null)
            {
                if (parent.TestHarnessLocationMode == Chutzpah.Models.TestHarnessLocationMode.SettingsFileAdjacent && !parent.IsDefaultSettings)
                {
                    this.TestHarnessLocationMode = Chutzpah.Models.TestHarnessLocationMode.Custom;
                    this.TestHarnessDirectory = parent.SettingsFileDirectory;
                }
                else
                {
                    this.TestHarnessDirectory = parent.TestHarnessDirectory;
                    this.TestHarnessLocationMode = parent.TestHarnessLocationMode;
                }
            }

            return this;
        }
    }
}


text:
import { Component } from '@angular/core';

import { LoggerService } from './logger.service';

@Component({
  selector: 'after-view-parent',
  template: `

AfterView

  <after-view  *ngIf="show"></after-view>

AfterView Logs

    <button (click)="reset()">Reset</button>

{{msg}}

  `,
  providers: [LoggerService]
})
export class AfterViewParentComponent {
  show = true;

  constructor(public logger: LoggerService) { }

  reset() {
    this.logger.clear();
    // quickly remove and reload AfterViewComponent which recreates it
    this.show = false;
    this.logger.tick_then(() => this.show = true);
  }
}


text:
$def with (error_message,user_id)
$var user_id: $user_id

Add New Feed

$if error_message:

$error_message

<form method="post" action="/feed/new">
  url: <input type="text" name="url" value="" />

  <input type="submit" value="submit" /> or Cancel
</form>


text:
#include "webrtc/modules/audio_processing/vad/pole_zero_filter.h"

#include <stdlib.h>
#include <string.h>
#include <algorithm>

namespace webrtc {

PoleZeroFilter* PoleZeroFilter::Create(const float* numerator_coefficients,
                                       size_t order_numerator,
                                       const float* denominator_coefficients,
                                       size_t order_denominator) {
  if (order_numerator > kMaxFilterOrder ||
      order_denominator > kMaxFilterOrder || denominator_coefficients[0] == 0 ||
      numerator_coefficients == NULL || denominator_coefficients == NULL)
    return NULL;
  return new PoleZeroFilter(numerator_coefficients, order_numerator,
                            denominator_coefficients, order_denominator);
}

PoleZeroFilter::PoleZeroFilter(const float* numerator_coefficients,
                               size_t order_numerator,
                               const float* denominator_coefficients,
                               size_t order_denominator)
    : past_input_(),
      past_output_(),
      numerator_coefficients_(),
      denominator_coefficients_(),
      order_numerator_(order_numerator),
      order_denominator_(order_denominator),
      highest_order_(std::max(order_denominator, order_numerator)) {
  memcpy(numerator_coefficients_, numerator_coefficients,
         sizeof(numerator_coefficients_[0]) * (order_numerator_ + 1));
  memcpy(denominator_coefficients_, denominator_coefficients,
         sizeof(denominator_coefficients_[0]) * (order_denominator_ + 1));

  if (denominator_coefficients_[0] != 1) {
    for (size_t n = 0; n <= order_numerator_; n++)
      numerator_coefficients_[n] /= denominator_coefficients_[0];
    for (size_t n = 0; n <= order_denominator_; n++)
      denominator_coefficients_[n] /= denominator_coefficients_[0];
  }
}

template <typename T>
static float FilterArPast(const T* past, size_t order,
                          const float* coefficients) {
  float sum = 0.0f;
  size_t past_index = order - 1;
  for (size_t k = 1; k <= order; k++, past_index--)
    sum += coefficients[k] * past[past_index];
  return sum;
}

int PoleZeroFilter::Filter(const int16_t* in,
                           size_t num_input_samples,
                           float* output) {
  if (in == NULL || output == NULL)
    return -1;
  // This is the typical case, just a memcpy.
  const size_t k = std::min(num_input_samples, highest_order_);
  size_t n;
  for (n = 0; n < k; n++) {
    output[n] = in[n] * numerator_coefficients_[0];
    output[n] += FilterArPast(&past_input_[n], order_numerator_,
                              numerator_coefficients_);
    output[n] -= FilterArPast(&past_output_[n], order_denominator_,
                              denominator_coefficients_);

    past_input_[n + order_numerator_] = in[n];
    past_output_[n + order_denominator_] = output[n];
  }
  if (highest_order_ < num_input_samples) {
    for (size_t m = 0; n < num_input_samples; n++, m++) {
      output[n] = in[n] * numerator_coefficients_[0];
      output[n] +=
          FilterArPast(&in[m], order_numerator_, numerator_coefficients_);
      output[n] -= FilterArPast(&output[m], order_denominator_,
                                denominator_coefficients_);
    }
    // Record into the past signal.
    memcpy(past_input_, &in[num_input_samples - order_numerator_],
           sizeof(in[0]) * order_numerator_);
    memcpy(past_output_, &output[num_input_samples - order_denominator_],
           sizeof(output[0]) * order_denominator_);
  } else {
    // Odd case that the length of the input is shorter that filter order.
    memmove(past_input_, &past_input_[num_input_samples],
            order_numerator_ * sizeof(past_input_[0]));
    memmove(past_output_, &past_output_[num_input_samples],
            order_denominator_ * sizeof(past_output_[0]));
  }
  return 0;
}

}  // namespace webrtc


text:
var hierarchy =
[
    [ "exception", null, [
      [ "tracery::TraceryException", "classtracery_1_1_tracery_exception.html", null ]
    ] ],
    [ "tracery::Grammar", "classtracery_1_1_grammar.html", null ],
    [ "tracery::Helpers", "classtracery_1_1_helpers.html", null ],
    [ "tracery::Modifier", "structtracery_1_1_modifier.html", null ],
    [ "tracery::Modifiers", "classtracery_1_1_modifiers.html", [
      [ "tracery::BaseEngModifiers", "classtracery_1_1_base_eng_modifiers.html", null ]
    ] ],
    [ "tracery::NodeAction", "classtracery_1_1_node_action.html", null ],
    [ "tracery::NodeSettings", "structtracery_1_1_node_settings.html", null ],
    [ "tracery::RandomNumberGenerator", "classtracery_1_1_random_number_generator.html", null ],
    [ "tracery::RuleSet", "classtracery_1_1_rule_set.html", null ],
    [ "tracery::Symbol", "classtracery_1_1_symbol.html", null ],
    [ "tracery::Tracery", "classtracery_1_1_tracery.html", null ],
    [ "tracery::TraceryNode", "classtracery_1_1_tracery_node.html", null ]
];


text:
var path = require('canonical-path');
var _ = require('lodash');
var staticSite = require('./static-site');

var projectBase = path.resolve(__dirname, '../..');

module.exports = function(config) {

  config = staticSite(config);
  config.set('buildConfig', require('../build.config'));

  //rendering.outputFolder set by gulp task

  config.merge('rendering.nunjucks.config.tags', {
    variableStart: '{$',
    variableEnd: '$}',
    blockStart: '{%',
    blockEnd: '%}'
  });

  config.set('logging.level', 'info');

  config.set('rendering.templateFolders', [
    path.resolve(__dirname, 'templates')
  ]);
  config.set('rendering.templatePatterns', [
    '${ doc.template }',
    '${doc.area}/${ doc.id }.${ doc.docType }.template.html',
    '${doc.area}/${ doc.id }.template.html',
    '${doc.area}/${ doc.docType }.template.html',
    '${ doc.id }.${ doc.docType }.template.html',
    '${ doc.id }.template.html',
    '${ doc.docType }.template.html'
  ]);

  config.append('processing.processors', [
    require('../docs/processors/version-data'),
    require('./processors/demos')
  ]);

  config.set('basePath', __dirname);
  config.set('source.projectPath', '.');

  config.set('source.files', [
    { pattern: 'demos/**/*.html', basePath: projectBase },
    { pattern: 'demos/**/*.js', basePath: projectBase },
    { pattern: 'demos/**/*.css', basePath: projectBase }
  ]);

  return config;
};


text:
package cc.seeed.iot.activity.user;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.umeng.analytics.MobclickAgent;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cc.seeed.iot.App;
import cc.seeed.iot.R;
import cc.seeed.iot.activity.BaseActivity;
import cc.seeed.iot.entity.User;
import cc.seeed.iot.logic.UserLogic;
import cc.seeed.iot.webapi.IotApi;
import cc.seeed.iot.webapi.IotService;
import cc.seeed.iot.webapi.model.UserResponse;
import retrofit.Callback;
import retrofit.RetrofitError;

public class ChangePwdActivity extends BaseActivity {
    private static final String TAG = "ChangePwdActivity";
    public Toolbar mToolbar;
    ProgressDialog dialog;

    @InjectView(R.id.input_old_password)
    EditText _oldPwdText;
    @InjectView(R.id.input_new_password)
    EditText _newPwdText;
    @InjectView(R.id.input_new_password_verify)
    EditText _newPwdVerifyText;
    @InjectView(R.id.btn_save)
    Button _savePwdButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pwd);
        ButterKnife.inject(this);

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setTitle("Change password");

        initView();
    }

    private void initView() {
        if (App.getApp().isDefaultServer()){
            _oldPwdText.setVisibility(View.VISIBLE);
        }else {
            _oldPwdText.setVisibility(View.GONE);
        }
        _savePwdButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                savePwd();
                hideKeyboard(v);
            }
        });
    }

    public void savePwd() {
        Log.d(TAG, "Save password");

        if (!validate()) {
            App.showToastShrot("Password changed failed");
            return;
        }

        _savePwdButton.setEnabled(false);

        dialog = new ProgressDialog(this, R.style.AppTheme_Dark_Dialog);
        dialog.setIndeterminate(true);
        dialog.setMessage("Change password...");
        dialog.show();

        String oldPwd = _oldPwdText.getText().toString().trim();
        String newPwd = _newPwdText.getText().toString().trim();
        UserLogic.getInstance().changePwd(oldPwd, newPwd);
        //checkOldPassword(progressDialog);
    }

    private boolean validate() {
        boolean valid = true;

        String oldPwd = _oldPwdText.getText().toString();
        String new_pwd = _newPwdText.getText().toString();
        String new_verify = _newPwdVerifyText.getText().toString();
        if (!App.getApp().isDefaultServer()){
            oldPwd = "123456";
        }
        if (oldPwd.isEmpty() || oldPwd.length() < 6) {
            _oldPwdText.setError("Password is too short (minimum is 6 characters)");
            valid = false;
        } else if (new_pwd.isEmpty() || new_pwd.length() < 6) {
            _newPwdText.setError("Password is too short (minimum is 6 characters)");
            valid = false;
        } else if (!new_pwd.equals(new_verify)) {
            _newPwdText.setError("Password doesn't match the confirmation");
            valid = false;
        } else {
            _newPwdText.setError(null);
        }

        return valid;
    }

    @Override
    public String[] monitorEvents() {
        return new String[]{Cmd_UserChangePwd};
    }

    @Override
    public void onEvent(String event, boolean ret, String errInfo, Object[] data) {
        if (Cmd_UserChangePwd.equals(event)) {
            if (dialog != null) {
                dialog.dismiss();
            }
            _savePwdButton.setEnabled(true);
            if (ret) {
                App.showToastShrot("Password changed successfully.");
                finish();
            } else {
                App.showToastLong(errInfo);
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }
}


text:
layout: post
title: My first post
date: 2011-12-30 15:52:01.000000000 +00:00
categories:
- Personale
tags:
- Wordpress
status: publish
type: post
published: true
meta: {}
author:
---

Ok, lets start this...

How do you start a blog? Hello? Who are talking to in your first blog post?

I imagine that this can be funny after some time blogging, when you read back your early times blogging.

Ok, so maybe I can talk about me, or may be better of what I'm doing here right now...

Its from several years ago that I'm in the so called blogosphere, but always as a visitor, or at least commenting some posts, thats all.. I always had the idea of writing my own blog, but the problem was the topis to write about.

One of my proposals for the new year was to create my own blog, and try to write something about the things I like, like mobile phones, Linux, power management, embedded boards like pandaboard, or may be something related to my professional life, like interesting queries or configuration of mysql databases, interesting features or tricks with Oracle ( you know, someone prefere to pay...) or maybe about technologies I never heard before.

I know it could be difficult to write about this differents subjects, but if this blog would be like my online image, I'm sorry but it is as it is... :)

Ok, as a first post I think its done. While writing this, I got the subject for my next post, the real first blog post: my recent masther thesis. The subject?

After the ads ;)


text:
package vn.gcall.gcall2;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.amulyakhare.textdrawable.TextDrawable;

import vn.gcall.gcall2.Helpers.SessionManager;
/*
* Show agent profile and a button to change password
* */
public class AgentProfileActivity extends Activity {
    private String agentName,agentEmail,agentPhone;
    private SessionManager manager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agent_profile);

        TextView avaName=(TextView) findViewById(R.id.agent_name);
        TextView profile_agent_name=(TextView)findViewById(R.id.profile_agent_name);
        TextView profile_agent_email=(TextView)findViewById(R.id.profile_agent_email);
        TextView profile_agent_phone=(TextView)findViewById(R.id.profile_agent_phone);
        ImageView imageView=(ImageView) findViewById(R.id.image_textDrawable);
        Button bnt_changePwd=(Button)findViewById(R.id.btn_change_pwd);
        manager=new SessionManager();
        agentEmail=manager.getStringPreferences(getApplicationContext(),"EMAIL");
        agentName=manager.getStringPreferences(getApplicationContext(),"FULLNAME");
        agentPhone=manager.getStringPreferences(getApplicationContext(),"PHONE");

        TextDrawable drawable= TextDrawable.builder().buildRound(agentName.substring(0,1), ContextCompat.getColor(getApplicationContext(),R.color.primary));
        imageView.setImageDrawable(drawable);
        avaName.setText(agentName);
        profile_agent_name.setText(agentName);
        profile_agent_email.setText(agentEmail);
        profile_agent_phone.setText(agentPhone);

        bnt_changePwd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(AgentProfileActivity.this,ChangePasswordActivity.class);
                startActivity(intent);
            }
        });
    }

}


text:
 <title>MiniTemplator Example 1</title>

MiniTemplator Example 1

The quick brown ${animal1} jumps over the lazy ${animal2}.

(The first sentence is a pangram)


text:
ACCEPTED

#### According to
Index Fungorum

#### Published in
Flora, Jena 64(6): 88 (1881)

#### Original name
Lecanora erythroleucodes Nyl.

### Remarks
null


text:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bft.util;

import bftsmart.tom.MessageContext;
import bftsmart.tom.util.TOMUtil;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import com.google.protobuf.Timestamp;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInput;
import java.io.DataInputStream;
import java.io.DataOutput;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;
import java.security.GeneralSecurityException;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.Security;
import java.security.Signature;
import java.security.SignatureException;
import java.security.cert.CertPathBuilder;
import java.security.cert.CertStore;
import java.security.cert.CertificateEncodingException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.CollectionCertStoreParameters;
import java.security.cert.PKIXBuilderParameters;
import java.security.cert.PKIXCertPathBuilderResult;
import java.security.cert.TrustAnchor;
import java.security.cert.X509CertSelector;
import java.security.cert.X509Certificate;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.Date;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.bouncycastle.asn1.ASN1Integer;
import org.bouncycastle.asn1.ASN1OutputStream;
import org.bouncycastle.asn1.DEROctetString;
import org.bouncycastle.asn1.pkcs.PrivateKeyInfo;
import org.bouncycastle.asn1.x500.AttributeTypeAndValue;
import org.bouncycastle.asn1.x500.RDN;
import org.bouncycastle.asn1.x500.X500Name;
import org.bouncycastle.asn1.x500.style.BCStyle;
import org.bouncycastle.asn1.x500.style.IETFUtils;
import org.bouncycastle.cert.jcajce.JcaX509CertificateHolder;
import org.bouncycastle.openssl.PEMKeyPair;
import org.bouncycastle.openssl.PEMParser;
import org.bouncycastle.openssl.jcajce.JcaPEMKeyConverter;
import org.bouncycastle.util.io.pem.PemObject;
import org.bouncycastle.util.io.pem.PemWriter;
import org.hyperledger.fabric.protos.common.Common;
import org.hyperledger.fabric.protos.common.Configtx;
import org.hyperledger.fabric.protos.msp.Identities;
import org.hyperledger.fabric.protos.msp.MspConfig;
import org.hyperledger.fabric.protos.orderer.Configuration;
import org.hyperledger.fabric.sdk.exception.CryptoException;
import org.hyperledger.fabric.sdk.security.CryptoPrimitives;

/**
 *
 * @author joao
 */
public class BFTCommon {

    private static Logger logger;
    private static CryptoPrimitives crypto;

    public final static String DEFAULT_CONFIG_DIR = "./config/";

    public static class BFTException extends Exception {

        public BFTException(String msg) {

            super(msg);
        }

    }

    public class BFTTuple {

        public Common.Block block = null;
        public MessageContext msgContext = null;
        public int sequence = -1;
        public String channelID = null;
        public boolean config = false;

        public MSPManager clonedManager = null;

        private BFTTuple(Common.Block block, MessageContext msgCtx, int sequence, String channelID, boolean config, MSPManager clonedManager) {

            this.block = block;
            this.msgContext = msgCtx;
            this.sequence = sequence;
            this.channelID = channelID;
            this.config = config;

            this.clonedManager = clonedManager;
        }
    }

    public class RequestTuple {

        public int id = -1;
        public String type = null;
        public String channelID = null;
        public byte[] payload = null;
        public byte[] signature = null;

        private RequestTuple(int id, String type, String channelID, byte[] payload, byte[] signature) {

            this.id = id;
            this.type = type;
            this.channelID = channelID;
            this.payload = payload;
            this.signature = signature;
        }

    }

    public class ReplyTuple {

        public Common.Block block;
        public Common.Metadata[] metadata;
        public String channel;
        public boolean config;

        private ReplyTuple (Common.Block block, Common.Metadata[] metadata, String channel, boolean config) {
            this.block = block;
            this.metadata = metadata;
            this.channel = channel;
            this.config = config;
        }
    }

    public static RequestTuple getRequestTuple(int id, String type, String channelID, byte[] payload, byte[] signature){

        BFTCommon t = new BFTCommon();
        RequestTuple r = t.new RequestTuple(id, type, channelID, payload, signature);
        return r;
    }

    public static BFTTuple getBFTTuple(Common.Block block, MessageContext msgCtx, int sequence, String channelID, boolean config, MSPManager clonedManager) {

        BFTCommon t = new BFTCommon();
        BFTTuple r = t.new BFTTuple(block, msgCtx, sequence, channelID, config, clonedManager);
        return r;
    }

    public static ReplyTuple getReplyTuple(Common.Block block, Common.Metadata[] metadata, String channel, boolean config) {

        BFTCommon t = new BFTCommon();
        ReplyTuple r = t.new ReplyTuple(block, metadata, channel, config);
        return r;
    }

    public static void init(CryptoPrimitives crypt) throws NoSuchAlgorithmException, NoSuchProviderException{

        crypto = crypt;
        logger = LoggerFactory.getLogger(Common.class);
    }

    public static String getBFTSMaRtConfigDir(String envVar) {

        String configDir = BFTCommon.DEFAULT_CONFIG_DIR;

        String envDir = System.getenv(envVar);

        if (envDir != null) {

            File path = new File(envDir);
            if (path.exists() && path.isDirectory()) configDir = envDir;
        }

        if (!configDir.endsWith("/")) configDir = configDir + "/";

        return configDir;

    }

    public static Duration parseDuration(String s) {

        return Duration.parse("PT"+s.trim().replaceAll("\\s+",""));
    }

    public static BFTCommon.RequestTuple deserializeRequest(byte[] request) throws IOException {

        ByteArrayInputStream bis = new ByteArrayInputStream(request);
        DataInput in = new DataInputStream(bis);

        int id = in.readInt();
        String type = in.readUTF();
        String channelID = in.readUTF();
        int l = in.readInt();
        byte[] payload = new byte[l];
        in.readFully(payload);

        bis.close();

        return BFTCommon.getRequestTuple(id, type, channelID, payload, null);

    }

    public static BFTCommon.RequestTuple deserializeSignedRequest(byte[] request) throws IOException {

        ByteArrayInputStream bis = new ByteArrayInputStream(request);
        DataInput in = new DataInputStream(bis);

        int l = in.readInt();
        byte[] msg = new byte[l];
        in.readFully(msg);
        l = in.readInt();
        byte[] sig = new byte[l];
        in.readFully(sig);

        bis.close();

        bis = new ByteArrayInputStream(msg);
        in = new DataInputStream(bis);

        int id = in.readInt();
        String type = in.readUTF();
        String channelID = in.readUTF();
        l = in.readInt();
        byte[] payload = new byte[l];
        in.readFully(payload);

        bis.close();

        return BFTCommon.getRequestTuple(id, type, channelID, payload, sig);

    }

    public static byte[] serializeRequest(int id, String type, String channelID, byte[] payload) throws IOException {

        ByteArrayOutputStream bos = new ByteArrayOutputStream(type.length() + channelID.length() + payload.length);
        DataOutput out = new DataOutputStream(bos);

        out.writeInt(id);
        out.writeUTF(type);
        out.writeUTF(channelID);
        out.writeInt(payload.length);
        out.write(payload);

        bos.flush();

        bos.close();

        return bos.toByteArray();
    }

    public static byte[] assembleSignedRequest(PrivateKey key, int id, String type, String channelID, byte[] payload) throws IOException {

        ByteArrayOutputStream bos = new ByteArrayOutputStream(type.length() + channelID.length() + payload.length);
        DataOutput out = new DataOutputStream(bos);

        out.writeInt(id);
        out.writeUTF(type);
        out.writeUTF(channelID);
        out.writeInt(payload.length);
        out.write(payload);

        bos.flush();
        bos.close();

        byte[] msg = bos.toByteArray();

        byte[] sig = TOMUtil.signMessage(key, msg);

        bos = new ByteArrayOutputStream(msg.length+sig.length);
        out = new DataOutputStream(bos);

        out.writeInt(msg.length);
        out.write(msg);
        out.writeInt(sig.length);
        out.write(sig);

        bos.flush();
        bos.close();

        return bos.toByteArray();
    }

    public static Common.Block createNextBlock(long number, byte[] previousHash, byte[][] envs) throws NoSuchAlgorithmException, NoSuchProviderException, BFTException {

        if (crypto == null) throw new BFTException("No CryptoPrimitive object suplied");

        //initialize
        Common.BlockHeader.Builder blockHeaderBuilder = Common.BlockHeader.newBuilder();
        Common.BlockData.Builder blockDataBuilder = Common.BlockData.newBuilder();
        Common.BlockMetadata.Builder blockMetadataBuilder = Common.BlockMetadata.newBuilder();
        Common.Block.Builder blockBuilder = Common.Block.newBuilder();

        //create header
        blockHeaderBuilder.setNumber(number);
        if (previousHash != null) blockHeaderBuilder.setPreviousHash(ByteString.copyFrom(previousHash));
        blockHeaderBuilder.setDataHash(ByteString.copyFrom(crypto.hash(BFTCommon.concatenate(envs))));

        //create metadata
        int numIndexes = Common.BlockMetadataIndex.values().length;
        for (int i = 0; i < numIndexes; i++) {
            blockMetadataBuilder.addMetadata(ByteString.EMPTY);
        }

        //create data
        for (int i = 0; i < envs.length; i++) {
            blockDataBuilder.addData(ByteString.copyFrom(envs[i]));
        }

        //crete block
        blockBuilder.setHeader(blockHeaderBuilder.build());
        blockBuilder.setMetadata(blockMetadataBuilder.build());
        blockBuilder.setData(blockDataBuilder.build());

        return blockBuilder.build();
    }

    public static Common.SignatureHeader createSignatureHeader(byte[] creator, byte[] nonce) {

        Common.SignatureHeader.Builder signatureHeaderBuilder = Common.SignatureHeader.newBuilder();

        signatureHeaderBuilder.setCreator(ByteString.copyFrom(creator));
        signatureHeaderBuilder.setNonce(ByteString.copyFrom(nonce));

        return signatureHeaderBuilder.build();
    }

    public static Common.Metadata createMetadataSignature(PrivateKey privKey, byte[] creator, byte[] nonce, byte[] plaintext, Common.BlockHeader blockHeader) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, SignatureException, IOException, CryptoException, BFTException {

        if (crypto == null) throw new BFTException("No CryptoPrimitive object suplied");

        Common.Metadata.Builder metadataBuilder = Common.Metadata.newBuilder();
        Common.MetadataSignature.Builder metadataSignatureBuilder = Common.MetadataSignature.newBuilder();

        Common.SignatureHeader sigHeader = createSignatureHeader(creator, nonce);

        metadataSignatureBuilder.setSignatureHeader(sigHeader.toByteString());

        byte[][] concat = {plaintext, sigHeader.toByteString().toByteArray(), encodeBlockHeaderASN1(blockHeader)};

        //byte[] sig = sign(concatenate(concat));
        byte[] sig = crypto.sign(privKey, BFTCommon.concatenate(concat));

        logger.debug("Signature for block #" + blockHeader.getNumber() + ": " + Arrays.toString(sig) + "\n");

        //parseSig(sig);
        metadataSignatureBuilder.setSignature(ByteString.copyFrom(sig));

        metadataBuilder.setValue((plaintext != null ? ByteString.copyFrom(plaintext) : ByteString.EMPTY));
        metadataBuilder.addSignatures(metadataSignatureBuilder);

        return metadataBuilder.build();
    }

    public static byte[] encodeBlockHeaderASN1(Common.BlockHeader header) throws IOException {

        //convert long to byte array
        //ByteArrayOutputStream bos = new ByteArrayOutputStream();
        //ObjectOutput out = new ObjectOutputStream(bos);
        //out.writeLong(header.getNumber());
        //out.flush();
        //bos.flush();
        //out.close();
        //bos.close();
        //byte[] number = bos.toByteArray();
        // encode the header in ASN1 format
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ASN1OutputStream asnos = new ASN1OutputStream(bos);

        asnos.writeObject(new ASN1Integer((int) header.getNumber()));
        //asnos.writeObject(new DERInteger((int) header.getNumber()));
        asnos.writeObject(new DEROctetString(header.getPreviousHash().toByteArray()));
        asnos.writeObject(new DEROctetString(header.getDataHash().toByteArray()));
        asnos.flush();
        bos.flush();
        asnos.close();
        bos.close();

        byte[] buffer = bos.toByteArray();

        //Add golang idiosyncrasies
        byte[] bytes = new byte[buffer.length + 2];
        bytes[0] = 48; // no idea what this means, but golang's encoding uses it
        bytes[1] = (byte) buffer.length; // length of the rest of the octet string, also used by golang
        for (int i = 0; i < buffer.length; i++) { // concatenate
            bytes[i + 2] = buffer[i];
        }

        return bytes;
    }

    public static String extractMSPID(byte[] bytes) {
        try {
            Common.Envelope env = Common.Envelope.parseFrom(bytes);
            Common.Payload payload = Common.Payload.parseFrom(env.getPayload());
            Common.SignatureHeader sigHeader = Common.SignatureHeader.parseFrom(payload.getHeader().getSignatureHeader());
            Identities.SerializedIdentity ident = Identities.SerializedIdentity.parseFrom(sigHeader.getCreator());
            return ident.getMspid();

        } catch (InvalidProtocolBufferException ex) {
            //ex.printStackTrace();
            return null;
        }

    }

    public static Set<MspConfig.FabricMSPConfig> extractFabricMSPConfigs(Set<String> orgs, Map<String, Configtx.ConfigGroup> groups) throws InvalidProtocolBufferException {

        Set<MspConfig.FabricMSPConfig> msps = new HashSet<>();

        for (String org : orgs) {

            Configtx.ConfigGroup group  = groups.get(org);
            //Configtx.ConfigGroup group  = groups.get(OrdererGroupKey).getGroupsMap().get(org);
            Configtx.ConfigValue value = group.getValuesMap().get("MSP");
            MspConfig.MSPConfig mspConf = MspConfig.MSPConfig.parseFrom(value.getValue());
            MspConfig.FabricMSPConfig fabMspConf = MspConfig.FabricMSPConfig.parseFrom(mspConf.getConfig());

            msps.add(fabMspConf);
        }

        return msps;
    }

    public static long extractTimestamp(Common.Payload payload) throws InvalidProtocolBufferException {

        Common.ChannelHeader header = Common.ChannelHeader.parseFrom(payload.getHeader().getChannelHeader());

        return Instant.ofEpochSecond(header.getTimestamp().getSeconds()).plusNanos(header.getTimestamp().getNanos()).toEpochMilli();

    }

    public static Configuration.BatchSize extractBachSize(Configtx.Config conf) throws InvalidProtocolBufferException {

        Map<String,Configtx.ConfigGroup> groups = conf.getChannelGroup().getGroupsMap();

        return Configuration.BatchSize.parseFrom(groups.get("Orderer").getValuesMap().get("BatchSize").getValue());

    }

    public static Configtx.ConfigEnvelope extractConfigEnvelope(Common.Block block) throws InvalidProtocolBufferException {

        Common.Envelope env = Common.Envelope.parseFrom(block.getData().getData(0));
        Common.Payload payload = Common.Payload.parseFrom(env.getPayload());
        Common.ChannelHeader chanHeader = Common.ChannelHeader.parseFrom(payload.getHeader().getChannelHeader());

        if (chanHeader.getType() == Common.HeaderType.CONFIG_VALUE) {

            return Configtx.ConfigEnvelope.parseFrom(payload.getData());

        }
        else return null;

    }

    public static Configtx.ConfigEnvelope makeConfigEnvelope (Configtx.Config config, Common.Envelope lastUpdate) {

        Configtx.ConfigEnvelope.Builder newConfEnvBuilder = Configtx.ConfigEnvelope.newBuilder();
        newConfEnvBuilder.setConfig(config);
        newConfEnvBuilder.setLastUpdate(lastUpdate);
        return newConfEnvBuilder.build();
    }

    public static Common.Envelope makeUnsignedEnvelope(ByteString dataMsg, ByteString sigHeader, Common.HeaderType headerType, int version, String chainID, long epoch, long timestamp) throws CryptoException {

        //if (crypto == null) throw new BFTException("No CryptoPrimitive object suplied");

        Common.ChannelHeader chanHeader = makeChannelHeader(headerType, version, chainID, epoch, timestamp);
        //Common.SignatureHeader sigHeader = createSignatureHeader(creator, nonce);

        Common.Header.Builder header = Common.Header.newBuilder();
        header.setChannelHeader(chanHeader.toByteString());
        //header.setSignatureHeader(sigHeader.toByteString());
        header.setSignatureHeader(sigHeader);

        Common.Payload.Builder payload = Common.Payload.newBuilder();
        payload.setData(dataMsg);
        payload.setHeader(header);

        byte[] bytes = payload.build().toByteArray();

        //byte[] sig = crypto.sign(privKey, bytes);

        Common.Envelope.Builder env = Common.Envelope.newBuilder();
        env.setPayload(ByteString.copyFrom(bytes));
        //env.setSignature(ByteString.copyFrom(sig));
        env.setSignature(ByteString.EMPTY);

        return env.build();
    }

    public static Common.ChannelHeader makeChannelHeader(Common.HeaderType headerType, int version, String chainID, long epoch, long timestamp) {

        Timestamp.Builder ts = Timestamp.newBuilder();
        ts.setSeconds(timestamp / 1000);
        ts.setNanos(0);

        Common.ChannelHeader.Builder result = Common.ChannelHeader.newBuilder();
        result.setType(headerType.getNumber());
        result.setVersion(version);
        result.setEpoch(epoch);
        result.setChannelId(chainID);
        result.setTimestamp(ts);
        result.setTlsCertHash(ByteString.EMPTY);

        return result.build();
    }

    public static boolean verifyFrontendSignature(int id, PublicKey key, RequestTuple tuple) {

        try {

            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            DataOutput out = new DataOutputStream(bos);

            out.writeInt(tuple.id);
            out.writeUTF(tuple.type);
            out.writeUTF(tuple.channelID);
            out.writeInt(tuple.payload.length);
            out.write(tuple.payload);

            bos.flush();
            bos.close();

            return TOMUtil.verifySignature(key, bos.toByteArray(), tuple.signature);

        } catch (IOException ex) {
            logger.error("Failed to verify frontend signature", ex);
            return false;
        }
    }

    public static Set<X509Certificate> selectSelfSigned(Set<X509Certificate> roots ) {

        // Try to verify root certificate signature with its own public key
        Set<X509Certificate> selfSigned = new HashSet<>();

        for (X509Certificate root : roots) {

            PublicKey key = root.getPublicKey();

            try {
                root.verify(key);
                selfSigned.add(root);
            } catch (CertificateException | NoSuchAlgorithmException | InvalidKeyException | NoSuchProviderException | SignatureException ex) {
                logger.error("Failed to select self-signed certificates", ex);
            }

        }

        return selfSigned;
    }

    public static Set<X509Certificate> extractCertificates(List<ByteString> list) throws CertificateException, IOException {

        Set<X509Certificate> certs = new HashSet<>();
        CertificateFactory certFactory = CertificateFactory.getInstance("X.509");

        for (ByteString bt : list) {

            InputStream in = new ByteArrayInputStream(bt.toByteArray());
            certs.add((X509Certificate)certFactory.generateCertificate(in));
            in.close();

        }

        return certs;

    }

    public static Map<String,Set<String>> cloneChanMSPs(Map<String,Set<String>> original) {

        Map<String,Set<String>> cloneMap = new TreeMap<>();
        Set<String> keys = original.keySet();

        keys.forEach((key) -> {

            Set<String> cloneSet = new HashSet<>();
            Set<String> originalSet = original.get(key);

            originalSet.forEach((msp) -> {

                cloneSet.add(msp);

            });

            cloneMap.put(key, cloneSet);

        });

        return cloneMap;

    }

    public static byte[] concatenate(byte[][] bytes) {

        int totalLength = 0;
        for (byte[] b : bytes) {
            if (b != null) {
                totalLength += b.length;
            }
        }

        byte[] concat = new byte[totalLength];
        int last = 0;

        for (int i = 0; i < bytes.length; i++) {
            if (bytes[i] != null) {
                for (int j = 0; j < bytes[i].length; j++) {
                    concat[last + j] = bytes[i][j];
                }

                last += bytes[i].length;
            }

        }

        return concat;
    }

    public static PrivateKey getPemPrivateKey(String filename) throws IOException {

        BufferedReader br = new BufferedReader(new FileReader(filename));

        PEMParser pp = new PEMParser(br);
        Object obj = pp.readObject();

        pp.close();
        br.close();

        if (obj instanceof PrivateKeyInfo) {

            PrivateKeyInfo keyInfo = (PrivateKeyInfo) obj;
            return (new JcaPEMKeyConverter().getPrivateKey(keyInfo));

        } else {

            PEMKeyPair pemKeyPair = (PEMKeyPair) obj;

            KeyPair kp = new JcaPEMKeyConverter().getKeyPair(pemKeyPair);
            return kp.getPrivate();

        }

    }

    //public static X509CertificateHolder getCertificate(String filename) throws IOException {
    public static X509Certificate getCertificate(String filename) throws IOException, CertificateException {

        /*BufferedReader br = new BufferedReader(new FileReader(filename));
        PEMParser pp = new PEMParser(br);
        X509CertificateHolder ret = (X509CertificateHolder) pp.readObject();

        br.close();
        pp.close();*/

        File file = new File(filename);
        InputStream is = new FileInputStream(file);
        return getCertificate(IOUtils.toByteArray(is));

    }

    public static X509Certificate getCertificate(byte[] serializedCert) throws IOException, CertificateException {

        CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
        InputStream in = new ByteArrayInputStream(serializedCert);

        X509Certificate ret = (X509Certificate)certFactory.generateCertificate(in);
        in.close();

        return ret;
    }

    public static byte[] getSerializedCertificate(X509Certificate certificate) throws IOException, CertificateEncodingException {

        PemObject pemObj = (new PemObject("", certificate.getEncoded()));

        StringWriter strWriter = new StringWriter();
        PemWriter writer = new PemWriter(strWriter);
        writer.writeObject(pemObj);

        writer.close();
        strWriter.close();

        return strWriter.toString().getBytes();

        /*Base64 base64 = new Base64();
        StringWriter strWriter = new StringWriter();

        strWriter.write(X509Factory.BEGIN_CERT);
        strWriter.write(base64.encodeAsString(certificate.getEncoded()));
        strWriter.write(X509Factory.END_CERT);

        return strWriter.toString().getBytes();*/
    }

    public static Identities.SerializedIdentity getSerializedIdentity(String Mspid, byte[] serializedCert) {

        Identities.SerializedIdentity.Builder ident = Identities.SerializedIdentity.newBuilder();
        ident.setMspid(Mspid);
        ident.setIdBytes(ByteString.copyFrom(serializedCert));
        return ident.build();
    }

    public static byte[] serializeContents(byte[][] contents) throws IOException {

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        DataOutputStream out = new DataOutputStream(bos);

        out.writeInt(contents.length);

        out.flush();
        bos.flush();
        for (int i = 0; i < contents.length; i++) {

            out.writeInt(contents[i].length);

            out.write(contents[i]);

            out.flush();
            bos.flush();
        }

        out.close();
        bos.close();
        return bos.toByteArray();

    }

    public static byte[][] deserializeContents(byte[] bytes) throws IOException {

        byte[][] batch = null;

        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        DataInputStream in = new DataInputStream(bis);
        int nContents =  in.readInt();
        batch = new byte[nContents][];

        for (int i = 0; i < nContents; i++) {

            int length = in.readInt();

            batch[i] = new byte[length];
            in.read(batch[i]);
        }
        in.close();
        bis.close();

        return batch;
    }

    public static PKIXCertPathBuilderResult verifyCertificate(X509Certificate cert, Set<X509Certificate> trustedRootCerts,
        Set<X509Certificate> intermediateCerts, Date date) throws GeneralSecurityException {

        // Create the selector that specifies the starting certificate
        X509CertSelector selector = new X509CertSelector();
        selector.setCertificate(cert);

        // Create the trust anchors (set of root CA certificates)
        Set<TrustAnchor> trustAnchors = new HashSet<TrustAnchor>();
        for (X509Certificate trustedRootCert : trustedRootCerts) {
            trustAnchors.add(new TrustAnchor(trustedRootCert, null));
        }

        // Configure the PKIX certificate builder algorithm parameters
        PKIXBuilderParameters pkixParams =
            new PKIXBuilderParameters(trustAnchors, selector);

        // Disable CRL checks (this is done manually as additional step)
        pkixParams.setRevocationEnabled(false);
        pkixParams.setSigProvider("BC");
        pkixParams.setDate(date); // necessary for determinism

        // Specify a list of intermediate certificates
        CertStore intermediateCertStore = CertStore.getInstance("Collection",
            new CollectionCertStoreParameters(intermediateCerts),"BC");
        pkixParams.addCertStore(intermediateCertStore);

        // Build and verify the certification chain
        CertPathBuilder builder = CertPathBuilder.getInstance("PKIX","BC");
        PKIXCertPathBuilderResult result =
            (PKIXCertPathBuilderResult) builder.build(pkixParams);

        return result;
    }

    public static byte[] getCertificationChainIdentifierFromChain(String hashFunction, List<X509Certificate> path) throws BFTException {

        try{

            MessageDigest digestEngine = MessageDigest.getInstance(hashFunction, "BC");

            for (X509Certificate cert : path) {

                digestEngine.update(cert.getEncoded());
            }

            return digestEngine.digest();

        } catch (Exception ex) {

            throw new BFTCommon.BFTException("Failed to compute certificate chain ID for certificate chain: " + ex.getMessage());

        }
    }

    public static String[] extractOUsFronCertificate(X509Certificate cert) throws BFTException {

        LinkedList<String> OUs = new LinkedList<>();

        try {
            X500Name x500name = new JcaX509CertificateHolder(cert).getSubject();

            for (RDN rdn : x500name.getRDNs(BCStyle.OU)) {

                for (AttributeTypeAndValue atv : rdn.getTypesAndValues()) {

                    OUs.add(IETFUtils.valueToString(atv.getValue()));
                }
            }

        } catch (Exception ex) {

            throw new BFTException("Error fetching OUs from certificate: " + ex.getMessage());
        }

        String[] result = new String[OUs.size()];
        OUs.toArray(result);

        return result;

    }

    public static boolean verifySignature(X509Certificate cert, byte[] payload, byte[] signature) throws NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException, SignatureException, BFTCommon.BFTException {

        Signature sigEngine = Signature.getInstance(cert.getSigAlgName(), "BC");
        sigEngine.initVerify(cert.getPublicKey());
        sigEngine.update(payload);

        return sigEngine.verify(signature);
    }

    public static byte[] hash(byte[] bytes, String algorithm) throws NoSuchAlgorithmException, NoSuchProviderException {

        MessageDigest digestEngine = MessageDigest.getInstance(algorithm,"BC");

        return digestEngine.digest(bytes);
    }

    public static Date toDate(long timestamp){

        return Date.from(Instant.ofEpochMilli(timestamp));
    }

    /*private void parseSig(byte[] sig) throws IOException {

        ASN1InputStream input = new ASN1InputStream(sig);

        ASN1Primitive p;
        while ((p = input.readObject()) != null) {
            ASN1Sequence asn1 = ASN1Sequence.getInstance(p);

            ASN1Integer r = ASN1Integer.getInstance(asn1.getObjectAt(0));
            ASN1Integer s = ASN1Integer.getInstance(asn1.getObjectAt(1));

            logger.info("r (int): " + r.getValue().toString());
            logger.info("s (int): " + s.getValue().toString());

            logger.info("r (bytes): " + Arrays.toString(r.getValue().toByteArray()));
            logger.info("s (bytes): " + Arrays.toString(s.getValue().toByteArray()));
        }

    }

    private byte[] sign(byte[] text) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, NoSuchProviderException, CryptoException {

        Signature signEngine = Signature.getInstance("SHA256withECDSA", BouncyCastleProvider.PROVIDER_NAME);
        signEngine.initSign(privKey);
        signEngine.update(text);
        return signEngine.sign();

    }

    private boolean verify(byte[] text, byte[] signature) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, NoSuchProviderException, PEMException {

        PublicKey k = new JcaPEMKeyConverter().getPublicKey(certificate.getSubjectPublicKeyInfo());
        Signature signEngine = Signature.getInstance("ECDSA", BouncyCastleProvider.PROVIDER_NAME);
        signEngine.initVerify(k);
        signEngine.update(text);
        return signEngine.verify(signature);
    }*/

}


text:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>Statechart Specs</title>

  <link rel="shortcut icon" type="image/png" href="jasmine/jasmine_favicon.png">

  <link rel="stylesheet" type="text/css" href="jasmine/jasmine.css">
  <script type="text/javascript" src="jasmine/jasmine.js"></script>
  <script type="text/javascript" src="jasmine/jasmine-html.js"></script>

  <!-- include source files here... -->
  <script type="text/javascript" src="../statechart.js"></script>

  <!-- include spec files here... -->
  <script type="text/javascript" src="./statechart_spec.js"></script>

  <script type="text/javascript">
    (function() {
      var jasmineEnv = jasmine.getEnv();
      jasmineEnv.updateInterval = 1000;

      var trivialReporter = new jasmine.TrivialReporter();

      jasmineEnv.addReporter(trivialReporter);

      jasmineEnv.specFilter = function(spec) {
        return trivialReporter.specFilter(spec);
      };

      var currentWindowOnload = window.onload;

      window.onload = function() {
        if (currentWindowOnload) {
          currentWindowOnload();
        }
        execJasmine();
      };

      function execJasmine() {
        jasmineEnv.execute();
      }

    })();
  </script>

</head>

<body>
</body>
</html>


text:
using System;
using System.Collections.Generic;

namespace School
{
    class School
    {
    }
}


text:
import csv
import os
import sys
import time
import collections
import threading
import logging

from ztag.errors import IgnoreObject

class Updater(object):
    """
    Updater encapsulates the behavior for the updates.csv file; put_update() is called with each
    update, but output is only written every :frequency: seconds.
    """
    def __init__(self, output=None, frequency=1.0, logger=None):
        self.output = output
        self.frequency = frequency
        self.logger = logger
        self.prev = None
        self._wrote_labels = False

    def put_update(self, row):
        if not self.output:
            return

        if self.prev and (row.time - self.prev.time) < self.frequency:
            return

        self.prev = row

        if not self._wrote_labels:
            self.output.write(row.get_csv_labels() + "\n")
            self._wrote_labels = True

        self.output.write(row.get_csv() + "\n")
        self.output.flush()

    def close(self):
        if self.output and self.output != sys.stderr:
            try:
                self.output.close()
            except BaseException as e:
                if self.logger:
                    self.logger.warn("Failed to close updates CSV stream: %s", str(e))

class UpdateRow(object):
    """
    UpdateRow encapsulates the information for a single update and the logic for outputting it as
    a CSV row.
    """
    ORDER = ("skipped", "handled", "delta_skipped", "delta_handled")

    def __init__(self, skipped, handled, updated_at=None, prev=None):
        """
        Construct a new row with the given number of skipped / handled entries, and calculate the
        deltas from prev (or set them to 0). Also sets time to now.
        :param skipped: current total number of skipped records
        :param handled: current total number of handled records
        :param prev: the previous UpdateRow
        """
        self.time = updated_at or time.time()
        self.skipped = skipped
        self.handled = handled
        if prev:
            self.delta_skipped = skipped - prev.skipped
            self.delta_handled = handled - prev.handled
        else:
            self.delta_skipped = 0
            self.delta_handled = 0

    @classmethod
    def get_csv_labels(cls):
        return ",".join(cls.ORDER)

    def get_csv(self):
        return ",".join(str(getattr(self, label)) for label in self.ORDER)

class Stream(object):

    def __init__(self, incoming, outgoing, transforms=None, logger=None, updates=None):
        super(Stream, self).__init__()
        self.incoming = incoming
        self.outgoing = outgoing
        self.transforms = transforms or list()
        self.logger = logger
        if updates:
            self.updater = Updater(output=updates, frequency=1.0, logger=logger)
        else:
            self.updater = None

    def put_update(self, skipped, handled):
        if not self.updater:
            return
        this_update = UpdateRow(skipped=skipped, handled=handled, prev=self.updater.prev)
        self.updater.put_update(this_update)

    def run(self):
        skipped = 0
        handled = 0
        for obj in self.incoming:
            self.put_update(handled=handled, skipped=skipped)
            try:
                out = obj
                for transformer in self.transforms:
                    out = transformer.transform(out)
                    if out is None:
                        raise IgnoreObject()
                self.outgoing.take(out)
                handled += 1
            except IgnoreObject as e:
                if self.logger:
                    self.logger.debug(e.original_exception)
                    self.logger.trace(obj)
                    if e.trback:
                        self.logger.warn(e.trback)
                skipped += 1
                continue
        self.outgoing.cleanup()
        if self.updater:
            self.updater.close()
        return (handled, skipped)

class Incoming(object):
    pass

class InputFile(Incoming):

    def __init__(self, input_file=sys.stdin):
        self.input_file = input_file

    def __iter__(self):
        for line in self.input_file:
            yield line

class InputCSV(Incoming):

    def __init__(self, input_file=sys.stdin):
        self.input_file = input_file
        self.csvdict = csv.DictReader(self.input_file)

    def __iter__(self):
        for record in self.csvdict:
            yield record

class Outgoing(object):

    def __init__(self, *args, **kwargs):
        pass

    def take(self, obj):
        raise NotImplementedError

    def cleanup(self):
        pass

class PythonPrint(Outgoing):

    def __init__(self, *args, **kwargs):
        super(PythonPrint, self).__init__()

    def take(self, obj):
        print obj

class OutputFile(Outgoing):

    def __init__(self, output_file=sys.stdout, *args, **kwargs):
        super(OutputFile, self).__init__()
        self.output_file = output_file

    def take(self, obj):
        self.output_file.write(obj)
        self.output_file.write("\n")

class RedisQueue(Outgoing):

    CERTIFICATES_QUEUE = "certificate"
    PUBKEY_QUEUE = "pubkey"
    # we might as well try to do a whole bunch. The _worst_ case scenario
    # by setting a limit too high is that the server runs out of memory
    # and kills python and the task fails. Which would have happened
    # anyway, because we couldn't connect to redis.
    MAX_RETRIES = 60
    BATCH_SIZE = 250

    def __init__(self, logger=None, destination=None, *args, **kwargs):
        import redis
        super(RedisQueue, self).__init__(*args, **kwargs)
        host = os.environ.get('ZTAG_REDIS_HOST', 'localhost')
        port = int(os.environ.get('ZTAG_REDIS_PORT', 6379))
        if destination == "full_ipv4":
            queue = "ipv4"
        elif destination == "alexa_top1mil":
            queue = "domain"
        else:
            raise Exception("invalid destination: %s" % destination)
        self.logger = logger
        self.queue = queue
        try:
            self.redis = redis.Redis(host=host, port=port, db=0,
                                     socket_connect_timeout=10)
        except redis.ConnectionError as e:
            msg = "could not connect to redis: %s" % str(e)
            self.logger.fatal(msg)
        # batching
        self.queued = 0
        self.retries = 0
        self.records = []
        self.certificates = []

    def push(self, noretry=False):
        import redis
        if self.queued == 0:
            return
        try:
            p = self.redis.pipeline()
            for r in self.records:
                p.rpush(self.queue, r)
            for r in self.certificates:
                p.rpush(self.CERTIFICATES_QUEUE, r)
            p.execute()
            self.queued = 0
            self.records = []
            self.certificates = []
            self.retries = 0
        except redis.ConnectionError as e:
            time.sleep(1.0)
            self.retries += 1
            if self.retries > self.MAX_RETRIES or noretry:
                msg = "redis connection error: %s" % str(e)
                self.logger.fatal(msg)
                self.redis = None

    def take(self, pbout):
        self.records.append(pbout.transformed)
        self.certificates.extend(pbout.certificates)
        self.queued += (len(pbout.certificates) + 1)
        if self.queued > self.BATCH_SIZE:
            self.push()

    def cleanup(self):
        return self.push(noretry=True)

class Kafka(Outgoing):

    def __init__(self, logger=None, destination=None, *args, **kwargs):
        from kafka import KafkaProducer
        if destination == "full_ipv4":
            self.topic = "ipv4"
        elif destination == "alexa_top1mil":
            self.topic = "domain"
        else:
            raise Exception("invalid destination: %s" % destination)
        host = os.environ.get('KAFKA_BOOTSTRAP_HOST', 'localhost:9092')
        self.main_producer = KafkaProducer(bootstrap_servers=host)
        self.cert_producer = KafkaProducer(bootstrap_servers=host)

    def take(self, pbout):
        for certificate in pbout.certificates:
            self.cert_producer.send("certificate", certificate)
        self.main_producer.send(self.topic, pbout.transformed)

    def cleanup(self):
        if self.main_producer:
            self.main_producer.flush()
        if self.cert_producer:
            self.cert_producer.flush()

failed_msg_t = collections.namedtuple('failed_msg_t', 'topic msg attempt')

class PubsubState():
    '''
    Hold state with single course-grained lock. Restrict to safe operations
    on shared memory.
    '''

    def __init__(self):
        self._lock = threading.Lock()
        self._npending_msgs = 0
        self._failed_msgs = []

        # An individual thread raising an exception or calling
        # sys.exit() will only end that thread. Use this to
        # signal the rest of the threads to exit.
        self.exit_exception = None

    def inc_npending(self):
        self._lock.acquire()
        self._npending_msgs += 1
        self._lock.release()

    def dec_npending(self):
        self._lock.acquire()
        self._npending_msgs -= 1
        self._lock.release()

    def get_npending(self):
        '''
        No lock required to simply read int; no direct writes allowed.
        '''
        return self._npending_msgs

    def add_failed_msg(self, topic, msg, attempt):
        self._lock.acquire()
        self._failed_msgs.append(failed_msg_t(topic, msg, attempt))
        self._lock.release()

    def retrieve_failed_msgs(self):
        '''
        Retrieve list of failed messages and reset running list. Returned
        value is no longer shared data.
        '''
        self._lock.acquire()
        retval = self._failed_msgs
        self._failed_msgs = []
        self._lock.release()
        return retval

class Pubsub(Outgoing):

    MAX_ATTEMPTS = 5

    def __init__(self, logger=None, destination=None, *args, **kwargs):
        import google
        from google.cloud import pubsub, pubsub_v1
        self.logger = logger
        if logger is None:
            self.logger = logging.getLogger('null-logger')
            self.logger.setLevel(9999)
        if destination == "full_ipv4":
            self.topic_url = os.environ.get('PUBSUB_IPV4_TOPIC_URL')
        elif destination == "alexa_top1mil":
            self.topic_url = os.environ.get('PUBSUB_ALEXA_TOPIC_URL')
        self.cert_topic_url = os.environ.get('PUBSUB_CERT_TOPIC_URL')
        if not self.topic_url:
            raise Exception('missing $PUBSUB_[IPV4|ALEXA]_TOPIC_URL')
        if not self.cert_topic_url:
            raise Exception('missing $PUBSUB_CERT_TOPIC_URL')
        batch_settings = pubsub_v1.types.BatchSettings(
            # "The entire request including one or more messages must
            #  be smaller than 10MB, after decoding."
            max_bytes=8192000,  # 8 MB
            max_latency=15,     # 15 seconds
        )
        self.publisher = pubsub.PublisherClient(batch_settings)
        self.publish_count = {}
        try:
            self.publisher.get_topic(self.topic_url)
            self.publisher.get_topic(self.cert_topic_url)
        except google.api_core.exceptions.GoogleAPICallError as e:
            logger.error(e.message)
            raise
        self._state = PubsubState()

    def _make_done_callback(self, topic, data, attempt):
        def done_callback(future):
            if self._state.exit_exception:
                sys.exit(1)
            exception = future.exception()
            if not exception:
                self.logger.debug("Publish attempt #{attempt}/{max} on topic '{topic}' "
                                  "succeeded.".format(attempt=attempt + 1,
                                                      max=self.MAX_ATTEMPTS,
                                                      topic=topic))
                self._state.dec_npending()
            else:
                self.logger.error("Publish attempt #{attempt}/{max} failed for data '{data}' on"
                                  "topic '{topic}' {error}"
                                  .format(attempt=attempt + 1,
                                          max=self.MAX_ATTEMPTS,
                                          data=data,
                                          topic=topic,
                                          error=str(exception)))
                if attempt >= self.MAX_ATTEMPTS:
                    self._state.exit_exception = exception
                    sys.exit(1)
                self._state.add_failed_msg(topic, data, attempt + 1)

        return done_callback

    def _publish_with_callback(self, topic, data, attempt):
        if attempt == 0:
            self._state.inc_npending()
        cb = self._make_done_callback(topic, data, attempt)
        publish_future = self.publisher.publish(topic, data)
        publish_future.add_done_callback(cb)

    def take(self, pbout):
        for certificate in pbout.certificates:
            self._publish_with_callback(self.cert_topic_url, certificate, 0)
        self._publish_with_callback(self.topic_url, pbout.transformed, 0)

    def cleanup(self):
        while self._state.get_npending() > 0:
            time.sleep(10)
            if self._state.exit_exception:
                self.logger.error("Max attempts exceeded; raising most recent exception.")
                raise self._state.exit_exception
            failed_msgs = self._state.retrieve_failed_msgs()
            self.logger.debug("Failed message queuelen: {}, "
                              "messages pending: {}"
                              .format(len(failed_msgs),
                                      self._state.get_npending()))
            for failed in failed_msgs:
                self._publish_with_callback(failed.topic, failed.msg,
                                            failed.attempt + 1)
        self.logger.debug("Pubsub cleanup: Finished.")