static void pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length) { int check; png_byte *n_data; png_FILE_p io_ptr; /* Check if data really is near. If so, use usual code. */ n_data = (png_byte *)CVT_PTR_NOCHECK(data); io_ptr = (png_FILE_p)CVT_PTR(png_ptr->io_ptr); if ((png_bytep)n_data == data) READFILE(io_ptr, n_data, length, check); else { png_byte buf[NEAR_BUF_SIZE]; png_size_t read, remaining, err; check = 0; remaining = length; do { read = MIN(NEAR_BUF_SIZE, remaining); READFILE(io_ptr, buf, 1, err); png_memcpy(data, buf, read); /* Copy far buffer to near buffer */ if (err != read) break; else check += err; data += read; remaining -= read; } while (remaining != 0); } if (check != length) png_error(png_ptr, "read Error"); }
static void pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length) { png_size_t check; /* fread() returns 0 on error, so it is OK to store this in a png_size_t * instead of an int, which is what fread() actually returns. */ READFILE((png_FILE_p)png_ptr->io_ptr, data, length, check); if (check != length) png_error(png_ptr, "Read Error!"); }
static void pngtest_read_data(png_structp png_ptr, png_bytep data, png_size_t length) { png_size_t check = 0; png_voidp io_ptr; /* fread() returns 0 on error, so it is OK to store this in a png_size_t * instead of an int, which is what fread() actually returns. */ io_ptr = png_get_io_ptr(png_ptr); if (io_ptr != NULL) { READFILE((png_FILE_p)io_ptr, data, length, check); } if (check != length) { png_error(png_ptr, "Read Error!"); } }
/* * (Re-)read the "users" file into memory. */ static int mod_instantiate(UNUSED CONF_SECTION *conf, void *instance) { rlm_files_t *inst = instance; #undef READFILE #define READFILE(_x, _y) do { if (getusersfile(inst, inst->_x, &inst->_y, inst->compat_mode) != 0) { ERROR("Failed reading %s", inst->_x); mod_detach(inst);return -1;} } while (0) READFILE(filename, common); READFILE(usersfile, users); READFILE(acctusersfile, acctusers); #ifdef WITH_PROXY READFILE(preproxy_usersfile, preproxy_users); READFILE(postproxy_usersfile, postproxy_users); #endif READFILE(auth_usersfile, auth_users); READFILE(postauth_usersfile, postauth_users); return 0; }
int ExtractBits(PNG_CONST TCHAR *inname, PNG_CONST TCHAR *outname) { static HANDLE fpin; static HANDLE fpout; /* "static" prevents setjmp corruption */ png_structp read_ptr; png_infop read_info_ptr, end_info_ptr; png_structp write_ptr = NULL; png_infop write_info_ptr = NULL; png_infop write_end_info_ptr = NULL; png_bytep row_buf; png_uint_32 y; png_uint_32 width, height; int num_pass, pass; int bit_depth, color_type; char inbuf[256], outbuf[256]; row_buf = NULL; if ((fpin = CreateFile(inname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE) { fprintf(STDERR, "Could not find input file %s\n", inname); return (1); } if ((fpout = CreateFile(outname, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL)) == INVALID_HANDLE_VALUE) { fprintf(STDERR, "Could not open output file %s\n", outname); FCLOSE(fpin); return (1); } png_debug(0, "Allocating read and write structures"); read_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, png_voidp_NULL, png_error_ptr_NULL, png_error_ptr_NULL); png_set_error_fn(read_ptr, (png_voidp)inname, pngtest_error, pngtest_warning); png_debug(0, "Allocating read_info, write_info and end_info structures"); read_info_ptr = png_create_info_struct(read_ptr); end_info_ptr = png_create_info_struct(read_ptr); png_debug(0, "Setting jmpbuf for read struct"); if (setjmp(png_jmpbuf(read_ptr))) { fprintf(STDERR, "%s -> %s: libpng read error\n", inname, outname); png_free(read_ptr, row_buf); row_buf = NULL; png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr); FCLOSE(fpin); FCLOSE(fpout); return (1); } png_debug(0, "Initializing input and output streams"); png_set_read_fn(read_ptr, (png_voidp)fpin, pngtest_read_data); if (status_dots_requested == 1) { png_set_read_status_fn(read_ptr, read_row_callback); } else { png_set_read_status_fn(read_ptr, png_read_status_ptr_NULL); } png_debug(0, "Reading info struct"); png_read_info(read_ptr, read_info_ptr); png_debug(0, "Transferring info struct"); { int interlace_type, compression_type, filter_type; if (png_get_IHDR(read_ptr, read_info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, &compression_type, &filter_type)) { png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth, color_type, PNG_INTERLACE_NONE, compression_type, filter_type); } } { int intent; if (png_get_sRGB(read_ptr, read_info_ptr, &intent)) png_set_sRGB(write_ptr, write_info_ptr, intent); } { png_colorp palette; int num_palette; if (png_get_PLTE(read_ptr, read_info_ptr, &palette, &num_palette)) png_set_PLTE(write_ptr, write_info_ptr, palette, num_palette); } { png_color_8p sig_bit; if (png_get_sBIT(read_ptr, read_info_ptr, &sig_bit)) png_set_sBIT(write_ptr, write_info_ptr, sig_bit); } { png_bytep trans; int num_trans; png_color_16p trans_values; if (png_get_tRNS(read_ptr, read_info_ptr, &trans, &num_trans, &trans_values)) { int sample_max = (1 << read_info_ptr->bit_depth); /* libpng doesn't reject a tRNS chunk with out-of-range samples */ if (!((read_info_ptr->color_type == PNG_COLOR_TYPE_GRAY && (int)trans_values->gray > sample_max) || (read_info_ptr->color_type == PNG_COLOR_TYPE_RGB && ((int)trans_values->red > sample_max || (int)trans_values->green > sample_max || (int)trans_values->blue > sample_max)))) png_set_tRNS(write_ptr, write_info_ptr, trans, num_trans, trans_values); } } png_debug(0, "Writing row data"); num_pass = png_set_interlace_handling(read_ptr); for(pass = 0; pass < num_pass; pass++) { png_debug1(0, "Writing row data for pass %d", pass); for(y = 0; y < height; y++) { png_debug2(0, "Allocating row buffer (pass %d, y = %ld)...", pass, y); row_buf = (png_bytep)png_malloc(read_ptr, png_get_rowbytes(read_ptr, read_info_ptr)); png_debug2(0, "0x%08lx (%ld bytes)", (unsigned long)row_buf, png_get_rowbytes(read_ptr, read_info_ptr)); png_read_rows(read_ptr, (png_bytepp)&row_buf, png_bytepp_NULL, 1); png_debug2(0, "Freeing row buffer (pass %d, y = %ld)", pass, y); png_free(read_ptr, row_buf); row_buf = NULL; } } png_debug(0, "Reading and writing end_info data"); png_read_end(read_ptr, end_info_ptr); { png_uint_32 iwidth, iheight; iwidth = png_get_image_width(write_ptr, write_info_ptr); iheight = png_get_image_height(write_ptr, write_info_ptr); fprintf(STDERR, "\n Image width = %lu, height = %lu\n", (unsigned long)iwidth, (unsigned long)iheight); } png_debug(0, "Destroying data structs"); png_debug(1, "destroying read_ptr, read_info_ptr, end_info_ptr"); png_destroy_read_struct(&read_ptr, &read_info_ptr, &end_info_ptr); png_debug(0, "Destruction complete."); FCLOSE(fpin); FCLOSE(fpout); png_debug(0, "Opening files for comparison"); if ((fpin = CreateFile(inname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE) { fprintf(STDERR, "Could not find file %s\n", inname); return (1); } if ((fpout = CreateFile(outname, GENERIC_READ, 0, NULL, OPEN_EXISTING, 0, NULL)) == INVALID_HANDLE_VALUE) { fprintf(STDERR, "Could not find file %s\n", outname); FCLOSE(fpin); return (1); } for(;;) { DWORD num_in, num_out; READFILE(fpin, inbuf, 1, num_in); READFILE(fpout, outbuf, 1, num_out); if (num_in != num_out) { fprintf(STDERR, "\nFiles %s and %s are of a different size\n", inname, outname); if (wrote_question == 0) { fprintf(STDERR, " Was %s written with the same maximum IDAT chunk size (%d bytes),", inname, PNG_ZBUF_SIZE); fprintf(STDERR, "\n filtering heuristic (libpng default), compression"); fprintf(STDERR, " level (zlib default),\n and zlib version (%s)?\n\n", ZLIB_VERSION); wrote_question = 1; } FCLOSE(fpin); FCLOSE(fpout); return (0); } if (!num_in) break; if (png_memcmp(inbuf, outbuf, num_in)) { fprintf(STDERR, "\nFiles %s and %s are different\n", inname, outname); if (wrote_question == 0) { fprintf(STDERR, " Was %s written with the same maximum IDAT chunk size (%d bytes),", inname, PNG_ZBUF_SIZE); fprintf(STDERR, "\n filtering heuristic (libpng default), compression"); fprintf(STDERR, " level (zlib default),\n and zlib version (%s)?\n\n", ZLIB_VERSION); wrote_question = 1; } FCLOSE(fpin); FCLOSE(fpout); return (0); } } FCLOSE(fpin); FCLOSE(fpout); return (0); }
/****************************************************************************** * Name: ProcessCommRequests * * Purpose: worker thread function that communicates with the com port ******************************************************************************/ UINT AFX_CDECL ProcessCommRequests( LPVOID lParam) { if( !lParam) return 2; DWORD dwWait; // loop ends by returning on error or when the terminate flag is set while( TRUE) { dwWait = WaitForSingleObject( COMMEVENT, TIMEOUT); while( dwWait!=WAIT_OBJECT_0) { // check for error if( dwWait==WAIT_ABANDONED_0 || dwWait == WAIT_FAILED) { ResetEvent( COMMEVENT); PurgeComm( HPORT, PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR); return 1; } // check for termination if( TERMINATE_FLAG) { ResetEvent( COMMEVENT); PurgeComm( HPORT, PURGE_TXABORT|PURGE_RXABORT|PURGE_TXCLEAR|PURGE_RXCLEAR); return 0; } dwWait = WaitForSingleObject( COMMEVENT, TIMEOUT); } CCommRequest* pRequest = (((CCommDevice*)lParam)->m_pRequest); switch( pRequest->m_dwRequestType) { case REQUEST_READ: { // clear request error field pRequest->m_dwRequestError = ERR_NOERROR; DWORD dwBytesRead = READFILE( pRequest->m_lpszResult, pRequest->m_dwResultLength, pRequest->m_dwMaxTries, pRequest->m_dwTimeout); pRequest->m_dwResultLength = dwBytesRead; pRequest->m_dwRequestError = COMMSTATE; COMMSTATE = ERR_NOERROR; ((CCommDevice*)lParam)->m_pRequest = NULL; ResetEvent( COMMEVENT); if( !dwBytesRead) { switch( pRequest->m_dwRequestError) { case ERR_PORTERROR: case ERR_NOPORTLOCK: case ERR_NODATALOCK: { if( pRequest->m_pWindow && ::IsWindow( pRequest->m_pWindow->m_hWnd)) pRequest->m_pWindow->PostMessage( WM_COMM_REQUEST_COMPLETE, 0, (long)pRequest); else if( pRequest->m_pCompleteFunction) (*pRequest->m_pCompleteFunction)( pRequest); return 1; } case ERR_TERMINATED: { if( pRequest->m_pWindow && ::IsWindow( pRequest->m_pWindow->m_hWnd)) pRequest->m_pWindow->PostMessage( WM_COMM_REQUEST_COMPLETE, 0, (long)pRequest); else if( pRequest->m_pCompleteFunction) (*pRequest->m_pCompleteFunction)( pRequest); return 0; } } } if( pRequest->m_pWindow && ::IsWindow( pRequest->m_pWindow->m_hWnd)) pRequest->m_pWindow->PostMessage( WM_COMM_REQUEST_COMPLETE, 0, (long)pRequest); else if( pRequest->m_pCompleteFunction) (*pRequest->m_pCompleteFunction)( pRequest); break; } case REQUEST_WRITE: { // clear request error field pRequest->m_dwRequestError = ERR_NOERROR; DWORD dwBytesWritten = WRITEFILE( pRequest->m_lpszData, pRequest->m_dwDataLength); pRequest->m_dwDataLength = dwBytesWritten; pRequest->m_dwRequestError = COMMSTATE; COMMSTATE = ERR_NOERROR; ((CCommDevice*)lParam)->m_pRequest = NULL; ResetEvent( COMMEVENT); if( !dwBytesWritten) { switch( pRequest->m_dwRequestError) { case ERR_PORTERROR: case ERR_NOPORTLOCK: { if( pRequest->m_pWindow && ::IsWindow( pRequest->m_pWindow->m_hWnd)) pRequest->m_pWindow->PostMessage( WM_COMM_REQUEST_COMPLETE, 0, (long)pRequest); else if( pRequest->m_pCompleteFunction) (*pRequest->m_pCompleteFunction)( pRequest); return 1; } case ERR_TERMINATED: { if( pRequest->m_pWindow && ::IsWindow( pRequest->m_pWindow->m_hWnd)) pRequest->m_pWindow->PostMessage( WM_COMM_REQUEST_COMPLETE, 0, (long)pRequest); else if( pRequest->m_pCompleteFunction) (*pRequest->m_pCompleteFunction)( pRequest); return 0; } } } if( pRequest->m_pWindow && ::IsWindow( pRequest->m_pWindow->m_hWnd)) pRequest->m_pWindow->PostMessage( WM_COMM_REQUEST_COMPLETE, 0, (long)pRequest); else if( pRequest->m_pCompleteFunction) (*pRequest->m_pCompleteFunction)( pRequest); break; } case REQUEST_COMMAND: { // clear request error field pRequest->m_dwRequestError = ERR_NOERROR; DWORD dwBytesRead = COMMAND( pRequest->m_lpszData, pRequest->m_dwDataLength, pRequest->m_lpszResult, pRequest->m_dwResultLength, pRequest->m_dwMaxTries, pRequest->m_dwTimeout); pRequest->m_dwResultLength = dwBytesRead; pRequest->m_dwRequestError = COMMSTATE; COMMSTATE = ERR_NOERROR; ((CCommDevice*)lParam)->m_pRequest = NULL; ResetEvent( COMMEVENT); if( !dwBytesRead) { switch( pRequest->m_dwRequestError) { case ERR_PORTERROR: case ERR_NOPORTLOCK: case ERR_NODATALOCK: { if( pRequest->m_pWindow && ::IsWindow( pRequest->m_pWindow->m_hWnd)) pRequest->m_pWindow->PostMessage( WM_COMM_REQUEST_COMPLETE, 0, (long)pRequest); else if( pRequest->m_pCompleteFunction) (*pRequest->m_pCompleteFunction)( pRequest); return 1; } case ERR_TERMINATED: { if( pRequest->m_pWindow && ::IsWindow( pRequest->m_pWindow->m_hWnd)) pRequest->m_pWindow->PostMessage( WM_COMM_REQUEST_COMPLETE, 0, (long)pRequest); else if( pRequest->m_pCompleteFunction) (*pRequest->m_pCompleteFunction)( pRequest); return 0; } } } if( pRequest->m_pWindow && ::IsWindow( pRequest->m_pWindow->m_hWnd)) pRequest->m_pWindow->PostMessage( WM_COMM_REQUEST_COMPLETE, 0, (long)pRequest); else if( pRequest->m_pCompleteFunction) (*pRequest->m_pCompleteFunction)( pRequest); break; } case REQUEST_SPECIAL: { // clear request error field pRequest->m_dwRequestError = ERR_NOERROR; HANDLESPECIALREQUEST( pRequest); COMMSTATE = ERR_NOERROR; ((CCommDevice*)lParam)->m_pRequest = NULL; ResetEvent( COMMEVENT); switch( pRequest->m_dwRequestError) { case ERR_PORTERROR: case ERR_NOPORTLOCK: case ERR_NODATALOCK: { if( pRequest->m_pWindow && ::IsWindow( pRequest->m_pWindow->m_hWnd)) pRequest->m_pWindow->PostMessage( WM_COMM_REQUEST_COMPLETE, 0, (long)pRequest); else if( pRequest->m_pCompleteFunction) (*pRequest->m_pCompleteFunction)( pRequest); return 1; } case ERR_TERMINATED: { if( pRequest->m_pWindow && ::IsWindow( pRequest->m_pWindow->m_hWnd)) pRequest->m_pWindow->PostMessage( WM_COMM_REQUEST_COMPLETE, 0, (long)pRequest); else if( pRequest->m_pCompleteFunction) (*pRequest->m_pCompleteFunction)( pRequest); return 0; } } if( pRequest->m_pWindow && ::IsWindow( pRequest->m_pWindow->m_hWnd)) pRequest->m_pWindow->PostMessage( WM_COMM_REQUEST_COMPLETE, 0, (long)pRequest); else if( pRequest->m_pCompleteFunction) (*pRequest->m_pCompleteFunction)( pRequest); break; } case REQUEST_CALLBACK: default: { // clear any previous request errors pRequest->m_dwRequestError = ERR_NOERROR; ((CCommDevice*)lParam)->m_pRequest = NULL; ResetEvent( COMMEVENT); if( pRequest->m_pWindow && ::IsWindow( pRequest->m_pWindow->m_hWnd)) pRequest->m_pWindow->PostMessage( WM_COMM_REQUEST_COMPLETE, 0, (long)pRequest); else if( pRequest->m_pCompleteFunction) (*pRequest->m_pCompleteFunction)( pRequest); break; } } } }