Galvanic copper plating

Abstract

Galvanization is a method of coating metal objects with layer of other metal. It can be useful to protect against corrosion or to create decorative shapes on metal objects. Chemical background of this process is difficult, but the idea is simple (forgive me my indolence) - use electricity to move a bit of metal to the coated object.

a coin coated with coper by using galvanization

This experiment should be done with carefully and away from fire! Explosive mixture of H2 and O2 is being created during this process.

Tools and materials

  • plastic plate,
  • one glass of warm water,
  • pinch of kitchen salt,
  • copper wire with removed isolation,
  • aluminum tea-spoon,
  • DC power supply (3-10V).

try/catch in C [PHP interpreter]

Abstract

PHP interpreter is written in C, but uses try/catch construct known from other popular languages. This approach is different from everyday C-like error handling, where it's done by using function return value or by modifying error variable passed as an argument to a function.

    zend_try {
        PG(during_request_startup) = 1;
        php_output_activate(TSRMLS_C);
       /* unimportant stuff */
    } zend_catch {
        retval = FAILURE;
    } zend_end_try();

If one of instructions in a "try" block raises an exception then the evaluation of this block is terminated and execution flow moves to a "catch" block.

Access to object's fields by using pointer arithmetic

Normally a method uses its object's field by using their name. It's presented below, where we want to get value of description field from getDescription method.

class Car {
    std::string getDescription() {
        return description; // <- we return value of this field
    }

    std::string description;
};

The same result can be obtained by using a bit of magic with this, pointers and casting:

#include <iostream>

class Car {
public:
    Car (int _weight, int _maxSpeed, std::string _description) :
        weight(_weight), maxSpeed(_maxSpeed), description(_description) {
    }
    std::string getDescription() {
        return description;
    }
    std::string getDescriptionUsingThisKeyword() {
        return *((std::string*)((int*)this + 2));
    } 
private:
    int weight, maxSpeed;
    std::string description;
};

int main() {
    Car myCar(200, 200, "my best car ever");
    std::cout << myCar.getDescription() << std::endl;
    std::cout << myCar.getDescriptionUsingThisKeyword() << std::endl;
}

Output:

bash-3.2$ ./a.out 
my best car ever
my best car ever

It's error-prone, e.g. when someone changes order of fields in the class. I think, that it's cool but I wouldn't like to see this in a real code :)

Images hardcoded in a source code [PHP interpreter]

Abstract

Recently I presented a simple way to protect an image against saving, today I will show how PHP interpreter protect some of its images against changing.

Image may be hardcoded in a regular array, where each field contains corresponding value of a byte in the original image file. You may imagine that you open image in a hex editor, you see its content as a chain of values of the bytes, and then you retype them as a fields in your array (this can be automatized of course).

Smarter strings in C

In C, strings are usually stored in variables with char* type, it's just container for characters, there isn't stored other data/metadata. This can be tedious, e.g. if we want to iterate thought string we need to obtain its length (e.g by invoke strlen() function on it) or carry its length in some additional variables.

This can be simplified by wrapping raw char* string in a simple structure. Its string's length has unsigned type, therefore mistakes with assigning negative values will be easier to find. Its first argument is its content (with char* type), so it can be just passed to functions, that requires regular char* arguments (although it will create warning, it's safe IMHO).

Here is implementation:
#include <stdio.h>
#include <string.h>

typedef struct string {
    char* data;
    unsigned length;
} string;

int main() {
    string myHome;
    myHome.data = "it's big and white";
    myHome.length = strlen(myHome.data);

    // now we don't have to check for size of string,
    // e.g. we don't have to use strlen() to iterate thought charcters
    int i;
    for (i = 0; i < myHome.length; i++) {
        printf("%c", myHome.data[i]);
    }
    printf("\n");

    // backward compatibility with char*
    printf("%s\n", myHome.data);
    printf("%s\n", myHome); // this will raise warning but is legal
}

It's a bit unpopular trick, but may be seen in snippets on some forums, also here is tutorial about similar implementation of such strings.

How to replace modulo operator by using bitwise AND operator?

Modulo operation on a and b returns a reminder from dividing a by b. It can be implemented by using bitwise AND operator if b is a power of two.

Following samples in C and Python illustrate it.

#include <stdio.h>

int main() {
    // 8(dec) = 1000(bin)
    // 7(dec) =  111(bin)
    printf("19 mod 8 (with %%) %d\n", 19 % 8);
    printf("19 mod 8 (with &) %d\n",  19 & 7);

    // 16(dec) = 10000(bin)
    // 15(dec) =  1111(bin)
    printf("16 mod 16 (with %%) %d\n", 16 % 16);
    printf("16 mod 16 (with &) %d\n",  16 & 15);
}

bash-3.2$ llvm-gcc modulo.c && ./a.out
19 mod 8 (with %) 3
19 mod 8 (with &) 3
16 mod 16 (with %) 0
16 mod 16 (with &) 0
bash-3.2$ python
Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) 
[GCC 4.2.1 (Apple Inc. build 5646)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 19 % 8
3
>>> 19 & 7
3
>>> 

Why do we need to decrease b?