Previous 199869 Revisions Next

r34587 Saturday 24th January, 2015 at 14:33:06 UTC by Couriersud
More code alignment (windows/sdl). (nw)
[src/osd/sdl]netdev_pcap.c netdev_pcap_osx.c sdlos_win32.c strconv.c
[src/osd/windows]netdev_pcap.c strconv.c winfile.c

trunk/src/osd/sdl/netdev_pcap.c
r243098r243099
66#include "emu.h"
77#include "osdnet.h"
88
9#define pcap_compile_dl pcap_compile
10#define pcap_findalldevs_dl pcap_findalldevs
11#define pcap_open_live_dl pcap_open_live
12#define pcap_next_ex_dl pcap_next_ex
13#define pcap_close_dl pcap_close
14#define pcap_setfilter_dl pcap_setfilter
15#define pcap_sendpacket_dl pcap_sendpacket
16#define pcap_set_datalink_dl pcap_set_datalink
17
18
919class netdev_pcap : public netdev
1020{
1121public:
r243098r243099
2434   : netdev(ifdev, rate)
2535{
2636   char errbuf[PCAP_ERRBUF_SIZE];
27   m_p = pcap_open_live(name, 65535, 1, 1, errbuf);
37   m_p = pcap_open_live_dl(name, 65535, 1, 1, errbuf);
2838   if(!m_p)
2939   {
3040      osd_printf_verbose("Unable to open %s: %s\n", name, errbuf);
3141      return;
3242   }
33   if(pcap_set_datalink(m_p, DLT_EN10MB) == -1)
43   if(pcap_set_datalink_dl(m_p, DLT_EN10MB) == -1)
3444   {
35      osd_printf_verbose("Unable to set %s to ethernet", name);
36      pcap_close(m_p);
45      osd_printf_verbose("Unable to set %s to ethernet\n", name);
46      pcap_close_dl(m_p);
3747      m_p = NULL;
3848      return;
3949   }
r243098r243099
4656   struct bpf_program fp;
4757   if(!m_p) return;
4858   sprintf(filter, "ether dst %.2X:%.2X:%.2X:%.2X:%.2X:%.2X or ether multicast or ether broadcast", (unsigned char)mac[0], (unsigned char)mac[1], (unsigned char)mac[2],(unsigned char)mac[3], (unsigned char)mac[4], (unsigned char)mac[5]);
49   pcap_compile(m_p, &fp, filter, 1, 0);
50   pcap_setfilter(m_p, &fp);
59   if(pcap_compile_dl(m_p, &fp, filter, 1, 0) == -1) {
60      osd_printf_verbose("Error with pcap_compile\n");
61   }
62   if(pcap_setfilter_dl(m_p, &fp) == -1) {
63      osd_printf_verbose("Error with pcap_setfilter\n");
64   }
5165}
5266
5367int netdev_pcap::send(UINT8 *buf, int len)
5468{
5569   if(!m_p) return 0;
56   return (!pcap_sendpacket(m_p, buf, len))?len:0;
70   return (!pcap_sendpacket_dl(m_p, buf, len))?len:0;
5771}
5872
5973int netdev_pcap::recv_dev(UINT8 **buf)
6074{
6175   struct pcap_pkthdr *header;
6276   if(!m_p) return 0;
63   return (pcap_next_ex(m_p, &header, (const u_char **)buf) == 1)?header->len:0;
77   return (pcap_next_ex_dl(m_p, &header, (const u_char **)buf) == 1)?header->len:0;
6478}
6579
6680netdev_pcap::~netdev_pcap()
6781{
68   if(m_p) pcap_close(m_p);
82   if(m_p) pcap_close_dl(m_p);
6983}
7084
7185static CREATE_NETDEV(create_pcap)
r243098r243099
7892{
7993   pcap_if_t *devs;
8094   char errbuf[PCAP_ERRBUF_SIZE];
81   if(pcap_findalldevs(&devs, errbuf) == -1)
95   if(pcap_findalldevs_dl(&devs, errbuf) == -1)
8296   {
8397      osd_printf_verbose("Unable to get network devices: %s\n", errbuf);
8498      return;
8599   }
86100
87   if (devs)
88   {
89      while(devs->next)
90      {
91         add_netdev(devs->name, devs->description, create_pcap);
92         devs = devs->next;
93      }
94   }
101    while(devs)
102    {
103        add_netdev(devs->name, devs->description, create_pcap);
104        devs = devs->next;
105    }
95106}
96107
97108void deinit_pcap()
trunk/src/osd/sdl/netdev_pcap_osx.c
r243098r243099
88#include <pthread.h>
99#include <libkern/OSAtomic.h>
1010
11#define pcap_compile_dl pcap_compile
12#define pcap_findalldevs_dl pcap_findalldevs
13#define pcap_open_live_dl pcap_open_live
14#define pcap_next_ex_dl pcap_next_ex
15#define pcap_close_dl pcap_close
16#define pcap_setfilter_dl pcap_setfilter
17#define pcap_sendpacket_dl pcap_sendpacket
18#define pcap_set_datalink_dl pcap_set_datalink
19
1120struct netdev_pcap_context {
1221   UINT8 *pkt;
1322   int len;
r243098r243099
6170   : netdev(ifdev, rate)
6271{
6372   char errbuf[PCAP_ERRBUF_SIZE];
64   m_p = pcap_open_live(name, 65535, 1, 1, errbuf);
73   m_p = pcap_open_live_dl(name, 65535, 1, 1, errbuf);
6574   if(!m_p)
6675   {
6776      osd_printf_verbose("Unable to open %s: %s\n", name, errbuf);
6877      return;
6978   }
70   if(pcap_set_datalink(m_p, DLT_EN10MB) == -1)
79   if(pcap_set_datalink_dl(m_p, DLT_EN10MB) == -1)
7180   {
7281      osd_printf_verbose("Unable to set %s to ethernet\n", name);
73      pcap_close(m_p);
82      pcap_close_dl(m_p);
7483      m_p = NULL;
7584      return;
7685   }
r243098r243099
8897   struct bpf_program fp;
8998   if(!m_p) return;
9099   sprintf(filter, "not ether src %.2X:%.2X:%.2X:%.2X:%.2X:%.2X and (ether dst %.2X:%.2X:%.2X:%.2X:%.2X:%.2X or ether multicast or ether broadcast or ether dst 09:00:07:ff:ff:ff)", (unsigned char)mac[0], (unsigned char)mac[1], (unsigned char)mac[2],(unsigned char)mac[3], (unsigned char)mac[4], (unsigned char)mac[5], (unsigned char)mac[0], (unsigned char)mac[1], (unsigned char)mac[2],(unsigned char)mac[3], (unsigned char)mac[4], (unsigned char)mac[5]);
91   if(pcap_compile(m_p, &fp, filter, 1, 0) == -1) {
100   if(pcap_compile_dl(m_p, &fp, filter, 1, 0) == -1) {
92101      osd_printf_verbose("Error with pcap_compile\n");
93102   }
94   if(pcap_setfilter(m_p, &fp) == -1) {
103   if(pcap_setfilter_dl(m_p, &fp) == -1) {
95104      osd_printf_verbose("Error with pcap_setfilter\n");
96105   }
97106}
r243098r243099
99108int netdev_pcap::send(UINT8 *buf, int len)
100109{
101110   if(!m_p) return 0;
102   return (!pcap_sendpacket(m_p, buf, len))?len:0;
111   return (!pcap_sendpacket_dl(m_p, buf, len))?len:0;
103112}
104113
105114int netdev_pcap::recv_dev(UINT8 **buf)
r243098r243099
121130
122131netdev_pcap::~netdev_pcap()
123132{
124   if(m_p) pcap_close(m_p);
133   if(m_p) pcap_close_dl(m_p);
125134}
126135
127136static CREATE_NETDEV(create_pcap)
r243098r243099
134143{
135144   pcap_if_t *devs;
136145   char errbuf[PCAP_ERRBUF_SIZE];
137   if(pcap_findalldevs(&devs, errbuf) == -1)
146   if(pcap_findalldevs_dl(&devs, errbuf) == -1)
138147   {
139148      osd_printf_verbose("Unable to get network devices: %s\n", errbuf);
140149      return;
141150   }
142151
152#if 1
153    while(devs)
154    {
155        add_netdev(devs->name, devs->description, create_pcap);
156        devs = devs->next;
157    }
158#else
143159   if (devs)
144160   {
145161      while(devs->next)
r243098r243099
148164         devs = devs->next;
149165      }
150166   }
167#endif
151168}
152169
153170void deinit_pcap()
trunk/src/osd/sdl/sdlos_win32.c
r243098r243099
102102   return result;
103103}
104104
105//============================================================
106//  astring_from_utf8
107//============================================================
108
109CHAR *astring_from_utf8(const char *utf8string)
110{
111   WCHAR *wstring;
112   int char_count;
113   CHAR *result;
114
115   // convert MAME string (UTF-8) to UTF-16
116   char_count = MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, NULL, 0);
117   wstring = (WCHAR *)alloca(char_count * sizeof(*wstring));
118   MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, wstring, char_count);
119
120   // convert UTF-16 to "ANSI code page" string
121   char_count = WideCharToMultiByte(CP_ACP, 0, wstring, -1, NULL, 0, NULL, NULL);
122   result = (CHAR *)osd_malloc_array(char_count * sizeof(*result));
123   if (result != NULL)
124      WideCharToMultiByte(CP_ACP, 0, wstring, -1, result, char_count, NULL, NULL);
125
126   return result;
127}
128
129//============================================================
130//  wstring_from_utf8
131//============================================================
132
133WCHAR *wstring_from_utf8(const char *utf8string)
134{
135   int char_count;
136   WCHAR *result;
137
138   // convert MAME string (UTF-8) to UTF-16
139   char_count = MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, NULL, 0);
140   result = (WCHAR *)osd_malloc_array(char_count * sizeof(*result));
141   if (result != NULL)
142      MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, result, char_count);
143
144   return result;
145}
146
trunk/src/osd/sdl/strconv.c
r243098r243099
1// license:BSD-3-Clause
2// copyright-holders:Aaron Giles
13//============================================================
24//
3//  strconv.c - SDL (POSIX) string conversion
5//  strconv.c - Win32 string conversion
46//
5//  Copyright (c) 1996-2010, Nicola Salmoria and the MAME Team.
6//  Visit http://mamedev.org for licensing and usage restrictions.
7//
8//  SDLMAME by Olivier Galibert and R. Belmont
9//
107//============================================================
118
12#ifdef SDLMAME_WIN32
9#if defined(SDLMAME_WIN32) || defined(OSD_WINDOWS)
1310#define WIN32_LEAN_AND_MEAN
1411#include <windows.h>
1512#endif
1613
17#include <stdlib.h>
18
1914// MAMEOS headers
2015#include "strconv.h"
2116#include "unicode.h"
2217
23#ifdef SDLMAME_WIN32
18#if defined(SDLMAME_WIN32) || defined(OSD_WINDOWS)
2419//============================================================
20//  astring_from_utf8
21//============================================================
22
23CHAR *astring_from_utf8(const char *utf8string)
24{
25   WCHAR *wstring;
26   int char_count;
27   CHAR *result;
28
29   // convert MAME string (UTF-8) to UTF-16
30   char_count = MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, NULL, 0);
31   wstring = (WCHAR *)alloca(char_count * sizeof(*wstring));
32   MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, wstring, char_count);
33
34   // convert UTF-16 to "ANSI code page" string
35   char_count = WideCharToMultiByte(CP_ACP, 0, wstring, -1, NULL, 0, NULL, NULL);
36   result = (CHAR *)osd_malloc_array(char_count * sizeof(*result));
37   if (result != NULL)
38      WideCharToMultiByte(CP_ACP, 0, wstring, -1, result, char_count, NULL, NULL);
39
40   return result;
41}
42
43
44//============================================================
2545//  utf8_from_astring
2646//============================================================
2747
r243098r243099
4565   return result;
4666}
4767
68
4869//============================================================
70//  wstring_from_utf8
71//============================================================
72
73WCHAR *wstring_from_utf8(const char *utf8string)
74{
75   int char_count;
76   WCHAR *result;
77
78   // convert MAME string (UTF-8) to UTF-16
79   char_count = MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, NULL, 0);
80   result = (WCHAR *)osd_malloc_array(char_count * sizeof(*result));
81   if (result != NULL)
82      MultiByteToWideChar(CP_UTF8, 0, utf8string, -1, result, char_count);
83
84   return result;
85}
86
87
88//============================================================
4989//  utf8_from_wstring
5090//============================================================
5191
r243098r243099
62102
63103   return result;
64104}
65#endif
66105
67106//============================================================
68107//  osd_uchar_from_osdchar
69108//============================================================
70109
71int osd_uchar_from_osdchar(unicode_char *uchar, const char *osdchar, size_t count)
110int osd_uchar_from_osdchar(UINT32 *uchar, const char *osdchar, size_t count)
72111{
73   wchar_t wch;
112   WCHAR wch;
74113
75   count = mbstowcs(&wch, (char *)osdchar, 1);
76   if (count != -1)
114   count = MIN(count, IsDBCSLeadByte(*osdchar) ? 2 : 1);
115   if (MultiByteToWideChar(CP_ACP, 0, osdchar, (DWORD)count, &wch, 1) != 0)
77116      *uchar = wch;
78117   else
79118      *uchar = 0;
119   return (int) count;
120}
80121
81   return count;
122#else
123
124//============================================================
125//  osd_uchar_from_osdchar
126//============================================================
127
128int osd_uchar_from_osdchar(unicode_char *uchar, const char *osdchar, size_t count)
129{
130    wchar_t wch;
131
132    count = mbstowcs(&wch, (char *)osdchar, 1);
133    if (count != -1)
134        *uchar = wch;
135    else
136        *uchar = 0;
137
138    return count;
82139}
140
141#endif
trunk/src/osd/windows/netdev_pcap.c
r243098r243099
112112   catch (DWORD e)
113113   {
114114      FreeLibrary(handle);
115      osd_printf_verbose("Unable to load winpcap: %lx\n", e);
115      osd_printf_warning("Unable to load winpcap: %lx\n", e);
116116      return;
117117   }
118118   if(pcap_findalldevs_dl(&devs, errbuf) == -1)
119119   {
120120      FreeLibrary(handle);
121      osd_printf_verbose("Unable to get network devices: %s\n", errbuf);
121      osd_printf_warning("Unable to get network devices: %s\n", errbuf);
122122      return;
123123   }
124124
trunk/src/osd/windows/strconv.c
r243098r243099
66//
77//============================================================
88
9// standard windows headers
9#if defined(SDLMAME_WIN32) || defined(OSD_WINDOWS)
1010#define WIN32_LEAN_AND_MEAN
1111#include <windows.h>
12#endif
1213
1314// MAMEOS headers
1415#include "strconv.h"
1516#include "unicode.h"
1617
17
18#if defined(SDLMAME_WIN32) || defined(OSD_WINDOWS)
1819//============================================================
1920//  astring_from_utf8
2021//============================================================
r243098r243099
101102
102103   return result;
103104}
105
106//============================================================
107//  osd_uchar_from_osdchar
108//============================================================
109
110int osd_uchar_from_osdchar(UINT32 *uchar, const char *osdchar, size_t count)
111{
112   WCHAR wch;
113
114   count = MIN(count, IsDBCSLeadByte(*osdchar) ? 2 : 1);
115   if (MultiByteToWideChar(CP_ACP, 0, osdchar, (DWORD)count, &wch, 1) != 0)
116      *uchar = wch;
117   else
118      *uchar = 0;
119   return (int) count;
120}
121
122#else
123
124//============================================================
125//  osd_uchar_from_osdchar
126//============================================================
127
128int osd_uchar_from_osdchar(unicode_char *uchar, const char *osdchar, size_t count)
129{
130   wchar_t wch;
131
132   count = mbstowcs(&wch, (char *)osdchar, 1);
133   if (count != -1)
134      *uchar = wch;
135   else
136      *uchar = 0;
137
138   return count;
139}
140
141#endif
trunk/src/osd/windows/winfile.c
r243098r243099
383383
384384
385385//============================================================
386//  osd_uchar_from_osdchar
387//============================================================
388
389int osd_uchar_from_osdchar(UINT32 *uchar, const char *osdchar, size_t count)
390{
391   WCHAR wch;
392
393   count = MIN(count, IsDBCSLeadByte(*osdchar) ? 2 : 1);
394   if (MultiByteToWideChar(CP_ACP, 0, osdchar, (DWORD)count, &wch, 1) != 0)
395      *uchar = wch;
396   else
397      *uchar = 0;
398   return (int) count;
399}
400
401
402//============================================================
403386//  create_path_recursive
404387//============================================================
405388


Previous 199869 Revisions Next


© 1997-2024 The MAME Team