1. BLOB二进制对象(blob.c/h)
  2. 数据结构
  3.  
  4. struct blob_attr {
  5. uint32_t id_len; /** 高1位为extend标志,高7位存储id,
  6. * 低24位存储data的内存大小 */
  7. char data[];
  8. } __packed;
  9.  
  10. struct blob_attr_info {
  11. unsigned int type;
  12. unsigned int minlen;
  13. unsigned int maxlen;
  14. bool (*validate)(const struct blob_attr_info *, struct blob_attr *);
  15. };
  16.  
  17. struct blob_buf {
  18. struct blob_attr *head;
  19. bool (*grow)(struct blob_buf *buf, int minlen);
  20. int buflen;
  21. void *buf;
  22. };
  23.  
  24. 存储结构
  25.  
  26. blob_buff内存结构
  27. 接口说明
  28. 获取BLOB属性信息
  29.  
  30. /**
  31. * 返回指向BLOB属性数据区指针
  32. */
  33. static inline void * blob_data(const struct blob_attr *attr)
  34.  
  35. /**
  36. * 返回BLOB属性ID
  37. */
  38. static inline unsigned int blob_id(const struct blob_attr *attr)
  39.  
  40. /**
  41. * 判断BLOB属性扩展标志是否为真
  42. */
  43. static inline bool blob_is_extended(const struct blob_attr *attr)
  44.  
  45. /**
  46. * 返回BLOB属性有效存储空间大小
  47. */
  48. static inline unsigned int blob_len(const struct blob_attr *attr)
  49.  
  50. /*
  51. * 返回BLOB属性完全存储空间大小(包括头部)
  52. */
  53. static inline unsigned int blob_raw_len(const struct blob_attr *attr)
  54.  
  55. /*
  56. * 返回BLOB属性填补后存储空间大小(包括头部)
  57. */
  58. static inline unsigned int blob_pad_len(const struct blob_attr *attr)
  59.  
  60. 获取BLOB数据信息
  61.  
  62. static inline uint8_t blob_get_u8(const struct blob_attr *attr)
  63.  
  64. static inline uint16_t blob_get_u16(const struct blob_attr *attr)
  65.  
  66. static inline uint32_t blob_get_u32(const struct blob_attr *attr)
  67.  
  68. static inline uint64_t blob_get_u64(const struct blob_attr *attr)
  69.  
  70. static inline int8_t blob_get_int8(const struct blob_attr *attr)
  71.  
  72. static inline int16_t blob_get_int16(const struct blob_attr *attr)
  73.  
  74. static inline int32_t blob_get_int32(const struct blob_attr *attr)
  75.  
  76. static inline int64_t blob_get_int64(const struct blob_attr *attr)
  77.  
  78. static inline const char * blob_get_string(const struct blob_attr *attr)
  79.  
  80. 设置BLOB数据信息
  81.  
  82. static inline struct blob_attr *
  83. blob_put_string(struct blob_buf *buf, int id, const char *str)
  84.  
  85. static inline struct blob_attr *
  86. blob_put_u8(struct blob_buf *buf, int id, uint8_t val)
  87.  
  88. static inline struct blob_attr *
  89. blob_put_u16(struct blob_buf *buf, int id, uint16_t val)
  90.  
  91. static inline struct blob_attr *
  92. blob_put_u32(struct blob_buf *buf, int id, uint32_t val)
  93.  
  94. static inline struct blob_attr *
  95. blob_put_u64(struct blob_buf *buf, int id, uint64_t val)
  96.  
  97. #define blob_put_int8 blob_put_u8
  98. #define blob_put_int16 blob_put_u16
  99. #define blob_put_int32 blob_put_u32
  100. #define blob_put_int64 blob_put_u64
  101.  
  102. struct blob_attr *
  103. blob_put(struct blob_buf *buf, int id, const void *ptr, unsigned int len)
  104.  
  105. /**
  106. * ptr - 指向struct blob_attr
  107. */
  108. struct blob_attr *
  109. blob_put_raw(struct blob_buf *buf, const void *ptr, unsigned int len)
  110.  
  111. 遍历
  112.  
  113. #define __blob_for_each_attr(pos, attr, rem)
  114. #define blob_for_each_attr(pos, attr, rem)
  115.  
  116. 复制
  117.  
  118. struct blob_attr * blob_memdup(struct blob_attr *attr)
  119.  
  120. 数据类型判断
  121.  
  122. enum {
  123. BLOB_ATTR_UNSPEC,
  124. BLOB_ATTR_NESTED, /** 嵌套 */
  125. BLOB_ATTR_BINARY,
  126. BLOB_ATTR_STRING,
  127. BLOB_ATTR_INT8,
  128. BLOB_ATTR_INT16,
  129. BLOB_ATTR_INT32,
  130. BLOB_ATTR_INT64,
  131. BLOB_ATTR_LAST
  132. };
  133. bool blob_check_type(const void *ptr, unsigned int len, int type)
  134.  
  135. 嵌套操作
  136.  
  137. void * blob_nest_start(struct blob_buf *buf, int id)
  138. Void blob_nest_end(struct blob_buf *buf, void *cookie)
  139.  
  140. 判断
  141.  
  142. bool blob_attr_equal(const struct blob_attr *a1, const struct blob_attr *a2)
  143.  
  144. 初始/销毁
  145.  
  146. /**
  147. * 初始化BLOB buffer
  148. */
  149. int blob_buf_init(struct blob_buf *buf, int id)
  150.  
  151. /**
  152. * 销毁BLOB buffer
  153. */
  154. void blob_buf_free(struct blob_buf *buf)
  155.  
  156. 解析BLOB
  157.  
  158. /**
  159. * 从attr串中根据info策略过滤,得到的结果存储在data属性数组中
  160. *
  161. * @param attr 输入BLOB属性串
  162. * @param data 输出BLOB属性数组
  163. * @param info 属性过滤策略
  164. * @param max data数组大小
  165. */
  166. int blob_parse(struct blob_attr *attr, struct blob_attr **data,
  167. const struct blob_attr_info *info, int max)
  168.  
  169. BLOB消息对象(blobmsg.c/h)
  170. 数据结构
  171.  
  172. struct blobmsg_hdr {
  173. uint16_t namelen;
  174. uint8_t name[];
  175. } __packed;
  176.  
  177. struct blobmsg_policy {
  178. const char *name;
  179. enum blobmsg_type type;
  180. };
  181.  
  182. 存储结构
  183.  
  184. blobmsg内存结构
  185. 消息类型
  186.  
  187. enum blobmsg_type {
  188. BLOBMSG_TYPE_UNSPEC,
  189. BLOBMSG_TYPE_ARRAY,
  190. BLOBMSG_TYPE_TABLE,
  191. BLOBMSG_TYPE_STRING,
  192. BLOBMSG_TYPE_INT64,
  193. BLOBMSG_TYPE_INT32,
  194. BLOBMSG_TYPE_INT16,
  195. BLOBMSG_TYPE_INT8,
  196. __BLOBMSG_TYPE_LAST,
  197. BLOBMSG_TYPE_LAST = __BLOBMSG_TYPE_LAST - ,
  198. BLOBMSG_TYPE_BOOL = BLOBMSG_TYPE_INT8,
  199. };
  200.  
  201. 接口说明
  202. 基本操作
  203.  
  204. /**
  205. * 根据BLOB消息名字长度计算出blobmsg头部大小
  206. */
  207. static inline int blobmsg_hdrlen(unsigned int namelen)
  208.  
  209. /**
  210. * 获取BLOB消息名字
  211. */
  212. static inline const char *blobmsg_name(const struct blob_attr *attr)
  213.  
  214. /**
  215. * 获取BLOB消息类型
  216. */
  217. static inline int blobmsg_type(const struct blob_attr *attr)
  218.  
  219. /**
  220. * 获取BLOB消息数据内容
  221. */
  222. static inline void *blobmsg_data(const struct blob_attr *attr)
  223.  
  224. /**
  225. * 获取BLOB消息数据内容大小
  226. */
  227. static inline int blobmsg_data_len(const struct blob_attr *attr)
  228. static inline int blobmsg_len(const struct blob_attr *attr)
  229.  
  230. 数据类型判断
  231.  
  232. /**
  233. * 判断BLOBMSG属性类型是否合法
  234. */
  235. bool blobmsg_check_attr(const struct blob_attr *attr, bool name)
  236.  
  237. 设置
  238.  
  239. int blobmsg_add_field(struct blob_buf *buf, int type, const char *name,
  240. const void *data, unsigned int len)
  241.  
  242. static inline int
  243. blobmsg_add_u8(struct blob_buf *buf, const char *name, uint8_t val)
  244.  
  245. static inline int
  246. blobmsg_add_u16(struct blob_buf *buf, const char *name, uint16_t val)
  247.  
  248. static inline int
  249. blobmsg_add_u32(struct blob_buf *buf, const char *name, uint32_t val)
  250.  
  251. static inline int
  252. blobmsg_add_u64(struct blob_buf *buf, const char *name, uint64_t val)
  253.  
  254. static inline int
  255. blobmsg_add_string(struct blob_buf *buf, const char *name, const char *string)
  256.  
  257. static inline int
  258. blobmsg_add_blob(struct blob_buf *buf, struct blob_attr *attr)
  259.  
  260. /**
  261. * 格式化设备BLOGMSG
  262. */
  263. void blobmsg_printf(struct blob_buf *buf, const char *name, const char *format, ...)
  264.  
  265. 获取
  266.  
  267. static inline uint8_t blobmsg_get_u8(struct blob_attr *attr)
  268. static inline bool blobmsg_get_bool(struct blob_attr *attr)
  269. static inline uint16_t blobmsg_get_u16(struct blob_attr *attr)
  270. static inline uint32_t blobmsg_get_u32(struct blob_attr *attr)
  271. static inline uint64_t blobmsg_get_u64(struct blob_attr *attr)
  272. static inline char *blobmsg_get_string(struct blob_attr *attr)
  273.  
  274. 创建
  275.  
  276. /**
  277. * 创建BLOBMSG,返回数据区开始地址
  278. */
  279. void *blobmsg_alloc_string_buffer(struct blob_buf *buf, const char *name,
  280. unsigned int maxlen)
  281.  
  282. /**
  283. * 扩大BLOGMSG,返回数据区开始地址
  284. */
  285. void *blobmsg_realloc_string_buffer(struct blob_buf *buf, unsigned int maxlen)
  286.  
  287. void blobmsg_add_string_buffer(struct blob_buf *buf)
  288.  
  289. 遍历
  290.  
  291. #define blobmsg_for_each_attr(pos, attr, rem)
  292.  
  293. 嵌套
  294.  
  295. static inline void * blobmsg_open_array(struct blob_buf *buf, const char *name)
  296. static inline void blobmsg_close_array(struct blob_buf *buf, void *cookie)
  297.  
  298. static inline void *blobmsg_open_table(struct blob_buf *buf, const char *name)
  299. static inline void blobmsg_close_table(struct blob_buf *buf, void *cookie)
  300.  
  301. 解析BLOGMSG
  302.  
  303. /**
  304. * 从data BLOGMSG串中根据policy策略过滤,得到的结果存储在tb BLOGATTR数组中
  305. *
  306. * @param policy 过滤策略
  307. * @param policy_len 策略个数
  308. * @param tb 返回属性数据
  309. * @param len data属性个数
  310. */
  311. int blobmsg_parse(const struct blobmsg_policy *policy, int policy_len,
  312. struct blob_attr **tb, void *data, unsigned int len)
  313.  
  314. 例子
  315. UCI转化为BLOB
  316.  
  317. UCI配置文件:
  318. /etc/config/test
  319.  
  320. config policy test
  321. option name 'test'
  322. option enable '
  323. option dns '1.1.1.1 2.2.2.2'
  324.  
  325. 定义参数列表:
  326.  
  327. enum {
  328. POLICY_ATTR_NAME, /** name */
  329. POLICY_ATTR_ENABLE, /** enable */
  330. POLICY_ATTR_DNS, /** dns */
  331. __POLICY_ATTR_MAX
  332. };
  333.  
  334. static const struct blobmsg_policy policy_attrs[__POLICY_ATTR_MAX] = {
  335. [POLICY_ATTR_NAME] = { .name = "name", .type = BLOBMSG_TYPE_STRING },
  336. [POLICY_ATTR_ENABLE] = { .name = "enable", .type = BLOBMSG_TYPE_BOOL },
  337. [POLICY_ATTR_DNS] = { .name = "dns", .type = BLOBMSG_TYPE_ARRAY },
  338. };
  339.  
  340. /** 定义BLOBMSG_TYPE_ARRAY类型参数的实际数据类型 */
  341. static const struct uci_blob_param_info policy_attr_info[__POLICY_ATTR_MAX] = {
  342. [POLICY_ATTR_DNS] = { .type = BLOBMSG_TYPE_STRING },
  343. };
  344.  
  345. static const struct uci_blob_param_list policy_attr_list = {
  346. .n_params = __POLICY_ATTR_MAX,
  347. .params = policy_attrs,
  348. .info = policy_attr_info,
  349. };
  350.  
  351. 转化为BLOB:
  352.  
  353. static struct uci_context *g_uci_ctx;
  354. static struct blob_buf *b;
  355.  
  356. void
  357. transform(const char *config)
  358. {
  359. struct uci_context *ctx = g_uci_ctx;
  360. struct uci_package *p = NULL;
  361.  
  362. if (!ctx) {
  363. ctx = uci_alloc_context();
  364. g_uci_ctx = ctx;
  365. uci_set_confdir(ctx, NULL);
  366. } else {
  367. p = uci_lookup_package(ctx, config);
  368. if (p)
  369. uci_unload(ctx, p);
  370. }
  371.  
  372. if (uci_load(ctx, config, &p))
  373. return;
  374.  
  375. struct uci_element *e;
  376. struct blob_attr *config = NULL;
  377. uci_foreach_element(&p->sectons, e) {
  378. struct uci_section *s = uci_to_section(e);
  379.  
  380. blob_buf_init(&b, );
  381. uci_to_blob(&b, s, &policy_attr_list);
  382. config = blob_memdup(b.head);
  383.  
  384. /**
  385. * do something with `config`
  386. * free(config), when not use it
  387. */
  388. }
  389. }
  390.  
  391. 使用转化后的blob_attr
  392.  
  393. void
  394. foo(blob_attr *confg)
  395. {
  396. struct blob_attr *tb[__POLICY_ATTR_MAX];
  397.  
  398. blobmsg_parse(policy_attrs, __POLICY_ATTR_MAX, tb,
  399. blob_data(config), blob_len(config));
  400.  
  401. /**
  402. * do something with *tb[]
  403. */
  404. }

