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 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
|
#if !defined(lint) && !defined(DOS)
static char rcsid[] = "$Id: store.c 1074 2008-06-04 00:08:43Z hubert@u.washington.edu $";
#endif
/*
* ========================================================================
* Copyright 2006-2008 University of Washington
* Copyright 2013 Eduardo Chappa
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* ========================================================================
*/
/*
* GENERALIZED STORAGE FUNCTIONS. Idea is to allow creation of
* storage objects that can be written into and read from without
* the caller knowing if the storage is core or in a file
* or whatever.
*/
#include "../pith/headers.h"
#include "../pith/store.h"
#include "../pith/status.h"
#include "../pith/state.h"
#include "../pico/keydefs.h"
#ifdef SMIME
#include <openssl/buffer.h>
#endif /* SMIME */
/*
* Internal prototypes
*/
void *so_file_open(STORE_S *);
int so_cs_writec(int, STORE_S *);
int so_cs_writec_locale(int, STORE_S *);
int so_file_writec(int, STORE_S *);
int so_file_writec_locale(int, STORE_S *);
int so_cs_readc(unsigned char *, STORE_S *);
int so_cs_readc_locale(unsigned char *, STORE_S *);
int so_cs_readc_getchar(unsigned char *c, void *extraarg);
int so_file_readc(unsigned char *, STORE_S *);
int so_file_readc_locale(unsigned char *, STORE_S *);
int so_file_readc_getchar(unsigned char *c, void *extraarg);
int so_cs_puts(STORE_S *, char *);
int so_cs_puts_locale(STORE_S *, char *);
int so_file_puts(STORE_S *, char *);
int so_file_puts_locale(STORE_S *, char *);
int so_reaquire(STORE_S *);
#ifdef _WINDOWS
int so_file_readc_windows(unsigned char *, STORE_S *);
#endif /* _WINDOWS */
#ifdef SMIME
int so_bio_writec(int, STORE_S *);
int so_bio_readc(unsigned char *, STORE_S *);
int so_bio_puts(STORE_S *, char *);
#endif /* SMIME */
/*
* place holders for externally defined storage object driver
*/
static struct externalstoreobjectdata {
STORE_S *(*get)(void);
int (*give)(STORE_S **);
int (*writec)(int, STORE_S *);
int (*readc)(unsigned char *, STORE_S *);
int (*puts)(STORE_S *, char *);
int (*seek)(STORE_S *, long, int);
int (*truncate)(STORE_S *, off_t);
int (*tell)(STORE_S *);
} etsod;
#define MSIZE_INIT 8192
#define MSIZE_INC 4096
#ifdef S_IREAD
#define OP_MD_USER (S_IREAD | S_IWRITE)
#else
#define OP_MD_USER 0600
#endif
#ifdef S_IRUSR
#define OP_MD_ALL (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | \
S_IROTH | S_IWOTH)
#else
#define OP_MD_ALL 0666
#endif
/*
* allocate resources associated with the specified type of
* storage. If requesting a named file object, open it for
* appending, else just open a temp file.
*
* return the filled in storage object
*/
STORE_S *
so_get(SourceType source, char *name, int rtype)
/* requested storage type */
/* file name */
/* file access type */
{
STORE_S *so = (STORE_S *)fs_get(sizeof(STORE_S));
memset(so, 0, sizeof(STORE_S));
so->flags |= rtype;
so->cb.cbuf[0] = '\0';
so->cb.cbufp = so->cb.cbuf;
so->cb.cbufend = so->cb.cbuf;
if(name) /* stash the name */
so->name = cpystr(name);
#ifdef DOS
else if(source == TmpFileStar || source == FileStar){
/*
* Coerce to TmpFileStar. The MSC library's "tmpfile()"
* doesn't observe the "TMP" or "TEMP" environment vars and
* always wants to write "\". This is problematic in shared,
* networked environments.
*/
source = TmpFileStar;
so->name = temp_nam(NULL, "pi");
}
#else
else if(source == TmpFileStar) /* make one up! */
so->name = temp_nam(NULL, "pine-tmp");
#endif
so->src = source;
if(so->src == FileStar || so->src == TmpFileStar){
#ifdef _WINDOWS
so->writec = so_file_writec;
so->readc = (rtype & READ_FROM_LOCALE) ? so_file_readc_windows
: so_file_readc;
#else /* UNIX */
so->writec = (rtype & WRITE_TO_LOCALE) ? so_file_writec_locale
: so_file_writec;
so->readc = (rtype & READ_FROM_LOCALE) ? so_file_readc_locale
: so_file_readc;
#endif /* UNIX */
so->puts = (rtype & WRITE_TO_LOCALE) ? so_file_puts_locale
: so_file_puts;
/*
* The reason for both FileStar and TmpFileStar types is
* that, named or unnamed, TmpFileStar's are unlinked
* when the object is given back to the system. This is
* useful for keeping us from running out of file pointers as
* the pointer associated with the object can be temporarily
* returned to the system without destroying the object.
*
* The programmer is warned to be careful not to assign the
* TmpFileStar type to any files that are expected to remain
* after the dust has settled!
*/
if(so->name){
if(!(so->txt = so_file_open(so))){
dprint((1, "so_get error: %s : %s",
so->name ? so->name : "?",
error_description(errno)));
if(source == TmpFileStar)
our_unlink(so->name);
fs_give((void **)&so->name);
fs_give((void **)&so); /* so freed & set to NULL */
}
}
else{
if(!(so->txt = (void *) create_tmpfile())){
dprint((1, "so_get error: tmpfile : %s",
error_description(errno)));
fs_give((void **)&so); /* so freed & set to NULL */
}
}
}
else if(so->src == ExternalText && etsod.get){
so->writec = etsod.writec;
so->readc = etsod.readc;
so->puts = etsod.puts;
if(!(so->txt = (*etsod.get)())){
dprint((1, "so_get error: external driver allocation error"));
if(so->name)
fs_give((void **)&so->name);
fs_give((void **)&so); /* so freed & set to NULL */
}
}
#ifdef SMIME
else if(so->src == BioType){
so->writec = so_bio_writec;
so->readc = so_bio_readc;
so->puts = so_bio_puts;
if(!(so->txt = BIO_new(BIO_s_mem()))){
dprint((1, "so_get error: BIO driver allocation error"));
if(so->name)
fs_give((void **) &so->name);
fs_give((void **) &so); /* so freed & set to NULL */
}
}
#endif /* SMIME */
else{
so->writec = (rtype & WRITE_TO_LOCALE) ? so_cs_writec_locale
: so_cs_writec;
so->readc = (rtype & READ_FROM_LOCALE) ? so_cs_readc_locale
: so_cs_readc;
so->puts = (rtype & WRITE_TO_LOCALE) ? so_cs_puts_locale
: so_cs_puts;
so->txt = (void *)fs_get((size_t) MSIZE_INIT * sizeof(char));
so->dp = so->eod = (unsigned char *) so->txt;
so->eot = so->dp + MSIZE_INIT;
memset(so->eod, 0, so->eot - so->eod);
}
return(so);
}
/*
* so_give - free resources associated with a storage object and then
* the object itself.
*/
int
so_give(STORE_S **so)
{
int ret = 0;
if(!(so && (*so)))
return(ret);
if((*so)->src == FileStar || (*so)->src == TmpFileStar){
if((*so)->txt) /* disassociate from storage */
ret = fclose((FILE *)(*so)->txt) == EOF ? -1 : 0;
if((*so)->name && (*so)->src == TmpFileStar)
our_unlink((*so)->name); /* really disassociate! */
}
else if((*so)->txt && (*so)->src == ExternalText){
if(etsod.give)
(*etsod.give)((*so)->txt);
}
#ifdef SMIME
else if((*so)->txt && (*so)->src == BioType){
BIO *b = (BIO *) (*so)->txt;
BIO_free(b);
}
#endif /* SMIME */
else if((*so)->txt)
fs_give((void **)&((*so)->txt));
if((*so)->name)
fs_give((void **)&((*so)->name)); /* blast the name */
/* release attribute list */
mail_free_body_parameter(&(*so)->attr);
fs_give((void **)so); /* release the object */
return(ret);
}
/*
* so_register_external_driver - hokey way to get pico-dependent storage object
* support out of pith library
*/
void
so_register_external_driver(STORE_S *(*get)(void),
int (*give)(STORE_S **),
int (*writec)(int, STORE_S *),
int (*readc)(unsigned char *, STORE_S *),
int (*puts)(STORE_S *, char *),
int (*seek)(STORE_S *, long int, int),
int (*truncate)(STORE_S *, off_t),
int (*tell)(STORE_S *))
{
memset(&etsod, 0, sizeof(etsod));
if(get)
etsod.get = get;
if(give)
etsod.give = give;
if(writec)
etsod.writec = writec;
if(readc)
etsod.readc = readc;
if(puts)
etsod.puts = puts;
if(seek)
etsod.seek = seek;
if(truncate)
etsod.truncate = truncate;
if(tell)
etsod.tell = tell;
}
void *
so_file_open(STORE_S *so)
{
char *type = ((so->flags) & WRITE_ACCESS) ? "a+" : "r";
int flags, fd,
mode = (((so->flags) & OWNER_ONLY) || so->src == TmpFileStar)
? OP_MD_USER : OP_MD_ALL;
/*
* Careful. EDIT_ACCESS and WRITE_TO_LOCALE/READ_FROM_LOCALE will
* not work together.
*/
if(so->flags & WRITE_ACCESS){
flags = O_RDWR | O_CREAT | O_APPEND | O_BINARY;
}
else{
flags = O_RDONLY;
if(so->flags & READ_FROM_LOCALE){
flags |= _O_WTEXT;
}
else{
flags |= O_BINARY;
}
}
/*
* Use open instead of fopen so we can make temp files private.
* I believe the "b" or "t" in the type isn't necessary in the fdopen call
* because we already set O_BINARY or _O_U8TEXT or _O_WTEXT in the open.
*/
return(((fd = our_open(so->name, flags, mode)) > -1)
? (so->txt = (void *) fdopen(fd, type)) : NULL);
}
/*
* put a character into the specified storage object,
* expanding if neccessary
*
* return 1 on success and 0 on failure
*/
int
so_cs_writec(int c, STORE_S *so)
{
if(so->dp >= so->eot){
size_t incr;
size_t cur_o = so->dp - (unsigned char *) so->txt;
size_t data_o = so->eod - (unsigned char *) so->txt;
size_t size = (so->eot - (unsigned char *) so->txt);
/*
* We estimate the size we're going to need at the beginning
* so it shouldn't normally happen that we run out of space and
* come here. If we do come here, it is probably because there
* are lots of handles in the message or lots of quote coloring.
* In either case, the overhead of those is high so we don't want
* to keep adding little bits and resizing. Add 50% each time
* instead.
*/
incr = MAX(size/2, MSIZE_INC);
size += incr;
fs_resize(&so->txt, size * sizeof(char));
so->dp = (unsigned char *) so->txt + cur_o;
so->eod = (unsigned char *) so->txt + data_o;
so->eot = (unsigned char *) so->txt + size;
memset(so->eod, 0, so->eot - so->eod);
}
*so->dp++ = (unsigned char) c;
if(so->dp > so->eod)
so->eod = so->dp;
return(1);
}
/*
* The locale version converts from UTF-8 to user's locale charset
* before writing the characters.
*/
int
so_cs_writec_locale(int c, STORE_S *so)
{
int rv = 1;
int i, outchars;
unsigned char obuf[MAX(MB_LEN_MAX,32)];
if((outchars = utf8_to_locale(c, &so->cb, obuf, sizeof(obuf))) != 0){
for(i = 0; i < outchars; i++)
if(so_cs_writec(obuf[i], so) != 1){
rv = 0;
break;
}
}
return(rv);
}
int
so_file_writec(int c, STORE_S *so)
{
unsigned char ch = (unsigned char) c;
int rv = 0;
if(so->txt || so_reaquire(so))
do
rv = fwrite(&ch,sizeof(unsigned char),(size_t)1,(FILE *)so->txt);
while(!rv && ferror((FILE *)so->txt) && errno == EINTR);
return(rv);
}
/*
* The locale version converts from UTF-8 to user's locale charset
* before writing the characters.
*/
int
so_file_writec_locale(int c, STORE_S *so)
{
int rv = 1;
int i, outchars;
unsigned char obuf[MAX(MB_LEN_MAX,32)];
if((outchars = utf8_to_locale(c, &so->cb, obuf, sizeof(obuf))) != 0){
for(i = 0; i < outchars; i++)
if(so_file_writec(obuf[i], so) != 1){
rv = 0;
break;
}
}
return(rv);
}
/*
* get a character from the specified storage object.
*
* return 1 on success and 0 on failure
*/
int
so_cs_readc(unsigned char *c, STORE_S *so)
{
return((so->dp < so->eod) ? *c = *(so->dp)++, 1 : 0);
}
/*
* The locale version converts from user's locale charset to UTF-8
* after reading the characters and before returning to the caller.
*/
int
so_cs_readc_locale(unsigned char *c, STORE_S *so)
{
return(generic_readc_locale(c, so_cs_readc_getchar, so, &so->cb));
}
int
so_cs_readc_getchar(unsigned char *c, void *extraarg)
{
STORE_S *so;
so = (STORE_S *) extraarg;
return(so_cs_readc(c, so));
}
int
so_file_readc(unsigned char *c, STORE_S *so)
{
int rv = 0;
if(so->txt || so_reaquire(so))
do
rv = fread(c, sizeof(char), (size_t)1, (FILE *)so->txt);
while(!rv && ferror((FILE *)so->txt) && errno == EINTR);
return(rv);
}
/*
* The locale version converts from user's locale charset to UTF-8
* after reading the characters and before returning to the caller.
*/
int
so_file_readc_locale(unsigned char *c, STORE_S *so)
{
return(generic_readc_locale(c, so_file_readc_getchar, so, &so->cb));
}
int
so_file_readc_getchar(unsigned char *c, void *extraarg)
{
STORE_S *so;
so = (STORE_S *) extraarg;
return(so_file_readc(c, so));
}
#ifdef _WINDOWS
/*
* Read unicode characters from windows filesystem and return
* them as a stream of UTF-8 characters. The stream is assumed
* opened so that it will know how to put together the unicode.
*/
int
so_file_readc_windows(unsigned char *c, STORE_S *so)
{
int rv = 0;
UCS ucs;
/* already got some from previous call? */
if(so->cb.cbufend > so->cb.cbuf){
*c = *so->cb.cbufp;
so->cb.cbufp++;
rv++;
if(so->cb.cbufp >= so->cb.cbufend){
so->cb.cbufend = so->cb.cbuf;
so->cb.cbufp = so->cb.cbuf;
}
return(rv);
}
if(so->txt || so_reaquire(so)){
/* windows only so second arg is ignored */
ucs = read_a_wide_char((FILE *) so->txt, NULL);
rv = (ucs == CCONV_EOF) ? 0 : 1;
}
if(rv){
/*
* Now we need to convert the UCS character to UTF-8
* and dole out the UTF-8 one char at a time.
*/
so->cb.cbufend = utf8_put(so->cb.cbuf, (unsigned long) ucs);
so->cb.cbufp = so->cb.cbuf;
if(so->cb.cbufend > so->cb.cbuf){
*c = *so->cb.cbufp;
so->cb.cbufp++;
if(so->cb.cbufp >= so->cb.cbufend){
so->cb.cbufend = so->cb.cbuf;
so->cb.cbufp = so->cb.cbuf;
}
}
else
*c = '?';
}
return(rv);
}
#endif /* _WINDOWS */
/*
* write a string into the specified storage object,
* expanding if necessary (and cheating if the object
* happens to be a file!)
*
* return 1 on success and 0 on failure
*/
int
so_cs_puts(STORE_S *so, char *s)
{
int slen = strlen(s);
if(so->dp + slen >= so->eot){
register size_t cur_o = so->dp - (unsigned char *) so->txt;
register size_t data_o = so->eod - (unsigned char *) so->txt;
register size_t len = so->eot - (unsigned char *) so->txt;
while(len <= cur_o + slen + 1){
size_t incr;
incr = MAX(len/2, MSIZE_INC);
len += incr;
}
fs_resize(&so->txt, len * sizeof(char));
so->dp = (unsigned char *)so->txt + cur_o;
so->eod = (unsigned char *)so->txt + data_o;
so->eot = (unsigned char *)so->txt + len;
memset(so->eod, 0, so->eot - so->eod);
}
memcpy(so->dp, s, slen);
so->dp += slen;
if(so->dp > so->eod)
so->eod = so->dp;
return(1);
}
int
so_cs_puts_locale(STORE_S *so, char *s)
{
int slen = strlen(s);
while(slen--)
if(!so_cs_writec_locale((unsigned char) *s++, so))
return(0);
return(1);
}
int
so_file_puts(STORE_S *so, char *s)
{
int rv = *s ? 0 : 1;
if(!rv && (so->txt || so_reaquire(so)))
do
rv = fwrite(s, strlen(s)*sizeof(char), (size_t)1, (FILE *)so->txt);
while(!rv && ferror((FILE *)so->txt) && errno == EINTR);
return(rv);
}
int
so_file_puts_locale(STORE_S *so, char *s)
{
int slen = strlen(s);
while(slen--)
if(!so_file_writec_locale((unsigned char) *s++, so))
return(0);
return(1);
}
#ifdef SMIME
/*
* put a character into the specified storage object,
* expanding if neccessary
*
* return 1 on success and 0 on failure
*/
int
so_bio_writec(int c, STORE_S *so)
{
if(so->txt && so->src == BioType){
unsigned char ch[1];
BIO *b = (BIO *) so->txt;
ch[0] = (unsigned char) (c & 0xff);
if(BIO_write(b, ch, 1) >= 1)
return(1);
}
return(0);
}
int
so_bio_readc(unsigned char *c, STORE_S *so)
{
if(so->txt && so->src == BioType){
unsigned char ch[1];
BIO *b = (BIO *) so->txt;
if(BIO_read(b, ch, 1) >= 1){
*c = ch[0];
return(1);
}
}
return(0);
}
/*
* write a string into the specified storage object,
* expanding if necessary (and cheating if the object
* happens to be a file!)
*
* return 1 on success and 0 on failure
*/
int
so_bio_puts(STORE_S *so, char *s)
{
if(so->txt && so->src == BioType){
BIO *b = (BIO *) so->txt;
int slen = strlen(s);
if(BIO_puts(b, s) >= slen)
return(1);
}
return(1);
}
#endif /* SMIME */
/*
*
*/
int
so_nputs(STORE_S *so, char *s, long int n)
{
while(n--)
if(!so_writec((unsigned char) *s++, so))
return(0); /* ERROR putting char ! */
return(1);
}
/*
* Position the storage object's pointer to the given offset
* from the start of the object's data.
*/
int
so_seek(STORE_S *so, long int pos, int orig)
{
if(so->src == CharStar){
switch(orig){
case 0 : /* SEEK_SET */
return((pos < so->eod - (unsigned char *) so->txt)
? so->dp = (unsigned char *)so->txt + pos, 0 : -1);
case 1 : /* SEEK_CUR */
return((pos > 0)
? ((pos < so->eod - so->dp) ? so->dp += pos, 0: -1)
: ((pos < 0)
? ((-pos < so->dp - (unsigned char *)so->txt)
? so->dp += pos, 0 : -1)
: 0));
case 2 : /* SEEK_END */
return((pos > 0)
? -1
: ((-pos <= so->eod - (unsigned char *) so->txt)
? so->dp = so->eod + pos, 0 : -1));
default :
return(-1);
}
}
else if(so->src == ExternalText){
if(etsod.seek)
return((*etsod.seek)(so->txt, pos, orig));
fatal("programmer botch: unsupported so_seek call");
/*NOTREACHED*/
return(0); /* suppress dumb compiler warnings */
}
#ifdef SMIME
else if(so->src == BioType){
BIO *b = (BIO *) so->txt;
if(b && BIO_method_type(b) != BIO_TYPE_MEM)
(void) BIO_reset(b);
return(0);
}
#endif /* SMIME */
else /* FileStar or TmpFileStar */
return((so->txt || so_reaquire(so))
? fseek((FILE *)so->txt,pos,orig)
: -1);
}
/*
* Change the given storage object's size to that specified. If size
* is less than the current size, the internal pointer is adjusted and
* all previous data beyond the given size is lost.
*
* Returns 0 on failure.
*/
int
so_truncate(STORE_S *so, long int size)
{
if(so->src == CharStar){
if(so->eod < (unsigned char *) so->txt + size){ /* alloc! */
unsigned char *newtxt = (unsigned char *) so->txt;
register size_t len = so->eot - (unsigned char *) so->txt;
while(len <= size)
len += MSIZE_INC; /* need to resize! */
if(len > so->eot - (unsigned char *) newtxt){
fs_resize((void **) &newtxt, len * sizeof(char));
so->eot = newtxt + len;
so->eod = newtxt + (so->eod - (unsigned char *) so->txt);
memset(so->eod, 0, so->eot - so->eod);
}
so->eod = newtxt + size;
so->dp = newtxt + (so->dp - (unsigned char *) so->txt);
so->txt = newtxt;
}
else if(so->eod > (unsigned char *) so->txt + size){
if(so->dp > (so->eod = (unsigned char *)so->txt + size))
so->dp = so->eod;
memset(so->eod, 0, so->eot - so->eod);
}
return(1);
}
else if(so->src == ExternalText){
if(etsod.truncate)
return((*etsod.truncate)(so, (off_t) size));
fatal("programmer botch: unsupported so_truncate call");
/*NOTREACHED*/
return(0); /* suppress dumb compiler warnings */
}
#ifdef SMIME
else if(so->src == BioType){
fatal("programmer botch: unsupported so_truncate call for BioType");
/*NOTREACHED*/
return(0); /* suppress dumb compiler warnings */
#ifdef notdef
long len;
BIO *b = (BIO *) so->txt;
if(b){
BUF_MEM *biobuf = NULL;
BIO_get_mem_ptr(b, &biobuf);
if(biobuf){
BUF_MEM_grow(biobuf, size);
return(1);
}
}
return(0);
#endif /* notdef */
}
#endif /* SMIME */
else /* FileStar or TmpFileStar */
return(fflush((FILE *) so->txt) != EOF
&& fseek((FILE *) so->txt, size, 0) == 0
&& ftruncate(fileno((FILE *)so->txt), (off_t) size) == 0);
}
/*
* Report given storage object's position indicator.
* Returns 0 on failure.
*/
long
so_tell(STORE_S *so)
{
if(so->src == CharStar){
return((long) (so->dp - (unsigned char *) so->txt));
}
else if(so->src == ExternalText){
if(etsod.tell)
return((*etsod.tell)(so));
fatal("programmer botch: unsupported so_tell call");
/*NOTREACHED*/
return(0); /* suppress dumb compiler warnings */
}
#ifdef SMIME
else if(so->src == BioType){
fatal("programmer botch: unsupported so_tell call for BioType");
/*NOTREACHED*/
return(0); /* suppress dumb compiler warnings */
}
#endif /* SMIME */
else /* FileStar or TmpFileStar */
return(ftell((FILE *) so->txt));
}
/*
* so_attr - hook to hang random attributes onto the storage object
*/
char *
so_attr(STORE_S *so, char *key, char *value)
{
if(so && key){
if(value){
PARAMETER **pp = &so->attr;
while(1){
if(*pp){
if((*pp)->attribute && !strcmp(key, (*pp)->attribute)){
if((*pp)->value)
fs_give((void **)&(*pp)->value);
break;
}
pp = &(*pp)->next;
}
else{
*pp = mail_newbody_parameter();
(*pp)->attribute = cpystr(key);
break;
}
}
return((*pp)->value = cpystr(value));
}
else{
PARAMETER *p;
for(p = so->attr; p; p = p->next)
if(p->attribute && !strcmp(key, p->attribute))
return(p->value);
}
}
return(NULL);
}
/*
* so_release - a rather misnamed function. the idea is to release
* what system resources we can (e.g., open files).
* while maintaining a reference to it.
* it's up to the functions that deal with this object
* next to re-aquire those resources.
*/
int
so_release(STORE_S *so)
{
if(so->txt && so->name && (so->src == FileStar || so->src == TmpFileStar)){
if(fget_pos((FILE *)so->txt, (fpos_t *)&(so->used)) == 0){
fclose((FILE *)so->txt); /* free the handle! */
so->txt = NULL;
}
}
return(1);
}
/*
* so_reaquire - get any previously released system resources we
* may need for the given storage object.
* NOTE: at the moment, only FILE * types of objects are
* effected, so it only needs to be called before
* references to them.
*
*/
int
so_reaquire(STORE_S *so)
{
int rv = 1;
if(!so->txt && (so->src == FileStar || so->src == TmpFileStar)){
if(!(so->txt = so_file_open(so))){
q_status_message2(SM_ORDER,3,5, "ERROR reopening %.200s : %.200s",
so->name, error_description(errno));
rv = 0;
}
else if(fset_pos((FILE *)so->txt, (fpos_t *)&(so->used))){
q_status_message2(SM_ORDER, 3, 5,
"ERROR positioning in %.200s : %.200s",
so->name, error_description(errno));
rv = 0;
}
}
return(rv);
}
/*
* so_text - return a pointer to the text the store object passed
*/
void *
so_text(STORE_S *so)
{
return((so) ? so->txt : NULL);
}
/*
* Similar to fgets but reading from a storage object.
*/
char *
so_fgets(STORE_S *so, char *s, size_t size)
{
unsigned char c;
char *p = s;
while(--size > 0 && so_readc(&c, so) > 0){
*p++ = (char) c;
if(c == '\n')
break;
}
*p = '\0';
return((p>s) ? s : NULL);
}
|