1776628609.FILE5_47-57-g797a2755.add-le-be-guid.patch 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. Subject: Add LE/BE GUID
  2. Origin: upstream, commit FILE5_47-57-g797a2755 <https://github.com/file/file/commit/FILE5_47-57-g797a2755>
  3. Author: Christos Zoulas <christos@zoulas.com>
  4. Date: Sun Apr 19 19:56:49 2026 +0000
  5. --- a/doc/magic.man
  6. +++ b/doc/magic.man
  7. @@ -373,7 +373,9 @@
  8. .Dv rel-oid-iri .
  9. These types can be followed by an optional numeric size, which indicates
  10. the field width in bytes.
  11. -.It Dv guid
  12. +.It Dv guid ,
  13. +.It Dv leguid ,
  14. +.It Dv beguid
  15. A Globally Unique Identifier, parsed and printed as
  16. XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX.
  17. It's format is a string.
  18. --- a/src/Makefile.am
  19. +++ b/src/Makefile.am
  20. @@ -10,7 +10,8 @@
  21. libmagic_la_SOURCES = buffer.c magic.c apprentice.c softmagic.c ascmagic.c \
  22. encoding.c compress.c is_csv.c is_json.c is_simh.c is_tar.c readelf.c \
  23. print.c fsmagic.c funcs.c file.h readelf.h tar.h apptype.c der.c der.h \
  24. - file_opts.h elfclass.h mygetopt.h cdf.c cdf_time.c readcdf.c cdf.h
  25. + file_opts.h elfclass.h mygetopt.h cdf.c cdf_time.c readcdf.c cdf.h \
  26. + swap.c swap.h
  27. libmagic_la_LDFLAGS = -no-undefined -version-info 1:0:0
  28. if MINGW
  29. MINGWLIBS = -lgnurx -lshlwapi
  30. --- a/src/apprentice.c
  31. +++ b/src/apprentice.c
  32. @@ -36,6 +36,7 @@
  33. #endif /* lint */
  34. #include "magic.h"
  35. +#include "swap.h"
  36. #include <stdlib.h>
  37. #ifdef HAVE_UNISTD_H
  38. #include <unistd.h>
  39. @@ -50,12 +51,6 @@
  40. #endif
  41. #include <dirent.h>
  42. #include <limits.h>
  43. -#ifdef HAVE_BYTESWAP_H
  44. -#include <byteswap.h>
  45. -#endif
  46. -#ifdef HAVE_SYS_BSWAP_H
  47. -#include <sys/bswap.h>
  48. -#endif
  49. #define EATAB {while (*l && isascii(CAST(unsigned char, *l)) && \
  50. @@ -131,20 +126,6 @@
  51. file_private void byteswap(struct magic *, uint32_t);
  52. file_private void bs1(struct magic *);
  53. -#if defined(HAVE_BYTESWAP_H)
  54. -#define swap2(x) bswap_16(x)
  55. -#define swap4(x) bswap_32(x)
  56. -#define swap8(x) bswap_64(x)
  57. -#elif defined(HAVE_SYS_BSWAP_H)
  58. -#define swap2(x) bswap16(x)
  59. -#define swap4(x) bswap32(x)
  60. -#define swap8(x) bswap64(x)
  61. -#else
  62. -file_private uint16_t swap2(uint16_t);
  63. -file_private uint32_t swap4(uint32_t);
  64. -file_private uint64_t swap8(uint64_t);
  65. -#endif
  66. -
  67. file_private char *mkdbname(struct magic_set *, const char *, int);
  68. file_private struct magic_map *apprentice_buf(struct magic_set *, struct magic *,
  69. size_t);
  70. @@ -285,6 +266,8 @@
  71. { XX("clear"), FILE_CLEAR, FILE_FMT_NONE },
  72. { XX("der"), FILE_DER, FILE_FMT_STR },
  73. { XX("guid"), FILE_GUID, FILE_FMT_STR },
  74. + { XX("leguid"), FILE_LEGUID, FILE_FMT_STR },
  75. + { XX("beguid"), FILE_BEGUID, FILE_FMT_STR },
  76. { XX("offset"), FILE_OFFSET, FILE_FMT_QUAD },
  77. { XX("bevarint"), FILE_BEVARINT, FILE_FMT_STR },
  78. { XX("levarint"), FILE_LEVARINT, FILE_FMT_STR },
  79. @@ -926,6 +909,8 @@
  80. case FILE_LEVARINT:
  81. return 8;
  82. + case FILE_LEGUID:
  83. + case FILE_BEGUID:
  84. case FILE_GUID:
  85. return 16;
  86. @@ -989,6 +974,8 @@
  87. case FILE_BEVARINT:
  88. case FILE_LEVARINT:
  89. case FILE_GUID:
  90. + case FILE_LEGUID:
  91. + case FILE_BEGUID:
  92. case FILE_BEID3:
  93. case FILE_LEID3:
  94. case FILE_OFFSET:
  95. @@ -1251,6 +1238,8 @@
  96. case FILE_LEVARINT:
  97. case FILE_DER:
  98. case FILE_GUID:
  99. + case FILE_LEGUID:
  100. + case FILE_BEGUID:
  101. case FILE_OFFSET:
  102. case FILE_MSDOSDATE:
  103. case FILE_BEMSDOSDATE:
  104. @@ -1727,6 +1716,8 @@
  105. case FILE_CLEAR:
  106. case FILE_DER:
  107. case FILE_GUID:
  108. + case FILE_LEGUID:
  109. + case FILE_BEGUID:
  110. case FILE_OCTAL:
  111. break;
  112. default:
  113. @@ -2947,6 +2938,8 @@
  114. if (errno == 0)
  115. *p = ep;
  116. return 0;
  117. + case FILE_BEGUID:
  118. + case FILE_LEGUID:
  119. case FILE_GUID:
  120. if (file_parse_guid(*p, m->value.guid) == -1) {
  121. file_magwarn(ms, "Error parsing guid `%s'", *p);
  122. @@ -3422,7 +3415,7 @@
  123. ptr = CAST(uint32_t *, map->p);
  124. if (*ptr != MAGICNO) {
  125. - if (swap4(*ptr) != MAGICNO) {
  126. + if (file_swap4(*ptr) != MAGICNO) {
  127. file_error(ms, 0, "bad magic in `%s'", dbname);
  128. return -1;
  129. }
  130. @@ -3430,7 +3423,7 @@
  131. } else
  132. needsbyteswap = 0;
  133. if (needsbyteswap)
  134. - version = swap4(ptr[1]);
  135. + version = file_swap4(ptr[1]);
  136. else
  137. version = ptr[1];
  138. if (version != VERSIONNO) {
  139. @@ -3443,7 +3436,7 @@
  140. nentries = 0;
  141. for (i = 0; i < MAGIC_SETS; i++) {
  142. if (needsbyteswap)
  143. - map->nmagic[i] = swap4(ptr[i + 2]);
  144. + map->nmagic[i] = file_swap4(ptr[i + 2]);
  145. else
  146. map->nmagic[i] = ptr[i + 2];
  147. if (i != MAGIC_SETS - 1)
  148. @@ -3576,69 +3569,6 @@
  149. bs1(&magic[i]);
  150. }
  151. -#if !defined(HAVE_BYTESWAP_H) && !defined(HAVE_SYS_BSWAP_H)
  152. -/*
  153. - * swap a short
  154. - */
  155. -file_private uint16_t
  156. -swap2(uint16_t sv)
  157. -{
  158. - uint16_t rv;
  159. - uint8_t *s = RCAST(uint8_t *, RCAST(void *, &sv));
  160. - uint8_t *d = RCAST(uint8_t *, RCAST(void *, &rv));
  161. - d[0] = s[1];
  162. - d[1] = s[0];
  163. - return rv;
  164. -}
  165. -
  166. -/*
  167. - * swap an int
  168. - */
  169. -file_private uint32_t
  170. -swap4(uint32_t sv)
  171. -{
  172. - uint32_t rv;
  173. - uint8_t *s = RCAST(uint8_t *, RCAST(void *, &sv));
  174. - uint8_t *d = RCAST(uint8_t *, RCAST(void *, &rv));
  175. - d[0] = s[3];
  176. - d[1] = s[2];
  177. - d[2] = s[1];
  178. - d[3] = s[0];
  179. - return rv;
  180. -}
  181. -
  182. -/*
  183. - * swap a quad
  184. - */
  185. -file_private uint64_t
  186. -swap8(uint64_t sv)
  187. -{
  188. - uint64_t rv;
  189. - uint8_t *s = RCAST(uint8_t *, RCAST(void *, &sv));
  190. - uint8_t *d = RCAST(uint8_t *, RCAST(void *, &rv));
  191. -# if 0
  192. - d[0] = s[3];
  193. - d[1] = s[2];
  194. - d[2] = s[1];
  195. - d[3] = s[0];
  196. - d[4] = s[7];
  197. - d[5] = s[6];
  198. - d[6] = s[5];
  199. - d[7] = s[4];
  200. -# else
  201. - d[0] = s[7];
  202. - d[1] = s[6];
  203. - d[2] = s[5];
  204. - d[3] = s[4];
  205. - d[4] = s[3];
  206. - d[5] = s[2];
  207. - d[6] = s[1];
  208. - d[7] = s[0];
  209. -# endif
  210. - return rv;
  211. -}
  212. -#endif
  213. -
  214. file_protected uintmax_t
  215. file_varint2uintmax_t(const unsigned char *us, int t, size_t *l)
  216. {
  217. @@ -3675,16 +3605,16 @@
  218. file_private void
  219. bs1(struct magic *m)
  220. {
  221. - m->flag = swap2(m->flag);
  222. - m->offset = swap4(CAST(uint32_t, m->offset));
  223. - m->in_offset = swap4(CAST(uint32_t, m->in_offset));
  224. - m->lineno = swap4(CAST(uint32_t, m->lineno));
  225. + m->flag = file_swap2(m->flag);
  226. + m->offset = file_swap4(CAST(uint32_t, m->offset));
  227. + m->in_offset = file_swap4(CAST(uint32_t, m->in_offset));
  228. + m->lineno = file_swap4(CAST(uint32_t, m->lineno));
  229. if (IS_STRING(m->type)) {
  230. - m->str_range = swap4(m->str_range);
  231. - m->str_flags = swap4(m->str_flags);
  232. + m->str_range = file_swap4(m->str_range);
  233. + m->str_flags = file_swap4(m->str_flags);
  234. } else {
  235. - m->value.q = swap8(m->value.q);
  236. - m->num_mask = swap8(m->num_mask);
  237. + m->value.q = file_swap8(m->value.q);
  238. + m->num_mask = file_swap8(m->num_mask);
  239. }
  240. }
  241. @@ -3707,6 +3637,7 @@
  242. return FILE_BADSIZE;
  243. }
  244. }
  245. +
  246. file_protected size_t
  247. file_pstring_get_length(struct magic_set *ms, const struct magic *m,
  248. const char *ss)
  249. --- a/src/file.h
  250. +++ b/src/file.h
  251. @@ -179,7 +179,7 @@
  252. #define MAXstring 128 /* max len of "string" types */
  253. #define MAGICNO 0xF11E041C
  254. -#define VERSIONNO 20
  255. +#define VERSIONNO 21
  256. #define FILE_MAGICSIZE 432
  257. #define FILE_GUID_SIZE sizeof("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")
  258. @@ -292,17 +292,19 @@
  259. #define FILE_CLEAR 47
  260. #define FILE_DER 48
  261. #define FILE_GUID 49
  262. -#define FILE_OFFSET 50
  263. -#define FILE_BEVARINT 51
  264. -#define FILE_LEVARINT 52
  265. -#define FILE_MSDOSDATE 53
  266. -#define FILE_LEMSDOSDATE 54
  267. -#define FILE_BEMSDOSDATE 55
  268. -#define FILE_MSDOSTIME 56
  269. -#define FILE_LEMSDOSTIME 57
  270. -#define FILE_BEMSDOSTIME 58
  271. -#define FILE_OCTAL 59
  272. -#define FILE_NAMES_SIZE 60 /* size of array to contain all names */
  273. +#define FILE_LEGUID 50
  274. +#define FILE_BEGUID 51
  275. +#define FILE_OFFSET 52
  276. +#define FILE_BEVARINT 53
  277. +#define FILE_LEVARINT 54
  278. +#define FILE_MSDOSDATE 55
  279. +#define FILE_LEMSDOSDATE 56
  280. +#define FILE_BEMSDOSDATE 57
  281. +#define FILE_MSDOSTIME 58
  282. +#define FILE_LEMSDOSTIME 59
  283. +#define FILE_BEMSDOSTIME 60
  284. +#define FILE_OCTAL 61
  285. +#define FILE_NAMES_SIZE 62 /* size of array to contain all names */
  286. #define IS_STRING(t) \
  287. ((t) == FILE_STRING || \
  288. @@ -557,7 +559,8 @@
  289. file_protected char *file_copystr(char *, size_t, size_t, const char *);
  290. file_protected int file_checkfmt(char *, size_t, const char *);
  291. file_protected size_t file_printedlen(const struct magic_set *);
  292. -file_protected int file_print_guid(char *, size_t, const uint64_t *);
  293. +file_protected int file_print_leguid(char *, size_t, const uint64_t *);
  294. +file_protected int file_print_beguid(char *, size_t, const uint64_t *);
  295. file_protected int file_parse_guid(const char *, uint64_t *);
  296. file_protected int file_replace(struct magic_set *, const char *, const char *);
  297. file_protected int file_printf(struct magic_set *, const char *, ...)
  298. --- a/src/funcs.c
  299. +++ b/src/funcs.c
  300. @@ -31,6 +31,7 @@
  301. #endif /* lint */
  302. #include "magic.h"
  303. +#include "swap.h"
  304. #include <assert.h>
  305. #include <stdarg.h>
  306. #include <stdlib.h>
  307. @@ -934,12 +935,9 @@
  308. return 0;
  309. }
  310. -file_protected int
  311. -file_print_guid(char *str, size_t len, const uint64_t *guid)
  312. +file_private int
  313. +file_print_guid(char *str, size_t len, const struct guid *g)
  314. {
  315. - const struct guid *g = CAST(const struct guid *,
  316. - CAST(const void *, guid));
  317. -
  318. #ifndef WIN32
  319. return snprintf(str, len, "%.8X-%.4hX-%.4hX-%.2hhX%.2hhX-"
  320. "%.2hhX%.2hhX%.2hhX%.2hhX%.2hhX%.2hhX",
  321. @@ -956,6 +954,26 @@
  322. }
  323. file_protected int
  324. +file_print_leguid(char *str, size_t len, const uint64_t *guid)
  325. +{
  326. + const struct guid *g = CAST(const struct guid *,
  327. + CAST(const void *, guid));
  328. + return file_print_guid(str, len, g);
  329. +}
  330. +
  331. +file_protected int
  332. +file_print_beguid(char *str, size_t len, const uint64_t *guid)
  333. +{
  334. + const struct guid *g = CAST(const struct guid *,
  335. + CAST(const void *, guid));
  336. + struct guid gg = *g;
  337. + gg.data1 = file_swap4(gg.data1);
  338. + gg.data2 = file_swap2(gg.data2);
  339. + gg.data3 = file_swap2(gg.data3);
  340. + return file_print_guid(str, len, &gg);
  341. +}
  342. +
  343. +file_protected int
  344. file_pipe_closexec(int *fds)
  345. {
  346. #ifdef __MINGW32__
  347. --- a/src/print.c
  348. +++ b/src/print.c
  349. @@ -227,12 +227,17 @@
  350. case FILE_DER:
  351. (void) fprintf(stderr, "'%s'", m->value.s);
  352. break;
  353. + case FILE_LEGUID:
  354. case FILE_GUID:
  355. - (void) file_print_guid(tbuf, sizeof(tbuf),
  356. + (void) file_print_leguid(tbuf, sizeof(tbuf),
  357. + m->value.guid);
  358. + (void) fprintf(stderr, "%s", tbuf);
  359. + break;
  360. + case FILE_BEGUID:
  361. + (void) file_print_beguid(tbuf, sizeof(tbuf),
  362. m->value.guid);
  363. (void) fprintf(stderr, "%s", tbuf);
  364. break;
  365. -
  366. default:
  367. (void) fprintf(stderr, "*bad type %d*", m->type);
  368. break;
  369. --- a/src/softmagic.c
  370. +++ b/src/softmagic.c
  371. @@ -802,7 +802,13 @@
  372. return -1;
  373. break;
  374. case FILE_GUID:
  375. - (void) file_print_guid(buf, sizeof(buf), ms->ms_value.guid);
  376. + case FILE_LEGUID:
  377. + (void) file_print_leguid(buf, sizeof(buf), ms->ms_value.guid);
  378. + if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
  379. + return -1;
  380. + break;
  381. + case FILE_BEGUID:
  382. + (void) file_print_beguid(buf, sizeof(buf), ms->ms_value.guid);
  383. if (file_printf(ms, F(ms, desc, "%s"), buf) == -1)
  384. return -1;
  385. break;
  386. @@ -965,6 +971,8 @@
  387. }
  388. break;
  389. + case FILE_BEGUID:
  390. + case FILE_LEGUID:
  391. case FILE_GUID:
  392. o = CAST(int32_t, (ms->offset + 2 * sizeof(uint64_t)));
  393. break;
  394. @@ -1326,6 +1334,8 @@
  395. case FILE_USE:
  396. case FILE_DER:
  397. case FILE_GUID:
  398. + case FILE_LEGUID:
  399. + case FILE_BEGUID:
  400. return 1;
  401. default:
  402. file_magerror(ms, "invalid type %d in mconvert()", m->type);
  403. @@ -1891,6 +1901,8 @@
  404. return 0;
  405. break;
  406. + case FILE_LEGUID:
  407. + case FILE_BEGUID:
  408. case FILE_GUID:
  409. if (OFFSET_OOB(nbytes, offset, 16))
  410. return 0;
  411. @@ -2404,6 +2416,8 @@
  412. return 0;
  413. }
  414. return matched;
  415. + case FILE_BEGUID:
  416. + case FILE_LEGUID:
  417. case FILE_GUID:
  418. l = 0;
  419. v = memcmp(m->value.guid, p->guid, sizeof(p->guid));
  420. --- /dev/null
  421. +++ b/src/swap.c
  422. @@ -0,0 +1,101 @@
  423. +/*
  424. + * Copyright (c) Ian F. Darwin 1986-1995.
  425. + * Software written by Ian F. Darwin and others;
  426. + * maintained 1995-present by Christos Zoulas and others.
  427. + *
  428. + * Redistribution and use in source and binary forms, with or without
  429. + * modification, are permitted provided that the following conditions
  430. + * are met:
  431. + * 1. Redistributions of source code must retain the above copyright
  432. + * notice immediately at the beginning of the file, without modification,
  433. + * this list of conditions, and the following disclaimer.
  434. + * 2. Redistributions in binary form must reproduce the above copyright
  435. + * notice, this list of conditions and the following disclaimer in the
  436. + * documentation and/or other materials provided with the distribution.
  437. + *
  438. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  439. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  440. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  441. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  442. + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  443. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  444. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  445. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  446. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  447. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  448. + * SUCH DAMAGE.
  449. + */
  450. +/*
  451. + * apprentice - make one pass through /etc/magic, learning its secrets.
  452. + */
  453. +
  454. +#include "file.h"
  455. +
  456. +#ifndef lint
  457. +FILE_RCSID("@(#)$File$")
  458. +#endif /* lint */
  459. +
  460. +#include "swap.h"
  461. +
  462. +#if !defined(HAVE_BYTESWAP_H) && !defined(HAVE_SYS_BSWAP_H)
  463. +/*
  464. + * swap a short
  465. + */
  466. +file_protected uint16_t
  467. +file_swap2(uint16_t sv)
  468. +{
  469. + uint16_t rv;
  470. + uint8_t *s = RCAST(uint8_t *, RCAST(void *, &sv));
  471. + uint8_t *d = RCAST(uint8_t *, RCAST(void *, &rv));
  472. + d[0] = s[1];
  473. + d[1] = s[0];
  474. + return rv;
  475. +}
  476. +
  477. +/*
  478. + * swap an int
  479. + */
  480. +file_protected uint32_t
  481. +file_swap4(uint32_t sv)
  482. +{
  483. + uint32_t rv;
  484. + uint8_t *s = RCAST(uint8_t *, RCAST(void *, &sv));
  485. + uint8_t *d = RCAST(uint8_t *, RCAST(void *, &rv));
  486. + d[0] = s[3];
  487. + d[1] = s[2];
  488. + d[2] = s[1];
  489. + d[3] = s[0];
  490. + return rv;
  491. +}
  492. +
  493. +/*
  494. + * swap a quad
  495. + */
  496. +file_protected uint64_t
  497. +file_swap8(uint64_t sv)
  498. +{
  499. + uint64_t rv;
  500. + uint8_t *s = RCAST(uint8_t *, RCAST(void *, &sv));
  501. + uint8_t *d = RCAST(uint8_t *, RCAST(void *, &rv));
  502. +# if 0
  503. + d[0] = s[3];
  504. + d[1] = s[2];
  505. + d[2] = s[1];
  506. + d[3] = s[0];
  507. + d[4] = s[7];
  508. + d[5] = s[6];
  509. + d[6] = s[5];
  510. + d[7] = s[4];
  511. +# else
  512. + d[0] = s[7];
  513. + d[1] = s[6];
  514. + d[2] = s[5];
  515. + d[3] = s[4];
  516. + d[4] = s[3];
  517. + d[5] = s[2];
  518. + d[6] = s[1];
  519. + d[7] = s[0];
  520. +# endif
  521. + return rv;
  522. +}
  523. +#endif
  524. --- /dev/null
  525. +++ b/src/swap.h
  526. @@ -0,0 +1,48 @@
  527. +/*
  528. + * Copyright (c) Ian F. Darwin 1986-1995.
  529. + * Software written by Ian F. Darwin and others;
  530. + * maintained 1995-present by Christos Zoulas and others.
  531. + *
  532. + * Redistribution and use in source and binary forms, with or without
  533. + * modification, are permitted provided that the following conditions
  534. + * are met:
  535. + * 1. Redistributions of source code must retain the above copyright
  536. + * notice immediately at the beginning of the file, without modification,
  537. + * this list of conditions, and the following disclaimer.
  538. + * 2. Redistributions in binary form must reproduce the above copyright
  539. + * notice, this list of conditions and the following disclaimer in the
  540. + * documentation and/or other materials provided with the distribution.
  541. + *
  542. + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  543. + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  544. + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  545. + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  546. + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  547. + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  548. + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  549. + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  550. + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  551. + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  552. + * SUCH DAMAGE.
  553. + */
  554. +
  555. +#ifdef HAVE_BYTESWAP_H
  556. +#include <byteswap.h>
  557. +#endif
  558. +#ifdef HAVE_SYS_BSWAP_H
  559. +#include <sys/bswap.h>
  560. +#endif
  561. +
  562. +#if defined(HAVE_BYTESWAP_H)
  563. +#define file_swap2(x) bswap_16(x)
  564. +#define file_swap4(x) bswap_32(x)
  565. +#define swap8(x) bswap_64(x)
  566. +#elif defined(HAVE_SYS_BSWAP_H)
  567. +#define file_swap2(x) bswap16(x)
  568. +#define file_swap4(x) bswap32(x)
  569. +#define file_swap8(x) bswap64(x)
  570. +#else
  571. +file_protected uint16_t file_swap2(uint16_t);
  572. +file_protected uint32_t file_swap4(uint32_t);
  573. +file_protected uint64_t file_swap8(uint64_t);
  574. +#endif