<commit_before>#include <gtest/gtest.h>

#include "div_func.hpp"
#include "div_l2.hpp"
#include "div_bc.hpp"
#include "div_renyi.hpp"
#include "div_hellinger.hpp"
#include "dkn.hpp"
#include "np_divs.hpp"

#include <algorithm>

#include <boost/assign/std/vector.hpp>

#include <flann/flann.hpp>
#include <flann/io/hdf5.h>

using namespace boost::assign; // for vector +=
using namespace std;
using namespace NPDivs;

using flann::load_from_file;
using flann::Index;
using flann::IndexParams;
using flann::SearchParams;
using flann::L2;

typedef flann::Matrix<float> Matrix;

namespace {

class NPDivTest : public ::testing::Test {
    protected:

    NPDivTest() : index_params(flann::KDTreeSingleIndexParams()),
                  search_params(SearchParams())
    {}

    virtual ~NPDivTest() {}

    const IndexParams index_params;
    const SearchParams search_params;
};

TEST_F(NPDivTest, DKNTwoD) {
    float d[] = { -2.999, -5.672,
                  -9.051, -1.417,
                   2.066, -0.519,
                  -0.859, -8.354,
                   2.159, -0.470,
                  -5.365, -0.469,
                   9.829,  2.735,
                  -7.356, -9.513,
                  -2.687,  2.312,
                  -9.168, -2.966 };
    Matrix dataset(d, 10, 2);

    float q[] = { -2.920, -9.522,
                   2.363,  6.885,
                   0.963,  4.673,
                   6.671,  0.481 };
    Matrix query(q, 4, 2);

    vector<float> expected;
    expected += 3.8511, 7.3594, 5.2820, 4.6111;

    Index<L2<float> > index(dataset, index_params);
    index.buildIndex();

    vector<float> results = NPDivs::DKN(index, query, 2, search_params);

    for (size_t i = 0; i < expected.size(); i++)
        EXPECT_NEAR(results[i], expected[i], .01);
}

class NPDivGaussiansTest : public NPDivTest {
    typedef NPDivTest super;

    protected:
    NPDivGaussiansTest() :
        fname("test_dists.hdf5"),
        num_groups(2),
        num_per_group(5),
        num_bags(num_groups * num_per_group),
        bags(new Matrix[num_bags]),

        num_df(4),
        expected(new Matrix[num_df])
    {
        // load bags
        boost::format path("gaussian/%d/%d");
        for (size_t group = 0; group < num_groups; group++) {
            for (size_t i = 0; i < num_per_group; i++) {
                load_from_file(
                        bags[group*num_per_group + i],
                        fname, (path % (group+1) % (i+1)).str());
            }
        }

        // specify divergence functions
        div_funcs.push_back(new DivL2());
        div_funcs.push_back(new DivRenyi(.999));
        div_funcs.push_back(new DivHellinger());
        div_funcs.push_back(new DivBC());
        ASSERT_EQ(div_funcs.size(), num_df);

        // load expectations
        for (size_t i = 0; i < num_df; i++) {
            load_from_file(expected[i],
                    fname, "gaussian/divs/" + div_funcs[i].name());
            ASSERT_EQ(expected[i].rows, num_bags);
            ASSERT_EQ(expected[i].cols, num_bags);
        }
    }

    virtual ~NPDivGaussiansTest() {
        // free bags
        for (size_t i = 0; i < num_bags; i++)
            delete[] bags[i].ptr();
        delete[] bags;

        // free expected
        for (size_t i = 0; i < num_df; i++)
            delete[] expected[i].ptr();
        delete[] expected;
    }

    const string fname;
    const size_t num_groups;
    const size_t num_per_group;
    const size_t num_bags;
    Matrix* bags;

    boost::ptr_vector<DivFunc> div_funcs;
    const size_t num_df;
    Matrix* expected;
};

TEST_F(NPDivGaussiansTest, NPDivsGaussiansToSelf) {
    Matrix* results = alloc_matrix_array<float>(num_df, num_bags, num_bags);

    // compute
    np_divs(bags, num_bags, div_funcs, results, 3, index_params, search_params);

    // compare to expectations
    for (size_t df = 0; df < num_df; df++)
        for (size_t i = 0; i < num_bags; i++)
            for (size_t j = 0; j < num_bags; j++)
                EXPECT_NEAR(results[df][i][j], expected[df][i][j], .015)
                    << boost::format("Big difference for df=%d, i=%d, j=%d")
                       % df % i % j;

    free_matrix_array(results, num_df);
}

TEST_F(NPDivGaussiansTest, NPDivGaussiansOneToTwo) {
    Matrix* results =
        alloc_matrix_array<float>(num_df, num_per_group, num_per_group);

    // copy out the upper-right block of expected, which is what we really want
    Matrix* real_expected =
        alloc_matrix_array<float>(num_df, num_per_group, num_per_group);

    for (size_t df = 0; df < num_df; df++)
        for (size_t i = 0; i < num_per_group; i++)
            for (size_t j = 0; j < num_per_group; j++)
                real_expected[df][i][j] = expected[df][i][num_per_group + j];

    // compute
    np_divs(bags, num_per_group, bags + num_per_group, num_per_group,
            div_funcs, results, 3, index_params, search_params);

    // compare to expectations
    for (size_t df = 0; df < num_df; df++)
        for (size_t i = 0; i < num_per_group; i++)
            for (size_t j = 0; j < num_per_group; j++)
                EXPECT_NEAR(results[df][i][j], real_expected[df][i][j], .015)
                    << boost::format("Big difference for df=%d, i=%d, j=%d")
                       % df % i % j;

    free_matrix_array(results, num_df);
    free_matrix_array(real_expected, num_df);
}

} // end namespace

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}
<commit_msg>test with 1, 2, 50 threads for both test cases<commit_after>#include <gtest/gtest.h>

#include "div_func.hpp"
#include "div_l2.hpp"
#include "div_bc.hpp"
#include "div_renyi.hpp"
#include "div_hellinger.hpp"
#include "dkn.hpp"
#include "np_divs.hpp"

#include <algorithm>

#include <boost/assign/std/vector.hpp>

#include <flann/flann.hpp>
#include <flann/io/hdf5.h>

using namespace boost::assign; // for vector +=
using namespace std;
using namespace NPDivs;

using flann::load_from_file;
using flann::Index;
using flann::IndexParams;
using flann::SearchParams;
using flann::L2;

typedef flann::Matrix<float> Matrix;

void expect_near_matrix_array(
    const Matrix *results, const Matrix *expected, size_t num, float dist=.015)
{
    for (size_t n = 0; n < num; n++) {
        const Matrix &m = results[n];
        for (size_t i = 0; i < m.rows; i++)
            for (size_t j = 0; j < m.cols; j++)
                EXPECT_NEAR(results[n][i][j], expected[n][i][j], dist)
                    << boost::format("Big difference for n=%d, i=%d, j=%d")
                       % n % i % j;
    }
}

namespace {

class NPDivTest : public ::testing::Test {
    protected:

    NPDivTest() : index_params(flann::KDTreeSingleIndexParams()),
                  search_params(SearchParams())
    {}

    virtual ~NPDivTest() {}

    const IndexParams index_params;
    const SearchParams search_params;
};

TEST_F(NPDivTest, DKNTwoD) {
    float d[] = { -2.999, -5.672,
                  -9.051, -1.417,
                   2.066, -0.519,
                  -0.859, -8.354,
                   2.159, -0.470,
                  -5.365, -0.469,
                   9.829,  2.735,
                  -7.356, -9.513,
                  -2.687,  2.312,
                  -9.168, -2.966 };
    Matrix dataset(d, 10, 2);

    float q[] = { -2.920, -9.522,
                   2.363,  6.885,
                   0.963,  4.673,
                   6.671,  0.481 };
    Matrix query(q, 4, 2);

    vector<float> expected;
    expected += 3.8511, 7.3594, 5.2820, 4.6111;

    Index<L2<float> > index(dataset, index_params);
    index.buildIndex();

    vector<float> results = NPDivs::DKN(index, query, 2, search_params);

    for (size_t i = 0; i < expected.size(); i++)
        EXPECT_NEAR(results[i], expected[i], .01);
}

class NPDivGaussiansTest : public NPDivTest {
    typedef NPDivTest super;

    protected:
    NPDivGaussiansTest() :
        fname("test_dists.hdf5"),
        num_groups(2),
        num_per_group(5),
        num_bags(num_groups * num_per_group),
        bags(new Matrix[num_bags]),

        num_df(4),
        expected(new Matrix[num_df])
    {
        // load bags
        boost::format path("gaussian/%d/%d");
        for (size_t group = 0; group < num_groups; group++) {
            for (size_t i = 0; i < num_per_group; i++) {
                load_from_file(
                        bags[group*num_per_group + i],
                        fname, (path % (group+1) % (i+1)).str());
            }
        }

        // specify divergence functions
        div_funcs.push_back(new DivL2());
        div_funcs.push_back(new DivRenyi(.999));
        div_funcs.push_back(new DivHellinger());
        div_funcs.push_back(new DivBC());
        ASSERT_EQ(div_funcs.size(), num_df);

        // load expectations
        for (size_t i = 0; i < num_df; i++) {
            load_from_file(expected[i],
                    fname, "gaussian/divs/" + div_funcs[i].name());
            ASSERT_EQ(expected[i].rows, num_bags);
            ASSERT_EQ(expected[i].cols, num_bags);
        }
    }

    virtual ~NPDivGaussiansTest() {
        // free bags
        for (size_t i = 0; i < num_bags; i++)
            delete[] bags[i].ptr();
        delete[] bags;

        // free expected
        for (size_t i = 0; i < num_df; i++)
            delete[] expected[i].ptr();
        delete[] expected;
    }

    const string fname;
    const size_t num_groups;
    const size_t num_per_group;
    const size_t num_bags;
    Matrix* bags;

