SkelCL
SkelCL is a high level multi GPU skeleton library developed at the university of Münster, Germany.
 All Classes Namespaces Files Functions Variables Typedefs Groups
Util.cpp
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright (c) 2011-2012 The SkelCL Team as listed in CREDITS.txt *
3  * http://skelcl.uni-muenster.de *
4  * *
5  * This file is part of SkelCL. *
6  * SkelCL is available under multiple licenses. *
7  * The different licenses are subject to terms and condition as provided *
8  * in the files specifying the license. See "LICENSE.txt" for details *
9  * *
10  *****************************************************************************
11  * *
12  * SkelCL is free software: you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation, either version 3 of the License, or *
15  * (at your option) any later version. See "LICENSE-gpl.txt" for details. *
16  * *
17  * SkelCL is distributed in the hope that it will be useful, *
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
20  * GNU General Public License for more details. *
21  * *
22  *****************************************************************************
23  * *
24  * For non-commercial academic use see the license specified in the file *
25  * "LICENSE-academic.txt". *
26  * *
27  *****************************************************************************
28  * *
29  * If you are interested in other licensing models, including a commercial- *
30  * license, please contact the author at michel.steuwer@uni-muenster.de *
31  * *
32  *****************************************************************************/
33 
39 
40 #include <iomanip>
41 #include <ios>
42 #include <sstream>
43 #include <string>
44 
45 #include <cmath>
46 #include <cstdlib>
47 
48 #ifdef _WIN32
49 #include <windows.h>
50 #include <bcrypt.h>
51 #pragma comment(lib, "bcrypt.lib") // link against the bcrypt library
52 #else
53 #pragma GCC diagnostic push
54 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
55 #include <openssl/sha.h>
56 #pragma GCC diagnostic pop
57 #endif
58 
59 #include <pvsutil/Assert.h>
60 #include <pvsutil/Logger.h>
61 
62 #include "SkelCL/detail/Util.h"
63 
64 #ifdef WIN32
65 namespace {
66 
67  BCRYPT_ALG_HANDLE createAlgoritmProvider() {
68  BCRYPT_ALG_HANDLE alg;
69  auto result = ::BCryptOpenAlgorithmProvider(
70  &alg, // algorithm handle
71  BCRYPT_SHA1_ALGORITHM, // hashing algorithm ID
72  nullptr, // use default provider
73  0 // default flags
74  );
75  ASSERT(result >= 0);
76  return alg;
77  }
78 
79  std::vector<BYTE> createHashBuffer(BCRYPT_ALG_HANDLE alg) {
80  DWORD propertyValue;
81  DWORD resultSize;
82 
83  NTSTATUS result = ::BCryptGetProperty(
84  alg,
85  BCRYPT_OBJECT_LENGTH,
86  reinterpret_cast<BYTE *>(&propertyValue),
87  sizeof(propertyValue),
88  &resultSize,
89  0);
90  ASSERT(result >= 0);
91 
92  return std::vector<BYTE>(propertyValue);
93  }
94 
95  BCRYPT_HASH_HANDLE createHash(BCRYPT_ALG_HANDLE alg,
96  std::vector<BYTE>& hashBuffer) {
97  BCRYPT_HASH_HANDLE hash;
98  auto result = ::BCryptCreateHash(
99  alg, // handle to parent
100  &hash, // hash object handle
101  hashBuffer.data(), // hash object buffer pointer
102  hashBuffer.size(), // hash object buffer length
103  nullptr, // no secret
104  0, // no secret
105  0 // no flags
106  );
107  ASSERT(result >= 0);
108 
109  return hash;
110  }
111 
112  void SHA1(const std::string& string, BYTE* buffer) {
113  auto alg = createAlgoritmProvider();
114 
115  auto hashBuffer = createHashBuffer(alg);
116 
117  auto hash = createHash(alg, hashBuffer);
118 
119  const void* data = static_cast<const void*>(string.data());
120  auto result = ::BCryptHashData(
121  hash, // hash object handle
122  static_cast<UCHAR *>(const_cast<void *>(data)), // safely remove const from buffer pointer
123  string.length(), // input buffer length in bytes
124  0 // no flags
125  );
126  ASSERT(result >= 0);
127 
128  result = ::BCryptFinishHash(
129  hash, // handle to hash object
130  buffer, // output buffer for hash value
131  sizeof(BYTE)*20, // size of this buffer
132  0 // no flags
133  );
134  ASSERT(result >= 0);
135 
136  ::BCryptDestroyHash(hash);
137  ::BCryptCloseAlgorithmProvider(alg, 0);
138  }
139 
140 }
141 #endif
142 
143 namespace skelcl {
144 
145 namespace detail {
146 
147 namespace util {
148 
149 std::string envVarValue(const std::string& envVar)
150 {
151  char* envValue = std::getenv(envVar.c_str());
152  if (envValue != NULL) {
153  return envValue;
154  } else {
155  return "";
156  }
157 }
158 
159 std::string hash(const std::string& string)
160 {
161 #ifdef WIN32
162  BYTE raw[20];
163  ::SHA1(string, raw);
164 #else
165  unsigned char raw[20];
166  const char* c_str = string.c_str();
167 #pragma GCC diagnostic push
168 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
169  SHA1(reinterpret_cast<const unsigned char*>(c_str), string.length(), raw);
170 #pragma GCC diagnostic pop
171 #endif
172  std::ostringstream buffer;
173  for (int i = 0; i < 20; ++i) {
174  buffer << std::hex
175  << std::setw(2)
176  << std::setfill('0')
177  << static_cast<int>( raw[i] );
178  }
179  return buffer.str();
180 }
181 
182 size_t devideAndRoundUp(size_t i, size_t j)
183 {
184  size_t r = i / j;
185  if (i % j != 0) {
186  r++;
187  }
188  return r;
189 }
190 
191 size_t devideAndAlign(size_t i, size_t j, size_t a)
192 {
193  size_t x = i / j;
194  if (i % j != 0)
195  ++x;
196  size_t r = x % a;
197  if (r != 0)
198  x = x + (a - r);
199  return x;
200 }
201 
202 size_t ceilToMultipleOf(size_t i, size_t j)
203 {
204  if (i == 0) return j;
205  size_t r = i % j;
206  if (r == 0)
207  return i;
208  else
209  return i + (j - r);
210 }
211 
212 bool isPowerOfTwo(size_t n)
213 {
214  return ((n & (n - 1)) == 0);
215 }
216 
217 int floorPow2(int n)
218 {
219  int exp;
220  frexp(static_cast<float>(n), &exp);
221  return 1 << (exp - 1);
222 }
223 
224 int ceilPow2(int n)
225 {
226  int exp;
227  frexp(static_cast<float>(n), &exp);
228  return 1 << exp;
229 }
230 
231 } // namespace util
232 
233 } // namespace detail
234 
235 } // namespace skelcl