Esempio n. 1
0
int main(int argc, char *argv[])
{
    int rc = 0;

    if (argc == 3 || argc == 5)
    {
        struct stat DirStat;
        if (   stat(argv[2], &DirStat) == 0
            && S_ISDIR(DirStat.st_mode))
        {
            char   *pszContent;
            rc = readFile(argv[1], &pszContent, NULL);
            if (!rc)
            {
                FILE *pFileList = NULL;
                if (argc == 5)
                    rc = openMakefileList(argv[3], argv[4], &pFileList);

                if (argc < 4 || pFileList)
                    rc = splitFile(argv[2], pszContent, pFileList);

                if (pFileList)
                    rc = closeMakefileList(pFileList, rc);
                free(pszContent);
            }
        }
        else
            rc = printErr("Given argument \"%s\" is not a valid directory.\n", argv[2]);
    }
    else
        rc = printErr("Syntax error: usage: filesplitter <infile> <outdir> [<list.kmk> <kmkvar>]\n");
    return rc;
}
Esempio n. 2
0
/**
 * Create Diffie-Hellman parameters and save them to files.
 * Compute the shared secret and computed key from the received other public key.
 * Save shared secret and computed key to file.
 */
bool replyToFirstMessage() {
    cout << "Reply To First Message" << endl;

    // Split received file into separate files
    vector<string> outputFiles;
    outputFiles.push_back(OTHER_FIRST_MESSAGE_RANDOM_NUMBER);
    outputFiles.push_back(OTHER_PUBLIC_KEY_FILE_NAME);
    vector<int> bytesPerFile;
    bytesPerFile.push_back(RANDOM_NUMBER_SIZE);
    splitFile(FIRST_MESSAGE_FILE_NAME, outputFiles, bytesPerFile);

    // Read in received Diffie-Hellman public key from file
    SecByteBlock otherPublicKey;
    readFromFile(OTHER_PUBLIC_KEY_FILE_NAME, otherPublicKey);

    // Read in received random number from file
    SecByteBlock otherFirstMessageRandomNumber;
    readFromFile(OTHER_FIRST_MESSAGE_RANDOM_NUMBER, otherFirstMessageRandomNumber);

    // Prepare Diffie-Hellman parameters
    SecByteBlock publicKey;
    SecByteBlock privateKey;
    generateDiffieHellmanParameters(publicKey, privateKey);

    // Write public key and private key to files
    writeToFile(PRIVATE_KEY_FILE_NAME, privateKey);
    writeToFile(PUBLIC_KEY_FILE_NAME, publicKey);

    // Compute shared secret
    SecByteBlock sharedSecret;
    if (!diffieHellmanSharedSecretAgreement(sharedSecret, otherPublicKey, privateKey)) {
        cerr << "Security Error in replyToFirstMessage. Diffie-Hellman shared secret could not be agreed to." << endl;
        return false;
    }

    // Compute key from shared secret
    SecByteBlock key;
    generateSymmetricKeyFromSharedSecret(key, sharedSecret);

    // Write shared secret and computed key to file
    writeToFile(SHARED_SECRET_FILE_NAME, sharedSecret);
    writeToFile(COMPUTED_KEY_FILE_NAME, key);

    // Generate random number
    SecByteBlock firstMessageRandomNumber;
    generateRandomNumber(firstMessageRandomNumber, RANDOM_NUMBER_SIZE);

    // Write random number to file
    writeToFile(FIRST_MESSAGE_RANDOM_NUMBER_FILE_NAME, firstMessageRandomNumber);

    vector<string> inputFileNames;
    inputFileNames.push_back(FIRST_MESSAGE_RANDOM_NUMBER_FILE_NAME);
    inputFileNames.push_back(PUBLIC_KEY_FILE_NAME);
    combineFiles(inputFileNames, FIRST_MESSAGE_REPLY_FILE_NAME);

    return true;
}
Esempio n. 3
0
int main(int argc, char* argv[]) {
    parseArgs(argc, argv);
    openInputFile();
    openOutputFiles();
    skipLines();
    splitFile();
    fprintf(stdout, "Lines Skipped: %ld / Lines Processed: %llu\n", _linesSkipped, _linesProcessed);
    closeOutputFiles();
    closeInputFile();
    exit(EXIT_SUCCESS);
}
Esempio n. 4
0
/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  main
 *  Description:  仅实现了分解文件的功能
 * =====================================================================================
 */
