diff options
author | Sean Allred <code@seanallred.com> | 2014-11-11 08:59:01 -0500 |
---|---|---|
committer | Sean Allred <code@seanallred.com> | 2014-11-11 08:59:01 -0500 |
commit | 92d30aeee31c80238a4ce5a825806dadc0e3a065 (patch) | |
tree | f6b9c678adba38733de594b76a0e97253436299a /sx-encoding.el | |
parent | 5305686182f9e233d0c4d056eab9f28a40e3932c (diff) | |
parent | 94e649f7318b2518fe31af7cdea74b7f7bfde1e3 (diff) |
Merge pull request #38 from vermiculus/gzip-testing
Proper GZIP testing
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..0b72365 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)))) + (sx-encoding-gzipped-p first-two-bytes))) + (provide 'sx-encoding) ;;; sx-encoding.el ends here |