Ejemplo n.º 1
0
char * Stream::readString(char * buf) {
  char * ret = buf;
  int c = timedRead();
  while (c >= 0) {
    *ret++ = (char)c;
    c = timedRead();
  }
  return ret;
}
Ejemplo n.º 2
0
std::string Stream::readStringUntil(char terminator) {
    std::string ret;
    int c = timedRead();
    while(c >= 0 && c != terminator) {
        ret += (char) c;
        c = timedRead();
    }
    return ret;
}
Ejemplo n.º 3
0
std::string Stream::readString() {
    std::string ret;
    int c = timedRead();
    while(c >= 0) {
        ret += (char) c;
        c = timedRead();
    }
    return ret;
}
Ejemplo n.º 4
0
String ICACHE_FLASH_ATTR Stream::readStringUntil(char terminator) {
    String ret;
    int c = timedRead();
    while(c >= 0 && c != terminator) {
        ret += (char) c;
        c = timedRead();
    }
    return ret;
}
Ejemplo n.º 5
0
String ICACHE_FLASH_ATTR Stream::readString() {
    String ret;
    int c = timedRead();
    while(c >= 0) {
        ret += (char) c;
        c = timedRead();
    }
    return ret;
}
Ejemplo n.º 6
0
/** \ingroup header
 * Read (and load) header from file handle.
 * @param fd		file handle
 * @param magicp	read (and verify) 8 bytes of (magic, 0)?
 * @return		header (or NULL on error)
 */
