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; }
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 init(const char* ipdb) { if (ipip.offset) { return 0; } FILE *file = fopen(ipdb, "rb"); fseek(file, 0, SEEK_END); long size = ftell(file); fseek(file, 0, SEEK_SET); ipip.data = (byte *) malloc(size * sizeof(byte)); fread(ipip.data, sizeof(byte), (size_t) size, file); fclose(file); uint indexLength = B2IU(ipip.data); ipip.index = (byte *) malloc(indexLength * sizeof(byte)); memcpy(ipip.index, ipip.data + 4, indexLength); ipip.offset = indexLength; ipip.flag = (uint *) malloc(65536 * sizeof(uint)); memcpy(ipip.flag, ipip.index, 65536 * sizeof(uint)); return 0; }
int init(const char *ipdb) { if (ipip.offset) { return 0; } FILE *file = fopen(ipdb, "rb"); if (!file) return -1; fseek(file, 0, SEEK_END); long size = ftell(file); fseek(file, 0, SEEK_SET); ipip.data = (byte *) malloc(size * sizeof(byte)); size_t r = fread(ipip.data, sizeof(byte), (size_t) size, file); if (r == 0) { return -1; } fclose(file); uint length = B2IU(ipip.data); ipip.index = (byte *) malloc(length * sizeof(byte)); memcpy(ipip.index, ipip.data + 4, length); ipip.offset = length; ipip.flag = (uint *) malloc(256 * sizeof(uint)); memcpy(ipip.flag, ipip.index, 256 * sizeof(uint)); return 0; }