    boost::ptr_vector<DivFunc> div_funcs;
    const size_t num_df;
    Matrix* expected;
};

TEST_F(NPDivGaussiansTest, NPDivsGaussiansToSelfSingleThreaded) {
    Matrix* results = alloc_matrix_array<float>(num_df, num_bags, num_bags);

    np_divs(bags, num_bags, div_funcs, results, 3,
            index_params, search_params, 1);

    expect_near_matrix_array(results, expected, num_df);

    free_matrix_array(results, num_df);
}

TEST_F(NPDivGaussiansTest, NPDivsGaussiansToSelfTwoThreaded) {
    Matrix* results = alloc_matrix_array<float>(num_df, num_bags, num_bags);

    np_divs(bags, num_bags, div_funcs, results, 3,
            index_params, search_params, 2);

    expect_near_matrix_array(results, expected, num_df);

    free_matrix_array(results, num_df);
}

TEST_F(NPDivGaussiansTest, NPDivsGaussiansToSelfManyThreaded) {
    Matrix* results = alloc_matrix_array<float>(num_df, num_bags, num_bags);

    np_divs(bags, num_bags, div_funcs, results, 3,
            index_params, search_params, 50);

    expect_near_matrix_array(results, expected, num_df);

    free_matrix_array(results, num_df);
}

TEST_F(NPDivGaussiansTest, NPDivGaussiansOneToTwoSingleThreaded) {
    Matrix* results =
        alloc_matrix_array<float>(num_df, num_per_group, num_per_group);

    // copy out the upper-right block of expected, which is what we really want
    Matrix* real_expected =
        alloc_matrix_array<float>(num_df, num_per_group, num_per_group);

    for (size_t df = 0; df < num_df; df++)
        for (size_t i = 0; i < num_per_group; i++)
            for (size_t j = 0; j < num_per_group; j++)
                real_expected[df][i][j] = expected[df][i][num_per_group + j];

    // compute
    np_divs(bags, num_per_group, bags + num_per_group, num_per_group,
            div_funcs, results, 3, index_params, search_params, 1);

    // check
    expect_near_matrix_array(results, real_expected, num_df);

    free_matrix_array(results, num_df);
    free_matrix_array(real_expected, num_df);
}

TEST_F(NPDivGaussiansTest, NPDivGaussiansOneToTwoTwoThreaded) {
    Matrix* results =
        alloc_matrix_array<float>(num_df, num_per_group, num_per_group);

    // copy out the upper-right block of expected, which is what we really want
    Matrix* real_expected =
        alloc_matrix_array<float>(num_df, num_per_group, num_per_group);

    for (size_t df = 0; df < num_df; df++)
        for (size_t i = 0; i < num_per_group; i++)
            for (size_t j = 0; j < num_per_group; j++)
                real_expected[df][i][j] = expected[df][i][num_per_group + j];

    // compute
    np_divs(bags, num_per_group, bags + num_per_group, num_per_group,
            div_funcs, results, 3, index_params, search_params, 2);

    // check
    expect_near_matrix_array(results, real_expected, num_df);

    free_matrix_array(results, num_df);
    free_matrix_array(real_expected, num_df);
}

TEST_F(NPDivGaussiansTest, NPDivGaussiansOneToTwoManyThreaded) {
    Matrix* results =
        alloc_matrix_array<float>(num_df, num_per_group, num_per_group);

    // copy out the upper-right block of expected, which is what we really want
    Matrix* real_expected =
        alloc_matrix_array<float>(num_df, num_per_group, num_per_group);

    for (size_t df = 0; df < num_df; df++)
        for (size_t i = 0; i < num_per_group; i++)
            for (size_t j = 0; j < num_per_group; j++)
                real_expected[df][i][j] = expected[df][i][num_per_group + j];

    // compute
    np_divs(bags, num_per_group, bags + num_per_group, num_per_group,
            div_funcs, results, 3, index_params, search_params, 50);

    // check
    expect_near_matrix_array(results, real_expected, num_df);

    free_matrix_array(results, num_df);
    free_matrix_array(real_expected, num_df);
}

} // end namespace

int main(int argc, char **argv) {
  ::testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}
<|endoftext|>


<commit_before>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: viewcontext.cxx,v $
 *
 *  $Revision: 1.4 $
 *
 *  last change: $Author: hr $ $Date: 2006-06-19 18:13:16 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

#ifndef _SD_XMLVIEWSETTINGSCONTEXT_HXX
#include "viewcontext.hxx"
#endif
#ifndef _SDXMLIMP_IMPL_HXX
#include "sdxmlimp_impl.hxx"
#endif

#ifndef _XMLOFF_XMLTOKEN_HXX
#include "xmltoken.hxx"
#endif
#ifndef _XMLOFF_XMLNMSPE_HXX
#include "xmlnmspe.hxx"
#endif
#ifndef _XMLOFF_NMSPMAP_HXX
#include "nmspmap.hxx"
#endif
#ifndef _XMLOFF_VISAREACONTEXT_HXX
#include "VisAreaContext.hxx"
#endif

using namespace com::sun::star;
using namespace rtl;
using ::xmloff::token::IsXMLToken;

using ::xmloff::token::XML_EMBEDDED_VISIBLE_AREA;

//------------------------------------------------------------------

SdXMLViewSettingsContext::SdXMLViewSettingsContext( SdXMLImport& rImport, USHORT nPrfx, const OUString& rLName, const uno::Reference<xml::sax::XAttributeList>& ) :
    SvXMLImportContext( rImport, nPrfx, rLName )
{
}

SdXMLViewSettingsContext::~SdXMLViewSettingsContext()
{
}

SvXMLImportContext *SdXMLViewSettingsContext::CreateChildContext( USHORT nPrefix,
                                     const OUString& rLocalName,
                                     const ::com::sun::star::uno::Reference<
                                          ::com::sun::star::xml::sax::XAttributeList>& xAttrList )
{
    SvXMLImportContext *pContext = 0;

    if (nPrefix == XML_NAMESPACE_OFFICE)
    {
        if ( IsXMLToken( rLocalName, XML_EMBEDDED_VISIBLE_AREA ) )
        {
            sal_Int16 nMeasureUnit = 0;

            uno::Reference< beans::XPropertySet > xProps( GetImport().GetModel(), uno::UNO_QUERY );
            if( xProps.is() )
                xProps->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "MapUnit" ) ) ) >>= nMeasureUnit;

            pContext = new XMLVisAreaContext(GetImport(), nPrefix, rLocalName, xAttrList, maVisArea, nMeasureUnit);
        }
    }

    if( !pContext )
        pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );

    return pContext;
}

void SdXMLViewSettingsContext::EndElement()
{
    uno::Reference< beans::XPropertySet > xProps( GetImport().GetModel(), uno::UNO_QUERY );
    if( xProps.is() )
    {
        uno::Any aAny;
        aAny <<= maVisArea;

        xProps->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "VisibleArea" ) ), aAny );
    }
}
<commit_msg>INTEGRATION: CWS pchfix02 (1.4.34); FILE MERGED 2006/09/01 17:59:35 kaib 1.4.34.1: #i68856# Added header markers and pch files<commit_after>/*************************************************************************
 *
 *  OpenOffice.org - a multi-platform office productivity suite
 *
 *  $RCSfile: viewcontext.cxx,v $
 *
 *  $Revision: 1.5 $
 *
 *  last change: $Author: obo $ $Date: 2006-09-17 10:30:11 $
 *
 *  The Contents of this file are made available subject to
 *  the terms of GNU Lesser General Public License Version 2.1.
 *
 *
 *    GNU Lesser General Public License Version 2.1
 *    =============================================
 *    Copyright 2005 by Sun Microsystems, Inc.
 *    901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License version 2.1, as published by the Free Software Foundation.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 *
 *    You should have received a copy of the GNU Lesser General Public
 *    License along with this library; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *    MA  02111-1307  USA
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_xmloff.hxx"

#ifndef _SD_XMLVIEWSETTINGSCONTEXT_HXX
#include "viewcontext.hxx"
#endif
#ifndef _SDXMLIMP_IMPL_HXX
#include "sdxmlimp_impl.hxx"
#endif

#ifndef _XMLOFF_XMLTOKEN_HXX
#include "xmltoken.hxx"
#endif
#ifndef _XMLOFF_XMLNMSPE_HXX
#include "xmlnmspe.hxx"
#endif
#ifndef _XMLOFF_NMSPMAP_HXX
#include "nmspmap.hxx"
#endif
#ifndef _XMLOFF_VISAREACONTEXT_HXX
#include "VisAreaContext.hxx"
#endif

using namespace com::sun::star;
using namespace rtl;
using ::xmloff::token::IsXMLToken;

using ::xmloff::token::XML_EMBEDDED_VISIBLE_AREA;

//------------------------------------------------------------------

SdXMLViewSettingsContext::SdXMLViewSettingsContext( SdXMLImport& rImport, USHORT nPrfx, const OUString& rLName, const uno::Reference<xml::sax::XAttributeList>& ) :
    SvXMLImportContext( rImport, nPrfx, rLName )
{
}

SdXMLViewSettingsContext::~SdXMLViewSettingsContext()
{
}

SvXMLImportContext *SdXMLViewSettingsContext::CreateChildContext( USHORT nPrefix,
                                     const OUString& rLocalName,
                                     const ::com::sun::star::uno::Reference<
                                          ::com::sun::star::xml::sax::XAttributeList>& xAttrList )
{
    SvXMLImportContext *pContext = 0;

    if (nPrefix == XML_NAMESPACE_OFFICE)
    {
        if ( IsXMLToken( rLocalName, XML_EMBEDDED_VISIBLE_AREA ) )
        {
            sal_Int16 nMeasureUnit = 0;

            uno::Reference< beans::XPropertySet > xProps( GetImport().GetModel(), uno::UNO_QUERY );
            if( xProps.is() )
                xProps->getPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "MapUnit" ) ) ) >>= nMeasureUnit;

            pContext = new XMLVisAreaContext(GetImport(), nPrefix, rLocalName, xAttrList, maVisArea, nMeasureUnit);
        }
    }

    if( !pContext )
        pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );

    return pContext;
}

void SdXMLViewSettingsContext::EndElement()
{
    uno::Reference< beans::XPropertySet > xProps( GetImport().GetModel(), uno::UNO_QUERY );
    if( xProps.is() )
    {
        uno::Any aAny;
        aAny <<= maVisArea;

        xProps->setPropertyValue( OUString( RTL_CONSTASCII_USTRINGPARAM( "VisibleArea" ) ), aAny );
    }
}
<|endoftext|>


<commit_before>#include<stdio.h>
#include<stdlib.h>
#include "pin.H"
#include <iostream>
#include <fstream>
#include <tr1/unordered_map>
#include <string>
#define PADSIZE 56

ofstream OutFile;
FILE * trace;
PIN_LOCK lock;
UINT32 cread = 0;
UINT32 cwrite = 0;
int c = 0;
INT32 numThreads = 0;
int dataRaces = 0;
int check = -1;
UINT32 id  = 0;
int slock = 0;
int sulock = 0;
int unq = 0;
int wunique = 0;
int numLocks = 0;
bool race = false;
VOID* raceLocation;

class thread_data_t
{
public:
  thread_data_t() : _countR(0), _countW(0) {}
  UINT64 _countR;
  UINT64 _countW;
   UINT8 _pad[PADSIZE];

};

class v_clock{
public:

  int* c;
};

class rw_clock{
public:
  int type;
  VOID* addr;
  int* c;
};

std::tr1::unordered_map<THREADID,v_clock*> threadhashtable;
std::tr1::unordered_map<VOID*,v_clock*> readhashtable;
std::tr1::unordered_map<VOID*,v_clock*> writehashtable;
std::tr1::unordered_map<VOID*,v_clock*> lockhashtable;

static TLS_KEY tls_key;

thread_data_t* get_tls(THREADID threadid)
{
  thread_data_t* tdata = static_cast<thread_data_t*>(PIN_GetThreadData(tls_key, threadid));
    return tdata;
}

v_clock* create_clock()
{
  v_clock* clock = new v_clock;

  clock->c = new int[3];
  clock->c[0]= 0;
  clock->c[1]=0;
  clock->c[2]=0;

  return clock;
}

//increase read count of thread by 1 and check if there is a read-write data race
VOID docountR(VOID*addr,THREADID threadid){
  thread_data_t* tdata = get_tls(threadid);
  tdata->_countR++;

  c++;

  std::tr1::unordered_map<VOID*,v_clock*>::const_iterator readgot = readhashtable.find(addr);
    std::tr1::unordered_map<THREADID,v_clock*>::const_iterator threadgot = threadhashtable.find(threadid);
    std::tr1::unordered_map<VOID*,v_clock*>::const_iterator writegot = writehashtable.find(addr);
    v_clock *clock = new v_clock;
    clock->c = new int[3];

    if(readgot==readhashtable.end()){
        clock->c[0] = threadgot->second->c[0];
        clock->c[1] = threadgot->second->c[1];
        clock->c[2] = threadgot->second->c[2];

        unq++;

        readhashtable.insert(std::make_pair<VOID*,v_clock*>(addr,clock));

  }
  else{

      if(writegot == writehashtable.end()){

          for(int i = 0; i < 3; i++){
              if(threadgot->second->c[i] > readgot->second->c[i]){

              readgot->second->c[i] = threadgot->second->c[i];

              }
          }

      }
      else{

          for(int i = 0; i < 3; i++){
              if(writegot->second->c[i] > threadgot->second->c[i]){
                  if(race==false){
                  dataRaces++;
                    race = true;
                      raceLocation = addr;

                  break;
                  }
              }
          }

          for(int i = 0; i < 3;i++){
              if(threadgot->second->c[i] > readgot->second->c[i]){
                  readgot->second->c[i] = threadgot->second->c[i];
              }

          }

      }

  }

}

