Exemplo n.º 1
0
/**
 * filters, transforms, and indexes file using ngrams to the index
 * 
 * file name - name of file to process
 * wikiindex - the judy arrays to store the index of the wiki in
 */
void indexWiki(char* inFileName, Pvoid_t *wikiIndex, int* articleCount) {
	
	//-------------------- initialization --------------------//
	bool articleIndex[lastNgram] = {0}; // boolean array of what trigrams are in an article
	struct stemmer * currentStemmer = create_stemmer();
	
	// file for writing the titles to
	FILE* titleFile = NULL;
	if (writeFiles) {
		titleFile = fopen("title_file", "w");
		if (NULL == titleFile) {
			fprintf(stderr, "Error open title file: %m\n");
			exit(1);
		}
	}
	
	// initializes the libxml library
	LIBXML_TEST_VERSION
	xmlTextReaderPtr wikiReader; //the reader for the document
	wikiReader = xmlReaderForFile(inFileName, NULL, XML_PARSE_RECOVER+XML_PARSE_NOWARNING+XML_PARSE_NOERROR+XML_PARSE_HUGE);
	if (NULL == wikiReader) {
		//fprintf(stderr, "%s %s\n", "Failed to open ", wikiFileName);
		fprintf(stderr, "Error opening XML wiki: %m\n");
		exit(1);
	}

	// for progress bar
	int percent = 0;
	long fileSize = getFileSize(inFileName);
	
	// initialization for currentArticle and its componens 
	article currentArticle;	
	currentArticle.title = g_string_sized_new(256);
	currentArticle.body  = g_string_sized_new(786432); //768*1024

	//-------------------- index the wiki --------------------//
	optionalPrint ("%s", "Adding collection to index.\n");
	optionalPrint ("%d", (int)(fileSize / 1048576));
	optionalPrint (" MB in file\n");
	displayProgressBar (xmlTextReaderByteConsumed(wikiReader), fileSize, &percent);
	 
	//prime the loop
	currentArticle.title->len = 0;
	currentArticle.body->len  = 0;
	xmlTextReaderRead(wikiReader);// at a <page> tag, drop in
	xmlTextReaderRead(wikiReader);// at a <page> tag, drop in
	 
	// reads from xml file until file is finished, adds articles to index, and writes tittles to file
	// processes one article per iteration
	while (getArticle (wikiReader, &currentArticle)) {
		currentArticle.articleNumber = *articleCount;
		*articleCount = *articleCount + 1;
		// filter / transform text
		removeMarkup(currentArticle.body);
		stemText(currentArticle.body, currentStemmer); //ngramming.h
		// index the text
		indexText(currentArticle.body, articleIndex); //ngramming.h
		addToIndex(articleIndex, wikiIndex, currentArticle.articleNumber);
		//adds titles to title file
		if (writeFiles) {fprintf(titleFile, "%s\n", currentArticle.title->str);}
		//re-prime the loop
		currentArticle.title->len = 0;
		currentArticle.body->len  = 0;
		displayProgressBar (xmlTextReaderByteConsumed(wikiReader), fileSize, &percent);
	}
	optionalPrint ("\n%s", "Finished indexing. \n");
	optionalPrint ("%lu", (long)(xmlTextReaderByteConsumed(wikiReader)/1048576));
	optionalPrint ("MB consumed\n");
	
	
	optionalPrint ("%d %s %d %s", *articleCount, "articles found", (int) currentArticle.body->allocated_len, "length allocated for article body\n");
	// clean up of structures needed to process wiki
	if (writeFiles) {fclose (titleFile);}
	free_stemmer(currentStemmer);
	xmlFreeTextReader(wikiReader);
	xmlCleanupParser();
	//g_string_free(currentArticle.title, TRUE);
	//g_string_free(currentArticle.body, TRUE); //malloc fail if this is uncommented ?!
}
Exemplo n.º 2
0
   const int FreeMgr::releaseSegment( CommBlock &cb, DataBlock* blockZero, const int freemgr_type, const IdxTreeGroupType segmentType,  IdxEmptyListEntry* assignPtr )
   {
       int         rc;    // return code from file ops
       DataBlock   workBlock;
       DataBlock   extraBlock;
       int          listOffset; // entry in block zero of head pointer
       IdxEmptyListEntry emptyPtr;
       IdxEmptyListEntry newSb; 
       uint64_t     numBlocks;
       FILE*       indexFile;
               
       indexFile = cb.file.pFile;

       if (!assignPtr){
//            printf ("DBG: Bad pointer: assignPtr is zero\n");
           return ERR_INVALID_PARAM;
       }

       if (!blockZero){
//            printf ("DBG: Bad pointer: blockZero is zero\n");
           return ERR_INVALID_PARAM;
       }
       if( isDebug( DEBUG_3 )) { printf("DBG: release ENTRY_%i segment \n", 1<<(unsigned int)segmentType); }
       if( isDebug( DEBUG_2 )) 
       { 
           cout<<"DBG: releasing fbo "<<assignPtr->fbo<<" sbid "<<assignPtr->sbid<<" entry "<<assignPtr->entry<<" from "<<((freemgr_type==LIST)?"LIST":"TREE")<<" chain ENTRY_"<<(1<<segmentType)<<" (type is "<<((assignPtr->type==EMPTY_PTR)?"EMPTY_PTR":"Not EMPTY_PTR")<<")\n";
       }
       
       numBlocks = getFileSize( indexFile )/BYTE_PER_BLOCK ;

       /*       if (assignPtr->fbo > numBlocks)
       {
           if( isDebug( DEBUG_1 )) { printf("DBG: Weirdness in releaseSegment.. assignPtr.fbo > numBlocks (%llu %llu)\n", assignPtr->fbo, numBlocks );};
           return ERR_FM_BAD_FBO;
       }*/
       
       if (assignPtr->type != EMPTY_PTR)
       {
//            printf("DBG: Weirdness in releaseSegment.. tried to return a pointer with type %i (expected %i)\n", (unsigned int)assignPtr->type, EMPTY_PTR );
           assignPtr->type = EMPTY_PTR;
           //            return ERR_FM_BAD_TYPE; // do not exit
       }
       if ( assignPtr->group != (uint64_t)segmentType )
       {
//            printf("DBG: Weirdness in releaseSegment.. tried to return a pointer from group %i to group %i\n", (unsigned int)assignPtr->group, segmentType );
           return ERR_FM_RELEASE_ERR;
       }
       
       //find the start of the chain
       if (segmentType == ENTRY_32)
       {
           rc = releaseSubblock( cb, blockZero, freemgr_type, assignPtr);
           if (rc != NO_ERROR)
           { 
//                    printf("DBG: Error releasing sb\n");
           }
           return rc;
       }
               
       listOffset = calcPtrOffset( segmentType );
       if (freemgr_type==LIST && !(segmentType== ENTRY_4 || segmentType== ENTRY_BLK))
       {
           printf("DBG: Assign segment size illegal %i\n", (unsigned int)segmentType);
           return ERR_INVALID_PARAM; // should not have got here so quit with error
       }

       getSubBlockEntry( blockZero, 0, listOffset, 8, &emptyPtr );

/*       if (emptyPtr.fbo > numBlocks)
       {
       if( isDebug( DEBUG_1 )) { printf("DBG: Weirdness in releaseSegment.. emptyPtr.fbo > numBlocks (%llu %llu)\n", emptyPtr.fbo, numBlocks );}
           return ERR_FM_BAD_FBO;
       }*/
       
       //sub block is full or chain never started
       if (emptyPtr.entry == ENTRY_PER_SUBBLOCK-1 || emptyPtr.fbo==0)
       { 
           if( isDebug( DEBUG_3 ))
           { 
               printf("DBG: No room in chain %i - need to add a sub-block\n", (unsigned int)segmentType);
           }
           if( isDebug( DEBUG_2 )) 
           { 
               cout<<"DBG: Empty ptr fbo "<<emptyPtr.fbo<<" sbid "<<emptyPtr.sbid<<" entry "<<emptyPtr.entry<<"\n";
           }
            //ask for a new sb to extend chain
               rc = assignSubblock( cb, blockZero, freemgr_type, &newSb);
               if (rc != NO_ERROR) { printf("DBG: Error extending chain\n");  return rc; }
               if( isDebug( DEBUG_2 )) 
               { 
                   cout<<"DBG: release segment, new SB is fbo "<<newSb.fbo<<" sbid "<<newSb.sbid<<" entry "<<newSb.entry<<"\n";
               }
               rc = readDBFile( cb, extraBlock.data, newSb.fbo );
               if (rc != NO_ERROR)
               { 
                   if (isDebug( DEBUG_1 )){printf("DBG: File error during releaseSegment (3)\n");}
                   return rc; 
               }
               
               emptyPtr.type = EMPTY_LIST; // writing into the LLP field so set type accordingly
               setSubBlockEntry( extraBlock.data, newSb.sbid, 0, 8, &emptyPtr );
               setSubBlockEntry( extraBlock.data, newSb.sbid, 1, 8, assignPtr );
               rc = writeDBFile( cb, &extraBlock, newSb.fbo );
               if (rc != NO_ERROR)
               { 
                   if (isDebug( DEBUG_1 )){printf("DBG: File error during releaseSegment (4)\n");}
                   return rc; 
               }
               
               newSb.entry = 1;
               newSb.type = EMPTY_LIST;
               newSb.group = segmentType;

               setSubBlockEntry( blockZero, 0, listOffset, 8, &newSb );
               blockZero->dirty = 1;

               uint64_t count;
               getSubBlockEntry( blockZero, 0, calcStatOffset(segmentType) , 8, &count );
               count++;
               setSubBlockEntry( blockZero, 0, calcStatOffset(segmentType) , 8, &count );
               

               return NO_ERROR ;
            
       }
       else 
       { // 
           emptyPtr.entry++;
           rc = readDBFile( cb, workBlock.data, emptyPtr.fbo );
           if (rc != NO_ERROR){ 
               if (isDebug( DEBUG_1 )){ printf("DBG: File error during releaseSegment\n"); }
               return rc; 
           }

           if( isDebug( DEBUG_2 )) { 
               cout<<"DBG: Empty map ENTRY_"<<( 1<<segmentType)<<" is fbo "<<emptyPtr.fbo<<" sbid "<<emptyPtr.sbid<<" entry "<<emptyPtr.entry<<"\n";
           }
           setSubBlockEntry( workBlock.data, emptyPtr.sbid, emptyPtr.entry, 8, assignPtr );
           rc = writeDBFile( cb, workBlock.data, emptyPtr.fbo );
           if (rc != NO_ERROR){ return rc; }
           
           emptyPtr.type = EMPTY_LIST;
           emptyPtr.group = segmentType;

           setSubBlockEntry( blockZero, 0, listOffset, 8, &emptyPtr );
           blockZero->dirty = 1;//sub block is full or chain never started

       }

       uint64_t count;
       getSubBlockEntry( blockZero, 0, calcStatOffset(segmentType) , 8, &count );
       count++;
       setSubBlockEntry( blockZero, 0, calcStatOffset(segmentType) , 8, &count );
               

       return NO_ERROR;
   }
