Files

57 lines
3.5 KiB
C

#pragma once
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#include "current_realm_record.h"
#include "engine_shipped_main_header.h"
#define GW_NATIVE_ACCOUNT_NAME_RVA 0x574b3f8u
#define GW_NATIVE_ACCOUNT_RESERVE_RVA 0x1e5c60u
#define GW_NATIVE_ACCOUNT_SSO UINT64_C(0x800000000000000f)
struct gw_native_account_string {uint64_t pointer,size,capacity;unsigned char small[16];};
typedef int (*gw_account_name_writable)(void*,size_t,void*);
typedef int (*gw_account_name_reserve)(void*,size_t,void*);
/* Filename component, not a BattleTag: '#' would be truncated by the original
* getter. Never manufacture a different identity by sanitizing invalid bytes. */
static inline size_t gw_account_name_length(const char* name,size_t bytes){
size_t n=0;if(!name||!bytes)return 0;
while(n<bytes&&n<=32u&&name[n]){
unsigned char c=(unsigned char)name[n];
if(c<32u||c>126u||strchr("\\/:*?\"<>|#",c))return 0;
++n;
}
if(!n||n==bytes||n>32u||name[0]==' '||name[n-1]==' '||name[n-1]=='.')return 0;
return n;
}
static inline int gw_account_string_layout(const struct gw_native_account_string* s){
uint64_t cap=s->capacity&UINT64_C(0x7fffffffffffffff);
if(!s->pointer||cap>4096u||s->size>cap)return 0;
if(s->capacity>>63)return s->capacity==GW_NATIVE_ACCOUNT_SSO&&s->pointer==(uint64_t)(uintptr_t)s->small;
return s->pointer!=(uint64_t)(uintptr_t)s->small;
}
/* Client reserve owns any heap growth/free. The caller never installs a pointer
* to its own storage. Its source name must remain independent of that object. */
static inline int gw_assign_native_account_name(struct gw_native_account_string* s,const char* name,size_t bytes,
gw_account_name_reserve reserve,gw_account_name_writable writable,void* context){
size_t n=gw_account_name_length(name,bytes);
if(!n||!s||!writable||!writable(s,sizeof(*s),context)||!gw_account_string_layout(s)||
!writable((void*)(uintptr_t)s->pointer,(size_t)s->size+1u,context))return -1;
if(n>15u||s->capacity!=GW_NATIVE_ACCOUNT_SSO){
if(!reserve||reserve(s,n,context)||!gw_account_string_layout(s))return -1;
}
if((s->capacity&UINT64_C(0x7fffffffffffffff))<n||!writable((void*)(uintptr_t)s->pointer,n+1u,context))return -1;
memmove((void*)(uintptr_t)s->pointer,name,n);
((unsigned char*)(uintptr_t)s->pointer)[n]=0;s->size=n;return 0;
}
/* Pure fake-image-testable admission; no account-id/Battle.net fields touched. */
static inline int gw_bind_native_account_name(void* image,size_t image_bytes,void* owner,size_t owner_bytes,
uint32_t build,int realm_bound,int before_account_load,uint32_t address,const char* name,size_t bytes,
gw_account_name_reserve reserve,gw_account_name_writable writable,void* context){
uint64_t current;uint32_t observed,id;unsigned char* p=(unsigned char*)image;unsigned char* o=(unsigned char*)owner;
if(build!=70334u||!realm_bound||!before_account_load||!p||!o||owner_bytes<0x8a1u||!address||
image_bytes<GW_CURRENT_REALM_OWNER_RVA+8u||image_bytes<GW_NATIVE_ACCOUNT_NAME_RVA+sizeof(struct gw_native_account_string)||
!gw_engine_shipped_main_header_matches(image,image_bytes))return -1;
memcpy(&current,p+GW_CURRENT_REALM_OWNER_RVA,8);memcpy(&observed,o+0x898,4);memcpy(&id,o+0x89c,4);
if(current!=(uint64_t)(uintptr_t)owner||observed!=address||id!=(address&0xffffu)||!id||!o[0x8a0])return -1;
return gw_assign_native_account_name((struct gw_native_account_string*)(p+GW_NATIVE_ACCOUNT_NAME_RVA),name,bytes,reserve,writable,context);
}