int find(const char *ip, char *result) { uint ips[4]; int num = sscanf(ip, "%d.%d.%d.%d", &ips[0], &ips[1], &ips[2], &ips[3]); if (num == 4) { uint ip_prefix_value = ips[0] * 256 + ips[1]; uint ip2long_value = B2IU(ips); uint start = ipip.flag[ip_prefix_value]; uint max_comp_len = ipip.offset - 262144 - 4; uint index_offset = 0; uint index_length = 0; for (start = start * 9 + 262144; start < max_comp_len; start += 9) { if (B2IU(ipip.index + start) >= ip2long_value) { index_offset = B2IL(ipip.index + start + 4) & 0x00FFFFFF; index_length = (ipip.index[start+7] << 8) + ipip.index[start+8]; break; } } memcpy(result, ipip.data + ipip.offset + index_offset - 262144, index_length); result[index_length] = '\0'; int current_tabs = 0; for (int i = 0; i < index_length; i++) { if (result[i] == '\t') { if (current_tabs++ == 4) { result[i] = 0; break; } result[i] = ' '; } } if (result[strlen(result) - 1] == ' ') { result[strlen(result) - 1] = 0; } } return 0; }
int find(const char *ip, unsigned char **result, size_t *size) { uint ips[4]; if (!ipip.offset) { return -2; } int num = sscanf(ip, "%d.%d.%d.%d", &ips[0], &ips[1], &ips[2], &ips[3]); if (num == 4) { uint ip_prefix_value = ips[0]; uint ip2long_value = B2IU(ips); uint start = ipip.flag[ip_prefix_value]; uint max_comp_len = ipip.offset - 1028; uint index_offset = 0; uint index_length = 0; for (start = start * 8 + 1024; start < max_comp_len; start += 8) { if (B2IU(ipip.index + start) >= ip2long_value) { index_offset = B2IL(ipip.index + start + 4) & 0x00FFFFFF; index_length = ipip.index[start + 7]; break; } } *size = index_length; *result = ipip.data + ipip.offset + index_offset - 1024; return 0; } return -1; }