FILE* File_OpenW( _In_z_ const wchar_t* path, _In_z_ const wchar_t* mode) { #if defined(_MSC_VER) FILE* fp; return _wfopen_s(&fp, path, mode) == 0 ? fp : NULL; #else char *filePath; char *fileMode; FILE* fp; if( !UCS2ToAscii( path, &filePath) ) return NULL; if( !UCS2ToAscii( mode, &fileMode) ) { PAL_Free(filePath); return NULL; } fp = fopen(filePath, fileMode); PAL_Free(filePath); PAL_Free(fileMode); return fp; #endif }
static int _Insert( HashMap* self, const char* key) { Bucket* bucket = (Bucket*)PAL_Calloc(1, sizeof(Bucket)); if (!bucket) return -1; bucket->key = PAL_Strdup(key); if (!bucket->key) { PAL_Free(bucket); return -1; } if (HashMap_Insert(self, (HashBucket*)bucket) != 0) { PAL_Free(bucket); PAL_Free(bucket->key); return -1; } return 0; }
static void _Release( HashBucket* bucket_) { Bucket* bucket = (Bucket*)bucket_; PAL_Free(bucket->key); PAL_Free(bucket); }
Internal_Dir* Internal_Dir_Open(_In_z_ const TChar* path, NitsCallSite cs) { #if defined(_MSC_VER) Internal_Dir* dir; wchar_t filespec[PAL_MAX_PATH_SIZE]; /* Allocate and zero-fill struct */ dir = (Internal_Dir*)PAL_CallocCallsite(cs, 1, sizeof(Internal_Dir)); if (!dir) return NULL; /* Build files spec */ { if (Wcslcpy(filespec, path, PAL_MAX_PATH_SIZE) >= PAL_MAX_PATH_SIZE) return NULL; if (Wcslcat(filespec, L"/*", PAL_MAX_PATH_SIZE) >= PAL_MAX_PATH_SIZE) return NULL; } /* Find first file matching the file spec */ dir->handle = _wfindfirst(filespec, &dir->fileinfo); if (dir->handle == -1) { PAL_Free(dir); return NULL; } /* Note that readdir() has not been called yet */ dir->firstTime = 1; return dir; #else Internal_Dir* self = (Internal_Dir*)PAL_CallocCallsite(cs, 1, sizeof(Internal_Dir)); if (!self) return NULL; self->dir = opendir(path); if (!self->dir) { PAL_Free(self); return NULL; } memcpy(self->dirName, path, (Tcslen(path)+1) * sizeof(TChar)); return self; #endif }
int Dir_Close(Dir* self) { if (!self) return -1; if (closedir(self->dir) != 0) { PAL_Free(self); return -1; } PAL_Free(self); return 0; }
Dir* Dir_Open(const char* path) { Dir* dir; char filespec[PAL_MAX_PATH_SIZE]; /* Allocate and zero-fill struct */ dir = (Dir*)PAL_Calloc(1, sizeof(Dir)); if (!dir) return NULL; /* Build files spec */ { if (Strlcpy(filespec, path, sizeof(filespec)) >= PAL_MAX_PATH_SIZE) return NULL; if (Strlcat(filespec, "/*", sizeof(filespec)) >= PAL_MAX_PATH_SIZE) return NULL; } /* Find first file matching the file spec */ dir->handle = _findfirst(filespec, &dir->fileinfo); if (dir->handle == -1) { PAL_Free(dir); return NULL; } /* Note that readdir() has not been called yet */ dir->firstTime = 1; return dir; }
int UCS2ToAscii( _In_z_ const wchar_t *input, _Outptr_result_z_ char **output) { size_t inputLen; size_t xCount = 0; char *incInput = (char*) input; char *incOutput; inputLen = wcslen(input); *output = (char*) PAL_Malloc(inputLen + 1 ); if( *output == NULL ) { return -1; } incOutput = *output; for( xCount = 0; xCount < inputLen; xCount++) { if( input[xCount] > 128 ) { PAL_Free(*output); return -1; } incOutput[xCount] = *incInput; incInput +=2; } incOutput[inputLen] = '\0'; return 0; }
MI_INLINE void _FinalizeMatchState( unsigned char** matchState) { if (*matchState != NULL) { PAL_Free(*matchState); *matchState = NULL; } }
int Internal_Dir_Close(Internal_Dir* dir) { #if defined(_MSC_VER) _findclose(dir->handle); PAL_Free(dir); return 0; #else if (!dir) return -1; if (closedir(dir->dir) != 0) { PAL_Free(dir); return -1; } PAL_Free(dir); return 0; #endif }
void MOF_Release(MOF_Heap* self) { MOF_Block* p; if (!self) return; for (p = self->head; p; ) { MOF_Block* next = p->next; PAL_Free(p); p = next; } self->head = NULL; }
Dir* Dir_Open(const char* path) { Dir* self = (Dir*)PAL_Calloc(1, sizeof(Dir)); if (!self) return NULL; self->dir = opendir(path); if (!self->dir) { PAL_Free(self); return NULL; } return self; }
int File_RemoveW( _In_z_ const wchar_t* path) { #if defined(_MSC_VER) return _wunlink(path) == 0 ? 0 : -1; #else char *filePath; int result; if( !UCS2ToAscii( path, &filePath) ) return -1; result = unlink(filePath) == 0 ? 0 : -1; PAL_Free(filePath); return result; #endif }
int Directory_ExistW( _In_z_ const wchar_t *path) { #if defined(_MSC_VER) return _waccess_s(path, 0); #else char *filePath; int result; if( !UCS2ToAscii( path, &filePath) ) return -1; result = access(filePath, 0); PAL_Free(filePath); return result; #endif }
void SetJobId() { MI_Char *palUuid; if(g_ConfigurationDetails.hasSetDetail==MI_TRUE) { return; //Which means details were set before. } palUuid = Generate_UUID( NitsMakeCallSite(-3, NULL, NULL, 0) ); if(palUuid == NULL) { return; } memcpy(g_ConfigurationDetails.jobGuidString, palUuid, JOB_UUID_LENGTH); g_ConfigurationDetails.jobGuidString[36] = '\0'; PAL_Free(palUuid); g_ConfigurationDetails.hasSetDetail=MI_TRUE; }
void LifecycleEvent_Free(_In_ LifecycleEvent* event) { if (event) { if (event->sourceInstance) { MI_Instance_Delete(event->sourceInstance); } if (event->previousInstance) { MI_Instance_Delete(event->previousInstance); } if (event->methodParam) { MI_Instance_Delete(event->methodParam); } PAL_Free(event); } }
void MOF_Free(MOF_Heap* self, void* ptr) { MOF_Block* p; if (!self || !ptr) return; p = (MOF_Block*)ptr - 1; MOF_ASSERT(p->magic == MAGIC); if (p->prev) p->prev->next = p->next; else self->head = p->next; if (p->next) p->next->prev = p->prev; /* Fill released memory with 0xDD characters */ memset(p, 0xDD, sizeof(MOF_Block) + p->size); PAL_Free(p); }
static void Pool_Delete( _In_ CachedLock_Pool* self ) { PAL_Free(self); }
int Dir_Close(Dir* dir) { _findclose(dir->handle); PAL_Free(dir); return 0; }
MI_Boolean WQL_MatchLike( _In_z_ const ZChar* pattern, _In_z_ const ZChar* string, ZChar escape) { size_t stringLength; size_t rowLength; const ZChar* orgString; unsigned char* matchState = NULL; unsigned char* currentRow; unsigned char* previousRow; if (!(*pattern) || !(*string)) { /* skip any trailing wildcard characters */ while (*pattern == WILDCARD) pattern++; if (!(*pattern) && !(*string)) return MI_TRUE; return MI_FALSE; } stringLength = Tcslen(string); rowLength = stringLength + 1; orgString = string; if (!_EnsureMatchState(stringLength, &matchState)) return MI_FALSE; currentRow = matchState + rowLength; previousRow = matchState; while (*pattern) { /* Wildcard match. */ if (*pattern == WILDCARD) { size_t c; /* skip any continuous wildcard characters */ while (*(pattern + 1) == WILDCARD) pattern++; for (c = 0; c <= stringLength; c++) { unsigned char match = previousRow[c]; size_t matchedPos; if (match == NO_MATCH) { continue; } /* found a prior pattern char matched, */ /* then mark that the current wildcard */ /* matching the remaining part of target string */ if (match == MATCHED_WITH_ONE_CHAR) { matchedPos = c + 1; /* current wild char still match, but it should be */ /* the same as its preivous pattern char, I.E., */ /* matched one char */ currentRow[c] = MATCHED_WITH_ONE_CHAR; } else { /* if previous one is a wildcard match, */ /* then this wildcard has the same matching result */ /* with preivous char */ matchedPos = c; } for (; matchedPos <= stringLength; matchedPos++) { currentRow[matchedPos] = MATCHED_WITH_WILDCARD_CHAR; } break; } _SwitchRow(rowLength, ¤tRow, &previousRow); pattern++; } /* Set match. */ else if (*pattern == '[') { size_t c; int foundMatch = MI_FALSE; int foundSkip = 0; /* find all matching points based on the */ /* previous matching result */ for (c = 0; c <= stringLength; c++) { unsigned char match = previousRow[c]; int skip; const ZChar* currentString; if (match == NO_MATCH) { continue; } currentString = orgString + c; if (match == MATCHED_WITH_WILDCARD_CHAR) { /* match the current char if preivous is a wildchar match */ /* otherwise match the next char */ currentString --; } else if (c == stringLength) { /* no match since no char left in target string */ break; } if (_MatchSet (pattern, currentString, &skip)) { size_t matchedPos; if (!foundMatch) { foundMatch = MI_TRUE; foundSkip = skip; } /* mark the current pattern char mataching result */ matchedPos = (size_t)(currentString - orgString) + 1; currentRow[matchedPos] = MATCHED_WITH_ONE_CHAR; } } _SwitchRow(rowLength, ¤tRow, &previousRow); /* no match */ if (!foundMatch) { break; } pattern += foundSkip; } /* Single character match. */ else { size_t c; int foundMatch = MI_FALSE; if (escape != '\0' && *pattern == escape) { pattern++; } /* find all matching points based on the */ /* previous matching result */ for (c = 0; c <= stringLength; c++) { unsigned char match = previousRow[c]; const ZChar* currentString; if (match == NO_MATCH) { continue; } currentString = orgString + c; if (match == MATCHED_WITH_WILDCARD_CHAR) { /* match the current char if preivous is a wildchar match, */ /* otherwise match the next char */ currentString --; } else if (c == stringLength) { /* no match since no char left in target string */ break; } if (_Toupper(*pattern) == _Toupper(*currentString) || *pattern == ANYSINGLECHAR) { size_t matchedPos; if (!foundMatch) { foundMatch = MI_TRUE; } /* mark the current pattern char mataching result */ matchedPos = (size_t)(currentString - orgString) + 1; currentRow[matchedPos] = MATCHED_WITH_ONE_CHAR; } } _SwitchRow(rowLength, ¤tRow, &previousRow); /* no match */ if (!foundMatch) { break; } pattern ++; } } /* Matched if the last pattern char mattached the last target string char */ /* NoMatch otherwise */ { MI_Boolean flag = (previousRow[stringLength] != 0) ? MI_TRUE : MI_FALSE; PAL_Free(matchState); return flag; } }