/**************************************************************************** * * * FUNCTION : GetTextData() * * * * PURPOSE : Allocates and returns a pointer to the data contained in * * hData. This assumes that hData points to text data and * * will properly handle huge text data by leaving out the * * middle of the string and placing the size of the string * * into this string portion. * * * * RETURNS : A pointer to the allocated string. * * * ****************************************************************************/ PSTR GetTextData( HDDEDATA hData) { PSTR psz; DWORD cb; #define CBBUF 1024 if (hData == NULL) { return(NULL); } cb = DdeGetData(hData, NULL, 0, 0); if (!hData || !cb) return NULL; if (cb > CBBUF) { // possibly HUGE object! psz = MyAlloc(CBBUF); DdeGetData(hData, psz, CBBUF - 46, 0L); wsprintf(&psz[CBBUF - 46], "<---Size=%ld", cb); } else { psz = MyAlloc((WORD)cb); DdeGetData(hData, (LPBYTE)psz, cb, 0L); } return psz; #undef CBBUF }
bool CDecoder::Alloc(size_t numItems) { Free(); _parents = (UInt16 *)MyAlloc(numItems * sizeof(UInt16)); if (_parents == 0) return false; _suffixes = (Byte *)MyAlloc(numItems * sizeof(Byte)); if (_suffixes == 0) return false; _stack = (Byte *)MyAlloc(numItems * sizeof(Byte)); return _stack != 0; }
PRESLINK AllocResLink( PRES pRes) { PRESLINK prl; PRES pResNew; PRES2 pRes2; LPTSTR pszName; INT cbName; LPTSTR pszType; if (!(prl = (PRESLINK)MyAlloc(sizeof(RESLINK)))) return NULL; prl->prlNext = NULL; prl->cbRes = ResourceSize(pRes); if (!(prl->hRes = GlobalAlloc(GMEM_MOVEABLE, prl->cbRes))) { MyFree(prl); Message(MSG_OUTOFMEMORY); return NULL; } pResNew = (PRES)GlobalLock(prl->hRes); memcpy(pResNew, pRes, prl->cbRes); GlobalUnlock(prl->hRes); pszType = ResourceType(pRes); if (IsOrd(pszType) && OrdID(pszType) == ORDID_RT_DIALOG) { prl->fDlgResource = TRUE; pszName = ResourceName(pRes); cbName = NameOrdLen(pszName); if (!(prl->pszName = MyAlloc(cbName))) { GlobalFree(prl->hRes); MyFree(prl); return NULL; } NameOrdCpy(prl->pszName, pszName); pRes2 = ResourcePart2(pRes); prl->wLanguage = pRes2->LanguageId; } else { prl->fDlgResource = FALSE; prl->pszName = NULL; prl->wLanguage = 0; } return prl; }
//***************************************************************** static Bool GetWorkMem() { // Запросить свободный блок общей памяти // А если не дадут, то использовать // свой кусок памяти, выделенный // на ROUT_Init() // Byte *p = NULL; long lth = 1024<<10; // 1M p = (Byte*)MyAlloc(lth, 0); if (!p ) { // Использовать собственный кусок памяти p = (Byte*)gOwnMemory; lth = gOwnMemorySize; DEBUG_PRINT("ROUT.CPP MyGetFreeMem: MyAlloc failed, using own memory"); } // Установить рабочую память INIT_MEMORY(p,lth); return TRUE; }
bool CCachedInStream::Alloc(unsigned blockSizeLog, unsigned numBlocksLog) { unsigned sizeLog = blockSizeLog + numBlocksLog; if (sizeLog >= sizeof(size_t) * 8) return false; size_t dataSize = (size_t)1 << sizeLog; if (_data == 0 || dataSize != _dataSize) { MidFree(_data); _data = (Byte *)MidAlloc(dataSize); if (_data == 0) return false; _dataSize = dataSize; } if (_tags == 0 || numBlocksLog != _numBlocksLog) { MyFree(_tags); _tags = (UInt64 *)MyAlloc(sizeof(UInt64) << numBlocksLog); if (_tags == 0) return false; _numBlocksLog = numBlocksLog; } _blockSizeLog = blockSizeLog; return true; }
CDynLimBuf & CDynLimBuf::operator+=(char c) throw() { if (_error) return *this; if (_size == _pos) { size_t n = _sizeLimit - _size; if (n == 0) { _error = true; return *this; } if (n > _size) n = _size; n += _pos; Byte *newBuf = (Byte *)MyAlloc(n); if (!newBuf) { _error = true; return *this; } memcpy(newBuf, _chars, _pos); MyFree(_chars); _chars = newBuf; _size = n; } _chars[_pos++] = c; return *this; }
/**************************************************************************** * * * FUNCTION : GetFormatName() * * * * PURPOSE : allocates and returns a pointer to a string representing * * a format. Use MyFree() to free this string. * * * * RETURNS : The number of characters copied into lpstr or 0 on error. * * * ****************************************************************************/ PSTR GetFormatName( WORD wFmt) { PSTR psz; WORD cb; if (wFmt == 1) { psz = MyAlloc(8); strcpy(psz, "CF_TEXT"); return psz; } psz = MyAlloc(255); *psz = '\0'; cb = GetClipboardFormatName(wFmt, psz, 255) + 1; return((PSTR)LocalReAlloc((HANDLE)psz, cb, LMEM_MOVEABLE)); }
void init_foods(void) { ULONG i; foods = (struct Food *) MyAlloc(MAX_FOODS * sizeof(struct Food)); memset(foods,0,sizeof(struct Food)*MAX_FOODS); for (i=0; i<MAX_FOODS; i++) foods[i].Head = -1; }
int main() { char buffer2[20]; char buffer3[20]; void * al; void *toout,*fromout; int page_size = getpagesize(); page_mask = ~((intptr_t)page_size-1); // Get some page aligned memory void * x = malloc(page_size * 2); target_space = ((intptr_t)x + page_size - 1) & page_mask; void * mm = (void*)target_space; al = MyAlloc(mm); // cause the swizzle swizzle_space = (intptr_t)al & page_mask; if (swizzle_space == target_space) { fprintf (stderr,"Error al not swizzled\n"); } ((char*)al)[0] = 1; buffer2[0] = 2; buffer3[0] = 3; mmemcpy(buffer2, al, n, &toout, &fromout); fprintf(stderr, "al[0] %d buffer2[0] %d toout - to %x fromout - from %x\n", ((char*)al)[0], buffer2[0], (char*)toout - (char*)buffer2, (char*)fromout- (char*)al); if (!Swizzled (al)) { fprintf (stderr,"Error1 al not swizzled\n"); } if (!Swizzled (fromout)) { fprintf (stderr,"Error2 fromout not swizzled\n"); } mmemcpy(al, buffer3, n, &toout, &fromout); if (!Swizzled (al)) { fprintf (stderr,"Error3 al not swizzled\n"); fflush (stderr); } if (!Swizzled (toout)) { fprintf (stderr,"Error4 toout not swizzled\n"); fflush (stderr); } fprintf(stderr, "al[0] %d buffer3[0] %d toout - to %x fromout - from %x\n",((char*)al)[0], buffer3[0], (char*)toout-(char *)al, (char*)fromout - (char*)buffer3); fflush (stderr); memindex(al); MyFree(al); return 0; }
bool CEncoder::Create(UInt32 numSymbols, const Byte *extraBits, UInt32 extraBase, UInt32 maxLength) { m_NumSymbols = numSymbols; m_ExtraBits = extraBits; m_ExtraBase = extraBase; m_MaxLength = maxLength; m_HeapSize = numSymbols * 2 + 1; Free(); m_Items = (CItem *)MyAlloc(m_HeapSize * sizeof(CItem)); m_Heap = (UInt32 *)MyAlloc(m_HeapSize * sizeof(UInt32)); m_Depth = (Byte *)MyAlloc(m_HeapSize * sizeof(Byte)); if (m_Items == 0 || m_Heap == 0 || m_Depth == 0) { Free(); return false; } return true; }
HRESULT CDecoder::CreateInputBuffer() { if (_inBuf == 0) { _inBuf = (Byte *)MyAlloc(kInBufSize); if (_inBuf == 0) return E_OUTOFMEMORY; } return S_OK; }
HRESULT CDecoder::CreateInputBuffer() { if (_inBuf == 0 || _inBufSize != _inBufSizeAllocated) { MyFree(_inBuf); _inBuf = (Byte *)MyAlloc(_inBufSize); if (_inBuf == 0) return E_OUTOFMEMORY; _inBufSizeAllocated = _inBufSize; } return S_OK; }
STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *prop, UInt32 size) { RINOK(SResToHRESULT(LzmaDec_Allocate(&_state, prop, size, &g_Alloc))); if (_inBuf == 0) { _inBuf = (Byte *)MyAlloc(kInBufSize); if (_inBuf == 0) return E_OUTOFMEMORY; } return S_OK; }
STDMETHODIMP CDecoder::SetDecoderProperties2(const Byte *prop, UInt32 size) { if (size != 1) return SZ_ERROR_UNSUPPORTED; RINOK(SResToHRESULT(Lzma2Dec_Allocate(&_state, prop[0], &g_Alloc))); if (_inBuf == 0) { _inBuf = (Byte *)MyAlloc(kInBufSize); if (_inBuf == 0) return E_OUTOFMEMORY; } return S_OK; }
WCHAR *MyMakeStr(WCHAR *s) { WCHAR * s1; if (s) { s1 = (WCHAR *) MyAlloc((wcslen(s) + 1) * sizeof(WCHAR)); /* allocate buffer */ wcscpy(s1, s); /* copy string */ } else s1 = s; return(s1); }
int MyCopyAll(FILE *srcfh, PFILE dstfh) { PCHAR buffer = MyAlloc(BUFSIZE); UINT n; while ((n = fread(buffer, 1, BUFSIZE, srcfh)) != 0) MyWrite(dstfh, buffer, n); MyFree(buffer); return TRUE; }
static int load_hunk ( BPTR file, BPTR **next_hunk_ptr, struct pheader *ph, SIPTR *funcarray, struct DosLibrary *DOSBase ) { struct hunk *hunk; /* Sanity check */ if (ph->memsz < ph->filesz) { SetIoErr(ERROR_BAD_HUNK); return 0; } if (!ph->memsz) return 1; hunk = MyAlloc(ph->memsz + sizeof(struct hunk), MEMF_ANY); if (hunk) { hunk->size = ph->memsz + sizeof(struct hunk); ph->paddr = hunk->data; /* Link the new hunk with the old next one. This makes it possible to handle insertion */ hunk->next = BPTR2HUNK(*next_hunk_ptr)->next; /* Link the previous one with the new one */ BPTR2HUNK(*next_hunk_ptr)->next = HUNK2BPTR(hunk); /* Update the pointer to the previous one, which is now the current one */ *next_hunk_ptr = HUNK2BPTR(hunk); /* Clear out the memory that is not filled with file contents */ memset(hunk->data + ph->filesz, 0, ph->memsz - ph->filesz); /* Finally read the segment from the file into memory */ return read_block(file, ph->offset, hunk->data, ph->filesz, funcarray, DOSBase); } SetIoErr(ERROR_NO_FREE_STORE); return 0; }
/* CPPTEST_TEST_CASE_CONTEXT void * MyAlloc(size_t) */ void TestSuite_sboot_c_7608004f_TestSuite_MyAlloc() { /* Pre-condition initialization */ /* Initializing argument 1 (size) */ size_t _size = 0u; { /* Tested function call */ void * _return = MyAlloc(_size); /* Post-condition check */ CPPTEST_POST_CONDITION_PTR("void * _return ", ( _return )); } }
LPTSTR NameOrdDup( LPTSTR pNameOrd) { register INT iLen; LPTSTR psz; iLen = NameOrdLen(pNameOrd); if (!(psz = (LPTSTR)MyAlloc(iLen))) return NULL; NameOrdCpy(psz, pNameOrd); return psz; }
//******************************************************************** ROUT_FUNC(Bool32) ROUT_Init(uint16_t wHighCode,HANDLE hStorage) { // DEBUG_PRINT("ROUT_Init(%d,%d)",wHighCode,hStorage); gwHighRC_rout = wHighCode; ghStorage_rout = hStorage; gwLowRC_rout = 0; // Собственный кусок памяти на одну страницу gOwnMemory = MyAlloc(gOwnMemorySize,0); if ( !gOwnMemory ) NO_MEMORY; return ROUT_GetReturnCode()==0? TRUE:FALSE; }
CDynLimBuf::CDynLimBuf(size_t limit) throw() { _chars = 0; _pos = 0; _size = 0; _sizeLimit = limit; _error = true; unsigned size = 1 << 4; if (size > limit) size = (unsigned)limit; _chars = (Byte *)MyAlloc(size); if (_chars) { _size = size; _error = false; } }
//******************************************************************** uint32_t ROUT_GetObjectSize( uint32_t objIndex // Индекс объекта начиная от 1 ) { // Гадкая функция для определения длины объекта Byte *p = NULL; long lth = 256<<10; // 256K long sizeMem = 0; ClearError(); for (long attempt = 1; attempt < 3; attempt++) { p = (Byte*)MyAlloc(lth,0); if (!p) { NO_MEMORY; return 0; } InitMemory(p,lth); // Выполнить конвертирование на памяти sizeMem = lth; if ( ROUT_GetObject( objIndex, 0, // lpMem &sizeMem) // sizeMem ) { FreeWorkMem(); return sizeMem + 2*gBumperSize; } // Повторить попытку с большей памятью FreeWorkMem(); lth = lth <<1 ; } return 0; // Неудача }
int MyCopy(FILE *srcfh, PFILE dstfh, ULONG nbytes) { PCHAR buffer = MyAlloc(BUFSIZE); UINT n; while (nbytes) { if (nbytes <= BUFSIZE) n = (UINT)nbytes; else n = BUFSIZE; nbytes -= n; MyRead(srcfh, buffer, n); MyWrite( dstfh, buffer, n); } MyFree(buffer); return(n); }
LPTSTR ids( UINT idString) { static LPTSTR apsz[CSTRINGS]; // String resource array cache. LPTSTR psz; INT cch; if (apsz[idString]) return apsz[idString]; if (!(psz = MyAlloc(CCHTEXTMAX * sizeof(TCHAR)))) return szEmpty; if (!(cch = LoadString(ghInst, idString, psz, CCHTEXTMAX))) { MyFree(psz); return szEmpty; } apsz[idString] = psz = MyRealloc(psz, (cch + 1) * sizeof(TCHAR)); return (psz ? psz : szEmpty); }
static void * load_block ( BPTR file, ULONG offset, ULONG size, SIPTR *funcarray, struct DosLibrary *DOSBase ) { void *block = MyAlloc(size, MEMF_ANY); if (block) { if (read_block(file, offset, block, size, funcarray, DOSBase)) return block; MyFree(block, size); } else SetIoErr(ERROR_NO_FREE_STORE); return NULL; }
PSTR ids( UINT idString) { static PSTR apstr[CSTRINGS]; // String resource array cache. PSTR pstr; INT cch; if (apstr[idString]) return apstr[idString]; if (!(pstr = MyAlloc(CCHTEXTMAX))) return ""; if (!(cch = LoadString(ghInst, idString, pstr, CCHTEXTMAX))) { MyFree(pstr); return ""; } apstr[idString] = pstr = MyRealloc(pstr, cch + 1); return (pstr ? pstr : ""); }
CDynLimBuf &CDynLimBuf::operator+=(const char *s) throw() { if (_error) return *this; unsigned len = MyStringLen(s); size_t rem = _sizeLimit - _pos; if (rem < len) { len = (unsigned)rem; _error = true; } if (_size - _pos < len) { size_t n = _pos + len; if (n - _size < _size) { n = _sizeLimit; if (n - _size > _size) n = _size * 2; } Byte *newBuf = (Byte *)MyAlloc(n); if (!newBuf) { _error = true; return *this; } memcpy(newBuf, _chars, _pos); MyFree(_chars); _chars = newBuf; _size = n; } memcpy(_chars + _pos, s, len); _pos += len; return *this; }
static void *SzAlloc(void *p, size_t size) { p = p; return MyAlloc(size); }
int Lzma86_Encode(Byte *dest, size_t *destLen, const Byte *src, size_t srcLen, int level, UInt32 dictSize, int filterMode) { ISzAlloc g_Alloc = { SzAlloc, SzFree }; size_t outSize2 = *destLen; Byte *filteredStream; Bool useFilter; int mainResult = SZ_ERROR_OUTPUT_EOF; CLzmaEncProps props; LzmaEncProps_Init(&props); props.level = level; props.dictSize = dictSize; *destLen = 0; if (outSize2 < LZMA86_HEADER_SIZE) return SZ_ERROR_OUTPUT_EOF; { int i; UInt64 t = srcLen; for (i = 0; i < 8; i++, t >>= 8) dest[LZMA86_SIZE_OFFSET + i] = (Byte)t; } filteredStream = 0; useFilter = (filterMode != SZ_FILTER_NO); if (useFilter) { if (srcLen != 0) { filteredStream = (Byte *)MyAlloc(srcLen); if (filteredStream == 0) return SZ_ERROR_MEM; memcpy(filteredStream, src, srcLen); } { UInt32 x86State; x86_Convert_Init(x86State); x86_Convert(filteredStream, srcLen, 0, &x86State, 1); } } { size_t minSize = 0; Bool bestIsFiltered = False; /* passes for SZ_FILTER_AUTO: 0 - BCJ + LZMA 1 - LZMA 2 - BCJ + LZMA agaian, if pass 0 (BCJ + LZMA) is better. */ int numPasses = (filterMode == SZ_FILTER_AUTO) ? 3 : 1; int i; for (i = 0; i < numPasses; i++) { size_t outSizeProcessed = outSize2 - LZMA86_HEADER_SIZE; size_t outPropsSize = 5; SRes curRes; Bool curModeIsFiltered = (numPasses > 1 && i == numPasses - 1); if (curModeIsFiltered && !bestIsFiltered) break; if (useFilter && i == 0) curModeIsFiltered = True; curRes = LzmaEncode(dest + LZMA86_HEADER_SIZE, &outSizeProcessed, curModeIsFiltered ? filteredStream : src, srcLen, &props, dest + 1, &outPropsSize, 0, NULL, &g_Alloc, &g_Alloc); if (curRes != SZ_ERROR_OUTPUT_EOF) { if (curRes != SZ_OK) { mainResult = curRes; break; } if (outSizeProcessed <= minSize || mainResult != SZ_OK) { minSize = outSizeProcessed; bestIsFiltered = curModeIsFiltered; mainResult = SZ_OK; } } } dest[0] = (Byte)(bestIsFiltered ? 1 : 0); *destLen = LZMA86_HEADER_SIZE + minSize; } if (useFilter) MyFree(filteredStream); return mainResult; }
int main( int argc, char *argv[] ) { char *buff = NULL; char *buff2, *buff3; char *buffn, *buffs; char *ptr; int i, bytes, j, k, sl; FILE *f; struct stat fs; char drive[_MAX_DRIVE], dir[_MAX_DIR]; char fname[_MAX_FNAME], ext[_MAX_EXT]; char path[_MAX_PATH]; char tmppath[_MAX_PATH]; char tmpfname[_MAX_FNAME], tmpext[_MAX_EXT]; unsigned cnt; unsigned lines; bind_size *index; bind_size *entries; for( j = argc - 1; j > 0; --j ) { if( argv[j][0] == '/' || argv[j][0] == '-' ) { sl = strlen( argv[j] ); for( i = 1; i < sl; i++ ) { switch( argv[j][i] ) { case 's': sflag = true; break; case 'q': qflag = true; break; case 'd': bindfile = &argv[j][i + 1]; i = sl; break; case '?': Banner(); Usage( NULL ); default: Banner(); Usage( "Invalid option" ); } } for( i = j; i < argc; i++ ) { argv[i]= argv[i + 1]; } argc--; } } Banner(); /* * now, check for null file name */ if( argc < 2 ) { Usage( "No executable to bind" ); } _splitpath( argv[1], drive, dir, fname, ext ); if( ext[0] == 0 ) { _makepath( path, drive, dir, fname, ".exe" ); } else { strcpy( path, argv[1] ); } if( stat( path, &fs ) == -1 ) { Abort( "Could not find executable \"%s\"", path ); } cnt = 0; if( !sflag ) { buff = MyAlloc( 65000 ); buff2 = MyAlloc( 32000 ); buff3 = MyAlloc( MAX_LINE_LEN ); /* * read in all data files */ MyPrintf( "Getting data files from" ); f = GetFromEnvAndOpen( bindfile ); MyPrintf( "\n" ); if( f == NULL ) { Abort( "Could not open %s", bindfile ); } while( (ptr = fgets( buff3, MAX_LINE_LEN, f )) != NULL ) { for( i = strlen( ptr ); i && isWSorCtrlZ( ptr[i - 1] ); --i ) { ptr[i - 1] = '\0'; } if( ptr[0] == '\0' ) { continue; } ptr = SkipLeadingSpaces( ptr ); if( ptr[0] == '#' ) { continue; } dats[FileCount] = MyAlloc( strlen( ptr ) + 1 ); strcpy( dats[FileCount], ptr ); FileCount++; if( FileCount >= MAX_DATA_FILES ) { Abort( "Too many files to bind!" ); } } fclose( f ); index = MyAlloc( FileCount * sizeof( bind_size ) ); entries = MyAlloc( FileCount * sizeof( bind_size ) ); buffn = buff; *(bind_size *)buffn = FileCount; buffn += sizeof( bind_size ); cnt += sizeof( bind_size ); buffs = buffn; buffn += sizeof( bind_size ); cnt += sizeof( bind_size ); k = 0; for( i = 0; i < FileCount; i++ ) { // j = strlen( dats[i] ) + 1; // memcpy( buffn, dats[i], j ); _splitpath( dats[i], NULL, NULL, tmpfname, tmpext ); _makepath( tmppath, NULL, NULL, tmpfname, tmpext ); j = strlen( tmppath ) + 1; memcpy( buffn, tmppath, j ); buffn += j; cnt += j; k += j; } *(bind_size *)buffs = k + 1; /* size of token list */ *buffn = 0; /* trailing zero */ buffn++; cnt++; buffs = buffn; buffn += FileCount * ( sizeof( bind_size ) + sizeof( bind_size ) ); cnt += FileCount * ( sizeof( bind_size ) + sizeof( bind_size ) ); for( j = 0; j < FileCount; j++ ) { MyPrintf( "Loading" ); f = GetFromEnvAndOpen( dats[j] ); if( f == NULL ) { Abort( "\nLoad of %s failed!", dats[j] ); } setvbuf( f, buff2, _IOFBF, 32000 ); bytes = lines = 0; index[j] = cnt; while( (ptr = fgets( buff3, MAX_LINE_LEN, f )) != NULL ) { unsigned len; for( len = strlen( ptr ); len && isWSorCtrlZ( ptr[len - 1] ); --len ) ptr[len - 1] = '\0'; if( ptr[0] == '\0' ) { continue; } ptr = SkipLeadingSpaces( ptr ); if( ptr[0] == '#' ) { continue; } len = strlen( ptr ); *buffn = (char)len; buffn++; memcpy( buffn, ptr, len ); buffn += len; cnt += len + 1; lines++; bytes += len; } fclose( f ); entries[j] = lines; MyPrintf( "Added %d lines (%d bytes)\n", lines, bytes ); } memcpy( buffs, index, FileCount * sizeof( bind_size ) ); buffs += FileCount * sizeof( bind_size ); memcpy( buffs, entries, FileCount * sizeof( bind_size ) ); } AddDataToEXE( path, buff, cnt, fs.st_size ); if( !sflag ) { MyPrintf( "Added %d bytes to \"%s\"\n", cnt, path ); } else { MyPrintf( "\"%s\" has been stripped of configuration information\n", path ); } return( 0 ); } /* main */