header.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* $MirOS: contrib/hosted/fwcf/header.c,v 1.7 2006/09/26 10:25:03 tg Exp $ */
  2. /*-
  3. * Copyright (c) 2006
  4. * Thorsten Glaser <tg@mirbsd.de>
  5. *
  6. * Licensee is hereby permitted to deal in this work without restric-
  7. * tion, including unlimited rights to use, publicly perform, modify,
  8. * merge, distribute, sell, give away or sublicence, provided all co-
  9. * pyright notices above, these terms and the disclaimer are retained
  10. * in all redistributions or reproduced in accompanying documentation
  11. * or other materials provided with binary redistributions.
  12. *
  13. * Licensor offers the work "AS IS" and WITHOUT WARRANTY of any kind,
  14. * express, or implied, to the maximum extent permitted by applicable
  15. * law, without malicious intent or gross negligence; in no event may
  16. * licensor, an author or contributor be held liable for any indirect
  17. * or other damage, or direct damage except proven a consequence of a
  18. * direct error of said person and intended use of this work, loss or
  19. * other issues arising in any way out of its use, even if advised of
  20. * the possibility of such damage or existence of a defect.
  21. */
  22. #include <sys/param.h>
  23. #include <err.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #ifdef DEBUG_ADLER
  27. #include <zlib.h>
  28. #endif
  29. #include "defs.h"
  30. #include "adler.h"
  31. #include "pack.h"
  32. __RCSID("$MirOS: contrib/hosted/fwcf/header.c,v 1.7 2006/09/26 10:25:03 tg Exp $"
  33. "\t" ADLER_H);
  34. char *
  35. mkheader(char *f_header, size_t hdrsize, uint32_t outer_len,
  36. uint32_t inner_len, uint8_t algo)
  37. {
  38. char *hdrptr = f_header;
  39. size_t hdrleft = hdrsize;
  40. #ifdef DEBUG
  41. fprintf(stderr, "header: inner=%d outer=%d\n", inner_len & 0xFFFFFF,
  42. outer_len & 0xFFFFFF);
  43. #endif
  44. STOREB('F');
  45. STOREB('W');
  46. STOREB('C');
  47. STOREB('F');
  48. outer_len = (outer_len & 0xFFFFFF) | (FWCF_VER << 24);
  49. STORED(outer_len);
  50. inner_len = (inner_len & 0xFFFFFF) | (algo << 24);
  51. STORED(inner_len);
  52. return (hdrptr);
  53. }
  54. char *
  55. mktrailer(char *data, size_t len)
  56. {
  57. char *hdrptr = data + len;
  58. size_t hdrleft = 4;
  59. ADLER_DECL;
  60. #ifdef DEBUG_ADLER
  61. uint32_t adler = adler32(1, (uint8_t *)data, len);
  62. #endif
  63. ADLER_CALC(data);
  64. #ifdef DEBUG_ADLER
  65. if ((s1 != (adler & 0xFFFF)) || (s2 != (adler >> 16)))
  66. errx(255, "adler32 implementation error: %04X%04X vs %08X",
  67. s2, s1, adler);
  68. #endif
  69. STOREW(s1);
  70. STOREW(s2);
  71. return (data);
  72. }