Exemple #1
0
static int HashTableTestFull02 (void)
{
    int result = 0;
    HashTable *ht = HashTableInit(32, HashTableGenericHash, NULL, NULL);
    if (ht == NULL)
        goto end;

    int r = HashTableAdd(ht, "test", 4);
    if (r != 0)
        goto end;

    char *rp = HashTableLookup(ht, "test", 4);
    if (rp == NULL)
        goto end;

    r = HashTableRemove(ht, "test2", 5);
    if (r == 0)
        goto end;

    /* all is good! */
    result = 1;
end:
    if (ht != NULL) HashTableFree(ht);
    return result;
}
Exemple #2
0
static int HashTableTestInit04 (void) {
    HashTable *ht = HashTableInit(0, HashTableGenericHash, NULL, NULL);
    if (ht == NULL)
        return 1;

    HashTableFree(ht);
    return 0;
}
Exemple #3
0
/* no hash function, so it should fail */
static int HashTableTestInit02 (void) {
    HashTable *ht = HashTableInit(1024, NULL, NULL, NULL);
    if (ht == NULL)
        return 1;

    HashTableFree(ht);
    return 0;
}
/**
 * \brief Inits the context to be used by the Classification Config parsing API.
 *
 *        This function initializes the hash table to be used by the Detection
 *        Engine Context to hold the data from the classification.config file,
 *        obtains the file desc to parse the classification.config file, and
 *        inits the regex used to parse the lines from classification.config
 *        file.
 *
 * \param de_ctx Pointer to the Detection Engine Context.
 *
 * \retval  0 On success.
 * \retval -1 On failure.
 */
int SCClassConfInitContext(DetectEngineCtx *de_ctx)
{
    char *filename = NULL;
    const char *eb = NULL;
    int eo;
    int opts = 0;

    /* init the hash table to be used by the classification config Classtypes */
    de_ctx->class_conf_ht = HashTableInit(4096, SCClassConfClasstypeHashFunc,
                                          SCClassConfClasstypeHashCompareFunc,
                                          SCClassConfClasstypeHashFree);
    if (de_ctx->class_conf_ht == NULL) {
        SCLogError(SC_ERR_HASH_TABLE_INIT, "Error initializing the hash "
                   "table");
        return -1;
    }

    /* if it is not NULL, use the file descriptor.  The hack so that we can
     * avoid using a dummy classification file for testing purposes and
     * instead use an input stream against a buffer containing the
     * classification strings */
    if (fd == NULL) {
        filename = SCClassConfGetConfFilename();
        if ( (fd = fopen(filename, "r")) == NULL) {
            SCLogError(SC_ERR_FOPEN, "Error opening file: \"%s\": %s", filename, strerror(errno));
            goto error;
        }
    }

    regex = pcre_compile(DETECT_CLASSCONFIG_REGEX, opts, &eb, &eo, NULL);
    if (regex == NULL) {
        SCLogDebug("Compile of \"%s\" failed at offset %" PRId32 ": %s",
                   DETECT_CLASSCONFIG_REGEX, eo, eb);
        goto error;
    }

    regex_study = pcre_study(regex, 0, &eb);
    if (eb != NULL) {
        SCLogDebug("pcre study failed: %s", eb);
        goto error;
    }

    return 0;

 error:
    if (de_ctx->class_conf_ht != NULL) {
        HashTableFree(de_ctx->class_conf_ht);
        de_ctx->class_conf_ht = NULL;
    }
    if (fd != NULL) {
        fclose(fd);
        fd = NULL;
    }

    printf("\nPlease check the \"classification-file\" option in your suricata.yaml file.\n");
    exit(EXIT_FAILURE);
//    return -1;
}
/**
 * \brief Inits the context to be used by the Reference Config parsing API.
 *
 *        This function initializes the hash table to be used by the Detection
 *        Engine Context to hold the data from reference.config file,
 *        obtains the file descriptor to parse the reference.config file, and
 *        inits the regex used to parse the lines from reference.config file.
 *
 * \param de_ctx Pointer to the Detection Engine Context.
 *
 * \retval  0 On success.
 * \retval -1 On failure.
 */
