international text chunk (iTXt)*/
| 4438 | |
| 4439 | /*international text chunk (iTXt)*/ |
| 4440 | static unsigned readChunk_iTXt(LodePNGInfo* info, const LodePNGDecompressSettings* zlibsettings, |
| 4441 | const unsigned char* data, size_t chunkLength) |
| 4442 | { |
| 4443 | unsigned error = 0; |
| 4444 | unsigned i; |
| 4445 | |
| 4446 | unsigned length, begin, compressed; |
| 4447 | char *key = 0, *langtag = 0, *transkey = 0; |
| 4448 | ucvector decoded; |
| 4449 | ucvector_init(&decoded); |
| 4450 | |
| 4451 | while(!error) /*not really a while loop, only used to break on error*/ |
| 4452 | { |
| 4453 | /*Quick check if the chunk length isn't too small. Even without check |
| 4454 | it'd still fail with other error checks below if it's too short. This just gives a different error code.*/ |
| 4455 | if(chunkLength < 5) CERROR_BREAK(error, 30); /*iTXt chunk too short*/ |
| 4456 | |
| 4457 | /*read the key*/ |
| 4458 | for(length = 0; length < chunkLength && data[length] != 0; length++) ; |
| 4459 | if(length + 3 >= chunkLength) CERROR_BREAK(error, 75); /*no null termination char, corrupt?*/ |
| 4460 | if(length < 1 || length > 79) CERROR_BREAK(error, 89); /*keyword too short or long*/ |
| 4461 | |
| 4462 | key = (char*)lodepng_malloc(length + 1); |
| 4463 | if(!key) CERROR_BREAK(error, 83); /*alloc fail*/ |
| 4464 | |
| 4465 | key[length] = 0; |
| 4466 | for(i = 0; i < length; i++) key[i] = data[i]; |
| 4467 | |
| 4468 | /*read the compression method*/ |
| 4469 | compressed = data[length + 1]; |
| 4470 | if(data[length + 2] != 0) CERROR_BREAK(error, 72); /*the 0 byte indicating compression must be 0*/ |
| 4471 | |
| 4472 | /*even though it's not allowed by the standard, no error is thrown if |
| 4473 | there's no null termination char, if the text is empty for the next 3 texts*/ |
| 4474 | |
| 4475 | /*read the langtag*/ |
| 4476 | begin = length + 3; |
| 4477 | length = 0; |
| 4478 | for(i = begin; i < chunkLength && data[i] != 0; i++) length++; |
| 4479 | |
| 4480 | langtag = (char*)lodepng_malloc(length + 1); |
| 4481 | if(!langtag) CERROR_BREAK(error, 83); /*alloc fail*/ |
| 4482 | |
| 4483 | langtag[length] = 0; |
| 4484 | for(i = 0; i < length; i++) langtag[i] = data[begin + i]; |
| 4485 | |
| 4486 | /*read the transkey*/ |
| 4487 | begin += length + 1; |
| 4488 | length = 0; |
| 4489 | for(i = begin; i < chunkLength && data[i] != 0; i++) length++; |
| 4490 | |
| 4491 | transkey = (char*)lodepng_malloc(length + 1); |
| 4492 | if(!transkey) CERROR_BREAK(error, 83); /*alloc fail*/ |
| 4493 | |
| 4494 | transkey[length] = 0; |
| 4495 | for(i = 0; i < length; i++) transkey[i] = data[begin + i]; |
| 4496 | |
| 4497 | /*read the actual text*/ |
no test coverage detected