lundi 26 mars 2012

Yenc decoder en C


// Created by Juergen Helbing in 2001 as a part of the MyNews project 

// This is a source code example for the yDecoder - see also:  http://www.yenc.org
// It is public domain - as well as the yEnc specification
//
// The progamming style is very "basic" and readable.
//
// The function is scanning the incoming file stream (fIn) for yEncoded blocks.
// The stream must be supported by the newsreader - in case of multipart
// the entire parts must be in the input stream. The newsreader should provide
// the decoder with a textfile which contains all the parts. This source
// can contain additional headers between the parts - they are simply skipped.

// This example requires the parts in correct sequence (1-2-3-4-...-n)
// Advanced decoders might be able to operate on mixed sequence - and even
// missing parts or parts from different "fill" posts

// print() and eprint() are logging displays from 'MyNews'
// Crc32 - functions are available in the yEncoder example

// ad_fgetscr() read data from the input stream into the buffer and removes CRLF.


unsigned long hex_to_ulong(char * text)  // Because strtol() does not deliver 32 bit on my C-Compiler
	{
	unsigned long res;
	unsigned char c;

	if (text==NULL) return(-1);

	res=0;
loop:
	c=*text; text++;
	if ((c>='0')&(c<='9'))
		{
		res=(res<<4)+((long)(c-48) & 0x0F);
		goto loop;
		}
	if ((c>='A')&(c<='F'))
		{
		res=(res<<4)+((long)(c-55) & 0x0F);
		goto loop;
		}
	if ((c>='a')&(c<='f'))
		{
		res=(res<<4)+((long)(c-87) & 0x0F);
		goto loop;
		}
	return(res);
	}



int yDecode(FILE * fOut, FILE * fIn, long y_line, long y_size,int y_part)
	{
	unsigned char srcbuf[4100];
	unsigned char desbuf[4100];
	unsigned char * srcp;
	unsigned char * desp;
	int deslen;
	int srclen;
	unsigned char c;
	int id;
	char * cp;
	long decolen;
	long y_begin;
	long y_end;
	long sumlen;
	int partnr;
	unsigned long crc32;
	char name[260];
	int esize;

	if (aDsp) print("yDecoder started...\r\n");

	sumlen=0; partnr=1;

part_start:
	CrcInit();  // Analyse only CRC per part
	decolen=0;
	deslen=0; desp=desbuf;

	if (y_part)  // This is a multipart message !
		{
		cp=ad_fgetscr((char*) srcbuf,4097,fIn);  // fgets especially with ad_length
		if (cp==NULL)
			{
			eprint("Unexpected eof in yEncoded file\r\n");
			return(1);
			}

		if (aDsp) print("=ypart-line:  %s\r\n",srcbuf);

		if (strncmp((char*) srcbuf,"=ypart ",7))
			{
			eprint("Missing =ypart line in yEncoded multipart message\r\n");
			return(2);
			}
		cp=strstr((char*)srcbuf,"end=");
		if (cp==NULL)
			{
			eprint("Missing end= in yEncoded multipart message\r\n");
			return(2);
			}
		y_end=atol(cp+4);

		cp=strstr((char*)srcbuf,"begin=");
		if (cp==NULL)
			{
			eprint("Missing begin= in yEncoded multipart message\r\n");
			return(2);
			}
		y_begin=atol(cp+6);

		if (aDsp) print("part-begin: %ld\r\n",y_begin);
		if (aDsp) print("part-end  : %ld\r\n",y_end);

		}


loop:

	cp=ad_fgetscr((char*) srcbuf,4097,fIn);  // fgets especially with ad_length
	if (cp==NULL)
		{
		eprint("Unexpected eof in yEncoded file\r\n");
		return(1);
		}
	if (strncmp((char*) srcbuf,"=yend ",6)==0)
		{
		if (aDsp) print("Endline (%d bytes): %s\r\n",decolen,srcbuf);
		goto end_of_file;
		}
	srclen=strlen((char*)srcbuf);
	if (srclen

Aucun commentaire:

Enregistrer un commentaire