BLOB二进制对象(blob.c/h)的更多相关文章

  1. MySql中Blob二进制对象的处理

    BLOB (binary large object),二进制大对象,是一个可以存储二进制文件的容器. 可以用于存储图片等信息 Demo1:存储图片 String sql="INSERT IN ...

  2. JDBC10 Blob二进制对象

    //将图片输入到数据库中 // String sql="insert into t_user2 (username,headImg) values (?,?)"; // ps=co ...

  3. html5中二进制对象Blob的使用——Blob与ArrayBuffer、TypeArray和String的相互转换

    在网页开发中遇到这样一个问题,在使用select的时候,想让里面的文字水平居中.首先想到的是text-align:center;但是发现在Chrome浏览器下不兼容,需要使用到text-align-l ...

  4. SharePoint 2013 对二进制大型对象(BLOB)进行爬网

    本文是参考MSDN文档做的示例,SharePoint 2013搜索二进制对象(BLOB),通过外部内容类型的方式将外部数据与SharePoint相关联,修改BCD模型,使SharePoint能够爬网外 ...

  5. HTML5中的二进制大对象Blob(转)

    HTML5中的Blob对象和MYSQL中的BLOB类型在概念上是有点区别的.MYSQL中的BLOB类型就只是个二进制数据容器.而HTML5中的Blob对象除了存放二进制数据外还可以设置这个数据的MIN ...

  6. 二进制学习——Blob,ArrayBuffer、File、FileReader和FormData的区别

    前言: Blob.ArrayBuffer.File.fileReader.formData这些名词总是经常看到,知道一点又好像不知道,像是同一个东西好像又不是,总是模模糊糊,最近终于下决心要弄清楚. ...

  7. [转]DataURL与File,Blob,canvas对象之间的互相转换的Javascript

    来源 http://blog.csdn.net/cuixiping/article/details/45932793 canvas转换为dataURL (从canvas获取dataURL) var d ...

  8. DataURL与File,Blob,canvas对象之间的互相转换的Javascript

    canvas转换为dataURL (从canvas获取dataURL) var dataurl = canvas.toDataURL('image/png'); var dataurl2 = canv ...

  9. JAVA处理Blob大对象

    Blob对象是SQL Blob的Java语言映射.SQL Blob是一个内置类型,它可以将一个二进制大对象保存在数据库中.接口ResultSet.CallableStatement和PreparedS ...

