bool LLWebRequest::request(const char* httpMethod, const char* url, const char* contentData, char* readBuffer, size_t readBufferLength)
 {
     bool result = FALSE;
     if (readBuffer) readBuffer[0] = 0; // terminate result buffer in case of failure
     
     if (sendRequest(httpMethod, url, contentData))
     {
         if (readBuffer)
         {
             // read HTTP headers:
             if (readHeaders())
             {
                 // read content data as c-string, keep 1 byte for termination:
                 int bytesRead = readAll(readBuffer, readBufferLength - 1);
                 
                 readBuffer[bytesRead] = 0; // Terminate read buffer with \0
                 result = TRUE;
             }
         }
         else
         {
             // okay, not expecting any result
             readHeaders(); // at least read headers
             result = TRUE;
         }
         stop();
     }
     return result;
 }
Esempio n. 2
0
void CNetRequestImpl::readResponse(CNetResponseImpl* pNetResp)
{
    DWORD dwLen = 10;
    wchar_t szHttpRes[10];
    DWORD nIndex = 0;

    if( !HttpQueryInfo( hRequest, HTTP_QUERY_STATUS_CODE, szHttpRes, &dwLen, &nIndex) )
    {
        pszErrFunction = L"HttpQueryInfo";
        return;
    }
    int nCode = _wtoi(szHttpRes);
    pNetResp->setResponseCode(nCode);

    if ( m_pHeaders )
    {
        if ( !readHeaders(*m_pHeaders) )
            return;
    }

    if ( nCode != 200 )
    {
        LOG(ERROR) + "An error occured connecting to the sync source: " + szHttpRes + " returned.";

        // If we're unauthorized, delete any cookies that might have been
        // stored so we don't reuse them later
        if ( nCode == 401 && m_pSession ) 
        {
            m_pSession->logout();
        }
	}

    if (pNetResp->isOK())
        pNetResp->setCookies(makeClientCookie());
}
Esempio n. 3
0
bool RegionFile::read() {
	std::ifstream file(filename.c_str(), std::ios_base::binary);
	int chunk_offsets[1024];
	if (!readHeaders(file, chunk_offsets))
		return false;
	file.seekg(0, std::ios::end);
	int filesize = file.tellg();
	file.seekg(0, std::ios::beg);

	std::vector<uint8_t> regiondata(filesize);
	file.read(reinterpret_cast<char*>(&regiondata[0]), filesize);

	for (int i = 0; i < 1024; i++) {
		// get the offsets, where the chunk data starts
		int offset = chunk_offsets[i];
		if (offset == 0)
			continue;

		// get data size and compression type
		int size = *(reinterpret_cast<int*>(&regiondata[offset]));
		size = util::bigEndian32(size) - 1;
		uint8_t compression = regiondata[offset + 4];

		chunk_data_compression[i] = compression;
		chunk_data[i].resize(size);
		std::copy(&regiondata[offset+5], &regiondata[offset+5+size], chunk_data[i].begin());
	}

	return true;
}
Esempio n. 4
0
void StreamingFile::play() {
  readHeaders();
  readyNextBuffer();
  readyNextBuffer();
  alSourceQueueBuffers(m_alsource, 2, m_albuffers);
  alSourcePlay(m_alsource);
}
Esempio n. 5
0
DsfFileReader::DsfFileReader(char* filePath) : DsdSampleReader()
{
	this->filePath = filePath;
	// first let's open the file
	file.open(filePath, fstreamPlus::in | fstreamPlus::binary);
	// throw exception if that did not work.
	if (!file.is_open()) {
		errorMsg = "could not open file";
		valid = false;
		return;
	}
	// read the header data
	if (!(valid = readHeaders()))
		return;

	// this code only works with single bit data (could be upgraded later on)
	if (samplesPerChar!=8) {
		errorMsg = "Sorry, only one bit data is supported";
		valid = false;
		return;
	}
	// read the metadata
	readMetadata();
	
	rewind(); // calls clearBuffer -> allocateBuffer
}
Esempio n. 6
0
void Connection::readyRead() {
    TRACE;
    was_NetData = true;

    if( is_WaitingHeaders ) {
	is_WaitingHeaders = ! readHeaders(p_ClientConnection, m_Headers);

	if( !is_WaitingHeaders ) {
	    // parse headers
	    m_RequestHeaders = getHeaders(m_Headers);

	    // prepare for reading body
	    m_Buffer.close();
	    m_Buffer.open(QBuffer::ReadWrite);
	    QString len = m_RequestHeaders["content-length"];
	    bool ok;
	    m_BodyLength = len.toInt(&ok);
	    m_BodyLength = readBody(p_ClientConnection, m_Buffer, m_BodyLength);
	}
    } else {
	m_BodyLength = readBody(p_ClientConnection, m_Buffer, m_BodyLength);
    }

    if( is_WaitingHeaders==false && m_BodyLength==0 )
	if( !onEmptyLine() )
	    p_ClientConnection->close();
}
Esempio n. 7
0
//==============================================================================
bool PEFile::loadFromFile(char* filePath) {
    unloadFile();

    return readFileData(filePath) &&
           readHeaders() &&
           readBody() &&
           readImportTable();
}
int ColumnOpCompress1::blocksInFile(IDBDataFile* pFile) const
{
   CompFileHeader compFileHeader;
   readHeaders(pFile, compFileHeader.fControlData, compFileHeader.fPtrSection);

   compress::IDBCompressInterface compressor;
   return compressor.getBlockCount(compFileHeader.fControlData);
}
Esempio n. 9
0
//==============================================================================
bool PEFile::loadFromMemory(char* memoryAddress) {
    unloadFile();

    peMemory = memoryAddress;

    return readHeaders()/* &&
		   readBody() &&
		   readImportTable()*/;
}
Esempio n. 10
0
void HTTPConnection::readHeaders() {
    while (_socket->canReadLine()) {
        QByteArray line = _socket->readLine();
        QByteArray trimmed = line.trimmed();
        if (trimmed.isEmpty()) {
            _socket->disconnect(this, SLOT(readHeaders()));

            QByteArray clength = requestHeader("Content-Length");
            if (clength.isEmpty()) {
                _parentManager->handleHTTPRequest(this, _requestUrl);

            } else {
                bool success = false;
                auto length = clength.toInt(&success);
                if (!success) {
                    qWarning() << "Invalid header." << _address << trimmed;
                    respond("400 Bad Request", "The header was malformed.");
                    return;
                }

                // Storing big requests in memory gets expensive, especially on servers
                // with limited memory. So we store big requests in a temporary file on disk
                // and map it to faster read/write access.
                static const int MAX_CONTENT_SIZE_IN_MEMORY = 10 * 1000 * 1000;
                if (length < MAX_CONTENT_SIZE_IN_MEMORY) {
                    _requestContent = MemoryStorage::make(length);
                } else {
                    _requestContent = FileStorage::make(length);
                }

                connect(_socket, SIGNAL(readyRead()), SLOT(readContent()));

                // read any content immediately available
                readContent();
            }
            return;
        }
        char first = line.at(0);
        if (first == ' ' || first == '\t') { // continuation
            _requestHeaders[_lastRequestHeader].append(trimmed);
            continue;
        }
        int idx = trimmed.indexOf(':');
        if (idx == -1) {
            qWarning() << "Invalid header." << _address << trimmed;
            respond("400 Bad Request", "The header was malformed.");
            return;
        }
        _lastRequestHeader = trimmed.left(idx).toLower();
        QByteArray& value = _requestHeaders[_lastRequestHeader];
        if (!value.isEmpty()) {
            value.append(", ");
        }
        value.append(trimmed.mid(idx + 1).trimmed());
    }
}
Esempio n. 11
0
bool
HttpClient::perform()
{
    writeRequest();
    if (!_conn->fresh() && (_conn->stream().obtain().size == 0)) {
        _conn.reset(new HttpConnection(_conn->server()));
        writeRequest();
    }
    return (readStatus() && readHeaders() && readContent());
}
Esempio n. 12
0
rWAVAudio::rWAVAudio(const char *sName, EDtError &rc)
   : FileReader(sName, rc)
{
   if (rc != DT_OK)
      return;

   readHeaders();
   setBlockSize(2352);
   setLittleEndian(true);
   setType(Data_Audio);
}
Esempio n. 13
0
void HTTPConnection::readRequest() {
    if (!_socket->canReadLine()) {
        return;
    }
    if (!_requestUrl.isEmpty()) {
        qDebug() << "Request URL was already set";
        return;
    }
    // parse out the method and resource
    QByteArray line = _socket->readLine().trimmed();
    if (line.startsWith("HEAD")) {
        _requestOperation = QNetworkAccessManager::HeadOperation;

    } else if (line.startsWith("GET")) {
        _requestOperation = QNetworkAccessManager::GetOperation;

    } else if (line.startsWith("PUT")) {
        _requestOperation = QNetworkAccessManager::PutOperation;

    } else if (line.startsWith("POST")) {
        _requestOperation = QNetworkAccessManager::PostOperation;

    } else if (line.startsWith("DELETE")) {
        _requestOperation = QNetworkAccessManager::DeleteOperation;

    } else {
        qWarning() << "Unrecognized HTTP operation." << _address << line;
        respond("400 Bad Request", "Unrecognized operation.");
        return;
    }
    int idx = line.indexOf(' ') + 1;
    _requestUrl.setUrl(line.mid(idx, line.lastIndexOf(' ') - idx));

    // switch to reading the header
    _socket->disconnect(this, SLOT(readRequest()));
    connect(_socket, SIGNAL(readyRead()), SLOT(readHeaders()));

    // read any headers immediately available
    readHeaders();
}
Esempio n. 14
0
/**
 * Reads the whole region file.
 */
