diff options
author | Sean Allred <code@seanallred.com> | 2014-11-10 22:53:10 -0500 |
---|---|---|
committer | Sean Allred <code@seanallred.com> | 2014-11-10 22:53:10 -0500 |
commit | 302be04d826a3f09c8cd3d313592f7976fa8c0cb (patch) | |
tree | c1d43fff50561e86791f444307adccf3371195a1 /sx-encoding.el | |
parent | 5305686182f9e233d0c4d056eab9f28a40e3932c (diff) |
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
Diffstat (limited to 'sx-encoding.el')
-rw-r--r-- | sx-encoding.el | 29 |
1 files changed, 29 insertions, 0 deletions
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 |