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