常规使用

// 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 handlingCopyright (C) Andrew Tridgell 1992-1998This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe 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 ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See theGNU General Public License for more details.You should have received a copy of the GNU General Public Licensealong 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)); // 49printf("%d\n", PVAL(a, 1)); // 50printf("%d\n", PVAL(a, 2));printf("%d\n", PVAL(a, 3));// `<<` 的优先级高于 `|`printf("%d\n", SVAL(a, 0)); // 50 * 256 + 49printf("%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

CVAL,PVAL,SVAL宏定义相关推荐

  1. C++ #define(宏定义)的使用

    C++ 宏定义 #define命令是C++语言中的一个宏定义命令,它用来将一个标识符定义为一个字符串,该标识符被称为宏名,被定义的字符串称为替换文本. 宏定义的一般形式 #define <宏名& ...

  2. 如何用C语言改变宏定义的大小,C语言中宏定义使用的小细节

    C语言中宏定义使用的小细节 #pragma#pragma 预处理指令详解 在所有的预处理指令中,#Pragma 指令可能是最复杂的了,它的作用是设定编译器的状态或者是指示编译器完成一些特定的动作.#p ...

  3. c 宏定义用法#define

    转自:https://blog.csdn.net/boring_wednesday/article/details/78756696 宏定义 语法     #define name Stuff #de ...

  4. C语言宏定义使用技巧

    写好C语言,漂亮的宏定义很重要,使用宏定义可以防止出错,提高可移植性,可读性,方便性等等.下面列举一些成熟软件中常用得宏定义...... 1,防止一个头文件被重复包含 #ifndef COMDEF_H ...

  5. 【C语言】07-预处理指令;-宏定义

    预处理指令简介; 1,C在对源程序进行编译之前,会对一些特殊的预处理指令作解释,产生一个新的源程序,此过程叫做编译预处理.C在经过编译预处理之后才对新的源码进行通常的编译; 2,预处理以'#'开头,且 ...

  6. 巧用宏定义进行调试 (转)

    巧用宏定义进行调试 在进行程序设计时,有时我们往往不希望借住于调试工具(如:gdb, vc),而以输出调试信息的方式进行调试时,我们就可以借住于强大的宏定义来进行调试.    一.在GCC下的定义方法 ...

  7. python用海伦公式求面积_用带参数的宏定义,通过海伦公式求三角形的面积

    要求: 海伦公式: ,其中 ,a,b,c为三角形的三个边.定义两个带参数的宏,一个用来求p,另一个用来求s 题目分析: 首先,题目要求是通过输入三角形的三条边,输出三角形的面积.因为不是任意的三条边都 ...

  8. iOS 开发 高级:使用 宏定义macros (#,##,...,__VA_ARGS_)

    一直以来用宏定义#define也就是定义一些简单的常量,至多也就是定义一个函数,很少关注宏定义的用法.直到看到这样的代码: [cpp] view plaincopy #define PLAYSOUND ...

  9. IOS,十六进制颜色和RGB颜色的宏定义

    2019独角兽企业重金招聘Python工程师标准>>> 16进制颜色值宏定义. //调用NSString *str = model.Color; NSString *strColor ...

  10. 关于函数式宏定义的学习

    题记:回头看看原本学过的知识,发现那些原本以为理解了的皮毛都突然让自己变得陌生.逐步学习,不懈努力. 测试题目时发现得出的结论与自己给出的不同,于是查阅资料,重新学习宏定义相关的文章.在C与C++编程 ...

最新文章

  1. 常用MySQL数据库命令
  2. Go语言fmt.Printf使用指南(占位符总结)
  3. [译]BitTorrent协议规范
  4. Broadcom获得65亿美元过度贷款以收购Brocade
  5. d).关于steal lock
  6. error: ‘nullptr’ was not declared in this scope
  7. 大数据公司数据挖掘的49个案例
  8. 运用matlab求黑塞矩阵,用matlab求黑塞矩阵
  9. java:解一元二次方程
  10. WML 中文参考手册
  11. 请在mysql配置文件修sql-mode或sql_mode为NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTIT windows下设置mysql的sql_mode
  12. 金融数据分析 实验四 金融风险价值计算
  13. docekrfile
  14. SQL 清空数据库的所有表数据
  15. 用php打印出日历_php简单日历函数
  16. chrome顶部变黑_黑暗模式来了!最新版Chrome浏览器让你的网页通通变黑(内附设置教程)...
  17. 为hexo添加hexo-admin组件
  18. Idea查看tomcat日志及部分错误例子
  19. React 入门实例教程(原作者: 阮一峰)
  20. 第二届2011年国信蓝点杯软件设计大赛预赛的试题5

热门文章

  1. 74HC573并联输出
  2. 超分算法在 WebRTC 高清视频传输弱网优化中的应用
  3. web开发第三方登陆之facebook登陆
  4. 决策树算法原理——cart
  5. OpenGL(十七)——Qt OpenGL在三维空间移动位图(会动的星星)
  6. Facebook反爬虫注册策略分析及养号实战
  7. 毕加索传记的艺术和历史
  8. 基于at89c51单片机的led数字倒计时器设计c语言,基于AT89C51单片机的LED数字倒计时器设计.docx...
  9. 判断输入的邮箱格式是否正确
  10. 【Docker学习笔记 七】深入理解Docker网络配置及微服务部署