File: misc.cc

package info (click to toggle)
jellyfish 2.3.1-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,276 kB
  • sloc: cpp: 35,703; sh: 995; ruby: 578; makefile: 397; python: 165; perl: 36
file content (94 lines) | stat: -rw-r--r-- 2,295 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*  This file is part of Jellyfish.

    This work is dual-licensed under 3-Clause BSD License or GPL 3.0.
    You can choose between one of them if you use this work.

`SPDX-License-Identifier: BSD-3-Clause OR  GPL-3.0`
*/

#include <signal.h>
#include <config.h>
#include <jellyfish/misc.hpp>
#include <jellyfish/err.hpp>
#include <jellyfish/backtrace.hpp>

namespace jellyfish {
uint64_t bogus_sum(void *data, size_t len) {
  uint64_t res = 0, tmp = 0;
  uint64_t *ptr = (uint64_t *)data;

  while(len >= sizeof(uint64_t)) {
    res ^= *ptr++;
    len -= sizeof(uint64_t);
  }
  
  if(len > 0) {
    memcpy(&tmp, ptr, len);
    res ^= tmp;
  }
  return res;
}

void disabled_misaligned_mem_access() {
#if defined(__GNUC__)
# if defined(__i386__)
  /* Enable Alignment Checking on x86 */ 
  __asm__("pushf\norl $0x40000,(%esp)\npopf");
# elif defined(__x86_64__)
  /* Enable Alignment Checking on x86_64 */
  __asm__("pushf\norl $0x40000,(%rsp)\npopf");
# endif
#endif 
}

// Return -1 if size cannot be obtained.
std::streamoff get_file_size(std::istream& is) {
  if(!is.good()) return -1;
  std::streampos cpos = is.tellg();
  if(!is.good()) { is.clear(); return -1; }
  is.seekg(0, std::ios::end);
  if(!is.good()) { is.clear(); return -1; }
  std::streamoff res = is.tellg() - cpos;
  if(!is.good()) { is.clear(); return -1; }
  is.seekg(cpos);
  return res;
}

template<long int n>
struct ConstFloorLog2 {
  static const int val = ConstFloorLog2<n / 2>::val + 1;
};
template<>
struct ConstFloorLog2<1> {
  static const int val = 0;
};
// Return length random bits
uint64_t random_bits(int length) {
  uint64_t res = 0;
  for(int i = 0; i < length; i += ConstFloorLog2<RAND_MAX>::val) {
    res ^= (uint64_t)random() << i;
  }
  return res & ((uint64_t)-1 >> (bsizeof(uint64_t) - length));
}

bool isblunt(char c) {
  return isalnum(c) || c == '_' || c == '-' || c == '/' || c == '.';
}
std::string quote_arg(const std::string& arg) {
  if(std::all_of(arg.begin(), arg.end(), isblunt))
    return arg;

  std::string res("'");
  size_t pos = 0;
  while(true) {
    size_t qpos = arg.find_first_of("'", pos);
    res += arg.substr(pos, qpos - pos);
    if(qpos == std::string::npos) break;
    res += "'\\''";
    pos = qpos + 1;
  }
  res += "'";
  return res;
}

} // namespace jellyfish