|
|
1.1 root 1: /* yenc.c */
2:
3: /* yEnc encoding/decoding routines */
4:
5: /* $Id: yenc.c,v 1.1 2003/05/18 03:48:57 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 2003 Rob Swindell - http://www.synchro.net/copyright.html *
12: * *
13: * This program is free software; you can redistribute it and/or *
14: * modify it under the terms of the GNU 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 General Public License for more details: gpl.txt or *
18: * http://www.fsf.org/copyleft/gpl.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: #include <stdio.h>
39: #include <stdlib.h>
40: #include <string.h>
41: #include <ctype.h>
42: #include "yenc.h"
43:
44: #define YENC_BIAS 42
45: #define YENC_ESCAPE_CHAR '='
46: #define YENC_ESCAPE_BIAS 64
47:
48: int ydecode(char *target, size_t tlen, const char *source, size_t slen)
49: {
50: char ch;
51: size_t rd=0;
52: size_t wr=0;
53:
54: if(slen==0)
55: slen=strlen(source);
56: while(rd<slen && wr<tlen) {
57: ch=source[rd++];
58: if(ch==YENC_ESCAPE_CHAR && rd<slen)
59: ch=source[rd++]-YENC_ESCAPE_BIAS;
60: ch-=YENC_BIAS;
61: target[wr++]=ch;
62: }
63:
64: return(wr);
65: }
66:
67: int yencode(char *target, size_t tlen, const char *source, size_t slen)
68: {
69: char ch;
70: size_t rd=0;
71: size_t wr=0;
72: int done=0;
73:
74: if(slen==0)
75: slen=strlen(source);
76: while(rd<=slen && wr<tlen && !done) {
77: ch=source[rd++];
78: ch+=YENC_BIAS;
79: switch(ch) {
80: case 0:
81: case '\r':
82: case '\n':
83: case YENC_ESCAPE_CHAR:
84: if(wr+1>=tlen) { /* no room for escaped char */
85: done=1;
86: continue;
87: }
88: ch+=YENC_ESCAPE_BIAS;
89: target[wr++]=YENC_ESCAPE_CHAR;
90: break;
91: }
92: target[wr++]=ch;
93: }
94:
95: if(wr<tlen)
96: target[wr++]=0;
97: return(wr);
98: }
99:
100: #if defined(YDECODE_TEST)
101:
102: static char* truncstr(char* str, const char* set)
103: {
104: char* p;
105:
106: p=strpbrk(str,set);
107: if(p!=NULL)
108: *p=0;
109:
110: return(p);
111: }
112:
113: int main(int argc, char**argv)
114: {
115: char str[1024];
116: char buf[256];
117: char* p;
118: FILE* in;
119: FILE* out=NULL;
120: int len;
121: int line;
122:
123: if(argc<2) {
124: fprintf(stderr,"usage: %s infile\n",argv[0]);
125: return 1;
126: }
127:
128: if((in=fopen(argv[1],"rb"))==NULL) {
129: perror(argv[1]);
130: return 1;
131: }
132:
133: while(!feof(in)) {
134: memset(str,0,sizeof(str));
135: if(fgets(str,sizeof(str),in)==NULL)
136: break;
137: truncstr(str,"\r\n");
138: if(strncmp(str,"=ybegin ",8)==0) {
139: p=strstr(str,"name=");
140: p+=5;
141: if((out=fopen(p,"wb"))==NULL) {
142: perror(p);
143: return 1;
144: }
145: fprintf(stderr,"Creating %s\n",p);
146: line=1;
147: continue;
148: }
149: if(strncmp(str,"=yend ",6)==0) {
150: if(out!=NULL) {
151: fclose(out);
152: out=NULL;
153: }
154: continue;
155: }
156: if(out==NULL)
157: continue;
158: len=ydecode(buf,sizeof(buf),str,0);
159: if(len<0) {
160: fprintf(stderr,"!Error decoding: %s\n",str);
161: break;
162: }
163: fwrite(buf,len,1,out);
164: line++;
165: }
166:
167: return 0;
168: }
169: #elif defined(YENCODE_TEST)
170:
171: int main(int argc, char**argv)
172: {
173: char str[1024];
174: char buf[256];
175: FILE* in;
176: int len;
177:
178: if(argc<2) {
179: fprintf(stderr,"usage: %s infile\n",argv[0]);
180: return 1;
181: }
182:
183: if((in=fopen(argv[1],"rb"))==NULL) {
184: perror(argv[1]);
185: return 1;
186: }
187:
188: while(!feof(in)) {
189: len=fread(buf,1,45,in);
190: if(len<0)
191: break;
192: len=yencode(str,sizeof(str),buf,len);
193: if(len<1)
194: break;
195: printf("%.*s",len,str);
196: }
197:
198: return 0;
199: }
200:
201: #endif
202:
203:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.