add textcode api for uuencode and uudecode, base64 and quoted printable.
parent
980e24e92a
commit
d570a9c307
@ -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
|
@ -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;
|
||||
}
|
@ -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…
Reference in New Issue