Browse Source

more textcode stuff

master
Felix von Leitner 19 years ago
parent
commit
78cb1c062d
5 changed files with 91 additions and 0 deletions
  1. +16
    -0
      test/buffer_1.c
  2. +45
    -0
      test/uudecode.c
  3. +2
    -0
      textcode.h
  4. +1
    -0
      textcode/base64.c
  5. +27
    -0
      textcode/fmt_base64.c

+ 16
- 0
test/buffer_1.c View File

@ -0,0 +1,16 @@
#include "buffer.h"
main() {
buffer_puts(buffer_1,"ulong: ");
buffer_putulong(buffer_1,23);
buffer_puts(buffer_1,"\nlong: ");
buffer_putlong(buffer_1,-23);
buffer_puts(buffer_1,"\n8long: ");
buffer_put8long(buffer_1,23);
buffer_puts(buffer_1,"\nxlong:");
buffer_putspace(buffer_1);
buffer_putxlong(buffer_1,23);
buffer_putsflush(buffer_1,"\n");
buffer_putsalign(buffer_1,"aligned\n");
buffer_flush(buffer_1);
}

+ 45
- 0
test/uudecode.c View File

@ -0,0 +1,45 @@
#include <unistd.h>
#include "textcode.h"
#include "str.h"
#include "buffer.h"
int main(int argc,char* argv[]) {
char buf[4096];
buffer filein;
int fd=1;
if (argc>1) {
fd=open_read(argv[1]);
if (fd<0) {
buffer_puts(buffer_2,"error: could not open \"");
buffer_puts(buffer_2,argv[1]);
buffer_putsflush(buffer_2,"\"\n");
return 1;
}
}
buffer_init(&filein,read,fd,buf,sizeof buf);
/* skip to "^begin " */
for (;;) {
char line[1000]; /* uuencoded lines can never be longer than 64 characters */
int l;
if ((l=buffer_getline(&filein,line,(sizeof line)-1))==0) {
buffer_putsflush(buffer_2,"warning: hit end of file without finding any uuencoded data!\n");
return 0;
}
if (l>0 && buf[l-1]=='\r') --l; /* kill DOS line endings */
buf[l]=0;
if (!str_diffn(line,"begin ",6)) break;
}
/* read uuencoded lines */
for (;;) {
char line[1000]; /* uuencoded lines can never be longer than 64 characters */
int l;
if ((l=buffer_getline(&filein,line,(sizeof line)-1))==0) {
buffer_putsflush(buffer_2,"warning: hit end of file without finding \"end\"!\n");
return 0;
}
if (l>0 && buf[l-1]=='\r') --l; /* kill DOS line endings */
buf[l]=0;
if (!str_diffn(line,"begin ",6)) break;
}
/* TODO */
}

+ 2
- 0
textcode.h View File

@ -18,4 +18,6 @@ unsigned int scan_quotedprintable(const char *src,char *dest,unsigned int *destl
unsigned int scan_urlencode(const char *src,char *dest,unsigned int *destlen);
unsigned int scan_yenc(const char *src,char *dest,unsigned int *destlen);
extern const char base64[64];
#endif

+ 1
- 0
textcode/base64.c View File

@ -0,0 +1 @@
const char base64[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

+ 27
- 0
textcode/fmt_base64.c View File

@ -0,0 +1,27 @@
#include "fmt.h"
#include "textcode.h"
#include "haveinline.h"
static inline unsigned int enc(unsigned char x) {
return ((x-1)&077)+'!';
}
unsigned int fmt_base64(char* dest,const char* src,unsigned int len) {
register const unsigned char* s=(const unsigned char*) src;
unsigned short bits=0,temp=0;
unsigned long written=0,i;
for (i=0; i<len; ++i) {
temp<<=8; temp+=s[i]; bits+=8;
while (bits>6) {
if (dest) dest[written]=base64[((temp>>(bits-6))&63)];
++written; bits-=6;
}
}
if (bits) {
temp<<=(6-bits);
if (dest) dest[written]=base64[temp&63];
++written;
}
while (written&3) { if (dest) dest[written]='='; ++written; }
return written;
}

Loading…
Cancel
Save