Skip to content

Commit d801d1d

Browse files
committed
Don't have exif bugging out on short strings
* lisp/image/exif.el (exif--direct-ascii-value): New function (bug#40127). (exif--parse-directory): Use it to get the correct values for in-directory (i.e., shorter than 4 octets) strings.
1 parent bed04c5 commit d801d1d

File tree

3 files changed

+30
-2
lines changed

3 files changed

+30
-2
lines changed

lisp/image/exif.el

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@
7272
(283 y-resolution)
7373
(296 resolution-unit)
7474
(305 software)
75-
(306 date-time))
75+
(306 date-time)
76+
(315 artist))
7677
"Alist of tag values and their names.")
7778

7879
(defconst exif--orientation
@@ -216,7 +217,10 @@ If the orientation isn't present in the data, return nil."
216217
(+ (1+ value) length)))
217218
;; The value is stored directly
218219
;; in the directory.
219-
value)
220+
(if (eq (car field-format) 'ascii)
221+
(exif--direct-ascii-value
222+
value (1- length) le)
223+
value))
220224
(car field-format)
221225
le)))))
222226
(let ((next (exif--read-number 4 le)))
@@ -231,6 +235,19 @@ If the orientation isn't present in the data, return nil."
231235
;; We've reached the end of the directories.
232236
dir))))
233237

238+
(defun exif--direct-ascii-value (value bytes le)
239+
"Make VALUE into a zero-terminated string.
240+
VALUE is an integer representing BYTES characters."
241+
(with-temp-buffer
242+
(set-buffer-multibyte nil)
243+
(if le
244+
(dotimes (i bytes)
245+
(insert (logand (lsh value (* i -8)) 255)))
246+
(dotimes (i bytes)
247+
(insert (logand (lsh value (* (- (1- bytes) i) -8)) 255))))
248+
(insert 0)
249+
(buffer-string)))
250+
234251
(defun exif--process-value (value type le)
235252
"Do type-based post-processing of the value."
236253
(cl-case type

test/data/image/black-short.jpg

31 KB
Loading

test/lisp/image/exif-tests.el

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,15 @@
4141
(should (equal (exif-elem exif 'orientation) 1))
4242
(should (equal (exif-elem exif 'x-resolution) '(180 . 1)))))
4343

44+
(ert-deftest test-exif-parse-short ()
45+
(let ((exif (exif-parse-file (test-image-file "black-short.jpg"))))
46+
(should (equal (exif-elem exif 'make) "thr"))
47+
(should (equal (exif-elem exif 'model) "four"))
48+
(should (equal (exif-elem exif 'software) "em"))
49+
(should (equal (exif-elem exif 'artist) "z"))))
50+
51+
(ert-deftest test-exit-direct-ascii-value ()
52+
(equal (exif--direct-ascii-value 28005 2 t) (string ?e ?m 0))
53+
(equal (exif--direct-ascii-value 28005 2 nil) (string ?m ?e 0)))
54+
4455
;;; exif-tests.el ends here

0 commit comments

Comments
 (0)