瀏覽代碼

add textcode api for uuencode and uudecode, base64 and quoted printable.

master
Felix von Leitner 18 年之前
父節點
當前提交
2969eaa2d7
共有 6 個檔案被更改,包括 125 行新增4 行删除
  1. +5
    -0
      CHANGES
  2. +7
    -4
      Makefile
  3. +39
    -0
      t.c
  4. +14
    -0
      textcode.h
  5. +36
    -0
      textcode/fmt_uuencoded.c
  6. +24
    -0
      textcode/scan_uuencoded.c

+ 5
- 0
CHANGES 查看文件

@@ -1,3 +1,8 @@
0.12:
add textcode api for uuencode/uudecode, base64, quoted printable,
url-encoding and yenc.
implement fmt_uuencoded and scan_uuencoded.

0.11:
fix fmt_long (didn't count the '-'), which in turn broke
buffer_putlong


+ 7
- 4
Makefile 查看文件

@@ -5,7 +5,7 @@ MAN3DIR=${prefix}/man/man3

all: t byte.a fmt.a scan.a str.a uint.a open.a stralloc.a unix.a socket.a buffer.a mmap.a libowfat.a

VPATH=str:byte:fmt:scan:uint:open:stralloc:unix:socket:buffer:mmap
VPATH=str:byte:fmt:scan:uint:open:stralloc:unix:socket:buffer:mmap:textcode

# comment out the following line if you don't want to build with the
# diet libc (http://www.fefe.de/dietlibc/).
@@ -25,6 +25,7 @@ UNIX_OBJS=$(patsubst unix/%.c,%.o,$(wildcard unix/*.c))
SOCKET_OBJS=$(patsubst socket/%.c,%.o,$(wildcard socket/*.c))
BUFFER_OBJS=$(patsubst buffer/%.c,%.o,$(wildcard buffer/*.c))
MMAP_OBJS=$(patsubst mmap/%.c,%.o,$(wildcard mmap/*.c))
TEXTCODE_OBJS=$(patsubst textcode/%.c,%.o,$(wildcard textcode/*.c))

$(BYTE_OBJS): byte.h
$(FMT_OBJS): fmt.h
@@ -35,6 +36,7 @@ $(STRA_OBJS): stralloc.h
$(SOCKET_OBJS): socket.h
$(BUFFER_OBJS): buffer.h
$(MMAP_OBJS): mmap.h
$(TEXTCODE_OBJS): textcode.h

byte.a: $(BYTE_OBJS)
fmt.a: $(FMT_OBJS)
@@ -47,10 +49,11 @@ unix.a: $(UNIX_OBJS)
socket.a: $(SOCKET_OBJS)
buffer.a: $(BUFFER_OBJS)
mmap.a: $(MMAP_OBJS)
textcode.a: $(TEXTCODE_OBJS)

libowfat.a: $(BYTE_OBJS) $(FMT_OBJS) $(SCAN_OBJS) $(STR_OBJS) \
$(UINT_OBJS) $(OPEN_OBJS) $(STRA_OBJS) $(UNIX_OBJS) $(SOCKET_OBJS) \
$(BUFFER_OBJS) $(MMAP_OBJS)
$(BUFFER_OBJS) $(MMAP_OBJS) $(TEXTCODE_OBJS)

%.o: %.c
$(DIET) $(CC) -c $< -o $@ $(CFLAGS)
@@ -60,7 +63,7 @@ $(BUFFER_OBJS) $(MMAP_OBJS)
-ranlib $@

t: t.o socket.a stralloc.a buffer.a scan.a uint.a mmap.a open.a fmt.a \
str.a byte.a
str.a byte.a textcode.a
$(DIET) $(CC) -g -o $@ $^

.PHONY: clean tar install rename
@@ -112,4 +115,4 @@ socket_accept4.o socket_accept6.o socket_connected.o socket_local4.o \
socket_local6.o socket_recv4.o socket_recv6.o socket_remote4.o \
socket_remote6.o: havesl.h

fmt_xlong.o scan_xlong.o fmt_ip6_flat.o: haveinline.h
fmt_xlong.o scan_xlong.o fmt_ip6_flat.o $(TEXTCODE_OBJS): haveinline.h

+ 39
- 0
t.c 查看文件

@@ -9,6 +9,7 @@
#include "ip4.h"
#include "mmap.h"
#include "open.h"
#include "textcode.h"
#include <stdio.h>
#include <unistd.h>

@@ -16,10 +17,48 @@
__asm__ __volatile__ ("rdtsc" : "=a" (low) : : "edx")

int main(int argc,char* argv[]) {
unsigned long size;
char* buf=mmap_read(argv[1],&size);
if (buf) {
const char* c=buf;
const char* max=buf+size;
while (c<max) {
char tmp[100];
unsigned int scanned;
unsigned int x=scan_uuencoded(c,tmp,&scanned);
if (!x) {
if (!strncmp(c,"end\n",4))
return 0;
else {
parseerror:
buffer_putsflush(buffer_2,"parse error!\n");
exit(1);
}
}
write(1,tmp,scanned);
c+=x;
if (*c!='\n') goto parseerror;
++c;
}
}
return 0;
#if 0
unsigned long size;
char* buf=mmap_read(argv[1],&size);
if (buf) {
unsigned int x=fmt_uuencoded(0,buf,size);
unsigned int y;
char* tmp=malloc(x+1);
y=fmt_uuencoded(tmp,buf,size);
write(1,tmp,x);
}
#endif
#if 0
char buf[]="00000000000000000000000000000001";
char ip[16];
if (scan_ip6_flat(buf,ip) != str_len(buf))
buffer_putsflush(buffer_2,"parse error!\n");
#endif
#if 0
int fd=open_read("t.c");
buffer b;


+ 14
- 0
textcode.h 查看文件

@@ -0,0 +1,14 @@
#ifndef TEXTCODE_H
#define TEXTCODE_H

unsigned int fmt_uuencoded(char* dest,const char* src,unsigned int len);
unsigned int fmt_base64(char* dest,const char* src,unsigned int len);
unsigned int fmt_quotedprintable(char* dest,const char* src,unsigned int len);
unsigned int fmt_yenc(char* dest,const char* src,unsigned int len);

unsigned int scan_uuencoded(const char *src,char *dest,unsigned int *destlen);
unsigned int scan_base64(const char *src,char *dest,unsigned int *destlen);
unsigned int scan_quotedprintable(const char *src,char *dest,unsigned int *destlen);
unsigned int scan_yenc(const char *src,char *dest,unsigned int *destlen);

#endif

+ 36
- 0
textcode/fmt_uuencoded.c 查看文件

@@ -0,0 +1,36 @@
#include "fmt.h"
#include "textcode.h"
#include "haveinline.h"

static inline unsigned int enc(unsigned char x) {
return ((x-1)&077)+'!';
}

unsigned int fmt_uuencoded(char* dest,const char* src,unsigned int len) {
unsigned int i;
register const unsigned char* s=(const unsigned char*) src;
const char* orig=dest;
unsigned long tmp;
while (len) {
{
register unsigned int diff;
if (len>45) { i=15; diff=45; } else { i=(len+2)/3; diff=len; }
if (orig) *dest=enc(diff); ++dest;
len-=diff;
}
for (; i; --i) {
tmp=((unsigned long)s[0] << 16) +
((unsigned long)s[1] << 8) +
((unsigned long)s[2]);
if (orig) {
dest[0]=enc((tmp>>(3*6))&077);
dest[1]=enc((tmp>>(2*6))&077);
dest[2]=enc((tmp>>(1*6))&077);
dest[3]=enc(tmp&077);
}
dest+=4; s+=3;
}
if (orig) *dest='\n'; ++dest;
}
return dest-orig;
}

+ 24
- 0
textcode/scan_uuencoded.c 查看文件

@@ -0,0 +1,24 @@
#include "textcode.h"
#include "haveinline.h"

unsigned int scan_uuencoded(const char *src,char *dest,unsigned int *destlen) {
unsigned int len;
unsigned long tmp;
register const unsigned char* s=(const unsigned char*) src;
const char* orig=dest;
if ((len=*s-' ')>64) return 0;
++s;
while (len>0) {
if (s[0]-' '>64 || s[1]-' '>64 || s[2]-' '>64 || s[3]-' '>64) return 0;
tmp=(((s[0]-' ')&077) << (3*6)) +
(((s[1]-' ')&077) << (2*6)) +
(((s[2]-' ')&077) << (1*6)) +
(((s[3]-' ')&077));
s+=4;
if (len) { *dest=tmp>>16; ++dest; --len; }
if (len) { *dest=tmp>>8; ++dest; --len; }
if (len) { *dest=tmp&0xff; ++dest; --len; }
}
*destlen=dest-orig;
return (const char*)s-src;
}

Loading…
取消
儲存