static int SCRConfInitContext(DetectEngineCtx *de_ctx)
{
    char *filename = NULL;
    const char *eb = NULL;
    int eo;
    int opts = 0;

    /* init the hash table to be used by the reference config references */
    de_ctx->reference_conf_ht = HashTableInit(128, SCRConfReferenceHashFunc,
                                              SCRConfReferenceHashCompareFunc,
                                              SCRConfReferenceHashFree);
    if (de_ctx->reference_conf_ht == NULL) {
        SCLogError(SC_ERR_HASH_TABLE_INIT, "Error initializing the hash "
                   "table");
        return -1;
    }

    /* if it is not NULL, use the file descriptor.  The hack so that we can
     * avoid using a dummy reference file for testing purposes and
     * instead use an input stream against a buffer containing the
     * reference strings */
    if (fd == NULL) {
        filename = SCRConfGetConfFilename();
        if ((fd = fopen(filename, "r")) == NULL) {
            SCLogError(SC_ERR_FOPEN, "Error opening file: \"%s\": %s", filename,
                       strerror(errno));
            goto error;
        }
    }

    regex = pcre_compile(SC_RCONF_REGEX, opts, &eb, &eo, NULL);
    if (regex == NULL) {
        SCLogDebug("Compile of \"%s\" failed at offset %" PRId32 ": %s",
                   SC_RCONF_REGEX, eo, eb);
        goto error;
    }

    regex_study = pcre_study(regex, 0, &eb);
    if (eb != NULL) {
        SCLogDebug("pcre study failed: %s", eb);
        goto error;
    }

    return 0;

 error:
    if (de_ctx->reference_conf_ht != NULL) {
        HashTableFree(de_ctx->reference_conf_ht);
        de_ctx->reference_conf_ht = NULL;
    }
    if (fd != NULL) {
        fclose(fd);
        fd = NULL;
    }

    return -1;
}
Exemple #6
0
/*-----------------------------------------------------------------------------
 *  初始化hash table
 *-----------------------------------------------------------------------------*/
