Esempio n. 1
0
int main(int argc, char **argv)
{
	char *sosuFile = makeTempFile("sosu");
	uint64 value;
	uint64 max = toValue64(nextArg());

	errorCase(max < 11);
	errorCase(max == UINT64MAX);

	SosuFp = fileOpen(sosuFile, "a+b");
	SosuCnt = 0;

	for(value = 11; value <= max; value += 2)
	{
		if((value & 0x3ffe) == 0)
			cmdTitle_x(xcout("Prime2tox - %I64u", value));

		if(
			value % 3 != 0 &&
			value % 5 != 0 &&
			value % 7 != 0 &&
			IsSosu(value)
			)
			AddSosu(value);
	}
	cmdTitle("Prime2tox - Completed");
	DispSosu();

	fileClose(SosuFp);
	removeFile(sosuFile);
	memFree(sosuFile);
}
Esempio n. 2
0
// write a temporary file containing the memory buffer which is to be scanned
// if your CS plugin does not have the ability to scan memory directly (e.g. clamdscan), this gets used by the default scanMemory to turn it into a file
int CSPlugin::writeMemoryTempFile(const char *object, unsigned int objectsize, String *filename)
{
    int tempfd = makeTempFile(filename); // String gets modified
    if (tempfd < 0) {
#ifdef DGDEBUG
        std::cerr << "Error creating temp file in writeMemoryTempFile." << std::endl;
#endif
        syslog(LOG_ERR, "%s", "Error creating temp file in writeMemoryTempFile.");
        return DGCS_ERROR;
    }
    errno = 0;
#ifdef DGDEBUG
    std::cout << "About to writeMemoryTempFile " << (*filename) << " size: " << objectsize << std::endl;
#endif

    while (true) {
        if (write(tempfd, object, objectsize) < 0) {
            if (errno == EINTR) {
                continue; // was interupted by a signal so restart
            }
        }
        break; // end the while
    }
    close(tempfd); // finished writing so close file
    return DGCS_OK; // all ok
}
Esempio n. 3
0
TEST(TestMemFile, RealFileInCache) {
  string temp_fn;
  ASSERT_TRUE(makeTempFile("123abc", &temp_fn));

  StaticContentCache::TheFileCache = std::make_shared<FileCache>();
  StaticContentCache::TheFileCache->write("/content", temp_fn.c_str());

  auto mf = req::make<MemFile>();
  ASSERT_TRUE(mf->open("/content", "r"));

  ASSERT_EQ(mf->getc(), '1');
  ASSERT_EQ(mf->getc(), '2');
  ASSERT_EQ(mf->getc(), '3');
  ASSERT_EQ(mf->getc(), 'a');
  ASSERT_EQ(mf->getc(), 'b');
  ASSERT_EQ(mf->getc(), 'c');

  ASSERT_EQ(mf->getc(), EOF);

  ASSERT_TRUE(mf->close());

  ASSERT_EQ(unlink(temp_fn.c_str()), 0);
}
Esempio n. 4
0
TEST(TestMemFile, RealFileInCache) {
  string temp_fn;
  ASSERT_TRUE(makeTempFile("123abc", &temp_fn));

  StaticContentCache::TheFileCache = FileCachePtr(new FileCache());
  StaticContentCache::TheFileCache->write("/content", temp_fn.c_str());

  MemFile mf;
  ASSERT_TRUE(mf.open("/content", "r"));

  ASSERT_EQ(mf.getc(), '1');
  ASSERT_EQ(mf.getc(), '2');
  ASSERT_EQ(mf.getc(), '3');
  ASSERT_EQ(mf.getc(), 'a');
  ASSERT_EQ(mf.getc(), 'b');
  ASSERT_EQ(mf.getc(), 'c');

  ASSERT_EQ(mf.getc(), EOF);

  ASSERT_TRUE(mf.close());

  ASSERT_EQ(unlink(temp_fn.c_str()), 0);
}
/*
 * Plot specified x/y coordinates, x as int32_t and y as float, with an arbitrary number
 * of separate lines, as lines and/or points.
 * Output is PDF file, location specified by 'fileName'.
 */
