Ejemplo n.º 1
0
/*FUNCTION*----------------------------------------------------------------
*
* Function Name  : get_spair
* Returned Value : unsigned char, converted hex byte
* Comments       : Gets pair of characters, and converts to hex byte
*     
*END*--------------------------------------------------------------------*/
static uint_8 get_spair
   (
     /* [IN] the array to parse */
     uint_8 *arr,
     /* [IN] point to array data */
     uint_8 point
   )
{
    /* Body */
    uint_8 ch;
    uint_8 upper,lower;

    ch = arr[point];
    upper = (uint_8)(get_hex_value(ch));
    if(upper == 0xFF) 
    {
        /* Not a proper S19 file */
        _s19_file_done = TRUE;
    } /* EndIf */
    upper = (uint_8)(upper << 4);
    ch=arr[point+1];
    lower= (uint_8)(get_hex_value(ch));

    if(lower == 0xFF) 
    {
        /* Not a proper S19 file */
        _s19_file_done = TRUE;
    } /* EndIf */
    return (uint_8)(upper | lower);
} /* EndBody */
Ejemplo n.º 2
0
gint qp_decode_line(gchar *str)
{
	gchar *inp = str, *outp = str;

	while (*inp != '\0') {
		if (*inp == '=') {
			if (inp[1] && inp[2] &&
			    get_hex_value(outp, inp[1], inp[2]) == TRUE) {
				inp += 3;
			} else if (inp[1] == '\0' || isspace((guchar)inp[1])) {
				/* soft line break */
				break;
			} else {
				/* broken QP string */
				*outp = *inp++;
			}
		} else {
			*outp = *inp++;
		}
		outp++;
	}

	*outp = '\0';

	return outp - str;
}
Ejemplo n.º 3
0
gint qp_decode_q_encoding(guchar *out, const gchar *in, gint inlen)
{
	const gchar *inp = in;
	guchar *outp = out;

	if (inlen < 0)
		inlen = G_MAXINT;

	while (inp - in < inlen && *inp != '\0') {
		if (*inp == '=' && inp + 3 - in <= inlen) {
			if (get_hex_value(outp, inp[1], inp[2]) == TRUE) {
				inp += 3;
			} else {
				*outp = *inp++;
			}
		} else if (*inp == '_') {
			*outp = ' ';
			inp++;
		} else {
			*outp = *inp++;
		}
		outp++;
	}

	*outp = '\0';

	return outp - out;
}
Ejemplo n.º 4
0
int hts_unescapeEntitiesWithCharset(const char *src, char *dest, const size_t max, const char *charset) {
  size_t i, j, ampStart, ampStartDest;
  int uc;
  int hex;
  unsigned int hash;

  assert(max != 0);
  for(i = 0, j = 0, ampStart = (size_t) -1, ampStartDest = 0,
        uc = -1, hex = 0, hash = 0 ; src[i] != '\0' ; i++) {
    /* start of entity */
    if (src[i] == '&') {
      ampStart = i;
      ampStartDest = j;
      hash = 0;
      uc = -1;
    }
    /* inside a potential entity */
    else if (ampStart != (size_t) -1) {
      /* &#..; entity */
      if (ampStart + 1 == i && src[ampStart + 1] == '#') {
        uc = 0;
        hex = 0;
      }
      /* &#x..; entity */
      else if (ampStart + 2 == i && src[ampStart + 1] == '#'
               && src[ampStart + 2] == 'x') {
        hex = 1;
      }
      /* end of entity */
      else if (src[i] == ';') {
        size_t len;
        
        /* decode entity */
        if (uc == -1) {
          /* &foo; */
          uc = decode_entity(hash, /*&src[ampStart + 1],*/
                             i - ampStart - 1);
          /* FIXME: TEMPORARY HACK FROM PREVIOUS VERSION TO BE INVESTIGATED */
          if (uc == 160) {
            uc = 32;
          }
        }
        
        /* end */
        ampStart = (size_t) -1;
        
        /* success ? */
        if (uc > 0) {
          const size_t maxOut = max - ampStartDest;
          /* write at position */
          if (charset != NULL && hts_isCharsetUTF8(charset)) {
            len = hts_writeUTF8(uc, &dest[ampStartDest], maxOut);
          } else {
            size_t ulen;
            char buffer[32];
            len = 0;
            if ( ( ulen = hts_writeUTF8(uc, buffer, sizeof(buffer)) ) != 0) {
              char *s;
              buffer[ulen] = '\0';
              s = hts_convertStringFromUTF8(buffer, strlen(buffer), charset);
              if (s != NULL) {
                const size_t sLen = strlen(s);
                if (sLen < maxOut) {
                  /* Do not copy \0. */
                  memcpy(&dest[ampStartDest], s, sLen);
                  len = sLen;
                }
                free(s);
              }
            }
          }
          if (len > 0) {
            /* new dest position */
            j = ampStartDest + len;
            /* do not copy ; */
            continue;
          }
        }
      }
      /* numerical entity */
      else if (uc != -1) {
        /* decimal */
        if (!hex) {
          if (src[i] >= '0' && src[i] <= '9') {
            const int h = src[i] - '0';
            uc *= 10;
            uc += h;
          } else {
            /* abandon */
            ampStart = (size_t) -1;
          }
        }
        /* hex */
        else {
          const int h = get_hex_value(src[i]);
          if (h != -1) {
            uc *= 16;
            uc += h;
          } else {
            /* abandon */
            ampStart = (size_t) -1;
          }
        }
      }
      /* alphanumerical entity */
      else {
        /* alphanum and not too far ('&thetasym;' is the longest) */
        if (i <= ampStart + 10 &&
            (
             (src[i] >= '0' && src[i] <= '9')
             || (src[i] >= 'A' && src[i] <= 'Z')
             || (src[i] >= 'a' && src[i] <= 'z')
             )
            ) {
          /* compute hash */
          HASH_ADD(hash, (unsigned char) src[i]);
        } else {
          /* abandon */
          ampStart = (size_t) -1;
        }
      }
    }
    
    /* copy */
    if (j + 1 > max) {
      /* overflow */
      return -1;
    }
    if (src != dest || i != j) {
      dest[j] = src[i];
    }
    j++;
  }
  dest[j] = '\0';

  return 0;
}
Ejemplo n.º 5
0
int hts_unescapeUrlSpecial(const char *src, char *dest, const size_t max,
                           const int flags) {
  size_t i, j, lastI, lastJ, k, utfBufferJ, utfBufferSize;
  int seenQuery = 0;
  char utfBuffer[32];

  assert(src != dest);
  assert(max != 0);

  for(i = 0, j = 0, k = 0, utfBufferJ = 0, utfBufferSize = 0,
      lastI = (size_t) -1, lastJ = (size_t) -1
      ; src[i] != '\0' ; i++) {
    char c = src[i];
    unsigned char cUtf = (unsigned char) c;

    /* Replacement for ' ' */
    if (c == '+' && seenQuery) {
      c = cUtf = ' ';
      k = 0;  /* cancel any sequence */
    }
    /* Escape sequence start */
    else if (c == '%') {
      /* last known position of % written on destination
         copy blindly c, we'll rollback later */
      lastI = i;
      lastJ = j;
    }
    /* End of sequence seen */
    else if (i >= 2 && i == lastI + 2) {
      const int a1 = get_hex_value(src[lastI + 1]);
      const int a2 = get_hex_value(src[lastI + 2]);
      if (a1 != -1 && a2 != -1) {
        const char ec = a1*16 + a2;  /* new character */
        cUtf = (unsigned char) ec;

        /* Shortcut for ASCII (do not unescape non-printable) */
        if (
            (cUtf < 0x80 && cUtf >= 32)
            && ( flags & UNESCAPE_URL_NO_ASCII ) == 0
            ) {
          /* Rollback new write position and character */
          j = lastJ;
          c = ec;
        }
      } else {
        k = 0;  /* cancel any sequence */
      }
    }
    /* ASCII (and not in %xx) */
    else if (cUtf < 0x80 && i != lastI + 1) {
      k = 0;  /* cancel any sequence */
      if (c == '?' && !seenQuery) {
        seenQuery = 1;
      }
    }
    
    /* UTF-8 sequence in progress (either a raw or a %xx character) */
    if (cUtf >= 0x80) {
      /* Leading UTF ? */
      if (HTS_IS_LEADING_UTF8(cUtf)) {
        k = 0;  /* cancel any sequence */
      }

      /* Copy */
      if (k < sizeof(utfBuffer)) {
        /* First character */
        if (k == 0) {
          /* New destination-centric offset of utf-8 buffer beginning */
          if (i == lastI + 2) {  /* just read a %xx */
            utfBufferJ = lastJ;  /* position of % */
          } else {
            utfBufferJ = j;      /* current position otherwise */
          }

          /* Sequence length */
          utfBufferSize = hts_getUTF8SequenceLength(cUtf);
        }

        /* Copy */
        utfBuffer[k++] = cUtf;

        /* Flush UTF-8 buffer when completed. */
        if (k == utfBufferSize) {
          const size_t nRead = hts_readUTF8(utfBuffer, utfBufferSize, NULL);

          /* Reset UTF-8 buffer in all cases. */
          k = 0;

          /* Was the character read successfully ? */
          if (nRead == utfBufferSize) {
            /* Rollback write position to sequence start write position */
            j = utfBufferJ;

            /* Copy full character sequence */
            memcpy(&dest[j], utfBuffer, utfBufferSize);
            j += utfBufferSize;

            /* Skip current character */
            continue;
          }
        }
      }
    }

    /* Check for overflow */
    if (j + 1 > max) {
      return -1;
    }

    /* Copy current */
    dest[j++] = c;
  }
  dest[j] = '\0';

  return 0;
}
Ejemplo n.º 6
0
void parse_urb_body(struct urb* urb)
{
	int setup_packet = 0;

	while (fetch_line())
	{
		if (line[0] == '[')
			break;
			
		if (strstr(line, "TransferBufferLength"))
		{
			urb->len = get_hex_value();
		}
		else if (strstr(line, "TransferFlags"))
		{
			urb->flags = get_hex_value();
		}
		else if (strstr(line, "Index"))
		{
			urb->index = get_hex_value();
		}
		else if (strstr(line, "DescriptorType"))
		{
			urb->type = get_hex_value();
		}
		else if (strstr(line, "RequestTypeReservedBits"))
		{
			urb->reserved = get_hex_value();
		}
		else if (strstr(line, "Request"))
		{
			urb->request = get_hex_value();
		}
		else if (strstr(line, "Value"))
		{
			urb->value = get_hex_value();
		}
		else if (strstr(line, "SetupPacket"))
		{
			setup_packet = 1;
		}
		else if (strstr(line, "00000000:"))
		{
			if (!setup_packet && !urb->data)
				urb->data = parse_data_dump(urb->len);
		}
		else if (strstr(line, "-- URB_FUNCTION_"))
		{
			if (strstr(line, "URB_FUNCTION_CONTROL_TRANSFER"))
				urb->func = FUNC_CONTROL_TXFER;
			else if (strstr(line, "URB_FUNCTION_CLASS_INTERFACE"))
				urb->func = FUNC_CLASS_INTERFACE;
			else if (strstr(line, "URB_FUNCTION_GET_DESCRIPTOR_FROM_DEVICE"))
				urb->func = FUNC_GET_DESC;
			else if (strstr(line, "URB_FUNCTION_SELECT_CONFIGURATION"))
				urb->func = FUNC_SELECT_CONFIG;
			else if (strstr(line, "URB_FUNCTION_GET_DESCRIPTOR_FROM_INTERFACE"))
				urb->func = FUNC_GET_DESC_FROM_IFACE;
			else if (strstr(line, "URB_FUNCTION_BULK_OR_INTERRUPT_TRANSFER"))
				urb->func = FUNC_BULK_TXFER;
			else if (strstr(line, "URB_FUNCTION_RESET_PIPE"))
				urb->func = FUNC_RESET_PIPE;
			else if (strstr(line, "URB_FUNCTION_ABORT_PIPE"))
				urb->func = FUNC_ABORT_PIPE;
			else
			{
				urb->func = FUNC_UNKNOWN;
				printf("Unknown FUNC: %s\n", line);
				exit(0);
			}
		}
	}
}
Ejemplo n.º 7
0
/* This function pulls intel hex formatted code from the serial port and loads 
   it into a temporary location. Once a complete SPM page length is stored,
   it executes a flash memory write.
    Return Error
	* ERR 1 - File upload synchronization is off (i.e. the ':' was not found)
	* ERR 2 - the checksum failed.

 */
