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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
/*
* Based on node-yencode library by Anime Tosho:
* https://github.com/animetosho/node-yencode
*
* Copyright (C) 2017 Anime Tosho (animetosho)
* Copyright (C) 2017 Andrey Prygunkov <hugbug@users.sourceforge.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "nzbget.h"
#include "YEncode.h"
namespace YEncode
{
// return values:
// - 0: no end sequence found
// - 1: \r\n=y sequence found, src points to byte after 'y'
// - 2: \r\n.\r\n sequence found, src points to byte after last '\n'
int decode_scalar(const unsigned char** src, unsigned char** dest, size_t len, YencDecoderState* state) {
const unsigned char *es = (*src) + len; // end source pointer
unsigned char *p = *dest; // destination pointer
long i = -(long)len; // input position
unsigned char c; // input character
if(len < 1) return 0;
#define YDEC_CHECK_END(s) if(i == 0) { \
*state = s; \
*src = es; \
*dest = p; \
return 0; \
}
if(state) switch(*state) {
case YDEC_STATE_CRLFEQ: do_decode_endable_scalar_ceq:
if(es[i] == 'y') {
*state = YDEC_STATE_NONE;
*src = es+i+1;
*dest = p;
return 1;
} // else fall thru and escape
case YDEC_STATE_EQ:
c = es[i];
*p++ = c - 42 - 64;
i++;
if(c != '\r') break;
YDEC_CHECK_END(YDEC_STATE_CR)
// fall through
case YDEC_STATE_CR:
if(es[i] != '\n') break;
i++;
YDEC_CHECK_END(YDEC_STATE_CRLF)
case YDEC_STATE_CRLF: do_decode_endable_scalar_c0:
if(es[i] == '.') {
i++;
YDEC_CHECK_END(YDEC_STATE_CRLFDT)
// fallthru
} else if(es[i] == '=') {
i++;
YDEC_CHECK_END(YDEC_STATE_CRLFEQ)
goto do_decode_endable_scalar_ceq;
} else
break;
case YDEC_STATE_CRLFDT:
if(es[i] == '\r') {
i++;
YDEC_CHECK_END(YDEC_STATE_CRLFDTCR)
// fallthru
} else if(es[i] == '=') { // check for dot-stuffed ending: \r\n.=y
i++;
YDEC_CHECK_END(YDEC_STATE_CRLFEQ)
goto do_decode_endable_scalar_ceq;
} else
break;
case YDEC_STATE_CRLFDTCR:
if(es[i] == '\n') {
*state = YDEC_STATE_CRLF;
*src = es + i + 1;
*dest = p;
return 2;
} else
break;
case YDEC_STATE_NONE: break; // silence compiler warning
} else // treat as YDEC_STATE_CRLF
goto do_decode_endable_scalar_c0;
for(; i < -2; i++) {
c = es[i];
switch(c) {
case '\r': {
if(es[i+1] == '\n') {
c = es[i+2];
if(c == '.') {
// skip past \r\n. sequences
i += 3;
YDEC_CHECK_END(YDEC_STATE_CRLFDT)
// check for end
if(es[i] == '\r') {
i++;
YDEC_CHECK_END(YDEC_STATE_CRLFDTCR)
if(es[i] == '\n') {
*src = es + i + 1;
*dest = p;
*state = YDEC_STATE_CRLF;
return 2;
} else i--;
} else if(es[i] == '=') {
i++;
YDEC_CHECK_END(YDEC_STATE_CRLFEQ)
if(es[i] == 'y') {
*src = es + i + 1;
*dest = p;
*state = YDEC_STATE_NONE;
return 1;
} else {
// escape char & continue
c = es[i];
*p++ = c - 42 - 64;
i -= (c == '\r');
}
} else i--;
}
else if(c == '=') {
i += 3;
YDEC_CHECK_END(YDEC_STATE_CRLFEQ)
if(es[i] == 'y') {
// ended
*src = es + i + 1;
*dest = p;
*state = YDEC_STATE_NONE;
return 1;
} else {
// escape char & continue
c = es[i];
*p++ = c - 42 - 64;
i -= (c == '\r');
}
}
}
} case '\n':
continue;
case '=':
c = es[i+1];
*p++ = c - 42 - 64;
i += (c != '\r'); // if we have a \r, reprocess character to deal with \r\n. case
continue;
default:
*p++ = c - 42;
}
}
if(state) *state = YDEC_STATE_NONE;
if(i == -2) { // 2nd last char
c = es[i];
switch(c) {
case '\r':
if(state && es[i+1] == '\n') {
*state = YDEC_STATE_CRLF;
*src = es;
*dest = p;
return 0;
}
case '\n':
break;
case '=':
c = es[i+1];
*p++ = c - 42 - 64;
i += (c != '\r');
break;
default:
*p++ = c - 42;
}
i++;
}
// do final char; we process this separately to prevent an overflow if the final char is '='
if(i == -1) {
c = es[i];
if(c != '\n' && c != '\r' && c != '=') {
*p++ = c - 42;
} else if(state) {
if(c == '=') *state = YDEC_STATE_EQ;
else if(c == '\r') *state = YDEC_STATE_CR;
else *state = YDEC_STATE_NONE;
}
}
#undef YDEC_CHECK_END
*src = es;
*dest = p;
return 0;
}
void init_decode_scalar() {
decode = decode_scalar;
}
}
|