lzstest.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*
  2. * OpenConnect (SSL + DTLS) VPN client
  3. *
  4. * Copyright © 2008-2015 Intel Corporation.
  5. *
  6. * Author: David Woodhouse <dwmw2@infradead.org>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * version 2.1, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. */
  17. #define __OPENCONNECT_INTERNAL_H__
  18. struct oc_packed_uint16_t {
  19. unsigned short d;
  20. } __attribute__((packed));
  21. #include "../lzs.c" /* lzs_decompress() / lzs_compress() */
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <errno.h>
  25. #include <string.h>
  26. #define NR_PKTS 2048
  27. #define MAX_PKT 65536
  28. /*
  29. * Compressed data can encode an 11-bit offset of zero, which is invalid.
  30. * 10 00000000000 00 110000000
  31. * Compr offset len end marker
  32. *
  33. * In bytes:
  34. * 1000.0000 0000.0001 1000.0000
  35. */
  36. static const unsigned char zero_ofs[] = { 0x80, 0x01, 0x80 };
  37. int main(void)
  38. {
  39. int i, j, ret;
  40. int pktlen;
  41. unsigned char pktbuf[MAX_PKT + 3];
  42. unsigned char comprbuf[MAX_PKT * 9 / 8 + 2];
  43. unsigned char uncomprbuf[MAX_PKT];
  44. srand(0xdeadbeef);
  45. uncomprbuf[0] = 0x5a;
  46. uncomprbuf[1] = 0xa5;
  47. ret = lzs_decompress(uncomprbuf, 3, zero_ofs, sizeof(zero_ofs));
  48. if (ret != -EINVAL) {
  49. fprintf(stderr, "Decompressing zero-offset should have failed -EINVAL: %d, bytes %08x %08x\n",
  50. ret, uncomprbuf[0], uncomprbuf[1]);
  51. exit(1);
  52. }
  53. for (i = 0; i < NR_PKTS; i++) {
  54. if (i)
  55. pktlen = (rand() % MAX_PKT) + 1;
  56. else
  57. pktlen = MAX_PKT;
  58. for (j = 0; j < pktlen; j++)
  59. pktbuf[j] = rand();
  60. ret = lzs_compress(comprbuf, sizeof(comprbuf), pktbuf, pktlen);
  61. if (ret < 0) {
  62. fprintf(stderr, "Compressing packet %d failed: %s\n", i, strerror(-ret));
  63. exit(1);
  64. }
  65. ret = lzs_decompress(uncomprbuf, pktlen, comprbuf, sizeof(comprbuf));
  66. if (ret != pktlen) {
  67. fprintf(stderr, "Compressing packet %d failed\n", i);
  68. exit(1);
  69. }
  70. if (memcmp(uncomprbuf, pktbuf, pktlen)) {
  71. fprintf(stderr, "Comparing packet %d failed\n", i);
  72. exit(1);
  73. }
  74. }
  75. return 0;
  76. }