Browse Source

fix struct packing when building with MSVC

Signed-off-by: Jean-Baptiste Kempf <jb@videolan.org>
Steve Lhomme 9 years ago
parent
commit
cd8e239514
4 changed files with 94 additions and 88 deletions
  1. 10 4
      src/bdsm_common.h
  2. 4 4
      src/netbios_defs.h
  3. 8 8
      src/smb_ntlm.h
  4. 72 72
      src/smb_packets.h

+ 10 - 4
src/bdsm_common.h

@@ -31,10 +31,16 @@
 #ifndef BDSM_COMMON_H
 #define BDSM_COMMON_H
 
-#ifdef _WIN32
-#define SMB_PACKED_STRUCT __attribute__((packed, gcc_struct))
-#else
-#define SMB_PACKED_STRUCT __attribute__((packed))
+#ifdef _MSC_VER
+#  define SMB_PACKED_START  __pragma(pack(push, 1))
+#  define SMB_PACKED_END    __pragma(pack(pop))
+#elif defined(__GNUC__)
+#  define SMB_PACKED_START
+# ifdef _WIN32
+#  define SMB_PACKED_END    __attribute__((packed, gcc_struct))
+# else
+#  define SMB_PACKED_END    __attribute__((packed))
+# endif
 #endif
 
 #endif // BDSM_COMMON_H

+ 4 - 4
src/netbios_defs.h

@@ -64,7 +64,7 @@
 #define NETBIOS_OP_SESSION_RETARGET   0x84
 #define NETBIOS_OP_SESSION_KEEPALIVE  0x85
 
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint16_t                    trn_id;     // Transaction ID
     uint16_t                    flags;      // Various flags
@@ -73,15 +73,15 @@ typedef struct
     uint16_t                    ns_count;   // Number of authorities (?)
     uint16_t                    ar_count;   // Additionnal (??)
     char                        payload[];
-} SMB_PACKED_STRUCT   netbios_query_packet;
+} SMB_PACKED_END   netbios_query_packet;
 
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint8_t                     opcode;     // 'TYPE'
     uint8_t                     flags;      // 0-6 reserved (== 0), byte 7 is the
     // beginning of the length field (!!)
     uint16_t                    length;     // payload length;
     uint8_t                     payload[];
-} SMB_PACKED_STRUCT   netbios_session_packet;
+} SMB_PACKED_END   netbios_session_packet;
 
 #endif

+ 8 - 8
src/smb_ntlm.h

@@ -40,7 +40,7 @@
 
 typedef uint8_t smb_ntlmh[SMB_NTLM_HASH_SIZE];
 
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint32_t    header;
     uint32_t    reserved;
@@ -48,7 +48,7 @@ typedef struct
     uint64_t    challenge;
     uint32_t    unknown;
     uint8_t     target[];
-} SMB_PACKED_STRUCT smb_ntlm_blob;
+} SMB_PACKED_END smb_ntlm_blob;
 
 
 
@@ -64,16 +64,16 @@ typedef struct
   uint16_t  FIELD ## _maxlen;    \
   uint32_t  FIELD ## _offset;
 
-typedef struct
+SMB_PACKED_START typedef struct
 {
     _NTLMSSP_COMMON
     uint32_t    flags;
     _NTLMSSP_FIELD(domain)
     _NTLMSSP_FIELD(host)
     uint8_t     names[];
-} SMB_PACKED_STRUCT smb_ntlmssp_nego;
+} SMB_PACKED_END smb_ntlmssp_nego;
 
-typedef struct
+SMB_PACKED_START typedef struct
 {
     _NTLMSSP_COMMON
     _NTLMSSP_FIELD(name)
@@ -82,9 +82,9 @@ typedef struct
     uint64_t    reserved;
     _NTLMSSP_FIELD(tgt) // Target Info
     uint8_t     data[];
-} SMB_PACKED_STRUCT smb_ntlmssp_challenge;
+} SMB_PACKED_END smb_ntlmssp_challenge;
 
-typedef struct
+SMB_PACKED_START typedef struct
 {
     _NTLMSSP_COMMON
     _NTLMSSP_FIELD(lm)
@@ -96,7 +96,7 @@ typedef struct
 
     uint32_t    flags;
     uint8_t     data[];
-} SMB_PACKED_STRUCT smb_ntlmssp_auth;
+} SMB_PACKED_END smb_ntlmssp_auth;
 
 uint64_t    smb_ntlm_generate_challenge();
 void        smb_ntlm_generate_xkey(smb_ntlmh cli_session_key);

+ 72 - 72
src/smb_packets.h

@@ -44,7 +44,7 @@
 ////////////////////////////////////////////////////////////////////////////////
 // Main structures for holding packet data and building packets
 
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint8_t         magic[4];     // { 0xff, 0x53, 0x4d, 0x42 } "\xffSMB"
     uint8_t         command;      // The actual SMB command