Header headerRead(FD_t fd, enum hMagic magicp)
{
    int32_t block[4];
    int32_t reserved;
    int32_t * ei = NULL;
    int32_t il;
    int32_t dl;
    int32_t magic;
    Header h = NULL;
    size_t len;
    int i;

    memset(block, 0, sizeof(block));
    i = 2;
    if (magicp == HEADER_MAGIC_YES)
	i += 2;

    /* FIX: cast? */
    if (timedRead(fd, (char *)block, i*sizeof(*block)) != (i * sizeof(*block)))
	goto exit;

    i = 0;

    if (magicp == HEADER_MAGIC_YES) {
	magic = block[i++];
	if (memcmp(&magic, rpm_header_magic, sizeof(magic)))
	    goto exit;
	reserved = block[i++];
    }
    
    il = ntohl(block[i]);	i++;
    dl = ntohl(block[i]);	i++;

    len = sizeof(il) + sizeof(dl) + (il * sizeof(struct entryInfo_s)) + dl;

    /* Sanity checks on header intro. */
    if (hdrchkTags(il) || hdrchkData(dl) || len > headerMaxbytes)
	goto exit;

    ei = xmalloc(len);
    ei[0] = htonl(il);
    ei[1] = htonl(dl);
    len -= sizeof(il) + sizeof(dl);

    /* FIX: cast? */
    if (timedRead(fd, (char *)&ei[2], len) != len)
	goto exit;
    
    h = headerLoad(ei);

exit:
    if (h == NULL && ei != NULL) {
	free(ei);
    }
    return h;
}
Ejemplo n.º 7
0
ssize_t eHttpStream::httpChunkedRead(void *buf, size_t count)
{
	ssize_t ret = -1;
	size_t total_read = partialPktSz;

	// write partial packet from the previous read
	if (partialPktSz > 0)
	{
		memcpy(buf, partialPkt, partialPktSz);
		partialPktSz = 0;
	}

	if (!isChunked)
	{
		ret = timedRead(streamSocket,((char*)buf) + total_read , count - total_read, 5000, 100);
		if (ret > 0)
		{
			ret += total_read;
			ret = syncNextRead(buf, ret);
		}
	}
	else
	{
		while (total_read < count)
		{
			if (0 == currentChunkSize)
			{
				do
				{
					ret = readLine(streamSocket, &tmpBuf, &tmpBufSize);
					if (ret < 0) return -1;
				} while (!*tmpBuf && ret > 0); /* skip CR LF from last chunk */
				if (ret == 0)
					break;
				currentChunkSize = strtol(tmpBuf, NULL, 16);
				if (currentChunkSize == 0) return -1;
			}

			size_t to_read = count - total_read;
			if (currentChunkSize < to_read)
				to_read = currentChunkSize;

			// do not wait too long if we have something in the buffer already
			ret = timedRead(streamSocket, ((char*)buf) + total_read, to_read, ((total_read)? 100 : 5000), 100);
			if (ret <= 0)
				break;
			currentChunkSize -= ret;
			total_read += ret;
		}
		if (total_read > 0)
		{
			ret = syncNextRead(buf, total_read);
		}
	}
	return ret;
}
Ejemplo n.º 8
0
char * Stream::readStringUntil(char * buf, char terminator) {
  char * ret = buf;
  int c = timedRead();
  while (c >= 0 && c != terminator)
  {
    *ret++ = (char)c;
    c = timedRead();
  }
  return ret;
}
Ejemplo n.º 9
0
// reads data from the stream until the target string of the given length is found
// search terminated if the terminator string is found
// returns true if target string is found, false if terminated or timed out
bool Stream::findUntil(char *target, size_t targetLen, char *terminator, size_t termLen)
{
  size_t index = 0;  // maximum target string length is 64k bytes!
  size_t termIndex = 0;
  int c;
  
  if( *target == 0)
  return true;   // return true if target is a null string
  while( (c = timedRead()) > 0){
    
    if(c != target[index])
    index = 0; // reset index if any char does not match
    
    if( c == target[index]){
      //////Serial.print("found "); Serial.write(c); Serial.print("index now"); Serial.println(index+1);
      if(++index >= targetLen){ // return true if all chars in the target match
        return true;
      }
    }
    
    if(termLen > 0 && c == terminator[termIndex]){
      if(++termIndex >= termLen)
      return false;       // return false if terminate string found before target string
    }
    else
    termIndex = 0;
  }
  return false;
}
Ejemplo n.º 10
0
ssize_t eHttpStream::read(off_t offset, void *buf, size_t count)
{
	if (connectionStatus == BUSY)
		return 0;
	else if (connectionStatus == FAILED)
		return -1;
	return timedRead(streamSocket, buf, count, 5000, 500);
}
Ejemplo n.º 11
0
ssize_t eHttpStream::read(off_t offset, void *buf, size_t count)
{
	sock_mutex.lock();
	sock_mutex.unlock();
	if (streamSocket < 0) {
		eDebug("eHttpStream::read not valid fd");
		return -1;
	}
	return timedRead(streamSocket, buf, count, 5000, 500);
}
Ejemplo n.º 12
0
// read characters from stream into buffer
// terminates if length characters have been read, or timeout (see setTimeout)
// returns the number of characters placed in the buffer
// the buffer is NOT null terminated.
//
size_t Stream::readBytes(char *buffer, size_t length)
{
  size_t count = 0;
  while (count < length) {
    int c = timedRead();
    if (c < 0) break;
    *buffer++ = (char)c;
    count++;
  }
  return count;
}
Ejemplo n.º 13
0
size_t Stream::readBytesUntil(char terminator, char *buffer, size_t length)
{
  if (length < 1) return 0;
  size_t index = 0;
  while (index < length) {
    int c = timedRead();
    if (c < 0 || c == terminator) break;
    *buffer++ = (char)c;
    index++;
  }
  return index; // return number of characters, not including null terminator
}
Ejemplo n.º 14
0
/**
* @brief Read characters into buffer.
* Terminates if length characters have been read, timeout, or
* if the terminator character has been detected
* @param The terminator character to look for
* @param The buffer to read into.
* @param The number of bytes to read.
* @return The number of bytes read. 0 means no valid data found.
*/
size_t RN2483::readBytesUntil(char terminator, char *buffer, size_t bytesToRead)
{
    if (bytesToRead < 1) return 0;
    size_t index = 0;
    while (index < (bytesToRead - 1 )) {
        int c = timedRead(1000);
        if (c < 0 || c == terminator) break;
        *buffer++ = (char)c;
        index++;
    }
    return index; // return number of characters, not including null terminator
}
Ejemplo n.º 15
0
bool EspDrv::getDataBuf(uint8_t sock, uint8_t *_data, uint16_t *_dataLen)
{
	for(int i=0; i<_bufPos; i++)
	{
		int c = timedRead();
		_data[i] = (char)c;
	}

	*_dataLen = _bufPos;
	_bufPos = 0;

	return true;
}
Ejemplo n.º 16
0
size_t ESP8266_Serial::readBytesUntilAndIncluding(char terminator, char *buffer, size_t length, byte maxOneLineOnly)
{
    if (length < 1) return 0;
    size_t index = 0;
    while (index < length) {
        int c = timedRead();
        if (c < 0) break;
        *buffer++ = (char)c;
        index++;
        if(c == terminator) break;
        if(maxOneLineOnly && ( c == '\n') ) break;
    }
    return index; // return number of characters, not including null terminator
}
Ejemplo n.º 17
0
String Stream::readStringUntil(char terminator, size_t max)
{
	String str;
	size_t length = str.length();
	while (length < max) {
		int c = timedRead();
		if (c < 0) {
			setReadError();
			break;	// timeout
		}
		if (c == 0 || c == terminator) break;
		str += (char)c;
	}
	return str;
}
Ejemplo n.º 18
0
/**
 * Receive the data into a buffer.
 * It reads up to bufSize bytes.
 * @return	received data size for success else -1.
 */
