CVAL,PVAL,SVAL宏定义

本文档详细介绍了Samba开源代码中用于处理不同字节序的宏定义,包括提取和设置不同长度数值的宏,如SVAL、IVAL、SVALS等。这些宏主要用于在不同字节序的系统间进行机器无关的数据交换,确保数据的一致性。文中还给出了一个简单的示例来演示如何使用这些宏。
摘要由CSDN通过智能技术生成

常规使用

// smb 开源代码
flags = IVAL(inhdr, SMB2_HDR_FLAGS);
opcode = SVAL(inhdr, SMB2_HDR_OPCODE);
mid = BVAL(inhdr, SMB2_HDR_MESSAGE_ID);

源码中解释

https://github.com/samba-team/samba/blob/master/lib/util/byteorder.h

/* 
   Unix SMB/CIFS implementation.
   SMB Byte handling
   Copyright (C) Andrew Tridgell 1992-1998
   
   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 3 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, see <http://www.gnu.org/licenses/>.
*/

#ifndef _BYTEORDER_H
#define _BYTEORDER_H

#include "bytearray.h"

/*
   This file implements macros for machine independent short and 
   int manipulation
Here is a description of this file that I emailed to the samba list once:
> I am confused about the way that byteorder.h works in Samba. I have
> looked at it, and I would have thought that you might make a distinction
> between LE and BE machines, but you only seem to distinguish between 386
> and all other architectures.
> 
> Can you give me a clue?
sure.
Ok, now to the macros themselves. I'll take a simple example, say we
want to extract a 2 byte integer from a SMB packet and put it into a
type called uint16_t that is in the local machines byte order, and you
want to do it with only the assumption that uint16_t is _at_least_ 16
bits long (this last condition is very important for architectures
that don't have any int types that are 2 bytes long)
You do this:
#define CVAL(buf,pos) (((uint8_t *)(buf))[pos])
#define PVAL(buf,pos) ((unsigned int)CVAL(buf,pos))
#define SVAL(buf,pos) (PVAL(buf,pos)|PVAL(buf,(pos)+1)<<8)
then to extract a uint16_t value at offset 25 in a buffer you do this:
char *buffer = foo_bar();
uint16_t xx = SVAL(buffer,25);
We are using the byteoder independence of the ANSI C bitshifts to do
the work. A good optimising compiler should turn this into efficient
code, especially if it happens to have the right byteorder :-)
I know these macros can be made a bit tidier by removing some of the
casts, but you need to look at byteorder.h as a whole to see the
reasoning behind them. byteorder.h defines the following macros:
SVAL(buf,pos) - extract a 2 byte SMB value
IVAL(buf,pos) - extract a 4 byte SMB value
BVAL(buf,pos) - extract a 8 byte SMB value
SVALS(buf,pos) - signed version of SVAL()
IVALS(buf,pos) - signed version of IVAL()
BVALS(buf,pos) - signed version of BVAL()
SSVAL(buf,pos,val) - put a 2 byte SMB value into a buffer
SIVAL(buf,pos,val) - put a 4 byte SMB value into a buffer
SBVAL(buf,pos,val) - put a 8 byte SMB value into a buffer
SSVALS(buf,pos,val) - signed version of SSVAL()
SIVALS(buf,pos,val) - signed version of SIVAL()
SBVALS(buf,pos,val) - signed version of SBVAL()
RSVAL(buf,pos) - like SVAL() but for NMB byte ordering
RSVALS(buf,pos) - like SVALS() but for NMB byte ordering
RIVAL(buf,pos) - like IVAL() but for NMB byte ordering
RIVALS(buf,pos) - like IVALS() but for NMB byte ordering
RSSVAL(buf,pos,val) - like SSVAL() but for NMB ordering
RSIVAL(buf,pos,val) - like SIVAL() but for NMB ordering
RSIVALS(buf,pos,val) - like SIVALS() but for NMB ordering
it also defines lots of intermediate macros, just ignore those :-)
*/


/****************************************************************************
 *
 * ATTENTION: Do not use those macros anymore, use the ones from bytearray.h
 *
 ****************************************************************************/

#define CVAL(buf,pos) ((uint32_t)_DATA_BYTE_CONST(buf, pos))
#define CVAL_NC(buf,pos) _DATA_BYTE(buf, pos) /* Non-const version of CVAL */
#define PVAL(buf,pos) (CVAL(buf,pos))
#define SCVAL(buf,pos,val) (CVAL_NC(buf,pos) = (val))

/****************************************************************************
 *
 * ATTENTION: Do not use those macros anymore, use the ones from bytearray.h
 *
 ****************************************************************************/

#define SVAL(buf,pos) (uint32_t)PULL_LE_U16(buf, pos)
#define IVAL(buf,pos) PULL_LE_U32(buf, pos)
#define SSVALX(buf,pos,val) (CVAL_NC(buf,pos)=(uint8_t)((val)&0xFF),CVAL_NC(buf,pos+1)=(uint8_t)((val)>>8))
#define SIVALX(buf,pos,val) (SSVALX(buf,pos,val&0xFFFF),SSVALX(buf,pos+2,val>>16))
#define SVALS(buf,pos) ((int16_t)SVAL(buf,pos))
#define IVALS(buf,pos) ((int32_t)IVAL(buf,pos))
#define SSVAL(buf,pos,val) PUSH_LE_U16(buf, pos, val)
#define SIVAL(buf,pos,val) PUSH_LE_U32(buf, pos, val)
#define SSVALS(buf,pos,val) PUSH_LE_U16(buf, pos, val)
#define SIVALS(buf,pos,val) PUSH_LE_U32(buf, pos, val)

