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; } }
/** * Parse a file given the file name. */ Bool XML_ParseFile(Str path, const XMLCallbacks * cb, void * ctx) { Bool ok = False; File * f = FILE_Open(path, READ_TEXT_MODE, NULL); if (f) { ok = XML_ParseStream(f, cb, ctx); FILE_Close(f); } return ok; }
/** * Flushes the deflator and the underlying stream. * Closes the low level stream. No I/O is possible * after this function has been invoked. */ STATIC void ZipClose(File * f) { Zip * zf = ZipCast(f); if (zf) { if (zf->f) { if (zf->out && !(zf->zflags & ZIP_FINISH)) { ZipFlush2(zf, Z_FINISH); } FILE_Close(zf->f); zf->f = NULL; } ZipDetach2(zf); /* deallocates buffers */ } }
/** * Decodes a BASE64 encoded data block. Returns True if string has been * successfully decoded, or False if it the input is not a valid BASE64 * sequence cannot be decoded or if memory allocation fails. */ STATIC Bool BASE64_DecodeStr(Str base64, Buffer * out, const I8u* decodeMap) { if (base64) { size_t len = StrLen(base64); if (len > 0) { Bool ok = False; File* in = FILE_MemIn(base64, len); if (in) { ok = BASE64_DecodeFile2(in, len, out, decodeMap); FILE_Close(in); } return ok; } } /* empty string is OK */ return True; }
void Main_Read (void) { USB_INT32S fdr; USB_INT32U bytes_read; fdr = FILE_Open(FILENAME_R, RDONLY); if (fdr > 0) { PRINT_Log("Reading from %s...\n", FILENAME_R); do { bytes_read = FILE_Read(fdr, UserBuffer, MAX_BUFFER_SIZE); } while (bytes_read); FILE_Close(fdr); PRINT_Log("Read Complete\n"); } else { PRINT_Log("Could not open file %s\n", FILENAME_R); return; } }
/** * Initializes GwTrace app context, connects to the gateway and * accepts one incoming connection. */ static Bool GWTRACE_Init(GwTrace* trace, Str host, Port gwPort, Str file) { trace->server = XRPC_CreateServer(NULL); if (trace->server) { /* Register our side of the SEI proxu protocol */ XRpcProtocol protocol; memset(&protocol, 0, sizeof (protocol)); protocol.getmethod = GWTRACE_GetMethod; if (XRPC_RegisterProtocol(trace->server, ECMTGW_LISTENER_PROTOCOL, "EcmtGW listener", "Nokia", 1, 0, &protocol, trace)) { /* connect to the Gateway */ PRINT_Verbose("%s: connecting to %s:%hu\n",pname, host ? host : "localhost", gwPort); trace->session = XRPC_Connect(trace->server, host, gwPort); if (trace->session) { if (file) { trace->file = FILE_Open(file, WRITE_BINARY_MODE, NULL); if (!trace->file) { PRINT_Error("%s: can't create file %s\n",pname,file); } } if (!file || trace->file) { /* Create thread to parse incoming XML */ if (XRPC_CreateSessionThread(trace->session, GWTRACE_Session,trace)) { return True; } if (trace->file) FILE_Close(trace->file); } } XRPC_UnregisterProtocol(trace->server, ECMTGW_LISTENER_PROTOCOL); } XRPC_FreeServer(trace->server); } return False; }
/** * The program entry point */ int main(int argc, char * argv[]) { int mask = ECMTGW_LISTEN_DEFAULT_MASK; Bool traceReceive = False; Bool traceSend = False; Str host = NULL; Str file = NULL; CmdLine* c; GwTrace trace; XRpcRegistry * r; /* Initialize the XRPC library */ XRPC_Init(); /* First step of initializing GwTrace context */ memset(&trace, 0, sizeof(trace)); VECTOR_Init(&trace.includeUid, 0, NULL, NULL); VECTOR_Init(&trace.excludeUid, 0, NULL, NULL); /* Parse command line */ c = CMDLINE_Create(pname); if (c) { CmdOpt* includeOpt; CmdOpt* excludeOpt; Bool done = False; Bool help = False; CMDLINE_SetMaxArgs(c, 1); CMDLINE_AddTrueOpt(c,'h',"help", "print this help and exit",&help); CMDLINE_AddTrueOpt(c,'s',"sent", "trace packets sent to the handset",&traceSend); CMDLINE_AddTrueOpt(c,'r',"receive", "trace packets received from the handset",&traceReceive); includeOpt = CMDLINE_AddOpt(c,'u',"include", "include this UID in the trace (repeatable)", GWTRACE_ParseUidOpt, &trace.includeUid, "UID"); excludeOpt = CMDLINE_AddOpt(c,'x',"exclude", "exclude this UID from the trace (repeatable)", GWTRACE_ParseUidOpt, &trace.excludeUid, "UID"); CMDLINE_SetParamName(CMDLINE_AddStrOpt(c,'o',"output", "write binary Ecmt messages into a file",&file),"FILE"); CMDLINE_SetRepeatable(includeOpt); CMDLINE_SetRepeatable(excludeOpt); CMDLINE_Exclude(includeOpt, excludeOpt); if (!CMDLINE_Parse1(c,argv+1,argc-1,0,&host) || help) { CMDLINE_Usage(c, "[HOST]", 0); CMDLINE_Delete(c); VECTOR_Destroy(&trace.includeUid); VECTOR_Destroy(&trace.excludeUid); XRPC_Deinit(); return 0; } CMDLINE_Delete(c); } if (traceReceive || traceSend) { mask = 0; if (traceReceive) mask |= ECMTGW_LISTEN_MASK_RECEIVE; if (traceSend) mask |= ECMTGW_LISTEN_MASK_SEND; } /* connect to the registry */ r = XREG_ConnectRegistry(host, XREG_DEFAULT_PORT); if (r) { /* find the server port */ XRpcPort gwPort = 0; XREG_List(r, ECMTGW_PROTOCOL, GWTRACE_ListCB, &gwPort); XREG_FreeRegistry(r); if (gwPort) { if (EVENT_Init(&exitEvent)) { if (GWTRACE_Init(&trace, host, gwPort, file)) { /* Install signal handlers */ #ifndef _WIN32 signal(SIGPIPE, GWTRACE_Interrupt); #endif /* _WIN32 */ signal(SIGINT, GWTRACE_Interrupt); /* Enable notifications */ XRPC_FormatNotify(XRPC_GetClient(trace.session), ECMTGW_PROTOCOL, ECMTGW_REGISTER_LISTENER_METHOD,"%" ECMTGW_LISTENER_PROTOCOL_PARAM"!s%" ECMTGW_LISTENER_MASK_PARAM"!i", ECMTGW_LISTENER_PROTOCOL, mask); /* Wait */ EVENT_Wait(&exitEvent); /* cleanup */ XRPC_FreeSession(trace.session); XRPC_FreeServer(trace.server); if (trace.file) FILE_Close(trace.file); } EVENT_Destroy(&exitEvent); } } else { PRINT_Error("%s: Ecmt Gateway is not running.\n",pname); } } else if (host) { PRINT_Verbose("%s: XRPC registry is not running on %s\n",pname,host); PRINT_Error("%s: Ecmt Gateway is not running on %s.\n",pname,host); } else { PRINT_Verbose("%s: XRPC registry is not running\n",pname); PRINT_Error("%s: Ecmt Gateway is not running.\n",pname); } VECTOR_Destroy(&trace.includeUid); VECTOR_Destroy(&trace.excludeUid); /* Deinitialize the XRPC library */ XRPC_Deinit(); return 0; }
/** * Same as the above, only allows you to specify file mode (text vs binary) * on those platforms where it matters (e.g. Windows). */ Bool FILE_Save2(Str fname, FileSaveCB cb, void * ctx, Bool txt, IODesc io) { Bool success = False; ASSERT(fname && fname[0]); if (fname && fname[0]) { Char * tmp1 = FILE_DirName(fname, TEMP_FILE_NAME_LEN); if (tmp1 && (!tmp1[0] || FILE_MkDir(tmp1))) { const char * mode = txt ? WRITE_TEXT_MODE : WRITE_BINARY_MODE; size_t dirlen = StrLen(tmp1); File * f; /* * write the temp file */ FILE_MakeUnique(tmp1, dirlen, TEMP_FILE_NAME_LEN); f = FILE_Open(tmp1, mode, io); if (f) { Bool saved = (*cb)(f,fname,ctx); FILE_Close(f); if (saved) { if (FILE_CanRead(fname)) { /* * generate another unique file name */ size_t nchars = dirlen + 1 + TEMP_FILE_NAME_LEN; Char * tmp2 = MEM_NewArray(Char,nchars); if (tmp2) { StrCpy(tmp2, tmp1); FILE_MakeUnique(tmp2,dirlen,TEMP_FILE_NAME_LEN); /* * rename target -> tmp2 */ Verbose(TEXT("Renaming %s into %s\n"),fname,tmp2); if (FILE_Rename(fname, tmp2)) { /* * rename tmp1 -> target */ Verbose(TEXT("Renaming %s into %s\n"),tmp1,fname); if (FILE_Rename(tmp1, fname)) { /* * finally, remove tmp2 (old target) * perhaps, we should ignore the error * returned by FILE_Delete? */ Verbose(TEXT("Deleting %s\n"),tmp2); success = FILE_Delete(tmp2); } } MEM_Free(tmp2); } /* * there's no target, just rename tmp1 -> target */ } else { Verbose(TEXT("Renaming %s into %s\n"),tmp1,fname); success = FILE_Rename(tmp1, fname); } } else { /* * just quietly delete the temporary file. An error * message, if any, should have been provided by the * callback. */ FILE_Delete(tmp1); } } } MEM_Free(tmp1); } return success; }