dn_comp.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #include <string.h>
  2. #include <resolv.h>
  3. #include "libc.h"
  4. /* RFC 1035 message compression */
  5. /* label start offsets of a compressed domain name s */
  6. static int getoffs(short *offs, const unsigned char *base, const unsigned char *s)
  7. {
  8. int i=0;
  9. for (;;) {
  10. while (*s & 0xc0) {
  11. if ((*s & 0xc0) != 0xc0) return 0;
  12. s = base + ((s[0]&0x3f)<<8 | s[1]);
  13. }
  14. if (!*s) return i;
  15. if (s-base >= 0x4000) return 0;
  16. offs[i++] = s-base;
  17. s += *s + 1;
  18. }
  19. }
  20. /* label lengths of an ascii domain name s */
  21. static int getlens(unsigned char *lens, const char *s, int l)
  22. {
  23. int i=0,j=0,k=0;
  24. for (;;) {
  25. for (; j<l && s[j]!='.'; j++);
  26. if (j-k-1u > 62) return 0;
  27. lens[i++] = j-k;
  28. if (j==l) return i;
  29. k = ++j;
  30. }
  31. }
  32. /* longest suffix match of an ascii domain with a compressed domain name dn */
  33. static int match(int *offset, const unsigned char *base, const unsigned char *dn,
  34. const char *end, const unsigned char *lens, int nlen)
  35. {
  36. int l, o, m=0;
  37. short offs[128];
  38. int noff = getoffs(offs, base, dn);
  39. if (!noff) return 0;
  40. for (;;) {
  41. l = lens[--nlen];
  42. o = offs[--noff];
  43. end -= l;
  44. if (l != base[o] || memcmp(base+o+1, end, l))
  45. return m;
  46. *offset = o;
  47. m += l;
  48. if (nlen) m++;
  49. if (!nlen || !noff) return m;
  50. end--;
  51. }
  52. }
  53. int __dn_comp(const char *src, unsigned char *dst, int space, unsigned char **dnptrs, unsigned char **lastdnptr)
  54. {
  55. int i, j, n, m=0, offset, bestlen=0, bestoff;
  56. unsigned char lens[127];
  57. unsigned char **p;
  58. const char *end;
  59. size_t l = strnlen(src, 255);
  60. if (l && src[l-1] == '.') l--;
  61. if (l>253 || space<=0) return -1;
  62. if (!l) {
  63. *dst = 0;
  64. return 1;
  65. }
  66. end = src+l;
  67. n = getlens(lens, src, l);
  68. if (!n) return -1;
  69. p = dnptrs;
  70. if (p && *p) for (p++; *p; p++) {
  71. m = match(&offset, *dnptrs, *p, end, lens, n);
  72. if (m > bestlen) {
  73. bestlen = m;
  74. bestoff = offset;
  75. if (m == l)
  76. break;
  77. }
  78. }
  79. /* encode unmatched part */
  80. if (space < l-bestlen+2+(bestlen-1 < l-1)) return -1;
  81. memcpy(dst+1, src, l-bestlen);
  82. for (i=j=0; i<l-bestlen; i+=lens[j++]+1)
  83. dst[i] = lens[j];
  84. /* add tail */
  85. if (bestlen) {
  86. dst[i++] = 0xc0 | bestoff>>8;
  87. dst[i++] = bestoff;
  88. } else
  89. dst[i++] = 0;
  90. /* save dst pointer */
  91. if (i>2 && lastdnptr && dnptrs && *dnptrs) {
  92. while (*p) p++;
  93. if (p+1 < lastdnptr) {
  94. *p++ = dst;
  95. *p=0;
  96. }
  97. }
  98. return i;
  99. }
  100. weak_alias(__dn_comp, dn_comp);