int main() { struct bitio* b=bit_open("prova.txt", "w"); uint64_t a; bitio_write(b, 'a', 64); bitio_write(b, 'b', 64); bitio_write(b, 'c', 32); bitio_write(b, 'd', 8); bitio_write(b, 'd', 8); bitio_write(b, 'd', 8); bitio_write(b, 0x08, 6); bitio_write(b, 0xefafbfcf, 32); bitio_write(b, 'a', 64); bitio_write(b, 'b', 64); bitio_write(b, 'a', 64); bitio_close(b); b=bit_open("prova.txt", "r"); return 0; }
int main(int argc, char *argv[]) { BITMAP *bp; int i; #ifdef MOVIE log_start(fopen("lines.log","w")); #endif bp=bit_open(SCREEN_DEV); bit_blit(bp,0,0,BIT_WIDE(bp),BIT_HIGH(bp),BIT_CLR,(BITMAP*)0,0,0); srand(getpid()*time((time_t*)0)); for (i=0; i<1000; i++) bit_line(bp,rand()%BIT_WIDE(bp),rand()%BIT_HIGH(bp),rand()%BIT_WIDE(bp),rand()%BIT_HIGH(bp),BIT_SET); bit_destroy(bp); #ifdef MOVIE log_end(); #endif exit(0); }
/* Test for a little-endian machine */ #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ current_order = LITTLE_EN; #else current_order = BIG_EN; #endif if((pippo >> 1) == 0) printf("LITTLE\n"); else printf("BIG\n"); // read the header b_file = bit_open(opt.file_in,BIT_RD,128); ret = read_header(b_file,&header); if(ret == -1) return -1; // Initialize window structure bzero(&win, sizeof(struct window)); win.look_ah_length = header.look_ah_len; win.window_length = header.window_len; win.dict_position = 0; win.data_position = win.window_length; win.window = calloc( win.window_length*K, sizeof(char) ); // special code eof_code = win.look_ah_length + 1; forward_code = win.look_ah_length + 2;
/* compressor algorithm for lz78: * from is the path of the file to compress, size is the size of the dictionary * to is the compressed file name, it can be chose by the user, * otherwise a standard name is put in its place*/ void compress(char* from, char* to, int size){ //fprintf(stderr, "compressor\n"); //int, i, flen; //length of filename //int i; unsigned int tmp=0; //temporary variable in which we put the byte read unsigned int itmp=0, size_tmp=0; //itmp is the index to write; int position=0, father=0; //postion in the dictionary //int fd=open(filename, 'r'); //file descriptor dictionary* dict=malloc(sizeof(dictionary)); //dictionary int blen=1; //bit lenght of my dictionary's indexes size_tmp=size; struct bitio* bit; header hdr; //header of the file int result=0; int longest_match=1, tmp_longest_match=0; FILE* file_read, *file_write; //compute how many bit to write in the compressed file while(size_tmp>>=1) blen++; fprintf(stderr, "blen: %i\n", blen); //initialize the dictionary dict_init(dict, size, 256); father=0; //initialize the bitio structure to write bits instead of bytes bit=bit_open(to, "w"); if(bit==NULL) fprintf(stderr, "null bitio\n"); //creation of the header of the file hdr.dictionary_size=size; hdr.longest_match=0; //prepare the space for the overwriting //write the header in the top of the file //flen=strlen(hdr.extension); bitio_write(bit, (uint64_t)hdr.dictionary_size, sizeof(int)*8); bitio_write(bit, (uint64_t)hdr.longest_match, sizeof(int)*8); //for(i=0; i<6; i++) //bitio_write(bit, (uint64_t)hdr.extension[i], sizeof(char)*8); //read a byte from the file //fprintf(stderr, "%i\n", fd); if(from==NULL) file_read=stdin; else file_read=fopen(from, "r"); //fprintf(stderr, "%c\n", (char)tmp); //first read result=fread(&tmp, 1, 1, file_read); father=tmp; //read the file until reach the EOF or an error occurs do { //read one byte from the file result=fread(&tmp, 1, 1, file_read); //fprintf(stderr, "%i ",tmp ); itmp=father; tmp_longest_match++; //search if we already have the actual string in the dictionary position=dict_search(&father, tmp, dict); //fprintf(stderr, "new_father: %i %i\n", father, position); if(position!=0) { if(longest_match < tmp_longest_match) longest_match = tmp_longest_match; tmp_longest_match=0; //add the string in the dictionary dict_add_word(position, father, tmp, dict); //fprintf(stderr, "%i: %i %i\n", position, father, tmp); bitio_write(bit, (uint64_t)itmp, blen); //fprintf(stderr, "\n"); father=tmp; } } while(result!=0 || !feof(file_read)); //write the last position reached bitio_write(bit, father, blen); //close all the structures writing EOFC on the compressed file bitio_write(bit, EOFC, blen); bitio_close(bit); //fprintf(stderr, "%i\n", longest_match); //write the longest match in the file file_write=fopen(to, "r+b"); fseek(file_write, sizeof(int), SEEK_SET); hdr.longest_match=longest_match; fwrite(&hdr.longest_match, sizeof(int), 1, file_write); fclose(file_write); //free all other structures suppress_dictionary(dict); free(dict); fclose(file_read); fprintf(stderr, "compression executed\n"); }
int arithmetic_decoding(const char* encoded_file, const char* decoded_file) { FILE* fout; uint8_t byte; int i; int i_ss; int i_d; int n; int bitc = 0; uint64_t tmp, mask, stream_slice; int bits_to_read; struct CST* cumulative_sums_tree_pointer; struct BitBuffer* bbp = bit_open(encoded_file, BIT_BUFFER_READ); if (bbp == NULL) { printf("%s: No such file\n", encoded_file); return -1; } cumulative_sums_tree_pointer = CST_create(NUM_CODEPOINTS); if (cumulative_sums_tree_pointer == NULL) { printf("Error: CST creation failed\n"); bit_close(bbp); return -1; } fout = fopen(decoded_file, "wb"); if (fout == NULL) { printf("Error: cannot create output file %s\n", decoded_file); return -1; } // we deal with the zero-frequency problem initializing all counters to 1 and consequently NUM_CODEPOINTS to total_count total_count = NUM_CODEPOINTS; for (i=1; i<NUM_CODEPOINTS; i++) counter_increment(cumulative_sums_tree_pointer, i); LOW = 0; HIGH = 0xFFFFFFFFFFFFFFFF; bits_to_read = sizeof(uint64_t) * 8; stream_slice = 0x0000000000000000; for (;;) { n = bit_read(bbp, &tmp, bits_to_read); if (n == -1) { printf("Error occurred!!\n"); return -1; } // here n could be zero, but this is not a concern bitc+=n; if (bits_to_read == sizeof(uint64_t) * 8) mask = 0xFFFFFFFFFFFFFFFF; else mask = ((uint64_t)1 << bits_to_read) - 1; stream_slice = ((stream_slice << bits_to_read) | (tmp & mask)); /* i = 0; mask = 0x0000000000000001; while (i < bits_to_read) { stream_slice <<= 1; if (tmp & mask) stream_slice |= 0x0000000000000001; mask <<= 1; i++; } */ n = bs(cumulative_sums_tree_pointer, stream_slice); if (n == -1) return -1; if (n == NUM_CODEPOINTS - 1) // EOF symbol received break; byte = (uint8_t)n; fwrite(&byte, 1, 1, fout); tmp = ((HIGH - LOW) / total_count); HIGH = LOW + tmp * (cumulative_sums_lookup(cumulative_sums_tree_pointer, byte + 1)); LOW = LOW + tmp * cumulative_sums_lookup(cumulative_sums_tree_pointer, byte); // deadlock resolution if ((HIGH & 0xC000000000000000) == 0x8000000000000000 && (LOW & 0xC000000000000000) == 0x4000000000000000) { i_d = 2; mask = 0x2000000000000000; while (mask && (LOW & mask) && (HIGH & mask) == 0) { i_d++; mask >>= 1; } LOW &= ~mask; HIGH |= mask; HIGH <<= i_d; LOW <<= i_d; } else
//decompressor algorithm for lz78 //filename is the path of the file to decompress, size is the size of the dictionary void decompress(char* input, char* filename) { int /*i,*/index_bits, flag, aux, child_root=0, pos; bool first_read = true, res_retrieve=false; uint64_t read_index, prev_current=0; bitio* comp_file; char* tmp; dictionary* dict; word* sequence = NULL; //Variable in which store the word in each step word* print_seq; header hdr; int decomp_file; //eofc_pos = dict->symbols; comp_file = bit_open(input, "r"); //Preparing my data structures //retrieve header bitio_read(comp_file, (uint64_t*)&hdr.dictionary_size, sizeof(int)*8); //for(i=0; i<6; i++) //bitio_read(comp_file, (uint64_t*)&hdr.extension[i], sizeof(char)*8); dict = malloc(sizeof(dictionary)); dict_init(dict, hdr.dictionary_size, 256); aux = hdr.dictionary_size; //Compute the number of bits representing the indexes fprintf(stderr, "%i\n", aux); index_bits = 1; while(aux >>= 1) index_bits++; tmp=malloc(strlen(filename)); strcpy(tmp, filename); //strcat(tmp, ".\0"); //strcat(tmp, hdr.extension); decomp_file=open(tmp, O_CREAT|O_RDWR, S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH); bitio_read(comp_file, &read_index, index_bits); //Read the first index //decomp_file = open(decompname, O_CREAT|O_RDWR, S_IWUSR|S_IRUSR|S_IRGRP|S_IROTH); //fprintf(stderr, "\n%i\n", fdim); fprintf(stderr, "%s %i\n", tmp, index_bits); while(read_index != EOFC) { //Until the end of compressed file //fprintf(stdout, "%i\n", (int)read_index); //retrieve word from index res_retrieve=dict_retrieve_word(dict, read_index, &sequence); //fprintf(stderr, "\n%x\n", (unsigned int)read_index); //critical situation if(res_retrieve==false) { pos=dict_search((int)prev_current, child_root, dict, &flag); dict_add_word(pos, (int)prev_current, child_root, dict, &flag); dict_retrieve_word(dict, read_index, &sequence); first_read=true; } //fprintf(stderr, "decompressor %i\n", decomp_file); child_root = sequence->symbol; //fprintf(stderr, "decompressor %i\n", decomp_file); print_seq = sequence; //Write word on file while(print_seq != NULL) { //fprintf(stdout, "%i\n",print_seq->symbol); write(decomp_file, (void*)&(print_seq->symbol), 1); print_seq = print_seq->next; free(sequence); //Deallocate word structure sequence = print_seq; } if(first_read==false) { //Add the first symbol of my sequence, as child of the previous current node pos=dict_search((int)prev_current, child_root, dict, &flag); dict_add_word(pos, (int)prev_current, child_root, dict, &flag); } first_read = false; prev_current = read_index; bitio_read(comp_file, &read_index, index_bits); //Read the index } //Closure of all data structures close(comp_file->fd); free(comp_file); close(decomp_file); //print_dict(dict); suppress_dictionary(dict); fprintf(stderr, "decompression executed\n"); }
/*{{{ main*/ int main(int argc, char *argv[]) { /*{{{ variables*/ int x, login_y, password_y; char loginstr[9], passwordstr[9], ret; char ttystr[_POSIX_PATH_MAX]; char *background = (char *)0; char *fontname = (char *)0; /*}}} */ /*{{{ parse arguments*/ { int c; while ((c = getopt(argc, argv, "b:f:")) != EOF) switch (c) { case 'b': background = optarg; break; case 'f': fontname = optarg; break; } /*{{{ parse tty*/ { int tty; if (optind + 1 > argc) { fprintf(stderr, "Usage: %s tty\n", argv[0]); exit(1); } else { strcpy(ttystr, "/dev/"); strcat(ttystr, argv[optind++]); } close(0); close(1); close(2); setsid(); if ((tty = open(ttystr, O_RDWR)) != 0) { fprintf(stderr, "%s: Can't open controlling terminal on fd 0.\n", argv[0]); exit(1); } fchmod(tty, 0600); fchown(tty, getuid(), getgid()); open(argv[optind], O_RDWR); open(argv[optind], O_RDWR); } /*}}} */ } /*}}} */ /*{{{ get into grafics mode*/ signal(SIGTERM, quit); signal(SIGHUP, quit); set_tty(0); if ((screen = bit_open(SCREEN_DEV)) == (BITMAP *)0) { reset_tty(0); exit(EX_NOPERM); } bit_grafscreen(); /*}}} */ /*{{{ load font*/ if (fontname) { char fontpath[_POSIX_PATH_MAX]; if (*fontname == '/' || *fontname == '.') strcpy(fontpath, fontname); else { strcpy(fontpath, ICONDIR); strcat(fontpath, "/"); strcat(fontpath, fontname); } if ((font = open_font(fontname)) == (struct font *)0) font = open_font((char *)0); } else font = open_font((char *)0); /*}}} */ /*{{{ draw background*/ bit_blit(screen, 0, 0, screen->wide, screen->high, BIT_CLR, (BITMAP *)0, 0, 0); if (background) { BITMAP *bp; FILE *fp; char backgroundpath[_POSIX_PATH_MAX]; if (*background == '/' || *background == '.') strcpy(backgroundpath, background); else { strcpy(backgroundpath, ICONDIR); strcat(backgroundpath, "/"); strcat(backgroundpath, background); } if ((fp = fopen(backgroundpath, "r")) != (FILE *)0 && (bp = bitmapread(fp)) != (BITMAP *)0) { int x, y; for (x = 0; x < screen->wide; x += bp->wide) bit_blit( screen, x, 0, screen->wide - x < bp->wide ? screen->wide - x : bp->wide, bp->high, BIT_SRC, bp, 0, 0); for (y = 0; y < screen->high; y += bp->high) bit_blit( screen, 0, y, screen->wide, screen->high - y < bp->high ? screen->high - y : bp->high, BIT_SRC, screen, 0, 0); } } /*}}} */ /*{{{ draw hostname*/ { int bx, bw, by, bh; char hostname[_POSIX_PATH_MAX]; struct hostent *h; gethostname(hostname, sizeof(hostname)); if ((h = gethostbyname(hostname)) != (struct hostent *)0) strcpy(hostname, h->h_name); bw = font->head.wide * (strlen(hostname) + 2); bh = 2 * font->head.high; bx = (screen->wide - bw) / 2; by = screen->high / 6 - bh / 2; cutebox(bx, by, bw, bh); printstr(bx + font->head.wide, by + bh - font->head.high / 2, hostname); } /*}}} */ /*{{{ draw login box*/ { int bx, bw, by, bh; bx = (screen->wide - font->head.wide * 40) / 2; by = (screen->high - font->head.high * 8) / 2; bw = font->head.wide * 40; bh = font->head.high * 8; cutebox(bx, by, bw, bh); } /*}}} */ /*{{{ draw login box contents*/ x = (screen->wide - font->head.wide * 18) / 2; login_y = screen->high / 2 - font->head.wide / 6; password_y = screen->high / 2 + font->head.high / 6 + font->head.high; printstr(x, password_y, "Password: "******"Press ESC for terminal login"); /*}}} */ while (1) { /*{{{ get login and password or escape*/ printstr(x, login_y, "Login: "******"mgr", (char *)0 }; int i; sprintf(env_user, "USER=%s", pw->pw_name); sprintf(env_logname, "LOGNAME=%s", pw->pw_name); sprintf(env_home, "HOME=%s", pw->pw_dir); sprintf(env_shell, "SHELL=%s", pw->pw_shell == (char *)0 || pw->pw_shell[0] == '\0' ? "/bin/sh" : pw->pw_shell); sprintf(env_path, "PATH=%s", PATH); sprintf(env_mail, "MAIL=%s/%s", MAILDIR, pw->pw_name); if (chdir(pw->pw_dir) != 0) chdir("/"); if (ttyname(0)) { chown(ttyname(0), pw->pw_uid, pw->pw_gid); chmod(ttyname(0), 0600); } for (i = 1; i <= _NSIG; i++) signal(i, SIG_DFL); bit_destroy(screen); reset_tty(0); initgroups(pw->pw_name, pw->pw_gid); setgid(pw->pw_gid); setuid(pw->pw_uid); sprintf(mgrlogin, "%s/.mgrlogin", pw->pw_dir); execve(mgrlogin, login_argv, login_env); execve(MGR_BINARY, login_argv, login_env); exit(EX_OSFILE); } /*}}} */ else /*{{{ incorrect login*/ { printstr((screen->wide - font->head.wide * 16) / 2, login_y + 3 * font->head.high, "Login incorrect"); } /*}}} */ } /*}}} */ } }
/*--Function for Decompressor--*/ void decompress(struct bitio* file_to_read,struct bitio* file,lz78_decompressor* in) { //char * out=malloc(sizeof(char)*150); char out[50]; out[0]='\0'; int i=0; int j=0; char first_char=0; file_to_read=bit_open(in->file_to_decompress,0); if(file_to_read==NULL){ printf("File not found or you don't have enough privilege to open a file\n"); return; } uint64_t inp=0; int temp=0; char prec_char=0; int first_father; int ret=0; int x=0; //fseek(file_to_read->f,4,SEEK_SET); in->info_ptr=getinfo(file_to_read); reset: while(x>=0){ ret=bit_read(file_to_read,16,&inp); first_father=temp=(int)(inp&((1UL<<16)-1))-1; if(first_father<=0)goto fine_file; while((first_char=look_up_array(temp))==-1){ out[i]=(char)array_tree[temp].c; temp=(int)array_tree[temp].father_num; i++; } if(ret<=0) { fine_file:printf("The file was correctly extracted...\n"); //no more data to read flush_out_buffer(file); break; } out[i]=first_char; if(x==0){ prec_char=(int)(inp&((1UL<<16)-1))-1; //printf("ricevuto :%d Inserisco il nodo %d con padre %d\n",first_father,(int)in->counter_child_tree-1,prec_char); array_tree[(int)in->counter_child_tree-1].father_num=(int)((inp&((1UL<<16)-1))-1); in->counter_child_tree++; } if(x>0){ //printf("Inserisco %c nel nodo %d temp:%d\n",array_tree[temp].c,(int)in->counter_child_tree-2,temp); array_tree[(int)in->counter_child_tree-2].c=array_tree[temp].c; //printf("Inserisco il nodo %d con padre %d\n",(int)in->counter_child_tree-1,(int)(inp&((1UL<<16)-1))-1); /*I have to add a new node*/ array_tree[(int)in->counter_child_tree-1].father_num=(int)(inp&((1UL<<16)-1))-1; out[0]=((int)in->counter_child_tree-2==(int)((inp&((1UL<<16)-1))-1))?array_tree[temp].c:(char)array_tree[(int)((inp&((1UL<<16)-1))-1)].c; //If i have received the last node insert, i have to update the output string becouse i didn't have the char of the node received in->counter_child_tree++; } //out[i+1]='\0'; for(j=i;j>=0;j--){bit_write(file,8, out[j]); //printf("%c",(out[j])); } // I print the buffer string //printf("\n"); i=0; x++; out[0]='\0'; if(in->counter_child_tree>=in->d_max-1){ //need to empty the array based tree for the decompressor free(array_tree); in->counter_child_tree=0; array_init(6700417,in); x=0; goto reset; } } /*rewind(file_to_read->f); uint64_t r; bit_read(file_to_read,8,&r); printf("%c aad\n",(char)r&((1UL<<8)-1)); bit_read(file_to_read,8,&r); printf("%c aad\n",(char)r&((1UL<<8)-1)); bit_read(file_to_read,8,&r); printf("%c aad\n",(char)r&((1UL<<8)-1)); bit_read(file_to_read,8,&r); printf("%c aad\n",(char)r&((1UL<<8)-1)); */ bit_close(file_to_read); }
/*--Function for compress file--*/ void compress(char * str_in,lz78_compressor * in, struct bitio* file) { char* buf=malloc(16); uint64_t inp=0; int father=0; int child=0; int go=1; uint64_t c=0; int flag=0; int ret=0; struct bitio*file_out=bit_open(in->output_file,1); /*After compress i put the info of the file at the beginning of the file*/ info* ret_info=malloc(sizeof(info)); /*Put the info at the beginning of the file and return a structure with the info*/ ret_info=addinfo(file_out,file,in->file_to_compress,0,in->d_max); in->number_of_code^=in->number_of_code; //XOR reset: if(flag==0){ /*Only for the first character*/ ret=bit_read(file,(u_int)8,&inp); buf[1]=(char)(inp&((1UL<<8)-1)); //printf("Ric: %X\n",buf[1] & 0xff); } printf("-----------\n"); while(go==1){ while(look_up_and_add(father,buf[1],in)==0){//repeat the operation until i don't fint a node =>scroll the tree buf[0]=buf[1]; father=hash_table[hash(child,(char)(inp&((1UL<<8)-1)))].child_index; //printf("Padre: %d\n",father); ret=bit_read(file,(u_int)8,&inp); buf[1]=(char)(inp&((1UL<<8)-1)); //printf("Ric: %X\n",buf[1] & 0xff); child=father; } //printf("Output: %X Char %c\n",father & 0xff,buf[1]); bit_write(file_out,16, (int) father); in->number_of_code++; father=0; // restart from the root child=0; c++; if(ret==0)break; if(in->counter_child_tree>=in->d_max-1){ //I need to empty the tree and restart the compressio from the current position free(hash_table); in->counter_child_tree=0; hash_init(6700417,in); goto reset; }; } flush_out_buffer(file_out); //add the SHA-1 of the compressed-file in the header long old_index=ftell(file_out->f);//save the curren pointer to position of the file struct bitio* file_out_rd=bit_open(in->output_file,0);//I re-open the output file but in in read mode this time getSHA1(88,in->number_of_code,file_out_rd,ret_info); //I do the SHA-1 over the whole file bit_close(file_out_rd); fseek(file_out->f,(long)0x40,SEEK_SET); //I write the SHA-1 hash in the correct position //printf("SHA-1: "); for (int i = 0; i < 20; i ++) { bit_write(file_out,(u_int)8*sizeof(unsigned char),(unsigned char)ret_info->sha1[i]); // printf(" %2x", (unsigned char)ret_info->sha1[i]); } bit_write(file_out,(u_int)32,in->number_of_code); //printf("\n"); flush_out_buffer(file_out); fseek(file_out->f,old_index,SEEK_SET); // restore the pointer of the file in order to close the file in the rigth way //close the file descriptor bit_close(file_out); }