file2lz4c.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 ZeroTier Networks LLC
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. /* Converts files to LZ4-compressed C arrays, used in building installers. */
  28. #include <stdio.h>
  29. #include <string.h>
  30. #include <stdlib.h>
  31. #include <stdint.h>
  32. #include <iostream>
  33. #include "node/Utils.hpp"
  34. #include "ext/lz4/lz4.h"
  35. #include "ext/lz4/lz4hc.h"
  36. using namespace ZeroTier;
  37. int main(int argc,char **argv)
  38. {
  39. char tmp[16];
  40. if (argc != 3) {
  41. std::cerr << "Usage: " << argv[0] << " <file> <name of C array>" << std::endl;
  42. return -1;
  43. }
  44. std::string buf;
  45. if (!Utils::readFile(argv[1],buf)) {
  46. std::cerr << "Could not read " << argv[1] << std::endl;
  47. return -1;
  48. }
  49. unsigned char *compbuf = new unsigned char[LZ4_compressBound((int)buf.length())];
  50. int complen = LZ4_compressHC(buf.data(),(char *)compbuf,(int)buf.length());
  51. if (complen <= 0) {
  52. std::cerr << "Error compressing data." << std::endl;
  53. return -1;
  54. }
  55. std::cout << "#define " << argv[2] << "_UNCOMPRESSED_LEN " << buf.length() << std::endl;
  56. std::cout << "#define " << argv[2] << "_LZ4_LEN " << complen << std::endl;
  57. std::cout << "static const unsigned char " << argv[2] << '[' << argv[2] << "_LZ4_LEN] = {";
  58. for(int i=0;i<complen;++i) {
  59. if (!(i % 15))
  60. std::cout << std::endl << '\t';
  61. Utils::snprintf(tmp,sizeof(tmp),"0x%.2x",(unsigned int)compbuf[i]);
  62. std::cout << tmp;
  63. if (i != (complen - 1))
  64. std::cout << ',';
  65. }
  66. std::cout << std::endl << "};" << std::endl;
  67. delete [] compbuf;
  68. return 0;
  69. }