Murga-Projects Forums

Full Version: MurgaLua Compiler
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

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

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

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

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;
}

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;
}

Pages: 1 2
Reference URL's