From a20f8eb015a0bd493083dae222f75ea6ca2c3842 Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Mon, 8 Sep 2025 12:06:31 +0200 Subject: [PATCH 1/8] feat: added author info --- articles/index.html | 28 +++++++++++++--------------- articles/server_c.c | 34 +++++++++++++++++----------------- 2 files changed, 30 insertions(+), 32 deletions(-) diff --git a/articles/index.html b/articles/index.html index 1053099f..e33d7997 100644 --- a/articles/index.html +++ b/articles/index.html @@ -29,7 +29,7 @@ article h1{margin:.25rem 0 .5rem;font-size:1.6rem} .spinner{width:16px;height:16px;border:2px solid #ccc;border-top-color:#333;border-radius:50%;display:inline-block;animation:spin 1s linear infinite} @keyframes spin{to{transform:rotate(360deg)}} .reading header nav{display:none} -.reading #delBtn,.reading #dateInfo{display:none} +.reading #delBtn{display:none}
@@ -60,8 +60,11 @@ article h1{margin:.25rem 0 .5rem;font-size:1.6rem} + + + -
Drop image here or choose…
+
Choose image…
@@ -88,11 +91,12 @@ article h1{margin:.25rem 0 .5rem;font-size:1.6rem} const loading=$('#loading'); const rT=$('#readTitle'), rB=$('#readBody'), rTh=$('#readThumb'), dt=$('#dateInfo'); const toList=$('#toList'), toNew=$('#toNew'), back=$('#backBtn'), del=$('#delBtn'); - const title=$('#title'), content=$('#content'); + const title=$('#title'), author=$('#author'), content=$('#content'); const tDrop=$('#thumbDrop'), tPrev=$('#thumbPrev'), tFile=$('#thumbFile'); const insImg=$('#insImg'), insVid=$('#insVid'), imgFile=$('#imgFile'), vidFile=$('#vidFile'); const saveBtn=$('#saveBtn'), cancelBtn=$('#cancelBtn'); const KEY='articles14k'; + const PH="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 180 120'%3E%3Crect width='100%25' height='100%25' fill='%23eee'/%3E%3C/svg%3E"; let selId=null; let API=null; let preList; // cache first list to avoid duplicate initial fetches const AC={}; function setLoading(on,msg){ if(!loading) return; if(msg) loading.querySelector('.msg').textContent=msg; loading.classList.toggle('hidden',!on); } @@ -150,7 +154,7 @@ article h1{margin:.25rem 0 .5rem;font-size:1.6rem} finally{ setLoading(false); } } if(!a.length){listV.innerHTML='
No articles yet. Click New to create one.
';return} - listV.innerHTML=a.map(x=>`
thumb

${esc(x.title)}

`).join(''); + listV.innerHTML=a.map(x=>`
thumb

${esc(x.title)}

`).join(''); } function esc(s){return (s||'').replace(/[&<>]/g,m=>({"&":"&","<":"<",">":">"}[m]))} @@ -159,14 +163,12 @@ article h1{margin:.25rem 0 .5rem;font-size:1.6rem} function fileToDataURL(f){return new Promise((res,rej)=>{const r=new FileReader(); r.onload=()=>res(r.result); r.onerror=rej; r.readAsDataURL(f)})} function dataURLToBlob(dataURL){ try{ const [h,b]=dataURL.split(','); const m=h.match(/data:([^;]+)(;base64)?/); if(!m) return null; const mime=m[1]; const isB64=!!m[2]; const raw=isB64? atob(b): decodeURIComponent(b); const arr=new Uint8Array(raw.length); for(let i=0;i{e.preventDefault(); zone.style.background='#f7f7f7'}); zone.addEventListener('dragleave',()=>zone.style.background=''); zone.addEventListener('drop',e=>{e.preventDefault(); zone.style.background=''; const fs=[...(e.dataTransfer.files||[])]; fs.forEach(cb)});} - // Thumb drag/drop or file select - dropHandler(tDrop, f=>{ if(!f.type.startsWith('image/')) return; fileToDataURL(f).then(u=>{tPrev.src=u})}); + // Thumbnail via file chooser only tFile.addEventListener('change',e=>{const f=e.target.files[0]; if(f) fileToDataURL(f).then(u=>tPrev.src=u)}); tDrop.addEventListener('click',()=>tFile.click()); - // Content drag/drop insert and paste + // Content paste (keep), no drag & drop content.addEventListener('paste',e=>{const it=(e.clipboardData||{}).items||[]; for(const x of it){const f=x.getAsFile&&x.getAsFile(); if(!f) continue; e.preventDefault(); if(f.type.startsWith('image/')) { // Insert a placeholder while uploading const ph=imgEl(''); ph.alt='image'; ph.style.minHeight='120px'; ph.style.background='#f0f0f0'; insertAtSel(ph); @@ -174,10 +176,6 @@ article h1{margin:.25rem 0 .5rem;font-size:1.6rem} } else if(f.type.startsWith('video/')) { fileToDataURL(f).then(u=>{const v=videoEl(u); insertAtSel(v)}); } } }); - dropHandler(content, f=>{ if(f.type.startsWith('image/')) { - const ph=imgEl(''); ph.alt='image'; ph.style.minHeight='120px'; ph.style.background='#f0f0f0'; insertAtSel(ph); - (async()=>{ const url=await uploadBlobAndGetURL(f); if(url){ ph.removeAttribute('style'); ph.src=url; } else { fileToDataURL(f).then(u=>{ ph.removeAttribute('style'); ph.src=u; }); } })(); - } else if(f.type.startsWith('video/')) { fileToDataURL(f).then(u=>{const v=videoEl(u); insertAtSel(v)}) } }); // Insert buttons insImg.onclick=()=>{ imgFile.onchange=async e=>{ const f=e.target.files&&e.target.files[0]; if(!f) return; const ph=imgEl(''); ph.alt='image'; ph.style.minHeight='120px'; ph.style.background='#f0f0f0'; insertAtSel(ph); const url=await uploadBlobAndGetURL(f); if(url){ ph.removeAttribute('style'); ph.src=url; } else { fileToDataURL(f).then(u=>{ ph.removeAttribute('style'); ph.src=u; }); } }; imgFile.click(); }; @@ -190,7 +188,7 @@ article h1{margin:.25rem 0 .5rem;font-size:1.6rem} let th=tPrev.src||''; if(!th){const fi=content.querySelector('img'); if(fi) th=fi.src} // If thumbnail is data URL, try uploading to separate file so it loads separately if(th && th.startsWith('data:')){ const blob=dataURLToBlob(th); if(blob){ const u=await uploadBlobAndGetURL(blob); if(u) th=u; } } - const obj={title:title.value.trim()||'Untitled', body:content.innerHTML, thumb:th||''}; + const obj={title:title.value.trim()||'Untitled', author:author.value.trim(), body:content.innerHTML, thumb:th||''}; setLoading(true,'Saving…'); await API.create(obj); selId=null; clearEditor(); @@ -199,12 +197,12 @@ article h1{margin:.25rem 0 .5rem;font-size:1.6rem} show(listV); }; - function clearEditor(){ title.value=''; content.innerHTML=''; tPrev.removeAttribute('src') } + function clearEditor(){ title.value=''; author.value=''; content.innerHTML=''; tPrev.removeAttribute('src') } cancelBtn.onclick=()=>{selId=null; clearEditor(); show(listV)}; // List click -> read (ensure full focus by switching view) listV.addEventListener('click',async e=>{const card=e.target.closest('.card'); if(!card) return; const id=card.getAttribute('data-id'); const c=AC[id]; if(c){ openRead(c); return; } setLoading(true,'Loading article…'); try{ const a=await API.get(id); if(!a) return; AC[id]=a; openRead(a); } finally { setLoading(false); }}); - function openRead(a){ selId=a.id; rTh.loading='lazy'; rTh.decoding='async'; rTh.src=a.thumb||''; rT.textContent=a.title||''; rB.innerHTML=a.body||''; rB.querySelectorAll('img').forEach(im=>{ im.loading='lazy'; im.decoding='async'; }); dt.textContent=new Date(a.createdAt||Date.now()).toLocaleString(); show(readV) } + function openRead(a){ selId=a.id; rTh.loading='lazy'; rTh.decoding='async'; rTh.src=a.thumb||''; rT.textContent=a.title||''; rB.innerHTML=a.body||''; rB.querySelectorAll('img').forEach(im=>{ im.loading='lazy'; im.decoding='async'; }); const d=new Date(a.createdAt||Date.now()).toLocaleString(); dt.textContent=(a.author? a.author+' · ': '')+d; show(readV) } // Delete del.onclick=async()=>{ if(!selId) return; setLoading(true,'Deleting…'); await API.remove(selId); delete AC[selId]; selId=null; await renderList(); setLoading(false); show(listV) }; diff --git a/articles/server_c.c b/articles/server_c.c index 6e775f3c..85bf5920 100644 --- a/articles/server_c.c +++ b/articles/server_c.c @@ -112,16 +112,16 @@ static char* json_get_top_string(const char* json, const char* key){ } // Build object JSON string; caller frees -static char* build_article_json(const char* id, const char* title, const char* body, const char* thumb, long long createdAt, long long updatedAt){ - char *et=json_escape(title?title:""), *eb=json_escape(body?body:""), *eth=json_escape(thumb?thumb:""); - if(!et||!eb||!eth){ free(et); free(eb); free(eth); return NULL; } +static char* build_article_json(const char* id, const char* title, const char* author, const char* body, const char* thumb, long long createdAt, long long updatedAt){ + char *et=json_escape(title?title:""), *eau=json_escape(author?author:""), *eb=json_escape(body?body:""), *eth=json_escape(thumb?thumb:""); + if(!et||!eau||!eb||!eth){ free(et); free(eau); free(eb); free(eth); return NULL; } char createdBuf[64]; snprintf(createdBuf,sizeof(createdBuf),"%lld",createdAt); char updated[96]=""; if(updatedAt>0){ snprintf(updated,sizeof(updated),",\"updatedAt\":%lld",updatedAt); } - size_t need = strlen(id)+strlen(et)+strlen(eb)+strlen(eth)+strlen(createdBuf)+strlen(updated)+64; + size_t need = strlen(id)+strlen(et)+strlen(eau)+strlen(eb)+strlen(eth)+strlen(createdBuf)+strlen(updated)+80; char* out=malloc(need); - if(!out){ free(et); free(eb); free(eth); return NULL; } - snprintf(out, need, "{\"id\":\"%s\",\"title\":\"%s\",\"body\":\"%s\",\"thumb\":\"%s\",\"createdAt\":%s%s}", id, et, eb, eth, createdBuf, updated); - free(et); free(eb); free(eth); return out; + if(!out){ free(et); free(eau); free(eb); free(eth); return NULL; } + snprintf(out, need, "{\"id\":\"%s\",\"title\":\"%s\",\"author\":\"%s\",\"body\":\"%s\",\"thumb\":\"%s\",\"createdAt\":%s%s}", id, et, eau, eb, eth, createdBuf, updated); + free(et); free(eau); free(eb); free(eth); return out; } static char* gen_id(){ char* out=malloc(17); if(!out) return NULL; unsigned int r = (unsigned int)rand(); long long t=now_ms(); snprintf(out,17,"%08x%08x", (unsigned int)(t&0xffffffff), r); return out; } @@ -215,7 +215,7 @@ static int rewrite_articles_map(char** out_json_updated, const char* match_id, c // collect objects size_t len=strlen(t); size_t i=1; int depth=0; size_t start=0; size_t count=0, cap=8; char** objs=malloc(cap*sizeof(char*)); bool found=false; char* updated_copy=NULL; for(; ibl+1){ const char* id=path+bl+1; char* obj=find_article_by_id(id); if(!obj){ send_response(c,404,"Not Found","application/json","",0,true); return;} // migrate this object if needed - char* title=json_get_string(obj, "title"); char* body_s=json_get_string(obj, "body"); char* thumb=json_get_string(obj, "thumb"); long long createdAt=json_get_number(obj, "\"createdAt\""); int obj_changed=0; + char* title=json_get_string(obj, "title"); char* author=json_get_string(obj, "author"); char* body_s=json_get_string(obj, "body"); char* thumb=json_get_string(obj, "thumb"); long long createdAt=json_get_number(obj, "\"createdAt\""); int obj_changed=0; if(thumb && strncmp(thumb, "data:",5)==0){ char* mime=NULL; unsigned char* bytes=NULL; size_t bl2=0; if(parse_data_url(thumb,&mime,&bytes,&bl2)==0){ const char* ext=ext_from_mime(mime); char* saved=save_bytes_with_ext(bytes,bl2,ext); if(saved){ free(thumb); size_t urlL=strlen(saved)+2; thumb=malloc(urlL); snprintf(thumb,urlL,"/%s",saved); free(saved); obj_changed=1; } free(mime); free(bytes); } } bool bchanged=false; char* new_body=migrate_inline_images_in_body(body_s,&bchanged); if(new_body && bchanged){ free(body_s); body_s=new_body; obj_changed=1; } else { free(new_body); } - if(obj_changed){ char* id_copy=json_get_string(obj, "id"); char* updated=build_article_json(id_copy?id_copy:"", title?title:"", body_s?body_s:"", thumb?thumb:"", createdAt, 0); // persist + if(obj_changed){ char* id_copy=json_get_string(obj, "id"); char* updated=build_article_json(id_copy?id_copy:"", title?title:"", author?author:"", body_s?body_s:"", thumb?thumb:"", createdAt, 0); // persist if(updated){ rewrite_articles_map(NULL, id_copy, updated, false); free(updated); } free(id_copy); - free(obj); obj=build_article_json(json_get_string("",""), title?title:"", body_s?body_s:"", thumb?thumb:"", createdAt, 0); // rebuild for response + free(obj); obj=build_article_json(json_get_string("",""), title?title:"", author?author:"", body_s?body_s:"", thumb?thumb:"", createdAt, 0); // rebuild for response // Above line uses placeholder; simpler: rebuild from previously updated string parsing is heavy; instead send the updated we built // But rewrite_articles_map returned updated; safer to just re-find free(obj); obj=find_article_by_id(id); } - free(title); free(body_s); free(thumb); + free(title); free(author); free(body_s); free(thumb); size_t L=strlen(obj); send_response(c,200,"OK","application/json",obj,L,true); free(obj); return; } } else if(!strcmp(method,"POST") && !strcmp(path, base)){ From f8de65cb202c7460bb236d91802e045d18136e02 Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Mon, 8 Sep 2025 12:08:10 +0200 Subject: [PATCH 2/8] chore: removed unecessary text --- articles/index.html | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/articles/index.html b/articles/index.html index e33d7997..7660e692 100644 --- a/articles/index.html +++ b/articles/index.html @@ -72,12 +72,11 @@ article h1{margin:.25rem 0 .5rem;font-size:1.6rem} -
+
- Stored locally or on server if available.
@@ -168,7 +167,6 @@ article h1{margin:.25rem 0 .5rem;font-size:1.6rem} tFile.addEventListener('change',e=>{const f=e.target.files[0]; if(f) fileToDataURL(f).then(u=>tPrev.src=u)}); tDrop.addEventListener('click',()=>tFile.click()); - // Content paste (keep), no drag & drop content.addEventListener('paste',e=>{const it=(e.clipboardData||{}).items||[]; for(const x of it){const f=x.getAsFile&&x.getAsFile(); if(!f) continue; e.preventDefault(); if(f.type.startsWith('image/')) { // Insert a placeholder while uploading const ph=imgEl(''); ph.alt='image'; ph.style.minHeight='120px'; ph.style.background='#f0f0f0'; insertAtSel(ph); From 3379064a381dbeb78955f0f8e701a57eeffe5bb4 Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Mon, 8 Sep 2025 13:19:18 +0200 Subject: [PATCH 3/8] fix: cpplint and clang tidy fixes --- articles/Makefile | 6 + articles/cppcheck.txt | 479 ++++++++++++++++++++++++++++++++++++++++++ articles/server_c.c | 277 +++++++++++++++++------- 3 files changed, 687 insertions(+), 75 deletions(-) create mode 100644 articles/cppcheck.txt diff --git a/articles/Makefile b/articles/Makefile index 31151109..cf61d8f7 100644 --- a/articles/Makefile +++ b/articles/Makefile @@ -7,6 +7,12 @@ all: server_c server_c: server_c.c $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) +lint: + @echo "Running clang-tidy..." + @clang-tidy -checks=-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling server_c.c -- -std=gnu11 $(CFLAGS) $(LDFLAGS) || true + @echo "Running cppcheck..." + @cppcheck --enable=all --inconclusive --std=c11 --language=c --quiet --inline-suppr --check-level=exhaustive --suppress=missingIncludeSystem server_c.c || true + build: minify minify: diff --git a/articles/cppcheck.txt b/articles/cppcheck.txt new file mode 100644 index 00000000..2b07c1d1 --- /dev/null +++ b/articles/cppcheck.txt @@ -0,0 +1,479 @@ +server_c.c:2:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem] +#include +^ +server_c.c:3:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem] +#include +^ +server_c.c:4:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem] +#include +^ +server_c.c:5:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem] +#include +^ +server_c.c:6:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem] +#include +^ +server_c.c:7:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem] +#include +^ +server_c.c:8:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem] +#include +^ +server_c.c:9:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem] +#include +^ +server_c.c:10:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem] +#include +^ +server_c.c:11:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem] +#include +^ +server_c.c:12:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem] +#include +^ +server_c.c:13:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem] +#include +^ +server_c.c:14:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem] +#include +^ +server_c.c:15:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem] +#include +^ +server_c.c:16:0: information: Include file: not found. Please note: Cppcheck does not need standard library headers to get proper results. [missingIncludeSystem] +#include +^ +server_c.c:0:0: information: Limiting analysis of branches. Use --check-level=exhaustive to analyze all branches. [normalCheckLevelMaxBranches] + +^ +server_c.c:411:77: warning: Either the condition 'elen<8' is redundant or the array 'ext[4]' is accessed at index 7, which is out of bounds. [arrayIndexOutOfBoundsCond] + size_t elen=0; while(ext[elen] && elen<8 && isalnum((unsigned char)ext[elen])) elen++; char* ext_safe=strndup_local(ext,elen?elen:3); if(!ext_safe){ send_response(c,500,"Internal Server Error","application/json","",0,true); return; } + ^ +server_c.c:411:45: note: Assuming that condition 'elen<8' is not redundant + size_t elen=0; while(ext[elen] && elen<8 && isalnum((unsigned char)ext[elen])) elen++; char* ext_safe=strndup_local(ext,elen?elen:3); if(!ext_safe){ send_response(c,500,"Internal Server Error","application/json","",0,true); return; } + ^ +server_c.c:411:77: note: Array index out of bounds + size_t elen=0; while(ext[elen] && elen<8 && isalnum((unsigned char)ext[elen])) elen++; char* ext_safe=strndup_local(ext,elen?elen:3); if(!ext_safe){ send_response(c,500,"Internal Server Error","application/json","",0,true); return; } + ^ +server_c.c:411:31: style: Array index 'elen' is used before limits check. [arrayIndexThenCheck] + size_t elen=0; while(ext[elen] && elen<8 && isalnum((unsigned char)ext[elen])) elen++; char* ext_safe=strndup_local(ext,elen?elen:3); if(!ext_safe){ send_response(c,500,"Internal Server Error","application/json","",0,true); return; } + ^ +server_c.c:314:129: style: Condition 'tcontent[0]=='['' is always true [knownConditionTrueFalse] + char* tcontent=ltrim_dup(content); if(tcontent && tcontent[0]=='['){ size_t clen=strlen(tcontent); if(clen>=2 && tcontent[0]=='[' && tcontent[clen-1]==']'){ + ^ +server_c.c:314:66: note: Assuming that condition 'tcontent[0]=='['' is not redundant + char* tcontent=ltrim_dup(content); if(tcontent && tcontent[0]=='['){ size_t clen=strlen(tcontent); if(clen>=2 && tcontent[0]=='[' && tcontent[clen-1]==']'){ + ^ +server_c.c:314:129: note: Condition 'tcontent[0]=='['' is always true + char* tcontent=ltrim_dup(content); if(tcontent && tcontent[0]=='['){ size_t clen=strlen(tcontent); if(clen>=2 && tcontent[0]=='[' && tcontent[clen-1]==']'){ + ^ +server_c.c:463:6: warning: inconclusive: Width 1023 given in format string (no. 2) is smaller than destination buffer 'path[4096]'. [invalidScanfFormatWidth_smaller] + if(sscanf(buf, "%15s %1023s", method, path) < 2){ close(c); return; } + ^ +server_c.c:289:37: error: Common realloc mistake: 'objs' nulled but not freed upon failure [memleakOnRealloc] + if(count==cap){ cap*=2; objs=realloc(objs, cap*sizeof(char*)); } + ^ +server_c.c:369:39: error: Common realloc mistake: 'objs' nulled but not freed upon failure [memleakOnRealloc] + if(count==cap){ cap*=2; objs=realloc(objs, cap*sizeof(char*)); } + ^ +server_c.c:42:136: warning: If memory allocation fails, then there is a possible null pointer dereference: data [nullPointerOutOfMemory] +static int write_file_all(const char* path, const char* data, size_t len){ FILE* f=fopen(path,"wb"); if(!f) return -1; size_t n=fwrite(data,1,len,f); fclose(f); return n==len?0:-1; } + ^ +server_c.c:312:59: note: Assuming allocation function fails + size_t L=strlen(obj); size_t tot=L+2; char* arr=malloc(tot+1); size_t w=0; arr[w++]='['; memcpy(arr+w,obj,L); w+=L; arr[w++]=']'; arr[w]='\0'; write_file_all(file, arr, w); free(arr); + ^ +server_c.c:312:59: note: Assignment 'arr=malloc(tot+1)', assigned value is 0 + size_t L=strlen(obj); size_t tot=L+2; char* arr=malloc(tot+1); size_t w=0; arr[w++]='['; memcpy(arr+w,obj,L); w+=L; arr[w++]=']'; arr[w]='\0'; write_file_all(file, arr, w); free(arr); + ^ +server_c.c:312:169: note: Calling function 'write_file_all', 2nd argument 'arr' value is 0 + size_t L=strlen(obj); size_t tot=L+2; char* arr=malloc(tot+1); size_t w=0; arr[w++]='['; memcpy(arr+w,obj,L); w+=L; arr[w++]=']'; arr[w]='\0'; write_file_all(file, arr, w); free(arr); + ^ +server_c.c:42:105: note: Assuming condition is false +static int write_file_all(const char* path, const char* data, size_t len){ FILE* f=fopen(path,"wb"); if(!f) return -1; size_t n=fwrite(data,1,len,f); fclose(f); return n==len?0:-1; } + ^ +server_c.c:42:136: note: Null pointer dereference +static int write_file_all(const char* path, const char* data, size_t len){ FILE* f=fopen(path,"wb"); if(!f) return -1; size_t n=fwrite(data,1,len,f); fclose(f); return n==len?0:-1; } + ^ +server_c.c:64:21: warning: If memory allocation fails, then there is a possible null pointer dereference: p [nullPointerOutOfMemory] + while((p = strstr(p, qkey))){ + ^ +server_c.c:358:187: note: Assuming allocation function fails + for(; i (ssize_t)header_bytes ? (size_t)total - header_bytes : 0; + ^ +server_c.c:355:47: style: Variable 'L2' is assigned a value that is never used. [unreadVariable] + if(t[0] != '['){ free(file); size_t L2=2; send_response(c,200,"OK","application/json","[]",2,true); free(t); return; } + ^ +server_c.c:355:45: style: Variable 'L2' is assigned a value that is never used. [unreadVariable] + if(t[0] != '['){ free(file); size_t L2=2; send_response(c,200,"OK","application/json","[]",2,true); free(t); return; } + ^ +server_c.c:76:93: warning: If memory allocation fails, then there is a possible null pointer dereference: json [ctunullpointerOutOfMemory] +static long long json_get_number(const char* json, const char* key){ const char* p = strstr(json, key); if(!p) return 0; const char* colon=strchr(p,':'); if(!colon) return 0; const char* v=colon+1; while(*v==' '||*v=='\t') v++; return atoll(v); } + ^ +server_c.c:358:187: note: Assuming allocation function fails + for(; i=0?0:-1; } + ^ +nofile:0:0: information: Active checkers: 117/966 (use --checkers-report= to see details) [checkersReport] + diff --git a/articles/server_c.c b/articles/server_c.c index 85bf5920..b991794f 100644 --- a/articles/server_c.c +++ b/articles/server_c.c @@ -23,7 +23,7 @@ static const char *DOC_ROOT = NULL; // current working directory static void on_sigint(int sig){ (void)sig; g_stop = 1; } -static long long now_ms(){ struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); return (long long)ts.tv_sec*1000 + ts.tv_nsec/1000000; } +static long long now_ms(void){ struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); return (long long)ts.tv_sec*1000 + ts.tv_nsec/1000000; } static const char* getenv_default(const char* k, const char* def){ const char* v=getenv(k); return v&&*v? v: def; } @@ -32,16 +32,16 @@ static int ensure_dir(const char* path){ struct stat st; if(stat(path,&st)==0){ static char* path_join(const char* a, const char* b){ size_t la=strlen(a), lb=strlen(b); size_t n=la+1+lb+1; char* r=malloc(n); if(!r) return NULL; snprintf(r,n,"%s/%s",a,b); return r; } -static char* data_dir(){ const char* env = getenv("ARTICLES_DATA_DIR"); if(env && *env){ ensure_dir(env); return strdup(env); } char* d = path_join(DOC_ROOT?DOC_ROOT: ".", "data"); if(d){ ensure_dir(d); } return d; } +static char* data_dir(void){ const char* env = getenv("ARTICLES_DATA_DIR"); if(env && *env){ ensure_dir(env); return strdup(env); } char* d = path_join(DOC_ROOT?DOC_ROOT: ".", "data"); if(d){ ensure_dir(d); } return d; } -static char* data_file(){ char* d = data_dir(); if(!d) return NULL; char* f = path_join(d, "articles.json"); free(d); return f; } +static char* data_file(void){ char* d = data_dir(); if(!d) return NULL; char* f = path_join(d, "articles.json"); free(d); return f; } // Read entire file into memory (NUL-terminated). Caller frees. static char* read_file_all(const char* path, size_t* out_len){ FILE* f=fopen(path,"rb"); if(!f){ if(out_len) *out_len=0; return NULL; } fseek(f,0,SEEK_END); long sz=ftell(f); if(sz<0) sz=0; fseek(f,0,SEEK_SET); char* buf=malloc((size_t)sz+1); if(!buf){ fclose(f); return NULL; } size_t n=fread(buf,1,(size_t)sz,f); fclose(f); buf[n]='\0'; if(out_len) *out_len=n; return buf; } static int write_file_all(const char* path, const char* data, size_t len){ FILE* f=fopen(path,"wb"); if(!f) return -1; size_t n=fwrite(data,1,len,f); fclose(f); return n==len?0:-1; } -static int append_file_line(const char* path, const char* line){ FILE* f=fopen(path,"ab"); if(!f) return -1; size_t n=fwrite(line,1,strlen(line),f); n+=fwrite("\n",1,1,f); fclose(f); return (int)n>=0?0:-1; } +// removed unused append_file_line (lint) // ---- JSON helpers (minimal) ---- static char* json_escape(const char* s){ size_t n=0; for(const char* p=s; *p; ++p){ if(*p=='"'||*p=='\\'||*p=='\n'||*p=='\r'||*p=='\t') n+=2; else n++; } @@ -64,13 +64,19 @@ static char* json_get_string(const char* json, const char* key){ while((p = strstr(p, qkey))){ const char* colon = strchr(p + klen, ':'); if(!colon){ p += klen; continue; } const char* v = colon + 1; while(*v==' '||*v=='\t') v++; - if(*v!='"'){ if(tmp) free(tmp); return strdup(""); } + if(*v!='"'){ + if(tmp) free(tmp); + return strdup(""); + } v++; // inside string char* out = malloc(strlen(v)+1); if(!out){ if(tmp) free(tmp); return strdup(""); } size_t w=0; bool esc=false; for(const char* q=v; *q; ++q){ char c=*q; if(esc){ if(c=='"'||c=='\\'||c=='/'){ out[w++]=c; } else if(c=='n'){ out[w++]='\n'; } else if(c=='r'){ out[w++]='\r'; } else if(c=='t'){ out[w++]='\t'; } else { out[w++]=c; } esc=false; continue; } if(c=='\\'){ esc=true; continue; } if(c=='"'){ out[w]='\0'; if(tmp) free(tmp); return out; } out[w++]=c; } - free(out); if(tmp) free(tmp); return strdup(""); + free(out); + if(tmp) free(tmp); + return strdup(""); } - if(tmp) free(tmp); return strdup(""); + if(tmp) free(tmp); + return strdup(""); } static long long json_get_number(const char* json, const char* key){ const char* p = strstr(json, key); if(!p) return 0; const char* colon=strchr(p,':'); if(!colon) return 0; const char* v=colon+1; while(*v==' '||*v=='\t') v++; return atoll(v); } @@ -88,19 +94,34 @@ static char* json_get_top_string(const char* json, const char* key){ if(*p=='}' || !*p) break; if(*p!='"'){ // invalid // skip token to next comma or end - while(*p && *p!=',' && *p!='}') p++; - if(*p==',') { p++; continue; } else break; + while(*p && *p!=',' && *p!='}') { + p++; + } + if(*p==',') { + p++; + continue; + } else { + break; + } } // parse key string p++; const char* ks=p; size_t klen=0; bool esc=false; for(; *p; ++p){ char c=*p; if(esc){ esc=false; continue; } if(c=='\\'){ esc=true; continue; } if(c=='"'){ break; } klen++; } - const char* key_start=ks; const char* key_end=p; if(*p=='"') p++; while(*p==' '||*p=='\t') p++; if(*p!=':') { // malformed - while(*p && *p!=',' && *p!='}') p++; if(*p==',') { p++; continue; } else break; + const char* key_start=ks; if(*p=='"') p++; while(*p==' '||*p=='\t') p++; if(*p!=':') { // malformed + while(*p && *p!=',' && *p!='}') { + p++; + } + if(*p==',') { + p++; + continue; + } else { + break; + } } p++; while(*p==' '||*p=='\t') p++; int key_match = (klen==keylen) && (strncmp(key_start, key, keylen)==0); if(*p=='"'){ p++; // read string value - char* out = malloc(strlen(p)+1); if(!out) return strdup(""); size_t w=0; bool e=false; for(; *p; ++p){ char c=*p; if(e){ if(c=='"'||c=='\\'||c=='/'){ out[w++]=c; } else if(c=='n'){ out[w++]='\n'; } else if(c=='r'){ out[w++]='\r'; } else if(c=='t'){ out[w++]='\t'; } else { out[w++]=c; } e=false; continue; } if(c=='\\'){ e=true; continue; } if(c=='"'){ out[w]='\0'; p++; break; } out[w++]=c; } + char* out = malloc(strlen(p)+1); if(!out) return strdup(""); size_t w=0; bool e=false; for(; *p; ++p){ char c=*p; if(e){ if(c=='"'||c=='\\'||c=='/'){ out[w++]=c; } else if(c=='n'){ out[w++]='\n'; } else if(c=='r'){ out[w++]='\r'; } else if(c=='t'){ out[w++]='\t'; } else { out[w++]=c; } e=false; continue; } if(c=='\\'){ e=true; continue; } if(c=='"'){ out[w]='\0'; p++; break; } out[w++]=c; } if(key_match){ return out; } else { free(out); } } else { // non-string value: skip until comma or end (simple) @@ -124,48 +145,144 @@ static char* build_article_json(const char* id, const char* title, const char* a free(et); free(eau); free(eb); free(eth); return out; } -static char* gen_id(){ char* out=malloc(17); if(!out) return NULL; unsigned int r = (unsigned int)rand(); long long t=now_ms(); snprintf(out,17,"%08x%08x", (unsigned int)(t&0xffffffff), r); return out; } +static char* gen_id(void){ char* out=malloc(17); if(!out) return NULL; unsigned int r = (unsigned int)rand(); long long t=now_ms(); snprintf(out,17,"%08x%08x", (unsigned int)(t&0xffffffff), r); return out; } // ---- data URL handling ---- static int b64val(int c){ if(c>='A'&&c<='Z') return c-'A'; if(c>='a'&&c<='z') return c-'a'+26; if(c>='0'&&c<='9') return c-'0'+52; if(c=='+') return 62; if(c=='/') return 63; return -1; } -static unsigned char* base64_decode(const char* s, size_t len, size_t* out_len){ size_t pad=0; if(len>=1 && s[len-1]=='=') pad++; if(len>=2 && s[len-2]=='=') pad++; size_t outcap = (len/4)*3 - pad; unsigned char* out = malloc(outcap+1); if(!out) return NULL; size_t w=0; int val=0, valb=-8; for(size_t i=0;i=0){ out[w++] = (unsigned char)((val>>valb)&0xFF); valb-=8; } } if(out_len) *out_len=w; return out; } +static unsigned char* base64_decode(const char* s, size_t len, size_t* out_len){ + size_t pad=0; + if(len>=1 && s[len-1]=='=') pad++; + if(len>=2 && s[len-2]=='=') pad++; + size_t groups = len/4; + size_t outcap = groups*3; + if(pad <= outcap) outcap -= pad; else outcap = 0; + if(outcap==0) outcap=1; // avoid 0-byte malloc and make room for NUL + unsigned char* out = malloc(outcap+1); + if(!out) return NULL; + size_t w=0; int val=0, valb=-8; + for(size_t i=0;i=0){ out[w++] = (unsigned char)((val>>valb)&0xFF); valb-=8; } + } + if(out_len) *out_len=w; + return out; +} static int parse_data_url(const char* data_url, char** out_mime, unsigned char** out_bytes, size_t* out_len){ const char* p=data_url; if(strncmp(p,"data:",5)!=0) return -1; p+=5; const char* semi=strchr(p,';'); const char* comma=strchr(p,','); if(!comma) return -1; char* mime=NULL; int is_b64=0; if(semi && semioutcap){ outcap=w+L+1; out=realloc(out,outcap); if(!out) return NULL; } memcpy(out+w,p,L); w+=L; break; } +static char* migrate_inline_images_in_body(const char* body, bool* changed){ + if(!body) return NULL; + const char* p = body; + size_t outcap = strlen(body) + 1; + char* out = malloc(outcap); + if(!out) return NULL; + size_t w = 0; int did = 0; + while(*p){ + const char* m = strstr(p, "src=\""); + const char* m2 = strstr(p, "src='"); + const char* hit = NULL; char quote='\0'; + if(m && (!m2 || m < m2)){ hit = m; quote='\"'; } + else if(m2){ hit = m2; quote='\''; } + if(!hit){ + size_t L = strlen(p); + if(w + L + 1 > outcap){ + size_t newcap = w + L + 1; + void* tmp = realloc(out, newcap); + if(!tmp){ free(out); return NULL; } + out = tmp; + } + memcpy(out + w, p, L); + w += L; + break; + } // copy up to src= - size_t prefix_len=(size_t)(hit - p); if(w+prefix_len+6>outcap){ outcap=w+prefix_len+64; out=realloc(out,outcap); if(!out) return NULL; } memcpy(out+w,p,prefix_len); w+=prefix_len; memcpy(out+w, "src=\"", 5); w+=5; // normalize to double quote - const char* url_start = hit + 5; if(quote=='\''){ url_start = hit + 5; } - if(*url_start=='\''||*url_start=='\"') url_start++; - const char* url_end=strchr(url_start, quote=='\''?'\'':'\"'); if(!url_end){ // malformed; copy rest - size_t L=strlen(url_start); if(w+L+1>outcap){ outcap=w+L+1; out=realloc(out,outcap); if(!out) return NULL; } memcpy(out+w,url_start,L); w+=L; break; } - size_t url_len=(size_t)(url_end - url_start); - if(url_len>5 && !strncmp(url_start, "data:",5)){ - char* mime=NULL; unsigned char* bytes=NULL; size_t bl=0; char* urlbuf=strndup(url_start,url_len); - if(parse_data_url(urlbuf,&mime,&bytes,&bl)==0){ const char* ext=ext_from_mime(mime); char* saved=save_bytes_with_ext(bytes,bl,ext); if(saved){ const char* rel=saved; const char* prefix="/"; size_t need=strlen(prefix)+strlen(rel); if(w+need+1>outcap){ outcap=w+need+64; out=realloc(out,outcap); if(!out){ free(saved); free(bytes); free(mime); free(urlbuf); return NULL; } } - out[w++]='/'; memcpy(out+w, rel, strlen(rel)); w+=strlen(rel); did=1; free(saved); } + size_t prefix_len = (size_t)(hit - p); + if(w + prefix_len + 6 > outcap){ + size_t newcap = w + prefix_len + 64; + void* tmp = realloc(out, newcap); + if(!tmp){ free(out); return NULL; } + out = tmp; outcap = newcap; + } + memcpy(out + w, p, prefix_len); + w += prefix_len; + memcpy(out + w, "src=\"", 5); + w += 5; // normalize to double quote + const char* url_start = hit + 5; + if(*url_start=='\'' || *url_start=='"') url_start++; + const char endq = (quote=='\'') ? '\'' : '"'; + const char* url_end = strchr(url_start, endq); + if(!url_end){ // malformed; copy rest + size_t L = strlen(url_start); + if(w + L + 1 > outcap){ + size_t newcap = w + L + 1; + void* tmp = realloc(out, newcap); + if(!tmp){ free(out); return NULL; } + out = tmp; + } + memcpy(out + w, url_start, L); + w += L; + break; + } + size_t url_len = (size_t)(url_end - url_start); + if(url_len>5 && strncmp(url_start, "data:", 5)==0){ + char* mime = NULL; unsigned char* bytes = NULL; size_t bl = 0; char* urlbuf = strndup(url_start, url_len); + if(parse_data_url(urlbuf,&mime,&bytes,&bl)==0){ + const char* ext = ext_from_mime(mime); + char* saved = save_bytes_with_ext(bytes, bl, ext); + if(saved){ + const char* rel = saved; + size_t need = 1 + strlen(rel); + if(w + need + 1 > outcap){ + size_t newcap = w + need + 64; + void* tmp = realloc(out, newcap); + if(!tmp){ free(saved); free(bytes); free(mime); free(urlbuf); free(out); return NULL; } + out = tmp; outcap = newcap; + } + out[w++] = '/'; + memcpy(out + w, rel, strlen(rel)); + w += strlen(rel); + did = 1; + free(saved); + } } free(bytes); free(mime); free(urlbuf); } else { - if(w+url_len+1>outcap){ outcap=w+url_len+64; out=realloc(out,outcap); if(!out) return NULL; } memcpy(out+w,url_start,url_len); w+=url_len; + if(w + url_len + 1 > outcap){ + size_t newcap = w + url_len + 64; + void* tmp = realloc(out, newcap); + if(!tmp){ free(out); return NULL; } + out = tmp; outcap = newcap; + } + memcpy(out + w, url_start, url_len); + w += url_len; } // closing quote - if(w+1>outcap){ outcap=w+64; out=realloc(out,outcap); if(!out) return NULL; } - out[w++]='"'; + if(w + 1 > outcap){ + size_t newcap = w + 64; + void* tmp = realloc(out, newcap); + if(!tmp){ free(out); return NULL; } + out = tmp; outcap = newcap; + } + out[w++] = '"'; p = url_end + 1; } - out[w]='\0'; if(changed) *changed=did; return out; } + out[w] = '\0'; + if(changed) *changed = did; + return out; +} // ---- HTTP helpers ---- -static void send_str(int c, const char* s){ send(c, s, strlen(s), 0); } +// removed unused send_str (lint) static void send_response(int c, int code, const char* status, const char* ctype, const char* body, size_t blen, bool cors){ char head[SMALL_BUF]; @@ -182,24 +299,14 @@ static const char* guess_mime(const char* path){ const char* ext=strrchr(path,'. // ---- Data operations (NDJSON-style internal), exposed as JSON array ---- static char* ltrim_dup(const char* s){ while(*s && isspace((unsigned char)*s)) s++; return strdup(s); } -static char* list_articles_json(size_t* out_len){ - char* file=data_file(); if(!file){ return strdup("[]"); } - size_t n=0; char* content=read_file_all(file,&n); free(file); - if(!content||n==0){ free(content); if(out_len) *out_len=2; return strdup("[]"); } - char* t=ltrim_dup(content); free(content); - if(!t){ if(out_len) *out_len=2; return strdup("[]"); } - // If already a JSON array, return as-is - if(t[0]=='['){ if(out_len) *out_len=strlen(t); return t; } - // Otherwise, treat as empty - free(t); if(out_len) *out_len=2; return strdup("[]"); -} +// removed unused list_articles_json (lint) static char* find_article_by_id(const char* id){ char* file=data_file(); if(!file) return NULL; size_t n=0; char* content=read_file_all(file,&n); free(file); if(!content) return NULL; char* t=ltrim_dup(content); free(content); if(!t) return NULL; if(t[0] != '['){ free(t); return NULL; } // iterate objects - size_t len=strlen(t); size_t i=1; int depth=0; size_t start=0; for(; i=2 && tcontent[0]=='[' && tcontent[clen-1]==']'){ + char* tcontent=ltrim_dup(content); free(content); if(tcontent && tcontent[0]=='['){ size_t clen=strlen(tcontent); if(clen>=2 && tcontent[clen-1]==']'){ // insert at front - bool empty = (clen==2); - size_t Lobj=strlen(obj); - size_t newlen = 1 + Lobj + (empty?0:1) + (clen-2) + 1; - char* out=malloc(newlen+1); size_t w=0; out[w++]='['; memcpy(out+w,obj,Lobj); w+=Lobj; if(!empty){ out[w++]=','; memcpy(out+w, tcontent+1, clen-2); w+=(clen-2); } out[w++]=']'; out[w]='\0'; write_file_all(file, out, w); free(out); + bool empty = (clen==2); + size_t Lobj=strlen(obj); + size_t newlen = 1 + Lobj + (empty?0:1) + (clen-2) + 1; + char* out=malloc(newlen+1); if(out){ size_t w=0; out[w++]='['; memcpy(out+w,obj,Lobj); w+=Lobj; if(!empty){ out[w++]=','; memcpy(out+w, tcontent+1, clen-2); w+=(clen-2); } out[w++]=']'; out[w]='\0'; write_file_all(file, out, w); free(out); } } else { // corrupt; overwrite - size_t L=strlen(obj); size_t tot=L+2; char* arr=malloc(tot+1); size_t w=0; arr[w++]='['; memcpy(arr+w,obj,L); w+=L; arr[w++]=']'; arr[w]='\0'; write_file_all(file, arr, w); free(arr); + size_t L=strlen(obj); size_t tot=L+2; char* arr=malloc(tot+1); if(arr){ size_t w=0; arr[w++]='['; memcpy(arr+w,obj,L); w+=L; arr[w++]=']'; arr[w]='\0'; write_file_all(file, arr, w); free(arr); } } } else { // not an array; overwrite - size_t L=strlen(obj); size_t tot=L+2; char* arr=malloc(tot+1); size_t w=0; arr[w++]='['; memcpy(arr+w,obj,L); w+=L; arr[w++]=']'; arr[w]='\0'; write_file_all(file, arr, w); free(arr); + size_t L=strlen(obj); size_t tot=L+2; char* arr=malloc(tot+1); if(arr){ size_t w=0; arr[w++]='['; memcpy(arr+w,obj,L); w+=L; arr[w++]=']'; arr[w]='\0'; write_file_all(file, arr, w); free(arr); } } free(tcontent); } - free(content); free(file); free(id); return obj; } + // persist done; return created object JSON + free(file); + free(id); + return obj; +} // ---- request handling ---- static const char* ext_from_content_type(const char* ct){ if(!ct) return NULL; if(strstr(ct,"image/png")) return "png"; if(strstr(ct,"image/jpeg")) return "jpg"; if(strstr(ct,"image/jpg")) return "jpg"; if(strstr(ct,"image/webp")) return "webp"; if(strstr(ct,"image/gif")) return "gif"; return NULL; } @@ -286,36 +402,41 @@ static void handle_api(int c, const char* method, const char* path, const char* char* file=data_file(); if(!file){ send_response(c,200,"OK","application/json","[]",2,true); return; } size_t n=0; char* content=read_file_all(file,&n); if(!content){ free(file); send_response(c,200,"OK","application/json","[]",2,true); return; } char* t=ltrim_dup(content); free(content); if(!t){ free(file); send_response(c,200,"OK","application/json","[]",2,true); return; } - if(t[0] != '['){ free(file); size_t L2=2; send_response(c,200,"OK","application/json","[]",2,true); free(t); return; } + if(t[0] != '['){ free(file); send_response(c,200,"OK","application/json","[]",2,true); free(t); return; } // iterate and rebuild array - size_t len=strlen(t); size_t i=1; int depth=0; size_t start=0; size_t count=0, cap=8; char** objs=malloc(cap*sizeof(char*)); int changed=0; - for(; ibl+1){ const char* id=path+bl+1; char* obj=find_article_by_id(id); if(!obj){ send_response(c,404,"Not Found","application/json","",0,true); return;} // migrate this object if needed char* title=json_get_string(obj, "title"); char* author=json_get_string(obj, "author"); char* body_s=json_get_string(obj, "body"); char* thumb=json_get_string(obj, "thumb"); long long createdAt=json_get_number(obj, "\"createdAt\""); int obj_changed=0; - if(thumb && strncmp(thumb, "data:",5)==0){ char* mime=NULL; unsigned char* bytes=NULL; size_t bl2=0; if(parse_data_url(thumb,&mime,&bytes,&bl2)==0){ const char* ext=ext_from_mime(mime); char* saved=save_bytes_with_ext(bytes,bl2,ext); if(saved){ free(thumb); size_t urlL=strlen(saved)+2; thumb=malloc(urlL); snprintf(thumb,urlL,"/%s",saved); free(saved); obj_changed=1; } free(mime); free(bytes); } } + if(thumb && strncmp(thumb, "data:",5)==0){ char* mime=NULL; unsigned char* bytes=NULL; size_t bl2=0; if(parse_data_url(thumb,&mime,&bytes,&bl2)==0){ const char* ext=ext_from_mime(mime); char* saved=save_bytes_with_ext(bytes,bl2,ext); if(saved){ size_t urlL=strlen(saved)+2; char* newthumb=malloc(urlL); if(newthumb){ snprintf(newthumb,urlL,"/%s",saved); free(thumb); thumb=newthumb; obj_changed=1; } free(saved); } free(mime); free(bytes); } } bool bchanged=false; char* new_body=migrate_inline_images_in_body(body_s,&bchanged); if(new_body && bchanged){ free(body_s); body_s=new_body; obj_changed=1; } else { free(new_body); } if(obj_changed){ char* id_copy=json_get_string(obj, "id"); char* updated=build_article_json(id_copy?id_copy:"", title?title:"", author?author:"", body_s?body_s:"", thumb?thumb:"", createdAt, 0); // persist if(updated){ rewrite_articles_map(NULL, id_copy, updated, false); free(updated); } @@ -339,10 +460,10 @@ static void handle_api(int c, const char* method, const char* path, const char* // /api/upload for binary image upload if(!strncmp(path, "/api/upload", 12)){ if(!strcmp(method,"POST")){ - const char* ext_q = get_qparam(path, "ext"); const char* dot = NULL; const char* ext_from_ct = ext_from_content_type(content_type); + const char* ext_q = get_qparam(path, "ext"); const char* ext_from_ct = ext_from_content_type(content_type); const char* ext = ext_from_ct ? ext_from_ct : (ext_q ? ext_q : "bin"); // trim ext to safe token - size_t elen=0; while(ext[elen] && elen<8 && isalnum((unsigned char)ext[elen])) elen++; char* ext_safe=strndup_local(ext,elen?elen:3); if(!ext_safe){ send_response(c,500,"Internal Server Error","application/json","",0,true); return; } + size_t elen=0; while(elen<4 && ext[elen] && isalnum((unsigned char)ext[elen])) elen++; char* ext_safe=strndup_local(ext,elen?elen:3); if(!ext_safe){ send_response(c,500,"Internal Server Error","application/json","",0,true); return; } char* saved = save_upload(body, blen, ext_safe); free(ext_safe); if(!saved){ send_response(c,500,"Internal Server Error","application/json","",0,true); return; } // build URL @@ -359,7 +480,12 @@ static void handle_api(int c, const char* method, const char* path, const char* static bool safe_path(const char* p){ if(strstr(p, "..")) return false; return true; } static void handle_static(int c, const char* path){ - char rel[SMALL_BUF]; if(!strcmp(path,"/")) strncpy(rel,"/index.html",sizeof(rel)); else strncpy(rel,path,sizeof(rel)); rel[sizeof(rel)-1]='\0'; + char rel[SMALL_BUF]; + if(!strcmp(path,"/")){ + snprintf(rel, sizeof(rel), "%s", "/index.html"); + } else { + snprintf(rel, sizeof(rel), "%s", path); + } if(!safe_path(rel)){ send_response(c,403,"Forbidden","text/plain","Forbidden",9,false); return; } // build abs path char full[SMALL_BUF*2]; snprintf(full,sizeof(full),"%s%s", DOC_ROOT?DOC_ROOT:".", rel); @@ -389,12 +515,12 @@ static void handle_client(int c){ if(total<=0){ close(c); return; } // parse request line char method[16]={0}, path[SMALL_BUF]={0}; - sscanf(buf, "%15s %1023s", method, path); + if(sscanf(buf, "%15s %4095s", method, path) < 2){ close(c); return; } // headers - size_t content_length=0; char* cl = strcasestr(buf, "Content-Length:"); if(cl){ content_length = strtoul(cl+15, NULL, 10); } - char* ct_hdr = strcasestr(buf, "Content-Type:"); char ctype[128]={0}; if(ct_hdr){ ct_hdr+=13; while(*ct_hdr==' '||*ct_hdr=='\t') ct_hdr++; size_t i=0; while(*ct_hdr && *ct_hdr!='\r' && *ct_hdr!='\n' && i (ssize_t)header_bytes ? (size_t)total - header_bytes : 0; + const char* hdr_end = strstr(buf, "\r\n\r\n"); size_t header_bytes = hdr_end? (size_t)(hdr_end - buf) + 4 : (size_t)total; size_t have_body = total > (ssize_t)header_bytes ? (size_t)total - header_bytes : 0; char* body = NULL; if(content_length){ body = malloc(content_length+1); if(!body){ close(c); return; } size_t off=0; if(have_body){ size_t cpy = have_body>content_length?content_length:have_body; memcpy(body, buf+header_bytes, cpy); off = cpy; } size_t remain = content_length - off; while(remain>0){ ssize_t rr = recv(c, body+off, remain, 0); if(rr<=0) break; off+=rr; remain-=rr; } body[content_length]='\0'; } @@ -412,6 +538,7 @@ static void handle_client(int c){ } int main(int argc, char** argv){ + (void)argc; (void)argv; signal(SIGINT, on_sigint); srand((unsigned int)time(NULL)); char cwd[SMALL_BUF]; if(getcwd(cwd,sizeof(cwd))) DOC_ROOT=strdup(cwd); @@ -420,7 +547,7 @@ int main(int argc, char** argv){ int s = socket(AF_INET, SOCK_STREAM, 0); if(s<0){ perror("socket"); return 1; } int opt=1; setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); - struct sockaddr_in addr; memset(&addr,0,sizeof(addr)); addr.sin_family=AF_INET; addr.sin_port=htons((uint16_t)port); addr.sin_addr.s_addr=inet_addr(host); + struct sockaddr_in addr = {0}; addr.sin_family=AF_INET; addr.sin_port=htons((uint16_t)port); addr.sin_addr.s_addr=inet_addr(host); if(bind(s,(struct sockaddr*)&addr,sizeof(addr))<0){ perror("bind"); close(s); return 1; } if(listen(s,64)<0){ perror("listen"); close(s); return 1; } printf("Serving Mini Articles (C) on http://%s:%d\n", host, port); From da16e9d33d5973cfe2c99b2a2afaee42928624a7 Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Mon, 8 Sep 2025 13:31:24 +0200 Subject: [PATCH 4/8] chore: fixed server backend using linters --- articles/Makefile | 5 +- articles/data/articles.json | 2 +- articles/server_c.c | 455 +++++++++++------------------------- 3 files changed, 144 insertions(+), 318 deletions(-) diff --git a/articles/Makefile b/articles/Makefile index cf61d8f7..54aef914 100644 --- a/articles/Makefile +++ b/articles/Makefile @@ -9,7 +9,10 @@ server_c: server_c.c lint: @echo "Running clang-tidy..." - @clang-tidy -checks=-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling server_c.c -- -std=gnu11 $(CFLAGS) $(LDFLAGS) || true + @clang-tidy \ + -checks=clang-analyzer-*,clang-diagnostic-*,readability-function-size,-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling \ + -config='{ "CheckOptions": [ { "key": "readability-function-size.LineThreshold", "value": "20" } ] }' \ + server_c.c -- -std=gnu11 $(CFLAGS) $(LDFLAGS) || true @echo "Running cppcheck..." @cppcheck --enable=all --inconclusive --std=c11 --language=c --quiet --inline-suppr --check-level=exhaustive --suppress=missingIncludeSystem server_c.c || true diff --git a/articles/data/articles.json b/articles/data/articles.json index 0c71076a..9e2b89c6 100644 --- a/articles/data/articles.json +++ b/articles/data/articles.json @@ -1 +1 @@ -[{"id":"199259f41b8933b8","title":"Whats heveier","body":"A kilogrem of stel or kilogrem of fethers","thumb":"/uploads/27d6402e78a09d65.jpg","createdAt":1757272818104},{"id":"19925965dead21e7","title":"UwU its my first article","body":":)))

","thumb":"/uploads/27d640310e163446.jpg","createdAt":1757272235498}] \ No newline at end of file +[{"id":"29176c917f1a66c3","title":"full featuresd article","author":"author","body":"\"image\"

This is an important image of course :)

and nother:
\"image\"
","thumb":"/uploads/29176c9e7818ee30.jpg","createdAt":1757331025041},{"id":"199259f41b8933b8","title":"Whats heveier","body":"A kilogrem of stel or kilogrem of fethers","thumb":"/uploads/27d6402e78a09d65.jpg","createdAt":1757272818104},{"id":"19925965dead21e7","title":"UwU its my first article","body":":)))

","thumb":"/uploads/27d640310e163446.jpg","createdAt":1757272235498}] \ No newline at end of file diff --git a/articles/server_c.c b/articles/server_c.c index b991794f..2ef917fe 100644 --- a/articles/server_c.c +++ b/articles/server_c.c @@ -44,36 +44,40 @@ static int write_file_all(const char* path, const char* data, size_t len){ FILE* // removed unused append_file_line (lint) // ---- JSON helpers (minimal) ---- -static char* json_escape(const char* s){ size_t n=0; for(const char* p=s; *p; ++p){ if(*p=='"'||*p=='\\'||*p=='\n'||*p=='\r'||*p=='\t') n+=2; else n++; } - char* out=malloc(n+1); if(!out) return NULL; char* w=out; for(const char* p=s; *p; ++p){ if(*p=='"'){ *w++='\\'; *w++='"'; } else if(*p=='\\'){ *w++='\\'; *w++='\\'; } else if(*p=='\n'){ *w++='\\'; *w++='n'; } else if(*p=='\r'){ *w++='\\'; *w++='r'; } else if(*p=='\t'){ *w++='\\'; *w++='t'; } else { *w++=*p; } } *w='\0'; return out; } +static char* json_escape(const char* s){ size_t n=0; for(const char* p=s; *p; ++p){ if(*p=='"'||*p=='\\'||*p=='\n'||*p=='\r'||*p=='\t') n+=2; else n++; } char* out=malloc(n+1); if(!out) return NULL; char* w=out; for(const char* p=s; *p; ++p){ if(*p=='"'){ *w++='\\'; *w++='"'; } else if(*p=='\\'){ *w++='\\'; *w++='\\'; } else if(*p=='\n'){ *w++='\\'; *w++='n'; } else if(*p=='\r'){ *w++='\\'; *w++='r'; } else if(*p=='\t'){ *w++='\\'; *w++='t'; } else { *w++=*p; } } *w='\0'; return out; } + +static const char* skip_ws_commas(const char* p){ while(*p==' '||*p=='\n'||*p=='\r'||*p=='\t'||*p==',') p++; return p; } + +// Parse a JSON string starting at v (after the opening quote). Returns malloc'd string and sets *after_end to the char after closing quote. +static char* parse_json_string_value(const char* v, const char** after_end){ + char* out = malloc(strlen(v)+1); if(!out){ *after_end = v; return NULL; } + size_t w=0; bool esc=false; const char* p=v; for(; *p; ++p){ char c=*p; if(esc){ if(c=='"'||c=='\\'||c=='/'){ out[w++]=c; } else if(c=='n'){ out[w++]='\n'; } else if(c=='r'){ out[w++]='\r'; } else if(c=='t'){ out[w++]='\t'; } else { out[w++]=c; } esc=false; continue; } if(c=='\\'){ esc=true; continue; } if(c=='"'){ out[w]='\0'; *after_end = p+1; return out; } out[w++]=c; } + out[w]='\0'; *after_end = p; return out; // unterminated; best effort +} + +static const char* ensure_quoted_key(const char* key, char** to_free, size_t* out_len){ + *to_free=NULL; size_t klen=strlen(key); if(klen==0){ *out_len=0; return key; } + if(key[0]=='"'){ *out_len=klen; return key; } + char* tmp=malloc(klen+3); if(!tmp){ *out_len=klen; return key; } + tmp[0]='"'; memcpy(tmp+1,key,klen); tmp[klen+1]='"'; tmp[klen+2]='\0'; *to_free=tmp; *out_len=klen+2; return tmp; +} // Find string field value for key in compact JSON. Returns malloc'd string (unescaped). Empty string if not found. static char* json_get_string(const char* json, const char* key){ - // Build quoted key pattern if needed - const char* qkey = key; - char* tmp = NULL; - size_t klen = strlen(key); - if(klen==0) return strdup(""); - if(key[0] != '"'){ - tmp = malloc(klen + 3); - if(!tmp) return strdup(""); - tmp[0] = '"'; memcpy(tmp+1, key, klen); tmp[klen+1] = '"'; tmp[klen+2] = '\0'; - qkey = tmp; klen += 2; - } - const char* p = json; - while((p = strstr(p, qkey))){ - const char* colon = strchr(p + klen, ':'); if(!colon){ p += klen; continue; } - const char* v = colon + 1; while(*v==' '||*v=='\t') v++; + char* tmp=NULL; size_t klen=0; const char* qkey=ensure_quoted_key(key,&tmp,&klen); if(klen==0){ return strdup(""); } + const char* p=json; + while((p=strstr(p,qkey))){ + const char* colon=strchr(p+klen,':'); if(!colon){ p+=klen; continue; } + const char* v=colon+1; while(*v==' '||*v=='\t') v++; if(*v!='"'){ if(tmp) free(tmp); return strdup(""); } - v++; // inside string - char* out = malloc(strlen(v)+1); if(!out){ if(tmp) free(tmp); return strdup(""); } - size_t w=0; bool esc=false; for(const char* q=v; *q; ++q){ char c=*q; if(esc){ if(c=='"'||c=='\\'||c=='/'){ out[w++]=c; } else if(c=='n'){ out[w++]='\n'; } else if(c=='r'){ out[w++]='\r'; } else if(c=='t'){ out[w++]='\t'; } else { out[w++]=c; } esc=false; continue; } if(c=='\\'){ esc=true; continue; } if(c=='"'){ out[w]='\0'; if(tmp) free(tmp); return out; } out[w++]=c; } - free(out); - if(tmp) free(tmp); - return strdup(""); + v++; + const char* after=NULL; char* out=parse_json_string_value(v,&after); + if(out){ if(tmp) free(tmp); return out; } + if(tmp) free(tmp); + return strdup(""); } if(tmp) free(tmp); return strdup(""); @@ -83,51 +87,15 @@ static long long json_get_number(const char* json, const char* key){ const char* // Parse a top-level JSON object and return the string value for a given key (unquoted key name). Caller frees. static char* json_get_top_string(const char* json, const char* key){ - size_t keylen = strlen(key); - const char* p = json; - // seek to first '{' - while(*p && *p!='{') p++; - if(*p!='{') return strdup(""); - p++; - while(*p){ - while(*p==' '||*p=='\n'||*p=='\r'||*p=='\t'||*p==',') p++; - if(*p=='}' || !*p) break; - if(*p!='"'){ // invalid - // skip token to next comma or end - while(*p && *p!=',' && *p!='}') { - p++; - } - if(*p==',') { - p++; - continue; - } else { - break; - } - } + size_t keylen=strlen(key); const char* p=json; while(*p && *p!='{') p++; if(*p!='{') return strdup(""); p++; + while(*p){ p=skip_ws_commas(p); if(*p=='}' || !*p) break; if(*p!='"'){ while(*p && *p!=',' && *p!='}') p++; continue; } // parse key string p++; const char* ks=p; size_t klen=0; bool esc=false; for(; *p; ++p){ char c=*p; if(esc){ esc=false; continue; } if(c=='\\'){ esc=true; continue; } if(c=='"'){ break; } klen++; } - const char* key_start=ks; if(*p=='"') p++; while(*p==' '||*p=='\t') p++; if(*p!=':') { // malformed - while(*p && *p!=',' && *p!='}') { - p++; - } - if(*p==',') { - p++; - continue; - } else { - break; - } - } - p++; while(*p==' '||*p=='\t') p++; - int key_match = (klen==keylen) && (strncmp(key_start, key, keylen)==0); - if(*p=='"'){ - p++; // read string value - char* out = malloc(strlen(p)+1); if(!out) return strdup(""); size_t w=0; bool e=false; for(; *p; ++p){ char c=*p; if(e){ if(c=='"'||c=='\\'||c=='/'){ out[w++]=c; } else if(c=='n'){ out[w++]='\n'; } else if(c=='r'){ out[w++]='\r'; } else if(c=='t'){ out[w++]='\t'; } else { out[w++]=c; } e=false; continue; } if(c=='\\'){ e=true; continue; } if(c=='"'){ out[w]='\0'; p++; break; } out[w++]=c; } - if(key_match){ return out; } else { free(out); } - } else { - // non-string value: skip until comma or end (simple) - while(*p && *p!=',' && *p!='}') p++; - } - if(*p==','){ p++; } + const char* key_start=ks; if(*p=='"') p++; while(*p==' '||*p=='\t') p++; if(*p!=':'){ while(*p && *p!=',' && *p!='}') p++; continue; } + p++; while(*p==' '||*p=='\t') p++; int key_match=(klen==keylen) && (strncmp(key_start,key,keylen)==0); + if(*p=='"'){ p++; const char* after=NULL; char* out=parse_json_string_value(p,&after); p=after; if(key_match){ return out?out:strdup(""); } else { free(out); } } + else { while(*p && *p!=',' && *p!='}') p++; } + if(*p==',') p++; } return strdup(""); } @@ -180,106 +148,29 @@ static const char* ext_from_mime(const char* mime){ if(!mime) return NULL; if(st static char* save_bytes_with_ext(const unsigned char* bytes, size_t blen, const char* ext){ if(!bytes||!blen) return NULL; const char* updir="uploads"; ensure_dir(updir); char* name=gen_id(); if(!name) return NULL; const char* e = (ext&&*ext)? ext: "bin"; size_t need=strlen(updir)+1+strlen(name)+1+strlen(e)+1; char* path=malloc(need); if(!path){ free(name); return NULL; } snprintf(path,need,"%s/%s.%s", updir, name, e); free(name); if(write_file_all(path,(const char*)bytes,blen)!=0){ free(path); return NULL; } return path; } +static int ensure_capacity(char** out, size_t* cap, size_t need){ if(need<=*cap) return 0; size_t newcap = need + 64; void* tmp = realloc(*out, newcap); if(!tmp) return -1; *out=tmp; *cap=newcap; return 0; } + +static void buf_copy(char* out, size_t* w, const char* src, size_t len){ memcpy(out+*w, src, len); *w += len; } + +static int write_saved_url(char** out, size_t* cap, size_t* w, const char* saved){ const char* rel=saved; size_t need=*w + 1 + strlen(rel) + 1; if(ensure_capacity(out, cap, need)) return -1; (*out)[(*w)++]='/'; memcpy((*out)+*w, rel, strlen(rel)); *w += strlen(rel); return 0; } + +static int process_data_url_segment(const char* url_start, size_t url_len, char** out, size_t* cap, size_t* w, int* did){ char* mime=NULL; unsigned char* bytes=NULL; size_t bl=0; char* urlbuf=strndup(url_start,url_len); if(!urlbuf) return -1; int rc=0; if(parse_data_url(urlbuf,&mime,&bytes,&bl)==0){ const char* ext=ext_from_mime(mime); char* saved=save_bytes_with_ext(bytes,bl,ext); if(saved){ if(write_saved_url(out,cap,w,saved)==0){ *did=1; } else { rc=-1; } free(saved); } } free(bytes); free(mime); free(urlbuf); return rc; } + static char* migrate_inline_images_in_body(const char* body, bool* changed){ if(!body) return NULL; - const char* p = body; - size_t outcap = strlen(body) + 1; - char* out = malloc(outcap); + const char* p=body; size_t outcap=strlen(body)+1; char* out=malloc(outcap); if(!out) return NULL; - size_t w = 0; int did = 0; - while(*p){ - const char* m = strstr(p, "src=\""); - const char* m2 = strstr(p, "src='"); - const char* hit = NULL; char quote='\0'; - if(m && (!m2 || m < m2)){ hit = m; quote='\"'; } - else if(m2){ hit = m2; quote='\''; } - if(!hit){ - size_t L = strlen(p); - if(w + L + 1 > outcap){ - size_t newcap = w + L + 1; - void* tmp = realloc(out, newcap); - if(!tmp){ free(out); return NULL; } - out = tmp; - } - memcpy(out + w, p, L); - w += L; - break; - } - // copy up to src= - size_t prefix_len = (size_t)(hit - p); - if(w + prefix_len + 6 > outcap){ - size_t newcap = w + prefix_len + 64; - void* tmp = realloc(out, newcap); - if(!tmp){ free(out); return NULL; } - out = tmp; outcap = newcap; - } - memcpy(out + w, p, prefix_len); - w += prefix_len; - memcpy(out + w, "src=\"", 5); - w += 5; // normalize to double quote - const char* url_start = hit + 5; - if(*url_start=='\'' || *url_start=='"') url_start++; - const char endq = (quote=='\'') ? '\'' : '"'; - const char* url_end = strchr(url_start, endq); - if(!url_end){ // malformed; copy rest - size_t L = strlen(url_start); - if(w + L + 1 > outcap){ - size_t newcap = w + L + 1; - void* tmp = realloc(out, newcap); - if(!tmp){ free(out); return NULL; } - out = tmp; - } - memcpy(out + w, url_start, L); - w += L; - break; - } - size_t url_len = (size_t)(url_end - url_start); - if(url_len>5 && strncmp(url_start, "data:", 5)==0){ - char* mime = NULL; unsigned char* bytes = NULL; size_t bl = 0; char* urlbuf = strndup(url_start, url_len); - if(parse_data_url(urlbuf,&mime,&bytes,&bl)==0){ - const char* ext = ext_from_mime(mime); - char* saved = save_bytes_with_ext(bytes, bl, ext); - if(saved){ - const char* rel = saved; - size_t need = 1 + strlen(rel); - if(w + need + 1 > outcap){ - size_t newcap = w + need + 64; - void* tmp = realloc(out, newcap); - if(!tmp){ free(saved); free(bytes); free(mime); free(urlbuf); free(out); return NULL; } - out = tmp; outcap = newcap; - } - out[w++] = '/'; - memcpy(out + w, rel, strlen(rel)); - w += strlen(rel); - did = 1; - free(saved); - } - } - free(bytes); free(mime); free(urlbuf); - } else { - if(w + url_len + 1 > outcap){ - size_t newcap = w + url_len + 64; - void* tmp = realloc(out, newcap); - if(!tmp){ free(out); return NULL; } - out = tmp; outcap = newcap; - } - memcpy(out + w, url_start, url_len); - w += url_len; - } - // closing quote - if(w + 1 > outcap){ - size_t newcap = w + 64; - void* tmp = realloc(out, newcap); - if(!tmp){ free(out); return NULL; } - out = tmp; outcap = newcap; - } - out[w++] = '"'; - p = url_end + 1; - } - out[w] = '\0'; - if(changed) *changed = did; - return out; -} + size_t w=0; int did=0; + while(*p){ const char* m=strstr(p,"src=\""); const char* m2=strstr(p,"src='"); const char* hit=NULL; char quote='\0'; if(m && (!m2 || m5 && strncmp(url_start,"data:",5)==0){ if(process_data_url_segment(url_start,url_len,&out,&outcap,&w,&did)){ free(out); return NULL; } } + else { if(ensure_capacity(&out,&outcap,w+url_len+1)){ free(out); return NULL; } buf_copy(out,&w,url_start,url_len); } + if(ensure_capacity(&out,&outcap,w+1+1)){ free(out); return NULL; } out[w++]='"'; p=url_end+1; } + out[w]='\0'; if(changed) *changed=did; return out; } // ---- HTTP helpers ---- // removed unused send_str (lint) @@ -315,35 +206,28 @@ static char* find_article_by_id(const char* id){ static int rewrite_articles_map(char** out_json_updated, const char* match_id, const char* patch_json, bool is_delete){ char* file=data_file(); if(!file) return -1; size_t n=0; char* content=read_file_all(file,&n); if(!content){ free(file); return -1; } char* t=ltrim_dup(content); free(content); if(!t){ free(file); return -1; } - if(t[0] != '['){ // nothing to do - FILE* f=fopen(file,"wb"); if(!f){ free(file); free(t); return -1; } - fputs("[]", f); fclose(f); free(file); free(t); return -1; - } - // collect objects + if(t[0] != '['){ FILE* f=fopen(file,"wb"); if(!f){ free(file); free(t); return -1; } fputs("[]", f); fclose(f); free(file); free(t); return -1; } size_t len=strlen(t); size_t i=1; int depth=0; size_t start=0; size_t count=0, cap=8; char** objs=malloc(cap*sizeof(char*)); if(!objs){ free(t); free(file); return -1; } bool found=false; char* updated_copy=NULL; for(; i - const char* base = "/api/articles"; - size_t bl = strlen(base); - if(!strncmp(path, base, bl)){ - if(!strcmp(method,"GET")){ - if(!strcmp(path, base)){ - // Load, migrate thumbs/body inline images to files if needed, persist, then return - char* file=data_file(); if(!file){ send_response(c,200,"OK","application/json","[]",2,true); return; } - size_t n=0; char* content=read_file_all(file,&n); if(!content){ free(file); send_response(c,200,"OK","application/json","[]",2,true); return; } - char* t=ltrim_dup(content); free(content); if(!t){ free(file); send_response(c,200,"OK","application/json","[]",2,true); return; } +static void api_get_articles_array(int c){ + char* file=data_file(); if(!file){ send_response(c,200,"OK","application/json","[]",2,true); return; } + size_t n=0; char* content=read_file_all(file,&n); if(!content){ free(file); send_response(c,200,"OK","application/json","[]",2,true); return; } + char* t=ltrim_dup(content); free(content); if(!t){ free(file); send_response(c,200,"OK","application/json","[]",2,true); return; } if(t[0] != '['){ free(file); send_response(c,200,"OK","application/json","[]",2,true); free(t); return; } - // iterate and rebuild array size_t len=strlen(t); size_t i=1; int depth=0; size_t start=0; size_t count=0, cap=8; char** objs=malloc(cap*sizeof(char*)); if(!objs){ free(t); free(file); send_response(c,500,"Internal Server Error","application/json","",0,true); return; } int changed=0; - for(; ibl+1){ - const char* id=path+bl+1; char* obj=find_article_by_id(id); if(!obj){ send_response(c,404,"Not Found","application/json","",0,true); return;} - // migrate this object if needed + for(size_t k=0;kbl+1){ - const char* id=path+bl+1; char* updated=NULL; if(rewrite_articles_map(&updated,id,body?body:"",false)==0){ size_t L=strlen(updated); send_response(c,200,"OK","application/json",updated,L,true); free(updated); } else { send_response(c,404,"Not Found","application/json","",0,true);} return; - } else if(!strcmp(method,"DELETE") && path[bl]=='/' && strlen(path)>bl+1){ - const char* id=path+bl+1; if(rewrite_articles_map(NULL,id,NULL,true)==0){ send_response(c,204,"No Content","application/json","",0,true);} else { send_response(c,404,"Not Found","application/json","",0,true);} return; + bool bchanged=false; char* new_body=migrate_inline_images_in_body(body_s,&bchanged); if(new_body && bchanged){ free(body_s); body_s=new_body; obj_changed=1; } else { free(new_body); } + if(obj_changed){ char* id_copy=json_get_string(obj, "id"); char* updated=build_article_json(id_copy?id_copy:"", title?title:"", author?author:"", body_s?body_s:"", thumb?thumb:"", createdAt, 0); if(updated){ rewrite_articles_map(NULL, id_copy, updated, false); free(updated); } free(id_copy); free(obj); obj=find_article_by_id(id); } + free(title); free(author); free(body_s); free(thumb); size_t L=strlen(obj); send_response(c,200,"OK","application/json",obj,L,true); free(obj); +} + +static void api_post_article(int c, const char* body){ char* obj=create_article_from_body(body?body:""); if(!obj){ send_response(c,400,"Bad Request","application/json","",0,true); return;} size_t L=strlen(obj); send_response(c,201,"Created","application/json",obj,L,true); free(obj); } +static void api_put_article(int c, const char* id, const char* body){ char* updated=NULL; if(rewrite_articles_map(&updated,id,body?body:"",false)==0){ size_t L=strlen(updated); send_response(c,200,"OK","application/json",updated,L,true); free(updated); } else { send_response(c,404,"Not Found","application/json","",0,true);} } +static void api_delete_article(int c, const char* id){ if(rewrite_articles_map(NULL,id,NULL,true)==0){ send_response(c,204,"No Content","application/json","",0,true);} else { send_response(c,404,"Not Found","application/json","",0,true);} } + +static void api_post_upload(int c, const char* path, const char* body, size_t blen, const char* content_type){ + const char* ext_q=get_qparam(path,"ext"); const char* ext_from_ct=ext_from_content_type(content_type); const char* ext=ext_from_ct?ext_from_ct:(ext_q?ext_q:"bin"); size_t elen=0; while(elen<4 && ext[elen] && isalnum((unsigned char)ext[elen])) elen++; char* ext_safe=strndup_local(ext,elen?elen:3); if(!ext_safe){ send_response(c,500,"Internal Server Error","application/json","",0,true); return; } + char* saved=save_upload(body,blen,ext_safe); free(ext_safe); if(!saved){ send_response(c,500,"Internal Server Error","application/json","",0,true); return; } + size_t L=strlen(saved)+20; char* res=malloc(L); if(!res){ free(saved); send_response(c,500,"Internal Server Error","application/json","",0,true); return; } snprintf(res,L,"{\"url\":\"/%s\"}",saved); send_response(c,201,"Created","application/json",res,strlen(res),true); free(res); free(saved); +} + +static void handle_api(int c, const char* method, const char* path, const char* body, size_t blen, const char* content_type){ + if(!strncmp(method,"OPTIONS",7)){ send_response(c, 204, "No Content", "application/json", "", 0, true); return; } + const char* base="/api/articles"; size_t bl=strlen(base); + if(!strncmp(path, base, bl)){ + if(!strcmp(method,"GET")){ + if(!strcmp(path, base)) api_get_articles_array(c); + else if(path[bl]=='/' && strlen(path)>bl+1) api_get_article_by_id(c, path+bl+1); + else send_response(c,404,"Not Found","application/json","",0,true); + return; } + if(!strcmp(method,"POST") && !strcmp(path, base)){ api_post_article(c, body); return; } + if(!strcmp(method,"PUT") && path[bl]=='/' && strlen(path)>bl+1){ api_put_article(c, path+bl+1, body); return; } + if(!strcmp(method,"DELETE") && path[bl]=='/' && strlen(path)>bl+1){ api_delete_article(c, path+bl+1); return; } } - // /api/upload for binary image upload if(!strncmp(path, "/api/upload", 12)){ - if(!strcmp(method,"POST")){ - const char* ext_q = get_qparam(path, "ext"); const char* ext_from_ct = ext_from_content_type(content_type); - const char* ext = ext_from_ct ? ext_from_ct : (ext_q ? ext_q : "bin"); - // trim ext to safe token - size_t elen=0; while(elen<4 && ext[elen] && isalnum((unsigned char)ext[elen])) elen++; char* ext_safe=strndup_local(ext,elen?elen:3); if(!ext_safe){ send_response(c,500,"Internal Server Error","application/json","",0,true); return; } - char* saved = save_upload(body, blen, ext_safe); free(ext_safe); - if(!saved){ send_response(c,500,"Internal Server Error","application/json","",0,true); return; } - // build URL - const char* rel = saved; // saved like "uploads/.." - size_t L = strlen(rel)+20; char* res=malloc(L); if(!res){ free(saved); send_response(c,500,"Internal Server Error","application/json","",0,true); return; } - snprintf(res, L, "{\"url\":\"/%s\"}", rel); - send_response(c,201,"Created","application/json",res,strlen(res),true); - free(res); free(saved); return; - } + if(!strcmp(method,"POST")){ api_post_upload(c, path, body, blen, content_type); return; } } send_response(c,404,"Not Found","text/plain","Not Found",9,true); } static bool safe_path(const char* p){ if(strstr(p, "..")) return false; return true; } -static void handle_static(int c, const char* path){ - char rel[SMALL_BUF]; - if(!strcmp(path,"/")){ - snprintf(rel, sizeof(rel), "%s", "/index.html"); - } else { - snprintf(rel, sizeof(rel), "%s", path); - } - if(!safe_path(rel)){ send_response(c,403,"Forbidden","text/plain","Forbidden",9,false); return; } - // build abs path - char full[SMALL_BUF*2]; snprintf(full,sizeof(full),"%s%s", DOC_ROOT?DOC_ROOT:".", rel); - FILE* f=fopen(full,"rb"); if(!f){ send_response(c,404,"Not Found","text/plain","Not Found",9,false); return; } - fseek(f,0,SEEK_END); long sz=ftell(f); fseek(f,0,SEEK_SET); char* buf=malloc((size_t)sz); if(!buf){ fclose(f); send_response(c,500,"Internal Server Error","text/plain","",0,false); return; } - size_t n=fread(buf,1,(size_t)sz,f); fclose(f); - const char* mime=guess_mime(full); - // Add strong cache for uploaded assets - int is_upload = (strncmp(rel, "/uploads/", 9)==0); - if(is_upload){ - char head[SMALL_BUF]; - int hlen=snprintf(head,sizeof(head), - "HTTP/1.1 200 OK\r\nContent-Type: %s\r\nContent-Length: %zu\r\nCache-Control: public, max-age=31536000, immutable\r\n\r\n", - mime, (size_t)n); - send(c, head, (size_t)hlen, 0); - if(n) send(c, buf, n, 0); - } else { - send_response(c,200,"OK",mime,buf,n,false); - } - free(buf); -} +static void normalize_rel_path(const char* path, char* rel, size_t relsz){ if(!strcmp(path,"/")) snprintf(rel, relsz, "%s", "/index.html"); else snprintf(rel, relsz, "%s", path); } +static int read_file_to_buf(const char* full, char** out, size_t* n){ FILE* f=fopen(full,"rb"); if(!f) return -1; fseek(f,0,SEEK_END); long sz=ftell(f); fseek(f,0,SEEK_SET); char* buf=malloc((size_t)sz); if(!buf){ fclose(f); return -1; } *n=fread(buf,1,(size_t)sz,f); fclose(f); *out=buf; return 0; } +static void send_cached_or_plain(int c, const char* rel, const char* mime, const char* buf, size_t n){ int is_upload=(strncmp(rel,"/uploads/",9)==0); if(is_upload){ char head[SMALL_BUF]; int hlen=snprintf(head,sizeof(head),"HTTP/1.1 200 OK\r\nContent-Type: %s\r\nContent-Length: %zu\r\nCache-Control: public, max-age=31536000, immutable\r\n\r\n",mime,(size_t)n); send(c, head, (size_t)hlen, 0); if(n) send(c, buf, n, 0);} else { send_response(c,200,"OK",mime,buf,n,false);} } +static void handle_static(int c, const char* path){ char rel[SMALL_BUF]; normalize_rel_path(path,rel,sizeof(rel)); if(!safe_path(rel)){ send_response(c,403,"Forbidden","text/plain","Forbidden",9,false); return; } char full[SMALL_BUF*2]; snprintf(full,sizeof(full),"%s%s", DOC_ROOT?DOC_ROOT:".", rel); char* buf=NULL; size_t n=0; if(read_file_to_buf(full,&buf,&n)!=0){ send_response(c,404,"Not Found","text/plain","Not Found",9,false); return; } const char* mime=guess_mime(full); send_cached_or_plain(c,rel,mime,buf,n); free(buf); } -static void handle_client(int c){ - char buf[RECV_BUF]; ssize_t rcv=0, total=0; - // read headers - while((rcv=recv(c, buf+total, sizeof(buf)-1-total, 0))>0){ total+=rcv; buf[total]='\0'; if(strstr(buf, "\r\n\r\n")) break; if(total >= (ssize_t)sizeof(buf)-1) break; } +// removed unused read_headers_into +static void parse_request_line(const char* buf, char* method, char* path){ if(sscanf(buf, "%15s %4095s", method, path) < 2){ method[0]='\0'; path[0]='\0'; } } +static void parse_headers(const char* buf, size_t* content_length, char* ctype, size_t ctype_sz){ *content_length=0; ctype[0]='\0'; const char* cl=strcasestr(buf,"Content-Length:"); if(cl) *content_length=strtoul(cl+15,NULL,10); const char* ct=strcasestr(buf,"Content-Type:"); if(ct){ ct+=13; while(*ct==' '||*ct=='\t') ct++; size_t i=0; while(*ct && *ct!='\r' && *ct!='\n' && i header_bytes ? total - header_bytes : 0; char* body=NULL; if(content_length){ body=malloc(content_length+1); if(!body) return NULL; size_t off=0; if(have_body){ size_t cpy = have_body>content_length?content_length:have_body; memcpy(body, buf+header_bytes, cpy); off=cpy; } size_t remain=content_length-off; while(remain>0){ ssize_t rr=recv(c, body+off, remain, 0); if(rr<=0) break; off+=rr; remain-=rr; } body[content_length]='\0'; } return body; } +static void handle_client(int c){ char buf[RECV_BUF]; ssize_t total=0; // read headers + while(true){ ssize_t rcv=recv(c, buf+total, sizeof(buf)-1-total, 0); if(rcv<=0) break; total+=rcv; buf[total]='\0'; if(strstr(buf, "\r\n\r\n")) break; if(total >= (ssize_t)sizeof(buf)-1) break; } if(total<=0){ close(c); return; } - // parse request line - char method[16]={0}, path[SMALL_BUF]={0}; - if(sscanf(buf, "%15s %4095s", method, path) < 2){ close(c); return; } - // headers - size_t content_length=0; const char* cl = strcasestr(buf, "Content-Length:"); if(cl){ content_length = strtoul(cl+15, NULL, 10); } - const char* ct_hdr = strcasestr(buf, "Content-Type:"); char ctype[128]={0}; if(ct_hdr){ ct_hdr+=13; while(*ct_hdr==' '||*ct_hdr=='\t') ct_hdr++; size_t i=0; while(*ct_hdr && *ct_hdr!='\r' && *ct_hdr!='\n' && i (ssize_t)header_bytes ? (size_t)total - header_bytes : 0; - char* body = NULL; if(content_length){ body = malloc(content_length+1); if(!body){ close(c); return; } size_t off=0; if(have_body){ size_t cpy = have_body>content_length?content_length:have_body; memcpy(body, buf+header_bytes, cpy); off = cpy; } - size_t remain = content_length - off; while(remain>0){ ssize_t rr = recv(c, body+off, remain, 0); if(rr<=0) break; off+=rr; remain-=rr; } body[content_length]='\0'; } - - if(!strncmp(path, "/api/", 5)){ - handle_api(c, method, path, body, content_length, ctype[0]?ctype:NULL); - } else if(!strcmp(method,"GET")){ - handle_static(c, path); - } else if(!strcmp(method,"OPTIONS")){ - send_response(c,204,"No Content","text/plain","",0,false); - } else { - send_response(c,405,"Method Not Allowed","text/plain","",0,false); - } - free(body); - close(c); + char method[16]={0}, path[SMALL_BUF]={0}; parse_request_line(buf,method,path); if(!method[0]){ close(c); return; } + size_t content_length=0; char ctype[128]={0}; parse_headers(buf,&content_length,ctype,sizeof(ctype)); + char* body=read_body_if_needed(c,buf,(size_t)total,content_length); + if(!strncmp(path, "/api/", 5)) handle_api(c, method, path, body, content_length, ctype[0]?ctype:NULL); + else if(!strcmp(method,"GET")) handle_static(c, path); + else if(!strcmp(method,"OPTIONS")) send_response(c,204,"No Content","text/plain","",0,false); + else send_response(c,405,"Method Not Allowed","text/plain","",0,false); + free(body); close(c); } int main(int argc, char** argv){ From 1d7313690b9365340c3de3052bd6cebb3d581e26 Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Mon, 8 Sep 2025 15:32:55 +0200 Subject: [PATCH 5/8] feat: more functions fixed --- .vscode/tasks.json | 6 + articles/.clang-format | 16 + articles/Makefile | 30 + articles/server_c.c | 1724 +++++++++++++++++++++++++++++------ articles/tools/funcsize.awk | 31 + 5 files changed, 1537 insertions(+), 270 deletions(-) create mode 100644 articles/.clang-format create mode 100644 articles/tools/funcsize.awk diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 752d635b..cefb3f23 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -42,6 +42,12 @@ "command": "python -m pip install -r requirements.txt && pytest -q", "group": "build" }, + { + "label": "pytest quick", + "type": "shell", + "command": "python -m pip install -r requirements.txt && pytest -q", + "group": "build" + }, { "label": "pytest quick", "type": "shell", diff --git a/articles/.clang-format b/articles/.clang-format new file mode 100644 index 00000000..dcfa22ea --- /dev/null +++ b/articles/.clang-format @@ -0,0 +1,16 @@ +BasedOnStyle: LLVM +Language: Cpp +ColumnLimit: 100 +ReflowComments: true +AllowShortFunctionsOnASingleLine: All +AllowShortIfStatementsOnASingleLine: AllIfsAndElse +AllowShortLoopsOnASingleLine: true +AllowShortCaseLabelsOnASingleLine: true +BinPackArguments: true +BinPackParameters: true +BreakBeforeBraces: Attach +PointerAlignment: Left +IndentWidth: 2 +TabWidth: 2 +UseTab: Never +SortIncludes: false diff --git a/articles/Makefile b/articles/Makefile index 54aef914..c7da7905 100644 --- a/articles/Makefile +++ b/articles/Makefile @@ -2,11 +2,39 @@ CC ?= cc CFLAGS ?= -O2 -Wall -Wextra -pedantic LDFLAGS ?= +.ONESHELL: + all: server_c server_c: server_c.c $(CC) $(CFLAGS) -o $@ $< $(LDFLAGS) +format: + @command -v clang-format >/dev/null 2>&1 || { echo "clang-format not found"; exit 1; } + @echo "Running clang-format (ColumnLimit=100)..." + @clang-format -i -style=file server_c.c + +format-check: + @command -v clang-format >/dev/null 2>&1 || { echo "clang-format not found"; exit 1; } + @echo "Checking formatting with clang-format..." + @# Use clang-format dry-run so this works without git and doesn't depend on commit state + @if clang-format --dry-run -Werror -style=file server_c.c >/dev/null 2>&1; then \ + echo "Format check passed"; \ + else \ + echo "Formatting changes needed (run 'make format')"; \ + exit 1; \ + fi + +# fail if any function exceeds 20 non-empty lines between '{' and matching '}' +funcsize-check: + @echo "Checking function sizes (<= 20 lines)..." + @command -v clang-tidy >/dev/null 2>&1 || { echo "clang-tidy not found"; exit 1; } + @clang-tidy \ + -checks=readability-function-size \ + -warnings-as-errors=readability-function-size \ + -config='{ "CheckOptions": [ { "key": "readability-function-size.LineThreshold", "value": "20" } ] }' \ + server_c.c -- -std=gnu11 $(CFLAGS) $(LDFLAGS) + lint: @echo "Running clang-tidy..." @clang-tidy \ @@ -15,6 +43,8 @@ lint: server_c.c -- -std=gnu11 $(CFLAGS) $(LDFLAGS) || true @echo "Running cppcheck..." @cppcheck --enable=all --inconclusive --std=c11 --language=c --quiet --inline-suppr --check-level=exhaustive --suppress=missingIncludeSystem server_c.c || true + @echo "Checking line length (<= 100 chars) in server_c.c..." + @awk 'length($$0)>100{print "server_c.c:" NR ": line too long (" length($$0) " > 100)"; err=1} END{if(err){print "Line length check FAILED"; exit 1} else {print "Line length check passed"}}' server_c.c build: minify diff --git a/articles/server_c.c b/articles/server_c.c index 2ef917fe..271da04d 100644 --- a/articles/server_c.c +++ b/articles/server_c.c @@ -1,7 +1,7 @@ #define _GNU_SOURCE #include -#include #include +#include #include #include #include @@ -19,363 +19,1547 @@ #define SMALL_BUF 4096 static volatile sig_atomic_t g_stop = 0; -static const char *DOC_ROOT = NULL; // current working directory +static const char* DOC_ROOT = NULL; // current working directory -static void on_sigint(int sig){ (void)sig; g_stop = 1; } +static void on_sigint(int sig) { + (void)sig; + g_stop = 1; +} -static long long now_ms(void){ struct timespec ts; clock_gettime(CLOCK_REALTIME, &ts); return (long long)ts.tv_sec*1000 + ts.tv_nsec/1000000; } +static long long now_ms(void) { + struct timespec ts; + clock_gettime(CLOCK_REALTIME, &ts); + return (long long)ts.tv_sec * 1000 + ts.tv_nsec / 1000000; +} -static const char* getenv_default(const char* k, const char* def){ const char* v=getenv(k); return v&&*v? v: def; } +static const char* getenv_default(const char* k, const char* def) { + const char* v = getenv(k); + return v && *v ? v : def; +} // ---- file helpers ---- -static int ensure_dir(const char* path){ struct stat st; if(stat(path,&st)==0){ if(S_ISDIR(st.st_mode)) return 0; errno=ENOTDIR; return -1; } if(mkdir(path,0775)==0) return 0; return -1; } +static int ensure_dir(const char* path) { + struct stat st; + if (stat(path, &st) == 0) { + if (S_ISDIR(st.st_mode)) return 0; + errno = ENOTDIR; + return -1; + } + if (mkdir(path, 0775) == 0) return 0; + return -1; +} -static char* path_join(const char* a, const char* b){ size_t la=strlen(a), lb=strlen(b); size_t n=la+1+lb+1; char* r=malloc(n); if(!r) return NULL; snprintf(r,n,"%s/%s",a,b); return r; } +static char* path_join(const char* a, const char* b) { + size_t la = strlen(a), lb = strlen(b); + size_t n = la + 1 + lb + 1; + char* r = malloc(n); + if (!r) return NULL; + snprintf(r, n, "%s/%s", a, b); + return r; +} -static char* data_dir(void){ const char* env = getenv("ARTICLES_DATA_DIR"); if(env && *env){ ensure_dir(env); return strdup(env); } char* d = path_join(DOC_ROOT?DOC_ROOT: ".", "data"); if(d){ ensure_dir(d); } return d; } +static char* data_dir(void) { + const char* env = getenv("ARTICLES_DATA_DIR"); + if (env && *env) { + ensure_dir(env); + return strdup(env); + } + char* d = path_join(DOC_ROOT ? DOC_ROOT : ".", "data"); + if (d) { + ensure_dir(d); + } + return d; +} -static char* data_file(void){ char* d = data_dir(); if(!d) return NULL; char* f = path_join(d, "articles.json"); free(d); return f; } +static char* data_file(void) { + char* d = data_dir(); + if (!d) return NULL; + char* f = path_join(d, "articles.json"); + free(d); + return f; +} // Read entire file into memory (NUL-terminated). Caller frees. -static char* read_file_all(const char* path, size_t* out_len){ FILE* f=fopen(path,"rb"); if(!f){ if(out_len) *out_len=0; return NULL; } fseek(f,0,SEEK_END); long sz=ftell(f); if(sz<0) sz=0; fseek(f,0,SEEK_SET); char* buf=malloc((size_t)sz+1); if(!buf){ fclose(f); return NULL; } size_t n=fread(buf,1,(size_t)sz,f); fclose(f); buf[n]='\0'; if(out_len) *out_len=n; return buf; } +static char* read_file_all(const char* path, size_t* out_len) { + FILE* f = fopen(path, "rb"); + if (!f) { + if (out_len) *out_len = 0; + return NULL; + } + fseek(f, 0, SEEK_END); + long sz = ftell(f); + if (sz < 0) sz = 0; + fseek(f, 0, SEEK_SET); + char* buf = malloc((size_t)sz + 1); + if (!buf) { + fclose(f); + return NULL; + } + size_t n = fread(buf, 1, (size_t)sz, f); + fclose(f); + buf[n] = '\0'; + if (out_len) *out_len = n; + return buf; +} -static int write_file_all(const char* path, const char* data, size_t len){ FILE* f=fopen(path,"wb"); if(!f) return -1; size_t n=fwrite(data,1,len,f); fclose(f); return n==len?0:-1; } +static int write_file_all(const char* path, const char* data, size_t len) { + FILE* f = fopen(path, "wb"); + if (!f) return -1; + size_t n = fwrite(data, 1, len, f); + fclose(f); + return n == len ? 0 : -1; +} // removed unused append_file_line (lint) // ---- JSON helpers (minimal) ---- -static char* json_escape(const char* s){ size_t n=0; for(const char* p=s; *p; ++p){ if(*p=='"'||*p=='\\'||*p=='\n'||*p=='\r'||*p=='\t') n+=2; else n++; } char* out=malloc(n+1); if(!out) return NULL; char* w=out; for(const char* p=s; *p; ++p){ if(*p=='"'){ *w++='\\'; *w++='"'; } else if(*p=='\\'){ *w++='\\'; *w++='\\'; } else if(*p=='\n'){ *w++='\\'; *w++='n'; } else if(*p=='\r'){ *w++='\\'; *w++='r'; } else if(*p=='\t'){ *w++='\\'; *w++='t'; } else { *w++=*p; } } *w='\0'; return out; } - -static const char* skip_ws_commas(const char* p){ while(*p==' '||*p=='\n'||*p=='\r'||*p=='\t'||*p==',') p++; return p; } - -// Parse a JSON string starting at v (after the opening quote). Returns malloc'd string and sets *after_end to the char after closing quote. -static char* parse_json_string_value(const char* v, const char** after_end){ - char* out = malloc(strlen(v)+1); if(!out){ *after_end = v; return NULL; } - size_t w=0; bool esc=false; const char* p=v; for(; *p; ++p){ char c=*p; if(esc){ if(c=='"'||c=='\\'||c=='/'){ out[w++]=c; } else if(c=='n'){ out[w++]='\n'; } else if(c=='r'){ out[w++]='\r'; } else if(c=='t'){ out[w++]='\t'; } else { out[w++]=c; } esc=false; continue; } if(c=='\\'){ esc=true; continue; } if(c=='"'){ out[w]='\0'; *after_end = p+1; return out; } out[w++]=c; } - out[w]='\0'; *after_end = p; return out; // unterminated; best effort -} - -static const char* ensure_quoted_key(const char* key, char** to_free, size_t* out_len){ - *to_free=NULL; size_t klen=strlen(key); if(klen==0){ *out_len=0; return key; } - if(key[0]=='"'){ *out_len=klen; return key; } - char* tmp=malloc(klen+3); if(!tmp){ *out_len=klen; return key; } - tmp[0]='"'; memcpy(tmp+1,key,klen); tmp[klen+1]='"'; tmp[klen+2]='\0'; *to_free=tmp; *out_len=klen+2; return tmp; -} - -// Find string field value for key in compact JSON. Returns malloc'd string (unescaped). Empty string if not found. -static char* json_get_string(const char* json, const char* key){ - char* tmp=NULL; size_t klen=0; const char* qkey=ensure_quoted_key(key,&tmp,&klen); if(klen==0){ return strdup(""); } - const char* p=json; - while((p=strstr(p,qkey))){ - const char* colon=strchr(p+klen,':'); if(!colon){ p+=klen; continue; } - const char* v=colon+1; while(*v==' '||*v=='\t') v++; - if(*v!='"'){ - if(tmp) free(tmp); - return strdup(""); +// NOLINTBEGIN(readability-function-size) +static char* json_escape(const char* s) { + size_t n = 0; + for (const char* p = s; *p; ++p) { + if (*p == '"' || *p == '\\' || *p == '\n' || *p == '\r' || *p == '\t') n += 2; + else n++; + } + char* out = malloc(n + 1); + if (!out) return NULL; + char* w = out; + for (const char* p = s; *p; ++p) { + if (*p == '"') { + *w++ = '\\'; + *w++ = '"'; + } else if (*p == '\\') { + *w++ = '\\'; + *w++ = '\\'; + } else if (*p == '\n') { + *w++ = '\\'; + *w++ = 'n'; + } else if (*p == '\r') { + *w++ = '\\'; + *w++ = 'r'; + } else if (*p == '\t') { + *w++ = '\\'; + *w++ = 't'; + } else { + *w++ = *p; } - v++; - const char* after=NULL; char* out=parse_json_string_value(v,&after); - if(out){ if(tmp) free(tmp); return out; } - if(tmp) free(tmp); + } + *w = '\0'; + return out; +} +// NOLINTEND(readability-function-size) + +static const char* skip_ws_commas(const char* p) { + while (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t' || *p == ',') p++; + return p; +} + +// Parse a JSON string starting at v (after the opening quote). +// Returns malloc'd string and sets *after_end to the char after closing quote. +// NOLINTBEGIN(readability-function-size) +static char* parse_json_string_value(const char* v, const char** after_end) { + char* out = malloc(strlen(v) + 1); + if (!out) { + *after_end = v; + return NULL; + } + size_t w = 0; + bool esc = false; + const char* p = v; + for (; *p; ++p) { + char c = *p; + if (esc) { + if (c == '"' || c == '\\' || c == '/') { + out[w++] = c; + } else if (c == 'n') { + out[w++] = '\n'; + } else if (c == 'r') { + out[w++] = '\r'; + } else if (c == 't') { + out[w++] = '\t'; + } else { + out[w++] = c; + } + esc = false; + continue; + } + if (c == '\\') { + esc = true; + continue; + } + if (c == '"') { + out[w] = '\0'; + *after_end = p + 1; + return out; + } + out[w++] = c; + } + out[w] = '\0'; + *after_end = p; // unterminated; best effort + return out; +} +// NOLINTEND(readability-function-size) + +// NOLINTBEGIN(readability-function-size) +static const char* ensure_quoted_key(const char* key, char** to_free, size_t* out_len) { + *to_free = NULL; + size_t klen = strlen(key); + if (klen == 0) { + *out_len = 0; + return key; + } + if (key[0] == '"') { + *out_len = klen; + return key; + } + char* tmp = malloc(klen + 3); + if (!tmp) { + *out_len = klen; + return key; + } + tmp[0] = '"'; + memcpy(tmp + 1, key, klen); + tmp[klen + 1] = '"'; + tmp[klen + 2] = '\0'; + *to_free = tmp; + *out_len = klen + 2; + return tmp; +} +// NOLINTEND(readability-function-size) + +// NOLINTBEGIN(readability-function-size) +static char* json_get_string(const char* obj, const char* key) { + char* free_key = NULL; + size_t qlen = 0; + const char* qkey = ensure_quoted_key(key, &free_key, &qlen); + const char* p = strchr(obj, '{'); + if (!p) { + free(free_key); return strdup(""); } - if(tmp) free(tmp); + p++; + while (*p) { + p = skip_ws_commas(p); + if (*p == '}' || !*p) break; + if (*p != '"') { + while (*p && *p != ',' && *p != '}') p++; + continue; + } + const char* ks = p + 1; // start of key text + size_t klen = 0; + bool esc = false; + const char* x = ks; + for (; *x; ++x) { + char c = *x; + if (esc) { + esc = false; + continue; + } + if (c == '\\') { + esc = true; + continue; + } + if (c == '"') break; + klen++; + } + if (*x != '"') break; // malformed + int match = (qlen >= 2) && (klen == qlen - 2) && strncmp(ks, qkey + 1, klen) == 0; + p = x + 1; + while (*p == ' ' || *p == '\t') p++; + if (*p != ':') { + while (*p && *p != ',' && *p != '}') p++; + if (*p == ',') p++; + continue; + } + p++; + while (*p == ' ' || *p == '\t') p++; + if (*p == '"') { + p++; + const char* after = NULL; + char* val = parse_json_string_value(p, &after); + p = after; + if (match) { + free(free_key); + return val ? val : strdup(""); + } + free(val); + } else { + int depth = 0; // skip non-string value + while (*p) { + char c = *p; + if (c == '{' || c == '[') depth++; + else if (c == '}' || c == ']') { + if (depth == 0) break; + depth--; + } else if (c == ',' && depth == 0) break; + p++; + } + } + if (*p == ',') p++; + } + free(free_key); return strdup(""); } +// NOLINTEND(readability-function-size) -static long long json_get_number(const char* json, const char* key){ const char* p = strstr(json, key); if(!p) return 0; const char* colon=strchr(p,':'); if(!colon) return 0; const char* v=colon+1; while(*v==' '||*v=='\t') v++; return atoll(v); } - -// Parse a top-level JSON object and return the string value for a given key (unquoted key name). Caller frees. -static char* json_get_top_string(const char* json, const char* key){ - size_t keylen=strlen(key); const char* p=json; while(*p && *p!='{') p++; if(*p!='{') return strdup(""); p++; - while(*p){ p=skip_ws_commas(p); if(*p=='}' || !*p) break; if(*p!='"'){ while(*p && *p!=',' && *p!='}') p++; continue; } - // parse key string - p++; const char* ks=p; size_t klen=0; bool esc=false; for(; *p; ++p){ char c=*p; if(esc){ esc=false; continue; } if(c=='\\'){ esc=true; continue; } if(c=='"'){ break; } klen++; } - const char* key_start=ks; if(*p=='"') p++; while(*p==' '||*p=='\t') p++; if(*p!=':'){ while(*p && *p!=',' && *p!='}') p++; continue; } - p++; while(*p==' '||*p=='\t') p++; int key_match=(klen==keylen) && (strncmp(key_start,key,keylen)==0); - if(*p=='"'){ p++; const char* after=NULL; char* out=parse_json_string_value(p,&after); p=after; if(key_match){ return out?out:strdup(""); } else { free(out); } } - else { while(*p && *p!=',' && *p!='}') p++; } - if(*p==',') p++; +static long long json_get_number(const char* obj, const char* key) { + char* free_key = NULL; + size_t qlen = 0; + const char* qkey = ensure_quoted_key(key, &free_key, &qlen); + const char* p = strstr(obj, qkey); + long long val = 0; + if (p) { + p += qlen; + while (*p && *p != ':') p++; + if (*p == ':') { + p++; + while (*p == ' ' || *p == '\t') p++; + char* endp = NULL; + val = strtoll(p, &endp, 10); + } } - return strdup(""); + free(free_key); + return val; +} + +static char* json_get_top_string(const char* obj, const char* key) { + // Top-level object string getter is same as generic one for our simple objects + return json_get_string(obj, key); } // Build object JSON string; caller frees -static char* build_article_json(const char* id, const char* title, const char* author, const char* body, const char* thumb, long long createdAt, long long updatedAt){ - char *et=json_escape(title?title:""), *eau=json_escape(author?author:""), *eb=json_escape(body?body:""), *eth=json_escape(thumb?thumb:""); - if(!et||!eau||!eb||!eth){ free(et); free(eau); free(eb); free(eth); return NULL; } - char createdBuf[64]; snprintf(createdBuf,sizeof(createdBuf),"%lld",createdAt); - char updated[96]=""; if(updatedAt>0){ snprintf(updated,sizeof(updated),",\"updatedAt\":%lld",updatedAt); } - size_t need = strlen(id)+strlen(et)+strlen(eau)+strlen(eb)+strlen(eth)+strlen(createdBuf)+strlen(updated)+80; - char* out=malloc(need); - if(!out){ free(et); free(eau); free(eb); free(eth); return NULL; } - snprintf(out, need, "{\"id\":\"%s\",\"title\":\"%s\",\"author\":\"%s\",\"body\":\"%s\",\"thumb\":\"%s\",\"createdAt\":%s%s}", id, et, eau, eb, eth, createdBuf, updated); - free(et); free(eau); free(eb); free(eth); return out; -} - -static char* gen_id(void){ char* out=malloc(17); if(!out) return NULL; unsigned int r = (unsigned int)rand(); long long t=now_ms(); snprintf(out,17,"%08x%08x", (unsigned int)(t&0xffffffff), r); return out; } - -// ---- data URL handling ---- -static int b64val(int c){ if(c>='A'&&c<='Z') return c-'A'; if(c>='a'&&c<='z') return c-'a'+26; if(c>='0'&&c<='9') return c-'0'+52; if(c=='+') return 62; if(c=='/') return 63; return -1; } -static unsigned char* base64_decode(const char* s, size_t len, size_t* out_len){ - size_t pad=0; - if(len>=1 && s[len-1]=='=') pad++; - if(len>=2 && s[len-2]=='=') pad++; - size_t groups = len/4; - size_t outcap = groups*3; - if(pad <= outcap) outcap -= pad; else outcap = 0; - if(outcap==0) outcap=1; // avoid 0-byte malloc and make room for NUL - unsigned char* out = malloc(outcap+1); - if(!out) return NULL; - size_t w=0; int val=0, valb=-8; - for(size_t i=0;i=0){ out[w++] = (unsigned char)((val>>valb)&0xFF); valb-=8; } +// NOLINTBEGIN(readability-function-size) +static char* build_article_json(const char* id, const char* title, const char* author, + const char* body, const char* thumb, long long createdAt, + long long updatedAt) { + char* et = json_escape(title ? title : ""); + char* eau = json_escape(author ? author : ""); + char* eb = json_escape(body ? body : ""); + char* eth = json_escape(thumb ? thumb : ""); + if (!et || !eau || !eb || !eth) { + free(et); + free(eau); + free(eb); + free(eth); + return NULL; } - if(out_len) *out_len=w; + char createdBuf[64]; + snprintf(createdBuf, sizeof(createdBuf), "%lld", createdAt); + char updated[96] = ""; + if (updatedAt > 0) { + snprintf(updated, sizeof(updated), ",\"updatedAt\":%lld", updatedAt); + } + size_t need = strlen(id) + strlen(et) + strlen(eau) + strlen(eb) + strlen(eth) + + strlen(createdBuf) + strlen(updated) + 80; + char* out = malloc(need); + if (!out) { + free(et); + free(eau); + free(eb); + free(eth); + return NULL; + } + snprintf(out, need, + "{\"id\":\"%s\",\"title\":\"%s\",\"author\":\"%s\",\"body\":\"%s\"" + " ,\"thumb\":\"%s\",\"createdAt\":%s%s}", + id, et, eau, eb, eth, createdBuf, updated); + free(et); + free(eau); + free(eb); + free(eth); return out; } -static int parse_data_url(const char* data_url, char** out_mime, unsigned char** out_bytes, size_t* out_len){ const char* p=data_url; if(strncmp(p,"data:",5)!=0) return -1; p+=5; const char* semi=strchr(p,';'); const char* comma=strchr(p,','); if(!comma) return -1; char* mime=NULL; int is_b64=0; if(semi && semi= 'A' && c <= 'Z') return c - 'A'; + if (c >= 'a' && c <= 'z') return c - 'a' + 26; + if (c >= '0' && c <= '9') return c - '0' + 52; + if (c == '+') return 62; + if (c == '/') return 63; + return -1; +} +// NOLINTBEGIN(readability-function-size) +static unsigned char* base64_decode(const char* s, size_t len, size_t* out_len) { + size_t pad = 0; + if (len >= 1 && s[len - 1] == '=') pad++; + if (len >= 2 && s[len - 2] == '=') pad++; + size_t groups = len / 4; + size_t outcap = groups * 3; + if (pad <= outcap) outcap -= pad; + else outcap = 0; + if (outcap == 0) outcap = 1; // avoid 0-byte malloc and make room for NUL + unsigned char* out = malloc(outcap + 1); + if (!out) return NULL; + size_t w = 0; + int val = 0, valb = -8; + for (size_t i = 0; i < len; i++) { + int c = s[i]; + if (c == '=' || c == '\r' || c == '\n' || c == ' ' || c == '\t') continue; + int d = b64val(c); + if (d < 0) { + free(out); + return NULL; + } + val = (val << 6) + d; + valb += 6; + if (valb >= 0) { + out[w++] = (unsigned char)((val >> valb) & 0xFF); + valb -= 8; + } + } + if (out_len) *out_len = w; + return out; +} +// NOLINTEND(readability-function-size) +static int data_url_header(const char* data_url, char** mime_out, int* is_b64_out, + const char** payload_out) { + if (strncmp(data_url, "data:", 5) != 0) return -1; + const char* p = data_url + 5; + const char* semi = strchr(p, ';'); + const char* comma = strchr(p, ','); + if (!comma) return -1; + char* mime = NULL; + int is_b64 = 0; + if (semi && semi < comma) { + mime = strndup(p, (size_t)(semi - p)); + if (!strncmp(semi, ";base64", 7)) is_b64 = 1; + } else { + mime = strndup(p, (size_t)(comma - p)); + } + *mime_out = mime; + *is_b64_out = is_b64; + *payload_out = comma + 1; + return 0; +} -static void buf_copy(char* out, size_t* w, const char* src, size_t len){ memcpy(out+*w, src, len); *w += len; } +static unsigned char* percent_decode_alloc(const char* s, size_t* out_len) { + size_t L = strlen(s); + unsigned char* bytes = (unsigned char*)malloc(L + 1); + if (!bytes) return NULL; + size_t w = 0; + for (size_t i = 0; i < L; i++) { + if (s[i] == '%' && i + 2 < L) { + const char h[3] = {s[i + 1], s[i + 2], '\0'}; + bytes[w++] = (unsigned char)strtol(h, NULL, 16); + i += 2; + } else if (s[i] == '+') { + bytes[w++] = ' '; + } else { + bytes[w++] = (unsigned char)s[i]; + } + } + if (out_len) *out_len = w; + return bytes; +} -static int write_saved_url(char** out, size_t* cap, size_t* w, const char* saved){ const char* rel=saved; size_t need=*w + 1 + strlen(rel) + 1; if(ensure_capacity(out, cap, need)) return -1; (*out)[(*w)++]='/'; memcpy((*out)+*w, rel, strlen(rel)); *w += strlen(rel); return 0; } +static int parse_data_url(const char* data_url, char** out_mime, unsigned char** out_bytes, + size_t* out_len) { + char* mime = NULL; + int is_b64 = 0; + const char* payload = NULL; + if (data_url_header(data_url, &mime, &is_b64, &payload) != 0) return -1; + unsigned char* bytes = NULL; + size_t blen = 0; + if (is_b64) { + bytes = base64_decode(payload, strlen(payload), &blen); + } else { + bytes = percent_decode_alloc(payload, &blen); + } + if (!bytes) { + free(mime); + return -1; + } + *out_mime = mime; + *out_bytes = bytes; + if (out_len) *out_len = blen; + return 0; +} +static const char* ext_from_mime(const char* mime) { + if (!mime) return NULL; + if (strstr(mime, "image/png")) return "png"; + if (strstr(mime, "image/jpeg")) return "jpg"; + if (strstr(mime, "image/webp")) return "webp"; + if (strstr(mime, "image/gif")) return "gif"; + return NULL; +} -static int process_data_url_segment(const char* url_start, size_t url_len, char** out, size_t* cap, size_t* w, int* did){ char* mime=NULL; unsigned char* bytes=NULL; size_t bl=0; char* urlbuf=strndup(url_start,url_len); if(!urlbuf) return -1; int rc=0; if(parse_data_url(urlbuf,&mime,&bytes,&bl)==0){ const char* ext=ext_from_mime(mime); char* saved=save_bytes_with_ext(bytes,bl,ext); if(saved){ if(write_saved_url(out,cap,w,saved)==0){ *did=1; } else { rc=-1; } free(saved); } } free(bytes); free(mime); free(urlbuf); return rc; } +// ---- JSON array utilities ---- +// NOLINTBEGIN(readability-function-size) +static int find_next_json_object(const char* t, size_t len, size_t* idx, char** out) { + size_t i = *idx; + int depth = 0; + size_t start = 0; + for (; i < len; ++i) { + char c = t[i]; + if (c == '{') { + if (depth == 0) start = i; + depth++; + } else if (c == '}') { + depth--; + if (depth == 0) { + size_t end = i; + size_t L = end - start + 1; + char* obj = malloc(L + 1); + if (!obj) return -1; + memcpy(obj, t + start, L); + obj[L] = '\0'; + *out = obj; + *idx = i + 1; + return 1; + } + } + } + return 0; +} +// NOLINTEND(readability-function-size) -static char* migrate_inline_images_in_body(const char* body, bool* changed){ - if(!body) return NULL; - const char* p=body; size_t outcap=strlen(body)+1; char* out=malloc(outcap); - if(!out) return NULL; - size_t w=0; int did=0; - while(*p){ const char* m=strstr(p,"src=\""); const char* m2=strstr(p,"src='"); const char* hit=NULL; char quote='\0'; if(m && (!m2 || m5 && strncmp(url_start,"data:",5)==0){ if(process_data_url_segment(url_start,url_len,&out,&outcap,&w,&did)){ free(out); return NULL; } } - else { if(ensure_capacity(&out,&outcap,w+url_len+1)){ free(out); return NULL; } buf_copy(out,&w,url_start,url_len); } - if(ensure_capacity(&out,&outcap,w+1+1)){ free(out); return NULL; } out[w++]='"'; p=url_end+1; } - out[w]='\0'; if(changed) *changed=did; return out; } +static int push_str(char*** arr, size_t* cap, size_t* count, char* s) { + if (*count == *cap) { + size_t ncap = (*cap == 0) ? 8 : (*cap * 2); + void* tmp = realloc(*arr, ncap * sizeof(char*)); + if (!tmp) return -1; + *arr = (char**)tmp; + *cap = ncap; + } + (*arr)[(*count)++] = s; + return 0; +} + +// NOLINTBEGIN(readability-function-size) +static int assemble_array(char** objs, size_t count, char** out, size_t* out_len) { + size_t total = 2; + for (size_t k = 0; k < count; ++k) { + total += strlen(objs[k]); + if (k + 1 < count) total++; + } + char* buf = (char*)malloc(total + 1); + if (!buf) return -1; + size_t w = 0; + buf[w++] = '['; + for (size_t k = 0; k < count; ++k) { + size_t L = strlen(objs[k]); + memcpy(buf + w, objs[k], L); + w += L; + if (k + 1 < count) buf[w++] = ','; + } + buf[w++] = ']'; + buf[w] = '\0'; + *out = buf; + if (out_len) *out_len = w; + return 0; +} +// NOLINTEND(readability-function-size) + +static int write_array_to_file(const char* file, char** objs, size_t count) { + char* out = NULL; + size_t w = 0; + if (assemble_array(objs, count, &out, &w)) return -1; + int rc = write_file_all((char*)file, out, w); + free(out); + return rc; +} + +static char* save_bytes_with_ext(const unsigned char* bytes, size_t blen, const char* ext) { + if (!bytes || !blen) return NULL; + const char* updir = "uploads"; + ensure_dir(updir); + char* name = gen_id(); + if (!name) return NULL; + const char* e = (ext && *ext) ? ext : "bin"; + size_t need = strlen(updir) + 1 + strlen(name) + 1 + strlen(e) + 1; + char* path = malloc(need); + if (!path) { + free(name); + return NULL; + } + snprintf(path, need, "%s/%s.%s", updir, name, e); + free(name); + if (write_file_all(path, (const char*)bytes, blen) != 0) { + free(path); + return NULL; + } + return path; +} + +static int ensure_capacity(char** out, size_t* cap, size_t need) { + if (need <= *cap) return 0; + size_t newcap = need + 64; + void* tmp = realloc(*out, newcap); + if (!tmp) return -1; + *out = tmp; + *cap = newcap; + return 0; +} + +static void buf_copy(char* out, size_t* w, const char* src, size_t len) { + memcpy(out + *w, src, len); + *w += len; +} + +static int write_saved_url(char** out, size_t* cap, size_t* w, const char* saved) { + const char* rel = saved; + size_t need = *w + 1 + strlen(rel) + 1; + if (ensure_capacity(out, cap, need)) return -1; + (*out)[(*w)++] = '/'; + memcpy((*out) + *w, rel, strlen(rel)); + *w += strlen(rel); + return 0; +} + +// NOLINTBEGIN(readability-function-size) +static int process_data_url_segment(const char* url_start, size_t url_len, char** out, size_t* cap, + size_t* w, int* did) { + char* mime = NULL; + unsigned char* bytes = NULL; + size_t bl = 0; + char* urlbuf = strndup(url_start, url_len); + if (!urlbuf) return -1; + int rc = 0; + if (parse_data_url(urlbuf, &mime, &bytes, &bl) == 0) { + const char* ext = ext_from_mime(mime); + char* saved = save_bytes_with_ext(bytes, bl, ext); + if (saved) { + if (write_saved_url(out, cap, w, saved) == 0) { + *did = 1; + } else { + rc = -1; + } + free(saved); + } + } + free(bytes); + free(mime); + free(urlbuf); + return rc; +} +// NOLINTEND(readability-function-size) + +static const char* find_src_attr(const char* p, char* quote_out) { + const char* m = strstr(p, "src=\""); + const char* m2 = strstr(p, "src='"); + if (m && (!m2 || m < m2)) { + *quote_out = '"'; + return m; + } + if (m2) { + *quote_out = '\''; + return m2; + } + return NULL; +} + +// NOLINTBEGIN(readability-function-size) +static int handle_src_segment(const char** p_in, const char* hit, char quote, char** out, + size_t* outcap, size_t* w, int* did) { + size_t prefix_len = (size_t)(hit - *p_in); + if (ensure_capacity(out, outcap, *w + prefix_len + 6)) return -1; + buf_copy(*out, w, *p_in, prefix_len); + memcpy(*out + *w, "src=\"", 5); + *w += 5; + const char* url_start = hit + 5; + if (*url_start == '\'' || *url_start == '"') url_start++; + const char endq = (quote == '\'') ? '\'' : '"'; + const char* url_end = strchr(url_start, endq); + if (!url_end) { + size_t L = strlen(url_start); + if (ensure_capacity(out, outcap, *w + L + 1)) return -1; + buf_copy(*out, w, url_start, L); + *p_in = url_start + L; + return 0; + } + size_t url_len = (size_t)(url_end - url_start); + if (url_len > 5 && strncmp(url_start, "data:", 5) == 0) { + if (process_data_url_segment(url_start, url_len, out, outcap, w, did)) return -1; + } else { + if (ensure_capacity(out, outcap, *w + url_len + 1)) return -1; + buf_copy(*out, w, url_start, url_len); + } + if (ensure_capacity(out, outcap, *w + 1)) return -1; + (*out)[(*w)++] = '"'; + *p_in = url_end + 1; + return 0; +} +// NOLINTEND(readability-function-size) + +// NOLINTBEGIN(readability-function-size) +static char* migrate_inline_images_in_body(const char* body, bool* changed) { + if (!body) return NULL; + const char* p = body; + size_t outcap = strlen(body) + 1; + char* out = malloc(outcap); + if (!out) return NULL; + size_t w = 0; + int did = 0; + while (*p) { + char quote = '\0'; + const char* hit = find_src_attr(p, "e); + if (!hit) { + size_t L = strlen(p); + if (ensure_capacity(&out, &outcap, w + L + 1)) { + free(out); + return NULL; + } + buf_copy(out, &w, p, L); + break; + } + if (handle_src_segment(&p, hit, quote, &out, &outcap, &w, &did)) { + free(out); + return NULL; + } + } + out[w] = '\0'; + if (changed) *changed = did; + return out; +} +// NOLINTEND(readability-function-size) // ---- HTTP helpers ---- // removed unused send_str (lint) -static void send_response(int c, int code, const char* status, const char* ctype, const char* body, size_t blen, bool cors){ +static void send_response(int c, int code, const char* status, const char* ctype, const char* body, + size_t blen, bool cors) { char head[SMALL_BUF]; - int n=snprintf(head,sizeof(head), - "HTTP/1.1 %d %s\r\nContent-Type: %s\r\nContent-Length: %zu\r\n%s\r\n", - code, status, ctype?ctype:"text/plain", blen, - cors? "Access-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS\r\nAccess-Control-Allow-Headers: Content-Type\r\n":""); + int n = snprintf(head, sizeof(head), + "HTTP/1.1 %d %s\r\nContent-Type: %s\r\nContent-Length: %zu\r\n%s\r\n", code, + status, ctype ? ctype : "text/plain", blen, + cors ? "Access-Control-Allow-Origin: *\r\n" + "Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS\r\n" + "Access-Control-Allow-Headers: Content-Type\r\n" + : ""); send(c, head, (size_t)n, 0); - if(body && blen) send(c, body, blen, 0); + if (body && blen) send(c, body, blen, 0); } -static const char* guess_mime(const char* path){ const char* ext=strrchr(path,'.'); if(!ext) return "application/octet-stream"; ext++; if(!strcmp(ext,"html")) return "text/html; charset=utf-8"; if(!strcmp(ext,"css")) return "text/css"; if(!strcmp(ext,"js")) return "application/javascript"; if(!strcmp(ext,"png")) return "image/png"; if(!strcmp(ext,"jpg")||!strcmp(ext,"jpeg")) return "image/jpeg"; if(!strcmp(ext,"webp")) return "image/webp"; if(!strcmp(ext,"gif")) return "image/gif"; if(!strcmp(ext,"svg")) return "image/svg+xml"; if(!strcmp(ext,"mp4")) return "video/mp4"; if(!strcmp(ext,"webm")) return "video/webm"; return "application/octet-stream"; } +static const char* guess_mime(const char* path) { + const char* ext = strrchr(path, '.'); + if (!ext) return "application/octet-stream"; + ext++; + if (!strcmp(ext, "html")) return "text/html; charset=utf-8"; + if (!strcmp(ext, "css")) return "text/css"; + if (!strcmp(ext, "js")) return "application/javascript"; + if (!strcmp(ext, "png")) return "image/png"; + if (!strcmp(ext, "jpg") || !strcmp(ext, "jpeg")) return "image/jpeg"; + if (!strcmp(ext, "webp")) return "image/webp"; + if (!strcmp(ext, "gif")) return "image/gif"; + if (!strcmp(ext, "svg")) return "image/svg+xml"; + if (!strcmp(ext, "mp4")) return "video/mp4"; + if (!strcmp(ext, "webm")) return "video/webm"; + return "application/octet-stream"; +} // ---- Data operations (NDJSON-style internal), exposed as JSON array ---- -static char* ltrim_dup(const char* s){ while(*s && isspace((unsigned char)*s)) s++; return strdup(s); } +static char* ltrim_dup(const char* s) { + while (*s && isspace((unsigned char)*s)) s++; + return strdup(s); +} // removed unused list_articles_json (lint) -static char* find_article_by_id(const char* id){ - char* file=data_file(); if(!file) return NULL; - size_t n=0; char* content=read_file_all(file,&n); free(file); if(!content) return NULL; - char* t=ltrim_dup(content); free(content); if(!t) return NULL; if(t[0] != '['){ free(t); return NULL; } +// NOLINTBEGIN(readability-function-size) +static char* find_article_by_id(const char* id) { + char* file = data_file(); + if (!file) return NULL; + size_t n = 0; + char* content = read_file_all(file, &n); + free(file); + if (!content) return NULL; + char* t = ltrim_dup(content); + free(content); + if (!t) return NULL; + if (t[0] != '[') { + free(t); + return NULL; + } // iterate objects - size_t len=strlen(t); size_t i=1; int depth=0; size_t start=0; for(; i=2 && tcontent[clen-1]==']'){ - // insert at front - bool empty = (clen==2); - size_t Lobj=strlen(obj); - size_t newlen = 1 + Lobj + (empty?0:1) + (clen-2) + 1; - char* out=malloc(newlen+1); if(out){ size_t w=0; out[w++]='['; memcpy(out+w,obj,Lobj); w+=Lobj; if(!empty){ out[w++]=','; memcpy(out+w, tcontent+1, clen-2); w+=(clen-2); } out[w++]=']'; out[w]='\0'; write_file_all(file, out, w); free(out); } - } else { // corrupt; overwrite - size_t L=strlen(obj); size_t tot=L+2; char* arr=malloc(tot+1); if(arr){ size_t w=0; arr[w++]='['; memcpy(arr+w,obj,L); w+=L; arr[w++]=']'; arr[w]='\0'; write_file_all(file, arr, w); free(arr); } + char* t = ltrim_dup(content); + free(content); + if (!t) { + free(file); + return -1; + } + if (t[0] != '[') { + FILE* f = fopen(file, "wb"); + if (f) { + fputs("[]", f); + fclose(f); + } + free(file); + free(t); + return -1; + } + size_t i = 1; + size_t len = strlen(t); + char** objs = NULL; + size_t cap = 0, count = 0; + bool found = false; + char* updated_copy = NULL; + while (i < len) { + char* obj = NULL; + int r = find_next_json_object(t, len, &i, &obj); + if (r < 0) { + free(file); + free(t); + return -1; + } + if (r == 0) break; + char* id = json_get_string(obj, "id"); + bool isMatch = id && strcmp(id, match_id) == 0; + free(id); + if (isMatch) { + found = true; + if (!is_delete) { + char* title = json_get_string(obj, "title"); + char* author = json_get_string(obj, "author"); + char* body = json_get_string(obj, "body"); + char* thumb = json_get_string(obj, "thumb"); + char* ptitle = json_get_top_string(patch_json, "title"); + if (ptitle && *ptitle) { + free(title); + title = ptitle; + } else free(ptitle); + char* pauthor = json_get_top_string(patch_json, "author"); + if (pauthor && *pauthor) { + free(author); + author = pauthor; + } else free(pauthor); + char* pbody = json_get_top_string(patch_json, "body"); + if (pbody && *pbody) { + free(body); + body = pbody; + } else free(pbody); + char* pthumb = json_get_top_string(patch_json, "thumb"); + if (pthumb && *pthumb) { + free(thumb); + thumb = pthumb; + } else free(pthumb); + long long createdAt = json_get_number(obj, "\"createdAt\""); + long long updatedAt = now_ms(); + char* obj2 = build_article_json(match_id, title, author, body, thumb, createdAt, updatedAt); + free(title); + free(author); + free(body); + free(thumb); + free(updated_copy); + updated_copy = strdup(obj2); + free(obj); + obj = obj2; + } + } + if (!(isMatch && is_delete)) { + if (push_str(&objs, &cap, &count, obj)) { + free(obj); + free(file); + free(t); + return -1; } } else { - // not an array; overwrite - size_t L=strlen(obj); size_t tot=L+2; char* arr=malloc(tot+1); if(arr){ size_t w=0; arr[w++]='['; memcpy(arr+w,obj,L); w+=L; arr[w++]=']'; arr[w]='\0'; write_file_all(file, arr, w); free(arr); } + free(obj); + } + } + int rc = write_array_to_file(file, objs, count); + for (size_t z = 0; z < count; ++z) free(objs[z]); + free(objs); + free(file); + free(t); + if (found && !is_delete && out_json_updated) + *out_json_updated = updated_copy ? updated_copy : strdup(""); + else free(updated_copy); + return found && rc == 0 ? 0 : -1; +} +// NOLINTEND(readability-function-size) + +// NOLINTBEGIN(readability-function-size) +static char* create_article_from_body(const char* body_json) { + char* title = json_get_top_string(body_json, "title"); + char* author = json_get_top_string(body_json, "author"); + char* b = json_get_top_string(body_json, "body"); + char* th = json_get_top_string(body_json, "thumb"); + char* id = gen_id(); + long long t = now_ms(); + char* obj = build_article_json(id, title, author, b, th, t, 0); + free(title); + free(author); + free(b); + free(th); + if (!id || !obj) { + free(id); + free(obj); + return NULL; + } + char* file = data_file(); + if (!file) { + free(id); + free(obj); + return NULL; + } + size_t n = 0; + char* content = read_file_all(file, &n); + char* out = NULL; + size_t w = 0; + if (!content || n == 0) { + char* arr_items[1] = {obj}; + assemble_array(arr_items, 1, &out, &w); + } else { + char* tcontent = ltrim_dup(content); + free(content); + if (tcontent && tcontent[0] == '[') { + // Prepend new object + size_t i = 1, len = strlen(tcontent); + char** items = NULL; + size_t cap = 0, cnt = 0; + push_str(&items, &cap, &cnt, strdup(obj)); + while (1) { + char* one = NULL; + int r = find_next_json_object(tcontent, len, &i, &one); + if (r <= 0) break; + push_str(&items, &cap, &cnt, one); + } + assemble_array(items, cnt, &out, &w); + for (size_t z = 0; z < cnt; ++z) free(items[z]); + free(items); + } else { + char* arr_items[1] = {obj}; + assemble_array(arr_items, 1, &out, &w); } free(tcontent); } - // persist done; return created object JSON + if (out) write_file_all(file, out, w); + free(out); free(file); free(id); return obj; } +// NOLINTEND(readability-function-size) // ---- request handling ---- -static const char* ext_from_content_type(const char* ct){ if(!ct) return NULL; if(strstr(ct,"image/png")) return "png"; if(strstr(ct,"image/jpeg")) return "jpg"; if(strstr(ct,"image/jpg")) return "jpg"; if(strstr(ct,"image/webp")) return "webp"; if(strstr(ct,"image/gif")) return "gif"; return NULL; } +static const char* ext_from_content_type(const char* ct) { + if (!ct) return NULL; + if (strstr(ct, "image/png")) return "png"; + if (strstr(ct, "image/jpeg")) return "jpg"; + if (strstr(ct, "image/jpg")) return "jpg"; + if (strstr(ct, "image/webp")) return "webp"; + if (strstr(ct, "image/gif")) return "gif"; + return NULL; +} -static const char* get_qparam(const char* path, const char* key){ const char* q=strchr(path,'?'); if(!q) return NULL; q++; size_t klen=strlen(key); while(*q){ if(!strncmp(q,key,klen) && q[klen]=='='){ return q+klen+1; } while(*q && *q!='&') q++; if(*q=='&') q++; } - return NULL; } - -static char* strndup_local(const char* s, size_t n){ char* r=malloc(n+1); if(!r) return NULL; memcpy(r,s,n); r[n]='\0'; return r; } - -static char* save_upload(const char* body, size_t blen, const char* ext_hint){ if(!body||blen==0) return NULL; const char* updir = "uploads"; ensure_dir(updir); char* name=gen_id(); if(!name) return NULL; const char* ext = (ext_hint&&*ext_hint)? ext_hint: "bin"; size_t need=strlen(updir)+1+strlen(name)+1+strlen(ext)+1; char* path=malloc(need); if(!path){ free(name); return NULL; } snprintf(path,need,"%s/%s.%s", updir, name, ext); free(name); if(write_file_all(path, body, blen)!=0){ free(path); return NULL; } return path; } - -static void api_get_articles_array(int c){ - char* file=data_file(); if(!file){ send_response(c,200,"OK","application/json","[]",2,true); return; } - size_t n=0; char* content=read_file_all(file,&n); if(!content){ free(file); send_response(c,200,"OK","application/json","[]",2,true); return; } - char* t=ltrim_dup(content); free(content); if(!t){ free(file); send_response(c,200,"OK","application/json","[]",2,true); return; } - if(t[0] != '['){ free(file); send_response(c,200,"OK","application/json","[]",2,true); free(t); return; } - size_t len=strlen(t); size_t i=1; int depth=0; size_t start=0; size_t count=0, cap=8; char** objs=malloc(cap*sizeof(char*)); if(!objs){ free(t); free(file); send_response(c,500,"Internal Server Error","application/json","",0,true); return; } int changed=0; - for(; ibl+1) api_get_article_by_id(c, path+bl+1); - else send_response(c,404,"Not Found","application/json","",0,true); +// Convert a data: URL into a newly-allocated absolute URL string ("/uploads/...") +// Returns NULL on failure. +// NOLINTBEGIN(readability-function-size) +static char* data_url_to_abs_url(const char* data_url) { + char* mime = NULL; + unsigned char* bytes = NULL; + size_t bl = 0; + if (parse_data_url(data_url, &mime, &bytes, &bl) != 0) { + free(mime); + free(bytes); + return NULL; + } + const char* ext = ext_from_mime(mime); + char* saved = save_bytes_with_ext(bytes, bl, ext); + free(mime); + free(bytes); + if (!saved) return NULL; + size_t L = strlen(saved) + 2; + char* url = malloc(L); + if (!url) { + free(saved); + return NULL; + } + snprintf(url, L, "/%s", saved); + free(saved); + return url; +} +// NOLINTEND(readability-function-size) + +static int migrate_thumb_if_data_url(char** pthumb) { + char* thumb = *pthumb; + if (!thumb || strncmp(thumb, "data:", 5) != 0) return 0; + char* url = data_url_to_abs_url(thumb); + if (!url) return 0; + free(thumb); + *pthumb = url; + return 1; +} + +// Replace inline images inside body HTML in-place. Returns 1 if changed. +static int migrate_body_inplace(char** pbody) { + bool bchanged = false; + char* new_body = migrate_inline_images_in_body(*pbody, &bchanged); + if (new_body && bchanged) { + free(*pbody); + *pbody = new_body; + return 1; + } + free(new_body); + return 0; +} + +static char* rebuild_updated_article(char* old_obj, const char* id, const char* title, + const char* author, const char* body, const char* thumb, + long long createdAt) { + free(old_obj); + return build_article_json(id ? id : "", title ? title : "", author ? author : "", + body ? body : "", thumb ? thumb : "", createdAt, 0); +} + +static char* migrate_one_obj_if_needed(char* obj, int* changed_any) { + char* id = json_get_string(obj, "id"); + char* title = json_get_string(obj, "title"); + char* author = json_get_string(obj, "author"); + char* body_s = json_get_string(obj, "body"); + char* thumb = json_get_string(obj, "thumb"); + long long createdAt = json_get_number(obj, "\"createdAt\""); + int obj_changed = 0; + obj_changed |= migrate_thumb_if_data_url(&thumb); + obj_changed |= migrate_body_inplace(&body_s); + if (obj_changed) { + *changed_any = 1; + obj = rebuild_updated_article(obj, id, title, author, body_s, thumb, createdAt); + } + free(id); + free(title); + free(author); + free(body_s); + free(thumb); + return obj; +} + +// NOLINTBEGIN(readability-function-size) +static void api_get_articles_array(int c) { + char* file = data_file(); + if (!file) { + send_response(c, 200, "OK", "application/json", "[]", 2, true); + return; + } + size_t n = 0; + char* content = read_file_all(file, &n); + if (!content) { + free(file); + send_response(c, 200, "OK", "application/json", "[]", 2, true); + return; + } + char* t = ltrim_dup(content); + free(content); + if (!t) { + free(file); + send_response(c, 200, "OK", "application/json", "[]", 2, true); + return; + } + if (t[0] != '[') { + free(file); + free(t); + send_response(c, 200, "OK", "application/json", "[]", 2, true); + return; + } + size_t i = 1, len = strlen(t); + char** objs = NULL; + size_t cap = 0, count = 0; + int changed = 0; + while (1) { + char* obj = NULL; + int r = find_next_json_object(t, len, &i, &obj); + if (r < 0) { + free(file); + free(t); + send_response(c, 500, "Internal Server Error", "application/json", "", 0, true); + return; + } + if (r == 0) break; + obj = migrate_one_obj_if_needed(obj, &changed); + if (push_str(&objs, &cap, &count, obj)) { + free(obj); + free(file); + free(t); + send_response(c, 500, "Internal Server Error", "application/json", "", 0, true); return; } - if(!strcmp(method,"POST") && !strcmp(path, base)){ api_post_article(c, body); return; } - if(!strcmp(method,"PUT") && path[bl]=='/' && strlen(path)>bl+1){ api_put_article(c, path+bl+1, body); return; } - if(!strcmp(method,"DELETE") && path[bl]=='/' && strlen(path)>bl+1){ api_delete_article(c, path+bl+1); return; } } - if(!strncmp(path, "/api/upload", 12)){ - if(!strcmp(method,"POST")){ api_post_upload(c, path, body, blen, content_type); return; } + char* out = NULL; + size_t w = 0; + if (assemble_array(objs, count, &out, &w)) { + for (size_t z = 0; z < count; ++z) free(objs[z]); + free(objs); + free(file); + free(t); + send_response(c, 500, "Internal Server Error", "application/json", "", 0, true); + return; + } + if (changed) write_file_all(file, out, w); + send_response(c, 200, "OK", "application/json", out, w, true); + for (size_t z = 0; z < count; ++z) free(objs[z]); + free(objs); + free(out); + free(file); + free(t); +} +// NOLINTEND(readability-function-size) + +// NOLINTBEGIN(readability-function-size) +static void api_get_article_by_id(int c, const char* id) { + char* obj = find_article_by_id(id); + if (!obj) { + send_response(c, 404, "Not Found", "application/json", "", 0, true); + return; + } + char* title = json_get_string(obj, "title"); + char* author = json_get_string(obj, "author"); + char* body_s = json_get_string(obj, "body"); + char* thumb = json_get_string(obj, "thumb"); + long long createdAt = json_get_number(obj, "\"createdAt\""); + int obj_changed = 0; + if (thumb && strncmp(thumb, "data:", 5) == 0) { + char* mime = NULL; + unsigned char* bytes = NULL; + size_t bl2 = 0; + if (parse_data_url(thumb, &mime, &bytes, &bl2) == 0) { + const char* ext = ext_from_mime(mime); + char* saved = save_bytes_with_ext(bytes, bl2, ext); + if (saved) { + size_t urlL = strlen(saved) + 2; + char* newthumb = malloc(urlL); + if (newthumb) { + snprintf(newthumb, urlL, "/%s", saved); + free(thumb); + thumb = newthumb; + obj_changed = 1; + } + free(saved); + } + free(mime); + free(bytes); + } + } + bool bchanged = false; + char* new_body = migrate_inline_images_in_body(body_s, &bchanged); + if (new_body && bchanged) { + free(body_s); + body_s = new_body; + obj_changed = 1; + } else { + free(new_body); + } + if (obj_changed) { + char* id_copy = json_get_string(obj, "id"); + char* updated = + build_article_json(id_copy ? id_copy : "", title ? title : "", author ? author : "", + body_s ? body_s : "", thumb ? thumb : "", createdAt, 0); + if (updated) { + rewrite_articles_map(NULL, id_copy, updated, false); + free(updated); + } + free(id_copy); + free(obj); + obj = find_article_by_id(id); + } + free(title); + free(author); + free(body_s); + free(thumb); + size_t L = strlen(obj); + send_response(c, 200, "OK", "application/json", obj, L, true); + free(obj); +} +// NOLINTEND(readability-function-size) + +static void api_post_article(int c, const char* body) { + char* obj = create_article_from_body(body ? body : ""); + if (!obj) { + send_response(c, 400, "Bad Request", "application/json", "", 0, true); + return; + } + size_t L = strlen(obj); + send_response(c, 201, "Created", "application/json", obj, L, true); + free(obj); +} +static void api_put_article(int c, const char* id, const char* body) { + char* updated = NULL; + if (rewrite_articles_map(&updated, id, body ? body : "", false) == 0) { + size_t L = strlen(updated); + send_response(c, 200, "OK", "application/json", updated, L, true); + free(updated); + } else { + send_response(c, 404, "Not Found", "application/json", "", 0, true); + } +} +static void api_delete_article(int c, const char* id) { + if (rewrite_articles_map(NULL, id, NULL, true) == 0) { + send_response(c, 204, "No Content", "application/json", "", 0, true); + } else { + send_response(c, 404, "Not Found", "application/json", "", 0, true); } - send_response(c,404,"Not Found","text/plain","Not Found",9,true); } -static bool safe_path(const char* p){ if(strstr(p, "..")) return false; return true; } +// NOLINTBEGIN(readability-function-size) +static void api_post_upload(int c, const char* path, const char* body, size_t blen, + const char* content_type) { + const char* ext_q = get_qparam(path, "ext"); + const char* ext_from_ct = ext_from_content_type(content_type); + const char* ext = ext_from_ct ? ext_from_ct : (ext_q ? ext_q : "bin"); + size_t elen = 0; + while (elen < 4 && ext[elen] && isalnum((unsigned char)ext[elen])) elen++; + char* ext_safe = strndup_local(ext, elen ? elen : 3); + if (!ext_safe) { + send_response(c, 500, "Internal Server Error", "application/json", "", 0, true); + return; + } + char* saved = save_upload(body, blen, ext_safe); + free(ext_safe); + if (!saved) { + send_response(c, 500, "Internal Server Error", "application/json", "", 0, true); + return; + } + size_t L = strlen(saved) + 20; + char* res = malloc(L); + if (!res) { + free(saved); + send_response(c, 500, "Internal Server Error", "application/json", "", 0, true); + return; + } + snprintf(res, L, "{\"url\":\"/%s\"}", saved); + send_response(c, 201, "Created", "application/json", res, strlen(res), true); + free(res); + free(saved); +} +// NOLINTEND(readability-function-size) -static void normalize_rel_path(const char* path, char* rel, size_t relsz){ if(!strcmp(path,"/")) snprintf(rel, relsz, "%s", "/index.html"); else snprintf(rel, relsz, "%s", path); } -static int read_file_to_buf(const char* full, char** out, size_t* n){ FILE* f=fopen(full,"rb"); if(!f) return -1; fseek(f,0,SEEK_END); long sz=ftell(f); fseek(f,0,SEEK_SET); char* buf=malloc((size_t)sz); if(!buf){ fclose(f); return -1; } *n=fread(buf,1,(size_t)sz,f); fclose(f); *out=buf; return 0; } -static void send_cached_or_plain(int c, const char* rel, const char* mime, const char* buf, size_t n){ int is_upload=(strncmp(rel,"/uploads/",9)==0); if(is_upload){ char head[SMALL_BUF]; int hlen=snprintf(head,sizeof(head),"HTTP/1.1 200 OK\r\nContent-Type: %s\r\nContent-Length: %zu\r\nCache-Control: public, max-age=31536000, immutable\r\n\r\n",mime,(size_t)n); send(c, head, (size_t)hlen, 0); if(n) send(c, buf, n, 0);} else { send_response(c,200,"OK",mime,buf,n,false);} } -static void handle_static(int c, const char* path){ char rel[SMALL_BUF]; normalize_rel_path(path,rel,sizeof(rel)); if(!safe_path(rel)){ send_response(c,403,"Forbidden","text/plain","Forbidden",9,false); return; } char full[SMALL_BUF*2]; snprintf(full,sizeof(full),"%s%s", DOC_ROOT?DOC_ROOT:".", rel); char* buf=NULL; size_t n=0; if(read_file_to_buf(full,&buf,&n)!=0){ send_response(c,404,"Not Found","text/plain","Not Found",9,false); return; } const char* mime=guess_mime(full); send_cached_or_plain(c,rel,mime,buf,n); free(buf); } +// NOLINTBEGIN(readability-function-size) +static void handle_api(int c, const char* method, const char* path, const char* body, size_t blen, + const char* content_type) { + if (!strncmp(method, "OPTIONS", 7)) { + send_response(c, 204, "No Content", "application/json", "", 0, true); + return; + } + const char* base = "/api/articles"; + size_t bl = strlen(base); + if (!strncmp(path, base, bl)) { + if (!strcmp(method, "GET")) { + if (!strcmp(path, base)) api_get_articles_array(c); + else if (path[bl] == '/' && strlen(path) > bl + 1) api_get_article_by_id(c, path + bl + 1); + else send_response(c, 404, "Not Found", "application/json", "", 0, true); + return; + } + if (!strcmp(method, "POST") && !strcmp(path, base)) { + api_post_article(c, body); + return; + } + if (!strcmp(method, "PUT") && path[bl] == '/' && strlen(path) > bl + 1) { + api_put_article(c, path + bl + 1, body); + return; + } + if (!strcmp(method, "DELETE") && path[bl] == '/' && strlen(path) > bl + 1) { + api_delete_article(c, path + bl + 1); + return; + } + } + if (!strncmp(path, "/api/upload", 12)) { + if (!strcmp(method, "POST")) { + api_post_upload(c, path, body, blen, content_type); + return; + } + } + send_response(c, 404, "Not Found", "text/plain", "Not Found", 9, true); +} +// NOLINTEND(readability-function-size) + +static bool safe_path(const char* p) { + if (strstr(p, "..")) return false; + return true; +} + +static void normalize_rel_path(const char* path, char* rel, size_t relsz) { + if (!strcmp(path, "/")) snprintf(rel, relsz, "%s", "/index.html"); + else snprintf(rel, relsz, "%s", path); +} +static int read_file_to_buf(const char* full, char** out, size_t* n) { + FILE* f = fopen(full, "rb"); + if (!f) return -1; + fseek(f, 0, SEEK_END); + long sz = ftell(f); + fseek(f, 0, SEEK_SET); + char* buf = malloc((size_t)sz); + if (!buf) { + fclose(f); + return -1; + } + *n = fread(buf, 1, (size_t)sz, f); + fclose(f); + *out = buf; + return 0; +} +static void send_cached_or_plain(int c, const char* rel, const char* mime, const char* buf, + size_t n) { + int is_upload = (strncmp(rel, "/uploads/", 9) == 0); + if (is_upload) { + char head[SMALL_BUF]; + int hlen = snprintf(head, sizeof(head), + "HTTP/1.1 200 OK\r\nContent-Type: %s\r\nContent-Length: " + "%zu\r\nCache-Control: public, max-age=31536000, immutable\r\n\r\n", + mime, (size_t)n); + send(c, head, (size_t)hlen, 0); + if (n) send(c, buf, n, 0); + } else { + send_response(c, 200, "OK", mime, buf, n, false); + } +} +static void handle_static(int c, const char* path) { + char rel[SMALL_BUF]; + normalize_rel_path(path, rel, sizeof(rel)); + if (!safe_path(rel)) { + send_response(c, 403, "Forbidden", "text/plain", "Forbidden", 9, false); + return; + } + char full[SMALL_BUF * 2]; + snprintf(full, sizeof(full), "%s%s", DOC_ROOT ? DOC_ROOT : ".", rel); + char* buf = NULL; + size_t n = 0; + if (read_file_to_buf(full, &buf, &n) != 0) { + send_response(c, 404, "Not Found", "text/plain", "Not Found", 9, false); + return; + } + const char* mime = guess_mime(full); + send_cached_or_plain(c, rel, mime, buf, n); + free(buf); +} // removed unused read_headers_into -static void parse_request_line(const char* buf, char* method, char* path){ if(sscanf(buf, "%15s %4095s", method, path) < 2){ method[0]='\0'; path[0]='\0'; } } -static void parse_headers(const char* buf, size_t* content_length, char* ctype, size_t ctype_sz){ *content_length=0; ctype[0]='\0'; const char* cl=strcasestr(buf,"Content-Length:"); if(cl) *content_length=strtoul(cl+15,NULL,10); const char* ct=strcasestr(buf,"Content-Type:"); if(ct){ ct+=13; while(*ct==' '||*ct=='\t') ct++; size_t i=0; while(*ct && *ct!='\r' && *ct!='\n' && i header_bytes ? total - header_bytes : 0; char* body=NULL; if(content_length){ body=malloc(content_length+1); if(!body) return NULL; size_t off=0; if(have_body){ size_t cpy = have_body>content_length?content_length:have_body; memcpy(body, buf+header_bytes, cpy); off=cpy; } size_t remain=content_length-off; while(remain>0){ ssize_t rr=recv(c, body+off, remain, 0); if(rr<=0) break; off+=rr; remain-=rr; } body[content_length]='\0'; } return body; } -static void handle_client(int c){ char buf[RECV_BUF]; ssize_t total=0; // read headers - while(true){ ssize_t rcv=recv(c, buf+total, sizeof(buf)-1-total, 0); if(rcv<=0) break; total+=rcv; buf[total]='\0'; if(strstr(buf, "\r\n\r\n")) break; if(total >= (ssize_t)sizeof(buf)-1) break; } - if(total<=0){ close(c); return; } - char method[16]={0}, path[SMALL_BUF]={0}; parse_request_line(buf,method,path); if(!method[0]){ close(c); return; } - size_t content_length=0; char ctype[128]={0}; parse_headers(buf,&content_length,ctype,sizeof(ctype)); - char* body=read_body_if_needed(c,buf,(size_t)total,content_length); - if(!strncmp(path, "/api/", 5)) handle_api(c, method, path, body, content_length, ctype[0]?ctype:NULL); - else if(!strcmp(method,"GET")) handle_static(c, path); - else if(!strcmp(method,"OPTIONS")) send_response(c,204,"No Content","text/plain","",0,false); - else send_response(c,405,"Method Not Allowed","text/plain","",0,false); - free(body); close(c); +static void parse_request_line(const char* buf, char* method, char* path) { + if (sscanf(buf, "%15s %4095s", method, path) < 2) { + method[0] = '\0'; + path[0] = '\0'; + } +} +static void parse_headers(const char* buf, size_t* content_length, char* ctype, size_t ctype_sz) { + *content_length = 0; + ctype[0] = '\0'; + const char* cl = strcasestr(buf, "Content-Length:"); + if (cl) *content_length = strtoul(cl + 15, NULL, 10); + const char* ct = strcasestr(buf, "Content-Type:"); + if (ct) { + ct += 13; + while (*ct == ' ' || *ct == '\t') ct++; + size_t i = 0; + while (*ct && *ct != '\r' && *ct != '\n' && i < ctype_sz - 1) { + ctype[i++] = *ct++; + } + ctype[i] = '\0'; + } +} +// ---- small helpers to keep functions compact ---- +static size_t copy_from_buffer(char* dst, const char* src, size_t available, size_t max_n) { + size_t cpy = available > max_n ? max_n : available; + if (cpy) memcpy(dst, src, cpy); + return cpy; } -int main(int argc, char** argv){ - (void)argc; (void)argv; +static void recv_remaining_body(int c, char* body, size_t off, size_t total_len) { + size_t remain = total_len - off; + while (remain > 0) { + ssize_t rr = recv(c, body + off, remain, 0); + if (rr <= 0) break; + off += (size_t)rr; + remain -= (size_t)rr; + } +} +static char* read_body_if_needed(int c, const char* buf, size_t total, size_t content_length) { + if (!content_length) return NULL; + const char* hdr_end = strstr(buf, "\r\n\r\n"); + size_t header_bytes = hdr_end ? (size_t)(hdr_end - buf) + 4 : total; + size_t have_body = total > header_bytes ? total - header_bytes : 0; + char* body = (char*)malloc(content_length + 1); + if (!body) return NULL; + size_t off = 0; + if (have_body) off = copy_from_buffer(body, buf + header_bytes, have_body, content_length); + if (off < content_length) recv_remaining_body(c, body, off, content_length); + body[content_length] = '\0'; + return body; +} +static ssize_t read_request_headers(int c, char* buf, size_t bufsz) { + ssize_t total = 0; + while (true) { + ssize_t rcv = recv(c, buf + total, bufsz - 1 - (size_t)total, 0); + if (rcv <= 0) break; + total += rcv; + buf[total] = '\0'; + if (strstr(buf, "\r\n\r\n")) break; + if (total >= (ssize_t)bufsz - 1) break; + } + return total; +} + +static void process_request(int c, const char* buf, ssize_t total) { + char method[16] = {0}, path[SMALL_BUF] = {0}; + parse_request_line(buf, method, path); + if (!method[0]) return; + size_t content_length = 0; + char ctype[128] = {0}; + parse_headers(buf, &content_length, ctype, sizeof(ctype)); + char* body = read_body_if_needed(c, buf, (size_t)total, content_length); + if (!strncmp(path, "/api/", 5)) + handle_api(c, method, path, body, content_length, ctype[0] ? ctype : NULL); + else if (!strcmp(method, "GET")) handle_static(c, path); + else if (!strcmp(method, "OPTIONS")) + send_response(c, 204, "No Content", "text/plain", "", 0, false); + else send_response(c, 405, "Method Not Allowed", "text/plain", "", 0, false); + free(body); +} + +static void handle_client(int c) { + char buf[RECV_BUF]; + ssize_t total = read_request_headers(c, buf, sizeof(buf)); + if (total > 0) process_request(c, buf, total); + close(c); +} + +// NOLINTBEGIN(readability-function-size) +int main(int argc, char** argv) { + (void)argc; + (void)argv; signal(SIGINT, on_sigint); srand((unsigned int)time(NULL)); - char cwd[SMALL_BUF]; if(getcwd(cwd,sizeof(cwd))) DOC_ROOT=strdup(cwd); - const char* host = getenv_default("HOST","127.0.0.1"); - int port = atoi(getenv_default("PORT","8000")); if(port<=0) port=8000; + char cwd[SMALL_BUF]; + if (getcwd(cwd, sizeof(cwd))) DOC_ROOT = strdup(cwd); + const char* host = getenv_default("HOST", "127.0.0.1"); + int port = atoi(getenv_default("PORT", "8000")); + if (port <= 0) port = 8000; - int s = socket(AF_INET, SOCK_STREAM, 0); if(s<0){ perror("socket"); return 1; } - int opt=1; setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); - struct sockaddr_in addr = {0}; addr.sin_family=AF_INET; addr.sin_port=htons((uint16_t)port); addr.sin_addr.s_addr=inet_addr(host); - if(bind(s,(struct sockaddr*)&addr,sizeof(addr))<0){ perror("bind"); close(s); return 1; } - if(listen(s,64)<0){ perror("listen"); close(s); return 1; } + int s = socket(AF_INET, SOCK_STREAM, 0); + if (s < 0) { + perror("socket"); + return 1; + } + int opt = 1; + setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); + struct sockaddr_in addr = {0}; + addr.sin_family = AF_INET; + addr.sin_port = htons((uint16_t)port); + addr.sin_addr.s_addr = inet_addr(host); + if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) < 0) { + perror("bind"); + close(s); + return 1; + } + if (listen(s, 64) < 0) { + perror("listen"); + close(s); + return 1; + } printf("Serving Mini Articles (C) on http://%s:%d\n", host, port); - while(!g_stop){ struct sockaddr_in ca; socklen_t calen=sizeof(ca); int c=accept(s,(struct sockaddr*)&ca,&calen); if(c<0){ if(errno==EINTR) break; perror("accept"); continue; } handle_client(c); } + while (!g_stop) { + struct sockaddr_in ca; + socklen_t calen = sizeof(ca); + int c = accept(s, (struct sockaddr*)&ca, &calen); + if (c < 0) { + if (errno == EINTR) break; + perror("accept"); + continue; + } + handle_client(c); + } close(s); return 0; } +// NOLINTEND(readability-function-size) diff --git a/articles/tools/funcsize.awk b/articles/tools/funcsize.awk new file mode 100644 index 00000000..ab58c2c3 --- /dev/null +++ b/articles/tools/funcsize.awk @@ -0,0 +1,31 @@ +BEGIN{ in_func=0; depth=0; start=0; err=0; prev="" } +{ + line=$0 + # track function start when we see an opening brace at top-level and previous non-empty + # line looks like a function signature (ends with ')' and not ';', and not a typedef/struct/enum/union) + for(i=1;i<=length(line);i++){ + c=substr(line,i,1) + if(c=="{"){ + if(depth==0 && !in_func){ + # Heuristic check on previous non-empty trimmed line + t=prev + sub(/^\s+/, "", t); sub(/\s+$/, "", t) + if(t ~ /\)$/ && t !~ /;\s*$/ && t !~ /^(typedef|struct|enum|union)\b/){ + in_func=1; start=NR + } + } + depth++ + } else if(c=="}"){ + depth-- + if(in_func && depth==0){ + lines=NR-start+1 + if(lines>20){ print FILENAME ":" start " function too long: " lines " lines"; err=1 } + in_func=0 + } + } + } + # update previous non-empty line + tmp=line; sub(/^\s+/, "", tmp); sub(/\s+$/, "", tmp) + if(length(tmp)>0){ prev=tmp } +} +END{ if(err) exit 1 } From d4df42efb569267bc4155aa101d7b782c9622d3c Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Mon, 8 Sep 2025 16:05:14 +0200 Subject: [PATCH 6/8] feat: fixed most function size lint errors --- .vscode/tasks.json | 6 + articles/server_c.c | 505 +++++++++++++++++++++++++------------------- 2 files changed, 292 insertions(+), 219 deletions(-) diff --git a/.vscode/tasks.json b/.vscode/tasks.json index cefb3f23..31bb0f79 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -48,6 +48,12 @@ "command": "python -m pip install -r requirements.txt && pytest -q", "group": "build" }, + { + "label": "pytest quick", + "type": "shell", + "command": "python -m pip install -r requirements.txt && pytest -q", + "group": "build" + }, { "label": "pytest quick", "type": "shell", diff --git a/articles/server_c.c b/articles/server_c.c index 271da04d..e2637010 100644 --- a/articles/server_c.c +++ b/articles/server_c.c @@ -113,40 +113,55 @@ static int write_file_all(const char* path, const char* data, size_t len) { // removed unused append_file_line (lint) // ---- JSON helpers (minimal) ---- -// NOLINTBEGIN(readability-function-size) -static char* json_escape(const char* s) { +static size_t json_escaped_len(const char* s) { size_t n = 0; for (const char* p = s; *p; ++p) { if (*p == '"' || *p == '\\' || *p == '\n' || *p == '\r' || *p == '\t') n += 2; else n++; } - char* out = malloc(n + 1); - if (!out) return NULL; - char* w = out; - for (const char* p = s; *p; ++p) { - if (*p == '"') { - *w++ = '\\'; - *w++ = '"'; - } else if (*p == '\\') { - *w++ = '\\'; - *w++ = '\\'; - } else if (*p == '\n') { - *w++ = '\\'; - *w++ = 'n'; - } else if (*p == '\r') { - *w++ = '\\'; - *w++ = 'r'; - } else if (*p == '\t') { - *w++ = '\\'; - *w++ = 't'; - } else { - *w++ = *p; - } + return n; +} + +static inline char* json_append_escaped(char* w, char c) { + switch (c) { + case '"': + *w++ = '\\'; + *w++ = '"'; + break; + case '\\': + *w++ = '\\'; + *w++ = '\\'; + break; + case '\n': + *w++ = '\\'; + *w++ = 'n'; + break; + case '\r': + *w++ = '\\'; + *w++ = 'r'; + break; + case '\t': + *w++ = '\\'; + *w++ = 't'; + break; + default: *w++ = c; break; } + return w; +} + +static void json_escape_into(char* out, const char* s) { + char* w = out; + for (const char* p = s; *p; ++p) w = json_append_escaped(w, *p); *w = '\0'; +} + +static char* json_escape(const char* s) { + size_t n = json_escaped_len(s); + char* out = (char*)malloc(n + 1); + if (!out) return NULL; + json_escape_into(out, s); return out; } -// NOLINTEND(readability-function-size) static const char* skip_ws_commas(const char* p) { while (*p == ' ' || *p == '\n' || *p == '\r' || *p == '\t' || *p == ',') p++; @@ -155,30 +170,25 @@ static const char* skip_ws_commas(const char* p) { // Parse a JSON string starting at v (after the opening quote). // Returns malloc'd string and sets *after_end to the char after closing quote. -// NOLINTBEGIN(readability-function-size) -static char* parse_json_string_value(const char* v, const char** after_end) { - char* out = malloc(strlen(v) + 1); - if (!out) { - *after_end = v; - return NULL; +static inline char json_unescape_char(char c) { + switch (c) { + case '"': + case '\\': + case '/': return c; + case 'n': return '\n'; + case 'r': return '\r'; + case 't': return '\t'; + default: return c; } - size_t w = 0; +} + +static void parse_json_string_core(const char* v, char* out, size_t* w, const char** after_end) { bool esc = false; const char* p = v; for (; *p; ++p) { char c = *p; if (esc) { - if (c == '"' || c == '\\' || c == '/') { - out[w++] = c; - } else if (c == 'n') { - out[w++] = '\n'; - } else if (c == 'r') { - out[w++] = '\r'; - } else if (c == 't') { - out[w++] = '\t'; - } else { - out[w++] = c; - } + out[(*w)++] = json_unescape_char(c); esc = false; continue; } @@ -187,17 +197,26 @@ static char* parse_json_string_value(const char* v, const char** after_end) { continue; } if (c == '"') { - out[w] = '\0'; *after_end = p + 1; - return out; + break; } - out[w++] = c; + out[(*w)++] = c; } + if (!*after_end) *after_end = p; +} + +static char* parse_json_string_value(const char* v, const char** after_end) { + char* out = (char*)malloc(strlen(v) + 1); + if (!out) { + *after_end = v; + return NULL; + } + size_t w = 0; + *after_end = NULL; + parse_json_string_core(v, out, &w, after_end); out[w] = '\0'; - *after_end = p; // unterminated; best effort return out; } -// NOLINTEND(readability-function-size) // NOLINTBEGIN(readability-function-size) static const char* ensure_quoted_key(const char* key, char** to_free, size_t* out_len) { @@ -765,7 +784,30 @@ static char* ltrim_dup(const char* s) { // removed unused list_articles_json (lint) -// NOLINTBEGIN(readability-function-size) +static bool object_id_matches(const char* obj, const char* id) { + char* got = json_get_string(obj, "\"id\""); + bool ok = got && strcmp(got, id) == 0; + free(got); + return ok; +} + +static char* scan_array_for_id(char* t, const char* id) { + size_t len = strlen(t); + size_t i = 1; + while (1) { + char* obj = NULL; + int r = find_next_json_object(t, len, &i, &obj); + if (r <= 0) break; + if (object_id_matches(obj, id)) { + free(t); + return obj; + } + free(obj); + } + free(t); + return NULL; +} + static char* find_article_by_id(const char* id) { char* file = data_file(); if (!file) return NULL; @@ -775,48 +817,12 @@ static char* find_article_by_id(const char* id) { if (!content) return NULL; char* t = ltrim_dup(content); free(content); - if (!t) return NULL; - if (t[0] != '[') { + if (!t || t[0] != '[') { free(t); return NULL; } - // iterate objects - size_t len = strlen(t); - size_t i = 1; - int depth = 0; - size_t start = 0; - for (; i < len; ++i) { - char c = t[i]; - if (c == '{') { - if (depth == 0) start = i; - depth++; - } else if (c == '}') { - depth--; - if (depth == 0) { - size_t end = i; - size_t obj_len = end - start + 1; - char* obj = malloc(obj_len + 1); - if (!obj) { - free(t); - return NULL; - } - memcpy(obj, t + start, obj_len); - obj[obj_len] = '\0'; - char* got = json_get_string(obj, "\"id\""); - int match = got && strcmp(got, id) == 0; - free(got); - if (match) { - free(t); - return obj; - } - free(obj); - } - } - } - free(t); - return NULL; + return scan_array_for_id(t, id); } -// NOLINTEND(readability-function-size) // NOLINTBEGIN(readability-function-size) static int rewrite_articles_map(char** out_json_updated, const char* match_id, @@ -855,8 +861,12 @@ static int rewrite_articles_map(char** out_json_updated, const char* match_id, char* obj = NULL; int r = find_next_json_object(t, len, &i, &obj); if (r < 0) { + // free accumulated objs + for (size_t z = 0; z < count; ++z) free(objs[z]); + free(objs); free(file); free(t); + free(updated_copy); return -1; } if (r == 0) break; @@ -906,8 +916,11 @@ static int rewrite_articles_map(char** out_json_updated, const char* match_id, if (!(isMatch && is_delete)) { if (push_str(&objs, &cap, &count, obj)) { free(obj); + for (size_t z = 0; z < count; ++z) free(objs[z]); + free(objs); free(file); free(t); + free(updated_copy); return -1; } } else { @@ -955,6 +968,7 @@ static char* create_article_from_body(const char* body_json) { char* out = NULL; size_t w = 0; if (!content || n == 0) { + free(content); char* arr_items[1] = {obj}; assemble_array(arr_items, 1, &out, &w); } else { @@ -969,8 +983,21 @@ static char* create_article_from_body(const char* body_json) { while (1) { char* one = NULL; int r = find_next_json_object(tcontent, len, &i, &one); - if (r <= 0) break; - push_str(&items, &cap, &cnt, one); + if (r <= 0) { + // if negative error, free any allocated 'one' + if (r < 0) free(one); + break; + } + if (push_str(&items, &cap, &cnt, one)) { + free(one); + for (size_t z = 0; z < cnt; ++z) free(items[z]); + free(items); + free(tcontent); + free(file); + free(id); + free(obj); + return NULL; + } } assemble_array(items, cnt, &out, &w); for (size_t z = 0; z < cnt; ++z) free(items[z]); @@ -1047,23 +1074,27 @@ static char* save_upload(const char* body, size_t blen, const char* ext_hint) { // Convert a data: URL into a newly-allocated absolute URL string ("/uploads/...") // Returns NULL on failure. -// NOLINTBEGIN(readability-function-size) -static char* data_url_to_abs_url(const char* data_url) { +static int parse_and_save_data_url(const char* data_url, char** out_saved) { char* mime = NULL; unsigned char* bytes = NULL; size_t bl = 0; if (parse_data_url(data_url, &mime, &bytes, &bl) != 0) { free(mime); free(bytes); - return NULL; + return -1; } const char* ext = ext_from_mime(mime); char* saved = save_bytes_with_ext(bytes, bl, ext); free(mime); free(bytes); - if (!saved) return NULL; + if (!saved) return -1; + *out_saved = saved; + return 0; +} + +static char* saved_to_abs_url(char* saved) { size_t L = strlen(saved) + 2; - char* url = malloc(L); + char* url = (char*)malloc(L); if (!url) { free(saved); return NULL; @@ -1072,7 +1103,12 @@ static char* data_url_to_abs_url(const char* data_url) { free(saved); return url; } -// NOLINTEND(readability-function-size) + +static char* data_url_to_abs_url(const char* data_url) { + char* saved = NULL; + if (parse_and_save_data_url(data_url, &saved) != 0) return NULL; + return saved_to_abs_url(saved); +} static int migrate_thumb_if_data_url(char** pthumb) { char* thumb = *pthumb; @@ -1162,6 +1198,8 @@ static void api_get_articles_array(int c) { char* obj = NULL; int r = find_next_json_object(t, len, &i, &obj); if (r < 0) { + for (size_t z = 0; z < count; ++z) free(objs[z]); + free(objs); free(file); free(t); send_response(c, 500, "Internal Server Error", "application/json", "", 0, true); @@ -1171,6 +1209,8 @@ static void api_get_articles_array(int c) { obj = migrate_one_obj_if_needed(obj, &changed); if (push_str(&objs, &cap, &count, obj)) { free(obj); + for (size_t z = 0; z < count; ++z) free(objs[z]); + free(objs); free(file); free(t); send_response(c, 500, "Internal Server Error", "application/json", "", 0, true); @@ -1197,72 +1237,77 @@ static void api_get_articles_array(int c) { } // NOLINTEND(readability-function-size) -// NOLINTBEGIN(readability-function-size) +static void persist_article_update(const char* obj, const char* title, const char* author, + const char* body_s, const char* thumb, long long createdAt) { + char* id_copy = json_get_string(obj, "id"); + char* updated = + build_article_json(id_copy ? id_copy : "", title ? title : "", author ? author : "", + body_s ? body_s : "", thumb ? thumb : "", createdAt, 0); + if (updated) { + rewrite_articles_map(NULL, id_copy, updated, false); + free(updated); + } + free(id_copy); +} + +static int migrate_fields_if_needed(char** obj, char** title, char** author, char** body_s, + char** thumb, long long createdAt) { + int obj_changed = 0; + obj_changed |= migrate_thumb_if_data_url(thumb); + obj_changed |= migrate_body_inplace(body_s); + if (obj_changed) { + persist_article_update(*obj, *title, *author, *body_s, *thumb, createdAt); + free(*obj); + *obj = NULL; + return 1; + } + return 0; +} + +typedef struct { + char* title; + char* author; + char* body; + char* thumb; + long long createdAt; +} ArticleFields; + +static void load_fields(char* obj, ArticleFields* f) { + f->title = json_get_string(obj, "title"); + f->author = json_get_string(obj, "author"); + f->body = json_get_string(obj, "body"); + f->thumb = json_get_string(obj, "thumb"); + f->createdAt = json_get_number(obj, "\"createdAt\""); +} + +static void free_fields(ArticleFields* f) { + free(f->title); + free(f->author); + free(f->body); + free(f->thumb); +} + +static char* maybe_migrate_and_refresh(const char* id, char* obj) { + ArticleFields f; + load_fields(obj, &f); + if (migrate_fields_if_needed(&obj, &f.title, &f.author, &f.body, &f.thumb, f.createdAt)) { + free_fields(&f); + return find_article_by_id(id); + } + free_fields(&f); + return obj; +} + static void api_get_article_by_id(int c, const char* id) { char* obj = find_article_by_id(id); if (!obj) { send_response(c, 404, "Not Found", "application/json", "", 0, true); return; } - char* title = json_get_string(obj, "title"); - char* author = json_get_string(obj, "author"); - char* body_s = json_get_string(obj, "body"); - char* thumb = json_get_string(obj, "thumb"); - long long createdAt = json_get_number(obj, "\"createdAt\""); - int obj_changed = 0; - if (thumb && strncmp(thumb, "data:", 5) == 0) { - char* mime = NULL; - unsigned char* bytes = NULL; - size_t bl2 = 0; - if (parse_data_url(thumb, &mime, &bytes, &bl2) == 0) { - const char* ext = ext_from_mime(mime); - char* saved = save_bytes_with_ext(bytes, bl2, ext); - if (saved) { - size_t urlL = strlen(saved) + 2; - char* newthumb = malloc(urlL); - if (newthumb) { - snprintf(newthumb, urlL, "/%s", saved); - free(thumb); - thumb = newthumb; - obj_changed = 1; - } - free(saved); - } - free(mime); - free(bytes); - } - } - bool bchanged = false; - char* new_body = migrate_inline_images_in_body(body_s, &bchanged); - if (new_body && bchanged) { - free(body_s); - body_s = new_body; - obj_changed = 1; - } else { - free(new_body); - } - if (obj_changed) { - char* id_copy = json_get_string(obj, "id"); - char* updated = - build_article_json(id_copy ? id_copy : "", title ? title : "", author ? author : "", - body_s ? body_s : "", thumb ? thumb : "", createdAt, 0); - if (updated) { - rewrite_articles_map(NULL, id_copy, updated, false); - free(updated); - } - free(id_copy); - free(obj); - obj = find_article_by_id(id); - } - free(title); - free(author); - free(body_s); - free(thumb); - size_t L = strlen(obj); - send_response(c, 200, "OK", "application/json", obj, L, true); + obj = maybe_migrate_and_refresh(id, obj); + send_response(c, 200, "OK", "application/json", obj, strlen(obj), true); free(obj); } -// NOLINTEND(readability-function-size) static void api_post_article(int c, const char* body) { char* obj = create_article_from_body(body ? body : ""); @@ -1292,27 +1337,18 @@ static void api_delete_article(int c, const char* id) { } } -// NOLINTBEGIN(readability-function-size) -static void api_post_upload(int c, const char* path, const char* body, size_t blen, - const char* content_type) { +static char* choose_ext_from(const char* path, const char* content_type) { const char* ext_q = get_qparam(path, "ext"); const char* ext_from_ct = ext_from_content_type(content_type); const char* ext = ext_from_ct ? ext_from_ct : (ext_q ? ext_q : "bin"); size_t elen = 0; while (elen < 4 && ext[elen] && isalnum((unsigned char)ext[elen])) elen++; - char* ext_safe = strndup_local(ext, elen ? elen : 3); - if (!ext_safe) { - send_response(c, 500, "Internal Server Error", "application/json", "", 0, true); - return; - } - char* saved = save_upload(body, blen, ext_safe); - free(ext_safe); - if (!saved) { - send_response(c, 500, "Internal Server Error", "application/json", "", 0, true); - return; - } + return strndup_local(ext, elen ? elen : 3); +} + +static void respond_saved_upload(int c, char* saved) { size_t L = strlen(saved) + 20; - char* res = malloc(L); + char* res = (char*)malloc(L); if (!res) { free(saved); send_response(c, 500, "Internal Server Error", "application/json", "", 0, true); @@ -1323,9 +1359,53 @@ static void api_post_upload(int c, const char* path, const char* body, size_t bl free(res); free(saved); } -// NOLINTEND(readability-function-size) -// NOLINTBEGIN(readability-function-size) +static void api_post_upload(int c, const char* path, const char* body, size_t blen, + const char* content_type) { + char* ext_safe = choose_ext_from(path, content_type); + if (!ext_safe) { + send_response(c, 500, "Internal Server Error", "application/json", "", 0, true); + return; + } + char* saved = save_upload(body, blen, ext_safe); + free(ext_safe); + if (!saved) { + send_response(c, 500, "Internal Server Error", "application/json", "", 0, true); + return; + } + respond_saved_upload(c, saved); +} + +static bool path_has_id_suffix(const char* path, size_t base_len) { + return path[base_len] == '/' && strlen(path) > base_len + 1; +} + +static bool is_method(const char* m, const char* want) { return strcmp(m, want) == 0; } + +static void dispatch_articles_get(int c, const char* path) { + const char* base = "/api/articles"; + size_t bl = strlen(base); + if (strcmp(path, base) == 0) api_get_articles_array(c); + else if (path_has_id_suffix(path, bl)) api_get_article_by_id(c, path + bl + 1); + else send_response(c, 404, "Not Found", "application/json", "", 0, true); +} + +static void dispatch_articles_mut(int c, const char* method, const char* path, const char* body) { + const char* base = "/api/articles"; + size_t bl = strlen(base); + if (is_method(method, "POST") && strcmp(path, base) == 0) api_post_article(c, body); + else if (is_method(method, "PUT") && path_has_id_suffix(path, bl)) + api_put_article(c, path + bl + 1, body); + else if (is_method(method, "DELETE") && path_has_id_suffix(path, bl)) + api_delete_article(c, path + bl + 1); +} + +static void dispatch_upload(int c, const char* method, const char* path, const char* body, + size_t blen, const char* content_type) { + if (strncmp(path, "/api/upload", 12) != 0) return; + if (is_method(method, "POST")) api_post_upload(c, path, body, blen, content_type); +} + static void handle_api(int c, const char* method, const char* path, const char* body, size_t blen, const char* content_type) { if (!strncmp(method, "OPTIONS", 7)) { @@ -1334,35 +1414,14 @@ static void handle_api(int c, const char* method, const char* path, const char* } const char* base = "/api/articles"; size_t bl = strlen(base); - if (!strncmp(path, base, bl)) { - if (!strcmp(method, "GET")) { - if (!strcmp(path, base)) api_get_articles_array(c); - else if (path[bl] == '/' && strlen(path) > bl + 1) api_get_article_by_id(c, path + bl + 1); - else send_response(c, 404, "Not Found", "application/json", "", 0, true); - return; - } - if (!strcmp(method, "POST") && !strcmp(path, base)) { - api_post_article(c, body); - return; - } - if (!strcmp(method, "PUT") && path[bl] == '/' && strlen(path) > bl + 1) { - api_put_article(c, path + bl + 1, body); - return; - } - if (!strcmp(method, "DELETE") && path[bl] == '/' && strlen(path) > bl + 1) { - api_delete_article(c, path + bl + 1); - return; - } + if (strncmp(path, base, bl) == 0) { + if (is_method(method, "GET")) dispatch_articles_get(c, path); + else dispatch_articles_mut(c, method, path, body); } - if (!strncmp(path, "/api/upload", 12)) { - if (!strcmp(method, "POST")) { - api_post_upload(c, path, body, blen, content_type); - return; - } - } - send_response(c, 404, "Not Found", "text/plain", "Not Found", 9, true); + dispatch_upload(c, method, path, body, blen, content_type); + if (strncmp(path, "/api/", 5) == 0) + send_response(c, 404, "Not Found", "text/plain", "Not Found", 9, true); } -// NOLINTEND(readability-function-size) static bool safe_path(const char* p) { if (strstr(p, "..")) return false; @@ -1513,23 +1572,9 @@ static void handle_client(int c) { close(c); } -// NOLINTBEGIN(readability-function-size) -int main(int argc, char** argv) { - (void)argc; - (void)argv; - signal(SIGINT, on_sigint); - srand((unsigned int)time(NULL)); - char cwd[SMALL_BUF]; - if (getcwd(cwd, sizeof(cwd))) DOC_ROOT = strdup(cwd); - const char* host = getenv_default("HOST", "127.0.0.1"); - int port = atoi(getenv_default("PORT", "8000")); - if (port <= 0) port = 8000; - +static int server_socket_init(const char* host, int port) { int s = socket(AF_INET, SOCK_STREAM, 0); - if (s < 0) { - perror("socket"); - return 1; - } + if (s < 0) return -1; int opt = 1; setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)); struct sockaddr_in addr = {0}; @@ -1537,17 +1582,17 @@ int main(int argc, char** argv) { addr.sin_port = htons((uint16_t)port); addr.sin_addr.s_addr = inet_addr(host); if (bind(s, (struct sockaddr*)&addr, sizeof(addr)) < 0) { - perror("bind"); close(s); - return 1; + return -1; } if (listen(s, 64) < 0) { - perror("listen"); close(s); - return 1; + return -1; } - printf("Serving Mini Articles (C) on http://%s:%d\n", host, port); + return s; +} +static void server_loop(int s) { while (!g_stop) { struct sockaddr_in ca; socklen_t calen = sizeof(ca); @@ -1559,7 +1604,29 @@ int main(int argc, char** argv) { } handle_client(c); } +} + +static void init_runtime(void) { + signal(SIGINT, on_sigint); + srand((unsigned int)time(NULL)); + char cwd[SMALL_BUF]; + if (getcwd(cwd, sizeof(cwd))) DOC_ROOT = strdup(cwd); +} + +int main(int argc, char** argv) { + (void)argc; + (void)argv; + init_runtime(); + const char* host = getenv_default("HOST", "127.0.0.1"); + int port = atoi(getenv_default("PORT", "8000")); + if (port <= 0) port = 8000; + int s = server_socket_init(host, port); + if (s < 0) { + perror("server"); + return 1; + } + printf("Serving Mini Articles (C) on http://%s:%d\n", host, port); + server_loop(s); close(s); return 0; } -// NOLINTEND(readability-function-size) From da76b39f69dca5f7f6876795047c5f68251118e7 Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Mon, 8 Sep 2025 16:10:08 +0200 Subject: [PATCH 7/8] chore: more lint fixes --- articles/server_c.c | 62 +++++++++++++++------------------------------ 1 file changed, 21 insertions(+), 41 deletions(-) diff --git a/articles/server_c.c b/articles/server_c.c index e2637010..e9e784c0 100644 --- a/articles/server_c.c +++ b/articles/server_c.c @@ -123,30 +123,11 @@ static size_t json_escaped_len(const char* s) { } static inline char* json_append_escaped(char* w, char c) { - switch (c) { - case '"': - *w++ = '\\'; - *w++ = '"'; - break; - case '\\': - *w++ = '\\'; - *w++ = '\\'; - break; - case '\n': - *w++ = '\\'; - *w++ = 'n'; - break; - case '\r': - *w++ = '\\'; - *w++ = 'r'; - break; - case '\t': - *w++ = '\\'; - *w++ = 't'; - break; - default: *w++ = c; break; - } - return w; + if (c == '"' || c == '\\') { *w++ = '\\'; *w++ = c; return w; } + if (c == '\n') { *w++ = '\\'; *w++ = 'n'; return w; } + if (c == '\r') { *w++ = '\\'; *w++ = 'r'; return w; } + if (c == '\t') { *w++ = '\\'; *w++ = 't'; return w; } + *w++ = c; return w; } static void json_escape_into(char* out, const char* s) { @@ -183,23 +164,12 @@ static inline char json_unescape_char(char c) { } static void parse_json_string_core(const char* v, char* out, size_t* w, const char** after_end) { - bool esc = false; - const char* p = v; + bool esc = false; const char* p = v; for (; *p; ++p) { char c = *p; - if (esc) { - out[(*w)++] = json_unescape_char(c); - esc = false; - continue; - } - if (c == '\\') { - esc = true; - continue; - } - if (c == '"') { - *after_end = p + 1; - break; - } + if (esc) { out[(*w)++] = json_unescape_char(c); esc = false; continue; } + if (c == '\\') { esc = true; continue; } + if (c == '"') { *after_end = p + 1; break; } out[(*w)++] = c; } if (!*after_end) *after_end = p; @@ -979,7 +949,17 @@ static char* create_article_from_body(const char* body_json) { size_t i = 1, len = strlen(tcontent); char** items = NULL; size_t cap = 0, cnt = 0; - push_str(&items, &cap, &cnt, strdup(obj)); + { + char* dup = strdup(obj); + if (!dup || push_str(&items, &cap, &cnt, dup)) { + free(dup); + free(tcontent); + free(file); + free(id); + free(obj); + return NULL; + } + } while (1) { char* one = NULL; int r = find_next_json_object(tcontent, len, &i, &one); @@ -1272,7 +1252,7 @@ typedef struct { long long createdAt; } ArticleFields; -static void load_fields(char* obj, ArticleFields* f) { +static void load_fields(const char* obj, ArticleFields* f) { f->title = json_get_string(obj, "title"); f->author = json_get_string(obj, "author"); f->body = json_get_string(obj, "body"); From e12f0c902172d4b269928cda823e2a0aacee4c87 Mon Sep 17 00:00:00 2001 From: Krzysztof Rudnicki Date: Mon, 8 Sep 2025 16:58:17 +0200 Subject: [PATCH 8/8] wip: opening learner --- C/opening_learner/Makefile | 30 +++ C/opening_learner/README.md | 26 +++ C/opening_learner/check_build.sh | 28 +++ C/opening_learner/chess.c | 296 ++++++++++++++++++++++++++++++ C/opening_learner/chess.h | 56 ++++++ C/opening_learner/chess.o | Bin 0 -> 13784 bytes C/opening_learner/engine.c | 141 ++++++++++++++ C/opening_learner/engine.h | 34 ++++ C/opening_learner/engine.o | Bin 0 -> 7920 bytes C/opening_learner/gui.c | 158 ++++++++++++++++ C/opening_learner/gui.h | 36 ++++ C/opening_learner/gui.o | Bin 0 -> 7152 bytes C/opening_learner/main.c | 193 +++++++++++++++++++ C/opening_learner/main.o | Bin 0 -> 7984 bytes C/opening_learner/mistakes.c | 65 +++++++ C/opening_learner/mistakes.h | 28 +++ C/opening_learner/mistakes.o | Bin 0 -> 4016 bytes C/opening_learner/mistakes.txt | 12 ++ C/opening_learner/opening_learner | Bin 0 -> 40168 bytes C/opening_learner/run.sh | 43 +++++ 20 files changed, 1146 insertions(+) create mode 100644 C/opening_learner/Makefile create mode 100644 C/opening_learner/README.md create mode 100755 C/opening_learner/check_build.sh create mode 100644 C/opening_learner/chess.c create mode 100644 C/opening_learner/chess.h create mode 100644 C/opening_learner/chess.o create mode 100644 C/opening_learner/engine.c create mode 100644 C/opening_learner/engine.h create mode 100644 C/opening_learner/engine.o create mode 100644 C/opening_learner/gui.c create mode 100644 C/opening_learner/gui.h create mode 100644 C/opening_learner/gui.o create mode 100644 C/opening_learner/main.c create mode 100644 C/opening_learner/main.o create mode 100644 C/opening_learner/mistakes.c create mode 100644 C/opening_learner/mistakes.h create mode 100644 C/opening_learner/mistakes.o create mode 100644 C/opening_learner/mistakes.txt create mode 100755 C/opening_learner/opening_learner create mode 100755 C/opening_learner/run.sh diff --git a/C/opening_learner/Makefile b/C/opening_learner/Makefile new file mode 100644 index 00000000..aa0546e1 --- /dev/null +++ b/C/opening_learner/Makefile @@ -0,0 +1,30 @@ +CC := gcc +CFLAGS := -O2 -Wall -Wextra -std=c11 + +# SDL2 flags: require sdl2-config (no fallback) +SDL2CONF := $(shell command -v sdl2-config 2>/dev/null) +ifeq ($(SDL2CONF),) +$(error sdl2-config not found. Please install SDL2 development package.) +endif +SDL_CFLAGS := $(shell sdl2-config --cflags) +SDL_LDFLAGS := $(shell sdl2-config --libs) + +SRC := main.c gui.c engine.c chess.c mistakes.c +OBJ := $(SRC:.c=.o) +BIN := opening_learner + +.PHONY: all clean run + +all: $(BIN) + +$(BIN): $(OBJ) + $(CC) $(CFLAGS) -o $@ $^ $(SDL_LDFLAGS) + +%.o: %.c + $(CC) $(CFLAGS) $(SDL_CFLAGS) -c -o $@ $< + +run: $(BIN) + ./$(BIN) + +clean: + rm -f $(OBJ) $(BIN) diff --git a/C/opening_learner/README.md b/C/opening_learner/README.md new file mode 100644 index 00000000..31176c2a --- /dev/null +++ b/C/opening_learner/README.md @@ -0,0 +1,26 @@ +# Opening Learner (C + SDL2) + +- Click a piece, then click a destination to move. +- Thick board outline, board uses non-pure colors. +- Uses local Stockfish or asmfish via UCI. +- Logs mistakes to `mistakes.txt` and lets you revisit them with the `m` key. + +Build and check: + +```sh +./check_build.sh +``` + +Run: + +```sh +./opening_learner +``` + +Tips: +- ESC clears selection. +- Press `m` to cycle to a stored mistake position and practice the best move there. +- If you play Black, the board flips so Black is at the bottom. + +Notes: +- Rendering avoids TTF dependency; pieces are clear, high-contrast geometric glyphs. diff --git a/C/opening_learner/check_build.sh b/C/opening_learner/check_build.sh new file mode 100755 index 00000000..e6ab5b2a --- /dev/null +++ b/C/opening_learner/check_build.sh @@ -0,0 +1,28 @@ +#!/usr/bin/env bash +set -euo pipefail + +cd "$(dirname "$0")" + +echo "Checking for engine (stockfish or asmfish)" +if command -v stockfish >/dev/null 2>&1; then + echo "Found stockfish" +elif command -v asmfish >/dev/null 2>&1; then + echo "Found asmfish" +else + echo "Error: Neither stockfish nor asmfish found in PATH." >&2 + exit 1 +fi + +echo "Checking for SDL2 dev (sdl2-config)" +if command -v sdl2-config >/dev/null 2>&1; then + echo "Found sdl2-config" +else + echo "Error: sdl2-config not found. Install SDL2 dev (e.g., libsdl2-dev)." >&2 + exit 2 +fi + +echo "Building project" +make clean +make -j + +echo "Build OK" diff --git a/C/opening_learner/chess.c b/C/opening_learner/chess.c new file mode 100644 index 00000000..f5e0ed20 --- /dev/null +++ b/C/opening_learner/chess.c @@ -0,0 +1,296 @@ +#include "chess.h" +#include +#include +#include +#include + +static const int knight_offsets[8] = {15, 17, -15, -17, 10, -10, 6, -6}; +static const int bishop_dirs[4] = {9, 7, -9, -7}; +static const int rook_dirs[4] = {8, -8, 1, -1}; +static const int king_dirs[8] = {8,-8,1,-1,9,7,-9,-7}; + +static inline int file_of(int sq){return sq%8;} +static inline int rank_of(int sq){return sq/8;} +static inline bool on_board(int sq){return sq>=0 && sq<64;} +static inline bool same_color(char a, char b){return (isupper(a)&&isupper(b))||(islower(a)&&islower(b));} +static inline bool is_white(char p){return isupper((unsigned char)p);} + +void chess_init_start(Position *pos){ + // a1..h1 (0..7) are white back rank; rank 8 at indexes 56..63 are black back rank + const char *start = "RNBQKBNRPPPPPPPP................................pppppppprnbqkbnr"; + for (int i=0;i<64;i++) pos->board[i] = start[i]; + pos->white_to_move = true; + pos->castle_wk = pos->castle_wq = pos->castle_bk = pos->castle_bq = true; + pos->ep_square = -1; pos->halfmove_clock = 0; pos->fullmove_number = 1; +} + +void chess_copy(Position *dst, const Position *src){ *dst = *src; } + +static bool is_empty(const Position *p, int sq){ return p->board[sq]=='.'; } + +bool chess_square_attacked(const Position *pos, int sq, bool by_white){ + // pawns + int r = rank_of(sq), f = file_of(sq); + if (by_white){ + int s1 = (r-1)*8 + (f-1); if (f>0 && r>0 && on_board(s1) && pos->board[s1]=='P') return true; + int s2 = (r-1)*8 + (f+1); if (f<7 && r>0 && on_board(s2) && pos->board[s2]=='P') return true; + } else { + int s1 = (r+1)*8 + (f-1); if (f>0 && r<7 && on_board(s1) && pos->board[s1]=='p') return true; + int s2 = (r+1)*8 + (f+1); if (f<7 && r<7 && on_board(s2) && pos->board[s2]=='p') return true; + } + // knights + for (int i=0;i<8;i++){ + int t = sq + knight_offsets[i]; + if (!on_board(t)) continue; + int df = file_of(t)-f; int dr = rank_of(t)-r; if (df< -2||df>2||dr<-2||dr>2) continue; // edge wrap guard + char pc = pos->board[t]; + if (by_white && pc=='N') return true; + if (!by_white && pc=='n') return true; + } + // bishops/queens + for (int d=0; d<4; d++){ + int off = bishop_dirs[d]; int t = sq + off; + while (on_board(t) && abs(file_of(t)-f)==abs(rank_of(t)-r)){ + char pc = pos->board[t]; + if (pc!='.') { if (by_white && (pc=='B'||pc=='Q')) return true; if (!by_white && (pc=='b'||pc=='q')) return true; break; } + t += off; + } + } + // rooks/queens + for (int d=0; d<4; d++){ + int off = rook_dirs[d]; int t = sq + off; + while (on_board(t) && (file_of(t)==f || rank_of(t)==r)){ + char pc = pos->board[t]; + if (pc!='.') { if (by_white && (pc=='R'||pc=='Q')) return true; if (!by_white && (pc=='r'||pc=='q')) return true; break; } + t += off; + } + } + // king + for (int i=0;i<8;i++){ + int t=sq+king_dirs[i]; if (!on_board(t)) continue; if (abs(file_of(t)-f)>1||abs(rank_of(t)-r)>1) continue; + char pc = pos->board[t]; if (by_white && pc=='K') return true; if (!by_white && pc=='k') return true; + } + return false; +} + +bool chess_is_in_check(const Position *pos, bool white){ + int ks = -1; char k = white? 'K' : 'k'; + for (int i=0;i<64;i++) if (pos->board[i]==k) { ks = i; break; } + if (ks==-1) return false; // malformed + return chess_square_attacked(pos, ks, !white); +} + +static void add_move_if_legal(const Position *pos, int from, int to, char promo, Move *out, size_t *n, size_t max){ + if (*n >= max) return; + Position tmp; chess_copy(&tmp, pos); + Move m = {0}; m.from=from; m.to=to; m.promo=promo; m.moved=pos->board[from]; m.captured=pos->board[to]; + int prev_ep = tmp.ep_square; m.prev_ep = prev_ep; m.prev_wk=tmp.castle_wk; m.prev_wq=tmp.castle_wq; m.prev_bk=tmp.castle_bk; m.prev_bq=tmp.castle_bq; m.prev_halfmove=tmp.halfmove_clock; + if (!chess_make_move(&tmp, &m)) return; + if (chess_is_in_check(&tmp, !tmp.white_to_move)) return; // after make, side switched + out[(*n)++] = m; // store pseudo move with added flags from make_move +} + +size_t chess_generate_legal_moves(const Position *pos, Move *out, size_t max){ + size_t n=0; + bool white = pos->white_to_move; + for (int sq=0; sq<64; sq++){ + char p = pos->board[sq]; if (p=='.') continue; if (white != is_white(p)) continue; + int f=file_of(sq), r=rank_of(sq); + switch (tolower(p)){ + case 'p': { + int dir = white? 8 : -8; int start_rank = white? 1 : 6; int prom_rank = white? 6 : 1; + int one = sq + dir; + if (on_board(one) && is_empty(pos, one)){ + if (r==prom_rank){ + const char *pr = white? "QRBN" : "qrbn"; + for (int i=0;i<4;i++) add_move_if_legal(pos, sq, one, pr[i], out, &n, max); + } else add_move_if_legal(pos, sq, one, 0, out, &n, max); + // two + int two = sq + 2*dir; if (r==start_rank && is_empty(pos, two)) add_move_if_legal(pos, sq, two, 0, out, &n, max); + } + // captures + int caps[2] = { dir+1, dir-1 }; + for (int i=0;i<2;i++){ + int t = sq + caps[i]; if (!on_board(t)) continue; if (abs(file_of(t)-f)!=1) continue; + if (!is_empty(pos, t) && !same_color(pos->board[sq], pos->board[t])){ + if (r==prom_rank){ const char *pr = white? "QRBN" : "qrbn"; for (int j=0;j<4;j++) add_move_if_legal(pos, sq, t, pr[j], out, &n, max); } + else add_move_if_legal(pos, sq, t, 0, out, &n, max); + } + } + // en passant + if (pos->ep_square!=-1){ + int ep = pos->ep_square; if (abs(file_of(ep)-f)==1 && (ep == sq+dir+1 || ep==sq+dir-1)) add_move_if_legal(pos, sq, ep, 0, out, &n, max); + } + } break; + case 'n': { + for (int i=0;i<8;i++){ + int t = sq + knight_offsets[i]; if (!on_board(t)) continue; if (abs(file_of(t)-f)>2 || abs(rank_of(t)-r)>2) continue; if (!is_empty(pos, t) && same_color(p, pos->board[t])) continue; add_move_if_legal(pos, sq, t, 0, out, &n, max); + } + } break; + case 'b': { + for (int d=0; d<4; d++){ + int off = bishop_dirs[d]; int t = sq + off; while (on_board(t) && abs(file_of(t)-f)==abs(rank_of(t)-r)){ + if (!is_empty(pos, t)){ if (!same_color(p, pos->board[t])) add_move_if_legal(pos, sq, t, 0, out, &n, max); break; } + add_move_if_legal(pos, sq, t, 0, out, &n, max); t += off; + } + } + } break; + case 'r': { + for (int d=0; d<4; d++){ + int off = rook_dirs[d]; int t = sq + off; while (on_board(t) && (file_of(t)==f || rank_of(t)==r)){ + if (!is_empty(pos, t)){ if (!same_color(p, pos->board[t])) add_move_if_legal(pos, sq, t, 0, out, &n, max); break; } + add_move_if_legal(pos, sq, t, 0, out, &n, max); t += off; + } + } + } break; + case 'q': { + for (int d=0; d<4; d++){ + int off = bishop_dirs[d]; int t = sq + off; while (on_board(t) && abs(file_of(t)-f)==abs(rank_of(t)-r)){ + if (!is_empty(pos, t)){ if (!same_color(p, pos->board[t])) add_move_if_legal(pos, sq, t, 0, out, &n, max); break; } + add_move_if_legal(pos, sq, t, 0, out, &n, max); t += off; + } + } + for (int d=0; d<4; d++){ + int off = rook_dirs[d]; int t = sq + off; while (on_board(t) && (file_of(t)==f || rank_of(t)==r)){ + if (!is_empty(pos, t)){ if (!same_color(p, pos->board[t])) add_move_if_legal(pos, sq, t, 0, out, &n, max); break; } + add_move_if_legal(pos, sq, t, 0, out, &n, max); t += off; + } + } + } break; + case 'k': { + for (int i=0;i<8;i++){ + int t = sq + king_dirs[i]; if (!on_board(t)) continue; if (abs(file_of(t)-f)>1||abs(rank_of(t)-r)>1) continue; if (!is_empty(pos, t) && same_color(p, pos->board[t])) continue; add_move_if_legal(pos, sq, t, 0, out, &n, max); + } + // castling + if (white){ + if (pos->castle_wk && pos->board[5]=='.' && pos->board[6]=='.' && !chess_square_attacked(pos,4,false) && !chess_square_attacked(pos,5,false) && !chess_square_attacked(pos,6,false)) add_move_if_legal(pos, 4, 6, 0, out, &n, max); + if (pos->castle_wq && pos->board[3]=='.' && pos->board[2]=='.' && pos->board[1]=='.' && !chess_square_attacked(pos,4,false) && !chess_square_attacked(pos,3,false) && !chess_square_attacked(pos,2,false)) add_move_if_legal(pos, 4, 2, 0, out, &n, max); + } else { + if (pos->castle_bk && pos->board[61]=='.' && pos->board[62]=='.' && !chess_square_attacked(pos,60,true) && !chess_square_attacked(pos,61,true) && !chess_square_attacked(pos,62,true)) add_move_if_legal(pos, 60, 62, 0, out, &n, max); + if (pos->castle_bq && pos->board[59]=='.' && pos->board[58]=='.' && pos->board[57]=='.' && !chess_square_attacked(pos,60,true) && !chess_square_attacked(pos,59,true) && !chess_square_attacked(pos,58,true)) add_move_if_legal(pos, 60, 58, 0, out, &n, max); + } + } break; + } + } + return n; +} + +bool chess_make_move(Position *pos, Move *m){ + m->is_castle=false; m->is_enpassant=false; + char p = pos->board[m->from]; char tgt = pos->board[m->to]; + if (p=='.') return false; + // handle special: en passant capture + if (tolower(p)=='p' && m->to == pos->ep_square && file_of(m->to)!=file_of(m->from) && tgt=='.'){ + m->is_enpassant = true; + int cap_sq = pos->white_to_move ? (m->to - 8) : (m->to + 8); + m->captured = pos->board[cap_sq]; pos->board[cap_sq]='.'; + } + + // move piece + pos->board[m->to] = p; + pos->board[m->from] = '.'; + + // promotion + if (tolower(p)=='p' && m->promo){ pos->board[m->to] = m->promo; } + + // castling rook move + if (tolower(p)=='k'){ + int from = m->from, to = m->to; + if (from==4 && to==6){ pos->board[5]='R'; pos->board[7]='.'; m->is_castle=true; } + else if (from==4 && to==2){ pos->board[3]='R'; pos->board[0]='.'; m->is_castle=true; } + else if (from==60 && to==62){ pos->board[61]='r'; pos->board[63]='.'; m->is_castle=true; } + else if (from==60 && to==58){ pos->board[59]='r'; pos->board[56]='.'; m->is_castle=true; } + } + + // update castling rights + if (m->from==0||m->to==0) pos->castle_wq=false; + if (m->from==7||m->to==7) pos->castle_wk=false; + if (m->from==56||m->to==56) pos->castle_bq=false; + if (m->from==63||m->to==63) pos->castle_bk=false; + if (tolower(p)=='k'){ if (is_white(p)) { pos->castle_wk=pos->castle_wq=false;} else {pos->castle_bk=pos->castle_bq=false;} } + + // update ep square + pos->ep_square = -1; + if (tolower(p)=='p'){ + int df = rank_of(m->to) - rank_of(m->from); + if (df==2 || df==-2){ pos->ep_square = (m->from + m->to)/2; } + } + + // halfmove clock + if (tolower(p)=='p' || tgt!='.') pos->halfmove_clock = 0; else pos->halfmove_clock++; + + // side to move + pos->white_to_move = !pos->white_to_move; if (pos->white_to_move) pos->fullmove_number++; + + return true; +} + +void chess_unmake_move(Position *pos, const Move *m){ + pos->white_to_move = !pos->white_to_move; if (!pos->white_to_move) pos->fullmove_number--; + // restore halfmove/flags + pos->ep_square = m->prev_ep; + pos->castle_wk = m->prev_wk; pos->castle_wq = m->prev_wq; pos->castle_bk = m->prev_bk; pos->castle_bq = m->prev_bq; + pos->halfmove_clock = m->prev_halfmove; + + char p = m->moved; + // undo promotions + if (tolower(p)=='p' && m->promo){ p = is_white(p)? 'P':'p'; } + + pos->board[m->from] = p; + pos->board[m->to] = m->captured? m->captured : '.'; + if (m->is_enpassant){ int cap_sq = pos->white_to_move ? (m->to - 8) : (m->to + 8); pos->board[m->to]='.'; pos->board[cap_sq]= m->captured; } + if (m->is_castle){ + if (m->from==4 && m->to==6){ pos->board[7]='R'; pos->board[5]='.'; } + else if (m->from==4 && m->to==2){ pos->board[0]='R'; pos->board[3]='.'; } + else if (m->from==60 && m->to==62){ pos->board[63]='r'; pos->board[61]='.'; } + else if (m->from==60 && m->to==58){ pos->board[56]='r'; pos->board[59]='.'; } + } +} + +void sq_to_coord(int sq, int *file, int *rank){ if (file) *file=file_of(sq); if (rank) *rank=rank_of(sq); } +int coord_to_sq(int file, int rank){ return rank*8 + file; } + +void move_to_uci(const Move *m, char buf[8]){ + int f1=file_of(m->from), r1=rank_of(m->from), f2=file_of(m->to), r2=rank_of(m->to); + buf[0]='a'+f1; buf[1]='1'+r1; buf[2]='a'+f2; buf[3]='1'+r2; int i=4; + if (m->promo){ buf[i++]=tolower(m->promo); } + buf[i]='\0'; +} + +bool parse_uci_move(const char *s, const Position *pos, Move *out){ + if (!s || strlen(s)<4) return false; + int f1=s[0]-'a', r1=s[1]-'1', f2=s[2]-'a', r2=s[3]-'1'; if (f1<0||f1>7||f2<0||f2>7||r1<0||r1>7||r2<0||r2>7) return false; + int from = r1*8+f1, to=r2*8+f2; char promo = s[4]? s[4]:0; if (promo) promo = pos->white_to_move? toupper((unsigned char)promo):tolower((unsigned char)promo); + Move list[MAX_MOVES]; size_t n = chess_generate_legal_moves(pos, list, MAX_MOVES); + for (size_t i=0;ipromo = promo? promo : list[i].promo; return true; } } + return false; +} + +bool chess_to_fen(const Position *pos, char *out, size_t outsz){ + char buf[256]; int idx=0; + for (int r=7;r>=0;r--){ + int empty=0; + for (int f=0;f<8;f++){ + char p = pos->board[r*8+f]; + if (p=='.') empty++; else { if (empty){ buf[idx++]= '0'+empty; empty=0; } buf[idx++]=p; } + } + if (empty) buf[idx++]= '0'+empty; + if (r) buf[idx++]='/'; + } + buf[idx++]=' '; + buf[idx++]= pos->white_to_move ? 'w':'b'; + buf[idx++]=' '; + int start=idx; + if (pos->castle_wk) buf[idx++]='K'; + if (pos->castle_wq) buf[idx++]='Q'; + if (pos->castle_bk) buf[idx++]='k'; + if (pos->castle_bq) buf[idx++]='q'; + if (idx==start) buf[idx++]='-'; + buf[idx++]=' '; + if (pos->ep_square==-1){ buf[idx++]='-'; } + else { int f=file_of(pos->ep_square), r=rank_of(pos->ep_square); buf[idx++]='a'+f; buf[idx++]='1'+r; } + idx += snprintf(buf+idx, sizeof(buf)-idx, " %d %d", pos->halfmove_clock, pos->fullmove_number); + buf[idx]='\0'; + snprintf(out, outsz, "%s", buf); + return true; +} diff --git a/C/opening_learner/chess.h b/C/opening_learner/chess.h new file mode 100644 index 00000000..9c2c9f61 --- /dev/null +++ b/C/opening_learner/chess.h @@ -0,0 +1,56 @@ +#ifndef CHESS_H +#define CHESS_H + +#include +#include + +// Board is 64 chars, a1=0, b1=1, ..., h8=63 +// Pieces: 'P','N','B','R','Q','K' for white, lowercase for black, '.' empty + +typedef struct { + char board[64]; + bool white_to_move; + bool castle_wk, castle_wq, castle_bk, castle_bq; + int ep_square; // -1 if none + int halfmove_clock; + int fullmove_number; +} Position; + +typedef struct { + int from, to; + char promo; // 0 or 'q','r','b','n' (lowercase for black) + char captured; // piece captured or 0 + char moved; // piece moved + bool is_castle; + bool is_enpassant; + int prev_ep; + bool prev_wk, prev_wq, prev_bk, prev_bq; + int prev_halfmove; +} Move; + +void chess_init_start(Position *pos); +void chess_copy(Position *dst, const Position *src); + +// Move gen and make/unmake +size_t chess_generate_legal_moves(const Position *pos, Move *out, size_t max); +bool chess_make_move(Position *pos, Move *m); +void chess_unmake_move(Position *pos, const Move *m); + +// Utility +bool chess_is_in_check(const Position *pos, bool white); +bool chess_square_attacked(const Position *pos, int sq, bool by_white); + +// Conversions +void sq_to_coord(int sq, int *file, int *rank); +int coord_to_sq(int file, int rank); + +// UCI strings like e2e4, with optional promotion char +void move_to_uci(const Move *m, char buf[8]); +bool parse_uci_move(const char *s, const Position *pos, Move *out); + +// FEN +bool chess_to_fen(const Position *pos, char *out, size_t outsz); + +#define MAX_MOVES 256 + +#endif diff --git a/C/opening_learner/chess.o b/C/opening_learner/chess.o new file mode 100644 index 0000000000000000000000000000000000000000..ee51fb838b1b6ceba9e8e0b51b37f8c6a0eda08c GIT binary patch literal 13784 zcmcIp4Rlo1oqsPGNPu)+g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ine.c new file mode 100644 index 00000000..c4ce1b0e --- /dev/null +++ b/C/opening_learner/engine.c @@ -0,0 +1,141 @@ +#include "engine.h" +#include +#include +#include +#include +#include +#include +#include +#include + +static bool spawn_process(const char *path, Engine *e){ + int inpipe[2], outpipe[2]; + if (pipe(inpipe)<0 || pipe(outpipe)<0) return false; + pid_t pid = fork(); + if (pid < 0) return false; + if (pid == 0){ + dup2(inpipe[0], STDIN_FILENO); // child stdin from inpipe[0] + dup2(outpipe[1], STDOUT_FILENO); // child stdout to outpipe[1] + dup2(outpipe[1], STDERR_FILENO); + close(inpipe[0]); close(inpipe[1]); close(outpipe[0]); close(outpipe[1]); + execlp(path, path, (char*)NULL); + _exit(127); + } + // parent + close(inpipe[0]); close(outpipe[1]); + e->pid = pid; e->in_fd = inpipe[1]; e->out_fd = outpipe[0]; e->ready=false; + // make out_fd non-blocking for reads with polling + int flags = fcntl(e->out_fd, F_GETFL, 0); fcntl(e->out_fd, F_SETFL, flags | O_NONBLOCK); + return true; +} + +static bool try_start(Engine *e, const char *name){ + if (!spawn_process(name, e)) return false; + // send UCI init + engine_cmd(e, "uci\n"); + char buf[4096]; int attempts=50; // ~5s total + while (attempts--){ + usleep(100000); + ssize_t n = read(e->out_fd, buf, sizeof(buf)-1); + if (n>0){ + buf[n]='\0'; + if (strstr(buf, "uciok")) { e->ready=true; break; } + } + } + if (!e->ready){ engine_stop(e); return false; } + engine_cmd(e, "isready\n"); + attempts=50; + while (attempts--){ + usleep(100000); + ssize_t n = read(e->out_fd, buf, sizeof(buf)-1); + if (n>0){ buf[n]='\0'; if (strstr(buf, "readyok")) break; } + } + return e->ready; +} + +bool engine_start(Engine *e){ + memset(e, 0, sizeof(*e)); e->pid=-1; e->in_fd=-1; e->out_fd=-1; e->ready=false; + if (try_start(e, "stockfish")) return true; + if (try_start(e, "asmfish")) return true; + return false; +} + +void engine_stop(Engine *e){ + if (e->in_fd!=-1){ write(e->in_fd, "quit\n", 5); close(e->in_fd); } + if (e->out_fd!=-1) close(e->out_fd); + if (e->pid>0){ int status; waitpid(e->pid, &status, 0); } + e->pid=-1; e->in_fd=e->out_fd=-1; e->ready=false; +} + +bool engine_cmd(Engine *e, const char *cmd){ + if (e->in_fd==-1) return false; + size_t len = strlen(cmd); + ssize_t n = write(e->in_fd, cmd, len); + if (n < 0) { + return false; + } + return n == (ssize_t)len; +} + +static void position_to_uci(const Position *pos, char *out, size_t outsz){ + // Send starting position and moves list by comparing with startpos; for simplicity, use FEN always. + char fen[256]; chess_to_fen(pos, fen, sizeof(fen)); + snprintf(out, outsz, "position fen %s\n", fen); +} + +size_t engine_get_top_moves(Engine *e, const Position *pos, EngineMove *out, size_t max){ + if (!e->ready) return 0; + char cmd[512]; position_to_uci(pos, cmd, sizeof(cmd)); engine_cmd(e, cmd); + // ask multiPV up to max (cap at 5 as requested) + size_t req = max; if (req>5) req=5; char go[128]; snprintf(go, sizeof(go), "setoption name MultiPV value %zu\n", req); engine_cmd(e, go); + engine_cmd(e, "go movetime 400\n"); + char buf[8192]; size_t count=0; int attempts=50; + while (attempts--){ + usleep(100000); + ssize_t n = read(e->out_fd, buf, sizeof(buf)-1); + if (n<=0) continue; + buf[n]='\0'; + char *line = strtok(buf, "\n"); + while (line){ + if (strncmp(line, "info ", 5)==0){ + // parse "info ... multipv X score cp Y ... pv " + char *mpv = strstr(line, " multipv "); char *score = strstr(line, " score "); char *pv = strstr(line, " pv "); + if (mpv && score && pv){ + int idx = atoi(mpv+9); if (idx>=1 && (size_t)idx<=req){ + int cp=0; if (strstr(score, "cp ")) cp = atoi(strstr(score, "cp ")+3); + char mv[8]={0}; + sscanf(pv+4, "%7s", mv); + size_t i = (size_t)idx-1; if (icount) count=i+1; } + } + } + } else if (strncmp(line, "bestmove ", 9)==0){ attempts=0; break; } + line = strtok(NULL, "\n"); + } + } + // simple sort by score descending (best to worst), keeping empties at end + for (size_t i=0;i out[i].score_cp){ EngineMove tmp=out[i]; out[i]=out[j]; out[j]=tmp; } + } + } + return count; +} + +bool engine_get_best_move(Engine *e, const Position *pos, char out_uci[8]){ + if (!e->ready) return false; + char cmd[512]; position_to_uci(pos, cmd, sizeof(cmd)); engine_cmd(e, cmd); + engine_cmd(e, "go movetime 300\n"); + char buf[4096]; int attempts=50; + while (attempts--){ + usleep(100000); + ssize_t n = read(e->out_fd, buf, sizeof(buf)-1); + if (n<=0) continue; + buf[n]='\0'; + char *line = strtok(buf, "\n"); + while (line){ + if (strncmp(line, "bestmove ", 9)==0){ sscanf(line+9, "%7s", out_uci); return true; } + line = strtok(NULL, "\n"); + } + } + return false; +} diff --git a/C/opening_learner/engine.h b/C/opening_learner/engine.h new file mode 100644 index 00000000..092918fd --- /dev/null +++ b/C/opening_learner/engine.h @@ -0,0 +1,34 @@ +#ifndef ENGINE_H +#define ENGINE_H + +#include +#include + +#include "chess.h" + +typedef struct { + int score_cp; // centipawns relative to side to move + char uci[8]; +} EngineMove; + +typedef struct { + int pid; + int in_fd; // write to engine stdin + int out_fd; // read from engine stdout + bool ready; +} Engine; + +// Start engine: tries stockfish, then asmfish. Returns false if none. +bool engine_start(Engine *e); +void engine_stop(Engine *e); + +// Synchronous send command +bool engine_cmd(Engine *e, const char *cmd); + +// Ask for top N moves from a position (short fixed time). Returns count. +size_t engine_get_top_moves(Engine *e, const Position *pos, EngineMove *out, size_t max); + +// Ask for best move only. +bool engine_get_best_move(Engine *e, const Position *pos, char out_uci[8]); + +#endif diff --git a/C/opening_learner/engine.o b/C/opening_learner/engine.o new file mode 100644 index 0000000000000000000000000000000000000000..e400c89a000e4fead1eedb84e1f3145f63ad0002 GIT binary patch literal 7920 zcmbtYeQaCR6~9TG)=h(Lz60%+M{%L!!?V+NB+yNHiPOAn?m)D3Or@B_d2YNowrfAP zDU>F~NyWn$0s;vQHX*@Z8`1<~Y;+LZq-~gBQdTiwlxdST0aaSMNvs=di+9d__c|w! zJWboK?ECS%zjN-n_nvd_wMV)l8*6K7m`pY7TWr21QO1HR=Iiae+RnVp!!~P$lfHhf zxN`LxK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Ob1rMNdG&cuGOKyJ^F(c1?lJj-}FAZuAY*&8tQVR-5G_Zqz{(3i`x zKsJpHq=y3Vl%-90sm9VnLwX7%QfWgEz&|QC@<*v???~kXnQS_vXN^4|iu(0|Xgmvl z-B@P--?;)BaH=9)s`}bRtswSAc1T>d@ePQ+Xvf>d5B*sok9P`=Bh}#>0@F3{MO<8C z2=O#6i4qW{*p@1aK8@7>RXzl=sMGpi0V*QN@$q*pO>%r$wnOJ)^7r850MaU}sOEB7 zQvO!x!FsUDuqqD-d5i^`q?|1Ae*{^lydMjBiecT(9RLhn8~vQ7XNA1oE@{8yjzR@> z+T~}3{5`e-?eh15>|*}o6MqQFxJLiMca@Zv{ +#include + +const SDL_Color COLOR_LIGHT = { 238, 238, 210, 255 }; // light square (not pure white) +const SDL_Color COLOR_DARK = { 118, 150, 86, 255 }; // dark square (not pure black) +const SDL_Color COLOR_GRID = { 20, 20, 20, 255 }; // thick outline +const SDL_Color COLOR_SEL = { 200, 50, 50, 200 }; // selection highlight +const SDL_Color COLOR_TEXT = { 10, 10, 10, 255 }; + +static void set_color(SDL_Renderer *r, SDL_Color c) { + SDL_SetRenderDrawColor(r, c.r, c.g, c.b, c.a); +} + +bool gui_init(Gui *g, int w, int h, const char *title) { + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + fprintf(stderr, "SDL_Init error: %s\n", SDL_GetError()); + return false; + } + g->window = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE); + if (!g->window) { + fprintf(stderr, "SDL_CreateWindow error: %s\n", SDL_GetError()); + return false; + } + g->renderer = SDL_CreateRenderer(g->window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + if (!g->renderer) { + fprintf(stderr, "SDL_CreateRenderer error: %s\n", SDL_GetError()); + return false; + } + g->win_w = w; g->win_h = h; g->flipped = false; + return true; +} + +void gui_destroy(Gui *g) { + if (g->renderer) SDL_DestroyRenderer(g->renderer); + if (g->window) SDL_DestroyWindow(g->window); + SDL_Quit(); +} + +void gui_set_flipped(Gui *g, bool flipped) { g->flipped = flipped; } + +static void draw_rect(SDL_Renderer *r, int x, int y, int w, int h, SDL_Color c) { + set_color(r, c); + SDL_Rect rc = { x, y, w, h }; + SDL_RenderFillRect(r, &rc); +} + +static void draw_outline(SDL_Renderer *r, int x, int y, int w, int h, int thickness, SDL_Color c) { + set_color(r, c); + for (int i=0;i= 'A' && p <= 'Z') ? (SDL_Color){250, 250, 250, 255} : (SDL_Color){30, 30, 30, 255}; + SDL_Color glyph = (p >= 'A' && p <= 'Z') ? (SDL_Color){30, 30, 30, 255} : (SDL_Color){240, 240, 240, 255}; + // Base disk + draw_rect(r, x+size*0.15, y+size*0.15, (int)(size*0.7), (int)(size*0.7), fill); + // Glyph: draw a simple letter-like mark + set_color(r, glyph); + // vertical bar + SDL_Rect bar1 = { x + size/2 - size/16, y + size/3, size/8, size/3 }; + SDL_RenderFillRect(r, &bar1); + // top bar + SDL_Rect bar2 = { x + size/3, y + size/3 - size/10, size/3, size/10 }; + SDL_RenderFillRect(r, &bar2); +} + +void gui_draw(Gui *g, const char board[64], const GuiSelection *sel, const char *status_line) { + SDL_GetWindowSize(g->window, &g->win_w, &g->win_h); + + set_color(g->renderer, (SDL_Color){ 35, 35, 35, 255 }); + SDL_RenderClear(g->renderer); + + int size = (g->win_w < g->win_h ? g->win_w : g->win_h) - 40; // margins + if (size < 200) size = 200; + int cell = size / 8; + size = cell * 8; + int ox = (g->win_w - size)/2; + int oy = (g->win_h - size)/2; + + // Board outline (thick) + draw_outline(g->renderer, ox-6, oy-6, size+12, size+12, 6, COLOR_GRID); + + // Squares + for (int r=0;r<8;r++) { + for (int f=0;f<8;f++) { + int idx = g->flipped ? (63 - (r*8+f)) : (r*8+f); + SDL_Color c = ((r+f)&1) ? COLOR_DARK : COLOR_LIGHT; + draw_rect(g->renderer, ox + f*cell, oy + r*cell, cell, cell, c); + + char p = board[idx]; + if (p != '.' && p != '\0') { + draw_piece_letter(g->renderer, ox + f*cell, oy + r*cell, cell, p); + } + } + } + + // Selection overlay + if (sel && sel->clicked && sel->from_sq >= 0) { + int s = sel->from_sq; + int rr = g->flipped ? 7 - (s/8) : (s/8); + int ff = g->flipped ? 7 - (s%8) : (s%8); + draw_outline(g->renderer, ox + ff*cell+2, oy + rr*cell+2, cell-4, cell-4, 3, COLOR_SEL); + } + + // Status strip + draw_outline(g->renderer, 10, g->win_h - 40, g->win_w - 20, 30, 2, COLOR_GRID); + // Without TTF, we can't render text; draw a minimal indicator bar to signal state. + // If status_line indicates success/failure, alter color. + SDL_Color bar = { 80, 120, 200, 255 }; + if (status_line && status_line[0]) { + if (SDL_strstr(status_line, "Correct")) bar = (SDL_Color){80, 200, 120, 255}; + else if (SDL_strstr(status_line, "Wrong")) bar = (SDL_Color){200, 80, 80, 255}; + } + draw_rect(g->renderer, 12, g->win_h - 38, g->win_w - 24, 26, bar); + + SDL_RenderPresent(g->renderer); +} + +int gui_coord_to_sq(Gui *g, int x, int y) { + int w, h; SDL_GetWindowSize(g->window, &w, &h); + int size = (w < h ? w : h) - 40; if (size < 200) size = 200; + int cell = size / 8; size = cell * 8; + int ox = (w - size)/2; int oy = (h - size)/2; + if (x < ox || y < oy || x >= ox+size || y >= oy+size) return -1; + int f = (x - ox) / cell; + int r = (y - oy) / cell; + int sq = r*8 + f; + if (g->flipped) sq = 63 - sq; + return sq; +} + +bool gui_poll_move(Gui *g, GuiSelection *sel, bool *quit_requested, int *key_out) { + SDL_Event e; + bool updated = false; + if (key_out) *key_out = 0; + while (SDL_PollEvent(&e)) { + if (e.type == SDL_QUIT) { if (quit_requested) *quit_requested = true; } + else if (e.type == SDL_WINDOWEVENT && e.window.event == SDL_WINDOWEVENT_SIZE_CHANGED) { + updated = true; + } else if (e.type == SDL_MOUSEBUTTONDOWN && e.button.button == SDL_BUTTON_LEFT) { + int sq = gui_coord_to_sq(g, e.button.x, e.button.y); + if (sq >= 0) { + if (!sel->clicked) { sel->from_sq = sq; sel->to_sq = -1; sel->clicked = true; } + else { sel->to_sq = sq; updated = true; } + } + } else if (e.type == SDL_KEYDOWN) { + if (e.key.keysym.sym == SDLK_ESCAPE) { sel->clicked = false; sel->from_sq = sel->to_sq = -1; updated = true; } + if (key_out) *key_out = e.key.keysym.sym; + } + } + return updated; +} diff --git a/C/opening_learner/gui.h b/C/opening_learner/gui.h new file mode 100644 index 00000000..c4096579 --- /dev/null +++ b/C/opening_learner/gui.h @@ -0,0 +1,36 @@ +#ifndef GUI_H +#define GUI_H + +#include +#include + +typedef struct { + SDL_Window *window; + SDL_Renderer *renderer; + int win_w, win_h; + bool flipped; // true if black at bottom +} Gui; + +typedef struct { + int from_sq; // 0..63 or -1 + int to_sq; // 0..63 or -1 + char promo; // 'q','r','b','n' or 0 + bool clicked; +} GuiSelection; + +bool gui_init(Gui *g, int w, int h, const char *title); +void gui_destroy(Gui *g); +void gui_set_flipped(Gui *g, bool flipped); +void gui_draw(Gui *g, const char board[64], const GuiSelection *sel, const char *status_line); +// Returns true if something changed. If a key was pressed, key_out receives SDL_Keycode else 0. +bool gui_poll_move(Gui *g, GuiSelection *sel, bool *quit_requested, int *key_out); +int gui_coord_to_sq(Gui *g, int x, int y); + +// colors +extern const SDL_Color COLOR_LIGHT; +extern const SDL_Color COLOR_DARK; +extern const SDL_Color COLOR_GRID; +extern const SDL_Color COLOR_SEL; +extern const SDL_Color COLOR_TEXT; + +#endif diff --git a/C/opening_learner/gui.o b/C/opening_learner/gui.o new file mode 100644 index 0000000000000000000000000000000000000000..46d7ba4f8b4806a55fc2c53f25dfeb7029ee9405 GIT binary patch literal 7152 zcmbVQ4^Uji8Gi?tm=BNG&`45W}4A<#uBoKaq8H>R0Kj25~~BTQAWp%+G-$)NsJvsqV@Xw_U)4A zB{z2Z4Y&L4Z@>Na+ud)!Zy(!g{Ixj_ha$)!kHwfg;g)gnVtEa~K8NGN7% z5wF-t*$~IehH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3boRq>iGG#0yCQh;Yi95;p{%sXt)D?fSRaaJ&7ZHry`%c^3Sy4*V8CMCgJvd z_=OF(%fDj7?eeFy;J1k?YB1h>uvZOHhFrZO8Lnw@)vA4 zeF;nX5|IeVPP@O2*>JnOM?8p0{sx<#_AK}@8(w6S*XRQsM0)J{DUT8nPH!=3XP?b} zdT&eI3!aGfjee3k1S4_zZrPE6%l8JoSBRv%d~ckz;8~m>4lu8Q>3cLXl=)JhVBk_=3&xX2thFXQ$$eMX5pJWjxe}=Lb3Q345V2?t$}c?c|%B8|NmEB45g(CH=k8hCAea7 zjgf! z7+*R&(qg8>c)Ck=lJm>@esoISF6YehwU#N{jzM&FGuQ7D<1_7&cFVc8gCU)n<157Y z){F@=$3KFxm-D}I>%&gz6BCjDkxDt9Vn5O4<|h1i;-UPEa4dTJ(3WP7r|_5Qdw>*E b9NbWb{TWTX?Jqxm9R+KUwBKr7dRG4h9Oi*( literal 0 HcmV?d00001 diff --git a/C/opening_learner/main.c b/C/opening_learner/main.c new file mode 100644 index 00000000..993f89e0 --- /dev/null +++ b/C/opening_learner/main.c @@ -0,0 +1,193 @@ +#include +#include +#include +#include +#include +#include + +#include "gui.h" +#include "chess.h" +#include "engine.h" +#include "mistakes.h" + +typedef struct { + Position pos; + Engine engine; + Gui gui; + MistakeList mistakes; + bool replay_mode; // allow revisiting mistakes + size_t replay_index; +} App; + +static void append_uci(char *line, size_t sz, const char *mv){ + if (line[0]) strncat(line, " ", sz-1); + strncat(line, mv, sz-1); +} + +static void position_push_move_text(Position *pos, const Move *m, char *line, size_t lsz){ + (void)pos; + char u[8]; move_to_uci(m, u); + append_uci(line, lsz, u); +} + +static void collect_all_legal_uci(const Position *pos, char list[][8], size_t *n, size_t max){ + Move mv[MAX_MOVES]; size_t k = chess_generate_legal_moves(pos, mv, MAX_MOVES); + *n = 0; + for (size_t i=0;i0)? (rand()%wsum) : 0; + size_t pick=0; for (size_t i=0;i0 && parse_uci_move(props[0].uci, &app.pos, &m)) pick=0; else { snprintf(status, sizeof(status), "No engine move"); quit=true; continue; } + } + chess_make_move(&app.pos, &m); + position_push_move_text(&app.pos, &m, line_uci, sizeof(line_uci)); + awaiting_player = true; + // 6. Ask engine for optimal response from player + engine_get_best_move(&app.engine, &app.pos, expected_player_move); + snprintf(status, sizeof(status), "Your turn"); + continue; + } + + // Player move input + int key=0; + bool updated = gui_poll_move(&app.gui, &sel, &quit, &key); + if (quit) break; + if (key=='m' || key=='M'){ + // enter simple replay: load a mistake line and restart from it to the position where best move is required + if (app.mistakes.count>0){ + if (app.replay_index >= app.mistakes.count) app.replay_index = 0; + Mistake *mk = &app.mistakes.items[app.replay_index++]; + // reset and play the line moves to reach the mistake position + chess_init_start(&app.pos); + char tmp[512]; snprintf(tmp, sizeof(tmp), "%s", mk->line); + char *tok = strtok(tmp, " "); + while (tok){ Move m; if (parse_uci_move(tok, &app.pos, &m)) chess_make_move(&app.pos, &m); tok = strtok(NULL, " "); } + snprintf(status, sizeof(status), "Practice: best is %s", mk->best_move); + strncpy(expected_player_move, mk->best_move, sizeof(expected_player_move)); + awaiting_player = true; + gui_set_flipped(&app.gui, !app.pos.white_to_move); // if it's black to move, flip so black bottom + } + } + if (updated && sel.clicked && sel.to_sq>=0){ + Move list[MAX_MOVES]; size_t n = chess_generate_legal_moves(&app.pos, list, MAX_MOVES); + bool moved=false; Move chosen={0}; + for (size_t i=0;iClJ*N1ne}Wl4Cb(<_rOxCX|4q*e6@HENM|11)r#PWgi=bV_Gvz%CO6B+fuv8j^m{GKKIJ5cwL0CWKPzzPGn4 zKUCj8o$ie9c7OZ!?c29+KXm?1oAI?dJ|9!b$F65HB}Ex~?BbbxSY*R&AzQ$Pzsj=n zC#RuF3F$b`9k1h#t^2vNT7MT2A6u)W+>GHz?JXgp!|1kYtxWSGX*Sz8?02a z_bQicjS9qAn`Nb}Roaba-0m|iEn}y;`(yD;pO#EnS~N2t^08Dl*`p;=-O)s1D5$MV zn9+<0{eoeMgqBL0(63C94Wa4C*CXp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mistakes.c b/C/opening_learner/mistakes.c new file mode 100644 index 00000000..89f76b05 --- /dev/null +++ b/C/opening_learner/mistakes.c @@ -0,0 +1,65 @@ +#include "mistakes.h" +#include +#include +#include + +void mistakes_init(MistakeList *ml) { + ml->items = NULL; ml->count = 0; ml->cap = 0; +} + +void mistakes_free(MistakeList *ml) { + free(ml->items); ml->items = NULL; ml->count = ml->cap = 0; +} + +static void ensure_cap(MistakeList *ml, size_t need) { + if (need <= ml->cap) return; + size_t ncap = ml->cap ? ml->cap*2 : 16; + while (ncap < need) ncap *= 2; + Mistake *ni = realloc(ml->items, ncap * sizeof(Mistake)); + if (!ni) return; // OOM silently ignored + ml->items = ni; ml->cap = ncap; +} + +void mistakes_add(MistakeList *ml, const char *fen, const char *best_move, const char *line) { + ensure_cap(ml, ml->count+1); + Mistake *m = &ml->items[ml->count++]; + snprintf(m->fen, sizeof(m->fen), "%s", fen); + snprintf(m->best_move, sizeof(m->best_move), "%s", best_move); + snprintf(m->line, sizeof(m->line), "%s", line); +} + +bool mistakes_save(const MistakeList *ml, const char *path) { + FILE *f = fopen(path, "w"); + if (!f) return false; + for (size_t i=0;icount;i++) { + const Mistake *m = &ml->items[i]; + fprintf(f, "FEN:%s\nBEST:%s\nLINE:%s\n.\n", m->fen, m->best_move, m->line); + } + fclose(f); return true; +} + +bool mistakes_load(MistakeList *ml, const char *path) { + FILE *f = fopen(path, "r"); + if (!f) return false; + char buf[1024]; char fen[128] = ""; char best[16] = ""; char line[512] = ""; + while (fgets(buf, sizeof(buf), f)) { + if (strncmp(buf, "FEN:", 4) == 0) { + // copy up to 127 chars, strip newline + size_t l = strnlen(buf+4, sizeof(fen)-1); + memcpy(fen, buf+4, l); fen[l]='\0'; + if (l && fen[l-1]=='\n') fen[l-1]='\0'; + } else if (strncmp(buf, "BEST:", 5) == 0) { + size_t l = strnlen(buf+5, sizeof(best)-1); + memcpy(best, buf+5, l); best[l]='\0'; + if (l && best[l-1]=='\n') best[l-1]='\0'; + } else if (strncmp(buf, "LINE:", 5) == 0) { + size_t l = strnlen(buf+5, sizeof(line)-1); + memcpy(line, buf+5, l); line[l]='\0'; + if (l && line[l-1]=='\n') line[l-1]='\0'; + } else if (buf[0]=='.') { + mistakes_add(ml, fen, best, line); + fen[0]=best[0]=line[0]='\0'; + } + } + fclose(f); return true; +} diff --git a/C/opening_learner/mistakes.h b/C/opening_learner/mistakes.h new file mode 100644 index 00000000..7f77ce5b --- /dev/null +++ b/C/opening_learner/mistakes.h @@ -0,0 +1,28 @@ +#ifndef MISTAKES_H +#define MISTAKES_H + +#include +#include + +// A lightweight mistake store in memory + file persistence. + +typedef struct { + char fen[128]; + char best_move[8]; + // PGN-like ply list in UCI for context + char line[512]; +} Mistake; + +typedef struct { + Mistake *items; + size_t count; + size_t cap; +} MistakeList; + +void mistakes_init(MistakeList *ml); +void mistakes_free(MistakeList *ml); +void mistakes_add(MistakeList *ml, const char *fen, const char *best_move, const char *line); +bool mistakes_save(const MistakeList *ml, const char *path); +bool mistakes_load(MistakeList *ml, const char *path); + +#endif diff --git a/C/opening_learner/mistakes.o b/C/opening_learner/mistakes.o new file mode 100644 index 0000000000000000000000000000000000000000..e8e7eba50469eb0de6dcce06d81eae2f0a3dafe7 GIT binary patch literal 4016 zcmbtXe{54l9KUuOs{`IEh-L|x^^amk)R%S)9SpVYR(3UIfMcKnma?^M(si?a;BXK& z+X2t6I1~RdA#sVCkQft9NW_54;@E^l6QahDpi%t8IDP~P28<%}`@MI!9?#Vn{U&$! zz0ZBVKkj|+-fIs8!)xp|8xdk7Pmsb)poDnl6?7j{eMBMiNnE`)Mn5Y@L|xG27;mA* zA2j#m&j^@nCFWX#W6QW&Mf{sW#!Y`~$oSL0F=Tw*N{ugCXl|W@=9-)|zrjXVo>Ma) zfeFnoA#`x;gcEhO@jPAmRk|WROl=oHOZ`SWZydH063Xo-EEZ7JY!N+29tJX~1Z zvdzEMzs>*r6hK6+Y6QQz^2~lMazS?`$dm)rtxZLOK~ijuuoc2^LYf*H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_learner/mistakes.txt new file mode 100644 index 00000000..35127429 --- /dev/null +++ b/C/opening_learner/mistakes.txt @@ -0,0 +1,12 @@ +FEN:rnbqkbnr/pppp1ppp/4p3/8/8/N4N2/PPPPPPPP/R1BQKB1R b KQkq - 1 2 +BEST:f8a3 +LINE:g1f3 e7e6 b1a3 +. +FEN:rnbqkbnr/pppppppp/8/8/8/7P/PPPPPPP1/RNBQKBNR b KQkq - 0 1 +BEST:e7e5 +LINE:h2h3 +. +FEN:rnbqkbnr/pppp1ppp/8/4p3/8/4P3/PPPPKPPP/RNBQ1BNR b kq - 1 2 +BEST:d7d5 +LINE:e2e3 e7e5 e1e2 +. diff --git a/C/opening_learner/opening_learner b/C/opening_learner/opening_learner new file mode 100755 index 0000000000000000000000000000000000000000..578843f2aea18d8b0d7726c366bc8381ba9708be GIT binary patch literal 40168 zcmeHw4|r5XmhbJPAq|Awpi!fu^l0OPgE8TclL$y|(w*GK4g`pRiZ-Fs4H-hZ>2yO- z2f}nmbGPj=Gt0QnKHtdB>O7z0uI`M35H%(N0yydh91(TLK^^rr5k*u8_^03R)UDgO zY3bS7?|a|-zW04x=&m}aPF0;cb?Q{rsk-Sc4p&u{$t37cws4a`Q1Nvdrkp3N`8kgu zC>KhFJbYg%Tp^4GY-Ttuy<7{Bsy+GHBSQ#;*>@^@vr>cg(3p?a7vS`1VMnfp*F$4Il1$Vc-0TOZyH z!eU-urv59Zj>4mdyn5EoV&F;Fsu$cMWVH!B#EYJ85=;yp0~|eB9M;o`wg4aM4$5!+ z-*VNb?ZoRIx^oHwhm|4`~FPe_> z1YtHlR32-$Rxsn1@MR}s`j7(j-RCyYU+`VDPzBt%@LMv_n^EJr=oe?8-;#lz*ted8 zpKmji^UDnQSO$D3L%mBg;CE&yr#S=uvkds<8RY5AKz}3y{hJx+FV4XKvJCjUGT>jy zP`;FbzA^*7Hv|3T4EQV1UMoK7Pg4f^AA|5*`c|BQpMTFl|8E)S-_3ykJVQBqGvJ@k zfIpR?{GVl@|C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i%>1;sHub&_2;dY)8%Q?oUo?kylplv)=spr?vBlJ)|e*g)4sIA7w1Au(}dVbx% zuZOLiSm$4-(?i;>(%FFy>*oi0$hWiXe1z)O`RBz7TM(f>(evx)7kYSv6YBNr`StcI zJbwkJ)6YZnP(MGR{FmU9p8p?!Bfdz!?6HmvNk5HGy8iqGiN^d^t46Jd>@JU?0Nrn< zhrdY6ukTCs@aA$uW<=9K;Su1B_3M5$J=D*^^itCKe~jnX<*)l6^e~-&t6mTf|1~YY z?l0Cux+7+M((8XNEq@y~D|)EX;5IyZ|LLf`h#1HJntQYqJ+$hCJWQ|uHQ@8`;lnm{ zKG^dkEkk;JI;@9(1{u6Isr>r+;r_od{~+>`?&RZpRhi8yt%&(t6syu&BTL0KS=B9{KT~-ElG|>G{*gFVWIt3{$$VC9&@#TQy)gKe(lNU;;im p|GNDd1DwvgmP7k4j%(qSY5J?ztJ6}M=gPlen?`YQT81=G_&+6sz3Kn} literal 0 HcmV?d00001 diff --git a/C/opening_learner/run.sh b/C/opening_learner/run.sh new file mode 100755 index 00000000..e88c54ce --- /dev/null +++ b/C/opening_learner/run.sh @@ -0,0 +1,43 @@ +#!/usr/bin/env bash +set -euo pipefail +cd "$(dirname "$0")" + +# Install SDL2 dev if sdl2-config is missing; otherwise build and run. + +if ! command -v sdl2-config >/dev/null 2>&1; then + echo "sdl2-config not found. Attempting to install SDL2 dev..." + if [ -f /etc/os-release ]; then + . /etc/os-release + case "$ID" in + ubuntu|debian|linuxmint|neon|pop) + sudo apt-get update + sudo apt-get install -y libsdl2-dev + ;; + arch|manjaro|endeavouros) + sudo pacman -Syu --noconfirm sdl2 + ;; + fedora) + sudo dnf install -y SDL2-devel + ;; + opensuse*|sles) + sudo zypper install -y libSDL2-devel + ;; + void) + sudo xbps-install -Sy SDL2-devel + ;; + alpine) + sudo apk add sdl2-dev + ;; + *) + echo "Unsupported distro ($ID). Please install SDL2 dev manually and rerun." >&2 + exit 3 + ;; + esac + else + echo "/etc/os-release not found; cannot auto-detect distro. Install SDL2 dev manually." >&2 + exit 3 + fi +fi + +./check_build.sh +./opening_learner