int EspDrv::getDataBuf(uint8_t connId, uint8_t *buf, uint16_t bufSize)
{
	if (connId!=_connId)
		return false;

	if(_bufPos<bufSize)
		bufSize = _bufPos;
	
	for(int i=0; i<bufSize; i++)
	{
		int c = timedRead();
		//LOGDEBUG(c);
		if(c==-1)
			return -1;
		
		buf[i] = (char)c;
		_bufPos--;
	}

	return bufSize;
}
Ejemplo n.º 19
0
String HttpClient::responseBody()
{
    int bodyLength = contentLength();
    String response;

    if (bodyLength > 0)
    {
        // try to reserve bodyLength bytes
        if (response.reserve(bodyLength) == 0) {
            // String reserve failed
            return String((const char*)NULL);
        }
    }

    // keep on timedRead'ing, until:
    //  - we have a content length: body length equals consumed or no bytes
    //                              available
    //  - no content length:        no bytes are available
    while (iBodyLengthConsumed != bodyLength)
    {
        int c = timedRead();

        if (c == -1) {
            // read timed out, done
            break;
        }

        if (!response.concat((char)c)) {
            // adding char failed
            return String((const char*)NULL);
        }
    }

    if (bodyLength > 0 && (unsigned int)bodyLength != response.length()) {
        // failure, we did not read in reponse content length bytes
        return String((const char*)NULL);
    }

    return response;
}
Ejemplo n.º 20
0
ssize_t readLine(int fd, char** buffer, size_t* bufsize)
{
	size_t i = 0;
	int result;
	while (1) 
	{
		if (i >= *bufsize) 
		{
			char *newbuf = (char*)realloc(*buffer, (*bufsize)+1024);
			if (newbuf == NULL)
				return -ENOMEM;
			*buffer = newbuf;
			*bufsize = (*bufsize) + 1024;
		}
		result = timedRead(fd, (*buffer) + i, 1, 3000, 100);
		if (result <= 0 || (*buffer)[i] == '\n')
		{
			(*buffer)[i] = '\0';
			return result <= 0 ? -1 : i;
		}
		if ((*buffer)[i] != '\r') i++;
	}
	return -1;
}
int main(int argc, char *argv[])
{
	char buffer[4096];
	int fd;
	long long size;
	if (argc < 3)
	{
		printf("usage: %s <volumename> <filename> [<size>]\n", argv[0]);
		exit(EXIT_FAILURE);
	}
	if (argc < 4)
	{
		struct stat st;
		if (stat(argv[2], &st) < 0)
		{
			std::cerr << "failed to open " << argv[2] << std::endl;
			exit(EXIT_FAILURE);
		}
		size = st.st_size;
	}
	else
	{
		size = atoi(argv[3]);
	}
	int volumeid = find_volumeid(argv[1]);
	if (volumeid >= 0)
	{
		std::cout << "found existing volume, resize to " << size << std::endl;
		int ret;
		struct ubi_rsvol_req req;
		fd = open("/dev/ubi0", O_RDONLY);
		if (fd < 0)
		{
			std::cerr << "failed to open " << "/dev/ubi0" <<  std::endl;
			exit(EXIT_FAILURE);
		}
		req.bytes = size;
		req.vol_id = volumeid;
		ret = ioctl(fd, UBI_IOCRSVOL, &req);
		close(fd);
	}
	else
	{
		std::cout << "create new volume, size " << size << std::endl;
		int ret;
		struct ubi_mkvol_req r;
		size_t n;

		memset(&r, 0, sizeof(struct ubi_mkvol_req));

		r.vol_id = UBI_VOL_NUM_AUTO;
		r.alignment = 1;
		r.bytes = size;
		r.vol_type = UBI_STATIC_VOLUME;

		n = strlen(argv[1]);
		if (n > UBI_MAX_VOLUME_NAME)
		{
			std::cerr << "volume name too long, max " << UBI_MAX_VOLUME_NAME << std::endl;
			exit(EXIT_FAILURE);
		}

		strncpy(r.name, argv[1], UBI_MAX_VOLUME_NAME + 1);
		r.name_len = n;

		fd = open("/dev/ubi0", O_RDONLY);
		if (fd < 0)
		{
			std::cerr << "failed to open " << "/dev/ubi0" <<  std::endl;
			exit(EXIT_FAILURE);
		}

		ret = ioctl(fd, UBI_IOCMKVOL, &r);
		if (ret < 0) 
		{
			close(fd);
			std::cerr << "failed to create volume" <<  std::endl;
			exit(EXIT_FAILURE);
		}

		close(fd);
		volumeid = r.vol_id;
		for (int i = 0; i < 10; i++)
		{
			snprintf(buffer, sizeof(buffer), "/dev/ubi0_%d", volumeid);
			if (access(buffer, F_OK) >= 0) break;
			/* HACK: give udev/mdev some time to create the devicenode */
			usleep(100000);
		}
	}
	snprintf(buffer, sizeof(buffer), "/dev/ubi0_%d", volumeid);
	fd = open(buffer, O_RDWR);
	if (fd < 0)
	{
		std::cerr << "failed to open " << buffer <<  std::endl;
		exit(EXIT_FAILURE);
	}
	ioctl(fd, UBI_IOCVOLUP, &size);
	int in;
	if (!strcmp(argv[2], "-"))
	{
		in = 0;
	}
	else
	{
		in = open(argv[2], O_RDONLY);
	}
	if (in < 0)
	{
		std::cerr << "failed to open " << argv[2] <<  std::endl;
		exit(EXIT_FAILURE);
	}
	while (1)
	{
		int result = timedRead(in, buffer, sizeof(buffer), 5000, 5000);
		if (result <= 0) break;
		if (writeAll(fd, buffer, result) < 0) break;
	}
	exit(EXIT_SUCCESS);
}
Ejemplo n.º 22
0
int Stream::findMulti( struct Stream::MultiTarget *targets, int tCount) {
  // any zero length target string automatically matches and would make
  // a mess of the rest of the algorithm.
  for (struct MultiTarget *t = targets; t < targets+tCount; ++t) {
    if (t->len <= 0)
      return t - targets;
  }

  while (1) {
    int c = timedRead();
    if (c < 0)
      return -1;

    for (struct MultiTarget *t = targets; t < targets+tCount; ++t) {
      // the simple case is if we match, deal with that first.
      if (c == t->str[t->index]) {
        if (++t->index == t->len)
          return t - targets;
        else
          continue;
      }

      // if not we need to walk back and see if we could have matched further
      // down the stream (ie '1112' doesn't match the first position in '11112'
      // but it will match the second position so we can't just reset the current
      // index to 0 when we find a mismatch.
      if (t->index == 0)
        continue;

      int origIndex = t->index;
      do {
        --t->index;
        // first check if current char works against the new current index
        if (c != t->str[t->index])
          continue;

        // if it's the only char then we're good, nothing more to check
        if (t->index == 0) {
          t->index++;
          break;
        }

        // otherwise we need to check the rest of the found string
        int diff = origIndex - t->index;
        size_t i;
        for (i = 0; i < t->index; ++i) {
          if (t->str[i] != t->str[i + diff])
            break;
        }

        // if we successfully got through the previous loop then our current
        // index is good.
        if (i == t->index) {
          t->index++;
          break;
        }

        // otherwise we just try the next index
      } while (t->index);
    }
  }
  // unreachable
  return -1;
}
Ejemplo n.º 23
0
static rpmRC rpmpkgReadHeader(rpmKeyring keyring, rpmVSFlags vsflags, 
		       FD_t fd, Header *hdrp, char ** msg)
{
    char *buf = NULL;
    int32_t block[4];
    int32_t il;
    int32_t dl;
    int32_t * ei = NULL;
    size_t uc;
    size_t nb;
    Header h = NULL;
    rpmRC rc = RPMRC_FAIL;		/* assume failure */
    int xx;

    if (hdrp)
	*hdrp = NULL;
    if (msg)
	*msg = NULL;

    memset(block, 0, sizeof(block));
    if ((xx = timedRead(fd, (char *)block, sizeof(block))) != sizeof(block)) {
	rasprintf(&buf, 
		_("hdr size(%d): BAD, read returned %d\n"), (int)sizeof(block), xx);
	goto exit;
    }
    if (memcmp(block, rpm_header_magic, sizeof(rpm_header_magic))) {
	rasprintf(&buf, _("hdr magic: BAD\n"));
	goto exit;
    }
    il = ntohl(block[2]);
    if (hdrchkTags(il)) {
	rasprintf(&buf, _("hdr tags: BAD, no. of tags(%d) out of range\n"), il);
	goto exit;
    }
    dl = ntohl(block[3]);
    if (hdrchkData(dl)) {
	rasprintf(&buf,
		  _("hdr data: BAD, no. of bytes(%d) out of range\n"), dl);
	goto exit;
    }

    nb = (il * sizeof(struct entryInfo_s)) + dl;
    uc = sizeof(il) + sizeof(dl) + nb;
    ei = xmalloc(uc);
    ei[0] = block[2];
    ei[1] = block[3];
    if ((xx = timedRead(fd, (char *)&ei[2], nb)) != nb) {
	rasprintf(&buf, _("hdr blob(%zd): BAD, read returned %d\n"), nb, xx);
	goto exit;
    }

    /* Sanity check header tags */
    rc = headerVerify(keyring, vsflags, ei, uc, msg);
    if (rc != RPMRC_OK)
	goto exit;

    /* OK, blob looks sane, load the header. */
    h = headerLoad(ei);
    if (h == NULL) {
	rasprintf(&buf, _("hdr load: BAD\n"));
	rc = RPMRC_FAIL;
        goto exit;
    }
    ei = NULL;	/* XXX will be freed with header */
    
exit:
    if (hdrp && h && rc == RPMRC_OK)
	*hdrp = headerLink(h);
    ei = _free(ei);
    h = headerFree(h);

    if (msg != NULL && *msg == NULL && buf != NULL) {
	*msg = buf;
    } else {
	free(buf);
    }

    return rc;
}
Ejemplo n.º 24
0
ssize_t eHttpStream::read(off_t offset, void *buf, size_t count)
{
	return timedRead(streamSocket, buf, count, 5000, 500);
}