/****************************************************************************
 *
 * ATTENTION: Do not use those macros anymore, use the ones from bytearray.h
 *
 ****************************************************************************/

/* 64 bit macros */
#define BVAL(p, ofs) PULL_LE_U64(p, ofs)
#define BVALS(p, ofs) ((int64_t)BVAL(p,ofs))
#define SBVAL(p, ofs, v) PUSH_LE_U64(p, ofs, v)
#define SBVALS(p, ofs, v) (SBVAL(p,ofs,(uint64_t)v))

/****************************************************************************
 *
 * ATTENTION: Do not use those macros anymore, use the ones from bytearray.h
 *
 ****************************************************************************/

/* now the reverse routines - these are used in nmb packets (mostly) */
#define SREV(x) ((((x)&0xFF)<<8) | (((x)>>8)&0xFF))
#define IREV(x) ((SREV(x)<<16) | (SREV((x)>>16)))
#define BREV(x) ((IREV((uint64_t)x)<<32) | (IREV(((uint64_t)x)>>32)))

/****************************************************************************
 *
 * ATTENTION: Do not use those macros anymore, use the ones from bytearray.h
 *
 ****************************************************************************/

#define RSVAL(buf,pos) (uint32_t)PULL_BE_U16(buf, pos)
#define RSVALS(buf,pos) PULL_BE_U16(buf, pos)
#define RIVAL(buf,pos) PULL_BE_U32(buf, pos)
#define RIVALS(buf,pos) PULL_BE_U32(buf, pos)
#define RBVAL(buf,pos) PULL_BE_U64(buf, pos)
#define RBVALS(buf,pos) PULL_BE_U64(buf, pos)
#define RSSVAL(buf,pos,val) PUSH_BE_U16(buf, pos, val)
#define RSSVALS(buf,pos,val) PUSH_BE_U16(buf, pos, val)
#define RSIVAL(buf,pos,val) PUSH_BE_U32(buf, pos, val)
#define RSIVALS(buf,pos,val) PUSH_BE_U32(buf, pos, val)
#define RSBVAL(buf,pos,val) PUSH_BE_U64(buf, pos, val)
#define RSBVALS(buf,pos,val) PUSH_BE_U64(buf, pos, val)

/****************************************************************************
 *
 * ATTENTION: Do not use those macros anymore, use the ones from bytearray.h
 *
 ****************************************************************************/

#endif /* _BYTEORDER_H */

个人实验

#include <stdio.h>
#include <stdint.h>
#include "byteorder.h"

int main() {
    char a[] = "1232323s";
    printf("%d\n", CVAL(a, 0));
    printf("%d\n", CVAL(a, 1));
    printf("%d\n", CVAL(a, 2));
    printf("%d\n", CVAL(a, 3));

    printf("%d\n", PVAL(a, 0)); // 49
    printf("%d\n", PVAL(a, 1)); // 50
    printf("%d\n", PVAL(a, 2));
    printf("%d\n", PVAL(a, 3));
    
    // `<<` 的优先级高于 `|`
    printf("%d\n", SVAL(a, 0)); // 50 * 256 + 49
    printf("%d\n", SVAL(a, 1));
    printf("%d\n", SVAL(a, 2));
    printf("%d\n", SVAL(a, 3));

    return 0;
}
➜  parse_replay gcc test.c -o test.out
➜  parse_replay ./test.out            
49
50
51
50
49
50
51
50
12849
13106
12851
13106

综合理解

CVAL(buf,pos) 将buf中pos处的字节以无符号字符返回
PVAL(buf,pos) 将buf中pos处的字节以无符号整型返回
SCVAL(buf,pos,val) 将buf中pos处的字节设置为val
SVAL(buf,pos) 将buf中pos处的字节以无符号小端整型(16bit,ushort)返回
IVAL(buf,pos) 将buf中pos处的字节以无符号小端整型(32bit)返回
SVALS(buf,pos) 将buf中pos处的字节以有符号小端整型(16bit)返回
IVALS(buf,pos) 将buf中pos处的字节以有符号小端整型(32bit)返回
SSVAL(buf,pos,val) 将buf中pos处的无符号小端整型(16bit)值设置为val
SIVAL(buf,pos,val) 将buf中pos处的无符号小端整型(32bit)值设置为val
SSVALS(buf,pos,val) 将buf中pos处的有符号小端整型(16bit)值设置为val
SIVALS(buf,pos,val) 将buf中pos处的有符号小端整型(32bit)值设置为val
RSVAL(buf,pos) 将buf中pos处的字节以无符号大端整型(16bit)返回
RIVAL(buf,pos) 将buf中pos处的字节以无符号大端整型(32bit)返回
RSSVAL(buf,pos,val) 将buf中pos处的无符号大端整型(16bit,ushort)值设置为val
RSIVAL(buf,pos,val) 将buf中pos处的无符号大端整型(32bit)值设置为val

参考

http://www.fei-le.com/article/204317.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值