Chinaunix首页 | 论坛 | 博客
  • 博客访问: 283657
  • 博文数量: 48
  • 博客积分: 2931
  • 博客等级: 少校
  • 技术积分: 720
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-19 22:12
文章分类

全部博文(48)

文章存档

2012年(1)

2011年(6)

2010年(33)

2009年(5)

2008年(3)

我的朋友

分类:

2008-12-25 18:17:18

/*
 * uuencode.c -
 * Simple uuencode utility
  */


#include "stdio.h"
#include "stdlib.h"
#include "string.h"


#define MAX_LINELEN 45
#define LINE_BUF_SIZE 256

#define ENCODE_BYTE(b) (((b) == 0) ? 0x60 : ((b) + 0x20))
#define DECODE_BYTE(b) ((b == 0x60) ? 0 : b - 0x20)

typedef unsigned char BYTE;


int unencode(char *inbuf,int inlen,char *outbuf)
{
    int linecnt,start=1;
    char obuf[4];
    BYTE *inbytep;

    outbuf[0]=ENCODE_BYTE (inlen);
    
    for (linecnt = inlen, inbytep = inbuf;
         linecnt > 0;
         linecnt -= 3, inbytep += 3){
              obuf [0] = ENCODE_BYTE ((inbytep [0] & 0xFC) >> 2);
              obuf [1] = ENCODE_BYTE (((inbytep [0] & 0x03) << 4) +
                                ((inbytep [1] & 0xF0) >> 4));
              obuf [2] = ENCODE_BYTE (((inbytep [1] & 0x0F) << 2) +
                                ((inbytep [2] & 0xC0) >> 6));
              obuf [3] = ENCODE_BYTE (inbytep [2] & 0x3F);
        memcpy(outbuf+start,obuf,4);
        start=start+4;
        }
    outbuf[start]='\0';
}


int undecode(char *inbuf,char *outbuf)
{
    int linelen = 0 ,linecnt,start=0;
    BYTE outbyte [3];
    char *linep = NULL;
    
    /* The first byte of the line represents the length of the DECODED line */
    linelen = DECODE_BYTE (inbuf [0]);
        linep = inbuf + 1;
        
    for (linecnt = linelen; linecnt > 0; linecnt -= 3, linep += 4){
        /* Check for premature end-of-line */
            if ((linep [0] == '\0') || (linep [1] == '\0') ||
                      (linep [2] == '\0') || (linep [3] == '\0')){
                   fprintf (stderr, "uudecode: Error in encoded block\n");
             return;
              }
        
        /* Decode the 4-byte block */
           outbyte [0] = DECODE_BYTE (linep [0]);
              outbyte [1] = DECODE_BYTE (linep [1]);
             outbyte [0] <<= 2;
              outbyte [0] |= (outbyte [1] >> 4) & 0x03;
              outbyte [1] <<= 4;
              outbyte [2] = DECODE_BYTE (linep [2]);
              outbyte [1] |= (outbyte [2] >> 2) & 0x0F;
              outbyte [2] <<= 6;
              outbyte [2] |= DECODE_BYTE (linep [3]) & 0x3F;

              /* Write the decoded bytes to the output file */
        if(linecnt>3){
            memcpy(outbuf+start,outbyte,3);
            start=start+3;
        }else{
            memcpy(outbuf+start,outbyte,linecnt);
            start=start+linecnt;
        }
    }
    return start;
}

int main (int argc, char *argv [])
{
    FILE *infile = NULL;
      int linelen;
      BYTE inbuf [MAX_LINELEN];
      char outbuf [512];
    
    FILE *outfp;


      if (argc != 2){
            fprintf (stderr, "Syntax: uuencode \n");
            exit (1);
      }

      infile = fopen (argv [1], "rb");
      if (infile == NULL){
            fprintf (stderr, "uuencode: Couldn't open input file %s\n", argv[1]);
            exit (1);
      }

    outfp=fopen("123.xmp","wb");
      do{
            linelen = fread (inbuf, 1, MAX_LINELEN, infile);
        unencode(inbuf,linelen,outbuf);
        printf("%s\n",outbuf);
        
        char temp[512];
        int n=undecode(outbuf,temp);
        fwrite(temp,1,n,outfp);
      } while (linelen != 0);
    
    fclose(outfp);
      fclose (infile);
    return 0;
}


阅读(904) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~