@@ -58,34 +58,34 @@ typedef struct
     uint16_t        pid;          // Process ID.
     uint16_t        uid;          // User ID.
     uint16_t        mux_id;       // Multiplex ID. Increment it sometimes.
-} SMB_PACKED_STRUCT        smb_header;
+} SMB_PACKED_END        smb_header;
 
-typedef struct
+SMB_PACKED_START typedef struct
 {
     smb_header    header;       // A packet header full of gorgeous goodness.
     uint8_t         payload[];    // Ze yummy data inside. Eat 5 fruits/day !
-} SMB_PACKED_STRUCT       smb_packet;
+} SMB_PACKED_END       smb_packet;
 
 
 ////////////////////////////////////////////////////////////////////////////////
 // Individual SMB command payload description
 
 // Simple structure used for several requests/responses
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint8_t         wct;              // 0
     uint16_t        bct;
-} SMB_PACKED_STRUCT   smb_simple_struct;
+} SMB_PACKED_END   smb_simple_struct;
 
 
 //-> Negotiate Protocol
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint8_t         wct; // zero
     uint16_t        bct;
     char            dialects[];
 
-} SMB_PACKED_STRUCT   smb_nego_req;
+} SMB_PACKED_END   smb_nego_req;
 
 
 #define SMB_NEGO_RESP_COMMON \
@@ -103,20 +103,20 @@ typedef struct
   uint16_t        bct;
 
 //<- Negotiate Protocol
-typedef struct
+SMB_PACKED_START typedef struct
 {
     SMB_NEGO_RESP_COMMON
     uint64_t        challenge;      // Normally 8 bytes, if not then wtf monkey
     uint8_t         payload[];      // The rest isn't really meaningfull for us
-} SMB_PACKED_STRUCT   smb_nego_resp;
+} SMB_PACKED_END   smb_nego_resp;
 
 //<- Negotiate Protocol
-typedef struct
+SMB_PACKED_START typedef struct
 {
     SMB_NEGO_RESP_COMMON
     uint8_t         srv_guid[16];
     uint8_t         gssapi[];
-} SMB_PACKED_STRUCT   smb_nego_xsec_resp;
+} SMB_PACKED_END   smb_nego_xsec_resp;
 
 #define SMB_SESSION_REQ_COMMON \
   uint8_t         wct;          /* +-13 :) */                                  \
@@ -127,7 +127,7 @@ typedef struct
   uint32_t        session_key;  /* 0x00000000 */
 
 //-> Session Setup
-typedef struct
+SMB_PACKED_START typedef struct
 {
     SMB_SESSION_REQ_COMMON
     uint16_t        oem_pass_len; // Length of LM2 response
@@ -136,10 +136,10 @@ typedef struct
     uint32_t        caps;         // Capabilities
     uint16_t        payload_size;
     uint8_t         payload[];
-} SMB_PACKED_STRUCT   smb_session_req;
+} SMB_PACKED_END   smb_session_req;
 
 //-> Session Setup
-typedef struct
+SMB_PACKED_START typedef struct
 {
     SMB_SESSION_REQ_COMMON
     uint16_t        xsec_blob_size; // Length of GSSAPI/SPNEGO blob
@@ -147,20 +147,20 @@ typedef struct
     uint32_t        caps;           // Capabilities
     uint16_t        payload_size;
     uint8_t         payload[];
-} SMB_PACKED_STRUCT   smb_session_xsec_req;
+} SMB_PACKED_END   smb_session_xsec_req;
 
 
 //<- Session Setup
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint8_t         wct;
     SMB_ANDX_MEMBERS
     uint16_t        action;
     uint16_t        bct;
     uint8_t         bullshit[];
-} SMB_PACKED_STRUCT   smb_session_resp;
+} SMB_PACKED_END   smb_session_resp;
 
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint8_t         wct;
     SMB_ANDX_MEMBERS
@@ -168,12 +168,12 @@ typedef struct
     uint16_t        xsec_blob_size;
     uint16_t        payload_size;
     uint8_t         payload[];
-} SMB_PACKED_STRUCT   smb_session_xsec_resp;
+} SMB_PACKED_END   smb_session_xsec_resp;
 
 
 
 //-> Tree Connect
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint8_t         wct;              // 4
     SMB_ANDX_MEMBERS
@@ -182,10 +182,10 @@ typedef struct
     uint16_t        bct;
     uint8_t         payload[];        // Password | Path | Service
 
-} SMB_PACKED_STRUCT   smb_tree_connect_req;
+} SMB_PACKED_END   smb_tree_connect_req;
 
 //<- Tree Connect
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint8_t         wct;              // 7
     SMB_ANDX_MEMBERS
