Solved

Y64 Help needed Porting.

Posted on 2009-06-26
7
301 Views
Last Modified: 2012-05-07
I'm trying to port a Y64/Yahoo64 routine to Delphi problem is I'm having difficulty understanding the code I'm porting, I have several sources of this Y64 but none in Pascal/Delphi.

It's apparently similar to Mac64 which produces a similar string but it's 2 bytes longer.

If i can work out

                  $dest.=$yahoo64[ord($source[$in])>>2];                   
            $dest.=$yahoo64[((ord($source[$in])<<4)&0x30) | (ord($source[$in+1])>>4)];
            $dest.=$yahoo64[((ord($source[$in+1])<<2)&0x3C) | (ord($source[$in+2])>>6)];
            $dest.=$yahoo64[ord($source[$in+2])&0x3F];
            $in += 3;

How to covert that to Pascal I can finish the rest myself . or if anyone knows where y64 exists already in pascal save me a huge headache.

Thanks..
function y64($source)
{  $yahoo64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._"; 
	// something with 16 bytes not hex
	$inlen = 16;
	$in=0;
	// in este ce primim
	for(; $inlen >=3; $inlen -=3)
	{
			$dest.=$yahoo64[ord($source[$in])>>2]; 			
		$dest.=$yahoo64[((ord($source[$in])<<4)&0x30) | (ord($source[$in+1])>>4)]; 
		$dest.=$yahoo64[((ord($source[$in+1])<<2)&0x3C) | (ord($source[$in+2])>>6)]; 
		$dest.=$yahoo64[ord($source[$in+2])&0x3F]; 
		$in += 3;
	}	
	if($inlen>0)
	{
		// enters here :)
		$dest.=$yahoo64[ord($source[$in])>>2]; 
		$fragment = ((ord($source[$in])<<4)&0x30);
		if($inlen>1){$fragment |= (ord($source[$in+1])>>4);			}
		$dest.=$yahoo64[$fragment];
		if($inlen<2)
			{$dest.="-";}
		else
			{$dest.=$yahoo64[((ord($source[$in+1])<<2)&0x3c)]; }
		$dest.="-";
		
	}	
        
	
	return $dest;
}
 
or this routine...
 
