// Generate marcille47.c with the desired CRC. #include #include #include #include #include #include namespace { // Input text. static constexpr char kInput[] = R"( char* /*(c) 2025 */#ifdef r *J){if(j<3||(M=atoi(J[1]))<36||(A =atoi(J[ 2]))<12||(M|A)>2047){return+printf("%s\40" "{36<" "=w" "<2048} {12<=h<2048} [seed]\n",*J);}I=M+1;C=I*A;R =6;for(q=malloc (C*2);R*R3?atoi(J[3]):(int*)0-&j);p=q;for(y=0;y.47?0:1:15;}for(t=0;t<3;t++){for (y=1;y3?2:0)for(z=v=-1;v<=1;v++)for(u=-1; u<2;u++)z+=q[(y+v)*I+x+u]&1;}p=q;for(x=0;x=C&&(x=strlen(q))>=C){x=t=+4;}if (t&3){for(v= +u%I;x%I-v&&q[x ]-2;x+=x%I>v?-1:1){q[x]=0;}for(;x-u &&q[x]-2;x +=x>u?-I:I){q[x ]=0;}q[x]=0;}*o=u=x;for(y=1;y;)if (!q[v=o[-- y]]){q[v]=+2; o[y++]=v-M-1;o[y++]=v-1;o[y++]=v+ I;o[y++]= v+1;}}p=q;for (y= 0;y1||z==u;*(E?&j:&z)=u){u=(j+z) /2;S=u/90. ; *J="\x1bPq\"1;1;%d;%d#1;2;100;100;100#1"; memset(b,v= 0,R*R);for(p=m;i(0)>0;p+=3){for(memset(o ,0,u*LL);i( 2)>0;p+=6){for(t=0;ty?v:y;*g=*g?*gx?*g: x;}}g =o ;for( y=0;yH:A?>D;LCRJWP" "WU`X`Y" "" "`[_]h`bj^mSuFt>j3ZDPDM':r;s" "t?" "" "q@oEoFnElDk>k /* */int t, M,A,R,C,I,LL,E ,*o,*g,x,y,z,t,u,v;void V(){for( v=0;*p- 45 &&p[v] ==*p&&v<99;v++){}if(v>3&&(x=strcspn (w,n )) >2){*w++=33;if(x>3&&v>9){*w++=v/10 +48;*w ++ =v%10+48;}else{v=v>9?9:v;*w++=48+v ;}p+=v-1;}* w++ =*p++;}void U(){for(;y<=v;y+=6){p =w;for(x=0;x63)p=w; }w=p;* w++=45;}}typedef double s;s(Z),S;s i(int d){return (p[d]-39)*S;}s l(s a,s d){return+a+(d-a) *Z;}s L(){return l(l(l(i(00 ), i(2)),l(i(2),i(4))) ,l(l(i(2),i(4 )),l(i( 4),i(6 ))));}void W(){E=C-03>(w+= strspn(w,n) )-q;{ ;}} #include/* */ #include #define r (s)rand()\ /*o*// RAND_MAX int main (int j, #include __FILE__ #/**/ endif )"; // Replace this text with CRC. static const char kCrcKey[] = "XXXXXXXX"; // Replace this text with adjustment string. static const char kAdjustKey[] = "XXXX X"; // Table for updating CRC one byte at a time. static std::array crc_table; // Mapping from upper 8 bits of a CRC to an index in crc_table, // such that (crc_table[prefix_table[byte]] >> 24) == byte. static std::array prefix_table; // Insert target CRC into string, returns true on success. static bool InsertCrc(std::string *text, uint32_t target_crc) { const size_t offset = text->find(kCrcKey); if( offset == std::string::npos ) return false; std::array buffer; sprintf(buffer.data(), "%08x", target_crc); for(int i = 0; i < 8; i++) (*text)[offset + i] = buffer[i]; return true; } // Initialize crc_table and prefix_table. static void InitCrcTables() { static constexpr uint32_t kPoly = 0xedb88320U; for(int i = 0; i < 256; i++) { uint32_t crc = i; for(int j = 8; j > 0; j--) { if( (crc & 1) != 0 ) crc = (crc >> 1) ^ kPoly; else crc >>= 1; } crc_table[i] = crc; } for(int i = 0; i < 256; i++) prefix_table[(crc_table[i] >> 24) & 0xff] = i; } // Compute partial CRC by scanning forward one byte at a time. static uint32_t ForwardCrc(const std::string &data, uint32_t crc) { for(uint8_t c : data) crc = ((crc >> 8) & 0xffffff) ^ crc_table[(crc ^ c) & 0xff]; return crc; } // Compute reverse partial CRC by scanning backward one byte at a time. static uint32_t ReverseCrc(const std::string &data, uint32_t crc) { for(int i = static_cast(data.size()); i-- > 0;) { // a1.b1.c1.d1 = (a0.b0.c0) ^ crc_table[d0 ^ byte] // // There is exactly one crc_table entry with the prefix a1, so we can // get the right side of the XOR with a single lookup. const int r_index = prefix_table[(crc >> 24) & 0xff]; const uint32_t r_value = crc_table[r_index]; // r_index = d0^byte -> d0 = r_index^byte. const uint8_t byte = data[i]; const uint32_t d0 = r_index ^ byte; // b1.c1.d1 = (a0.b0.c0) ^ (r_value & 0xffffff) -> // a0.b0.c0 = b1.c1.d1 ^ (r_value & 0xffffff) const uint32_t abc0 = r_value ^ crc; // Reassemble the combined CRC bytes. crc = ((abc0 & 0xffffff) << 8) | d0; } return crc; } // Brute force CRC for a particular target. Returns true on success. static bool AdjustTextForCrc(std::string data, uint32_t target_crc) { if( data.size() != 4992 ) { fprintf(stderr, "%08x: Unexpected input size: %d\n", target_crc, static_cast(data.size())); return false; } // Replace substring with desired CRC. if( !InsertCrc(&data, target_crc) ) { fprintf(stderr, "%08x: missing placeholder for CRC (%s)\n", target_crc, kCrcKey); return false; } // Break input into three sections: prefix + adjustment + suffix. const size_t adjust_offset = data.find(kAdjustKey); if( adjust_offset == std::string::npos ) { fprintf(stderr, "%08x: missing placeholder for adjustment string (%s)\n", target_crc, kAdjustKey); return false; } const std::string prefix = data.substr(0, adjust_offset); std::string adjustment = data.substr(adjust_offset, strlen(kAdjustKey)); const std::string suffix = data.substr(adjust_offset + strlen(kAdjustKey)); // Compute CRC of prefix and suffix. const uint32_t prefix_crc_end = ForwardCrc(prefix, ~0); const uint32_t suffix_crc_start = ReverseCrc(suffix, ~target_crc); // Brute-force the middle part. for(size_t i = 0; i < strlen(kAdjustKey); i++) { if( kAdjustKey[i] == 'X' ) adjustment[i] = 33; } uint64_t steps = 0; for(;; steps++) { if( ForwardCrc(adjustment, prefix_crc_end) == suffix_crc_start ) break; size_t i = 0; for(; i < strlen(kAdjustKey); i++) { if( kAdjustKey[i] == 'X' ) { adjustment[i]++; if( adjustment[i] < 127 ) break; adjustment[i] = 33; } } if( i == strlen(kAdjustKey) ) { fprintf(stderr, "%08x: failed after %" PRIu64 " steps\n", target_crc, steps); return false; } if( (steps & 0xffffff) == 0 ) { fprintf(stderr, "%08x: testing adjustment %" PRIu64 ": %s\n", target_crc, steps, adjustment.c_str()); } } // Success. fprintf(stderr, "%08x: success at %" PRIu64 ": %s\n", target_crc, steps, adjustment.c_str()); fwrite(prefix.data(), prefix.size(), 1, stdout); fwrite(adjustment.data(), adjustment.size(), 1, stdout); fwrite(suffix.data(), suffix.size(), 1, stdout); return true; } } // namespace int main(int argc, char **argv) { uint32_t target_crc; if( argc != 2 || sscanf(argv[1], "%x", &target_crc) != 1 ) return printf("%s {target_crc}\n", *argv); InitCrcTables(); return AdjustTextForCrc(kInput, target_crc) ? 0 : 1; }