@@ -194,14 +194,14 @@ typedef struct
     uint32_t        guest_rights;
     uint16_t        bct;
     uint8_t         payload[];
-} SMB_PACKED_STRUCT   smb_tree_connect_resp;
+} SMB_PACKED_END   smb_tree_connect_resp;
 
 //-> Tree Disconnect / <- Tree Disconnect
 typedef smb_simple_struct smb_tree_disconnect_req;
 typedef smb_simple_struct smb_tree_disconnect_resp;
 
 //-> Create File
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint8_t         wct;                // 24
     SMB_ANDX_MEMBERS
@@ -219,10 +219,10 @@ typedef struct
     uint8_t         security_flags;
     uint16_t        bct;
     uint8_t         path[];             // UTF16 Path, starting with '\'
-} SMB_PACKED_STRUCT   smb_create_req;
+} SMB_PACKED_END   smb_create_req;
 
 //<- Create File
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint8_t         wct;                // 34
     SMB_ANDX_MEMBERS
@@ -240,23 +240,23 @@ typedef struct
     uint16_t        ipc_state;
     uint8_t         is_dir;
     uint16_t        bct;                // 0
-} SMB_PACKED_STRUCT   smb_create_resp;
+} SMB_PACKED_END   smb_create_resp;
 
 
 
 //-> Close File
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint8_t         wct;                // 3
     uint16_t        fid;
     uint32_t        last_write;         // Not defined == 0xffffffff
     uint16_t        bct;                // 0
-} SMB_PACKED_STRUCT   smb_close_req;
+} SMB_PACKED_END   smb_close_req;
 
 
 
 //-> Read File
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint8_t         wct;                // 12
     SMB_ANDX_MEMBERS
@@ -268,10 +268,10 @@ typedef struct
     uint16_t        remaining;
     uint32_t        offset_high;        // Continuation of offset field'
     uint16_t        bct;                // 0
-} SMB_PACKED_STRUCT   smb_read_req;
+} SMB_PACKED_END   smb_read_req;
 
 //<- Read File
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint8_t         wct;                // 12
     SMB_ANDX_MEMBERS
@@ -284,10 +284,10 @@ typedef struct
     uint32_t        reserved2;
     uint16_t        reserved3;
     uint16_t        bct;
-} SMB_PACKED_STRUCT   smb_read_resp;
+} SMB_PACKED_END   smb_read_resp;
 
 //-> Write File
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint8_t         wct;                // 14
     SMB_ANDX_MEMBERS
@@ -302,10 +302,10 @@ typedef struct
     uint32_t        offset_high;        // Continuation of offset field'
     uint16_t        bct;
     uint8_t         padding;
-} SMB_PACKED_STRUCT   smb_write_req;
+} SMB_PACKED_END   smb_write_req;
 
 //<- Write File
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint8_t         wct;                // 6
     SMB_ANDX_MEMBERS
@@ -314,63 +314,63 @@ typedef struct
     uint16_t        available;
     uint32_t        reserved;
     uint16_t        bct;
-} SMB_PACKED_STRUCT   smb_write_resp;
+} SMB_PACKED_END   smb_write_resp;
 
 //-> Remove File
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint8_t         wct;                // 0x01
     uint16_t        search_attributes;  // 0x0000 for "normal" (not hidden/ystem) files
     uint16_t        bct;                // >= 2
     uint8_t         buffer_format;      // 0x04
-} SMB_PACKED_STRUCT   smb_file_rm_req;
+} SMB_PACKED_END   smb_file_rm_req;
 
 //<- Remove File
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint8_t         wct;                // 0x00
     uint16_t        bct;                // 0x0000
-} SMB_PACKED_STRUCT   smb_file_rm_resp;
+} SMB_PACKED_END   smb_file_rm_resp;
 
 //-> Remove Directory
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint8_t         wct;                // 0x00
     uint16_t        bct;                // >= 2
     uint8_t         buffer_format;      // 0x04
-} SMB_PACKED_STRUCT   smb_directory_rm_req;
+} SMB_PACKED_END   smb_directory_rm_req;
 
 //<- Remove Directory
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint8_t         wct;                // 0x00
     uint16_t        bct;                // 0x0000
-} SMB_PACKED_STRUCT   smb_directory_rm_resp;
+} SMB_PACKED_END   smb_directory_rm_resp;
 
 //-> Move File
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint8_t         wct;                // 0x01
     uint16_t        search_attributes;  // 0x0000 for "normal" (not hidden/ystem) files
     uint16_t        bct;                // >= 2
-} SMB_PACKED_STRUCT   smb_file_mv_req;
+} SMB_PACKED_END   smb_file_mv_req;
 
 //<- Move File
 typedef smb_simple_struct smb_file_mv_resp;
 
 //-> Create Directory
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint8_t         wct;                // 0x00
     uint16_t        bct;                // >= 2
     uint8_t         buffer_format;      // 0x04
