common.h 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright 2015 The Bazel Authors. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. //
  15. // common.h -- common definitions.
  16. //
  17. #ifndef INCLUDED_DEVTOOLS_IJAR_COMMON_H
  18. #define INCLUDED_DEVTOOLS_IJAR_COMMON_H
  19. #include <stddef.h>
  20. #include <stdint.h>
  21. #include <string.h>
  22. #ifdef _WIN32
  23. #define PATH_MAX 4096
  24. typedef int mode_t;
  25. #endif // _WIN32
  26. namespace devtools_ijar {
  27. typedef unsigned long long u8;
  28. typedef uint32_t u4;
  29. typedef uint16_t u2;
  30. typedef uint8_t u1;
  31. // be = big endian, le = little endian
  32. inline u1 get_u1(const u1 *&p) {
  33. return *p++;
  34. }
  35. inline u2 get_u2be(const u1 *&p) {
  36. u4 x = (p[0] << 8) | p[1];
  37. p += 2;
  38. return x;
  39. }
  40. inline u2 get_u2le(const u1 *&p) {
  41. u4 x = (p[1] << 8) | p[0];
  42. p += 2;
  43. return x;
  44. }
  45. inline u4 get_u4be(const u1 *&p) {
  46. u4 x = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
  47. p += 4;
  48. return x;
  49. }
  50. inline u4 get_u4le(const u1 *&p) {
  51. u4 x = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
  52. p += 4;
  53. return x;
  54. }
  55. inline u8 get_u8le(const u1 *&p) {
  56. u4 lo = get_u4le(p);
  57. u4 hi = get_u4le(p);
  58. u8 x = ((u8)hi << 32) | lo;
  59. return x;
  60. }
  61. inline void put_u1(u1 *&p, u1 x) {
  62. *p++ = x;
  63. }
  64. inline void put_u2be(u1 *&p, u2 x) {
  65. *p++ = x >> 8;
  66. *p++ = x & 0xff;
  67. }
  68. inline void put_u2le(u1 *&p, u2 x) {
  69. *p++ = x & 0xff;
  70. *p++ = x >> 8;;
  71. }
  72. inline void put_u4be(u1 *&p, u4 x) {
  73. *p++ = x >> 24;
  74. *p++ = (x >> 16) & 0xff;
  75. *p++ = (x >> 8) & 0xff;
  76. *p++ = x & 0xff;
  77. }
  78. inline void put_u4le(u1 *&p, u4 x) {
  79. *p++ = x & 0xff;
  80. *p++ = (x >> 8) & 0xff;
  81. *p++ = (x >> 16) & 0xff;
  82. *p++ = x >> 24;
  83. }
  84. inline void put_u8le(u1 *&p, u8 x) {
  85. put_u4le(p, x & 0xffffffff);
  86. put_u4le(p, (x >> 32) & 0xffffffff);
  87. }
  88. // Copy n bytes from src to p, and advance p.
  89. inline void put_n(u1 *&p, const u1 *src, size_t n) {
  90. memcpy(p, src, n);
  91. p += n;
  92. }
  93. extern bool verbose;
  94. } // namespace devtools_ijar
  95. #endif // INCLUDED_DEVTOOLS_IJAR_COMMON_H