int main(int argc, char**argv) { custom_free_f(&use_fake_malloc, memset(custom_malloc_f(&use_fake_malloc, 127), 0x7e, 127)); if (getenv("NO_MALLOC")) { custom_alloc_opaque = &use_fake_malloc; } if (getenv("RUST_MALLOC")) { custom_alloc_opaque = NULL; custom_malloc = NULL; custom_free = NULL; } const unsigned char* data = example; size_t len = sizeof(example); unsigned char* to_free = NULL; int i; long long int truncation_location = 0; if (find_first_arg(argc, argv, &truncation_location)) { FILE * fp = fopen(find_first_arg(argc, argv, &truncation_location), "rb"); if (fp != NULL) { size_t ret; (void)fseek(fp, 0, SEEK_END); len = ftell(fp); (void)fseek(fp, 0, SEEK_SET); if (truncation_location && len > truncation_location) { len = truncation_location; } to_free = malloc(len); ret = fread(to_free, 1, len, fp); if (ret == 0) { return -1; } data = to_free; (void)fclose(fp); } } for (i= 1; i<= 11; i+= 1){ struct VecU8 brotli_file = new_vec_u8(); struct VecU8 rt_file = new_vec_u8(); BrotliDecoderResult dres; int32_t res; if (getenv("NO_WORK_POOL")) { res = compress_immediate_thread_spawn(data, len, &brotli_file, argc, argv, i); } else { res = compress(data, len, &brotli_file, argc, argv, i); } if (res != 1) { fprintf(stderr, "Failed to compress code:%d\n", (int) res); abort(); } dres = decompress(brotli_file.data, brotli_file.size, &rt_file); if (dres != BROTLI_DECODER_RESULT_SUCCESS) { fprintf(stderr, "Failed to decompress file size %d code:%d\n", (int) brotli_file.size, (int)res); abort(); } if (rt_file.size != len) { FILE * fp = fopen("/tmp/fail.rt", "wb"); fwrite(rt_file.data, 1, rt_file.size, fp); fclose(fp); fp = fopen("/tmp/fail.dv", "wb"); fwrite(brotli_file.data, 1, brotli_file.size, fp); fclose(fp); fp = fopen("/tmp/fail.or", "wb"); fwrite(data, 1, len, fp); fclose(fp); fprintf(stderr, "Decompressed file size %ld != %ld\n", (long) rt_file.size, (long)len); abort(); } if (memcmp(rt_file.data, data, len) != 0) { fprintf(stderr, "Roundtrip Contents mismatch\n"); abort(); } #ifdef _WIN32 printf("File length %ld reduced to %ld, %0.2f%%\n", (long)len, (long)brotli_file.size,(double)brotli_file.size * 100.0 / (double)len); #else char buf[512]; int ret; ret = write(1, "File length ", strlen("File Length ")); if (ret <= 0) { return ret; } custom_atoi(buf, len); ret = write(1, buf, strlen(buf)); if (ret <= 0) { return ret; } ret = write(1, " reduced to ", strlen(" reduced to ")); if (ret <= 0) { return ret; } custom_atoi(buf, brotli_file.size); ret = write(1, buf, strlen(buf)); if (ret <= 0) { return ret; } ret = write(1, ", ", strlen(", ")); if (ret <= 0) { return ret; } custom_atoi(buf, brotli_file.size * 100 / len); ret = write(1, buf, strlen(buf)); if (ret <= 0) { return ret; } ret = write(1, ".", strlen(".")); if (ret <= 0) { return ret; } custom_atoi(buf, ((brotli_file.size * 1000000 + len/2)/ len) % 10000 + 10000); ret = write(1, buf + 1, strlen(buf) - 1); if (ret <= 0) { return ret; } ret = write(1, "%\n", strlen("%\n")); if (ret <= 0) { return ret; } #endif release_vec_u8(&brotli_file); release_vec_u8(&rt_file); } if (to_free != NULL) { free(to_free); } return 0; }
/**Main Function with command line arguments.*/ int main(int argc, char const *argv[]) { /**Storage variables for max_restarts and failure chance.*/ int max_restarts=-1, failure_chance=-1; /** Check if the argument count is 5,3 or 1. If the argc is 1, it means assume default value as 5 and 0 for max restarts and failure chance respectively. -f and -n options are optional. If either of the options are not passed, default value is assumed. Else, pass the limit for forks with option -n within a range 1-50 and -f failure option. */ if((argc!=5)&&(argc!=1)&&((argc!=3))) { printf("Invalid usage of the command server_task3_1.bin.\nUsage: server_task3_1.bin -n <N> -f <F> where N is a number in the range 1-50\nF is a number in the range of 0-100\n"); /**Process returns and terminates with error.*/ return EXIT_FAILURE; } /**Assume default argument.*/ else if(argc == 1) { /**Setting the max_restarts and failure_chance storage variables to default values.*/ failure_chance = DEFAULT_FAILURE_CHANCE; max_restarts = DEFAULT_MAX_RESTARTS; } /**Condition to check one of the options where passed.*/ else if(argc == 3) { /**Condition to check if the option passed was -n*/ if(strcmp(argv[argc-2],"-n")==0) { /**Setting failure_chance to default value*/ failure_chance=DEFAULT_FAILURE_CHANCE; /**Setting the passed argument for max_restarts.*/ max_restarts = custom_atoi((char*) argv[argc-1]); /**Check if the argument passed is not the given range of 1-50 or not.*/ if((max_restarts>50)||(max_restarts<1)) { fprintf(stderr, "Error:Max Restarts out of range. Expected range 1-50\n"); printf("Invalid usage of the command server_task3_1.bin.\nUsage: server_task3_1.bin -n <N> -f <F> where N is a number in the range 1-50\nF is a number in the range of 0-100\n"); /**Process returns and terminates with error.*/ return EXIT_FAILURE; } } /**Condition to check if the option passed was -f*/ else if(strcmp(argv[argc-2],"-f")==0) { /**Setting max_restarts to default value*/ max_restarts = DEFAULT_MAX_RESTARTS; /**Setting the passed argument for failure_chance.*/ failure_chance = custom_atoi((char*) argv[argc-1]); /**Check if the argument passed is not the given range of 0-100 or not.*/ if((failure_chance>100)||(failure_chance<0)) { fprintf(stderr, "Error:Failure Chance out of range. Expected range 0-100\n"); printf("Invalid usage of the command server_task3_1.bin.\nUsage: server_task3_1.bin -n <N> -f <F> where N is a number in the range 1-50\nF is a number in the range of 0-100\n"); /**Process returns and terminates with error.*/ return EXIT_FAILURE; } } /**Invalid option.*/ else { printf("Invalid usage of the command server_task3_1.bin.\nUsage: server_task3_1.bin -n <N> -f <F> where N is a number in the range 1-50\nF is a number in the range of 0-100\n"); /**Process returns and terminates with error.*/ return EXIT_FAILURE; } } /**Check if -f was the first option used before option -n*/ else if((strcmp(argv[argc-2],"-f")==0)&&(strcmp(argv[argc-4],"-n")==0)) { /**Setting the max_restarts with command line arguments provided.*/ max_restarts = custom_atoi((char*) argv[argc-3]); /**Check if the argument passed is not the given range of 1-50 or not.*/ if((max_restarts>50)||(max_restarts<1)) { fprintf(stderr, "Error:Max Restarts out of range. Expected range 1-50\n"); printf("Invalid usage of the command server_task3_1.bin.\nUsage: server_task3_1.bin -n <N> -f <F> where N is a number in the range 1-50\nF is a number in the range of 0-100\n"); /**Process returns and terminates with error.*/ return EXIT_FAILURE; } /**Setting the failure_chance with command line arguments provided.*/ failure_chance = custom_atoi((char*) argv[argc-1]); /**Check if the argument passed is not the given range of 0-100 or not.*/ if((failure_chance>100)||(failure_chance<0)) { fprintf(stderr, "Error:Failure Chance out of range. Expected range 0-100\n"); printf("Invalid usage of the command server_task3_1.bin.\nUsage: server_task3_1.bin -n <N> -f <F> where N is a number in the range 1-50\nF is a number in the range of 0-100\n"); /**Process returns and terminates with error.*/ return EXIT_FAILURE; } } /**Check if -n was the first option used before option -f*/ else if((strcmp(argv[argc-2],"-n")==0)&&(strcmp(argv[argc-4],"-f")==0)) { /**Setting the max_restarts with command line arguments provided.*/ max_restarts = custom_atoi((char*) argv[argc-1]); /**Check if the argument passed is not the given range of 1-50 or not.*/ if((max_restarts>50)||(max_restarts<1)) { fprintf(stderr, "Error:Max Restarts out of range. Expected range 1-50\n"); printf("Invalid usage of the command server_task3_1.bin.\nUsage: server_task3_1.bin -n <N> -f <F> where N is a number in the range 1-50\nF is a number in the range of 0-100\n"); /**Process returns and terminates with error.*/ return EXIT_FAILURE; } /**Setting the failure_chance with command line arguments provided.*/ failure_chance = custom_atoi((char*) argv[argc-3]); /**Check if the argument passed is not the given range of 0-100 or not.*/ if((failure_chance>100)||(failure_chance<0)) { fprintf(stderr, "Error:Failure Chance out of range. Expected range 0-100\n"); printf("Invalid usage of the command server_task3_1.bin.\nUsage: server_task3_1.bin -n <N> -f <F> where N is a number in the range 1-50\nF is a number in the range of 0-100\n"); /**Process returns and terminates with error.*/ return EXIT_FAILURE; } } /**Condition for Invalid arguments passed.*/ else { printf("Invalid usage of the command server_task3_1.bin.\nUsage: server_task3_1.bin -n <N> -f <F> where N is a number in the range 1-50\nF is a number in the range of 0-100\n"); /**Process returns and terminates with error.*/ return EXIT_FAILURE; } /**Invoking the request processing method.*/ if(server(max_restarts, failure_chance)==EXIT_SUCCESS) { /**Process returns and terminates with success.*/ return EXIT_SUCCESS; } else { fprintf(stderr, "Error:Testing Task3 has failed.\n"); /**Process returns and terminates with error.*/ return EXIT_FAILURE; } }