File: fmt_uuencoded.c

package info (click to toggle)
libowfat 0.34-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,288 kB
  • sloc: ansic: 20,181; makefile: 16
file content (39 lines) | stat: -rw-r--r-- 931 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "fmt.h"
#include "textcode.h"
#include "haveinline.h"

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

size_t fmt_uuencoded(char* dest,const char* src,size_t len) {
  size_t i;
  register const unsigned char* s=(const unsigned char*) src;
  const char* orig=dest;
  size_t tmp;
  if (!dest) return len>((size_t)-1)/2?(size_t)-1:(len+2)/3*4;
  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;
}