bool RegionFile::loadAll() {
	std::ifstream file(filename.c_str(), std::ios_base::binary);
	if (!readHeaders(file))
		return false;
	file.seekg(0, std::ios::end);
	int filesize = file.tellg();
	file.seekg(0, std::ios::beg);

	regiondata.resize(filesize);
	file.read((char*) &regiondata[0], filesize);

	return true;
}
Esempio n. 15
0
bool Loader::loadExecutableAndInitProcess()
{
  debug ( LOADER,"Loader::loadExecutableAndInitProcess: going to load an executable\n" );

  if(!readHeaders())
    return false;

  debug ( LOADER,"loadExecutableAndInitProcess: Entry: %zx, num Sections %zx\n",hdr_->e_entry, (size_t)hdr_->e_phnum );
  if (LOADER & OUTPUT_ADVANCED)
    Elf::printElfHeader ( *hdr_ );

  if (USERTRACE & OUTPUT_ENABLED)
    loadDebugInfoIfAvailable();

  return true;
}
Esempio n. 16
0
QList<QByteArray> ElfReader::dependencies()
{
    QList<QByteArray> result;

    ElfMapper mapper(this);
    if (!mapper.map()) {
        m_errorString = QStringLiteral("Mapper failure");
        return result;
    }
    quint64 dynStrOffset = 0;
    quint64 dynamicOffset = 0;
    quint64 dynamicSize = 0;

    foreach (const ElfSectionHeader &eh, readHeaders().sectionHeaders) {
        if (eh.name  == QByteArrayLiteral(".dynstr")) {
            dynStrOffset = eh.offset;
        } else if (eh.name  == QByteArrayLiteral(".dynamic")) {
            dynamicOffset = eh.offset;
            dynamicSize = eh.size;
        }
        if (dynStrOffset && dynamicOffset)
            break;
    }

    if (!dynStrOffset || !dynamicOffset) {
        m_errorString = QStringLiteral("Not a dynamically linked executable.");
        return result;
    }

    const unsigned char *dynamicData = mapper.ustart + dynamicOffset;
    const unsigned char *dynamicDataEnd = dynamicData + dynamicSize;
    while (dynamicData < dynamicDataEnd) {
        const quint32 tag = getWord(dynamicData, m_elfData);
        if (tag == DT_NULL)
            break;
        if (m_elfData.elfclass == Elf_ELFCLASS64)
            dynamicData += sizeof(quint32); // padding to d_val/d_ptr.
        if (tag == DT_NEEDED) {
            const quint32 offset = getWord(dynamicData, m_elfData);
            if (m_elfData.elfclass == Elf_ELFCLASS64)
                dynamicData += sizeof(quint32); // past d_ptr.
            const char *name = mapper.start + dynStrOffset + offset;
            result.push_back(name);
        }
    }
    return result;
}
Esempio n. 17
0
void HTTPConnection::readHeaders() {
    while (_socket->canReadLine()) {
        QByteArray line = _socket->readLine();
        QByteArray trimmed = line.trimmed();
        if (trimmed.isEmpty()) {
            _socket->disconnect(this, SLOT(readHeaders()));

            QByteArray clength = _requestHeaders.value("Content-Length");
            if (clength.isEmpty()) {
                _parentManager->handleHTTPRequest(this, _requestUrl);

            } else {
                _requestContent.resize(clength.toInt());
                connect(_socket, SIGNAL(readyRead()), SLOT(readContent()));

                // read any content immediately available
                readContent();
            }
            return;
        }
        char first = line.at(0);
        if (first == ' ' || first == '\t') { // continuation
            _requestHeaders[_lastRequestHeader].append(trimmed);
            continue;
        }
        int idx = trimmed.indexOf(':');
        if (idx == -1) {
            qWarning() << "Invalid header." << _address << trimmed;
            respond("400 Bad Request", "The header was malformed.");
            return;
        }
        _lastRequestHeader = trimmed.left(idx);
        QByteArray& value = _requestHeaders[_lastRequestHeader];
        if (!value.isEmpty()) {
            value.append(", ");
        }
        value.append(trimmed.mid(idx + 1).trimmed());
    }
}
Esempio n. 18
0
DsdiffFileReader::DsdiffFileReader(char* filePath) : DsdSampleReader()
{
	// set some defaults
	ast.hours = 0;
	ast.minutes = 0;
	ast.seconds = 0;
	ast.samples = 0;
	samplesPerChar = 8; // always 8 samples per char (dsd wide not supported by dsdiff).
	sampleBufferLenPerChan = 1024;
	bufferMarker = 0;
	errorMsg = "";
	lsConfig=65535;
	isEm=false;
	// first let's open the file
	file.open(filePath, fstreamPlus::in | fstreamPlus::binary);
	// throw exception if that did not work.
	if (!file.is_open()) {
		errorMsg = "could not open file";
		valid = false;
		return;
	}
	// try and read the header data
	if (!(valid = readHeaders()))
		return;
		
	// find the start and end of the tracks.
	processTracks();
		
	// if DST data, then initialise the decoder
	if (checkIdent(compressionType,const_cast<dsf2flac_int8*>("DST "))) {
		DST_InitDecoder(&dstEbunch, getNumChannels(), getSamplingFreq()/44100);
		dstEbunchAllocated = true;
	}
	
	rewind(); // calls allocateBlockBuffer
	
	return;
}
Esempio n. 19
0
int main(int argc, char **argv)
{
    // Initialize variables
    struct hostent* pHostInfo;   // Holds info about a machine
    struct sockaddr_in Address;  // Internet socket address stuct
    long nHostAddress;
    char *body;
    ssize_t bytesRead;
    char strHostName[HOST_NAME_SIZE];
    double usec;
    int contentLength;
    int nHostPort;
    int hSocket, epollfd;
    int count = 1;
    int debug = FALSE, verbose = FALSE, veryVerbose = FALSE;
    int i, c, rval, index;

    extern char *optarg;


    // Command Line Arguments //
    // Parse and validate the command line arguments
    if (argc < 5) {
        printf("usage: %s [-d] [-v|w] <host-name> <port> <path> <count>\n", argv[0]);
        return 1;
    }
    while ((c = getopt(argc, argv, "dvw")) != -1) {
        switch (c) {
            case 'd':
                // Print the times for each response
                debug = TRUE;
                break;
            case 'v':
                // Enable verbose output
                verbose = TRUE;
                break;
            case 'w':
                verbose = TRUE;
                veryVerbose = TRUE;
                break;
            default:
                // ?
                fprintf(stderr, "Option -%c not recognized. Ignoring.\n", c);
                break;
        }
    }

    // Get the host name
    if (strlen(argv[optind]) >= HOST_NAME_SIZE) {
        fprintf(stderr, "Error: Host name too large. Must be less than %d characters long.\n", HOST_NAME_SIZE);
        return 1;
    }
    strcpy(strHostName, argv[optind]);

    // Get the port
    nHostPort = atoi(argv[optind + 1]);
    if (nHostPort <= 0) {
        fprintf(stderr, "Error: Invalid port number provided.\n");
        return 1;
    }

    // Get the count
    count = atoi(argv[optind + 3]);
    if (count <= 0) {
        fprintf(stderr, "Error: Invalid count provided. Count must be a positive number.\n");
    }

    // Build variables the depend on the count variable
    int hSockets[count];   // Handles to the sockets
    double timings[count];
    struct timeval oldtime[count];


    // Setup the Address //
    // Get the IP address from name (DNS Lookup)
    pHostInfo = gethostbyname(strHostName);
    if (pHostInfo == NULL) {
        perror("DNS Lookup Failure");
        fprintf(stderr, "Error: No such hostname: %s\n", strHostName);
        return 1;
    }
    if (verbose) {
        printf("--Host: %s -> %s\n", pHostInfo->h_name, inet_ntoa(*(struct in_addr *) pHostInfo->h_addr_list[0]));
    }
    // Copy address into long
    memcpy(&nHostAddress, pHostInfo->h_addr, pHostInfo->h_length);

    // Fill address struct
    memset(&Address, 0, sizeof(Address));// Make sure it is empty first
    Address.sin_addr.s_addr = nHostAddress;
    Address.sin_port = htons(nHostPort);
    Address.sin_family = AF_INET;


    // Build the HTTP message
    char *message = malloc(MAX_GET);
    sprintf(
        message,
        "GET %s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\nAccept: text/html, text/plain, */*\r\nUser-Agent: CS360WebTest/1.0\r\nReferer: http://%s/\r\n\r\n",
        argv[optind + 2],
        strHostName,
        strHostName
    );
    if (veryVerbose) {
        printf("Request:\n%s\n", message);
    }

    // Create an epoll interface
    epollfd = epoll_create(count);

    if (verbose) {
        printf("--Creating sockets.\n");
    }
    for (i = 0; i < count; i++) {
        // Connect to the server //
        // Make a socket
        hSockets[i] = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
        if (hSockets[i] == SOCKET_ERROR) {
            perror("Failure to create socket");
            fprintf(stderr, "Error: Could not create connection (%d)\n", errno);
            return 2;
        }
    }

    if (verbose) {
        printf("--Connecting to http://%s:%d %d times.\n", strHostName, nHostPort, count);
    }
    for (i = 0; i < count; i++) {
        // Connect to host
        if (connect(hSockets[i], (struct sockaddr*) &Address, sizeof(Address)) == SOCKET_ERROR) {
            perror("Connection error");
            fprintf(stderr, "Error: Could not connect to host (%d)\n", errno);
            return 2;
        }

        // HTTP //
        // Send HTTP to the socket
        write(hSockets[i], message, strlen(message));


        // Store the event
        struct epoll_event event;
        event.data.fd = hSockets[i];
        event.events = EPOLLIN;
        int ret = epoll_ctl(epollfd, EPOLL_CTL_ADD, hSockets[i], &event);
        if (ret < 0) {
            perror("Epoll Control Add");
        }

        // Store the request start time
        gettimeofday(&oldtime[i], NULL);

        if (verbose) {
            printf("--(%02d) Created socket #%02d\n", i, hSockets[i]);
        }
    }
    free(message);

    if (verbose) {
        printf("--Handling sockets.\n");
    }
    for (i = 0; i < count; i++) {
        // Gets the next connection that is ready.
        struct epoll_event event;
        rval = epoll_wait(epollfd, &event, 1, -1);
        if (rval < 0) {
            perror("Epoll Wait");
        }
        index = event.data.fd - FD_OFFSET;
        hSocket = event.data.fd;
        if (verbose) {
            printf("--(%02d) Handling socket %02d (Index: %d)\n", i, hSocket, index);
        }

        // Read the response back from the socket
        // Read the HTTP headers
        contentLength = readHeaders(hSocket, veryVerbose);
        if (verbose) {
            printf("--Content Length: %d\n", contentLength);
        }

        if (contentLength >= 0) {
            // Read the body
            body = malloc((contentLength + 1) * sizeof(char));
            bytesRead = read(hSocket, body, contentLength);
            if (bytesRead == SOCKET_ERROR) {
                perror("Failure reading from socket");
                fprintf(stderr, "Error: Problem reading response body (%d)\n", errno);
                return 2;
            }

            // Print the body
            if (count == 1) {
                body[bytesRead] = 0;// Make sure the body is null terminated
                printf("%s\n", body);
            }
        } else {
            if (verbose) {
                printf("--Reading to the end of the socket.\n");
            }

            // Read as much as we can from the socket
            body = malloc(BUFFER_SIZE * sizeof(char));
            while ((bytesRead = read(hSocket, body, BUFFER_SIZE)) != 0) {
                if (bytesRead == SOCKET_ERROR) {
                    perror("Failure reading from socket");
                    fprintf(stderr, "Error: Problem reading response body (%d)\n", errno);
                    return 2;
                }
                // Set the last character to null
                body[bytesRead] = 0;

                // Print the body
                if (count == 1) {
                    printf("%s", body);
                }

                // Check if we've reached the end of the socket stream
                if (bytesRead < BUFFER_SIZE) {
                    break;
                }
            }
            printf("\n");
        }

        // Report on the timings
        struct timeval newtime;
        // Get the current time and subtract the starting time for this request.
        gettimeofday(&newtime, NULL);
        usec = (newtime.tv_sec - oldtime[index].tv_sec) * USEC_PER_SEC
                      + (newtime.tv_usec - oldtime[index].tv_usec);
        timings[i] = usec;
        if (debug) {
            printf("Time: %f seconds\n", usec / USEC_PER_SEC);
        }

        // Remove this socket from being epolled
        epoll_ctl(epollfd, EPOLL_CTL_DEL, hSocket, &event);
    }

    if (verbose) {
        printf("--Closing socket connections.\n");
    }
    for (i = 0; i < count; i++) {
        // End Connection //
        if (close(hSockets[i]) == SOCKET_ERROR) {
            perror("Failed to close socket");
            fprintf(stderr, "Error: Could not close connection (%d)\n", errno);
            return 2;
        }
    }

    // Generate the report //
    if (verbose) {
        printf("--Generating report.\n");
    }
    double mean = 0.0, variance = 0.0, standardDeviation = 0.0;

    // Find the average time
    for (i = 0; i < count; i++) {
        mean += timings[i];
    }
    mean = mean / (double) count;

    // Find the standard deviation
    for (i = 0; i < count; i++) {
        variance += pow(timings[i] - mean, 2);
    }
    variance = variance / (double) count;
    standardDeviation = sqrt(variance);

    // Find the standa
    printf("Average response time: %f seconds.\n", mean / USEC_PER_SEC);
    printf("Standard Deviation: %f seconds.\n", standardDeviation / USEC_PER_SEC);


    return 0;
}
Esempio n. 20
0
static
void runCodeDespairVarF(uint code_len, FILE *input, FILE *output) {
  uint lim_code = pow(2, code_len);
  uint i, j;
  HEADER *h;
  uchar *echar_table;
  uchar *mchar_table;
  uint *num_rules;
  uint t_num_rules;
  RULEVar **rule;
  ENC **enc;
  BITIN *bitin;

  h = readHeaders(input);

  echar_table = (uchar*)malloc(sizeof(uchar)*h->char_size);
  fread(echar_table, sizeof(uchar), h->char_size, input);
  mchar_table = (uchar*)malloc(sizeof(uchar)*h->char_size);
  fread(mchar_table, sizeof(uchar), h->char_size, input);
  num_rules = (uint*)malloc(sizeof(uint)*h->num_contexts);
  fread(num_rules, sizeof(uint), h->num_contexts, input);
  t_num_rules = 0;
  for (i = 0; i < h->num_contexts; i++) {
    t_num_rules += num_rules[i];
  }

  rule    = (RULEVar **)malloc(sizeof(RULEVar *)*h->num_contexts);
  rule[0] = (RULEVar *)malloc(sizeof(RULEVar)*t_num_rules);
  enc     = (ENC **)malloc(sizeof(ENC *)*h->num_contexts);
  enc[0]  = (ENC *)malloc(sizeof(ENC)*t_num_rules);
  for (i = 0, j = 0; i < h->num_contexts; i++) {
    rule[i] = rule[0] + j;
    enc[i]  = enc[0]  + j;
    j += num_rules[i];
  }

  bitin = createBitin(input);
  for (i = 0; i < h->num_contexts; i++) {
    for (j = 0; j < h->char_size; j++) {
      rule[i][j].left  = j; rule[i][j].right = 0;
    }
    for (j = h->char_size; j < num_rules[i]; j++) {
      rule[i][j].left  = (CODE)readBits(bitin, code_len);
      rule[i][j].right = (CODE)readBits(bitin, code_len);
    }
  }

  for (i = 0; i < h->num_contexts; i++) {
    for (j = 0; j < h->char_size; j++) {
      enc[i][j].tid = 
	tailContextId(i, h->mchar_size, h->cont_len, 
		      mchar_table[(uchar)j]);
      enc[i][j].len = 1;
      enc[i][j].string = (uchar*)malloc(sizeof(uchar)*2);
      enc[i][j].string[0] = echar_table[(uchar)j];
      enc[i][j].string[1] = '\0';
    }
  }
  for (j = h->char_size; j < lim_code; j++) {
    for (i = 0; i < h->num_contexts; i++) {
      CODE  l, r;
      PCODE p;
      if (j >= num_rules[i]) continue;
      l = rule[i][j].left; r = rule[i][j].right;
      p = enc[i][l].tid;
      enc[i][j].len = enc[i][l].len + enc[p][r].len;
      enc[i][j].string = (uchar*)malloc(sizeof(uchar)*(enc[i][j].len+1));
      strcpy((char*)enc[i][j].string, (char*)enc[i][l].string);
      strcat((char*)enc[i][j].string, (char*)enc[p][r].string);
      enc[i][j].tid = enc[p][r].tid;
    }
  }

  free(echar_table); free(mchar_table);
  free(rule[0]); free(rule);

  printf("Expanding Compressed Text ...");
  fflush(stdout);
  {
    uint  i;
    CODE  c;
    uchar wb[BUFFER_SIZE];
    uint  wb_pos;
    uchar *wb_ptr, *str;
    PCODE p = HEAD_PCODE;

    wb_ptr = wb; wb_pos = 0;
    for (i = 0; i < h->seq_len; i++) {
      c = (CODE)readBits(bitin, code_len);
      str = enc[p][c].string;
      while (*str != '\0') {
	*wb_ptr++ = *str++;
	if (++wb_pos == BUFFER_SIZE) {
	  fwrite(wb, 1, BUFFER_SIZE, output);
	  wb_ptr = wb; wb_pos = 0;
	}
      }
      p = enc[p][c].tid;
    }
    fwrite(wb, 1, wb_pos, output);
  }
  printf("Done!!\n");
  fflush(stdout);
  
  for (i = 0; i < h->num_contexts; i++) {
    for (j = 0; j < num_rules[i]; j++) {
      free(enc[i][j].string);
    }
  }

  free(h);
  free(enc[0]); free(enc);
  free(num_rules);
  destructBitin(bitin);
}
Esempio n. 21
0
bool RegionFile::loadHeaders() {
	std::ifstream file(filename.c_str(), std::ios_base::binary);
	return readHeaders(file);
}
Esempio n. 22
0
void ppAnalyse(char *headersName, char *sizesName, char *pairsPsl, 
    char *finDir, char *pairOut, char *mispairOut)
