Vor einiger Zeit habe ich die Idee gehabt eine Firefox Extension zu schreiben, welche es ermöglicht binäre Dateianhänge aus dem Usenet zu laden. Das ganze hat auch soweit geklappt und funktioniert. Leider nicht in der Geschwindigkeit wie erhofft. Daher habe ich das Projekt eingestellt und möchte nun meinen Yenc Decoder für den Rest der Welt bereitstellen. Vielleicht kann ja einer etwas damit anfangen. Ansonsten seht es einfach als Proof-of-Concept. Es fehlt allerdings noch die CRC Prüfung, ansonsten ist alles notwendige drin.
Wer Interesse hat das Projekt mit mir fortzuführen, soll sich per Kontaktformular bei mir melden. Kompletter Source im erweiterten Teil dieses Beitrags und als Datei hier zum runterladen.
JAVASCRIPT:
-
/**
-
* Javascript YencDecoder
-
* @author Kjell Bublitz
-
* @license GPL
-
*/
-
var specialChar = false;
-
-
var yencDecoder =
-
{
-
yencPart: "",
-
yencTotal: "",
-
yencLine: "",
-
yencSize: "",
-
yencName: "",
-
yencCRC: "",
-
yencPCRC: "",
-
-
/**
-
* Searches the yEnc Part of a postbody and processes it. Extracting footer and header data,
-
* and the binary data itself. Once it has everything it decodes the yEnc Body.
-
* @param {String} the complete message
-
* @return {Object} yencObject with all infos
-
*/
-
processMessage: function(inText)
-
{
-
// Reset
-
this.yencPart = "";
-
this.yencTotal = "";
-
this.yencLine = "";
-
this.yencSize = "";
-
this.yencName = "";
-
this.yencCRC = "";
-
this.yencPCRC = "";
-
-
var ypartIdx = inText.search('=ypart');
-
var ypart = inText.match("=ypart.*");
-
-
var yheadIdx = inText.search('=ybegin');
-
var yhead = inText.match("=ybegin.*");
-
-
var yfootIdx = inText.search('=yend');
-
var yfoot = inText.match("=yend.*");
-
-
var yDataStart = yheadIdx;
-
var yDataEnd = yfootIdx;
-
-
// CONTROL
-
if(ypartIdx != -1)
-
{
-
thold = ypart[0].length + 1;
-
yDataStart = ypartIdx;
-
}
-
-
if(yhead && yfoot) // we need both
-
{
-
var thold = yhead[0].length + 1;
-
var yencData = inText.substring(yDataStart, yDataEnd).substr( thold );
-
-
if(this.parseYencHead(yhead[0]) && this.parseYencFoot(yfoot[0]) && yencData.length> 0)
-
{
-
var yObject = {
-
part: this.yencPart,
-
total: this.yencTotal,
-
line: this.yencLine,
-
size: this.yencSize,
-
crc32: this.yencCRC,
-
pcrc: this.yencPCRC,
-
name: this.yencName,
-
data: this.decodeYenc(yencData)
-
};
-
return yObject;
-
}
-
}
-
return false;
-
},
-
/**
-
* Extract the head data from the yenc post. Stuff like size, filename, partnumber, etc..
-
* @param {String} the complete =ybegin line
-
* @return {Boolean}
-
*/
-
parseYencHead: function(yhead)
-
{
-
if (typeof yhead == 'string')
-
{
-
var yheadSplit = yhead.split(' ');
-
-
for(var x=0;x <yheadSplit.length;x++)
-
{
-
if(yheadSplit[x].match('part=.*')) {
-
this.yencPart = yheadSplit[x].split('=')[1];
-
}
-
if(yheadSplit[x].match('total=.*')) {
-
this.yencTotal = yheadSplit[x].split('=')[1];
-
}
-
if(yheadSplit[x].match('line=.*')) {
-
this.yencLine = yheadSplit[x].split('=')[1];
-
}
-
if(yheadSplit[x].match('size=.*')) {
-
this.yencSize = yheadSplit[x].split('=')[1];
-
}
-
if(yheadSplit[x].match('name=.*')) {
-
this.yencName = yheadSplit[x].split('=')[1];
-
}
-
}
-
return true;
-
}
-
return false;
-
},
-
/**
-
* Extract the CRC sum from the ending line
-
* @param {String} the complete =yend line
-
* @return {Boolean}
-
*/
-
parseYencFoot: function (yfoot)
-
{
-
if (typeof yfoot == 'string')
-
{
-
var yfootSplit = yfoot.split(' ');
-
-
for(var x=0;x <yfootSplit.length;x++)
-
{
-
if(yfootSplit[x].match('part=.*')) {
-
this.yencCRC = yfootSplit[x].split('=')[1];
-
}
-
if(yfootSplit[x].match('total=.*')) {
-
this.yencPCRC = yfootSplit[x].split('=')[1];
-
}
-
}
-
return true;
-
}
-
return false;
-
},
-
/**
-
* Decoded char by char.
-
* @param {String} Encoded
-
* @return {String} Decoded
-
*/
-
decodeYenc: function(yEncoded)
-
{
-
var yDecoded = "", yencChar, ascChar, percent_done;
-
-
for(var c=0;c <yEncoded.length; c++)
-
{
-
yencChar = this.charToInt(yEncoded[c]);
-
ascChar = this.decodeChar(yencChar);
-
-
if(specialChar == false) {
-
yDecoded += this.intToChar(ascChar);
-
}
-
}
-
return yDecoded;
-
},
-
/**
-
* Get CharacterCode
-
* @param {Char} ASCII Character
-
* @return {Integer} Character Code
-
*/
-
charToInt: function(ch)
-
{
-
var ordZero = '0'.charCodeAt(0);
-
return ch.charCodeAt(0);
-
},
-
/**
-
* Get ASCII Character
-
* @param {Integer} Character Code
-
* @return {Char} ASCII Character
-
*/
-
intToChar: function(ch)
-
{
-
var ordZero = '0'.charCodeAt(0); //48
-
return String.fromCharCode(ch);
-
},
-
/**
-
* Get Yenc Decoded CharCode
-
* @param {Integer} Character Code
-
* @return {Integer} New Character Code
-
*/
-
decodeChar: function(character)
-
{
-
var result;
-
-
if (character != 61)
-
{
-
result = specialChar?character-106:character-42;
-
specialChar = false;
-
}
-
else
-
{
-
specialChar = true;
-
}
-
return result;
-
}
-
};


Diesen Eintrag kommentieren