//increase write count of thread by 1 and checj if there is a write-write data race
VOID docountW(VOID*addr,THREADID threadid){
  thread_data_t* tdata = get_tls(threadid);
  tdata->_countW++;
  c++;

  std::tr1::unordered_map<VOID*,v_clock*>::const_iterator writegot = writehashtable.find(addr);
    std::tr1::unordered_map<THREADID,v_clock*>::const_iterator threadgot = threadhashtable.find(threadid);
    v_clock *clock = new v_clock;
    clock->c = new int[3];
  if(writegot==writehashtable.end()){
      clock->c[0] = threadgot->second->c[0];
      clock->c[1] = threadgot->second->c[1];
      clock->c[2] = threadgot->second->c[2];

      wunique++;

      writehashtable.insert(std::make_pair<VOID*,v_clock*>(addr,clock));

  }else{

      //address already has write clock created
      for(int i = 0; i < 3;i++){

          if(writegot->second->c[i] > threadgot->second->c[i]){
              //write-write data race
              if(race == false){
              dataRaces++;
              race = true;
                  raceLocation = addr;

              break;
              }
          }

      }

      for(int i = 0; i < 3;i++){
          if(threadgot->second->c[i] > writegot->second->c[i]){
              writegot->second->c[i] = threadgot->second->c[i];
          }

      }

  }

}

VOID CreateLockClock(VOID *ptr){
        v_clock *clock = create_clock();
    std::tr1::unordered_map<VOID*,v_clock*>::const_iterator lockgot = lockhashtable.find(ptr);
    if(lockgot == lockhashtable.end()){
        numLocks++;
        lockhashtable.insert(std::make_pair<VOID*,v_clock*>(ptr,clock));
    }

}

VOID AcquireLock(VOID*ptr,THREADID threadid){

  std::tr1::unordered_map<VOID*,v_clock*>::const_iterator lockgot = lockhashtable.find(ptr);
std::tr1::unordered_map<THREADID,v_clock*>::const_iterator threadgot = threadhashtable.find(threadid);

    slock++;
       if(lockgot != lockhashtable.end()){

        for(int i = 0; i < 3; i++){
            if(lockgot->second->c[i] > threadgot->second->c[i]){
                threadgot->second->c[i]  = lockgot->second->c[i];
            }
        }
    }

}

VOID ReleaseLock(VOID*ptr,THREADID threadid){

  std::tr1::unordered_map<THREADID,v_clock*>::const_iterator threadgot = threadhashtable.find(threadid);
  std::tr1::unordered_map<VOID*,v_clock*>::const_iterator lockgot = lockhashtable.find(ptr);

    sulock++;

    if(lockgot!=lockhashtable.end()){

        for(int i =0; i < 3; i++){
        if(threadgot->second->c[i] > lockgot->second->c[i]){
            lockgot->second->c[i] = threadgot->second->c[i];

        }
    }

    }
    else if(lockgot == lockhashtable.end()){

    }

        threadgot->second->c[threadid] +=1;

}

VOID ThreadStart(THREADID threadid, CONTEXT *ctx, INT32 flags, VOID *v){
  PIN_GetLock(&lock, threadid+1);

  numThreads++;
  v_clock *clock = create_clock();
   v_clock *mainclock = new v_clock;
    mainclock->c = new int[3];
    std::tr1::unordered_map<THREADID,v_clock*>::const_iterator got = threadhashtable.find(threadid);
 std::tr1::unordered_map<THREADID,v_clock*>::const_iterator gotagain =threadhashtable.find(0);
    if(got==threadhashtable.end()){

        if(threadid!=0){

            //vector clock of thread is max of main thread and <0,0,0>
            mainclock->c[0] = gotagain->second->c[0];
            mainclock->c[1] = gotagain->second->c[1];
            mainclock->c[2] = gotagain->second->c[2];

            mainclock->c[threadid]+=1;

        threadhashtable.insert(std::make_pair<THREADID,v_clock*>(threadid,mainclock));

            //update main thread vector clock
            gotagain->second->c[0]+=1;

        }else{
            //create main thread vector clock

            threadhashtable.insert(std::make_pair<THREADID,v_clock*>(threadid,clock));

        }
    }

  PIN_ReleaseLock(&lock);
  thread_data_t* tdata = new thread_data_t;
  PIN_SetThreadData(tls_key, tdata, threadid);
}
VOID ThreadFini(THREADID threadid, const CONTEXT *ctxt, INT32 code, VOID *v){
  PIN_GetLock(&lock, threadid+1);

  PIN_ReleaseLock(&lock);

}

VOID Trace(TRACE trace, VOID *v){

  for (BBL bbl = TRACE_BblHead(trace); BBL_Valid(bbl);bbl = BBL_Next(bbl)){

    for ( INS ins = BBL_InsHead(bbl); INS_Valid(ins); ins = INS_Next(ins))
      {
	UINT32 memOperands = INS_MemoryOperandCount(ins);

	for (UINT32 memOp = 0; memOp < memOperands; memOp++)
	  {
	    if (INS_MemoryOperandIsRead(ins, memOp))
	      {
INS_InsertPredicatedCall(
			 ins, IPOINT_BEFORE, (AFUNPTR)docountR,IARG_MEMORYOP_EA,memOp,IARG_THREAD_ID,
       				 IARG_END);

	      }
	else  if (INS_MemoryOperandIsWritten(ins, memOp))
	      {
		INS_InsertPredicatedCall(
					 ins, IPOINT_BEFORE, (AFUNPTR)docountW,IARG_MEMORYOP_EA,memOp,
					 IARG_THREAD_ID,
					 IARG_END);

	      }
	  }
      }
  }
}

// Is called for every instruction and instruments reads and writes

VOID ImageLoad(IMG img, VOID *){

  RTN rtn = RTN_FindByName(img, "_ShreyasAcquired");
  if( RTN_Valid(rtn)){
    RTN_Open(rtn);

       RTN_InsertCall(rtn,IPOINT_BEFORE,(AFUNPTR)CreateLockClock,IARG_FUNCARG_ENTRYPOINT_VALUE,0,IARG_END);
      RTN_InsertCall(rtn,IPOINT_BEFORE,(AFUNPTR)AcquireLock,IARG_FUNCARG_ENTRYPOINT_VALUE,0,IARG_THREAD_ID,IARG_END);
      RTN_Close(rtn);

  }

    RTN wRtn = RTN_FindByName(img,"_deposit");
    if(RTN_Valid(wRtn)){
        RTN_Open(wRtn);
        for(INS ins1 = RTN_InsHead(wRtn);INS_Valid(ins1); ins1 = INS_Next(ins1)){
            UINT32 mems = INS_MemoryOperandCount(ins1);
            for (UINT32 mem = 0; mem < mems; mem++){
                if(INS_MemoryOperandIsRead(ins1,mem)){
                    INS_InsertPredicatedCall(ins1,IPOINT_BEFORE,(AFUNPTR)docountR,IARG_MEMORYOP_EA,mem,IARG_THREAD_ID,IARG_END);
                }
                if(INS_MemoryOperandIsWritten(ins1,mem)){
                    INS_InsertPredicatedCall(ins1,IPOINT_BEFORE,(AFUNPTR)docountW,IARG_MEMORYOP_EA,mem,IARG_THREAD_ID,IARG_END);
                }
            }
        }
        RTN_Close(wRtn);
    }

    RTN dRtn = RTN_FindByName(img,"_withdraw");
    if(RTN_Valid(dRtn)){
        RTN_Open(dRtn);
        for(INS ins1 = RTN_InsHead(dRtn);INS_Valid(ins1); ins1 = INS_Next(ins1)){
            UINT32 mems = INS_MemoryOperandCount(ins1);
            for (UINT32 mem = 0; mem < mems; mem++){
                if(INS_MemoryOperandIsRead(ins1,mem)){
                    INS_InsertPredicatedCall(ins1,IPOINT_BEFORE,(AFUNPTR)docountR,IARG_MEMORYOP_EA,mem,IARG_THREAD_ID,IARG_END);
                }
            if(INS_MemoryOperandIsWritten(ins1,mem)){
                    INS_InsertPredicatedCall(ins1,IPOINT_BEFORE,(AFUNPTR)docountW,IARG_MEMORYOP_EA,mem,IARG_THREAD_ID,IARG_END);
                }
            }
        }
        RTN_Close(dRtn);
    }

    RTN mRtn = RTN_FindByName(img,"_mainFunc");
    if(RTN_Valid(mRtn)){
        RTN_Open(mRtn);
        for(INS ins1 = RTN_InsHead(mRtn);INS_Valid(ins1); ins1 = INS_Next(ins1)){
            UINT32 mems = INS_MemoryOperandCount(ins1);
            for (UINT32 mem = 0; mem < mems; mem++){
                if(INS_MemoryOperandIsRead(ins1,mem)){
                    INS_InsertPredicatedCall(ins1,IPOINT_BEFORE,(AFUNPTR)docountR,IARG_MEMORYOP_EA,mem,IARG_THREAD_ID,IARG_END);
                }
                if(INS_MemoryOperandIsWritten(ins1,mem)){
                    INS_InsertPredicatedCall(ins1,IPOINT_BEFORE,(AFUNPTR)docountW,IARG_MEMORYOP_EA,mem,IARG_THREAD_ID,IARG_END);

                }
            }

        }
        RTN_Close(mRtn);
    }

    RTN rtn1 = RTN_FindByName(img,"_ShreyasUnlock");
    if(RTN_Valid(rtn1)){
        RTN_Open(rtn1);

        RTN_InsertCall(rtn1,IPOINT_BEFORE,(AFUNPTR)ReleaseLock,IARG_FUNCARG_ENTRYPOINT_VALUE,0,IARG_THREAD_ID,IARG_END);

        RTN_Close(rtn1);
    }

}

KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool", "o", "pinatrace.out", "specify output file name");

VOID Fini(INT32 code, VOID *v)
{

      if(dataRaces == 0){
        OutFile << "Race: NO" << endl;
    }
    else{
        OutFile << "Race: YES, at: " << raceLocation << endl;
    }

    OutFile << "done" << endl;

  OutFile.close();

}

/* ===================================================================== */
/* Print Help Message                                                    */
/* ===================================================================== */

INT32 Usage()
{
    PIN_ERROR( "This Pintool finds data races in C++ programs\n"
              + KNOB_BASE::StringKnobSummary() + "\n");
    return -1;
}

/* ===================================================================== */
/* Main                                                                  */
/* ===================================================================== */

int main(int argc, char *argv[])
{
    PIN_InitLock(&lock);

    if (PIN_Init(argc, argv)) return Usage();
    PIN_InitSymbols();

     OutFile.open(KnobOutputFile.Value().c_str());
    //trace = fopen("pinatrace.out","w");

    tls_key = PIN_CreateThreadDataKey(0);

    PIN_AddThreadStartFunction(ThreadStart,0);

     IMG_AddInstrumentFunction(ImageLoad, 0);

    PIN_AddFiniFunction(Fini, 0);

    // Never returns
    PIN_StartProgram();

    return 0;
}
<commit_msg>Code clean-up<commit_after>#include<stdio.h>
#include<stdlib.h>
#include "pin.H"
#include <iostream>
#include <fstream>
#include <tr1/unordered_map>
#include <string>
#define PADSIZE 56

ofstream OutFile;
FILE * trace;
PIN_LOCK lock;
UINT32 cread = 0;
UINT32 cwrite = 0;
int c = 0;
INT32 numThreads = 0;
int dataRaces = 0;
int check = -1;
UINT32 id  = 0;
int slock = 0;
int sulock = 0;
int unq = 0;
int wunique = 0;
int numLocks = 0;
bool race = false;
VOID* raceLocation;

class thread_data_t
{
public:
  thread_data_t() : _countR(0), _countW(0) {}
  UINT64 _countR;
  UINT64 _countW;
   UINT8 _pad[PADSIZE];

};

class v_clock{
public:

  int* c;
};

class rw_clock{
public:
  int type;
  VOID* addr;
  int* c;
};

std::tr1::unordered_map<THREADID,v_clock*> threadhashtable;
std::tr1::unordered_map<VOID*,v_clock*> readhashtable;
std::tr1::unordered_map<VOID*,v_clock*> writehashtable;
std::tr1::unordered_map<VOID*,v_clock*> lockhashtable;

static TLS_KEY tls_key;

thread_data_t* get_tls(THREADID threadid)
{
  thread_data_t* tdata = static_cast<thread_data_t*>(PIN_GetThreadData(tls_key, threadid));
    return tdata;
}

v_clock* create_clock()
{
  v_clock* clock = new v_clock;

  clock->c = new int[3];
  clock->c[0]= 0;
  clock->c[1]=0;
  clock->c[2]=0;

  return clock;
}

//increase read count of thread by 1 and check if there is a read-write data race
VOID docountR(VOID*addr,THREADID threadid){
  thread_data_t* tdata = get_tls(threadid);
  tdata->_countR++;

  c++;

  std::tr1::unordered_map<VOID*,v_clock*>::const_iterator readgot = readhashtable.find(addr);
    std::tr1::unordered_map<THREADID,v_clock*>::const_iterator threadgot = threadhashtable.find(threadid);
    std::tr1::unordered_map<VOID*,v_clock*>::const_iterator writegot = writehashtable.find(addr);
    v_clock *clock = new v_clock;
    clock->c = new int[3];

    if(readgot==readhashtable.end()){
        clock->c[0] = threadgot->second->c[0];
        clock->c[1] = threadgot->second->c[1];
        clock->c[2] = threadgot->second->c[2];

        unq++;

        readhashtable.insert(std::make_pair<VOID*,v_clock*>(addr,clock));

  }
  else{

      if(writegot == writehashtable.end()){

          for(int i = 0; i < 3; i++){
              if(threadgot->second->c[i] > readgot->second->c[i]){

              readgot->second->c[i] = threadgot->second->c[i];

              }
          }

      }
      else{

          for(int i = 0; i < 3; i++){
              if(writegot->second->c[i] > threadgot->second->c[i]){
                  if(race==false){
                  dataRaces++;
                    race = true;
                      raceLocation = addr;

                  break;
                  }
              }
          }

          for(int i = 0; i < 3;i++){
              if(threadgot->second->c[i] > readgot->second->c[i]){
                  readgot->second->c[i] = threadgot->second->c[i];
              }

          }

      }

  }

}

//increase write count of thread by 1 and checj if there is a write-write data race
VOID docountW(VOID*addr,THREADID threadid){
  thread_data_t* tdata = get_tls(threadid);
  tdata->_countW++;
  c++;

  std::tr1::unordered_map<VOID*,v_clock*>::const_iterator writegot = writehashtable.find(addr);
    std::tr1::unordered_map<THREADID,v_clock*>::const_iterator threadgot = threadhashtable.find(threadid);
    v_clock *clock = new v_clock;
    clock->c = new int[3];
  if(writegot==writehashtable.end()){
      clock->c[0] = threadgot->second->c[0];
      clock->c[1] = threadgot->second->c[1];
      clock->c[2] = threadgot->second->c[2];

      wunique++;

      writehashtable.insert(std::make_pair<VOID*,v_clock*>(addr,clock));

  }else{

      //address already has write clock created
      for(int i = 0; i < 3;i++){

          if(writegot->second->c[i] > threadgot->second->c[i]){
              //write-write data race
              if(race == false){
              dataRaces++;
              race = true;
                  raceLocation = addr;

              break;
              }
          }

      }

      for(int i = 0; i < 3;i++){
          if(threadgot->second->c[i] > writegot->second->c[i]){
              writegot->second->c[i] = threadgot->second->c[i];
          }

      }

  }

}

VOID CreateLockClock(VOID *ptr){
    v_clock *clock = create_clock();
    std::tr1::unordered_map<VOID*,v_clock*>::const_iterator lockgot = lockhashtable.find(ptr);
    if(lockgot == lockhashtable.end()){
        numLocks++;
        lockhashtable.insert(std::make_pair<VOID*,v_clock*>(ptr,clock));
    }

}

VOID AcquireLock(VOID*ptr,THREADID threadid){

	std::tr1::unordered_map<VOID*,v_clock*>::const_iterator lockgot = lockhashtable.find(ptr);
	std::tr1::unordered_map<THREADID,v_clock*>::const_iterator threadgot = threadhashtable.find(threadid);

    	slock++;
       	if(lockgot != lockhashtable.end()){

        	for(int i = 0; i < 3; i++){
            		if(lockgot->second->c[i] > threadgot->second->c[i]){
                		threadgot->second->c[i]  = lockgot->second->c[i];
            		}
        	}
	 }

}

VOID ReleaseLock(VOID*ptr,THREADID threadid){

  	std::tr1::unordered_map<THREADID,v_clock*>::const_iterator threadgot = threadhashtable.find(threadid);
  	std::tr1::unordered_map<VOID*,v_clock*>::const_iterator lockgot = lockhashtable.find(ptr);

    	sulock++;

    	if(lockgot!=lockhashtable.end()){

        for(int i =0; i < 3; i++){
        	if(threadgot->second->c[i] > lockgot->second->c[i]){
            		lockgot->second->c[i] = threadgot->second->c[i];

        	}
    	}

    }

    threadgot->second->c[threadid] +=1;

}

VOID ThreadStart(THREADID threadid, CONTEXT *ctx, INT32 flags, VOID *v){
  PIN_GetLock(&lock, threadid+1);

  	numThreads++;
  	v_clock *clock = create_clock();
	 v_clock *mainclock = new v_clock;
    	mainclock->c = new int[3];
    	std::tr1::unordered_map<THREADID,v_clock*>::const_iterator got = threadhashtable.find(threadid);
 	std::tr1::unordered_map<THREADID,v_clock*>::const_iterator gotagain =threadhashtable.find(0);
    	if(got==threadhashtable.end()){

        	if(threadid!=0){

            		//vector clock of thread is max of main thread and <0,0,0>
            		mainclock->c[0] = gotagain->second->c[0];
            		mainclock->c[1] = gotagain->second->c[1];
            		mainclock->c[2] = gotagain->second->c[2];

            		mainclock->c[threadid]+=1;

        		threadhashtable.insert(std::make_pair<THREADID,v_clock*>(threadid,mainclock));

            		//update main thread vector clock
            		gotagain->second->c[0]+=1;

        	}else{
            //create main thread vector clock

            		threadhashtable.insert(std::make_pair<THREADID,v_clock*>(threadid,clock));

        	}
    }

  PIN_ReleaseLock(&lock);
  thread_data_t* tdata = new thread_data_t;
  PIN_SetThreadData(tls_key, tdata, threadid);
}
VOID ThreadFini(THREADID threadid, const CONTEXT *ctxt, INT32 code, VOID *v){
  PIN_GetLock(&lock, threadid+1);

  PIN_ReleaseLock(&lock);

}

VOID Trace(TRACE trace, VOID *v){

  for (BBL bbl = TRACE_BblHead(trace); BBL_Valid(bbl);bbl = BBL_Next(bbl)){

    for ( INS ins = BBL_InsHead(bbl); INS_Valid(ins); ins = INS_Next(ins))
      {
	UINT32 memOperands = INS_MemoryOperandCount(ins);

	for (UINT32 memOp = 0; memOp < memOperands; memOp++)
	  {
	    if (INS_MemoryOperandIsRead(ins, memOp))
	      {
INS_InsertPredicatedCall(
			 ins, IPOINT_BEFORE, (AFUNPTR)docountR,IARG_MEMORYOP_EA,memOp,IARG_THREAD_ID,
       				 IARG_END);

	      }
	else  if (INS_MemoryOperandIsWritten(ins, memOp))
	      {
		INS_InsertPredicatedCall(
					 ins, IPOINT_BEFORE, (AFUNPTR)docountW,IARG_MEMORYOP_EA,memOp,
					 IARG_THREAD_ID,
					 IARG_END);

	      }
	  }
      }
  }
}

// Is called for every instruction and instruments reads and writes

VOID ImageLoad(IMG img, VOID *){

  RTN rtn = RTN_FindByName(img, "_ShreyasAcquired");
  if( RTN_Valid(rtn)){
    RTN_Open(rtn);

       RTN_InsertCall(rtn,IPOINT_BEFORE,(AFUNPTR)CreateLockClock,IARG_FUNCARG_ENTRYPOINT_VALUE,0,IARG_END);
      RTN_InsertCall(rtn,IPOINT_BEFORE,(AFUNPTR)AcquireLock,IARG_FUNCARG_ENTRYPOINT_VALUE,0,IARG_THREAD_ID,IARG_END);
      RTN_Close(rtn);

  }

    RTN wRtn = RTN_FindByName(img,"_deposit");
    if(RTN_Valid(wRtn)){
        RTN_Open(wRtn);
        for(INS ins1 = RTN_InsHead(wRtn);INS_Valid(ins1); ins1 = INS_Next(ins1)){
            UINT32 mems = INS_MemoryOperandCount(ins1);
            for (UINT32 mem = 0; mem < mems; mem++){
                if(INS_MemoryOperandIsRead(ins1,mem)){
                    INS_InsertPredicatedCall(ins1,IPOINT_BEFORE,(AFUNPTR)docountR,IARG_MEMORYOP_EA,mem,IARG_THREAD_ID,IARG_END);
                }
                if(INS_MemoryOperandIsWritten(ins1,mem)){
                    INS_InsertPredicatedCall(ins1,IPOINT_BEFORE,(AFUNPTR)docountW,IARG_MEMORYOP_EA,mem,IARG_THREAD_ID,IARG_END);
                }
            }
        }
        RTN_Close(wRtn);
    }

    RTN dRtn = RTN_FindByName(img,"_withdraw");
    if(RTN_Valid(dRtn)){
        RTN_Open(dRtn);
        for(INS ins1 = RTN_InsHead(dRtn);INS_Valid(ins1); ins1 = INS_Next(ins1)){
            UINT32 mems = INS_MemoryOperandCount(ins1);
            for (UINT32 mem = 0; mem < mems; mem++){
                if(INS_MemoryOperandIsRead(ins1,mem)){
                    INS_InsertPredicatedCall(ins1,IPOINT_BEFORE,(AFUNPTR)docountR,IARG_MEMORYOP_EA,mem,IARG_THREAD_ID,IARG_END);
                }
            if(INS_MemoryOperandIsWritten(ins1,mem)){
                    INS_InsertPredicatedCall(ins1,IPOINT_BEFORE,(AFUNPTR)docountW,IARG_MEMORYOP_EA,mem,IARG_THREAD_ID,IARG_END);
                }
            }
        }
        RTN_Close(dRtn);
    }

    RTN mRtn = RTN_FindByName(img,"_mainFunc");
    if(RTN_Valid(mRtn)){
        RTN_Open(mRtn);
        for(INS ins1 = RTN_InsHead(mRtn);INS_Valid(ins1); ins1 = INS_Next(ins1)){
            UINT32 mems = INS_MemoryOperandCount(ins1);
            for (UINT32 mem = 0; mem < mems; mem++){
                if(INS_MemoryOperandIsRead(ins1,mem)){
                    INS_InsertPredicatedCall(ins1,IPOINT_BEFORE,(AFUNPTR)docountR,IARG_MEMORYOP_EA,mem,IARG_THREAD_ID,IARG_END);
                }
                if(INS_MemoryOperandIsWritten(ins1,mem)){
                    INS_InsertPredicatedCall(ins1,IPOINT_BEFORE,(AFUNPTR)docountW,IARG_MEMORYOP_EA,mem,IARG_THREAD_ID,IARG_END);

                }
            }

        }
        RTN_Close(mRtn);
    }

    RTN rtn1 = RTN_FindByName(img,"_ShreyasUnlock");
    if(RTN_Valid(rtn1)){
        RTN_Open(rtn1);

        RTN_InsertCall(rtn1,IPOINT_BEFORE,(AFUNPTR)ReleaseLock,IARG_FUNCARG_ENTRYPOINT_VALUE,0,IARG_THREAD_ID,IARG_END);

        RTN_Close(rtn1);
    }

}

