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 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
/* Copyright (C) 1993, 1994 Aladdin Enterprises. All rights reserved.
This file is part of GNU Ghostscript.
GNU Ghostscript is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY. No author or distributor accepts responsibility to
anyone for the consequences of using it or for whether it serves any
particular purpose or works at all, unless he says so in writing. Refer
to the GNU Ghostscript General Public License for full details.
*/
/* sstring.c */
/* String and hexstring streams (filters) */
#include "stdio_.h" /* includes std.h */
#include "memory_.h"
#include "strimpl.h"
#include "sfilter.h"
#include "scanchar.h"
/* ------ ASCIIHexEncode ------ */
/* Process a buffer */
private int
s_AXE_process(stream_state *st, stream_cursor_read *pr,
stream_cursor_write *pw, bool last)
{ register const byte *p = pr->ptr;
register byte *q = pw->ptr;
int rcount = pr->limit - p;
int wcount = pw->limit - q;
register int count;
register const char _ds *hex_digits = "0123456789abcdef";
int status = 0;
if ( last )
wcount--; /* leave room for '>' */
wcount -= (wcount + 64) / 65; /* leave room for \n */
wcount >>= 1; /* 2 chars per input byte */
count = (wcount < rcount ? (status = 1, wcount) : rcount);
while ( --count >= 0 )
{ *++q = hex_digits[*++p >> 4];
*++q = hex_digits[*p & 0xf];
if ( !(count & 31) )
*++q = '\n';
}
if ( last && status == 0 )
{ *++q = '>';
}
pr->ptr = p;
pw->ptr = q;
return status;
}
/* Stream template */
const stream_template s_AXE_template =
{ &st_stream_state, NULL, s_AXE_process, 1, 3
};
/* ------ ASCIIHexDecode ------ */
private_st_AXD_state();
#define ss ((stream_AXD_state *)st)
/* Initialize the state */
private int
s_AXD_init(stream_state *st)
{ return s_AXD_init_inline(ss);
}
/* Process a buffer */
private int
s_AXD_process(stream_state *st, stream_cursor_read *pr,
stream_cursor_write *pw, bool last)
{ int code = s_hex_process(pr, pw, &ss->odd, hex_ignore_whitespace);
switch ( code )
{
case 0:
if ( ss->odd >= 0 && last )
{ if ( pw->ptr == pw->limit )
return 1;
*++(pw->ptr) = ss->odd << 4;
}
/* falls through */
default:
return code;
case ERRC:
;
}
/* Check for EOD. ERRC implies at least one more character */
/* was read. */
if ( *pr->ptr != '>' ) /* EOD */
return ERRC;
if ( ss->odd >= 0 )
{ if ( pw->ptr == pw->limit )
return 1;
*++(pw->ptr) = ss->odd << 4;
}
return EOFC;
}
#undef ss
/* Stream template */
const stream_template s_AXD_template =
{ &st_AXD_state, s_AXD_init, s_AXD_process, 2, 1
};
/* ------ PSStringEncode ------ */
/* Process a buffer */
private int
s_PSSE_process(stream_state *st, stream_cursor_read *pr,
stream_cursor_write *pw, bool last)
{ register const byte *p = pr->ptr;
const byte *rlimit = pr->limit;
register byte *q = pw->ptr;
byte *wlimit = pw->limit;
int status = 0;
/* This doesn't have to be very efficient. */
while ( p < rlimit )
{ register int c;
if ( q == wlimit )
{ status = 1;
break;
}
c = *++p;
if ( c < 32 || c >= 127 || c == '(' || c == ')' || c == '\\' )
{ if ( q + 1 == wlimit )
{ status = 1;
break;
}
*++q = '\\';
}
*++q = c;
}
if ( last && status == 0 )
{ if ( q == wlimit )
status = 1;
else
*++q = ')';
}
pr->ptr = p;
pw->ptr = q;
return status;
}
/* Stream template */
const stream_template s_PSSE_template =
{ &st_stream_state, NULL, s_PSSE_process, 1, 4
};
/* ------ PSStringDecode ------ */
private_st_PSSD_state();
#define ss ((stream_PSSD_state *)st)
/* Initialize the state */
private int
s_PSSD_init(stream_state *st)
{ return s_PSSD_init_inline(ss);
}
/* Process a buffer */
private int
s_PSSD_process(stream_state *st, stream_cursor_read *pr,
stream_cursor_write *pw, bool last)
{ register const byte *p = pr->ptr;
const byte *rlimit = pr->limit;
register byte *q = pw->ptr;
byte *wlimit = pw->limit;
int status = 0;
register int c;
#define check_p(n)\
if ( p == rlimit ) { p -= n; goto out; }
#define check_q(n)\
if ( q == wlimit ) { p -= n; status = 1; goto out; }
while ( p < rlimit )
{ c = *++p;
if ( c == '\\' && !ss->from_string )
{ check_p(1);
switch ( (c = *++p) )
{
case 'n':
c = '\n';
goto put;
case 'r':
c = '\r';
goto put;
case 't':
c = '\t';
goto put;
case 'b':
c = '\b';
goto put;
case 'f':
c = '\f';
goto put;
default: /* ignore the \ */
put: check_q(2);
*++q = c;
continue;
case char_CR: /* ignore, check for following \n */
check_p(2);
if ( p[1] == char_EOL )
p++;
continue;
case char_EOL: /* ignore */
continue;
case '0': case '1': case '2': case '3':
case '4': case '5': case '6': case '7':
{ int d;
check_p(2);
d = p[1];
c -= '0';
if ( d >= '0' && d <= '7' )
{ if ( p + 1 == rlimit )
{ p -= 2;
goto out;
}
check_q(2);
c = (c << 3) + d - '0';
d = p[2];
if ( d >= '0' && d <= '7' )
{ c = (c << 3) + d - '0';
p += 2;
}
else
p++;
}
else
check_q(2);
*++q = c;
continue;
}
}
}
else
switch ( c )
{
case '(':
check_q(1);
ss->depth++;
break;
case ')':
if ( ss->depth == 0 )
{ status = EOFC;
goto out;
}
check_q(1);
ss->depth--;
break;
case char_CR: /* convert to \n */
check_p(1);
check_q(1);
if ( p[1] == char_EOL )
p++;
*++q = '\n';
continue;
case char_EOL:
c = '\n';
default:
check_q(1);
break;
}
*++q = c;
}
#undef check_p
#undef check_q
out: pr->ptr = p;
pw->ptr = q;
if ( last && status == 0 && p != rlimit )
status = ERRC;
return status;
}
#undef ss
/* Stream template */
const stream_template s_PSSD_template =
{ &st_PSSD_state, s_PSSD_init, s_PSSD_process, 4, 1
};
/* ------ Utilities ------ */
/*
* Convert hex data to binary. Return 1 if we filled the string, 0 if
* we ran out of input data before filling the string, or ERRC on error.
* The caller must set *odd_digit to -1 before the first call;
* after each call, if an odd number of hex digits has been read (total),
* *odd_digit is the odd digit value, otherwise *odd_digit = -1.
* See strimpl.h for the definition of syntax.
*/
int
s_hex_process(stream_cursor_read *pr, stream_cursor_write *pw,
int *odd_digit, hex_syntax syntax)
{ const byte *p = pr->ptr;
const byte *rlimit = pr->limit;
byte *q = pw->ptr;
byte *wlimit = pw->limit;
byte *q0 = q;
byte val1 = (byte)*odd_digit;
byte val2;
uint rcount;
byte *flimit;
register const byte _ds *decoder = scan_char_decoder;
int code = 0;
if ( q >= wlimit )
return 1;
if ( val1 <= 0xf )
goto d2;
d1: if ( (rcount = (rlimit - p) >> 1) == 0 )
goto x1;
/* Set up a fast end-of-loop check, so we don't have to test */
/* both p and q against their respective limits. */
flimit = (rcount < wlimit - q ? q + rcount : wlimit);
f1: if ( (val1 = decoder[p[1]]) <= 0xf &&
(val2 = decoder[p[2]]) <= 0xf
)
{ p += 2;
*++q = (val1 << 4) + val2;
if ( q < flimit )
goto f1;
if ( q >= wlimit )
goto px;
}
x1: if ( p >= rlimit )
goto end1;
if ( (val1 = decoder[*++p]) > 0xf )
{ if ( val1 == ctype_space )
{ switch ( syntax )
{
case hex_ignore_whitespace:
goto x1;
case hex_ignore_leading_whitespace:
if ( q == q0 && *odd_digit < 0 )
goto x1;
--p;
code = 1;
goto end1;
case hex_ignore_garbage:
goto x1;
}
}
else if ( syntax == hex_ignore_garbage )
goto x1;
code = ERRC;
goto end1;
}
d2: if ( p >= rlimit )
{ *odd_digit = val1;
goto ended;
}
if ( (val2 = decoder[*++p]) > 0xf )
{ if ( val2 == ctype_space )
switch ( syntax )
{
case hex_ignore_whitespace:
goto d2;
case hex_ignore_leading_whitespace:
if ( q == q0 )
goto d2;
--p;
*odd_digit = val1;
code = 1;
goto ended;
}
if ( syntax == hex_ignore_garbage )
goto d2;
*odd_digit = val1;
code = ERRC;
goto ended;
}
*++q = (val1 << 4) + val2;
if ( q < wlimit ) goto d1;
px: code = 1;
end1: *odd_digit = -1;
ended: pr->ptr = p;
pw->ptr = q;
return code;
}
|