int InitHashtable(HashTable *pstHashTable,key_t key, size_t uRowNum, size_t uNodeSize, size_t auNodeNums[], size_t auMods[], int iCreateFlags, Compare compare)
{
    int ret = 0;
    volatile char *pTable = NULL;

    if(pstHashTable == NULL || uRowNum == 0)
    {
        return -1;
    }

    /*key_t uShmKey =  CACHE_HASHTABLE_BASE_KEY + key;*/
    /*printf("uRowNum = %d uNodeSize = %d \n", uRowNum, uNodeSize);*/
    size_t dwTableSize = HashTableEvalTableSize(uRowNum, uNodeSize, auNodeNums);
    DD("HashTable size = %d\n", (int)dwTableSize);
    if(dwTableSize == 0)
    {
        return -2;
    }

    pTable =  GetShm(key, dwTableSize, 0666);
    if(pTable == NULL)
    {
        if(iCreateFlags == 0)
        {
            printf("shm not existd.should create! shmkey = 0x%x\n", key);
            return -3;
        }
        else if(iCreateFlags == 1)
        {
            if((ret = GetShm2((volatile void **)&pTable, key, dwTableSize, 0666 | IPC_CREAT)) < 0)
            {
                printf("GetShm2 error. %s\n", strerror(errno));
                return -4;
            }
            /*printf("first GetShm2 ret = %d\n", ret);*/
            bzero((void *)pTable,dwTableSize);
        }
        else 
        {
            return -5;
        }
    }
    /*printf("pTable = 0x%x\n", pTable);*/
    /*g_pstHashTable = (HashTable *)pTable;*/
    /*pTable += sizeof(HashTable);*/

    if((ret = HashTableInit(pstHashTable,(void *)pTable, uNodeSize,uRowNum, auNodeNums, auMods, iCreateFlags, compare)) < 0)
    {
        return -5;
    }

    /**ppstHashTable = g_pstHashTable;*/
    return 0;
}
Exemple #7
0
static int HashTableTestInit03 (void) {
    int result = 0;
    HashTable *ht = HashTableInit(1024, HashTableGenericHash, NULL, NULL);
    if (ht == NULL)
        return 0;

    if (ht->Hash == HashTableGenericHash)
        result = 1;

    HashTableFree(ht);
    return result;
}
Exemple #8
0
static int HashTableTestInit06 (void) {
    int result = 0;
    HashTable *ht = HashTableInit(1024, HashTableGenericHash, HashTableDefaultCompareTest, NULL);
    if (ht == NULL)
        return 0;

    if (ht->Compare == HashTableDefaultCompareTest)
        result = 1;

    HashTableFree(ht);
    return result;
}
void DigestCacheInit(DigestCache* self, size_t heap_size, const char* filename)
{
  ReadWriteLockInit(&self->m_Lock);

  self->m_State = nullptr;
  self->m_StateFilename = filename;

  HeapInit(&self->m_Heap, heap_size, HeapFlags::kDefault);
  LinearAllocInit(&self->m_Allocator, &self->m_Heap, heap_size / 2, "digest allocator");
  MmapFileInit(&self->m_StateFile);
  HashTableInit(&self->m_Table, &self->m_Heap, HashTable::kFlagPathStrings);

  self->m_AccessTime = time(nullptr);

  MmapFileMap(&self->m_StateFile, filename);
  if (MmapFileValid(&self->m_StateFile))
  {
    const DigestCacheState* state = (const DigestCacheState*) self->m_StateFile.m_Address;
    if (DigestCacheState::MagicNumber == state->m_MagicNumber)
    {
      const uint64_t time_now = time(nullptr);

      // Throw out records that haven't been accessed in a week.
      const uint64_t cutoff_time = time_now - 7 * 24 * 60 * 60;

      self->m_State = state;

      //HashTablePrepareBulkInsert(&self->m_Table, state->m_Records.GetCount());

      for (const FrozenDigestRecord& record : state->m_Records)
      {
        if (record.m_AccessTime < cutoff_time)
          continue;

        DigestCacheRecord* r = LinearAllocate<DigestCacheRecord>(&self->m_Allocator);
        r->m_Hash          = record.m_FilenameHash;
        r->m_ContentDigest = record.m_ContentDigest;
        r->m_Next          = nullptr;
        r->m_String        = record.m_Filename.Get();
        r->m_Timestamp     = record.m_Timestamp;
        r->m_AccessTime    = record.m_AccessTime;
        HashTableInsert(&self->m_Table, r);
      }
      Log(kDebug, "digest cache initialized -- %d entries", state->m_Records.GetCount());
    }
    else
    {
      MmapFileUnmap(&self->m_StateFile);
    }
  }
}
Exemple #10
0
static int HashTableTestAdd02 (void) {
    int result = 0;
    HashTable *ht = HashTableInit(32, HashTableGenericHash, NULL, NULL);
    if (ht == NULL)
        goto end;

    int r = HashTableAdd(ht, NULL, 4);
    if (r == 0)
        goto end;

    /* all is good! */
    result = 1;
end:
    if (ht != NULL) HashTableFree(ht);
    return result;
}
/**
 * \brief Inits the context to be used by the Reference Config parsing API.
 *
 *        This function initializes the hash table to be used by the Detection
 *        Engine Context to hold the data from reference.config file,
 *        obtains the file descriptor to parse the reference.config file, and
 *        inits the regex used to parse the lines from reference.config file.
 *
 * \param de_ctx Pointer to the Detection Engine Context.
 *
 * \retval  0 On success.
 * \retval -1 On failure.
 */