KNOB<string> KnobOutputFile(KNOB_MODE_WRITEONCE, "pintool", "o", "pinatrace.out", "specify output file name");

VOID Fini(INT32 code, VOID *v)
{

      if(dataRaces == 0){
        OutFile << "Race: NO" << endl;
    }
    else{
        OutFile << "Race: YES, at: " << raceLocation << endl;
    }

    OutFile << "done" << endl;

  OutFile.close();

}

/* ===================================================================== */
/* Print Help Message                                                    */
/* ===================================================================== */

INT32 Usage()
{
    PIN_ERROR( "This Pintool finds data races in C++ programs\n"
              + KNOB_BASE::StringKnobSummary() + "\n");
    return -1;
}

/* ===================================================================== */
/* Main                                                                  */
/* ===================================================================== */

int main(int argc, char *argv[])
{

    PIN_InitLock(&lock);

    if (PIN_Init(argc, argv)) return Usage();
    PIN_InitSymbols();

    OutFile.open(KnobOutputFile.Value().c_str());
    //trace = fopen("pinatrace.out","w");

    tls_key = PIN_CreateThreadDataKey(0);

    PIN_AddThreadStartFunction(ThreadStart,0);

    IMG_AddInstrumentFunction(ImageLoad, 0);

    PIN_AddFiniFunction(Fini, 0);

    // Never returns
    PIN_StartProgram();

    return 0;
}
<|endoftext|>


<commit_before>#ifndef __CalculateOverlap_CXX__
#define __CalculateOverlap_CXX__

#include "itkImageFileReader.h"
#include "itkImage.h"

#include "itkImageRegionConstIterator.h"
#include "itkListSample.h"
#include "itkListSampleToHistogramGenerator.h"
#include "itkHistogramToEntropyImageFilter.h"

//-------------------------------------------------------------------------------------

int main( int argc, char *argv[] )
{
	/** Declare stuff. */
	std::string imageFileName = "";
	std::string maskFileName = "";
	bool useInsideMask = true;
	unsigned int numberOfBins = 0;
	double sumOfSquaredVoxels = 0.0;
	unsigned int numberOfVoxels = 0;

	/** Get and check the arguments. */
	if ( argc == 3 )
	{
		imageFileName = argv[ 1 ];
		numberOfBins = atoi( argv[ 2 ] );
	}
	else if ( argc == 4 )
	{
		imageFileName = argv[ 1 ];
		maskFileName = argv[ 2 ];
		numberOfBins = atoi( argv[ 3 ] );
	}
	else if ( argc == 5 )
	{
		imageFileName = argv[ 1 ];
		maskFileName = argv[ 2 ];
		if ( atoi( argv[ 3 ] ) == 0 ) useInsideMask = false;
		else useInsideMask = true;
		numberOfBins = atoi( argv[ 4 ] );
	}
	else
	{
		std::cout << "Usage:" << std::endl;
		std::cout << "pxcomputedistances image [mask] [useVoxelsInMask] numberOfBins" << std::endl;
		std::cout << "where useVoxelsInMask = 1, yields taking into account all voxels inside the mask," << std::endl;
		std::cout << "and where useVoxelsInMask = 0, yields taking into account all voxels outside the mask." << std::endl;
		std::cout << "Default is useVoxelsInMask = 1." << std::endl;
		return 1;
	}

	/** Image definitions and reader typedef. */
	const unsigned int	Dimension = 2;
	typedef short				PixelType;
	typedef itk::Image< PixelType, Dimension >					ImageType;
	typedef itk::ImageFileReader< ImageType >						ImageReaderType;

	/** Create readers. */
	ImageReaderType::Pointer imageReader = ImageReaderType::New();
	ImageReaderType::Pointer maskReader = 0;
	imageReader->SetFileName( imageFileName.c_str() );
	if ( argc > 3 )
	{
		maskReader = ImageReaderType::New();
		maskReader->SetFileName( maskFileName.c_str() );
	}

	/** Update reader pipeline. */
	try
	{
		imageReader->Update();
		if ( argc > 3 ) maskReader->Update();
	}
	catch( itk::ExceptionObject &e )
	{
		std::cerr << "Caught ITK exception: " << e << std::endl;
		return 1;
	}

	/** Some typedefs for */
	typedef itk::ImageRegionConstIterator< ImageType >	ConstIteratorType;
	typedef float					MeasurementValueType;
	const unsigned int MeasurementVectorLength = 1;
	typedef	itk::Vector< MeasurementValueType, MeasurementVectorLength >			MeasurementVectorType;
	typedef itk::Statistics::ListSample< MeasurementVectorType >			ListSampleType;

	/** Create iterators over image and mask. */
	ConstIteratorType it( imageReader->GetOutput(),
		imageReader->GetOutput()->GetLargestPossibleRegion() );
	it.GoToBegin();
	ConstIteratorType itMask;
	if ( argc > 3 )
	{
		itMask = ConstIteratorType( maskReader->GetOutput(),
			maskReader->GetOutput()->GetLargestPossibleRegion() );
		itMask.GoToBegin();
	}

	/** Create a list of samples. */
	ListSampleType::Pointer listOfSamples = ListSampleType::New();
	listOfSamples->SetMeasurementVectorSize( MeasurementVectorLength );
	MeasurementVectorType sample;

	/** Fill the list. */
	if ( argc == 3 )
	{
		while ( !it.IsAtEnd() )
		{
			/** Get the sample and put it in the list. */
			sample[ 0 ] = static_cast<MeasurementValueType>( it.Get() );
			listOfSamples->PushBack( sample );
			/** Update the sumOfSquaredVoxels. */
			sumOfSquaredVoxels += static_cast<double>( sample[ 0 ] * sample[ 0 ] );
			/** Increase iterators. */
			++it;
		}
	}
	else if ( argc > 3 && useInsideMask )
	{
		while ( !it.IsAtEnd() )
		{
			/** Get the sample and put it in the list. */
			if ( itMask.Value() )
			{
				sample[ 0 ] = static_cast<MeasurementValueType>( it.Get() );
				listOfSamples->PushBack( sample );
				/** Update the sumOfSquaredVoxels. */
				sumOfSquaredVoxels += static_cast<double>( sample[ 0 ] * sample[ 0 ] );
			}
			/** Increase iterators. */
			++it;
			++itMask;
		}
	}
	else if ( argc > 3 && !useInsideMask )
	{
		while ( !it.IsAtEnd() )
		{
			/** Get the sample and put it in the list. */
			if ( !itMask.Value() )
			{
				sample[ 0 ] = static_cast<MeasurementValueType>( it.Get() );
				listOfSamples->PushBack( sample );
				/** Update the sumOfSquaredVoxels. */
				sumOfSquaredVoxels += static_cast<double>( sample[ 0 ] * sample[ 0 ] );
			}
			/** Increase iterators. */
			++it;
			++itMask;
		}
	}

	/** Typedefs for the creation of a histogram and entropy calculator. */
	typedef itk::Statistics::ListSampleToHistogramGenerator<
		ListSampleType, MeasurementValueType >						HistogramGeneratorType;
	typedef HistogramGeneratorType::HistogramType				HistogramType;
	typedef HistogramType::SizeType											HistogramSizeType;
	typedef itk::HistogramToEntropyImageFilter<
		HistogramType >																		HistogramToEntropyType;
	typedef HistogramToEntropyType::OutputImageType			OutputImageType;

	/** Create numberOfBinS. */
	HistogramSizeType numberOfBinS;
	numberOfBinS.Fill( numberOfBins );

	/** Create histogram and entropy image. */
	HistogramGeneratorType::Pointer histogramGenerator
		= HistogramGeneratorType::New();
	HistogramToEntropyType::Pointer histogramToEntropy
		= HistogramToEntropyType::New();

	/** Setup and update the histogram generator. */
	histogramGenerator->SetNumberOfBins( numberOfBinS );
	histogramGenerator->SetMarginalScale( 10.0 );
	try
	{
		histogramGenerator->SetListSample( listOfSamples );
		histogramGenerator->Update();
	}
	catch( itk::ExceptionObject &e )
	{
		std::cerr << "Caught ITK exception: " << e << std::endl;
		return 1;
	}

	/** Connect histogram to entropy filter. */
	histogramToEntropy->SetInput( histogramGenerator->GetOutput() );

	/** Update pipeline. */
	try
	{
		histogramToEntropy->Update();
	}
	catch( itk::ExceptionObject &e )
	{
		std::cerr << "Caught ITK exception: " << e << std::endl;
		return 1;
	}

	/** Sum over the entropy image to get the total entropy. */
	typedef itk::ImageRegionConstIterator< OutputImageType >	ConstOutputIteratorType;
	ConstOutputIteratorType itE( histogramToEntropy->GetOutput(),
		histogramToEntropy->GetOutput()->GetLargestPossibleRegion() );
	itE.GoToBegin();

	double totalEntropy = 0.0;
	while ( !itE.IsAtEnd() )
	{
		totalEntropy += itE.Get();
		/** Update iterators. */
		++itE;
	} // end while

	/** Calculate stuff. */
	numberOfVoxels = listOfSamples->Size();
	double averageSD = 0.0;
	if ( numberOfVoxels != 0 )
	{
		averageSD = sumOfSquaredVoxels / numberOfVoxels;
	}

	/** Print stuff to screen. *
	std::cout << "The sum of squared voxels equals: " << sumOfSquaredVoxels << std::endl;
	std::cout << "This was calculated over " << numberOfVoxels
		<< " number of voxels, and therefore the average equals: ";
	if ( numberOfVoxels == 0 )
	{
		std::cout << "0" << std::endl;
	}
	else
	{
		std::cout << sumOfSquaredVoxels / numberOfVoxels << std::endl;
	}
	std::cout << "The entropy equals: " << totalEntropy << std::endl;

	/** Print stuff to screen. (SquaredDistance and Entropy). */
	std::cout << "NrOfVoxels:\t\t" << numberOfVoxels << std::endl;
	std::cout << "SquaredDistance:\t" << sumOfSquaredVoxels << std::endl;
	std::cout << "AverageSquaredDistance:\t" << averageSD << std::endl;
	std::cout << "Entropy:\t\t" << totalEntropy << std::endl;

	/** Return a value. */
	return 0;

} // end main

#endif // #ifndef __CalculateOverlap_CXX__
<commit_msg>------------------------------------------------------------ MS:<commit_after>#ifndef __CalculateOverlap_CXX__
#define __CalculateOverlap_CXX__

#include "itkImageFileReader.h"
#include "itkImage.h"

#include "itkImageRegionConstIterator.h"
#include "itkListSample.h"
#include "itkListSampleToHistogramGenerator.h"
#include "itkHistogramToEntropyImageFilter.h"

#include <iomanip>

//-------------------------------------------------------------------------------------

