libebml_ng
ull.h
Go to the documentation of this file.
1 #ifndef EBML_NG_ULL_H
2 #define EBML_NG_ULL_H
3 
4 #include "libebml_ng/struct.h"
5 
6 namespace ebml {
7  DEF_SIZE(unsigned long long) {
8  unsigned long long k = 1;
9  unsigned long long c = 256;
10 
11  while (k < 8) {
12  if (value < c) {
13  return k;
14  }
15  k++;
16  c <<= 8;
17  }
18 
19  return 8;
20  }
21 
22  DEF_PACK(unsigned long long) {
23  char* as_chars;
24 
25  if (size == 0 or size > 8) {
26  throw std::invalid_argument("pack: invalid value for size");
27  };
28 
29  if (size != 8 and (value >> (8*size)) != 0) {
30  throw std::overflow_error("pack: int too big to convert");
31  };
32 
33  as_chars = (char*)&value;
34 
35  if (std::endian::native == std::endian::little) {
36  memcpy(dest, as_chars, size);
37  _reverse(dest, 0, size - 1);
38  } else {
39  memcpy(dest, as_chars + (8 - size), size);
40  }
41 
42  return size;
43  }
44 
45  DEF_UNPACK(unsigned long long) {
46  unsigned long long ret = 0;
47  char* as_chars = (char*)&ret;
48 
49  if (size == 0) {
50  return 0;
51  };
52 
53  if (size > 8) {
54  throw std::invalid_argument("unpack<unsigned long long>: invalid value for size");
55  };
56 
57  if (std::endian::native == std::endian::little) {
58  memcpy(as_chars, src, size);
59  _reverse(as_chars, 0, size - 1);
60  } else {
61  memcpy(as_chars + (8 - size), src, size);
62  }
63 
64  return ret;
65  }
66 }
67 #endif
DEF_SIZE(long long)
Definition: ll.h:7
DEF_UNPACK(float)
Definition: double.h:30
size_t size(const std::string &value)
Definition: binary.h:8
void _reverse(char *s, unsigned int j, unsigned int k)
Reverses the contents of a character array between indices [j, k].
Definition: struct.h:26
Definition: basictypes.h:40
DEF_PACK(long long)
Definition: ll.h:22