SXE_RETURN md5_to_hex(SOPHOS_MD5 * md5, char * md5_in_hex, unsigned md5_in_hex_length) { SXE_RETURN result = SXE_RETURN_OK; SXEE86("(md5=%08x%08x%08x%08x,md5_in_hex='%p',md5_in_hex_length='%u'", md5->word[3], md5->word[2], md5->word[1], md5->word[0], md5_in_hex, md5_in_hex_length); SXEA11(md5_in_hex_length == (MD5_IN_HEX_LENGTH + 1), "Incorrect length of char * for md5_to_hex(): '%u'", md5_in_hex_length); snprintf(md5_in_hex , 9, "%08x", htonl(md5->word[0])); snprintf(md5_in_hex + 8, 9, "%08x", htonl(md5->word[1])); snprintf(md5_in_hex + 16, 9, "%08x", htonl(md5->word[2])); snprintf(md5_in_hex + 24, 9, "%08x", htonl(md5->word[3])); SXEL62("md5_in_hex: '%.*s'", MD5_IN_HEX_LENGTH, md5_in_hex); SXER81("return %s", sxe_return_to_string(result)); return result; }
/* Note that the decoded_name will be NUL terminated, and therefore the * maximum_name_length_maximum should probably be 254 and not 253 (though, with * pointers, the decoded name could well be *longer* than 253 characters! */ static SXE_RETURN sxe_dns_decode_name( const unsigned char * dns_packet , unsigned dns_packet_length , unsigned name_offset , char * decoded_name , unsigned decoded_name_length_maximum, unsigned * decoded_name_length ) { SXE_RETURN result = SXE_RETURN_ERROR_INTERNAL; unsigned decoded_name_offset = 0; SXEE86("sxe_dns_decode_name(dns_packet=%p, dns_packet_length=%u, name_offset=%u, decoded_name=%p, decoded_name_length_maximum=%u, decoded_name_length=%p)", dns_packet, dns_packet_length, name_offset, decoded_name, decoded_name_length_maximum, decoded_name_length); SXEA10(decoded_name_length != 0, "A value for decoded_name_length must be provided"); SXEA10(decoded_name == NULL || decoded_name_length_maximum != 0, "NAME can not be decoded into an empty buffer"); while (name_offset < dns_packet_length) { unsigned char len = dns_packet[name_offset]; if (len == '\0') { SXEL60("NAME is terminated normally"); if (decoded_name_offset > 0) { --decoded_name_offset; } if (decoded_name != NULL) { decoded_name[decoded_name_offset] = '\0'; } *decoded_name_length = decoded_name_offset; result = SXE_RETURN_OK; goto SXE_EARLY_OUT; } else if ((len & SXE_DNS_LABEL_LENGTH_POINTER) == 0) { SXEL73("Normal label of length %u '%.*s'", len, len, &dns_packet[name_offset + 1]); /* Don't allow the NAME to exceed the known packet length */ if ((decoded_name != NULL) && (decoded_name_offset + len + 1 >= decoded_name_length_maximum)) { SXEL51("sxe_dns_decode_name(): NAME is invalid; Decoded NAME is longer than the provided buffer length %u", decoded_name_length_maximum); goto SXE_ERROR_OUT; } /* Copy the label */ if (decoded_name != NULL) { memcpy((unsigned char*)(decoded_name + decoded_name_offset), dns_packet + name_offset + 1, len); decoded_name[decoded_name_offset + len] = '.'; } decoded_name_offset += len + 1; } else if ((len & SXE_DNS_LABEL_LENGTH_POINTER) == SXE_DNS_LABEL_LENGTH_POINTER) { SXEL60("NAME includes a pointer"); name_offset = (len & ~SXE_DNS_LABEL_LENGTH_POINTER) + dns_packet[name_offset + 1]; SXEL61("New NAME offset is %u", name_offset); /* Prevent looping forever - a pointer should never point to a pointer */ if ((dns_packet[name_offset] & SXE_DNS_LABEL_LENGTH_POINTER) != 0) { SXEL51("sxe_dns_decode_name(): NAME contains a pointer which points to another pointer at packet offset %u", name_offset); goto SXE_ERROR_OUT; } continue; } else { SXEL51("sxe_dns_decode_name(): NAME is invalid; NAME contains an invalid length/pointer value at packet offset %u", name_offset); goto SXE_ERROR_OUT; } name_offset += len + 1; } SXEL60("NAME is invalid because it extends outside the packet"); SXE_ERROR_OUT: SXE_EARLY_OUT: SXER81("return // result=%s", sxe_return_to_string(result)); return result; }