Working With Variable-Length Pointers in UPS Patches

The Delta Project is aimed at creating multimedia editing tools and other utilities for bringing to life the dreams and inspirations of game creators around the globe.

Working With Variable-Length Pointers in UPS Patches

Postby Simion32 » February 26th, 2013, 3:53 pm

You aren't expected to know how to use this. It's for curious programmers that happen to notice this topic and want to know how to decode one of these pesky UPS pointers.

I had a very difficult time figuring this thing out, due to the documentation on the UPS format being overly general.

So, here's the code, in all its glory (use as you wish, no credit required):

#include <cstddef>
#include <vector>
#define vector std::vector

typedef unsigned long long Hex64;
typedef unsigned char Hex08;

Hex64 decodeUPS(vector<Hex08>& data, size_t& current_address)
{
Hex64 ups = 0x0000000000000000LL;
Hex64 shift = 1;
while(true)
{
Hex08 x = (data[current_address++] & 0xFF);
ups += ((x & 0x7F) * shift);
if(x & 0x80) break;
shift <<= 7;
ups += shift;
}
return ups;
}

void encodeUPS(vector<Hex08>& data, Hex64 ups)
{
while(true)
{
Hex64 x = (ups & 0x000000000000007FLL);
ups >>= 7;
if(ups == 0)
{
data.push_back( (0x80 | x) & 0xFF );
break;
}
data.push_back( x & 0xFF );
ups--;
}
}

If you have any questions about the code, feel free to ask.
Sage of Discovery
Bananas received 332
Posts: 2738
Joined: 2008

Return to The Delta Project

Who is online

Users browsing this forum: No registered users and 2 guests