From 49dac1e014ec77fad2f57a13eeda7b4b342e8685 Mon Sep 17 00:00:00 2001 From: jsorg71 Date: Thu, 11 Jan 2007 07:53:06 +0000 Subject: [PATCH] remove uirdesktop --- uirdesktop/orders.h | 368 --------- uirdesktop/parse.h | 95 --- uirdesktop/proto.h | 316 -------- uirdesktop/rdesktop.h | 126 --- uirdesktop/rdp5.c | 121 --- uirdesktop/secure.c | 1007 ----------------------- uirdesktop/ssl_calls.c | 1645 -------------------------------------- uirdesktop/tcp.c | 316 -------- uirdesktop/types.h | 270 ------- uirdesktop/uimain.c | 1185 --------------------------- uirdesktop/uimain.h | 96 --- uirdesktop/win32.c | 1733 ---------------------------------------- 12 files changed, 7278 deletions(-) delete mode 100644 uirdesktop/orders.h delete mode 100644 uirdesktop/parse.h delete mode 100755 uirdesktop/proto.h delete mode 100644 uirdesktop/rdesktop.h delete mode 100644 uirdesktop/rdp5.c delete mode 100644 uirdesktop/secure.c delete mode 100755 uirdesktop/ssl_calls.c delete mode 100644 uirdesktop/tcp.c delete mode 100644 uirdesktop/types.h delete mode 100755 uirdesktop/uimain.c delete mode 100755 uirdesktop/uimain.h delete mode 100755 uirdesktop/win32.c diff --git a/uirdesktop/orders.h b/uirdesktop/orders.h deleted file mode 100644 index b1272822..00000000 --- a/uirdesktop/orders.h +++ /dev/null @@ -1,368 +0,0 @@ -/* - rdesktop: A Remote Desktop Protocol client. - RDP order processing - Copyright (C) Matthew Chapman 1999-2005 - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -*/ - -#define RDP_ORDER_STANDARD 0x01 -#define RDP_ORDER_SECONDARY 0x02 -#define RDP_ORDER_BOUNDS 0x04 -#define RDP_ORDER_CHANGE 0x08 -#define RDP_ORDER_DELTA 0x10 -#define RDP_ORDER_LASTBOUNDS 0x20 -#define RDP_ORDER_SMALL 0x40 -#define RDP_ORDER_TINY 0x80 - -enum RDP_ORDER_TYPE -{ - RDP_ORDER_DESTBLT = 0, - RDP_ORDER_PATBLT = 1, - RDP_ORDER_SCREENBLT = 2, - RDP_ORDER_LINE = 9, - RDP_ORDER_RECT = 10, - RDP_ORDER_DESKSAVE = 11, - RDP_ORDER_MEMBLT = 13, - RDP_ORDER_TRIBLT = 14, - RDP_ORDER_POLYGON = 20, - RDP_ORDER_POLYGON2 = 21, - RDP_ORDER_POLYLINE = 22, - RDP_ORDER_ELLIPSE = 25, - RDP_ORDER_ELLIPSE2 = 26, - RDP_ORDER_TEXT2 = 27 -}; - -enum RDP_SECONDARY_ORDER_TYPE -{ - RDP_ORDER_RAW_BMPCACHE = 0, - RDP_ORDER_COLCACHE = 1, - RDP_ORDER_BMPCACHE = 2, - RDP_ORDER_FONTCACHE = 3, - RDP_ORDER_RAW_BMPCACHE2 = 4, - RDP_ORDER_BMPCACHE2 = 5, - RDP_ORDER_BRUSHCACHE = 7 -}; - -typedef struct _DESTBLT_ORDER -{ - sint16 x; - sint16 y; - sint16 cx; - sint16 cy; - uint8 opcode; - -} -DESTBLT_ORDER; - -typedef struct _PATBLT_ORDER -{ - sint16 x; - sint16 y; - sint16 cx; - sint16 cy; - uint8 opcode; - uint32 bgcolour; - uint32 fgcolour; - BRUSH brush; - -} -PATBLT_ORDER; - -typedef struct _SCREENBLT_ORDER -{ - sint16 x; - sint16 y; - sint16 cx; - sint16 cy; - uint8 opcode; - sint16 srcx; - sint16 srcy; - -} -SCREENBLT_ORDER; - -typedef struct _LINE_ORDER -{ - uint16 mixmode; - sint16 startx; - sint16 starty; - sint16 endx; - sint16 endy; - uint32 bgcolour; - uint8 opcode; - PEN pen; - -} -LINE_ORDER; - -typedef struct _RECT_ORDER -{ - sint16 x; - sint16 y; - sint16 cx; - sint16 cy; - uint32 colour; - -} -RECT_ORDER; - -typedef struct _DESKSAVE_ORDER -{ - uint32 offset; - sint16 left; - sint16 top; - sint16 right; - sint16 bottom; - uint8 action; - -} -DESKSAVE_ORDER; - -typedef struct _TRIBLT_ORDER -{ - uint8 colour_table; - uint8 cache_id; - sint16 x; - sint16 y; - sint16 cx; - sint16 cy; - uint8 opcode; - sint16 srcx; - sint16 srcy; - uint32 bgcolour; - uint32 fgcolour; - BRUSH brush; - uint16 cache_idx; - uint16 unknown; - -} -TRIBLT_ORDER; - -typedef struct _MEMBLT_ORDER -{ - uint8 colour_table; - uint8 cache_id; - sint16 x; - sint16 y; - sint16 cx; - sint16 cy; - uint8 opcode; - sint16 srcx; - sint16 srcy; - uint16 cache_idx; - -} -MEMBLT_ORDER; - -#define MAX_DATA 256 - -typedef struct _POLYGON_ORDER -{ - sint16 x; - sint16 y; - uint8 opcode; - uint8 fillmode; - uint32 fgcolour; - uint8 npoints; - uint8 datasize; - uint8 data[MAX_DATA]; - -} -POLYGON_ORDER; - -typedef struct _POLYGON2_ORDER -{ - sint16 x; - sint16 y; - uint8 opcode; - uint8 fillmode; - uint32 bgcolour; - uint32 fgcolour; - BRUSH brush; - uint8 npoints; - uint8 datasize; - uint8 data[MAX_DATA]; - -} -POLYGON2_ORDER; - -typedef struct _POLYLINE_ORDER -{ - sint16 x; - sint16 y; - uint8 opcode; - uint32 fgcolour; - uint8 lines; - uint8 datasize; - uint8 data[MAX_DATA]; - -} -POLYLINE_ORDER; - -typedef struct _ELLIPSE_ORDER -{ - sint16 left; - sint16 top; - sint16 right; - sint16 bottom; - uint8 opcode; - uint8 fillmode; - uint32 fgcolour; - -} -ELLIPSE_ORDER; - -typedef struct _ELLIPSE2_ORDER -{ - sint16 left; - sint16 top; - sint16 right; - sint16 bottom; - uint8 opcode; - uint8 fillmode; - BRUSH brush; - uint32 bgcolour; - uint32 fgcolour; - -} -ELLIPSE2_ORDER; - -#define MAX_TEXT 256 - -typedef struct _TEXT2_ORDER -{ - uint8 font; - uint8 flags; - uint8 opcode; - uint8 mixmode; - uint32 bgcolour; - uint32 fgcolour; - sint16 clipleft; - sint16 cliptop; - sint16 clipright; - sint16 clipbottom; - sint16 boxleft; - sint16 boxtop; - sint16 boxright; - sint16 boxbottom; - BRUSH brush; - sint16 x; - sint16 y; - uint8 length; - uint8 text[MAX_TEXT]; - -} -TEXT2_ORDER; - -typedef struct _RDP_ORDER_STATE -{ - uint8 order_type; - BOUNDS bounds; - - DESTBLT_ORDER destblt; - PATBLT_ORDER patblt; - SCREENBLT_ORDER screenblt; - LINE_ORDER line; - RECT_ORDER rect; - DESKSAVE_ORDER desksave; - MEMBLT_ORDER memblt; - TRIBLT_ORDER triblt; - POLYGON_ORDER polygon; - POLYGON2_ORDER polygon2; - POLYLINE_ORDER polyline; - ELLIPSE_ORDER ellipse; - ELLIPSE2_ORDER ellipse2; - TEXT2_ORDER text2; - -} -RDP_ORDER_STATE; - -typedef struct _RDP_RAW_BMPCACHE_ORDER -{ - uint8 cache_id; - uint8 pad1; - uint8 width; - uint8 height; - uint8 bpp; - uint16 bufsize; - uint16 cache_idx; - uint8 *data; - -} -RDP_RAW_BMPCACHE_ORDER; - -typedef struct _RDP_BMPCACHE_ORDER -{ - uint8 cache_id; - uint8 pad1; - uint8 width; - uint8 height; - uint8 bpp; - uint16 bufsize; - uint16 cache_idx; - uint16 pad2; - uint16 size; - uint16 row_size; - uint16 final_size; - uint8 *data; - -} -RDP_BMPCACHE_ORDER; - -/* RDP_BMPCACHE2_ORDER */ -#define ID_MASK 0x0007 -#define MODE_MASK 0x0038 -#define SQUARE 0x0080 -#define PERSIST 0x0100 -#define FLAG_51_UNKNOWN 0x0800 - -#define MODE_SHIFT 3 - -#define LONG_FORMAT 0x80 -#define BUFSIZE_MASK 0x3FFF /* or 0x1FFF? */ - -#define MAX_GLYPH 32 - -typedef struct _RDP_FONT_GLYPH -{ - uint16 character; - uint16 unknown; - uint16 baseline; - uint16 width; - uint16 height; - uint8 data[MAX_GLYPH]; - -} -RDP_FONT_GLYPH; - -#define MAX_GLYPHS 256 - -typedef struct _RDP_FONTCACHE_ORDER -{ - uint8 font; - uint8 nglyphs; - RDP_FONT_GLYPH glyphs[MAX_GLYPHS]; - -} -RDP_FONTCACHE_ORDER; - -typedef struct _RDP_COLCACHE_ORDER -{ - uint8 cache_id; - COLOURMAP map; - -} -RDP_COLCACHE_ORDER; diff --git a/uirdesktop/parse.h b/uirdesktop/parse.h deleted file mode 100644 index 758e53fa..00000000 --- a/uirdesktop/parse.h +++ /dev/null @@ -1,95 +0,0 @@ -/* - rdesktop: A Remote Desktop Protocol client. - Parsing primitives - Copyright (C) Matthew Chapman 1999-2005 - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -*/ - -/* Parser state */ -typedef struct stream -{ - unsigned char *p; - unsigned char *end; - unsigned char *data; - unsigned int size; - - /* Offsets of various headers */ - unsigned char *iso_hdr; - unsigned char *mcs_hdr; - unsigned char *sec_hdr; - unsigned char *rdp_hdr; - unsigned char *channel_hdr; - -} - *STREAM; - -#define s_push_layer(s,h,n) { (s)->h = (s)->p; (s)->p += n; } -#define s_pop_layer(s,h) (s)->p = (s)->h; -#define s_mark_end(s) (s)->end = (s)->p; -#define s_check(s) ((s)->p <= (s)->end) -#define s_check_rem(s,n) ((s)->p + n <= (s)->end) -#define s_check_end(s) ((s)->p == (s)->end) - -#if defined(L_ENDIAN) && !defined(NEED_ALIGN) -#define in_uint16_le(s,v) { v = *(uint16 *)((s)->p); (s)->p += 2; } -#define in_uint32_le(s,v) { v = *(uint32 *)((s)->p); (s)->p += 4; } -#define out_uint16_le(s,v) { *(uint16 *)((s)->p) = v; (s)->p += 2; } -#define out_uint32_le(s,v) { *(uint32 *)((s)->p) = v; (s)->p += 4; } - -#else -#define in_uint16_le(s,v) { v = *((s)->p++); v += *((s)->p++) << 8; } -#define in_uint32_le(s,v) { in_uint16_le(s,v) \ - v += *((s)->p++) << 16; v += *((s)->p++) << 24; } -#define out_uint16_le(s,v) { *((s)->p++) = (v) & 0xff; *((s)->p++) = ((v) >> 8) & 0xff; } -#define out_uint32_le(s,v) { out_uint16_le(s, (v) & 0xffff); out_uint16_le(s, ((v) >> 16) & 0xffff); } -#endif - -#if defined(B_ENDIAN) && !defined(NEED_ALIGN) -#define in_uint16_be(s,v) { v = *(uint16 *)((s)->p); (s)->p += 2; } -#define in_uint32_be(s,v) { v = *(uint32 *)((s)->p); (s)->p += 4; } -#define out_uint16_be(s,v) { *(uint16 *)((s)->p) = v; (s)->p += 2; } -#define out_uint32_be(s,v) { *(uint32 *)((s)->p) = v; (s)->p += 4; } - -#define B_ENDIAN_PREFERRED -#define in_uint16(s,v) in_uint16_be(s,v) -#define in_uint32(s,v) in_uint32_be(s,v) -#define out_uint16(s,v) out_uint16_be(s,v) -#define out_uint32(s,v) out_uint32_be(s,v) - -#else -#define in_uint16_be(s,v) { v = *((s)->p++); next_be(s,v); } -#define in_uint32_be(s,v) { in_uint16_be(s,v); next_be(s,v); next_be(s,v); } -#define out_uint16_be(s,v) { *((s)->p++) = ((v) >> 8) & 0xff; *((s)->p++) = (v) & 0xff; } -#define out_uint32_be(s,v) { out_uint16_be(s, ((v) >> 16) & 0xffff); out_uint16_be(s, (v) & 0xffff); } -#endif - -#ifndef B_ENDIAN_PREFERRED -#define in_uint16(s,v) in_uint16_le(s,v) -#define in_uint32(s,v) in_uint32_le(s,v) -#define out_uint16(s,v) out_uint16_le(s,v) -#define out_uint32(s,v) out_uint32_le(s,v) -#endif - -#define in_uint8(s,v) v = *((s)->p++); -#define in_uint8p(s,v,n) { v = (s)->p; (s)->p += n; } -#define in_uint8a(s,v,n) { memcpy(v,(s)->p,n); (s)->p += n; } -#define in_uint8s(s,n) (s)->p += n; -#define out_uint8(s,v) *((s)->p++) = v; -#define out_uint8p(s,v,n) { memcpy((s)->p,v,n); (s)->p += n; } -#define out_uint8a(s,v,n) out_uint8p(s,v,n); -#define out_uint8s(s,n) { memset((s)->p,0,n); (s)->p += n; } - -#define next_be(s,v) v = ((v) << 8) + *((s)->p++); diff --git a/uirdesktop/proto.h b/uirdesktop/proto.h deleted file mode 100755 index 16852311..00000000 --- a/uirdesktop/proto.h +++ /dev/null @@ -1,316 +0,0 @@ -/* -*- c-basic-offset: 8 -*- - rdesktop: A Remote Desktop Protocol client. - Copyright (C) Matthew Chapman 1999-2005 - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -*/ - -#ifndef RDESKTOP_PROTO_H -#define RDESKTOP_PROTO_H - -/* *INDENT-OFF* */ -#ifdef __cplusplus -extern "C" { -#endif -/* *INDENT-ON* */ -/* bitmap.c */ -BOOL bitmap_decompress(uint8 * output, int width, int height, uint8 * input, int size, int Bpp); -BOOL bitmap_decompress_ex(uint8 * output, int width, int height, uint8 * input, int size, - int in_bpp, int out_bpp); -/* cache.c */ -void cache_rebuild_bmpcache_linked_list(uint8 id, sint16 * idx, int count); -void cache_bump_bitmap(uint8 id, uint16 idx, int bump); -void cache_evict_bitmap(uint8 id); -RD_HBITMAP cache_get_bitmap(uint8 id, uint16 idx); -void cache_put_bitmap(uint8 id, uint16 idx, RD_HBITMAP bitmap); -void cache_save_state(void); -FONTGLYPH *cache_get_font(uint8 font, uint16 character); -void cache_put_font(uint8 font, uint16 character, uint16 offset, uint16 baseline, uint16 width, - uint16 height, RD_HGLYPH pixmap); -DATABLOB *cache_get_text(uint8 cache_id); -void cache_put_text(uint8 cache_id, void *data, int length); -uint8 *cache_get_desktop(uint32 offset, int cx, int cy, int bytes_per_pixel); -void cache_put_desktop(uint32 offset, int cx, int cy, int scanline, int bytes_per_pixel, - uint8 * data); -RD_HCURSOR cache_get_cursor(uint16 cache_idx); -void cache_put_cursor(uint16 cache_idx, RD_HCURSOR cursor); -/* channels.c */ -VCHANNEL *channel_register(char *name, uint32 flags, void (*callback) (STREAM)); -STREAM channel_init(VCHANNEL * channel, uint32 length); -void channel_send(STREAM s, VCHANNEL * channel); -void channel_process(STREAM s, uint16 mcs_channel); -/* cliprdr.c */ -void cliprdr_send_simple_native_format_announce(uint32 format); -void cliprdr_send_native_format_announce(uint8 * formats_data, uint32 formats_data_length); -void cliprdr_send_data_request(uint32 format); -void cliprdr_send_data(uint8 * data, uint32 length); -void cliprdr_set_mode(const char *optarg); -BOOL cliprdr_init(void); -/* disk.c */ -int disk_enum_devices(uint32 * id, char *optarg); -RD_NTSTATUS disk_query_information(RD_NTHANDLE handle, uint32 info_class, STREAM out); -RD_NTSTATUS disk_set_information(RD_NTHANDLE handle, uint32 info_class, STREAM in, STREAM out); -RD_NTSTATUS disk_check_notify(RD_NTHANDLE handle); -RD_NTSTATUS disk_create_notify(RD_NTHANDLE handle, uint32 info_class); -RD_NTSTATUS disk_query_volume_information(RD_NTHANDLE handle, uint32 info_class, STREAM out); -RD_NTSTATUS disk_query_directory(RD_NTHANDLE handle, uint32 info_class, char *pattern, STREAM out); -/* mppc.c */ -int mppc_expand(uint8 * data, uint32 clen, uint8 ctype, uint32 * roff, uint32 * rlen); -/* ewmhints.c */ -int get_current_workarea(uint32 * x, uint32 * y, uint32 * width, uint32 * height); -void ewmh_init(void); -/* iso.c */ -STREAM iso_init(int length); -void iso_send(STREAM s); -STREAM iso_recv(uint8 * rdpver); -BOOL iso_connect(char *server, char *username); -BOOL iso_reconnect(char *server); -void iso_disconnect(void); -void iso_reset_state(void); -/* licence.c */ -void licence_process(STREAM s); -/* mcs.c */ -STREAM mcs_init(int length); -void mcs_send_to_channel(STREAM s, uint16 channel); -void mcs_send(STREAM s); -STREAM mcs_recv(uint16 * channel, uint8 * rdpver); -BOOL mcs_connect(char *server, STREAM mcs_data, char *username); -BOOL mcs_reconnect(char *server, STREAM mcs_data); -void mcs_disconnect(void); -void mcs_reset_state(void); -/* orders.c */ -void process_orders(STREAM s, uint16 num_orders); -void reset_order_state(void); -/* parallel.c */ -int parallel_enum_devices(uint32 * id, char *optarg); -/* printer.c */ -int printer_enum_devices(uint32 * id, char *optarg); -/* printercache.c */ -int printercache_load_blob(char *printer_name, uint8 ** data); -void printercache_process(STREAM s); -/* pstcache.c */ -void pstcache_touch_bitmap(uint8 cache_id, uint16 cache_idx, uint32 stamp); -BOOL pstcache_load_bitmap(uint8 cache_id, uint16 cache_idx); -BOOL pstcache_save_bitmap(uint8 cache_id, uint16 cache_idx, uint8 * key, uint8 width, - uint8 height, uint16 length, uint8 * data); -int pstcache_enumerate(uint8 id, HASH_KEY * keylist); -BOOL pstcache_init(uint8 cache_id); -/* rdesktop.c */ -int main(int argc, char *argv[]); -void generate_random(uint8 * random); -void *xmalloc(int size); -char *xstrdup(const char *s); -void *xrealloc(void *oldmem, int size); -void xfree(void *mem); -void error(char *format, ...); -void warning(char *format, ...); -void unimpl(char *format, ...); -void hexdump(unsigned char *p, unsigned int len); -char *next_arg(char *src, char needle); -void toupper_str(char *p); -BOOL str_startswith(const char *s, const char *prefix); -BOOL str_handle_lines(const char *input, char **rest, str_handle_lines_t linehandler, void *data); -BOOL subprocess(char *const argv[], str_handle_lines_t linehandler, void *data); -char *l_to_a(long N, int base); -int load_licence(unsigned char **data); -void save_licence(unsigned char *data, int length); -BOOL rd_pstcache_mkdir(void); -int rd_open_file(char *filename); -void rd_close_file(int fd); -int rd_read_file(int fd, void *ptr, int len); -int rd_write_file(int fd, void *ptr, int len); -int rd_lseek_file(int fd, int offset); -BOOL rd_lock_file(int fd, int start, int len); -/* rdp5.c */ -void rdp5_process(STREAM s); -/* rdp.c */ -void rdp_out_unistr(STREAM s, char *string, int len); -int rdp_in_unistr(STREAM s, char *string, int uni_len); -void rdp_send_input(uint32 time, uint16 message_type, uint16 device_flags, uint16 param1, - uint16 param2); -void rdp_send_client_window_status(int status); -void process_colour_pointer_pdu(STREAM s); -void process_cached_pointer_pdu(STREAM s); -void process_system_pointer_pdu(STREAM s); -void process_bitmap_updates(STREAM s); -void process_palette(STREAM s); -void process_disconnect_pdu(STREAM s, uint32 * ext_disc_reason); -void rdp_main_loop(BOOL * deactivated, uint32 * ext_disc_reason); -BOOL rdp_loop(BOOL * deactivated, uint32 * ext_disc_reason); -BOOL rdp_connect(char *server, uint32 flags, char *domain, char *password, char *command, - char *directory); -BOOL rdp_reconnect(char *server, uint32 flags, char *domain, char *password, char *command, - char *directory, char *cookie); -void rdp_reset_state(void); -void rdp_disconnect(void); -/* rdpdr.c */ -int get_device_index(RD_NTHANDLE handle); -void convert_to_unix_filename(char *filename); -BOOL rdpdr_init(void); -void rdpdr_add_fds(int *n, fd_set * rfds, fd_set * wfds, struct timeval *tv, BOOL * timeout); -struct async_iorequest *rdpdr_remove_iorequest(struct async_iorequest *prev, - struct async_iorequest *iorq); -void rdpdr_check_fds(fd_set * rfds, fd_set * wfds, BOOL timed_out); -BOOL rdpdr_abort_io(uint32 fd, uint32 major, RD_NTSTATUS status); -/* rdpsnd.c */ -void rdpsnd_send_completion(uint16 tick, uint8 packet_index); -BOOL rdpsnd_init(void); -/* rdpsnd_oss.c */ -BOOL wave_out_open(void); -void wave_out_close(void); -BOOL wave_out_format_supported(RD_WAVEFORMATEX * pwfx); -BOOL wave_out_set_format(RD_WAVEFORMATEX * pwfx); -void wave_out_volume(uint16 left, uint16 right); -void wave_out_write(STREAM s, uint16 tick, uint8 index); -void wave_out_play(void); -/* secure.c */ -void sec_hash_48(uint8 * out, uint8 * in, uint8 * salt1, uint8 * salt2, uint8 salt); -void sec_hash_16(uint8 * out, uint8 * in, uint8 * salt1, uint8 * salt2); -void buf_out_uint32(uint8 * buffer, uint32 value); -void sec_sign(uint8 * signature, int siglen, uint8 * session_key, int keylen, uint8 * data, - int datalen); -void sec_decrypt(uint8 * data, int length); -STREAM sec_init(uint32 flags, int maxlen); -void sec_send_to_channel(STREAM s, uint32 flags, uint16 channel); -void sec_send(STREAM s, uint32 flags); -void sec_process_mcs_data(STREAM s); -STREAM sec_recv(uint8 * rdpver); -BOOL sec_connect(char *server, char *username); -BOOL sec_reconnect(char *server); -void sec_disconnect(void); -void sec_reset_state(void); -/* serial.c */ -int serial_enum_devices(uint32 * id, char *optarg); -BOOL serial_get_event(RD_NTHANDLE handle, uint32 * result); -BOOL serial_get_timeout(RD_NTHANDLE handle, uint32 length, uint32 * timeout, uint32 * itv_timeout); -/* tcp.c */ -STREAM tcp_init(uint32 maxlen); -void tcp_send(STREAM s); -STREAM tcp_recv(STREAM s, uint32 length); -BOOL tcp_connect(char *server); -void tcp_disconnect(void); -char *tcp_get_address(void); -void tcp_reset_state(void); -/* xclip.c */ -void ui_clip_format_announce(uint8 * data, uint32 length); -void ui_clip_handle_data(uint8 * data, uint32 length); -void ui_clip_request_failed(void); -void ui_clip_request_data(uint32 format); -void ui_clip_sync(void); -void ui_clip_set_mode(const char *optarg); -void xclip_init(void); -/* xkeymap.c */ -BOOL xkeymap_from_locale(const char *locale); -FILE *xkeymap_open(const char *filename); -void xkeymap_init(void); -BOOL handle_special_keys(uint32 keysym, unsigned int state, uint32 ev_time, BOOL pressed); -key_translation xkeymap_translate_key(uint32 keysym, unsigned int keycode, unsigned int state); -void xkeymap_send_keys(uint32 keysym, unsigned int keycode, unsigned int state, uint32 ev_time, - BOOL pressed, uint8 nesting); -uint16 xkeymap_translate_button(unsigned int button); -char *get_ksname(uint32 keysym); -void save_remote_modifiers(uint8 scancode); -void restore_remote_modifiers(uint32 ev_time, uint8 scancode); -void ensure_remote_modifiers(uint32 ev_time, key_translation tr); -unsigned int read_keyboard_state(void); -uint16 ui_get_numlock_state(unsigned int state); -void reset_modifier_keys(void); -void rdp_send_scancode(uint32 time, uint16 flags, uint8 scancode); -/* xwin.c */ -BOOL get_key_state(unsigned int state, uint32 keysym); -BOOL ui_init(void); -void ui_deinit(void); -BOOL ui_create_window(void); -void ui_resize_window(void); -void ui_destroy_window(void); -void xwin_toggle_fullscreen(void); -int ui_select(int rdp_socket); -void ui_move_pointer(int x, int y); -RD_HBITMAP ui_create_bitmap(int width, int height, uint8 * data); -RD_HBITMAP ui_create_bitmap_ex(int width, int height, uint8 * data, int data_size, int compressed); -void ui_paint_bitmap(int x, int y, int cx, int cy, int width, int height, uint8 * data); -void ui_paint_bitmap_ex(int x, int y, int cx, int cy, int width, int height, uint8 * data, - int data_size, int compressed); -void ui_destroy_bitmap(RD_HBITMAP bmp); -RD_HGLYPH ui_create_glyph(int width, int height, uint8 * data); -void ui_destroy_glyph(RD_HGLYPH glyph); -RD_HCURSOR ui_create_cursor(unsigned int x, unsigned int y, int width, int height, uint8 * andmask, - uint8 * xormask); -void ui_set_cursor(RD_HCURSOR cursor); -void ui_destroy_cursor(RD_HCURSOR cursor); -void ui_set_null_cursor(void); -RD_HCOLOURMAP ui_create_colourmap(COLOURMAP * colours); -void ui_destroy_colourmap(RD_HCOLOURMAP map); -void ui_set_colourmap(RD_HCOLOURMAP map); -void ui_set_clip(int x, int y, int cx, int cy); -void ui_reset_clip(void); -void ui_bell(void); -void ui_destblt(uint8 opcode, int x, int y, int cx, int cy); -void ui_patblt(uint8 opcode, int x, int y, int cx, int cy, BRUSH * brush, int bgcolour, - int fgcolour); -void ui_screenblt(uint8 opcode, int x, int y, int cx, int cy, int srcx, int srcy); -void ui_memblt(uint8 opcode, int x, int y, int cx, int cy, RD_HBITMAP src, int srcx, int srcy); -void ui_triblt(uint8 opcode, int x, int y, int cx, int cy, RD_HBITMAP src, int srcx, int srcy, - BRUSH * brush, int bgcolour, int fgcolour); -void ui_line(uint8 opcode, int startx, int starty, int endx, int endy, PEN * pen); -void ui_rect(int x, int y, int cx, int cy, int colour); -void ui_polygon(uint8 opcode, uint8 fillmode, RD_POINT * point, int npoints, BRUSH * brush, - int bgcolour, int fgcolour); -void ui_polyline(uint8 opcode, RD_POINT * points, int npoints, PEN * pen); -void ui_ellipse(uint8 opcode, uint8 fillmode, int x, int y, int cx, int cy, BRUSH * brush, - int bgcolour, int fgcolour); -void ui_draw_glyph(int mixmode, int x, int y, int cx, int cy, RD_HGLYPH glyph, int srcx, int srcy, - int bgcolour, int fgcolour); -void ui_draw_text(uint8 font, uint8 flags, uint8 opcode, int mixmode, int x, int y, int clipx, - int clipy, int clipcx, int clipcy, int boxx, int boxy, int boxcx, int boxcy, - BRUSH * brush, int bgcolour, int fgcolour, uint8 * text, uint8 length); -void ui_desktop_save(uint32 offset, int x, int y, int cx, int cy); -void ui_desktop_restore(uint32 offset, int x, int y, int cx, int cy); -void ui_begin_update(void); -void ui_end_update(void); -void ui_seamless_begin(BOOL hidden); -void ui_seamless_hide_desktop(void); -void ui_seamless_unhide_desktop(void); -void ui_seamless_toggle(void); -void ui_seamless_create_window(unsigned long id, unsigned long group, unsigned long parent, - unsigned long flags); -void ui_seamless_destroy_window(unsigned long id, unsigned long flags); -void ui_seamless_move_window(unsigned long id, int x, int y, int width, int height, - unsigned long flags); -void ui_seamless_restack_window(unsigned long id, unsigned long behind, unsigned long flags); -void ui_seamless_settitle(unsigned long id, const char *title, unsigned long flags); -void ui_seamless_setstate(unsigned long id, unsigned int state, unsigned long flags); -void ui_seamless_syncbegin(unsigned long flags); -void ui_seamless_ack(unsigned int serial); -void ui_logon(void); -/* lspci.c */ -BOOL lspci_init(void); -/* seamless.c */ -BOOL seamless_init(void); -unsigned int seamless_send_sync(void); -unsigned int seamless_send_state(unsigned long id, unsigned int state, unsigned long flags); -unsigned int seamless_send_position(unsigned long id, int x, int y, int width, int height, - unsigned long flags); -void seamless_select_timeout(struct timeval *tv); -unsigned int seamless_send_zchange(unsigned long id, unsigned long below, unsigned long flags); -unsigned int seamless_send_focus(unsigned long id, unsigned long flags); - -/* *INDENT-OFF* */ -#ifdef __cplusplus -} -#endif -/* *INDENT-ON* */ - -#endif diff --git a/uirdesktop/rdesktop.h b/uirdesktop/rdesktop.h deleted file mode 100644 index 0f39b6e3..00000000 --- a/uirdesktop/rdesktop.h +++ /dev/null @@ -1,126 +0,0 @@ -/* - rdesktop: A Remote Desktop Protocol client. - Master include file - Copyright (C) Matthew Chapman 1999-2005 - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -*/ - -#include -#include -#include -#ifdef _WIN32 -#include /* winsock2.h first */ -#include -#include -#else /* WIN32 */ -#include -#include -#include -#ifdef HAVE_SYS_SELECT_H -#include -#else /* HAVE_SYS_SELECT_H */ -#include -#include -#endif /* HAVE_SYS_SELECT_H */ -#endif /* WIN32 */ -#include /* PATH_MAX */ - -#define VERSION "1.4.1" - -#ifdef WITH_DEBUG -#define DEBUG(args) printf args; -#else -#define DEBUG(args) -#endif - -#ifdef WITH_DEBUG_KBD -#define DEBUG_KBD(args) printf args; -#else -#define DEBUG_KBD(args) -#endif - -#ifdef WITH_DEBUG_RDP5 -#define DEBUG_RDP5(args) printf args; -#else -#define DEBUG_RDP5(args) -#endif - -#ifdef WITH_DEBUG_CLIPBOARD -#define DEBUG_CLIPBOARD(args) printf args; -#else -#define DEBUG_CLIPBOARD(args) -#endif - -#ifdef WITH_DEBUG_CHANNEL -#define DEBUG_CHANNEL(args) printf args; -#else -#define DEBUG_CHANNEL(args) -#endif - -#define STRNCPY(dst,src,n) { strncpy(dst,src,n-1); dst[n-1] = 0; } - -#ifndef MIN -#define MIN(x,y) (((x) < (y)) ? (x) : (y)) -#endif - -#ifndef MAX -#define MAX(x,y) (((x) > (y)) ? (x) : (y)) -#endif - -/* timeval macros */ -#ifndef timerisset -#define timerisset(tvp)\ - ((tvp)->tv_sec || (tvp)->tv_usec) -#endif -#ifndef timercmp -#define timercmp(tvp, uvp, cmp)\ - ((tvp)->tv_sec cmp (uvp)->tv_sec ||\ - (tvp)->tv_sec == (uvp)->tv_sec &&\ - (tvp)->tv_usec cmp (uvp)->tv_usec) -#endif -#ifndef timerclear -#define timerclear(tvp)\ - ((tvp)->tv_sec = (tvp)->tv_usec = 0) -#endif - -/* If configure does not define the endianess, try - to find it out */ -#if !defined(L_ENDIAN) && !defined(B_ENDIAN) -#if __BYTE_ORDER == __LITTLE_ENDIAN -#define L_ENDIAN -#elif __BYTE_ORDER == __BIG_ENDIAN -#define B_ENDIAN -#else -#error Unknown endianness. Edit rdesktop.h. -#endif -#endif /* B_ENDIAN, L_ENDIAN from configure */ - -/* No need for alignment on x86 and amd64 */ -#if !defined(NEED_ALIGN) -#if !(defined(__x86__) || defined(__x86_64__) || \ - defined(__AMD64__) || defined(_M_IX86) || \ - defined(__i386__)) -#define NEED_ALIGN -#endif -#endif - -#include "parse.h" -#include "constants.h" -#include "types.h" - -#ifndef MAKE_PROTO -#include "proto.h" -#endif diff --git a/uirdesktop/rdp5.c b/uirdesktop/rdp5.c deleted file mode 100644 index 78d97168..00000000 --- a/uirdesktop/rdp5.c +++ /dev/null @@ -1,121 +0,0 @@ -/* -*- c-basic-offset: 8 -*- - rdesktop: A Remote Desktop Protocol client. - Protocol services - RDP5 short form PDU processing - Copyright (C) Matthew Chapman 1999-2005 - Copyright (C) Erik Forsberg 2003 - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -*/ - -#include "rdesktop.h" - -extern uint8 *g_next_packet; - -extern RDPCOMP g_mppc_dict; - -void -rdp5_process(STREAM s) -{ - uint16 length, count, x, y; - uint8 type, ctype; - uint8 *next; - - uint32 roff, rlen; - struct stream *ns = &(g_mppc_dict.ns); - struct stream *ts; - -#if 0 - printf("RDP5 data:\n"); - hexdump(s->p, s->end - s->p); -#endif - - ui_begin_update(); - while (s->p < s->end) - { - in_uint8(s, type); - if (type & RDP5_COMPRESSED) - { - in_uint8(s, ctype); - in_uint16_le(s, length); - type ^= RDP5_COMPRESSED; - } - else - { - ctype = 0; - in_uint16_le(s, length); - } - g_next_packet = next = s->p + length; - - if (ctype & RDP_MPPC_COMPRESSED) - { - if (mppc_expand(s->p, length, ctype, &roff, &rlen) == -1) - error("error while decompressing packet\n"); - - /* allocate memory and copy the uncompressed data into the temporary stream */ - ns->data = (uint8 *) xrealloc(ns->data, rlen); - - memcpy((ns->data), (unsigned char *) (g_mppc_dict.hist + roff), rlen); - - ns->size = rlen; - ns->end = (ns->data + ns->size); - ns->p = ns->data; - ns->rdp_hdr = ns->p; - - ts = ns; - } - else - ts = s; - - switch (type) - { - case 0: /* update orders */ - in_uint16_le(ts, count); - process_orders(ts, count); - break; - case 1: /* update bitmap */ - in_uint8s(ts, 2); /* part length */ - process_bitmap_updates(ts); - break; - case 2: /* update palette */ - in_uint8s(ts, 2); /* uint16 = 2 */ - process_palette(ts); - break; - case 3: /* update synchronize */ - break; - case 5: /* null pointer */ - ui_set_null_cursor(); - break; - case 6: /* default pointer */ - break; - case 8: /* pointer position */ - in_uint16_le(ts, x); - in_uint16_le(ts, y); - if (s_check(ts)) - ui_move_pointer(x, y); - break; - case 9: /* color pointer */ - process_colour_pointer_pdu(ts); - break; - case 10: /* cached pointer */ - process_cached_pointer_pdu(ts); - break; - default: - unimpl("RDP5 opcode %d\n", type); - } - - s->p = next; - } - ui_end_update(); -} diff --git a/uirdesktop/secure.c b/uirdesktop/secure.c deleted file mode 100644 index 2a8c9164..00000000 --- a/uirdesktop/secure.c +++ /dev/null @@ -1,1007 +0,0 @@ -/* -*- c-basic-offset: 8 -*- - rdesktop: A Remote Desktop Protocol client. - Protocol services - RDP encryption and licensing - Copyright (C) Matthew Chapman 1999-2005 - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -*/ - -#include "rdesktop.h" - -//#include -//#include -//#include -//#include -//#include - -void * -ssl_sha1_info_create(void); -void -ssl_sha1_info_delete(void * sha1_info); -void -ssl_sha1_clear(void * sha1_info); -void -ssl_sha1_transform(void * sha1_info, char * data, int len); -void -ssl_sha1_complete(void * sha1_info, char * data); -void * -ssl_md5_info_create(void); -void -ssl_md5_info_delete(void * md5_info); -void * -ssl_md5_info_create(void); -void -ssl_md5_info_delete(void * md5_info); -void -ssl_md5_clear(void * md5_info); -void -ssl_md5_transform(void * md5_info, char * data, int len); -void -ssl_md5_complete(void * md5_info, char * data); -void * -ssl_rc4_info_create(void); -void -ssl_rc4_info_delete(void * rc4_info); -void -ssl_rc4_set_key(void * rc4_info, char * key, int len); -void -ssl_rc4_crypt(void * rc4_info, char * in_data, char * out_data, int len); -int -ssl_mod_exp(char* out, int out_len, char* in, int in_len, - char* mod, int mod_len, char* exp, int exp_len); - -extern char g_hostname[16]; -extern int g_width; -extern int g_height; -extern unsigned int g_keylayout; -extern int g_keyboard_type; -extern int g_keyboard_subtype; -extern int g_keyboard_functionkeys; -extern BOOL g_encryption; -extern BOOL g_licence_issued; -extern BOOL g_use_rdp5; -extern BOOL g_console_session; -extern int g_server_depth; -extern uint16 mcs_userid; -extern VCHANNEL g_channels[]; -extern unsigned int g_num_channels; - -static int rc4_key_len; -static void * rc4_decrypt_key = 0; -static void * rc4_encrypt_key = 0; -//static RSA *server_public_key; -static void * server_public_key; -static uint32 server_public_key_len; - -static uint8 sec_sign_key[16]; -static uint8 sec_decrypt_key[16]; -static uint8 sec_encrypt_key[16]; -static uint8 sec_decrypt_update_key[16]; -static uint8 sec_encrypt_update_key[16]; -static uint8 sec_crypted_random[SEC_MAX_MODULUS_SIZE]; - -uint16 g_server_rdp_version = 0; - -/* These values must be available to reset state - Session Directory */ -static int sec_encrypt_use_count = 0; -static int sec_decrypt_use_count = 0; - -/* - * I believe this is based on SSLv3 with the following differences: - * MAC algorithm (5.2.3.1) uses only 32-bit length in place of seq_num/type/length fields - * MAC algorithm uses SHA1 and MD5 for the two hash functions instead of one or other - * key_block algorithm (6.2.2) uses 'X', 'YY', 'ZZZ' instead of 'A', 'BB', 'CCC' - * key_block partitioning is different (16 bytes each: MAC secret, decrypt key, encrypt key) - * encryption/decryption keys updated every 4096 packets - * See http://wp.netscape.com/eng/ssl3/draft302.txt - */ - -/* - * 48-byte transformation used to generate master secret (6.1) and key material (6.2.2). - * Both SHA1 and MD5 algorithms are used. - */ -void -sec_hash_48(uint8 * out, uint8 * in, uint8 * salt1, uint8 * salt2, uint8 salt) -{ - uint8 shasig[20]; - uint8 pad[4]; - void * sha; - void * md5; - int i; - - for (i = 0; i < 3; i++) - { - memset(pad, salt + i, i + 1); - sha = ssl_sha1_info_create(); - ssl_sha1_clear(sha); - ssl_sha1_transform(sha, pad, i + 1); - ssl_sha1_transform(sha, in, 48); - ssl_sha1_transform(sha, salt1, 32); - ssl_sha1_transform(sha, salt2, 32); - ssl_sha1_complete(sha, shasig); - ssl_sha1_info_delete(sha); - md5 = ssl_md5_info_create(); - ssl_md5_clear(md5); - ssl_md5_transform(md5, in, 48); - ssl_md5_transform(md5, shasig, 20); - ssl_md5_complete(md5, out + i * 16); - ssl_md5_info_delete(md5); - } -} - -/* - * 16-byte transformation used to generate export keys (6.2.2). - */ -void -sec_hash_16(uint8 * out, uint8 * in, uint8 * salt1, uint8 * salt2) -{ - void * md5; - - md5 = ssl_md5_info_create(); - ssl_md5_clear(md5); - ssl_md5_transform(md5, in, 16); - ssl_md5_transform(md5, salt1, 32); - ssl_md5_transform(md5, salt2, 32); - ssl_md5_complete(md5, out); - ssl_md5_info_delete(md5); -} - -/* Reduce key entropy from 64 to 40 bits */ -static void -sec_make_40bit(uint8 * key) -{ - key[0] = 0xd1; - key[1] = 0x26; - key[2] = 0x9e; -} - -/* Generate encryption keys given client and server randoms */ -static void -sec_generate_keys(uint8 * client_random, uint8 * server_random, int rc4_key_size) -{ - uint8 pre_master_secret[48]; - uint8 master_secret[48]; - uint8 key_block[48]; - - /* Construct pre-master secret */ - memcpy(pre_master_secret, client_random, 24); - memcpy(pre_master_secret + 24, server_random, 24); - - /* Generate master secret and then key material */ - sec_hash_48(master_secret, pre_master_secret, client_random, server_random, 'A'); - sec_hash_48(key_block, master_secret, client_random, server_random, 'X'); - - /* First 16 bytes of key material is MAC secret */ - memcpy(sec_sign_key, key_block, 16); - - /* Generate export keys from next two blocks of 16 bytes */ - sec_hash_16(sec_decrypt_key, &key_block[16], client_random, server_random); - sec_hash_16(sec_encrypt_key, &key_block[32], client_random, server_random); - - if (rc4_key_size == 1) - { - DEBUG(("40-bit encryption enabled\n")); - sec_make_40bit(sec_sign_key); - sec_make_40bit(sec_decrypt_key); - sec_make_40bit(sec_encrypt_key); - rc4_key_len = 8; - } - else - { - DEBUG(("rc_4_key_size == %d, 128-bit encryption enabled\n", rc4_key_size)); - rc4_key_len = 16; - } - - /* Save initial RC4 keys as update keys */ - memcpy(sec_decrypt_update_key, sec_decrypt_key, 16); - memcpy(sec_encrypt_update_key, sec_encrypt_key, 16); - - /* Initialise RC4 state arrays */ - - ssl_rc4_info_delete(rc4_decrypt_key); - rc4_decrypt_key = ssl_rc4_info_create(); - ssl_rc4_set_key(rc4_decrypt_key, sec_decrypt_key, rc4_key_len); - - ssl_rc4_info_delete(rc4_encrypt_key); - rc4_encrypt_key = ssl_rc4_info_create(); - ssl_rc4_set_key(rc4_encrypt_key, sec_encrypt_key, rc4_key_len); -} - -static uint8 pad_54[40] = { - 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, - 54, 54, 54, - 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, 54, - 54, 54, 54 -}; - -static uint8 pad_92[48] = { - 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, - 92, 92, 92, 92, 92, 92, 92, - 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, 92, - 92, 92, 92, 92, 92, 92, 92 -}; - -/* Output a uint32 into a buffer (little-endian) */ -void -buf_out_uint32(uint8 * buffer, uint32 value) -{ - buffer[0] = (value) & 0xff; - buffer[1] = (value >> 8) & 0xff; - buffer[2] = (value >> 16) & 0xff; - buffer[3] = (value >> 24) & 0xff; -} - -/* Generate a MAC hash (5.2.3.1), using a combination of SHA1 and MD5 */ -void -sec_sign(uint8 * signature, int siglen, uint8 * session_key, int keylen, uint8 * data, int datalen) -{ - uint8 shasig[20]; - uint8 md5sig[16]; - uint8 lenhdr[4]; - void * sha; - void * md5; - - buf_out_uint32(lenhdr, datalen); - - sha = ssl_sha1_info_create(); - ssl_sha1_clear(sha); - ssl_sha1_transform(sha, session_key, keylen); - ssl_sha1_transform(sha, pad_54, 40); - ssl_sha1_transform(sha, lenhdr, 4); - ssl_sha1_transform(sha, data, datalen); - ssl_sha1_complete(sha, shasig); - ssl_sha1_info_delete(sha); - - md5 = ssl_md5_info_create(); - ssl_md5_clear(md5); - ssl_md5_transform(md5, session_key, keylen); - ssl_md5_transform(md5, pad_92, 48); - ssl_md5_transform(md5, shasig, 20); - ssl_md5_complete(md5, md5sig); - ssl_md5_info_delete(md5); - - memcpy(signature, md5sig, siglen); -} - -/* Update an encryption key */ -static void -sec_update(uint8 * key, uint8 * update_key) -{ - uint8 shasig[20]; - void * sha; - void * md5; - void * update; - - sha = ssl_sha1_info_create(); - ssl_sha1_clear(sha); - ssl_sha1_transform(sha, update_key, rc4_key_len); - ssl_sha1_transform(sha, pad_54, 40); - ssl_sha1_transform(sha, key, rc4_key_len); - ssl_sha1_complete(sha, shasig); - ssl_sha1_info_delete(sha); - - md5 = ssl_md5_info_create(); - ssl_md5_clear(md5); - ssl_md5_transform(md5, update_key, rc4_key_len); - ssl_md5_transform(md5, pad_92, 48); - ssl_md5_transform(md5, shasig, 20); - ssl_md5_complete(md5, key); - ssl_md5_info_delete(md5); - - - update = ssl_rc4_info_create(); - ssl_rc4_set_key(update, key, rc4_key_len); - ssl_rc4_crypt(update, key, key, rc4_key_len); - ssl_rc4_info_delete(update); - - if (rc4_key_len == 8) - sec_make_40bit(key); -} - -/* Encrypt data using RC4 */ -static void -sec_encrypt(uint8 * data, int length) -{ - if (sec_encrypt_use_count == 4096) - { - sec_update(sec_encrypt_key, sec_encrypt_update_key); - ssl_rc4_set_key(rc4_encrypt_key, sec_encrypt_key, rc4_key_len); - sec_encrypt_use_count = 0; - } - ssl_rc4_crypt(rc4_encrypt_key, data, data, length); - sec_encrypt_use_count++; -} - -/* Decrypt data using RC4 */ -void -sec_decrypt(uint8 * data, int length) -{ - if (sec_decrypt_use_count == 4096) - { - sec_update(sec_decrypt_key, sec_decrypt_update_key); - ssl_rc4_set_key(rc4_decrypt_key, sec_decrypt_key, rc4_key_len); - sec_decrypt_use_count = 0; - } - ssl_rc4_crypt(rc4_decrypt_key, data, data, length); - sec_decrypt_use_count++; -} - -/*static void -reverse(uint8 * p, int len) -{ - int i, j; - uint8 temp; - - for (i = 0, j = len - 1; i < j; i++, j--) - { - temp = p[i]; - p[i] = p[j]; - p[j] = temp; - } -}*/ - -/* Perform an RSA public key encryption operation */ -static void -sec_rsa_encrypt(uint8 * out, uint8 * in, int len, uint32 modulus_size, uint8 * modulus, - uint8 * exponent) -{ - ssl_mod_exp(out, 64, in, 32, modulus, modulus_size, exponent, 4); -/* - BN_CTX *ctx; - BIGNUM mod, exp, x, y; - uint8 inr[SEC_MODULUS_SIZE]; - int outlen; - - reverse(modulus, SEC_MODULUS_SIZE); - reverse(exponent, SEC_EXPONENT_SIZE); - memcpy(inr, in, len); - reverse(inr, len); - - ctx = BN_CTX_new(); - BN_init(&mod); - BN_init(&exp); - BN_init(&x); - BN_init(&y); - - BN_bin2bn(modulus, SEC_MODULUS_SIZE, &mod); - BN_bin2bn(exponent, SEC_EXPONENT_SIZE, &exp); - BN_bin2bn(inr, len, &x); - BN_mod_exp(&y, &x, &exp, &mod, ctx); - outlen = BN_bn2bin(&y, out); - reverse(out, outlen); - if (outlen < SEC_MODULUS_SIZE) - memset(out + outlen, 0, SEC_MODULUS_SIZE - outlen); - - BN_free(&y); - BN_clear_free(&x); - BN_free(&exp); - BN_free(&mod); - BN_CTX_free(ctx);*/ -} - -/* Initialise secure transport packet */ -STREAM -sec_init(uint32 flags, int maxlen) -{ - int hdrlen; - STREAM s; - - if (!g_licence_issued) - hdrlen = (flags & SEC_ENCRYPT) ? 12 : 4; - else - hdrlen = (flags & SEC_ENCRYPT) ? 12 : 0; - s = mcs_init(maxlen + hdrlen); - s_push_layer(s, sec_hdr, hdrlen); - - return s; -} - -/* Transmit secure transport packet over specified channel */ -void -sec_send_to_channel(STREAM s, uint32 flags, uint16 channel) -{ - int datalen; - - s_pop_layer(s, sec_hdr); - if (!g_licence_issued || (flags & SEC_ENCRYPT)) - out_uint32_le(s, flags); - - if (flags & SEC_ENCRYPT) - { - flags &= ~SEC_ENCRYPT; - datalen = s->end - s->p - 8; - -#ifdef WITH_DEBUG - DEBUG(("Sending encrypted packet:\n")); - hexdump(s->p + 8, datalen); -#endif - - sec_sign(s->p, 8, sec_sign_key, rc4_key_len, s->p + 8, datalen); - sec_encrypt(s->p + 8, datalen); - } - - mcs_send_to_channel(s, channel); -} - -/* Transmit secure transport packet */ - -void -sec_send(STREAM s, uint32 flags) -{ - sec_send_to_channel(s, flags, MCS_GLOBAL_CHANNEL); -} - - -/* Transfer the client random to the server */ -static void -sec_establish_key(void) -{ - uint32 length = server_public_key_len + SEC_PADDING_SIZE; - uint32 flags = SEC_CLIENT_RANDOM; - STREAM s; - - s = sec_init(flags, length+4); - - out_uint32_le(s, length); - out_uint8p(s, sec_crypted_random, server_public_key_len); - out_uint8s(s, SEC_PADDING_SIZE); - - s_mark_end(s); - sec_send(s, flags); -} - -/* Output connect initial data blob */ -static void -sec_out_mcs_data(STREAM s) -{ - int hostlen = 2 * strlen(g_hostname); - int length = 158 + 76 + 12 + 4; - unsigned int i; - - if (g_num_channels > 0) - length += g_num_channels * 12 + 8; - - if (hostlen > 30) - hostlen = 30; - - /* Generic Conference Control (T.124) ConferenceCreateRequest */ - out_uint16_be(s, 5); - out_uint16_be(s, 0x14); - out_uint8(s, 0x7c); - out_uint16_be(s, 1); - - out_uint16_be(s, (length | 0x8000)); /* remaining length */ - - out_uint16_be(s, 8); /* length? */ - out_uint16_be(s, 16); - out_uint8(s, 0); - out_uint16_le(s, 0xc001); - out_uint8(s, 0); - - out_uint32_le(s, 0x61637544); /* OEM ID: "Duca", as in Ducati. */ - out_uint16_be(s, ((length - 14) | 0x8000)); /* remaining length */ - - /* Client information */ - out_uint16_le(s, SEC_TAG_CLI_INFO); - out_uint16_le(s, 212); /* length */ - out_uint16_le(s, g_use_rdp5 ? 4 : 1); /* RDP version. 1 == RDP4, 4 == RDP5. */ - out_uint16_le(s, 8); - out_uint16_le(s, g_width); - out_uint16_le(s, g_height); - out_uint16_le(s, 0xca01); - out_uint16_le(s, 0xaa03); - out_uint32_le(s, g_keylayout); - out_uint32_le(s, 2600); /* Client build. We are now 2600 compatible :-) */ - - /* Unicode name of client, padded to 32 bytes */ - rdp_out_unistr(s, g_hostname, hostlen); - out_uint8s(s, 30 - hostlen); - - /* See - http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wceddk40/html/cxtsksupportingremotedesktopprotocol.asp */ - out_uint32_le(s, g_keyboard_type); - out_uint32_le(s, g_keyboard_subtype); - out_uint32_le(s, g_keyboard_functionkeys); - out_uint8s(s, 64); /* reserved? 4 + 12 doublewords */ - out_uint16_le(s, 0xca01); /* colour depth? */ - out_uint16_le(s, 1); - - out_uint32(s, 0); - out_uint8(s, g_server_depth); - out_uint16_le(s, 0x0700); - out_uint8(s, 0); - out_uint32_le(s, 1); - out_uint8s(s, 64); /* End of client info */ - - out_uint16_le(s, SEC_TAG_CLI_4); - out_uint16_le(s, 12); - out_uint32_le(s, g_console_session ? 0xb : 9); - out_uint32(s, 0); - - /* Client encryption settings */ - out_uint16_le(s, SEC_TAG_CLI_CRYPT); - out_uint16_le(s, 12); /* length */ - out_uint32_le(s, g_encryption ? 0x3 : 0); /* encryption supported, 128-bit supported */ - out_uint32(s, 0); /* Unknown */ - - DEBUG_RDP5(("g_num_channels is %d\n", g_num_channels)); - if (g_num_channels > 0) - { - out_uint16_le(s, SEC_TAG_CLI_CHANNELS); - out_uint16_le(s, g_num_channels * 12 + 8); /* length */ - out_uint32_le(s, g_num_channels); /* number of virtual channels */ - for (i = 0; i < g_num_channels; i++) - { - DEBUG_RDP5(("Requesting channel %s\n", g_channels[i].name)); - out_uint8a(s, g_channels[i].name, 8); - out_uint32_be(s, g_channels[i].flags); - } - } - - s_mark_end(s); -} - -/* Parse a public key structure */ -static BOOL -sec_parse_public_key(STREAM s, uint8 ** modulus, uint8 ** exponent) -{ - uint32 magic, modulus_len; - - in_uint32_le(s, magic); - if (magic != SEC_RSA_MAGIC) - { - error("RSA magic 0x%x\n", magic); - return False; - } - - in_uint32_le(s, modulus_len); - modulus_len -= SEC_PADDING_SIZE; - if ((modulus_len < 64) || (modulus_len > SEC_MAX_MODULUS_SIZE)) - { - error("Bad server public key size (%u bits)\n", modulus_len*8); - return False; - } - - in_uint8s(s, 8); /* modulus_bits, unknown */ - in_uint8p(s, *exponent, SEC_EXPONENT_SIZE); - in_uint8p(s, *modulus, modulus_len); - in_uint8s(s, SEC_PADDING_SIZE); - server_public_key_len = modulus_len; - - return s_check(s); -} - -/* Parse a crypto information structure */ -static BOOL -sec_parse_crypt_info(STREAM s, uint32 * rc4_key_size, - uint8 ** server_random, uint8 ** modulus, uint8 ** exponent) -{ - uint32 crypt_level, random_len, rsa_info_len; - uint32 /*cacert_len, cert_len,*/ flags; - //X509 *cacert, *server_cert; - uint16 tag, length; - uint8 *next_tag, *end; - - in_uint32_le(s, *rc4_key_size); /* 1 = 40-bit, 2 = 128-bit */ - in_uint32_le(s, crypt_level); /* 1 = low, 2 = medium, 3 = high */ - if (crypt_level == 0) /* no encryption */ - return False; - in_uint32_le(s, random_len); - in_uint32_le(s, rsa_info_len); - - if (random_len != SEC_RANDOM_SIZE) - { - error("random len %d, expected %d\n", random_len, SEC_RANDOM_SIZE); - return False; - } - - in_uint8p(s, *server_random, random_len); - - /* RSA info */ - end = s->p + rsa_info_len; - if (end > s->end) - return False; - - in_uint32_le(s, flags); /* 1 = RDP4-style, 0x80000002 = X.509 */ - if (flags & 1) - { - DEBUG_RDP5(("We're going for the RDP4-style encryption\n")); - in_uint8s(s, 8); /* unknown */ - - while (s->p < end) - { - in_uint16_le(s, tag); - in_uint16_le(s, length); - - next_tag = s->p + length; - - switch (tag) - { - case SEC_TAG_PUBKEY: - if (!sec_parse_public_key(s, modulus, exponent)) - return False; - DEBUG_RDP5(("Got Public key, RDP4-style\n")); - - break; - - case SEC_TAG_KEYSIG: - /* Is this a Microsoft key that we just got? */ - /* Care factor: zero! */ - /* Actually, it would probably be a good idea to check if the public key is signed with this key, and then store this - key as a known key of the hostname. This would prevent some MITM-attacks. */ - break; - - default: - unimpl("crypt tag 0x%x\n", tag); - } - - s->p = next_tag; - } - } - else - { -#if 0 - uint32 certcount; - - DEBUG_RDP5(("We're going for the RDP5-style encryption\n")); - in_uint32_le(s, certcount); /* Number of certificates */ - - if (certcount < 2) - { - error("Server didn't send enough X509 certificates\n"); - return False; - } - - for (; certcount > 2; certcount--) - { /* ignore all the certificates between the root and the signing CA */ - uint32 ignorelen; - X509 *ignorecert; - - DEBUG_RDP5(("Ignored certs left: %d\n", certcount)); - - in_uint32_le(s, ignorelen); - DEBUG_RDP5(("Ignored Certificate length is %d\n", ignorelen)); - ignorecert = d2i_X509(NULL, &(s->p), ignorelen); - - if (ignorecert == NULL) - { /* XXX: error out? */ - DEBUG_RDP5(("got a bad cert: this will probably screw up the rest of the communication\n")); - } - -#ifdef WITH_DEBUG_RDP5 - DEBUG_RDP5(("cert #%d (ignored):\n", certcount)); - X509_print_fp(stdout, ignorecert); -#endif - } - - /* Do da funky X.509 stuffy - - "How did I find out about this? I looked up and saw a - bright light and when I came to I had a scar on my forehead - and knew about X.500" - - Peter Gutman in a early version of - http://www.cs.auckland.ac.nz/~pgut001/pubs/x509guide.txt - */ - - in_uint32_le(s, cacert_len); - DEBUG_RDP5(("CA Certificate length is %d\n", cacert_len)); - cacert = d2i_X509(NULL, &(s->p), cacert_len); - /* Note: We don't need to move s->p here - d2i_X509 is - "kind" enough to do it for us */ - if (NULL == cacert) - { - error("Couldn't load CA Certificate from server\n"); - return False; - } - - /* Currently, we don't use the CA Certificate. - FIXME: - *) Verify the server certificate (server_cert) with the - CA certificate. - *) Store the CA Certificate with the hostname of the - server we are connecting to as key, and compare it - when we connect the next time, in order to prevent - MITM-attacks. - */ - - X509_free(cacert); - - in_uint32_le(s, cert_len); - DEBUG_RDP5(("Certificate length is %d\n", cert_len)); - server_cert = d2i_X509(NULL, &(s->p), cert_len); - if (NULL == server_cert) - { - error("Couldn't load Certificate from server\n"); - return False; - } - - in_uint8s(s, 16); /* Padding */ - - /* Note: Verifying the server certificate must be done here, - before sec_parse_public_key since we'll have to apply - serious violence to the key after this */ - - if (!sec_parse_x509_key(server_cert)) - { - DEBUG_RDP5(("Didn't parse X509 correctly\n")); - X509_free(server_cert); - return False; - } - X509_free(server_cert); - return True; /* There's some garbage here we don't care about */ -#endif - } - return s_check_end(s); -} - -/* Process crypto information blob */ -static void -sec_process_crypt_info(STREAM s) -{ - uint8 *server_random, *modulus, *exponent; - uint8 client_random[SEC_RANDOM_SIZE]; - uint32 rc4_key_size; - uint8 inr[SEC_MODULUS_SIZE]; - - if (!sec_parse_crypt_info(s, &rc4_key_size, &server_random, &modulus, &exponent)) - { - DEBUG(("Failed to parse crypt info\n")); - return; - } - - DEBUG(("Generating client random\n")); - /* Generate a client random, and hence determine encryption keys */ - /* This is what the MS client do: */ - memset(inr, 0, SEC_RANDOM_SIZE); - /* *ARIGL!* Plaintext attack, anyone? - I tried doing: - generate_random(inr); - ..but that generates connection errors now and then (yes, - "now and then". Something like 0 to 3 attempts needed before a - successful connection. Nice. Not! - */ - - generate_random(client_random); - if (NULL != server_public_key) - { /* Which means we should use - RDP5-style encryption */ -#if 0 - memcpy(inr + SEC_RANDOM_SIZE, client_random, SEC_RANDOM_SIZE); - reverse(inr + SEC_RANDOM_SIZE, SEC_RANDOM_SIZE); - - RSA_public_encrypt(SEC_MODULUS_SIZE, - inr, sec_crypted_random, server_public_key, RSA_NO_PADDING); - - reverse(sec_crypted_random, SEC_MODULUS_SIZE); - - RSA_free(server_public_key); - server_public_key = NULL; -#endif - } - else - { /* RDP4-style encryption */ - sec_rsa_encrypt(sec_crypted_random, - client_random, SEC_RANDOM_SIZE, server_public_key_len, modulus, exponent); - } - sec_generate_keys(client_random, server_random, rc4_key_size); -} - - -/* Process SRV_INFO, find RDP version supported by server */ -static void -sec_process_srv_info(STREAM s) -{ - in_uint16_le(s, g_server_rdp_version); - DEBUG_RDP5(("Server RDP version is %d\n", g_server_rdp_version)); - if (1 == g_server_rdp_version) - { - g_use_rdp5 = 0; - g_server_depth = 8; - } -} - - -/* Process connect response data blob */ -void -sec_process_mcs_data(STREAM s) -{ - uint16 tag, length; - uint8 *next_tag; - uint8 len; - - in_uint8s(s, 21); /* header (T.124 ConferenceCreateResponse) */ - in_uint8(s, len); - if (len & 0x80) - in_uint8(s, len); - - while (s->p < s->end) - { - in_uint16_le(s, tag); - in_uint16_le(s, length); - - if (length <= 4) - return; - - next_tag = s->p + length - 4; - - switch (tag) - { - case SEC_TAG_SRV_INFO: - sec_process_srv_info(s); - break; - - case SEC_TAG_SRV_CRYPT: - sec_process_crypt_info(s); - break; - - case SEC_TAG_SRV_CHANNELS: - /* FIXME: We should parse this information and - use it to map RDP5 channels to MCS - channels */ - break; - - default: - unimpl("response tag 0x%x\n", tag); - } - - s->p = next_tag; - } -} - -/* Receive secure transport packet */ -STREAM -sec_recv(uint8 * rdpver) -{ - uint32 sec_flags; - uint16 channel; - STREAM s; - - while ((s = mcs_recv(&channel, rdpver)) != NULL) - { - if (rdpver != NULL) - { - if (*rdpver != 3) - { - if (*rdpver & 0x80) - { - in_uint8s(s, 8); /* signature */ - sec_decrypt(s->p, s->end - s->p); - } - return s; - } - } - if (g_encryption || !g_licence_issued) - { - in_uint32_le(s, sec_flags); - - if (sec_flags & SEC_ENCRYPT) - { - in_uint8s(s, 8); /* signature */ - sec_decrypt(s->p, s->end - s->p); - } - - if (sec_flags & SEC_LICENCE_NEG) - { - licence_process(s); - continue; - } - - if (sec_flags & 0x0400) /* SEC_REDIRECT_ENCRYPT */ - { - uint8 swapbyte; - - in_uint8s(s, 8); /* signature */ - sec_decrypt(s->p, s->end - s->p); - - /* Check for a redirect packet, starts with 00 04 */ - if (s->p[0] == 0 && s->p[1] == 4) - { - /* for some reason the PDU and the length seem to be swapped. - This isn't good, but we're going to do a byte for byte - swap. So the first foure value appear as: 00 04 XX YY, - where XX YY is the little endian length. We're going to - use 04 00 as the PDU type, so after our swap this will look - like: XX YY 04 00 */ - swapbyte = s->p[0]; - s->p[0] = s->p[2]; - s->p[2] = swapbyte; - - swapbyte = s->p[1]; - s->p[1] = s->p[3]; - s->p[3] = swapbyte; - - swapbyte = s->p[2]; - s->p[2] = s->p[3]; - s->p[3] = swapbyte; - } -#ifdef WITH_DEBUG - /* warning! this debug statement will show passwords in the clear! */ - hexdump(s->p, s->end - s->p); -#endif - } - - } - - if (channel != MCS_GLOBAL_CHANNEL) - { - channel_process(s, channel); - *rdpver = 0xff; - return s; - } - - return s; - } - - return NULL; -} - -/* Establish a secure connection */ -BOOL -sec_connect(char *server, char *username) -{ - struct stream mcs_data; - - /* We exchange some RDP data during the MCS-Connect */ - mcs_data.size = 512; - mcs_data.p = mcs_data.data = (uint8 *) xmalloc(mcs_data.size); - sec_out_mcs_data(&mcs_data); - - if (!mcs_connect(server, &mcs_data, username)) - return False; - - /* sec_process_mcs_data(&mcs_data); */ - if (g_encryption) - sec_establish_key(); - xfree(mcs_data.data); - return True; -} - -/* Establish a secure connection */ -BOOL -sec_reconnect(char *server) -{ - struct stream mcs_data; - - /* We exchange some RDP data during the MCS-Connect */ - mcs_data.size = 512; - mcs_data.p = mcs_data.data = (uint8 *) xmalloc(mcs_data.size); - sec_out_mcs_data(&mcs_data); - - if (!mcs_reconnect(server, &mcs_data)) - return False; - - /* sec_process_mcs_data(&mcs_data); */ - if (g_encryption) - sec_establish_key(); - xfree(mcs_data.data); - return True; -} - -/* Disconnect a connection */ -void -sec_disconnect(void) -{ - mcs_disconnect(); -} - -/* reset the state of the sec layer */ -void -sec_reset_state(void) -{ - g_server_rdp_version = 0; - sec_encrypt_use_count = 0; - sec_decrypt_use_count = 0; - mcs_reset_state(); -} diff --git a/uirdesktop/ssl_calls.c b/uirdesktop/ssl_calls.c deleted file mode 100755 index a7439259..00000000 --- a/uirdesktop/ssl_calls.c +++ /dev/null @@ -1,1645 +0,0 @@ -/* - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - xrdp: A Remote Desktop Protocol server. - Copyright (C) Jay Sorg 2004-2005 - - ssl calls - -*/ - -#include "rdesktop.h" - -#define APP_CC - -/*****************************************************************************/ -static void * g_malloc(int size, int zero) -{ - void * p; - - p = xmalloc(size); - if (zero) - { - memset(p, 0, size); - } - return p; -} - -/*****************************************************************************/ -static void g_free(void * in) -{ - xfree(in); -} - -/*****************************************************************************/ -/*****************************************************************************/ -/* rc4 stuff */ -/* An implementation of the ARC4 algorithm - * - * Copyright (C) 2001-2003 Christophe Devine - */ -struct rc4_state -{ - int x; - int y; - int m[256]; -}; - -/*****************************************************************************/ -void* APP_CC -ssl_rc4_info_create(void) -{ - return g_malloc(sizeof(struct rc4_state), 1);; -} - -/*****************************************************************************/ -void APP_CC -ssl_rc4_info_delete(void* rc4_info) -{ - g_free(rc4_info); -} - -/*****************************************************************************/ -void APP_CC -ssl_rc4_set_key(void* rc4_info, char* key, int len) -{ - int i; - int j; - int k; - int a; - int* m; - struct rc4_state* s; - - s = (struct rc4_state*)rc4_info; - s->x = 0; - s->y = 0; - m = s->m; - for (i = 0; i < 256; i++) - { - m[i] = i; - } - j = 0; - k = 0; - for (i = 0; i < 256; i++) - { - a = m[i]; - j = (unsigned char)(j + a + key[k]); - m[i] = m[j]; - m[j] = a; - k++; - if (k >= len) - { - k = 0; - } - } -} - -/*****************************************************************************/ -void APP_CC -ssl_rc4_crypt(void* rc4_info, char* in_data, char* out_data, int len) -{ - int i; - int x; - int y; - int a; - int b; - int* m; - struct rc4_state* s; - - s = (struct rc4_state*)rc4_info; - x = s->x; - y = s->y; - m = s->m; - for (i = 0; i < len; i++) - { - x = (unsigned char)(x + 1); - a = m[x]; - y = (unsigned char)(y + a); - b = m[y]; - m[x] = b; - m[y] = a; - out_data[i] = in_data[i] ^ (m[(unsigned char)(a + b)]); - } - s->x = x; - s->y = y; -} - -/*****************************************************************************/ -/*****************************************************************************/ -/* sha1 stuff */ -/* FIPS-180-1 compliant SHA-1 implementation - * - * Copyright (C) 2001-2003 Christophe Devine - */ -struct sha1_context -{ - int total[2]; - int state[5]; - char buffer[64]; -}; - -/*****************************************************************************/ -void* APP_CC -ssl_sha1_info_create(void) -{ - return g_malloc(sizeof(struct sha1_context), 1); -} - -/*****************************************************************************/ -void APP_CC -ssl_sha1_info_delete(void* sha1_info) -{ - g_free(sha1_info); -} - -/*****************************************************************************/ -void APP_CC -ssl_sha1_clear(void* sha1_info) -{ - struct sha1_context* ctx; - - ctx = (struct sha1_context*)sha1_info; - memset(ctx, 0, sizeof(struct sha1_context)); - ctx->state[0] = 0x67452301; - ctx->state[1] = 0xEFCDAB89; - ctx->state[2] = 0x98BADCFE; - ctx->state[3] = 0x10325476; - ctx->state[4] = 0xC3D2E1F0; -} - -#undef GET_UINT32 -#define GET_UINT32(n, b, i) \ -{ \ - (n) = ((b)[(i) + 0] << 24) | \ - ((b)[(i) + 1] << 16) | \ - ((b)[(i) + 2] << 8) | \ - ((b)[(i) + 3] << 0); \ -} - -#undef PUT_UINT32 -#define PUT_UINT32(n, b, i) \ -{ \ - (b)[(i) + 0] = ((n) >> 24); \ - (b)[(i) + 1] = ((n) >> 16); \ - (b)[(i) + 2] = ((n) >> 8); \ - (b)[(i) + 3] = ((n) >> 0); \ -} - -/*****************************************************************************/ -static void APP_CC -sha1_process(struct sha1_context* ctx, char* in_data) -{ - int temp; - int W[16]; - int A; - int B; - int C; - int D; - int E; - unsigned char* data; - - data = (unsigned char*)in_data; - - GET_UINT32(W[0], data, 0); - GET_UINT32(W[1], data, 4); - GET_UINT32(W[2], data, 8); - GET_UINT32(W[3], data, 12); - GET_UINT32(W[4], data, 16); - GET_UINT32(W[5], data, 20); - GET_UINT32(W[6], data, 24); - GET_UINT32(W[7], data, 28); - GET_UINT32(W[8], data, 32); - GET_UINT32(W[9], data, 36); - GET_UINT32(W[10], data, 40); - GET_UINT32(W[11], data, 44); - GET_UINT32(W[12], data, 48); - GET_UINT32(W[13], data, 52); - GET_UINT32(W[14], data, 56); - GET_UINT32(W[15], data, 60); - -#define S(x, n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n))) - -#define R(t) \ -( \ - temp = W[(t - 3) & 0x0F] ^ \ - W[(t - 8) & 0x0F] ^ \ - W[(t - 14) & 0x0F] ^ \ - W[(t - 0) & 0x0F], \ - (W[t & 0x0F] = S(temp, 1)) \ -) - -#undef P -#define P(a, b, c, d, e, x) \ -{ \ - e += S(a, 5) + F(b, c, d) + K + x; \ - b = S(b, 30); \ -} - - A = ctx->state[0]; - B = ctx->state[1]; - C = ctx->state[2]; - D = ctx->state[3]; - E = ctx->state[4]; - -#define F(x, y, z) (z ^ (x & (y ^ z))) -#define K 0x5A827999 - - P(A, B, C, D, E, W[0]); - P(E, A, B, C, D, W[1]); - P(D, E, A, B, C, W[2]); - P(C, D, E, A, B, W[3]); - P(B, C, D, E, A, W[4]); - P(A, B, C, D, E, W[5]); - P(E, A, B, C, D, W[6]); - P(D, E, A, B, C, W[7]); - P(C, D, E, A, B, W[8]); - P(B, C, D, E, A, W[9]); - P(A, B, C, D, E, W[10]); - P(E, A, B, C, D, W[11]); - P(D, E, A, B, C, W[12]); - P(C, D, E, A, B, W[13]); - P(B, C, D, E, A, W[14]); - P(A, B, C, D, E, W[15]); - P(E, A, B, C, D, R(16)); - P(D, E, A, B, C, R(17)); - P(C, D, E, A, B, R(18)); - P(B, C, D, E, A, R(19)); - -#undef K -#undef F - -#define F(x, y, z) (x ^ y ^ z) -#define K 0x6ED9EBA1 - - P(A, B, C, D, E, R(20)); - P(E, A, B, C, D, R(21)); - P(D, E, A, B, C, R(22)); - P(C, D, E, A, B, R(23)); - P(B, C, D, E, A, R(24)); - P(A, B, C, D, E, R(25)); - P(E, A, B, C, D, R(26)); - P(D, E, A, B, C, R(27)); - P(C, D, E, A, B, R(28)); - P(B, C, D, E, A, R(29)); - P(A, B, C, D, E, R(30)); - P(E, A, B, C, D, R(31)); - P(D, E, A, B, C, R(32)); - P(C, D, E, A, B, R(33)); - P(B, C, D, E, A, R(34)); - P(A, B, C, D, E, R(35)); - P(E, A, B, C, D, R(36)); - P(D, E, A, B, C, R(37)); - P(C, D, E, A, B, R(38)); - P(B, C, D, E, A, R(39)); - -#undef K -#undef F - -#define F(x, y, z) ((x & y) | (z & (x | y))) -#define K 0x8F1BBCDC - - P(A, B, C, D, E, R(40)); - P(E, A, B, C, D, R(41)); - P(D, E, A, B, C, R(42)); - P(C, D, E, A, B, R(43)); - P(B, C, D, E, A, R(44)); - P(A, B, C, D, E, R(45)); - P(E, A, B, C, D, R(46)); - P(D, E, A, B, C, R(47)); - P(C, D, E, A, B, R(48)); - P(B, C, D, E, A, R(49)); - P(A, B, C, D, E, R(50)); - P(E, A, B, C, D, R(51)); - P(D, E, A, B, C, R(52)); - P(C, D, E, A, B, R(53)); - P(B, C, D, E, A, R(54)); - P(A, B, C, D, E, R(55)); - P(E, A, B, C, D, R(56)); - P(D, E, A, B, C, R(57)); - P(C, D, E, A, B, R(58)); - P(B, C, D, E, A, R(59)); - -#undef K -#undef F - -#define F(x, y, z) (x ^ y ^ z) -#define K 0xCA62C1D6 - - P(A, B, C, D, E, R(60)); - P(E, A, B, C, D, R(61)); - P(D, E, A, B, C, R(62)); - P(C, D, E, A, B, R(63)); - P(B, C, D, E, A, R(64)); - P(A, B, C, D, E, R(65)); - P(E, A, B, C, D, R(66)); - P(D, E, A, B, C, R(67)); - P(C, D, E, A, B, R(68)); - P(B, C, D, E, A, R(69)); - P(A, B, C, D, E, R(70)); - P(E, A, B, C, D, R(71)); - P(D, E, A, B, C, R(72)); - P(C, D, E, A, B, R(73)); - P(B, C, D, E, A, R(74)); - P(A, B, C, D, E, R(75)); - P(E, A, B, C, D, R(76)); - P(D, E, A, B, C, R(77)); - P(C, D, E, A, B, R(78)); - P(B, C, D, E, A, R(79)); - -#undef K -#undef F - - ctx->state[0] += A; - ctx->state[1] += B; - ctx->state[2] += C; - ctx->state[3] += D; - ctx->state[4] += E; -} - -/*****************************************************************************/ -void APP_CC -ssl_sha1_transform(void* sha1_info, char* data, int len) -{ - int left; - int fill; - struct sha1_context* ctx; - - ctx = (struct sha1_context*)sha1_info; - if (len == 0) - { - return; - } - left = ctx->total[0] & 0x3F; - fill = 64 - left; - ctx->total[0] += len; - ctx->total[0] &= 0xFFFFFFFF; - if (ctx->total[0] < len) - { - ctx->total[1]++; - } - if (left && (len >= fill)) - { - memcpy(ctx->buffer + left, data, fill); - sha1_process(ctx, ctx->buffer); - len -= fill; - data += fill; - left = 0; - } - while (len >= 64) - { - sha1_process(ctx, data); - len -= 64; - data += 64; - } - if (len != 0) - { - memcpy(ctx->buffer + left, data, len); - } -} - -static unsigned char sha1_padding[64] = -{ - 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 -}; - -/*****************************************************************************/ -void APP_CC -ssl_sha1_complete(void* sha1_info, char* data) -{ - int last; - int padn; - int high; - int low; - char msglen[8]; - struct sha1_context* ctx; - - ctx = (struct sha1_context*)sha1_info; - high = (ctx->total[0] >> 29) | (ctx->total[1] << 3); - low = (ctx->total[0] << 3); - PUT_UINT32(high, msglen, 0); - PUT_UINT32(low, msglen, 4); - last = ctx->total[0] & 0x3F; - padn = (last < 56) ? (56 - last) : (120 - last); - ssl_sha1_transform(ctx, sha1_padding, padn); - ssl_sha1_transform(ctx, msglen, 8); - PUT_UINT32(ctx->state[0], data, 0); - PUT_UINT32(ctx->state[1], data, 4); - PUT_UINT32(ctx->state[2], data, 8); - PUT_UINT32(ctx->state[3], data, 12); - PUT_UINT32(ctx->state[4], data, 16); -} - -/*****************************************************************************/ -/*****************************************************************************/ -/* md5 stuff */ -/* RFC 1321 compliant MD5 implementation - * - * Copyright (C) 2001-2003 Christophe Devine - */ - -struct md5_context -{ - int total[2]; - int state[4]; - char buffer[64]; -}; - -/*****************************************************************************/ -void* APP_CC -ssl_md5_info_create(void) -{ - return g_malloc(sizeof(struct md5_context), 1); -} - -/*****************************************************************************/ -void APP_CC -ssl_md5_info_delete(void* md5_info) -{ - g_free(md5_info); -} - -/*****************************************************************************/ -void APP_CC -ssl_md5_clear(void* md5_info) -{ - struct md5_context* ctx; - - ctx = (struct md5_context*)md5_info; - memset(ctx, 0, sizeof(struct md5_context)); - ctx->state[0] = 0x67452301; - ctx->state[1] = 0xEFCDAB89; - ctx->state[2] = 0x98BADCFE; - ctx->state[3] = 0x10325476; -} - -#undef GET_UINT32 -#define GET_UINT32(n, b, i) \ -{ \ - (n) = ((b)[(i) + 0] << 0) | \ - ((b)[(i) + 1] << 8) | \ - ((b)[(i) + 2] << 16) | \ - ((b)[(i) + 3] << 24); \ -} - -#undef PUT_UINT32 -#define PUT_UINT32(n, b, i) \ -{ \ - (b)[(i) + 0] = ((n) >> 0); \ - (b)[(i) + 1] = ((n) >> 8); \ - (b)[(i) + 2] = ((n) >> 16); \ - (b)[(i) + 3] = ((n) >> 24); \ -} - -/*****************************************************************************/ -static void -md5_process(struct md5_context* ctx, char* in_data) -{ - int X[16]; - int A; - int B; - int C; - int D; - unsigned char* data; - - data = (unsigned char*)in_data; - GET_UINT32(X[0], data, 0); - GET_UINT32(X[1], data, 4); - GET_UINT32(X[2], data, 8); - GET_UINT32(X[3], data, 12); - GET_UINT32(X[4], data, 16); - GET_UINT32(X[5], data, 20); - GET_UINT32(X[6], data, 24); - GET_UINT32(X[7], data, 28); - GET_UINT32(X[8], data, 32); - GET_UINT32(X[9], data, 36); - GET_UINT32(X[10], data, 40); - GET_UINT32(X[11], data, 44); - GET_UINT32(X[12], data, 48); - GET_UINT32(X[13], data, 52); - GET_UINT32(X[14], data, 56); - GET_UINT32(X[15], data, 60); - -#define S(x, n) ((x << n) | ((x & 0xFFFFFFFF) >> (32 - n))) - -#undef P -#define P(a, b, c, d, k, s, t) \ -{ \ - a += F(b, c, d) + X[k] + t; \ - a = S(a, s) + b; \ -} - - A = ctx->state[0]; - B = ctx->state[1]; - C = ctx->state[2]; - D = ctx->state[3]; - -#define F(x, y, z) (z ^ (x & (y ^ z))) - - P(A, B, C, D, 0, 7, 0xD76AA478); - P(D, A, B, C, 1, 12, 0xE8C7B756); - P(C, D, A, B, 2, 17, 0x242070DB); - P(B, C, D, A, 3, 22, 0xC1BDCEEE); - P(A, B, C, D, 4, 7, 0xF57C0FAF); - P(D, A, B, C, 5, 12, 0x4787C62A); - P(C, D, A, B, 6, 17, 0xA8304613); - P(B, C, D, A, 7, 22, 0xFD469501); - P(A, B, C, D, 8, 7, 0x698098D8); - P(D, A, B, C, 9, 12, 0x8B44F7AF); - P(C, D, A, B, 10, 17, 0xFFFF5BB1); - P(B, C, D, A, 11, 22, 0x895CD7BE); - P(A, B, C, D, 12, 7, 0x6B901122); - P(D, A, B, C, 13, 12, 0xFD987193); - P(C, D, A, B, 14, 17, 0xA679438E); - P(B, C, D, A, 15, 22, 0x49B40821); - -#undef F - -#define F(x, y, z) (y ^ (z & (x ^ y))) - - P(A, B, C, D, 1, 5, 0xF61E2562); - P(D, A, B, C, 6, 9, 0xC040B340); - P(C, D, A, B, 11, 14, 0x265E5A51); - P(B, C, D, A, 0, 20, 0xE9B6C7AA); - P(A, B, C, D, 5, 5, 0xD62F105D); - P(D, A, B, C, 10, 9, 0x02441453); - P(C, D, A, B, 15, 14, 0xD8A1E681); - P(B, C, D, A, 4, 20, 0xE7D3FBC8); - P(A, B, C, D, 9, 5, 0x21E1CDE6); - P(D, A, B, C, 14, 9, 0xC33707D6); - P(C, D, A, B, 3, 14, 0xF4D50D87); - P(B, C, D, A, 8, 20, 0x455A14ED); - P(A, B, C, D, 13, 5, 0xA9E3E905); - P(D, A, B, C, 2, 9, 0xFCEFA3F8); - P(C, D, A, B, 7, 14, 0x676F02D9); - P(B, C, D, A, 12, 20, 0x8D2A4C8A); - -#undef F - -#define F(x, y, z) (x ^ y ^ z) - - P(A, B, C, D, 5, 4, 0xFFFA3942); - P(D, A, B, C, 8, 11, 0x8771F681); - P(C, D, A, B, 11, 16, 0x6D9D6122); - P(B, C, D, A, 14, 23, 0xFDE5380C); - P(A, B, C, D, 1, 4, 0xA4BEEA44); - P(D, A, B, C, 4, 11, 0x4BDECFA9); - P(C, D, A, B, 7, 16, 0xF6BB4B60); - P(B, C, D, A, 10, 23, 0xBEBFBC70); - P(A, B, C, D, 13, 4, 0x289B7EC6); - P(D, A, B, C, 0, 11, 0xEAA127FA); - P(C, D, A, B, 3, 16, 0xD4EF3085); - P(B, C, D, A, 6, 23, 0x04881D05); - P(A, B, C, D, 9, 4, 0xD9D4D039); - P(D, A, B, C, 12, 11, 0xE6DB99E5); - P(C, D, A, B, 15, 16, 0x1FA27CF8); - P(B, C, D, A, 2, 23, 0xC4AC5665); - -#undef F - -#define F(x, y, z) (y ^ (x | ~z)) - - P(A, B, C, D, 0, 6, 0xF4292244); - P(D, A, B, C, 7, 10, 0x432AFF97); - P(C, D, A, B, 14, 15, 0xAB9423A7); - P(B, C, D, A, 5, 21, 0xFC93A039); - P(A, B, C, D, 12, 6, 0x655B59C3); - P(D, A, B, C, 3, 10, 0x8F0CCC92); - P(C, D, A, B, 10, 15, 0xFFEFF47D); - P(B, C, D, A, 1, 21, 0x85845DD1); - P(A, B, C, D, 8, 6, 0x6FA87E4F); - P(D, A, B, C, 15, 10, 0xFE2CE6E0); - P(C, D, A, B, 6, 15, 0xA3014314); - P(B, C, D, A, 13, 21, 0x4E0811A1); - P(A, B, C, D, 4, 6, 0xF7537E82); - P(D, A, B, C, 11, 10, 0xBD3AF235); - P(C, D, A, B, 2, 15, 0x2AD7D2BB); - P(B, C, D, A, 9, 21, 0xEB86D391); - -#undef F - - ctx->state[0] += A; - ctx->state[1] += B; - ctx->state[2] += C; - ctx->state[3] += D; -} - -/*****************************************************************************/ -void APP_CC -ssl_md5_transform(void* md5_info, char* data, int len) -{ - int left; - int fill; - struct md5_context* ctx; - - ctx = (struct md5_context*)md5_info; - if (len == 0) - { - return; - } - left = ctx->total[0] & 0x3F; - fill = 64 - left; - ctx->total[0] += len; - ctx->total[0] &= 0xFFFFFFFF; - if (ctx->total[0] < len) - { - ctx->total[1]++; - } - if (left && (len >= fill)) - { - memcpy(ctx->buffer + left, data, fill); - md5_process(ctx, ctx->buffer); - len -= fill; - data += fill; - left = 0; - } - while (len >= 64) - { - md5_process(ctx, data); - len -= 64; - data += 64; - } - if (len != 0) - { - memcpy(ctx->buffer + left, data, len); - } -} - -static unsigned char md5_padding[64] = -{ - 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 -}; - -/*****************************************************************************/ -void APP_CC -ssl_md5_complete(void* md5_info, char* data) -{ - int last; - int padn; - int high; - int low; - char msglen[8]; - struct md5_context* ctx; - - ctx = (struct md5_context*)md5_info; - high = (ctx->total[0] >> 29) | (ctx->total[1] << 3); - low = (ctx->total[0] << 3); - PUT_UINT32(low, msglen, 0); - PUT_UINT32(high, msglen, 4); - last = ctx->total[0] & 0x3F; - padn = (last < 56) ? (56 - last) : (120 - last); - ssl_md5_transform(ctx, md5_padding, padn); - ssl_md5_transform(ctx, msglen, 8); - PUT_UINT32(ctx->state[0], data, 0); - PUT_UINT32(ctx->state[1], data, 4); - PUT_UINT32(ctx->state[2], data, 8); - PUT_UINT32(ctx->state[3], data, 12); -} - -/*****************************************************************************/ -/*****************************************************************************/ -/* big number stuff */ -/******************* SHORT COPYRIGHT NOTICE************************* -This source code is part of the BigDigits multiple-precision -arithmetic library Version 1.0 originally written by David Ireland, -copyright (c) 2001 D.I. Management Services Pty Limited, all rights -reserved. It is provided "as is" with no warranties. You may use -this software under the terms of the full copyright notice -"bigdigitsCopyright.txt" that should have been included with -this library. To obtain a copy send an email to - or visit . -This notice must be retained in any copy. -****************** END OF COPYRIGHT NOTICE*************************/ -/************************* COPYRIGHT NOTICE************************* -This source code is part of the BigDigits multiple-precision -arithmetic library Version 1.0 originally written by David Ireland, -copyright (c) 2001 D.I. Management Services Pty Limited, all rights -reserved. You are permitted to use compiled versions of this code as -part of your own executable files and to distribute unlimited copies -of such executable files for any purposes including commercial ones -provided you keep the copyright notices intact in the source code -and that you ensure that the following characters remain in any -object or executable files you distribute: - -"Contains multiple-precision arithmetic code originally written -by David Ireland, copyright (c) 2001 by D.I. Management Services -Pty Limited , and is used with permission." - -David Ireland and DI Management Services Pty Limited make no -representations concerning either the merchantability of this -software or the suitability of this software for any particular -purpose. It is provided "as is" without express or implied warranty -of any kind. - -Please forward any comments and bug reports to . -The latest version of the source code can be downloaded from -www.di-mgt.com.au/crypto.html. -****************** END OF COPYRIGHT NOTICE*************************/ - -typedef unsigned int DIGIT_T; -#define HIBITMASK 0x80000000 -#define MAX_DIG_LEN 51 -#define MAX_DIGIT 0xffffffff -#define BITS_PER_DIGIT 32 -#define MAX_HALF_DIGIT 0xffff -#define B_J (MAX_HALF_DIGIT + 1) -#define LOHALF(x) ((DIGIT_T)((x) & 0xffff)) -#define HIHALF(x) ((DIGIT_T)((x) >> 16 & 0xffff)) -#define TOHIGH(x) ((DIGIT_T)((x) << 16)) - -#define mpNEXTBITMASK(mask, n) \ -{ \ - if (mask == 1) \ - { \ - mask = HIBITMASK; \ - n--; \ - } \ - else \ - { \ - mask >>= 1; \ - } \ -} - -/*****************************************************************************/ -static DIGIT_T APP_CC -mpAdd(DIGIT_T* w, DIGIT_T* u, DIGIT_T* v, unsigned int ndigits) -{ - /* Calculates w = u + v - where w, u, v are multiprecision integers of ndigits each - Returns carry if overflow. Carry = 0 or 1. - - Ref: Knuth Vol 2 Ch 4.3.1 p 266 Algorithm A. */ - DIGIT_T k; - unsigned int j; - - /* Step A1. Initialise */ - k = 0; - for (j = 0; j < ndigits; j++) - { - /* Step A2. Add digits w_j = (u_j + v_j + k) - Set k = 1 if carry (overflow) occurs */ - w[j] = u[j] + k; - if (w[j] < k) - { - k = 1; - } - else - { - k = 0; - } - w[j] += v[j]; - if (w[j] < v[j]) - { - k++; - } - } /* Step A3. Loop on j */ - return k; /* w_n = k */ -} - -/*****************************************************************************/ -static void APP_CC -mpSetDigit(DIGIT_T* a, DIGIT_T d, unsigned int ndigits) -{ /* Sets a = d where d is a single digit */ - unsigned int i; - - for (i = 1; i < ndigits; i++) - { - a[i] = 0; - } - a[0] = d; -} - -/*****************************************************************************/ -static int APP_CC -mpCompare(DIGIT_T* a, DIGIT_T* b, unsigned int ndigits) -{ - /* Returns sign of (a - b) */ - if (ndigits == 0) - { - return 0; - } - while (ndigits--) - { - if (a[ndigits] > b[ndigits]) - { - return 1; /* GT */ - } - if (a[ndigits] < b[ndigits]) - { - return -1; /* LT */ - } - } - return 0; /* EQ */ -} - -/*****************************************************************************/ -static void APP_CC -mpSetZero(DIGIT_T* a, unsigned int ndigits) -{ /* Sets a = 0 */ - unsigned int i; - - for (i = 0; i < ndigits; i++) - { - a[i] = 0; - } -} - -/*****************************************************************************/ -static void APP_CC -mpSetEqual(DIGIT_T* a, DIGIT_T* b, unsigned int ndigits) -{ /* Sets a = b */ - unsigned int i; - - for (i = 0; i < ndigits; i++) - { - a[i] = b[i]; - } -} - -/*****************************************************************************/ -static unsigned int APP_CC -mpSizeof(DIGIT_T* a, unsigned int ndigits) -{ /* Returns size of significant digits in a */ - while (ndigits--) - { - if (a[ndigits] != 0) - { - return (++ndigits); - } - } - return 0; -} - -/*****************************************************************************/ -static DIGIT_T APP_CC -mpShiftLeft(DIGIT_T* a, DIGIT_T* b, unsigned int x, unsigned int ndigits) -{ /* Computes a = b << x */ - unsigned int i; - unsigned int y; - DIGIT_T mask; - DIGIT_T carry; - DIGIT_T nextcarry; - - /* Check input - NB unspecified result */ - if (x >= BITS_PER_DIGIT) - { - return 0; - } - /* Construct mask */ - mask = HIBITMASK; - for (i = 1; i < x; i++) - { - mask = (mask >> 1) | mask; - } - if (x == 0) - { - mask = 0x0; - } - y = BITS_PER_DIGIT - x; - carry = 0; - for (i = 0; i < ndigits; i++) - { - nextcarry = (b[i] & mask) >> y; - a[i] = b[i] << x | carry; - carry = nextcarry; - } - return carry; -} - -/*****************************************************************************/ -static DIGIT_T APP_CC -mpShiftRight(DIGIT_T* a, DIGIT_T* b, unsigned int x, unsigned int ndigits) -{ /* Computes a = b >> x */ - unsigned int i; - unsigned int y; - DIGIT_T mask; - DIGIT_T carry; - DIGIT_T nextcarry; - - /* Check input - NB unspecified result */ - if (x >= BITS_PER_DIGIT) - { - return 0; - } - /* Construct mask */ - mask = 0x1; - for (i = 1; i < x; i++) - { - mask = (mask << 1) | mask; - } - if (x == 0) - { - mask = 0x0; - } - y = BITS_PER_DIGIT - x; - carry = 0; - i = ndigits; - while (i--) - { - nextcarry = (b[i] & mask) << y; - a[i] = b[i] >> x | carry; - carry = nextcarry; - } - return carry; -} - -/*****************************************************************************/ -static void APP_CC -spMultSub(DIGIT_T* uu, DIGIT_T qhat, DIGIT_T v1, DIGIT_T v0) -{ - /* Compute uu = uu - q(v1v0) - where uu = u3u2u1u0, u3 = 0 - and u_n, v_n are all half-digits - even though v1, v2 are passed as full digits. */ - DIGIT_T p0; - DIGIT_T p1; - DIGIT_T t; - - p0 = qhat * v0; - p1 = qhat * v1; - t = p0 + TOHIGH(LOHALF(p1)); - uu[0] -= t; - if (uu[0] > MAX_DIGIT - t) - { - uu[1]--; /* Borrow */ - } - uu[1] -= HIHALF(p1); -} - -/*****************************************************************************/ -static int APP_CC -spMultiply(DIGIT_T* p, DIGIT_T x, DIGIT_T y) -{ /* Computes p = x * y */ - /* Ref: Arbitrary Precision Computation - http://numbers.computation.free.fr/Constants/constants.html - - high p1 p0 low - +--------+--------+--------+--------+ - | x1*y1 | x0*y0 | - +--------+--------+--------+--------+ - +-+--------+--------+ - |1| (x0*y1 + x1*y1) | - +-+--------+--------+ - ^carry from adding (x0*y1+x1*y1) together - +-+ - |1|< carry from adding LOHALF t - +-+ to high half of p0 */ - DIGIT_T x0; - DIGIT_T y0; - DIGIT_T x1; - DIGIT_T y1; - DIGIT_T t; - DIGIT_T u; - DIGIT_T carry; - - /* Split each x,y into two halves - x = x0 + B * x1 - y = y0 + B * y1 - where B = 2^16, half the digit size - Product is - xy = x0y0 + B(x0y1 + x1y0) + B^2(x1y1) */ - - x0 = LOHALF(x); - x1 = HIHALF(x); - y0 = LOHALF(y); - y1 = HIHALF(y); - - /* Calc low part - no carry */ - p[0] = x0 * y0; - - /* Calc middle part */ - t = x0 * y1; - u = x1 * y0; - t += u; - if (t < u) - { - carry = 1; - } - else - { - carry = 0; - } - /* This carry will go to high half of p[1] - + high half of t into low half of p[1] */ - carry = TOHIGH(carry) + HIHALF(t); - - /* Add low half of t to high half of p[0] */ - t = TOHIGH(t); - p[0] += t; - if (p[0] < t) - { - carry++; - } - - p[1] = x1 * y1; - p[1] += carry; - - return 0; -} - -/*****************************************************************************/ -static DIGIT_T APP_CC -spDivide(DIGIT_T* q, DIGIT_T* r, DIGIT_T* u, DIGIT_T v) -{ /* Computes quotient q = u / v, remainder r = u mod v - where u is a double digit - and q, v, r are single precision digits. - Returns high digit of quotient (max value is 1) - Assumes normalised such that v1 >= b/2 - where b is size of HALF_DIGIT - i.e. the most significant bit of v should be one - - In terms of half-digits in Knuth notation: - (q2q1q0) = (u4u3u2u1u0) / (v1v0) - (r1r0) = (u4u3u2u1u0) mod (v1v0) - for m = 2, n = 2 where u4 = 0 - q2 is either 0 or 1. - We set q = (q1q0) and return q2 as "overflow' */ - DIGIT_T qhat; - DIGIT_T rhat; - DIGIT_T t; - DIGIT_T v0; - DIGIT_T v1; - DIGIT_T u0; - DIGIT_T u1; - DIGIT_T u2; - DIGIT_T u3; - DIGIT_T uu[2]; - DIGIT_T q2; - - /* Check for normalisation */ - if (!(v & HIBITMASK)) - { - *q = *r = 0; - return MAX_DIGIT; - } - - /* Split up into half-digits */ - v0 = LOHALF(v); - v1 = HIHALF(v); - u0 = LOHALF(u[0]); - u1 = HIHALF(u[0]); - u2 = LOHALF(u[1]); - u3 = HIHALF(u[1]); - - /* Do three rounds of Knuth Algorithm D Vol 2 p272 */ - - /* ROUND 1. Set j = 2 and calculate q2 */ - /* Estimate qhat = (u4u3)/v1 = 0 or 1 - then set (u4u3u2) -= qhat(v1v0) - where u4 = 0. */ - qhat = u3 / v1; - if (qhat > 0) - { - rhat = u3 - qhat * v1; - t = TOHIGH(rhat) | u2; - if (qhat * v0 > t) - { - qhat--; - } - } - uu[1] = 0; /* (u4) */ - uu[0] = u[1]; /* (u3u2) */ - if (qhat > 0) - { - /* (u4u3u2) -= qhat(v1v0) where u4 = 0 */ - spMultSub(uu, qhat, v1, v0); - if (HIHALF(uu[1]) != 0) - { /* Add back */ - qhat--; - uu[0] += v; - uu[1] = 0; - } - } - q2 = qhat; - /* ROUND 2. Set j = 1 and calculate q1 */ - /* Estimate qhat = (u3u2) / v1 - then set (u3u2u1) -= qhat(v1v0) */ - t = uu[0]; - qhat = t / v1; - rhat = t - qhat * v1; - /* Test on v0 */ - t = TOHIGH(rhat) | u1; - if ((qhat == B_J) || (qhat * v0 > t)) - { - qhat--; - rhat += v1; - t = TOHIGH(rhat) | u1; - if ((rhat < B_J) && (qhat * v0 > t)) - { - qhat--; - } - } - /* Multiply and subtract - (u3u2u1)' = (u3u2u1) - qhat(v1v0) */ - uu[1] = HIHALF(uu[0]); /* (0u3) */ - uu[0] = TOHIGH(LOHALF(uu[0])) | u1; /* (u2u1) */ - spMultSub(uu, qhat, v1, v0); - if (HIHALF(uu[1]) != 0) - { /* Add back */ - qhat--; - uu[0] += v; - uu[1] = 0; - } - /* q1 = qhat */ - *q = TOHIGH(qhat); - /* ROUND 3. Set j = 0 and calculate q0 */ - /* Estimate qhat = (u2u1) / v1 - then set (u2u1u0) -= qhat(v1v0) */ - t = uu[0]; - qhat = t / v1; - rhat = t - qhat * v1; - /* Test on v0 */ - t = TOHIGH(rhat) | u0; - if ((qhat == B_J) || (qhat * v0 > t)) - { - qhat--; - rhat += v1; - t = TOHIGH(rhat) | u0; - if ((rhat < B_J) && (qhat * v0 > t)) - { - qhat--; - } - } - /* Multiply and subtract - (u2u1u0)" = (u2u1u0)' - qhat(v1v0) */ - uu[1] = HIHALF(uu[0]); /* (0u2) */ - uu[0] = TOHIGH(LOHALF(uu[0])) | u0; /* (u1u0) */ - spMultSub(uu, qhat, v1, v0); - if (HIHALF(uu[1]) != 0) - { /* Add back */ - qhat--; - uu[0] += v; - uu[1] = 0; - } - /* q0 = qhat */ - *q |= LOHALF(qhat); - /* Remainder is in (u1u0) i.e. uu[0] */ - *r = uu[0]; - return q2; -} - -/*****************************************************************************/ -static int APP_CC -QhatTooBig(DIGIT_T qhat, DIGIT_T rhat, DIGIT_T vn2, DIGIT_T ujn2) -{ /* Returns true if Qhat is too big - i.e. if (Qhat * Vn-2) > (b.Rhat + Uj+n-2) */ - DIGIT_T t[2]; - - spMultiply(t, qhat, vn2); - if (t[1] < rhat) - { - return 0; - } - else if (t[1] > rhat) - { - return 1; - } - else if (t[0] > ujn2) - { - return 1; - } - return 0; -} - -/*****************************************************************************/ -static DIGIT_T APP_CC -mpShortDiv(DIGIT_T* q, DIGIT_T* u, DIGIT_T v, unsigned int ndigits) -{ - /* Calculates quotient q = u div v - Returns remainder r = u mod v - where q, u are multiprecision integers of ndigits each - and d, v are single precision digits. - - Makes no assumptions about normalisation. - - Ref: Knuth Vol 2 Ch 4.3.1 Exercise 16 p625 */ - unsigned int j; - unsigned int shift; - DIGIT_T t[2]; - DIGIT_T r; - DIGIT_T bitmask; - DIGIT_T overflow; - DIGIT_T* uu; - - if (ndigits == 0) - { - return 0; - } - if (v == 0) - { - return 0; /* Divide by zero error */ - } - /* Normalise first */ - /* Requires high bit of V - to be set, so find most signif. bit then shift left, - i.e. d = 2^shift, u' = u * d, v' = v * d. */ - bitmask = HIBITMASK; - for (shift = 0; shift < BITS_PER_DIGIT; shift++) - { - if (v & bitmask) - { - break; - } - bitmask >>= 1; - } - v <<= shift; - overflow = mpShiftLeft(q, u, shift, ndigits); - uu = q; - /* Step S1 - modified for extra digit. */ - r = overflow; /* New digit Un */ - j = ndigits; - while (j--) - { - /* Step S2. */ - t[1] = r; - t[0] = uu[j]; - overflow = spDivide(&q[j], &r, t, v); - } - /* Unnormalise */ - r >>= shift; - return r; -} - -/*****************************************************************************/ -static DIGIT_T APP_CC -mpMultSub(DIGIT_T wn, DIGIT_T* w, DIGIT_T* v, DIGIT_T q, unsigned int n) -{ /* Compute w = w - qv - where w = (WnW[n-1]...W[0]) - return modified Wn. */ - DIGIT_T k; - DIGIT_T t[2]; - unsigned int i; - - if (q == 0) /* No change */ - { - return wn; - } - k = 0; - for (i = 0; i < n; i++) - { - spMultiply(t, q, v[i]); - w[i] -= k; - if (w[i] > MAX_DIGIT - k) - { - k = 1; - } - else - { - k = 0; - } - w[i] -= t[0]; - if (w[i] > MAX_DIGIT - t[0]) - { - k++; - } - k += t[1]; - } - /* Cope with Wn not stored in array w[0..n-1] */ - wn -= k; - return wn; -} - -/*****************************************************************************/ -static int APP_CC -mpDivide(DIGIT_T* q, DIGIT_T* r, DIGIT_T* u, unsigned int udigits, - DIGIT_T* v, unsigned int vdigits) -{ /* Computes quotient q = u / v and remainder r = u mod v - where q, r, u are multiple precision digits - all of udigits and the divisor v is vdigits. - - Ref: Knuth Vol 2 Ch 4.3.1 p 272 Algorithm D. - - Do without extra storage space, i.e. use r[] for - normalised u[], unnormalise v[] at end, and cope with - extra digit Uj+n added to u after normalisation. - - WARNING: this trashes q and r first, so cannot do - u = u / v or v = u mod v. */ - unsigned int shift; - int n; - int m; - int j; - int qhatOK; - int cmp; - DIGIT_T bitmask; - DIGIT_T overflow; - DIGIT_T qhat; - DIGIT_T rhat; - DIGIT_T t[2]; - DIGIT_T* uu; - DIGIT_T* ww; - - /* Clear q and r */ - mpSetZero(q, udigits); - mpSetZero(r, udigits); - /* Work out exact sizes of u and v */ - n = (int)mpSizeof(v, vdigits); - m = (int)mpSizeof(u, udigits); - m -= n; - /* Catch special cases */ - if (n == 0) - { - return -1; /* Error: divide by zero */ - } - if (n == 1) - { /* Use short division instead */ - r[0] = mpShortDiv(q, u, v[0], udigits); - return 0; - } - if (m < 0) - { /* v > u, so just set q = 0 and r = u */ - mpSetEqual(r, u, udigits); - return 0; - } - if (m == 0) - { /* u and v are the same length */ - cmp = mpCompare(u, v, (unsigned int)n); - if (cmp < 0) - { /* v > u, as above */ - mpSetEqual(r, u, udigits); - return 0; - } - else if (cmp == 0) - { /* v == u, so set q = 1 and r = 0 */ - mpSetDigit(q, 1, udigits); - return 0; - } - } - /* In Knuth notation, we have: - Given - u = (Um+n-1 ... U1U0) - v = (Vn-1 ... V1V0) - Compute - q = u/v = (QmQm-1 ... Q0) - r = u mod v = (Rn-1 ... R1R0) */ - /* Step D1. Normalise */ - /* Requires high bit of Vn-1 - to be set, so find most signif. bit then shift left, - i.e. d = 2^shift, u' = u * d, v' = v * d. */ - bitmask = HIBITMASK; - for (shift = 0; shift < BITS_PER_DIGIT; shift++) - { - if (v[n - 1] & bitmask) - { - break; - } - bitmask >>= 1; - } - /* Normalise v in situ - NB only shift non-zero digits */ - overflow = mpShiftLeft(v, v, shift, n); - /* Copy normalised dividend u*d into r */ - overflow = mpShiftLeft(r, u, shift, n + m); - uu = r; /* Use ptr to keep notation constant */ - t[0] = overflow; /* New digit Um+n */ - /* Step D2. Initialise j. Set j = m */ - for (j = m; j >= 0; j--) - { - /* Step D3. Calculate Qhat = (b.Uj+n + Uj+n-1)/Vn-1 */ - qhatOK = 0; - t[1] = t[0]; /* This is Uj+n */ - t[0] = uu[j+n-1]; - overflow = spDivide(&qhat, &rhat, t, v[n - 1]); - /* Test Qhat */ - if (overflow) - { /* Qhat = b */ - qhat = MAX_DIGIT; - rhat = uu[j + n - 1]; - rhat += v[n - 1]; - if (rhat < v[n - 1]) /* Overflow */ - { - qhatOK = 1; - } - } - if (!qhatOK && QhatTooBig(qhat, rhat, v[n - 2], uu[j + n - 2])) - { /* Qhat.Vn-2 > b.Rhat + Uj+n-2 */ - qhat--; - rhat += v[n - 1]; - if (!(rhat < v[n - 1])) - { - if (QhatTooBig(qhat, rhat, v[n - 2], uu[j + n - 2])) - { - qhat--; - } - } - } - /* Step D4. Multiply and subtract */ - ww = &uu[j]; - overflow = mpMultSub(t[1], ww, v, qhat, (unsigned int)n); - /* Step D5. Test remainder. Set Qj = Qhat */ - q[j] = qhat; - if (overflow) - { /* Step D6. Add back if D4 was negative */ - q[j]--; - overflow = mpAdd(ww, ww, v, (unsigned int)n); - } - t[0] = uu[j + n - 1]; /* Uj+n on next round */ - } /* Step D7. Loop on j */ - /* Clear high digits in uu */ - for (j = n; j < m+n; j++) - { - uu[j] = 0; - } - /* Step D8. Unnormalise. */ - mpShiftRight(r, r, shift, n); - mpShiftRight(v, v, shift, n); - return 0; -} - -/*****************************************************************************/ -static int APP_CC -mpModulo(DIGIT_T* r, DIGIT_T* u, unsigned int udigits, - DIGIT_T* v, unsigned int vdigits) -{ - /* Calculates r = u mod v - where r, v are multiprecision integers of length vdigits - and u is a multiprecision integer of length udigits. - r may overlap v. - - Note that r here is only vdigits long, - whereas in mpDivide it is udigits long. - - Use remainder from mpDivide function. */ - /* Double-length temp variable for divide fn */ - DIGIT_T qq[MAX_DIG_LEN * 2]; - /* Use a double-length temp for r to allow overlap of r and v */ - DIGIT_T rr[MAX_DIG_LEN * 2]; - - /* rr[2n] = u[2n] mod v[n] */ - mpDivide(qq, rr, u, udigits, v, vdigits); - mpSetEqual(r, rr, vdigits); - mpSetZero(rr, udigits); - mpSetZero(qq, udigits); - return 0; -} - -/*****************************************************************************/ -static int APP_CC -mpMultiply(DIGIT_T* w, DIGIT_T* u, DIGIT_T* v, unsigned int ndigits) -{ - /* Computes product w = u * v - where u, v are multiprecision integers of ndigits each - and w is a multiprecision integer of 2*ndigits - Ref: Knuth Vol 2 Ch 4.3.1 p 268 Algorithm M. */ - DIGIT_T k; - DIGIT_T t[2]; - unsigned int i; - unsigned int j; - unsigned int m; - unsigned int n; - - n = ndigits; - m = n; - /* Step M1. Initialise */ - for (i = 0; i < 2 * m; i++) - { - w[i] = 0; - } - for (j = 0; j < n; j++) - { - /* Step M2. Zero multiplier? */ - if (v[j] == 0) - { - w[j + m] = 0; - } - else - { - /* Step M3. Initialise i */ - k = 0; - for (i = 0; i < m; i++) - { - /* Step M4. Multiply and add */ - /* t = u_i * v_j + w_(i+j) + k */ - spMultiply(t, u[i], v[j]); - t[0] += k; - if (t[0] < k) - { - t[1]++; - } - t[0] += w[i + j]; - if (t[0] < w[i+j]) - { - t[1]++; - } - w[i + j] = t[0]; - k = t[1]; - } - /* Step M5. Loop on i, set w_(j+m) = k */ - w[j + m] = k; - } - } /* Step M6. Loop on j */ - return 0; -} - -/*****************************************************************************/ -static int APP_CC -mpModMult(DIGIT_T* a, DIGIT_T* x, DIGIT_T* y, - DIGIT_T* m, unsigned int ndigits) -{ /* Computes a = (x * y) mod m */ - /* Double-length temp variable */ - DIGIT_T p[MAX_DIG_LEN * 2]; - - /* Calc p[2n] = x * y */ - mpMultiply(p, x, y, ndigits); - /* Then modulo */ - mpModulo(a, p, ndigits * 2, m, ndigits); - mpSetZero(p, ndigits * 2); - return 0; -} - -/*****************************************************************************/ -int APP_CC -ssl_mod_exp(char* out, int out_len, char* in, int in_len, - char* mod, int mod_len, char* exp, int exp_len) -{ - /* Computes y = x ^ e mod m */ - /* Binary left-to-right method */ - DIGIT_T mask; - DIGIT_T* e; - DIGIT_T* x; - DIGIT_T* y; - DIGIT_T* m; - unsigned int n; - int max_size; - char* l_out; - char* l_in; - char* l_mod; - char* l_exp; - - if (in_len > out_len || in_len == 0 || - out_len == 0 || mod_len == 0 || exp_len == 0) - { - return 0; - } - max_size = out_len; - if (in_len > max_size) - { - max_size = in_len; - } - if (mod_len > max_size) - { - max_size = mod_len; - } - if (exp_len > max_size) - { - max_size = exp_len; - } - l_out = (char*)g_malloc(max_size, 1); - l_in = (char*)g_malloc(max_size, 1); - l_mod = (char*)g_malloc(max_size, 1); - l_exp = (char*)g_malloc(max_size, 1); - memcpy(l_in, in, in_len); - memcpy(l_mod, mod, mod_len); - memcpy(l_exp, exp, exp_len); - max_size = out_len; - e = (DIGIT_T*)l_exp; - x = (DIGIT_T*)l_in; - y = (DIGIT_T*)l_out; - m = (DIGIT_T*)l_mod; - /* Find second-most significant bit in e */ - n = mpSizeof(e, max_size / 4); - for (mask = HIBITMASK; mask > 0; mask >>= 1) - { - if (e[n - 1] & mask) - { - break; - } - } - mpNEXTBITMASK(mask, n); - /* Set y = x */ - mpSetEqual(y, x, max_size / 4); - /* For bit j = k - 2 downto 0 step -1 */ - while (n) - { - mpModMult(y, y, y, m, max_size / 4); /* Square */ - if (e[n - 1] & mask) - { - mpModMult(y, y, x, m, max_size / 4); /* Multiply */ - } - /* Move to next bit */ - mpNEXTBITMASK(mask, n); - } - memcpy(out, l_out, out_len); - g_free(l_out); - g_free(l_in); - g_free(l_mod); - g_free(l_exp); - return out_len; -} - diff --git a/uirdesktop/tcp.c b/uirdesktop/tcp.c deleted file mode 100644 index e2e60d77..00000000 --- a/uirdesktop/tcp.c +++ /dev/null @@ -1,316 +0,0 @@ -/* -*- c-basic-offset: 8 -*- - rdesktop: A Remote Desktop Protocol client. - Protocol services - TCP layer - Copyright (C) Matthew Chapman 1999-2005 - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -*/ - -#ifndef _WIN32 -#include /* select read write close */ -#include /* socket connect setsockopt */ -#include /* timeval */ -#include /* gethostbyname */ -#include /* sockaddr_in */ -#include /* TCP_NODELAY */ -#include /* inet_addr */ -#include /* errno */ -#endif /* _WIN32 */ - -#include "rdesktop.h" - -#ifdef _WIN32 -#define socklen_t int -#define TCP_CLOSE(_sck) closesocket(_sck) -#define TCP_STRERROR "tcp error" -#define TCP_SLEEP(_n) Sleep(_n) -#define TCP_BLOCKS (WSAGetLastError() == WSAEWOULDBLOCK) -#else /* _WIN32 */ -#define TCP_CLOSE(_sck) close(_sck) -#define TCP_STRERROR strerror(errno) -#define TCP_SLEEP(_n) sleep(_n) -#define TCP_BLOCKS (errno == EWOULDBLOCK) -#endif /* _WIN32 */ - -#ifndef INADDR_NONE -#define INADDR_NONE ((unsigned long) -1) -#endif - -static int sock; -static struct stream in; -static struct stream out; -int g_tcp_port_rdp = TCP_PORT_RDP; -int g_bytes_in = 0; - -/* Initialise TCP transport data packet */ -STREAM -tcp_init(uint32 maxlen) -{ - if (maxlen > out.size) - { - out.data = (uint8 *) xrealloc(out.data, maxlen); - out.size = maxlen; - } - - out.p = out.data; - out.end = out.data + out.size; - return &out; -} - -/* Send TCP transport data packet */ -void -tcp_send(STREAM s) -{ - int length = s->end - s->data; - int sent, total = 0; - - while (total < length) - { - sent = send(sock, s->data + total, length - total, 0); - if (sent <= 0) - { - if (sent == -1 && TCP_BLOCKS) - { - TCP_SLEEP(0); - sent = 0; - } - else - { - error("send: %s\n", TCP_STRERROR); - return; - } - } - total += sent; - } -} - -/* Receive a message on the TCP layer */ -STREAM -tcp_recv(STREAM s, uint32 length) -{ - unsigned int new_length, end_offset, p_offset; - int rcvd = 0; - - g_bytes_in += length; - if (s == NULL) - { - /* read into "new" stream */ - if (length > in.size) - { - in.data = (uint8 *) xrealloc(in.data, length); - in.size = length; - } - in.end = in.p = in.data; - s = ∈ - } - else - { - /* append to existing stream */ - new_length = (s->end - s->data) + length; - if (new_length > s->size) - { - p_offset = s->p - s->data; - end_offset = s->end - s->data; - s->data = (uint8 *) xrealloc(s->data, new_length); - s->size = new_length; - s->p = s->data + p_offset; - s->end = s->data + end_offset; - } - } - - while (length > 0) - { - if (!ui_select(sock)) - /* User quit */ - return NULL; - - rcvd = recv(sock, s->end, length, 0); - if (rcvd < 0) - { - if (rcvd == -1 && TCP_BLOCKS) - { - TCP_SLEEP(0); - rcvd = 0; - } - else - { - error("recv: %s\n", TCP_STRERROR); - return NULL; - } - } - else if (rcvd == 0) - { - error("Connection closed\n"); - return NULL; - } - - s->end += rcvd; - length -= rcvd; - } - - return s; -} - -/* Establish a connection on the TCP layer */ -BOOL -tcp_connect(char *server) -{ - int lvalue; - -#ifdef IPv6 - - int n; - struct addrinfo hints, *res, *ressave; - char tcp_port_rdp_s[10]; - - snprintf(tcp_port_rdp_s, 10, "%d", g_tcp_port_rdp); - - memset(&hints, 0, sizeof(struct addrinfo)); - hints.ai_family = AF_UNSPEC; - hints.ai_socktype = SOCK_STREAM; - - if ((n = getaddrinfo(server, tcp_port_rdp_s, &hints, &res))) - { - error("getaddrinfo: %s\n", gai_strerror(n)); - return False; - } - - ressave = res; - sock = -1; - while (res) - { - sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); - if (!(sock < 0)) - { - if (connect(sock, res->ai_addr, res->ai_addrlen) == 0) - break; - TCP_CLOSE(sock); - sock = -1; - } - res = res->ai_next; - } - freeaddrinfo(ressave); - - if (sock == -1) - { - error("%s: unable to connect\n", server); - return False; - } - -#else /* no IPv6 support */ - - struct hostent *nslookup; - struct sockaddr_in servaddr; - - if ((nslookup = gethostbyname(server)) != NULL) - { - memcpy(&servaddr.sin_addr, nslookup->h_addr, sizeof(servaddr.sin_addr)); - } - else if ((servaddr.sin_addr.s_addr = inet_addr(server)) == INADDR_NONE) - { - error("%s: unable to resolve host\n", server); - return False; - } - - if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) - { - error("socket: %s\n", TCP_STRERROR); - return False; - } - - servaddr.sin_family = AF_INET; - servaddr.sin_port = htons((uint16) g_tcp_port_rdp); - - if (connect(sock, (struct sockaddr *) &servaddr, sizeof(struct sockaddr)) < 0) - { - error("connect: %s\n", TCP_STRERROR); - TCP_CLOSE(sock); - return False; - } - -#endif /* IPv6 */ - - //lvalue = 1; - //setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (void *) &lvalue, sizeof(lvalue)); - lvalue = 8192 * 2; - setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (void *) &lvalue, sizeof(lvalue)); - lvalue = 8192 * 2; - setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (void *) &lvalue, sizeof(lvalue)); - - in.size = 8192 * 2; - in.data = (uint8 *) xmalloc(in.size); - - out.size = 8192 * 2; - out.data = (uint8 *) xmalloc(out.size); - - return True; -} - -/* Disconnect on the TCP layer */ -void -tcp_disconnect(void) -{ - TCP_CLOSE(sock); -} - -char * -tcp_get_address() -{ - static char ipaddr[32]; - struct sockaddr_in sockaddr; - socklen_t len = sizeof(sockaddr); - if (getsockname(sock, (struct sockaddr *) &sockaddr, &len) == 0) - { - unsigned char *ip = (unsigned char *) &sockaddr.sin_addr; - sprintf(ipaddr, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); - } - else - strcpy(ipaddr, "127.0.0.1"); - return ipaddr; -} - -/* reset the state of the tcp layer */ -/* Support for Session Directory */ -void -tcp_reset_state(void) -{ - sock = -1; /* reset socket */ - - /* Clear the incoming stream */ - if (in.data != NULL) - xfree(in.data); - in.p = NULL; - in.end = NULL; - in.data = NULL; - in.size = 0; - in.iso_hdr = NULL; - in.mcs_hdr = NULL; - in.sec_hdr = NULL; - in.rdp_hdr = NULL; - in.channel_hdr = NULL; - - /* Clear the outgoing stream */ - if (out.data != NULL) - xfree(out.data); - out.p = NULL; - out.end = NULL; - out.data = NULL; - out.size = 0; - out.iso_hdr = NULL; - out.mcs_hdr = NULL; - out.sec_hdr = NULL; - out.rdp_hdr = NULL; - out.channel_hdr = NULL; -} diff --git a/uirdesktop/types.h b/uirdesktop/types.h deleted file mode 100644 index f8b62821..00000000 --- a/uirdesktop/types.h +++ /dev/null @@ -1,270 +0,0 @@ -/* - rdesktop: A Remote Desktop Protocol client. - Common data types - Copyright (C) Matthew Chapman 1999-2005 - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -*/ - -typedef int BOOL; - -#ifndef True -#define True (1) -#define False (0) -#endif - -typedef unsigned char uint8; -typedef signed char sint8; -typedef unsigned short uint16; -typedef signed short sint16; -typedef unsigned int uint32; -typedef signed int sint32; - -typedef void *RD_HBITMAP; -typedef void *RD_HGLYPH; -typedef void *RD_HCOLOURMAP; -typedef void *RD_HCURSOR; - -typedef struct _RD_POINT -{ - sint16 x, y; -} -RD_POINT; - -typedef struct _COLOURENTRY -{ - uint8 red; - uint8 green; - uint8 blue; - -} -COLOURENTRY; - -typedef struct _COLOURMAP -{ - uint16 ncolours; - COLOURENTRY *colours; - -} -COLOURMAP; - -typedef struct _BOUNDS -{ - sint16 left; - sint16 top; - sint16 right; - sint16 bottom; - -} -BOUNDS; - -typedef struct _PEN -{ - uint8 style; - uint8 width; - uint32 colour; - -} -PEN; - -typedef struct _BRUSH -{ - uint8 xorigin; - uint8 yorigin; - uint8 style; - uint8 pattern[8]; - -} -BRUSH; - -typedef struct _FONTGLYPH -{ - sint16 offset; - sint16 baseline; - uint16 width; - uint16 height; - RD_HBITMAP pixmap; - -} -FONTGLYPH; - -typedef struct _DATABLOB -{ - void *data; - int size; - -} -DATABLOB; - -typedef struct _key_translation -{ - /* For normal scancode translations */ - uint8 scancode; - uint16 modifiers; - /* For sequences. If keysym is nonzero, the fields above are not used. */ - uint32 seq_keysym; /* Really KeySym */ - struct _key_translation *next; -} -key_translation; - -typedef struct _VCHANNEL -{ - uint16 mcs_id; - char name[8]; - uint32 flags; - struct stream in; - void (*process) (STREAM); -} -VCHANNEL; - -/* PSTCACHE */ -typedef uint8 HASH_KEY[8]; - -/* Header for an entry in the persistent bitmap cache file */ -typedef struct _PSTCACHE_CELLHEADER -{ - HASH_KEY key; - uint8 width, height; - uint16 length; - uint32 stamp; -} -CELLHEADER; - -#define MAX_CBSIZE 256 - -/* RDPSND */ -typedef struct -{ - uint16 wFormatTag; - uint16 nChannels; - uint32 nSamplesPerSec; - uint32 nAvgBytesPerSec; - uint16 nBlockAlign; - uint16 wBitsPerSample; - uint16 cbSize; - uint8 cb[MAX_CBSIZE]; -} RD_WAVEFORMATEX; - -typedef struct _RDPCOMP -{ - uint32 roff; - uint8 hist[RDP_MPPC_DICT_SIZE]; - struct stream ns; -} -RDPCOMP; - -/* RDPDR */ -typedef uint32 RD_NTSTATUS; -typedef uint32 RD_NTHANDLE; - -typedef struct _DEVICE_FNS -{ - RD_NTSTATUS(*create) (uint32 device, uint32 desired_access, uint32 share_mode, - uint32 create_disposition, uint32 flags_and_attributes, char *filename, - RD_NTHANDLE * handle); - RD_NTSTATUS(*close) (RD_NTHANDLE handle); - RD_NTSTATUS(*read) (RD_NTHANDLE handle, uint8 * data, uint32 length, uint32 offset, - uint32 * result); - RD_NTSTATUS(*write) (RD_NTHANDLE handle, uint8 * data, uint32 length, uint32 offset, - uint32 * result); - RD_NTSTATUS(*device_control) (RD_NTHANDLE handle, uint32 request, STREAM in, STREAM out); -} -DEVICE_FNS; - - -typedef struct rdpdr_device_info -{ - uint32 device_type; - RD_NTHANDLE handle; - char name[8]; - char *local_path; - void *pdevice_data; -} -RDPDR_DEVICE; - -typedef struct rdpdr_serial_device_info -{ - int dtr; - int rts; - uint32 control, xonoff, onlimit, offlimit; - uint32 baud_rate, - queue_in_size, - queue_out_size, - wait_mask, - read_interval_timeout, - read_total_timeout_multiplier, - read_total_timeout_constant, - write_total_timeout_multiplier, write_total_timeout_constant, posix_wait_mask; - uint8 stop_bits, parity, word_length; - uint8 chars[6]; - struct termios *ptermios, *pold_termios; - int event_txempty, event_cts, event_dsr, event_rlsd, event_pending; -} -SERIAL_DEVICE; - -typedef struct rdpdr_parallel_device_info -{ - char *driver, *printer; - uint32 queue_in_size, - queue_out_size, - wait_mask, - read_interval_timeout, - read_total_timeout_multiplier, - read_total_timeout_constant, - write_total_timeout_multiplier, - write_total_timeout_constant, posix_wait_mask, bloblen; - uint8 *blob; -} -PARALLEL_DEVICE; - -typedef struct rdpdr_printer_info -{ - FILE *printer_fp; - char *driver, *printer; - uint32 bloblen; - uint8 *blob; - BOOL default_printer; -} -PRINTER; - -typedef struct notify_data -{ - time_t modify_time; - time_t status_time; - time_t total_time; - unsigned int num_entries; -} -NOTIFY; - -#ifndef PATH_MAX -#define PATH_MAX 256 -#endif - -#ifndef _WIN32 -typedef struct fileinfo -{ - uint32 device_id, flags_and_attributes, accessmask; - char path[PATH_MAX]; - DIR *pdir; - struct dirent *pdirent; - char pattern[PATH_MAX]; - BOOL delete_on_close; - NOTIFY notify; - uint32 info_class; -} -FILEINFO; -#endif - -typedef BOOL(*str_handle_lines_t) (const char *line, void *data); diff --git a/uirdesktop/uimain.c b/uirdesktop/uimain.c deleted file mode 100755 index ff68f3b3..00000000 --- a/uirdesktop/uimain.c +++ /dev/null @@ -1,1185 +0,0 @@ -/* -*- c-basic-offset: 8 -*- - rdesktop: A Remote Desktop Protocol client. - Main ui file - Copyright (C) Jay Sorg 2006 - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -*/ - -#include -#include "rdesktop.h" -#include "bsops.h" -#include "uimain.h" - -char g_username[256] = ""; -char g_hostname[256] = ""; -char g_servername[256] = ""; -char g_password[256] = ""; -char g_shell[256] = ""; -char g_directory[256] = ""; -char g_domain[256] = ""; -char * g_bs = 0; /* created in hardware file, used in bsops.c */ -int g_bs_Bpp = 4; -int g_bs_bpp = 32; -BOOL g_desktop_save = False; /* desktop save order */ -BOOL g_polygon_ellipse_orders = False; /* polygon / ellipse orders */ -BOOL g_bitmap_compression = True; -uint32 g_rdp5_performanceflags = - RDP5_NO_WALLPAPER | RDP5_NO_FULLWINDOWDRAG | RDP5_NO_MENUANIMATIONS; -BOOL g_bitmap_cache_persist_enable = False; -BOOL g_bitmap_cache_precache = True; -BOOL g_bitmap_cache = True; -BOOL g_encryption = True; -int g_server_depth = 8; -BOOL g_use_rdp5 = True; -int g_width = 800; -int g_height = 600; -uint32 g_keylayout = 0x409; /* Defaults to US keyboard layout */ -int g_keyboard_type = 0x4; /* Defaults to US keyboard layout */ -int g_keyboard_subtype = 0x0; /* Defaults to US keyboard layout */ -int g_keyboard_functionkeys = 0xc; /* Defaults to US keyboard layout */ -BOOL g_console_session = False; - -/* can't be static, hardware file or bsops need these */ -int g_tcp_sck = 0; -int pal_entries[256]; - -/* Session Directory redirection */ -BOOL g_redirect = False; -char g_redirect_server[64]; -char g_redirect_domain[16]; -char g_redirect_password[64]; -char g_redirect_username[64]; -char g_redirect_cookie[128]; -uint32 g_redirect_flags = 0; - -extern int g_tcp_port_rdp; - -static int g_deactivated = 0; -static uint32 g_ext_disc_reason = 0; - -struct bitmap -{ - uint8 * data; - uint32 width; - uint32 height; -}; - -/* in ui specific file eg win32.c, qt.c, dfb.c, ... */ -int -mi_create_window(void); -void -mi_invalidate(int x, int y, int cx, int cy); -int -mi_create_bs(void); -int -mi_main_loop(void); -void -mi_error(char * msg); -void -mi_warning(char * msg); -void -ui_invalidate(int x, int y, int cx, int cy); -void -mi_paint_rect(char * data, int width, int height, int x, int y, int cx, int cy); -void -mi_begin_update(void); -void -mi_end_update(void); -void -mi_fill_rect(int x, int y, int cx, int cy, int colour); -void -mi_screen_copy(int x, int y, int cx, int cy, int srcx, int srcy); -void -mi_set_clip(int x, int y, int cx, int cy); -void -mi_reset_clip(void); -void -mi_line(int x1, int y1, int x2, int y2, int colour); -void * -mi_create_cursor(unsigned int x, unsigned int y, - int width, int height, - unsigned char * andmask, unsigned char * xormask); -void -mi_destroy_cursor(void * cursor); -void -mi_set_cursor(void * cursor); -void -mi_set_null_cursor(void); -int -mi_read_keyboard_state(void); -void -mi_logon(void); - -/*****************************************************************************/ -/* put part of the screen from the backing store to the display */ -void -ui_invalidate(int x, int y, int cx, int cy) -{ - if (cx < 1 || cy < 1 || g_bs == 0) - { - return; - } - if (bs_warp_coords(&x, &y, &cx, &cy, 0, 0)) - { - mi_invalidate(x, y, cx, cy); - } -/* - if (bs_warp_coords(&x, &y, &cx, &cy, 0, 0)) - { - cx = (cx + 3) & ~3; - data = (char *) xmalloc(cx * cy * 4); - bs_copy_box(data, x, y, cx, cy, cx * ((g_server_depth + 7) / 8)); - mi_paint_rect(data, cx, cy, x, y, cx, cy); - xfree(data); - } -*/ -} - -/*****************************************************************************/ -static int -convert_colour(int in_colour) -{ - int r; - int g; - int b; - - r = 0; - g = 0; - b = 0; - if (g_server_depth == 8) - { - r = (pal_entries[in_colour & 0xff] & 0xff0000) >> 16; - g = (pal_entries[in_colour & 0xff] & 0xff00) >> 8; - b = pal_entries[in_colour & 0xff] & 0xff; - } - else if (g_server_depth == 15) - { - SPLIT_COLOUR15(in_colour, r, g, b); - } - else if (g_server_depth == 16) - { - SPLIT_COLOUR16(in_colour, r, g, b); - } - else if (g_server_depth == 24) - { - SPLIT_COLOUR32(in_colour, r, g, b); - } - if (g_bs_bpp == 32) - { - MAKE_COLOUR32(in_colour, r, g, b); - } - return in_colour; -} - -/*****************************************************************************/ -void -ui_bell(void) -{ -} - -/*****************************************************************************/ -int -ui_select(int in) -{ - if (g_tcp_sck == 0) - { - g_tcp_sck = in; - } - return 1; -} - -/*****************************************************************************/ -void * -ui_create_cursor(uint32 x, uint32 y, - int width, int height, - uint8 * andmask, uint8 * xormask) -{ - int i; - int j; - char am[32 * 4]; - char xm[32 * 4]; - - if (width != 32 || height != 32) - { - return 0; - } - memset(am, 0, 32 * 4); - memset(xm, 0, 32 * 4); - for (i = 0; i < 32; i++) - { - for (j = 0; j < 32; j++) - { - if (bs_is_pixel_on(andmask, j, i, 32, 1)) - { - bs_set_pixel_on(am, j, 31 - i, 32, 1, 1); - } - if (bs_is_pixel_on(xormask, j, i, 32, 24)) - { - bs_set_pixel_on(xm, j, 31 - i, 32, 1, 1); - } - } - } - return (void *) mi_create_cursor(x, y, width, height, am, xm); -} - -/*****************************************************************************/ -void -ui_destroy_cursor(void * cursor) -{ - mi_destroy_cursor(cursor); -} - -/*****************************************************************************/ -void -ui_set_cursor(void * cursor) -{ - mi_set_cursor(cursor); -} - -/*****************************************************************************/ -void -ui_set_null_cursor(void) -{ - mi_set_null_cursor(); -} - -/*****************************************************************************/ -void * -ui_create_glyph(int width, int height, uint8 * data) -{ - int i; - int j; - char * glyph_data; - struct bitmap * the_glyph; - - glyph_data = (char *) xmalloc(width * height); - memset(glyph_data, 0, width * height); - the_glyph = (struct bitmap *) xmalloc(sizeof(struct bitmap)); - memset(the_glyph, 0, sizeof(struct bitmap)); - the_glyph->width = width; - the_glyph->height = height; - the_glyph->data = glyph_data; - for (i = 0; i < height; i++) - { - for (j = 0; j < width; j++) - { - if (bs_is_pixel_on(data, j, i, width, 1)) - { - bs_set_pixel_on(glyph_data, j, i, width, 8, 255); - } - } - } - return the_glyph; -} - -/*****************************************************************************/ -void -ui_destroy_glyph(void * glyph) -{ - struct bitmap * the_glyph; - - the_glyph = glyph; - if (the_glyph != 0) - { - xfree(the_glyph->data); - } - xfree(the_glyph); -} - -/*****************************************************************************/ -void * -ui_create_bitmap(int width, int height, uint8 * data) -{ -#if 0 - struct bitmap * b; - int size; - - size = width * height * ((g_server_depth + 7) / 8); - b = (struct bitmap *) xmalloc(sizeof(struct bitmap)); - b->data = (uint8 *) xmalloc(size); - memcpy(b->data, data, size); - b->width = width; - b->height = height; - return b; -#endif - return 0; -} - -/*****************************************************************************/ -void * -ui_create_bitmap_ex(int width, int height, uint8 * data, int data_size, - int compressed) -{ - struct bitmap * b; - int size; - int i; - int j; - int pixel; - int red; - int green; - int blue; - unsigned short * s16; - unsigned char * s8; - unsigned int * d32; - - size = width * height * 4; - b = (struct bitmap *) xmalloc(sizeof(struct bitmap)); - b->data = (uint8 *) xmalloc(size); - if (compressed) - { - bitmap_decompress_ex(b->data, width, height, data, data_size, - g_server_depth, 32); - } - else - { - if (g_server_depth == 16 && g_bs_bpp == 32) - { - for (i = 0; i < height; i++) - { - s16 = ((unsigned short *) data) + (i * width); - d32 = ((unsigned int *) b->data) + (height - i - 1) * width; - for (j = 0; j < width; j++) - { - pixel = *s16; - s16++; - SPLIT_COLOUR16(pixel, red, green, blue); - MAKE_COLOUR32(pixel, red, green, blue); - *d32 = pixel; - d32++; - } - } - } - else if (g_server_depth == 24 && g_bs_bpp == 32) - { - for (i = 0; i < height; i++) - { - s8 = ((unsigned char *) data) + (i * width * 3); - d32 = ((unsigned int *) b->data) + (height - i - 1) * width; - for (j = 0; j < width; j++) - { - blue = *s8; - s8++; - green = *s8; - s8++; - red = *s8; - s8++; - MAKE_COLOUR32(pixel, red, green, blue); - *d32 = pixel; - d32++; - } - } - } - } - b->width = width; - b->height = height; - return b; -} - -/*****************************************************************************/ -void -ui_destroy_bitmap(void * bmp) -{ - struct bitmap * b; - - b = (struct bitmap *) bmp; - if (b != 0) - { - xfree(b->data); - } - xfree(b); -} - -/*****************************************************************************/ -void -ui_paint_bitmap(int x, int y, int cx, int cy, - int width, int height, uint8 * data) -{ -/* struct bitmap b; - - b.width = width; - b.height = height; - b.data = data; - ui_memblt(12, x, y, cx, cy, &b, 0, 0); */ -} - -/*****************************************************************************/ -void -ui_paint_bitmap_ex(int x, int y, int cx, int cy, - int width, int height, uint8 * data, int data_size, - int compressed) -{ - struct bitmap * b; - - b = ui_create_bitmap_ex(width, height, data, data_size, compressed); - ui_memblt(12, x, y, cx, cy, b, 0, 0); - ui_destroy_bitmap(b); -} - -/*****************************************************************************/ -void -ui_set_clip(int x, int y, int cx, int cy) -{ - bs_set_clip(x, y, cx, cy); - mi_set_clip(x, y, cx, cy); -} - -/*****************************************************************************/ -void -ui_reset_clip(void) -{ - bs_reset_clip(); - mi_reset_clip(); -} - -/*****************************************************************************/ -void * -ui_create_colourmap(COLOURMAP * colours) -{ - int i; - int n; - - n = MIN(256, colours->ncolours); - memset(pal_entries, 0, sizeof(pal_entries)); - for (i = 0; i < n; i++) - { - pal_entries[i] = (colours->colours[i].red << 16) | - (colours->colours[i].green << 8) | - colours->colours[i].blue; - } - return 0; -} - -/*****************************************************************************/ -void -ui_set_colourmap(void * map) -{ -} - -/*****************************************************************************/ -/* don't convert colour here */ -static void -draw_glyph(int x, int y, void * glyph, int fgcolor) -{ - struct bitmap * b; - - b = glyph; - bs_draw_glyph(x, y, b->data, b->width, b->height, fgcolor); -} - -/*****************************************************************************/ -#define DO_GLYPH(ttext,idx) \ -{ \ - glyph = cache_get_font(font, ttext[idx]); \ - if (!(flags & TEXT2_IMPLICIT_X)) \ - { \ - xyoffset = ttext[++idx]; \ - if (xyoffset & 0x80) \ - { \ - if (flags & TEXT2_VERTICAL) \ - { \ - y += ttext[idx + 1] | (ttext[idx + 2] << 8); \ - } \ - else \ - { \ - x += ttext[idx + 1] | (ttext[idx + 2] << 8); \ - } \ - idx += 2; \ - } \ - else \ - { \ - if (flags & TEXT2_VERTICAL) \ - { \ - y += xyoffset; \ - } \ - else \ - { \ - x += xyoffset; \ - } \ - } \ - } \ - if (glyph != NULL) \ - { \ - draw_glyph(x + glyph->offset, y + glyph->baseline, glyph->pixmap, \ - fgcolour); \ - if (flags & TEXT2_IMPLICIT_X) \ - { \ - x += glyph->width; \ - } \ - } \ -} - -/*****************************************************************************/ -void -ui_draw_text(uint8 font, uint8 flags, uint8 opcode, int mixmode, - int x, int y, - int clipx, int clipy, int clipcx, int clipcy, - int boxx, int boxy, int boxcx, int boxcy, BRUSH * brush, - int bgcolour, int fgcolour, uint8 * text, uint8 length) -{ - int i; - int j; - int xyoffset; - DATABLOB * entry; - FONTGLYPH * glyph; - - fgcolour = convert_colour(fgcolour); - bgcolour = convert_colour(bgcolour); - if (boxx + boxcx > g_width) - { - boxcx = g_width - boxx; - } - if (boxcx > 1) - { - bs_rect(boxx, boxy, boxcx, boxcy, bgcolour, 0xc); - } - else - { - if (mixmode == MIX_OPAQUE) - { - bs_rect(clipx, clipy, clipcx, clipcy, bgcolour, 0xc); - } - } - /* Paint text, character by character */ - for (i = 0; i < length;) - { - switch (text[i]) - { - case 0xff: - if (i + 2 < length) - { - cache_put_text(text[i + 1], text, text[i + 2]); - } - else - { - error("this shouldn't be happening\n"); - exit(1); - } - /* this will move pointer from start to first character after */ - /* FF command */ - length -= i + 3; - text = &(text[i + 3]); - i = 0; - break; - case 0xfe: - entry = cache_get_text(text[i + 1]); - if (entry != NULL) - { - if ((((uint8 *) (entry->data))[1] == 0) && - (!(flags & TEXT2_IMPLICIT_X))) - { - if (flags & TEXT2_VERTICAL) - { - y += text[i + 2]; - } - else - { - x += text[i + 2]; - } - } - for (j = 0; j < entry->size; j++) - { - DO_GLYPH(((uint8 *) (entry->data)), j); - } - } - if (i + 2 < length) - { - i += 3; - } - else - { - i += 2; - } - length -= i; - /* this will move pointer from start to first character after */ - /* FE command */ - text = &(text[i]); - i = 0; - break; - default: - DO_GLYPH(text, i); - i++; - break; - } - } - if (boxcx > 1) - { - ui_invalidate(boxx, boxy, boxcx, boxcy); - } - else - { - ui_invalidate(clipx, clipy, clipcx, clipcy); - } -} - -/*****************************************************************************/ -void -ui_line(uint8 opcode, int startx, int starty, int endx, int endy, - PEN * pen) -{ - int x; - int y; - int cx; - int cy; - int colour; - - colour = convert_colour(pen->colour); - - bs_line(opcode, startx, starty, endx, endy, pen->width, pen->style, - colour); - if (pen->style == 0 && pen->width < 2 && opcode == 12) - { - mi_line(startx, starty, endx, endy, colour); - } - else - { - x = MIN(startx, endx); - y = MIN(starty, endy); - cx = (MAX(startx, endx) + 1) - x; - cy = (MAX(starty, endy) + 1) - y; - ui_invalidate(x, y, cx, cy); - } -} - -/*****************************************************************************/ -void -ui_triblt(uint8 opcode, int x, int y, int cx, int cy, - void * src, int srcx, int srcy, - BRUSH* brush, int bgcolour, int fgcolour) -{ - /* not used */ -} - -/*****************************************************************************/ -void -ui_memblt(uint8 opcode, int x, int y, int cx, int cy, - void * src, int srcx, int srcy) -{ - struct bitmap* b; - - b = (struct bitmap *) src; - if (bs_memblt(opcode, x, y, cx, cy, b->data, b->width, b->height, - srcx, srcy)) - { - ui_invalidate(x, y, cx, cy); - } - else - { - //printf("skipped %d %d\r\n", cx, cy); - } -} - -/*****************************************************************************/ -void -ui_desktop_restore(uint32 offset, int x, int y, int cx, int cy) -{ -} - -/*****************************************************************************/ -void -ui_desktop_save(uint32 offset, int x, int y, int cx, int cy) -{ -} - -/*****************************************************************************/ -void -ui_rect(int x, int y, int cx, int cy, int colour) -{ - colour = convert_colour(colour); - bs_rect(x, y, cx, cy, colour, 12); - mi_fill_rect(x, y, cx, cy, colour); -} - -/*****************************************************************************/ -void -ui_screenblt(uint8 opcode, int x, int y, int cx, int cy, - int srcx, int srcy) -{ - bs_screenblt(opcode, x, y, cx, cy, srcx, srcy); - if (opcode == 12) - { - mi_screen_copy(x, y, cx, cy, srcx, srcy); - } - else - { - ui_invalidate(x, y, cx, cy); - } -} - -/*****************************************************************************/ -void -ui_patblt(uint8 opcode, int x, int y, int cx, int cy, - BRUSH * brush, int bgcolour, int fgcolour) -{ - bgcolour = convert_colour(bgcolour); - fgcolour = convert_colour(fgcolour); - bs_patblt(opcode, x, y, cx, cy, brush->style, brush->pattern, - brush->xorigin, brush->yorigin, bgcolour, fgcolour); - ui_invalidate(x, y, cx, cy); -} - -/*****************************************************************************/ -void -ui_destblt(uint8 opcode, int x, int y, int cx, int cy) -{ - bs_rect(x, y, cx, cy, 0, opcode); - ui_invalidate(x, y, cx, cy); - /* todo */ -} - -/*****************************************************************************/ -void -ui_move_pointer(int x, int y) -{ -} - -/*****************************************************************************/ -uint16 -ui_get_numlock_state(uint32 state) -{ - return (uint16) state; -} - -/*****************************************************************************/ -/* get the num, caps, and scroll lock state */ -/* scroll lock is 1, num lock is 2 and caps lock is 4 */ -/* just returning 0, the hardware specific file is responsable for this */ -uint32 -read_keyboard_state(void) -{ - return (uint32) mi_read_keyboard_state(); -} - -/*****************************************************************************/ -void -ui_set_modifier_state(int code) - -{ - - //error("%8.8x", code); - - rdp_send_input(0, RDP_INPUT_SYNCHRONIZE, 0, (uint16) code, 0); - -} - -/*****************************************************************************/ -void -ui_resize_window(void) -{ -} - -/*****************************************************************************/ -void -ui_begin_update(void) -{ - mi_begin_update(); -} - -/*****************************************************************************/ -void -ui_end_update(void) -{ - mi_end_update(); -} - -/*****************************************************************************/ -void -ui_polygon(uint8 opcode, uint8 fillmode, RD_POINT * point, int npoints, - BRUSH * brush, int bgcolour, int fgcolour) -{ - /* not used */ -} - -/*****************************************************************************/ -void -ui_polyline(uint8 opcode, RD_POINT * points, int npoints, PEN * pen) -{ - int i, x, y, dx, dy; - if (npoints > 0) - { - x = points[0].x; - y = points[0].y; - for (i = 1; i < npoints; i++) - { - dx = points[i].x; - dy = points[i].y; - ui_line(opcode, x, y, x + dx, y + dy, pen); - x = x + dx; - y = y + dy; - } - } -} - -/*****************************************************************************/ -void -ui_ellipse(uint8 opcode, uint8 fillmode, - int x, int y, int cx, int cy, - BRUSH * brush, int bgcolour, int fgcolour) -{ - /* not used */ -} - -/*****************************************************************************/ -void -ui_logon(void) -{ - mi_logon(); -} - -/*****************************************************************************/ -/* get a 32 byte random */ -void -generate_random(uint8 * random) -{ - int i; - - rand(); - rand(); - for (i = 0; i < 32; i++) - { - random[i] = rand() >> 16; /* higher bits are more random */ - } -} - -/*****************************************************************************/ -void -save_licence(uint8 * data, int length) -{ -} - -/*****************************************************************************/ -int -load_licence(uint8 ** data) -{ - return 0; -} - -/*****************************************************************************/ -void * -xrealloc(void * in, int size) -{ - if (size < 1) - { - size = 1; - } - return realloc(in, size); -} - -/*****************************************************************************/ -void * -xmalloc(int size) -{ - if (size < 1) - { - size = 1; - } - return malloc(size); -} - -/*****************************************************************************/ -void -xfree(void * in) -{ - if (in != 0) - { - free(in); - } -} - -/*****************************************************************************/ -char * -xstrdup(const char * s) -{ - int len; - char * p; - - if (s == 0) - { - return 0; - } - len = strlen(s); - p = (char *) xmalloc(len + 1); - strcpy(p, s); - return p; -} - -/*****************************************************************************/ -void -warning(char * format, ...) -{ - va_list ap; - char text[512]; - char text1[512]; - - sprintf(text1, "WARNING: "); - va_start(ap, format); - vsprintf(text, format, ap); - va_end(ap); - strcat(text1, text); - mi_warning(text1); -} - -/*****************************************************************************/ -void -unimpl(char * format, ...) -{ - va_list ap; - char text[512]; - char text1[512]; - - sprintf(text1, "UNIMPL: "); - va_start(ap, format); - vsprintf(text, format, ap); - va_end(ap); - strcat(text1, text); - mi_warning(text1); -} - -/*****************************************************************************/ -void -error(char * format, ...) -{ - va_list ap; - char text[512]; - char text1[512]; - - sprintf(text1, "ERROR: "); - va_start(ap, format); - vsprintf(text, format, ap); - va_end(ap); - strcat(text1, text); - mi_error(text1); -} - -/*****************************************************************************/ -BOOL -rd_pstcache_mkdir(void) -{ - return 0; -} - -/*****************************************************************************/ -int -rd_open_file(char * filename) -{ - return 0; -} - -/*****************************************************************************/ -void -rd_close_file(int fd) -{ - return; -} - -/*****************************************************************************/ -int -rd_read_file(int fd, void * ptr, int len) -{ - return 0; -} - -/*****************************************************************************/ -int -rd_write_file(int fd, void * ptr, int len) -{ - return 0; -} - -/*****************************************************************************/ -int -rd_lseek_file(int fd, int offset) -{ - return 0; -} - -/*****************************************************************************/ -BOOL -rd_lock_file(int fd, int start, int len) -{ - return False; -} - - -/*****************************************************************************/ -void -ui_mouse_move(int x, int y) -{ - rdp_send_input(0, RDP_INPUT_MOUSE, MOUSE_FLAG_MOVE, (uint16) x, (uint16) y); -} - - -/*****************************************************************************/ -void -ui_mouse_button(int button, int x, int y, int down) -{ - uint16 flags; - - flags = 0; - if (down) - { - flags |= MOUSE_FLAG_DOWN; - } - switch (button) - { - case 1: - flags |= MOUSE_FLAG_BUTTON1; - break; - case 2: - flags |= MOUSE_FLAG_BUTTON2; - break; - case 3: - flags |= MOUSE_FLAG_BUTTON3; - break; - case 4: - flags |= MOUSE_FLAG_BUTTON4; - break; - case 5: - flags |= MOUSE_FLAG_BUTTON5; - break; - } - rdp_send_input(0, RDP_INPUT_MOUSE, flags, (uint16) x, (uint16) y); -} - - -/*****************************************************************************/ -void -ui_key_down(int key, int ext) - -{ - rdp_send_input(0, RDP_INPUT_SCANCODE, (uint16) (RDP_KEYPRESS | ext), - (uint16) key, 0); -} - -/*****************************************************************************/ -void -ui_key_up(int key, int ext) -{ - rdp_send_input(0, RDP_INPUT_SCANCODE, (uint16) (RDP_KEYRELEASE | ext), - (uint16) key, 0); -} - -/*****************************************************************************/ -/* returns boolean, non zero is good */ -int -ui_read_wire(void) -{ - return rdp_loop(&g_deactivated, &g_ext_disc_reason); -} - -/*****************************************************************************/ -/* called after the command line parameters are processed */ -/* returns boolean, non zero is ok */ -int -ui_main(void) -{ - uint32 flags; - - /* try to connect */ - flags = RDP_LOGON_NORMAL; - if (g_password[0] != 0) - { - flags |= RDP_LOGON_AUTO; - } - if (!rdp_connect(g_servername, flags, g_domain, g_password, - g_shell, g_directory)) - { - return 0; - } - /* create the window */ - if (!mi_create_window()) - { - error("mi_create_window failed\r\n"); - return 0; - } - /* create backingstore stuff for use in bsops.c */ - if (!mi_create_bs()) - { - error("mi_create_bs failed\r\n"); - return 0; - } - /* init backingstore */ - bs_init(); - /* if all ok, enter main loop */ - return mi_main_loop(); -} - -/*****************************************************************************/ -/* called after the command line parameters are processed */ -/* returns boolean, non zero is ok */ -int -ui_lib_main(void) -{ - uint32 flags; - - /* try to connect */ - flags = RDP_LOGON_NORMAL; - if (g_password[0] != 0) - { - flags |= RDP_LOGON_AUTO; - } - if (!rdp_connect(g_servername, flags, g_domain, g_password, - g_shell, g_directory)) - { - return 0; - } - /* create the window */ - if (!mi_create_window()) - { - error("mi_create_window failed\r\n"); - return 0; - } - /* create backingstore stuff for use in bsops.c */ - if (!mi_create_bs()) - { - error("mi_create_bs failed\r\n"); - return 0; - } - /* init backingstore */ - bs_init(); - /* if all ok, enter main loop */ - return 1; -} - -/*****************************************************************************/ -/* produce a hex dump */ -void -hexdump(uint8 * p, uint32 len) -{ - uint8 * line = p; - int i, thisline, offset = 0; - - while (offset < (int)len) - { - printf("%04x ", offset); - thisline = len - offset; - if (thisline > 16) - thisline = 16; - - for (i = 0; i < thisline; i++) - printf("%02x ", line[i]); - - for (; i < 16; i++) - printf(" "); - - for (i = 0; i < thisline; i++) - printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.'); - - printf("\n"); - offset += thisline; - line += thisline; - } -} - diff --git a/uirdesktop/uimain.h b/uirdesktop/uimain.h deleted file mode 100755 index 6c098746..00000000 --- a/uirdesktop/uimain.h +++ /dev/null @@ -1,96 +0,0 @@ -/* -*- c-basic-offset: 8 -*- - rdesktop: A Remote Desktop Protocol client. - main ui header - Copyright (C) Jay Sorg 2005-2006 - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -*/ - -/* in uimain.c */ -int -ui_main(void); -int -ui_lib_main(void); -void -ui_invalidate(int x, int y, int cx, int cy); -int -ui_read_wire(void); -void -ui_mouse_move(int x, int y); -void -ui_mouse_button(int button, int x, int y, int down); -void -ui_key_down(int key, int ext); -void -ui_key_up(int key, int ext); - -void - -ui_set_modifier_state(int code); - -#define SPLIT_COLOUR15(c, r, g, b) \ -{ \ - r = ((c >> 7) & 0xf8) | ((c >> 12) & 0x7); \ - g = ((c >> 2) & 0xf8) | ((c >> 8) & 0x7); \ - b = ((c << 3) & 0xf8) | ((c >> 2) & 0x7); \ -} - -#define SPLIT_COLOUR16(c, r, g, b) \ -{ \ - r = ((c >> 8) & 0xf8) | ((c >> 13) & 0x7); \ - g = ((c >> 3) & 0xfc) | ((c >> 9) & 0x3); \ - b = ((c << 3) & 0xf8) | ((c >> 2) & 0x7); \ -} - -#define SPLIT_COLOUR32(c, r, g, b) \ -{ \ - r = ((c >> 0) & 0xff); \ - g = ((c >> 8) & 0xff); \ - b = ((c >> 16) & 0xff); \ -} - -#define MAKE_COLOUR15(c, r, g, b) \ -{ \ - c = ( \ - (((r & 0xff) >> 3) << 10) | \ - (((g & 0xff) >> 3) << 5) | \ - (((b & 0xff) >> 3) << 0) \ - ); \ -} - -#define MAKE_COLOUR16(c, r, g, b) \ -{ \ - c = ( \ - (((r & 0xff) >> 3) << 11) | \ - (((g & 0xff) >> 2) << 5) | \ - (((b & 0xff) >> 3) << 0) \ - ); \ -} - -#define MAKE_COLOUR32(c, r, g, b) \ -{ \ - c = ( \ - ((r & 0xff) << 16) | \ - ((g & 0xff) << 8) | \ - ((b & 0xff) << 0) \ - ); \ -} - -#undef UI_MAX -#define UI_MAX(a, b) (((a) > (b)) ? (a) : (b)) -#undef UI_MIN -#define UI_MIN(a, b) (((a) < (b)) ? (a) : (b)) - - diff --git a/uirdesktop/win32.c b/uirdesktop/win32.c deleted file mode 100755 index b7af58f4..00000000 --- a/uirdesktop/win32.c +++ /dev/null @@ -1,1733 +0,0 @@ -/* -*- c-basic-offset: 8 -*- - rdesktop: A Remote Desktop Protocol client. - win32 calls - Copyright (C) Jay Sorg 2006 - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. -*/ - -#ifdef _WIN32_WCE -#define MYWINCE -#endif - -#include /* winsock2.h first */ -#include -#ifdef MYWINCE -#include /* aygshell.lib */ -#endif /* MYWINCE */ -#include -#include -#include "uimain.h" - -extern char g_username[]; -extern char g_hostname[]; -extern char g_servername[]; -extern char g_password[]; -extern char g_shell[]; -extern char g_directory[]; -extern char g_domain[]; -extern int g_width; -extern int g_height; -extern int g_tcp_sck; -extern int g_server_depth; -extern int g_tcp_port_rdp; /* in tcp.c */ -extern int pal_entries[]; -extern char * g_bs; -extern int g_bs_bpp; -extern int g_bs_Bpp; - -static HWND g_Wnd = 0; -static HINSTANCE g_Instance = 0; -static HCURSOR g_cursor = 0; -static HBITMAP g_bs_bitmap = 0; -static int g_block = 0; -static int g_xoff = 0; /* offset from window to client coords */ -static int g_yoff = 0; -static int g_xscroll = 0; /* current scroll position */ -static int g_yscroll = 0; -static int g_screen_width = 0; -static int g_screen_height = 0; -static int g_wnd_cwidth = 0; /* set from WM_SIZE */ -static int g_wnd_cheight = 0; -static int g_fullscreen = 0; -static int g_workarea = 0; -static int g_mousex = 0; /* in client coords */ -static int g_mousey = 0; -static int g_width_height_set = 0; - -static int g_clip_left = 0; -static int g_clip_top = 0; -static int g_clip_right = 800; -static int g_clip_bottom = 600; -static RECT g_wnd_clip; /* this client area of whats actually visable */ - /* set from WM_SIZE */ -#ifdef MYWINCE -static int g_sip_up = 0; -#endif - -/*****************************************************************************/ -static void -str_to_uni(TCHAR * sizex, char * size1) -{ - int len; - int i; - - len = strlen(size1); - for (i = 0; i < len; i++) - { - sizex[i] = size1[i]; - } - sizex[len] = 0; -} - -/*****************************************************************************/ -/* returns non zero if it processed something */ -static int -check_sck(void) -{ - fd_set rfds; - struct timeval tm; - int count; - int rv; - - rv = 0; - if (g_block == 0) - { - g_block = 1; - /* see if there really is data */ - FD_ZERO(&rfds); - FD_SET((unsigned int)g_tcp_sck, &rfds); - ZeroMemory(&tm, sizeof(tm)); - count = select(g_tcp_sck + 1, &rfds, 0, 0, &tm); - if (count > 0) - { - if (ui_read_wire()) - { - rv = 1; - } - else - { - PostQuitMessage(0); - } - } - g_block = 0; - } - return rv; -} - -/*****************************************************************************/ -void -mi_error(char * msg) -{ -#ifdef WITH_DEBUG - printf(msg); -#else /* WITH_DEBUG */ - TCHAR lmsg[512]; - TCHAR ltitle[512]; - - str_to_uni(lmsg, msg); - str_to_uni(ltitle, "Error"); - MessageBox(g_Wnd, lmsg, ltitle, MB_OK); -#endif /* WITH_DEBUG */ -} - -/*****************************************************************************/ -static int -get_scan_code_from_ascii(int code) -{ - int rv; - - rv = 0; - switch (code & 0xff) - { - case 0x30: rv = 0x0b; break; // 0 - case 0x31: rv = 0x02; break; // 1 - case 0x32: rv = 0x03; break; // 2 - case 0x33: rv = 0x04; break; // 3 - case 0x34: rv = 0x05; break; // 4 - case 0x35: rv = 0x06; break; // 5 - case 0x36: rv = 0x07; break; // 6 - case 0x37: rv = 0x08; break; // 7 - case 0x38: rv = 0x09; break; // 8 - case 0x39: rv = 0x0a; break; // 9 - - case 0xbd: rv = 0x0c; break; // - - case 0xbb: rv = 0x0d; break; // = - case 0x08: rv = 0x0e; break; // backspace - case 0x09: rv = 0x0f; break; // tab - case 0xdb: rv = 0x1b; break; // ] - case 0xdd: rv = 0x1a; break; // [ - case 0x14: rv = 0x3a; break; // capslock - case 0xba: rv = 0x27; break; // ; - case 0xde: rv = 0x28; break; // ' - case 0x10: rv = 0x2a; break; // shift - case 0xbc: rv = 0x33; break; // , - case 0xbe: rv = 0x34; break; // . - case 0xbf: rv = 0x35; break; // / - case 0x0d: rv = 0x1c; break; // enter - case 0x27: rv = 0x4d; break; // arrow right - case 0x25: rv = 0x4b; break; // arrow left - case 0x26: rv = 0x48; break; // arrow up - case 0x28: rv = 0x50; break; // arrow down - case 0x20: rv = 0x39; break; // space - case 0xdc: rv = 0x2b; break; // backslash - case 0xc0: rv = 0x29; break; // ` - case 0x11: rv = 0x1d; break; // ctl - - case 0x41: rv = 0x1e; break; // a - case 0x42: rv = 0x30; break; // b - case 0x43: rv = 0x2e; break; // c - case 0x44: rv = 0x20; break; // d - case 0x45: rv = 0x12; break; // e - case 0x46: rv = 0x21; break; // f - case 0x47: rv = 0x22; break; // g - case 0x48: rv = 0x23; break; // h - case 0x49: rv = 0x17; break; // i - case 0x4a: rv = 0x24; break; // j - case 0x4b: rv = 0x25; break; // k - case 0x4c: rv = 0x26; break; // l - case 0x4d: rv = 0x32; break; // m - case 0x4e: rv = 0x31; break; // n - case 0x4f: rv = 0x18; break; // o - case 0x50: rv = 0x19; break; // p - case 0x51: rv = 0x10; break; // q - case 0x52: rv = 0x13; break; // r - case 0x53: rv = 0x1f; break; // s - case 0x54: rv = 0x14; break; // t - case 0x55: rv = 0x16; break; // u - case 0x56: rv = 0x2f; break; // v - case 0x57: rv = 0x11; break; // w - case 0x58: rv = 0x2d; break; // x - case 0x59: rv = 0x15; break; // y - case 0x5a: rv = 0x2c; break; // z - } - return rv; -} - -/*****************************************************************************/ -static void -mi_scroll(int dx, int dy) -{ - HRGN rgn; - - rgn = CreateRectRgn(0, 0, 0, 0); - ScrollWindowEx(g_Wnd, dx, dy, 0, 0, rgn, 0, SW_ERASE); - InvalidateRgn(g_Wnd, rgn, 0); - DeleteObject(rgn); -} - -/*****************************************************************************/ -int -mi_read_keyboard_state(void) -{ - short keydata; - int code; - - code = 0; - keydata = GetKeyState(VK_SCROLL); - if (keydata & 0x0001) - { - code |= 1; - } - keydata = GetKeyState(VK_NUMLOCK); - if (keydata & 0x0001) - { - code |= 2; - } - keydata = GetKeyState(VK_CAPITAL); - if (keydata & 0x0001) - { - code |= 4; - } - return code; -} - -/*****************************************************************************/ -static void -mi_check_modifier(void) -{ - int code; - - code = mi_read_keyboard_state(); - ui_set_modifier_state(code); -} - -/*****************************************************************************/ -static LRESULT -handle_WM_SETCURSOR(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) -{ - if (g_mousex >= g_wnd_clip.left && - g_mousey >= g_wnd_clip.top && - g_mousex < g_wnd_clip.right && - g_mousey < g_wnd_clip.bottom) - { - SetCursor(g_cursor); - } - /* need default behavoir here */ - return DefWindowProc(hWnd, message, wParam, lParam); -} - -/*****************************************************************************/ -static LRESULT -handle_WM_NCHITTEST(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) -{ - POINT pt; - - pt.x = LOWORD(lParam); - pt.y = HIWORD(lParam); - if (ScreenToClient(g_Wnd, &pt)) - { - g_mousex = pt.x; - g_mousey = pt.y; - } - return DefWindowProc(hWnd, message, wParam, lParam); -} - -/*****************************************************************************/ -static LRESULT -handle_WM_MOUSEMOVE(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) -{ - g_mousex = LOWORD(lParam); - g_mousey = HIWORD(lParam); - ui_mouse_move(g_mousex + g_xscroll, g_mousey + g_yscroll); - return 0; -} - -/*****************************************************************************/ -static LRESULT -handle_WM_LBUTTONDOWN(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) -{ - g_mousex = LOWORD(lParam); - g_mousey = HIWORD(lParam); - ui_mouse_button(1, g_mousex + g_xscroll, g_mousey + g_yscroll, 1); - return 0; -} - -/*****************************************************************************/ -static LRESULT -handle_WM_LBUTTONUP(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) -{ - g_mousex = LOWORD(lParam); - g_mousey = HIWORD(lParam); - ui_mouse_button(1, g_mousex + g_xscroll, g_mousey + g_yscroll, 0); - return 0; -} - -/*****************************************************************************/ -static LRESULT -handle_WM_RBUTTONDOWN(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) -{ - g_mousex = LOWORD(lParam); - g_mousey = HIWORD(lParam); - ui_mouse_button(2, g_mousex + g_xscroll, g_mousey + g_yscroll, 1); - return 0; -} - -/*****************************************************************************/ -static LRESULT -handle_WM_RBUTTONUP(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) -{ - g_mousex = LOWORD(lParam); - g_mousey = HIWORD(lParam); - ui_mouse_button(2, g_mousex + g_xscroll, g_mousey + g_yscroll, 0); - return 0; -} - -/*****************************************************************************/ -static LRESULT -handle_WM_MBUTTONDOWN(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) -{ - g_mousex = LOWORD(lParam); - g_mousey = HIWORD(lParam); - ui_mouse_button(3, g_mousex + g_xscroll, g_mousey + g_yscroll, 1); - return 0; -} - -/*****************************************************************************/ -static LRESULT -handle_WM_MBUTTONUP(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) -{ - g_mousex = LOWORD(lParam); - g_mousey = HIWORD(lParam); - ui_mouse_button(3, g_mousex + g_xscroll, g_mousey + g_yscroll, 0); - return 0; -} - -/*****************************************************************************/ -static LRESULT -handle_WM_MOUSEWHEEL(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) -{ - int delta; - - delta = ((signed short)HIWORD(wParam)); /* GET_WHEEL_DELTA_WPARAM(wParam); */ - if (delta > 0) - { - ui_mouse_button(4, 0, 0, 1); - ui_mouse_button(4, 0, 0, 0); - } - else - { - ui_mouse_button(5, 0, 0, 1); - ui_mouse_button(5, 0, 0, 0); - } - return 0; -} - -/*****************************************************************************/ -static LRESULT -handle_WM_KEY(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) -{ - int scancode; - int ext; - int down; - - ext = HIWORD(lParam); - scancode = ext; - down = !(ext & 0x8000); - scancode &= 0xff; - if (scancode == 0) - { - scancode = get_scan_code_from_ascii(wParam); - } - ext &= 0x0100; - if (scancode == 0x0045) /* num lock */ - { - ext &= ~0x0100; - } - if (down) - { - ui_key_down(scancode, ext); - } - else - { - ui_key_up(scancode, ext); - } - return 0; -} - -/*****************************************************************************/ -static LRESULT -handle_WM_PAINT(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) -{ - PAINTSTRUCT ps; - RECT rect; - HBRUSH brush; - - BeginPaint(hWnd, &ps); - /* paint the area outside the rdp screen with one colour */ - rect = ps.rcPaint; - rect.left = UI_MAX(rect.left, g_width); - if (!IsRectEmpty(&rect)) - { - brush = CreateSolidBrush(RGB(0, 0, 255)); - FillRect(ps.hdc, &rect, brush); - DeleteObject(brush); - } - rect = ps.rcPaint; - rect.top = UI_MAX(rect.top, g_height); - if (!IsRectEmpty(&rect)) - { - brush = CreateSolidBrush(RGB(0, 0, 255)); - FillRect(ps.hdc, &rect, brush); - DeleteObject(brush); - } - rect = ps.rcPaint; - EndPaint(hWnd, &ps); - ui_invalidate(rect.left + g_xscroll, - rect.top + g_yscroll, - (rect.right - rect.left) + 1, - (rect.bottom - rect.top) + 1); - return 0; -} - -/*****************************************************************************/ -static LRESULT -handle_WM_SIZE(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) -{ - int oldxscroll; - int oldyscroll; - - if (wParam == SIZE_MINIMIZED) - { - return DefWindowProc(hWnd, message, wParam, lParam); - } - g_wnd_cwidth = LOWORD(lParam); /* client width / height */ - g_wnd_cheight = HIWORD(lParam); - g_wnd_clip.left = 0; - g_wnd_clip.top = 0; - g_wnd_clip.right = g_wnd_clip.left + g_wnd_cwidth; - g_wnd_clip.bottom = g_wnd_clip.top + g_wnd_cheight; - if (g_wnd_cwidth < g_width || g_wnd_cheight < g_height) - { - SetScrollRange(g_Wnd, SB_HORZ, 0, g_width - g_wnd_cwidth, 1); - SetScrollRange(g_Wnd, SB_VERT, 0, g_height - g_wnd_cheight, 1); - } - oldxscroll = g_xscroll; - oldyscroll = g_yscroll; - if (g_wnd_cwidth >= g_width) - { - g_xscroll = 0; - } - else - { - g_xscroll = UI_MIN(g_xscroll, g_width - g_wnd_cwidth); - } - if (g_wnd_cheight >= g_height) - { - g_yscroll = 0; - } - else - { - g_yscroll = UI_MIN(g_yscroll, g_height - g_wnd_cheight); - } - mi_scroll(oldxscroll - g_xscroll, oldyscroll - g_yscroll); - if (wParam == SIZE_RESTORED || wParam == SIZE_MAXIMIZED) - { - /* check the caps, num, and scroll lock here */ - mi_check_modifier(); - } - return 0; -} - -/*****************************************************************************/ -static LRESULT -handle_WM_SIZING(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) -{ - LPRECT prect; - int width; - int height; - int style; - - prect = (LPRECT) lParam; /* total window rect */ - width = (prect->right - prect->left) - (g_xoff * 2); - height = (prect->bottom - prect->top) - (g_yoff + g_xoff); - if (height < g_height || width < g_width) - { - style = GetWindowLong(g_Wnd, GWL_STYLE); - if (!(style & WS_HSCROLL)) - { - style |= WS_HSCROLL | WS_VSCROLL; - SetWindowLong(g_Wnd, GWL_STYLE, style); - g_xscroll = 0; - g_yscroll = 0; - SetScrollPos(g_Wnd, SB_HORZ, g_xscroll, 1); - SetScrollPos(g_Wnd, SB_VERT, g_yscroll, 1); - } - } - else if (height >= g_height && width >= g_width) - { - style = GetWindowLong(g_Wnd, GWL_STYLE); - if (style & WS_HSCROLL) - { - style &= ~WS_HSCROLL; - style &= ~WS_VSCROLL; - SetWindowLong(g_Wnd, GWL_STYLE, style); - g_xscroll = 0; - g_yscroll = 0; - } - } - return 0; -} - -/*****************************************************************************/ -static LRESULT -handle_WM_HSCROLL(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) -{ - int code; - int oldxscroll; - - code = (int) LOWORD(wParam); /* scroll bar value */ - if (code == SB_LINELEFT) - { - oldxscroll = g_xscroll; - g_xscroll--; - g_xscroll = UI_MAX(g_xscroll, 0); - SetScrollPos(g_Wnd, SB_HORZ, g_xscroll, 1); - mi_scroll(oldxscroll - g_xscroll, 0); - } - else if (code == SB_LINERIGHT) - { - oldxscroll = g_xscroll; - g_xscroll++; - g_xscroll = UI_MIN(g_xscroll, g_width - g_wnd_cwidth); - SetScrollPos(g_Wnd, SB_HORZ, g_xscroll, 1); - mi_scroll(oldxscroll - g_xscroll, 0); - } - else if (code == SB_PAGELEFT) - { - oldxscroll = g_xscroll; - g_xscroll -= g_wnd_cwidth / 2; - g_xscroll = UI_MAX(g_xscroll, 0); - SetScrollPos(g_Wnd, SB_HORZ, g_xscroll, 1); - mi_scroll(oldxscroll - g_xscroll, 0); - } - else if (code == SB_PAGERIGHT) - { - oldxscroll = g_xscroll; - g_xscroll += g_wnd_cwidth / 2; - g_xscroll = UI_MIN(g_xscroll, g_width - g_wnd_cwidth); - SetScrollPos(g_Wnd, SB_HORZ, g_xscroll, 1); - mi_scroll(oldxscroll - g_xscroll, 0); - } - else if (code == SB_BOTTOM) - { - oldxscroll = g_xscroll; - g_xscroll = g_width - g_wnd_cwidth; - SetScrollPos(g_Wnd, SB_HORZ, g_xscroll, 1); - mi_scroll(oldxscroll - g_xscroll, 0); - } - else if (code == SB_TOP) - { - oldxscroll = g_xscroll; - g_xscroll = 0; - SetScrollPos(g_Wnd, SB_HORZ, g_xscroll, 1); - mi_scroll(oldxscroll - g_xscroll, 0); - } - else if (code == SB_THUMBPOSITION) - { - oldxscroll = g_xscroll; - g_xscroll = (signed short) HIWORD(wParam); - SetScrollPos(g_Wnd, SB_HORZ, g_xscroll, 1); - mi_scroll(oldxscroll - g_xscroll, 0); - } - return 0; -} - -/*****************************************************************************/ -static LRESULT -handle_WM_VSCROLL(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) -{ - int code; - int oldyscroll; - - code = (int) LOWORD(wParam); /* scroll bar value */ - if (code == SB_LINELEFT) - { - oldyscroll = g_yscroll; - g_yscroll--; - g_yscroll = UI_MAX(g_yscroll, 0); - SetScrollPos(g_Wnd, SB_VERT, g_yscroll, 1); - mi_scroll(0, oldyscroll - g_yscroll); - } - else if (code == SB_LINERIGHT) - { - oldyscroll = g_yscroll; - g_yscroll++; - g_yscroll = UI_MIN(g_yscroll, g_height - g_wnd_cheight); - SetScrollPos(g_Wnd, SB_VERT, g_yscroll, 1); - mi_scroll(0, oldyscroll - g_yscroll); - } - else if (code == SB_PAGELEFT) - { - oldyscroll = g_yscroll; - g_yscroll -= g_wnd_cheight / 2; - g_yscroll = UI_MAX(g_yscroll, 0); - SetScrollPos(g_Wnd, SB_VERT, g_yscroll, 1); - mi_scroll(0, oldyscroll - g_yscroll); - } - else if (code == SB_PAGERIGHT) - { - oldyscroll = g_yscroll; - g_yscroll += g_wnd_cheight / 2; - g_yscroll = UI_MIN(g_yscroll, g_height - g_wnd_cheight); - SetScrollPos(g_Wnd, SB_VERT, g_yscroll, 1); - mi_scroll(0, oldyscroll - g_yscroll); - } - else if (code == SB_BOTTOM) - { - oldyscroll = g_yscroll; - g_yscroll = g_height - g_wnd_cheight; - SetScrollPos(g_Wnd, SB_VERT, g_yscroll, 1); - mi_scroll(0, oldyscroll - g_yscroll); - } - else if (code == SB_TOP) - { - oldyscroll = g_yscroll; - g_yscroll = 0; - SetScrollPos(g_Wnd, SB_VERT, g_yscroll, 1); - mi_scroll(0, oldyscroll - g_yscroll); - } - else if (code == SB_THUMBPOSITION) - { - oldyscroll = g_yscroll; - g_yscroll = (signed short) HIWORD(wParam); - SetScrollPos(g_Wnd, SB_VERT, g_yscroll, 1); - mi_scroll(0, oldyscroll - g_yscroll); - } - return 0; -} - -#ifdef MYWINCE -/*****************************************************************************/ -static LRESULT -handle_WM_SETTINGCHANGE(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) -{ - SIPINFO si; - SHMENUBARINFO mb; - int x; - int y; - int w; - int h; - int style; - - ZeroMemory(&si, sizeof(SIPINFO)); - si.cbSize = sizeof(SIPINFO); - SHSipInfo(SPI_GETSIPINFO, lParam, &si, 0); - x = si.rcVisibleDesktop.left; - y = si.rcVisibleDesktop.top; - w = si.rcVisibleDesktop.right - x; - h = si.rcVisibleDesktop.bottom - y; - /* get rid of menu */ - DestroyWindow(SHFindMenuBar(g_Wnd)); - if (si.fdwFlags & SIPF_ON) - { - g_sip_up = 1; /* used for WM_SETFOCUS */ - ZeroMemory(&mb, sizeof(SHMENUBARINFO)); - mb.cbSize = sizeof(SHMENUBARINFO); - mb.hwndParent = g_Wnd; - mb.dwFlags = SHCMBF_EMPTYBAR; - SHCreateMenuBar(&mb); - MoveWindow(g_Wnd, x, y, w, h, FALSE); - SHFullScreen(g_Wnd, SHFS_SHOWTASKBAR | SHFS_SHOWSIPBUTTON | - SHFS_SHOWSTARTICON); - } - else - { - g_sip_up = 0; - if (g_fullscreen) - { - MoveWindow(g_Wnd, 0, 0, g_screen_width, g_screen_height, FALSE); - } - else - { - MoveWindow(g_Wnd, x, y, w, h, FALSE); - } - if ((g_fullscreen && g_width <= g_screen_width && - g_height <= g_screen_height) || - (!g_fullscreen && g_width <= w && g_height <= h)) - { - style = GetWindowLong(g_Wnd, GWL_STYLE); - if (style & WS_HSCROLL) - { - style &= ~WS_HSCROLL; - style &= ~WS_VSCROLL; - SetWindowLong(g_Wnd, GWL_STYLE, style); - g_xscroll = 0; - g_yscroll = 0; - } - } - if (g_fullscreen) - { - SHFullScreen(g_Wnd, SHFS_HIDETASKBAR | SHFS_SHOWSIPBUTTON | - SHFS_SHOWSTARTICON); - } - else - { - SHFullScreen(g_Wnd, SHFS_SHOWTASKBAR | SHFS_SHOWSIPBUTTON | - SHFS_SHOWSTARTICON); - } - } - return 0; -} -#endif /* MYWINCE */ - -/*****************************************************************************/ -LRESULT CALLBACK -WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) -{ - switch (message) - { - case WM_SETCURSOR: - return handle_WM_SETCURSOR(hWnd, message, wParam, lParam); - case 0x0084: /* WinCE don't have this WM_NCHITTEST: */ - return handle_WM_NCHITTEST(hWnd, message, wParam, lParam); - case WM_MOUSEMOVE: - return handle_WM_MOUSEMOVE(hWnd, message, wParam, lParam); - case WM_LBUTTONDOWN: - return handle_WM_LBUTTONDOWN(hWnd, message, wParam, lParam); - case WM_LBUTTONUP: - return handle_WM_LBUTTONUP(hWnd, message, wParam, lParam); - case WM_RBUTTONDOWN: - return handle_WM_RBUTTONDOWN(hWnd, message, wParam, lParam); - case WM_RBUTTONUP: - return handle_WM_RBUTTONUP(hWnd, message, wParam, lParam); - case WM_MBUTTONDOWN: - return handle_WM_MBUTTONDOWN(hWnd, message, wParam, lParam); - case WM_MBUTTONUP: - return handle_WM_MBUTTONUP(hWnd, message, wParam, lParam); - /* some windows compilers don't have these defined like vc6 */ - case 0x020a: /* WM_MOUSEWHEEL: */ - return handle_WM_MOUSEWHEEL(hWnd, message, wParam, lParam); - case WM_KEYDOWN: - case WM_KEYUP: - case WM_SYSKEYDOWN: - case WM_SYSKEYUP: - return handle_WM_KEY(hWnd, message, wParam, lParam); - case WM_CHAR: - case WM_DEADCHAR: - case WM_SYSCHAR: - case WM_SYSDEADCHAR: - break; - case WM_PAINT: - return handle_WM_PAINT(hWnd, message, wParam, lParam); - case WM_DESTROY: - PostQuitMessage(0); - break; - case WM_APP + 1: - case WM_TIMER: - check_sck(); - break; - case WM_SIZE: - return handle_WM_SIZE(hWnd, message, wParam, lParam); - case 532: /* not defined in wince WM_SIZING: */ - return handle_WM_SIZING(hWnd, message, wParam, lParam); - case WM_HSCROLL: - return handle_WM_HSCROLL(hWnd, message, wParam, lParam); - case WM_VSCROLL: - return handle_WM_VSCROLL(hWnd, message, wParam, lParam); -#ifdef MYWINCE - case WM_SETTINGCHANGE: - return handle_WM_SETTINGCHANGE(hWnd, message, wParam, lParam); -#endif /* MYWINCE */ - case WM_SETFOCUS: - mi_check_modifier(); -#ifdef MYWINCE - if (g_sip_up) - { - SHSipPreference(hWnd, SIP_UP); - } -#endif - return DefWindowProc(hWnd, message, wParam, lParam); - default: - return DefWindowProc(hWnd, message, wParam, lParam); - } - return 0; -} - -/*****************************************************************************/ -static HRGN -mi_clip(HDC dc) -{ - HRGN rgn; - - rgn = CreateRectRgn(g_clip_left + g_xoff - g_xscroll, - g_clip_top + g_yoff - g_yscroll, - g_clip_right + g_xoff - g_xscroll, - g_clip_bottom + g_yoff - g_yscroll); - SelectClipRgn(dc, rgn); - IntersectClipRect(dc, g_wnd_clip.left + g_xoff, g_wnd_clip.top + g_yoff, - g_wnd_clip.right + g_xoff, g_wnd_clip.bottom + g_yoff); - return rgn; -} - -/*****************************************************************************/ -static void -mi_show_error(char * caption) -{ - LPVOID lpMsgBuf; - TCHAR lcaption[512]; - - FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, - NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), - (LPTSTR) &lpMsgBuf, 0, NULL); -#ifdef WITH_DEBUG - printf(lpMsgBuf); -#else /* WITH_DEBUG */ - str_to_uni(lcaption, caption); - MessageBox(g_Wnd, (LPTSTR) lpMsgBuf, lcaption, - MB_OK | MB_ICONINFORMATION); -#endif /* WITH_DEBUG */ - LocalFree(lpMsgBuf); -} - -/*****************************************************************************/ -void -mi_invalidate(int x, int y, int cx, int cy) -{ - HDC dc; - HDC maindc; - HGDIOBJ save; - HRGN rgn; - int dstx; - int dsty; - - maindc = GetWindowDC(g_Wnd); - dc = CreateCompatibleDC(maindc); - if (dc == 0) - { - mi_show_error("CreateCompatibleDC failed in mi_invalidate"); - } - save = SelectObject(dc, g_bs_bitmap); - dstx = (x + g_xoff) - g_xscroll; - dsty = (y + g_yoff) - g_yscroll; - rgn = mi_clip(maindc); - BitBlt(maindc, dstx, dsty, cx, cy, dc, x, y, SRCCOPY); - SelectObject(dc, save); - DeleteDC(dc); - ReleaseDC(g_Wnd, maindc); - DeleteObject(rgn); -} - -/*****************************************************************************/ -int -mi_create_bs(void) -{ - HBITMAP bitmap; - BITMAPINFO bi; - HDC maindc; - - ZeroMemory(&bi, sizeof(bi)); - bi.bmiHeader.biSize = sizeof(bi.bmiHeader); - bi.bmiHeader.biWidth = g_width; - bi.bmiHeader.biHeight = -g_height; - bi.bmiHeader.biPlanes = 1; - bi.bmiHeader.biBitCount = 32; - bi.bmiHeader.biCompression = BI_RGB; - maindc = GetWindowDC(g_Wnd); - g_bs_bitmap = CreateDIBSection(maindc, &bi, DIB_RGB_COLORS, (void **) &g_bs, 0, 0); - ReleaseDC(g_Wnd, maindc); - g_bs_bpp = 32; - g_bs_Bpp = 4; - if (g_bs_bitmap != 0) - { - //FillMemory(g_bs, g_width * g_height * 4, 0xff); - ZeroMemory(g_bs, g_width * g_height * 4); - UpdateWindow(g_Wnd); - return 1; - } - return 0; -} - -/*****************************************************************************/ -/* returns non zero if ok */ -int -mi_create_window(void) -{ - RECT rc; - WNDCLASS wc; - TCHAR classname[512]; - TCHAR caption[512]; - DWORD style; - int x; - int y; - int w; - int h; - - if (g_Wnd != 0 || g_Instance != 0) - { - return 0; - } - g_Instance = GetModuleHandle(NULL); - ZeroMemory(&wc, sizeof(wc)); - wc.lpfnWndProc = WndProc; /* points to window procedure */ - /* name of window class */ - str_to_uni(classname, "rdesktop"); - wc.lpszClassName = classname; - str_to_uni(caption, "WinRDesktop"); - /* Register the window class. */ - if (!RegisterClass(&wc)) - { - return 0; /* Failed to register window class */ - } - rc.left = 0; - rc.right = rc.left + UI_MIN(g_width, g_screen_width); - rc.top = 0; - rc.bottom = rc.top + UI_MIN(g_height, g_screen_height); -#ifdef MYWINCE - SHInitExtraControls(); - x = CW_USEDEFAULT; - y = CW_USEDEFAULT; - w = CW_USEDEFAULT; - h = CW_USEDEFAULT; - style = WS_VISIBLE; - if (g_fullscreen) - { - x = 0; - y = 0; - w = g_screen_width; - h = g_screen_height; - } -#else /* MYWINCE */ - if (g_fullscreen) - { - style = WS_POPUP; - } - else - { - style = WS_OVERLAPPED | WS_CAPTION | WS_POPUP | WS_MINIMIZEBOX | - WS_SYSMENU | WS_SIZEBOX | WS_MAXIMIZEBOX; - } - if (g_screen_width < g_width || g_screen_height < g_height) - { - style |= WS_HSCROLL | WS_VSCROLL; - } - AdjustWindowRectEx(&rc, style, 0, 0); - x = CW_USEDEFAULT; - y = CW_USEDEFAULT; - w = rc.right - rc.left; - h = rc.bottom - rc.top; -#endif /* MYWINCE */ - g_Wnd = CreateWindow(wc.lpszClassName, caption, - style, x, y, w, h, - (HWND) NULL, (HMENU) NULL, g_Instance, - (LPVOID) NULL); - g_clip_left = 0; - g_clip_top = 0; - g_clip_right = g_clip_left + g_width; - g_clip_bottom = g_clip_top + g_height; - if (g_workarea) - { - ShowWindow(g_Wnd, SW_SHOWMAXIMIZED); - } - else - { - ShowWindow(g_Wnd, SW_SHOWNORMAL); - } - -#ifdef MYWINCE - if (g_fullscreen) - { - SHFullScreen(g_Wnd, SHFS_HIDETASKBAR | SHFS_SHOWSIPBUTTON | - SHFS_SHOWSTARTICON); - } - else - { - SHFullScreen(g_Wnd, SHFS_SHOWTASKBAR | SHFS_SHOWSIPBUTTON | - SHFS_SHOWSTARTICON); - } -#endif /* MYWINCE */ - - /* WinCE doesn't have WSAAsyncSelect */ -#ifdef MYWINCE - SetTimer(g_Wnd, 1, 1000 / 60, 0); /* 60 per second */ -#else /* MYWINCE */ - WSAAsyncSelect(g_tcp_sck, g_Wnd, WM_APP + 1, FD_READ); - SetTimer(g_Wnd, 1, 1000 / 60, 0); -#endif /* MYWINCE */ - return 1; -} - -/*****************************************************************************/ -int -mi_main_loop(void) -{ - MSG msg; - - while (GetMessage(&msg, NULL, 0, 0)) - { - TranslateMessage(&msg); - DispatchMessage(&msg); - } - return msg.wParam; -} - -/*****************************************************************************/ -void -mi_warning(char * msg) -{ -} - -/*****************************************************************************/ -void -mi_paint_rect(char * data, int width, int height, int x, int y, int cx, int cy) -{ - HBITMAP bitmap; - BITMAPINFO bi; - HDC dc; - HDC maindc; - HGDIOBJ save; - HRGN rgn; - void * bits; - int i; - int j; - int colour; - int red; - int green; - int blue; - - ZeroMemory(&bi, sizeof(bi)); - bi.bmiHeader.biSize = sizeof(bi.bmiHeader); - bi.bmiHeader.biWidth = width; - bi.bmiHeader.biHeight = -height; - bi.bmiHeader.biPlanes = 1; - bi.bmiHeader.biBitCount = 32; - bi.bmiHeader.biCompression = BI_RGB; - maindc = GetWindowDC(g_Wnd); - bitmap = CreateDIBSection(maindc, &bi, DIB_RGB_COLORS, (void **) &bits, 0, 0); - if (bitmap == 0) - { - mi_show_error("CreateDIBSection failed"); - } - - if (g_server_depth == 8) - { - for (i = cy - 1; i >= 0; i--) - { - for (j = cx - 1; j >= 0; j--) - { - colour = ((unsigned char*)data)[i * cx + j]; - red = (pal_entries[colour & 0xff] & 0xff0000) >> 16; - green = (pal_entries[colour & 0xff] & 0xff00) >> 8; - blue = pal_entries[colour & 0xff] & 0xff; - MAKE_COLOUR32(colour, red, green, blue); - ((unsigned int*)bits)[i * cx + j] = colour; - } - } - } - else if (g_server_depth == 15) - { - for (i = cy - 1; i >= 0; i--) - { - for (j = cx - 1; j >= 0; j--) - { - colour = ((unsigned short*)data)[i * cx + j]; - SPLIT_COLOUR15(colour, red, green, blue); - MAKE_COLOUR32(colour, red, green, blue); - ((unsigned int*)bits)[i * cx + j] = colour; - } - } - } - else if (g_server_depth == 16) - { - for (i = cy - 1; i >= 0; i--) - { - for (j = cx - 1; j >= 0; j--) - { - colour = ((unsigned short*)data)[i * cx + j]; - SPLIT_COLOUR16(colour, red, green, blue); - MAKE_COLOUR32(colour, red, green, blue); - ((unsigned int*)bits)[i * cx + j] = colour; - } - } - } - dc = CreateCompatibleDC(maindc); - if (dc == 0) - { - mi_show_error("CreateCompatibleDC failed"); - } - save = SelectObject(dc, bitmap); - rgn = mi_clip(maindc); - BitBlt(maindc, x + g_xoff - g_xscroll, y + g_yoff - g_yscroll, cx, cy, dc, - 0, 0, SRCCOPY); - SelectObject(dc, save); - DeleteObject(bitmap); - DeleteDC(dc); - ReleaseDC(g_Wnd, maindc); - DeleteObject(rgn); - -} - -/*****************************************************************************/ -static int -mi_process_a_param(char * param1, int state) -{ - char * p; - - if (state == 0) - { - if (strcmp(param1, "-g") == 0 || strcmp(param1, "geometry") == 0) - { - state = 1; - } - if (strcmp(param1, "-t") == 0 || strcmp(param1, "port") == 0) - { - state = 2; - } - if (strcmp(param1, "-a") == 0 || strcmp(param1, "bpp") == 0) - { - state = 3; - } - if (strcmp(param1, "-f") == 0 || strcmp(param1, "fullscreen") == 0) - { - g_fullscreen = 1; - } - if (strcmp(param1, "-u") == 0 || strcmp(param1, "username") == 0) - { - state = 5; - } - if (strcmp(param1, "-p") == 0 || strcmp(param1, "password") == 0) - { - state = 6; - } - if (strcmp(param1, "-d") == 0 || strcmp(param1, "domain") == 0) - { - state = 7; - } - if (strcmp(param1, "-s") == 0 || strcmp(param1, "shell") == 0) - { - state = 8; - } - if (strcmp(param1, "-c") == 0 || strcmp(param1, "directory") == 0) - { - state = 9; - } - if (strcmp(param1, "-n") == 0 || strcmp(param1, "hostname") == 0) - { - state = 10; - } - } - else - { - if (state == 1) /* -g */ - { - state = 0; - if (strcmp(param1, "workarea") == 0) - { - g_workarea = 1; - return state; - } - g_width = strtol(param1, &p, 10); - if (g_width <= 0) - { - mi_error("invalid geometry\r\n"); - } - if (*p == 'x') - { - g_height = strtol(p + 1, &p, 10); - } - if (g_height <= 0) - { - mi_error("invalid geometry\r\n"); - } - g_width_height_set = 1; - } - if (state == 2) /* -t */ - { - state = 0; - g_tcp_port_rdp = atol(param1); - } - if (state == 3) /* -a */ - { - state = 0; - g_server_depth = atol(param1); - if (g_server_depth != 8 && g_server_depth != 15 && - g_server_depth != 16 && g_server_depth != 24) - { - mi_error("invalid server bpp\r\n"); - } - } - if (state == 5) /* -u */ - { - state = 0; - strcpy(g_username, param1); - } - if (state == 6) /* -p */ - { - state = 0; - strcpy(g_password, param1); - } - if (state == 7) /* -d */ - { - state = 0; - strcpy(g_domain, param1); - } - if (state == 8) /* -s */ - { - state = 0; - strcpy(g_shell, param1); - } - if (state == 9) /* -c */ - { - state = 0; - strcpy(g_directory, param1); - } - if (state == 10) /* -n */ - { - state = 0; - strcpy(g_hostname, param1); - } - } - return state; -} - -/*****************************************************************************/ -static int -mi_post_param(void) -{ - /* after parameters */ - if (g_fullscreen) - { - g_xoff = 0; - g_yoff = 0; - if (!g_width_height_set) - { - g_width = g_screen_width; - g_height = g_screen_height; - } - } - else if (g_workarea) - { -#ifdef MYWINCE - g_xoff = 0; - g_yoff = 0; - g_width = g_screen_width; - g_height = g_screen_height - 26; /* start menu size is 26 */ -#else /* MYWINCE */ - g_xoff = GetSystemMetrics(SM_CXEDGE) * 2; - g_yoff = GetSystemMetrics(SM_CYCAPTION) + - GetSystemMetrics(SM_CYEDGE) * 2; - g_width = g_screen_width; - g_height = g_screen_height; - g_height = (g_height - g_yoff) - g_xoff - 20; /* todo */ -#endif /* MYWINCE */ - g_width_height_set = 1; - } - else - { -#ifdef MYWINCE - g_xoff = 0; - g_yoff = 0; -#else /* MYWINCE */ - g_xoff = GetSystemMetrics(SM_CXEDGE) * 2; - g_yoff = GetSystemMetrics(SM_CYCAPTION) + - GetSystemMetrics(SM_CYEDGE) * 2; -#endif /* MYWINCE */ - } - g_width = g_width & (~3); - return 1; -} - -/*****************************************************************************/ -static int -mi_check_config_file(void) -{ - HANDLE fd; - DWORD count; - TCHAR filename[256]; - char buffer[256]; - char vname[256]; - char value[256]; - int index; - int mode; - int vnameindex; - int valueindex; - int state; - int rv; - - rv = 0; - mode = 0; - vnameindex = 0; - valueindex = 0; - vname[vnameindex] = 0; - value[valueindex] = 0; -#ifdef MYWINCE - str_to_uni(filename, "\\My Documents\\winrdesktop.ini"); -#else /* MYWINCE */ - str_to_uni(filename, ".\\winrdesktop.ini"); -#endif /* MYWINCE */ - fd = CreateFile(filename, GENERIC_READ, - FILE_SHARE_READ | FILE_SHARE_WRITE, - NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); - count = 255; - while (ReadFile(fd, buffer, count, &count, NULL)) - { - if (count == 0) - { - break; - } - buffer[count] = 0; - index = 0; - while (index < (int) count) - { - if (buffer[index] == '=') - { - mode = 1; - } - else if (buffer[index] == 10 || buffer[index] == 13) - { - mode = 0; - vname[vnameindex] = 0; - value[valueindex] = 0; - if (strlen(vname) > 0 || strlen(value) > 0) - { - if (strcmp(vname, "server") == 0) - { - strcpy(g_servername, value); - rv = 1; - } - else - { - state = mi_process_a_param(vname, 0); - mi_process_a_param(value, state); - } - } - vnameindex = 0; - valueindex = 0; - } - else if (mode == 0) - { - vname[vnameindex] = buffer[index]; - vnameindex++; - } - else - { - value[valueindex] = buffer[index]; - valueindex++; - } - index++; - } - count = 255; - } - CloseHandle(fd); - if (rv) - { - mi_post_param(); - } - return rv; -} - -/*****************************************************************************/ -/* process the command line parameters */ -/* returns boolean, non zero is ok */ -static int -mi_process_cl(LPTSTR lpCmdLine) -{ - char param[256]; - char param1[256]; -#ifndef MYWINCE - TCHAR l_username[256]; -#endif - DWORD size; - int len; - int i; - int i1; - int state; - - strcpy(g_hostname, "test"); - strcpy(g_username, "pda"); - /* get username and convert it from unicode */ - size = 255; -#ifndef MYWINCE - /* WinCE doesn't have GetUserName */ - if (GetUserName(l_username, &size)) - { - for (i = size; i >= 0; i--) - { - g_username[i] = (char) l_username[i]; - } - g_username[size] = 0; - } - else - { - mi_show_error("GetUserName"); - } -#endif /* MYWINCE */ - /* get computer name */ - if (gethostname(g_hostname, 255) != 0) - { - mi_show_error("gethostname"); - } - /* defaults */ - strcpy(g_servername, "127.0.0.1"); - g_server_depth = 8; - g_screen_width = GetSystemMetrics(SM_CXSCREEN); - g_screen_height = GetSystemMetrics(SM_CYSCREEN); - /* process parameters */ - i1 = 0; - state = 0; - len = lstrlen(lpCmdLine); - if (len == 0) - { - return mi_check_config_file(); - } - for (i = 0; i < len; i++) - { - if (lpCmdLine[i] != 32 && lpCmdLine[i] != 9) /* space or tab */ - { - param[i1] = (char) lpCmdLine[i]; - i1++; - } - else - { - param[i1] = 0; - i1 = 0; - strcpy(param1, param); - state = mi_process_a_param(param1, state); - strcpy(g_servername, param1); - } - } - if (i1 > 0) - { - param[i1] = 0; - strcpy(param1, param); - state = mi_process_a_param(param1, state); - strcpy(g_servername, param1); - } - if (state == 0) - { - mi_post_param(); - } - return (state == 0); -} - -/*****************************************************************************/ -/* display the command line options available */ -static void -mi_show_params(void) -{ - char text1[512 * 4]; - TCHAR textx[512 * 4]; - TCHAR lcaption[64]; - - strcpy(text1, ""); - strcat(text1, "WinRDesktop - an RDP client based on rdesktop\r\n"); - strcat(text1, "You can't run this application without " ); - strcat(text1, "correct parameters\r\n"); - strcat(text1, "\r\n"); - strcat(text1, "command line options\r\n"); - strcat(text1, "\r\n"); - strcat(text1, "WinRDesktop [-g widthxheight] [-t port] [-a bpp]\r\n"); - strcat(text1, " [-f] [-u username] [-p password] [-d domain]\r\n"); - strcat(text1, " [-s shell] [-c working directory] [-n host name]\r\n"); - strcat(text1, " server-name-or-ip\r\n"); - strcat(text1, "\r\n"); - strcat(text1, "You can use a config file in the current directory\r\n"); - strcat(text1, "called WinRDesktop.ini\r\n"); - strcat(text1, "The file should look like this...\r\n"); - strcat(text1, "[main]\r\n"); - strcat(text1, "server=192.168.1.1\r\n"); - strcat(text1, "port=3389\r\n"); - strcat(text1, "username=user1\r\n"); - strcat(text1, "password=password1\r\n"); - strcat(text1, "bpp=16\r\n"); - strcat(text1, "geometry=800x600\r\n"); -#ifdef WITH_DEBUG - printf(text1); -#else /* WITH_DEBUG */ - str_to_uni(lcaption, "WinRDesktop"); - str_to_uni(textx, text1); - MessageBox(g_Wnd, textx, lcaption, MB_OK); -#endif /* WITH_DEBUG */ -} - -#ifdef WITH_DEBUG -/*****************************************************************************/ -int -main(int argc, char ** argv) -{ - WSADATA d; - - WSAStartup(MAKEWORD(2, 0), &d); - if (!mi_process_cl(argv[0])) - { - mi_show_params(); - WSACleanup(); - return 0; - } - return ui_main(); -} -#else /* WITH_DEBUG */ -/*****************************************************************************/ -int WINAPI -WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, - LPTSTR lpCmdLine, int nCmdShow) -{ - WSADATA d; - - WSAStartup(MAKEWORD(2, 0), &d); - if (!mi_process_cl(lpCmdLine)) - { - mi_show_params(); - WSACleanup(); - return 0; - } - return ui_main(); -} -#endif /* WITH_DEBUG */ - -/*****************************************************************************/ -void -mi_begin_update(void) -{ -} - -/*****************************************************************************/ -void -mi_end_update(void) -{ -} - -/*****************************************************************************/ -void -mi_fill_rect(int x, int y, int cx, int cy, int colour) -{ - HBRUSH brush; - RECT rect; - HDC maindc; - HRGN rgn; - int red; - int green; - int blue; - - red = (colour & 0xff0000) >> 16; - green = (colour & 0xff00) >> 8; - blue = colour & 0xff; - maindc = GetWindowDC(g_Wnd); - rgn = mi_clip(maindc); - brush = CreateSolidBrush(RGB(red, green, blue)); - rect.left = x + g_xoff - g_xscroll; - rect.top = y + g_yoff - g_yscroll; - rect.right = rect.left + cx; - rect.bottom = rect.top + cy; - FillRect(maindc, &rect, brush); - DeleteObject(brush); - ReleaseDC(g_Wnd, maindc); - DeleteObject(rgn); -} - -/*****************************************************************************/ -void -mi_line(int x1, int y1, int x2, int y2, int colour) -{ - HPEN pen; - HDC maindc; - HGDIOBJ save; - HRGN rgn; - int red; - int green; - int blue; - - red = (colour & 0xff0000) >> 16; - green = (colour & 0xff00) >> 8; - blue = colour & 0xff; - maindc = GetWindowDC(g_Wnd); - rgn = mi_clip(maindc); - pen = CreatePen(PS_SOLID, 0, RGB(red, green, blue)); - save = SelectObject(maindc, pen); - MoveToEx(maindc, x1 + g_xoff - g_xscroll, y1 + g_yoff - g_yscroll, 0); - LineTo(maindc, x2 + g_xoff - g_xscroll, y2 + g_yoff - g_yscroll); - SelectObject(maindc, save); - DeleteObject(pen); - ReleaseDC(g_Wnd, maindc); - DeleteObject(rgn); -} - -/*****************************************************************************/ -void -mi_screen_copy(int x, int y, int cx, int cy, int srcx, int srcy) -{ - RECT rect; - RECT clip_rect; - RECT draw_rect; - HRGN rgn; - int ok_to_ScrollWindowEx; - - /* WinCE can't scroll in 2 directions at once */ -#ifdef MYWINCE - ok_to_ScrollWindowEx = cx == 0 || cy == 0; -#else /* MYWINCE */ - ok_to_ScrollWindowEx = 1; -#endif /* MYWINCE */ - if (!ok_to_ScrollWindowEx) - { - rgn = CreateRectRgn(x - g_xscroll, y - g_yscroll, - (x - g_xscroll) + cx, - (y - g_yscroll) + cy); - InvalidateRgn(g_Wnd, rgn, 0); - DeleteObject(rgn); - } - else - { - /* this is all in client coords */ - rect.left = srcx - g_xscroll; - rect.top = srcy - g_yscroll; - rect.right = rect.left + cx; - rect.bottom = rect.top + cy; - clip_rect.left = g_clip_left - g_xscroll; - clip_rect.top = g_clip_top - g_yscroll; - clip_rect.right = g_clip_right - g_xscroll; - clip_rect.bottom = g_clip_bottom - g_yscroll; - if (IntersectRect(&draw_rect, &clip_rect, &g_wnd_clip)) - { - rgn = CreateRectRgn(0, 0, 0, 0); - ScrollWindowEx(g_Wnd, x - srcx, y - srcy, &rect, &draw_rect, - rgn, 0, SW_ERASE); - InvalidateRgn(g_Wnd, rgn, 0); - DeleteObject(rgn); - } - } -} - -/*****************************************************************************/ -void -mi_set_clip(int x, int y, int cx, int cy) -{ - g_clip_left = x; - g_clip_top = y; - g_clip_right = g_clip_left + cx; - g_clip_bottom = g_clip_top + cy; -} - -/*****************************************************************************/ -void -mi_reset_clip(void) -{ - g_clip_left = 0; - g_clip_top = 0; - g_clip_right = g_clip_left + g_width; - g_clip_bottom = g_clip_top + g_height; -} - -/*****************************************************************************/ -void * -mi_create_cursor(unsigned int x, unsigned int y, - int width, int height, - unsigned char * andmask, unsigned char * xormask) -{ -#ifdef MYWINCE - return (void *) 1; -#else /* MYWINCE */ - HCURSOR hCur; - - hCur = CreateCursor(g_Instance, x, y, width, height, andmask, xormask); - if (hCur == 0) - { - hCur = LoadCursor(NULL, IDC_ARROW); - } - return hCur; -#endif /* MYWINCE */ -} - -/*****************************************************************************/ -void -mi_destroy_cursor(void * cursor) -{ -#ifdef MYWINCE -#else /* MYWINCE */ - if (g_cursor == cursor) - { - g_cursor = 0; - } - DestroyCursor(cursor); -#endif /* MYWINCE */ -} - -/*****************************************************************************/ -void -mi_set_cursor(void * cursor) -{ -#ifdef MYWINCE -#else /* MYWINCE */ - g_cursor = cursor; - SetCursor(g_cursor); -#endif /* MYWINCE */ -} - -/*****************************************************************************/ -void -mi_set_null_cursor(void) -{ -} -