int plplotLines(
    PlplotSetup *setup,
    uint32_t numSamples,	/* size of ix[] and fy[] arrays */
    uint32_t numLines,		/* size of lineDef[] array */
    int32_t *ix,			/* numSamples */
    LineDef *lineDef,		/* numLines */
    const char *graphName,
    const char *fileName,
    bool plotPoints,		/* true: plot points */
    bool plotLine,			/* true: plot line */
    bool skipTrailZeroes)	/* don't plot zero Y values at end of graph */
{
    if(setup == NULL) {
        printf("***plplotLine: setup required\n");
        return -1;
    }

    char tmpFile[TEMP_FN_LEN];
    makeTempFile(tmpFile, TEMP_FN_LEN);

    PLFLT fx[numSamples];
    PLFLT minX = setup->minX;
    PLFLT maxX = setup->maxX;
    PLFLT minY = setup->minY;
    PLFLT maxY = setup->maxY;

    for(uint32_t dex=0; dex<numSamples; dex++) {
        fx[dex] = ix[dex];
    }

    const char *yName = "";
    if(setup->yAxisName) {
        yName = setup->yAxisName;
    }
    const char *xName = "";
    if(setup->xAxisName) {
        xName = setup->xAxisName;
    }

    plsdev ("psc");

    /* standard: background white, foreground (axes, labels, etc.) black */
    plscolbg(PLOT_WHITE);
    plscol0(1, PLOT_BLACK);

    plsfnam(tmpFile);
    plsdiori(1.0);						// portrait
    plinit();
    plenv(minX, maxX, minY, maxY, 0, 0);
    pllab(xName, yName, graphName);

    for(uint32_t dex=0; dex<numLines; dex++) {
        uint32_t thisSamples = numSamples;
        if(skipTrailZeroes) {
            while((lineDef[dex].fy[thisSamples-1] == 0.0) && (thisSamples > 0)) {
                thisSamples--;
            }
            if(thisSamples == 0) {
                printf("***plplotLines: Warning: line with all zeroes skipped\n");
                continue;
            }
        }
        plotOneLine(thisSamples, fx,  &lineDef[dex], plotPoints, plotLine);
    }

    plend();

    int ourRtn = psToPdf(tmpFile, fileName);
    unlink(tmpFile);
    return ourRtn;
}
/*
 * Plot a histogram of prebinned data. X values are int32_t's, and the corresponding
 * Y values - the counts for each X - are uint32_t's.
 */
int plplotBins(
    uint32_t numBins,
    const int32_t *x,			/* numBins of X values, monotonically increasing */
    const uint32_t *y,			/* numBins of Y values for each associated X */
    const char *graphName,
    const char *fileName,
    const char *xAxisName,		/* optional */
    const char *yAxisName)		/* optional */
{
    char tmpFile[TEMP_FN_LEN];
    makeTempFile(tmpFile, TEMP_FN_LEN);

    PLINT totalBins = numBins + 2;

    /* PLFLT array of sample values */
    PLFLT *xf = (PLFLT *)malloc(totalBins * sizeof(PLFLT));

    /* these two will have Y values of zero */
    xf[0] = x[0] - 1;
    xf[totalBins - 1] = x[numBins-1] + 1;

    const int32_t *ip = x;
    PLFLT *op = xf + 1;
    for(uint32_t dex=0; dex<numBins; dex++) {
        *op++ = *ip++;
    }

    /* PLFLT array of bins */
    PLFLT *yf = (PLFLT *)malloc(totalBins * sizeof(PLFLT));
    yf[0] = 0.0;
    yf[totalBins - 1]  = 0.0;
    const uint32_t *uip = y;
    op = yf + 1;
    for(uint32_t dex=0; dex<numBins; dex++) {
        *op++ = *uip++;
    }

    /* get max Y value */
    uint32_t maxY = 0;
    uip = y;
    for(uint32_t dex=0; dex<numBins; dex++) {
        uint32_t currY = *uip++;
        if(currY > maxY) {
            maxY = currY;
        }
    }

    const char *yName = yAxisName ? yAxisName : "";
    const char *xName = xAxisName ? xAxisName : "";

#if	HIST_COLOR
    plsdev ("psc");
    plscolbg(255, 255, 255);			/* white background */
#else
    plsdev ("ps");
#endif
    plsfnam(tmpFile);
    plsdiori(1.0);						// portrait
    plinit();
    plenv(xf[0], xf[totalBins - 1], 0, maxY, 0, 0);

#if	HIST_COLOR
    /* can we alter colors of lines and the spaces inside the histograms? */
    plscolbg(255, 0, 0);				/* red background */
    plscol0(1, 255, 0, 0);				/* red foreground - no effect */
#endif

    pllab(xName, yName, graphName);

    plbin(totalBins, xf, yf, PL_BIN_CENTRED);
    plend();

    free(xf);
    free(yf);

    int ourRtn = psToPdf(tmpFile, fileName);
    unlink(tmpFile);
    return ourRtn;
}
/*
 * Plot a histogram showing the number of occurences of each possible
 * value of 'samples'.
 */