int main( int argc, char *argv[] )
{
	/** Declare stuff. */
	std::string imageFileName = "";
	std::string maskFileName = "";
	bool useInsideMask = true;
	unsigned int numberOfBins = 0;
	double sumOfSquaredVoxels = 0.0;
	unsigned int numberOfVoxels = 0;

	/** Get and check the arguments. */
	if ( argc == 3 )
	{
		imageFileName = argv[ 1 ];
		numberOfBins = atoi( argv[ 2 ] );
	}
	else if ( argc == 4 )
	{
		imageFileName = argv[ 1 ];
		maskFileName = argv[ 2 ];
		numberOfBins = atoi( argv[ 3 ] );
	}
	else if ( argc == 5 )
	{
		imageFileName = argv[ 1 ];
		maskFileName = argv[ 2 ];
		if ( atoi( argv[ 3 ] ) == 0 ) useInsideMask = false;
		else useInsideMask = true;
		numberOfBins = atoi( argv[ 4 ] );
	}
	else
	{
		std::cout << "Usage:" << std::endl;
		std::cout << "pxcomputedistances image [mask] [useVoxelsInMask] numberOfBins" << std::endl;
		std::cout << "where useVoxelsInMask = 1, yields taking into account all voxels inside the mask," << std::endl;
		std::cout << "and where useVoxelsInMask = 0, yields taking into account all voxels outside the mask." << std::endl;
		std::cout << "Default is useVoxelsInMask = 1." << std::endl;
		return 1;
	}

	/** Image definitions and reader typedef. */
	const unsigned int	Dimension = 2;
	typedef short				PixelType;
	typedef itk::Image< PixelType, Dimension >					ImageType;
	typedef itk::ImageFileReader< ImageType >						ImageReaderType;

	/** Create readers. */
	ImageReaderType::Pointer imageReader = ImageReaderType::New();
	ImageReaderType::Pointer maskReader = 0;
	imageReader->SetFileName( imageFileName.c_str() );
	if ( argc > 3 )
	{
		maskReader = ImageReaderType::New();
		maskReader->SetFileName( maskFileName.c_str() );
	}

	/** Update reader pipeline. */
	try
	{
		imageReader->Update();
		if ( argc > 3 ) maskReader->Update();
	}
	catch( itk::ExceptionObject &e )
	{
		std::cerr << "Caught ITK exception: " << e << std::endl;
		return 1;
	}

	/** Some typedefs for */
	typedef itk::ImageRegionConstIterator< ImageType >	ConstIteratorType;
	typedef float					MeasurementValueType;
	const unsigned int MeasurementVectorLength = 1;
	typedef	itk::Vector< MeasurementValueType, MeasurementVectorLength >			MeasurementVectorType;
	typedef itk::Statistics::ListSample< MeasurementVectorType >			ListSampleType;

	/** Create iterators over image and mask. */
	ConstIteratorType it( imageReader->GetOutput(),
		imageReader->GetOutput()->GetLargestPossibleRegion() );
	it.GoToBegin();
	ConstIteratorType itMask;
	if ( argc > 3 )
	{
		itMask = ConstIteratorType( maskReader->GetOutput(),
			maskReader->GetOutput()->GetLargestPossibleRegion() );
		itMask.GoToBegin();
	}

	/** Create a list of samples. */
	ListSampleType::Pointer listOfSamples = ListSampleType::New();
	listOfSamples->SetMeasurementVectorSize( MeasurementVectorLength );
	MeasurementVectorType sample;

	/** Fill the list. */
	if ( argc == 3 )
	{
		while ( !it.IsAtEnd() )
		{
			/** Get the sample and put it in the list. */
			sample[ 0 ] = static_cast<MeasurementValueType>( it.Get() );
			listOfSamples->PushBack( sample );
			/** Update the sumOfSquaredVoxels. */
			sumOfSquaredVoxels += static_cast<double>( sample[ 0 ] * sample[ 0 ] );
			/** Increase iterators. */
			++it;
		}
	}
	else if ( argc > 3 && useInsideMask )
	{
		while ( !it.IsAtEnd() )
		{
			/** Get the sample and put it in the list. */
			if ( itMask.Value() )
			{
				sample[ 0 ] = static_cast<MeasurementValueType>( it.Get() );
				listOfSamples->PushBack( sample );
				/** Update the sumOfSquaredVoxels. */
				sumOfSquaredVoxels += static_cast<double>( sample[ 0 ] * sample[ 0 ] );
			}
			/** Increase iterators. */
			++it;
			++itMask;
		}
	}
	else if ( argc > 3 && !useInsideMask )
	{
		while ( !it.IsAtEnd() )
		{
			/** Get the sample and put it in the list. */
			if ( !itMask.Value() )
			{
				sample[ 0 ] = static_cast<MeasurementValueType>( it.Get() );
				listOfSamples->PushBack( sample );
				/** Update the sumOfSquaredVoxels. */
				sumOfSquaredVoxels += static_cast<double>( sample[ 0 ] * sample[ 0 ] );
			}
			/** Increase iterators. */
			++it;
			++itMask;
		}
	}

	/** Typedefs for the creation of a histogram and entropy calculator. */
	typedef itk::Statistics::ListSampleToHistogramGenerator<
		ListSampleType, MeasurementValueType >						HistogramGeneratorType;
	typedef HistogramGeneratorType::HistogramType				HistogramType;
	typedef HistogramType::SizeType											HistogramSizeType;
	typedef itk::HistogramToEntropyImageFilter<
		HistogramType >																		HistogramToEntropyType;
	typedef HistogramToEntropyType::OutputImageType			OutputImageType;

	/** Create numberOfBinS. */
	HistogramSizeType numberOfBinS;
	numberOfBinS.Fill( numberOfBins );

	/** Create histogram and entropy image. */
	HistogramGeneratorType::Pointer histogramGenerator
		= HistogramGeneratorType::New();
	HistogramToEntropyType::Pointer histogramToEntropy
		= HistogramToEntropyType::New();

	/** Setup and update the histogram generator. */
	histogramGenerator->SetNumberOfBins( numberOfBinS );
	histogramGenerator->SetMarginalScale( 10.0 );
	try
	{
		histogramGenerator->SetListSample( listOfSamples );
		histogramGenerator->Update();
	}
	catch( itk::ExceptionObject &e )
	{
		std::cerr << "Caught ITK exception: " << e << std::endl;
		return 1;
	}

	/** Connect histogram to entropy filter. */
	histogramToEntropy->SetInput( histogramGenerator->GetOutput() );

	/** Update pipeline. */
	try
	{
		histogramToEntropy->Update();
	}
	catch( itk::ExceptionObject &e )
	{
		std::cerr << "Caught ITK exception: " << e << std::endl;
		return 1;
	}

	/** Sum over the entropy image to get the total entropy. */
	typedef itk::ImageRegionConstIterator< OutputImageType >	ConstOutputIteratorType;
	ConstOutputIteratorType itE( histogramToEntropy->GetOutput(),
		histogramToEntropy->GetOutput()->GetLargestPossibleRegion() );
	itE.GoToBegin();

	double totalEntropy = 0.0;
	while ( !itE.IsAtEnd() )
	{
		totalEntropy += itE.Get();
		/** Update iterators. */
		++itE;
	} // end while

	/** Calculate stuff. */
	numberOfVoxels = listOfSamples->Size();
	double averageSD = 0.0;
	if ( numberOfVoxels != 0 )
	{
		averageSD = sumOfSquaredVoxels / numberOfVoxels;
	}

	/** Print stuff to screen. *
	std::cout << "The sum of squared voxels equals: " << sumOfSquaredVoxels << std::endl;
	std::cout << "This was calculated over " << numberOfVoxels
		<< " number of voxels, and therefore the average equals: ";
	if ( numberOfVoxels == 0 )
	{
		std::cout << "0" << std::endl;
	}
	else
	{
		std::cout << sumOfSquaredVoxels / numberOfVoxels << std::endl;
	}
	std::cout << "The entropy equals: " << totalEntropy << std::endl;

	/** Print stuff to screen. (SquaredDistance and Entropy). */
	std::cout << std::fixed;
	std::cout << std::showpoint;
	std::cout << std::setprecision( 10 );
	std::cout << "NrOfVoxels:\t\t" << numberOfVoxels << std::endl;
	std::cout << "SquaredDistance:\t" << static_cast<long long>( sumOfSquaredVoxels ) << std::endl;
	std::cout << "AverageSquaredDistance:\t" << averageSD << std::endl;
	std::cout << "Entropy:\t\t" << totalEntropy << std::endl;

	/** Return a value. */
	return 0;

} // end main

#endif // #ifndef __CalculateOverlap_CXX__
<|endoftext|>


<commit_before>#include "decrypt.h"

Tag5::Ptr find_decrypting_key(const PGP & k, const std::string &keyid){
    if (k.get_ASCII_Armor() == 2){
        std::vector <Packet::Ptr> packets = k.get_packets();
        for(Packet::Ptr const & p : packets){
            if ((p -> get_tag() == 5) || (p -> get_tag() == 7)){
                std::string data = p -> raw();
                Tag5::Ptr key = std::make_shared<Tag5>(data);
                if ( key->get_public_ptr()->get_keyid() != keyid ){
                    continue;
                }
                // make sure key has signing material
                if ((key -> get_pka() == 1) || // RSA
                    (key -> get_pka() == 2) || // RSA
                    (key -> get_pka() == 16)){ // ElGamal
                        return key;
                }
            }
        }
    }
    return Tag5::Ptr();
}

std::string pka_decrypt(const uint8_t pka, std::vector <PGPMPI> & data, const std::vector <PGPMPI> & pri, const std::vector <PGPMPI> & pub){
    if (pka < 3){   // RSA
        return mpitoraw(RSA_decrypt(data[0], pri, pub));
    }
    if (pka == 16){ // ElGamal
        return ElGamal_decrypt(data, pri, pub);
    }
    else{
        std::stringstream s; s << static_cast <int> (pka);
        throw std::runtime_error("Error: PKA number " + s.str() + " not allowed or unknown.");
    }
    return ""; // should never reach here; mainly just to remove compiler warnings
}

std::vector <PGPMPI> decrypt_secret_key(const Tag5::Ptr & pri, const std::string & passphrase){
    std::vector <PGPMPI> out;
    S2K::Ptr s2k = pri -> get_s2k();

    // calculate key used in encryption algorithm
    std::string key = s2k -> run(passphrase, Symmetric_Algorithm_Key_Length.at(Symmetric_Algorithms.at(pri -> get_sym())) >> 3);

    // decrypt secret key
    std::string secret_key = use_normal_CFB_decrypt(pri -> get_sym(), pri -> get_secret(), key, pri -> get_IV());
    // get checksum and remove it from the string
    const unsigned int hash_size = (pri -> get_s2k_con() == 254)?20:2;
    std::string checksum = secret_key.substr(secret_key.size() - hash_size, hash_size);
    secret_key = secret_key.substr(0, secret_key.size() - hash_size);

    // calculate and check checksum
    if(pri -> get_s2k_con() == 254){
        if (use_hash(s2k -> get_hash(), secret_key) != checksum){
            throw std::runtime_error("Error: Secret key checksum and calculated checksum do not match.");
        }
    }
    else{ // all other values; **UNTESTED**
        uint16_t sum = 0;
        for(char & c : secret_key){
            sum += static_cast <unsigned char> (c);
        }
        if (unhexlify(makehex(sum, 4)) != checksum){
            if (use_hash(s2k -> get_hash(), secret_key) != checksum){
                throw std::runtime_error("Error: Secret key checksum and calculated checksum do not match.");
            }
        }
    }

    // extract MPI values
    while (secret_key.size()){
        out.push_back(read_MPI(secret_key));
    }
    return out;
}