Exemplo n.º 3
0
int main(int argc, char *argv[])
{
    // techrypt < input file > [-d < IP-addr:port >][-l ]

    if((argc==4) && strncmp(*(argv+2),"-d",2)==0) //for sending file over network
    {
        char IP[16];
        char port[6];
        memset(IP,0,16);
        memset(port,0,6);
        
        char *token=NULL;
        int i=0;

        if (strchr(*(argv+3),':')) //check if port number has been provided
        {
            int sockfd = 0;
            struct sockaddr_in serv_addr; 
            
            FILE *f=fopen(*(argv+1),"r");
            int c=0,index=0,fLen=getFileSize(f);
            
            char fileContents[fLen];
            memset(fileContents,0,fLen);
            while((c=fgetc(f)) && !feof(f)) *(fileContents+index++)=c; //read file's contents into a buffer : 'fileContents'
            fclose(f);

            char passphrase[30];
            printf("Password: "******"\nCould not create socket!\n");
                        return 1;
                    } 

                    memset(&serv_addr, '0', sizeof(serv_addr)); 

                    serv_addr.sin_family = AF_INET;

                    char IPAddress[22];
                    memset(IPAddress,0,22);
                    strncpy(IPAddress,*(argv+3),21);
                    
                    for(token=strtok(*(argv+3),":");token!=NULL;token=strtok(NULL,":"),i++)
                    {
                        if (i==0) strncpy(IP,token,15); //get IP
                        else strncpy(port,token,5); //get port number
                    }

                    serv_addr.sin_port = htons(atoi(port)); 

                    if(inet_pton(AF_INET, IP, &serv_addr.sin_addr)<=0)
                    {
                        printf("\n'inet_pton' Error!\n");
                        return 1;
                    } 

                    if( connect(sockfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
                    {
                       printf("\nConnection Failed!\n");
                       return 1;
                    } 
                    printf("Transmitting to %s.\n",IPAddress);

                    char fNameLen[20];
                    memset(fNameLen,0,20);
                    sprintf(fNameLen,"%zu",strlen(*(argv+1))); //store file-name's length in variable 'fNameLen'

                    char fLength[20];
                    memset(fLength,0,20);
                    sprintf(fLength,"%d",ctxtLen+64); //store file's length in variable 'fLength'

                    char padASCII[20];
                    memset(padASCII,0,20);
                    sprintf(padASCII,"%d",pad); //store pad in variable 'padASCII'

                    char padLen[20];
                    memset(padLen,0,20);
                    sprintf(padLen,"%zu",strlen(padASCII)); //store pad's length in variable 'padLen'

                    write(sockfd,fNameLen,strlen(fNameLen)); //send file name's length
                    write(sockfd,*(argv+1),strlen(*(argv+1))); //send file's name
                    write(sockfd,padLen,strlen(padLen)); //send pad length
                    write(sockfd,"-",1); // send delimiter
                    write(sockfd,padASCII,strlen(padASCII)); //send pad
                    write(sockfd,fLength,strlen(fLength)); // send encrypted file's length
                    write(sockfd,ctxt,ctxtLen); // send encrypted file's contents
                    write(sockfd,mac,64); // send mac

                    printf("Successfully sent.\n");

                    if(ctxt)
                        free(ctxt);

                    return 0;
                }
                else
                    return 1;
            }
            else
            {
                printf("Error incurred during key generation!\n");
                return 1;
            }
        }
        else
        {
            printf("Invalid address!\n");
            return 1;
        }
    }
    if((argc==3) && strncmp(*(argv+2),"-l",2)==0) //for 'local' operation
    {
            char newFileName[strlen(*(argv+1))+3];
            memset(newFileName,0,strlen(*(argv+1))+3);

            strcpy(newFileName,*(argv+1));
            strcat(newFileName,".gt");

            if(!file_exists(newFileName)) // if file with name 'newFileName' exists, return 33
            {
                    FILE *f=fopen(*(argv+1),"r");
                    int c=0,index=0,fLen=getFileSize(f);
                    
                    char fileContents[fLen];
                    memset(fileContents,0,fLen);
                    while((c=fgetc(f)) && !feof(f)) *(fileContents+index++)=c; // read the contents of file (to be encrypted)
                    fclose(f);

                    char passphrase[30];
                    printf("Password: "******"%d",pad); //store pad's value in variable "pad"

                            FILE *f=fopen(newFileName,"w");
                            
                            fprintf(f,"%zu",strlen(padASCII));// write padLen
                            fprintf(f,"%c",'-'); // write delimiter
                            fprintf(f,"%d",pad); // write pad

                            index=0;
                            while(index<ctxtLen) fputc(*(ctxt+index++),f); // write encrypted file's contents

                            index=0;
                            while(index<64) fputc(*(mac+index++),f); // write mac
                            fclose(f);

                            if(ctxt)
                                free(ctxt);

                            return 0;
                        }
                        else
                            return 1;
                    }
                    else
                        return 1;
            }
            else
                return 33;
    }
    else
    {
        printf("Invalid option(s) given!\n");
        return 1;
    }
}
Exemplo n.º 4
0
/**
*@description send requested file to sock
*@param sockFd int  socket Id
*@param request Request  http_request struct
*@return if failed, return negative number, otherwise, nonnegative
*/
int sendRequestedFile(int sockFd, Request * httpRequest) {
	char ok_line[]       = "HTTP/1.0 200 OK\r\n";
	char badReq_line[]   = "HTTP/1.0 400 Bad Request\r\n";  
	char badReq_content[] = "<html><head></head><body><h1>400 Bad Request</h1></body></html>";
	char notFound_line[] = "HTTP/1.0 404 Not Found\r\n";
	char notFound_content[] = "<html><head></head><body><h1>404 Not Found</h1></body></html>";
	char suffix_line[]   = "\r\n";
	char writeBuffer[MAX_WRITE_BUFFER];
	char * responseLine;
	char *filePath = (httpRequest->requestLine).path;
	int fileSize;
	if (!checkFileExist(filePath)) {
	  printf("file %s doesn't exist\n", filePath);
	}
	short isResolved = httpRequest->isResolved;
	if ( isResolved ) {
		fileSize = getFileSize(filePath);
		if (fileSize < 0) {
			printf("request file %s not found, so send 404 response\n", filePath);
			if (send(sockFd, notFound_line, strlen(notFound_line), 0) < 0) {
				return -1;
			}

			//send content length
			memset(writeBuffer, '\0', sizeof(writeBuffer));
			snprintf(writeBuffer, sizeof(writeBuffer), "%s: %lu\r\n","Content-Length",strlen(notFound_content));
			if (send(sockFd, writeBuffer, strlen(writeBuffer), 0) < 0) {
				return -1;
			}

			//send persistent header
			memset(writeBuffer, '\0', sizeof(writeBuffer));
			if (httpRequest->isAlive == persistent) {
				snprintf(writeBuffer, sizeof(writeBuffer), "%s: %s\r\n", "Connection", "keep-alive");
			} else {
				snprintf(writeBuffer, sizeof(writeBuffer), "%s: %s\r\n", "Connection", "close");
			}
			if (send(sockFd, writeBuffer, strlen(writeBuffer), 0) < 0) {
				return -1;
			}

			//send content-type header
			memset(writeBuffer, '\0', sizeof(writeBuffer));
			snprintf(writeBuffer, sizeof(writeBuffer), "%s: %s\r\n", "Content-Type", "text/html");
			if (send(sockFd, writeBuffer, strlen(writeBuffer), 0) < 0) {
				return -1;
			}
			
			//send Date
			memset(writeBuffer, '\0', sizeof(writeBuffer));
			time_t current = time(0);
			time_t expire  = current + 60;
			struct tm currentTm = *gmtime(&current); 
			struct tm expireTm  = * gmtime(&expire);
			char timeStr[128];
			memset(timeStr, '\0', sizeof(timeStr));
			strftime(timeStr, sizeof(timeStr), "%a, %d %b %Y %H:%M:%S %Z", &currentTm);
			snprintf(writeBuffer, sizeof(writeBuffer), "%s: %s\r\n", "Date", timeStr);
			if (send(sockFd, writeBuffer, strlen(writeBuffer), 0) < 0) {
				return -1;
			}

			memset(writeBuffer, '\0', sizeof(writeBuffer));
			memset(timeStr, '\0', sizeof(timeStr));
			strftime(timeStr, sizeof(timeStr), "%a, %d %b %Y %H:%M:%S %Z", &expireTm);
			snprintf(writeBuffer, sizeof(writeBuffer), "%s: %s\r\n", "Expires", timeStr);
			if (send(sockFd, writeBuffer, strlen(writeBuffer), 0) < 0) {
				return -1;
			}
			//send suffix line
			if (send(sockFd, suffix_line, strlen(suffix_line), 0) < 0) {
				return -1;
			}
			//send content
			if (send(sockFd, notFound_content, strlen(notFound_content), 0) < 0) {
				return -1;
			}

			return 0;
		} else {

			printf("start to send request file of size %d bytes...\n", fileSize);
			//send 200 OK status line
			if (send(sockFd, ok_line, strlen(ok_line), 0) < 0) {
				return -1;
			}

			//send content-length header
			memset(writeBuffer, '\0', sizeof(writeBuffer));
			snprintf(writeBuffer, sizeof(writeBuffer), "%s: %d\r\n", "Content-Length", fileSize);
			if (send(sockFd, writeBuffer, strlen(writeBuffer), 0) < 0) {
				return -1;
			}
			
			//send persistent header
			memset(writeBuffer, '\0', sizeof(writeBuffer));
			if (httpRequest->isAlive == persistent) {
				snprintf(writeBuffer, sizeof(writeBuffer), "%s: %s\r\n", "Connection", "keep-alive");
			} else {
				snprintf(writeBuffer, sizeof(writeBuffer), "%s: %s\r\n", "Connection", "close");
			}
			if (send(sockFd, writeBuffer, strlen(writeBuffer), 0) < 0) {
				return -1;
			}

			//send content-type header
			memset(writeBuffer, '\0', sizeof(writeBuffer));
			snprintf(writeBuffer, sizeof(writeBuffer), "%s: %s\r\n", "Content-Type", "text/html");
			if (send(sockFd, writeBuffer, strlen(writeBuffer), 0) < 0) {
				return -1;
			}
			
			//send Date
			memset(writeBuffer, '\0', sizeof(writeBuffer));
			time_t current = time(0);
			time_t expire  = current + 60;
			struct tm currentTm = *gmtime(&current); 
			struct tm expireTm  = * gmtime(&expire);
			char timeStr[128];
			memset(timeStr, '\0', sizeof(timeStr));
			strftime(timeStr, sizeof(timeStr), "%a, %d %b %Y %H:%M:%S %Z", &currentTm);
			snprintf(writeBuffer, sizeof(writeBuffer), "%s: %s\r\n", "Date", timeStr);
			if (send(sockFd, writeBuffer, strlen(writeBuffer), 0) < 0) {
				return -1;
			}

			memset(writeBuffer, '\0', sizeof(writeBuffer));
			memset(timeStr, '\0', sizeof(timeStr));
			strftime(timeStr, sizeof(timeStr), "%a, %d %b %Y %H:%M:%S %Z", &expireTm);
			snprintf(writeBuffer, sizeof(writeBuffer), "%s: %s\r\n", "Expires", timeStr);
			if (send(sockFd, writeBuffer, strlen(writeBuffer), 0) < 0) {
				return -1;
			}
			//send suffix line
			if (send(sockFd, suffix_line, strlen(suffix_line), 0) < 0) {
				return -1;
			}
			
			//send request body
			if (transferFile(filePath, sockFd) == 0) {
				printf("send  file %s of %d bytes successfully\n", filePath, fileSize);
			} else {
				return -1;
			}
		}
	} else {
		printf("start to send 400 bad request\n");
		if (send(sockFd, badReq_line, strlen(badReq_line), 0) < 0 ) {
			return -1;
		}
		//send content length
		memset(writeBuffer, '\0', sizeof(writeBuffer));
		snprintf(writeBuffer, sizeof(writeBuffer), "%s: %lu\r\n","Content-Length",strlen(badReq_content));
		if (send(sockFd, writeBuffer, strlen(writeBuffer), 0) < 0) {
			return -1;
		}

		//send persistent header
		memset(writeBuffer, '\0', sizeof(writeBuffer));
		if (httpRequest->isAlive == persistent) {
			snprintf(writeBuffer, sizeof(writeBuffer), "%s: %s\r\n", "Connection", "keep-alive");
		} else {
			snprintf(writeBuffer, sizeof(writeBuffer), "%s: %s\r\n", "Connection", "close");
		}
		if (send(sockFd, writeBuffer, strlen(writeBuffer), 0) < 0) {
			return -1;
		}

		//send content-type header
		memset(writeBuffer, '\0', sizeof(writeBuffer));
		snprintf(writeBuffer, sizeof(writeBuffer), "%s: %s\r\n", "Content-Type", "text/html");
		if (send(sockFd, writeBuffer, strlen(writeBuffer), 0) < 0) {
			return -1;
		}
		
		//send Date
		memset(writeBuffer, '\0', sizeof(writeBuffer));
		time_t current = time(0);
		time_t expire  = current + 60;
		struct tm currentTm = *gmtime(&current); 
		struct tm expireTm  = * gmtime(&expire);
		char timeStr[128];
		memset(timeStr, '\0', sizeof(timeStr));
		strftime(timeStr, sizeof(timeStr), "%a, %d %b %Y %H:%M:%S %Z", &currentTm);
		snprintf(writeBuffer, sizeof(writeBuffer), "%s: %s\r\n", "Date", timeStr);
		if (send(sockFd, writeBuffer, strlen(writeBuffer), 0) < 0) {
			return -1;
		}

		memset(writeBuffer, '\0', sizeof(writeBuffer));
		memset(timeStr, '\0', sizeof(timeStr));
		strftime(timeStr, sizeof(timeStr), "%a, %d %b %Y %H:%M:%S %Z", &expireTm);
		snprintf(writeBuffer, sizeof(writeBuffer), "%s: %s\r\n", "Expires", timeStr);
		if (send(sockFd, writeBuffer, strlen(writeBuffer), 0) < 0) {
			return -1;
		}
		//send suffix line
		if (send(sockFd, suffix_line, strlen(suffix_line), 0) < 0) {
			return -1;
		}
		//send content
		if (send(sockFd, notFound_content, strlen(notFound_content), 0) < 0) {
			return -1;
		}
		return 0;
	}

	return 1;
}
Exemplo n.º 5
0
   const int FreeMgr::assignSegment( CommBlock &cb, DataBlock* blockZero, const int freemgr_type, const IdxTreeGroupType segmentType, IdxEmptyListEntry* assignPtr )
   {
       int          rc;    // return code from file ops
       DataBlock    workBlock;
       int          listOffset; // entry in block zero of head pointer
       IdxEmptyListEntry emptyEntry;
       IdxEmptyListEntry emptyPtr;
       IdxEmptyListEntry emptyMap;
       uint64_t          numBlocks;
       IdxEmptyListEntry newSb;
       FILE* indexFile;
               
       indexFile = cb.file.pFile;

       if( isDebug( DEBUG_3 )) { 
           printf("DBG: Assign ENTRY_%i segment in %s\n", 1<<(unsigned int)segmentType,(freemgr_type==LIST)?"LIST":"TREE");
       }
       
/* Check all input parameters are ok   */
       if (!blockZero){
           if( isDebug( DEBUG_1 )) { printf ("DBG: Bad pointer: blockZero is zero\n"); }
           return ERR_INVALID_PARAM;
       }
       
       if (!assignPtr)
       {
           if( isDebug( DEBUG_1 )) {  printf ("DBG: Bad pointer: assignPtr is zero\n"); }
           return ERR_INVALID_PARAM;
       }
       if (freemgr_type != TREE && freemgr_type!= LIST)
       {
           if( isDebug( DEBUG_0 )) { printf ("DBG: assignSegment: Must be TREE or LIST\n");}
           return ERR_INVALID_PARAM;
       }
           
       numBlocks = getFileSize( indexFile )/BYTE_PER_BLOCK ;
       //find the start of the chain
       if (segmentType == ENTRY_32)
       {
           rc = assignSubblock( cb, blockZero, freemgr_type, assignPtr);
           if (rc != NO_ERROR)
           { 
               if( isDebug( DEBUG_1 )) { printf("DBG: Error assigning sb (rc=%i)\n", rc); }
           }
           return rc;
       }
               
       listOffset = calcPtrOffset( segmentType );
       if (freemgr_type==LIST && !(segmentType== ENTRY_4 || segmentType== ENTRY_BLK))
       {
           if( isDebug( DEBUG_1 )) { printf("DBG: Assign segment size illegal %i\n", (unsigned int)segmentType); }
           return ERR_INVALID_PARAM; // should not have got here so quit with error
       }
       
       // read Empty Map in sb0
       getSubBlockEntry( blockZero, 0, listOffset, 8, &emptyPtr );
       if( isDebug( DEBUG_2 )) { 
           cout<<"DBG: Empty map ENTRY_"<<(1<<segmentType)<<"  was fbo "<<emptyPtr.fbo<<" sbid "<<(unsigned int)emptyPtr.sbid<<" entry "<<(unsigned int)emptyPtr.entry<<"\n";
       }

/*       if (emptyPtr.fbo > numBlocks)
       {
           printf("DBG: Weirdness in assignSegment.. emptyPtr.fbo > numBlocks\n");
           return ERR_FM_BAD_FBO;
       }*/
       
       // check to see if queue has been built
       // if not then assign a container block, add LLP in entry 0 pointing to nothing
       if (emptyPtr.fbo == 0){
           if( isDebug( DEBUG_3 )) { printf("DBG: Need to add sb to chain and entries to sb\n"); }
           
           // cannot assign more space to block list from a smaller list.. need to extend the file
           if ( segmentType == ENTRY_BLK ) {
               if ( isDebug( DEBUG_1 )){ printf("Out of space in BLOCK list, quitting\n"); }
               rc = extendFreespace( cb, blockZero, freemgr_type );
               if (rc != NO_ERROR){ 
                   if (isDebug( DEBUG_3 )){ printf("DBG: Error extending file\n"); }
                   return rc; 
               }
           }
           
           rc = assignSubblock( cb, blockZero, freemgr_type, &emptyPtr);
           if (rc != NO_ERROR){ printf("DBG: Error extending chain\n"); 
               return rc; 
           }
           rc = readDBFile( cb, workBlock.data, emptyPtr.fbo );
           if (rc != NO_ERROR){ /*printf("DBG: Error reading newly allocated sb\n");*/ 
               return rc; 
           }
           // update map to point to new bucket
           setSubBlockEntry( blockZero, 0, listOffset, 8, &emptyPtr); 

           nullPtr(&emptyEntry);
           emptyEntry.type = EMPTY_LIST;
           emptyEntry.group = segmentType;

           setSubBlockEntry( workBlock.data, emptyPtr.sbid, 0, 8, &emptyEntry);  // store head ptr

           rc = writeDBFile( cb, workBlock.data, emptyPtr.fbo );
           if (rc != NO_ERROR){ return rc; }
           if( isDebug( DEBUG_2 )) { 
		cout<<"DBG: Added fbo "<<emptyPtr.fbo<<" sbid "<<(unsigned int)emptyPtr.sbid<<" as bucket to chain ENTRY_"<<(1<<segmentType)<<"in"<<((freemgr_type==LIST)?"LIST":"TREE")<<"\n";
           }
       }

       // follow the chain to the head container 
       rc = readDBFile( cb, workBlock.data, emptyPtr.fbo );
       if (rc != NO_ERROR)
       { 
           if( isDebug( DEBUG_1 )) { cout<<"DBG: Error reading block ("<<emptyPtr.fbo<<") during segmentAssign: rc is "<<rc; }
           return rc; 
       }
       
       getSubBlockEntry( &workBlock, emptyPtr.sbid, emptyPtr.entry, 8, &emptyEntry );
           
       if ((emptyEntry.type != EMPTY_LIST) && (emptyEntry.type != EMPTY_PTR))
       {
           if( isDebug( DEBUG_0 )) { 
               printf("WTF: Bad entry in ENTRY_%i chain - type is %i (expected %i or %i)\n",  1<<segmentType, (unsigned int)emptyEntry.type, EMPTY_PTR, EMPTY_LIST );
               printMemSubBlock( &workBlock, emptyPtr.sbid );
           }
           if( isDebug( DEBUG_2 )) { 
               cout<<"DBG: fbo "<<emptyEntry.fbo<<" sbid "<<(unsigned int)emptyEntry.sbid<<" entry "<< (unsigned int)emptyEntry.entry<<" chain ENTRY_"<<(1<<segmentType)<<"in"<<((freemgr_type==LIST)?"LIST":"TREE")<<"\n";
           }

           return ERR_FM_BAD_TYPE;
       }
       
       if ((emptyEntry.fbo == 0) && (emptyEntry.type == EMPTY_PTR))
       {
           if ( isDebug( DEBUG_0 )) {printf("DBG: Bad entry in %i list - found EMPTY_PTR but indicates block 0\n",  1<<segmentType );}
           return ERR_FM_BAD_TYPE;
       }

       if ((emptyEntry.fbo == 0) && (emptyEntry.type == EMPTY_LIST)) 
       {
           /**
            * if at the end of the rainbow, in a bucket with no entries, fill the bucket
            * Allocate a sub block and split it into parts
           **/
           
           // cannot assign more space to block list from a smaller list.. need to extend the file
           if ( segmentType == ENTRY_BLK ) {
               if ( isDebug( DEBUG_1 )){ printf("\nNeed to extend block\n");}
               if ( isDebug( DEBUG_1 )){ printf("Out of space in BLOCK list\n"); }
               rc = extendFreespace( cb, blockZero, freemgr_type );
               if (rc != NO_ERROR){ 
                   if (isDebug( DEBUG_3 )){ printf("DBG: Error extending file\n"); }
                   return rc; 
               }
               getSubBlockEntry( blockZero, 0, listOffset, 8, &emptyPtr );
               rc = readDBFile( cb, workBlock.data, emptyPtr.fbo );
               getSubBlockEntry( &workBlock, emptyPtr.sbid, emptyPtr.entry, 8, &emptyEntry );

           } else {
           
           rc = assignSubblock( cb, blockZero, freemgr_type, &newSb);
           if (rc != NO_ERROR){ 
//                printf("DBG: Error extending chain\n"); 
               return rc; 
           }
           if (newSb.entry != 0){
               printf("WTF: Entry should be 0 after assign from sb list, instead is %i", (unsigned int)newSb.entry);
               return ERR_FM_ASSIGN_ERR;
           }
           if (isDebug(DEBUG_2)){
               cout<<"DBG: added fbo "<<newSb.fbo<<" sbid "<<(unsigned int)newSb.sbid<<" entry "<<(unsigned) newSb.entry<<" to "<<segmentType<<" list - need to split "<<(1<<(ENTRY_32-segmentType))<<"times\n";
           }
           
           newSb.entry = 0;
           newSb.group = segmentType;
           newSb.type = EMPTY_PTR;
           emptyEntry = newSb;

           int idx, inc;
           inc = 1<<segmentType;
           for (idx=0; idx < ENTRY_PER_SUBBLOCK - inc; idx+= inc){
               if( isDebug( DEBUG_3 )) { printf ("DBG: split..%i-%i\n", idx, idx+inc-1 );}
               newSb.entry = idx;
               releaseSegment( cb, blockZero, freemgr_type, segmentType,  &newSb );
           }
           emptyEntry.entry = idx;
           if( isDebug( DEBUG_3 )) 
           { 
               printf ("DBG: split and return..%i-%i\n", idx, idx+inc-1);
           }
           if( isDebug( DEBUG_2 )) 
           { 
             cout<<"DBG: Assigned fbo "<< emptyEntry.fbo<<" sbid "<<(unsigned int)emptyEntry.sbid<<" entry "<<(unsigned int)emptyEntry.entry<<" to chain ENTRY_"<<(1<<segmentType)<<"\n";
           }
           memcpy(assignPtr, &emptyEntry, 8);
           
           uint64_t count;
           getSubBlockEntry( blockZero, 0, calcStatOffset(segmentType) , 8, &count );
           count--;
           setSubBlockEntry( blockZero, 0, calcStatOffset(segmentType) , 8, &count );

           return NO_ERROR;
       }
       }
       /**
        * got here because we didn't need to populate a chain and did not fall into any traps
        * so either we are at the end of bucket or we have a valid entry
        **/
       if (emptyEntry.type == EMPTY_LIST) // reached end of this segment (should release it for re-use)
           {
               /**
                * release bucket
                **/
               if( isDebug( DEBUG_2 )) { 
                   cout<<"DBG: Need to release sb fbo "<<emptyPtr.fbo<<" sbid "<<emptyPtr.sbid<<" from chain ENTRY_"<<(1<<segmentType)<<" in "<<((freemgr_type==LIST)?"LIST":"TREE")<<"\n";
               }
               // when we stored the ptr in the empty map, we tweaked group, must change it back
               emptyPtr.type = EMPTY_PTR;
               emptyPtr.group = ENTRY_32;
               rc = releaseSubblock( cb, blockZero, freemgr_type, &emptyPtr );
               if (rc != NO_ERROR)
               { 
                   printf("Error releasing sb\n");
                   return rc; 
               }
               emptyPtr = emptyEntry;
               rc = readDBFile( cb, workBlock.data, emptyPtr.fbo );

               if (rc != NO_ERROR){ 
                   printf("DBG: Error following chain\n");
                   return rc; 
               }
               getSubBlockEntry( &workBlock, emptyPtr.sbid, emptyPtr.entry, 8, &emptyEntry );

           } 

       if (emptyEntry.type == EMPTY_PTR)
       {
       
           emptyPtr.entry--; 
           blockZero->dirty = 1;
           setSubBlockEntry( blockZero, 0, listOffset, 8, &emptyPtr );
           
           if( isDebug( DEBUG_3 )) { 
               printf("DBG: Empty entry is now %u\n",(unsigned int)emptyPtr.entry);
           }
           if( isDebug( DEBUG_2 )) 
           { 
               cout<<"DBG: Assigned fbo "<<emptyEntry.fbo<<" sbid "<<emptyEntry.sbid<<" entry "<<emptyEntry.entry<<" from chain ENTRY_"<<(1<<segmentType)<<"\n";
               cout<<"DBG: Empty map ENTRY_"<<(1<<segmentType)<<" now fbo "<<emptyPtr.fbo<<" sbid "<<emptyPtr.sbid<<" entry "<<emptyPtr.entry<<"\n";
           }
           memcpy(assignPtr, &emptyEntry, 8);
         
           // -- workblock may have changed on disk since it was read.. making the writeDBfile dangerous without a readDBfile first
          nullPtr(&emptyMap); // zero out the entry
          readDBFile( cb, &workBlock, emptyPtr.fbo );
          setSubBlockEntry( workBlock.data, emptyPtr.sbid, emptyPtr.entry+1, 8, &emptyMap );
          rc = writeDBFile( cb, &workBlock, emptyPtr.fbo );
           // --
           
           uint64_t count;
           getSubBlockEntry( blockZero, 0, calcStatOffset(segmentType) , 8, &count );
           count--;
           setSubBlockEntry( blockZero, 0, calcStatOffset(segmentType) , 8, &count );

           return NO_ERROR;
       }
       
       return ERR_FM_ASSIGN_ERR;
   }
