示例#1
0
static void
write_record (int move_back_flag)
{
  save_record = record_start;
  record_start = new_record;

  if (archive == STDIN)
    {
      archive = STDOUT;
      flush_write ();
      archive = STDIN;
    }
  else
    {
      move_archive (-(records_read + 1));
      flush_write ();
    }

  record_start = save_record;

  if (move_back_flag)
    {
      /* Move the tape head back to where we were.  */

      if (archive != STDIN)
	move_archive (records_read);

      records_read--;
    }

  blocks_needed = blocking_factor;
  new_blocks = 0;
}
/* Write out the record which has been filled.  If MOVE_BACK_FLAG,
   backspace to where we started.  */
static void
write_record (int move_back_flag)
{
  union block *save_record = record_start;
  record_start = new_record;

  if (acting_as_filter)
    {
      archive = STDOUT_FILENO;
      flush_write ();
      archive = STDIN_FILENO;
    }
  else
    {
      move_archive ((records_written + records_skipped) - records_read);
      flush_write ();
    }

  record_start = save_record;

  if (move_back_flag)
    {
      /* Move the tape head back to where we were.  */

      if (! acting_as_filter)
	move_archive (records_read - (records_written + records_skipped));
    }

  new_blocks = 0;
}
示例#3
0
static ssize_t write(struct file * file, const char __user * userbuf,
		     size_t count, loff_t * off)
{
	char *buffer = file->private_data;
	struct dentry *dentry = file->f_dentry;
	int size = dentry->d_inode->i_size;
	loff_t offs = *off;

	if (count > PAGE_SIZE)
		count = PAGE_SIZE;
	if (size) {
		if (offs > size)
			return 0;
		if (offs + count > size)
			count = size - offs;
	}

	if (copy_from_user(buffer, userbuf, count))
		return -EFAULT;

	count = flush_write(dentry, buffer, offs, count);
	if (count > 0)
		*off = offs + count;
	return count;
}
示例#4
0
// read_cb: a TCP connection is readable so read the bytes that are on
// it and pass them to OpenSSL
void read_cb(uv_stream_t *s, ssize_t nread, const uv_buf_t *buf)
{
  connection_state *state = (connection_state *)s->data;

  if (nread > 0) {

    // If there's data to read then pass it to OpenSSL via the BIO
    // TODO: check return value

    BIO_write(state->read_bio, buf->base, nread);
  }

  if ((nread == UV_EOF) || (nread < 0)) {
    connection_terminate(state->tcp);
  } else {
    if (do_ssl(state)) {
      write_queued_messages(state);
      flush_write(state);
    } else {
      connection_terminate(state->tcp);
    }
  }

  // Buffer was previously allocated by us in a call to
  // allocate_cb. libuv will not reuse so we must free.

  if (buf) {
    free(buf->base);
  }
}
示例#5
0
static ssize_t write(struct file *file, const char __user *userbuf,
		     size_t bytes, loff_t *off)
{
	struct bin_buffer *bb = file->private_data;
	struct dentry *dentry = file->f_path.dentry;
	int size = dentry->d_inode->i_size;
	loff_t offs = *off;
	int count = min_t(size_t, bytes, PAGE_SIZE);

	if (size) {
		if (offs > size)
			return 0;
		if (offs + count > size)
			count = size - offs;
	}

	mutex_lock(&bb->mutex);

	if (copy_from_user(bb->buffer, userbuf, count)) {
		count = -EFAULT;
		goto out_unlock;
	}

	count = flush_write(dentry, bb->buffer, offs, count);
	if (count > 0)
		*off = offs + count;

 out_unlock:
	mutex_unlock(&bb->mutex);
	return count;
}
示例#6
0
Errcode slow_to_wacom(SHORT port, char *s)
/* send a string out serial port with a delay afterwards */
{
	ser_write(port,s,strlen(s));
	flush_write(port);
	wait_milli(150);
	return(ser_status(port));
}
示例#7
0
文件: util.c 项目: grfrost/fpgajtag
/*
 * Read utility functions
 */
