Ejemplo n.º 1
0
int uleb128ToUint(FILE *fp, int *from) {
	setOffset(fp, *from);
	int *bits, *temp = (int *)malloc(sizeof(int) * 8);
	int count = 1, i, j, a, y, z = 0, somma = 0;
	//aumenta il count finchè trova byte con il bit più significativo settato a 1
	while((bits = byteToBits(fgetc(fp)))[7] == 1) {
		count++;
	}
	int **converted = (int **)malloc(sizeof(int *) * count);
	fseek(fp, *from, 0);
	//aggiorno il puntatore al prossimo byte per una lettura futura
	*from += count;
	//inizializzo l'array "converted"
	for(i = 0; i < count; i++) {
		converted[i] = (int *)malloc(sizeof(int) * 7);
	}
	//inserisce nell'array "converted" tutti i byte che compongono il numero
	for(i = 0; i < count; i++) {
		temp = byteToBits(fgetc(fp));
		for(j = 0; j < 7; j++) {
			converted[i][j] = temp[j];
		}
	}
	//rovescia l'array (little-endian)
	for(i = 0; i < count; i++) {
		y = 6;
		for(j = 0; j < 4; j++) {
			a = converted[i][j];
			converted[i][j] = converted[i][y];
			converted[i][y--] = a;
		}
	}
	//calcola il numero
	for(i = 0; i < count; i++) {
		for(j = 6; j >= 0; j--) {
			if(converted[i][j] == 1) {
				somma += potenza(2, z);
			}
			z++;
		}
	}
	return somma;
}
Ejemplo n.º 2
0
Archivo: crc.c Proyecto: stendler/TI3-C
void refillBits(char bits[16], FILE *fp){
    //memset(&bits,0,sizeof(bits));
    //TODO DECODE mode?
    unsigned char next1, next2, next3;
    next1 = (unsigned char)fgetc(fp);
    next2 = (unsigned char)fgetc(fp);
    //next3 = (unsigned char)fgetc(fp);
    //printf("NextChars: %d %d %d\n",next1,next2,next3);
    byteToBits(bits,next1,next2);
    fseek(fp,-1,SEEK_CUR);
    //if(next3 == EOF){
    //  return 1;
    //}else{
    //  return 0;
    //}
}
Ejemplo n.º 3
0
Archivo: crc.c Proyecto: stendler/TI3-C
int main(int argc, char *argv[])
{
  if(argc == 2){ //argumente ueberpruefen
    //char mode;
    /*unsigned char divident[2]; //CRC16 = x 16 + x 15 + x 2 + 1
    // 1100 0000  0000 0101
    divident[0] = 192;
    divident[1] = 5;*/
    char divisor[16];
    byteToBits(divisor,(unsigned char)192,(unsigned char)5);
    //DEBUG
    dbgPrintBits(divisor);

    FILE *fp = fopen(argv[1],"r");
    if(fp != NULL){
      //.crc endung? --> letzte 4 Zeichen auf .crc ueberpruefen
      /*for(int i = 0; i<10;i++){printf("%c %d\n",argv[1][i],argv[1][i]);   fflush(stdout);    }*/ //string terminiert bei 0 - wer haette es gedacht..
      int len = 0;
      while(argv[1][len] != 0){
        len++;
      }
      if(
        argv[1][len-4] == '.' &&
        argv[1][len-3] == 'c' &&
        argv[1][len-2] == 'r' &&
        argv[1][len-1] == 'c'
      ){
        //check checksum
        MODE = DECODE;
      }else{
        //create checksum
        //dateiinhalt um 2 leere (16x0) bytes erweitern
        //immer 2 bytes einlesen und durch divident teilen (mod 2) / bzw xor
        MODE = ENCODE;
      }
      printf("MODE: %d\n\n",MODE);
      //neuer dateiname
      char filename[128];
      memset(filename,0,sizeof filename);

      //filename = argv[1];
      int i = 0;
      while(argv[1][i] != 0 && i < 123){
        filename[i] = argv[1][i];
        i++;
      }
      if(MODE == 1){ //ENCODE
        filename[i] = '.';
        filename[i+1] = 'c';
        filename[i+2] = 'r';
        filename[i+3] = 'c';
      }else{  //DECODE
        filename[i] = 0;
      }
      //DEBUG
      printf("filename %s\n\n",filename);

      char remain[2];
      //create file
      FILE *outputf = fopen(filename,"w+");
      if(outputf != NULL){
      //write file
      if(MODE){
          //ENCODE : complete
          char buffer;
          while((buffer= fgetc(fp)) != EOF){
            fputc(buffer,outputf);
          }
      }else{
        //DECODE : all except the last 2 bytes
          char buffer;
          while ((buffer = fgetc(fp)) != EOF){
            fputc(buffer,outputf);
            remain[0] = fgetc(fp);
            remain[1] = fgetc(fp);
            buffer = fgetc(fp);
            if(buffer == EOF){
              break;
            }else{
              fseek(fp,-3,SEEK_CUR);
            }
          }
      }

      //DEBUG
      printf("File copied\n");
      //fseek
      if(fseek(outputf,0,SEEK_SET)!=0){
          printf("error seeking in file\n");
      }
      return 0;
      char bits[16], queue[16];
      refillBits(bits,outputf);
      refillBits(queue,outputf);

      //DEBUG
      printf("Filled bits: \n");
      dbgPrintBits(bits);
      dbgPrintBits(queue);

      shiftBits(bits,queue,outputf);
      //DEBUG
      printf("shifted bits: \n");
      dbgPrintBits(bits);
      dbgPrintBits(queue);

      //return 0;
      //algorithm
      while(queue[0] != -1){
        xor(bits,divisor);
        shiftBits(bits,queue,outputf);
      }

      if(MODE){
          //ENCODE --> add remainder to file
          int remIndicator = 0;
          int c = 128;
          for(int i = 0;i<16;i++){
            //DEBUG
            //printf("i = %d , byte: %d, c: %d\n",i,helpbyte,c);
            if(bits[i] == 1){
              //DEBUG
              //printf("Byte: %d, C: %d\n",helpbyte,c);
              remain[remIndicator] += c;
            }
            if(c == 1){
              if(remIndicator == 1){
                break;
              }
              //DEBUG
              //printf("RESET c = %d byte = %d\n",c,helpbyte);
              c = 128;
              remIndicator = 1;
            }else{
              //DEBUG
              //printf("c: %d :/ 2 = \n",c);
              c /= 2;
              //printf("%d\n",c);
            }
          }
          fputc(remain[0],outputf);
          fputc(remain[1],outputf);
      }else{
          //DECODE check remainder
          char checksum[16];
          byteToBits(checksum,remain[0],remain[1]);
          for(int z = 0; z<5;z++){
            if(checksum[i] != bits[i]){
              printf("CRC-Checksum stimmt nicht mit dem Inhalt ueberein!\n");
              fclose(outputf);
              remove(filename);
              return 0;
            }
          }
      }
      //printf("EOF: %d %d\n",EOF,(unsigned char)EOF);
      //printf("asci 10: %c\n",10);
      fclose(fp);
      fclose(outputf);
    }else{
      printf("Could not create File %s\n",filename);
    }
    }else{
      printf("File not found\n");
    }
  }else{
    printf("usage: %s <filename>\n",argv[0]);
  }
}