Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Thursday, July 21, 2011

Base64 Encoding and Decoding in C

 
Base64 is a method of encoding arbitrary data as plain ASCII text. It is one of the techniques employed by the MIME standard to send data other than plain text. Base64 encoding takes three bytes, each consisting of eight bits, and represents them as four printable characters in the ASCII standard. It does that in essentially two steps. 

The first step is to convert three bytes to four numbers of six bits. Base64 only uses 6 bits. The 64 characters are 10 digits (0 to 9), 26 lowercase characters (a to z), 26 uppercase characters (A to Z) as well as '+' and '/'.

For example, the three bytes are 155, 162 and 233, the corresponding bit stream is 100110111010001011101001, which in turn corresponds to the 6-bit values 38, 58, 11 and 41.


These numbers are converted to ASCII characters in the second step using the Base64 encoding table. The 6-bit values of our example translate to the ASCII sequence "m6Lp".
  • 155 -> 10011011
  • 162 -> 10100010
  • 233 -> 11101001
  • 100110 -> 38
  • 111010 -> 58
  • 001011 -> 11
  • 101001 -> 41
  • 38 -> m
  • 58 -> 6
  • 11 -> L
  • 41 -> p
This two-step process is applied to the whole sequence of bytes that are encoded. If the size of the original data in bytes is a multiple of three, everything works fine. If it is not, we might end up with one or two 8-bit bytes. For proper encoding, we have to append enough bytes with a value of '0' to create a 3-byte group. The Base64 uses '=' as a padding character.

Sample C Program

The following program in C can be used for Base64 Encoding and Decoding.



--
Sam

Friday, May 28, 2010

writing Bug free C code


The C language provides a minimal, efficient framework. A key to writing bug-free code is learning from your mistakes. Designing a module right the first time saves time. Programming without a goal is like a sailboat without a sail. You drift. Never reinvent the wheel. Always code what to do, not how to do it. As a general rule, try to keep functions under one page.


Always review changes before checking source code back in. An API must be designed right the first time because changes to an API are costly. Before you can write bug-free code, you must have a bug-free, rock-solid base. Provide a code wrapper around all system calls. Use macros as an aid to porting so that your code base does not change at all. Try to hide how something works to provide an abstraction that aids.

Macro names should be in uppercase. Macros beginning with an underscore are to be used only in other macros, not explicitly in source code. New data types should be in uppercase. New data types must be declared with a typedef statement, not a macro definition. Variables should be named using Hungarian notation. Hungarian notation allows you to know a variable's data type without seeing the data declaration. Functions should be named using the module/verb/noun convention. Think twice and do once.

    If I had eight hours to chop down a tree, 
    I'd spend six hours sharpening my axe.  -- AbrahamLincoln

    --
    Sam

    Tuesday, May 26, 2009

    Validate XML against XSD in C



    The XML documents can reference schema documents that specify how the XML documents should be structured. The rules for validating the XML document is specified in the schema which use the extension .xsd For more information about W3C XML Schema click here

    In this Tutorial I'll show how to use libxml2 to Validate XML documents.

    Libxml2 is the XML C parser and toolkit developed for the Gnome project (but usable outside of the Gnome platform), it is free software available under the MIT License. XML itself is a meta language to design markup languages, i.e. text language where semantic and structure are added to the content using extra "markup" information enclosed between angle brackets. HTML is the most well-known markup language. Though the library is written in C a variety of language bindings make it available in other environments.

    Libxml2 is known to be very portable, the library should build and work without serious troubles on a variety of systems (Linux, Unix, Windows, CygWin, MacOS, MacOS X, RISC Os, OS/2, VMS, QNX, MVS, ...)

    Sample program in C to validate xml against xsd using libxml2

    Environment & Settings:
    * Install libxml2 2.6.32 binary [Installed by default in most of the Linux].

    * Install libxml2-dev 2.6.32 package.

    * IDE Eclipse CDT [GCC Compiler]

    * Right click the project --> Properties --> C/C++ Build --> Settings

    select Tool Settings --> GCC C Linker --> Libraries
    Add "xml2"(without quotes) in Libraries

    select GCC C Compiler --> Directories --> Include Paths
    Add "/usr/include/libxml2" (without quotes)

    * This sample program uses test.xml and test.xsd

    Code: xmlvalidation.c
    
    

    FYI

    you can also validate XML against XSD using the following command in linux.

    xmllint --noout --schema test.xsd test.xml

    --
    Sam

    How to Recover deleted sticky notes on windows

    Most of us take important notes and reminders on the "Sticky Notes" feature available in most of the windows operating syste...