/* ppAnalyse - Analyse paired plasmid reads. */
{
struct hash *readHash = newHash(21);
struct hash *pairHash = newHash(20);
struct hash *fragHash = newHash(17);
struct hash *finHash = NULL;
struct hash *gsiHash = newHash(8);
struct pairInfo *pairList = NULL, *measuredList, *pair;
struct readInfo *readList = NULL, *rd;
struct groupSizeInfo *gsiList = NULL, *gsi;
int aliCount;
int finCount;
int i;
struct slName *finList, *name;

finHash = newHash(14);
finList = listDir(finDir, "*.fa");
if ((finCount = slCount(finList)) == 0)
    errAbort("No fa files in %s\n", finDir);
printf("Got %d (finished) .fa files in %s\n", finCount, finDir);
for (name = finList; name != NULL; name = name->next)
    {
    chopSuffix(name->name);
    hashStore(finHash, name->name);
    }
#ifdef SOMETIMES
#endif /* SOMETIMES */

gsiList = readSizes(sizesName, gsiHash);
printf("Got %d groups\n", slCount(gsiList));

readHeaders(headersName, readHash, pairHash, &readList, &pairList);
slReverse(&readList);
slReverse(&pairList);
printf("Got %d reads in %d pairs\n", slCount(readList), slCount(pairList));


savePairs(pairOut, pairList, gsiHash);
printf("Saved pairs to %s\n", pairOut);

aliCount = readAlignments(pairsPsl, readHash, fragHash);
printf("Got %d alignments in %s\n", aliCount, pairsPsl);


doReadStats(readList, aliCount);
doPairStats(pairList, finHash, gsiHash, mispairOut, &measuredList, &pairList);
gsiMeanAndVariance(measuredList, gsiList, gsiHash, finHash);
printf("Alignment length stats:\n");
for (i=0; i<10; ++i)
    {
    printf("%3d - %4d :  %6d  %4.2f%%\n",
    	i*100, (i+1)*100, aliSizes[i], 100.0 * (double)aliSizes[i]/(double)aliCount);
    }
doMeasuredStats(measuredList);

for (gsi = gsiList; gsi != NULL; gsi = gsi->next)
    {
    if (gsi->measuredCount > 0)
	{
	printf("%s: mean %f, std %f, min %d, max %d, samples %d\n",
	    gsi->name, gsi->measuredMean, sqrt(gsi->variance),
	    gsi->measuredMin, gsi->measuredMax,
	    gsi->measuredCount);
	printf("   %4.2f%% within guessed range (%d-%d)\n", 
		100.0 * (double)gsi->guessedCount/(double)gsi->measuredCount,
		gsi->guessedMin, gsi->guessedMax);
	printf("   w/in 200 %4.2f%%, w/in 400 %4.2f%%,  w/in 800 %4.2f%%,  w/in 1600 %4.3f%%, w/in 3200 %4.2f%%\n\n",
	    gsiTight(gsi, 200), gsiTight(gsi, 400), gsiTight(gsi, 800), gsiTight(gsi, 1600), gsiTight(gsi, 3200) );
	showHistogram(gsi);
	}
    }
}
Esempio n. 23
0
static
void runCodeDespair8F(FILE *input, FILE *output) {
  uint i, j;
  HEADER *h;
  uchar *echar_table;
  uchar *mchar_table;
  uint *num_rules;
  uint t_num_rules;
  RULE8 **rule;
  ENC **enc;

  h = readHeaders(input);
  echar_table = (uchar*)malloc(sizeof(uchar)*h->char_size);
  fread(echar_table, sizeof(uchar), h->char_size, input);
  mchar_table = (uchar*)malloc(sizeof(uchar)*h->char_size);
  fread(mchar_table, sizeof(uchar), h->char_size, input);

  num_rules = (uint*)malloc(sizeof(uint)*h->num_contexts);
  fread(num_rules, sizeof(uint), h->num_contexts, input);
  t_num_rules = 0;
  for (i = 0; i < h->num_contexts; i++) {
    t_num_rules += num_rules[i];
  }
  rule    = (RULE8 **)malloc(sizeof(RULE8 *)*h->num_contexts);
  rule[0] = (RULE8 *)malloc(sizeof(RULE8)*t_num_rules);
  enc     = (ENC **)malloc(sizeof(ENC *)*h->num_contexts);
  enc[0]  = (ENC *)malloc(sizeof(ENC)*t_num_rules);
  for (i = 0, j = 0; i < h->num_contexts; i++) {
    rule[i] = rule[0] + j;
    enc[i]  = enc[0]  + j;
    j += num_rules[i];
  }
  for (i = 0; i < h->num_contexts; i++) {
    for (j = 0; j < h->char_size; j++) {
      rule[i][j].left  = j; rule[i][j].right = 0;
    }
    fread(rule[i]+h->char_size, sizeof(RULE8), 
	  num_rules[i]-h->char_size, input);
  }
  for (i = 0; i < h->num_contexts; i++) {
    for (j = 0; j < h->char_size; j++) {
      enc[i][j].tid = 
	tailContextId(i, h->mchar_size, h->cont_len, 
		      mchar_table[(uchar)j]);
      enc[i][j].len = 1;
      enc[i][j].string = (uchar*)malloc(sizeof(uchar)*2);
      enc[i][j].string[0] = echar_table[(uchar)j];
      enc[i][j].string[1] = '\0';
    }
  }
  for (j = h->char_size; j < 256; j++) {
    for (i = 0; i < h->num_contexts; i++) {
      uchar l, r;
      PCODE p;
      if (j >= num_rules[i]) continue;
      l = rule[i][j].left; r = rule[i][j].right;
      p = enc[i][l].tid;
      enc[i][j].len = enc[i][l].len + enc[p][r].len;
      enc[i][j].string = (uchar*)malloc(sizeof(uchar)*(enc[i][j].len+1));
      strcpy((char*)enc[i][j].string, (char*)enc[i][l].string);
      strcat((char*)enc[i][j].string, (char*)enc[p][r].string);
      enc[i][j].tid = enc[p][r].tid;
    }
  }

  free(echar_table); free(mchar_table);
  free(rule[0]); free(rule);

  printf("Expanding Compressed Text ...");
  fflush(stdout);
  {
    uchar rb[BUFFER_SIZE];
    uint  rb_len;
    uchar *rb_ptr, c;
    uchar wb[BUFFER_SIZE];
    uint  wb_pos;
    uchar *str, *wb_ptr;
    PCODE p = 0;

    wb_ptr = wb; wb_pos = 0;
    while ((rb_len = fread(rb, sizeof(uchar), BUFFER_SIZE, input)) > 0) {
      rb_ptr = rb;
      for (i = 0; i < rb_len; i++) {
	str = enc[p][c=*rb_ptr++].string;
	while (*str != '\0') {
	  *wb_ptr++ = *str++;
	  if (++wb_pos == BUFFER_SIZE) {
	    fwrite(wb, 1, BUFFER_SIZE, output);
	    wb_ptr = wb; wb_pos = 0;
	  }
	}
	p = enc[p][c].tid;
      }
    }
    fwrite(wb, 1, wb_pos, output);
  }

  printf("Done!!\n");
  fflush(stdout);

  for (i = 0; i < h->num_contexts; i++) {
    for (j = 0; j < num_rules[i]; j++) {
      free(enc[i][j].string);
    }
  }

  free(h);
  free(enc[0]); free(enc);
  free(num_rules);
}
Esempio n. 24
0
bool RegionFile::readOnlyHeaders() {
	std::ifstream file(filename.c_str(), std::ios_base::binary);
	int chunk_offsets[1024];
	return readHeaders(file, chunk_offsets);
}
Esempio n. 25
0
int main(int argc, char *argv[]){
  glutInit(&argc, argv); //initialize GL Utility Toolkit(GLUT) and  extract command line arguments for GLUT and keep the counts for the remaining arguments 
  glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB );
  
  windowInit(&window);

  std::ifstream ifs;
  readFile(ifs);
  readHeaders(&ifs, &window.width, &window.height, &window.numberOfPolyhedra);//read from datafile for the window-dimension and numberOfPolyhedra there are 
  
  glutInitWindowSize(window.width, window.height);
  glutInitWindowPosition(100, 100); 

  // initialize the pixel buffers
  PixelBuffer = new float[window.width*window.height*3];   
  SubWindowPixelBuffer1 = new float[ (window.width* window.height/4) * 3];
  SubWindowPixelBuffer2 = new float[ (window.width* window.height/4) * 3];
  SubWindowPixelBuffer3 = new float[ (window.width* window.height/4) * 3];
  
  // create sub-windows and their callback functions
  int mainWindowID = glutCreateWindow("Project 2");
  glutDisplayFunc(callback_display);

  int subWindowID1 = glutCreateSubWindow(mainWindowID, 0, 0, window.width/2, window.height/2);
  glutDisplayFunc(callback_subdisplay1);
  glutKeyboardFunc(callback_keyboard); 
  createMenu();


  int subWindowID2 = glutCreateSubWindow(mainWindowID, window.width/2, 0, window.width/2, window.height/2);
  glutDisplayFunc(callback_subdisplay2);
  glutKeyboardFunc(callback_keyboard); 
  createMenu();

  int subWindowID3 = glutCreateSubWindow(mainWindowID, 0, window.height/2, window.width/2, window.height/2);
  glutDisplayFunc(callback_subdisplay3);
  glutKeyboardFunc(callback_keyboard); 
  createMenu();


  Graph graph(window.width,window.height, PixelBuffer); 
  globalGraphs[0] = &graph;//any function that wants to draw can use this pointer(globalGraph) to graph
  graph.fillScreen(1,1,1); // white background
  
  Graph subGraph1(window.width/2, window.height/2, SubWindowPixelBuffer1);
  subGraph1.fillScreen(0.9,0.9,0.9);
  globalGraphs[1] = &subGraph1; 

  Graph subGraph2(window.width/2, window.height/2, SubWindowPixelBuffer2);
  subGraph2.fillScreen(0.7, 0.7, 0.7);
  globalGraphs[2] = &subGraph2; 

  Graph subGraph3(window.width/2, window.height/2, SubWindowPixelBuffer3);
  subGraph3.fillScreen(0.5,0.5,0.5);
  globalGraphs[3] = &subGraph3;

  Polyhedron *polyhedra[window.numberOfPolyhedra+1];
  globalPolyhedra = polyhedra;
  DPRINT("Read polyhedra, number of polyhdra:%d\n", window.numberOfPolyhedra); 
  readPolyhedra(&ifs, globalGraphs, polyhedra, window.numberOfPolyhedra); 
  
  //in addition of the polyhedra read from the datafile:
  //add a  an rotional axis
  Edge edge = {0 , 1};
  polyhedra[window.numberOfPolyhedra++] = new Polyhedron(globalGraphs, window.tf.pairOfPointsForRotAxis ,2, &edge, 1);

  drawPolyhedra(polyhedra); // draw polyhedra(objects) the first time 
    
  //callback registration:
  glutSetWindow(mainWindowID);
  glutKeyboardFunc(callback_keyboard); 
  createMenu();
  
  glutSetCursor(GLUT_CURSOR_CROSSHAIR);
  glutMainLoop();
  return 0;
}
Esempio n. 26
0
/**
 * main function
 * divided to two brances for master & slave processors respectively
 * @param argc commandline argument count
 * @param argv array of commandline arguments
 * @return 0 if success
 */
