text:
#ifndef _INTEGRAND_H_
#define _INTEGRAND_H_

#include <Windows.h>
#include "MathLibDefinitions.h"

namespace mathlib
{
	// struct which represent integrand and various auxiliary variables
	// needed to perform multi-threaded quadrature.
	struct Fx
	{
		std::function<double(double)> fptr;

		double a, b;
		unsigned num_steps;
		unsigned int NWorkers;
		unsigned int * Workers;
		DWORD * ThIDArray;
		CRITICAL_SECTION cs;
		double result{ 0.0 };
	};

 __forceinline	void static InitFx(mathlib::Fx *, std::function<double(double)>, double, double, const unsigned int,
	 const unsigned int);

 __forceinline  void static DeallocFx(mathlib::Fx *);

     // Global struct ued to hold the result of Quadrture.
 // This is flawed solution.
	 struct QdrtrResult
	{
		double result ;
	};

#include "Integrand.inl"
}
#endif /*_INTEGRAND_H_*/


text:
<?php

/**
 * @namespace
 */
namespace Zend\Validator;
use Zend;
use Zend\Locale;

/**
 * @uses       \Zend\Locale\Locale
 * @uses       \Zend\Locale\Format
 * @uses       \Zend\Registry
 * @uses       \Zend\Validator\AbstractValidator
 * @category   Zend
 * @package    Zend_Validate
 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 */
class Float extends AbstractValidator
{
    const INVALID   = 'floatInvalid';
    const NOT_FLOAT = 'notFloat';

    /**
     * @var array
     */
    protected $_messageTemplates = array(
        self::INVALID   => "Invalid type given, value should be float, string, or integer",
        self::NOT_FLOAT => "'%value%' does not appear to be a float",
    );

    protected $_locale;

    /**
     * Constructor for the float validator
     *
     * @param string|Zend_Config|\Zend\Locale\Locale $locale
     */
    public function __construct($locale = null)
    {
        if ($locale instanceof \Zend\Config\Config) {
            $locale = $locale->toArray();
        }

        if (is_array($locale)) {
            if (array_key_exists('locale', $locale)) {
                $locale = $locale['locale'];
            } else {
                $locale = null;
            }
        }

        if (empty($locale)) {
            if (\Zend\Registry::isRegistered('Zend_Locale')) {
                $locale = \Zend\Registry::get('Zend_Locale');
            }
        }

        $this->setLocale($locale);
    }

    /**
     * Returns the set locale
     */
    public function getLocale()
    {
        return $this->_locale;
    }

    /**
     * Sets the locale to use
     *
     * @param string|\Zend\Locale\Locale $locale
     */
    public function setLocale($locale = null)
    {
        $this->_locale = Locale\Locale::findLocale($locale);
        return $this;
    }

    /**
     * Defined by Zend_Validate_Interface
     *
     * Returns true if and only if $value is a floating-point value
     *
     * @param  string $value
     * @return boolean
     */
    public function isValid($value)
    {
        if (!is_string($value) && !is_int($value) && !is_float($value)) {
            $this->_error(self::INVALID);
            return false;
        }

        if (is_float($value)) {
            return true;
        }

        $this->_setValue($value);
        try {
            if (!Locale\Format::isFloat($value, array('locale' => $this->_locale))) {
                $this->_error(self::NOT_FLOAT);
                return false;
            }
        } catch (Locale\Exception $e) {
            $this->_error(self::NOT_FLOAT);
            return false;
        }

        return true;
    }
}

redpajama_set_name: RedPajamaGithub


text:
// Dependencies.
var winston = require("winston");
var constants = require("../config/constants.js");
var sessionManager = require("../session/sessionManager.js");
var utils = require("../utils/utils.js");
var jsonUtils = require("../utils/jsonUtils.js");
var responseUtils = require("../utils/responseUtils.js");
var toDoDAO = require("../dao/toDo.js");
var async = require("async");
var validation = require("../utils/endpointsValidation.js");

var UpdateToDoEndpoint = function() {
  // Global module level variables.
  var MODULE_NAME = "UpdateToDoEndpoint.js: ";

  // Public functions (this.methodName makes a method publicly accessible, otherwise it is accessible only within module).
  this.updateToDo = updateToDo;

  /**
   * Update ToDo endpoint API.
   *
   * @params requestParams - Request parameters from client app.
   * @params callback - Returns final client success/failure response.
   */
  function updateToDo(requestParams, callback) {
    var METHOD_NAME = "updateToDo():"

    // Extract sessionId from request parameters.
    var sessionId = jsonUtils.getPath(requestParams, "request.header.sessionId");

    async.waterfall(
      [

        function doValidateSession(cb) {
          // If sessionId not provided return failure callback.
          if (utils.isBlank(sessionId)) {
            var responseJson = responseUtils.constructAuthFailureResponseJson();
            return callback(responseJson, null);
          }
          // Validate sessionId.
          sessionManager.isValidSession(sessionId, function handleIsValidSessionResponse(errorMessage, isValidSession) {
            // If sessionId is invalid return failure callback.
            if (!isValidSession) {
              var responseJson = responseUtils.constructAuthFailureResponseJson(sessionId);
              winston.error(MODULE_NAME + METHOD_NAME + "Invalid session response - " + JSON.stringify(responseJson));
              return callback(responseJson, null);
            }
            cb(null, sessionId);
          });
        },
        function doGetSession(sessionId, cb) {
          // Fetch required parameters from the session.
          sessionManager.getSession(sessionId, function handleGetSessionResponse(errorMessage, sessionObject) {
            // If error reading session attributes return failure callback.
            if (errorMessage) {
              var responseJson = responseUtils.constructAuthFailureResponseJson();
              winston.error(MODULE_NAME + METHOD_NAME + "Unable to retrieve session parameters - " + JSON.stringify(responseJson));
              return callback(responseJson, null);
            }

            // If session object does not exist return failure callback.
            if (sessionObject == null || sessionObject == undefined) {
              var responseJson = responseUtils.constructAuthFailureResponseJson();
              winston.error(MODULE_NAME + METHOD_NAME + "Empty session parameters - " + JSON.stringify(responseJson));
              return callback(responseJson, null);
            }
            cb(null, sessionObject);

          });
        },
        function doValidateSessionParameters(sessionObject, cb) {
          // Validate session.
          var validationSessionResponse = validation.validateSessionParameters(sessionObject);

          // If session parameters are invalid return failure callback.
          if (!validationSessionResponse.status) {
            var errorResponse = responseUtils.constructErrorResponseJson(constants.RESP_AUTH_FAILED, "Complete ToDo", "Operation Failed", validationSessionResponse.message);
            return callback(sessionObject, null);
          }
          cb(null, sessionObject);
        },
        function doValidateUpdateTodoRequest(sessionObject, cb) {
          // Validate request parameters.
          var validationResponse = validation.validateUpdateTodoRequest(requestParams);

          // If request parameters are invalid return failure callback.
          if (!validationResponse.status) {
            var errorResponse = responseUtils.constructErrorResponseJson(constants.RESP_BAD_INPUT, "Change ToDo", "Operation Failed", validationResponse.message);
            return callback(errorResponse, null);
          }
          cb(null, sessionObject);
        },
        function doUpdateToDo(sessionObject, cb) {
          // Construct update TODO details object.
          var params = {
            "userId": sessionObject.userId,
            "toDoId": requestParams.request.payload.updateToDo.toDoId,
            "description": requestParams.request.payload.updateToDo.description,
            "deadline": requestParams.request.payload.updateToDo.deadline,
            "latitude": requestParams.request.payload.updateToDo.latitude,
            "longitude": requestParams.request.payload.updateToDo.longitude,
            "status": requestParams.request.payload.updateToDo.status,
            "note": requestParams.request.payload.updateToDo.note
          };

          // Update TODO details from database.
          var updateToDoResponse = toDoDAO.updateToDo(params, function handleUpdateToDoResponse(error, responseJson) {
            // If update operation failed return failure callback.
            if (error) {
              var errorResponse = responseUtils.constructErrorResponseJson(constants.RESP_BAD_INPUT, "Update ToDo", "Operation Failed", error.message);
              return callback(errorResponse, null);
            }

            var response = responseUtils.constructStatusResponseJson("updateToDo", constants.RESP_SUCCESS, "ToDo updated successfully.");
            cb(null, response);
          });
        }

      ],
      function(error, ResponseData) {
        return callback(null, ResponseData);
      });
  };
};

// Export module.
module.exports = new UpdateToDoEndpoint();


text:
/***************************************************************span close*******************************************************************/
$('.success span, .warning span, .attention span, .information span').live('click', function() {
    $(this).parent().fadeOut('slow', function() {
        $(this).remove();
    });
});

/******************************************************************************************************************************************/
/********************************************************************************menu********************************************************/
$(document).ready(function() {
    $('ul.menu').superfish({
        delay:       600,                            // one second delay on mouseout
        animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation
        speed:       'normal',                          // faster animation speed
        autoArrows:  false,                           // disable generation of arrow mark-up
        dropShadows: false                            // disable drop shadows
    });
});
/********************************************************************************************************************************************/
/**********************************************************prettyphoto*****************************************************************************/
$(document).ready(function(){
    $("a[data-gal^='prettyPhoto']").prettyPhoto({
        animationSpeed:'slow',theme:'facebook',slideshow:5000, autoplay_slideshow: true
    });
});
/************************************************************************************************************************************************/
/***********************************************************************backtotop*******************************************************************/
jQuery(document).ready(function(){
    var IE='\v'=='v';
    // hide #back-top first
    jQuery("#back-top").hide();
    // fade in #back-top
    jQuery(function () {
        jQuery(window).scroll(function () {
            if (!IE) {
                if (jQuery(this).scrollTop() > 100) {
                    jQuery('#back-top').fadeIn();
                } else {
                    jQuery('#back-top').fadeOut();
                }
            }
            else {
                if (jQuery(this).scrollTop() > 100) {
                    jQuery('#back-top').show();
                } else {
                    jQuery('#back-top').hide();
                }
            }
        });

        // scroll body to 0px on click
        jQuery('#back-top a').click(function () {
            jQuery('body,html').animate({
                scrollTop: 0
            }, 800);
            return false;
        });
    });

});/************************************************************************************************************************************************/
$(function(){
    $('#header .links li').last().addClass('last');
    $('.breadcrumb a').last().addClass('last');
    $('.cart tr').eq(0).addClass('first');
});
/****************************************************************************bx-slider********************************************************/

$(document).ready(function(){
    $('#prod').bxSlider(
        {
            pager: true,
            controls:false
        }
    );
});

/**********************************************************tooltips**************************************************************************/
$(document).ready(function() {

    $(".list-services a.tooltips").easyTooltip();
    $(".product-info .wishlist a.tip").easyTooltip();
    $(".product-info .compare a.tip2").easyTooltip();
});
/*******************************************************************************************************************************************/

/********************************************************************************************************************************************/
$(document).ready(function() {
    $('.jcarousel-list li a img').css({opacity:'0.3'});
    $('.jcarousel-list li a img').hover(function(){
            jQuery(this).stop(true,false).animate({opacity:'1'}, {duration: 250});
        },function(){
            jQuery(this).stop(true,false).animate({opacity:'0.3'}, {duration: 250});
        }
    )
});

