static int _testLoadPluginWithAbsolutePath(const char *testName, const CharString applicationPath, const CharString resourcesPath) { File resourcesFile = newFileWithPath(resourcesPath); CharString vstDirName = newCharStringWithCString("vst"); File vstFile = newFileWithParent(resourcesFile, vstDirName); freeFile(resourcesFile); freeCharString(vstDirName); PlatformInfo platformInfo = newPlatformInfo(); CharString platformDirName = newCharStringWithCString(_getPlatformVstDirName(platformInfo)); File vstPlatformFile = newFileWithParent(vstFile, platformDirName); freeCharString(platformDirName); freePlatformInfo(platformInfo); freeFile(vstFile); CharString vstPluginName = newCharStringWithCString("again"); charStringAppendCString(vstPluginName, _getVst2xPlatformExtension()); File pluginFile = newFileWithParent(vstPlatformFile, vstPluginName); freeCharString(vstPluginName); freeFile(vstPlatformFile); CharString inputPath = getDefaultInputPath(resourcesPath); int result = runIntegrationTest( testName, buildTestArgumentString("--plugin \"%s\" --input \"%s\"", pluginFile->absolutePath->data, inputPath->data), RETURN_CODE_SUCCESS, kTestOutputPcm, applicationPath, resourcesPath); freeCharString(inputPath); freeFile(pluginFile); return result; }
void SslContext::setKeyFile(const std::string& filename) { if (error_) return; if (!enabled) return; TRACE("SslContext::setKeyFile: \"%s\"", filename.c_str()); gnutls_datum_t data; if ((error_ = loadFile(data, filename))) { log(x0::Severity::error, "Error loading private key file(%s): %s", filename.c_str(), error_.message().c_str()); return; } int rv; if ((rv = gnutls_x509_privkey_init(&x509PrivateKey_)) < 0) { freeFile(data); return; } if ((rv = gnutls_x509_privkey_import(x509PrivateKey_, &data, GNUTLS_X509_FMT_PEM)) < 0) { TRACE("setKeyFile: failed to import key as x509-fmt-pem. trying pkcs-plain."); rv = gnutls_x509_privkey_import_pkcs8(x509PrivateKey_, &data, GNUTLS_X509_FMT_PEM, nullptr, GNUTLS_PKCS_PLAIN); } if (rv < 0) { log(x0::Severity::error, "Error loading private key file(%s): %s", filename.c_str(), gnutls_strerror(rv)); freeFile(data); return; } freeFile(data); TRACE("setKeyFile: success."); }
boolByte programOptionsParseConfigFile(ProgramOptions self, const CharString filename) { boolByte result = false; File configFile = NULL; LinkedList configFileLines = NULL; CharString* argvCharStrings; int argc; char** argv; int i; if(filename == NULL || charStringIsEmpty(filename)) { logCritical("Cannot read options from empty filename"); return false; } configFile = newFileWithPath(filename); if(configFile == NULL || configFile->fileType != kFileTypeFile) { logCritical("Cannot read options from non-existent file '%s'", filename->data); freeFile(configFile); return false; } configFileLines = fileReadLines(configFile); if(configFileLines == NULL) { logInternalError("Could not split config file lines"); return false; } else if(linkedListLength(configFileLines) == 0) { logInfo("Config file '%s' is empty", filename->data); freeLinkedList(configFileLines); freeFile(configFile); return true; } else { // Don't need the file anymore, it can be freed here freeFile(configFile); } argvCharStrings = (CharString*)linkedListToArray(configFileLines); argc = linkedListLength(configFileLines); argv = (char**)malloc(sizeof(char*) * (argc + 1)); // Normally this would be the application name, don't care about it here argv[0] = NULL; for(i = 0; i < argc; i++) { argv[i + 1] = argvCharStrings[i]->data; } argc++; result = programOptionsParseArgs(self, argc, argv); freeLinkedListAndItems(configFileLines, (LinkedListFreeItemFunc)freeCharString); free(argvCharStrings); free(argv); return result; }
void errorReporterRemapPath(ErrorReporter self, CharString path) { File pathAsFile = newFileWithPath(path); CharString basename = fileGetBasename(pathAsFile); File parent = newFileWithPath(self->reportDirPath); File remappedPath = newFileWithParent(parent, basename); charStringCopy(path, remappedPath->absolutePath); freeCharString(basename); freeFile(parent); freeFile(pathAsFile); freeFile(remappedPath); }
void SslContext::setCertFile(const std::string& filename) { if (error_) return; if (!enabled) return; TRACE("SslContext::setCertFile: \"%s\"", filename.c_str()); gnutls_datum_t data; error_ = loadFile(data, filename); if (error_) { log(x0::Severity::error, "Error loading certificate file(%s): %s", filename.c_str(), error_.message().c_str()); return; } numX509Certs_ = sizeof(x509Certs_) / sizeof(*x509Certs_); // 8 int rv; rv = gnutls_x509_crt_list_import(x509Certs_, &numX509Certs_, &data, GNUTLS_X509_FMT_PEM, 0); if (rv < 0) TRACE("gnutls_x509_crt_list_import: \"%s\"", gnutls_strerror(rv)); for (unsigned i = 0; i < numX509Certs_; ++i) { // read Common Name (CN): TRACE("retrieving Common Name"); std::size_t len = 0; rv = gnutls_x509_crt_get_dn_by_oid(x509Certs_[i], GNUTLS_OID_X520_COMMON_NAME, 0, 0, nullptr, &len); if (rv == GNUTLS_E_SHORT_MEMORY_BUFFER && len > 1) { char *buf = new char[len + 1]; rv = gnutls_x509_crt_get_dn_by_oid(x509Certs_[i], GNUTLS_OID_X520_COMMON_NAME, 0, 0, buf, &len); if (rv < 0) TRACE("gnutls_x509_crt_get_dn_by_oid: \"%s\"", gnutls_strerror(rv)); certCN_ = buf; delete[] buf; TRACE("setCertFile: Common Name: \"%s\"", certCN_.c_str()); } // read Subject Alternative-Name: TRACE("retrieving Alternative-Name"); for (int k = 0; !(rv < 0); ++k) { len = 0; rv = gnutls_x509_crt_get_subject_alt_name(x509Certs_[i], k, nullptr, &len, nullptr); if (rv == GNUTLS_E_SHORT_MEMORY_BUFFER && len > 1) { char *buf = new char[len + 1]; rv = gnutls_x509_crt_get_subject_alt_name(x509Certs_[i], k, buf, &len, nullptr); if (rv < 0) TRACE("gnutls_x509_crt_get_subject_alt_name: \"%s\"", gnutls_strerror(rv)); buf[len] = '\0'; if (rv == GNUTLS_SAN_DNSNAME) dnsNames_.push_back(buf); TRACE("setCertFile: Subject: \"%s\"", buf); delete[] buf; } } } freeFile(data); TRACE("setCertFile: success."); }
int main(int argc, char** argv) { int err, strSize, wordsSize, symSize; char** file; FILE* in; int i; int j = 0; if(argc != 2) { perror("Bad arguments\n"); exit(1); } in = fopen(argv[1], "r"); file = scanFile(in, &err, &strSize, &wordsSize, &symSize); if(err == 3) { perror("Bad alloc\n"); freeFile(in, file, strSize); exit(3); } if(err == 4) { perror("Bad input file\n"); freeFile(in, file, strSize); exit(4); } if(err == 5) { perror("Bad file format\n"); freeFile(in, file, strSize); exit(5); } printf("Number of strings: %d\nNumber of words: %d\nNumber of symbols: %d\n", strSize, wordsSize, symSize + strSize); sort(file, strSize); for(i = 0; i < strSize; ++i) { j = 0; while(file[i][j] != '\n') { printf("%c", file[i][j]); ++j; } printf("\n"); } freeFile(in, file, strSize); return 0; }
static SampleSourceType _sampleSourceGuess(const CharString sampleSourceName) { File sourceFile = NULL; CharString sourceFileExtension = NULL; SampleSourceType result = SAMPLE_SOURCE_TYPE_PCM; if (sampleSourceName == NULL || charStringIsEmpty(sampleSourceName)) { result = SAMPLE_SOURCE_TYPE_SILENCE; } else { // Look for stdin/stdout if (strlen(sampleSourceName->data) == 1 && sampleSourceName->data[0] == '-') { result = SAMPLE_SOURCE_TYPE_PCM; } else { sourceFile = newFileWithPath(sampleSourceName); sourceFileExtension = fileGetExtension(sourceFile); freeFile(sourceFile); // If there is no file extension, then automatically assume raw PCM data. Deal with it! if (charStringIsEmpty(sourceFileExtension)) { result = SAMPLE_SOURCE_TYPE_PCM; } // Possible file extensions for raw PCM data else if (charStringIsEqualToCString(sourceFileExtension, "pcm", true) || charStringIsEqualToCString(sourceFileExtension, "raw", true) || charStringIsEqualToCString(sourceFileExtension, "dat", true)) { result = SAMPLE_SOURCE_TYPE_PCM; } #if USE_AUDIOFILE else if (charStringIsEqualToCString(sourceFileExtension, "aif", true) || charStringIsEqualToCString(sourceFileExtension, "aiff", true)) { result = SAMPLE_SOURCE_TYPE_AIFF; } #endif #if USE_FLAC else if (charStringIsEqualToCString(sourceFileExtension, "flac", true)) { result = SAMPLE_SOURCE_TYPE_FLAC; } #endif else if (charStringIsEqualToCString(sourceFileExtension, "wav", true) || charStringIsEqualToCString(sourceFileExtension, "wave", true)) { result = SAMPLE_SOURCE_TYPE_WAVE; } else { logCritical("Sample source '%s' does not match any supported type", sampleSourceName->data); result = SAMPLE_SOURCE_TYPE_INVALID; } } } freeCharString(sourceFileExtension); return result; }
boolByte errorReportCopyFileToReport(ErrorReporter self, CharString path) { boolByte success; // Copy the destination path so that the original is not modified CharString destination = newCharString(); charStringCopy(destination, path); errorReporterRemapPath(self, destination); File reportDirPath = newFileWithPath(self->reportDirPath); File distinationPath = newFileWithPath(path); File result = fileCopyTo(distinationPath, reportDirPath); success = fileExists(result); freeCharString(destination); freeFile(reportDirPath); freeFile(distinationPath); freeFile(result); return success; }
static void _removeOutputFile(const char *argument) { CharString outputFilename = newCharStringWithCString(argument); File outputFile = newFileWithPath(outputFilename); if (fileExists(outputFile)) { fileRemove(outputFile); } freeCharString(outputFilename); freeFile(outputFile); }
int runIntegrationTest(const char *testName, CharString testArguments, ReturnCode expectedResultCode, const TestOutputType testOutputType, const CharString mrsWatsonExePath, const CharString resourcesPath) { int result = 0; int returnCode; ReturnCode resultCode; ChannelCount failedAnalysisChannel; SampleCount failedAnalysisFrame; #if WINDOWS STARTUPINFOA startupInfo; PROCESS_INFORMATION processInfo; #endif // Remove files from a previous test run File outputFolder = newFileWithPathCString(kApplicationRunnerOutputFolder); if (fileExists(outputFolder)) { _removeOutputFiles(testName); } else { fileCreate(outputFolder, kFileTypeDirectory); } freeFile(outputFolder); if (mrsWatsonExePath == NULL) { return -1; } else { File mrsWatsonExe = newFileWithPath(mrsWatsonExePath); boolByte mrsWatsonExeExists = fileExists(mrsWatsonExe); freeFile(mrsWatsonExe); if (!mrsWatsonExeExists) { freeCharString(testArguments); return -1; } } if (resourcesPath == NULL) { freeCharString(testArguments); return -1; } else { File resourcesFile = newFileWithPath(resourcesPath); boolByte resourcesExists = fileExists(resourcesFile); freeFile(resourcesFile); if (!resourcesExists) { freeCharString(testArguments); return -1; } } // Create the command line argument CharString arguments = newCharStringWithCapacity(kCharStringLengthLong); charStringAppendCString(arguments, "\""); charStringAppend(arguments, mrsWatsonExePath); charStringAppendCString(arguments, "\""); charStringAppendCString(arguments, " "); CharString defaultArguments; CharString outputFilename = getTestOutputFilename(testName, testOutputType); defaultArguments = _getDefaultArguments(testName, resourcesPath, outputFilename); charStringAppend(arguments, defaultArguments); freeCharString(defaultArguments); charStringAppendCString(arguments, " "); charStringAppend(arguments, testArguments); // Although testArguments is passed into this function (and hence, it would // generally not take ownership of it), in this case we free the arguments // here to make writing the test cases simpler and reduce the amount of // boilerplate code. freeCharString(testArguments); #if WINDOWS memset(&startupInfo, 0, sizeof(startupInfo)); memset(&processInfo, 0, sizeof(processInfo)); startupInfo.cb = sizeof(startupInfo); returnCode = CreateProcessA( (LPCSTR)(mrsWatsonExePath->data), (LPSTR)(arguments->data), 0, 0, false, CREATE_DEFAULT_ERROR_MODE, 0, 0, &startupInfo, &processInfo); if (returnCode) { // TODO: Check return codes for these calls WaitForSingleObject(processInfo.hProcess, kApplicationRunnerWaitTimeoutInMs); GetExitCodeProcess(processInfo.hProcess, (LPDWORD)&resultCode); CloseHandle(processInfo.hProcess); CloseHandle(processInfo.hThread); } else { logCritical("Could not launch process, got error %s", stringForLastError(GetLastError())); return 1; } #else returnCode = system(arguments->data); resultCode = (ReturnCode)WEXITSTATUS(returnCode); #endif freeCharString(arguments); if (resultCode == RETURN_CODE_FORK_FAILED || resultCode == RETURN_CODE_SHELL_FAILED || resultCode == RETURN_CODE_LAUNCH_FAILED_OTHER) { logCritical("Could not launch shell, got return code %d\n\ Please check the executable path specified in the --mrswatson-path argument.", resultCode); return 1; } else if (resultCode == expectedResultCode) { CharString failedAnalysisFunctionName = newCharString(); if (testOutputType != kTestOutputNone) { if (analyzeFile(outputFilename->data, failedAnalysisFunctionName, &failedAnalysisChannel, &failedAnalysisFrame)) { // TODO: // if (!testEnvironment->results->keepFiles) { // _removeOutputFiles(testName); // } result = 0; } else { fprintf(stderr, "Audio analysis check for %s failed at frame %lu, channel %d. ", failedAnalysisFunctionName->data, failedAnalysisFrame, failedAnalysisChannel); result = 1; } } else { result = 0; // TODO: // if (!testEnvironment->results->keepFiles) { // _removeOutputFiles(testName); // } } freeCharString(failedAnalysisFunctionName); } else { fprintf(stderr, "Expected result code %d (%s), got %d (%s). ", expectedResultCode, _getResultCodeString(expectedResultCode), resultCode, _getResultCodeString(resultCode)); result = 1; } freeCharString(outputFilename); return result; }
void nameFilesFolder(int sid) { infoFile **files; int i=0, numberFile=0; DIR *d; struct dirent *dir; d = opendir(configstruct.rootPath); if (d) { while ((dir = readdir(d)) != NULL) { if( strcmp(dir->d_name, ".")!=0 && strcmp(dir->d_name, "..")!=0 ) i++; } } closedir(d); d = opendir(configstruct.rootPath); if (d) { files = (infoFile**) malloc( i*sizeof(infoFile*) ); i=0; while ((dir = readdir(d)) != NULL) { if( strcmp(dir->d_name, ".")!=0 && strcmp(dir->d_name, "..")!=0 ) { files[i] = readFile(configstruct.rootPath, dir->d_name); printf("%2d. %s %lu\n", i, files[i]->name, files[i]->size); i++; } } } closedir(d); printf("\nNumer pliku do wysłania : "); scanf("%d", &numberFile); printf("===== WYSYŁAM ====="); showFile( files[numberFile] ); sendFile(sid, files[numberFile] ); enum odp o; recv(sid, &o, sizeof(enum odp), 0); if (o==NOWY) { printf("%s : Utworzono nowy plik na serwerze\n\n", files[numberFile]->name); } else if (o==NIEZNANY) { printf("%s : Nieznana wersja pliku\n\n", files[numberFile]->name); printf("===== CO CHCZESC ZROBIĆ =====\n"); printf("0 - Anuluj\n"); printf("1 - Zapisz jako najnowszą wersję\n"); printf("2 - Zapisz pod nową nazwą\n"); printf("3 - Pobierz najnowsza wersje (zastąp aktualną)\n"); printf("4 - Pobierz najnowsza wersje pod nową nazwą\n"); while (1==1) { char wybor; scanf("%c", &wybor); if (wybor=='0') { enum odp o2 = NONE; send(sid, &o2, sizeof(enum odp), 0); break; } else if (wybor=='1') { enum odp o2 = NOWA_WERSJA; send(sid, &o2, sizeof(enum odp), 0); break; } else if (wybor=='2') { printf("Nowa nazwa plik : "); scanf("%s", files[numberFile]->name); enum odp o2 = NOWY_PLIK; send(sid, &o2, sizeof(enum odp), 0); sendFile(sid, files[numberFile]); break; } else if (wybor=='3') { enum odp o2 = POBIERZ_PLIK; send(sid, &o2, sizeof(enum odp), 0); infoFile *file2 = receiveFile(sid); showFile(file2); writeFile(configstruct.rootPath, file2); freeFile(file2); break; } else if (wybor=='4') { enum odp o2 = POBIERZ_PLIK_NAZWA; send(sid, &o2, sizeof(enum odp), 0); infoFile *file = receiveFile(sid); printf("Podaj nową nazwę pliku : "); scanf("%s", file->name); writeFile(configstruct.rootPath, file); freeFile(file); break; } } } else if (o==AKTUALNY) { printf("%s : To jest najnowsza wersja pliku\n\n", files[numberFile]->name); } else if (o==STARY) { printf("%s : To jest stara wersja pliku\n\n", files[numberFile]->name); } }
void errorReporterInitialize(ErrorReporter self) { CharString infoText = newCharStringWithCString(kErrorReportInfoText); CharString wrappedInfoText; time_t now; size_t length; size_t i; printf("=== Starting error report ===\n"); wrappedInfoText = charStringWrap(infoText, 0); // The second newline here is intentional printf("%s\n", wrappedInfoText->data); time(&now); self->started = true; snprintf(self->reportName->data, self->reportName->capacity, "MrsWatson Report %s", ctime(&now)); // Trim the final newline character from this string if it exists length = strlen(self->reportName->data); if (self->reportName->data[length - 1] == '\n') { self->reportName->data[length - 1] = '\0'; length--; } for (i = 0; i < length; i++) { if (!(charStringIsLetter(self->reportName, i) || charStringIsNumber(self->reportName, i))) { self->reportName->data[i] = '-'; } } #if UNIX snprintf(self->desktopPath->data, self->desktopPath->capacity, "%s/Desktop", getenv("HOME")); #elif WINDOWS SHGetFolderPathA(NULL, CSIDL_DESKTOPDIRECTORY, NULL, 0, self->desktopPath->data); #endif // Try to place the report on the user's desktop. However, if we cannot find // the desktop (which may occur with localized Linux installations, for instance), // then just dump it in the current directory instead. File desktopPath = newFileWithPath(self->desktopPath); File reportPath; if (!fileExists(desktopPath)) { logWarn("Could not find desktop location, placing error report in current directory instead"); CharString currentDirString = fileGetCurrentDirectory(); File currentDir = newFileWithPath(currentDirString); reportPath = newFileWithParent(currentDir, self->reportName); freeFile(currentDir); freeCharString(currentDirString); } else { reportPath = newFileWithParent(desktopPath, self->reportName); freeFile(desktopPath); } if (fileExists(reportPath)) { logCritical("The path '%s' already contains a previous error report. Please remove the report data and try again."); } else { fileCreate(reportPath, kFileTypeDirectory); } // Now we should have a real error report path charStringCopy(self->reportDirPath, reportPath->absolutePath); freeFile(reportPath); freeCharString(wrappedInfoText); freeCharString(infoText); }