CO_fifo.c 43 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381
  1. /*
  2. * FIFO circular buffer
  3. *
  4. * @file CO_fifo.c
  5. * @ingroup CO_CANopen_309_fifo
  6. * @author Janez Paternoster
  7. * @copyright 2020 Janez Paternoster
  8. *
  9. * This file is part of CANopenNode, an opensource CANopen Stack.
  10. * Project home page is <https://github.com/CANopenNode/CANopenNode>.
  11. * For more information on CANopen see <http://www.can-cia.org/>.
  12. *
  13. * Licensed under the Apache License, Version 2.0 (the "License");
  14. * you may not use this file except in compliance with the License.
  15. * You may obtain a copy of the License at
  16. *
  17. * http://www.apache.org/licenses/LICENSE-2.0
  18. *
  19. * Unless required by applicable law or agreed to in writing, software
  20. * distributed under the License is distributed on an "AS IS" BASIS,
  21. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the License for the specific language governing permissions and
  23. * limitations under the License.
  24. */
  25. #include "301/CO_fifo.h"
  26. #if (CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ENABLE
  27. #include <string.h>
  28. #include <ctype.h>
  29. #include <stdlib.h>
  30. #include "crc16-ccitt.h"
  31. #if (CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ASCII_COMMANDS
  32. #include <stdio.h>
  33. #include <inttypes.h>
  34. /* Non-graphical character for command delimiter */
  35. #define DELIM_COMMAND '\n'
  36. /* Graphical character for comment delimiter */
  37. #define DELIM_COMMENT '#'
  38. #endif /* (CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ASCII_COMMANDS */
  39. /* verify configuration */
  40. #if (CO_CONFIG_FIFO) & CO_CONFIG_FIFO_CRC16_CCITT
  41. #if !((CO_CONFIG_CRC16) & CO_CONFIG_CRC16_ENABLE)
  42. #error CO_CONFIG_CRC16_ENABLE must be enabled.
  43. #endif
  44. #endif
  45. /******************************************************************************/
  46. void CO_fifo_init(CO_fifo_t *fifo, char *buf, size_t bufSize) {
  47. if (fifo == NULL || buf == NULL || bufSize < 2) {
  48. return;
  49. }
  50. fifo->readPtr = 0;
  51. fifo->writePtr = 0;
  52. fifo->buf = buf;
  53. fifo->bufSize = bufSize;
  54. return;
  55. }
  56. /* Circular FIFO buffer example for fifo->bufSize = 7 (usable size = 6): ******
  57. * *
  58. * 0 * * * * *
  59. * 1 rp==wp readPtr writePtr * *
  60. * 2 * * * * *
  61. * 3 * * * writePtr *
  62. * 4 * writePtr readPtr readPtr *
  63. * 5 * * * * *
  64. * 6 * * * * *
  65. * *
  66. * empty 3 bytes 4 bytes buffer *
  67. * buffer in buff in buff full *
  68. ******************************************************************************/
  69. size_t CO_fifo_write(CO_fifo_t *fifo,
  70. const char *buf,
  71. size_t count,
  72. uint16_t *crc)
  73. {
  74. size_t i;
  75. char *bufDest;
  76. if (fifo == NULL || fifo->buf == NULL || buf == NULL) {
  77. return 0;
  78. }
  79. bufDest = &fifo->buf[fifo->writePtr];
  80. for (i = count; i > 0; i--) {
  81. size_t writePtrNext = fifo->writePtr + 1;
  82. /* is circular buffer full */
  83. if (writePtrNext == fifo->readPtr ||
  84. (writePtrNext == fifo->bufSize && fifo->readPtr == 0)) {
  85. break;
  86. }
  87. *bufDest = *buf;
  88. #if (CO_CONFIG_FIFO) & CO_CONFIG_FIFO_CRC16_CCITT
  89. if (crc != NULL) {
  90. crc16_ccitt_single(crc, (unsigned char)*buf);
  91. }
  92. #endif
  93. /* increment variables */
  94. if (writePtrNext == fifo->bufSize) {
  95. fifo->writePtr = 0;
  96. bufDest = &fifo->buf[0];
  97. }
  98. else {
  99. fifo->writePtr++;
  100. bufDest++;
  101. }
  102. buf++;
  103. }
  104. return count - i;
  105. }
  106. /******************************************************************************/
  107. size_t CO_fifo_read(CO_fifo_t *fifo, char *buf, size_t count, bool_t *eof) {
  108. size_t i;
  109. const char *bufSrc;
  110. if (eof != NULL) {
  111. *eof = false;
  112. }
  113. if (fifo == NULL || buf == NULL || fifo->readPtr == fifo->writePtr) {
  114. return 0;
  115. }
  116. bufSrc = &fifo->buf[fifo->readPtr];
  117. for (i = count; i > 0; ) {
  118. const char c = *bufSrc;
  119. /* is circular buffer empty */
  120. if (fifo->readPtr == fifo->writePtr) {
  121. break;
  122. }
  123. *(buf++) = c;
  124. /* increment variables */
  125. if (++fifo->readPtr == fifo->bufSize) {
  126. fifo->readPtr = 0;
  127. bufSrc = &fifo->buf[0];
  128. }
  129. else {
  130. bufSrc++;
  131. }
  132. i--;
  133. #if (CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ASCII_COMMANDS
  134. /* is delimiter? */
  135. if (eof != NULL && c == DELIM_COMMAND) {
  136. *eof = true;
  137. break;
  138. }
  139. #endif
  140. }
  141. return count - i;
  142. }
  143. #if (CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ALT_READ
  144. /******************************************************************************/
  145. size_t CO_fifo_altBegin(CO_fifo_t *fifo, size_t offset) {
  146. size_t i;
  147. if (fifo == NULL) {
  148. return 0;
  149. }
  150. fifo->altReadPtr = fifo->readPtr;
  151. for (i = offset; i > 0; i--) {
  152. /* is circular buffer empty */
  153. if (fifo->altReadPtr == fifo->writePtr) {
  154. break;
  155. }
  156. /* increment variable */
  157. if (++fifo->altReadPtr == fifo->bufSize) {
  158. fifo->altReadPtr = 0;
  159. }
  160. }
  161. return offset - i;
  162. }
  163. void CO_fifo_altFinish(CO_fifo_t *fifo, uint16_t *crc) {
  164. if (fifo == NULL) {
  165. return;
  166. }
  167. if (crc == NULL) {
  168. fifo->readPtr = fifo->altReadPtr;
  169. }
  170. else {
  171. const char *bufSrc = &fifo->buf[fifo->readPtr];
  172. while (fifo->readPtr != fifo->altReadPtr) {
  173. #if (CO_CONFIG_FIFO) & CO_CONFIG_FIFO_CRC16_CCITT
  174. crc16_ccitt_single(crc, (unsigned char)*bufSrc);
  175. #endif
  176. /* increment variable */
  177. if (++fifo->readPtr == fifo->bufSize) {
  178. fifo->readPtr = 0;
  179. bufSrc = &fifo->buf[0];
  180. }
  181. else {
  182. bufSrc++;
  183. }
  184. }
  185. }
  186. }
  187. size_t CO_fifo_altRead(CO_fifo_t *fifo, char *buf, size_t count) {
  188. size_t i;
  189. const char *bufSrc;
  190. bufSrc = &fifo->buf[fifo->altReadPtr];
  191. for (i = count; i > 0; i--) {
  192. const char c = *bufSrc;
  193. /* is there no more data */
  194. if (fifo->altReadPtr == fifo->writePtr) {
  195. break;
  196. }
  197. *(buf++) = c;
  198. /* increment variables */
  199. if (++fifo->altReadPtr == fifo->bufSize) {
  200. fifo->altReadPtr = 0;
  201. bufSrc = &fifo->buf[0];
  202. }
  203. else {
  204. bufSrc++;
  205. }
  206. }
  207. return count - i;
  208. }
  209. #endif /* (CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ALT_READ */
  210. #if (CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ASCII_COMMANDS
  211. /******************************************************************************/
  212. bool_t CO_fifo_CommSearch(CO_fifo_t *fifo, bool_t clear) {
  213. bool_t newCommand = false;
  214. size_t count;
  215. char *commandEnd;
  216. if (fifo == NULL || fifo->readPtr == fifo->writePtr) {
  217. return 0;
  218. }
  219. /* search delimiter until writePtr or until end of buffer */
  220. if (fifo->readPtr < fifo->writePtr) {
  221. count = fifo->writePtr - fifo->readPtr;
  222. } else {
  223. count = fifo->bufSize - fifo->readPtr;
  224. }
  225. commandEnd = (char *)memchr((const void *)&fifo->buf[fifo->readPtr],
  226. DELIM_COMMAND,
  227. count);
  228. if (commandEnd != NULL) {
  229. newCommand = true;
  230. }
  231. else if (fifo->readPtr > fifo->writePtr) {
  232. /* not found, search in the beginning of the circular buffer */
  233. commandEnd = (char *)memchr((const void *)&fifo->buf[0],
  234. DELIM_COMMAND,
  235. fifo->writePtr);
  236. if (commandEnd != NULL || fifo->readPtr == (fifo->writePtr + 1)) {
  237. /* command delimiter found or buffer full */
  238. newCommand = true;
  239. }
  240. }
  241. else if (fifo->readPtr == 0 && fifo->writePtr == (fifo->bufSize - 1)) {
  242. /* buffer full */
  243. newCommand = true;
  244. }
  245. /* Clear buffer if set so */
  246. if (clear) {
  247. if (commandEnd != NULL) {
  248. fifo->readPtr = (int)(commandEnd - fifo->buf) + 1;
  249. if (fifo->readPtr == fifo->bufSize) {
  250. fifo->readPtr = 0;
  251. }
  252. }
  253. else {
  254. fifo->readPtr = fifo->writePtr;
  255. }
  256. }
  257. return newCommand;
  258. }
  259. /******************************************************************************/
  260. bool_t CO_fifo_trimSpaces(CO_fifo_t *fifo, bool_t *insideComment) {
  261. bool_t delimCommandFound = false;
  262. if (fifo != NULL && insideComment != NULL) {
  263. while (fifo->readPtr != fifo->writePtr) {
  264. char c = fifo->buf[fifo->readPtr];
  265. if (c == DELIM_COMMENT) {
  266. *insideComment = true;
  267. }
  268. else if (isgraph((int)c) != 0 && !(*insideComment)) {
  269. break;
  270. }
  271. if (++fifo->readPtr == fifo->bufSize) {
  272. fifo->readPtr = 0;
  273. }
  274. if (c == DELIM_COMMAND) {
  275. delimCommandFound = true;
  276. *insideComment = false;
  277. break;
  278. }
  279. }
  280. }
  281. return delimCommandFound;
  282. }
  283. /******************************************************************************/
  284. size_t CO_fifo_readToken(CO_fifo_t *fifo,
  285. char *buf,
  286. size_t count,
  287. char *closed,
  288. bool_t *err)
  289. {
  290. bool_t delimCommandFound = false;
  291. bool_t delimCommentFound = false;
  292. size_t tokenSize = 0;
  293. if (fifo != NULL && buf != NULL && count > 1 && (err == NULL || *err == 0)
  294. && fifo->readPtr != fifo->writePtr
  295. ) {
  296. bool_t finished = false;
  297. char step = 0;
  298. size_t ptr = fifo->readPtr; /* current pointer (integer, 0 based) */
  299. char *c = &fifo->buf[ptr]; /* current char */
  300. do {
  301. switch (step) {
  302. case 0: /* skip leading empty characters, stop on delimiter */
  303. if (isgraph((int)*c) != 0) {
  304. if (*c == DELIM_COMMENT) {
  305. delimCommentFound = true;
  306. } else {
  307. buf[tokenSize++] = *c;
  308. step++;
  309. }
  310. }
  311. else if (*c == DELIM_COMMAND) {
  312. delimCommandFound = true;
  313. }
  314. break;
  315. case 1: /* search for end of the token */
  316. if (isgraph((int)*c) != 0) {
  317. if (*c == DELIM_COMMENT) {
  318. delimCommentFound = true;
  319. } else if (tokenSize < count) {
  320. buf[tokenSize++] = *c;
  321. }
  322. }
  323. else {
  324. if (*c == DELIM_COMMAND) {
  325. delimCommandFound = true;
  326. }
  327. step++;
  328. }
  329. break;
  330. case 2: /* skip trailing empty characters */
  331. if (isgraph((int)*c) != 0) {
  332. if (*c == DELIM_COMMENT) {
  333. delimCommentFound = true;
  334. } else {
  335. fifo->readPtr = ptr;
  336. finished = true;
  337. }
  338. }
  339. else if (*c == DELIM_COMMAND) {
  340. delimCommandFound = true;
  341. }
  342. break;
  343. }
  344. if (delimCommentFound == true) {
  345. /* Comment delimiter found, clear all till end of the line. */
  346. fifo->readPtr = ptr;
  347. delimCommandFound = CO_fifo_CommSearch(fifo, true);
  348. finished = true;
  349. }
  350. else if (delimCommandFound) {
  351. /* command delimiter found, set readPtr behind it. */
  352. if (++ptr == fifo->bufSize) ptr = 0;
  353. fifo->readPtr = ptr;
  354. finished = true;
  355. }
  356. else if (!finished) {
  357. /* find next character in the circular buffer */
  358. if (++ptr == fifo->bufSize) {
  359. ptr = 0;
  360. c = &fifo->buf[ptr];
  361. }
  362. else {
  363. c++;
  364. }
  365. /* end, if buffer is now empty */
  366. if (ptr == fifo->writePtr) {
  367. if (step == 2) {
  368. fifo->readPtr = ptr;
  369. }
  370. else {
  371. tokenSize = 0;
  372. }
  373. finished = true;
  374. }
  375. }
  376. } while (!finished);
  377. }
  378. /* set 'err' return value */
  379. if (err != NULL && *err == false) {
  380. if (tokenSize == count || (closed != NULL &&
  381. ((*closed == 1 && (!delimCommandFound || tokenSize == 0)) ||
  382. (*closed == 0 && (delimCommandFound || tokenSize == 0)))
  383. )) {
  384. *err = true;
  385. }
  386. }
  387. /* set 'closed' return value */
  388. if (closed != NULL) {
  389. *closed = delimCommandFound ? 1 : 0;
  390. }
  391. /* token was larger then size of the buffer, all was cleaned, return '' */
  392. if (tokenSize == count) {
  393. tokenSize = 0;
  394. }
  395. /* write string terminator character */
  396. if (buf != NULL && count > tokenSize) {
  397. buf[tokenSize] = '\0';
  398. }
  399. return tokenSize;
  400. }
  401. #endif /* (CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ASCII_COMMANDS */
  402. #if (CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ASCII_DATATYPES
  403. /******************************************************************************/
  404. /* Tables for mime-base64 encoding, as specified in RFC 2045, (without CR-LF,
  405. * but one long string). Base64 is used for encoding binary data into easy
  406. * transferable printable characters. In general, each three bytes of binary
  407. * data are translated into four characters, where characters are selected from
  408. * 64 characters long table. See https://en.wikipedia.org/wiki/Base64 */
  409. static const char base64EncTable[] =
  410. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  411. static const char base64DecTable[] = {
  412. -1, -1, -1, -1, -1, -1, -1, -1, -1,103,101, -1, -1,102, -1, -1,
  413. -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
  414. 103, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63,
  415. 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1,100, -1, -1,
  416. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  417. 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1,
  418. -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
  419. 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1};
  420. size_t CO_fifo_readU82a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end) {
  421. uint8_t n=0;
  422. if (fifo != NULL && count >= 6 && CO_fifo_getOccupied(fifo) == sizeof(n)) {
  423. CO_fifo_read(fifo, (char *)&n, sizeof(n), NULL);
  424. return sprintf(buf, "%"PRIu8, n);
  425. }
  426. else {
  427. return CO_fifo_readHex2a(fifo, buf, count, end);
  428. }
  429. }
  430. size_t CO_fifo_readU162a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end) {
  431. uint16_t n=0;
  432. if (fifo != NULL && count >= 8 && CO_fifo_getOccupied(fifo) == sizeof(n)) {
  433. CO_fifo_read(fifo, (char *)&n, sizeof(n), NULL);
  434. return sprintf(buf, "%"PRIu16, CO_SWAP_16(n));
  435. }
  436. else {
  437. return CO_fifo_readHex2a(fifo, buf, count, end);
  438. }
  439. }
  440. size_t CO_fifo_readU322a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end) {
  441. uint32_t n=0;
  442. if (fifo != NULL && count >= 12 && CO_fifo_getOccupied(fifo) == sizeof(n)) {
  443. CO_fifo_read(fifo, (char *)&n, sizeof(n), NULL);
  444. return sprintf(buf, "%"PRIu32, CO_SWAP_32(n));
  445. }
  446. else {
  447. return CO_fifo_readHex2a(fifo, buf, count, end);
  448. }
  449. }
  450. size_t CO_fifo_readU642a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end) {
  451. uint64_t n=0;
  452. if (fifo != NULL && count >= 20 && CO_fifo_getOccupied(fifo) == sizeof(n)) {
  453. CO_fifo_read(fifo, (char *)&n, sizeof(n), NULL);
  454. return sprintf(buf, "%"PRIu64, CO_SWAP_64(n));
  455. }
  456. else {
  457. return CO_fifo_readHex2a(fifo, buf, count, end);
  458. }
  459. }
  460. size_t CO_fifo_readX82a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end) {
  461. uint8_t n=0;
  462. if (fifo != NULL && count >= 6 && CO_fifo_getOccupied(fifo) == sizeof(n)) {
  463. CO_fifo_read(fifo, (char *)&n, sizeof(n), NULL);
  464. return sprintf(buf, "0x%02"PRIX8, n);
  465. }
  466. else {
  467. return CO_fifo_readHex2a(fifo, buf, count, end);
  468. }
  469. }
  470. size_t CO_fifo_readX162a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end) {
  471. uint16_t n=0;
  472. if (fifo != NULL && count >= 8 && CO_fifo_getOccupied(fifo) == sizeof(n)) {
  473. CO_fifo_read(fifo, (char *)&n, sizeof(n), NULL);
  474. return sprintf(buf, "0x%04"PRIX16, CO_SWAP_16(n));
  475. }
  476. else {
  477. return CO_fifo_readHex2a(fifo, buf, count, end);
  478. }
  479. }
  480. size_t CO_fifo_readX322a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end) {
  481. uint32_t n=0;
  482. if (fifo != NULL && count >= 12 && CO_fifo_getOccupied(fifo) == sizeof(n)) {
  483. CO_fifo_read(fifo, (char *)&n, sizeof(n), NULL);
  484. return sprintf(buf, "0x%08"PRIX32, CO_SWAP_32(n));
  485. }
  486. else {
  487. return CO_fifo_readHex2a(fifo, buf, count, end);
  488. }
  489. }
  490. size_t CO_fifo_readX642a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end) {
  491. uint64_t n=0;
  492. if (fifo != NULL && count >= 20 && CO_fifo_getOccupied(fifo) == sizeof(n)) {
  493. CO_fifo_read(fifo, (char *)&n, sizeof(n), NULL);
  494. return sprintf(buf, "0x%016"PRIX64, CO_SWAP_64(n));
  495. }
  496. else {
  497. return CO_fifo_readHex2a(fifo, buf, count, end);
  498. }
  499. }
  500. size_t CO_fifo_readI82a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end) {
  501. int8_t n=0;
  502. if (fifo != NULL && count >= 6 && CO_fifo_getOccupied(fifo) == sizeof(n)) {
  503. CO_fifo_read(fifo, (char *)&n, sizeof(n), NULL);
  504. return sprintf(buf, "%"PRId8, n);
  505. }
  506. else {
  507. return CO_fifo_readHex2a(fifo, buf, count, end);
  508. }
  509. }
  510. size_t CO_fifo_readI162a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end) {
  511. int16_t n=0;
  512. if (fifo != NULL && count >= 8 && CO_fifo_getOccupied(fifo) == sizeof(n)) {
  513. CO_fifo_read(fifo, (char *)&n, sizeof(n), NULL);
  514. return sprintf(buf, "%"PRId16, CO_SWAP_16(n));
  515. }
  516. else {
  517. return CO_fifo_readHex2a(fifo, buf, count, end);
  518. }
  519. }
  520. size_t CO_fifo_readI322a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end) {
  521. int32_t n=0;
  522. if (fifo != NULL && count >= 13 && CO_fifo_getOccupied(fifo) == sizeof(n)) {
  523. CO_fifo_read(fifo, (char *)&n, sizeof(n), NULL);
  524. return sprintf(buf, "%"PRId32, CO_SWAP_32(n));
  525. }
  526. else {
  527. return CO_fifo_readHex2a(fifo, buf, count, end);
  528. }
  529. }
  530. size_t CO_fifo_readI642a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end) {
  531. int64_t n=0;
  532. if (fifo != NULL && count >= 23 && CO_fifo_getOccupied(fifo) == sizeof(n)) {
  533. CO_fifo_read(fifo, (char *)&n, sizeof(n), NULL);
  534. return sprintf(buf, "%"PRId64, CO_SWAP_64(n));
  535. }
  536. else {
  537. return CO_fifo_readHex2a(fifo, buf, count, end);
  538. }
  539. }
  540. size_t CO_fifo_readR322a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end) {
  541. float32_t n=0;
  542. if (fifo != NULL && count >= 20 && CO_fifo_getOccupied(fifo) == sizeof(n)) {
  543. CO_fifo_read(fifo, (char *)&n, sizeof(n), NULL);
  544. return sprintf(buf, "%g", CO_SWAP_32(n));
  545. }
  546. else {
  547. return CO_fifo_readHex2a(fifo, buf, count, end);
  548. }
  549. }
  550. size_t CO_fifo_readR642a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end) {
  551. float64_t n=0;
  552. if (fifo != NULL && count >= 30 && CO_fifo_getOccupied(fifo) == sizeof(n)) {
  553. CO_fifo_read(fifo, (char *)&n, sizeof(n), NULL);
  554. return sprintf(buf, "%g", CO_SWAP_64(n));
  555. }
  556. else {
  557. return CO_fifo_readHex2a(fifo, buf, count, end);
  558. }
  559. }
  560. size_t CO_fifo_readHex2a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end) {
  561. (void)end; /* unused */
  562. size_t len = 0;
  563. if (fifo != NULL && count > 3) {
  564. /* Very first write is without leading space */
  565. if (!fifo->started) {
  566. char c;
  567. if (CO_fifo_getc(fifo, &c)) {
  568. len = sprintf(&buf[0], "%02"PRIX8, (uint8_t)c);
  569. fifo->started = true;
  570. }
  571. }
  572. while ((len + 3) < count) {
  573. char c;
  574. if (!CO_fifo_getc(fifo, &c)) {
  575. break;
  576. }
  577. len += sprintf(&buf[len], " %02"PRIX8, (uint8_t)c);
  578. }
  579. }
  580. return len;
  581. }
  582. size_t CO_fifo_readVs2a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end) {
  583. size_t len = 0;
  584. if (fifo != NULL && count > 3) {
  585. /* Start with '"' */
  586. if (!fifo->started) {
  587. buf[len++] = '"';
  588. fifo->started = true;
  589. }
  590. while ((len + 2) < count) {
  591. char c;
  592. if (!CO_fifo_getc(fifo, &c)) {
  593. if (end) {
  594. buf[len++] = '"';
  595. }
  596. break;
  597. }
  598. else if (c != 0 && c != '\r') { /* skip null and CR inside string */
  599. buf[len++] = c;
  600. if (c == '"') {
  601. buf[len++] = c;
  602. }
  603. }
  604. }
  605. }
  606. return len;
  607. }
  608. size_t CO_fifo_readB642a(CO_fifo_t *fifo, char *buf, size_t count, bool_t end) {
  609. /* mime-base64 encoding, see description above base64EncTable */
  610. size_t len = 0;
  611. if (fifo != NULL && count >= 4) {
  612. uint8_t step;
  613. uint16_t word;
  614. if (!fifo->started) {
  615. fifo->started = true;
  616. step = 0;
  617. word = 0;
  618. }
  619. else {
  620. /* get memorized variables from previous function calls */
  621. step = (uint8_t)(fifo->aux >> 16);
  622. word = (uint16_t)fifo->aux;
  623. }
  624. while ((len + 3) <= count) {
  625. char c;
  626. if (!CO_fifo_getc(fifo, &c)) {
  627. /* buffer is empty, is also SDO communication finished? */
  628. if (end) {
  629. /* add padding if necessary */
  630. switch (step) {
  631. case 1:
  632. buf[len++] = base64EncTable[(word >> 4) & 0x3F];
  633. buf[len++] = '=';
  634. buf[len++] = '=';
  635. break;
  636. case 2:
  637. buf[len++] = base64EncTable[(word >> 6) & 0x3F];
  638. buf[len++] = '=';
  639. break;
  640. }
  641. }
  642. break;
  643. }
  644. word |= (uint8_t)c;
  645. switch (step++) {
  646. case 0:
  647. buf[len++] = base64EncTable[(word >> 2) & 0x3F];
  648. break;
  649. case 1:
  650. buf[len++] = base64EncTable[(word >> 4) & 0x3F];
  651. break;
  652. default:
  653. buf[len++] = base64EncTable[(word >> 6) & 0x3F];
  654. buf[len++] = base64EncTable[word & 0x3F];
  655. step = 0;
  656. break;
  657. }
  658. word <<= 8;
  659. }
  660. /* memorize variables for next iteration */
  661. fifo->aux = (uint32_t)step << 16 | word;
  662. }
  663. return len;
  664. }
  665. /******************************************************************************/
  666. size_t CO_fifo_cpyTok2U8(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status) {
  667. char buf[15];
  668. char closed = -1;
  669. bool_t err = 0;
  670. size_t nWr = 0;
  671. size_t nRd = CO_fifo_readToken(src, buf, sizeof(buf), &closed, &err);
  672. CO_fifo_st st = (uint8_t)closed;
  673. if (nRd == 0 || err) st |= CO_fifo_st_errTok;
  674. else {
  675. char *sRet;
  676. uint32_t u32 = strtoul(buf, &sRet, 0);
  677. if (sRet != strchr(buf, '\0') || u32 > UINT8_MAX) st |= CO_fifo_st_errVal;
  678. else {
  679. uint8_t num = (uint8_t) u32;
  680. nWr = CO_fifo_write(dest, (const char *)&num, sizeof(num), NULL);
  681. if (nWr != sizeof(num)) st |= CO_fifo_st_errBuf;
  682. }
  683. }
  684. if (status != NULL) *status = st;
  685. return nWr;
  686. }
  687. size_t CO_fifo_cpyTok2U16(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status) {
  688. char buf[15];
  689. char closed = -1;
  690. bool_t err = 0;
  691. size_t nWr = 0;
  692. size_t nRd = CO_fifo_readToken(src, buf, sizeof(buf), &closed, &err);
  693. CO_fifo_st st = (uint8_t)closed;
  694. if (nRd == 0 || err) st |= CO_fifo_st_errTok;
  695. else {
  696. char *sRet;
  697. uint32_t u32 = strtoul(buf, &sRet, 0);
  698. if (sRet != strchr(buf, '\0') || u32 > UINT16_MAX) st |= CO_fifo_st_errVal;
  699. else {
  700. uint16_t num = CO_SWAP_16((uint16_t) u32);
  701. nWr = CO_fifo_write(dest, (const char *)&num, sizeof(num), NULL);
  702. if (nWr != sizeof(num)) st |= CO_fifo_st_errBuf;
  703. }
  704. }
  705. if (status != NULL) *status = st;
  706. return nWr;
  707. }
  708. size_t CO_fifo_cpyTok2U32(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status) {
  709. char buf[15];
  710. char closed = -1;
  711. bool_t err = 0;
  712. size_t nWr = 0;
  713. size_t nRd = CO_fifo_readToken(src, buf, sizeof(buf), &closed, &err);
  714. CO_fifo_st st = (uint8_t)closed;
  715. if (nRd == 0 || err) st |= CO_fifo_st_errTok;
  716. else {
  717. char *sRet;
  718. uint32_t u32 = strtoul(buf, &sRet, 0);
  719. if (sRet != strchr(buf, '\0')) st |= CO_fifo_st_errVal;
  720. else {
  721. uint32_t num = CO_SWAP_32(u32);
  722. nWr = CO_fifo_write(dest, (const char *)&num, sizeof(num), NULL);
  723. if (nWr != sizeof(num)) st |= CO_fifo_st_errBuf;
  724. }
  725. }
  726. if (status != NULL) *status = st;
  727. return nWr;
  728. }
  729. size_t CO_fifo_cpyTok2U64(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status) {
  730. char buf[25];
  731. char closed = -1;
  732. bool_t err = 0;
  733. size_t nWr = 0;
  734. size_t nRd = CO_fifo_readToken(src, buf, sizeof(buf), &closed, &err);
  735. CO_fifo_st st = (uint8_t)closed;
  736. if (nRd == 0 || err) st |= CO_fifo_st_errTok;
  737. else {
  738. char *sRet;
  739. uint64_t u64 = strtoull(buf, &sRet, 0);
  740. if (sRet != strchr(buf, '\0')) st |= CO_fifo_st_errVal;
  741. else {
  742. uint64_t num = CO_SWAP_64(u64);
  743. nWr = CO_fifo_write(dest, (const char *)&num, sizeof(num), NULL);
  744. if (nWr != sizeof(num)) st |= CO_fifo_st_errBuf;
  745. }
  746. }
  747. if (status != NULL) *status = (uint8_t) st;
  748. return nWr;
  749. }
  750. size_t CO_fifo_cpyTok2I8(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status) {
  751. char buf[15];
  752. char closed = -1;
  753. bool_t err = 0;
  754. size_t nWr = 0;
  755. size_t nRd = CO_fifo_readToken(src, buf, sizeof(buf), &closed, &err);
  756. CO_fifo_st st = (uint8_t)closed;
  757. if (nRd == 0 || err) st |= CO_fifo_st_errTok;
  758. else {
  759. char *sRet;
  760. int32_t i32 = strtol(buf, &sRet, 0);
  761. if (sRet != strchr(buf, '\0') || i32 < INT8_MIN || i32 > INT8_MAX) {
  762. st |= CO_fifo_st_errVal;
  763. } else {
  764. int8_t num = (int8_t) i32;
  765. nWr = CO_fifo_write(dest, (const char *)&num, sizeof(num), NULL);
  766. if (nWr != sizeof(num)) st |= CO_fifo_st_errBuf;
  767. }
  768. }
  769. if (status != NULL) *status = st;
  770. return nWr;
  771. }
  772. size_t CO_fifo_cpyTok2I16(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status) {
  773. char buf[15];
  774. char closed = -1;
  775. bool_t err = 0;
  776. size_t nWr = 0;
  777. size_t nRd = CO_fifo_readToken(src, buf, sizeof(buf), &closed, &err);
  778. CO_fifo_st st = (uint8_t)closed;
  779. if (nRd == 0 || err) st |= CO_fifo_st_errTok;
  780. else {
  781. char *sRet;
  782. int32_t i32 = strtol(buf, &sRet, 0);
  783. if (sRet != strchr(buf, '\0') || i32 < INT16_MIN || i32 > INT16_MAX) {
  784. st |= CO_fifo_st_errVal;
  785. } else {
  786. int16_t num = CO_SWAP_16((int16_t) i32);
  787. nWr = CO_fifo_write(dest, (const char *)&num, sizeof(num), NULL);
  788. if (nWr != sizeof(num)) st |= CO_fifo_st_errBuf;
  789. }
  790. }
  791. if (status != NULL) *status = st;
  792. return nWr;
  793. }
  794. size_t CO_fifo_cpyTok2I32(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status) {
  795. char buf[15];
  796. char closed = -1;
  797. bool_t err = 0;
  798. size_t nWr = 0;
  799. size_t nRd = CO_fifo_readToken(src, buf, sizeof(buf), &closed, &err);
  800. CO_fifo_st st = (uint8_t)closed;
  801. if (nRd == 0 || err) st |= CO_fifo_st_errTok;
  802. else {
  803. char *sRet;
  804. int32_t i32 = strtol(buf, &sRet, 0);
  805. if (sRet != strchr(buf, '\0')) st |= CO_fifo_st_errVal;
  806. else {
  807. int32_t num = CO_SWAP_32(i32);
  808. nWr = CO_fifo_write(dest, (const char *)&num, sizeof(num), NULL);
  809. if (nWr != sizeof(num)) st |= CO_fifo_st_errBuf;
  810. }
  811. }
  812. if (status != NULL) *status = st;
  813. return nWr;
  814. }
  815. size_t CO_fifo_cpyTok2I64(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status) {
  816. char buf[25];
  817. char closed = -1;
  818. bool_t err = 0;
  819. size_t nWr = 0;
  820. size_t nRd = CO_fifo_readToken(src, buf, sizeof(buf), &closed, &err);
  821. CO_fifo_st st = (uint8_t)closed;
  822. if (nRd == 0 || err) st |= CO_fifo_st_errTok;
  823. else {
  824. char *sRet;
  825. int64_t i64 = strtoll(buf, &sRet, 0);
  826. if (sRet != strchr(buf, '\0')) st |= CO_fifo_st_errVal;
  827. else {
  828. int64_t num = CO_SWAP_64(i64);
  829. nWr = CO_fifo_write(dest, (const char *)&num, sizeof(num), NULL);
  830. if (nWr != sizeof(num)) st |= CO_fifo_st_errBuf;
  831. }
  832. }
  833. if (status != NULL) *status = (uint8_t) st;
  834. return nWr;
  835. }
  836. size_t CO_fifo_cpyTok2R32(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status) {
  837. char buf[30];
  838. char closed = -1;
  839. bool_t err = 0;
  840. size_t nWr = 0;
  841. size_t nRd = CO_fifo_readToken(src, buf, sizeof(buf), &closed, &err);
  842. CO_fifo_st st = (uint8_t)closed;
  843. if (nRd == 0 || err) st |= CO_fifo_st_errTok;
  844. else {
  845. char *sRet;
  846. float32_t f32 = strtof(buf, &sRet);
  847. if (sRet != strchr(buf, '\0')) st |= CO_fifo_st_errVal;
  848. else {
  849. float32_t num = CO_SWAP_32(f32);
  850. nWr = CO_fifo_write(dest, (const char *)&num, sizeof(num), NULL);
  851. if (nWr != sizeof(num)) st |= CO_fifo_st_errBuf;
  852. }
  853. }
  854. if (status != NULL) *status = st;
  855. return nWr;
  856. }
  857. size_t CO_fifo_cpyTok2R64(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status) {
  858. char buf[40];
  859. char closed = -1;
  860. bool_t err = 0;
  861. size_t nWr = 0;
  862. size_t nRd = CO_fifo_readToken(src, buf, sizeof(buf), &closed, &err);
  863. CO_fifo_st st = (uint8_t)closed;
  864. if (nRd == 0 || err) st |= CO_fifo_st_errTok;
  865. else {
  866. char *sRet;
  867. float64_t f64 = strtof(buf, &sRet);
  868. if (sRet != strchr(buf, '\0')) st |= CO_fifo_st_errVal;
  869. else {
  870. float64_t num = CO_SWAP_64(f64);
  871. nWr = CO_fifo_write(dest, (const char *)&num, sizeof(num), NULL);
  872. if (nWr != sizeof(num)) st |= CO_fifo_st_errBuf;
  873. }
  874. }
  875. if (status != NULL) *status = st;
  876. return nWr;
  877. }
  878. size_t CO_fifo_cpyTok2Hex(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status) {
  879. size_t destSpace, destSpaceStart;
  880. bool_t finished = false;
  881. uint8_t step;
  882. char firstChar;
  883. CO_fifo_st st = 0;
  884. if (dest == NULL || src == NULL) {
  885. return 0;
  886. }
  887. /* get free space of the dest fifo */
  888. destSpaceStart = destSpace = CO_fifo_getSpace(dest);
  889. /* is this the first write into dest? */
  890. if (!dest->started) {
  891. bool_t insideComment = false;
  892. if (CO_fifo_trimSpaces(src, &insideComment) || insideComment) {
  893. /* command delimiter found without string, this is an error */
  894. st |= CO_fifo_st_errTok;
  895. }
  896. dest->started = true;
  897. step = 0;
  898. firstChar = 0;
  899. }
  900. else {
  901. /* get memorized variables from previous function calls */
  902. step = (uint8_t)(dest->aux >> 8);
  903. firstChar = (char)(dest->aux & 0xFF);
  904. }
  905. /* repeat until destination space available and no error and not finished
  906. * and source characters available */
  907. while (destSpace > 0 && (st & CO_fifo_st_errMask) == 0 && !finished) {
  908. char c;
  909. if (!CO_fifo_getc(src, &c)) {
  910. break;
  911. }
  912. if (step == 6) {
  913. /* command is inside comment, waiting for command delimiter */
  914. bool_t insideComment = true;
  915. if (c == DELIM_COMMAND || CO_fifo_trimSpaces(src, &insideComment)) {
  916. st |= CO_fifo_st_closed;
  917. finished = true;
  918. }
  919. continue;
  920. }
  921. if (isxdigit((int)c) != 0) {
  922. /* first or second hex digit */
  923. if (step == 0) {
  924. firstChar = c;
  925. step = 1;
  926. }
  927. else {
  928. /* write the byte */
  929. char s[3];
  930. int32_t num;
  931. s[0] = firstChar; s[1] = c; s[2] = 0;
  932. num = strtol(s, NULL, 16);
  933. CO_fifo_putc(dest, (char) num);
  934. destSpace--;
  935. step = 0;
  936. }
  937. }
  938. else if (isgraph((int)c) != 0) {
  939. /* printable character, not hex digit */
  940. if (c == '#') /* comment start */
  941. step = 6;
  942. else /* syntax error */
  943. st |= CO_fifo_st_errTok;
  944. }
  945. else {
  946. /* this is space or delimiter */
  947. if (step == 1) {
  948. /* write the byte */
  949. char s[2];
  950. int32_t num;
  951. s[0] = firstChar; s[1] = 0;
  952. num = strtol(s, NULL, 16);
  953. CO_fifo_putc(dest, (char) num);
  954. destSpace--;
  955. step = 0;
  956. }
  957. bool_t insideComment = false;
  958. if (c == DELIM_COMMAND || CO_fifo_trimSpaces(src, &insideComment)) {
  959. /* newline found, finish */
  960. st |= CO_fifo_st_closed;
  961. finished = true;
  962. }
  963. else if (insideComment) {
  964. step = 6;
  965. }
  966. }
  967. } /* while ... */
  968. if (!finished) {
  969. st |= CO_fifo_st_partial;
  970. /* memorize variables for next iteration */
  971. dest->aux = (uint32_t)step << 8 | (uint8_t)firstChar;
  972. }
  973. if (status != NULL) *status = st;
  974. return destSpaceStart - destSpace;
  975. }
  976. size_t CO_fifo_cpyTok2Vs(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status) {
  977. size_t destSpace, destSpaceStart;
  978. bool_t finished = false;
  979. uint8_t step;
  980. CO_fifo_st st = 0;
  981. if (dest == NULL || src == NULL) {
  982. return 0;
  983. }
  984. /* get free space of the dest fifo */
  985. destSpaceStart = destSpace = CO_fifo_getSpace(dest);
  986. /* is this the first write into dest? */
  987. if (!dest->started) {
  988. bool_t insideComment = false;
  989. if (CO_fifo_trimSpaces(src, &insideComment) || insideComment) {
  990. /* command delimiter found without string, this is an error */
  991. st |= CO_fifo_st_errTok;
  992. }
  993. dest->started = true;
  994. step = 0;
  995. }
  996. else {
  997. /* get memorized variables from previous function calls */
  998. step = (uint8_t)dest->aux;
  999. }
  1000. /* repeat until destination space available and no error and not finished
  1001. * and source characters available */
  1002. while (destSpace > 0 && (st & CO_fifo_st_errMask) == 0 && !finished) {
  1003. char c;
  1004. if (!CO_fifo_getc(src, &c)) {
  1005. break;
  1006. }
  1007. switch (step) {
  1008. case 0: /* beginning of the string, first write into dest */
  1009. if (c == '"') {
  1010. /* Indicated beginning of the string, skip this character. */
  1011. step = 1;
  1012. }
  1013. else {
  1014. /* this must be a single word string without '"' */
  1015. /* copy the character */
  1016. CO_fifo_putc(dest, c);
  1017. destSpace--;
  1018. step = 2;
  1019. }
  1020. break;
  1021. case 1: /* inside string, quoted string */
  1022. case 2: /* inside string, single word, no quotes */
  1023. if (c == '"') {
  1024. /* double quote found, this may be end of the string or escaped
  1025. * double quote (with two double quotes) */
  1026. step += 2;
  1027. }
  1028. else if (isgraph((int)c) == 0 && step == 2) {
  1029. /* end of single word string */
  1030. bool_t insideComment = false;
  1031. if (c == DELIM_COMMAND
  1032. || CO_fifo_trimSpaces(src, &insideComment)
  1033. ) {
  1034. st |= CO_fifo_st_closed;
  1035. finished = true;
  1036. }
  1037. else {
  1038. step = insideComment ? 6 : 5;
  1039. }
  1040. }
  1041. else if (c == DELIM_COMMAND) {
  1042. /* no closing quote, error */
  1043. st |= CO_fifo_st_errTok;
  1044. }
  1045. else {
  1046. /* copy the character */
  1047. CO_fifo_putc(dest, c);
  1048. destSpace--;
  1049. }
  1050. break;
  1051. case 3: /* previous was double quote, parsing quoted string */
  1052. case 4: /* previous was double quote, parsing no quoted word */
  1053. if (c == '"') {
  1054. /* escaped double quote, copy the character and continue */
  1055. CO_fifo_putc(dest, c);
  1056. destSpace--;
  1057. step -= 2;
  1058. }
  1059. else {
  1060. /* previous character was closing double quote */
  1061. if (step == 4) {
  1062. /* no opening double quote, syntax error */
  1063. st |= CO_fifo_st_errTok;
  1064. }
  1065. else {
  1066. if (isgraph((int)c) == 0) {
  1067. /* end of quoted string */
  1068. bool_t insideComment = false;
  1069. if (c == DELIM_COMMAND
  1070. || CO_fifo_trimSpaces(src, &insideComment)
  1071. ) {
  1072. st |= CO_fifo_st_closed;
  1073. finished = true;
  1074. }
  1075. else {
  1076. step = insideComment ? 6 : 5;
  1077. }
  1078. }
  1079. else {
  1080. /* space must follow closing double quote, error */
  1081. st |= CO_fifo_st_errTok;
  1082. }
  1083. }
  1084. }
  1085. break;
  1086. case 5: { /* String token is finished, waiting for command delimiter */
  1087. bool_t insideComment = false;
  1088. if (c == DELIM_COMMAND || CO_fifo_trimSpaces(src, &insideComment)) {
  1089. st |= CO_fifo_st_closed;
  1090. finished = true;
  1091. }
  1092. else if (insideComment) {
  1093. step = 6;
  1094. }
  1095. else if (isgraph((int)c) != 0) {
  1096. if (c == '#') /* comment start */
  1097. step = 6;
  1098. else /* syntax error */
  1099. st |= CO_fifo_st_errTok;
  1100. }
  1101. break;
  1102. }
  1103. case 6: { /* String token is finished, waiting for command delimiter */
  1104. bool_t insideComment = true;
  1105. if (c == DELIM_COMMAND || CO_fifo_trimSpaces(src, &insideComment)) {
  1106. st |= CO_fifo_st_closed;
  1107. finished = true;
  1108. }
  1109. break;
  1110. }
  1111. default: /* internal error */
  1112. st |= CO_fifo_st_errInt;
  1113. break;
  1114. }
  1115. }
  1116. if (!finished) {
  1117. st |= CO_fifo_st_partial;
  1118. /* memorize variables for next iteration */
  1119. dest->aux = step;
  1120. }
  1121. if (status != NULL) *status = st;
  1122. return destSpaceStart - destSpace;
  1123. }
  1124. size_t CO_fifo_cpyTok2B64(CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status) {
  1125. /* mime-base64 decoding, see description above base64EncTable */
  1126. size_t destSpace, destSpaceStart;
  1127. bool_t finished = false;
  1128. uint8_t step;
  1129. uint32_t dword;
  1130. CO_fifo_st st = 0;
  1131. if (dest == NULL || src == NULL) {
  1132. return 0;
  1133. }
  1134. /* get free space of the dest fifo */
  1135. destSpaceStart = destSpace = CO_fifo_getSpace(dest);
  1136. /* is this the first write into dest? */
  1137. if (!dest->started) {
  1138. bool_t insideComment = false;
  1139. if (CO_fifo_trimSpaces(src, &insideComment) || insideComment) {
  1140. /* command delimiter found without string, this is an error */
  1141. st |= CO_fifo_st_errTok;
  1142. }
  1143. dest->started = true;
  1144. step = 0;
  1145. dword = 0;
  1146. }
  1147. else {
  1148. /* get memorized variables from previous function calls */
  1149. step = (uint8_t)(dest->aux >> 24);
  1150. dword = dest->aux & 0xFFFFFF;
  1151. }
  1152. /* repeat until destination space available and no error and not finished
  1153. * and source characters available */
  1154. while (destSpace >= 3 && (st & CO_fifo_st_errMask) == 0 && !finished) {
  1155. char c;
  1156. if (!CO_fifo_getc(src, &c)) {
  1157. break;
  1158. }
  1159. if (step >= 5) {
  1160. /* String token is finished, waiting for command delimiter */
  1161. bool_t insideComment = step > 5;
  1162. if (c == DELIM_COMMAND || CO_fifo_trimSpaces(src, &insideComment)) {
  1163. st |= CO_fifo_st_closed;
  1164. finished = true;
  1165. }
  1166. else if (insideComment) {
  1167. step = 6;
  1168. }
  1169. else if (isgraph((int)c) != 0 && c != '=') {
  1170. if (c == '#') /* comment start */
  1171. step = 6;
  1172. else /* syntax error */
  1173. st |= CO_fifo_st_errTok;
  1174. }
  1175. continue;
  1176. }
  1177. char code = base64DecTable[c & 0x7F];
  1178. if (c < 0 || code < 0) {
  1179. st |= CO_fifo_st_errTok;
  1180. }
  1181. else if (code >= 64 /* '=' (pad) or DELIM_COMMAND or space */) {
  1182. /* base64 string finished, write remaining bytes */
  1183. switch (step) {
  1184. case 2:
  1185. CO_fifo_putc(dest, (char)(dword >> 4));
  1186. destSpace --;
  1187. break;
  1188. case 3:
  1189. CO_fifo_putc(dest, (char)(dword >> 10));
  1190. CO_fifo_putc(dest, (char)(dword >> 2));
  1191. destSpace -= 2;
  1192. break;
  1193. }
  1194. bool_t insideComment = false;
  1195. if (c == DELIM_COMMAND || CO_fifo_trimSpaces(src, &insideComment)) {
  1196. st |= CO_fifo_st_closed;
  1197. finished = true;
  1198. }
  1199. else {
  1200. step = insideComment ? 6 : 5;
  1201. }
  1202. }
  1203. else {
  1204. dword = (dword << 6) | code;
  1205. if (step++ == 3) {
  1206. CO_fifo_putc(dest, (char)((dword >> 16) & 0xFF));
  1207. CO_fifo_putc(dest, (char)((dword >> 8) & 0xFF));
  1208. CO_fifo_putc(dest, (char)(dword & 0xFF));
  1209. destSpace -= 3;
  1210. dword = 0;
  1211. step = 0;
  1212. }
  1213. }
  1214. } /* while ... */
  1215. if (!finished) {
  1216. st |= CO_fifo_st_partial;
  1217. /* memorize variables for next iteration */
  1218. dest->aux = (uint32_t)step << 24 | (dword & 0xFFFFFF);
  1219. }
  1220. if (status != NULL) *status = st;
  1221. return destSpaceStart - destSpace;
  1222. }
  1223. #endif /* (CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ASCII_DATATYPES */
  1224. #endif /* (CO_CONFIG_FIFO) & CO_CONFIG_FIFO_ENABLE */