From acafe9829b0534d0b052f7382c77ea7fc753260c Mon Sep 17 00:00:00 2001 From: midipix Date: Sun, 17 Jul 2016 03:00:30 -0400 Subject: internals: ptyc_open_file(), ptyc_open_dir(): [implementation and] integration. --- src/internal/ptycon_driver_impl.h | 3 ++ src/internal/ptycon_open_impl.c | 78 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 81 insertions(+) create mode 100644 src/internal/ptycon_open_impl.c (limited to 'src/internal') diff --git a/src/internal/ptycon_driver_impl.h b/src/internal/ptycon_driver_impl.h index 03f0c9b..67393ff 100644 --- a/src/internal/ptycon_driver_impl.h +++ b/src/internal/ptycon_driver_impl.h @@ -61,4 +61,7 @@ static inline struct ptyc_driver_ctx_impl * ptyc_get_driver_ictx(struct ptyc_dri return 0; } +int32_t ptyc_open_file(void ** hfile, void * hat, const char * arg, bool fprivate); +int32_t ptyc_open_dir(void ** hfile, void * hat, const char * arg, bool fprivate); + #endif diff --git a/src/internal/ptycon_open_impl.c b/src/internal/ptycon_open_impl.c new file mode 100644 index 0000000..efda8bc --- /dev/null +++ b/src/internal/ptycon_open_impl.c @@ -0,0 +1,78 @@ +/*********************************************************/ +/* ptycon: a pty-console bridge */ +/* Copyright (C) 2016 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.PTYCON. */ +/*********************************************************/ + +#include +#include +#include + +#include +#include "ptycon_driver_impl.h" + +static int32_t ptyc_open( + void ** hfile, + void * hat, + const char * arg, + uint32_t options, + bool fprivate) +{ + int32_t status; + nt_oa oa; + nt_iosb iosb; + nt_unicode_string path; + nt_unicode_conversion_params_utf8_to_utf16 params = {0,0,0,0,0,0,0,0,0}; + wchar16_t buffer[4096]; + wchar16_t * wch; + size_t nbytes; + + /* utf-8 --> utf-16 */ + params.src = (const unsigned char *)arg; + params.src_size_in_bytes= ntapi->tt_string_null_offset_multibyte(arg); + params.dst = buffer; + params.dst_size_in_bytes= sizeof(buffer); + + if ((status = ntapi->uc_convert_unicode_stream_utf8_to_utf16(¶ms))) + return status; + + /* convenience */ + for (wch=buffer, nbytes=params.bytes_written; nbytes; ) { + if (*wch == '/') + *wch = '\\'; + + nbytes -= sizeof(wchar16_t); + wch++; + } + + /* path */ + path.maxlen = 0; + path.strlen = (uint16_t)params.bytes_written; + path.buffer = buffer; + + /* oa */ + oa.len = sizeof(nt_oa); + oa.root_dir = hat; + oa.obj_name = &path; + oa.obj_attr = fprivate ? 0 : NT_OBJ_INHERIT; + oa.sec_desc = 0; + oa.sec_qos = 0; + + /* open */ + return ntapi->zw_open_file( + hfile, + NT_SEC_SYNCHRONIZE | NT_FILE_READ_ATTRIBUTES | NT_FILE_READ_DATA, + &oa,&iosb, + NT_FILE_SHARE_READ | NT_FILE_SHARE_WRITE | NT_FILE_SHARE_DELETE, + options | NT_FILE_SYNCHRONOUS_IO_ALERT); +} + +int32_t ptyc_open_file(void ** hfile, void * hat, const char * arg, bool fprivate) +{ + return ptyc_open(hfile,hat,arg,NT_FILE_NON_DIRECTORY_FILE,fprivate); +} + +int32_t ptyc_open_dir(void ** hfile, void * hat, const char * arg, bool fprivate) +{ + return ptyc_open(hfile,hat,arg,NT_FILE_DIRECTORY_FILE,fprivate); +} -- cgit v1.2.3