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 
Pages (2): « First < Previous 1 [2] Last »
MurgaLua Compiler
Author Message
Juergen
Member
***


Posts: 81
Group: Registered
Joined: May 2007
Status: Offline
Reputation: 0
Post: #11
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
JohnMurga
Administrator
*******


Posts: 381
Group: Administrators
Joined: Apr 2007
Status: Offline
Reputation: 2
Post: #12
RE: MurgaLua Compiler

Juergen Wrote:
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.


Thanks :-)

I'll try it out and replace my methods with this in the next build (along with an acknowledgment).

Cheers
JohnM

02-06-2008 08:21 AM
Visit this user's website Find all posts by this user Quote this message in a reply
iGame3D
Moderator
***


Posts: 231
Group: Moderators
Joined: Apr 2007
Status: Offline
Reputation: 0
Post: #13
MurgaLua Compiler Platforms

Can I compile for windows from a Mac?
I haven't tried yet.

02-06-2008 09:24 AM
Visit this user's website Find all posts by this user Quote this message in a reply
Juergen
Member
***


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

JohnMurga Wrote:

Juergen Wrote:
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.


Thanks :-)

I'll try it out and replace my methods with this in the next build (along with an acknowledgment).

Cheers
JohnM


I improved the hex2bin function a little. The hex2bin function now accepts every even numbered string, that contains valid characters ([0123456789aAbBcCdDeEfF]).

Juergen

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);
  lua_pushstring(L,"This isn't a valid hex string!");
  if (l1%2!=0) lua_error(L);
  while (l1--) {
    switch_case++;
    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=0; // everything is ok
         break;
       }
    }
    if (switch_case>0) { // we haven't found our letter
      if (switch_case>1) { //switching was fruitless -> exit
        lua_error(L);
      }

    lookup=(lookup==ll)?lu:ll; //switch lookup table
    l1++; //
    switch_case++; //try again
    }
  }
  luaL_pushresult(&b);
  return 1;
}

02-06-2008 09:29 AM
Find all posts by this user Quote this message in a reply
Juergen
Member
***


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

Juergen Wrote:
I improved the hex2bin function a little. The hex2bin function now accepts every even numbered string, that contains valid characters ([0123456789aAbBcCdDeEfF]).

Juergen

Another change. It looks now much simpler. Although it might be a little bit slower in some cases (lower case hex) and faster when used with upper case letters. But it is much more readable and also smaller.

Juergen

Code:
static int hex2bin(lua_State *L) {
  unsigned char found=0,i,j=0,obyte=0;
  size_t l1;
  char lookup[]="0123456789ABCDEF0123456789abcdef";
  const char *s1 = luaL_checklstring(L, 1, &l1);
  luaL_Buffer b;
  luaL_buffinit(L, &b);
  lua_pushstring(L,"This isn't a valid hex string!");
  if (l1%2!=0) lua_error(L);
  while (l1--) {
     found++;
     for(i=0;i<32;i++) {
       if (*s1==lookup[i]) {
         if (j==0) obyte=(i%16)<<4;
           else { obyte+=i%16; luaL_putchar(&b,(char)obyte); }
         j^=1;
         s1++;
         found--;
         break;
       }
    }
    if (found==1) lua_error(L);
  }
  luaL_pushresult(&b);
  return 1;
}

02-06-2008 08:14 PM
Find all posts by this user Quote this message in a reply
Pages (2): « First < Previous 1 [2] Last »
Post Reply  Post Thread 

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

Forum Jump: