wraps.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* $MirOS: contrib/hosted/fwcf/wraps.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. #ifdef DEBUG
  25. #include <stdio.h>
  26. #endif
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include "defs.h"
  31. #include "adler.h"
  32. #include "compress.h"
  33. #include "fts_subs.h"
  34. #include "pack.h"
  35. #include "sysdeps.h"
  36. __RCSID("$MirOS: contrib/hosted/fwcf/wraps.c,v 1.7 2006/09/26 10:25:03 tg Exp $");
  37. char *
  38. fwcf_packm(const char *dir, int algo, size_t *dstsz)
  39. {
  40. char empty_data = 0, *data, *f_data = NULL;
  41. size_t i;
  42. if (dir == NULL) {
  43. data = &empty_data;
  44. i = 1;
  45. } else {
  46. ftsf_start(dir);
  47. data = (f_data = ft_packm()) + sizeof (size_t);
  48. i = *(size_t *)f_data - sizeof (size_t);
  49. }
  50. data = fwcf_pack(data, i, algo, dstsz);
  51. if (f_data != NULL)
  52. free(f_data);
  53. return (data);
  54. }
  55. char *
  56. fwcf_pack(char *odata, size_t i, int algo, size_t *dstsz)
  57. {
  58. int j;
  59. size_t k;
  60. char *data, *cdata;
  61. if (i > 0xFFFFFF)
  62. errx(1, "inner size of %lu too large", (unsigned long)i);
  63. #ifdef DEBUG
  64. fprintf(stderr, "fwcf_pack: algo %02X packing %lu\n", algo, (unsigned long)i);
  65. #endif
  66. if ((j = compressor_get(algo)->compress(&cdata, odata, i)) == -1)
  67. errx(1, "%s compression failed", compressor_get(algo)->name);
  68. /* 12 bytes header, padding to 4-byte boundary, 4 bytes trailer */
  69. k = ((j + 19) / 4) * 4;
  70. #if DEF_FLASHPART > 0xFFFFFF
  71. # error DEF_FLASHPART too large
  72. #endif
  73. if (k > DEF_FLASHPART)
  74. errx(1, "%lu bytes too large for flash partition of %lu KiB",
  75. (u_long)k, DEF_FLASHPART / 1024UL);
  76. /* padded to size of flash block */
  77. #if (DEF_FLASHBLOCK & 3)
  78. # error DEF_FLASHBLOCK must be dword-aligned
  79. #endif
  80. *dstsz = ((k + (DEF_FLASHBLOCK - 1)) / DEF_FLASHBLOCK) * DEF_FLASHBLOCK;
  81. if ((data = malloc(*dstsz)) == NULL)
  82. err(1, "malloc");
  83. mkheader(data, *dstsz, k, i, algo);
  84. memcpy(data + 12, cdata, j);
  85. free(cdata);
  86. k = j + 12;
  87. while (k & 3)
  88. data[k++] = 0;
  89. mktrailer(data, k);
  90. k += 4;
  91. pull_rndata((u_int8_t *)data + k, *dstsz - k);
  92. return (data);
  93. }