uint8_t *read_data(void)
{
    static uint8_t last_read_data[10000];
    int i, j, expected_len = 0, extra_bytes = 0;

    if (trace)
        printf("[%s]\n", __FUNCTION__);
    if (buffer_current_size())
        *usbreadbuffer_ptr++ = SEND_IMMEDIATE; /* tell the FTDI that we are waiting... */
    flush_write(NULL);
    last_read_data_length = 0;
    for (i = 0; i < read_size_ptr; i++) {
        if (read_size[i] > 0)
            expected_len += read_size[i];
        else {
            if (i > 0 && read_size[i-1] < 0)
                extra_bytes++; /* we will squeeze out partial bytes in the processing below */
            else
                expected_len++;
        /* When there are 2 bit operations in a row, this is just accumulating
         * shifted bits into a register for return to user.  When exiting
         * Shift-DR/IR state, the last bit shifted is not performed with DATAWBITS,
         * but with a TMS operation.  For this reason, the combo of 2 bit ops
         * in a row is quite common.
         */
        }
    }
    if (expected_len + extra_bytes)
        ftdi_read_data(global_ftdi, last_read_data, expected_len + extra_bytes);
    last_read_data_length = expected_len;
    if (expected_len) {
        uint8_t *p = last_read_data;
        int validbits = 0;
        for (i = 0; i < read_size_ptr; i++) {
            if (read_size[i] < 0) {
                validbits -= read_size[i];
                if (validbits < 0 || validbits > 8) {
                    printf("[%s] validbits %d big\n", __FUNCTION__, validbits);
                    validbits = 8;
                    //exit(-1);
                }
                *p &= (0xff << (8-validbits));
                /* NOTE: when trying to combine back data bits that result from a TMS
                 * shift with > 1 bit, be aware that the number of bits shifted in reflects the
                 * _number of TMS bits shifted_, not the number of valid data bits (which is
                 * always only 1).
                 */
                if (i > 0 && read_size[i-1] < 0) {
                    *(p-1) = *p >> (8-validbits);    /* put result into LSBs */
                    /* Note: union datatypes work correctly, but int needs the data as MSBs! */
                    for (j = 0; j < expected_len; j++)  /* copies too much, but... */
                        *(p+j) = *(p+j+1);  /* move the data down in the buffer 1 byte */
                }
                else
                    p++;
            }
示例#8
0
	void flush_or_clean()
	{
	    switch(get_mode())
	    {
	    case gf_read_only:
		clean_read();
		break;
	    case gf_write_only:
	    case gf_read_write:
		flush_write();
		break;
	    default:
		throw SRC_BUG;
	    }
	};
示例#9
0
static ssize_t write(struct file *file, const char __user *userbuf,
		     size_t bytes, loff_t *off)
{
	struct bin_buffer *bb = file->private_data;
	struct dentry *dentry = file->f_path.dentry;
	int size = dentry->d_inode->i_size;
	loff_t offs = *off;
	int count = min_t(size_t, bytes, PAGE_SIZE);
	char *temp;

	if (!bytes)
		return 0;

	if (size) {
		if (offs > size)
			return 0;
		if (offs + count > size)
			count = size - offs;
	}

	temp = kmalloc(count, GFP_KERNEL);
	if (!temp)
		return -ENOMEM;

	if (copy_from_user(temp, userbuf, count)) {
		count = -EFAULT;
		goto out_free;
	}

	mutex_lock(&bb->mutex);

	memcpy(bb->buffer, temp, count);

	count = flush_write(dentry, bb->buffer, offs, count);
	mutex_unlock(&bb->mutex);

	if (count > 0)
		*off = offs + count;

out_free:
	kfree(temp);
	return count;
}
示例#10
0
static ssize_t write(struct file *file, const char __user *userbuf,
		     size_t bytes, loff_t *off)
{
	struct bin_buffer *bb = file->private_data;
	struct dentry *dentry = file->f_path.dentry;
	int size = dentry->d_inode->i_size;
	loff_t offs = *off;
	int count = min_t(size_t, bytes, PAGE_SIZE);
	char *temp;

	if (!bytes)
		return 0;

	if (size) {
		if (offs > size)
			return 0;
		if (offs + count > size)
			count = size - offs;
	}

	temp = memdup_user(userbuf, count);
	if (IS_ERR(temp))
		return PTR_ERR(temp);

	mutex_lock(&bb->mutex);

	memcpy(bb->buffer, temp, count);

	count = flush_write(dentry, bb->buffer, offs, count);
	mutex_unlock(&bb->mutex);

	if (count > 0)
		*off = offs + count;

	kfree(temp);
	return count;
}
示例#11
0
文件: level.c 项目: igitur/ffe
static void
print_level_text(struct level *l,uint8_t *buffer)
{
    register uint8_t *text = buffer;

    if(!buffer) return;
    start_write();
    while(*text)
    {
        if(*text == '%' && text[1])
        {
            text++;
            switch(*text)
            {
                case 'g':
                    if(l) writes(l->group_name);
                    break;
                case 'm':
                    if(l) writes(l->element_name);
                    break;
                case '%':
                    writec('%');
                    break;
                default:
                    writec('%');
                    writec(*text);
                    break;
            }
        } else
        {
            writec(*text);
        }
        text++;
    }
    flush_write();
}
示例#12
0
Errcode wac_input(Idriver *idr)
{
static char wlinebuf[32];
char *s = wlinebuf;
int count = sizeof(wlinebuf)-2;
int c;
int stype;
int temp;
int dp;
int ux,uy;
SHORT port = idr->comm_port;

ser_write(port, "RQ1\n", 4);
flush_write(port);
while (--count >= 0)
	{
	if ((c = timeout_get_char(port)) < Success)
		{
		return(c);
		}
	if ((*s++ = c) == '\n')
		break;
	}
*s = 0;
s = wlinebuf;
switch (*s++)	/* see what first character of packet says it is */
	{
	case '!':
		stype = PSTYLUS;
		break;
	case '#':
		stype = STYLUS;
		break;
	case '*':
		stype = PUCK;		/* it's a puck */
		break;
	default:
		flush_read(port);
		return(Err_version);
	}
s = skip_past_comma(s);
ux = atoi(s) - 120;
s = skip_past_comma(s);
uy = atoi(s) - 120;
s = skip_past_comma(s);
temp = atoi(s);
switch (stype)
	{
	case PSTYLUS:
		idr->buttons = 0; 
		wac_pos[2] = 0;
		if (temp > -99 && temp < 99)	/* filter to reasonable values */
			{
			wac_pos[0] = ux;
			wac_pos[1] = uy;
			if (temp < pmin)
				pmin = temp;
			if (temp > pmax)
				pmax = temp;
			dp = pmax-(pmin+PTHRESH);
			temp -= pmin+PTHRESH;
			if (temp > 0)
				{
				wac_pos[2] = temp*PMAX/dp;
				idr->buttons = 1;
				}
			}
		/* check for control key to interpret a right click 
			(Arr, no side button on pressure sensitive stylus!) */
		if (jkey_shift() & 0x4)	/* right button on control key */
			idr->buttons |= 0x2;
		break;
	case STYLUS:
		if (temp != 99)
			{
			wac_pos[0] = ux;
			wac_pos[1] = uy;
			idr->buttons = temp;
			}
		break;
	case PUCK:
		idr->buttons = 0;
		if (temp != 99)
			{
			wac_pos[0] = ux;
			wac_pos[1] = uy;
			if (temp&2)
				idr->buttons |= 1;
			if (temp&4)
				idr->buttons |= 2;
			}
		break;
		}
return(Success);
}
示例#13
0
// do_ssl: process pending data from OpenSSL and send any data that's
// waiting. Returns 1 if ok, 0 if the connection should be terminated
int do_ssl(connection_state *state)
{
  BYTE *response = NULL;
  int response_len = 0;
  kssl_error_code err;

  // First determine whether the SSL_accept has completed. If not then any
  // data on the TCP connection is related to the handshake and is not
  // application data.

  if (!state->connected) {
    if (!SSL_is_init_finished(state->ssl)) {
      int rc = SSL_do_handshake(state->ssl);
  
      if (rc != 1) {
        switch (SSL_get_error(state->ssl, rc)) {
        case SSL_ERROR_WANT_READ:
        case SSL_ERROR_WANT_WRITE:
          ERR_clear_error();
          return 1;
          
        default:
          ERR_clear_error();
          return 0;
        }
      }
    }

    state->connected = 1;
  }

  // Read whatever data needs to be read (controlled by state->need)

  while (state->need > 0) {
    int read = SSL_read(state->ssl, state->current, state->need);

    if (read <= 0) {
      int err = SSL_get_error(state->ssl, read);
      switch (err) {

        // Nothing to read so wait for an event notification by exiting
        // this function, or SSL needs to do a write (typically because of
        // a connection regnegotiation happening) and so an SSL_read
        // isn't possible right now. In either case return from this
        // function and wait for a callback indicating that the socket
        // is ready for a read.

      case SSL_ERROR_WANT_READ:
      case SSL_ERROR_WANT_WRITE:
        ERR_clear_error();
        return 1;

        // Connection termination

      case SSL_ERROR_ZERO_RETURN:
        ERR_clear_error();
        return 0;

        // Something went wrong so give up on connetion

      default:
        log_ssl_error(state->ssl, read);
        return 0;
      }
    }

    // Read some number of bytes into the state->current buffer so move that
    // pointer on and reduce the state->need. If there's still more
    // needed then loop around to see if we can read it. This is
    // essential because we will only get a single event when data
    // becomes ready and will need to read it all.

    state->need -= read;
    state->current += read;

    if (state->need > 0) {
      continue;
    }

    // All the required data has been read and is in state->start. If
    // it's a header then do basic checks on the header and then get
    // ready to receive the payload if there is one. If it's the
    // payload then the entire header and payload can now be
    // processed.

    if (state->state == CONNECTION_STATE_GET_HEADER) {
      err = parse_header(state->wire_header, &state->header);
      if (err != KSSL_ERROR_NONE) {
        return 0;
      }

      state->start = 0;

      if (state->header.version_maj != KSSL_VERSION_MAJ) {
        write_log(1, "Message version mismatch %02x != %02x",
                  state->header.version_maj, KSSL_VERSION_MAJ);
        write_error(state, state->header.id, KSSL_ERROR_VERSION_MISMATCH);
        clear_read_queue(state);
        free_read_state(state);
        set_get_header_state(state);
        return 1;
      }

      // If the header indicates that a payload is coming then read it
      // before processing the operation requested in the header

      if (state->header.length > 0) {
        if (!set_get_payload_state(state, state->header.length)) {
          write_log(1, "Memory allocation error");
          write_error(state, state->header.id, KSSL_ERROR_INTERNAL);
          clear_read_queue(state);
          free_read_state(state);
          set_get_header_state(state);
          return 1;
        }
        continue;
      }
    } if (state->state == CONNECTION_STATE_GET_PAYLOAD) {

      // Do nothing here. If we reach here then we know that the
      // entire payload has been read.

    } else {

      // This should be unreachable. If this occurs give up processing
      // and reset.

      write_log(1, "Connection in unknown state %d", state->state);
      free_read_state(state);
      set_get_header_state(state);
      return 1;
    }

    // When we reach here state->header is valid and filled in and if
    // necessary state->start points to the payload.

    uv_rwlock_rdlock(pk_lock);
    err = kssl_operate(&state->header, state->start, privates, &response,
                       &response_len);
    if (err != KSSL_ERROR_NONE) {
      log_err_error();
    } else  {
      queue_write(state, response, response_len);
    }
    uv_rwlock_rdunlock(pk_lock);

    // When this point is reached a complete header (and optional payload)
    // have been received and processed by the switch() statement above. So,
    // write the queued messages and then free the allocated memory and get
    // ready to receive another header.

    write_queued_messages(state);
    flush_write(state);

    free_read_state(state);
    set_get_header_state(state);

    // Loop around again in case there are multiple requests queued
    // up by OpenSSL. 
  }

  return 1;
}
示例#14
0
	void inherited_flush_read() { flush_write(); clean_read(); };
示例#15
0
	void inherited_sync_write() { flush_write(); };
示例#16
0
    void compressor::local_terminate()
    {
        if(compr != NULL)
        {
            S_I ret;

                // flushing the pending data
	    flush_write();
            clean_write();

            ret = compr->wrap.compressEnd();
            delete compr;
	    compr = NULL;

            switch(ret)
            {
            case WR_OK:
                break;
            case WR_DATA_ERROR: // some data remains in the compression pipe (data loss)
                throw SRC_BUG;
            case WR_STREAM_ERROR:
                throw Erange("compressor::~compressor", gettext("compressed data is corrupted"));
            default :
                throw SRC_BUG;
            }
        }

        if(decompr != NULL)
        {
                // flushing data
            flush_read();
            clean_read();

            S_I ret = decompr->wrap.decompressEnd();
            delete decompr;
	    decompr = NULL;

            switch(ret)
            {
            case WR_OK:
                break;
            default:
                throw SRC_BUG;
            }
        }

	if(lzo_read_buffer != NULL)
	{
	    flush_read();
	    clean_read();
	    delete [] lzo_read_buffer;
	    lzo_read_buffer = NULL;
	}

	if(lzo_write_buffer != NULL)
	{
	    flush_write();
	    clean_write();
	    delete [] lzo_write_buffer;
	    lzo_write_buffer = NULL;
	}

	if(lzo_compressed != NULL)
	{
	    delete [] lzo_compressed;
	    lzo_compressed = NULL;
	}

	if(lzo_wrkmem != NULL)
	{
	    delete [] lzo_wrkmem;
	    lzo_wrkmem = NULL;
	}
    }