| 1 |
/* This is dvipdfmx, an eXtended version of dvipdfm by Mark A. Wicks. |
| 2 |
|
| 3 |
Copyright (C) 2002-2020 by Jin-Hwan Cho and Shunsaku Hirata, |
| 4 |
the dvipdfmx project team. |
| 5 |
|
| 6 |
Copyright (C) 1998, 1999 by Mark A. Wicks <[email protected]> |
| 7 |
|
| 8 |
This program is free software; you can redistribute it and/or modify |
| 9 |
it under the terms of the GNU General Public License as published by |
| 10 |
the Free Software Foundation; either version 2 of the License, or |
| 11 |
(at your option) any later version. |
| 12 |
|
| 13 |
This program is distributed in the hope that it will be useful, |
| 14 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
GNU General Public License for more details. |
| 17 |
|
| 18 |
You should have received a copy of the GNU General Public License |
| 19 |
along with this program; if not, write to the Free Software |
| 20 |
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. |
| 21 |
*/ |
| 22 |
|
| 23 |
#ifdef HAVE_CONFIG_H |
| 24 |
#include <config.h> |
| 25 |
#endif |
| 26 |
|
| 27 |
#include <string.h> |
| 28 |
#include <stdlib.h> |
| 29 |
#include <errno.h> |
| 30 |
|
| 31 |
#include "system.h" |
| 32 |
#include "mem.h" |
| 33 |
#include "error.h" |
| 34 |
#include "dpxutil.h" |
| 35 |
#include "pst.h" |
| 36 |
#include "pst_obj.h" |
| 37 |
|
| 38 |
struct pst_obj |
| 39 |
{ |
| 40 |
pst_type type; |
| 41 |
void *data; |
| 42 |
}; |
| 43 |
|
| 44 |
static const char *pst_const_null = "null"; |
| 45 |
static const char *pst_const_mark = "mark"; |
| 46 |
/* |
| 47 |
static const char *pst_const_true = "true"; |
| 48 |
static const char *pst_const_false = "false"; |
| 49 |
*/ |
| 50 |
|
| 51 |
typedef char * pst_null; |
| 52 |
typedef struct { char value; } pst_boolean; |
| 53 |
typedef struct { int value; } pst_integer; |
| 54 |
typedef struct { double value; } pst_real; |
| 55 |
typedef struct { char *value; } pst_name; |
| 56 |
typedef struct |
| 57 |
{ |
| 58 |
unsigned int length; |
| 59 |
unsigned char *value; |
| 60 |
} pst_string; |
| 61 |
|
| 62 |
|
| 63 |
/* BOOLEAN */ |
| 64 |
static pst_boolean *pst_boolean_new (char value) ; |
| 65 |
static void pst_boolean_release (pst_boolean *obj); |
| 66 |
static int pst_boolean_IV (pst_boolean *obj); |
| 67 |
static double pst_boolean_RV (pst_boolean *obj); |
| 68 |
static unsigned char *pst_boolean_SV (pst_boolean *obj); |
| 69 |
static unsigned int pst_boolean_length (void); |
| 70 |
static void *pst_boolean_data_ptr(pst_boolean *obj); |
| 71 |
|
| 72 |
/* NUMBERS */ |
| 73 |
static pst_integer *pst_integer_new (int value) ; |
| 74 |
static void pst_integer_release (pst_integer *obj); |
| 75 |
static int pst_integer_IV (pst_integer *obj); |
| 76 |
static double pst_integer_RV (pst_integer *obj); |
| 77 |
static unsigned char *pst_integer_SV (pst_integer *obj); |
| 78 |
static unsigned int pst_integer_length (void); |
| 79 |
static void *pst_integer_data_ptr(pst_integer *obj); |
| 80 |
|
| 81 |
static pst_real *pst_real_new (double value) ; |
| 82 |
static void pst_real_release (pst_real *obj); |
| 83 |
static int pst_real_IV (pst_real *obj); |
| 84 |
static double pst_real_RV (pst_real *obj); |
| 85 |
static unsigned char *pst_real_SV (pst_real *obj); |
| 86 |
static void *pst_real_data_ptr (pst_real *obj); |
| 87 |
static unsigned int pst_real_length (void); |
| 88 |
|
| 89 |
/* NAME */ |
| 90 |
static pst_name *pst_name_new (const char *name) ; |
| 91 |
static void pst_name_release (pst_name *obj); |
| 92 |
static int pst_name_IV (void); |
| 93 |
static double pst_name_RV (void); |
| 94 |
static unsigned char *pst_name_SV (pst_name *obj); |
| 95 |
static void *pst_name_data_ptr (pst_name *obj); |
| 96 |
static unsigned int pst_name_length (pst_name *obj); |
| 97 |
|
| 98 |
/* STRING */ |
| 99 |
static pst_string *pst_string_parse_literal (unsigned char **inbuf, unsigned char *inbufend); |
| 100 |
static pst_string *pst_string_parse_hex (unsigned char **inbuf, unsigned char *inbufend); |
| 101 |
|
| 102 |
static pst_string *pst_string_new (unsigned char *str, unsigned int len); |
| 103 |
static void pst_string_release (pst_string *obj) ; |
| 104 |
static int pst_string_IV (pst_string *obj) ; |
| 105 |
static double pst_string_RV (pst_string *obj) ; |
| 106 |
static unsigned char *pst_string_SV (pst_string *obj) ; |
| 107 |
static void *pst_string_data_ptr (pst_string *obj) ; |
| 108 |
static unsigned int pst_string_length (pst_string *obj) ; |
| 109 |
|
| 110 |
|
| 111 |
#define TYPE_ERROR() ERROR("Operation not defined for this type of object.") |
| 112 |
|
| 113 |
pst_obj * |
| 114 |
pst_new_obj (pst_type type, void *data) |
| 115 |
{ |
| 116 |
pst_obj *obj; |
| 117 |
|
| 118 |
obj = NEW(1, struct pst_obj); |
| 119 |
obj->type = type; |
| 120 |
obj->data = data; |
| 121 |
|
| 122 |
switch (obj->type) { |
| 123 |
case PST_TYPE_BOOLEAN: |
| 124 |
case PST_TYPE_INTEGER: |
| 125 |
case PST_TYPE_REAL: |
| 126 |
case PST_TYPE_NAME: |
| 127 |
case PST_TYPE_STRING: |
| 128 |
case PST_TYPE_NULL: |
| 129 |
case PST_TYPE_MARK: |
| 130 |
case PST_TYPE_UNKNOWN: |
| 131 |
if (data == NULL) |
| 132 |
ERROR("NULL pointer data for object type: %d", obj->type); |
| 133 |
break; |
| 134 |
default: |
| 135 |
ERROR("Unrecognized object type: %d", obj->type); |
| 136 |
} |
| 137 |
return obj; |
| 138 |
} |
| 139 |
|
| 140 |
pst_obj * |
| 141 |
pst_new_mark (void) |
| 142 |
{ |
| 143 |
char *q; |
| 144 |
|
| 145 |
q = NEW(strlen(pst_const_mark)+1, char); |
| 146 |
strcpy(q, pst_const_mark); |
| 147 |
return pst_new_obj(PST_TYPE_MARK, (void *)q); |
| 148 |
} |
| 149 |
|
| 150 |
void |
| 151 |
pst_release_obj (pst_obj *obj) |
| 152 |
{ |
| 153 |
ASSERT(obj); |
| 154 |
switch (obj->type) { |
| 155 |
case PST_TYPE_BOOLEAN: pst_boolean_release(obj->data); break; |
| 156 |
case PST_TYPE_INTEGER: pst_integer_release(obj->data); break; |
| 157 |
case PST_TYPE_REAL: pst_real_release(obj->data); break; |
| 158 |
case PST_TYPE_NAME: pst_name_release(obj->data); break; |
| 159 |
case PST_TYPE_STRING: pst_string_release(obj->data); break; |
| 160 |
case PST_TYPE_NULL: |
| 161 |
case PST_TYPE_MARK: |
| 162 |
case PST_TYPE_UNKNOWN: |
| 163 |
if (obj->data) |
| 164 |
RELEASE(obj->data); |
| 165 |
break; |
| 166 |
default: |
| 167 |
ERROR("Unrecognized object type: %d", obj->type); |
| 168 |
} |
| 169 |
RELEASE(obj); |
| 170 |
} |
| 171 |
|
| 172 |
pst_type |
| 173 |
pst_type_of (pst_obj *obj) |
| 174 |
{ |
| 175 |
ASSERT(obj); |
| 176 |
return obj->type; |
| 177 |
} |
| 178 |
|
| 179 |
int |
| 180 |
pst_length_of (pst_obj *obj) |
| 181 |
{ |
| 182 |
int len = 0; |
| 183 |
|
| 184 |
ASSERT(obj); |
| 185 |
switch (obj->type) { |
| 186 |
case PST_TYPE_BOOLEAN: len = pst_boolean_length(); break; |
| 187 |
case PST_TYPE_INTEGER: len = pst_integer_length(); break; |
| 188 |
case PST_TYPE_REAL: len = pst_real_length(); break; |
| 189 |
case PST_TYPE_NAME: len = pst_name_length(obj->data); break; |
| 190 |
case PST_TYPE_STRING: len = pst_string_length(obj->data); break; |
| 191 |
case PST_TYPE_NULL: |
| 192 |
case PST_TYPE_MARK: |
| 193 |
TYPE_ERROR(); |
| 194 |
break; |
| 195 |
case PST_TYPE_UNKNOWN: |
| 196 |
len = strlen(obj->data); |
| 197 |
break; |
| 198 |
default: |
| 199 |
ERROR("Unrecognized object type: %d", obj->type); |
| 200 |
} |
| 201 |
|
| 202 |
return len; |
| 203 |
} |
| 204 |
|
| 205 |
int |
| 206 |
pst_getIV (pst_obj *obj) |
| 207 |
{ |
| 208 |
int iv = 0; |
| 209 |
|
| 210 |
ASSERT(obj); |
| 211 |
switch (obj->type) { |
| 212 |
case PST_TYPE_BOOLEAN: iv = pst_boolean_IV(obj->data); break; |
| 213 |
case PST_TYPE_INTEGER: iv = pst_integer_IV(obj->data); break; |
| 214 |
case PST_TYPE_REAL: iv = pst_real_IV(obj->data); break; |
| 215 |
case PST_TYPE_NAME: iv = pst_name_IV(); break; |
| 216 |
case PST_TYPE_STRING: iv = pst_string_IV(obj->data); break; |
| 217 |
case PST_TYPE_NULL: |
| 218 |
case PST_TYPE_MARK: |
| 219 |
TYPE_ERROR(); |
| 220 |
break; |
| 221 |
case PST_TYPE_UNKNOWN: |
| 222 |
ERROR("Cannot convert object of type UNKNOWN to integer value."); |
| 223 |
break; |
| 224 |
default: |
| 225 |
ERROR("Unrecognized object type: %d", obj->type); |
| 226 |
} |
| 227 |
|
| 228 |
return iv; |
| 229 |
} |
| 230 |
|
| 231 |
double |
| 232 |
pst_getRV (pst_obj *obj) |
| 233 |
{ |
| 234 |
double rv = 0.0; |
| 235 |
|
| 236 |
ASSERT(obj); |
| 237 |
switch (obj->type) { |
| 238 |
case PST_TYPE_BOOLEAN: rv = pst_boolean_RV(obj->data); break; |
| 239 |
case PST_TYPE_INTEGER: rv = pst_integer_RV(obj->data); break; |
| 240 |
case PST_TYPE_REAL: rv = pst_real_RV(obj->data); break; |
| 241 |
case PST_TYPE_NAME: rv = pst_name_RV(); break; |
| 242 |
case PST_TYPE_STRING: rv = pst_string_RV(obj->data); break; |
| 243 |
case PST_TYPE_NULL: |
| 244 |
case PST_TYPE_MARK: |
| 245 |
TYPE_ERROR(); |
| 246 |
break; |
| 247 |
case PST_TYPE_UNKNOWN: |
| 248 |
ERROR("Cannot convert object of type UNKNOWN to real value."); |
| 249 |
break; |
| 250 |
default: |
| 251 |
ERROR("Unrecognized object type: %d", obj->type); |
| 252 |
} |
| 253 |
|
| 254 |
return rv; |
| 255 |
} |
| 256 |
|
| 257 |
/* Length can be obtained by pst_length_of(). */ |
| 258 |
unsigned char * |
| 259 |
pst_getSV (pst_obj *obj) |
| 260 |
{ |
| 261 |
unsigned char *sv = NULL; |
| 262 |
|
| 263 |
ASSERT(obj); |
| 264 |
switch (obj->type) { |
| 265 |
case PST_TYPE_BOOLEAN: sv = pst_boolean_SV(obj->data); break; |
| 266 |
case PST_TYPE_INTEGER: sv = pst_integer_SV(obj->data); break; |
| 267 |
case PST_TYPE_REAL: sv = pst_real_SV(obj->data); break; |
| 268 |
case PST_TYPE_NAME: sv = pst_name_SV(obj->data); break; |
| 269 |
case PST_TYPE_STRING: sv = pst_string_SV(obj->data); break; |
| 270 |
case PST_TYPE_NULL: |
| 271 |
case PST_TYPE_MARK: |
| 272 |
TYPE_ERROR(); |
| 273 |
break; |
| 274 |
case PST_TYPE_UNKNOWN: |
| 275 |
{ |
| 276 |
int len; |
| 277 |
|
| 278 |
len = strlen((char *) obj->data); |
| 279 |
if (len > 0) { |
| 280 |
sv = NEW(len+1, unsigned char); |
| 281 |
memcpy(sv, obj->data, len); |
| 282 |
sv[len] = '\0'; |
| 283 |
} else { |
| 284 |
sv = NULL; |
| 285 |
} |
| 286 |
break; |
| 287 |
} |
| 288 |
default: |
| 289 |
ERROR("Unrecognized object type: %d", obj->type); |
| 290 |
} |
| 291 |
|
| 292 |
return sv; |
| 293 |
} |
| 294 |
|
| 295 |
void * |
| 296 |
pst_data_ptr (pst_obj *obj) |
| 297 |
{ |
| 298 |
char *p = NULL; |
| 299 |
|
| 300 |
ASSERT(obj); |
| 301 |
switch (obj->type) { |
| 302 |
case PST_TYPE_BOOLEAN: p = pst_boolean_data_ptr(obj->data); break; |
| 303 |
case PST_TYPE_INTEGER: p = pst_integer_data_ptr(obj->data); break; |
| 304 |
case PST_TYPE_REAL: p = pst_real_data_ptr(obj->data); break; |
| 305 |
case PST_TYPE_NAME: p = pst_name_data_ptr(obj->data); break; |
| 306 |
case PST_TYPE_STRING: p = pst_string_data_ptr(obj->data); break; |
| 307 |
case PST_TYPE_NULL: |
| 308 |
case PST_TYPE_MARK: |
| 309 |
TYPE_ERROR(); |
| 310 |
break; |
| 311 |
case PST_TYPE_UNKNOWN: |
| 312 |
p = obj->data; |
| 313 |
break; |
| 314 |
default: |
| 315 |
ERROR("Unrecognized object type: %d", obj->type); |
| 316 |
} |
| 317 |
|
| 318 |
return (void *)p; |
| 319 |
} |
| 320 |
|
| 321 |
/* BOOLEAN */ |
| 322 |
static pst_boolean * |
| 323 |
pst_boolean_new (char value) |
| 324 |
{ |
| 325 |
pst_boolean *obj; |
| 326 |
obj = NEW(1, pst_boolean); |
| 327 |
obj->value = value; |
| 328 |
return obj; |
| 329 |
} |
| 330 |
|
| 331 |
static void |
| 332 |
pst_boolean_release (pst_boolean *obj) |
| 333 |
{ |
| 334 |
ASSERT(obj); |
| 335 |
RELEASE(obj); |
| 336 |
} |
| 337 |
|
| 338 |
static int |
| 339 |
pst_boolean_IV (pst_boolean *obj) |
| 340 |
{ |
| 341 |
ASSERT(obj); |
| 342 |
return (int) obj->value; |
| 343 |
} |
| 344 |
|
| 345 |
static double |
| 346 |
pst_boolean_RV (pst_boolean *obj) |
| 347 |
{ |
| 348 |
ASSERT(obj); |
| 349 |
return (double) obj->value; |
| 350 |
} |
| 351 |
|
| 352 |
static unsigned char * |
| 353 |
pst_boolean_SV (pst_boolean *obj) |
| 354 |
{ |
| 355 |
unsigned char *str; |
| 356 |
|
| 357 |
ASSERT(obj); |
| 358 |
|
| 359 |
if (obj->value) { |
| 360 |
str = NEW(5, unsigned char); |
| 361 |
memcpy(str, "true", 4); |
| 362 |
str[4] = '\0'; |
| 363 |
} else { |
| 364 |
str = NEW(6, unsigned char); |
| 365 |
memcpy(str, "false", 5); |
| 366 |
str[5] = '\0'; |
| 367 |
} |
| 368 |
|
| 369 |
return str; |
| 370 |
} |
| 371 |
|
| 372 |
static unsigned int |
| 373 |
pst_boolean_length (void) |
| 374 |
{ |
| 375 |
TYPE_ERROR(); |
| 376 |
return 0; |
| 377 |
} |
| 378 |
|
| 379 |
static void * |
| 380 |
pst_boolean_data_ptr (pst_boolean *obj) |
| 381 |
{ |
| 382 |
ASSERT(obj); |
| 383 |
return (void*) &(obj->value); |
| 384 |
} |
| 385 |
|
| 386 |
pst_obj * |
| 387 |
pst_parse_boolean (unsigned char **inbuf, unsigned char *inbufend) |
| 388 |
{ |
| 389 |
if (*inbuf + 4 <= inbufend && |
| 390 |
memcmp(*inbuf, "true", 4) == 0 && |
| 391 |
PST_TOKEN_END(*inbuf + 4, inbufend)) { |
| 392 |
*inbuf += 4; |
| 393 |
return pst_new_obj(PST_TYPE_BOOLEAN, pst_boolean_new(1)); |
| 394 |
} else if (*inbuf + 5 <= inbufend && |
| 395 |
memcmp(*inbuf, "false", 5) == 0 && |
| 396 |
PST_TOKEN_END(*inbuf + 5, inbufend)) { |
| 397 |
*inbuf += 5; |
| 398 |
return pst_new_obj(PST_TYPE_BOOLEAN, pst_boolean_new(0)); |
| 399 |
} else |
| 400 |
return NULL; |
| 401 |
} |
| 402 |
|
| 403 |
|
| 404 |
/* NULL */ |
| 405 |
pst_obj * |
| 406 |
pst_parse_null (unsigned char **inbuf, unsigned char *inbufend) |
| 407 |
{ |
| 408 |
if (*inbuf + 4 <= inbufend && |
| 409 |
memcmp(*inbuf, "null", 4) == 0 && |
| 410 |
PST_TOKEN_END(*inbuf+4, inbufend)) { |
| 411 |
char *q; |
| 412 |
|
| 413 |
*inbuf += 4; |
| 414 |
q = NEW(strlen(pst_const_null)+1, char); |
| 415 |
strcpy(q, pst_const_null); |
| 416 |
return pst_new_obj(PST_TYPE_NULL, (void*)q); |
| 417 |
} else |
| 418 |
return NULL; |
| 419 |
} |
| 420 |
|
| 421 |
/* INTEGER */ |
| 422 |
static pst_integer * |
| 423 |
pst_integer_new (int value) |
| 424 |
{ |
| 425 |
pst_integer *obj; |
| 426 |
obj = NEW(1, pst_integer); |
| 427 |
obj->value = value; |
| 428 |
return obj; |
| 429 |
} |
| 430 |
|
| 431 |
static void |
| 432 |
pst_integer_release (pst_integer *obj) |
| 433 |
{ |
| 434 |
ASSERT(obj); |
| 435 |
RELEASE(obj); |
| 436 |
} |
| 437 |
|
| 438 |
static int |
| 439 |
pst_integer_IV (pst_integer *obj) |
| 440 |
{ |
| 441 |
ASSERT(obj); |
| 442 |
return (int) obj->value; |
| 443 |
} |
| 444 |
|
| 445 |
static double |
| 446 |
pst_integer_RV (pst_integer *obj) |
| 447 |
{ |
| 448 |
ASSERT(obj); |
| 449 |
return (double) obj->value; |
| 450 |
} |
| 451 |
|
| 452 |
static unsigned char * |
| 453 |
pst_integer_SV (pst_integer *obj) |
| 454 |
{ |
| 455 |
char *value; |
| 456 |
int len; |
| 457 |
char fmt_buf[PST_MAX_DIGITS+5]; |
| 458 |
|
| 459 |
ASSERT(obj); |
| 460 |
|
| 461 |
len = sprintf(fmt_buf, "%d", obj->value); |
| 462 |
|
| 463 |
value = NEW(len + 1, char); |
| 464 |
strcpy(value, fmt_buf); |
| 465 |
|
| 466 |
return (unsigned char *) value; |
| 467 |
} |
| 468 |
|
| 469 |
static void * |
| 470 |
pst_integer_data_ptr (pst_integer *obj) |
| 471 |
{ |
| 472 |
ASSERT(obj); |
| 473 |
return (void*) &(obj->value); |
| 474 |
} |
| 475 |
|
| 476 |
static unsigned int |
| 477 |
pst_integer_length (void) |
| 478 |
{ |
| 479 |
TYPE_ERROR(); |
| 480 |
return 0; |
| 481 |
} |
| 482 |
|
| 483 |
/* REAL */ |
| 484 |
static pst_real * |
| 485 |
pst_real_new (double value) |
| 486 |
{ |
| 487 |
pst_real *obj; |
| 488 |
|
| 489 |
obj = NEW(1, pst_real); |
| 490 |
obj->value = value; |
| 491 |
|
| 492 |
return obj; |
| 493 |
} |
| 494 |
|
| 495 |
static void |
| 496 |
pst_real_release (pst_real *obj) |
| 497 |
{ |
| 498 |
ASSERT(obj); |
| 499 |
RELEASE(obj); |
| 500 |
} |
| 501 |
|
| 502 |
static int |
| 503 |
pst_real_IV (pst_real *obj) |
| 504 |
{ |
| 505 |
ASSERT(obj); |
| 506 |
return (int) obj->value; |
| 507 |
} |
| 508 |
|
| 509 |
static double |
| 510 |
pst_real_RV (pst_real *obj) |
| 511 |
{ |
| 512 |
ASSERT(obj); |
| 513 |
return (double) obj->value; |
| 514 |
} |
| 515 |
|
| 516 |
static unsigned char * |
| 517 |
pst_real_SV (pst_real *obj) |
| 518 |
{ |
| 519 |
char *value; |
| 520 |
int len; |
| 521 |
char fmt_buf[PST_MAX_DIGITS+5]; |
| 522 |
|
| 523 |
ASSERT(obj); |
| 524 |
|
| 525 |
len = sprintf(fmt_buf, "%.5g", obj->value); |
| 526 |
|
| 527 |
value = NEW(len, char); |
| 528 |
strcpy(value, fmt_buf); |
| 529 |
|
| 530 |
return (unsigned char *) value; |
| 531 |
} |
| 532 |
|
| 533 |
static void * |
| 534 |
pst_real_data_ptr (pst_real *obj) |
| 535 |
{ |
| 536 |
ASSERT(obj); |
| 537 |
|
| 538 |
return (void*) &(obj->value); |
| 539 |
} |
| 540 |
|
| 541 |
static unsigned int |
| 542 |
pst_real_length (void) |
| 543 |
{ |
| 544 |
TYPE_ERROR(); |
| 545 |
return 0; |
| 546 |
} |
| 547 |
|
| 548 |
/* NOTE: the input buffer must be null-terminated, i.e., *inbufend == 0 */ |
| 549 |
/* leading white-space is ignored */ |
| 550 |
pst_obj * |
| 551 |
pst_parse_number (unsigned char **inbuf, unsigned char *inbufend) |
| 552 |
{ |
| 553 |
unsigned char *cur; |
| 554 |
int lval; |
| 555 |
double dval; |
| 556 |
|
| 557 |
errno = 0; |
| 558 |
lval = strtol((char *) *inbuf, (char **) (void *) &cur, 10); |
| 559 |
if (errno || *cur == '.' || *cur == 'e' || *cur == 'E') { |
| 560 |
/* real */ |
| 561 |
errno = 0; |
| 562 |
dval = strtod((char *) *inbuf, (char **) (void *) &cur); |
| 563 |
if (!errno && PST_TOKEN_END(cur, inbufend)) { |
| 564 |
*inbuf = cur; |
| 565 |
return pst_new_obj(PST_TYPE_REAL, pst_real_new(dval)); |
| 566 |
} |
| 567 |
} else if (cur != *inbuf && PST_TOKEN_END(cur, inbufend)) { |
| 568 |
/* integer */ |
| 569 |
*inbuf = cur; |
| 570 |
return pst_new_obj(PST_TYPE_INTEGER, pst_integer_new(lval)); |
| 571 |
} else if (lval >= 2 && lval <= 36 && *cur == '#' && isalnum(*++cur) && |
| 572 |
/* strtod allows leading "0x" for hex numbers, but we don't */ |
| 573 |
(lval != 16 || (cur[1] != 'x' && cur[1] != 'X'))) { |
| 574 |
/* integer with radix */ |
| 575 |
/* Can the base have a (plus) sign? I think yes. */ |
| 576 |
errno = 0; |
| 577 |
lval = strtol((char *) cur, (char **) (void *) &cur, lval); |
| 578 |
if (!errno && PST_TOKEN_END(cur, inbufend)) { |
| 579 |
*inbuf = cur; |
| 580 |
return pst_new_obj(PST_TYPE_INTEGER, pst_integer_new(lval)); |
| 581 |
} |
| 582 |
} |
| 583 |
/* error */ |
| 584 |
return NULL; |
| 585 |
} |
| 586 |
|
| 587 |
/* NAME */ |
| 588 |
|
| 589 |
/* |
| 590 |
* \0 is not allowed for name object. |
| 591 |
*/ |
| 592 |
|
| 593 |
static pst_name * |
| 594 |
pst_name_new (const char *name) |
| 595 |
{ |
| 596 |
pst_name *obj; |
| 597 |
|
| 598 |
obj = NEW(1, pst_name); |
| 599 |
obj->value = NEW(strlen(name)+1, char); |
| 600 |
strcpy(obj->value, name); |
| 601 |
|
| 602 |
return obj; |
| 603 |
} |
| 604 |
|
| 605 |
static void |
| 606 |
pst_name_release (pst_name *obj) |
| 607 |
{ |
| 608 |
ASSERT(obj); |
| 609 |
if (obj->value) |
| 610 |
RELEASE(obj->value); |
| 611 |
RELEASE(obj); |
| 612 |
} |
| 613 |
|
| 614 |
#if 0 |
| 615 |
static int |
| 616 |
pst_name_is_valid (const char *name) |
| 617 |
{ |
| 618 |
static const char *valid_chars = |
| 619 |
"!\"#$&'*+,-.0123456789:;=?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\\^_`abcdefghijklmnopqrstuvwxyz|~"; |
| 620 |
if (strspn(name, valid_chars) == strlen(name)) |
| 621 |
return 1; |
| 622 |
else |
| 623 |
return 0; |
| 624 |
} |
| 625 |
|
| 626 |
static int |
| 627 |
putxpair (unsigned char c, char **s) |
| 628 |
{ |
| 629 |
char hi = (c >> 4), lo = c & 0x0f; |
| 630 |
|
| 631 |
**s = (hi < 10) ? hi + '0' : hi + '7'; |
| 632 |
*(*s+1) = (lo < 10) ? lo + '0' : lo + '7'; |
| 633 |
*s += 2; |
| 634 |
|
| 635 |
return 2; |
| 636 |
} |
| 637 |
|
| 638 |
static char * |
| 639 |
pst_name_encode (const char *name) |
| 640 |
{ |
| 641 |
char *encoded_name, *p; |
| 642 |
int i, len; |
| 643 |
char c; |
| 644 |
|
| 645 |
len = strlen(name); |
| 646 |
if (len > PST_NAME_LEN_MAX) { |
| 647 |
WARN("Input string too long for name object. String will be truncated."); |
| 648 |
len = PST_NAME_LEN_MAX; |
| 649 |
} |
| 650 |
|
| 651 |
p = encoded_name = NEW(3*len+1, char); |
| 652 |
for (i = 0; i < len; i++) { |
| 653 |
c = name[i]; |
| 654 |
if (c < '!' || c > '~' || |
| 655 |
c == '#' || is_delim(c) || is_space(c)) { |
| 656 |
*p++ = '#'; |
| 657 |
putxpair(c, &p); |
| 658 |
} else { |
| 659 |
*p++ = c; |
| 660 |
} |
| 661 |
} |
| 662 |
*p = '\0'; |
| 663 |
|
| 664 |
return encoded_name; |
| 665 |
} |
| 666 |
#endif |
| 667 |
|
| 668 |
static int |
| 669 |
getxpair (unsigned char **s) |
| 670 |
{ |
| 671 |
int hi, lo; |
| 672 |
hi = xtoi(**s); |
| 673 |
if (hi < 0) |
| 674 |
return hi; |
| 675 |
(*s)++; |
| 676 |
lo = xtoi(**s); |
| 677 |
if (lo < 0) |
| 678 |
return lo; |
| 679 |
(*s)++; |
| 680 |
return ((hi << 4)| lo); |
| 681 |
} |
| 682 |
|
| 683 |
pst_obj * |
| 684 |
pst_parse_name (unsigned char **inbuf, unsigned char *inbufend) /* / is required */ |
| 685 |
{ |
| 686 |
unsigned char wbuf[PST_NAME_LEN_MAX+1]; |
| 687 |
unsigned char c, *p = wbuf, *cur = *inbuf; |
| 688 |
int len = 0; |
| 689 |
|
| 690 |
if (*cur != '/') |
| 691 |
return NULL; |
| 692 |
cur++; |
| 693 |
|
| 694 |
while (!PST_TOKEN_END(cur, inbufend)) { |
| 695 |
c = *cur++; |
| 696 |
if (c == '#') { |
| 697 |
int val; |
| 698 |
if (cur + 2 >= inbufend) { |
| 699 |
WARN("Premature end of input name string."); |
| 700 |
break; |
| 701 |
} |
| 702 |
val = getxpair(&cur); |
| 703 |
if (val <= 0) { |
| 704 |
WARN("Invalid char for name object. (ignored)"); |
| 705 |
continue; |
| 706 |
} else |
| 707 |
c = (unsigned char) val; |
| 708 |
} |
| 709 |
if (len < PST_NAME_LEN_MAX) |
| 710 |
*p++ = c; |
| 711 |
len++; |
| 712 |
} |
| 713 |
*p = '\0'; |
| 714 |
|
| 715 |
if (len > PST_NAME_LEN_MAX) |
| 716 |
WARN("String too long for name object. Output will be truncated."); |
| 717 |
|
| 718 |
*inbuf = cur; |
| 719 |
return pst_new_obj(PST_TYPE_NAME, pst_name_new((char *)wbuf)); |
| 720 |
} |
| 721 |
|
| 722 |
static int |
| 723 |
pst_name_IV (void) |
| 724 |
{ |
| 725 |
TYPE_ERROR(); |
| 726 |
return 0; |
| 727 |
} |
| 728 |
|
| 729 |
static double |
| 730 |
pst_name_RV (void) |
| 731 |
{ |
| 732 |
TYPE_ERROR(); |
| 733 |
return 0; |
| 734 |
} |
| 735 |
|
| 736 |
static unsigned char * |
| 737 |
pst_name_SV (pst_name *obj) |
| 738 |
{ |
| 739 |
char *value; |
| 740 |
|
| 741 |
value = NEW(strlen(obj->value)+1, char); |
| 742 |
strcpy(value, obj->value); |
| 743 |
|
| 744 |
return (unsigned char *) value; |
| 745 |
} |
| 746 |
|
| 747 |
static void * |
| 748 |
pst_name_data_ptr (pst_name *obj) |
| 749 |
{ |
| 750 |
ASSERT(obj); |
| 751 |
return obj->value; |
| 752 |
} |
| 753 |
|
| 754 |
static unsigned int |
| 755 |
pst_name_length (pst_name *obj) |
| 756 |
{ |
| 757 |
ASSERT(obj); |
| 758 |
return strlen(obj->value); |
| 759 |
} |
| 760 |
|
| 761 |
|
| 762 |
/* STRING */ |
| 763 |
|
| 764 |
/* |
| 765 |
* TODO: ascii85 string <~ .... ~> |
| 766 |
*/ |
| 767 |
static pst_string * |
| 768 |
pst_string_new (unsigned char *str, unsigned int len) |
| 769 |
{ |
| 770 |
pst_string *obj; |
| 771 |
obj = NEW(1, pst_string); |
| 772 |
obj->length = len; |
| 773 |
obj->value = NULL; |
| 774 |
if (len > 0) { |
| 775 |
obj->value = NEW(len, unsigned char); |
| 776 |
if (str) |
| 777 |
memcpy(obj->value, str, len); |
| 778 |
} |
| 779 |
return obj; |
| 780 |
} |
| 781 |
|
| 782 |
static void |
| 783 |
pst_string_release (pst_string *obj) |
| 784 |
{ |
| 785 |
ASSERT(obj); |
| 786 |
if (obj->value) |
| 787 |
RELEASE(obj->value); |
| 788 |
RELEASE(obj); |
| 789 |
} |
| 790 |
|
| 791 |
pst_obj * |
| 792 |
pst_parse_string (unsigned char **inbuf, unsigned char *inbufend) |
| 793 |
{ |
| 794 |
if (*inbuf + 2 >= inbufend) { |
| 795 |
return NULL; |
| 796 |
} else if (**inbuf == '(') |
| 797 |
return pst_new_obj(PST_TYPE_STRING, pst_string_parse_literal(inbuf, inbufend)); |
| 798 |
else if (**inbuf == '<' && *(*inbuf+1) == '~') |
| 799 |
ERROR("ASCII85 string not supported yet."); |
| 800 |
else if (**inbuf == '<') |
| 801 |
return pst_new_obj(PST_TYPE_STRING, pst_string_parse_hex(inbuf, inbufend)); |
| 802 |
return NULL; |
| 803 |
} |
| 804 |
|
| 805 |
/* Overflowed value is set to invalid char. */ |
| 806 |
static unsigned char |
| 807 |
ostrtouc (unsigned char **inbuf, unsigned char *inbufend, unsigned char *valid) |
| 808 |
{ |
| 809 |
unsigned char *cur = *inbuf; |
| 810 |
unsigned int val = 0; |
| 811 |
|
| 812 |
while (cur < inbufend && cur < *inbuf + 3 && |
| 813 |
(*cur >= '0' && *cur <= '7')) { |
| 814 |
val = (val << 3) | (*cur - '0'); |
| 815 |
cur++; |
| 816 |
} |
| 817 |
if (val > 255 || cur == *inbuf) |
| 818 |
*valid = 0; |
| 819 |
else |
| 820 |
*valid = 1; |
| 821 |
|
| 822 |
*inbuf = cur; |
| 823 |
return (unsigned char) val; |
| 824 |
} |
| 825 |
|
| 826 |
static unsigned char |
| 827 |
esctouc (unsigned char **inbuf, unsigned char *inbufend, unsigned char *valid) |
| 828 |
{ |
| 829 |
unsigned char unescaped, escaped; |
| 830 |
|
| 831 |
escaped = **inbuf; |
| 832 |
*valid = 1; |
| 833 |
switch (escaped) { |
| 834 |
/* Backslash, unbalanced paranthes */ |
| 835 |
case '\\': case ')': case '(': |
| 836 |
unescaped = escaped; |
| 837 |
(*inbuf)++; |
| 838 |
break; |
| 839 |
/* Other escaped char */ |
| 840 |
case 'n': unescaped = '\n'; (*inbuf)++; break; |
| 841 |
case 'r': unescaped = '\r'; (*inbuf)++; break; |
| 842 |
case 't': unescaped = '\t'; (*inbuf)++; break; |
| 843 |
case 'b': unescaped = '\b'; (*inbuf)++; break; |
| 844 |
case 'f': unescaped = '\f'; (*inbuf)++; break; |
| 845 |
/* |
| 846 |
* An end-of-line marker preceeded by backslash is not part of a |
| 847 |
* literal string |
| 848 |
*/ |
| 849 |
case '\r': |
| 850 |
unescaped = 0; |
| 851 |
*valid = 0; |
| 852 |
*inbuf += (*inbuf < inbufend - 1 && *(*inbuf+1) == '\n') ? 2 : 1; |
| 853 |
break; |
| 854 |
case '\n': |
| 855 |
unescaped = 0; |
| 856 |
*valid = 0; |
| 857 |
(*inbuf)++; |
| 858 |
break; |
| 859 |
/* Possibly octal notion */ |
| 860 |
default: |
| 861 |
unescaped = ostrtouc(inbuf, inbufend, valid); |
| 862 |
} |
| 863 |
|
| 864 |
return unescaped; |
| 865 |
} |
| 866 |
|
| 867 |
static pst_string * |
| 868 |
pst_string_parse_literal (unsigned char **inbuf, unsigned char *inbufend) |
| 869 |
{ |
| 870 |
unsigned char wbuf[PST_STRING_LEN_MAX]; |
| 871 |
unsigned char *cur = *inbuf, c = 0; |
| 872 |
int len = 0, balance = 1; |
| 873 |
|
| 874 |
if (cur + 2 > inbufend || *cur != '(') |
| 875 |
return NULL; |
| 876 |
|
| 877 |
cur++; |
| 878 |
while (cur < inbufend && len < PST_STRING_LEN_MAX && balance > 0) { |
| 879 |
c = *(cur++); |
| 880 |
switch (c) { |
| 881 |
case '\\': |
| 882 |
{ |
| 883 |
unsigned char unescaped, valid; |
| 884 |
unescaped = esctouc(&cur, inbufend, &valid); |
| 885 |
if (valid) |
| 886 |
wbuf[len++] = unescaped; |
| 887 |
} |
| 888 |
break; |
| 889 |
case '(': |
| 890 |
balance++; |
| 891 |
wbuf[len++] = '('; |
| 892 |
break; |
| 893 |
case ')': |
| 894 |
balance--; |
| 895 |
if (balance > 0) |
| 896 |
wbuf[len++] = ')'; |
| 897 |
break; |
| 898 |
/* |
| 899 |
* An end-of-line marker (\n, \r or \r\n), not preceeded by a backslash, |
| 900 |
* must be converted to single \n. |
| 901 |
*/ |
| 902 |
case '\r': |
| 903 |
if (cur < inbufend && *cur == '\n') |
| 904 |
cur++; |
| 905 |
wbuf[len++] = '\n'; |
| 906 |
break; |
| 907 |
default: |
| 908 |
wbuf[len++] = c; |
| 909 |
} |
| 910 |
} |
| 911 |
if (c != ')') |
| 912 |
return NULL; |
| 913 |
|
| 914 |
*inbuf = cur; |
| 915 |
return pst_string_new(wbuf, len); |
| 916 |
} |
| 917 |
|
| 918 |
static pst_string * |
| 919 |
pst_string_parse_hex (unsigned char **inbuf, unsigned char *inbufend) |
| 920 |
{ |
| 921 |
unsigned char wbuf[PST_STRING_LEN_MAX]; |
| 922 |
unsigned char *cur = *inbuf; |
| 923 |
unsigned int len = 0; |
| 924 |
|
| 925 |
if (cur + 2 > inbufend || *cur != '<' || |
| 926 |
(*cur == '<' && *(cur+1) == '<')) |
| 927 |
return NULL; |
| 928 |
|
| 929 |
cur++; |
| 930 |
/* PDF Reference does not specify how to treat invalid char */ |
| 931 |
while (cur < inbufend && len < PST_STRING_LEN_MAX) { |
| 932 |
int hi, lo; |
| 933 |
skip_white_spaces(&cur, inbufend); |
| 934 |
if (*cur == '>') |
| 935 |
break; |
| 936 |
hi = xtoi(*(cur++)); |
| 937 |
if (hi < 0) { |
| 938 |
WARN("Invalid char for hex string <%x> treated as <0>.", *(cur-1)); |
| 939 |
hi = 0; |
| 940 |
} |
| 941 |
skip_white_spaces(&cur, inbufend); |
| 942 |
if (*cur == '>') |
| 943 |
break; |
| 944 |
/* 0 is appended if final hex digit is missing */ |
| 945 |
lo = (cur < inbufend) ? xtoi(*(cur++)) : 0; |
| 946 |
if (lo < 0) { |
| 947 |
WARN("Invalid char for hex string <%x> treated as <0>.", *(cur-1)); |
| 948 |
lo = 0; |
| 949 |
} |
| 950 |
wbuf[len++] = (hi << 4) | lo; |
| 951 |
} |
| 952 |
if (*cur++ != '>') |
| 953 |
return NULL; |
| 954 |
|
| 955 |
*inbuf = cur; |
| 956 |
return pst_string_new(wbuf, len); |
| 957 |
} |
| 958 |
|
| 959 |
static int |
| 960 |
pst_string_IV (pst_string *obj) |
| 961 |
{ |
| 962 |
return (int) pst_string_RV(obj); |
| 963 |
} |
| 964 |
|
| 965 |
static double |
| 966 |
pst_string_RV (pst_string *obj) |
| 967 |
{ |
| 968 |
pst_obj *nobj; |
| 969 |
unsigned char *p, *end; |
| 970 |
double rv; |
| 971 |
|
| 972 |
ASSERT(obj); |
| 973 |
p = obj->value; |
| 974 |
end = p + obj->length; |
| 975 |
nobj = pst_parse_number(&p, end); |
| 976 |
if (nobj == NULL || p != end) |
| 977 |
ERROR("Cound not convert string to real value."); |
| 978 |
rv = pst_getRV(nobj); |
| 979 |
pst_release_obj(nobj); |
| 980 |
|
| 981 |
return rv; |
| 982 |
} |
| 983 |
|
| 984 |
static unsigned char * |
| 985 |
pst_string_SV (pst_string *obj) |
| 986 |
{ |
| 987 |
unsigned char *str = NULL; |
| 988 |
ASSERT(obj); |
| 989 |
str = NEW(obj->length + 1, unsigned char); |
| 990 |
memcpy(str, obj->value, obj->length); |
| 991 |
str[obj->length] = '\0'; |
| 992 |
return str; |
| 993 |
} |
| 994 |
|
| 995 |
static void * |
| 996 |
pst_string_data_ptr (pst_string *obj) |
| 997 |
{ |
| 998 |
ASSERT(obj); |
| 999 |
return obj->value; |
| 1000 |
} |
| 1001 |
|
| 1002 |
static unsigned int |
| 1003 |
pst_string_length (pst_string *obj) |
| 1004 |
{ |
| 1005 |
ASSERT(obj); |
| 1006 |
return obj->length; |
| 1007 |
} |