int unpackcpb_main(int argc, char *argv[])
{
			if(argv[1] && argv[2]){
			if(exist(argv[2])){
    	mkdir(argv[2], 0777);
    }
				chdir(argv[2]);
    	return splitFile(argv[1]);
    }
    return 1;
}
Esempio n. 5
0
int main(int argc, char *argv[])
/* Process command line. */
{
cgiSpoof(&argc, argv);
if (argc != 4)
    usage();
if (!isdigit(argv[2][0]))
    usage();
splitFile(argv[1], atoi(argv[2]), argv[3], cgiOptionalString("head"), cgiOptionalString("tail"));
return 0;
}
Esempio n. 6
0
/* 
 * ===  FUNCTION  ======================================================================
 *         Name:  main
 *  Description:  仅实现了分解文件的功能
 * =====================================================================================
 */
int main ( int argc, char *argv[] )
{
    if (argc==1) {
        printf("usage:%s cpb file.\n", argv[0]);
        exit(0);
    }
    
    char *cpb;
    cpb=argv[1];
    splitFile(cpb);

    return EXIT_SUCCESS;
}
Esempio n. 7
0
File: dll.c Progetto: Auzzy/school
void fileDLLToNet(char* filename)
{
	char* header = NULL;
	int headerLen = 0;
	char* data = NULL;
	int dataLen = readBMP(filename,&header,&headerLen,&data);

	if (dataLen >0)
	{
		message headerMessage;
		headerMessage.code = END_HEADER;
		headerMessage.data = (char*)malloc(headerLen*sizeof(char));
		memcpy(headerMessage.data,header,headerLen);
		headerMessage.dataLen = headerLen;

		packet* headerPackets = NULL;
		int headerPacketsLen = splitHeader(headerMessage,&headerPackets);

		int k;
		for (k = 0; k<headerPacketsLen; k++)
		{
			fromDLL(headerPackets[k]);
			free(headerPackets[k].mess.data);
		}
		free(headerPackets);
		free(headerMessage.data);
		
		message dataMessage;
		dataMessage.code = END_FILE;
		dataMessage.data = (char*)malloc(dataLen*sizeof(char));
		memcpy(dataMessage.data,data,dataLen);
		dataMessage.dataLen = dataLen;

		packet* dataPackets = NULL;
		int dataPacketsLen = splitFile(dataMessage,&dataPackets);

		for (k = 0; k<dataPacketsLen; k++)
		{
			fromDLL(dataPackets[k]);
			free(dataPackets[k].mess.data);
		}
		free(dataPackets);
		free(dataMessage.data);
	}
	else
		printf("ERROR: Test file doesn't exist.\n");
	
	free(header);
	free(data);
}
Esempio n. 8
0
int main(int argc, char* argv[]){
    int ret;
    AppContext ctx;

    ret = parseArgs(argc, argv, ctx);

    CHECK_RETURN(ret);

    loadSegLib(ctx.dictionary, ctx.segmentContxt);

    ret = splitFile(ctx);

    destorySegLib(ctx.segmentContxt);
    doExit(ctx);
    return ret;
}
Esempio n. 9
0
/**
 * Process public key and random number received from other user.
 * Encrypt facial recognition parameters.
 */
