diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | Makefile | 7 | ||||
| -rw-r--r-- | calc.c | 59 | ||||
| -rw-r--r-- | calc.h | 3 | ||||
| -rw-r--r-- | itoa_gen.py | 15 |
5 files changed, 64 insertions, 21 deletions
@@ -1,2 +1,3 @@ *.json calc +itoa_lut.h @@ -1,13 +1,12 @@ .PHONY: build -build: calc.c +build: calc.c lut clang -o calc -O3 -ggdb ./calc.c bench: build ./calc 10.0.0.0/8 2 -bench2: build - ./calc 10.0.0.0/6 2 - +lut: itoa_gen.py + python3 ./itoa_gen.py @@ -1,4 +1,5 @@ #include "calc.h" +#include "itoa_lut.h" #include <time.h> #include <errno.h> #include <stdint.h> @@ -27,6 +28,25 @@ uint32_t subnetmask_for_netspace(unsigned int desired_netspace) { return start; } +void write_ip(char **chunk_ptr, uint32_t ip) { + // local ptr so we don't have to deref every time + char *ptr = *chunk_ptr; + LutEntry e = {.bits = itoa_lut[(ip >> 24) & 0xff]}; + WRITE_STR(ptr, e.fields.chars, e.fields.len) + ptr[0] = '.'; + ptr += 1; + e.bits = itoa_lut[(ip >> 16) & 0xff]; + WRITE_STR(ptr, e.fields.chars, e.fields.len) + ptr[0] = '.'; + ptr += 1; + e.bits = itoa_lut[(ip >> 8) & 0xff]; + WRITE_STR(ptr, e.fields.chars, e.fields.len) + ptr[0] = '.'; + ptr += 1; + e.bits = itoa_lut[ip & 0xff]; + WRITE_STR(ptr, e.fields.chars, e.fields.len) + *chunk_ptr = ptr; +} int main(int argc, char** argv) { struct timespec start; @@ -64,31 +84,36 @@ int main(int argc, char** argv) { fprintf(out, "[\n"); // file start uint32_t offset = 0; int i = 0; + char chunk[1024] = {0}; while (i<req.count) { const uint32_t network = req.network + offset; const uint32_t first = network + 1; const uint32_t broadcast = first + ips_per_subnet - 2; const uint32_t last = broadcast - 1; offset += ips_per_subnet; + char* chunk_ptr = chunk; + WRITE_STR(chunk_ptr, "\t{\n\t\t\"Reseau\": \"", 16) + write_ip(&chunk_ptr, network); + WRITE_STR(chunk_ptr, "\",\n", 3) + WRITE_STR(chunk_ptr, "\t\t\"Premiere\": \"", 15) + write_ip(&chunk_ptr, first); + WRITE_STR(chunk_ptr, "\",\n", 3) + WRITE_STR(chunk_ptr, "\t\t\"Derniere\": \"", 15) + write_ip(&chunk_ptr, last); + WRITE_STR(chunk_ptr, "\",\n", 3) + WRITE_STR(chunk_ptr, "\t\t\"Broadcast\": \"", 16) + write_ip(&chunk_ptr, broadcast); + WRITE_STR(chunk_ptr, "\",\n", 3) + WRITE_STR(chunk_ptr, "\t\t\"Nbr\": \"", 10) + LutEntry e = {.bits = itoa_lut[ips_per_subnet - 2]}; + WRITE_STR(chunk_ptr, e.fields.chars, e.fields.len); if (i+1 == req.count) { - fprintf(out, - "\t{\n\t\t\"Reseau\": \"%hu.%hu.%hu.%hu\",\n\t\t\"Premiere\": \"%hu.%hu.%hu.%hu\",\n\t\t\"Derniere\": \"%hu.%hu.%hu.%hu\",\n\t\t\"Broadcast\": \"%hu.%hu.%hu.%hu\",\n\t\t\"Nbr\": \"%hu\"\n\t}\n", - UINT_TO_SEGMENTS(network), - UINT_TO_SEGMENTS(first), - UINT_TO_SEGMENTS(last), - UINT_TO_SEGMENTS(broadcast), - ips_per_subnet - 2 - ); - break; + WRITE_STR(chunk_ptr, "\"\n\t}\n", 5) + }else { + WRITE_STR(chunk_ptr, "\"\n\t},\n", 6) } - fprintf(out, - "\t{\n\t\t\"Reseau\": \"%hu.%hu.%hu.%hu\",\n\t\t\"Premiere\": \"%hu.%hu.%hu.%hu\",\n\t\t\"Derniere\": \"%hu.%hu.%hu.%hu\",\n\t\t\"Broadcast\": \"%hu.%hu.%hu.%hu\",\n\t\t\"Nbr\": \"%hu\"\n\t},\n", - UINT_TO_SEGMENTS(network), - UINT_TO_SEGMENTS(first), - UINT_TO_SEGMENTS(last), - UINT_TO_SEGMENTS(broadcast), - ips_per_subnet - 2 - ); + + fwrite(chunk, sizeof(char), chunk_ptr - chunk, out); i++; } fprintf(out, "]"); // file end @@ -2,10 +2,13 @@ #define UINT_TO_SEGMENTS(i) (i>>24 & 0xff),(i>>16 & 0xff),(i>>8 & 0xff),(i & 0xff) +#define WRITE_STR(ptr, str, len) for (int i=0; i<len; i++){ptr[0]=str[i]; ptr++;} + int print_usage(); uint32_t calc_netmask(unsigned int cidr); uint32_t segments_to_ip(unsigned int segments[4]); uint32_t cidr_for_netspace(unsigned int desired_netspace); +void write_ip(char **chunk_ptr, uint32_t ip); typedef struct { uint32_t network; diff --git a/itoa_gen.py b/itoa_gen.py new file mode 100644 index 0000000..0437c62 --- /dev/null +++ b/itoa_gen.py @@ -0,0 +1,15 @@ +with open("itoa_lut.h", "w") as f: + f.write("#include <stdint.h>\n") + f.write("typedef union { struct { char chars[3]; uint8_t len; } fields; uint32_t bits; } LutEntry;\n") + f.write("const uint32_t itoa_lut[256] = {") + out = [] + for i in range(0, 256): + running=len(str(i)) + for j in reversed(range(3)): + running = running << 8 + if j < len(str(i)): + running += ord(str(i)[j]) + out.append(hex(running)) + f.write(", ".join(out)) + f.write("};") + |