Exemplo n.º 6
0
int calculateVirtualStreamPos(struct VirtualStream * stream,ObjectIDHandler ObjID,unsigned int timeAbsMilliseconds,float * pos)
{
   if (stream==0) { fprintf(stderr,"calculateVirtualStreamPos called with null stream\n"); return 0; }
   if (stream->object==0) { fprintf(stderr,"calculateVirtualStreamPos called with null object array\n"); return 0; }
   if (stream->numberOfObjects<=ObjID) { fprintf(stderr,"calculateVirtualStreamPos ObjID %u is out of bounds (%u)\n",ObjID,stream->numberOfObjects); return 0; }
   if (stream->object[ObjID].frame == 0 )  { fprintf(stderr,"calculateVirtualStreamPos ObjID %u does not have a frame array allocated\n",ObjID); return 0; }
   if (stream->object[ObjID].numberOfFrames == 0 ) { fprintf(stderr,"calculateVirtualStreamPos ObjID %u has 0 frames\n",ObjID); return 0; }


   if (stream->autoRefresh != 0 )
    {
         //Check for refreshed version ?
       if (stream->autoRefresh < timeAbsMilliseconds-stream->lastRefresh )
          {
            unsigned long current_size = getFileSize(stream->filename);
            if (current_size != stream->fileSize)
             {
              refreshVirtualStream(stream);
              stream->lastRefresh = timeAbsMilliseconds;
             }
          }
    }

   unsigned int FrameIDToReturn = 0;
   unsigned int FrameIDLast = 0;
   unsigned int FrameIDNext = 0;


   /*!OK , Two major cases here..! The one is a simple Next frame getter , the second is a more complicated interpolated frame getter..! */
   if ( (stream->object[ObjID].MAX_numberOfFrames == 0 ) )
   {
       fillPosWithNull(stream,ObjID,pos);
       return 1;
   } else
   if  ( (stream->ignoreTime) || (stream->object[ObjID].MAX_numberOfFrames == 1 ) )
   {
    //We might want to ignore time and just return frame after frame on each call!
    //Also if we only got one frame for the object there is no point in trying to interpolate time etc.. so just handle things here..
    if ( stream->object[ObjID].lastFrame +1 >= stream->object[ObjID].MAX_numberOfFrames ) { stream->object[ObjID].lastFrame  = 0; }
    FrameIDToReturn = stream->object[ObjID].lastFrame;
    ++stream->object[ObjID].lastFrame;

    fillPosWithFrame(stream,ObjID,FrameIDToReturn,pos);

    FrameIDLast = FrameIDToReturn;
    FrameIDNext = FrameIDToReturn+1;
    if ( FrameIDNext >= stream->object[ObjID].numberOfFrames )
     {
       FrameIDNext  = 0;
     }

     return 1;

   } /*!END OF SIMPLE FRAME GETTER*/
   else
   { /*!START OF INTERPOLATED FRAME GETTER*/
     //This is the case when we respect time , we will pick two frames and interpolate between them
     if ( timeAbsMilliseconds > stream->object[ObjID].MAX_timeOfFrames )
     {
       //This means we have passed the last frame.. so lets find out where we really are..
       timeAbsMilliseconds = timeAbsMilliseconds % stream->object[ObjID].MAX_timeOfFrames;
       //timeAbsMilliseconds should contain a valid value now somewhere from 0->MAX_timeOfFrames
     }

     #if PRINT_DEBUGGING_INFO
     fprintf(stderr,"Object %u has %u frames , lets search where we are \n",ObjID,stream->object[ObjID].numberOfFrames);
     #endif

     //We scan all the frames to find out the "last one" and the "next one"
     unsigned int i =0;
     for ( i=0; i <stream->object[ObjID].MAX_numberOfFrames-1; i++ )
      {
       if (( stream->object[ObjID].frame[i].time <= timeAbsMilliseconds )
                 &&
           ( timeAbsMilliseconds <= stream->object[ObjID].frame[i+1].time )  )
            {
               //This is the "next" frame!
               FrameIDLast = i;
               FrameIDNext = i+1;
               //This should be handled by raw response to zero elemetn :P
              break;
            }
      }

    //We now have our Last and Next frame , all that remains is extracting the
    //interpolated time between them..!
    return fillPosWithInterpolatedFrame(stream,ObjID,pos,FrameIDLast,FrameIDNext,timeAbsMilliseconds);

   } /*!END OF INTERPOLATED FRAME GETTER*/

    return 0;
}
/**
  * Provera da li je tekuca pozicija u fajlu kraj tog fajla
  */
