|
|
1.1 ! root 1: /* smbtxt.c */ ! 2: ! 3: /* Synchronet message base (SMB) message text library routines */ ! 4: ! 5: /* $Id: smbtxt.c,v 1.15 2005/09/30 09:05:16 rswindell Exp $ */ ! 6: ! 7: /**************************************************************************** ! 8: * @format.tab-size 4 (Plain Text/Source Code File Header) * ! 9: * @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) * ! 10: * * ! 11: * Copyright 2005 Rob Swindell - http://www.synchro.net/copyright.html * ! 12: * * ! 13: * This library is free software; you can redistribute it and/or * ! 14: * modify it under the terms of the GNU Lesser General Public License * ! 15: * as published by the Free Software Foundation; either version 2 * ! 16: * of the License, or (at your option) any later version. * ! 17: * See the GNU Lesser General Public License for more details: lgpl.txt or * ! 18: * http://www.fsf.org/copyleft/lesser.html * ! 19: * * ! 20: * Anonymous FTP access to the most recent released source is available at * ! 21: * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net * ! 22: * * ! 23: * Anonymous CVS access to the development source and modification history * ! 24: * is available at cvs.synchro.net:/cvsroot/sbbs, example: * ! 25: * cvs -d :pserver:[email protected]:/cvsroot/sbbs login * ! 26: * (just hit return, no password is necessary) * ! 27: * cvs -d :pserver:[email protected]:/cvsroot/sbbs checkout src * ! 28: * * ! 29: * For Synchronet coding style and modification guidelines, see * ! 30: * http://www.synchro.net/source.html * ! 31: * * ! 32: * You are encouraged to submit any modifications (preferably in Unix diff * ! 33: * format) via e-mail to [email protected] * ! 34: * * ! 35: * Note: If this box doesn't appear square, then you need to fix your tabs. * ! 36: ****************************************************************************/ ! 37: ! 38: /* ANSI */ ! 39: #include <stdlib.h> /* malloc/realloc/free */ ! 40: #include <string.h> /* strlen */ ! 41: ! 42: /* SMB-specific */ ! 43: #include "smblib.h" ! 44: ! 45: char* SMBCALL smb_getmsgtxt(smb_t* smb, smbmsg_t* msg, ulong mode) ! 46: { ! 47: char* buf; ! 48: char* lzhbuf; ! 49: char* p; ! 50: char* str; ! 51: ushort xlat; ! 52: uint i; ! 53: int lzh; /* BOOL */ ! 54: long l=0,lzhlen,length; ! 55: ! 56: if((buf=(char*)malloc(sizeof(char)))==NULL) { ! 57: sprintf(smb->last_error ! 58: ,"malloc failure of %u bytes for buffer" ! 59: ,sizeof(char)); ! 60: return(NULL); ! 61: } ! 62: *buf=0; ! 63: ! 64: if(!(mode&GETMSGTXT_NO_HFIELDS)) { ! 65: for(i=0;i<(uint)msg->total_hfields;i++) { /* comment headers are part of text */ ! 66: if(msg->hfield[i].type!=SMB_COMMENT && msg->hfield[i].type!=SMTPSYSMSG) ! 67: continue; ! 68: str=(char*)msg->hfield_dat[i]; ! 69: length=strlen(str)+2; /* +2 for crlf */ ! 70: if((p=(char*)realloc(buf,l+length+1))==NULL) { ! 71: sprintf(smb->last_error ! 72: ,"realloc failure of %ld bytes for comment buffer" ! 73: ,l+length+1); ! 74: return(buf); ! 75: } ! 76: buf=p; ! 77: l+=sprintf(buf+l,"%s\r\n",str); ! 78: } ! 79: } ! 80: ! 81: for(i=0;i<(uint)msg->hdr.total_dfields;i++) { ! 82: if(msg->dfield[i].length<=sizeof(xlat)) ! 83: continue; ! 84: switch(msg->dfield[i].type) { ! 85: case TEXT_BODY: ! 86: if(mode&GETMSGTXT_NO_BODY) ! 87: continue; ! 88: break; ! 89: case TEXT_TAIL: ! 90: if(!(mode&GETMSGTXT_TAILS)) ! 91: continue; ! 92: break; ! 93: default: /* ignore other data types */ ! 94: continue; ! 95: } ! 96: fseek(smb->sdt_fp,msg->hdr.offset+msg->dfield[i].offset ! 97: ,SEEK_SET); ! 98: fread(&xlat,sizeof(xlat),1,smb->sdt_fp); ! 99: lzh=0; ! 100: if(xlat==XLAT_LZH) { ! 101: lzh=1; ! 102: fread(&xlat,sizeof(xlat),1,smb->sdt_fp); ! 103: } ! 104: if(xlat!=XLAT_NONE) /* no other translations currently supported */ ! 105: continue; ! 106: ! 107: length=msg->dfield[i].length-sizeof(xlat); ! 108: if(lzh) { ! 109: length-=sizeof(xlat); ! 110: if(length<1) ! 111: continue; ! 112: if((lzhbuf=(char*)malloc(length))==NULL) { ! 113: sprintf(smb->last_error ! 114: ,"malloc failure of %ld bytes for LZH buffer" ! 115: ,length); ! 116: return(buf); ! 117: } ! 118: smb_fread(smb,lzhbuf,length,smb->sdt_fp); ! 119: lzhlen=*(long*)lzhbuf; ! 120: if((p=(char*)realloc(buf,l+lzhlen+3L))==NULL) { ! 121: sprintf(smb->last_error ! 122: ,"realloc failure of %ld bytes for text buffer" ! 123: ,l+lzhlen+3L); ! 124: free(lzhbuf); ! 125: return(buf); ! 126: } ! 127: buf=p; ! 128: lzh_decode((char*)lzhbuf,length,(char*)buf+l); ! 129: free(lzhbuf); ! 130: l+=lzhlen; ! 131: } ! 132: else { ! 133: if((p=(char*)realloc(buf,l+length+3L))==NULL) { ! 134: sprintf(smb->last_error ! 135: ,"realloc failure of %ld bytes for text buffer" ! 136: ,l+length+3L); ! 137: return(buf); ! 138: } ! 139: buf=p; ! 140: p=buf+l; ! 141: l+=fread(p,1,length,smb->sdt_fp); ! 142: } ! 143: if(!l) ! 144: continue; ! 145: l--; ! 146: while(l && buf[l]==0) l--; ! 147: l++; ! 148: *(buf+l)='\r'; /* CR */ ! 149: l++; ! 150: *(buf+l)='\n'; /* LF */ ! 151: l++; ! 152: *(buf+l)=0; ! 153: } ! 154: ! 155: return(buf); ! 156: } ! 157: ! 158: void SMBCALL smb_freemsgtxt(char* buf) ! 159: { ! 160: if(buf!=NULL) ! 161: free(buf); ! 162: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.