void test_stream(void) { STREAM * stream; int pos; uint32 n; uint64 n64; stream = stream_new(1); pos = stream_get_pos(stream); stream_write_uint8(stream, 0xFE); stream_check_size(stream, 14); stream_write_uint16(stream, 0x0102); stream_write_uint32(stream, 0x03040506); stream_write_uint64(stream, 0x0708091011121314LL); /* freerdp_hexdump(stream->buffer, 15); */ stream_set_pos(stream, pos); stream_seek(stream, 3); stream_read_uint32(stream, n); stream_read_uint64(stream, n64); CU_ASSERT(n == 0x03040506); CU_ASSERT(n64 == 0x0708091011121314LL); stream_free(stream); }
static void printer_process_irp_write(PRINTER_DEVICE* printer_dev, IRP* irp) { rdpPrintJob* printjob = NULL; uint32 Length; uint64 Offset; stream_read_uint32(irp->input, Length); stream_read_uint64(irp->input, Offset); (void) Offset; stream_seek(irp->input, 20); /* Padding */ if (printer_dev->printer != NULL) printjob = printer_dev->printer->FindPrintJob(printer_dev->printer, irp->FileId); if (printjob == NULL) { irp->IoStatus = STATUS_UNSUCCESSFUL; Length = 0; log_warning("printjob id %d not found.", irp->FileId); } else { printjob->Write(printjob, stream_get_tail(irp->input), Length); log_debug("printjob id %d written %d bytes.", irp->FileId, Length); } stream_write_uint32(irp->output, Length); stream_write_uint8(irp->output, 0); /* Padding */ irp->Complete(irp); }
/* http://msdn.microsoft.com/en-us/library/dd390700.aspx */ static uint32 tsmf_codec_parse_VIDEOINFOHEADER(TS_AM_MEDIA_TYPE* mediatype, STREAM* s) { /* typedef struct tagVIDEOINFOHEADER { RECT rcSource; //16 RECT rcTarget; //16 32 DWORD dwBitRate; //4 36 DWORD dwBitErrorRate; //4 40 REFERENCE_TIME AvgTimePerFrame; //8 48 BITMAPINFOHEADER bmiHeader; } VIDEOINFOHEADER; */ uint64 AvgTimePerFrame; /* VIDEOINFOHEADER.rcSource, RECT(LONG left, LONG top, LONG right, LONG bottom) */ stream_seek_uint32(s); stream_seek_uint32(s); stream_read_uint32(s, mediatype->Width); stream_read_uint32(s, mediatype->Height); /* VIDEOINFOHEADER.rcTarget */ stream_seek(s, 16); /* VIDEOINFOHEADER.dwBitRate */ stream_read_uint32(s, mediatype->BitRate); /* VIDEOINFOHEADER.dwBitErrorRate */ stream_seek_uint32(s); /* VIDEOINFOHEADER.AvgTimePerFrame */ stream_read_uint64(s, AvgTimePerFrame); mediatype->SamplesPerSecond.Numerator = 1000000; mediatype->SamplesPerSecond.Denominator = (int)(AvgTimePerFrame / 10LL); return 48; }
static void serial_process_irp_read(SERIAL_DEVICE* serial, IRP* irp) { SERIAL_TTY* tty; uint32 Length; uint64 Offset; uint8* buffer = NULL; stream_read_uint32(irp->input, Length); stream_read_uint64(irp->input, Offset); DEBUG_SVC("length %u offset %llu", Length, Offset); tty = serial->tty; if (tty == NULL) { irp->IoStatus = STATUS_UNSUCCESSFUL; Length = 0; DEBUG_WARN("tty not valid."); } else { buffer = (uint8*)xmalloc(Length); if (!serial_tty_read(tty, buffer, &Length)) { irp->IoStatus = STATUS_UNSUCCESSFUL; xfree(buffer); buffer = NULL; Length = 0; DEBUG_WARN("read %s(%d) failed.", serial->path, tty->id); } else { DEBUG_SVC("read %llu-%llu from %d", Offset, Offset + Length, tty->id); } } stream_write_uint32(irp->output, Length); if (Length > 0) { stream_check_size(irp->output, Length); stream_write(irp->output, buffer, Length); } xfree(buffer); irp->Complete(irp); }
static lua_module_t lua_module_load_resource(const uuid_t uuid) { lua_module_t module = {0, 0}; bool success = false; const uint32_t expected_version = 1; uint64_t platform = 0; stream_t* stream; stream = resource_stream_open_static(uuid, platform); if (stream) { resource_header_t header = resource_stream_read_header(stream); if ((header.type == HASH_LUA) && (header.version == expected_version)) { success = true; } else { log_warnf(HASH_LUA, WARNING_INVALID_VALUE, STRING_CONST("Got unexpected type/version when loading Lua module: %" PRIx64 " : %u"), header.type, header.version); } stream_deallocate(stream); stream = nullptr; } if (success) stream = resource_stream_open_dynamic(uuid, platform); if (stream) { uint32_t version = stream_read_uint32(stream); size_t size = (size_t)stream_read_uint64(stream); if (version == expected_version) { void* buffer = memory_allocate(HASH_LUA, size, 0, MEMORY_TEMPORARY); if (stream_read(stream, buffer, size) == size) { module.bytecode = buffer; module.size = size; } else { memory_deallocate(buffer); } } else { log_warnf(HASH_LUA, WARNING_INVALID_VALUE, STRING_CONST("Got unexpected type/version when loading Lua module blob: %u"), version); } stream_deallocate(stream); stream = nullptr; } return module; }
static void parallel_process_irp_read(PARALLEL_DEVICE* parallel, IRP* irp) { uint32 Length; uint64 Offset; ssize_t status; uint8* buffer = NULL; stream_read_uint32(irp->input, Length); stream_read_uint64(irp->input, Offset); buffer = (uint8*) xmalloc(Length); status = read(parallel->file, irp->output->p, Length); if (status < 0) { irp->IoStatus = STATUS_UNSUCCESSFUL; xfree(buffer); buffer = NULL; Length = 0; DEBUG_WARN("read %s(%d) failed", parallel->path, parallel->id); } else { DEBUG_SVC("read %llu-%llu from %d", Offset, Offset + Length, parallel->id); } stream_write_uint32(irp->output, Length); if (Length > 0) { stream_check_size(irp->output, Length); stream_write(irp->output, buffer, Length); } xfree(buffer); irp->Complete(irp); }
static void serial_process_irp_write(SERIAL_DEVICE* serial, IRP* irp) { SERIAL_TTY* tty; uint32 Length; uint64 Offset; stream_read_uint32(irp->input, Length); stream_read_uint64(irp->input, Offset); stream_seek(irp->input, 20); /* Padding */ DEBUG_SVC("length %u offset %llu", Length, Offset); tty = serial->tty; if (tty == NULL) { irp->IoStatus = STATUS_UNSUCCESSFUL; Length = 0; DEBUG_WARN("tty not valid."); } else if (!serial_tty_write(tty, stream_get_tail(irp->input), Length)) { irp->IoStatus = STATUS_UNSUCCESSFUL; Length = 0; DEBUG_WARN("write %s(%d) failed.", serial->path, tty->id); } else { DEBUG_SVC("write %llu-%llu to %s(%d).", Offset, Offset + Length, serial->path, tty->id); } stream_write_uint32(irp->output, Length); stream_write_uint8(irp->output, 0); /* Padding */ irp->Complete(irp); }
static void parallel_process_irp_write(PARALLEL_DEVICE* parallel, IRP* irp) { uint32 Length; uint64 Offset; ssize_t status; uint32 len; stream_read_uint32(irp->input, Length); stream_read_uint64(irp->input, Offset); stream_seek(irp->input, 20); /* Padding */ DEBUG_SVC("Length %u Offset %llu", Length, Offset); len = Length; while (len > 0) { status = write(parallel->file, stream_get_tail(irp->input), len); if (status < 0) { irp->IoStatus = STATUS_UNSUCCESSFUL; Length = 0; DEBUG_WARN("write %s(%d) failed.", parallel->path, parallel->id); break; } stream_seek(irp->input, status); len -= status; } stream_write_uint32(irp->output, Length); stream_write_uint8(irp->output, 0); /* Padding */ irp->Complete(irp); }
/* http://msdn.microsoft.com/en-us/library/dd407326.aspx */ static uint32 tsmf_codec_parse_VIDEOINFOHEADER2(TS_AM_MEDIA_TYPE* mediatype, STREAM* s) { uint64 AvgTimePerFrame; /* VIDEOINFOHEADER2.rcSource, RECT(LONG left, LONG top, LONG right, LONG bottom) */ stream_seek_uint32(s); stream_seek_uint32(s); stream_read_uint32(s, mediatype->Width); stream_read_uint32(s, mediatype->Height); /* VIDEOINFOHEADER2.rcTarget */ stream_seek(s, 16); /* VIDEOINFOHEADER2.dwBitRate */ stream_read_uint32(s, mediatype->BitRate); /* VIDEOINFOHEADER2.dwBitErrorRate */ stream_seek_uint32(s); /* VIDEOINFOHEADER2.AvgTimePerFrame */ stream_read_uint64(s, AvgTimePerFrame); mediatype->SamplesPerSecond.Numerator = 1000000; mediatype->SamplesPerSecond.Denominator = (int)(AvgTimePerFrame / 10LL); /* Remaining fields before bmiHeader */ stream_seek(s, 24); return 72; }
boolean disk_file_set_information(DISK_FILE* file, uint32 FsInformationClass, uint32 Length, STREAM* input) { char* s; mode_t m; uint64 size; char* fullpath; struct stat st; UNICONV* uniconv; struct timeval tv[2]; uint64 LastWriteTime; uint32 FileAttributes; uint32 FileNameLength; switch (FsInformationClass) { case FileBasicInformation: /* http://msdn.microsoft.com/en-us/library/cc232094.aspx */ stream_seek_uint64(input); /* CreationTime */ stream_seek_uint64(input); /* LastAccessTime */ stream_read_uint64(input, LastWriteTime); stream_seek_uint64(input); /* ChangeTime */ stream_read_uint32(input, FileAttributes); if (fstat(file->fd, &st) != 0) return false; tv[0].tv_sec = st.st_atime; tv[0].tv_usec = 0; tv[1].tv_sec = (LastWriteTime > 0 ? FILE_TIME_RDP_TO_SYSTEM(LastWriteTime) : st.st_mtime); tv[1].tv_usec = 0; futimes(file->fd, tv); if (FileAttributes > 0) { m = st.st_mode; if ((FileAttributes & FILE_ATTRIBUTE_READONLY) == 0) m |= S_IWUSR; else m &= ~S_IWUSR; if (m != st.st_mode) fchmod(file->fd, st.st_mode); } break; case FileEndOfFileInformation: /* http://msdn.microsoft.com/en-us/library/cc232067.aspx */ case FileAllocationInformation: /* http://msdn.microsoft.com/en-us/library/cc232076.aspx */ stream_read_uint64(input, size); if (ftruncate(file->fd, size) != 0) return false; break; case FileDispositionInformation: /* http://msdn.microsoft.com/en-us/library/cc232098.aspx */ stream_read_uint8(input, file->delete_pending); break; case FileRenameInformation: /* http://msdn.microsoft.com/en-us/library/cc232085.aspx */ stream_seek_uint8(input); /* ReplaceIfExists */ stream_seek_uint8(input); /* RootDirectory */ stream_read_uint32(input, FileNameLength); uniconv = freerdp_uniconv_new(); s = freerdp_uniconv_in(uniconv, stream_get_tail(input), FileNameLength); freerdp_uniconv_free(uniconv); fullpath = disk_file_combine_fullpath(file->basepath, s); xfree(s); if (rename(file->fullpath, fullpath) == 0) { DEBUG_SVC("renamed %s to %s", file->fullpath, fullpath); disk_file_set_fullpath(file, fullpath); } else { DEBUG_WARN("rename %s to %s failed", file->fullpath, fullpath); free(fullpath); return false; } break; default: DEBUG_WARN("invalid FsInformationClass %d", FsInformationClass); return false; } return true; }