-} SMB_PACKED_STRUCT   smb_directory_mk_req;
+} SMB_PACKED_END   smb_directory_mk_req;
 
 //<- Create Directory
 typedef smb_simple_struct smb_directory_mk_resp;
 
 //-> Trans
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint8_t       wct;                    // 16
     uint16_t      total_param_count;
@@ -392,14 +392,14 @@ typedef struct
     uint16_t      fid;
     uint16_t      bct;
     uint8_t       payload[];
-} SMB_PACKED_STRUCT   smb_trans_req;
+} SMB_PACKED_END   smb_trans_req;
 
 
 
 
 
 //-> Trans2
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint8_t       wct;                // 15
     uint16_t      total_param_count;
@@ -421,10 +421,10 @@ typedef struct
     uint16_t      bct;
     uint8_t       padding[3];
     uint8_t       payload[];
-} SMB_PACKED_STRUCT   smb_trans2_req;
+} SMB_PACKED_END   smb_trans2_req;
 
 //// -> Trans2|FindFirst2
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint16_t      attrs;              // Search attributes
     uint16_t      count;              // Search count
@@ -432,10 +432,10 @@ typedef struct
     uint16_t      interest;           // What kind of info do we want ?
     uint32_t      storage;            // ? => 0
     uint8_t       pattern[];          // The queried pattern "\\folder\\*"
-} SMB_PACKED_STRUCT   smb_tr2_findfirst2;
+} SMB_PACKED_END   smb_tr2_findfirst2;
 
 //// -> Trans2|FindNext2
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint16_t      sid;                // Search handle
     uint16_t      count;              // Search count
@@ -443,19 +443,19 @@ typedef struct
     uint32_t      resume_key;         // Value returned by previous find2 call
     uint16_t      flags;
     uint8_t       pattern[];          // The queried pattern "\\folder\\*"
-} SMB_PACKED_STRUCT   smb_tr2_findnext2;
+} SMB_PACKED_END   smb_tr2_findnext2;
 
 //// -> Trans2|QueryPathInfo
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint16_t      interest;
     uint32_t      reserved;
     uint8_t       path[];
-} SMB_PACKED_STRUCT   smb_tr2_query;
+} SMB_PACKED_END   smb_tr2_query;
 
 //<- Trans2
 
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint8_t       wct;                // 10
     uint16_t      total_param_count;
@@ -472,10 +472,10 @@ typedef struct
     uint16_t      bct;
     uint8_t       padding;
     uint8_t       payload[];
-} SMB_PACKED_STRUCT   smb_trans2_resp;
+} SMB_PACKED_END   smb_trans2_resp;
 
 //// <- Trans2|FindFirst2Params
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint16_t      id;
     uint16_t      count;
@@ -483,19 +483,19 @@ typedef struct
     uint16_t      ea_error_offset;
     uint16_t      last_name_offset;
     uint16_t      padding;
-} SMB_PACKED_STRUCT   smb_tr2_findfirst2_params;
+} SMB_PACKED_END   smb_tr2_findfirst2_params;
 
 //// <- Trans2|FindNext2Params
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint16_t      count;
     uint16_t      eos;
     uint16_t      ea_error_offset;
     uint16_t      last_name_offset;
-} SMB_PACKED_STRUCT   smb_tr2_findnext2_params;
+} SMB_PACKED_END   smb_tr2_findnext2_params;
 
 //// <- Trans2|FindFirst2FileInfo
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint32_t      next_entry;
     uint32_t      index;
@@ -512,11 +512,11 @@ typedef struct
     uint8_t       reserved;
     uint8_t       short_name[24];
     uint8_t       name[];
-} SMB_PACKED_STRUCT   smb_tr2_find2_entry;
+} SMB_PACKED_END   smb_tr2_find2_entry;
 
 
 //// <- Trans2|QueryPathInfo
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint64_t      created;
     uint64_t      accessed;
@@ -533,10 +533,10 @@ typedef struct
     uint32_t      ea_list_len;
     uint32_t      name_len;
     uint8_t       name[];
-} SMB_PACKED_STRUCT   smb_tr2_path_info;
+} SMB_PACKED_END   smb_tr2_path_info;
 
 //-> Example
-typedef struct
+SMB_PACKED_START typedef struct
 {
     uint8_t         wct;                // ??
     SMB_ANDX_MEMBERS
@@ -544,7 +544,7 @@ typedef struct
     uint16_t        bct;
     //uint8_t         padding;
     uint8_t         file[];
-} SMB_PACKED_STRUCT   smb_example_t;
+} SMB_PACKED_END   smb_example_t;
 
 
 #endif