int plplotHist(
    const int32_t *samples,
    uint32_t numSamples,
    const char *graphName,
    const char *fileName,
    const char *xAxisName,		/* optional */
    const char *yAxisName)		/* optional */
{
    char tmpFile[TEMP_FN_LEN];
    makeTempFile(tmpFile, TEMP_FN_LEN);

    /* First determine the range, i.e. the number of bins */
    int32_t minSamp = samples[0];
    int32_t maxSamp = samples[0];
    for(uint32_t dex=0; dex<numSamples; dex++) {
        int32_t s = samples[dex];
        if(s < minSamp) {
            minSamp = s;
        }
        if(s > maxSamp) {
            maxSamp = s;
        }
    }

    /* When we specify PL_BIN_CENTRED, the min and max values are half the normal width */
    minSamp--;
    maxSamp++;

    PLINT numBins = maxSamp - minSamp + 1;

    /* One array containing the sample values, x */
    PLFLT *x = (PLFLT *)malloc(numBins * sizeof(PLFLT));
    int32_t binNum = minSamp;
    for(uint32_t dex=0; dex<(uint32_t)numBins; dex++) {
        x[dex] = binNum++;
    }

    /* Now make and fill the bins proper */
    PLFLT *y = (PLFLT *)malloc(numBins * sizeof(PLFLT));
    for(uint32_t dex=0; dex<(uint32_t)numBins; dex++) {
        y[dex] = 0;
    }
    PLFLT maxY = 0.0;

    for(uint32_t dex=0; dex<numSamples; dex++) {
        int32_t s = samples[dex];
        PLFLT *yp = y + s - minSamp;
        *yp += 1.0;
        if(*yp > maxY) {
            maxY = *yp;
        }
    }

    const char *yName = yAxisName ? yAxisName : "";
    const char *xName = xAxisName ? xAxisName : "";

#if	HIST_COLOR
    plsdev ("psc");
    plscolor(1);
    plscolbg(255, 255, 255);			/* white background */
#else
    plsdev ("ps");
#endif
    plsfnam(tmpFile);
    plsdiori(1.0);						// portrait
    plinit();
    plenv(minSamp, maxSamp, 0, maxY, 0, 0);

#if	HIST_COLOR
    /* can we alter colors of lines and the spaces inside the histograms? */
    plscolbg(255, 0, 0);				/* red background */
    plscol0(1, 255, 0, 0);				/* red foreground - no effect */
#endif

    pllab(xName, yName, graphName);

    plbin(numBins, x, y, PL_BIN_CENTRED);
    plend();

    free(x);
    free(y);

    int ourRtn = psToPdf(tmpFile, fileName);
    unlink(tmpFile);
    return ourRtn;
}
Esempio n. 8
0
static void MainLoop(void)
{
	uint ip = 0;

	if(!DoLock())
		return;

	removeFileIfExist(ParamsFile);
	removeFileIfExist(AnswerFile);

	mutexRelease(MutexHandle);

	SockStartup();
	cout("START\n");

	while(!collectEvents(StopAppEventHandle, 0))
	{
		collectEvents(StartEventHandle, 3000);

		if(!DoLock())
			break;

		if(existFile(ParamsFile))
		{
			char *ansFile;

			if(Serializer)
			{
				char *wrkFile = makeTempFile(NULL);

				Serializer(ParamsFile, wrkFile);
				removeFile(ParamsFile);
				moveFile(wrkFile, ParamsFile);
				memFree(wrkFile);
			}
			removeFileIfExist(AnswerFile); // Cleanup
			mutexRelease(MutexHandle);

			cout("REQUEST START %I64u\n", getFileSize(ParamsFile));
			ansFile = sockClient((uchar *)&ip, ServerDomain, ServerPort, ParamsFile, Idle);
			cout("REQUEST END %08x\n", ansFile);

			if(StopAppEventCaught || !DoLock())
			{
				if(ansFile)
				{
					removeFile(ansFile);
					memFree(ansFile);
				}
				break;
			}
			removeFileIfExist(AnswerFile); // Cleanup (2bs)

			if(ansFile)
			{
				if(Deserializer)
				{
					Deserializer(ansFile, AnswerFile);
					removeFile(ansFile);
				}
				else
					moveFile(ansFile, AnswerFile);

				memFree(ansFile);
				cout("ANSWER %I64u\n", getFileSize(AnswerFile));
			}
			removeFileIfExist(ParamsFile); // Cleanup
			eventSet(AnswerEventHandle); // リクエストの完了(応答)を通知
		}
		mutexRelease(MutexHandle);
	}
	cout("END\n");
	SockCleanup();

	if(handleWaitForMillis(MutexHandle, 2000))
	{
		// Cleanup
		removeFileIfExist(ParamsFile);
		removeFileIfExist(AnswerFile);

		mutexRelease(MutexHandle);
	}
}