unwraps.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /* $MirOS: contrib/hosted/fwcf/unwraps.c,v 1.10 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 "pack.h"
  34. #include "sysdeps.h"
  35. __RCSID("$MirOS: contrib/hosted/fwcf/unwraps.c,v 1.10 2006/09/26 10:25:03 tg Exp $");
  36. char *
  37. fwcf_unpack(int fd, size_t *inner)
  38. {
  39. uint8_t c, hdrbuf[12];
  40. size_t outer, x_inner, x, len, maxln;
  41. char *cdata, *udata;
  42. ADLER_DECL;
  43. if (inner == NULL)
  44. inner = &x_inner;
  45. if (read(fd, hdrbuf, 12) != 12)
  46. err(1, "read");
  47. if (strncmp((const char *)hdrbuf, "FWCF", 4))
  48. errx(1, "file format error");
  49. outer = LOADT(hdrbuf + 4);
  50. /* we don't need to support older versions, but specification
  51. major 0 and 1 are compatible */
  52. if (hdrbuf[7] > FWCF_VER)
  53. errx(1, "wrong file version %02Xh", hdrbuf[7]);
  54. *inner = LOADT(hdrbuf + 8);
  55. c = hdrbuf[11];
  56. maxln = ((outer + (DEF_FLASHBLOCK - 1)) / DEF_FLASHBLOCK)
  57. * DEF_FLASHBLOCK;
  58. if (((cdata = malloc(maxln)) == NULL) ||
  59. ((udata = malloc(*inner)) == NULL))
  60. err(1, "malloc");
  61. memcpy(cdata, hdrbuf, 12);
  62. if (read(fd, cdata + 12, maxln - 12) < (ssize_t)(outer - 12))
  63. err(1, "read");
  64. len = outer - 4;
  65. ADLER_CALC(cdata);
  66. if ((s1 != LOADW(cdata + outer - 4)) ||
  67. (s2 != LOADW(cdata + outer - 2)))
  68. errx(1, "crc mismatch: %02X%02X%02X%02X != %04X%04X",
  69. (uint8_t)cdata[outer - 1], (uint8_t)cdata[outer - 2],
  70. (uint8_t)cdata[outer - 3], (uint8_t)cdata[outer - 4],
  71. s2, s1);
  72. if ((x = compressor_get(c)->decompress(udata, *inner, cdata + 12,
  73. outer - 16)) != *inner)
  74. errx(1, "size mismatch: decompressed %lu, want %lu", (u_long)x,
  75. (u_long)*inner);
  76. push_rndata((uint8_t *)cdata + outer, maxln - outer);
  77. free(cdata);
  78. #ifdef DEBUG
  79. fprintf(stderr, "fwcf_unpack: decompressed outer %lu inner %lu\n",
  80. (u_long)outer, (u_long)*inner);
  81. #endif
  82. return (udata);
  83. }