|
|
| version 1.196, 2025/05/26 00:52:15 | version 1.201, 2026/10/25 16:58:12 |
|---|---|
| Line 1 | Line 1 |
| /** @file | /** @file |
| Parser: @b image parser class. | Parser: @b image parser class. |
| Copyright (c) 2001-2024 Art. Lebedev Studio (http://www.artlebedev.com) | Copyright (c) 2001-2026 Art. Lebedev Studio (https://www.artlebedev.com) |
| Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru> | Authors: Konstantin Morshnev <moko@design.ru>, Alexandr Petrosian <paf@design.ru> |
| */ | */ |
| Line 246 public: | Line 246 public: |
| ///*********************************************** support functions | ///*********************************************** support functions |
| /// Exif lives in one JPEG segment, whose length is 16 bits: nothing in Exif is longer | |
| #define JPG_EXIF_MAX_SIZE 0xFFFF | |
| /// limits of the work a measured file can require | |
| class Measure_budget { | |
| public: | |
| enum Kind { | |
| EXIF_VALUES, ///< bytes of the values lying outside of the Exif IFD entries | |
| EXIF_IFDS, ///< jumps from an IFD to the Exif and GPS IFDs | |
| MP4_DEPTH, ///< nesting of MP4 chunks: given back by release() | |
| KIND_COUNT | |
| }; | |
| Measure_budget(const String& aorigin): origin(aorigin) { | |
| left[EXIF_VALUES]=JPG_EXIF_MAX_SIZE; | |
| left[EXIF_IFDS]=4; // IFD0 has two of them (Exif and GPS), the rest is spare | |
| left[MP4_DEPTH]=8; // real files nest chunks 3 levels deep: moov/trak/mdia | |
| } | |
| void use(Kind kind, uint amount=1) { | |
| static const char* const messages[KIND_COUNT]={ | |
| "broken Exif - too long values", | |
| "broken Exif - too many IFD references", | |
| "broken MP4 file - chunks nested too deep" | |
| }; | |
| if(amount>left[kind]) | |
| throw Exception(IMAGE_FORMAT, &origin, messages[kind]); | |
| left[kind]-=amount; | |
| } | |
| /// gives back what use() took | |
| void release(Kind kind, uint amount=1) { | |
| left[kind]+=amount; | |
| } | |
| private: | |
| const String& origin; | |
| uint left[KIND_COUNT]; | |
| }; | |
| class Measure_reader { | class Measure_reader { |
| public: | public: |
| const String& file_name; | |
| Measure_budget budget; // of the file being measured | |
| Measure_reader(const String& afile_name): file_name(afile_name), budget(afile_name) {} | |
| virtual size_t read(const char* &buf, size_t limit)=0; | virtual size_t read(const char* &buf, size_t limit)=0; |
| virtual void seek(uint64_t value)=0; | virtual void seek(uint64_t value)=0; |
| virtual uint64_t tell()=0; | virtual uint64_t tell()=0; |
| Line 255 public: | Line 301 public: |
| }; | }; |
| class Measure_file_reader: public Measure_reader { | class Measure_file_reader: public Measure_reader { |
| const String& file_name; | |
| int f; | int f; |
| uint64_t size; | |
| public: | public: |
| Measure_file_reader(int af, const String& afile_name): | Measure_file_reader(int af, const String& afile_name, uint64_t asize): |
| file_name(afile_name), f(af) { | Measure_reader(afile_name), f(af), size(asize) { |
| } | } |
| override size_t read(const char* &abuf, size_t limit) { | override size_t read(const char* &abuf, size_t limit) { |
| Line 276 public: | Line 322 public: |
| override void seek(uint64_t value) { | override void seek(uint64_t value) { |
| if(pa_lseek(f, value, SEEK_SET)<0) | if(pa_lseek(f, value, SEEK_SET)<0) |
| throw Exception(IMAGE_FORMAT, &file_name, "seek to %.15g failed: %s (%d)", (double)value, strerror(errno), errno); | throw Exception(IMAGE_FORMAT, &file_name, "seek to %s failed: %s (%d)", pa_uitoa(value), strerror(errno), errno); |
| } | } |
| override uint64_t tell() { return pa_lseek(f, 0, SEEK_CUR); } | override uint64_t tell() { return pa_lseek(f, 0, SEEK_CUR); } |
| override uint64_t length() { return pa_lseek(f, 0, SEEK_END); } | override uint64_t length() { return size; } |
| }; | }; |
| class Measure_buf_reader: public Measure_reader { | class Measure_buf_reader: public Measure_reader { |
| const char* buf; size_t size; | const char* buf; size_t size; |
| const String& file_name; | |
| size_t offset; | size_t offset; |
| public: | public: |
| Measure_buf_reader(const char* abuf, size_t asize, const String& afile_name): | Measure_buf_reader(const char* abuf, size_t asize, const String& afile_name): |
| buf(abuf), size(asize), file_name(afile_name), offset(0) { | Measure_reader(afile_name), buf(abuf), size(asize), offset(0) { |
| } | } |
| override size_t read(const char* &abuf, size_t limit) { | override size_t read(const char* &abuf, size_t limit) { |
| Line 306 public: | Line 349 public: |
| override void seek(uint64_t value) { | override void seek(uint64_t value) { |
| if(value>(uint64_t)size) | if(value>(uint64_t)size) |
| throw Exception(IMAGE_FORMAT, &file_name, "seek to %.15g failed: out of buffer (%.15g)", value, size); | throw Exception(IMAGE_FORMAT, &file_name, "seek to %s failed: out of buffer (%s)", pa_uitoa(value), pa_uitoa(size)); |
| offset=(size_t)value; | offset=(size_t)value; |
| } | } |
| Line 411 static Value* parse_IFD_entry_formatted_ | Line 454 static Value* parse_IFD_entry_formatted_ |
| case 10: // signed rational | case 10: // signed rational |
| { | { |
| signed int numerator=(signed int)endian_to_uint(is_big, value); value+=component_size/2; | signed int numerator=(signed int)endian_to_uint(is_big, value); value+=component_size/2; |
| uint denominator=endian_to_uint(is_big, value); | signed int denominator=(signed int)endian_to_uint(is_big, value); |
| if(!denominator) | if(!denominator) |
| return 0; | return 0; |
| return new VDouble(numerator/denominator); | return new VDouble(((double)numerator)/denominator); |
| } | } |
| /* | /* |
| case 11: // single float | case 11: // single float |
| Line 443 static Value* parse_IFD_entry_formatted_ | Line 486 static Value* parse_IFD_entry_formatted_ |
| catch(...) { /*ignore bad date times*/ } | catch(...) { /*ignore bad date times*/ } |
| } | } |
| return new VString(*new String(cstr, String::L_TAINTED)); | const char* end=(const char*)memchr(cstr, 0, length); |
| return new VString(pa_strdup(cstr, end ? end-cstr : length)); | |
| } | } |
| if(components_count==1) | if(components_count==1) |
| Line 490 static Value* parse_IFD_entry_value(bool | Line 534 static Value* parse_IFD_entry_value(bool |
| // You can get the total data byte length by multiplies | // You can get the total data byte length by multiplies |
| // a 'bytes/components' value (see above chart) by number of components stored 'NNNNNNNN' area | // a 'bytes/components' value (see above chart) by number of components stored 'NNNNNNNN' area |
| uint components_count=endian_to_uint(is_big, entry.components_count); | uint components_count=endian_to_uint(is_big, entry.components_count); |
| if(components_count>JPG_EXIF_MAX_SIZE) | |
| throw Exception(IMAGE_FORMAT, &reader.file_name, "broken Exif - too many components"); | |
| uint value_size=component_size*components_count; | uint value_size=component_size*components_count; |
| // If its size is over 4bytes, 'DDDDDDDD' contains the offset to data stored address | // If its size is over 4bytes, 'DDDDDDDD' contains the offset to data stored address |
| Value* result; | Value* result; |
| Line 497 static Value* parse_IFD_entry_value(bool | Line 543 static Value* parse_IFD_entry_value(bool |
| if(value_size<=4) | if(value_size<=4) |
| result=parse_IFD_entry_formatted_value(is_big, format, component_size, components_count, entry.value_or_offset_to_it); | result=parse_IFD_entry_formatted_value(is_big, format, component_size, components_count, entry.value_or_offset_to_it); |
| else { | else { |
| reader.budget.use(Measure_budget::EXIF_VALUES, value_size); | |
| uint64_t remembered=reader.tell(); | uint64_t remembered=reader.tell(); |
| { | { |
| reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it)); | reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it)); |
| const char* value; | const char* value; |
| if(reader.read(value, value_size)<value_size) | if(reader.read(value, value_size)<value_size) |
| return 0; | result=0; |
| result=parse_IFD_entry_formatted_value(is_big, format, component_size, components_count, (const uchar*)value); | else |
| result=parse_IFD_entry_formatted_value(is_big, format, component_size, components_count, (const uchar*)value); | |
| } | } |
| reader.seek(remembered); | reader.seek(remembered); |
| } | } |
| Line 517 static void parse_IFD_entry(HashStringVa | Line 566 static void parse_IFD_entry(HashStringVa |
| ushort tag=endian_to_ushort(is_big, entry.tag); | ushort tag=endian_to_ushort(is_big, entry.tag); |
| if(tag==JPG_IFD_TAG_EXIF_OFFSET || tag==JPG_IFD_TAG_EXIF_GPS_OFFSET){ | if(tag==JPG_IFD_TAG_EXIF_OFFSET || tag==JPG_IFD_TAG_EXIF_GPS_OFFSET){ |
| reader.budget.use(Measure_budget::EXIF_IFDS); // an IFD can point to itself | |
| uint64_t remembered=reader.tell(); | uint64_t remembered=reader.tell(); |
| { | { |
| reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it)); | reader.seek(tiff_base+endian_to_uint(is_big, entry.value_or_offset_to_it)); |
| Line 907 static bool measure_mp4(const String& or | Line 958 static bool measure_mp4(const String& or |
| throw Exception(IMAGE_FORMAT, &origin_string, "broken MP4 file - truncated chunk extended size header"); | throw Exception(IMAGE_FORMAT, &origin_string, "broken MP4 file - truncated chunk extended size header"); |
| MP4_ExtSize *ext_size=(MP4_ExtSize *)buf; | MP4_ExtSize *ext_size=(MP4_ExtSize *)buf; |
| size=((uint64_t)endian_to_uint(true, ext_size->high) << 32) + endian_to_uint(true, ext_size->low); | size=((uint64_t)endian_to_uint(true, ext_size->high) << 32) + endian_to_uint(true, ext_size->low); |
| } | } else if(size==0) // the chunk extends to the end (of file or container) |
| size=(anext ? anext : reader.length())-next; | |
| next+=size; | next+=size; |
| // the chunk can't end before its header does (size is too small or wraps around): we would never get to the end | |
| if(next<reader.tell()) | |
| throw Exception(IMAGE_FORMAT, &origin_string, "broken MP4 file - bad chunk size"); | |
| if(first){ | if(first){ |
| if(strncmp(head->signature, "ftyp", 4)!=0) | if(strncmp(head->signature, "ftyp", 4)!=0) |
| throw Exception(IMAGE_FORMAT, &origin_string, "not MP4 file - wrong signature"); | throw Exception(IMAGE_FORMAT, &origin_string, "not MP4 file - wrong signature"); |
| first=false; | first=false; |
| anext=reader.length(); // to avoid reading beyond EOF | anext=reader.length(); // to avoid reading beyond EOF |
| } else if(strncmp(head->signature, "moov", 4)==0 || strncmp(head->signature, "mdia", 4)==0 || strncmp(head->signature, "trak", 4)==0) { | } else if(strncmp(head->signature, "moov", 4)==0 || strncmp(head->signature, "mdia", 4)==0 || strncmp(head->signature, "trak", 4)==0) { |
| reader.budget.use(Measure_budget::MP4_DEPTH); | |
| if(measure_mp4(origin_string, reader, width, height, next, lastTkhd)) | if(measure_mp4(origin_string, reader, width, height, next, lastTkhd)) |
| return true; | return true; |
| reader.budget.release(Measure_budget::MP4_DEPTH); | |
| } else if(strncmp(head->signature, "tkhd", 4)==0) { | } else if(strncmp(head->signature, "tkhd", 4)==0) { |
| if(size>8){ | if(size>8){ |
| reader.seek(next-8); | reader.seek(next-8); |
| Line 980 static void measure(const String& file_n | Line 1038 static void measure(const String& file_n |
| // methods | // methods |
| static void file_measure_action(struct stat& /*finfo*/, int f, const String& file_spec, void *context) { | static void file_measure_action(struct stat& finfo, int f, const String& file_spec, void *context) { |
| Measure_file_reader reader(f, file_spec); | Measure_file_reader reader(f, file_spec, finfo.st_size); |
| measure(file_spec, reader, *static_cast<Measure_info *>(context)); | measure(file_spec, reader, *static_cast<Measure_info *>(context)); |
| } | } |
| Line 994 static void _measure(Request& r, MethodP | Line 1052 static void _measure(Request& r, MethodP |
| if(params.count()>1) | if(params.count()>1) |
| if(HashStringValue* options=params.as_hash(1, "methods options")) { | if(HashStringValue* options=params.as_hash(1, "methods options")) { |
| int valid_options=0; | |
| for(HashStringValue::Iterator i(*options); i; i.next() ){ | for(HashStringValue::Iterator i(*options); i; i.next() ){ |
| String::Body key=i.key(); | String::Body key=i.key(); |
| Value* value=i.value(); | Value* value=i.value(); |
| if(key == "exif") { | if(key == "exif") { |
| if(r.process(*value).as_bool()) | if(r.process(*value).as_bool()) |
| info.exif=&exif; | info.exif=&exif; |
| valid_options++; | } else if(key == "xmp") { |
| } | |
| if(key == "xmp") { | |
| if(r.process(*value).as_bool()) | if(r.process(*value).as_bool()) |
| info.xmp=&xmp; | info.xmp=&xmp; |
| valid_options++; | } else if(key == "xmp-charset") { |
| } | |
| if(key == "xmp-charset") { | |
| info.xmp_charset=&pa_charsets.get(value->as_string()); | info.xmp_charset=&pa_charsets.get(value->as_string()); |
| valid_options++; | } else if(key == "video") { |
| } | |
| if(key == "video") { | |
| info.video=r.process(*value).as_bool(); | info.video=r.process(*value).as_bool(); |
| valid_options++; | } else |
| } | throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); |
| } | } |
| if(valid_options!=options->count()) | |
| throw Exception(PARSER_RUNTIME, 0, CALLED_WITH_INVALID_OPTION); | |
| } | } |
| const String* file_name; | const String* file_name; |