/***********************************************************************************************************************************************************************/
$(document).ready(function() {
    var fl=true;
    /* AddToCart */
    $('.addToCart').click(function() {
        if (fl) {
            fl=false;
            var tis = $(this);
            $.ajax({
                url: 'index.php?route=checkout/cart/add',
                type: 'post',
                data: 'product_id=' + tis.attr("data-id"),
                dataType: 'json',
                content: this,
                success: $.proxy(function(json) {
                    $('.success, .warning, .attention, .information, .error').remove();
                    if (json['redirect']) {
                        location = json['redirect'];
                    }
                    if (json['error']) {
                        if (json['error']['warning']) {
                            $('#notification').html('
' + json['error']['warning'] + '
');
                        }
                    }
                    if (json['success']) {
                        $('#notification').html('
' + json['success'] + '
');

                        $('.success').fadeIn('slow');
                        $('#cart-total').html(json['total']);
                        $('#cart .content').html(json['output']);
                        animateProduct(tis.parents().find(".image2 img") , $("#cart"));
                    }
                }, this)
            });
        }
    });

    function animateProduct(image,cart){

        var image_offset = image.offset();
        $("body").append('');
        var cart_offset = cart.offset();
        params = {
            top : cart_offset.top + 'px',
            left : cart_offset.left + 'px',
            opacity : 0.0,
            width : cart.width(),
            height : cart.height()
        };
        $('#temp').animate(params, 'slow', false, function () {
            $('#temp').remove();
            fl=true;
        });
    }});

/********************************************************************************************/
$(document).ready(function() {
    var fl=true;
    /* AddToCart */
    $('.addToCart-1').click(function() {
        if (fl) {
            fl=false;
            var tis = $(this);
            $.ajax({
                url: 'index.php?route=checkout/cart/add',
                type: 'post',
                data: 'product_id=' + tis.attr("data-id"),
                dataType: 'json',
                content: this,
                success: $.proxy(function(json) {
                    $('.success, .warning, .attention, .information, .error').remove();
                    if (json['redirect']) {
                        location = json['redirect'];
                    }
                    if (json['error']) {
                        if (json['error']['warning']) {
                            $('#notification').html('
' + json['error']['warning'] + '
');
                        }
                    }
                    if (json['success']) {
                        $('#notification').html('
' + json['success'] + '
');

                        $('.success').fadeIn('slow');
                        $('#cart-total').html(json['total']);
                        $('#cart .content').html(json['output']);
                        animateProduct(tis.parents().find(".image3 img") , $("#cart"));
                    }
                }, this)
            });
        }
    });

    function animateProduct(image,cart){

        var image_offset = image.offset();
        $("body").append('');
        var cart_offset = cart.offset();
        params = {
            top : cart_offset.top + 'px',
            left : cart_offset.left + 'px',
            opacity : 0.0,
            width : cart.width(),
            height : cart.height()
        };
        $('#temp').animate(params, 'slow', false, function () {
            $('#temp').remove();
            fl=true;
        });
    }});

/*************************************************************************************************************related coroucel*****************************************************************************/
$(document).ready(function() {
    $('.related-carousel .box-product ul').jcarousel({
        vertical: false,
        visible: 4,
        scroll: 1
    });
});
$(document).ready(function() {
    $('div.image-caroucel').jcarousel({
        vertical: false,
        visible: 3,
        scroll: 1
    });
});
/************************************************************************************************wrap span*****************************************************************************************************/
var fl2=true;
function addToCart(product_id) {
    if (fl2) {
        fl2=false;
        $.ajax({
            url: 'index.php?route=checkout/cart/add',
            type: 'post',
            data: 'product_id=' + product_id,
            dataType: 'json',
            success: function(json) {
                $('.success, .warning, .attention, .information, .error').remove();
                if (json['redirect']) {
                    location = json['redirect'];
                }
                if (json['error']) {
                    if (json['error']['warning']) {
                        $('#notification').html('
' + json['error']['warning'] + '
');
                    }
                }
                if (json['success']) {
                    $('#notification').html('
' + json['success'] + '
');

                    $('.success').fadeIn('slow');
                    $('#cart-total').html(json['total']);
                    $('#cart .content').html(json['output']);
                    var image = $('#img_'+product_id).offset();
                    if (image) {
                        var cart = $('#cart').offset();
                        $('').appendTo('body');
                        params = {
                            top : cart.top + 'px',
                            left : cart.left + 'px',
                            opacity : 0.2,
                            width :$('#img_'+product_id).width(),
                            height : $('#img_'+product_id).height()
                        };
                        // uncomment line below if you also want to scroll up
                        //$('html, body').animate({ scrollTop: 0 }, 'slow');
                        $('#temp').animate(params, 'slow', false, function () {
                            $('#temp').remove();
                            fl2=true;
                        });
                    } else {
//alert("hi there");
                    }
                }
            }
        });
    }
}

/******************************************************************************* BUTTONS ***************************************************************/


text:
title: "Metrics API"
description: "The Sensu metrics API provides HTTP access to internal Sensu metrics, including embedded etcd, memory usage, garbage collection, and gRPC metrics. Read on for the full reference."
api_title: "Metrics API"
type: "api"
version: "5.21"
product: "Sensu Go"
menu:
  sensu-go-5.21:
    parent: api
---

## Get Sensu metrics

The `/metrics` API endpoint provides HTTP GET access to internal Sensu metrics in [Prometheus][1] format, including embedded etcd, memory usage, garbage collection, and gRPC metrics.

### Example {#metrics-get-example}

The following example demonstrates a request to the `/metrics` API endpoint, resulting in plaintext output that contains internal Sensu metrics.

{{}}
curl -X GET \
http://127.0.0.1:8080/metrics

HTTP/1.1 200 OK
# HELP etcd_debugging_mvcc_compact_revision The revision of the last compaction in store.
# TYPE etcd_debugging_mvcc_compact_revision gauge
etcd_debugging_mvcc_compact_revision 300
# HELP etcd_debugging_mvcc_current_revision The current revision of store.
# TYPE etcd_debugging_mvcc_current_revision gauge
etcd_debugging_mvcc_current_revision 316
# HELP etcd_debugging_mvcc_db_compaction_keys_total Total number of db keys compacted.
# TYPE etcd_debugging_mvcc_db_compaction_keys_total counter
etcd_debugging_mvcc_db_compaction_keys_total 274
# HELP etcd_debugging_mvcc_db_compaction_pause_duration_milliseconds Bucketed histogram of db compaction pause duration.
# TYPE etcd_debugging_mvcc_db_compaction_pause_duration_milliseconds histogram
etcd_debugging_mvcc_db_compaction_pause_duration_milliseconds_bucket{le="1"} 0
etcd_debugging_mvcc_db_compaction_pause_duration_milliseconds_bucket{le="2"} 0
...
{{}}

### API Specification {#metrics-get-specification}

/metrics (GET)  |
---------------|------
description    | Returns internal Sensu metrics in Prometheus format, including embedded etcd, memory usage, garbage collection, and gRPC metrics.
example url    | http://hostname:8080/metrics
response type  | [Prometheus-formatted][1] plaintext
response codes |

**Success**: 200 (OK)

**Error**: 500 (Internal Server Error)

output         | {{}}
# HELP etcd_debugging_mvcc_compact_revision The revision of the last compaction in store.
# TYPE etcd_debugging_mvcc_compact_revision gauge
etcd_debugging_mvcc_compact_revision 300
# HELP etcd_debugging_mvcc_current_revision The current revision of store.
# TYPE etcd_debugging_mvcc_current_revision gauge
etcd_debugging_mvcc_current_revision 316
# HELP etcd_debugging_mvcc_db_compaction_keys_total Total number of db keys compacted.
# TYPE etcd_debugging_mvcc_db_compaction_keys_total counter
etcd_debugging_mvcc_db_compaction_keys_total 274
# HELP etcd_debugging_mvcc_db_compaction_pause_duration_milliseconds Bucketed histogram of db compaction pause duration.
# TYPE etcd_debugging_mvcc_db_compaction_pause_duration_milliseconds histogram
etcd_debugging_mvcc_db_compaction_pause_duration_milliseconds_bucket{le="1"} 0
etcd_debugging_mvcc_db_compaction_pause_duration_milliseconds_bucket{le="2"} 0
...
{{}}

[1]: https://prometheus.io/docs/concepts/data_model/


text:
package org.osgl.util;

import org.osgl.$;

import java.util.Iterator;

class CollectorIterator<T, R> implements Iterator<R> {

    private Iterator<? extends T> data;
    private String path;

    CollectorIterator
    (Iterator<? extends T> itr, String path) {
        this.data = $.requireNotNull(itr);
        this.path = S.requireNotBlank(path);
    }

    protected Iterator<? extends T> data() {
        return data;
    }

    @Override
    public boolean hasNext() {
        return data.hasNext();
    }

    @Override
    public R next() {
        return $.getProperty(data.next(), path);
    }

    @Override
    public void remove() {
        E.unsupport();
    }

    @Override
    public int hashCode() {
        return $.hc(data, path);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (obj instanceof CollectorIterator) {
            CollectorIterator that = (CollectorIterator)obj;
            return $.eq(that.data, data) && $.eq(that.path, path);
        }
        return false;
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder("CollectorIterator\npath[");
        sb.append(path).append("]\nbuf[\n").append(data).append("\n]");
        return sb.toString();
    }
}


text:
<!--
 Copyright Duke Comprehensive Cancer Center and SemanticBits

 Distributed under the OSI-approved BSD 3-Clause License.
 See http://ncip.github.com/c3pr/LICENSE.txt for details.
-->
<dataset>

    <STUDIES id="1000" version="1" blinded_indicator="1"  multi_institution_indicator="1" phase_code="Ph" randomized_indicator="1" type="ty" status="OPEN"   retired_indicator = "false" />
    <STUDIES id="1001" version="1" blinded_indicator="0"  multi_institution_indicator="1" phase_code="Ph2" randomized_indicator="1" type="ty"  status="OPEN" retired_indicator = "false" />
    <STUDIES id="1002" version="1" blinded_indicator="0"  multi_institution_indicator="1" phase_code="Ph" randomized_indicator="1" type="ty" status="OPEN"  retired_indicator = "false" />
    <STUDIES id="1003" version="1" blinded_indicator="0"  multi_institution_indicator="1" phase_code="Ph" randomized_indicator="1" type="ty" status="AMENDMENT_PENDING"  retired_indicator = "false" />

    <STUDY_VERSIONS  original_indicator="true" id="1000" study_id="1000" amendment_type="OPTIONAL" name="1.0" version_date="01-01-2001" long_title_text="long_title_text" short_title_text="short_title_text" description_text="description" precis_text="precis_text" data_entry_status="COMPLETE"  retired_indicator = "false"/>
    <STUDY_VERSIONS  original_indicator="true" id="1001" study_id="1001" amendment_type="OPTIONAL" name="1.0" version_date="01-01-2001" long_title_text="long_title_text2" short_title_text="short_title_text2" description_text="description2" precis_text="precis_text2" data_entry_status="COMPLETE"  retired_indicator = "false"/>
    <STUDY_VERSIONS  original_indicator="true" id="1002" study_id="1002" amendment_type="OPTIONAL" name="1.0" version_date="01-01-2001" long_title_text="long_title_text" short_title_text="short_title_text" description_text="description" precis_text="precis_text" data_entry_status="COMPLETE"  retired_indicator = "false"/>
    <STUDY_VERSIONS  original_indicator="true" id="1003" study_id="1003" amendment_type="OPTIONAL" name="1.0" version_date="01-01-2001" long_title_text="long_title_text" short_title_text="short_title_text" description_text="description" precis_text="precis_text" data_entry_status="COMPLETE"  retired_indicator = "false"/>

    <ADDRESSES id="1000" version="1" street_address="12359 sunrise valley drive" city="Reston" state_code="VA"
               postal_code="20191" country_code="USA" retired_indicator="false"/>
    <ADDRESSES id="1001" version="1" street_address="Coppermine Dr" city="Herndon" state_code="VA" postal_code="20171"
               country_code="USA" retired_indicator="false"/>

    <PARTICIPANTS id="1000" version="1" first_name="Rudolph" last_name="Clooney" birth_date="01-01-2000"
                  administrative_gender_code="M" race_code="White" add_id="1000" retired_indicator="false"/>

    <ORGANIZATIONS id="1000" version="1" name="duke healthcare" description_text="duke healthcare" dtype="Local" address_id="1000"
                    retired_indicator="false"/>
    <ORGANIZATIONS id="1001" version="1" name="National Cancer Institute" description_text="National Cancer Institute" dtype="Local"
                   address_id="1001" retired_indicator="false"/>
    <ORGANIZATIONS id="1002" version="1" name="CALGB" description_text="CALGB" dtype="Local" address_id="1000"
                 retired_indicator="false"/>
	<ORGANIZATIONS id="1003" version="1" name="CTEP" description_text="CTEP" dtype="Local" address_id="1000"
                 retired_indicator="false"/>

    <STUDY_ORGANIZATIONS id="1000" version="1" study_id="1000" hcs_id="1000" type="SST" retired_indicator="false" />
    <STUDY_ORGANIZATIONS id="1001" version="1" study_id="1000" hcs_id="1001" type="SFS" retired_indicator="false"/>
    <STUDY_ORGANIZATIONS id="1004" version="1" study_id="1000" hcs_id="1002" type="SCC" retired_indicator="false"/>
    <STUDY_ORGANIZATIONS id="1002" version="1"  study_id="1001" hcs_id="1000" type="SST" retired_indicator="false"  />
    <STUDY_ORGANIZATIONS id="1003" version="1" study_id="1001" hcs_id="1001" type="SFS" retired_indicator="false"/>
    <STUDY_ORGANIZATIONS id="1005" version="1" study_id="1001" hcs_id="1002" type="SCC" retired_indicator="false"/>

    <STU_SITE_STATUS_HISTORY id="1000" version="1" sto_id="1000" site_study_status="ACTIVE"  start_date="01-01-2000" />
    <STU_SITE_STATUS_HISTORY id="1002" version="1" sto_id="1002" site_study_status="ACTIVE"  start_date="01-01-2000" />

    <study_site_versions id="1000" version="1" start_date="01-01-2001" irb_approval_date="01-01-2000"
    stu_version_id="1000" sto_id="1000" retired_indicator="false"/>
    <study_site_versions id="1002" version="1" site_study_status="ACTIVE" start_date="01-01-2001" irb_approval_date="01-01-2000"
    stu_version_id="1001" sto_id="1002" retired_indicator="false"/>

    <IDENTIFIERS id="-1000" version="1" value="nci" type="local" system_name="nci" dtype="SAI" org_id="1003" stu_id="1003" hcs_id="1002" retired_indicator="false" primary_indicator="0"/>
	 <IDENTIFIERS id="0909" version="1" value="nci1" type="Coordinating Center Identifier" hcs_id="1000" dtype="OAI" stu_id="1000" retired_indicator="false"/>
     <IDENTIFIERS id="1002" version="1" value="nci2" type="Coordinating Center Identifier" hcs_id="1000" dtype="OAI" stu_id="1001" retired_indicator="false"/>
    <IDENTIFIERS id="10021" version="1" value="nci3" type="TEST" system_name="nci" dtype="SAI" stu_id="1001"  retired_indicator="false"/>
     <IDENTIFIERS id="1003" version="1" value="nci4" type="Coordinating Center Identifier" hcs_id="1000" dtype="OAI" stu_id="1002" retired_indicator="false"/>
    <IDENTIFIERS id="1004" version="1" value="pai1" type="Protocol Authority Identifier" hcs_id="1000" dtype="OAI" stu_id="1003" retired_indicator="false"/>
    <IDENTIFIERS id="1005" version="1" value="pai2" type="Protocol Authority Identifier" hcs_id="1000" dtype="OAI" stu_id="1002" retired_indicator="false"/>
    <IDENTIFIERS id="11000" version="1" value="Duke" type="CTEP" dtype="OAI" retired_indicator="false" org_id="1003" hcs_id="1000" primary_indicator="1"/>
    <IDENTIFIERS id="11001" version="1" value="NCI" type="CTEP" dtype="OAI" retired_indicator="false" org_id="1003" hcs_id="1001" primary_indicator="1"/>
    <IDENTIFIERS id="11002" version="1" value="CALGB" type="CTEP" dtype="OAI" retired_indicator="false" org_id="1003" hcs_id="1002" primary_indicator="1"/>

    <STUDY_SUBJECTS id="1000" version="1" start_date="2000-01-01" prt_id="1000" sti_id="1000" retired_indicator="false"/>

	<study_subject_versions id="1000" version="1" study_site_ver_id="1000" spa_id="1000" retired_indicator = "false" />

    <RANDOMIZATIONS id="1001" version="1" dtype="BR" retired_indicator="false"/>

    <EPOCHS id="1000" version="1" name="Treatment1000" description_text="treatement desc" stu_version_id="1000" treatment_indicator="1"
            retired_indicator="false" epoch_order="0"/>
    <EPOCHS id="1001" version="1" name="Treatment1001" description_text="cancer desc" stu_version_id="1000" treatment_indicator="1"
            retired_indicator="false" epoch_order="4"/>
    <EPOCHS id="1002" version="1" name="Treatment1002" description_text="treatement desc" stu_version_id="1000" treatment_indicator="1"
            retired_indicator="false" epoch_order="5"/>
    <EPOCHS id="1003" version="1" name="Treatment1003" description_text="cancer desc" stu_version_id="1000" treatment_indicator="1"
            retired_indicator="false" epoch_order="2"/>
    <EPOCHS id="1004" version="1" name="NonTreatment1004" description_text="non treatement desc" stu_version_id="1000"
            treatment_indicator="1" retired_indicator="false" epoch_order="1"/>
    <EPOCHS id="1005" version="1" name="NonTreatment1005" description_text="non treatment cancer desc" stu_version_id="1000"
            treatment_indicator="1" retired_indicator="false" epoch_order="3"/>

    <SCHEDULED_EPOCHS id="1000" version="1" start_date="01-01-2000" eligibility_indicator="1"
                      ELIGIBILITY_WAIVER_REASON_TEXT="reason" study_subject_ver_id="1000" eph_id="1000"
                      retired_indicator="false"/>

    <ARMS id="1000" version="1" name="Arm 1000" description_text="Arm 1000 desc" target_accrual_number="1000"
          eph_id="1000" retired_indicator="false"/>
    <ARMS id="1001" version="1" name="Arm 1001" description_text="Arm 1001 desc" target_accrual_number="1001"
          eph_id="1000" retired_indicator="false"/>
    <ARMS id="1002" version="1" name="Arm 1002" description_text="Arm 1002 desc" target_accrual_number="1002"
          eph_id="1001" retired_indicator="false"/>
    <ARMS id="1003" version="1" name="Arm 1003" description_text="Arm 1003 desc" target_accrual_number="1003"
          eph_id="1001" retired_indicator="false"/>
    <ARMS id="1004" version="1" name="Arm 1004" description_text="Arm 1004 desc" target_accrual_number="1004"
          eph_id="1002" retired_indicator="false"/>
    <ARMS id="1005" version="1" name="Arm 1005" description_text="Arm 1005 desc" target_accrual_number="1005"
          eph_id="1002" retired_indicator="false"/>
    <ARMS id="1006" version="1" name="Arm 1006" description_text="Arm 1006 desc" target_accrual_number="1006"
          eph_id="1003" retired_indicator="false"/>
    <ARMS id="1007" version="1" name="Arm 1007" description_text="Arm 1007 desc" target_accrual_number="1007"
          eph_id="1003" retired_indicator="false"/>

    <STRATUM_GROUPS id="1001" version="1" current_position="1" stratum_group_number="8" epochs_id="1000"
                    retired_indicator="false"/>

    <BOOK_RNDM_ENTRY id="1001" version="1" position="1" str_grp_id="1001" arms_id="1000" rndm_id="1001"
                     retired_indicator="false"/>

    <RESEARCH_STAFF id="1000" version="1" dtype="Local" first_name="Research Bill" last_name="Staff2"
                    retired_indicator="false"/>
    <RESEARCH_STAFF id="1001" version="1" dtype="Local" first_name="Research George" last_name="Staff2"
                    retired_indicator="false"/>
    <RESEARCH_STAFF id="1002" version="1" dtype="Local" first_name="Research Benjamin" last_name="Staff3"
                    retired_indicator="false"/>
    <RESEARCH_STAFF id="1003" version="1" dtype="Local" first_name="Research JFK" last_name="Staff4"
                    retired_indicator="false"/>

<rs_hc_site_assocn id="1000" rs_id="1000" hcs_id="1000" />
  <rs_hc_site_assocn id="1001" rs_id="1001" hcs_id="1000" />
  <rs_hc_site_assocn id="1002" rs_id="1002" hcs_id="1001" />
  <rs_hc_site_assocn id="1003" rs_id="1003" hcs_id="1001" />

    <INVESTIGATORS id="1000" version="1" nci_identifier="bill" first_name="Investigator Bill" last_name="Staff1"
                   dtype="Local" retired_indicator="false"/>
    <INVESTIGATORS id="1001" version="1" first_name="Investigator George" nci_identifier="george" last_name="Staff2"
    				dtype="Local" retired_indicator="false"/>
    <INVESTIGATORS id="1002" version="1" first_name="Investigator Benjamin" last_name="Staff3" nci_identifier="benjamin"
                   dtype="Local" retired_indicator="false"/>
    <INVESTIGATORS id="1003" version="1" first_name="Investigator JFK" last_name="Staff4" retired_indicator="false" nci_identifier="jfk" dtype="Local" />

    <HC_SITE_INVESTIGATORS id="1000" version="1" status_code="AC" status_date="01-01-2000" inv_id="1000"
                           hcs_id="1000" retired_indicator="false"/>
    <HC_SITE_INVESTIGATORS id="1001" version="1" status_code="AC" status_date="01-01-2000" inv_id="1001"
                           hcs_id="1000" retired_indicator="false"/>
    <HC_SITE_INVESTIGATORS id="1002" version="1" status_code="AC" status_date="01-01-2000" inv_id="1002"
                           hcs_id="1001" retired_indicator="false"/>
    <HC_SITE_INVESTIGATORS id="1003" version="1" status_code="IN" status_date="01-01-2000" inv_id="1003"
                           hcs_id="1001" retired_indicator="false"/>

    <STUDY_INVESTIGATORS id="1000" version="1" hsi_id="1000" sto_id="1000" start_date="01-01-2000" end_date="01-01-2000"
                         role_code="role1" status_code="AC" retired_indicator="false"/>
    <STUDY_INVESTIGATORS id="1001" version="1" hsi_id="1001" sto_id="1000" start_date="01-01-2000" end_date="01-01-2000"
                         role_code="role2" status_code="AC" retired_indicator="false"/>

    <DISEASE_CATEGORIES id="1000" version="1" name="AIDS-related Human Papillomavirus" retired_indicator="false"/>

    <DISEASE_TERMS id="1000" version="1" term="AIDS-related anal cancer" ctep_term="AIDS-related anal cancer"
                   category_id="1000" medra_code="10065860" retired_indicator="false"/>
    <DISEASE_TERMS id="1001" version="1" term="AIDS-related cervical cancer" ctep_term="AIDS-related cervical cancer"
                   category_id="1000" medra_code="10008229" retired_indicator="false"/>

    <STUDY_DISEASES id="1000" version="1" stu_version_id="1000" disease_term_id="1000" lead_disease="1"
                    retired_indicator="false"/>
    <STUDY_DISEASES id="1001" version="1" stu_version_id="1000" disease_term_id="1001" lead_disease="1"
                    retired_indicator="false"/>

    <DISEASE_HISTORY id="1000" version="1" retired_indicator="false"/>

    <ELIGIBILITY_CRITERIA id="2000" version="1" question_number="1" eph_id="1000" question_text="inclusion question 1"
                          dtype="I" retired_indicator="false"/>
    <ELIGIBILITY_CRITERIA id="2001" version="1" question_number="2" eph_id="1000" question_text="inclusion question 2"
                          dtype="I" retired_indicator="false"/>
    <ELIGIBILITY_CRITERIA id="2002" version="1" question_number="3" eph_id="1000" question_text="exclusion question 1"
                          dtype="E" retired_indicator="false"/>

    <SUBJECT_ELIGIBILITY_ANS id="1000" version="1" sceph_id="1000" elgct_id="2000" retired_indicator="false"/>

    <STRAT_CRITERIA id="1000" version="1" question_text="question" question_number="1" retired_indicator="false"/>

    <STRAT_CRI_PER_ANS id="1000" version="1" permissible_answer="answer" str_cri_id="1000" retired_indicator="false"/>

    <SUBJECT_STRAT_ANS id="1000" version="1" sceph_id="1000" str_cri_id="1000" retired_indicator="false"/>

    <STRAT_CRI_ANS_CMB id="1000" version="1" retired_indicator="false"/>

    <CONTACT_MECHANISMS id="1000" version="1" />
    <prt_org_associations version="1" prt_id="1000" org_id="1000"  retired_indicator = "false"/>
</dataset>


text:
define([
    'jquery',
    'underscore',
    'backbone',
    'app/product/model'
], function($, _, Backbone, ProductModel) {
    var Collection = Backbone.Collection.extend({
        model: ProductModel,
        url: '/search'
    });

    var collection = new Collection();

    return collection;
});


text:
'use strict';

define(function () {
  // export
  return function isEmpty (value) {
    return value === undefined || value === null || (typeof value === 'string' ?  value.length < 1 : false)
  }
})


text:
'use strict';

// Declare app level module which depends on views, and components
/*
angular.module('myApp', [
  'ngRoute',
  'myApp.view1',
  'myApp.view2',
  'myApp.version'
]).
config(['$routeProvider', function($routeProvider) {
  $routeProvider.otherwise({redirectTo: '/view1'});
}]); */

var app = angular.module('myApp', ['ngRoute', 'ngResource', 'ui.bootstrap']);
app.config(function ($routeProvider) {

    $routeProvider
    .when("/discussion", {
        controller: "discussionController",
        templateUrl: "/app/views/discussion.html"
    })
    .when("/UseCases", {
        controller: "UseCasesController",
        templateUrl: "/app/views/UseCases.html"
    })
    .when("/Team", {
        controller: "TeamController",
        templateUrl: "/app/views/Team.html"
    })
    .when("/Issues", {
        controller: "IssuesController",
        templateUrl: "/app/views/Issues.html"
    })
    .when("/Timeline", {
        controller: "TimelineController",
        templateUrl: "/app/views/Timeline.html"
    })
    .when("/Wireframes", {
        controller: "WireframesController",
        templateUrl: "/app/views/Wireframes.html"
    })
    .when('/photos/:id', {
      controller: 'PhotoController',
      templateUrl: 'views/photo.html'
    })
    .otherwise({ redirectTo: "/discussion" });

});


text:
const yargs = require('yargs').argv;

const APPLICATION_PATH = '../build';
const DEBUG_MODE  = process.env.NODE_ENV === 'development';
const NO_BROWSER = yargs.headless;
const PROJECT_DIR = yargs._[0] || '.';
const SERVER_HOST = yargs.addr || '127.0.0.1';
const SERVER_PATH = __dirname;
const SERVER_PORT = yargs.port || (DEBUG_MODE && 8080) || 9876;
const SSL_KEYS    = yargs.ssl;

const ActionTypes = {
    READ_PROJECT:     'action/project/read',

    CREATE_DIRECTORY: 'action/directory/create',
    UPDATE_DIRECTORY: 'action/directory/update',
    DELETE_DIRECTORY: 'action/directory/delete',

    CREATE_FILE:      'action/file/create',
    READ_FILE:        'action/file/read',
    UPDATE_FILE:      'action/file/update',
    DELETE_FILE:      'action/file/delete',

    EXIT_APP:         'action/control/exit',
    KEEP_ALIVE:       'action/control/keepalive',
    NOTIFICATION:     'action/control/notify'
};

const NotificationTypes = {
    INFO:    'notification/info',
    ERROR:   'notification/error',
    SUCCESS: 'notification/success',
    WARNING: 'notification/warning'
};

module.exports = {
    APPLICATION_PATH,
    DEBUG_MODE,
    NO_BROWSER,
    PROJECT_DIR,
    SERVER_HOST,
    SERVER_PATH,
    SERVER_PORT,
    SSL_KEYS,
    ActionTypes,
    NotificationTypes,
};


text:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">

	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!--	扫描实体类	-->
		<property name="typeAliasesPackage" value="
			com.xmomen.framework.mybatis.model,
			com.xmomen.demo.entity.model
			" />
		<!--	mybatis-config文件路径	-->
		<property name="configLocation" value="classpath:config/mybatis-config.xml" />
		<!--	扫描mapper.xml文件	-->
		<property name="mapperLocations" value="classpath*:com/xmomen/**/mapper/**/*.xml" />
	</bean>

	<bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" destroy-method="clearCache">
		<constructor-arg index="0" ref="sqlSessionFactory" />
	</bean>
	<bean id="mybatisDao" class="com.xmomen.framework.mybatis.dao.impl.MybatisDaoImpl">
		<property name="sqlSessionTemplate" ref="sqlSessionTemplate" />
	</bean>

	<!--	mapper接口类扫描配置	-->
	<bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!--	扫描基础包路径(多基础包路径用;分隔)	-->
		<property name="basePackage" value="
			com.xmomen.framework.mybatis.mapper;
			com.xmomen.demo.entity.mapper" />
		<property name="sqlSessionTemplateBeanName" value="sqlSessionTemplate"/>
	</bean>

</beans>


text:
bool Ness(World* starting_world, map<World*,bool> results, string m_logic){

    queue<World*> Q;
    vector<World*> children;
    map<World*, bool> visited;

    for(auto i: starting_world->getAdjacentList()) Q.push(i);

    for(auto i: starting_world->getAdjacentList()) Q.push(i);
    if(m_logic == "K4"){
        for(auto i: starting_world->getAdjacentList()){
            for(auto j: i->getAdjacentList()){
                Q.push(j);
            }
        }
    }

    while(!Q.empty()){

        World* t = Q.front();
        Q.pop();

        if(visited.find(t) == visited.end()) visited[t] = true;
        else if(visited[t]) continue;

        if(!results[t]) return false;

        if(m_logic == "D"){

            children = t->getAdjacentList();
            for(auto i : children) Q.push(i);

        }
    }

    return true;
}

bool Poss(World* starting_world, map<World*,bool> results, string m_logic){

    queue<World*> Q;
    vector<World*> children;
    map<World*, bool> visited;

    for(auto i: starting_world->getAdjacentList()) Q.push(i);
    if(m_logic == "K4"){
        for(auto i: starting_world->getAdjacentList()){
            for(auto j: i->getAdjacentList()){
                Q.push(j);
            }
        }
    }

    while(!Q.empty()){

        World* t = Q.front();
        Q.pop();

        if(visited.find(t) == visited.end()) visited[t] = true;
        else if(visited[t]) continue;

        if(results[t]) return true;

        if(m_logic == "D"){

            children = t->getAdjacentList();
            for(auto i : children) Q.push(i);

        }
    }

    return false;
}

bool Operator(Token op, bool x, bool y) {
    if (op.type != TokenType::BINARY_OP) throw logic_error("Passed token is not binary operator!");
    if (op.value == "AND") return And(x,y);
    if (op.value == "OR") return Or(x,y);
    if (op.value == "NAND") return Nand(x,y);
    if (op.value == "NOR") return Nor(x,y);
    if (op.value == "XOR") return Xor(x,y);
    if (op.value == "IMPL") return Impl(x,y);
    if (op.value == "EQU") return Equ(x,y);
    throw logic_error("Something unexpected happened!");
}

bool Operator(World* world, map<World*, bool> resutls, Token op, string m_logic) {
    if(op.type != TokenType::UNARY_OP || op.value == "NOT")throw logic_error("Passed token is not Ness or Poss!");
    if(op.value == "POSS") return Poss(world,resutls,m_logic);
    if(op.value == "NESS") return Ness(world, resutls,m_logic);
    throw logic_error("Something unexpected happened!");
}


text:
package org.wso2.carbon.integration.test.extensionflow;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.wso2.carbon.automation.engine.context.TestUserMode;
import org.wso2.carbon.databridge.commons.Event;
import org.wso2.carbon.event.simulator.stub.types.EventDto;
import org.wso2.carbon.integration.test.client.Wso2EventServer;
import org.wso2.cep.integration.common.utils.CEPIntegrationTest;
import org.wso2.cep.integration.common.utils.CEPIntegrationTestConstants;

import java.io.File;
import java.util.List;

/**
 * Testing RDBMS Event Table Extension Sample..
 */
public class TimeExtensionTestCase extends CEPIntegrationTest {

    private static final Log log = LogFactory.getLog(TimeExtensionTestCase.class);

    @BeforeClass(alwaysRun = true)
    public void init()
            throws Exception {

        super.init(TestUserMode.SUPER_TENANT_ADMIN);
        String loggedInSessionCookie = getSessionCookie();

        eventSimulatorAdminServiceClient = configurationUtil.getEventSimulatorAdminServiceClient(backendURL, loggedInSessionCookie);
        eventStreamManagerAdminServiceClient = configurationUtil.getEventStreamManagerAdminServiceClient(backendURL, loggedInSessionCookie);
        eventPublisherAdminServiceClient = configurationUtil.getEventPublisherAdminServiceClient(backendURL, loggedInSessionCookie);
        eventProcessorAdminServiceClient = configurationUtil.getEventProcessorAdminServiceClient(backendURL, loggedInSessionCookie);
    }

    @Test(groups = {"wso2.cep"}, description = "Testing Time Extension")
    public void timeExtensionTestScenario() throws Exception {

        int startESCount = eventStreamManagerAdminServiceClient.getEventStreamCount();
        int startEPCount = eventPublisherAdminServiceClient.getActiveEventPublisherCount();
        int startEXPCount = eventProcessorAdminServiceClient.getExecutionPlanConfigurationCount();

        //Add StreamDefinition
        String streamDefinitionAsString1 = getJSONArtifactConfiguration("extensionflows" + File.separator + "time",
                                                                        "org.wso2.sample.stock.quote.stream_1.0.0.json");
        eventStreamManagerAdminServiceClient.addEventStreamAsString(streamDefinitionAsString1);
        String streamDefinitionAsString2 = getJSONArtifactConfiguration("extensionflows" + File.separator + "time",
                                                                        "org.wso2.sample.stockPriceWithTimeStream_1.0.0.json");
        eventStreamManagerAdminServiceClient.addEventStreamAsString(streamDefinitionAsString2);
        Assert.assertEquals(eventStreamManagerAdminServiceClient.getEventStreamCount(), startESCount + 2);

        //Add Execution Plan
        String executionPlanAsString = getExecutionPlanFromFile("extensionflows" + File.separator + "time", "ExecutionPlan.siddhiql");
        eventProcessorAdminServiceClient.addExecutionPlan(executionPlanAsString);
        Assert.assertEquals(eventProcessorAdminServiceClient.getActiveExecutionPlanConfigurationCount(), startEXPCount + 1);

        //Add RDBMS publisher
        String eventPublisherConfig = getXMLArtifactConfiguration("extensionflows" + File.separator + "time", "Wso2EventPublisher.xml");
        eventPublisherAdminServiceClient.addEventPublisherConfiguration(eventPublisherConfig);
        Assert.assertEquals(eventPublisherAdminServiceClient.getActiveEventPublisherCount(), startEPCount + 1);

        // The data-bridge receiver
        Wso2EventServer agentServer = new Wso2EventServer("Wso2EventTestCase", CEPIntegrationTestConstants.TCP_PORT, true);
        Thread agentServerThread = new Thread(agentServer);
        agentServerThread.start();
        // Let the server start
        Thread.sleep(5000);

        EventDto eventDto = new EventDto();
        eventDto.setEventStreamId("org.wso2.sample.stock.quote.stream:1.0.0");
        eventDto.setAttributeValues(new String[]{"IBM", "50000", "50"});
        eventSimulatorAdminServiceClient.sendEvent(eventDto);
        Thread.sleep(5000);

        Assert.assertEquals(agentServer.getMsgCount(), 1, "Incorrect number of messages consumed!");
        List<Event> eventList = agentServer.getPreservedEventList();
        Event event = eventList.get(0);
        Object timeValue = (event.getPayloadData()[1]);
        Assert.assertNotNull(timeValue, "Invalid Time Property Value Found");
        Thread.sleep(2000);

        eventStreamManagerAdminServiceClient.removeEventStream("org.wso2.sample.stock.quote.stream", "1.0.0");
        eventStreamManagerAdminServiceClient.removeEventStream("org.wso2.sample.stockPriceWithTimeStream", "1.0.0");
        eventProcessorAdminServiceClient.removeInactiveExecutionPlan("ExecutionPlan.siddhiql");
        eventPublisherAdminServiceClient.removeInactiveEventPublisherConfiguration("Wso2EventPublisher.xml");

        Thread.sleep(2000);

        agentServer.stop();
    }

    @AfterClass(alwaysRun = true)
    public void destroy() throws Exception {
        super.cleanup();
    }

}


text:
@interface PodsDummy_Pods_HHRouter : NSObject
@end
@implementation PodsDummy_Pods_HHRouter
@end


text:
namespace io = process::io;

using std::cerr;
using std::endl;
using std::map;
using std::string;
using std::vector;

using process::Failure;
using process::Future;
using process::Owned;
using process::Subprocess;

using mesos::NetworkInfo;

using mesos::internal::slave::cni::spec::PluginError;

namespace mesos {
namespace internal {
namespace slave {
namespace cni {

Try<Owned<PortMapper>, PluginError> PortMapper::create(const string& _cniConfig)
{
  Option<string> cniCommand = os::getenv("CNI_COMMAND");
  if (cniCommand.isNone()) {
    return PluginError(
        "Unable to find environment variable 'CNI_COMMAND'",
        ERROR_BAD_ARGS);
  }

  // NOTE: While CNI spec allows 'CNI_CONTAINERID' to be optional, the
  // plugin uses the 'CNI_CONTAINERID' to tag iptable rules so that
  // we can delete the rules without IP address information (which
  // will not be available during DEL). Hence, making this
  // variable required.
  Option<string> cniContainerId = os::getenv("CNI_CONTAINERID");
  if (cniContainerId.isNone()) {
    return PluginError(
        "Unable to find environment variable 'CNI_CONTAINERID'",
        ERROR_BAD_ARGS);
  }

  Option<string> cniNetNs = os::getenv("CNI_NETNS");
  if (cniNetNs.isNone() && cniCommand.get() != spec::CNI_CMD_DEL) {
    return PluginError(
        "Unable to find environment variable 'CNI_NETNS' for "
        "non-'" + stringify(spec::CNI_CMD_DEL) + "' command",
        ERROR_BAD_ARGS);
  }

  Option<string> cniIfName = os::getenv("CNI_IFNAME");
  if (cniIfName.isNone()) {
    return PluginError(
        "Unable to find environment variable 'CNI_IFNAME'",
        ERROR_BAD_ARGS);
  }

  // 'CNI_ARGS' is optional.
  Option<string> cniArgs = os::getenv("CNI_ARGS");

  Option<string> cniPath = os::getenv("CNI_PATH");
  if (cniPath.isNone()) {
    return PluginError(
        "Unable to find environment variable 'CNI_PATH'",
        ERROR_BAD_ARGS);
  }

  // Verify the CNI config for this plugin.
  Try<JSON::Object> cniConfig = JSON::parse<JSON::Object>(_cniConfig);
  if (cniConfig.isError()) {
    return PluginError(cniConfig.error(), ERROR_BAD_ARGS);
  }

  // TODO(jieyu): Validate 'cniVersion' and 'type'.

  Result<JSON::String> name = cniConfig->find<JSON::String>("name");
  if (!name.isSome()) {
    return PluginError(
        "Failed to get the required field 'name': " +
        (name.isError() ? name.error() : "Not found"),
        ERROR_BAD_ARGS);
  }

  Result<JSON::String> chain = cniConfig->find<JSON::String>("chain");
  if (!chain.isSome()) {
    return PluginError(
        "Failed to get the required field 'chain': " +
        (chain.isError() ? chain.error() : "Not found"),
        ERROR_BAD_ARGS);
  }

  vector<string> excludeDevices;

  Result<JSON::Array> _excludeDevices =
    cniConfig->find<JSON::Array>("excludeDevices");

  if (_excludeDevices.isError()) {
    return PluginError(
        "Failed to parse field 'excludeDevices': " +
        _excludeDevices.error(),
        ERROR_BAD_ARGS);
  } else if (_excludeDevices.isSome()) {
    foreach (const JSON::Value& value, _excludeDevices->values) {
      if (!value.is<JSON::String>()) {
        return PluginError(
            "Failed to parse 'excludeDevices' list. "
            "The excluded device needs to be a string",
            ERROR_BAD_ARGS);
      }

      excludeDevices.push_back(value.as<JSON::String>().value);
    }
  }

  // While the 'args' field is optional in the CNI spec it is critical
  // to the port-mapper plugin to learn of any port-mappings that the
  // framework might have requested for this container when this plugin
  // is called in the Mesos context. However, to make the port-mapper
  // plugin more generic rather than Mesos specific, we will create a
  // fake 'args` field if it is not filled by the caller.
  Result<JSON::Object> args = cniConfig->find<JSON::Object>("args");
  if (args.isError()) {
    return PluginError(
        "Failed to get the field 'args': " + args.error(), ERROR_BAD_ARGS);
  } else if (args.isNone()) {
    JSON::Object _args;
    JSON::Object mesos;
    mesos.values["network_info"] = JSON::Object();
    _args.values["org.apache.mesos"] = mesos;
    args = _args;
  }

  // NOTE: We can't directly use `find` to check for 'network_info'
  // within the 'args' dict, since 'org.apache.mesos' will be treated
  // as a path by `find` instead of a key. So we need to retrieve the
  // 'org.apache.mesos' key first and then use it to find
  // 'network_info'.
  Result<JSON::Object> mesos = args->at<JSON::Object>("org.apache.mesos");
  if (!mesos.isSome()) {
    return PluginError(
        "Failed to get the field 'args{org.apache.mesos}': " +
        (mesos.isError() ? mesos.error() : "Not found"),
        ERROR_BAD_ARGS);
  }

  Result<JSON::Object> _networkInfo = mesos->find<JSON::Object>("network_info");
  if (!_networkInfo.isSome()) {
    return PluginError(
        "Failed to get the field 'args{org.apache.mesos}{network_info}': " +
        (_networkInfo.isError() ? _networkInfo.error() : "Not found"),
        ERROR_BAD_ARGS);
  }

  Try<NetworkInfo> networkInfo =
    ::protobuf::parse<NetworkInfo>(_networkInfo.get());

  if (networkInfo.isError()) {
    return PluginError(
        "Unable to parse `NetworkInfo`: " + networkInfo.error(),
        ERROR_BAD_ARGS);
  }

  // The port-mapper should always be used in conjunction with another
  // 'delegate' CNI plugin.
  Result<JSON::Object> _delegateConfig =
    cniConfig->find<JSON::Object>("delegate");

  if (!_delegateConfig.isSome()) {
    return PluginError(
        "Failed to get the required field 'delegate'" +
        (_delegateConfig.isError() ? _delegateConfig.error() : "Not found"),
        ERROR_BAD_ARGS);
  }

  // TODO(jieyu): Validate that 'cniVersion' and 'name' exist in
  // 'delegate' as it should be a valid CNI config JSON.

  // Make sure the 'delegate' plugin exists.
  Result<JSON::String> _delegatePlugin =
    _delegateConfig->find<JSON::String>("type");

  if (!_delegatePlugin.isSome()) {
    return PluginError(
        "Failed to get the delegate plugin 'type'" +
        (_delegatePlugin.isError() ? _delegatePlugin.error() : "Not found"),
        ERROR_BAD_ARGS);
  }

  Option<string> delegatePlugin = os::which(
      _delegatePlugin->value,
      cniPath.get());

  if (delegatePlugin.isNone()) {
    return PluginError(
        "Could not find the delegate plugin '" + _delegatePlugin->value +
        "' in '" + cniPath.get() + "'",
        ERROR_BAD_ARGS);
  }

  // Add the 'name' and 'args' field to the 'delegate' config.
  JSON::Object delegateConfig(_delegateConfig.get());
  delegateConfig.values["name"] = name.get();
  delegateConfig.values["args"] = args.get();

  return Owned<PortMapper>(
      new PortMapper(
          cniCommand.get(),
          cniContainerId.get(),
          cniNetNs,
          cniIfName.get(),
          cniArgs,
          cniPath.get(),
          networkInfo.get(),
          delegatePlugin.get(),
          delegateConfig,
          chain->value,
          excludeDevices));
}

string PortMapper::getIptablesRuleTag()
{
  return "container_id: " + cniContainerId;
}

string PortMapper::getIptablesRule(
    const net::IP& ip,
    const mesos::NetworkInfo::PortMapping& portMapping)
{
  string devices;

  // Get list of devices to exclude.
  if (!excludeDevices.empty()) {
    foreach (const string& device, excludeDevices) {
      devices = "! -i " + device + " ";
    }
  }

  const string protocol = portMapping.has_protocol()
    ? strings::lower(portMapping.protocol())
    : "tcp";

  // Iptables DNAT rule representing a specific port-mapping.
  return strings::format(
      " %s %s -p %s -m %s"
      " --dport %d -j DNAT --to-destination %s:%d"
      " -m comment --comment \"%s\"",
      chain,
      devices,
      protocol,
      protocol,
      portMapping.host_port(),
      stringify(ip),
      portMapping.container_port(),
      getIptablesRuleTag()).get();
}

Try<Nothing> PortMapper::addPortMapping(
    const net::IP& ip,
    const mesos::NetworkInfo::PortMapping& portMapping)
{
  Try<string> iptablesRule = getIptablesRule(ip, portMapping);
  if (iptablesRule.isError()) {
    return Error(iptablesRule.error());
  }

  // Run the following iptables script to install the DNAT rule under
  // the specified chain.
  string script = strings::format(
      R"~(
      #!/bin/sh
      exec 1>&2
      set -x

      # NOTE: We need iptables 1.4.20 and higher for the commands to
      # work. We use the '-w' flag with the iptables command to ensure
      # that iptables command are executed atomically. This flag is
      # available starting iptables 1.4.20.
      #
      # Check if the `chain` exists in the iptable. If it does not
      # exist go ahead and install the chain in the iptables NAT
      # table.
      iptables -w -n -t nat --list %s
      if [ $? -ne 0 ]; then
        # NOTE: When we create the chain, there is a possibility of a
        # race due to which a container launch can fail. This can
        # happen specifically when two containers are launched with
        # port-mapping with the same iptables chain and the chain does
        # not exist. In this scenario, there might be a race for the
        # chain creation with only one of the containers succeeding.
        # iptables, unfortunately, does not allow locks to be acquired
        # outside the iptables process and hence there is no way to
        # avoid this race. This event itself should be quite rare
        # since it can happen only when the chain is created the first
        # time and two commands for creation of the chain are executed
        # simultaneously.
        (iptables -w -t nat -N %s || exit 1)

        # Once the chain has been installed add a rule in the PREROUTING
        # chain to jump to this chain for any packets that are
        # destined to a local address.
        (iptables -w -t nat -A PREROUTING \
        -m addrtype --dst-type LOCAL -j %s || exit 1)

        # For locally generated packets we need a rule in the OUTPUT
        # chain as well, since locally generated packets directly hit
        # the output CHAIN, bypassing PREROUTING.
        (iptables -w -t nat -A OUTPUT \
        ! -d 127.0.0.0/8 -m addrtype \
        --dst-type LOCAL -j %s || exit 1)
      fi

      # Within the `chain` go ahead and install the DNAT rule, if it
      # does not exist.
      (iptables -w -t nat -C %s || iptables -w -t nat -A %s))~",
      chain,
      chain,
      chain,
      chain,
      iptablesRule.get(),
      iptablesRule.get()).get();

  if (os::system(script) != 0) {
    return ErrnoError("Failed to add DNAT rule with tag");
  }

  return Nothing();
}

Try<Nothing> PortMapper::delPortMapping()
{
  // The iptables command searches for the DNAT rules with tag
  // "container_id: <CNI_CONTAINERID>", and if it exists goes ahead
  // and deletes it.
  //
  // NOTE: We use a temp file here, instead of letting `sed` directly
  // executing the iptables commands because otherwise, it is possible
  // that the port mapping cleanup command will cause iptables to
  // deadlock if there are a lot of entires in the iptables, because
  // the `sed` won't process the next line while executing `iptables
  // -w -t nat -D ...`. But the executing of `iptables -w -t nat -D
  // ...` might get stuck if the first command `iptables -w -t nat -S
  // <TAG>` didn't finish (because the xtables lock is not released).
  // The first command might not finish if it has a lot of output,
  // filling the pipe that `sed` hasn't had a chance to process yet.
  // See details in MESOS-9127.
  string script = strings::format(
      R"~(
      #!/bin/sh
      set -x
      set -e

      FILE=$(mktemp)

      cleanup() {
        rm -f "$FILE"
      }

      trap cleanup EXIT

      iptables -w -t nat -S %s | sed -n "/%s/ s/-A/iptables -w -t nat -D/p" > $FILE
      sh $FILE
      )~",
      chain,
      getIptablesRuleTag()).get();

  // NOTE: Ideally we should be cleaning up the iptables chain in case
  // this is the last rule in the chain. However, this is a bit tricky
  // since when we try deleting the chain another incarnation of the
  // `mesos-cni-port-mapper` plugin might be injecting rules into the
  // same chain causing a race. Further, there is no way to ascertain
  // the failure due to this race, and returning a failure in this
  // scenario would (erroneously) fail the deletion of the container.
  // We therefore leave it to the operator to delete the iptables
  // chain once he deems it safe to remove the chain.
  if (os::system(script) != 0) {
    return ErrnoError("Unable to delete DNAT rules");
  }

  return Nothing();
}

Try<string, PluginError> PortMapper::handleAddCommand()
{
  Result<spec::NetworkInfo> delegateResult = delegate(cniCommand);
  if (delegateResult.isError()) {
    return PluginError(
        "Could not execute the delegate plugin '" +
        delegatePlugin + "' for ADD command: " + delegateResult.error(),
        ERROR_DELEGATE_FAILURE);
  }

  cerr << "Delegate CNI plugin '" << delegatePlugin
       << "' executed successfully for ADD command: "
       << JSON::protobuf(delegateResult.get()) << endl;

  // We support only IPv4.
  if (!delegateResult->has_ip4()) {
    return PluginError(
        "Delegate CNI plugin '" + delegatePlugin +
        "' did not return an IPv4 address",
        ERROR_DELEGATE_FAILURE);
  }

  // The IP from `delegateResult->ip4().ip()` is in CIDR notation. We
  // need to strip out the netmask.
  Try<net::IP::Network> ip = net::IP::Network::parse(
      delegateResult->ip4().ip(),
      AF_INET);

  if (ip.isError()) {
    return PluginError(
        "Could not parse IPv4 address return by delegate CNI plugin '" +
        delegatePlugin + "': " + ip.error(),
        ERROR_DELEGATE_FAILURE);
  }

  // Walk through each of the port-mappings and install a
  // D-NAT rule for the port-map.
  foreach (const NetworkInfo::PortMapping& portMapping,
           networkInfo.port_mappings()) {
    Try<Nothing> result = addPortMapping(ip->address(), portMapping);
    if (result.isError()) {
      return PluginError(result.error(), ERROR_PORTMAP_FAILURE);
    }
  }

  return stringify(JSON::protobuf(delegateResult.get()));
}

Try<Nothing, PluginError> PortMapper::handleDelCommand()
{
  // In case of `spec::CNI_CMD_DEL` we want to first delete the
  // iptables DNAT rules and then invoke the delegate plugin with the
  // DEL command. Reason being that if the delegate plugin is invoked
  // before the iptables DNAT rules are removed, the IP address
  // associated with the current container might be re-allocated to a
  // new container, which might start receiving traffic hitting these
  // DNAT rules.
  Try<Nothing> result = delPortMapping();
  if (result.isError()) {
    return PluginError(
        "Unable to remove iptables DNAT rules: " + result.error(),
        ERROR_PORTMAP_FAILURE);
  }

  cerr << "Launching delegate CNI plugin '" << delegatePlugin
       << "' with DEL command" << endl;

  Result<spec::NetworkInfo> delegateResult = delegate(spec::CNI_CMD_DEL);
  if (delegateResult.isError()) {
    return PluginError(
        "Could not execute the delegate plugin '" +
        delegatePlugin + "' for DEL command: " + delegateResult.error(),
        ERROR_DELEGATE_FAILURE);
  }

  cerr << "Successfully removed iptables DNAT rule and detached container "
       << "using CNI delegate plugin '" << delegatePlugin << "'" << endl;

  return Nothing();
}

Try<Option<string>, PluginError> PortMapper::execute()
{
  if (cniCommand == spec::CNI_CMD_ADD) {
    Try<string, PluginError> result = handleAddCommand();
    if (result.isError()) {
      return result.error();
    }

    return result.get();
  } else if (cniCommand == spec::CNI_CMD_DEL) {
    Try<Nothing, PluginError> result = handleDelCommand();
    if (result.isError()) {
      return result.error();
    }

    return None();
  }

  return PluginError(
      "Unsupported command: " + cniCommand,
      ERROR_UNSUPPORTED_COMMAND);
}

Result<spec::NetworkInfo> PortMapper::delegate(const string& command)
{
  map<string, string> environment;

  environment["CNI_COMMAND"] = command;
  environment["CNI_IFNAME"] = cniIfName;
  environment["CNI_PATH"] = cniPath;
  environment["CNI_CONTAINERID"] = cniContainerId;

  if (cniNetNs.isSome()) {
    environment["CNI_NETNS"] = cniNetNs.get();
  }

  if (cniArgs.isSome()) {
    environment["CNI_ARGS"] = cniArgs.get();
  }

  // Some CNI plugins need to run "iptables" to set up IP Masquerade,
  // so we need to set the "PATH" environment variable so that the
  // plugin can locate the "iptables" executable file.
  Option<string> value = os::getenv("PATH");
  if (value.isSome()) {
    environment["PATH"] = value.get();
  } else {
    environment["PATH"] = os::host_default_path();
  }

  Try<string> temp = os::mktemp();
  if (temp.isError()) {
    return Error("Failed to create the temp file: " + temp.error());
  }

  Try<Nothing> write = os::write(
      temp.get(),
      stringify(delegateConfig));

  if (write.isError()) {
    os::rm(temp.get());
    return Error("Failed to write the temp file: " + write.error());
  }

  Try<Subprocess> s = process::subprocess(
      delegatePlugin,
      {delegatePlugin},
      Subprocess::PATH(temp.get()),
      Subprocess::PIPE(),
      Subprocess::PIPE(),
      nullptr,
      environment);

  if (s.isError()) {
    return Error(
        "Failed to exec the delegate CNI plugin '" + delegatePlugin +
        "' subprocess: " + s.error());
  }

  auto result = await(
      s->status(),
      io::read(s->out().get()),
      io::read(s->err().get()));

  result.await();

  // At this point the subprocess has completed and hence we need to
  // remove the `temp` file that we created.
  os::rm(temp.get());

  if (!result.isReady()) {
    return Error(
        "Failed to wait for exec of delegate CNI plugin '" +
        delegatePlugin + "': " +
        (result.isDiscarded() ? "discarded" : result.failure()));
  }

  const Future<Option<int>>& status = std::get<0>(result.get());
  if (!status.isReady()) {
    return Error(
        "Failed to get the exit status of the delegate CNI plugin '" +
        delegatePlugin + "' subprocess: " +
        (status.isFailed() ? status.failure() : "discarded"));
  }

  if (status->isNone()) {
    return Error(
        "Failed to reap the delegate CNI plugin '" +
        delegatePlugin + "' subprocess");
  }

  // CNI plugin will print result or error to stdout.
  const Future<string>& output = std::get<1>(result.get());
  if (!output.isReady()) {
    return Error(
        "Failed to read stdout from the delegate CNI plugin '" +
        delegatePlugin + "' subprocess: " +
        (output.isFailed() ? output.failure() : "discarded"));
  }

  // We are reading stderr of the plugin since any log messages from
  // the CNI plugin would be thrown on stderr. This can be useful for
  // debugging issues when the plugin throws a `spec::Error`.
  const Future<string>& err = std::get<2>(result.get());
  if (!err.isReady()) {
    return Error(
        "Failed to read STDERR from the delegate CNI plugin '" +
        delegatePlugin + "' subprocess: " +
        (err.isFailed() ? err.failure() : "discarded"));
  }

  if (status.get() != 0) {
    cerr << "Delegate plugin reported error: " << err.get() << endl;

    return Error(
        "The delegate CNI plugin '" + delegatePlugin +
        "' return status " + stringify(status->get()) +
        ". Could not attach/detach container: " + output.get());
  }

  if (command == spec::CNI_CMD_ADD) {
    // Parse the output of CNI plugin.
    Try<spec::NetworkInfo> parse = spec::parseNetworkInfo(output.get());
    if (parse.isError()) {
      return Error(
          "Failed to parse the output of the delegate CNI plugin '" +
          delegatePlugin + "': " + parse.error());
    }

    return parse.get();
  }

  // For `spec::CNI_CMD_DEL` CNI plugins don't return a result. They
  // will report an error if any, which should be captured above.
  return None();
}

} // namespace cni {
} // namespace slave {
} // namespace internal {
} // namespace mesos {


text:
<?php

namespace WPEloquent\Model\Comment;

class Meta extends \Illuminate\Database\Eloquent\Model {
    protected $table   = 'commentmeta';
    public $timestamps = false;
    protected $fillable = ['meta_key', 'meta_value'];
    protected $primaryKey = 'meta_id';

    public function comment() {
        return $this->belongsTo(\WPEloquent\Model\Comment::class);
    }
}


text:
require 'webrick'

class PackagecloudServer < WEBrick::HTTPServlet::AbstractServlet
  DISTRIBUTIONS = File.read("spec/fixtures/distros.json")
  PACKAGE_CONTENTS = "{\"files\":[{\"filename\":\"jake_1.0.orig.tar.bz2\",\"size\":1108,\"md5sum\":\"a7a309b55424198ee98abcb8092d7be0\"},{\"filename\":\"jake_1.0-7.debian.tar.gz\",\"size\":1571,\"md5sum\":\"0fa5395e95ddf846b419e96575ce8044\"}]}"
  GEM_VERSION = "{\"major\":\"0\",\"minor\":\"2\",\"patch\":\"1\"}"
  REPO = '{"name": "test_repo","created_at": "2014-08-30T03:51:37.000Z","url": "https://packagecloud.io/joedamato/test_repo","last_push_human": "about 2 months ago","package_count_human": "4 packages","private": true,"fqname": "joedamato/test_repo"}'
  REPOS = "[#{REPO}]"
  READ_TOKENS = '{"read_tokens":[{"id":"1","name":"testread","value":"notreal"}]}'

  def json_response(request, response, body)
    response.status = 200
    response['Content-Type'] = "application/json"
    response.body = body
    $request, $response = request, response
  end

  def plain_response(request, response, body)
    response.status = 200
    response['Content-Type'] = "text/plain"
    response.body = body
    $request, $response = request, response
  end

  def created_response(request, response)
    response.status = 201
    response['Content-Type'] = "text/plain"
    response.body = "{}"
    ### Cheap hack to avoid parsing a multipart request here,
    ### instead we just check that the body isn't impossibly small
    if (request.body.size < 1000)
      raise "Request is too small! #{request.body.size}"
    end
    $request, $response = request, response
  end

  def default_response(request, response)
    response.status = 404
    response['Content-Type'] = "text/plain"
    response.body = "nope"
    $request, $response = request, response
  end

  def forbidden_response(request, response)
    response.status = 401
    response['Content-Type'] = "text/plain"
    response.body = "{ 'error': 'unauthenticated' }"

    $request, $response = request, response
  end

  def nocontent_response(request, response)
    response.status = 204
    response['Content-Type'] = 'text/plain'
    response.body = ''
    $request, $response = request, response
  end

  def can_acess?(request)
    request["Authorization"] == "Basic dGVzdF90b2tlbjo="
  end

  def route(request, response)
    return forbidden_response(request, response) unless can_acess?(request)

    path = request.path
    case path
      when "/api/v1/distributions.json"
        json_response(request, response, DISTRIBUTIONS)
      when "/api/v1/repos.json"
        if request.request_method == "GET"
          json_response(request, response, REPOS)
        else
          plain_response(request, response, "{}")
        end
      when "/api/v1/repos/joedamato/test_repo.json"
        json_response(request, response, REPO)
      when "/api/v1/repos/joedamato/test_repo/packages.json"
        created_response(request, response)
      when "/api/v1/repos/joedamato/test_repo/packages/contents.json"
        plain_response(request, response, PACKAGE_CONTENTS)
      when "/api/v1/gem_version.json"
        json_response(request, response, GEM_VERSION)
      when "/api/v1/repos/joedamato/test_repo/master_tokens/test_master_token/read_tokens.json"
        json_response(request, response, READ_TOKENS)
      else
        default_response(request, response)
        $request, $response = request, response
    end
  end

  def do_GET(request, response)
    route(request, response)
  end

  def do_POST(request, response)
    route(request, response)
  end

  def do_DELETE(request, response)
    return forbidden_response(request, response) unless can_acess?(request)

    path = request.path
    case path
      when "/api/v1/repos/joedamato/test_repo/master_tokens/test_master_token/read_tokens/1"
        nocontent_response(request, response)
      else
        default_response(request, response)
    end
  end
end


text:
title: "Fun with Rekall and the ThreatResponse Workstation"
date: 2017-11-26 01:00 UTC
tags: amazon web service aws_ir reinvent ir dfir forensics
blog: "blog"
published: true
---

# ThreatResponse Workstation and Rekall

Recently I've been working on more automation around the use of rekall.
Think "random sampling" and analysis but more effective than the TSA.

Python volatility has always been my go-to for processing live windows Memory
but rekall's shell has a lot to offer as well.  If you follow the ThreatResponse
project you know that we have just released the "ThreatResponse Workstation" in concert
with our friends at ephemeralsystems.com.  Ephemeral Systems provides all commercial
support around aws_ir and the ThreatResponse suite.

In order to use rekall you need three things:

1. A memory dump.  ( _This can be taken with MargaritaShotgun_ )
2. A rekall profile for the kernel variant.  One has been provided as a sample.
3. A rekall environment.  We'll use the one from the remnux project for the sake of simplicity.

> Rekall has a lot of dependencies.  Using a docker container to deliver the environment
makes all of that quite easy.  We want to focus on analysis not on the installation.

## Getting a memory capture

MargaritaShotgun is an incredibly useful tool for memory capture.  It has tons of
great features like jump box support, automatic kernel module resolution, and multipart
S3 upload.  If you type `margariashotgun --help` it can be kind of overwhelming if you've
never used the tool.  __That's why we stress preparation in the IR process.__  Know the tools
or you're going to have a bad time.

Here's an example of just acquiring memory from a single system using margarita shotgun and the
automatic kernel repository.  MargaritaShotgun is provided and installed on the threatresponse workstation.  https://github.com/EphemeralSystems/threatresponse-ws

> You can use our AMIs or build your own.

```bash

# margaritashotgun --server 52.37.143.97 --user ec2-user --key ~/.ssh/id_rsa --filename memcapture.lime --repository

```

## You've got a capture now what?

This is where the _magic_ happens!  Examiner's always say how powerful volatile data can be.
Memory analysis in linux never comes easy.  Under normal circumstances you would have to build a rekall profile.

> __What's rekall profile?__  Rekall profiles tell the the tooling how the variant of the kernel
lays out the memory space so it can do things like extract network connection, process lists, and
more.

The most recent version of the Amazon Linux kernel is `4.9.58-18.55` following the instructions
here: https://github.com/google/rekall/tree/master/tools/linux along with some heavy modifications
to the _Makefile_ I was able to build a profile.

That profile is available here:

https://s3-us-west-2.amazonaws.com/reinvent2018-west.threatresponse.cloud/4.9.58-18.55.amzn1.json

__What are some things you might do with rekall?__

1.  Dump the process tree and examine running processes against known good states.
2.  Dump the network connections like you would with netstat.
3.  Extract all process memory to individual dmp files for carving or running strings against.

__Sample Commands__

```bash

# Loading Rekall

rekall --profile 4.9.58-18.55.amzn1.json -f YOURDUMP.lime

# Getting the process tree

[1] memcapture.lime 03:00:48> pstree

# Getting netstat

[1] memcapture.lime 03:00:48> netstat

# Dumping all processes to individual files

[1] memcapture.lime 03:00:48> memdump

# Running strings ( Note: You need to exit the rekall shell )

$ strings yourfile.dmp | grep -i thing

```

## Jam Challenge Final Format!

Your finally should be in the form of XXXX:XXXX:XXXX:XXXX

Separate them with colons!!

## Video Walkthrough

See a complete video walkthrough here:

https://vimeo.com/244460871


text:
import { workspace } from "vscode";

export const CONFIG_BLOCK_NAME = "rfLanguageServer";

export class Config {
  public static settings = workspace.getConfiguration(CONFIG_BLOCK_NAME);

  public static reloadConfig() {
    Config.settings = workspace.getConfiguration(CONFIG_BLOCK_NAME);
  }

  public static getSettings() {
    Config.reloadConfig();

    return Config.settings;
  }

  /**
   * Returns configured include patterns or default pattern
   */
  public static getInclude() {
    Config.reloadConfig();

    const includePatterns = Config.settings
      ? Config.settings.get<string[]>("includePaths")
      : [];

    return _createGlob(
      includePatterns.length > 0
        ? includePatterns
        : ["**/*.robot", "**/*.resource"]
    );
  }

  public static getExclude() {
    Config.reloadConfig();

    const exlcudePatterns = Config.settings
      ? Config.settings.get<string[]>("excludePaths")
      : [];

    return _createGlob(exlcudePatterns);
  }
}

function _createGlob(patterns: string[]) {
  switch (patterns.length) {
    case 0:
      return "";
    case 1:
      return patterns[0];
    default:
      return `{${patterns.join(",")}}`;
  }
}


text:
using namespace tippbot::application;
using namespace std::chrono_literals;
using namespace tippbot::rating;

uint16_t ScoutApplication::run(ArgumentManager *argument_manager, Configuration *configuration)
{
	LOG(INFO) << "Start learning.";

	Database db;
	LeagueDao league_dao(&db);
	std::vector<uint16_t> years = configuration->scoutYears();
	ResultManager result_manager(configuration->scoutMaxYearDrift());
	sleep_ = argument_manager->getValue("sleep", 0);

	// Load league.
	League *league = nullptr;
	if (argument_manager->has("league"))
	{
		league = league_dao.findById(argument_manager->getValue("league", "").c_str());
	}
	else
	{
		league = league_dao.findAny();
	}

	if (league == nullptr)
	{
		LOG(WARNING) << "No league found.";
		return 1;
	}
	LOG(INFO) << "League: " << league->name();

	std::vector<FeatureInterface*> features = getFeatures(league);

	// Create different weight variants.
	LOG(INFO) << "Calculating variants for " << features.size() << " features (up to " << configuration->scoutMaxWeight() << " in steps of " << configuration->scoutWeightStep() << ")";
	CompositionManager composition_manager(features.size(), configuration->scoutMaxWeight(), configuration->scoutWeightStep());
	size_t count_compositions(composition_manager.remaining());
	LOG(INFO) << "..." << count_compositions;
	LOG(INFO) << "Using max year drift of " << configuration->scoutMaxYearDrift() << "%.";

	// Create threads.
	uint32_t count_threads = argument_manager->getValue("threads", 2);
	std::vector<std::thread> threads(count_threads);
	for (size_t i = 0; i < threads.size(); i++)
	{
		threads[i] = std::thread([this, league, years, &configuration, &composition_manager, &result_manager, &argument_manager]()
		{
			this->scout(league, years, configuration->rating(), composition_manager, result_manager, configuration->drawPrevention(), argument_manager->has("-for-bet"));
		});
	}

	// Write to console, while threads are running.
	// Will return, if there are no more compositions.
	this->writeToConsole(composition_manager, result_manager);

	// Wait for other threads.
	for(size_t i = 0; i < threads.size(); i++)
	{
		threads[i].join();
	}

	// Write best results to new config.
	std::string out_file_name = argument_manager->getValue("out", getDefaultScoutedConfigFileName(result_manager.bestAverageResult()));
	writeConfig(features, composition_manager, result_manager, configuration, out_file_name);

	for(FeatureInterface *feature : features)
	{
		delete feature;
	}
	features.clear();

	LOG(INFO) << "Finished learning.";

	return 0;
}

void ScoutApplication::scout(League *league, std::vector<uint16_t> years, Rating rating_score, CompositionManager &composition_manager, ResultManager &result_manager, float draw_prevention, bool for_bet)
{
	// Features.
	std::vector<FeatureInterface*> features = getFeatures(league);

	PredictionService prediction_service(&features, draw_prevention);
	RatingInterface *rating;
	if (for_bet)
	{
		rating = new BiasRating();
	}
	else
	{
		rating = new ExactResultRating(rating_score);
	}

	// Load encounters.
	std::vector<std::pair<Matchday*, std::vector<Encounter*>>> matchday_encounters;
	mutex_encounter_dao_.lock();
	for (uint16_t year : years)
	{
		for (uint16_t day = 1; day <= 34; day++)
		{
			Matchday* matchday = new Matchday(league, day, year);
			std::vector<Encounter*> encounters = encounter_dao_.getEncounters(matchday);
			matchday_encounters.push_back(std::make_pair(matchday, encounters));
		}
	}
	assert(matchday_encounters.size() == years.size() * league->countMatchdays());
	mutex_encounter_dao_.unlock();

	//TrainingStrategy training_strategy(&result_manager);
	auto start = std::chrono::system_clock::now();
	Composition *composition;
	while ((composition = composition_manager.next()) != nullptr)
	{
		for (size_t feature_index = 0; feature_index < features.size(); feature_index++)
		{
			features[feature_index]->setWeight((*composition)[feature_index]);
		}

		std::map<uint16_t, int16_t> results;
		for (uint16_t year : years)
		{
			results[year] = 0;
		}

		for (auto matchday : matchday_encounters)
		{
			prediction_service.predict(matchday.first, matchday.second);
			results[matchday.first->year()] += rating->rate(matchday.second);
			for (Encounter *encounter : matchday.second)
			{
				delete encounter->predictedResult();
			}
		}
		result_manager.registerResult(results, composition);
	}

	for (FeatureInterface *f : features)
	{
		delete f;
	}
	features.clear();

	delete rating;
}

void ScoutApplication::writeToConsole(CompositionManager &composition_manager, ResultManager &result_manager)
{
	std::cout << "Loading encounters" << std::flush;
	while (result_manager.bestAverageResult() < 1)
	{
		std::this_thread::sleep_for(500ms);
		std::cout << "." << std::flush;
	}
	std::cout << "Done." << std::endl;

	size_t count_compositions(composition_manager.size());
	auto start = std::chrono::high_resolution_clock::now();
	uint32_t progress(0);
	while (progress < 100)
	{
		size_t remaining_compositions = composition_manager.remaining();
		size_t current_composition = count_compositions - remaining_compositions;
		progress = 100.0f / float(count_compositions) * float(current_composition);

		auto now = std::chrono::high_resolution_clock::now();
		std::chrono::duration<double, std::milli> time_done = now - start;

		double rows_per_milli(time_done.count() / current_composition);
		double remaining_millis(remaining_compositions * rows_per_milli);

		int remaining_seconds(remaining_millis / 1000);
		int remaining_minutes(remaining_seconds / 60);
		remaining_seconds -= remaining_minutes * 60;

		std::stringstream year_results;
		if (result_manager.bestAverageResult() > 0)
		{
			for (auto year_result : result_manager.bestResults())
			{
				year_results << year_result.first << "(" << year_result.second << ") ";
			}
		}

		std::cout
			<< "\r "
			<< "[ " << progress << "% ]  "
			<< "[ "
			<< (remaining_minutes < 10 ? "0" : "") << remaining_minutes << "m "
			<< (remaining_seconds < 10 ? "0" : "") << remaining_seconds << "s"
			<< " ] \t Average best: " << result_manager.bestAverageResult()
			<< " " << year_results.str()
			<< "\t Current: " << result_manager.currentResult()
			<< std::flush;
		;

		std::this_thread::sleep_for(500ms);
	}

	std::cout << std::endl;
}

std::vector<Argument> ScoutApplication::arguments()
{
	return {
		Argument("l", "league", "Name of the league (default random)."),
		Argument("t", "threads", "Number of threads used for learning (default 2)."),
		Argument("o", "out", "Name of the configuration file, stored the best configuration in (default named by current date)."),
	};
}

void ScoutApplication::writeConfig(std::vector<FeatureInterface*> &features, CompositionManager &composition_manager, ResultManager &result_manager, Configuration *configuration, std::string &file_name)
{
	if (result_manager.bestAverageResult() > -10000)
	{
		LOG(INFO) << "Writing new configuration to " << file_name << " .";

		// Create comment
		std::stringstream comment_stream;
		comment_stream << "// Scores: ";
		for (auto result : result_manager.bestResults())
		{
			comment_stream << result.second << " in " << result.first << "   ";
		}

		// Set weights
		Json::Value new_feature_config;
		for (size_t i = 0; i < features.size(); i++)
		{
			new_feature_config[features[i]->name()] = result_manager.bestWeights()[i];
		}
		configuration->add("features", new_feature_config, comment_stream.str());
		configuration->write(file_name.c_str());

		LOG(INFO) << "done.";
	}
}

std::vector<FeatureInterface*> ScoutApplication::getFeatures(League *league)
{
	return {
			new CachedFeature<HomeFeature>(),
			// new CachedFeature<SeasonGoalsFeature>(),
			// new CachedFeature<ClashGoalsFeature>(),
			new CachedFeature<LastGamesGoalsFeature>(),
			new CachedFeature<LastHomeAwayGoalsFeature>(),
			new CachedFeature<LastHomeAwayClashGoalsFeature>(),
			new CachedFeature<SeasonHomeAwayGoalsFeature>(),
			//new CachedFeature<BetterTeamFeature>(),
			//new CachedFeature<RankFeature>(),
			//new CachedFeature<UnderdogFeature>(),
			// new CachedFeature<SeasonGoalDifferenceFeature>()
	};
}

std::string ScoutApplication::getDefaultScoutedConfigFileName(float average_score)
{
	time_t rawtime;
	struct std::tm * now;
	time(&rawtime);
	now = localtime(&rawtime);

	std::stringstream config_file;
	config_file
		<< "learned_"
		<< average_score << "_"
		<< now->tm_year + 1900 << "_"
		<< now->tm_mon + 1 << "_"
		<< now->tm_mday << "_"
		<< now->tm_hour << "_"
		<< now->tm_min
		<< ".cfg";

	return config_file.str();
}


text:
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.de/manual/5.3/en/appendixes.configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.0/phpunit.xsd"
         colors="true"
         bootstrap="vendor/autoload.php"
>
    <testsuites>
        <testsuite name="StackChat Test Suite">
            <directory>./tests</directory>
        </testsuite>
    </testsuites>

    <filter>
        <whitelist>
            <directory>./src</directory>
        </whitelist>
    </filter>
</phpunit>


text:
package org.pitest.classpath;

import java.io.IOException;

import org.pitest.classinfo.ClassByteArraySource;
import org.pitest.functional.Option;

public class ClassPathByteArraySource implements ClassByteArraySource {

  private final ClassPath classPath;

  public ClassPathByteArraySource() {
    this(new ClassPath());
  }

  public ClassPathByteArraySource(final ClassPath classPath) {
    this.classPath = classPath;
  }

  public Option<byte[]> getBytes(final String classname) {
    try {
      return Option.some(this.classPath.getClassData(classname));
    } catch (final IOException e) {
      return Option.none();
    }
  }
}


text:
package monitor

import (
	"context"
	"fmt"
	"time"

	"github.com/openshift/origin/pkg/monitor/monitorapi"

	corev1 "k8s.io/api/core/v1"
	informercorev1 "k8s.io/client-go/informers/core/v1"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/tools/cache"
)

func startNodeMonitoring(ctx context.Context, m Recorder, client kubernetes.Interface) {
	nodeChangeFns := []func(node, oldNode *corev1.Node) []monitorapi.Condition{
		func(node, oldNode *corev1.Node) []monitorapi.Condition {
			var conditions []monitorapi.Condition
			for i := range node.Status.Conditions {
				c := &node.Status.Conditions[i]
				previous := findNodeCondition(oldNode.Status.Conditions, c.Type, i)
				if previous == nil {
					continue
				}
				if c.Status != previous.Status {
					conditions = append(conditions, monitorapi.Condition{
						Level:   monitorapi.Warning,
						Locator: monitorapi.NodeLocator(node.Name),
						Message: fmt.Sprintf("condition/%s status/%s reason/%s changed", c.Type, c.Status, c.Reason),
					})
				}
			}
			if node.UID != oldNode.UID {
				conditions = append(conditions, monitorapi.Condition{
					Level:   monitorapi.Error,
					Locator: monitorapi.NodeLocator(node.Name),
					Message: fmt.Sprintf("node was deleted and recreated"),
				})
			}
			return conditions
		},
	}

	nodeInformer := informercorev1.NewNodeInformer(client, time.Hour, nil)
	nodeInformer.AddEventHandler(
		cache.ResourceEventHandlerFuncs{
			AddFunc: func(obj interface{}) {},
			DeleteFunc: func(obj interface{}) {
				node, ok := obj.(*corev1.Node)
				if !ok {
					return
				}
				m.Record(monitorapi.Condition{
					Level:   monitorapi.Warning,
					Locator: monitorapi.NodeLocator(node.Name),
					Message: "deleted",
				})
			},
			UpdateFunc: func(old, obj interface{}) {
				node, ok := obj.(*corev1.Node)
				if !ok {
					return
				}
				oldNode, ok := old.(*corev1.Node)
				if !ok {
					return
				}
				for _, fn := range nodeChangeFns {
					m.Record(fn(node, oldNode)...)
				}
			},
		},
	)

	m.AddSampler(func(now time.Time) []*monitorapi.Condition {
		var conditions []*monitorapi.Condition
		for _, obj := range nodeInformer.GetStore().List() {
			node, ok := obj.(*corev1.Node)
			if !ok {
				continue
			}
			isReady := false
			if c := findNodeCondition(node.Status.Conditions, corev1.NodeReady, 0); c != nil {
				isReady = c.Status == corev1.ConditionTrue
			}
			if !isReady {
				conditions = append(conditions, &monitorapi.Condition{
					Level:   monitorapi.Warning,
					Locator: monitorapi.NodeLocator(node.Name),
					Message: "node is not ready",
				})
			}
		}
		return conditions
	})

	go nodeInformer.Run(ctx.Done())
}


text:
package com.datastax.driver.core.policies;

import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.util.concurrent.TimeUnit;

import static com.datastax.driver.core.Assertions.assertThat;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.mockito.Mockito.when;

public class RollingCountTest {
    @Mock
    Clock clock;

    RollingCount rollingCount;

    @BeforeMethod(groups = "unit")
    public void setup() {
        MockitoAnnotations.initMocks(this);
        assertThat(clock.nanoTime()).isEqualTo(0);
        rollingCount = new RollingCount(clock);
    }

    @Test(groups = "unit")
    public void should_record_adds_in_first_interval() {
        // t = 0
        rollingCount.increment();
        setTime(1, SECONDS);
        rollingCount.increment();
        setTime(2, SECONDS);
        rollingCount.increment();

        // the count does not update in real time...
        assertThat(rollingCount.get()).isEqualTo(0);

        // but only at the end of each 5-second interval
        setTime(5, SECONDS);
        assertThat(rollingCount.get()).isEqualTo(3);
    }

    @Test(groups = "unit")
    public void should_record_adds_over_two_intervals() {
        rollingCount.add(2);

        setTime(5, SECONDS); // 2nd interval
        rollingCount.add(3);

        setTime(10, SECONDS);
        assertThat(rollingCount.get()).isEqualTo(5);
    }

    @Test(groups = "unit")
    public void should_record_adds_separated_by_idle_intervals() {
        rollingCount.add(2);

        setTime(10, SECONDS); // 3rd interval
        rollingCount.add(3);

        setTime(15, SECONDS);
        assertThat(rollingCount.get()).isEqualTo(5);
    }

    @Test(groups = "unit")
    public void should_rotate() {
        // 2 in [0,5[, then 1 every 5 seconds in [5,60[
        rollingCount.add(2);
        for (int i = 1; i < 12; i++) {
            setTime(i * 5, SECONDS);
            rollingCount.add(1);
        }
        setTime(60, SECONDS);
        // the previous minute is now [0,60[
        assertThat(rollingCount.get()).isEqualTo(13);

        rollingCount.add(1);
        setTime(65, SECONDS);
        // the previous minute is now [5,65[, so the 2 events from [0,5[ should be forgotten
        assertThat(rollingCount.get()).isEqualTo(12);
    }

    @Test(groups = "unit")
    public void should_rotate_with_idle_intervals() {
        // 1 every 5 seconds in [0,60[
        for (int i = 0; i < 12; i++) {
            setTime(i * 5, SECONDS);
            rollingCount.add(1);
        }
        // idle in [60, 75[, then 1 in [75,80[
        setTime(75, SECONDS);
        rollingCount.add(1);

        setTime(80, SECONDS);
        // the last minute is [20,80[, with 1 every 5 seconds except during 15 seconds
        assertThat(rollingCount.get()).isEqualTo(9);
    }

    @Test(groups = "unit")
    public void should_rotate_when_idle_for_full_period() {
        // 1 every 5 seconds in [0,60[
        for (int i = 0; i < 12; i++) {
            setTime(i * 5, SECONDS);
            rollingCount.add(1);
        }
        // idle for the next minute [60,120[, then 1 in [120,125[
        setTime(120, SECONDS);
        rollingCount.add(1);

        setTime(125, SECONDS);
        assertThat(rollingCount.get()).isEqualTo(1);
    }

    @Test(groups = "unit")
    public void should_rotate_when_idle_for_more_than_full_period() {
        // 1 every 5 seconds in [0,60[
        for (int i = 0; i < 12; i++) {
            setTime(i * 5, SECONDS);
            rollingCount.add(1);
        }
        // idle for the next minute and 5 seconds [60,125[, then 1 in [125,130[
        setTime(125, SECONDS);
        rollingCount.add(1);

        setTime(130, SECONDS);
        assertThat(rollingCount.get()).isEqualTo(1);
    }

    private void setTime(long time, TimeUnit unit) {
        when(clock.nanoTime()).thenReturn(NANOSECONDS.convert(time, unit));
    }
}


text:
package ast

import (
	"strings"
)

type Type int

const (
	String Type = 1 << iota
	Integer
	Float
	Boolean
	Null
	Resource
	Array
	Object
	Function

	Numeric = Float | Integer
	Unknown = String | Integer | Float | Boolean | Null | Resource | Array | Object | Function
)

var typeMap = map[Type]string{
	String:   "string",
	Integer:  "integer",
	Float:    "float",
	Boolean:  "boolean",
	Null:     "null",
	Resource: "resource",
	Array:    "array",
	Object:   "object",
	Function: "function",
}

func (t Type) Contains(typ Type) bool {
	return t&typ != 0
}

func (t Type) List() []Type {
	list := make([]Type, 0)
	for typ := range typeMap {
		if t.Contains(typ) {
			list = append(list, typ)
		}
	}
	return list
}

func (t Type) String() string {
	typeList := t.List()
	stringList := make([]string, len(typeList))
	for i, typ := range typeList {
		stringList[i] = typeMap[typ]
	}
	return strings.Join(stringList, "|")
}

type KeyType Type

const (
	StringKey  KeyType = KeyType(String)
	IntegerKey KeyType = KeyType(Integer)
)

type ArrayType struct {
	KeyType   KeyType
	ValueType Type
}

type ObjectType struct {
	Class             *Class
	DynamicProperties []Variable
}