char KernelFile::eof() const {
	if (getFileSize() == 0) return 1; // ako je prazan fajl, greska
	if (getFileSize() == pointer) return 2; // eof
	return 0;
}
Exemplo n.º 8
0
	MemoryMapImpl(const char *mappingObject,uint64_t size,bool createOk)
	{
		mData = NULL;
		mMapFile = NULL;
		mMapHandle = NULL;
		bool createFile = true;
		bool fileOk = false;

		HANDLE h = CreateFileA(mappingObject, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
		if ( h != NULL )
		{
			size = getFileSize(h);
			fileOk = true;
			createFile = false;
			printf("Found previous existing mapping file '%s' and using it.\r\n", mappingObject );
			CloseHandle(h);
		}

		if ( createFile && createOk )
		{
			printf("Creating memory map file: %s\r\n", mappingObject);
			HANDLE h = CreateFileA(mappingObject, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
			if ( h != NULL )
			{
				#define ONEMB (1024*1024)
				char *temp = (char *)malloc(ONEMB);
				if ( temp )
				{
					memset(temp,0,ONEMB);
					uint64_t writeSize = size;
					while ( writeSize > 0 )
					{
						DWORD w = writeSize >= ONEMB ? ONEMB : (DWORD)writeSize;
						DWORD bytesWritten=0;
						WriteFile(h,temp,w,&bytesWritten,NULL);
						if ( bytesWritten != w )
						{
							printf("Failed to write to file '%s', out of disk space?\r\n", mappingObject );
							break;
						}
						writeSize-=w;
					}
					if ( writeSize == 0)
					{
						fileOk = true;
						printf("Finished creating mapping file '%s'\r\n", mappingObject );
					}
				}
				CloseHandle(h);
			}
		}

		if ( fileOk )
		{
			mMapSize = size;
			mMapFile = CreateFileA(mappingObject, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
			if (mMapFile != INVALID_HANDLE_VALUE)
			{
				mMapHandle = CreateFileMappingA(mMapFile, NULL, PAGE_READWRITE, 0, 0, NULL);
				if (mMapHandle == INVALID_HANDLE_VALUE) 
				{
					CloseHandle(mMapFile);
					mMapFile = NULL;
				}
				else
				{
					mData = MapViewOfFile(mMapHandle, FILE_MAP_WRITE, 0, 0, 0);
					if ( mData == NULL) 
					{
						CloseHandle(mMapHandle);
						CloseHandle(mMapFile);
						mMapHandle = NULL;
						mMapFile = NULL;
					}
				}
			}
		}
	}
Exemplo n.º 9
0
/**************************************************************************
* Function: GetDirSizeandFiles

* Functionality: Get the total files and total file size of a directory

* @IN :srcname: the directory path name.
* @OUT: 
	totalSize: total file size
	filenum: total files
	0 OK else error 

* Date: 20080312
* Changed history:
* Date          Who             Reason
* 20080312  sandy           First  creation
***************************************************************************/ 
int GetDirSizeandFiles( const char * srcname, long long *totalSize, int *filenum)
{
	DIR *Psdir = NULL;
	DIR *PchilDir = NULL;
	struct stat filestat;
	char srcfile[FILE_PATH_LEN] = {0};
	struct dirent *srcdir;
	int iRet = 0;
	int isDir;
	
	if(!srcname || strlen(srcname) < 0){
		fprintf(stderr,"src dir is null\n");
		iRet = -1;
		goto Funcout;
	}
	if(!CheckFilePresent(srcname, &isDir)){
		return -1;
	}
	
	if(!isDir){
		*totalSize += getFileSize((char *)srcname);
		(*filenum) ++;
	}
	else{
		if((Psdir = opendir(srcname)) == NULL){
			fprintf(stderr,"open src dir failed\n");
			iRet = -1;
			goto Funcout;
		}
		while((srcdir = readdir(Psdir)) != NULL)
		{
			if(srcdir->d_ino == 0)
				continue;
			
			memset(srcfile, 0, FILE_PATH_LEN);
			strcpy(srcfile,srcname);
			strcat(srcfile, "/");
			strcat(srcfile, srcdir->d_name);
			
			if((lstat(srcfile, &filestat)) < 0)
			{
				continue;
			}
			
			if(((filestat.st_mode & S_IFMT) != S_IFLNK) && ((filestat.st_mode & S_IFMT) == S_IFDIR) && (strcmp(srcdir->d_name, ".") != 0) && (strcmp(srcdir->d_name, "..") != 0))
			{
				if((PchilDir = opendir(srcfile)) == NULL)
				{
					fprintf(stderr,"can not enter the dir %s\n", srcfile);
					iRet = -1;
					goto Funcout;
				}
			
				*totalSize += 1;
				GetDirSizeandFiles((const char *)srcfile, totalSize,filenum);
				closedir(PchilDir);
			}
			
			else if(((filestat.st_mode & S_IFMT) != S_IFLNK) && ((filestat.st_mode & S_IFMT) == S_IFREG) && (strcmp(srcdir->d_name, ".") != 0) && (strcmp(srcdir->d_name, "..") != 0))
			{
				*totalSize += filestat.st_size/(1024*1024);
				(*filenum) ++;
			}
		}
	}
	Funcout:
			if(Psdir)
			closedir(Psdir);
	
	return iRet;
}
Exemplo n.º 10
0
int main(int argc, char** argv) {

	OMX_PORT_PARAM_TYPE param;
	OMX_PARAM_PORTDEFINITIONTYPE sPortDef;
	OMX_AUDIO_PORTDEFINITIONTYPE sAudioPortDef;
	OMX_AUDIO_PARAM_PORTFORMATTYPE sAudioPortFormat;
	OMX_AUDIO_PARAM_PCMMODETYPE sPCMMode;
	OMX_BUFFERHEADERTYPE **inBuffers[2];

	char *componentName = "OMX.broadcom.video_render";
	unsigned char name[OMX_MAX_STRINGNAME_SIZE];
	OMX_UUIDTYPE uid;
	int startPortNumber;
	int nPorts;
	int i, n;

	bcm_host_init();

	fprintf(stderr, "Thread id is %p\n", pthread_self());
	if(argc < 2){
		display_help(argv);
		exit(1);
	}

	fd = open(argv[1], O_RDONLY);
	if(fd < 0){
		perror("Error opening input file\n");
		exit(1);
	}
	filesize = getFileSize(fd);


	err = OMX_Init();
	if(err != OMX_ErrorNone) {
		fprintf(stderr, "OMX_Init() failed\n", 0);
		exit(1);
	}
	/** Ask the core for a handle to the audio render component
	 */
	err = OMX_GetHandle(&handle, componentName, NULL /*app private data */, &callbacks);
	if(err != OMX_ErrorNone) {
		fprintf(stderr, "OMX_GetHandle failed\n", 0);
		exit(1);
	}
	err = OMX_GetComponentVersion(handle, name, &compVersion, &specVersion, &uid);
	if(err != OMX_ErrorNone) {
		fprintf(stderr, "OMX_GetComponentVersion failed\n", 0);
		exit(1);
	}

	/** disable other ports */
	disablePort(OMX_IndexParamOtherInit);

	/** Get audio port information */
	setHeader(&param, sizeof(OMX_PORT_PARAM_TYPE));
	err = OMX_GetParameter(handle, OMX_IndexParamVideoInit, &param);
	if(err != OMX_ErrorNone){
		fprintf(stderr, "Error in getting OMX_PORT_PARAM_TYPE parameter\n", 0);
		exit(1);
	}
	startPortNumber = ((OMX_PORT_PARAM_TYPE)param).nStartPortNumber;
	nPorts = ((OMX_PORT_PARAM_TYPE)param).nPorts;
	if (nPorts > 2) {
		fprintf(stderr, "Image device has more than one port\n");
		exit(1);
	}

	for (i = 0; i < nPorts; i++) {
		/* Get and check port information */
		setHeader(&sPortDef, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
		sPortDef.nPortIndex = startPortNumber + i;
		err = OMX_GetParameter(handle, OMX_IndexParamPortDefinition, &sPortDef);

		if(err != OMX_ErrorNone) {
			fprintf(stderr, "Error in getting OMX_PORT_DEFINITION_TYPE parameter\n", 0);
			exit(1);
		}
		if (sPortDef.eDomain != OMX_PortDomainVideo) {
			fprintf(stderr, "Port %d is not a video port\n", sPortDef.nPortIndex);
			exit(1);
		}

		if (sPortDef.eDir == OMX_DirInput)
			fprintf(stdout, "Port %d is an input port\n", sPortDef.nPortIndex);
		else
			fprintf(stdout, "Port %d is an output port\n", sPortDef.nPortIndex);

		if (sPortDef.format.video.eColorFormat == OMX_COLOR_FormatYUV420PackedPlanar)
			fprintf(stderr, "Port color Format is YUV420PackedPlanar\n");
		else
			fprintf(stderr, "Port has unknown color format\n");

		/* Set Image Format -- FIXME: hardcoded */
		sPortDef.format.video.nFrameWidth = 1920;
		sPortDef.format.video.nFrameHeight = 1080;
		sPortDef.format.video.nStride =
			ALIGN(sPortDef.format.video.nFrameWidth, 32);
		sPortDef.format.image.nSliceHeight =
			ALIGN(sPortDef.format.video.nFrameHeight, 16);
		sPortDef.nBufferSize = sPortDef.format.image.nStride *
			sPortDef.format.image.nSliceHeight * 3 / 2;

		/* Create minimum number of buffers for the port */
		nBuffers = sPortDef.nBufferCountActual = sPortDef.nBufferCountMin;
		fprintf(stderr, "Number of bufers is %d\n", nBuffers);
		err = OMX_SetParameter(handle, OMX_IndexParamPortDefinition, &sPortDef);
		if(err != OMX_ErrorNone){
			fprintf(stderr, "Error in setting OMX_PORT_PARAM_TYPE parameter\n", 0);
			exit(1);
		}
		if (sPortDef.bEnabled) {
			fprintf(stderr, "Port %d is enabled\n", sPortDef.nPortIndex);
		} else {
			fprintf(stderr, "Port %d is not enabled\n", sPortDef.nPortIndex);
		}
	}

	/* call to put state into idle before allocating buffers */
	printf("OMX_CommandStateSet, OMX_StateIdle\n");
	err = OMX_SendCommand(handle, OMX_CommandStateSet, OMX_StateIdle, NULL);
	if (err != OMX_ErrorNone) {
		fprintf(stderr, "Error on setting state to idle\n");
		exit(1);
	}

	for (i = 0; i < nPorts; i++) {
		/* Get and check port information */
		setHeader(&sPortDef, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
		sPortDef.nPortIndex = startPortNumber + i;
		err = OMX_GetParameter(handle, OMX_IndexParamPortDefinition, &sPortDef);

		if(err != OMX_ErrorNone) {
			fprintf(stderr, "Error in getting OMX_PORT_DEFINITION_TYPE parameter\n", 0);
			exit(1);
		}

		if (!sPortDef.bEnabled) {
			printf("OMX_CommandPortEnable, %d\n", startPortNumber);
			err = OMX_SendCommand(handle, OMX_CommandPortEnable, startPortNumber, NULL);
			if (err != OMX_ErrorNone) {
				fprintf(stderr, "Error on setting port to enabled\n");
				exit(1);
			}
		}
	}

	/* Configure buffers for the port */
	for (i = 0; i < nPorts; i++) {
		setHeader(&sPortDef, sizeof(OMX_PARAM_PORTDEFINITIONTYPE));
		sPortDef.nPortIndex = startPortNumber + i;
		err = OMX_GetParameter(handle, OMX_IndexParamPortDefinition, &sPortDef);

		nBufferSize = sPortDef.nBufferSize;
		fprintf(stderr, "Port %d has %d buffers of size %d\n", sPortDef.nPortIndex,
				nBuffers, nBufferSize);

		inBuffers[i] = malloc(nBuffers * sizeof(OMX_BUFFERHEADERTYPE *));
		if (inBuffers[i] == NULL) {
			fprintf(stderr, "Can't allocate buffers\n");
			exit(1);
		}

		for (n = 0; n < nBuffers; n++) {
			err = OMX_AllocateBuffer(handle, inBuffers[i] + n, startPortNumber + i, NULL,
						 nBufferSize);
			if (err != OMX_ErrorNone) {
				fprintf(stderr, "Error on AllocateBuffer in 1%i\n", err);
				exit(1);
			}
		}
	}

	printf("Transition to Idle\n");
	/* Make sure we've reached Idle state */
	waitFor(OMX_StateIdle);

	printf("Transition to Executing\n");
	/* Now try to switch to Executing state */
	err = OMX_SendCommand(handle, OMX_CommandStateSet, OMX_StateExecuting, NULL);
	if(err != OMX_ErrorNone){
		exit(1);
	}

	/* One buffer is the minimum for Broadcom component, so use that */
	pEmptyBuffer = inBuffers[0][0];
	emptyState = 1;
	/* Fill and empty buffer */
	for (;;) {
		int data_read = read(fd, pEmptyBuffer->pBuffer, nBufferSize);
		pEmptyBuffer->nFilledLen = data_read;
		pEmptyBuffer->nOffset = 0;
		filesize -= data_read;
		if (filesize <= 0) {
			pEmptyBuffer->nFlags = OMX_BUFFERFLAG_EOS;
		}
		fprintf(stderr, "Emptying again buffer %p %d bytes, %d to go\n", pEmptyBuffer, data_read, filesize);
		err = OMX_EmptyThisBuffer(handle, pEmptyBuffer);
		waitForEmpty();
		fprintf(stderr, "Waited for empty\n");
		if (bEOS) {
			fprintf(stderr, "Exiting loop\n");
			break;
		}
	}
	fprintf(stderr, "Buffers emptied\n");
	exit(0);
}
Exemplo n.º 11
0
void Track::writeData( QByteArray& data) const {
    QBuffer buffer( data);
    buffer.open(IO_WriteOnly);
    QDataStream stream( &buffer);
    stream.setByteOrder( QDataStream::LittleEndian);

    /** Write the track header **/
    stream << (Q_UINT32) 0x7469686D;        // 0x00 mhit
    stream << (Q_UINT32) 0xf4;              // 0x04 headerlen
    stream << (Q_UINT32) 0x0;               // 0x08 length - set later
    stream << (Q_UINT32) 0x0;               // 0x0C number of mhods
    stream << (Q_UINT32) getID();           // 0x10
    stream << (Q_UINT32) 1;                 // 0x14
    //stream << (Q_UINT32) 0;                 // 0x18
    stream << (Q_UINT32) 0x4d503320;        // ipod shiffle wants a "MP3 " here
    stream << vbr;                          // 0x1C
    stream << type;                         // 0x1D
    stream << compilation;                  // 0x1E
    stream << rating;                       // 0x1F
    stream << (Q_UINT32) getLastModified()+ MAC_EPOCH_DELTA; // 0x20
    stream << (Q_UINT32) getFileSize();     // 0x24
    stream << (Q_UINT32) getTrackLength();  // 0x28
    stream << (Q_UINT32) getTrackNumber();  // 0x2C
    stream << (Q_UINT32) getTrackCount();   // 0x30
    stream << (Q_UINT32) getYear();         // 0x34
    stream << (Q_UINT32) getBitrate();      // 0x38
    stream << (Q_UINT32) getSamplerate();   // 0x3C
    stream << (Q_UINT32) getVolumeAdjust(); // 0x40
    stream << (Q_UINT32) 0;                 // 0x44 empty space
    //stream << (Q_UINT32) getTrackLength();  // 0x48 empty space
    stream << (Q_UINT32) 0;  // 0x48 empty space
    stream << (Q_UINT32) 0;                 // 0x4C empty space
    stream << (Q_UINT32) getPlayCount();    // 0x50
    stream << (Q_UINT32) getPlayCount();    // 0x54
    stream << (Q_UINT32) getLastPlayed();   // 0x58
    stream << (Q_UINT32) getCdNumber();     // 0x5C
    stream << (Q_UINT32) getCdCount();      // 0x60
    stream << (Q_UINT32) 0;                 // 0x64 empty space //userid from apple store
    stream << (Q_UINT32) date_added;        // 0x68
    stream << (Q_UINT32) 0;                 // boockmarktime
    stream << (Q_UINT64) dbid;              // unique bit (64 bit)
    stream << (Q_UINT8) 0;                 // checked in iTZnes
    stream << (Q_UINT8) 0;                 // application rating
    stream << (Q_UINT16) 0;                 // BPM
    stream << (Q_UINT16) 0;                 // artworkcount
    stream << (Q_UINT16) 0xffff;            // unkown
    stream << (Q_UINT32) 0;                 // artwork size
    stream << (Q_UINT32) 0;                 // unkown
    stream << (float) -getSamplerate();      // samplerate as floating point "-"?!?
    stream << (Q_UINT32) 0;                 // date/time added
    stream << (Q_UINT32) file_format_code;  // unkown, but 0x0000000c for MP3 ?
    stream << (Q_UINT32) 0;                 // unkown
    stream << (Q_UINT32) 0;                 // unkown
    stream << (Q_UINT32) 0;                 // unkown
    stream << (Q_UINT32) 0;                 // unkown
    stream << (Q_UINT32) 0x02;              // unknown
    stream << (Q_UINT64) dbid; // same unique id as above
    for( int i= 0; i< 17; i++)
        stream << (Q_UINT32) 0;
    
    /** Write Track contents **/
    Q_UINT32 num_mhods = 0;
    for( PropertyMap::const_iterator element= properties.begin(); element!= properties.end(); ++element) {
        if( (*element).isEmpty())
            continue;

        const char *data= (const char *)(*element).ucs2();
        if( data == NULL)
            continue;

        int datalen= 2* (*element).length();

        stream << (Q_UINT32) 0x646F686D;    // mhod
        stream << (Q_UINT32) 0x18;    // headerlen
        stream << (Q_UINT32) 40+ datalen;
        stream << (Q_UINT32) element.key();
        stream << (Q_UINT32) 0;
        stream << (Q_UINT32) 0;
        stream << (Q_UINT32) 1;    // dummy - would refer to the trackID if used in playlist
        stream << (Q_UINT32) datalen;
        stream << (Q_UINT32) 0;
        stream << (Q_UINT32) 0;
        stream.writeRawBytes( data, datalen);
        num_mhods++;
    }
    buffer.at( 8);
    stream << (Q_UINT32)data.size();	// set content length
    stream << (Q_UINT32)num_mhods;	// set real mhod count
    buffer.close();
}
Exemplo n.º 12
0
int main(int argc, char* argv[])
{
    char hostName[200] = DEFAULT_HOSTNAME;
    int port = DEFAULT_RESMGR_TPM_PORT;

    TPMI_DH_OBJECT keyHandle;
    BYTE *msg = NULL;
    UINT16 length = 0;
    UINT16 size = 0;
    long fileSize = 0;

    TPMT_TK_HASHCHECK validation;
    TPMI_ALG_HASH halg;
    char outFilePath[PATH_MAX] = {0};
    char inMsgFileName[PATH_MAX] = {0};
    char *contextKeyFile = NULL;

    setbuf(stdout, NULL);
    setvbuf (stdout, NULL, _IONBF, BUFSIZ);

    int opt = -1;
    const char *optstring = "hvk:P:g:m:t:s:p:d:c:";
    static struct option long_options[] = {
      {"help",0,NULL,'h'},
      {"version",0,NULL,'v'},
      {"keyHandle",1,NULL,'k'},
      {"pwdk",1,NULL,'P'},
      {"halg",1,NULL,'g'},
      {"msg",1,NULL,'m'},
      {"sig",1,NULL,'s'},
      {"ticket",1,NULL,'t'},
      {"port",1,NULL,'p'},
      {"debugLevel",1,NULL,'d'},
      {"keyContext",1,NULL,'c'},
      {0,0,0,0}
    };

    int returnVal = 0;
    int flagCnt = 0;
    int h_flag = 0,
        v_flag = 0,
        k_flag = 0,
        P_flag = 0,
        g_flag = 0,
        m_flag = 0,
        t_flag = 0,
        c_flag = 0,
        s_flag = 0;

    if(argc == 1)
    {
        showHelp(argv[0]);
        return 0;
    }

    while((opt = getopt_long(argc,argv,optstring,long_options,NULL)) != -1)
    {
        switch(opt)
        {
        case 'h':
            h_flag = 1;
            break;
        case 'v':
            v_flag = 1;
            break;
        case 'k':
            if(getSizeUint32Hex(optarg,&keyHandle) != 0)
            {
                returnVal = -1;
                break;
            }
            k_flag = 1;
            break;
        case 'P':
            sessionData.hmac.t.size = sizeof(sessionData.hmac.t) - 2;
            if(str2ByteStructure(optarg,&sessionData.hmac.t.size,sessionData.hmac.t.buffer) != 0)
            {
                returnVal = -2;
                break;
            }
            P_flag = 1;
            break;
        case 'g':
            if(getSizeUint16Hex(optarg,&halg) != 0)
            {
                showArgError(optarg, argv[0]);
                returnVal = -3;
                break;
            }
            printf("halg = 0x%4.4x\n", halg);
            g_flag = 1;
            break;
        case 'm':
            safeStrNCpy(inMsgFileName, optarg, sizeof(inMsgFileName));
            m_flag = 1;
            break;
        case 't':
            size = sizeof(validation);
            if(loadDataFromFile(optarg, (UINT8 *)&validation, &size) != 0)
            {
                returnVal = -4;
                break;
            }
            t_flag = 1;
            break;
        case 's':
            safeStrNCpy(outFilePath, optarg, sizeof(outFilePath));
            if(checkOutFile(outFilePath) != 0)
            {
                returnVal = -5;
                break;
            }
            s_flag = 1;
            break;
        case 'p':
            if( getPort(optarg, &port) )
            {
                printf("Incorrect port number.\n");
                returnVal = -6;
            }
            break;
        case 'd':
            if( getDebugLevel(optarg, &debugLevel) )
            {
                printf("Incorrect debug level.\n");
                returnVal = -7;
            }
            break;
        case 'c':
            contextKeyFile = optarg;
            if(contextKeyFile == NULL || contextKeyFile[0] == '\0')
            {
                returnVal = -8;
                break;
            }
            printf("contextKeyFile = %s\n", contextKeyFile);
            c_flag = 1;
            break;
        case ':':
//              printf("Argument %c needs a value!\n",optopt);
            returnVal = -9;
            break;
        case '?':
//              printf("Unknown Argument: %c\n",optopt);
            returnVal = -10;
            break;
        //default:
        //  break;
        }
        if(returnVal)
            break;
    };

    if(returnVal != 0)
        goto end;

    if(m_flag)
    {
        if(getFileSize(inMsgFileName, &fileSize))
        {
            returnVal = -11;
            goto end;
        }
        if(fileSize == 0)
        {
            printf("the message file is empty !\n");
            returnVal = -12;
            goto end;
        }
        if(fileSize > 0xffff)
        {
            printf("the message file was too long !\n");
            returnVal = -13;
            goto end;
        }
        msg = (BYTE*)malloc(fileSize);
        if(msg == NULL)
        {
            returnVal = -14;
            goto end;
        }
        memset(msg, 0, fileSize);

        length = fileSize;
        if(loadDataFromFile(inMsgFileName, msg, &length) != 0)
        {
            returnVal = -15;
            goto end;
        }
#if 0
        printf("\nmsg length: %d\n",length);
        printf("msg content: ");
        for(int i = 0; i < length; i++)
        {
            printf("%02x ", msg[i]);
        }
        printf("\n");
        return -1;
#endif
    }

    if(P_flag == 0)
        sessionData.hmac.t.size = 0;
    if(t_flag == 0)
    {
        validation.tag = TPM_ST_HASHCHECK;
        validation.hierarchy = TPM_RH_NULL;
        validation.digest.t.size = 0;
    }

    flagCnt = h_flag + v_flag + k_flag + g_flag + m_flag + s_flag + c_flag;

    if(flagCnt == 1)
    {
        if(h_flag == 1)
            showHelp(argv[0]);
        else if(v_flag == 1)
            showVersion(argv[0]);
        else
        {
            showArgMismatch(argv[0]);
            returnVal = -16;
        }
    }
    else if((flagCnt == 4) && (k_flag == 1 || c_flag == 1) && (g_flag == 1) && (m_flag == 1) && (s_flag == 1))
    {
        prepareTest(hostName, port, debugLevel);

        if(c_flag)
            returnVal = loadTpmContextFromFile(sysContext, &keyHandle, contextKeyFile);
        if(returnVal == 0)
            returnVal = sign(keyHandle, halg, msg, length, &validation, outFilePath);

        finishTest();

        if(returnVal)
            returnVal = -17;
    }
    else
    {
        showArgMismatch(argv[0]);
        returnVal = -18;
    }

end:
    if(msg)
        free(msg);
    return returnVal;
}
Exemplo n.º 13
0
void initConnection(char *empfIP,char *port, char* file, int fenstergroesse){
	struct request nachricht;
	char* reqChar;
	int retSend, reqLen,test;
	unsigned long addr;
	struct hostent *host_info;

	WSADATA wsaData;
	WORD wVersionRequested;
	FILE *fp;

	//komplett von http://msdn.microsoft.com/de-de/library/windows/desktop/ms738630(v=vs.85).aspx übernommen
	int RetVal,testi,AddrLen;
	ADDRINFO Hints, *AddrInfo;
	char AddrName[NI_MAXHOST];
	struct sockaddr_storage Addr;
	//komplett von http://msdn.microsoft.com/de-de/library/windows/desktop/ms738630(v=vs.85).aspx übernommen


	wVersionRequested = MAKEWORD(2,1);
	if( WSAStartup( wVersionRequested,&wsaData ) == SOCKET_ERROR ){
		printf( "Error: WSAStartup() throws error nr. %d!\n" ,WSAGetLastError());
		exit(-1);
	}

	//komplett von http://msdn.microsoft.com/de-de/library/windows/desktop/ms738630(v=vs.85).aspx übernommen
	memset(&Hints, 0, sizeof (Hints));
    Hints.ai_family = AF_INET6;
    Hints.ai_socktype = SOCK_DGRAM;
    RetVal = getaddrinfo(empfIP, port, &Hints, &AddrInfo);
    if (RetVal != 0) {
        fprintf(stderr,
                "Cannot resolve address [%s] and port [%s], error %d: %s\n",
                empfIP, port, RetVal, gai_strerror(RetVal));
        WSACleanup();
        exit(-1);
    }
	//
    // Try each address getaddrinfo returned, until we find one to which
    // we can successfully connect.
    //
	for (AI = AddrInfo; AI != NULL; AI = AI->ai_next) {
		// Open a socket with the correct address family for this address.
		ConnSocket = socket(AI->ai_family, AI->ai_socktype, AI->ai_protocol);
		printf("socket call with family: %d socktype: %d, protocol: %d\n",
               AI->ai_family, AI->ai_socktype, AI->ai_protocol);
        if (ConnSocket == INVALID_SOCKET)
            printf("socket call failed with %d\n", WSAGetLastError());
		 if (ConnSocket == INVALID_SOCKET) {
            fprintf(stderr, "Error Opening socket, error %d\n",
                    WSAGetLastError());
            continue;
        }
		  printf("Attempting to connect to: %s\n", empfIP ? empfIP : "localhost");
        if (connect(ConnSocket, AI->ai_addr, (int) AI->ai_addrlen) != SOCKET_ERROR)
            break;

        testi = WSAGetLastError();
        if (getnameinfo(AI->ai_addr, (int) AI->ai_addrlen, AddrName,
                        sizeof (AddrName), NULL, 0, NI_NUMERICHOST) != 0)
            strcpy_s(AddrName, sizeof (AddrName), UNKNOWN_NAME);
        fprintf(stderr, "connect() to %s failed with error %d\n",
                AddrName, testi);
        closesocket(ConnSocket);
    }

	 if (AI == NULL) {
        fprintf(stderr, "Fatal error: unable to connect to the server.\n");
        WSACleanup();
        exit(-1);
    }
	 AddrLen = sizeof (Addr);
    if (getpeername(ConnSocket, (LPSOCKADDR) & Addr, (int *) &AddrLen) == SOCKET_ERROR) {
        fprintf(stderr, "getpeername() failed with error %d\n",
                WSAGetLastError());
    } else {
        if (getnameinfo((LPSOCKADDR) & Addr, AddrLen, AddrName,
                        sizeof (AddrName), NULL, 0, NI_NUMERICHOST) != 0)
            strcpy_s(AddrName, sizeof (AddrName), UNKNOWN_NAME);
        printf("Connected to %s, port %d, protocol %s, protocol family %s\n",
               AddrName, ntohs(SS_PORT(&Addr)),
               (AI->ai_socktype == SOCK_STREAM) ? "TCP" : "UDP",
               (AI->ai_family == PF_INET) ? "PF_INET" : "PF_INET6");
    }
	//komplett von http://msdn.microsoft.com/de-de/library/windows/desktop/ms738630(v=vs.85).aspx übernommen

	
	//schicke erste nachricht
	nachricht.FlNr = (long) fenstergroesse; //fenstergröße
	if(file != NULL) memcpy(nachricht.fname,file,sizeof(nachricht.fname));//dateiname
	nachricht.fname[strlen(file)] = '\0';
	nachricht.ReqType = ReqHello;//nachrichtentyp
	nachricht.name[0] = '\0';

	fp = openFile(file);
	nachricht.SeNr = getFileSize();//größe der datei
	closeFile(fp);
	
	//umwandeln des struct request in char*
	reqChar = (char*) malloc(sizeof(nachricht));
	memcpy(reqChar,&nachricht,sizeof(nachricht));
	sizeof(nachricht);
	UserInformation(2,&nachricht,NULL);

	//senden der ersten nachricht
	retSend = sendto(ConnSocket,reqChar,sizeof(nachricht),0,AI->ai_addr, (int) AI->ai_addrlen);

	//auswerten des returnwertes von send
	if(retSend != sizeof(nachricht)){
		printf("Error: Es wurden nicht alle Daten versand!");
	}
	
}
Exemplo n.º 14
0
void MapperGui::widgetHidden(const gcn::Event& event){
    // create a new map
    if( event.getSource() == loadMapBox ){
        if( loadMapBox->isInfoReady() ){
            //release old World (or create if NULL ptr)
            if( world != NULL ){
                notify("Releasing old world from memory");
                world->unload();
            } else {
                notify("Making a new AreaMap object");
                world = new AreaMap;
            }

            //make the new layers
            notify("Allocating layers...");
            world->setSize(6);
            for( size_t i = 0; i < world->size(); ++i ){
                world->getLayer(i).setSize( loadMapBox->getMapWidth(), loadMapBox->getMapHeight() );
            }


            // set other properties
            notify("Setting Properties");
            world->setFilename( loadMapBox->getFilename() );
            world->setMapName( loadMapBox->getMapName() );
            world->setExclusive( loadMapBox->isExclusive() );
            for( size_t i = 0; i < world->size(); ++i ){
                world->getLayer(i).setTileWidth( loadMapBox->getTileWidth() );
                world->getLayer(i).setTileHeight( loadMapBox->getTileHeight() );
            }
            unNotify();

            // free old surface
            if( worldImage != NULL ){
                SDL_FreeSurface(worldImage);
                worldImage = NULL;
            };
            //set the info as un usable  now
            loadMapBox->infoTaken();

            mapDetailsBox->setInfoSource( world );
        }
    } else



    // from the filename widget
    if( event.getSource() == filenameBox ){
        if( filenameBox->isInfoReady() ){
            std::string filename = correctFilepath(filenameBox->getFilename());
            switch( action ){
                case SAVEAS:
                    if( world != NULL ){
                        if ( world->size() == 0 ){
                            alert("Theres no map to save (try the new map button =P)");
                        } else if( (filename != "") && !filename.empty() ){
                            if( !world->saveMap(filename) ){
                                alert( ("Cant save file: " + filename) );
                            } else {
                                alert("Saved");
                            }
                        }
                    } else {
                        alert("Theres no map to save (try the new map button =P)");
                    }
                    action = NOTHING;
                    filenameBox->infoTaken();
                    break;

                case OPEN:
                    if( world == NULL ){ world = new AreaMap; }
                    if( !world->loadMap(filename) ){
                        alert( ("Cant open file: " + filename) );
                    } else {
                        //  free old worldImage
                        if( worldImage != NULL ){
                            SDL_FreeSurface(worldImage);
                            worldImage = NULL;
                        };
                    }
                    action = NOTHING;
                    filenameBox->infoTaken();
                    break;

                case NOTHING:
                    break;

                default:
                    action = NOTHING;
                    break;
            }
            mapDetailsBox->setInfoSource( world );
        }
    } else


    //trying to make a new tileste
    if( event.getSource() == makeTilesetBox ){
        if( makeTilesetBox->isInfoReady() ){
            //  check the save filename
            if( getFileSize(correctFilepath(makeTilesetBox->getSaveFilename())) ){
                notify("File already exists. If you save, old tileset def will be overwritten");
            }

            //set the info as un usable  now
            makeTilesetBox->infoTaken();
        }
    }

    mapTouched = 1;
}
void unpackStringsShock(char filePath[255])
{//this looks familiar.
//long BlockNo;
unsigned char *tmp_ark ;
long chunkPackedLength;
long chunkUnpackedLength;
	//char *filePath = SHOCK_STRINGS_FILE;
	FILE *file = NULL;      // File pointer
	
     if (fopen_s(&file,filePath, "rb") != 0)
		{
        printf("Could not open specified file\n");
        }
    else
		{

	 
		// Get the size of the file in bytes
		long fileSize = getFileSize(file);
	 
		// Allocate space in the tmp_ark for the whole file
	    
		tmp_ark = new unsigned char[fileSize];
		fread(tmp_ark, fileSize, 1,file);
		fclose(file);
		
		int blnLevelFound =0;
		long DirectoryAddress=getValAtAddress(tmp_ark,124,32);
		//printf("\nThe directory is at %d\n", DirectoryAddress);
		
		long NoOfChunks = getValAtAddress(tmp_ark,DirectoryAddress,16);
		//printf("there are %d chunks\n",NoOfChunks);
		long firstChunkAddress = getValAtAddress(tmp_ark,DirectoryAddress+2,32);
		//printf("The first chunk is at %d\n", firstChunkAddress);
		long address_pointer=DirectoryAddress+6;
		long AddressOfBlockStart= firstChunkAddress;
		for (int k=0; k< NoOfChunks; k++)
			{
			long chunkId = getValAtAddress(tmp_ark,address_pointer,16);
			chunkUnpackedLength =getValAtAddress(tmp_ark,address_pointer+2,24);
			long chunkType = getValAtAddress(tmp_ark,address_pointer+5,8);
			chunkPackedLength = getValAtAddress(tmp_ark,address_pointer+6,24);
			long chunkContentType = getValAtAddress(tmp_ark,address_pointer+9,8);
			//printf("Index: %d, Chunk %d, Unpack size %d, compression %d, packed size %d, content type %d\t",k,chunkId, chunkUnpackedLength, chunkType,chunkPackedLength,chunkContentType);
			//printf("Absolute address is %d\n",AddressOfBlockStart);
			long NoSubChunks = getValAtAddress(tmp_ark,AddressOfBlockStart,16);
			printf("\nChunk %d has %d sub chunks",k, NoSubChunks);
			printf("\n+=====================================+\n");
			long strPtr=2;
			for (int i = 0; i<NoSubChunks;i++)
				{
				long subChunkStart =AddressOfBlockStart+ getValAtAddress(tmp_ark,AddressOfBlockStart+strPtr,32);
				long subChunkEnd = AddressOfBlockStart+getValAtAddress(tmp_ark,AddressOfBlockStart+strPtr+4,32);
				if (subChunkEnd > subChunkStart)
					{
					printf("%d = %.*s\n",i, subChunkEnd- subChunkStart, tmp_ark + subChunkStart);			
					}
				strPtr+=4;
				}
		
			AddressOfBlockStart=AddressOfBlockStart+ chunkPackedLength;
			if ((AddressOfBlockStart % 4) != 0)
				AddressOfBlockStart = AddressOfBlockStart + 4 - (AddressOfBlockStart % 4); // chunk offsets always fall on 4-byte boundaries
			

			address_pointer=address_pointer+10;			
			}
		}	

	
}
Exemplo n.º 16
0
long long SQLiteFileSystem::getDatabaseFileSize(const String& fileName)
{        
    long long size;
    return getFileSize(fileName, size) ? size : 0;
}
void unpackStrings(int game)
{
char filePath[255];
switch (game)
	{
	case UWDEMO:
	case UW1:
		strcpy_s(filePath, UW1_STRINGS_FILE);break;	
	case UW2:
		strcpy_s(filePath, UW2_STRINGS_FILE);break;	
	case SHOCK:
		strcpy_s(filePath, SHOCK_STRINGS_FILE);break;	
	}
huffman_node *hman;
block_dir *blocks;
unsigned char *Buffer;
long NoOfNodes; long NoOfStringBlocks;
long address_pointer=0;
//char *str;

	FILE *file = NULL;      // File pointer
	//fopen_s(file,filePath, "rb");
    if (fopen_s(&file,filePath, "rb") != 0)
        {
        printf("Could not open specified file\n");
        }
	else
		{
		// Get the size of the file in bytes
		long fileSize = getFileSize(file);
	 
		// Allocate space in the buffer for the whole file
	    
		Buffer = new unsigned char[fileSize];
		fread(Buffer, fileSize, 1,file);
		fclose(file);
		NoOfNodes=getValAtAddress(Buffer,address_pointer,16);
		int i=0;
		hman = new huffman_node [NoOfNodes];
		address_pointer=address_pointer+2;
		while (i<NoOfNodes)
			{
			hman[i].symbol= Buffer[address_pointer+0];
			hman[i].parent= Buffer[address_pointer+1];
			hman[i].left= Buffer[address_pointer+2];
			hman[i].right= Buffer[address_pointer+3];
			printf("Node:%d parent=%d, left=%d, right=%d, symbol=%c\n",i,hman[i].parent, hman[i].left, hman[i].right, hman[i].symbol);
			i++;
			address_pointer=address_pointer+4;
			}
		//next is the number of string blocks
		NoOfStringBlocks=getValAtAddress(Buffer,address_pointer,16);
		blocks=new block_dir[NoOfStringBlocks];
		address_pointer=address_pointer+2;
		i=0;
		while (i<NoOfStringBlocks)
			{
			blocks[i].block_no = getValAtAddress(Buffer,address_pointer,16);
			address_pointer=address_pointer+2;
			blocks[i].address = getValAtAddress(Buffer,address_pointer,32);	
			address_pointer=address_pointer+4;
			blocks[i].NoOfEntries = getValAtAddress(Buffer,blocks[i].address,16);	//look ahead and get no of entries.
			
			i++;
			}
		i=0;	
		while (i<NoOfStringBlocks)
			{
			//printf("Block %d is at address %d. It has %d entries\n",blocks[i].block_no,blocks[i].address, blocks[i].NoOfEntries );
			address_pointer=2 + blocks[i].address + blocks[i].NoOfEntries *2;
			//printf("It's strings begin at %d\n", address_pointer);
			//printf("It should end at %d\n",blocks[i+1].address );
			printf("\n+=====================================+\n");
			printf("Block Name: %d\n", blocks[i].block_no);
			long strAdd;
			int blnFnd;
			strAdd= address_pointer;
			for (int j=0;j< blocks[i].NoOfEntries;j++)
			{
				//Based on abysmal /uwadv implementations.
				blnFnd=0;
				char c;
				
				int bit = 0;
				int raw = 0;
				int node=0;
				do {
					node = NoOfNodes - 1; // starting node

					// huffman tree decode loop
					while (char(hman[node].left) != -1
						&& char(hman[node].right) != -1)
					{

						if (bit == 0) {
							bit = 8;
							raw = Buffer[address_pointer++];	//stream.get<uint8_t>();
						}

						// decide which node is next
						node = raw & 0x80 ? short (hman[node].right)
							   : short (hman[node].left);

						raw <<= 1;
						bit--;
					}

					// have a new symbol
					if ((hman[node].symbol !='|') && (hman[node].symbol !=10)){
						if (blnFnd==0)
							//{printf("\nBlock %d String %d at %d:",blocks[i].block_no, j, strAdd);	}
							{printf("\n%03d=",j);	}
						printf("%c",hman[node].symbol);
						blnFnd = 1;
					}
				} while (hman[node].symbol != '|');		
			}
			i++;
			}	
		}
}
Exemplo n.º 18
0
/* Calculate the length of the POST.
   Force chunked data transfer if size of files can't be obtained.
 */
void ResourceHandleManager::setupPOST(ResourceHandle* job, struct curl_slist** headers)
{
    ResourceHandleInternal* d = job->getInternal();
    Vector<FormDataElement> elements;
    // Fix crash when httpBody is null (see bug #16906).
    if (job->request().httpBody())
        elements = job->request().httpBody()->elements();
    size_t numElements = elements.size();

    if (!numElements)
        return;

    // Do not stream for simple POST data
    if (numElements == 1) {
        job->request().httpBody()->flatten(d->m_postBytes);
        if (d->m_postBytes.size() != 0) {
            curl_easy_setopt(d->m_handle, CURLOPT_POST, TRUE);
            curl_easy_setopt(d->m_handle, CURLOPT_POSTFIELDSIZE, d->m_postBytes.size());
            curl_easy_setopt(d->m_handle, CURLOPT_POSTFIELDS, d->m_postBytes.data());
        }
        return;
    }

    // Obtain the total size of the POST
#if COMPILER(MSVC)
    // work around compiler error in Visual Studio 2005.  It can't properly
    // handle math with 64-bit constant declarations.
#pragma warning(disable: 4307)
#endif
    static const long long maxCurlOffT = (1LL << (sizeof(curl_off_t) * 8 - 1)) - 1;
    curl_off_t size = 0;
    bool chunkedTransfer = false;
    for (size_t i = 0; i < numElements; i++) {
        FormDataElement element = elements[i];
        if (element.m_type == FormDataElement::encodedFile) {
            long long fileSizeResult;
            if (getFileSize(element.m_filename, fileSizeResult)) {
                if (fileSizeResult > maxCurlOffT) {
                    // File size is too big for specifying it to cURL
                    chunkedTransfer = true;
                    break;
                }
                size += fileSizeResult;
            } else {
                chunkedTransfer = true;
                break;
            }
        } else
            size += elements[i].m_data.size();
    }

    curl_easy_setopt(d->m_handle, CURLOPT_POST, TRUE);

    // cURL guesses that we want chunked encoding as long as we specify the header
    if (chunkedTransfer)
        *headers = curl_slist_append(*headers, "Transfer-Encoding: chunked");
    else
        curl_easy_setopt(d->m_handle, CURLOPT_POSTFIELDSIZE_LARGE, size);

    curl_easy_setopt(d->m_handle, CURLOPT_READFUNCTION, readCallback);
    curl_easy_setopt(d->m_handle, CURLOPT_READDATA, job);
}
Exemplo n.º 19
0
	bool CBR5Model::loadFromFile(std::string fileName)
	{
		/* ***************** 1. load the data ***************** */

		FILE *br5 = fopen(fileName.c_str(),"rb");

		if (!br5)
		{
			return false;
		}

		long fileSize = getFileSize(br5);

		byte *data = new byte[fileSize];
		byte *tmp = data;

		// read the full model data
		size_t read = fread(data,sizeof(byte),fileSize,br5);

		if (read!=fileSize)
		{
			return false;
		}

		// read the mark
		char mark[3];
		memcpy(mark,data,3);
		data+=3;

		if (mark[0]!='B' || mark[1]!='R' || mark[2]!='5')
		{
			// file is not a valid br5 model
			fclose(br5);
			return false;
		}

		// read texture name
		memcpy(textureName,data,256);
		data+=256;

		// read number of vertices
		vertexCount = *(unsigned int*)data;
		data+=4;

		textureCoordinates = new float[vertexCount*2];

		// read texture coordinates
		memcpy(textureCoordinates,data,sizeof(float)*vertexCount*2);
		data+=sizeof(float)*vertexCount*2;

		// read number of frames
		framesCount = *(unsigned int*)data;
		data+=4;

		if (framesCount==1)
		{
			// non animated version

			// read vertices
			vertexCoordinates = new float[vertexCount*3];
			memcpy(vertexCoordinates,data,sizeof(float)*vertexCount*3);
			data+=sizeof(float)*vertexCount*3;
		}
		else
		{
			// read vertices
			/*vertexCoordinates = new float[vertexCount*3];
			memcpy(vertexCoordinates,data,sizeof(float)*vertexCount*3);
			data+=sizeof(float)*vertexCount*3;*/

			// read other anims
			vertexCoordinatesAnim = new float*[framesCount];

			for (unsigned int i=0; i<framesCount; i++)
			{
				vertexCoordinatesAnim[i] = new float[vertexCount*3];
				memcpy(vertexCoordinatesAnim[i],data,sizeof(float)*vertexCount*3);
				data+=sizeof(float)*vertexCount*3;
			}

			vertexCoordinates = new float[vertexCount*3];

			memcpy(vertexCoordinates,vertexCoordinatesAnim[0],sizeof(float)*vertexCount*3);
		}

		fclose(br5);

		data = tmp;
		delete [] data;

		startFrame = 0;
		endFrame = framesCount-1;


		/* ***************** 3. setup the VBO ***************** */

		vbo = new CVBO(framesCount==1?CVBO::BT_STATIC_DRAW:CVBO::BT_STREAM_DRAW,false);
		vbo->setVertexData(vertexCount,3,sizeof(GLfloat),vertexCoordinates,CVBO::DT_FLOAT);
		vbo->setEnumMode(CVBO::EM_TRIANGLES);

		setupTexture();

		// calculate the bounding box
		boundingBox.reset();
		for (unsigned int v=0; v<vertexCount*3; v+=3)
		{
			boundingBox.update(vertexCoordinates+v);
		}
		boundingBox.calculateExtents();

		return true;
	}
Exemplo n.º 20
0
int main()
{
    PARCHIVE archive = malloc(sizeof(ARCHIVE));
    memset(archive, 0, sizeof(ARCHIVE));

    PFILE_NODE curr;

    FILE *fp = fopen(ARCHIVE_NAME, "r+b");    // 아카이브 파일을 읽기/쓰기 모드로 열기
    if (fp == NULL)                           // 아카이브 파일이 없으면
    {
        fp = fopen(ARCHIVE_NAME, "w+b");      // 아카이브 파일을 생성
        if (fp == NULL)                       // 파일 생성(열기)에 실패하면
            return 0;                         // 프로그램 종료

        // 새 아카이브 헤더 생성
        archive->header.magic = 'AF';         // 매직 넘버 AF 저장(리틀 엔디언에서는 FA로 저장됨)
        archive->header.version = 1;          // 파일 버전 1 저장

        // 아카이브 파일에 아카이브 헤더 저장
        if (fwrite(&archive->header, sizeof(ARCHIVE_HEADER), 1, fp) < 1)
        {
            printf("아카이브 헤더 쓰기 실패\n");
            fclose(fp);
            return 0;
        }
    }
    else    // 아카이브 파일이 있으면
    {
        // 아카이브 파일에서 아카이브 헤더 읽기
        if (fread(&archive->header, sizeof(ARCHIVE_HEADER), 1, fp) < 1)
        {
            printf("아카이브 헤더 읽기 실패\n");
            fclose(fp);
            return 0;
        }
    }

    // 아카이브 파일 매직 넘버 검사
    if (archive->header.magic != 'AF')
    {
        printf("아카이브 파일이 아닙니다.\n");
        fclose(fp);
        return 0;
    }

    // 아카이브 파일 버전 검사
    if (archive->header.version != 1)
    {
        printf("버전이 맞지 않습니다.\n");
        fclose(fp);
        return 0;
    }

    archive->fp = fp;    // 아카이브 파일 포인터 저장

    int ret = 0;
    uint32_t size = getFileSize(fp);    // 아카이브 파일의 크기를 구함
    uint32_t currPos = ftell(fp);       // 현재 파일 포인터의 위치를 구함

    while (size > currPos)         // 파일 포인터의 위치가 파일 크기보다 작을 때 반복
    {
        PFILE_NODE node = malloc(sizeof(FILE_NODE));
        memset(node, 0, sizeof(FILE_NODE));

        // 파일 정보 읽기
        if (fread(&node->desc, sizeof(FILE_DESC), 1, fp) < 1)
        {
            printf("아카이브 파일 읽기 실패\n");
            free(node);       // 동적 메모리 해제
            ret = -1;         // -1은 실패
            goto FINALIZE;    // 모든 동적 메모리 해제 코드로 이동
        }

        // 연결 리스트에 파일 정보 노드(FILE_NODE) 추가
        node->next = archive->fileList.next;
        archive->fileList.next = node;

        // 현재 파일 포인터의 위치에 파일 크기를 더하여 다음 파일 정보 위치로 이동
        currPos = ftell(fp) + node->desc.size;
        fseek(fp, currPos, SEEK_SET);
    }

    extract(archive, "hello.txt");    // hello.txt 파일 추출

FINALIZE:
    // 파일 목록 연결 리스트를 순회하면서 메모리 해제
    curr = archive->fileList.next;    // 첫 번째 노드
    while (curr != NULL)
    {
        PFILE_NODE next = curr->next;
        free(curr);

        curr = next;
    }

    fclose(archive->fp);    // 아카이브 파일 포인터 닫기

    free(archive);    // 아카이브 메인 구조체 해제

    return ret;       // 성공이냐 실패냐에 따라 0 또는 -1을 반환
}
Exemplo n.º 21
0
/**
 * Starts a new process for debugging.
 *
 * @param path The path to the executable of the process.
 * @param tids The thread IDs of the threads that belong to the target process.
 *
 * @return A NaviError code that describes whether the operation was successful or not.
 */
NaviError LinuxSystem::startProcess(
    const NATIVE_STRING path,
    const std::vector<const NATIVE_STRING>& commands) {


  pid_t pid = fork();

  if (pid == -1) {
    msglog->log(LOG_ALWAYS, "Error: Couldn't fork process");
    return NaviErrors::COULDNT_OPEN_TARGET_PROCESS;
  } else if (pid == 0) {
    ptrace(PTRACE_TRACEME, 0, 0, 0);

    char** child_arguments = new char*[1 + commands.size() + 1];
    child_arguments[0] = new char[strlen(path) + 1];
    strcpy(child_arguments[0], path);

    for (unsigned int i = 0; i < commands.size(); i++) {
      child_arguments[i + 1] = new char[strlen(commands[i]) + 1];
      strcpy(child_arguments[i + 1], commands[i]);
    }

    child_arguments[1 + commands.size()] = 0;

    // Child process
    if (execvp(path, child_arguments) == -1) {
      msglog->log(LOG_ALWAYS, "Error: Could not start the child process '%s'",
                  path);
      exit(0);
    }

    return NaviErrors::SUCCESS;
  } else {
    int status;
    if (waitpid(pid, &status, __WALL) == -1) {
      msglog->log(LOG_ALWAYS, "Error: Wait for target process failed '%s'",
                  path);
      exit(0);
    }

    if (WIFSTOPPED(status)) {
      if (WSTOPSIG(status) == SIGTRAP) {
        msglog->log(LOG_VERBOSE, "Initial STOP signal received");
      } else {
        msglog->log(LOG_ALWAYS, "Error: Received unexpected STOP signal");
        exit(0);
      }
    } else {
      msglog->log(LOG_ALWAYS, "Error: Did not receive initial STOP signal");
      exit(0);
    }

    if (ptrace(
        PTRACE_SETOPTIONS,
        pid,
        0,
        PTRACE_O_TRACECLONE | PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK
            | PTRACE_O_TRACEVFORKDONE) == -1) {
      msglog->log(LOG_ALWAYS, "Error: Could not set ptrace options");
      exit(0);
    }

    msglog->log(LOG_VERBOSE, "PID of the child process is %d", pid);

    setPID(pid);
    Thread ts(pid, SUSPENDED);
    tids.push_back(ts);
    setActiveThread(pid);

    lastMapFileSize = getFileSize(
        "/proc/" + zylib::zycon::toString(pid) + "/maps");

    fillModules(pid, modules);
    this->modules = modules;

    std::map<std::string, Module>::const_iterator cit = this->modules.find(
        getTargetApplicationPath().string());
    if (cit != this->modules.end()) {
      Module processModule = cit->second;
      processStart(processModule, ts);
    } else {
      msglog->log(LOG_ALWAYS,
                  "Error: Unable to determine main process module for '%s'",
                  getTargetApplicationPath().string().c_str());
      exit(0);
    }

    return NaviErrors::SUCCESS;
  }
}
Exemplo n.º 22
0
int append(PARCHIVE archive, char *filename)    // 파일 추가 함수 정의
{
    int ret = 0;    // 함수가 성공했는지 실패했는지 반환값으로 표현

    // 추가할 파일 열기
    FILE *fp = fopen(filename, "rb");
    if (fp == NULL)
    {
        printf("%s 파일이 없습니다.\n", filename);
        return -1;    // 함수 종료. -1은 실패
    }

    uint8_t *buffer;
    uint32_t size;

    size = getFileSize(fp);    // 추가할 파일의 크기를 구함
    buffer = malloc(size);

    // 추가할 파일의 내용을 읽음
    if (fread(buffer, size, 1, fp) < 1)
    {
        printf("%s 파일 읽기 실패\n", filename);
        ret = -1;       // -1은 실패
        goto Error1;    // fp를 닫고 buffer를 해제하는 에러 처리로 이동
    }

    // 새 파일 정보 생성
    PFILE_DESC desc = malloc(sizeof(FILE_DESC));
    memset(desc, 0, sizeof(FILE_DESC));
    strcpy(desc->name, filename);    // 파일 정보 구조체에 추가할 파일의 이름 저장
    desc->size = size;               // 파일 정보 구조체에 추가할 파일의 크기 저장

    // 아카이브 헤더 바로 다음으로 파일 포인터를 이동시킴
    fseek(archive->fp, sizeof(ARCHIVE_HEADER), SEEK_SET);

    // 파일 데이터의 시작 위치는 현재 파일 포인터의 위치에 
    // 파일 정보 크기만큼 순방향으로 이동시킨 값
    desc->dataOffset = ftell(archive->fp) + sizeof(FILE_DESC);

    // 아카이브 파일에 새 파일 정보 쓰기
    if (fwrite(desc, sizeof(FILE_DESC), 1, archive->fp) < 1)
    {
        printf("파일 정보 쓰기 실패\n");
        ret = -1;
        goto Error2;    // fp를 닫고, desc와 buffer를 해제하는 에러 처리로 이동
    }

    // 아카이브 파일에 새 파일 데이터 쓰기
    if (fwrite(buffer, size, 1, archive->fp) < 1)
    {
        printf("파일 데이터 쓰기 실패\n");
        ret = -1;
        goto Error2;    // fp를 닫고, desc와 buffer를 해제하는 에러 처리로 이동
    }

    printf("%s 파일 추가 성공\n크기: %d\n", filename, size);

Error2:
    free(desc);      // 파일 정보 저장용 동적 메모리 해제

Error1:
    free(buffer);    // 파일 내용 저장용 동적 메모리 해제

    fclose(fp);      // 파일 포인터 닫기

    return ret;      // 성공이냐 실패냐에 따라 0 또는 -1을 반환
}
Exemplo n.º 23
0
   const int FreeMgr::init( CommBlock &cb, int freemgrType) 
   {
     
       /**
        * Tree has 6 chains, (1,2,4,8,16,32 entries) starting at entry 1 in sb0 of block0
        * List has 3 chains (4,32,1024 entries) starting at first entry of file
        *
        * Starting at Block 1, add each subblock after sb0 to the free list in sb0
        * then go back and build the smaller chains on demand. This way we make sure all SBs
		* are in a chain somewhere.. but this is not best behaviour when allocating blocks
        *
       **/

        int         rc;    // return code from file ops
        DataBlock   blockZero;
        DataBlock   workBlock;
        int         sbIdx;
        uint64_t     blkIdx;
        IdxEmptyListEntry emptyEntry;  // populate the chains with pointer to empty entries
        IdxEmptyListEntry nextPointer; // pointer at end of sub-block of pointers to emptyEntries
        uint64_t     numBlocks; 
        FILE*       indexFile;

        indexFile = cb.file.pFile;

        if (!indexFile) // Make sure that we have non-null filehandle
        {
//             printf("DBG: File handle is null\n");
            return ERR_INVALID_PARAM;
        }
        if (freemgrType!=TREE && freemgrType!=LIST){
//             printf("DBG: Bad type in freeMgr Init\n");
            return ERR_INVALID_PARAM;
        }

        numBlocks = getFileSize( indexFile )/BYTE_PER_BLOCK ;
        
//         printf ("DBG: File size: %lu Total blocks: %llu (%u)\n", getFileSize( indexFile ), numBlocks, BYTE_PER_BLOCK);
//         printf ("DBG: Adding sub-blocks: %llu \n",  numBlocks * 32);

		// Clear the list of pointers in sb0
        initBlockzero( &blockZero );

        // initialize the non varying fields
        nullPtr(&emptyEntry);
        emptyEntry.type = EMPTY_PTR;
        emptyEntry.group = ENTRY_32;

        // nextPointer identifies next sub-block with empty pointers
        // initially there is no next sub-block so zero it out
        nullPtr(&nextPointer);
        nextPointer.type = EMPTY_LIST;
        nextPointer.group = ENTRY_32;
        nextPointer.entry = ENTRY_PER_SUBBLOCK-1;  // last entry on the list.. think of the list as a stack

        if (initType == 0){
            if( isDebug( DEBUG_3 )) { printf("\nOld style init\n"); }
            
        for ( blkIdx = numBlocks-1; blkIdx > 0; blkIdx-- )
        {
            emptyEntry.fbo  = blkIdx;// map fbo to lbid before storage
//             emptyEntry.fbo  = mapLBID( cb, blkIdx, rc );// map fbo to lbid before storage
//             if (rc != NO_ERROR ){ printf("DBG: Error resolving LBID for OID: %u FBO: %llu\n", cb.file.oid, blkIdx-1 ); return rc; }

            if( isDebug( DEBUG_3 )) { cout<<"DBG: Working on block "<<emptyEntry.fbo<<"\n"; }
            memset(workBlock.data, 0, sizeof(workBlock.data));

            /** 
             * each block after zeroth block uses sb0 to store entry list
             * first entry (#0) is the llp to additional sblks with entries
             * entries 1-31 are pointers
             **/
                			
			// sb0 is used for initial map, so start at sb1
            for (sbIdx=1; sbIdx < BYTE_PER_BLOCK/BYTE_PER_SUBBLOCK; sbIdx++)  
            {
                if( isDebug( DEBUG_3 )) { printf("DBG: Working on subblock %u\n", sbIdx); }
                emptyEntry.sbid = sbIdx;
                emptyEntry.type = EMPTY_PTR;
				// store pointer in sb0 -  replace this with a releaseSubblock call
                setSubBlockEntry( workBlock.data, 0, sbIdx, 8, &emptyEntry);  
                if( isDebug( DEBUG_2 )) {
                    cout<<"DBG: Init sb fbo "<<emptyEntry.fbo<<" sbid "<<emptyEntry.sbid<<" entry "<<emptyEntry.entry<<"\n";
                }
            }
			// having stored all pointers, store linkptr
            setSubBlockEntry( workBlock.data, 0, 0, 8, &nextPointer);  
//             nextPointer.fbo = mapLBID( cb, blkIdx, rc ); // remember this block ID
            nextPointer.fbo = blkIdx; // remember this block ID
//             if (rc != NO_ERROR ){ printf("DBG: Error resolving LBID for OID: %u FBO: %llu\n", cb.file.oid, blkIdx-1 ); return rc; }

            rc = writeDBFile( cb, workBlock.data, emptyEntry.fbo );
            if (rc != NO_ERROR){ return rc; }

        }

		// chain for segments of sub block size
        setSubBlockEntry( blockZero.data, 0, calcPtrOffset(ENTRY_32), 8, &nextPointer); 
        
        /**
         * the next algorithm uses the release sub-block method and does not layout the map on particular boundaries.
         **/
        
        } else if (initType  == 1) { // new style - use release 
            if( isDebug( DEBUG_3 )) { printf("\nNew style\n"); }
            
            for ( blkIdx = numBlocks-1; blkIdx > 0; blkIdx-- )
            {
                
                emptyEntry.fbo  = blkIdx;// map fbo to lbid before storage

//                 emptyEntry.fbo  = mapLBID( cb, blkIdx, rc );// map fbo to lbid before storage
//                 if (rc != NO_ERROR ){ printf("DBG: Error resolving LBID for OID: %u FBO: %llu\n", cb.file.oid, blkIdx-1 ); return rc; }

                if( isDebug( DEBUG_3 )) { cout<<"DBG: Working on block "<<emptyEntry.fbo<<"\n"; }
                memset(workBlock.data, 0, sizeof(workBlock.data));
            
                for (sbIdx=BYTE_PER_BLOCK/BYTE_PER_SUBBLOCK-1; sbIdx >-1; sbIdx--)  
                {
                    if( isDebug( DEBUG_3 )) { printf("DBG: Working on subblock %u\n", sbIdx); }
                    emptyEntry.sbid = sbIdx;
                    emptyEntry.type = EMPTY_PTR;
                // store pointer in sb0 -  replace this with a releaseSubblock call
                rc = releaseSubblock( cb, &blockZero, freemgrType, &emptyEntry );
                if (rc != NO_ERROR){ printf("Error releasing sb\n"); return rc; }
                }
            }

            /**
             * the next algorithm uses the release sub-block method and does not layout the map on particular boundaries.
             * It also allows pieces to be allocated
            **/
        

        } else  if (initType  == 2){
                /** The following calls to init accept FBO not LBID..
                 * This makes it easier to work on a range of blocks
                 **/
                // use the first block of the new range for sub-block chain
                // and the rest for block chain
            rc = init( cb, &blockZero,  freemgrType,  ENTRY_32,  1, 50);
            if ( rc != NO_ERROR ) { return rc; }
            rc = init( cb, &blockZero,  freemgrType,  ENTRY_BLK, 51, numBlocks-52);
            if ( rc != NO_ERROR ) { return rc; }

        } 
        // now write sb0 back to disk
        if ( isDebug( DEBUG_2 ))
        { 
            printf("Writing SB0 back to disk\n"); 
            printMemSubBlock( &blockZero, 0 );
        }
        
        uint64_t lbid = mapLBID( cb, 0, rc);
        if (rc != NO_ERROR ){ return rc; }

        rc = writeDBFile( cb, blockZero.data, lbid );
        if (rc != NO_ERROR)
        { 
            return rc; 
        }
       
        return NO_ERROR;

   }
Exemplo n.º 24
0
str
SQLinitClient(Client c)
{
	mvc *m;
	str schema;
	str msg = MAL_SUCCEED;
	backend *be;
	bstream *bfd = NULL;
	stream *fd = NULL;
	static int maybeupgrade = 1;

#ifdef _SQL_SCENARIO_DEBUG
	mnstr_printf(GDKout, "#SQLinitClient\n");
#endif
	if (SQLinitialized == 0 && (msg = SQLprelude(NULL)) != MAL_SUCCEED)
		return msg;
	MT_lock_set(&sql_contextLock);
	/*
	 * Based on the initialization return value we can prepare a SQLinit
	 * string with all information needed to initialize the catalog
	 * based on the mandatory scripts to be executed.
	 */
	if (sqlinit) {		/* add sqlinit to the fdin stack */
		buffer *b = (buffer *) GDKmalloc(sizeof(buffer));
		size_t len = strlen(sqlinit);
		bstream *fdin;

		buffer_init(b, _STRDUP(sqlinit), len);
		fdin = bstream_create(buffer_rastream(b, "si"), b->len);
		bstream_next(fdin);
		MCpushClientInput(c, fdin, 0, "");
	}
	if (c->sqlcontext == 0) {
		m = mvc_create(c->idx, 0, SQLdebug, c->fdin, c->fdout);
		global_variables(m, "monetdb", "sys");
		if (isAdministrator(c) || strcmp(c->scenario, "msql") == 0)	/* console should return everything */
			m->reply_size = -1;
		be = (void *) backend_create(m, c);
	} else {
		be = c->sqlcontext;
		m = be->mvc;
		mvc_reset(m, c->fdin, c->fdout, SQLdebug, NR_GLOBAL_VARS);
		backend_reset(be);
	}
	if (m->session->tr)
		reset_functions(m->session->tr);
	/* pass through credentials of the user if not console */
	schema = monet5_user_set_def_schema(m, c->user);
	if (!schema) {
		_DELETE(schema);
		throw(PERMD, "SQLinitClient", "08004!schema authorization error");
	}
	_DELETE(schema);

	/*expect SQL text first */
	be->language = 'S';
	/* Set state, this indicates an initialized client scenario */
	c->state[MAL_SCENARIO_READER] = c;
	c->state[MAL_SCENARIO_PARSER] = c;
	c->state[MAL_SCENARIO_OPTIMIZE] = c;
	c->sqlcontext = be;

	initSQLreferences();
	/* initialize the database with predefined SQL functions */
	if (SQLnewcatalog == 0) {
		/* check whether table sys.systemfunctions exists: if
		 * it doesn't, this is probably a restart of the
		 * server after an incomplete initialization */
		sql_schema *s = mvc_bind_schema(m, "sys");
		sql_table *t = s ? mvc_bind_table(m, s, "systemfunctions") : NULL;
		if (t == NULL)
			SQLnewcatalog = 1;
	}
	if (SQLnewcatalog > 0) {
		char path[PATHLENGTH];
		str fullname;

		SQLnewcatalog = 0;
		maybeupgrade = 0;
		snprintf(path, PATHLENGTH, "createdb");
		slash_2_dir_sep(path);
		fullname = MSP_locate_sqlscript(path, 1);
		if (fullname) {
			str filename = fullname;
			str p, n;
			fprintf(stdout, "# SQL catalog created, loading sql scripts once\n");
			do {
				p = strchr(filename, PATH_SEP);
				if (p)
					*p = '\0';
				if ((n = strrchr(filename, DIR_SEP)) == NULL) {
					n = filename;
				} else {
					n++;
				}
				fprintf(stdout, "# loading sql script: %s\n", n);
				fd = open_rastream(filename);
				if (p)
					filename = p + 1;

				if (fd) {
					size_t sz;
					sz = getFileSize(fd);
					if (sz > (size_t) 1 << 29) {
						mnstr_destroy(fd);
						msg = createException(MAL, "createdb", "file %s too large to process", filename);
					} else {
						bfd = bstream_create(fd, sz == 0 ? (size_t) (128 * BLOCK) : sz);
						if (bfd && bstream_next(bfd) >= 0)
							msg = SQLstatementIntern(c, &bfd->buf, "sql.init", TRUE, FALSE, NULL);
						bstream_destroy(bfd);
					}
					if (m->sa)
						sa_destroy(m->sa);
					m->sa = NULL;
					if (msg)
						p = NULL;
				}
			} while (p);
			GDKfree(fullname);
		} else
			fprintf(stderr, "!could not read createdb.sql\n");
	} else {		/* handle upgrades */
		if (!m->sa)
			m->sa = sa_create();
		if (maybeupgrade)
			SQLupgrades(c,m);
		maybeupgrade = 0;
	}
	MT_lock_unset(&sql_contextLock);
	fflush(stdout);
	fflush(stderr);

	/* send error from create scripts back to the first client */
	if (msg) {
		error(c->fdout, msg);
		handle_error(m, c->fdout, 0);
		sqlcleanup(m, mvc_status(m));
	}
	return msg;
}
Exemplo n.º 25
0
   const int FreeMgr::assignSubblock( CommBlock &cb, DataBlock* blockZero, const int freemgrType,  IdxEmptyListEntry* assignPtr ) 
   {
       int          rc;    // return code from file ops
       DataBlock    workBlock, tempBlock;
       int          listOffset; // entry in block zero of head pointer
       IdxEmptyListEntry emptyEntry;
       IdxEmptyListEntry emptyPtr, emptyMap;
       IdxEmptyListEntry newBlock;
       uint64_t      numBlocks;
       FILE* indexFile;
               
       indexFile = cb.file.pFile;

       /**
        * Separated subblock assignment out from general segment assignment
        * Reduces the hoops to jump through
        **/
       
       numBlocks = getFileSize( indexFile )/BYTE_PER_BLOCK ;
       if( isDebug( DEBUG_3 )) { printf("DBG: Assign subblock \n"); }
       
       //find the start of the chain
       listOffset = calcPtrOffset( ENTRY_32 );
       
       getSubBlockEntry( blockZero, 0, listOffset, 8, &emptyPtr );
       if( isDebug( DEBUG_2 ))
       {
           cout<<"DBG: EM (start assign) sb fbo "<<emptyPtr.fbo<<" sbid "<<emptyPtr.sbid<<" entry "<<emptyPtr.entry<<"\n";
       }

       if (emptyPtr.type != EMPTY_LIST ){
           return ERR_FM_BAD_TYPE;
       }   
//        if (emptyPtr.fbo > numBlocks)
//        {
//            if( isDebug( DEBUG_1 )) { printf("DBG: Weirdness in assignSubblock.. emptyPtr.fbo > numBlocks\n"); }
//            return ERR_FM_BAD_FBO;
//        }

       // follow the chain to the empty entry
       rc = readDBFile( cb, workBlock.data, emptyPtr.fbo );
       if (rc != NO_ERROR)
       { 
//            printf("DBG: RC Weirdness: rc is %i", rc); 
           return rc; 
       }
           
       getSubBlockEntry( &workBlock, emptyPtr.sbid, emptyPtr.entry, 8, &emptyEntry );
       if( isDebug( DEBUG_2 )) 
       { 
           cout<<"DBG: Next avail sb fbo "<<emptyEntry.fbo<<" sbid "<<" entry "<<emptyEntry.entry<<"\n";
       }

           if (emptyEntry.fbo == 0) // then nowhere to go.. exit
           {
               //if( isDebug( DEBUG_1 )) { printf("DBG: No space in subblock list\n"); }
               if( isDebug( DEBUG_2 )) { 
                   cout<<"DBG: fbo "<<emptyEntry.fbo<<" sbid "<<emptyEntry.sbid<<" entry "<<emptyEntry.entry<<"\n";
               }
               
               //-- try and assign from BLOCK list 
//                printf("Go ask for a block\n");
               rc = assignSegment( cb, blockZero, freemgrType, ENTRY_BLK, &newBlock);

//                rc = extendFreespace( indexFile, blockZero, freemgrType );

               if (rc != NO_ERROR){ 
                   if( isDebug( DEBUG_1 )) { printf("DBG: Could not get block from block list\n"); }
                   return rc;
               }
               // got a block so now split it
               newBlock.entry = 0;
               newBlock.group = ENTRY_32;
               newBlock.type = EMPTY_PTR;
               emptyEntry = newBlock;

               //-- assign almost all sub blocks - keep last one
               int sbIdx;
               for (sbIdx=BYTE_PER_BLOCK/BYTE_PER_SUBBLOCK-1; sbIdx >0; sbIdx--)  
               {
                   if( isDebug( DEBUG_3 )) { cout<<"DBG: Working on fbo "<<newBlock.fbo<<" sbid "<<sbIdx<<"\n"; }
                   emptyEntry.sbid = sbIdx;
                   emptyEntry.type = EMPTY_PTR;
                   rc = releaseSubblock( cb, blockZero, freemgrType, &emptyEntry );
                   if (rc != NO_ERROR)
                   { 
                       printf("DBG: Error releasing sb\n"); 
                       return rc; 
                   }
               }
               emptyEntry.sbid=0;
           }

           if ((emptyEntry.type != EMPTY_LIST) && (emptyEntry.type != EMPTY_PTR))
           {
               if( isDebug( DEBUG_0 )) 
               { 
                   printf("WTF: Bad entry in in subblock list- type is %i (expected %i or %i)\n",  (unsigned int)emptyEntry.type, EMPTY_PTR, EMPTY_LIST); 
               }
               if( isDebug( DEBUG_2 )) 
               {
                   cout<<"DBG: fbo "<<emptyEntry.fbo<<" sbid "<<emptyEntry.sbid<<" entry "<<emptyEntry.entry<<"\n"; 
               }
               return ERR_FM_BAD_TYPE;
           }
           
           if (emptyEntry.type == EMPTY_PTR)
           {
               // this is what we expect normally
               emptyPtr.entry--; // only decrement if we didn't just drain a bucket 
               setSubBlockEntry( blockZero, 0, listOffset, 8, &emptyPtr );
               memcpy(assignPtr, &emptyEntry, 8);

           } else if (emptyEntry.type == EMPTY_LIST && emptyPtr.entry == 0 )
           {
               if (emptyPtr.entry >0) printf("\nWTF!! %i\n", (unsigned int) emptyPtr.entry);
                // reached end of this bucket (should release it for re-use)
               // this is not the typical case..
               if( isDebug( DEBUG_3 )) { 
                   cout<<"DBG: Drained bucket sb fbo "<<emptyPtr.fbo<<" sbid "<<emptyPtr.sbid<<" entry "<<emptyPtr.entry<<"\n";
               }
               //blank the llp
               rc = readDBFile( cb, tempBlock.data, emptyPtr.fbo );
               if (rc != NO_ERROR) { 
                   if (isDebug( DEBUG_1 )){ cout<<"DBG: File error during releaseSubblock, fbo/lbid: "<<emptyPtr.fbo<<"\n"; }
                   return rc; 
               }
               
               nullPtr(&emptyMap); // zero out the entry

               setSubBlockEntry( tempBlock.data, emptyPtr.sbid, 0, 8, &emptyMap );
               rc = writeDBFile( cb, &tempBlock, emptyPtr.fbo );
               if (rc != NO_ERROR){ return rc; }

               memcpy(assignPtr, &emptyPtr, 8);
               assignPtr->type = EMPTY_PTR;
               
               if( isDebug( DEBUG_2 )) { 
                cout<<"DBG: Change head pointer to fbo "<<emptyEntry.fbo<<" sbid "<<emptyEntry.sbid<<" entry "<<emptyEntry.entry<<"\n";
               }
               setSubBlockEntry( blockZero, 0, listOffset, 8, &emptyEntry );
           } else 
           {
               printf("DBG: Weirdness - not list and not ptr\n"); 
           }
//            printf("DBG: Assigned sb fbo %llu sbid %u entry %u\n", assignPtr->fbo, (unsigned int)assignPtr->sbid, (unsigned int)assignPtr->entry);
           
           getSubBlockEntry( blockZero, 0, listOffset, 8, &emptyMap );
           if(isDebug(DEBUG_3)){ 
               cout<<"DBG: EM (sb assign 1) sb fbo "<<emptyMap.fbo<<" sbid "<<emptyMap.sbid<<" entry "<<emptyMap.entry<<"\n";
           }

           blockZero->dirty = 1;
           
           uint64_t count;
           getSubBlockEntry( blockZero, 0, calcStatOffset(ENTRY_32) , 8, &count );
           count--;
           setSubBlockEntry( blockZero, 0, calcStatOffset(ENTRY_32) , 8, &count );

           
           // -- workblock may have changed on disk since it was read.. making the writeDBfile dangerous without a readDBfile first
           nullPtr(&emptyMap); // zero out the entry
           readDBFile( cb, &workBlock, emptyPtr.fbo );
           setSubBlockEntry( workBlock.data, emptyPtr.sbid, emptyPtr.entry+1, 8, &emptyMap );
           rc = writeDBFile( cb, &workBlock, emptyPtr.fbo );
           // --
//            if( isDebug( DEBUG_3 )) { printf("DBG: Assign subblock -- all done\n"); }

           return NO_ERROR;

   }
Exemplo n.º 26
0
void BuildXDataFile(int game)
{

	int right; int left;	//portraits.

	//this looks familiar.
	//long BlockNo;
	unsigned char *tmp_ark;
	long chunkPackedLength;
	long chunkUnpackedLength;
	char *filePath = SHOCK_STRINGS_FILE;
	FILE *file = NULL;      // File pointer

	if (fopen_s(&file, filePath, "rb") != 0)
	{
		printf("Could not open specified file\n");
	}
	else
	{
		// Get the size of the file in bytes
		long fileSize = getFileSize(file);

		// Allocate space in the tmp_ark for the whole file

		tmp_ark = new unsigned char[fileSize];
		fread(tmp_ark, fileSize, 1, file);
		fclose(file);
		
		int blnLevelFound = 0;
		long DirectoryAddress = getValAtAddress(tmp_ark, 124, 32);
		//printf("\nThe directory is at %d\n", DirectoryAddress);

		long NoOfChunks = getValAtAddress(tmp_ark, DirectoryAddress, 16);
		//printf("there are %d chunks\n", NoOfChunks);
		long firstChunkAddress = getValAtAddress(tmp_ark, DirectoryAddress + 2, 32);
		//printf("The first chunk is at %d\n", firstChunkAddress);
		long address_pointer = DirectoryAddress + 6;
		long AddressOfBlockStart = firstChunkAddress;
		for (int k = 0; k< NoOfChunks; k++)
		{
			long chunkId = getValAtAddress(tmp_ark, address_pointer, 16);
			chunkUnpackedLength = getValAtAddress(tmp_ark, address_pointer + 2, 24);
			long chunkType = getValAtAddress(tmp_ark, address_pointer + 5, 8);
			chunkPackedLength = getValAtAddress(tmp_ark, address_pointer + 6, 24);
			long chunkContentType = getValAtAddress(tmp_ark, address_pointer + 9, 8);
			long NoSubChunks = getValAtAddress(tmp_ark, AddressOfBlockStart, 16);
			//printf("\n@Chunk:%d\n{", chunkId);
			if ((chunkId >= 2441) && (chunkId <= 2621))
				{//we have a log.
				int step = 0;
					long strPtr = 2;
					for (int i = 0; i<NoSubChunks; i++)
					{
						long subChunkStart = AddressOfBlockStart + getValAtAddress(tmp_ark, AddressOfBlockStart + strPtr, 32);
						long subChunkEnd = AddressOfBlockStart + getValAtAddress(tmp_ark, AddressOfBlockStart + strPtr + 4, 32);
						if (subChunkEnd > subChunkStart)
						{
							switch (step)
							{
							case 0:
								{//Build the xdata header
								printf("readables/shock/log_%04d\n\t{\n\tprecache\n",chunkId);
								printf("\t\"num_pages\" : \"1\"\n");

								//Info Line : has the following format :
								//[event][colour]LeftId[, []RightId]
								//	event : 'iEE' or 't'
								//	EE = Hex Number of Log / eMail to follow immediately
								//	't' is set for Texts following a 'iEE' Text
								//colour : 'cCC'
								//		 CC = Hex Number of Colour Index in Palette;
								//only Sender and Subject are drawn in this colour
								//	LeftId, RightId:
								//decimal subchunk number of left(and right) bitmaps to show;
								//char *str;
								char *str;
								str=new char[subChunkEnd - subChunkStart];
								for (int x = 0; x < subChunkEnd - subChunkStart; x++)
									{
									str[x] = tmp_ark[subChunkStart + x];
									}
								//sprintf_s(str, "%.*s", subChunkEnd - subChunkStart, tmp_ark + subChunkStart);
							
								//The first line defines who appears in the gui
								//printf(" %.*s\n", subChunkEnd - subChunkStart, tmp_ark + subChunkStart);
								right=-1; left=-1;
								parseInfoLine(str, subChunkEnd - subChunkStart, &left, &right);
								strPtr += 4;
								step++;
								break;
								}
							case 1:
								{
								//Then the title of the document
								printf("\t\"page1_title\" :\n\t\t{\n");
								printf("\t\t\"%.*s\\n\"\n\t\t}", subChunkEnd - subChunkStart, tmp_ark + subChunkStart);
								strPtr += 4;
								step++;
								break;
								}
							case 2:
								{
								//And then the body
								printf("\n\t\"page1_body\" :\n\t\t{\n\t\t\"");
								}//and falls thru
							default:
								{//And the body
								printf("%.*s\\n", subChunkEnd - subChunkStart, tmp_ark + subChunkStart);
								step++;
								strPtr += 4;
								break;
								}
							}
						}
					}
					if (step >= 1)
					{	//Finish out the xdata definition
						printf("\"\n\t\t}\n\t\t\n\t\"gui_page1\"	: \"guis/readables/log.gui\"\n");
						if (left != -1)
						{
							printf("\t\"page1_PortraitLeft\" : \"guis/assets/shock/logs/0040_%04d.tga\"\n", left);
						}
						else
						{
							printf("\t\"page1_PortraitLeft\" : \"guis/assets/shock/logs/0040_0030.tga\"\n");
						}

						if (right != -1)
						{
							printf("\t\"page1_PortraitRight\" : \"guis/assets/shock/logs/0040_%04d.tga\"", right);
						}
						else
						{
							printf("\t\"page1_PortraitRight\" : \"guis/assets/shock/logs/0040_0030.tga\"");
						}
						
						printf("\n}\n\n");
						}
				
				}
			AddressOfBlockStart = AddressOfBlockStart + chunkPackedLength;
			if ((AddressOfBlockStart % 4) != 0)
				AddressOfBlockStart = AddressOfBlockStart + 4 - (AddressOfBlockStart % 4); // chunk offsets always fall on 4-byte boundaries


			address_pointer = address_pointer + 10;
		}
	}


}
Exemplo n.º 27
0
   const int FreeMgr::releaseSubblock( CommBlock &cb, DataBlock* blockZero, const int freemgr_type,  IdxEmptyListEntry* assignPtr ) 
   {
       int         rc;    // return code from file ops
       DataBlock   workBlock;
       DataBlock   extraBlock;
       int          listOffset; // entry in block zero of head pointer
       IdxEmptyListEntry emptyPtr, emptyMap ;
       uint64_t     numBlocks;
       FILE*       indexFile;
               
       indexFile = cb.file.pFile;
       /**
        * Release sub-block - handle de-allocation of only sub-blocks
        * This makes the assign/release code for smaller segments simpler and 
        * separates the tasks of handling the list containers and the list contents
        * When called, we look at the bucket indicated as head of chain and if it full
        * or not present (no room left in chain) then insert the returned SB as a bucket and 
        * move the head pointer
        **/
       
       
       //if( isDebug( DEBUG_1 )) { printf("DBG: releaseSubblock\n"); }
       if( isDebug( DEBUG_2 )) { 
           cout<<"DBG: releasing sb fbo "<<assignPtr->fbo<<" sbid "<<assignPtr->sbid<<" entry "<<assignPtr->entry<<" from "<<((freemgr_type==LIST)?"LIST":"TREE")<<" (type is "<<((assignPtr->type==EMPTY_PTR)?"EMPTY_PTR":"Not EMPTY_PTR")<<")\n";
       }
       if (!assignPtr){
//            printf ("DBG: Bad pointer: assignPtr is zero\n");
           return ERR_INVALID_PARAM;
       }

       if (!blockZero){
           printf ("DBG: Bad pointer: pointer for blockZero is zero\n");
           return ERR_INVALID_PARAM;
       }

       numBlocks = getFileSize( indexFile )/BYTE_PER_BLOCK ;
/*       if (assignPtr->fbo > numBlocks)
       {
       if( isDebug( DEBUG_1 )) { printf("DBG: Weirdness in releaseSubblock.. assignPtr.fbo > numBlocks (%llu %llu)\n", assignPtr->fbo, numBlocks );}
           return ERR_FM_BAD_FBO;
       }*/
       if (assignPtr->type != EMPTY_PTR)
       {
           printf("DBG: Weirdness in releaseSubblock.. tried to return a pointer with type %i (expected %i)\n", (unsigned int)assignPtr->type, EMPTY_PTR );
           return ERR_FM_BAD_TYPE;
       }
       if ( assignPtr->group != ENTRY_32 )
       {
           printf("DBG: Weirdness in releaseSubblock.. tried to return a pointer from group %i to subblock group\n", (unsigned int)assignPtr->group );
           return ERR_INVALID_PARAM;
       }           
       //find the start of the chain
       listOffset = calcPtrOffset( ENTRY_32 );

       getSubBlockEntry( blockZero, 0, listOffset, 8, &emptyPtr );
       if( isDebug( DEBUG_2 )) { 
          cout<<"DBG: EM (sb release 1) sb fbo "<<emptyPtr.fbo<<" sbid "<<emptyPtr.sbid<<" entry "<<emptyPtr.entry<<"\n";
       }

       //sub block is full or chain empty
       if (emptyPtr.entry == ENTRY_PER_SUBBLOCK-1 || emptyPtr.fbo==0)
       { 
       // change type from EMPTY_PTR to EMPTY_LIST 
           assignPtr->type = EMPTY_LIST;
           
           //if( isDebug( DEBUG_1 )) { printf("DBG: No room in subblock chain - need to add a sub-block\n");  }
           if( isDebug( DEBUG_2 )) { 
                   cout<<"DBG: Change head pointer to fbo "<<assignPtr->fbo<<" sbid "<<assignPtr->sbid<<" entry "<<assignPtr->entry<<"\n";
           }
           
           // change head pointer to released segment
           setSubBlockEntry( blockZero, 0, listOffset, 8, assignPtr );
           blockZero->dirty = 1;
           
           // read in released segment to set llp of new block to point to current head of chain
           rc = readDBFile( cb, extraBlock.data, assignPtr->fbo );
           if (rc != NO_ERROR){ 
               if (isDebug( DEBUG_1 )){
                   cout<<"DBG: File error during releaseSegment (2), rc: "<<rc<<" fbo/lbid: "<<assignPtr->fbo<<"\n"; 
               }
               return rc;
           }
           
           if( isDebug( DEBUG_2 )) { 
               cout<<"DBG: Set LLP for fbo "<<assignPtr->fbo<<" sbid "<<assignPtr->sbid<<" entry "<<assignPtr->entry<<"to fbo "<<emptyPtr.fbo<<" sbid "<<emptyPtr.sbid<<" entry "<<emptyPtr.entry<<"\n";
           }
           if( isDebug( DEBUG_3)){
               printf("Before\n");
               printMemSubBlock( &extraBlock, assignPtr->sbid );
           }
           
           emptyPtr.type = EMPTY_LIST;
           //memset( extraBlock.data, 0, BYTE_PER_SUBBLOCK);
           setSubBlockEntry( extraBlock.data, assignPtr->sbid, 0, 8, &emptyPtr );
           rc = writeDBFile( cb, &extraBlock, assignPtr->fbo );
           if (rc != NO_ERROR){ return rc; }
           
           if( isDebug( DEBUG_2 )) { 
               getSubBlockEntry( blockZero, 0, listOffset, 8, &emptyMap );
               cout<<"DBG: EM (sb release 2) sb fbo "<<emptyMap.fbo<<" sbid "<<emptyMap.sbid<<" entry "<<emptyMap.entry<<"\n";
           }
           if( isDebug( DEBUG_3)){
               printf("After\n");
               printMemSubBlock( &extraBlock, assignPtr->sbid );
           }
           
       }
       else 
       { // 
           emptyPtr.entry++;
           rc = readDBFile( cb, workBlock.data, emptyPtr.fbo );
           if (rc != NO_ERROR) { 
               if (isDebug( DEBUG_1 ))
               {
                   printf("DBG: File error during releaseSubblock\n"); 
               }
               return rc;
           }
           
           if( isDebug( DEBUG_3)){
               printf("Before\n");
               printMemSubBlock( &workBlock, emptyPtr.sbid );
           }

           setSubBlockEntry( workBlock.data, emptyPtr.sbid, emptyPtr.entry, 8, assignPtr );
           rc = writeDBFile( cb, &workBlock, emptyPtr.fbo );
           if (rc != NO_ERROR){ return rc; }

           if( isDebug( DEBUG_2 )) { 
               cout<<"DBG: setting emptyPtr sb fbo "<<emptyPtr.fbo<<" sbid "<<emptyPtr.sbid<<" entry "<<emptyPtr.entry<<"\n";
           }
           if( isDebug( DEBUG_3)){
               printf("After\n");
               printMemSubBlock( &workBlock, emptyPtr.sbid );
           }
           
           emptyPtr.type = EMPTY_LIST;
           emptyPtr.group = ENTRY_32;

           setSubBlockEntry( blockZero, 0, listOffset, 8, &emptyPtr );
           blockZero->dirty = 1;

       }
       uint64_t count;
       
       getSubBlockEntry( blockZero, 0, calcStatOffset(ENTRY_32) , 8, &count );
       count++;
       setSubBlockEntry( blockZero, 0, calcStatOffset(ENTRY_32) , 8, &count );
               

       return NO_ERROR;
   }
Exemplo n.º 28
0
int64_t File::getSize()
{
   int64_t retSize = 0;
   getFileSize( _name, retSize );
   return retSize;
}
Exemplo n.º 29
0
void load_trace_raw ( char* fname )
{
	unsigned long totalBytes = getFileSize ( fname );
	unsigned long currBytes = 0;

	FILE* fp = fopen ( fname, "rb" );
	char header[2048];
	char buf[2048];

	Call cl;
	Event e;
	char typ, nameID;
	unsigned long long tstart, tstop;
	int fnum, size = 0;	
	int cstart = 0, cnum = 0;
	mMaxSize = 1;

	int num_frame = 0;
	int num_draw = 0;

	Frame f;
	f.clear ();
	frame = 0;

	while ( !feof(fp) && (num_draw < maxDraw || maxDraw==0)) {
			
		readbytes ( header, 18, 1, fp );		// 18 byte header
		parseHeader ( header, typ, nameID, tstart, tstop );
		switch ( typ ) {
		case 'C': {
			readbytes ( buf, 20, 1, fp );			
			if ( num_frame >= startFrame ) {				
				parseTrace ( buf, nameID, cl );
				mCalls.push_back ( cl );
				cnum++;
			}
			} break;
		case 'D': {
			
				currBytes = getFilePos ( fp );
				if ( f.totalDraw % 100 == 0 ) {
					if ( maxDraw == 0 ) {
						app_printf ( "%dk read, %.2f%% of file, %.2f%% done\n", currBytes/1024, currBytes*100.0f/totalBytes, currBytes*100.0f/totalBytes );	
					} else {
						app_printf ( "%dk read, %.2f%% of file, %.2f%% done\n", currBytes/1024, currBytes*100.0f/totalBytes, num_draw*100.0f/maxDraw );	
					}
				}				
				readbytes ( buf, NUM_BIN*9 + 9, 1, fp );
				
				if ( num_frame >= startFrame ) {
					parseTrace ( buf, nameID, e );
				
					e.frame = frame;
					e.call_num = cnum;
					e.call_start = cstart;
					mEvents.push_back ( e );
			
					cstart += cnum;
					cnum = 0;
					if ( e.bin_size[BIN_DRAW] > mMaxSize ) mMaxSize = e.bin_size[BIN_DRAW];
					if ( e.bin_id[BIN_DRAW] > mMaxPrim ) mMaxPrim = e.bin_id[BIN_DRAW];
					num_draw++;
		
					for (int n=0; n < NUM_BIN; n++) {
						f.binChange[n] += (e.bin_change[n]==BIN_CREATE || e.bin_change[n]==BIN_CHANGE) ? 1 : 0;
						f.binSwitch[n] += (e.bin_change[n]==BIN_SWITCH ) ? 1 : 0;
						f.binReuse[n] += (e.bin_change[n]==BIN_REUSE ) ? 1 : 0;
						f.binUnique[n] = (e.bin_id[n] > f.binUnique[n] ) ? e.bin_id[n] : f.binUnique[n];
					}
					f.totalDraw++;
					f.totalTransfer += e.bin_size[BIN_DRAW];
					f.totalPrim += e.bin_id[BIN_DRAW];		
				}
			} break;
		case 'F': {
			
			readbytes ( buf, 8, 1, fp );
			
			if ( num_frame >= startFrame ) {
				mFrames.push_back ( f );		// record frame data
				f.clear ();
				frame++;
				parseTrace ( buf, fnum, size );
				e.name_id = nameID;
				e.name = "Present";
				e.call_num = cnum;
				e.call_start = cstart;			
				e.count = 1;
				for (int n=0; n < NUM_BIN; n++ ) {
					e.bin_id[n] = -1;
					e.bin_change[n] = -1;
				}
				mEvents.push_back ( e );
				cstart += cnum;			
				cnum = 0;				
			}
			num_frame++;

			} break;
		};
	}
	// read may not have gotten to end of frame
	mFrames.push_back ( f );

	if ( mFrames.size() == 0 || mEvents.size() == 0 ) {
		app_printf ( "Error: No frames or events detected.\n" );
		app_printf ( "Try running trace again. \n" );
		_getch();
		exit(-1);
	}

	fclose ( fp );

}
Exemplo n.º 30
0
// Handles the file uploads to the server
// Returns the number of files successfully sent
int handleFileUploads(char* hooli_dir, hooli_file* uploads, char* tokenstring, 
  char* hftp_hostname, char* hftp_port, int numfiles) {

  int filesUploaded = 0;    // Number of files successfully uploaded
  int current_file_num = 0; // Number of current file (out of numfiles)

  host server;           // Address of the server
  // Create a socket to listen on specified port
  int sockfd = create_client_socket(hftp_hostname, hftp_port, &server);
  int pollval;           // Polling event value

  uint8_t sequence = 0;  // Current message sequence
  uint8_t token[TOKEN_LENGTH];
  memcpy(token, tokenstring, TOKEN_LENGTH);
  FILE* fp;              // File pointer

  while(uploads) {
    current_file_num++;
    syslog(LOG_INFO, "Uploading file %d of %d: '%s'", current_file_num, 
      numfiles, uploads->filepath);

    // Get absolute path to file
    char* file_abs_path;
    asprintf(&file_abs_path, "%s%s", hooli_dir, uploads->filepath);
    // Get the size of the file
    uint32_t file_size;
    // If there's an error, go to the next file
    if(-1 == (file_size = getFileSize(file_abs_path))) {
      free(file_abs_path);
      continue;
    }

    uint32_t bytes_uploaded = 0; // Number of bytes of file uploaded

    // Send an initialization msg
    hftp_response_message* response = NULL;
    while(NULL == response) {
      response = sendControlMessage(hftp_hostname, 
      hftp_port, INITMSG, sequence, file_size, (uint32_t)uploads->checksum, 
      token, uploads->filepath, &server, sockfd);
    }

    response->error_code = ntohs(response->error_code);
    syslog(LOG_DEBUG, " * * Received ACK for sequence %d, error code %d", 
        response->sequence, response->error_code);

    // If initialization failed, go to next file
    if(1 == response->error_code) {
      close(sockfd);
      free(response);
      break;
    }
    free(response);

    char* serverIP = server.friendly_ip;  // Note the IP of the server
    sequence = !sequence;                 // Update sequence
    fp = fopen(file_abs_path, "r");
    free(file_abs_path);

    // Will update user after so many bytes uploaded
    int upload_log_point = 100000;
    
    // Upload the file in data messages
    while(bytes_uploaded < file_size) {
      // Log progress after uploaded amount of bytes specified
      if(bytes_uploaded >= upload_log_point) {
        // Double the log point now
        upload_log_point *= 2;
        double percent_complete = (double)bytes_uploaded/file_size*100;
        syslog(LOG_INFO, " * %.02f%% complete (%d of %d bytes)", 
          percent_complete, bytes_uploaded, file_size);
      }

      hftp_data_message* request = (hftp_data_message*)create_message();
      uint16_t bytes_read = fread(request->data, 1, MAX_DATA_LENGTH, fp);

      // If unable to read the file, log an error and move on to next file
      if(!bytes_read) {
        syslog(LOG_WARNING, "Unable to read file '%s'", uploads->filepath);
        fclose(fp);
        break;
      }

      request->type = DATAMSG;
      request->sequence = sequence;
      request->data_length = htons(bytes_read);
      // Length is number bytes read from file + 4 bytes of headers
      request->length = bytes_read + 4;

      // We will poll sockfd for the POLLIN event
      struct pollfd fd = {
        .fd = sockfd,
        .events = POLLIN
      };

      bool resend = true;          // True when the message should be (re)sent
      bool ignoreResponse = false; // True when the wrong ack is received
      while(resend) {
        if(!ignoreResponse) {
          syslog(LOG_DEBUG, " * * Data request, sequence %d, of size %d", 
            sequence, request->length);
          send_message(sockfd, (message*)request, &server);
        }
        // Poll the socket for 10 seconds
        pollval = poll(&fd, 1, 10000);
        if(pollval == 1 && fd.revents == POLLIN) {
          response = (hftp_response_message*)receive_message(sockfd, &server);

          // Check that response is from server, ignore otherwise
          if(0 != strcmp(server.friendly_ip, serverIP)) {
            syslog(LOG_DEBUG, " * * Response not from server, ignoring");
            ignoreResponse = true;
            // free(response);
            continue;
          }

          // Check response sequence
          // If wrong sequence, ignore response
          if(response->sequence == sequence) {
            resend = false;
            sequence = !sequence;
            bytes_uploaded += bytes_read;
            free(request);
          } else {
            ignoreResponse = true;
            free(response);
          }         
        } else {
          ignoreResponse = false;
        }
      }

      response->error_code = ntohs(response->error_code);

      syslog(LOG_DEBUG, " * * Received ACK for sequence %d, error code %d", 
        response->sequence, response->error_code);

      // If an error occured, move to the next file
      if(response->error_code) {
        free(response);
        fclose(fp);
        break;
      }
      free(response);
    }

    // Log progress complete
    double percent_complete = file_size/bytes_uploaded*100;
    syslog(LOG_INFO, " * %.02f%% complete (%d of %d bytes successfully transferred)", 
      percent_complete, bytes_uploaded, file_size);

    if(bytes_uploaded == file_size) {
      filesUploaded ++;
    }
    fclose(fp);
    uploads = uploads->next;
  }

  // Send termination message
  hftp_response_message* response = NULL;
  while(NULL == response) {
    response = sendControlMessage(hftp_hostname, 
    hftp_port, TERMMSG, sequence, 0, 0, token, "nofile", &server, sockfd);
  }

  response->error_code = ntohs(response->error_code);
  syslog(LOG_DEBUG, " * * Received ACK for sequence %d, error code %d", 
        response->sequence, response->error_code);
  free(response);

  close(sockfd);
  return filesUploaded;
}