int main(int argc, char* argv[])
{
    	int rank;
	int size;
    	int num_clusters;
    	int num_points;
	int dex;
	int job_size;
	int job_done=0;
	
	Point* centroids;
	Point* points;
	Point* received_points;
	int  * slave_clusters;
	int  * former_clusters;
	int  * latter_clusters;
    	
	MPI_Init(&argc, &argv);
	
	MPI_Status status;

    	MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    	MPI_Comm_size(MPI_COMM_WORLD, &size);
	
	//creation of derived MPI structure
	MPI_Datatype MPI_POINT;
	MPI_Datatype type=MPI_DOUBLE;
	int blocklen=2;
	MPI_Aint disp=0;
	MPI_Type_create_struct(1,&blocklen,&disp,&type,&MPI_POINT);
	MPI_Type_commit(&MPI_POINT);

/******** MASTER PROCESSOR WORKS HERE******************************************************/ 
      
   	if(rank==MASTER)
  	{
		//inputting from file
		FILE *input;
    		input=fopen(argv[1],"r");
		readHeaders(input,&num_clusters,&num_points);
    		points=(Point*)malloc(sizeof(Point)*num_points);
		readPoints(input,points,num_points);
		fclose(input);

		//other needed memory locations
		former_clusters=(int*)malloc(sizeof(int)*num_points);
		latter_clusters=(int*)malloc(sizeof(int)*num_points);
		job_size=num_points/(size-1);
		centroids=malloc(sizeof(Point)*num_clusters);
		
		//reseting and initializing to default behaviour		
		initialize(centroids,num_clusters);
		resetData(former_clusters,num_points);
		resetData(latter_clusters,num_points);
		
		//Sending the essential data to slave processors
		for(dex=1;dex<size;dex++)
		{
			printf("Sending to [%d]\n",dex);
			MPI_Send(&job_size              ,1           , MPI_INT        ,dex,0,MPI_COMM_WORLD);
			MPI_Send(&num_clusters          ,1           , MPI_INT        ,dex,0,MPI_COMM_WORLD);
			MPI_Send(centroids              ,num_clusters, MPI_POINT      ,dex,0,MPI_COMM_WORLD);
			MPI_Send(points+(dex-1)*job_size,job_size    , MPI_POINT      ,dex,0,MPI_COMM_WORLD);
		}
    		printf("Sent!\n");

		MPI_Barrier(MPI_COMM_WORLD);

		//Main job of master processor is done here		
		while(1)
		{	
			MPI_Barrier(MPI_COMM_WORLD);
			
			printf("Master Receiving\n");
			for(dex=1;dex<size;dex++)
				MPI_Recv(latter_clusters+(job_size*(dex-1)),job_size,MPI_INT,dex,0,MPI_COMM_WORLD,&status);
			
			printf("Master Received\n");
			
			calculateNewCentroids(points,latter_clusters,centroids,num_clusters,num_points);
			printf("New Centroids are done!\n");
			if(checkConvergence(latter_clusters,former_clusters,num_points)==0)
			{
				printf("Converged!\n");
				job_done=1;
			}
			else    
			{
				printf("Not converged!\n");
				for(dex=0;dex<num_points;dex++)
					former_clusters[dex]=latter_clusters[dex];
			}
			
			//Informing slaves that no more job to be done
			for(dex=1;dex<size;dex++)
				MPI_Send(&job_done,1, MPI_INT,dex,0,MPI_COMM_WORLD);

			MPI_Barrier(MPI_COMM_WORLD);
			if(job_done==1)
				break;
	
			//Sending the recently created centroids			
			for(dex=1;dex<size;dex++)
				MPI_Send(centroids,num_clusters, MPI_POINT,dex,0, MPI_COMM_WORLD);

			MPI_Barrier(MPI_COMM_WORLD);
		}
		
		//Outputting to the output file		
		FILE* output=fopen(argv[2],"w");
		fprintf(output,"%d\n",num_clusters);
		fprintf(output,"%d\n",num_points);
		for(dex=0;dex<num_clusters;dex++)
			fprintf(output,"%lf,%lf\n",centroids[dex]._x,centroids[dex]._y);
		for(dex=0;dex<num_points;dex++)
			fprintf(output,"%lf,%lf,%d\n",points[dex]._x,points[dex]._y,latter_clusters[dex]+1);
		fclose(output);
	}
/*************END OF MASTER PROCESSOR'S BRANCH -- SLAVE PROCESSORS' JOB IS TO FOLLOW ************************/
	else
	{
		//Receiving the essential data
		printf("Receiving\n");
		MPI_Recv(&job_size    ,1           ,MPI_INT  ,MASTER,0,MPI_COMM_WORLD,&status);
		MPI_Recv(&num_clusters,1           ,MPI_INT  ,MASTER,0,MPI_COMM_WORLD,&status);
		centroids=malloc(sizeof(Point)*num_clusters);
		MPI_Recv(centroids    ,num_clusters,MPI_POINT,MASTER,0,MPI_COMM_WORLD,&status);
		printf("part_size =%d\n",job_size);
		received_points=(Point*)malloc(sizeof(Point)*job_size);
		slave_clusters=(int*)malloc(sizeof(int)*job_size);
		MPI_Recv(received_points,job_size,MPI_POINT      ,MASTER,0,MPI_COMM_WORLD,&status);
		printf("Received [%d]\n",rank);

		MPI_Barrier(MPI_COMM_WORLD);
		
		while(1)
		{
			printf("Calculation of new clusters [%d]\n",rank);
			for(dex=0;dex<job_size;dex++)
			{
				slave_clusters[dex]=whoIsYourDaddy(received_points[dex],centroids,num_clusters);
			}
			
			printf("sending to master [%d]\n",rank);
			MPI_Send(slave_clusters,job_size, MPI_INT,MASTER, 0, MPI_COMM_WORLD);
			MPI_Barrier(MPI_COMM_WORLD);
			MPI_Barrier(MPI_COMM_WORLD);
			MPI_Recv(&job_done,1, MPI_INT,MASTER,0,MPI_COMM_WORLD,&status);
					
			if(job_done==1) //No more work to be done
				break;
			
			//Receiving recently created centroids from master
			MPI_Recv(centroids,num_clusters,MPI_POINT,MASTER,0, MPI_COMM_WORLD,&status);

			MPI_Barrier(MPI_COMM_WORLD);
		}
	}
	//End of all	
	MPI_Finalize();
    	return 0;
}