phpdbg_eol.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Authors: Anatol Belski <ab@php.net> |
  16. +----------------------------------------------------------------------+
  17. */
  18. #ifdef HAVE_CONFIG_H
  19. # include "config.h"
  20. #endif
  21. #include "phpdbg.h"
  22. #include "phpdbg_eol.h"
  23. ZEND_EXTERN_MODULE_GLOBALS(phpdbg)
  24. #define EOL_LIST_LEN 4
  25. struct phpdbg_eol_rep phpdbg_eol_list[EOL_LIST_LEN] = {
  26. {"CRLF", "\r\n", PHPDBG_EOL_CRLF},
  27. /* {"LFCR", "\n\r", PHPDBG_EOL_LFCR},*/
  28. {"LF", "\n", PHPDBG_EOL_LF},
  29. {"CR", "\r", PHPDBG_EOL_CR},
  30. };
  31. int phpdbg_eol_global_update(char *name)
  32. {
  33. if (0 == memcmp(name, "CRLF", 4) || 0 == memcmp(name, "crlf", 4) || 0 == memcmp(name, "DOS", 3) || 0 == memcmp(name, "dos", 3)) {
  34. PHPDBG_G(eol) = PHPDBG_EOL_CRLF;
  35. } else if (0 == memcmp(name, "LF", 2) || 0 == memcmp(name, "lf", 2) || 0 == memcmp(name, "UNIX", 4) || 0 == memcmp(name, "unix", 4)) {
  36. PHPDBG_G(eol) = PHPDBG_EOL_LF;
  37. } else if (0 == memcmp(name, "CR", 2) || 0 == memcmp(name, "cr", 2) || 0 == memcmp(name, "MAC", 3) || 0 == memcmp(name, "mac", 3)) {
  38. PHPDBG_G(eol) = PHPDBG_EOL_CR;
  39. } else {
  40. return FAILURE;
  41. }
  42. return SUCCESS;
  43. }
  44. char *phpdbg_eol_name(int id)
  45. {
  46. size_t i = 0;
  47. while (i < EOL_LIST_LEN) {
  48. if (id == phpdbg_eol_list[i].id) {
  49. return phpdbg_eol_list[i].name;
  50. }
  51. i++;
  52. }
  53. return NULL;
  54. }
  55. char *phpdbg_eol_rep(int id)
  56. {
  57. size_t i = 0;
  58. while (i < EOL_LIST_LEN) {
  59. if (id == phpdbg_eol_list[i].id) {
  60. return phpdbg_eol_list[i].rep;
  61. }
  62. i++;
  63. }
  64. return NULL;
  65. }
  66. /* Inspired by https://ccrma.stanford.edu/~craig/utility/flip/flip.cpp */
  67. void phpdbg_eol_convert(char **str, int *len)
  68. {
  69. char *in = *str, *out ;
  70. int in_len = *len, out_len, cursor, i;
  71. char last, cur;
  72. if ((PHPDBG_G(flags) & PHPDBG_IS_REMOTE) != PHPDBG_IS_REMOTE) {
  73. return;
  74. }
  75. out_len = *len;
  76. if (PHPDBG_EOL_CRLF == PHPDBG_G(eol)) { /* XXX add LFCR case if it's gonna be needed */
  77. /* depending on the source EOL the out str will have all CR/LF duplicated */
  78. for (i = 0; i < in_len; i++) {
  79. if (0x0a == in[i] || 0x0d == in[i]) {
  80. out_len++;
  81. }
  82. }
  83. out = (char *)emalloc(out_len);
  84. last = cur = in[0];
  85. i = cursor = 0;
  86. for (; i < in_len;) {
  87. if (0x0a == cur && last != 0x0d) {
  88. out[cursor] = 0x0d;
  89. cursor++;
  90. out[cursor] = cur;
  91. } else if(0x0d == cur) {
  92. if (i + 1 < in_len && 0x0a != in[i+1]) {
  93. out[cursor] = cur;
  94. cursor++;
  95. out[cursor] = 0x0a;
  96. last = 0x0a;
  97. } else {
  98. out[cursor] = 0x0d;
  99. last = 0x0d;
  100. }
  101. } else {
  102. out[cursor] = cur;
  103. last = cur;
  104. }
  105. i++;
  106. cursor++;
  107. cur = in[i];
  108. }
  109. } else if (PHPDBG_EOL_LF == PHPDBG_G(eol) || PHPDBG_EOL_CR == PHPDBG_G(eol)) {
  110. char want, kick;
  111. if (PHPDBG_EOL_LF == PHPDBG_G(eol)) {
  112. want = 0x0a;
  113. kick = 0x0d;
  114. } else {
  115. want = 0x0d;
  116. kick = 0x0a;
  117. }
  118. /* We gonna have a smaller or equally long string, estimation is almost neglecting */
  119. out = (char *)emalloc(out_len);
  120. last = cur = in[0];
  121. i = cursor = 0;
  122. for (; cursor < in_len;) {
  123. if (kick == cur) {
  124. out[cursor] = want;
  125. } else if (want == cur) {
  126. if (kick != last) {
  127. out[cursor] = want;
  128. }
  129. } else {
  130. out[cursor] = cur;
  131. }
  132. last = cur;
  133. cursor++;
  134. cur = in[cursor];
  135. }
  136. } else {
  137. return;
  138. }
  139. efree(*str);
  140. *str = erealloc(out, cursor);
  141. *len = cursor;
  142. in = NULL;
  143. }