char ihex_load(void)
{
    uint8_t
	i,temp_char,
	byte_count = 0,
	data_pairs,
	data_type,
	temp_byte,
	temp_store,
	address_lo,
	address_hi;

    unsigned int data; // temporary location to store program words
    
    unsigned long temp_address = 0ul;   //Tmp page fill address ??
	
	
    do
    {
	// Wait for characters 
	    do
		{
			temp_char = uart_read();
			if ( (temp_char == 13)  || (temp_char == 10))   // get and dispose of the CR or LF
					temp_char = 0;                           // Wait for first character of line
		} while (!temp_char);
	   if(temp_char != ':') // check to make sure the first character is ':'
	   {
	    return(1);
	   }

	
	/* get the count of data pairs */
 
	data_pairs  = get_hex_value();
	
	/* get the address to put the data */
	/* although we collect this data, we do not use it.  All data
	   programmed through this bootloader starts at application program
	   space location 0x0000. The collection is neccessary only for
	   the checksum calculation. */
	address_hi = get_hex_value();
	address_lo = get_hex_value();
	
	/* get the data type */
	data_type  = get_hex_value();
	
	temp_store = data_pairs + address_hi + address_lo + data_type;
	
	for( i = 0; i < data_pairs; i++ )
	{

		temp_byte = get_hex_value();		
	    page_data[byte_count] = temp_byte;	
		byte_count++;
	    temp_store += temp_byte;
	}
	    
	/* get the checksum */
    temp_byte = get_hex_value();
	

	

	/* check the data and checksum */
	if( (char)(temp_store + temp_byte) )
	{
	    return(2);
	}


	/* fill the rest of the page buffer with 0xFF if the last records are not 
	   a full page in length */
	if( data_type)   // End of File record 
	{
		
		for(byte_count; byte_count < SPM_PAGESIZE; byte_count++ )
	    {
			page_data[byte_count] = 0xFF;
	    }
	}

	/* if the page buffer is full, write the buffer to the temp flash buffer */
	if( byte_count >= SPM_PAGESIZE )
	{
	    byte_count = 0;

	    /* store data in temp buffer and write to flash */
	    for( i = 0; i < SPM_PAGESIZE; i += 2 )
	    {
		/* swap the bytes for little endian format */
		data = page_data[i];
		data |= ( page_data[ i + 1 ] << 8 );
		boot_page_fill( temp_address, data ); // call asm routine to load temporary flash buffer
		temp_address += 2; // select next word in temporary buffer
	    }

	    /* write to the flash */
	    boot_page_write( temp_address - SPM_PAGESIZE );
	    boot_spm_busy_wait();	
	    boot_rww_enable();				//Re-enable the RWW section
	    uart_write_char('*');
		// Display - characters 
		E_OUT_PORTA  =  0x73;  // Show P while programming 
		E_OUT_PB1(0);
		E_OUT_PB2(1);
	}
 
   } while( data_type != 1);            // Do while not end of File
	
	uart_write_char('E');                // End of file
    return(0);
}