void Main_Copy (void) { USB_INT32S fdr; USB_INT32S fdw; USB_INT32U bytes_read; fdr = FILE_Open(FILENAME_R, RDONLY); if (fdr > 0) { fdw = FILE_Open(FILENAME_W, RDWR); if (fdw > 0) { PRINT_Log("Copying from %s to %s...\n", FILENAME_R, FILENAME_W); do { bytes_read = FILE_Read(fdr, UserBuffer, MAX_BUFFER_SIZE); FILE_Write(fdw, UserBuffer, bytes_read); } while (bytes_read); FILE_Close(fdw); } else { PRINT_Log("Could not open file %s\n", FILENAME_W); return; } FILE_Close(fdr); PRINT_Log("Copy completed\n"); } else { PRINT_Log("Could not open file %s\n", FILENAME_R); return; } }
void Main_Write (void) { USB_INT32S fdw; USB_INT32S fdr; USB_INT32U tot_bytes_written; USB_INT32U bytes_written; fdr = FILE_Open(FILENAME_R, RDONLY); if (fdr > 0) { FILE_Read(fdr, UserBuffer, MAX_BUFFER_SIZE); fdw = FILE_Open(FILENAME_W, RDWR); if (fdw > 0) { tot_bytes_written = 0; PRINT_Log("Writing to %s...\n", FILENAME_W); do { bytes_written = FILE_Write(fdw, UserBuffer, MAX_BUFFER_SIZE); tot_bytes_written += bytes_written; } while (tot_bytes_written < WRITE_SIZE); FILE_Close(fdw); PRINT_Log("Write completed\n"); } else { PRINT_Log("Could not open file %s\n", FILENAME_W); return; } FILE_Close(fdr); } else { PRINT_Log("Could not open file %s\n", FILENAME_R); return; } }
/** * Compresses and writes len bytes */ STATIC int ZipWrite(File * f, const void * buf, int len) { Zip * zf = ZipCast(f); /* check if output is transparent */ if (!(zf->zflags & ZIP_OUT)) { int nbytes = FILE_Write(zf->f, buf, len); if (nbytes < 0) zf->zflags |= ZIP_OUT_ERR; return nbytes; } /* check for errors (ZIP_OUT_ERR) and caller's stupidity (ZIP_FINISH) */ ASSERT(!(zf->zflags & ZIP_FINISH)); if (zf->zflags & (ZIP_OUT_ERR | ZIP_FINISH)) { return -1; } /* check if we can write to the low level file */ if (!FILE_AllowsWrites(zf->f)) { zf->zflags |= ZIP_OUT_ERR; return -1; } /* "lazy" allocation of the input context */ if (!zf->out) { if (!ZipInitOut(zf)) { return -1; } } /* deflate and write data */ zf->out->next_in = (Bytef*)buf; zf->out->avail_in = len; while (zf->out->avail_in != 0) { if (zf->out->avail_out == 0) { zf->out->next_out = zf->outbuf; if (FILE_Write(zf->f, zf->outbuf, zf->bufsize) != zf->bufsize) { zf->zflags |= ZIP_OUT_ERR; return -1; } zf->out->avail_out = zf->bufsize; } if (deflate(zf->out, Z_NO_FLUSH) != Z_OK) break; } zf->outcrc = crc32(zf->outcrc, (const Bytef *)buf, len); return (len - zf->out->avail_in); }
/** * Initializes the output zlib context */ STATIC Bool ZipInitOut(Zip * zf) { /* allocate buffer */ zf->outbuf = (I8u*)MEM_Alloc(zf->bufsize); if (zf->outbuf) { /* allocate zlib context */ zf->out = MEM_New(z_stream); if (zf->out) { int zerr; int bits = ((zf->zflags & ZIP_ZHDR) ? (-MAX_WBITS) : MAX_WBITS); /* tell zlib to use our memory allocation functions */ memset(zf->out, 0, sizeof(*zf->out)); zf->out->zalloc = ZipMemAlloc; zf->out->zfree = ZipMemFree; /* windowBits is passed < 0 to suppress zlib header */ zerr = deflateInit2(zf->out, Z_BEST_COMPRESSION, Z_DEFLATED, bits, 8, Z_DEFAULT_STRATEGY); if (zerr == Z_OK) { if (zf->zflags & ZIP_GZIP) { /* write a very simple .gz header */ I8u hdr[10]; memset(hdr, 0, sizeof(hdr)); hdr[0] = (I8u)GzMagic[0]; hdr[1] = (I8u)GzMagic[1]; hdr[2] = Z_DEFLATED; hdr[9] = OS_CODE; if (FILE_Write(zf->f,hdr,sizeof(hdr)) == sizeof(hdr)) { FILE_Flush(zf->f); zf->out->next_out = zf->outbuf; zf->out->avail_out = zf->bufsize; return True; } } else { /* not writing the header */ zf->out->next_out = zf->outbuf; zf->out->avail_out = zf->bufsize; return True; } deflateEnd(zf->out); } MEM_Free(zf->out); zf->out = NULL; } MEM_Free(zf->outbuf); zf->outbuf = NULL; } zf->zflags |= ZIP_OUT_ERR; return False; }
static void GWTRACE_Dump(GwTrace* trace, Str what, const XRpcContainer* param) { /* decode parameters */ const XRpcIntElement* uidParam = XRPC_GetIntElementByName(param, ECMTGW_LISTENER_UID_PARAM); const XRpcBinaryElement* dataParam = XRPC_GetBinaryElementByName(param, ECMTGW_LISTENER_DATA_PARAM); if (uidParam && dataParam) { int uid = XRPC_GetInt(uidParam); if (GWTRACE_AcceptMessage(trace, uid)) { int len = XRPC_GetBinaryDataSize(dataParam); const XRpcByte * data = XRPC_GetBinaryData(dataParam); /* dump to the console */ PRINT_Output("%s: UID=0x%08x, %d byte(s)\n",what,uid,len); PRINT_Dump(PRINT_Output, data, len, 0); /* write to a file */ if (trace->file) { int total = len + ECMT_MSG_HEADER_SIZE; if (FILE_Printf(trace->file,"0x%08X0x%08X",total,uid) > 0 && FILE_Write(trace->file, data, len) > 0) { FILE_Flush(trace->file); } else { /* I/O error. print an error message and exit */ Str fname = FILE_Name(trace->file); if (fname) { PRINT_Error("%s: error writing %s\n",pname,fname); } EVENT_Set(&exitEvent); } } } } }
/** * Sends all the data compressed so far to the underlying stream. */ STATIC Bool ZipFlush2(Zip * zf, int flush) { if (zf->out) { int zerr = Z_OK; Bool done = False; ASSERT(!zf->out->avail_in); zf->out->avail_in = 0; /* should be zero already anyway */ for (;;) { int len = zf->bufsize - zf->out->avail_out; if (len != 0) { if (FILE_Write(zf->f, zf->outbuf, len) != len) { zf->zflags |= ZIP_OUT_ERR; return False; } zf->out->next_out = zf->outbuf; zf->out->avail_out = zf->bufsize; } if (done) break; zerr = deflate(zf->out, flush); /* ignore the second of two consecutive flushes */ if (len == 0 && zerr == Z_BUF_ERROR) zerr = Z_OK; /* deflate has finished flushing only when it hasn't used * up all the available space in the output buffer */ if (zf->out->avail_out || zerr == Z_STREAM_END) { done = True; } if (zerr != Z_OK && zerr != Z_STREAM_END) break; } return BoolValue(FILE_Flush(zf->f) && (zerr == Z_STREAM_END)); } return True; }