News : The level of daily SPAM has reached insane proportions, all registrations are now manual. I ask you to send me an e-mail (john (at) murga (dot) org), to confirm that you want me to create an account for you.


Post Reply  Post Thread 
MurgaLua Compiler
Author Message
Juergen
Member
***


Posts: 81
Group: Registered
Joined: May 2007
Status: Offline
Reputation: 0
Post: #1
RE: MurgaLua Compiler

JohnMurga Wrote:
With some of these things I enjoy trying to write it in Lua ... And as it is I just knocked it as quickly as I could for simple 16 byte values, so it didn't matter much.

The Lua implementation is pretty trivial too :-)

PHP Code:
function decodeDataFromHex (myString)
    
local myFunction = function (someData)
        return 
base.string.char (base.tonumber(someData16 ))
    
end
    
return base.string.gsub(myString'(%x%x)'myFunction)
end

function encodeDataAsHex (myString)
    
local myFunction = function (someData)
        return (
'%02x'):format(someData:byte())
    
end
    
return base.string.gsub(myString'.'myFunction )
end 


But I'll change it to "C" for the next release.

Cheers
JohnM


Yes it looks pretty trivial, but it hides a lot of complexity. (and it sure isn't PHP ;-)

My I suggest the following implementations:

The bin2hex function cal also called with a optinal second string argument, that produces a lower case hex string when it is set to "l".

Code:
static int bin2hex (lua_State *L) {
  size_t l1; //,i=0;
  unsigned char ll[]="0123456789abcdef";
  unsigned char lu[]="0123456789ABCDEF";
  unsigned char *lookup=lu;
  const char *s1 = luaL_checklstring(L, 1, &l1);
  luaL_Buffer b;
  if (lua_isstring(L, 2)) lookup=((char ) *lua_tolstring(L,2,NULL)=='l')?ll:lu;
  luaL_buffinit(L, &b);
  while (l1--) {
    luaL_putchar(&b, lookup[((unsigned char) (*s1)>>4)%16]);
    luaL_putchar(&b, lookup[(unsigned char)(*s1++)%16]);
  }
  luaL_pushresult(&b);
  return 1;
}


The hex2bin function works with a upper or lower case string, but not with a mixed case string.

Code:
static int hex2bin(lua_State *L) {
  unsigned char switch_case=0,i,j=0,obyte=0;
  size_t l1;
  unsigned char ll[]="0123456789abcdef";
  unsigned char lu[]="0123456789ABCDEF";
  unsigned char *lookup=lu;
  const char *s1 = luaL_checklstring(L, 1, &l1);
  luaL_Buffer b;
  luaL_buffinit(L, &b);
  while (l1--) {
    switch_case|=1; // bit 1 -> switch, bit 2 -> already switched
    for(i=0;i<16;i++) {
       if ((unsigned char) *s1==lookup[i]) {
         obyte+=(j==0)?i<<4:i;
         if (j==1) { luaL_putchar(&b, (char) obyte); obyte=0; }
         j^=1;
         s1++;
         switch_case&=2; // no we do not have to switch the case
         break;
       }
    }
    if (switch_case&1) { // did we switch the case?
      if (switch_case&2) { //has the case been already been switched?
        lua_pushstring(L,"This isn't a valid hex string!");
        lua_error(L);
      }
      lookup=ll;
      l1++;
      switch_case|= 2; //we have switched the case
    }
  }
  luaL_pushresult(&b);
  return 1;
}


I'd like to ask the Kepler guys, if they include it in the md5 library, since they have the bin2hex version already (also in Lua). If they take it, it would make sense to use the version from the md5 library. Since the functionality is new, I think there aren't many users.

Juergen

This post was last modified: 02-06-2008 05:48 AM by Juergen.

02-06-2008 04:33 AM
Find all posts by this user Quote this message in a reply
Post Reply  Post Thread 

Messages In This Thread
MurgaLua Compiler - iGame3D - 01-31-2008, 09:54 PM
RE: MurgaLua Compiler - mikshaw - 02-01-2008, 12:00 AM
RE: MurgaLua Compiler - JohnMurga - 02-01-2008, 02:45 AM
RE: MurgaLua Compiler - JohnMurga - 02-01-2008, 02:50 AM
RE: MurgaLua Compiler - iGame3D - 02-01-2008, 08:05 AM
RE: MurgaLua Compiler - Juergen - 02-01-2008, 09:08 AM
RE: MurgaLua Compiler - JohnMurga - 02-01-2008, 09:38 AM
RE: MurgaLua Compiler - iGame3D - 02-01-2008, 03:26 PM
RE: MurgaLua Compiler - Juergen - 02-01-2008, 09:08 PM
RE: MurgaLua Compiler - JohnMurga - 02-01-2008, 11:47 PM
RE: MurgaLua Compiler - Juergen - 02-06-2008 04:33 AM
RE: MurgaLua Compiler - JohnMurga - 02-06-2008, 08:21 AM
MurgaLua Compiler Platforms - iGame3D - 02-06-2008, 09:24 AM
RE: MurgaLua Compiler - Juergen - 02-06-2008, 09:29 AM
RE: MurgaLua Compiler - Juergen - 02-06-2008, 08:14 PM

View a Printable Version
Send this Thread to a Friend
Subscribe to this Thread | Add Thread to Favorites

Forum Jump: