From 302be04d826a3f09c8cd3d313592f7976fa8c0cb Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Mon, 10 Nov 2014 22:53:10 -0500 Subject: Proper GZIP testing Test for a GZIP'd using magic bytes. See the appropriate answer on Emacs.SE (1) for details. (1): http://emacs.stackexchange.com/a/2978 --- sx-encoding.el | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'sx-encoding.el') diff --git a/sx-encoding.el b/sx-encoding.el index 8ac12ca..98f3a7c 100644 --- a/sx-encoding.el +++ b/sx-encoding.el @@ -74,6 +74,35 @@ (substring ss 1)))))))) (replace-regexp-in-string "&[^; ]*;" get-function string))) +(defun sx-encoding-gzipped-p (data) + "Checks for magic bytes in DATA. + +Check if the first two bytes of a string in DATA match magic +numbers identifying the gzip file format. See [1] for the file +format description. + +http://www.gzip.org/zlib/rfc-gzip.html + +http://emacs.stackexchange.com/a/2978" + (equal (substring (string-as-unibyte data) 0 2) + (unibyte-string 31 139))) + +(defun sx-encoding-gzipped-buffer-p (filename) + "Check if the BUFFER is gzip-compressed. + +See `gzip-check-magic' for details." + (sx-encoding-gzip-check-magic (buffer-string))) + +(defun sx-encoding-gzipped-file-p (file) + "Check if the FILE is gzip-compressed. + +See `gzip-check-magic' for details." + (let ((first-two-bytes (with-temp-buffer + (set-buffer-multibyte nil) + (insert-file-contents-literally file nil 0 2) + (buffer-string)))) + (gzip-check-magic first-two-bytes))) + (provide 'sx-encoding) ;;; sx-encoding.el ends here -- cgit v1.2.3 From 28500694aa199c1a1bb496067ffd956bb8a9f3a9 Mon Sep 17 00:00:00 2001 From: Sean Allred Date: Tue, 11 Nov 2014 08:46:53 -0500 Subject: Use correct function name --- sx-encoding.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sx-encoding.el') diff --git a/sx-encoding.el b/sx-encoding.el index 98f3a7c..0b72365 100644 --- a/sx-encoding.el +++ b/sx-encoding.el @@ -101,7 +101,7 @@ See `gzip-check-magic' for details." (set-buffer-multibyte nil) (insert-file-contents-literally file nil 0 2) (buffer-string)))) - (gzip-check-magic first-two-bytes))) + (sx-encoding-gzipped-p first-two-bytes))) (provide 'sx-encoding) ;;; sx-encoding.el ends here -- cgit v1.2.3