bool thirdMessage() {
    cout << "Third Message" << endl;

    // Split received file into random number and public key
    vector<string> outputFiles;
    outputFiles.push_back(OTHER_FIRST_MESSAGE_RANDOM_NUMBER);
    outputFiles.push_back(OTHER_PUBLIC_KEY_FILE_NAME);
    vector<int> bytesPerFile;
    bytesPerFile.push_back(RANDOM_NUMBER_SIZE);
    splitFile(FIRST_MESSAGE_REPLY_FILE_NAME, outputFiles, bytesPerFile);

    // Read from file
    SecByteBlock privateKey;
    SecByteBlock otherPublicKey;
    readFromFile(PRIVATE_KEY_FILE_NAME, privateKey);
    readFromFile(OTHER_PUBLIC_KEY_FILE_NAME, otherPublicKey);

    // Compute shared secret
    SecByteBlock sharedSecret;
    if (!diffieHellmanSharedSecretAgreement(sharedSecret, otherPublicKey, privateKey)) {
        cerr << "Security Error in thirdMessage. Diffie-Hellman shared secret could not be agreed to." << endl;
        return false;
    }

    // Compute key from shared secret
    SecByteBlock key;
    generateSymmetricKeyFromSharedSecret(key, sharedSecret);

    // Write shared secret and computed key to file
    writeToFile(SHARED_SECRET_FILE_NAME, sharedSecret);
    writeToFile(COMPUTED_KEY_FILE_NAME, key);

    // Read in the current initialization vector from file
    byte curIv[AES::BLOCKSIZE];
    // TODO: actually read it in
    // Set to 0 for now
    memset(curIv, 0, AES::BLOCKSIZE);

    // Encrypt facial recognition params
    encryptFile(FACIAL_RECOGNITION_FILE_NAME, 
            THIRD_MESSAGE_FILE_NAME, 
            key, curIv);
    return true;
}
Esempio n. 10
0
int main(int nargs,char *argv[])
{
	FILE *fp;
	FILE *outPut;
	struct stat fileStat;
	struct stat fileStat1;
	if(nargs != 5)
	{
		printf("ERROR:Not enough parameters. you dun goofed bro!!!!\n");
		exit(1);
	}
	else
	{	
		if(strcmp(argv[1],argv[4]) == 0)
		{
		 printf("ERROR:Can not output to input file\n");
		 exit(1);
		}
		//num of print to console
     int numPrint;
	  //num of process to fork
		 int numProc;
		if(sscanf(argv[2],"%i",&numPrint) != 1 || sscanf(argv[2],"%i",&numPrint) != 1)
		{
				printf("ERROR: parameter 2 and 3 have to be integers!\n");
					exit(1);
		}
    numPrint = atoi(argv[2]);
    numProc = atoi(argv[3]);

    if(stat(argv[1],&fileStat) < 0)
		{
			printf("ERROR:FILE OR DIRECTORY NOT EXIST\n");    
        exit(1);
		}
		if(stat(argv[4],&fileStat1) == 0)
		{
			printf("ERROR:OUTPUT FILE EXIST AND WILL BE OVER WRITTEN\n");    
        exit(1);
		}
		else
		{
			char *ftp = argv[4];
    	outPut = fopen(ftp,"w+"); 
		}

    

    if(outPut == NULL)
    {
     printf("ERROR: couldnt open file for writing\n");
     exit(1);

    }
//if command line read in a file or read in a directory
			if (S_ISREG(fileStat.st_mode) && (fileStat.st_mode & S_IRUSR))
			{
				
				       //get file size
				
          unsigned int fileSize = fileStat.st_size;
          int fd = open(argv[1],O_RDONLY);
					char *file = NULL;
					printf("FileSize: %d\n", fileSize);
				
					int i;
          fp = fopen(argv[1], "r");
						 if(fp == NULL)
					   {
						  printf("ERROR: Couldnt open file might not exist!!!!!\n");
						  exit(1);
					   }
						//mmap the file
					 	file = splitFile(fp,fileSize,numProc,fd);
						int length = fileSize/numProc;
						int j;
						//usable stuff
						int startingIndex[numProc];
						int endingIndex[numProc];
						int tempPosition = 0;

						//moving starting and ending points
						for(i = 0; i < numProc; i++)
						{
							startingIndex[i] = i * length;
							endingIndex[i] = (i * length) + length;
						}

						for(i = 1; i < numProc; i++)
						{
								while(file[startingIndex[i]] != ' ' && file[startingIndex[i]] != '\t' && file[startingIndex[i]] != '\n')
								{
									
									startingIndex[i]++;
									endingIndex[i-1]++;
								}
						}

						for(i = 0; i < numProc; i++)
						{
							printf("block number:%d\n",i);
								//call child stuff 
							for(j = startingIndex[i]; j < endingIndex[i]; j++)
							{
								//filter
								char tempAry[500];
								tempPosition = 0; 
								//cut out the white space caused by having to move the starting and ending indexs
								//printf("file[startingIndex[i]]: %c\n",file[startingIndex[i]]);
								while(file[j] != ' ' && file[j] != '\t' && file[j] != '\n' && j < endingIndex[i])
								{
											tempAry[tempPosition] = file[j];
											tempPosition++;
											j++;
												
								}
								tempAry[tempPosition] = '\0';
								
								if(strlen(tempAry) > 0)	
								{
									if(wordfilter(tempAry) == TRUE)
									{
										printf("%s ",tempAry);
								  }	
									
								}	
										
							}
							printf("\n");			
						}
					
				   readFILE(fp);
	         sortLinkedList();   			
		       printList(numPrint,outPut);
           fclose(fp);
					 freeAll();
			}
			else if (S_ISDIR(fileStat.st_mode) && (fileStat.st_mode & S_IRUSR))
			{
			//printf("This is a directory and you can read noice\n");
           searchdir(argv[1],0);
					 sortLinkedList();
					 printList(numPrint,outPut);
					 freeAll();
   		 }
			else
			{
				printf("ERROR:your input is either not a file, not a directory,or the input could not be read please check permissions on file and re-run if you believe a error has occured"); 
			}
	}
 fclose(outPut);
    
return 0;
}
Esempio n. 11
0
int main(int argc, char **argv)
try {
	if (argc != 3) {
		printUsageAndDie(argv[0]);
	}

	std::ifstream origFile(argv[1]);
	if (! origFile) {
		throw std::runtime_error("Cannot open " + std::string(argv[1]));
	}

	std::ifstream splitFile(argv[2]);
	if (! splitFile) {
		throw std::runtime_error("Cannot open " + std::string(argv[2]));
	}

	unsigned startPos = 0;
	int origPos = 0;
	std::string line;

	unsigned lineNo = 0;

	while (std::getline(splitFile, line)) {

		++lineNo;
		
		unsigned endPos = startPos + line.size();

		unsigned origStartPos;

		if (line.size() == 0) { /// empty line
			origStartPos = origPos;
		}
		else {
			
			/// collect heading spaces
			std::string headingWs;
			for (std::string::const_iterator ch = line.begin();
					ch != line.end(); ++ch) {
				if (std::isspace(*ch)) {
					headingWs += *ch;
				}
				else {
					break;
				}
			}

			// if (line[0] == ' ') {
			//	throw std::runtime_error(
			//		"Error: Split text contains a heading space");
			// }

			/// skip newlines in the original file
			char origChar;
			while (origFile.get(origChar)) {

				++origPos;

				if (origChar != '\n') {
					origFile.unget();
					--origPos;
					break;
				}
			}

			/// collect spaces in the original file
			std::string origWs;
			while (origFile.get(origChar)) {

				++origPos;

				if (origChar == ' ' || origChar == '\t') {
					origWs += origChar;
				}
				else {
					origFile.unget();
					--origPos;
					break;
				}
			}

			#if 0
			/// skip spaces and newlines in the original file
			char origChar;
			do {
				if (! origFile.get(origChar)) {
					throw std::runtime_error(
						"Split/original text do not match");
				}
				++origPos;
			} while (origChar == ' ' || origChar == '\n');

			origFile.unget();
			--origPos;
			#endif

			/// check if the heading space part has a corresponding region
			/// in the original file
			if (origWs.size() < headingWs.size()) {
				throw std::runtime_error("Split/original text do not match");
			}

			unsigned skipLen = origWs.size() - headingWs.size();

			if (! headingWs.empty() && headingWs != origWs.substr(skipLen)) {
				throw std::runtime_error("Split/original text do not match");
			}

			origStartPos = origPos - headingWs.size();

			/// Check the identity for the rest of the line
			for (unsigned i = headingWs.size(); i < line.size(); ++i) {
				if (! origFile.get(origChar) || origChar != line[i]) {
					std::string msg = errorMsgLong(
						origPos, origChar, lineNo, i, line[i]);
					throw std::runtime_error(msg);
				}
				++origPos;
			}
		}

		std::cout << startPos << '\t' << endPos << '\t' << origStartPos
				<< std::endl;

		/// +1 to skip the new line character
		startPos = endPos + 1;
	}

	return 0;
}
catch (std::runtime_error &e)
{
	std::cerr << e.what() << std::endl;
	exit(1);
}
catch (...) {
	std::cerr << "Unknown exception" << std::endl;
	exit(1);
}