随机推荐

  1. wan口mac=lan口mac加一,wlan是lan口mac加二

    (1)路由器有两个mac地址,一个用于外网(wan),一个用于内网(wlan和lan): (2)一般路由器上面或者配置路由器的网页上面只标注外网的mac地址: (3)内网的mac地址和外网mac地址一 ...

  2. cf437B The Child and Set

    B. The Child and Set time limit per test 1 second memory limit per test 256 megabytes input standard ...

  3. spring加载properties配置文件

    public static void main(String[] args){    String path="E:/workspace/bocMarketData/src/config/P ...

  4. Linux下的文件查找类命令(转载)

    如何快速有效的定位文件系统内所需要查找的文件呢?Linux为我们提供了一些文件查找类的命令,我们需要掌握以下几个命令: http://blog.csdn.net/sailor201211/articl ...

  5. [转] Trie树详解及其应用

    一.知识简介         最近在看字符串算法了,其中字典树.AC自动机和后缀树的应用是最广泛的了,下面将会重点介绍下这几个算法的应用.       字典树(Trie)可以保存一些字符串->值 ...

  6. 关于ognl.OgnlException: target is null for setProperty(null的解决方案

    在跑struts2的时候有时候会出现上面的错,特别是新手, 这种情况是在struts2高级的POJO访问时候出现的s 警告: Error setting expression 'user.passwo ...

  7. poj 3692 Kindergarten (最大独立集之逆匹配)

    Description In a kindergarten, there are a lot of kids. All girls of the kids know each other and al ...

  8. css中的段落样式及背景

    一.段落样式 css中关于段落的样式主要有行高,缩进,段落对齐,文字间距,文字溢出,段落换行等.它们的具体语法如下: line-height : normal | length text-indent ...

  9. RMAN的show,list,crosscheck,delete命令

    1.SHOW命令:      显示rman配置: RMAN> show all; 2.REPORT命令: 2.1.RMAN> report schema 报告目标数据库的物理结构; 2.2 ...

  10. [Editor(typeof(ImageUrlEditor), typeof(UITypeEditor))]无效的可能原因

    开发的用户控件封存在dll中,其他都很顺利,就是这个图片弹出选择路径怎么也搞不出来!(浪费了我半天*2,o(︶︿︶)o 唉,犟脾气拗不过 看了很多搜索信息都说加: [Editor(typeof(Ima ...