std::string decrypt_message(PGP & m, PGP& pri, const std::string & passphrase){
    if ((m.get_ASCII_Armor() != 0)/* && (m.get_ASCII_Armor() != 3) && (m.get_ASCII_Armor() != 4)*/){
        throw std::runtime_error("Error: No encrypted message found.");
    }

    if (pri.get_ASCII_Armor() != 2){
        throw std::runtime_error("Error: No Private Key found.");
    }
    // reused variables
    uint8_t packet;
    std::string data;
    std::string checksum;

    std::string session_key;                    // session key
    uint8_t sym;                                // symmetric key algorithm used to encrypt original data
    unsigned int BS;                            // blocksize of symmetric key algorithm

    // find session key
    std::vector <Packet::Ptr> message_packets = m.get_packets();
    for(Packet::Ptr const & p : message_packets){
        if ((p -> get_tag() == 1) || (p -> get_tag() == 3)){
            data = p -> raw();
            packet = p -> get_tag();
            break;
        }
    }

    if (packet == 1){ // Public-Key Encrypted Session Key Packet (Tag 1)
        Tag1 tag1(data);
        uint8_t pka = tag1.get_pka();
        std::vector <PGPMPI> session_key_mpi = tag1.get_mpi();

        // find corresponding secret key
        Tag5::Ptr sec = find_decrypting_key(pri, tag1.get_keyid());

        if (!sec){
            throw std::runtime_error("Error: Correct Private Key not found.");
        }

        std::vector <PGPMPI> pub = sec -> get_mpi();
        std::vector <PGPMPI> pri = decrypt_secret_key(sec, passphrase);

        // get session key
        session_key = zero + pka_decrypt(pka, session_key_mpi, pri, pub);                   // symmetric algorithm, session key, 2 octet checksum wrapped in EME_PKCS1_ENCODE
        session_key = EME_PKCS1v1_5_DECODE(session_key);                                    // remove EME_PKCS1 encoding
        sym = session_key[0];                                                               // get symmetric algorithm
        checksum = session_key.substr(session_key.size() - 2, 2);                           // get 2 octet checksum
        session_key = session_key.substr(1, session_key.size() - 3);                        // remove both from session key
        uint16_t sum = 0;
        for(char & c : session_key){                                                        // calculate session key checksum
            sum += static_cast <uint8_t> (c);
        }
        if (unhexlify(makehex(sum, 4)) != checksum){                                        // check session key checksums
            throw std::runtime_error("Error: Calculated session key checksum does not match given checksum.");
        }
    }
    else if (packet == 3){ //Symmetric-Key Encrypted Session Key Packet (Tag 3)
        /* untested */
        Tag3 tag3(data);
        data = tag3.get_key(passphrase);
        sym = data[0];
        session_key = data.substr(1, data.size() - 1);
    }
    else{
        throw std::runtime_error("Error: No session key packet found.");
    }

    BS = Symmetric_Algorithm_Block_Length.at(Symmetric_Algorithms.at(sym)) >> 3;           // get blocksize

    // Find encrypted data
    data = "";
    for(Packet::Ptr const & p : message_packets){
        if (p -> get_tag() == 9){
            data = p -> raw();
            Tag9 tag9(data);
            data = tag9.get_encrypted_data();
            packet = 9;
            break;
        }
        else if (p -> get_tag() == 18){
            data = p -> raw();
            Tag18 tag18(data);
            data = tag18.get_protected_data();
            packet = 18;
            break;
        }
    }
    if (!data.size()){
        throw std::runtime_error("Error: No encrypted data packets found.");
    }

    if (sym == 2){ // Triple DES
        data = use_OpenPGP_CFB_decrypt(sym, packet, data, session_key.substr(0, 8), session_key.substr(8, 8), session_key.substr(16, 8)); // decrypt encrypted data
    }
    else{
        data = use_OpenPGP_CFB_decrypt(sym, packet, data, session_key); // decrypt encrypted data
    }

    if (packet == 18){ // Symmetrically Encrypted Integrity Protected Data Packet (Tag 18)
        checksum = data.substr(data.size() - 20, 20);       // get given SHA1 checksum
        data = data.substr(0, data.size() - 20);            // remove SHA1 checksum
        if (use_hash(2, data) != checksum){                 // check SHA1 checksum
            throw std::runtime_error("Error: Given checksum and calculated checksum do not match.");
        }
        data = data.substr(0, data.size() - 2);             // get rid of \xd3\x14
    }

    data = data.substr(BS + 2, data.size() - BS - 2);       // get rid of prefix

    if (packet == 9){ // Symmetrically Encrypted Data Packet (Tag 9)
        // figure out which compression algorithm was used
        // uncompressed literal data packet
        if ((data[0] == 'b') || (data[0] == 't') || (data[0] == 'u')){
            data = Tag11(data).write(); // add in Tag11 headers to be removed later
        }
        // BZIP2
        else if (data.substr(0, 2) == "BZ"){
            data = PGP_decompress(3, data);
        }
        // ZLIB
        else if ((data.substr(0, 2) == "\x78\x01") || (data.substr(0, 2) == "\x78\x9c") || (data.substr(0, 2) == "\x78\xda")){
            data = PGP_decompress(2, data);
        }
        // DEFLATE
        else{
            data = PGP_decompress(1, data);
        }
    }

    // get rid of header and figure out what type of packet data it is
    bool format;
    data = read_packet_header(data, packet, format);

    // output data
    switch (packet){
        case 8: // Compressed Data Packet
            {
                data = Tag8(data).get_data(); // compressed packets
                std::vector <Packet::Ptr> compressed_packets;

                while (data.size()){ // extract packets
                    compressed_packets.push_back(read_packet(data) -> clone());
                }

                // extract all packet data; probably needs better formatting
                for(const Packet::Ptr & p : compressed_packets){
                    if (p -> get_tag() == 11){
                        Tag11 tag11(data);
                        if (tag11.get_filename() == ""){
                            data = tag11.get_literal();
                        }
                        else{
                            tag11.write();
                            data = "Data written to file '" + Tag11(data).get_filename() + "'";
                        }
                        std::cout << data << std::endl;
                    }
                    // else{
                        // data += p -> show() + "\n";
                    // }
                }
            }
            break;
        case 11: // Literal Data Packet
            {
                Tag11 tag11(data);
                if (tag11.get_filename() == ""){
                    data = tag11.get_literal();
                }
                else{
                    tag11.write();
                    data = "Data written to file '" + Tag11(data).get_filename() + "'";
                }
            }
            break;
        default:
            {
                std::stringstream s; s << packet;
                throw std::runtime_error("Error: No action defined for packet type " + s.str());
            }
            break;
    }
    return data;
}
<commit_msg>Debugging debug code<commit_after>#include "decrypt.h"

Tag5::Ptr find_decrypting_key(const PGP & k, const std::string &keyid){
    if (k.get_ASCII_Armor() == 2){
        std::vector <Packet::Ptr> packets = k.get_packets();
        for(Packet::Ptr const & p : packets){
            if ((p -> get_tag() == 5) || (p -> get_tag() == 7)){
                std::string data = p -> raw();
                Tag5::Ptr key = std::make_shared<Tag5>(data);
                if ( key->get_public_ptr()->get_keyid() != keyid ){
                    continue;
                }
                // make sure key has signing material
                if ((key -> get_pka() == 1) || // RSA
                    (key -> get_pka() == 2) || // RSA
                    (key -> get_pka() == 16)){ // ElGamal
                        return key;
                }
            }
        }
    }
    return Tag5::Ptr();
}

std::string pka_decrypt(const uint8_t pka, std::vector <PGPMPI> & data, const std::vector <PGPMPI> & pri, const std::vector <PGPMPI> & pub){
    if (pka < 3){   // RSA
        return mpitoraw(RSA_decrypt(data[0], pri, pub));
    }
    if (pka == 16){ // ElGamal
        return ElGamal_decrypt(data, pri, pub);
    }
    else{
        std::stringstream s; s << static_cast <int> (pka);
        throw std::runtime_error("Error: PKA number " + s.str() + " not allowed or unknown.");
    }
    return ""; // should never reach here; mainly just to remove compiler warnings
}

std::vector <PGPMPI> decrypt_secret_key(const Tag5::Ptr & pri, const std::string & passphrase){
    std::vector <PGPMPI> out;
    S2K::Ptr s2k = pri -> get_s2k();

    // calculate key used in encryption algorithm
    std::string key = s2k -> run(passphrase, Symmetric_Algorithm_Key_Length.at(Symmetric_Algorithms.at(pri -> get_sym())) >> 3);

    // decrypt secret key
    std::string secret_key = use_normal_CFB_decrypt(pri -> get_sym(), pri -> get_secret(), key, pri -> get_IV());
    // get checksum and remove it from the string
    const unsigned int hash_size = (pri -> get_s2k_con() == 254)?20:2;
    std::string checksum = secret_key.substr(secret_key.size() - hash_size, hash_size);
    secret_key = secret_key.substr(0, secret_key.size() - hash_size);

    // calculate and check checksum
    if(pri -> get_s2k_con() == 254){
        if (use_hash(s2k -> get_hash(), secret_key) != checksum){
            throw std::runtime_error("Error: Secret key checksum and calculated checksum do not match.");
        }
    }
    else{ // all other values; **UNTESTED**
        uint16_t sum = 0;
        for(char & c : secret_key){
            sum += static_cast <unsigned char> (c);
        }
        if (unhexlify(makehex(sum, 4)) != checksum){
            if (use_hash(s2k -> get_hash(), secret_key) != checksum){
                throw std::runtime_error("Error: Secret key checksum and calculated checksum do not match.");
            }
        }
    }

    // extract MPI values
    while (secret_key.size()){
        out.push_back(read_MPI(secret_key));
    }
    return out;
}