static FILE *SCRConfInitContextAndLocalResources(DetectEngineCtx *de_ctx, FILE *fd)
{
    char *filename = NULL;

    /* init the hash table to be used by the reference config references */
    de_ctx->reference_conf_ht = HashTableInit(128, SCRConfReferenceHashFunc,
                                              SCRConfReferenceHashCompareFunc,
                                              SCRConfReferenceHashFree);
    if (de_ctx->reference_conf_ht == NULL) {
        SCLogError(SC_ERR_HASH_TABLE_INIT, "Error initializing the hash "
                   "table");
        goto error;
    }

    /* if it is not NULL, use the file descriptor.  The hack so that we can
     * avoid using a dummy reference file for testing purposes and
     * instead use an input stream against a buffer containing the
     * reference strings */
    if (fd == NULL) {
        filename = SCRConfGetConfFilename();
        if ((fd = fopen(filename, "r")) == NULL) {
#ifdef UNITTESTS
            if (RunmodeIsUnittests())
                goto error; // silently fail
#endif
            SCLogError(SC_ERR_FOPEN, "Error opening file: \"%s\": %s", filename,
                       strerror(errno));
            goto error;
        }
    }

    return fd;

 error:
    if (de_ctx->reference_conf_ht != NULL) {
        HashTableFree(de_ctx->reference_conf_ht);
        de_ctx->reference_conf_ht = NULL;
    }
    if (fd != NULL) {
        fclose(fd);
        fd = NULL;
    }

    return NULL;
}
Exemple #12
0
int main( int argc, char **argv ) {
    
    HashTable hash;
    
    HashTableInit(&hash);
    
    unsigned char c = 'd';
    
    hash.create(65536);
    
    hash.put("hi", &c, sizeof(unsigned char));
    
    printf("%s\n", hash.getValue("hi"));
    
    hash.destroy();

    
}
Exemple #13
0
void ConfigurationSetValue(struct Configuration* configuration, const char* section, const char* key, const char* value) {
	struct Table* currentSection = &configuration->root;
	if (section) {
		currentSection = HashTableLookup(&configuration->sections, section);
		if (!currentSection) {
			if (value) {
				currentSection = malloc(sizeof(*currentSection));
				HashTableInit(currentSection, 0, _sectionDeinit);
				HashTableInsert(&configuration->sections, section, currentSection);
			} else {
				return;
			}
		}
	}
	if (value) {
		HashTableInsert(currentSection, key, strdup(value));
	} else {
		HashTableRemove(currentSection, key);
	}
}
BOOLEAN InitializeIpTable(LPCWSTR binary_file)
{
	HANDLE file;
	OBJECT_ATTRIBUTES attrib;
	UNICODE_STRING str;
	IO_STATUS_BLOCK block;
	unsigned int num = 0,ip = 0;
	LARGE_INTEGER offset = {0};
	unsigned int j = 0;
	initialized = FALSE;
	RtlInitUnicodeString(&str, binary_file);
	InitializeObjectAttributes(&attrib, &str, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE,NULL,NULL);

	ZwCreateFile(&file, GENERIC_READ, &attrib, &block, NULL,
		FILE_ATTRIBUTE_NORMAL, FILE_SHARE_READ, FILE_OPEN,FILE_NON_DIRECTORY_FILE | FILE_SYNCHRONOUS_IO_NONALERT, NULL, 0);

	if(block.Status != STATUS_SUCCESS) {
		PrintLog("Cannot Read IP Table\n");
		return FALSE;
	}

	ZwReadFile(file,NULL,NULL,NULL,&block,&num,4,&offset,NULL);
	offset.QuadPart += 4;
	HashTableInit();

	for(j = 0; j < num; j++) {
		ZwReadFile(file,NULL,NULL,NULL,&block,&ip,4,&offset,NULL);
		offset.QuadPart += 4;
		HashTableInsert(ip);
	}

	ZwClose(file);
	KdPrint(("%d logs of IP Address loaded.\n",num));

	return TRUE;
}
Exemple #15
0
void LuaProfilerInit(MemAllocHeap* heap, MemAllocLinear* alloc, lua_State* L)
{
  LuaProfilerState* self = &s_Profiler;

  memset(self, 0, sizeof *self);

  self->m_LuaState            = L;
  self->m_Heap                = heap;
  self->m_Allocator           = alloc;

  HashTableInit(&self->m_Functions, heap, 0);

  RehashInvocationTable(1024);

  s_TopLevel.m_Hash   = 0;
  s_TopLevel.m_String = "toplevel;global;;0";
  s_TopLevel.m_Next   = nullptr;

  s_Profiler.m_CurrentInvocation = PushInvocation(&s_TopLevel);
  s_Profiler.m_CurrentInvocation->m_StartTick = TimerGet();

  // Install debug hook.
  lua_sethook(L, ProfilerLuaEvent, LUA_MASKCALL|LUA_MASKRET, 0);
}
Exemple #16
0
void ConfigurationInit(struct Configuration* configuration) {
	HashTableInit(&configuration->sections, 0, _tableDeinit);
	HashTableInit(&configuration->root, 0, _sectionDeinit);
}
Exemple #17
0
int PiggyInit (void)
{
	int bHamOk = 0, bSoundOk = 0;
	int i;

/*---*/PrintLog ("   Initializing hash tables\n");
HashTableInit (bitmapNames, MAX_BITMAP_FILES);
HashTableInit (bitmapNames + 1, D1_MAX_BITMAP_FILES);
HashTableInit (soundNames, MAX_SOUND_FILES);
HashTableInit (soundNames + 1, MAX_SOUND_FILES);

/*---*/PrintLog ("   Initializing sound data (%d sounds)\n", MAX_SOUND_FILES);
for (i=0; i<MAX_SOUND_FILES; i++)	{
	gameData.pig.sound.sounds [0][i].nLength [0] =
	gameData.pig.sound.sounds [0][i].nLength [1] = 0;
	gameData.pig.sound.sounds [0][i].data [0] =
	gameData.pig.sound.sounds [0][i].data [1] = NULL;
	soundOffset [0][i] = 0;
}
/*---*/PrintLog ("   Initializing bitmap index (%d indices)\n", MAX_BITMAP_FILES);
for (i = 0; i < MAX_BITMAP_FILES; i++)     
	gameData.pig.tex.bitmapXlat [i] = i;

if (!bogusBitmap_initialized) {
	int i;
	ubyte c;
/*---*/PrintLog ("   Initializing placeholder bitmap\n");
	bogusBitmap_initialized = 1;
	memset (&bogusBitmap, 0, sizeof (grsBitmap));
	bogusBitmap.bmProps.w = 
	bogusBitmap.bmProps.h = 
	bogusBitmap.bmProps.rowSize = 64;
	bogusBitmap.bmTexBuf = bogus_data;
	bogusBitmap.bmPalette = gamePalette;
	c = GrFindClosestColor (gamePalette, 0, 0, 63);
	memset (bogus_data, c, 4096);
	c = GrFindClosestColor (gamePalette, 63, 0, 0);
	// Make a big red X !
	for (i=0; i<1024; i++) {
		bogus_data [i * 1024 + i] = c;
		bogus_data [i * 1024 + (1023 - i)] = c;
		}
	PiggyRegisterBitmap (&bogusBitmap, "bogus", 1);
	bogusSound.nLength [0] = 1024*1024;
	bogusSound.data [0] = bogus_data;
	bitmapOffsets [0][0] =
	bitmapOffsets [1][0] = 0;
}

if (FindArg ("-bigpig"))
	bBigPig = 1;

if (FindArg ("-lowmem"))
	bLowMemory = 1;

if (FindArg ("-nolowmem"))
	bLowMemory = 0;

if (bLowMemory)
	gameStates.sound.digi.bLoMem = 1;
/*---*/PrintLog ("   Loading game data\n");
#if 1 //def EDITOR //need for d1 mission briefings
PiggyInitPigFile ((char *) DEFAULT_PIGFILE);
#endif
/*---*/PrintLog ("   Loading main ham file\n");
bSoundOk = bHamOk = ReadHamFile ();

if (gameData.pig.tex.nHamFileVersion >= 3) {
/*---*/PrintLog ("   Loading sound file\n");
	bSoundOk = ReadSoundFile ();
	}
if (gameStates.app.bFixModels)
	gameStates.app.bFixModels = gameStates.app.bDemoData ? 0 : LoadRobotReplacements ("d2x-xl", 0, 1) > 0;
LoadTextureBrightness ("descent2", gameData.pig.tex.defaultBrightness [0]);
LoadTextureBrightness ("descent", gameData.pig.tex.defaultBrightness [1]);
LoadTextureColors ("descent2", gameData.render.color.defaultTextures [0]);
LoadTextureColors ("descent", gameData.render.color.defaultTextures [1]);
atexit (PiggyClose);
return (bHamOk && bSoundOk);               //read ok
}
int main(int argc, char** argv)
{
	HashTableInit();
	ReadArpCacheAndShow();
	return 1;
}
Exemple #19
0
struct mScriptBridge* mScriptBridgeCreate(void) {
	struct mScriptBridge* sb = malloc(sizeof(*sb));
	HashTableInit(&sb->engines, 0, _seDeinit);
	sb->debugger = NULL;
	return sb;
}