function y64($source)
{ $yahoo64="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._";
// something with 16 bytes not hex
$inlen = 16;
$in=0;
// in este ce primim
for(; $inlen >=3; $inlen -=3)
{
$dest.=$yahoo64[ord($source[$in])>>2];
$dest.=$yahoo64[((ord($source[$in])<<4)&0x30) | (ord($source[$in+1])>>4)];
$dest.=$yahoo64[((ord($source[$in+1])<<2)&0x3C) | (ord($source[$in+2])>>6)];
$dest.=$yahoo64[ord($source[$in+2])&0x3F];
$in += 3;
}
if($inlen>0)
{
// enters here :)
$dest.=$yahoo64[ord($source[$in])>>2];
$fragment = ((ord($source[$in])<<4)&0x30);
if($inlen>1){$fragment |= (ord($source[$in+1])>>4); }
$dest.=$yahoo64[$fragment];
if($inlen<2)
{$dest.="-";}
else
{$dest.=$yahoo64[((ord($source[$in+1])<<2)&0x3c)]; }
$dest.="-";
 
}

Open in new window

0
Comment
Question by:tobjectpascal
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
7 Comments
 
LVL 4

Author Comment

by:tobjectpascal
ID: 24726444
Here's the C# version..

Might be easier to understand.

public static string to_y64(byte[] input) {
 
 /* raw bytes in quasi-big-endian order to base 64 string (NUL-terminated) */
 
 const string base64digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._";
 
 int inlen=input.Length;
 char[] temp = new char[24];
 int i=0,j=0;
 
 for (;inlen >= 3;inlen -= 3) {
  temp[i++] = base64digits[input[j] >> 2];
  temp[i++] = base64digits[((input[j] << 4) & 0x30) | (input[j+1] >> 4)];
  temp[i++] = base64digits[((input[j+1] << 2) & 0x3c) | (input[j+2] >> 6)];
  temp[i++] = base64digits[input[j+2] & 0x3f];
  j += 3;
 }
 
 if (inlen > 0) {
  int fragment;
  temp[i++] = base64digits[input[j] >> 2];
  fragment = (input[j] << 4) & 0x30;
  if (inlen > 1)
   fragment |= input[j+1] >> 4;
  temp[i++] = base64digits[fragment];
  temp[i++] = (inlen < 2) ? '-' : base64digits[(input[j+1] << 2) & 0x3c];
  temp[i++] = '-';
 }
 return GetString(temp);
}

Open in new window

0
 
LVL 4

Author Comment

by:tobjectpascal
ID: 24726912
yet another example.

static unsigned yahoobase64 (unsigned char *src, unsigned srclen, char *dst) {
	static const char *enc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		"abcdefghijklmnopqrstuvwxyz0123456789._";
	unsigned char *end = src + srclen;
	char *pdst = dst;
	unsigned x;
	for ( ; src < end; pdst += 4) 
		if (end - src >= 3) {
			x = (src [0] << 16) + (src [1] << 8) + src [2];
			pdst [3] = enc [x & 0x3f]; x >>= 6;
			pdst [2] = enc [x & 0x3f]; x >>= 6;
			pdst [1] = enc [x & 0x3f]; x >>= 6;
			pdst [0] = enc [x & 0x3f];
			src += 3;
			continue;
		}
		else if (end - src == 2) {
			x = (src [0] << 16) + (src [1] << 8);
			pdst [3] = '-'; x >>= 6;
			pdst [2] = enc [x & 0x3f]; x >>= 6;
			pdst [1] = enc [x & 0x3f]; x >>= 6;
			pdst [0] = enc [x & 0x3f];
			src += 2;
			continue;
		}
		else if (end - src == 1) {
			x = src [0] << 16;
			pdst [3] = '-'; x >>= 6;
			pdst [2] = '-'; x >>= 6;
			pdst [1] = enc [x & 0x3f]; x >>= 6;
			pdst [0] = enc [x & 0x3f];
			src += 1;
			continue;
		}
	*pdst = '\0';
	return pdst - dst;
}

Open in new window

0
 
LVL 4

Author Comment

by:tobjectpascal
ID: 24727266
It's C code lol
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Accepted Solution

by:
ThievingSix earned 500 total points
ID: 24733255
This should work although I can't do any debugging because I don't know what should be inputted and how it comes out. If this doesn't work the way it's supposed to give me a Input and what it should Output.
function YahooBase64(Source: PChar; SourceLen: DWORD): PChar;
const
  Enc : PChar = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._';
var
  pEnd : PChar;
  pDst : PChar;
  X : DWORD;
begin
  pEnd := Source + SourceLen;
  Result := AllocMem(24);
  pDst := Result;
  X := 0;
  While Source < pEnd Do
    begin
    If (pEnd - Source) >= 3 Then
      begin
      X := (Byte(Source[0]) SHL 16) + (Byte(Source[1]) SHL 8) + Byte(Source[2]);
      pDst[3] := Enc[X AND $3F];
      X := X SHR 6;
      pDst[2] := Enc[X AND $3F];
      X := X SHR 6;
      pDst[1] := Enc[X AND $3F];
      X := X SHR 6;
      pDst[0] := Enc[X AND $3F];
      Inc(Source,3);
      Continue;
    end
    Else If (pEnd - Source) = 2 Then
      begin
      X := (Byte(Source[0]) SHL 16) + (Byte(Source[1]) SHL 8);
      pDst[3] := '-';
      X := X SHR 6;
      pDst[2] := Enc[X AND $3F];
      X := X SHR 6;
      pDst[1] := Enc[X AND $3F];
      X := X SHR 6;
      pDst[0] := Enc[X AND $3F];
      Inc(Source,2);
      Continue;
    end
    Else If (pEnd - Source) = 1 Then
      begin
      X := Byte(Source[0]) SHL 16;
      pDst[3] := '-';
      X := X SHR 6;
      pDst[2] := '-';
      X := X SHR 6;
      pDst[1] := Enc[X AND $3F];
      X := X SHR 6;
      pDst[0] := Enc[X AND $3F];
      Inc(Source);
      Continue;
    end;
    Inc(pDst,4);
  end;
  pDst^ := #0;
  Dec(Source,SourceLen);
end;

Open in new window

0
 
LVL 4

Author Comment

by:tobjectpascal
ID: 24733415
wow thanks, you can have the points just for the effort I was hoping for a couple of lines of code but wow thank you :)

points are yours just for trying......
0
 
LVL 4

Author Closing Comment

by:tobjectpascal
ID: 31597085
10/10 for effort thanks :)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

687 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question