xmledit.c.html 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512
  1. <?xml version='1.0' encoding='iso-8859-1'?>
  2. <!doctype html public '-//W3C//DTD XHTML 1.0 Strict//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'>
  3. <html xmlns='http://www.w3c.org/1999/xhtml' lang='en-us'>
  4. <head>
  5. <title>
  6. xmledit.c
  7. </title>
  8. <meta http-equiv='content-type' content='text/html;iso-8859-1'/>
  9. <meta name='generator' content='motley-tools 1.9.4 13:40:33 Feb 18 2015'/>
  10. <meta name='author' content='cmaier@cmassoc.net'/>
  11. <meta name='robots' content='noindex,nofollow'/>
  12. <link href='toolkit.css' rel='stylesheet' type='text/css'/>
  13. </head>
  14. <body>
  15. <div class='headerlink'>
  16. [<a href='xmldata.c.html' title=' xmldata.c '>PREV</a>]
  17. [<a href='toolkit.html' title=' Index '>HOME</a>]
  18. [<a href='xmlelement.c.html' title=' xmlelement.c '>NEXT</a>]
  19. </div>
  20. <pre>
  21. /*====================================================================*
  22. *
  23. * xmledit.c -
  24. *
  25. * node.h
  26. *
  27. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  28. * Copyright (c) 2001-2006 by Charles Maier Associates;
  29. * Licensed under the Internet Software Consortium License;
  30. *
  31. *--------------------------------------------------------------------*/
  32. #ifndef XMLEDIT_SOURCE
  33. #define XMLEDIT_SOURCE
  34. /*====================================================================*
  35. * system header files;
  36. *--------------------------------------------------------------------*/
  37. #include &lt;stdint.h&gt;
  38. #include &lt;string.h&gt;
  39. #include &lt;limits.h&gt;
  40. #include &lt;ctype.h&gt;
  41. #include &lt;errno.h&gt;
  42. /*====================================================================*
  43. * custom header files;
  44. *--------------------------------------------------------------------*/
  45. #include &quot;../tools/number.h&quot;
  46. #include &quot;../tools/memory.h&quot;
  47. #include &quot;../tools/error.h&quot;
  48. #include &quot;../nodes/node.h&quot;
  49. /*====================================================================*
  50. * constants;
  51. *--------------------------------------------------------------------*/
  52. #define XML_BAD_NUMBER 1
  53. #define XML_BAD_OFFSET 2
  54. #define XML_BAD_EXTENT 3
  55. /*====================================================================*
  56. * variables;
  57. *--------------------------------------------------------------------*/
  58. static char const * member = &quot;&quot;;
  59. static char const * string = &quot;&quot;;
  60. static unsigned offset = 0;
  61. static unsigned length = 0;
  62. static bool series = false;
  63. /*====================================================================*
  64. *
  65. * void position (size_t extent);
  66. *
  67. * sanity check offset and extent before editing memory;
  68. *
  69. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  70. * Copyright (c) 2001-2006 by Charles Maier Associates;
  71. * Licensed under the Internet Software Consortium License;
  72. *
  73. *--------------------------------------------------------------------*/
  74. static void position (size_t extent)
  75. {
  76. if (!length)
  77. {
  78. error (XML_BAD_EXTENT, EPERM, &quot;%s has no length&quot;, member);
  79. }
  80. if (offset &gt; extent)
  81. {
  82. error (XML_BAD_OFFSET, EPERM, &quot;%s offset of 0x%04X exceeds &quot; DATA_OBJECT &quot; offset of 0x%04X&quot;, member, offset, (unsigned int) extent);
  83. }
  84. if ((offset + length) &gt; extent)
  85. {
  86. error (XML_BAD_EXTENT, EPERM, &quot;%s length of %u bytes exceeds &quot; DATA_OBJECT &quot; length of &quot; SIZE_T_SPEC &quot; bytes&quot;, member, length, extent);
  87. }
  88. return;
  89. }
  90. /*====================================================================*
  91. *
  92. * signed xmlinteger (NODE const * node, unsigned radix);
  93. *
  94. * convert numeric string to an unsigned integer; all string digits
  95. * string digits must be valid for the specifid radix; radix can be
  96. * 1 through 16 but 2, 8, 10 and 16 are the only sensible choices;
  97. *
  98. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  99. * Copyright (c) 2001-2006 by Charles Maier Associates;
  100. * Licensed under the Internet Software Consortium License;
  101. *
  102. *--------------------------------------------------------------------*/
  103. static unsigned xmlinteger (NODE const * node, unsigned radix)
  104. {
  105. unsigned digit;
  106. unsigned value = 0;
  107. while ((digit = todigit (*string)) &lt; radix)
  108. {
  109. value *= radix;
  110. value += digit;
  111. string++;
  112. }
  113. if (*string)
  114. {
  115. error (XML_BAD_NUMBER, EPERM, &quot;%s %s is not numeric&quot;, member, node-&gt;text);
  116. }
  117. return (value);
  118. }
  119. /*====================================================================*
  120. *
  121. * void xmlstring (void * memory, size_t extent);
  122. *
  123. * xmlstring is expressed as character text; truncate string and
  124. * pad memory with NULs as needed;
  125. *
  126. * per the schema, an series cannot have a string member;
  127. *
  128. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  129. * Copyright (c) 2001-2006 by Charles Maier Associates;
  130. * Licensed under the Internet Software Consortium License;
  131. *
  132. *--------------------------------------------------------------------*/
  133. static void xmlstring (void * memory, size_t extent)
  134. {
  135. char * buffer = (char *)(memory);
  136. if (series)
  137. {
  138. error (XML_BAD_NUMBER, ENOTSUP, &quot;%s found inside struct&quot;, member);
  139. }
  140. if (length)
  141. {
  142. while (length &gt; 1)
  143. {
  144. if (isprint (*string))
  145. {
  146. buffer [offset] = *string++;
  147. }
  148. else
  149. {
  150. buffer [offset] = (char)(0);
  151. }
  152. offset++;
  153. length--;
  154. }
  155. buffer [offset] = (char)(0);
  156. offset++;
  157. length--;
  158. }
  159. return;
  160. }
  161. /*====================================================================*
  162. *
  163. * void xmlmemory (void * memory, size_t extent);
  164. *
  165. * xmlmemory is a hexadecimal string of variable extent; an empty
  166. * string increments offset and decrements length but nothing is
  167. * written to the memory;
  168. *
  169. * per the schema, if xmlmemory is not inside an series then it must
  170. * match the object extent;
  171. *
  172. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  173. * Copyright (c) 2001-2006 by Charles Maier Associates;
  174. * Licensed under the Internet Software Consortium License;
  175. *
  176. *--------------------------------------------------------------------*/
  177. static void xmlmemory (void * memory, size_t extent)
  178. {
  179. uint8_t * buffer = (uint8_t *)(memory);
  180. if (!*string)
  181. {
  182. offset++;
  183. length--;
  184. }
  185. while ((*string) &amp;&amp; (length))
  186. {
  187. uint8_t msb = todigit (*string++);
  188. uint8_t lsb = todigit (*string++);
  189. if ((msb &gt; 0x0F) || (lsb &gt; 0x0F))
  190. {
  191. error (XML_BAD_NUMBER, EINVAL, &quot;%s value is not hexadecimal&quot;, member);
  192. }
  193. buffer [offset] = (msb &lt;&lt; 4) + lsb;
  194. offset++;
  195. length--;
  196. }
  197. if ((length) &amp;&amp; (!series))
  198. {
  199. error (XML_BAD_NUMBER, EINVAL, &quot;%s value is too short&quot;, member);
  200. }
  201. if (*string)
  202. {
  203. error (XML_BAD_NUMBER, EINVAL, &quot;%s value is too long&quot;, member);
  204. }
  205. return;
  206. }
  207. /*====================================================================*
  208. *
  209. * void xmlnumber (void * memory, size_t extent);
  210. *
  211. * xmlnumber is a decimal integer string of variable length; the
  212. * value cannot exceed length bytes and offset is incremented by
  213. * length bytes;
  214. *
  215. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  216. * Copyright (c) 2001-2006 by Charles Maier Associates;
  217. * Licensed under the Internet Software Consortium License;
  218. *
  219. *--------------------------------------------------------------------*/
  220. static void xmlnumber (void * memory, size_t extent)
  221. {
  222. uint64_t number = 0;
  223. uint64_t maximum = 0;
  224. maximum = ~maximum;
  225. if (length &lt; sizeof (number))
  226. {
  227. maximum &lt;&lt;= (length &lt;&lt; 3);
  228. maximum = ~maximum;
  229. }
  230. while (isdigit (*string))
  231. {
  232. number *= 10;
  233. number += *string - '0';
  234. if (number &gt; maximum)
  235. {
  236. error (XML_BAD_NUMBER, EINVAL, &quot;%s value exceeds %u bytes&quot;, member, length);
  237. }
  238. string++;
  239. }
  240. if (*string)
  241. {
  242. error (XML_BAD_NUMBER, EINVAL, &quot;%s value is not decimal&quot;, member);
  243. }
  244. memcpy ((uint8_t *)(memory) + offset, &amp;number, length);
  245. offset += length;
  246. length -= length;
  247. return;
  248. }
  249. /*====================================================================*
  250. *
  251. * void xmlbyte (void * memory, unsigned extent);
  252. *
  253. * xmlbyte is a decimal integer string of variable extent; the
  254. * value cannot exceed 255; an empty string increments offset and
  255. * decrements length but nothing is written to the memory;
  256. *
  257. * per the schema, if xmlbyte is not inside an series then it must
  258. * it must match the object extent which must be 1 by implication;
  259. *
  260. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  261. * Copyright (c) 2001-2006 by Charles Maier Associates;
  262. * Licensed under the Internet Software Consortium License;
  263. *
  264. *--------------------------------------------------------------------*/
  265. static void xmlbyte (void * memory, size_t extent)
  266. {
  267. if (*string)
  268. {
  269. uint16_t number = 0;
  270. while (isdigit (*string))
  271. {
  272. number *= 10;
  273. number += *string - '0';
  274. if (number &gt; 0xFF)
  275. {
  276. error (XML_BAD_NUMBER, EINVAL, &quot;%s value exceeds 8 bits&quot;, member);
  277. }
  278. string++;
  279. }
  280. if (*string)
  281. {
  282. error (XML_BAD_NUMBER, EINVAL, &quot;%s value is not decimal&quot;, member);
  283. }
  284. memcpy ((uint8_t *)(memory) + offset, &amp;number, sizeof (uint8_t));
  285. }
  286. offset++;
  287. length--;
  288. if ((length) &amp;&amp; (!series))
  289. {
  290. error (XML_BAD_NUMBER, EINVAL, &quot;%s is too short&quot;, member);
  291. }
  292. return;
  293. }
  294. /*====================================================================*
  295. *
  296. * static char const * xmlcontent1 (struct node const * node);
  297. *
  298. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  299. * Copyright (c) 2001-2006 by Charles Maier Associates;
  300. * Licensed under the Internet Software Consortium License;
  301. *
  302. *--------------------------------------------------------------------*/
  303. static char const * xmlcontent1 (struct node const * node)
  304. {
  305. if (node)
  306. {
  307. node = node-&gt;below;
  308. }
  309. while (node)
  310. {
  311. if (node-&gt;type == NODE_DATA)
  312. {
  313. return (node-&gt;text);
  314. }
  315. node = node-&gt;after;
  316. }
  317. return (&quot;&quot;);
  318. }
  319. /*====================================================================*
  320. *
  321. * char const * xmlvalue1 (struct node const * node);
  322. *
  323. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  324. * Copyright (c) 2001-2006 by Charles Maier Associates;
  325. * Licensed under the Internet Software Consortium License;
  326. *
  327. *--------------------------------------------------------------------*/
  328. char const * xmlvalue1 (struct node const * node)
  329. {
  330. if (node)
  331. {
  332. node = node-&gt;below;
  333. }
  334. while (node)
  335. {
  336. if (node-&gt;type == NODE_VALU)
  337. {
  338. return (node-&gt;text);
  339. }
  340. node = node-&gt;after;
  341. }
  342. return (&quot;&quot;);
  343. }
  344. /*====================================================================*
  345. *
  346. * static char const * xmlattribute1 (struct node const * node, char const * name);
  347. *
  348. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  349. * Copyright (c) 2001-2006 by Charles Maier Associates;
  350. * Licensed under the Internet Software Consortium License;
  351. *
  352. *--------------------------------------------------------------------*/
  353. static char const * xmlattribute1 (struct node const * node, char const * name)
  354. {
  355. if (node)
  356. {
  357. node = node-&gt;below;
  358. }
  359. while (node)
  360. {
  361. if (node-&gt;type == NODE_ATTR)
  362. {
  363. if (!strcmp (node-&gt;text, name))
  364. {
  365. name = xmlvalue1 (node);
  366. return (name);
  367. }
  368. }
  369. node=node-&gt;after;
  370. }
  371. return (&quot;&quot;);
  372. }
  373. /*====================================================================*
  374. *
  375. * signed xmledit (struct node const * node, void * memory, size_t extent);
  376. *
  377. * Motley Tools by Charles Maier &lt;cmaier@cmassoc.net&gt;;
  378. * Copyright (c) 2001-2006 by Charles Maier Associates;
  379. * Licensed under the Internet Software Consortium License;
  380. *
  381. *--------------------------------------------------------------------*/
  382. signed xmledit (struct node const * node, void * memory, size_t extent)
  383. {
  384. if (node)
  385. {
  386. node = node-&gt;below;
  387. }
  388. while (node)
  389. {
  390. if (node-&gt;type == NODE_ELEM)
  391. {
  392. if (!strcmp (node-&gt;text, DATA_MEMBER))
  393. {
  394. member = xmlattribute1 (node, DATA_NAME);
  395. offset = (unsigned)(-1);
  396. length = (unsigned)(-1);
  397. series = false;
  398. }
  399. else if (!strcmp (node-&gt;text, DATA_OFFSET))
  400. {
  401. string = xmlcontent1 (node);
  402. offset = xmlinteger (node, 16);
  403. }
  404. else if (!strcmp (node-&gt;text, DATA_LENGTH))
  405. {
  406. string = xmlcontent1 (node);
  407. length = xmlinteger (node, 10);
  408. }
  409. else if (!strcmp (node-&gt;text, DATA_STRUCT))
  410. {
  411. series = true;
  412. }
  413. else if (!strcmp (node-&gt;text, DATA_STRING))
  414. {
  415. string = xmlcontent1 (node);
  416. position (extent);
  417. xmlstring (memory, extent);
  418. }
  419. else if (!strcmp (node-&gt;text, DATA_MEMORY))
  420. {
  421. string = xmlcontent1 (node);
  422. position (extent);
  423. xmlmemory (memory, extent);
  424. }
  425. else if (!strcmp (node-&gt;text, DATA_HUGE))
  426. {
  427. length = sizeof (uint64_t);
  428. position (extent);
  429. string = xmlcontent1 (node);
  430. xmlnumber (memory, extent);
  431. }
  432. else if (!strcmp (node-&gt;text, DATA_LONG))
  433. {
  434. length = sizeof (uint32_t);
  435. position (extent);
  436. string = xmlcontent1 (node);
  437. xmlnumber (memory, extent);
  438. }
  439. else if (!strcmp (node-&gt;text, DATA_WORD))
  440. {
  441. length = sizeof (uint16_t);
  442. position (extent);
  443. string = xmlcontent1 (node);
  444. xmlnumber (memory, extent);
  445. }
  446. else if (!strcmp (node-&gt;text, DATA_BYTE))
  447. {
  448. position (extent);
  449. string = xmlcontent1 (node);
  450. xmlbyte (memory, extent);
  451. }
  452. xmledit (node, memory, extent);
  453. }
  454. node = node-&gt;after;
  455. }
  456. return (0);
  457. }
  458. /*====================================================================*
  459. *
  460. *--------------------------------------------------------------------*/
  461. #endif
  462. </pre>
  463. <div class='footerlink'>
  464. [<a href='xmldata.c.html' title=' xmldata.c '>PREV</a>]
  465. [<a href='toolkit.html' title=' Index '>HOME</a>]
  466. [<a href='xmlelement.c.html' title=' xmlelement.c '>NEXT</a>]
  467. </div>
  468. </body>
  469. </html>