#include "frames.hpp" #include #include namespace frames { int parse_u32(const char* p,std::size_t n,std::uint32_t* out) { if (!p || !out || n==0) return ARG; bool overflow=false; std::uint32_t value=0; for (std::size_t i=0;i(p[i]); if (c<'0' || c>'9') return ARG; unsigned digit=c-'0'; if (!overflow) { if (value>(std::numeric_limits::max()-digit)/10) overflow=true; else value=value*10+digit; } } if (overflow) return RANGE; *out=value; return OK; } int tokenize(const char* p,std::size_t n,std::vector* out) { if (!p || !out || n>8192) return ARG; for (std::size_t i=0;i(p[i]); if (c!=' ' && c!='\t' && !(c>='!' && c<='~')) return ARG; } std::vector result; std::size_t cursor=0; while (cursor& values={}) { std::ostringstream out; out << "{\"rc\":" << rc << ",\"out\":["; for (std::size_t i=0;i tokens; if (tokenize(p,n,&tokens)!=OK || tokens.empty()) return response(ARG); const std::string command=tokens.front(); unsigned required=0; if (command=="RESET" || command=="STATS") required=0; else if (command=="DUP" || command=="CLOSE" || command=="VIEW" || command=="ACTOR") required=1; else if (command=="NEW" || command=="LEAVE" || command=="CANCEL" || command=="WAIT") required=2; else if (command=="ENTER" || command=="REQUEST") required=3; else return response(ARG); if (tokens.size()!=required+1) return response(ARG); std::vector args(required); for (unsigned i=0;i1 || args[1]<1 || args[1]>MAX_DEPTH) return response(ARG); Attr attr; attr_mode(&attr,int(args[0])); attr_limit(&attr,args[1]); int h=-1; int rc=create(b,&attr,&h); return rc==OK ? response(rc,{std::uint64_t(h)}) : response(rc); } if (command=="ACTOR") { if (args[0]>7) return response(ARG); ActorView value{}; int rc=actor_view(b,int(args[0]),&value); return rc==OK ? response(rc,{value.held_objects,value.held_frames,value.pending_objects}) : response(rc); } if (args[0]>=MAX_HANDLES) return response(ARG); int h=int(args[0]); if (command=="DUP") { int next=-1; int rc=duplicate(b,h,&next); return rc==OK ? response(rc,{std::uint64_t(next)}) : response(rc); } if (command=="CLOSE") return response(close(b,h)); if (command=="VIEW") { View v{}; int rc=view(b,h,&v); return rc==OK ? show(v) : response(rc); } if (command=="WAIT") { WaitView w{}; int rc=queued(b,h,args[1],&w); return rc==OK ? response(rc,{std::uint64_t(w.actor),w.note}) : response(rc); } if (args[1]>7) return response(ARG); int actor=int(args[1]); if (command=="LEAVE") return response(leave(b,h,actor)); if (command=="CANCEL") return response(cancel(b,h,actor)); if (command=="ENTER") return response(enter(b,h,actor,args[2])); return response(request(b,h,actor,args[2])); } }