55 lines
2.6 KiB
C
55 lines
2.6 KiB
C
/* Pure bounded request partitioning. No engine, account, socket or allocator.
|
|
* Each callback receives one complete original CMSG_HOTFIX_REQUEST body.
|
|
* Validate the entire input before emitting; preserve builds and push IDs.
|
|
*/
|
|
#ifndef GW_HOTFIX_REQUEST_SPLIT_H
|
|
#define GW_HOTFIX_REQUEST_SPLIT_H
|
|
#include <stdint.h>
|
|
#define GW_HOTFIX_REQUEST_MAX_PUSHES 4096u
|
|
typedef int (*gw_hotfix_emit)(void*,const unsigned char*,uint32_t);
|
|
static uint32_t gw_hotfix_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);
|
|
}
|
|
/* Sorted membership index: O(n log n) worst case, 16.5 KiB bounded stack.
|
|
* Emission still walks the original request, retaining first-occurrence order. */
|
|
#ifndef GW_HOTFIX_SPLIT_LESS
|
|
#define GW_HOTFIX_SPLIT_LESS(a,b) ((a)<(b))
|
|
#endif
|
|
static void gw_hotfix_sift(uint32_t* ids,uint32_t root,uint32_t count){
|
|
while(root<count/2){
|
|
uint32_t child=root*2+1,swap;
|
|
if(child+1<count&&GW_HOTFIX_SPLIT_LESS(ids[child],ids[child+1]))++child;
|
|
if(!GW_HOTFIX_SPLIT_LESS(ids[root],ids[child]))break;
|
|
swap=ids[root];ids[root]=ids[child];ids[child]=swap;root=child;
|
|
}
|
|
}
|
|
static void gw_hotfix_sort(uint32_t* ids,uint32_t count){
|
|
uint32_t i;
|
|
for(i=count/2;i>0;--i)gw_hotfix_sift(ids,i-1,count);
|
|
for(i=count;i>1;--i){uint32_t swap=ids[0];ids[0]=ids[i-1];ids[i-1]=swap;gw_hotfix_sift(ids,0,i-1);}
|
|
}
|
|
static int gw_hotfix_split_request(const unsigned char* body,uint32_t size,gw_hotfix_emit emit,void* context){
|
|
uint32_t count,index,previous;unsigned char request[16];
|
|
uint32_t sorted[GW_HOTFIX_REQUEST_MAX_PUSHES],seen[GW_HOTFIX_REQUEST_MAX_PUSHES/32]={0};
|
|
if(!body||!emit||size<12)return -1;
|
|
count=gw_hotfix_u32(body+8);
|
|
if(gw_hotfix_u32(body)!=70334u||count>GW_HOTFIX_REQUEST_MAX_PUSHES||size!=12u+4u*count)return -1;
|
|
if(!count)return emit(context,body,size);
|
|
for(index=0;index<count;++index)sorted[index]=gw_hotfix_u32(body+12+4*index);
|
|
gw_hotfix_sort(sorted,count);
|
|
for(index=0;index<8;++index)request[index]=body[index];
|
|
request[8]=1;request[9]=request[10]=request[11]=0;
|
|
for(index=0;index<count;++index){
|
|
const unsigned char* push=body+12+4*index;
|
|
uint32_t value=gw_hotfix_u32(push),lo=0,hi=count,bit;
|
|
while(lo<hi){uint32_t mid=lo+(hi-lo)/2;if(GW_HOTFIX_SPLIT_LESS(sorted[mid],value))lo=mid+1;else hi=mid;}
|
|
bit=1u<<(lo%32);
|
|
if(seen[lo/32]&bit)continue;
|
|
seen[lo/32]|=bit;
|
|
for(previous=0;previous<4;++previous)request[12+previous]=push[previous];
|
|
{int result=emit(context,request,sizeof(request));if(result)return result;}
|
|
}
|
|
return 0;
|
|
}
|
|
#endif
|