Magic Lantern Forum

Developing Magic Lantern => Reverse Engineering => Topic started by: Maqs on April 07, 2014, 01:21:08 PM

Title: NameService - a key (null-terminated string)/value (pointer/int) store
Post by: Maqs on April 07, 2014, 01:21:08 PM
Hello,

the "NameService" is used by different parts of the firmware. One well-known place for this would be register_function/call.

register_function calls NameService_Lookup to check whether a function is already registered or not. If it's registered, it frees the memory used by the old entry and allocates new memory. At the end, NameService_Register is called to register the new function.
unregister_function calls NameService_Lookup and NameService_Unregister in that order. Besides the calls to the callback function, call(..) only calls NameService_Lookup.

None of the NameService functions deal with allocating or freeing memory for the stored data.

Below, you will find a quick overview over the structures and a few functions I found, some of them untested.

Code: [Select]
struct NameServiceEntry
{
    struct NameServiceEntry *next;
    void *data;
    char Name[];
};

struct NameService
{
    char *aNameService;
    int number_of_entries;
    int semaphore;
    int hash_table_size;
    void *ptr1;   // ptr to ptr1
    void *ptr2;   // ptr to ptr2
    struct NameServiceEntry *hash_table[hash_table_size];
};

// name will be used for the named semaphore created, can be zero if create_semaphore is 0
// if create_semaphore, creates (and uses) a semaphore
struct NameService *NameService_Create(char *name, int hash_table_size, int create_semaphore);

// Free NameService structures (not the data!).
// 0 on success
int NameService_Destroy(struct NameService *n);

// (Over)Write named entry.
// Will overwrite existing entries silently without free()ing it! Use NameService_Lookup first and free data.
// 0 on success
int NameService_Register(struct NameService *n, char *name, void *data);

// Remove named entry.
// 0 on success
int NameService_Unregister(struct NameService *n, char *name);

// Lookup named entry.
// 0 on success
int NameService_Lookup(struct NameService *n, char *name, void **data);     // 0 on success, data points to entry

// Calls callback for each NameService entry. This is useful for freeing all the data.
// 0 on success
int NameService_ForEach(struct NameService *n, void (*callback)(char *name, void *data, void *priv), void *priv);

// 0 if n is a NameService
int NameService_IsInstance(struct NameService *n);

// Internal hash function used by NameService.
int NameService_HashFunction(char *name);

Stubs for 6D.113:
Code: [Select]
FF32CD20 NameService_Create
FF32D304 NameService_Destroy
FF32D114 NameService_Register
FF32D124 NameService_Unregister
FF32D2EC NameService_Lookup
FF32D480 NameService_IsInstance
FF32D554 NameService_ForEach
FF32D5F4 NameService_HashFunction
Title: Re: NameService - a key (null-terminated string)/value (pointer/int) store
Post by: g3gg0 on April 07, 2014, 07:26:17 PM
thanks :)
looks like C++ code (1st parameter is this)
Title: Re: NameService - a key (null-terminated string)/value (pointer/int) store
Post by: nanomad on April 08, 2014, 08:57:32 AM
Most of Canon high level code has a C++ feeling, with this being the first parameter. I don't think it's actual C++ though