static int ns_name_pack(const unsigned char *src, unsigned char *dst, int dstsiz, unsigned char **dnptrs, unsigned char **lastdnptr) { unsigned char *dstp; unsigned char **cpp, **lpp, *eob, *msg; unsigned char *srcp; int n, l, first = 1; srcp = (unsigned char *) src; dstp = dst; eob = dstp + dstsiz; lpp = cpp = NULL; if (dnptrs != NULL) { if ((msg = *dnptrs++) != NULL) { for (cpp = dnptrs; *cpp != NULL; cpp++); lpp = cpp; } } else { msg = NULL; } // Make sure the domain we are about to add is legal l = 0; do { n = *srcp; if ((n & NS_CMPRSFLGS) != 0) { errno = EMSGSIZE; return -1; } l += n + 1; if (l > NS_MAXCDNAME) { errno = EMSGSIZE; return -1; } srcp += n + 1; } while (n != 0); // From here on we need to reset compression pointer array on error srcp = (unsigned char *) src; do { // Look to see if we can use pointers n = *srcp; if (n != 0 && msg != NULL) { l = dn_find(srcp, msg, dnptrs, lpp); if (l >= 0) { if (dstp + 1 >= eob) goto cleanup; *dstp++ = (l >> 8) | NS_CMPRSFLGS; *dstp++ = l % 256; return dstp - dst; } // Not found, save it if (lastdnptr != NULL && cpp < lastdnptr - 1 && (dstp - msg) < 0x4000 && first) { *cpp++ = dstp; *cpp = NULL; first = 0; } } // Copy label to buffer if (n & NS_CMPRSFLGS) goto cleanup; if (dstp + 1 + n >= eob) goto cleanup; memcpy(dstp, srcp, n + 1); srcp += n + 1; dstp += n + 1; } while (n != 0);
/* * MRns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr) * Pack domain name 'domain' into 'comp_dn'. * return: * Size of the compressed name, or -1. * notes: * 'dnptrs' is an array of pointers to previous compressed names. * dnptrs[0] is a pointer to the beginning of the message. The array * ends with NULL. * 'lastdnptr' is a pointer to the end of the array pointed to * by 'dnptrs'. * Side effects: * The list of pointers in dnptrs is updated for labels inserted into * the message as we compress the name. If 'dnptr' is NULL, we don't * try to compress names. If 'lastdnptr' is NULL, we don't update the * list. */ int MRns_name_pack(const u_char *src, u_char *dst, unsigned dstsiz, const u_char **dnptrs, const u_char **lastdnptr) { u_char *dstp; const u_char **cpp, **lpp, *eob, *msg; const u_char *srcp; unsigned n; int l; srcp = src; dstp = dst; eob = dstp + dstsiz; lpp = cpp = NULL; if (dnptrs != NULL) { if ((msg = *dnptrs++) != NULL) { for (cpp = dnptrs; *cpp != NULL; cpp++) (void)NULL; lpp = cpp; /* end of list to search */ } } else msg = NULL; /* make sure the domain we are about to add is legal */ l = 0; do { n = *srcp; if ((n & NS_CMPRSFLGS) != 0) { errno = EMSGSIZE; return (-1); } l += n + 1; if (l > MAXCDNAME) { errno = EMSGSIZE; return (-1); } srcp += n + 1; } while (n != 0); /* from here on we need to reset compression pointer array on error */ srcp = src; do { /* Look to see if we can use pointers. */ n = *srcp; if (n != 0 && msg != NULL) { l = dn_find(srcp, msg, (const u_char * const *)dnptrs, (const u_char * const *)lpp); if (l >= 0) { if (dstp + 1 >= eob) { goto cleanup; } *dstp++ = (l >> 8) | NS_CMPRSFLGS; *dstp++ = l % 256; return (dstp - dst); } /* Not found, save it. */ if (lastdnptr != NULL && cpp < lastdnptr - 1 && (dstp - msg) < 0x4000) { *cpp++ = dstp; *cpp = NULL; } } /* copy label to buffer */ if (n & NS_CMPRSFLGS) { /* Should not happen. */ goto cleanup; } if (dstp + 1 + n >= eob) { goto cleanup; } memcpy(dstp, srcp, n + 1); srcp += n + 1; dstp += n + 1; } while (n != 0);
/* * Compress domain name 'exp_dn' into 'comp_dn'. * Return the size of the compressed name or -1. * 'length' is the size of the array pointed to by 'comp_dn'. * 'dnptrs' is a list of pointers to previous compressed names. dnptrs[0] * is a pointer to the beginning of the message. The list ends with NULL. * 'lastdnptr' is a pointer to the end of the arrary pointed to * by 'dnptrs'. Side effect is to update the list of pointers for * labels inserted into the message as we compress the name. * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr' * is NULL, we don't update the list. */ int dn_comp(const char *exp_dn, u_char *comp_dn, int length, u_char **dnptrs, u_char **lastdnptr) { u_char *cp, *dn; int c, l; u_char **cpp, **lpp, *sp, *eob; u_char *msg; dn = (u_char *)exp_dn; cp = comp_dn; eob = cp + length; lpp = cpp = NULL; if (dnptrs != NULL) { if ((msg = *dnptrs++) != NULL) { for (cpp = dnptrs; *cpp != NULL; cpp++) ; lpp = cpp; /* end of list to search */ } } else msg = NULL; for (c = *dn++; c != '\0'; ) { /* look to see if we can use pointers */ if (msg != NULL) { if ((l = dn_find(dn-1, msg, dnptrs, lpp)) >= 0) { if (cp+1 >= eob) return (-1); *cp++ = (l >> 8) | INDIR_MASK; *cp++ = l % 256; return (cp - comp_dn); } /* not found, save it */ if (lastdnptr != NULL && cpp < lastdnptr-1) { *cpp++ = cp; *cpp = NULL; } } sp = cp++; /* save ptr to length byte */ do { if (c == '.') { c = *dn++; break; } if (c == '\\') { if ((c = *dn++) == '\0') break; } if (cp >= eob) { if (msg != NULL) *lpp = NULL; return (-1); } *cp++ = c; } while ((c = *dn++) != '\0'); /* catch trailing '.'s but not '..' */ if ((l = cp - sp - 1) == 0 && c == '\0') { cp--; break; } if (l <= 0 || l > MAXLABEL) { if (msg != NULL) *lpp = NULL; return (-1); } *sp = l; } if (cp >= eob) { if (msg != NULL) *lpp = NULL; return (-1); } *cp++ = '\0'; return (cp - comp_dn); }