diff --git a/lab2/main.c b/lab2/main.c new file mode 100644 index 00000000..df5c43f5 --- /dev/null +++ b/lab2/main.c @@ -0,0 +1,147 @@ +#include +#include +#include +#include + +#define base 1000000000 + + +typedef struct uint1024_t { + uint32_t *chunk; + size_t count; + size_t size; +} uint1024_t; + +void str_reverse(uint8_t *arr, size_t size) { + uint8_t temp; + for (size_t i = 0, j = size-1; i < size / 2; i++, j--) { + temp = arr[i]; + arr[i] = arr[j]; + arr[j] = temp; + } +} + +void print_uint1024_t(uint1024_t *x) { + if (x->count == 0) { + printf("null\n"); + return; + } + ssize_t i = x->count-1; + for (; i > 0; --i) { + if (x->chunk[i] != 0) { + break; + } + } + printf("%u", x->chunk[i]); + for (--i; i != -1; --i) { + printf("%.9u", x->chunk[i]); + } + putchar('\n'); +} + +uint1024_t *add_op(uint1024_t *x, uint1024_t *y) { + + size_t max_count; + + if (x->count > y->count) + max_count = x->count; + else + max_count = y->count; + + uint8_t carry = 0; + + uint1024_t *result; + result = (uint1024_t*)malloc(sizeof(uint1024_t)); + memcpy(result, x, sizeof(uint1024_t)); + + for (size_t i = 0; i < max_count; ++i) { + uint64_t temp = carry + x->chunk[i] + y->chunk[i]; + result->chunk[i] = temp % base; + carry = temp / base; + } + + if (carry) { + result->chunk[result->count] = carry; + result->count++; + } + + + return result; +} + +uint1024_t *init(uint8_t *str) { + + const size_t buff_size = 9; + + uint1024_t *new_uint = (uint1024_t*)malloc(sizeof(uint1024_t)); + new_uint->size = 32; + new_uint->count = 0; + new_uint->chunk = (uint32_t*)malloc(new_uint->size * sizeof(uint32_t)); + + + uint8_t num[buff_size + 1]; + memset(num, 0, (buff_size + 1) * sizeof(uint8_t)); + + size_t next = 0; + size_t index = 0; + size_t length = strlen(str); + + for (int i = length - 1; i >= 0; i--) { + num[index++] = str[i]; + if (index == buff_size) { + index = 0; + str_reverse(num, buff_size); + new_uint->chunk[next++] = atoi(num); + new_uint->count++; + } + } + if (index != 0) { + num[index] = '\0'; + str_reverse(num, index); + new_uint->chunk[next++] = atoi(num); + new_uint->count++; + } + + return new_uint; +} + +void _free(uint1024_t *x) { + for (int i = 0; i < 32; i++){ + free(x->chunk[i]); + } + free(x->chunk); + free(x); +} + +int main() { + + char str1[309]; + char str2[309]; + + printf ("enter first uint1024_t num "); + scanf("%309s", str1); + + printf ("enter second uint1024_t num "); + scanf("%309s", str2); + + uint1024_t *x = init(str1); + uint1024_t *y = init(str2); + + printf ("\nfirst uint1024_t num "); + print_uint1024_t(x); + printf ("\nsecond uint1024_t num "); + print_uint1024_t(y); + + printf ("\nsum uint1024_t num "); + print_uint1024_t(add_op(x, y)); + + + _free(x); + _free(y); + + + + return 0; +} + + diff --git a/lab3/main.c b/lab3/main.c new file mode 100644 index 00000000..03f4692b --- /dev/null +++ b/lab3/main.c @@ -0,0 +1,144 @@ +#include +#include +#include +#include + +const char *calendar[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; + +typedef struct data { + char *remote_addr; + char *local_time; + char *time_zone; + char *request; + char *status; + char *bytes_sent; +} data; + +int get_time(char *request_time) { + + char month[4]; + int time_value; + struct tm local_time = {}; + + sscanf(request_time, "%d/%3s/%d:%d:%d:%d", &local_time.tm_mday, month, &local_time.tm_year, &local_time.tm_hour, &local_time.tm_min, &local_time.tm_sec); + local_time.tm_year -= 1900; + + for (int i = 0; i < 12; i++){ + if (strcmp(month, calendar[i])){ + local_time.tm_mon = i; + break; + } + } + + time_value = mktime(&local_time); + + //printf("time value = %d\n", time_value); + return time_value; +} + +data get_data(char *request){ + + data result; + + result.remote_addr = strtok(request, "-"); + + strtok(NULL, "["); + result.local_time = strtok(NULL, " "); + + result.time_zone = strtok(NULL, "]"); + + strtok(NULL, "\""); + result.request = strtok(NULL, "\""); + + result.status = strtok(NULL, " "); + result.bytes_sent = strtok(NULL, "\n"); + + return result; + +} + +int data_correct(data request_data){ + + if(request_data.local_time == NULL || request_data.request == NULL || request_data.status == NULL || request_data.time_zone == NULL || request_data.remote_addr == NULL || request_data.bytes_sent == NULL){ + return 0; + } + return 1; +} + +int main(int argc, char** argv){ + + if (argc != 3){ + printf("too many or too less arguments (file name ; time window) \n"); + exit(-1); + } + + char *file_name = argv[1]; + + int tm_window = atoi(argv[2]); + + if (tm_window < 1){ + printf("time window error"); + exit(-1); + } + + unsigned long string_counter = 0; + unsigned long error_counter = 0; + int max_string = 256; + char *cur_request = calloc(4096, sizeof(char)); + int *time_array = calloc(max_string, sizeof(long long)); + + FILE *input = fopen(file_name, "r"); + + if (input == NULL) { + printf("file path error"); + exit(-1); + } + + while (!feof(input)) { + + fgets(cur_request, 4096, input); + data request_data = get_data(cur_request); + + if (data_correct(request_data)){ + + if (request_data.status[0] == '5'){ + printf("%s\n", request_data.request); + error_counter = error_counter + 1; + } + + if (string_counter == max_string){ + max_string = max_string * 2; + time_array = realloc(time_array, max_string * sizeof(long long)); + } + + time_array[string_counter] = get_time(request_data.local_time); + string_counter++; + } + } + + int left = 0; + int right = 0; + int max_request = 0; + int maxl, maxr; + + for (int left = 0; left < string_counter; left++){ + right = left; + + while (right < string_counter && time_array[right] - time_array[left] <= tm_window) { + right++; + } + + if ((right - left) > max_request){ + max_request = right - left; + maxl = time_array[left]; + maxr = time_array[right] - 1; + } + } + + printf("======================================================\n"); + printf("max request : [%d] in time window : [%d] from time gap : [%d] to : [%d]\n", max_request, tm_window, maxl, maxr); + printf("total [5xx] errors: [%d]\n", error_counter); + + return 0; +} + diff --git a/lab4/lab4.c b/lab4/lab4.c new file mode 100644 index 00000000..895b93d1 --- /dev/null +++ b/lab4/lab4.c @@ -0,0 +1,302 @@ +#include +#include +#include + +typedef struct TAGS{ + char version[3]; + char ver[2]; + char flags; + char size[4]; +} TAGS; + +typedef union bytesize{ + int value; + char byte[4]; +}; + +typedef struct FRAMES{ + char name[4]; + char size[4]; + char flags[2]; + char unicode; +} FRAMES; + +TAGS tag_header; +FRAMES frame_header; + +int string_count(char *str){ + int i = 0; + while(str[i]!= '\0'){ + i++; + } + return i; +} + +int synchsafe_to_int(char byte[4]) +{ + return byte[0] << 24 | byte[1] << 16 | byte[2] << 8 | byte[3]; +} + +char *int_to_synchsafe(int val) +{ + union bytesize x; + x.value = val; + char byte[4]; + + byte[0] = x.byte[0] & 0x7f; + byte[1] = (x.byte[1] >> 7) & 0x7f; + byte[2] = (x.byte[2] >> 14) & 0x7f; + byte[3] = (x.byte[3] >> 21) & 0x7f; + + return byte; +} + +int header_size_to_int(char byte[4]) { + return byte[0] << 21 | byte[1] << 14 | byte[2] << 7 | byte[3]; +} + +int size_of_file(const char *file_name){ + + int file_size = 0; + FILE *new = fopen(file_name, "rb"); + + while(fgetc(new) != EOF) + file_size++; + + fclose(new); + + return file_size; +} + +void is_correct_file(char *file_name){ + + if (file_name == NULL){ + printf("Error opening file"); + exit(1); + } +} + +void show(char *file_name){ + + FILE *file = fopen(file_name, "rb"); + is_correct_file(file); + + fread(&tag_header, 1, 10, file); + printf("%sv%d.%d\n", tag_header.version, tag_header.ver[0], tag_header.ver[1]); + + + int tag_size = header_size_to_int(tag_header.size); + printf("tag_size = %d\n", tag_size); + + int file_size = size_of_file(file_name); + printf("file_size = %d\n", file_size); + + while(ftell(file) < tag_size){ + + fread(&frame_header, 1, 11, file); + if (frame_header.name[0] == 0){ + break; + } + int frame_size = synchsafe_to_int(frame_header.size); + if (frame_header.name[0] == 'A'){ + fseek(file, frame_size, SEEK_CUR); + continue; + } + char *content = calloc(frame_size, 1); + fgets(content, frame_size, file); + printf("id: %5s || size: %5d || value: ", frame_header.name, frame_size); + printf("%s\n", content); + free(content); + } + fclose(file); +} + +void get(char *file_name, char *frame){ + + FILE *file = fopen(file_name, "rb"); + is_correct_file(file); + + fread(&tag_header, 1, 10, file); + printf("%sv%d.%d\n", tag_header.version, tag_header.ver[0], tag_header.ver[1]); + + int tag_size = header_size_to_int(tag_header.size); + printf("tag size = %d\n", tag_size); + + while(ftell(file) < tag_size){ + + fread(&frame_header, 1, 11, file); + int frame_size = synchsafe_to_int(frame_header.size); + + if (strcmp(frame, frame_header.name) == 0){ + + int frame_size = synchsafe_to_int(frame_header.size); + char *content = calloc(frame_size, 1); + fgets(content, frame_size, file); + printf("id: %5s || size: %5d || value: ", frame_header.name, frame_size); + printf("%s\n", content); + free(content); + break; + } + + fseek(file, frame_size - 1, SEEK_CUR); + + } + fclose(file); +} + +void set_value(char *file_name, char *frame, char *value){ + + FILE *file = fopen(file_name, "rb"); + is_correct_file(file); + int file_size = size_of_file(file_name); + + fread(&tag_header, 1, 10, file); + printf("%sv%d.%d ", tag_header.version, tag_header.ver[0], tag_header.ver[1]); + + int tag_size = header_size_to_int(tag_header.size); + printf("tag size = %d\n", tag_size); + + while(ftell(file) < tag_size){ + + fread(&frame_header, 1, 11, file); + + if (strcmp(frame, frame_header.name) == 0){ + + int frame_size = synchsafe_to_int(frame_header.size); + + int left_ptr = ftell(file) - 7; + int right_ptr = left_ptr + frame_size + 6; + + int value_size = string_count(value); + + char *left_buf = calloc(1, left_ptr); + char *right_buf = calloc(1, file_size - right_ptr); + + fseek(file, 0 , 0); + fread(left_buf, 1, left_ptr, file); + + fseek(file, right_ptr, 0); + fread(right_buf, 1, file_size - right_ptr, file); + + FILE *new = fopen("newmp3.mp3", "wb"); + + fwrite(left_buf, 1, left_ptr, new); + + union bytesize x; + x.value = value_size + 1; + + char info_buf[7]; + info_buf[0] = x.byte[3]; + info_buf[1] = x.byte[2]; + info_buf[2] = x.byte[1]; + info_buf[3] = x.byte[0]; + info_buf[4] = frame_header.flags[0]; + info_buf[5] = frame_header.flags[1]; + info_buf[6] = frame_header.unicode; + + fwrite(info_buf, 1, 7, new); + + fwrite(value, 1, value_size, new); + + fwrite(right_buf, 1, file_size - right_ptr, new); + + fclose(new); + + printf("\n frame was successfully edited \n"); + break; + } + } + fclose(file); +} + +void add_frame(char *file_name, char *frame, char *value){ + + FILE *file = fopen(file_name, "rb"); + is_correct_file(file); + int file_size = size_of_file(file_name); + + fread(&tag_header, 1, 10, file); + printf("%sv%d.%d\n", tag_header.version, tag_header.ver[0], tag_header.ver[1]); + + int tag_size = header_size_to_int(tag_header.size); + printf("start size = %d\n", tag_size); + + fseek(file, tag_size, SEEK_SET); + + int pos = ftell(file); //end of tag section + + char new_tag_header[6]; + char *left_buf = calloc(1, pos - 10); + char *right_buf = calloc(1, file_size - pos); + + fseek(file, 10, SEEK_SET); + fread(left_buf, 1, pos - 10, file); + + fseek(file, pos, SEEK_SET); + fread(right_buf, 1, file_size - pos, file); + + fseek(file, 0 , 0); + + fread(new_tag_header, 1, 6, file); + + int new_tag_size = tag_size + 4 + string_count(value); + + printf("new size = %d\n", new_tag_size); + + char size_buf = tag_header.size; + + FILE *new = fopen("newmp3.mp3", "wb"); + + fwrite(new_tag_header, 1, 6, new); + fwrite(tag_header.size, 1, 4, new); + fwrite(left_buf, 1, pos - 10, new); + + fwrite(frame, 1, 4, new); + + union bytesize x; + x.value = string_count(value) + 1; + + char new_header[7]; + new_header[0] = x.byte[3]; + new_header[1] = x.byte[2]; + new_header[2] = x.byte[1]; + new_header[3] = x.byte[0]; + new_header[4] = '0'; + new_header[5] = '0'; + new_header[6] = '0'; + + fwrite(new_header, 1, 7, new); + + fwrite(value, 1, string_count(value), new); + + fwrite(right_buf, 1, file_size - pos, new); + + printf("\n frame was successfully aded"); + fclose(new); + fclose(file); +} + +int main(int argc, char *argv[]) +{ + + char *get_val; + char *name = strpbrk(argv[1], "=") + 1; + + if(strcmp(argv[argc - 1], "--show") == 0) + show(name); + + else if(argv[argc - 1][2] == 'g') + { + get_val = strpbrk(argv[argc - 1], "=") + 1; + get(name, get_val); + } + + else if(argc > 3) + { + char *frame = strpbrk(argv[2], "=") + 1; + char *set_val = strpbrk(argv[3], "=") + 1; + set_value(name, frame, set_val); + } + return 0; +} + diff --git a/lab5/main.c b/lab5/main.c new file mode 100644 index 00000000..8e481e99 --- /dev/null +++ b/lab5/main.c @@ -0,0 +1,177 @@ +#include +#include +#include + +void reverse(char s[]) + { + int i, j; + char c; + + for (i = 0, j = strlen(s)-1; i 0); + if (sign < 0) + s[i++] = '-'; + s[i] = '\0'; + reverse(s); + } + + +int** calculation(int** arr, int height, int width) { + + int** temp = (int**)malloc(sizeof(int*) * height); + for (int i = 0; i < height; i++) + temp[i] = (int*)malloc(sizeof(int) * width); + + for (int i = 0; i < height; i++) { + for (int j = 0; j < width; j++) { + + int neighbours = 0, i_plus, i_minus, j_plus, j_minus; + + if (i + 1 > height - 1) + i_plus = i + 1 - height; + else + i_plus = i + 1; + + if (i - 1 < 0) + i_minus = i - 1 + height; + else + i_minus = i - 1; + + if (j + 1 > width - 1) + j_plus = j + 1 - width; + else + j_plus = j + 1; + + if (j - 1 < 0) + j_minus = j - 1 + width; + else + j_minus = j - 1; + + neighbours = arr[i_minus][j] + arr[i_minus][j_minus] + arr[i_minus][j_plus] + arr[i_plus][j] + + arr[i_plus][j_minus] + arr[i_plus][j_plus] + + arr[i][j_plus] + arr[i][j_minus]; + + switch (neighbours) { + case 0: temp[i][j] = 0; break; + case 1: temp[i][j] = 0; break; + case 2: temp[i][j] = arr[i][j]; break; + case 3: temp[i][j] = 1; break; + default: temp[i][j] = 0; break; + } + } + } + return temp; +} + +int main(int argc, char* argv[]) { + int height, width, size, generations, freq = 1; + unsigned char bmp_header[54]; + char* directory = NULL; + FILE* f = NULL; + + for (int i = 0; i < argc; i++) { + if (!strcmp("--input", argv[i])) + f = fopen(argv[i + 1], "rb"); + + if (!strcmp("--output", argv[i])) { + directory = argv[i + 1]; + } + if (!strcmp("--max_iter", argv[i])) + generations = strtol(argv[i + 1], 0, 10); + + if (!strcmp("--dump_freq", argv[i])) + freq = strtol(argv[i + 1], 0, 10); + } + + fread(bmp_header, sizeof(unsigned char), 54, f); + + width = bmp_header[21] * 256 * 256 * 256 + bmp_header[20] * 256 * 256 + bmp_header[19] * 256 + bmp_header[18]; + height = bmp_header[25] * 256 * 256 * 256 + bmp_header[24] * 256 * 256 + bmp_header[23] * 256 + bmp_header[22]; + size = bmp_header[5] * 256 * 256 * 256 + bmp_header[4] * 256 * 256 + bmp_header[3] * 256 + bmp_header[2]; + + printf("bmp size = %d\n", size); + printf("bmp width = %d\n", width); + printf("bmp height = %d\n", height); + + unsigned char* buff = (unsigned char*) malloc((size - 54) * sizeof(unsigned char)); + fread(buff, sizeof(unsigned char), size, f); + + int** a = (int**)malloc(height * sizeof(int*)); + for (int i = 0; i < height; i++) { + a[i] = (int*)malloc(width * sizeof(int)); + } + + int factor_4 = -(width % 4); + for (int i = height - 1; i >= 0; i--) { + factor_4 += width % 4; + for (int j = 0; j < width; j++) { + if (buff[factor_4] == 255) + a[i][j] = 0; + else + a[i][j] = 1; + factor_4 += 3; + } + } + + + + for (int i = 1; i <= generations; i++) { + a = calculation(a, height, width); + if (i % freq != 0) + continue; + + char file_name[256], generation_value[10], name[30] = "gen"; + strcpy(file_name, directory); + strcat(file_name, "\\"); + itoa(i, generation_value); + strcat(name, generation_value); + strcat(name, ".bmp"); + strcat(file_name, name); + FILE* out = fopen(file_name, "wb"); + fwrite(bmp_header, 1, 54, out); + factor_4 = 0; + + for (int i = height - 1; i >= 0; i--) { + for (int j = 0; j < width; j++) { + for (int k = 0; k < 3; k++) { + if (a[i][j] == 1) + buff[factor_4] = 0; + else + buff[factor_4] = 255; + factor_4++; + } + } + while (factor_4 % 4 != 0) { + buff[factor_4] = 0; + factor_4++; + } + } + fwrite(buff, sizeof(unsigned char), size, out); + printf("next gen %d was successfully created\n" , i); + fclose(out); + } + + for (int i = 0; i < height - 1; i++) { + free(a[i]); + } + free(a); + free(buff); + return 0; + +} + diff --git a/lab6/main.c b/lab6/main.c new file mode 100644 index 00000000..b30d54cc --- /dev/null +++ b/lab6/main.c @@ -0,0 +1,133 @@ +#include +#include +#include +#include + +union filesize { + unsigned int uint_size; + unsigned char byte[4]; +} filesize; + +void create(int argc, char *argv[], char *archive_name) { + + FILE *archive = fopen(archive_name, "wb"); + FILE *file; + + for (int i = 5; i < argc; i++) { + file = fopen(argv[i], "rb"); + + fseek(file, 0, SEEK_END); + unsigned int file_size = ftell(file); + fseek(file, 0, SEEK_SET); + + if (file_size > UINT_MAX) { + printf("file is too large"); + break; + } + + unsigned char file_name_length = strlen(argv[i]) + 1; + if (file_name_length > UCHAR_MAX) { + printf("file name is too large"); + break; + } + + fputc(file_name_length, archive); + + filesize.uint_size = file_size; + fputc(filesize.byte[3], archive); + fputc(filesize.byte[2], archive); + fputc(filesize.byte[1], archive); + fputc(filesize.byte[0], archive); + + fwrite(argv[i], sizeof(unsigned char), file_name_length, archive); + + unsigned char *buf = calloc(file_size, sizeof(unsigned char)); + fread(buf, sizeof(unsigned char), file_size, file); + fwrite(buf, sizeof(unsigned char), file_size, archive); + free(buf); + fclose(file); + } + printf("%s - was successfully created \n", archive_name); + fclose(archive); +} + +void extract(char *archive_name) { + FILE *archive = fopen(archive_name, "rb"); + int name_length; + + while ((name_length = fgetc(archive)) != EOF) { + char file_length[4]; + fread(file_length, sizeof(unsigned char), 4, archive); + filesize.byte[3] = file_length[0]; + filesize.byte[2] = file_length[1]; + filesize.byte[1] = file_length[2]; + filesize.byte[0] = file_length[3]; + + unsigned char *file_name = calloc(name_length, sizeof(unsigned char)); + for (unsigned char i = 0; i < name_length; i++) { + file_name[i] = fgetc(archive); + } + + FILE *file = fopen(file_name, "wb"); + + unsigned char *buf = calloc(sizeof(unsigned char), filesize.uint_size); + fread(buf, sizeof(unsigned char), filesize.uint_size, archive); + fwrite(buf, sizeof(unsigned char), filesize.uint_size, file); + + printf("%s - was successfully exctracted \n", file_name); + free(file_name); + free(buf); + fclose(file); + } + fclose(archive); +} + +void list(char *archive_name) { + FILE *archive = fopen(archive_name, "rb"); + unsigned int name_length; + + int file_counter = 0; + + while ((name_length = getc(archive)) != EOF) { //EOF + char file_length[4]; + fread(file_length, sizeof(unsigned char), 4, archive); + filesize.byte[3] = file_length[0]; + filesize.byte[2] = file_length[1]; + filesize.byte[1] = file_length[2]; + filesize.byte[0] = file_length[3]; + + + + unsigned char *file_name = calloc(name_length, sizeof(unsigned char)); + fread(file_name, sizeof(unsigned char), name_length, archive); + + fseek(archive, filesize.uint_size, SEEK_CUR); + + printf("%s ", file_name); + + printf(" %d bytes\n", filesize.uint_size); + + file_counter++; + + free(file_name); + } + + printf("total files : %d \n", file_counter); + + fclose(archive); +} + +int main(int argc, char *argv[]) { + + char *archive_name = argv[3]; + + if (strncmp(argv[4], "--create", 8) == 0) + create(argc, argv, archive_name); + if (strncmp(argv[4], "--extract", 9) == 0) + extract(archive_name); + if (strncmp(argv[4], "--list", 6) == 0) + list(archive_name); + + return 0; +} +