Files
2026-11-23 03:28:32 +02:00

125 lines
6.5 KiB
C

#pragma once
/* Owned copied world bundles only. Metadata uses the original independent path.
* No packet interpretation beyond the existing count/opcode/length framing. */
#include <stdint.h>
#include <stddef.h>
#include <string.h>
#define GW_WORLD_LOCATOR_MAX_RECORDS 4096u
#define GW_WORLD_LOCATOR_MAX_BYTES (16u*1024u*1024u)
#define GW_WORLD_LOCATOR_TIMEOUT_MS 30000u /* hotfix-served Spell rows can take >5 s to publish with the full Octo package */
struct gw_world_locator_bundle {
struct gw_world_locator_bundle* next;
uint32_t bytes,records,cursor;
};
struct gw_world_locator_fifo {
struct gw_world_locator_bundle *head,*tail;
uint32_t bundles,records,bytes;
};
typedef void* (*gw_world_locator_alloc)(void*,size_t);
typedef void (*gw_world_locator_free)(void*,void*);
static inline uint32_t gw_world_locator_u32(const unsigned char* p){
return (uint32_t)p[0]|((uint32_t)p[1]<<8)|((uint32_t)p[2]<<16)|((uint32_t)p[3]<<24);
}
static inline uint64_t gw_world_locator_u64(const unsigned char* p){
return gw_world_locator_u32(p)|((uint64_t)gw_world_locator_u32(p+4)<<32);
}
/* Source-only production enables the bounded first-known gate without an
* environment prerequisite. An explicit private0 remains a test override;
* identity always comes from authenticated op45, never environment text. */
static inline int gw_world_locator_config(const char* option,size_t option_bytes,int production_default){
if(!option_bytes)return production_default?1:0;
if(option&&option_bytes==1u&&option[0]=='0')return 0;
return option&&option_bytes==1u&&option[0]=='1'?1:-1;
}
static inline int gw_world_locator_expired(uint64_t started,uint64_t now){
return now-started>=GW_WORLD_LOCATOR_TIMEOUT_MS;
}
/* Decoder of our appended Core getter ABI, never a native known-packet parser. */
static inline int gw_world_locator_spell_ids(const unsigned char* body,uint32_t bytes,uint64_t expected_guid,uint32_t* ids,uint32_t* count){
uint32_t n,index,previous=0;
if(!body||!ids||!count||bytes<12u||bytes>12u+4096u*4u||gw_world_locator_u64(body)!=expected_guid)return -1;
n=gw_world_locator_u32(body+8);if(n>4096u||bytes!=12u+n*4u)return -1;
for(index=0;index<n;++index){
uint32_t id=gw_world_locator_u32(body+12u+index*4u);
if(!id||id>INT32_MAX||(index&&id<=previous))return -1;
ids[index]=id;previous=id;
}
*count=n;return 0;
}
/* Existing DBReply serializer output, inspected locally and never submitted.
* A Source-known canonical removal is a consistency failure, not permission
* to hide the Source ID or wait indefinitely. Unknown/baked IDs need no gate. */
static inline int gw_world_locator_registry_status(const unsigned char* body,uint32_t bytes,uint32_t spell){
uint32_t status;
if(!body||bytes<17u||bytes>1048576u||gw_world_locator_u32(body)!=0xe111669eu||gw_world_locator_u32(body+4)!=spell||
(body[12]&31u)||gw_world_locator_u32(body+13)!=bytes-17u)return -1;
status=body[12]>>5;
if(status==1u)return bytes>17u?1:-1;
return status==3u?0:-1;
}
static inline int gw_world_locator_bundle_shape(const unsigned char* body,uint32_t bytes,uint32_t* records){
uint32_t at=4,count,index;
if(!body||bytes<4||bytes>1048576u||!records)return -1;
count=gw_world_locator_u32(body);if(count>1024u)return -1;
for(index=0;index<count;++index){
uint32_t size;
if(bytes-at<8u)return -1;
size=gw_world_locator_u32(body+at+4);at+=8;
if(size>bytes-at)return -1;
at+=size;
}
if(at!=bytes)return -1;
*records=count;return 0;
}
static inline int gw_world_locator_fifo_append(struct gw_world_locator_fifo* fifo,const unsigned char* body,uint32_t bytes,gw_world_locator_alloc allocate,void* context){
struct gw_world_locator_bundle* node;uint32_t records;
if(!fifo||!allocate||gw_world_locator_bundle_shape(body,bytes,&records))return -1;
if(!records)return 0;
if(fifo->bundles>=GW_WORLD_LOCATOR_MAX_RECORDS||fifo->records>GW_WORLD_LOCATOR_MAX_RECORDS||fifo->bytes>GW_WORLD_LOCATOR_MAX_BYTES||records>GW_WORLD_LOCATOR_MAX_RECORDS-fifo->records||bytes>GW_WORLD_LOCATOR_MAX_BYTES-fifo->bytes)return -2;
node=(struct gw_world_locator_bundle*)allocate(context,sizeof(*node)+(size_t)bytes);
if(!node)return -3;
node->next=0;node->bytes=bytes;node->records=records;node->cursor=4;
memcpy(node+1,body,bytes);
if(fifo->tail)fifo->tail->next=node;else fifo->head=node;
fifo->tail=node;++fifo->bundles;fifo->records+=records;fifo->bytes+=bytes;
return 0;
}
static inline void gw_world_locator_fifo_pop(struct gw_world_locator_fifo* fifo,gw_world_locator_free release,void* context){
struct gw_world_locator_bundle* node=fifo->head;
if(!node)return;
fifo->head=node->next;if(!fifo->head)fifo->tail=0;
--fifo->bundles;fifo->records-=node->records;fifo->bytes-=node->bytes;
release(context,node);
}
static inline void gw_world_locator_fifo_clear(struct gw_world_locator_fifo* fifo,gw_world_locator_free release,void* context){
while(fifo->head)gw_world_locator_fifo_pop(fifo,release,context);
}
/* Same immutable PE identity/section contract as the accepted owned r3 reader.
* Full artifact SHA remains the existing launch/profile admission obligation. */
static inline int gw_world_locator_main_header(const unsigned char* body,size_t bytes,uintptr_t base){
uint32_t pe,opt,table,image_size,index;uint16_t sections,optional_bytes;int text=0;
if(!body||bytes!=4096u||base!=UINT64_C(0x140000000)||body[0]!='M'||body[1]!='Z')return 0;
pe=gw_world_locator_u32(body+0x3c);
if(pe<0x40u||pe>bytes-512u||memcmp(body+pe,"PE\0\0",4))return 0;
if((body[pe+4]|((unsigned)body[pe+5]<<8))!=0x8664u||gw_world_locator_u32(body+pe+8)!=1791563511u)return 0;
sections=(uint16_t)(body[pe+6]|((unsigned)body[pe+7]<<8));
optional_bytes=(uint16_t)(body[pe+20]|((unsigned)body[pe+21]<<8));opt=pe+24u;
if((body[opt]|((unsigned)body[opt+1]<<8))!=0x20bu||optional_bytes<112u||gw_world_locator_u64(body+opt+24)!=base)return 0;
image_size=gw_world_locator_u32(body+opt+56);
if(image_size!=139165696u||0x79c3d70u+0x288u>image_size||gw_world_locator_u32(body+opt+60)!=0x400u)return 0;
table=opt+optional_bytes;
if(!sections||sections>=64u||table>bytes||sections>(bytes-table)/40u)return 0;
for(index=0;index<sections;++index){
uint32_t at=table+index*40u;
if(!memcmp(body+at,".text\0\0\0",8)){
if(text||gw_world_locator_u32(body+at+8)!=75236444u||gw_world_locator_u32(body+at+12)!=0x1000u||!(gw_world_locator_u32(body+at+36)&0x20000000u))return 0;
text=1;
}
}
return text;
}