Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cstdint>
- #include <cstdio>
- #include <cstring>
- #include <cstdlib>
- namespace crc32
- {
- void generate_table(uint32_t(&table)[256])
- {
- for (uint32_t i = 0; i < 256; i++)
- {
- auto c = i;
- for (size_t j = 0; j < 8; j++)
- if (c & 1)
- c = 0xEDB88320 ^ (c >> 1);
- else
- c >>= 1;
- table[i] = c;
- }
- }
- static uint32_t update(uint32_t(&table)[256], uint32_t initial, const void* buf, size_t len)
- {
- auto c = initial ^ 0xFFFFFFFF;
- const auto* u = static_cast<const uint8_t*>(buf);
- for (size_t i = 0; i < len; ++i)
- {
- c = table[(c ^ u[i]) & 0xFF] ^ (c >> 8);
- }
- return c ^ 0xFFFFFFFF;
- }
- };
- int main(int argc, char** argv)
- {
- const auto fp = fopen(argv[1], "rb");
- fseek(fp, 0, SEEK_END);
- const auto sz = ftell(fp);
- rewind(fp);
- const auto png = new std::uint8_t[sz];
- fread(png, 1, sz, fp);
- fclose(fp);
- auto it = png + sz - 4;
- while (*(uint32_t*)it != 'DNEI')
- --it;
- const auto new_sz = atoll(argv[3]);
- const auto new_png = new std::uint8_t[new_sz];
- memset(new_png, 'a', new_sz);
- memcpy(new_png, png, it - png);
- auto inv = (uint32_t*)(it - png + new_png);
- *inv++ = new_sz - sz - 12;
- *inv++ = 'iTXt';
- const auto start = inv;
- (uint8_t*&)inv += new_sz - sz - 12;
- uint32_t table[256];
- crc32::generate_table(table);
- *inv = crc32::update(table, 0, start, (char*)inv - (char*)start);
- memcpy(new_png + new_sz - (png + sz - it), it, png + sz - it);
- const auto ofp = fopen(argv[2], "wb");
- fwrite(new_png, new_sz, 1, ofp);
- fclose(fp);
- delete[] png;
- delete[] new_png;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement