Example #1
0
void url_stream_decode(const char *ch,char* dest, int state[]){
  //printf("%c,%d\n",*ch,state[CURSOR]);
   if(state[STAGE]==0){
      if(*ch=='+'){
        dest[state[CURSOR]++]=' ';
      }else if(*ch=='%'){
        state[STAGE]=1;
      }else{
        dest[state[CURSOR]++]=*ch;
      }
   }else if(state[STAGE]==1){
      state[STAGE]=2; 
   }else if (state[STAGE]==2){
//printf("Make char %c:%d, %c:%d\n",*(ch-1),(int)(*(ch-1)),*ch,(int)(*ch));
      if(validHex((char*)ch)&&validHex((char *)(ch-1))){ 
       dest[state[CURSOR]++] = (c_to_hex(*(ch-1)) << 4) + c_to_hex(*ch);
      }
      state[STAGE]=0;
   }
}
Example #2
0
/******************************************************************************
**  Function: read_hex_data()
**
**  Purpose:
**    This routine reads hex digits from the file specified by fc and places the
**    data in the buffer specified by the void buffer pointer.  The software will
**    read data until it hits the end of the line or it has read max_bytes of data.
**    Note that his code assumes that there will be only one data item (packet) per line.
*/
static void read_hex_data(utf_file_context_type *fc, void *buffer, unsigned short int max_bytes, unsigned short int data_type)
{

    unsigned short int  i;
    unsigned short int  bytes_read = 0;
    unsigned long int   *dword_ptr = (unsigned long int *)buffer;
    unsigned short int  *word_ptr  = (unsigned short int *)buffer;
    unsigned char       *byte_ptr  = (unsigned char *)buffer;

    switch(data_type) {

        /* assumes that 1 NUMBER token has already been read prior to calling this routine */

        case UTF_AS_BYTE:

            while ((token != END_OF_LINE) && (bytes_read < max_bytes)) {

                *byte_ptr = 0;

                for (i=0; i < 2; i++) {
                    if (token == END_OF_LINE) UTF_error("File: %s, Line: %d, Not Enough Characters For AS_BYTE Read", fc->filename, fc->line_number-1);
                    if (token != NUMBER)      UTF_error("File: %s, Line: %d, Invalid Hex Number Read", fc->filename, fc->line_number);
                    *byte_ptr = (*byte_ptr << 4) + c_to_hex(number_token);
                    token = get_token(fc);
                }

                bytes_read += 1;
                byte_ptr++;
            }

            if ((token != END_OF_LINE) && (bytes_read >= max_bytes))
                UTF_error("File: %s, Line: %d, Input Data Exceeds Max Size of %d", fc->filename, fc->line_number, max_bytes);

            break;

        case UTF_AS_WORD:

            if ((max_bytes % 2) != 0) UTF_error("File: %s, Line: %d, Invalid Max_Bytes Parameter For AS_WORD Read, %d", fc->filename, fc->line_number, max_bytes);

            while ((token != END_OF_LINE) && (bytes_read < max_bytes)) {

                *word_ptr = 0;

                for (i=0; i < 4; i++) {
                    if (token == END_OF_LINE) UTF_error("File: %s, Line: %d, Not Enough Characters For AS_WORD Read", fc->filename, fc->line_number-1);
                    if (token != NUMBER)      UTF_error("File: %s, Line: %d, Invalid Hex Number Read", fc->filename, fc->line_number);
                    *word_ptr = (*word_ptr << 4) + c_to_hex(number_token);
                    token = get_token(fc);
                }

                bytes_read += 2;
                word_ptr++;
            }

            if ((token != END_OF_LINE) && (bytes_read >= max_bytes))
                UTF_error("File: %s, Line: %d, Input Data Exceeds Max Size of %d", fc->filename, fc->line_number, max_bytes);

            break;

        case UTF_AS_DWORD:

            if ((max_bytes % 4) != 0) UTF_error("File: %s, Line: %d, Invalid Max_Bytes Parameter For AS_DWORD Read, %d", fc->filename, fc->line_number, max_bytes);

            while ((token != END_OF_LINE) && (bytes_read < max_bytes)) {

                *dword_ptr = 0;

                for (i=0; i < 8; i++) {
                    if (token == END_OF_LINE) UTF_error("File: %s, Line: %d, Not Enough Characters For AS_DWORD Read", fc->filename, fc->line_number-1);
                    if (token != NUMBER)      UTF_error("File: %s, Line: %d, Invalid Hex Number Read", fc->filename, fc->line_number);
                    *dword_ptr = (*dword_ptr << 4) + c_to_hex(number_token);
                    token = get_token(fc);
                }

                bytes_read += 4;
                dword_ptr++;
            }

            if ((token != END_OF_LINE) && (bytes_read >= max_bytes))
                UTF_error("File: %s, Line: %d, Input Data Exceeds Max Size of %d", fc->filename, fc->line_number, max_bytes);

            break;

        default:
            UTF_error("File: %s, Line: %d, Invalid DATA_TYPE For Read, %d", fc->filename, fc->line_number, data_type);
            break;
    }
}