You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

581 lines
17 KiB

  1. /*
  2. * libcucul Canvas for ultrafast compositing of Unicode letters
  3. * Copyright (c) 2002-2006 Sam Hocevar <sam@zoy.org>
  4. * All Rights Reserved
  5. *
  6. * $Id$
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the Do What The Fuck You Want To
  10. * Public License, Version 2, as published by Sam Hocevar. See
  11. * http://sam.zoy.org/wtfpl/COPYING for more details.
  12. */
  13. /*
  14. * This file contains various import functions.
  15. */
  16. #include "config.h"
  17. #include "common.h"
  18. #if !defined(__KERNEL__)
  19. # if defined(HAVE_ERRNO_H)
  20. # include <errno.h>
  21. # endif
  22. # if defined(HAVE_ARPA_INET_H)
  23. # include <arpa/inet.h>
  24. # elif defined(HAVE_NETINET_IN_H)
  25. # include <netinet/in.h>
  26. # endif
  27. # include <stdio.h>
  28. # include <stdlib.h>
  29. # include <string.h>
  30. #endif
  31. #include "cucul.h"
  32. #include "cucul_internals.h"
  33. static inline uint32_t sscanu32(void const *s)
  34. {
  35. uint32_t x;
  36. memcpy(&x, s, 4);
  37. return ntohl(x);
  38. }
  39. static inline uint16_t sscanu16(void const *s)
  40. {
  41. uint16_t x;
  42. memcpy(&x, s, 2);
  43. return ntohs(x);
  44. }
  45. /* ANSI Graphic Rendition Combination Mode */
  46. struct ansi_grcm
  47. {
  48. uint8_t fg, bg; /* ANSI-context fg/bg */
  49. uint8_t efg, ebg; /* Effective (libcucul) fg/bg */
  50. uint8_t bold, negative, concealed;
  51. };
  52. static cucul_canvas_t *import_caca(void const *, unsigned int);
  53. static cucul_canvas_t *import_text(void const *, unsigned int);
  54. static cucul_canvas_t *import_ansi(void const *, unsigned int, int);
  55. static void ansi_parse_grcm(cucul_canvas_t *, struct ansi_grcm *,
  56. unsigned int, unsigned int const *);
  57. /** \brief Import a buffer into a canvas
  58. *
  59. * Import a libcucul buffer as returned by cucul_load_memory()
  60. * or cucul_load_file() into an internal libcucul canvas.
  61. *
  62. * Valid values for \c format are:
  63. * - \c "": attempt to autodetect the file format.
  64. * - \c "text": import ASCII text files.
  65. * - \c "ansi": import ANSI files.
  66. * - \c "utf8": import UTF-8 files with ANSI colour codes.
  67. * - \c "caca": import native libcaca files.
  68. *
  69. * If an error occurs, NULL is returned and \b errno is set accordingly:
  70. * - \c ENOMEM Not enough memory to allocate canvas.
  71. * - \c EINVAL Invalid format requested.
  72. *
  73. * \param buffer A \e libcucul buffer containing the data to be loaded
  74. * into a canvas.
  75. * \param format A string describing the input format.
  76. * \return A libcucul canvas, or NULL in case of error.
  77. */
  78. cucul_canvas_t * cucul_import_canvas(cucul_buffer_t *buffer, char const *format)
  79. {
  80. char const *buf = (char const*)buffer->data;
  81. if(!strcasecmp("caca", format))
  82. return import_caca(buffer->data, buffer->size);
  83. if(!strcasecmp("utf8", format))
  84. return import_ansi(buffer->data, buffer->size, 1);
  85. if(!strcasecmp("text", format))
  86. return import_text(buffer->data, buffer->size);
  87. if(!strcasecmp("ansi", format))
  88. return import_ansi(buffer->data, buffer->size, 0);
  89. /* Autodetection */
  90. if(!strcasecmp("", format))
  91. {
  92. unsigned int i;
  93. /* If 4 first letters are CACA */
  94. if(buffer->size >= 4 && (uint8_t)buf[0] == 0xca &&
  95. (uint8_t)buf[1] == 0xca && buf[2] == 'C' && buf[3] == 'V')
  96. return import_caca(buffer->data, buffer->size);
  97. /* If we find ESC[ argv, we guess it's an ANSI file */
  98. for(i = 0; i + 1 < buffer->size; i++)
  99. if((buf[i] == 0x1b) && (buf[i + 1] == '['))
  100. return import_ansi(buffer->data, buffer->size, 0);
  101. /* Otherwise, import it as text */
  102. return import_text(buffer->data, buffer->size);
  103. }
  104. #if defined(HAVE_ERRNO_H)
  105. errno = EINVAL;
  106. #endif
  107. return NULL;
  108. }
  109. /** \brief Get available import formats
  110. *
  111. * Return a list of available import formats. The list is a NULL-terminated
  112. * array of strings, interleaving a string containing the internal value for
  113. * the import format, to be used with cucul_import_canvas(), and a string
  114. * containing the natural language description for that import format.
  115. *
  116. * This function never fails.
  117. *
  118. * \return An array of strings.
  119. */
  120. char const * const * cucul_get_import_list(void)
  121. {
  122. static char const * const list[] =
  123. {
  124. "", "autodetect",
  125. "text", "plain text",
  126. "caca", "native libcaca format",
  127. "ansi", "ANSI coloured text",
  128. NULL, NULL
  129. };
  130. return list;
  131. }
  132. /*
  133. * XXX: the following functions are local.
  134. */
  135. static cucul_canvas_t *import_caca(void const *data, unsigned int size)
  136. {
  137. cucul_canvas_t *cv;
  138. uint8_t const *buf = (uint8_t const *)data;
  139. unsigned int control_size, data_size, full_size, frames, f, n;
  140. uint16_t version, flags;
  141. if(size < 20)
  142. goto invalid_caca;
  143. if(buf[0] != 0xca || buf[1] != 0xca || buf[2] != 'C' || buf[3] != 'V')
  144. goto invalid_caca;
  145. control_size = sscanu32(buf + 4);
  146. data_size = sscanu32(buf + 8);
  147. version = sscanu16(buf + 12);
  148. frames = sscanu32(buf + 14);
  149. flags = sscanu16(buf + 18);
  150. if(size != 4 + control_size + data_size)
  151. goto invalid_caca;
  152. if(control_size < 16 + frames * 24)
  153. goto invalid_caca;
  154. for(full_size = 0, f = 0; f < frames; f++)
  155. {
  156. unsigned int width, height, duration;
  157. uint32_t attr;
  158. int x, y;
  159. width = sscanu32(buf + 4 + 16 + f * 24);
  160. height = sscanu32(buf + 4 + 16 + f * 24 + 4);
  161. duration = sscanu32(buf + 4 + 16 + f * 24 + 8);
  162. attr = sscanu32(buf + 4 + 16 + f * 24 + 12);
  163. x = (int32_t)sscanu32(buf + 4 + 16 + f * 24 + 16);
  164. y = (int32_t)sscanu32(buf + 4 + 16 + f * 24 + 20);
  165. full_size += width * height * 8;
  166. }
  167. if(full_size != data_size)
  168. goto invalid_caca;
  169. /* FIXME: read all frames, not only the first one */
  170. cv = cucul_create_canvas(sscanu32(buf + 4 + 16),
  171. sscanu32(buf + 4 + 16 + 4));
  172. if(!cv)
  173. {
  174. #if defined(HAVE_ERRNO_H)
  175. errno = ENOMEM;
  176. #endif
  177. return NULL;
  178. }
  179. for(n = sscanu32(buf + 4 + 16) * sscanu32(buf + 4 + 16 + 4); n--; )
  180. {
  181. cv->chars[n] = sscanu32(buf + 4 + control_size + 8 * n);
  182. cv->attrs[n] = sscanu32(buf + 4 + control_size + 8 * n + 4);
  183. }
  184. cv->curattr = sscanu32(buf + 4 + 16 + 12);
  185. return cv;
  186. invalid_caca:
  187. #if defined(HAVE_ERRNO_H)
  188. errno = EINVAL;
  189. #endif
  190. return NULL;
  191. }
  192. static cucul_canvas_t *import_text(void const *data, unsigned int size)
  193. {
  194. cucul_canvas_t *cv;
  195. char const *text = (char const *)data;
  196. unsigned int width = 0, height = 0, x = 0, y = 0, i;
  197. cv = cucul_create_canvas(width, height);
  198. if(!cv)
  199. {
  200. #if defined(HAVE_ERRNO_H)
  201. errno = ENOMEM;
  202. #endif
  203. return NULL;
  204. }
  205. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
  206. for(i = 0; i < size; i++)
  207. {
  208. unsigned char ch = *text++;
  209. if(ch == '\r')
  210. continue;
  211. if(ch == '\n')
  212. {
  213. x = 0;
  214. y++;
  215. continue;
  216. }
  217. if(x >= width || y >= height)
  218. {
  219. if(x >= width)
  220. width = x + 1;
  221. if(y >= height)
  222. height = y + 1;
  223. cucul_set_canvas_size(cv, width, height);
  224. }
  225. cucul_putchar(cv, x, y, ch);
  226. x++;
  227. }
  228. if(y > height)
  229. cucul_set_canvas_size(cv, width, height = y);
  230. return cv;
  231. }
  232. static cucul_canvas_t *import_ansi(void const *data, unsigned int size,
  233. int utf8)
  234. {
  235. struct ansi_grcm grcm;
  236. unsigned char const *buffer = (unsigned char const*)data;
  237. cucul_canvas_t *cv;
  238. unsigned int i, j, skip, dummy = 0;
  239. unsigned int width = 0, height = 0, wch = 1;
  240. unsigned long int ch;
  241. int x = 0, y = 0, save_x = 0, save_y = 0;
  242. cv = cucul_create_canvas(width, height);
  243. if(!cv)
  244. {
  245. #if defined(HAVE_ERRNO_H)
  246. errno = ENOMEM;
  247. #endif
  248. return NULL;
  249. }
  250. ansi_parse_grcm(cv, &grcm, 1, &dummy);
  251. for(i = 0; i < size; i += skip)
  252. {
  253. skip = 1;
  254. /* Wrap long lines */
  255. if((unsigned int)x >= 80)
  256. {
  257. x = 0;
  258. y++;
  259. }
  260. if(buffer[i] == '\x1a' && size - i >= 8
  261. && !memcmp(buffer + i + 1, "SAUCE00", 7))
  262. break; /* End before SAUCE data */
  263. if(buffer[i] == '\r')
  264. continue; /* DOS sucks */
  265. if(buffer[i] == '\n')
  266. {
  267. x = 0;
  268. y++;
  269. continue;
  270. }
  271. /* Interpret escape commands, as per Standard ECMA-48 "Control
  272. * Functions for Coded Character Sets", 5.4. Control sequences. */
  273. if(buffer[i] == '\x1b' && buffer[i + 1] == '[')
  274. {
  275. unsigned int argc = 0, argv[101];
  276. unsigned int param, inter, final;
  277. /* Compute offsets to parameter bytes, intermediate bytes and
  278. * to the final byte. Only the final byte is mandatory, there
  279. * can be zero of the others.
  280. * 0 param=2 inter final final+1
  281. * +-----+------------------+---------------------+-----------------+
  282. * | CSI | parameter bytes | intermediate bytes | final byte |
  283. * | | 0x30 - 0x3f | 0x20 - 0x2f | 0x40 - 0x7e |
  284. * | ^[[ | 0123456789:;<=>? | SPC !"#$%&'()*+,-./ | azAZ@[\]^_`{|}~ |
  285. * +-----+------------------+---------------------+-----------------+
  286. */
  287. param = 2;
  288. for(inter = param; i + inter < size; inter++)
  289. if(buffer[i + inter] < 0x30 || buffer[i + inter] > 0x3f)
  290. break;
  291. for(final = inter; i + final < size; final++)
  292. if(buffer[i + final] < 0x20 || buffer[i + final] > 0x2f)
  293. break;
  294. if(buffer[i + final] < 0x40 || buffer[i + final] > 0x7e)
  295. break; /* Invalid Final Byte */
  296. skip += final;
  297. /* Sanity checks */
  298. if(param < inter && buffer[i + param] >= 0x3c)
  299. {
  300. fprintf(stderr, "private sequence \"^[[%.*s\"\n",
  301. final - param + 1, buffer + i + param);
  302. continue; /* Private sequence, skip it entirely */
  303. }
  304. if(final - param > 100)
  305. continue; /* Suspiciously long sequence, skip it */
  306. /* Parse parameter bytes as per ECMA-48 5.4.2: Parameter string
  307. * format */
  308. if(param < inter)
  309. {
  310. argv[0] = 0;
  311. for(j = param; j < inter; j++)
  312. {
  313. if(buffer[i + j] == ';')
  314. argv[++argc] = 0;
  315. else if(buffer[i + j] >= '0' && buffer[i + j] <= '9')
  316. argv[argc] = 10 * argv[argc] + (buffer[i + j] - '0');
  317. }
  318. argc++;
  319. }
  320. /* Interpret final byte. The code representations are given in
  321. * ECMA-48 5.4: Control sequences, and the code definitions are
  322. * given in ECMA-48 8.3: Definition of control functions. */
  323. switch(buffer[i + final])
  324. {
  325. case 'f': /* CUP - Cursor Position */
  326. case 'H': /* HVP - Character And Line Position */
  327. x = (argc > 1 && argv[1] > 0) ? argv[1] - 1 : 0;
  328. y = (argc > 0 && argv[0] > 0) ? argv[0] - 1 : 0;
  329. break;
  330. case 'A': /* CUU - Cursor Up */
  331. y -= argc ? argv[0] : 1;
  332. if(y < 0)
  333. y = 0;
  334. break;
  335. case 'B': /* CUD - Cursor Down */
  336. y += argc ? argv[0] : 1;
  337. break;
  338. case 'C': /* CUF - Cursor Right */
  339. x += argc ? argv[0] : 1;
  340. break;
  341. case 'D': /* CUB - Cursor Left */
  342. x -= argc ? argv[0] : 1;
  343. if(x < 0)
  344. x = 0;
  345. break;
  346. case 's': /* Private (save cursor position) */
  347. save_x = x;
  348. save_y = y;
  349. break;
  350. case 'u': /* Private (reload cursor position) */
  351. x = save_x;
  352. y = save_y;
  353. break;
  354. case 'J': /* ED - Erase In Page */
  355. if(argv[0] == 2)
  356. x = y = 0;
  357. break;
  358. case 'K': /* EL - Erase In Line */
  359. if(width < 80)
  360. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
  361. cucul_set_canvas_size(cv, width = 80, height);
  362. for(j = x; j < 80; j++)
  363. cucul_putchar(cv, j, y, ' ');
  364. x = 80;
  365. break;
  366. case 'm': /* SGR - Select Graphic Rendition */
  367. ansi_parse_grcm(cv, &grcm, argc, argv);
  368. break;
  369. default:
  370. fprintf(stderr, "unknown command %c\n", buffer[i + final]);
  371. break;
  372. }
  373. continue;
  374. }
  375. /* Get the character we’re going to paste */
  376. if(utf8)
  377. {
  378. unsigned int bytes;
  379. if(i + 6 < size)
  380. ch = cucul_utf8_to_utf32((char const *)(buffer + i), &bytes);
  381. else
  382. {
  383. /* Add a trailing zero to what we're going to read */
  384. char tmp[7];
  385. memcpy(tmp, buffer + i, size - i);
  386. tmp[size - i] = '\0';
  387. ch = cucul_utf8_to_utf32(tmp, &bytes);
  388. }
  389. if(!bytes)
  390. {
  391. /* If the Unicode is invalid, assume it was latin1. */
  392. ch = buffer[i];
  393. bytes = 1;
  394. }
  395. wch = cucul_utf32_is_fullwidth(ch) ? 2 : 1;
  396. skip += bytes - 1;
  397. }
  398. else
  399. {
  400. ch = cucul_cp437_to_utf32(buffer[i]);
  401. }
  402. /* Make sure the canvas is big enough. */
  403. if((unsigned int)x + wch > width)
  404. {
  405. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
  406. cucul_set_canvas_size(cv, width = x + wch, height);
  407. }
  408. if((unsigned int)y >= height)
  409. {
  410. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
  411. cucul_set_canvas_size(cv, width, height = y + 1);
  412. }
  413. /* Now paste our character */
  414. cucul_set_color_ansi(cv, grcm.efg, grcm.ebg);
  415. cucul_putchar(cv, x, y, ch);
  416. x += wch;
  417. }
  418. if((unsigned int)y > height)
  419. {
  420. cucul_set_color_ansi(cv, CUCUL_DEFAULT, CUCUL_TRANSPARENT);
  421. cucul_set_canvas_size(cv, width, height = y);
  422. }
  423. return cv;
  424. }
  425. /* XXX : ANSI loader helper */
  426. static void ansi_parse_grcm(cucul_canvas_t *cv, struct ansi_grcm *g,
  427. unsigned int argc, unsigned int const *argv)
  428. {
  429. static uint8_t const ansi2cucul[] =
  430. {
  431. CUCUL_BLACK, CUCUL_RED, CUCUL_GREEN, CUCUL_BROWN,
  432. CUCUL_BLUE, CUCUL_MAGENTA, CUCUL_CYAN, CUCUL_LIGHTGRAY
  433. };
  434. unsigned int j;
  435. for(j = 0; j < argc; j++)
  436. {
  437. /* Defined in ECMA-48 8.3.117: SGR - SELECT GRAPHIC RENDITION */
  438. if(argv[j] >= 30 && argv[j] <= 37)
  439. g->fg = ansi2cucul[argv[j] - 30];
  440. else if(argv[j] >= 40 && argv[j] <= 47)
  441. g->bg = ansi2cucul[argv[j] - 40];
  442. else if(argv[j] >= 90 && argv[j] <= 97)
  443. g->fg = ansi2cucul[argv[j] - 90] + 8;
  444. else if(argv[j] >= 100 && argv[j] <= 107)
  445. g->bg = ansi2cucul[argv[j] - 100] + 8;
  446. else switch(argv[j])
  447. {
  448. case 0: /* default rendition */
  449. g->fg = CUCUL_DEFAULT;
  450. g->bg = CUCUL_TRANSPARENT;
  451. g->bold = g->negative = g->concealed = 0;
  452. break;
  453. case 1: /* bold or increased intensity */
  454. g->bold = 1;
  455. break;
  456. case 4: /* singly underlined */
  457. break;
  458. case 5: /* slowly blinking (less then 150 per minute) */
  459. break;
  460. case 7: /* negative image */
  461. g->negative = 1;
  462. break;
  463. case 8: /* concealed characters */
  464. g->concealed = 1;
  465. break;
  466. case 22: /* normal colour or normal intensity (neither bold nor faint) */
  467. g->bold = 0;
  468. break;
  469. case 28: /* revealed characters */
  470. g->concealed = 0;
  471. break;
  472. case 39: /* default display colour (implementation-defined) */
  473. g->fg = CUCUL_DEFAULT;
  474. break;
  475. case 49: /* default background colour (implementation-defined) */
  476. g->bg = CUCUL_TRANSPARENT;
  477. break;
  478. default:
  479. fprintf(stderr, "unknown sgr %i\n", argv[j]);
  480. break;
  481. }
  482. }
  483. if(g->concealed)
  484. {
  485. g->efg = g->ebg = CUCUL_TRANSPARENT;
  486. }
  487. else
  488. {
  489. g->efg = g->negative ? g->bg : g->fg;
  490. g->ebg = g->negative ? g->fg : g->bg;
  491. if(g->bold)
  492. {
  493. if(g->efg < 8)
  494. g->efg += 8;
  495. else if(g->efg == CUCUL_DEFAULT)
  496. g->efg = CUCUL_WHITE;
  497. }
  498. }
  499. }