rendered paste body#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#define u unsigned#define i int#define c u char#define s u short#define f(p,n) fseek(p,n,SEEK_SET)#define F(p,n) fseek(p,n,SEEK_END)#define r(p,n,f) fread(p,1,n,f)#define w(p,n,f) frwite(p,1,n,f)#define R return#define m 0x20000#define P FILE#define W(x) while(x)#define N(...) printf(__VA_ARGS__)u g(P*$){c _;f($,0x14);r(&_,1,$);R m<<_;}c p(P*$){c _[4];F($,-4);r(_,4,$);if(*_==_[1]&&_[1]==_[2]&&_[2]==_[3])R *_;R 0xf0;}i o(P*$){u S=g($);c d=p($);i M;c *_,*L,*b;s d2;u d4,l;if(d==0xf0)R -1;d2=((s)d)<<8|d;d4=((u)d2)<<16|d2;_=calloc(m,1);for(;;){f($,S-m);r(_,m,$);M=m-4;W(*((u*)(_+M))==d4&&M>=0)M-=4;if(M<0)M=0;if(*((u*)(_+M))!=d4)break;else S-=m;}S-=m;S+=M;l=*((u*)(_+M));B=(c*)&l4;L=B+3;W(L>=B&&*L==d)L--;L++;S+=(i)(L-B);free(_);R S;}i main(i c,char**v){P*I,*O;i l,L;char *_;if(c!=3){N("Usage: %s input.nds output.nds\n",v[0]);R 1;}if((I=fopen(v[1],"rb"))==NULL){N("Cannot open input: %s\n",strerror(errno));R 2;}if((O=fopen(v[2],"wb"))==NULL){N("Cannot open ouput: %s\n",strerror(errno));R 2;}N("Input: %s, size=%""d m\n",v[1],g(I)/m);l=o(I);switch(l%4){case 1:l++;case 2:l++;case 3:l++;}N("pa""dding starts at %x (adjusted to 32-bit boundary)\n",l);_=malloc(m);f(I,0);L=l;W(l-m>=0){r(_,m,I);w(_,m,O);l-=m;}r(_,l,I);w(_,l,O);free(_);fclose(I);fclose(O);N("Done!\n");N("Wrote %d bytes to %s\n",L,v[2]);R 0;}