Welcome to the Creatures Wiki! Log in and join the community.

Talk:MNG files

From Creatures Wiki
Revision as of 05:46, 8 February 2005 by Dylan (talk)
Jump to navigation Jump to search

Since we have a VB and assembler example, why not a C example? C is very popular, and all.

#include <stdlib.h>
#include <stdint.h>

uint8_t *scramble(uint8_t *data, uint32_t length)
{
    uint8_t hb  = 0x5;
    uint8_t *out = (uint8_t *) malloc(sizeof(uint8_t) * length);
    uint8_t i;


    for (i = 0; i < length; i++) {
        out[i] = data[i] ^ hb;
        if (hb < 0x3F)
            hb += 0xC1;
        else
            hb += (0xC1 - 0x100);
    }
    
    return out;
}

Does that code look alright? Dylan 16:55, 7 Feb 2005 (PST)

Since you're not using const, why not do it in-place? bd_ 18:48, 7 Feb 2005 (PST)

Not using in-place because I was trying to copy the VB example. Dylan 21:36, 7 Feb 2005 (PST)

#include <stdlib.h>
#include <stdint.h>

uint8_t *scramble(uint8_t *data, uint32_t length)
{
    uint8_t hb  = 0x5;
    uint8_t i;


    for (i = 0; i < length; i++) {
        data[i] ^= hb;
        if (hb < 0x3F)
            hb += 0xC1;
        else
            hb += (0xC1 - 0x100);
    }
    
    return out;
}

Here's without the uint8 stuff,and optimization. Dylan 21:36, 7 Feb 2005 (PST)

Updated Dylan 21:44, 7 Feb 2005 (PST)

#define MNG_START 0x5
#define MNG_STEP  0xC1

unsigned char *scramble(unsigned char *data, unsigned int length)
{
    unsigned char hb  = MNG_START;
    unsigned char *start = data;
    
    while (length--) {
        *data ^= hb;
        hb    += MNG_STEP;
        data++;
    }
    
    return start;
}