std::string decrypt_message(PGP & m, PGP& pri, const std::string & passphrase){
    if ((m.get_ASCII_Armor() != 0)/* && (m.get_ASCII_Armor() != 3) && (m.get_ASCII_Armor() != 4)*/){
        throw std::runtime_error("Error: No encrypted message found.");
    }

    if (pri.get_ASCII_Armor() != 2){
        throw std::runtime_error("Error: No Private Key found.");
    }
    // reused variables
    uint8_t packet;
    std::string data;
    std::string checksum;

    std::string session_key;                    // session key
    uint8_t sym;                                // symmetric key algorithm used to encrypt original data
    unsigned int BS;                            // blocksize of symmetric key algorithm

    // find session key
    std::vector <Packet::Ptr> message_packets = m.get_packets();
    for(Packet::Ptr const & p : message_packets){
        if ((p -> get_tag() == 1) || (p -> get_tag() == 3)){
            data = p -> raw();
            packet = p -> get_tag();
            break;
        }
    }

    if (packet == 1){ // Public-Key Encrypted Session Key Packet (Tag 1)
        Tag1 tag1(data);
        uint8_t pka = tag1.get_pka();
        std::vector <PGPMPI> session_key_mpi = tag1.get_mpi();

        // find corresponding secret key
        Tag5::Ptr sec = find_decrypting_key(pri, tag1.get_keyid());

        if (!sec){
            throw std::runtime_error("Error: Correct Private Key not found.");
        }

        std::vector <PGPMPI> pub = sec -> get_mpi();
        std::vector <PGPMPI> pri = decrypt_secret_key(sec, passphrase);

        // get session key
        session_key = zero + pka_decrypt(pka, session_key_mpi, pri, pub);                   // symmetric algorithm, session key, 2 octet checksum wrapped in EME_PKCS1_ENCODE
        session_key = EME_PKCS1v1_5_DECODE(session_key);                                    // remove EME_PKCS1 encoding
        sym = session_key[0];                                                               // get symmetric algorithm
        checksum = session_key.substr(session_key.size() - 2, 2);                           // get 2 octet checksum
        session_key = session_key.substr(1, session_key.size() - 3);                        // remove both from session key
        uint16_t sum = 0;
        for(char & c : session_key){                                                        // calculate session key checksum
            sum += static_cast <uint8_t> (c);
        }
        if (unhexlify(makehex(sum, 4)) != checksum){                                        // check session key checksums
            throw std::runtime_error("Error: Calculated session key checksum does not match given checksum.");
        }
    }
    else if (packet == 3){ //Symmetric-Key Encrypted Session Key Packet (Tag 3)
        /* untested */
        Tag3 tag3(data);
        data = tag3.get_key(passphrase);
        sym = data[0];
        session_key = data.substr(1, data.size() - 1);
    }
    else{
        throw std::runtime_error("Error: No session key packet found.");
    }

    BS = Symmetric_Algorithm_Block_Length.at(Symmetric_Algorithms.at(sym)) >> 3;           // get blocksize

    // Find encrypted data
    data = "";
    for(Packet::Ptr const & p : message_packets){
        if (p -> get_tag() == 9){
            data = p -> raw();
            Tag9 tag9(data);
            data = tag9.get_encrypted_data();
            packet = 9;
            break;
        }
        else if (p -> get_tag() == 18){
            data = p -> raw();
            Tag18 tag18(data);
            data = tag18.get_protected_data();
            packet = 18;
            break;
        }
    }
    if (!data.size()){
        throw std::runtime_error("Error: No encrypted data packets found.");
    }

    if (sym == 2){ // Triple DES
        data = use_OpenPGP_CFB_decrypt(sym, packet, data, session_key.substr(0, 8), session_key.substr(8, 8), session_key.substr(16, 8)); // decrypt encrypted data
    }
    else{
        data = use_OpenPGP_CFB_decrypt(sym, packet, data, session_key); // decrypt encrypted data
    }

    if (packet == 18){ // Symmetrically Encrypted Integrity Protected Data Packet (Tag 18)
        checksum = data.substr(data.size() - 20, 20);       // get given SHA1 checksum
        data = data.substr(0, data.size() - 20);            // remove SHA1 checksum
        if (use_hash(2, data) != checksum){                 // check SHA1 checksum
            throw std::runtime_error("Error: Given checksum and calculated checksum do not match.");
        }
        data = data.substr(0, data.size() - 2);             // get rid of \xd3\x14
    }

    data = data.substr(BS + 2, data.size() - BS - 2);       // get rid of prefix

    if (packet == 9){ // Symmetrically Encrypted Data Packet (Tag 9)
        // figure out which compression algorithm was used
        // uncompressed literal data packet
        if ((data[0] == 'b') || (data[0] == 't') || (data[0] == 'u')){
            data = Tag11(data).write(); // add in Tag11 headers to be removed later
        }
        // BZIP2
        else if (data.substr(0, 2) == "BZ"){
            data = PGP_decompress(3, data);
        }
        // ZLIB
        else if ((data.substr(0, 2) == "\x78\x01") || (data.substr(0, 2) == "\x78\x9c") || (data.substr(0, 2) == "\x78\xda")){
            data = PGP_decompress(2, data);
        }
        // DEFLATE
        else{
            data = PGP_decompress(1, data);
        }
    }

    // get rid of header and figure out what type of packet data it is
    bool format;
    data = read_packet_header(data, packet, format);

    // output data
    switch (packet){
        case 8: // Compressed Data Packet
            {
                data = Tag8(data).get_data(); // compressed packets
                std::vector <Packet::Ptr> compressed_packets;

                while (data.size()){ // extract packets
                    compressed_packets.push_back(read_packet(data) -> clone());
                }

                // extract all packet data; probably needs better formatting
                for(const Packet::Ptr & p : compressed_packets){
                    if (p -> get_tag() == 11){
                        Tag11 tag11(data);
                        if (tag11.get_filename() == ""){
                            data += tag11.get_literal();
                        }
                        else{
                            tag11.write();
                            data += "Data written to file '" + Tag11(data).get_filename() + "'";
                        }
                        std::cout << data << std::endl;
                    }
                    // else{
                        // data += p -> show() + "\n";
                    // }
                }
            }
            break;
        case 11: // Literal Data Packet
            {
                Tag11 tag11(data);
                if (tag11.get_filename() == ""){
                    data = tag11.get_literal();
                }
                else{
                    tag11.write();
                    data = "Data written to file '" + Tag11(data).get_filename() + "'";
                }
            }
            break;
        default:
            {
                std::stringstream s; s << packet;
                throw std::runtime_error("Error: No action defined for packet type " + s.str());
            }
            break;
    }
    return data;
}
<|endoftext|>


<commit_before>
#include "viennacl.h"
#include "tests.h"
// vex::Context* ViennaCLTest::CreateContext() {
//  ctx = new vex::Context(vex::Filter::Env && vex::Filter::DoublePrecision);
//  return ctx;
//}

void ViennaCLTest::WarmUp() {

  const size_t N = 1024 * 1024;
  const size_t M = 1024;
  std::vector<double> A(N, 0);
  std::vector<double> B = random_vector<double>(N);
  std::vector<double> C = random_vector<double>(N);
  std::vector<double> D = random_vector<double>(N);
  viennacl::vector<double> a, b, c, d;
  viennacl::copy(A, a);
  viennacl::copy(B, b);
  viennacl::copy(C, c);
  viennacl::copy(D, d);
  a += b + viennacl::linalg::element_prod(c , d);
}

viennacl::vector<double> ViennaCLTest::ConvertVector(blaze::DynamicVector<double>& blaze_vec) {
  unsigned int num_rows = blaze_vec.size();

  std::vector<double> temp(num_rows);

  for (int i = 0; i < num_rows; i++) {
    temp[i] = blaze_vec[i];
  }

  viennacl::vector<double> vcl_vec(num_rows);
  viennacl::copy(temp, vcl_vec);

  return vcl_vec;
}

viennacl::compressed_matrix<double> ViennaCLTest::ConvertMatrix(CSR& data) {
  unsigned int& num_rows = data.num_rows;
  unsigned int& num_cols = data.num_cols;
  unsigned int& num_nonzero = data.num_nonzero;

  std::vector<size_t>& row = data.row;
  std::vector<size_t>& col = data.col;
  std::vector<double>& val = data.val;

  // vex::SpMat<double> mat(*ctx, num_rows, num_cols, row.data(), col.data(), val.data());
  viennacl::compressed_matrix<double> mat(num_rows,num_cols, num_nonzero);
  mat.set(row.data(), col.data(), val.data(), num_rows, num_cols, num_nonzero);

  return mat;
}

void ViennaCLTest::RunSPMV(viennacl::compressed_matrix<double>& D_T, viennacl::compressed_matrix<double>& M_invD, viennacl::vector<double>& gamma) {
  unsigned int num_rows = gamma.size();

  viennacl::vector<double> temporary(num_rows);
  viennacl::vector<double> result(num_rows);

  for (size_t i = 0; i < RUNS; i++) {
    temporary = viennacl::linalg::prod(M_invD, gamma);
    result = viennacl::linalg::prod(D_T, temporary);
    viennacl::ocl::get_queue().finish();
  }
}
<commit_msg>Fix the finish command based on a viennaCL example<commit_after>
#include "viennacl.h"
#include "tests.h"
// vex::Context* ViennaCLTest::CreateContext() {
//  ctx = new vex::Context(vex::Filter::Env && vex::Filter::DoublePrecision);
//  return ctx;
//}

void ViennaCLTest::WarmUp() {

  const size_t N = 1024 * 1024;
  const size_t M = 1024;
  std::vector<double> A(N, 0);
  std::vector<double> B = random_vector<double>(N);
  std::vector<double> C = random_vector<double>(N);
  std::vector<double> D = random_vector<double>(N);
  viennacl::vector<double> a, b, c, d;
  viennacl::copy(A, a);
  viennacl::copy(B, b);
  viennacl::copy(C, c);
  viennacl::copy(D, d);
  a += b + viennacl::linalg::element_prod(c , d);
}

viennacl::vector<double> ViennaCLTest::ConvertVector(blaze::DynamicVector<double>& blaze_vec) {
  unsigned int num_rows = blaze_vec.size();

  std::vector<double> temp(num_rows);

  for (int i = 0; i < num_rows; i++) {
    temp[i] = blaze_vec[i];
  }

  viennacl::vector<double> vcl_vec(num_rows);
  viennacl::copy(temp, vcl_vec);

  return vcl_vec;
}

viennacl::compressed_matrix<double> ViennaCLTest::ConvertMatrix(CSR& data) {
  unsigned int& num_rows = data.num_rows;
  unsigned int& num_cols = data.num_cols;
  unsigned int& num_nonzero = data.num_nonzero;

  std::vector<size_t>& row = data.row;
  std::vector<size_t>& col = data.col;
  std::vector<double>& val = data.val;

  // vex::SpMat<double> mat(*ctx, num_rows, num_cols, row.data(), col.data(), val.data());
  viennacl::compressed_matrix<double> mat(num_rows,num_cols, num_nonzero);
  mat.set(row.data(), col.data(), val.data(), num_rows, num_cols, num_nonzero);

  return mat;
}

void ViennaCLTest::RunSPMV(viennacl::compressed_matrix<double>& D_T, viennacl::compressed_matrix<double>& M_invD, viennacl::vector<double>& gamma) {
  unsigned int num_rows = gamma.size();

  viennacl::vector<double> temporary(num_rows);
  viennacl::vector<double> result(num_rows);

  for (size_t i = 0; i < RUNS; i++) {
    temporary = viennacl::linalg::prod(M_invD, gamma);
    result = viennacl::linalg::prod(D_T, temporary);
    viennacl::backend::finish();
  }
}
<|endoftext|>


<commit_before>#!/bin/bash
#
#3> <> prov:specializationOf <https://github.com/timrdf/csv2rdf4lod-automation/blob/master/bin/util/prefix.cc>;
#3> <> prov:wasDerivedFrom   <https://github.com/timrdf/csv2rdf4lod-automation/blob/master/bin/util/o-of-p.sh> .
#
# Print the objects of the given predicate in the given RDF file.
#
# To handle more files than 'ls' can provide:
#   find . -name "[^.]*" | xargs      nt-nodes.sh > nodes.txt

if [[ $# -eq 0 || "$1" == "--help" ]]; then
   echo
   echo "usage: `basename $0` <uri>+"
   echo " <uri> : e.g. sio:has-member  http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
   exit
fi

while [ $# -gt 0 ]; do

   uri="$1"
   shift

   if [[ "$uri" =~ http* ]]; then
      echo $uri
   else
      prefix=${uri%%:*}
      namespace=`curl -s http://prefix.cc/$prefix.file.txt | awk '{print $2}'`
      uri=$namespace${uri#*:}
   fi
   echo $uri

done
<commit_msg>o of p prefix.cc<commit_after>#!/bin/bash
#
#3> <> prov:specializationOf <https://github.com/timrdf/csv2rdf4lod-automation/blob/master/bin/util/prefix.cc>;
#3> <> prov:wasDerivedFrom   <https://github.com/timrdf/csv2rdf4lod-automation/blob/master/bin/util/o-of-p.sh> .
#
# Print the objects of the given predicate in the given RDF file.
#
# To handle more files than 'ls' can provide:
#   find . -name "[^.]*" | xargs      nt-nodes.sh > nodes.txt

if [[ $# -eq 0 || "$1" == "--help" ]]; then
   echo
   echo "usage: `basename $0` <uri>+"
   echo " <uri> : e.g. sio:has-member  http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
   exit
fi

while [ $# -gt 0 ]; do

   uri="$1"
   shift

   if [[ "$uri" =~ http* ]]; then
      echo $uri
   else
      prefix=${uri%%:*}
      namespace=`curl -s http://prefix.cc/$prefix.file.txt | awk '{print $2}'`
      uri=$namespace${uri#*:}
      echo $uri
   fi

done
<|endoftext|>