aboutsummaryrefslogtreecommitdiff
path: root/sx-encoding.el
diff options
context:
space:
mode:
authorArtur Malabarba <bruce.connor.am@gmail.com>2014-11-13 02:07:43 +0000
committerArtur Malabarba <bruce.connor.am@gmail.com>2014-11-13 02:07:43 +0000
commit6355c676f48506ecb730acb200085f7b211e08c4 (patch)
tree8173b74b21d1445997159adc9a5cca261d9c8892 /sx-encoding.el
parent4cf7825918bfb60e2d1d1ce1dd342665f1161fa2 (diff)
parentcfd909e8524c37ac3bc94bec56c4e577b2c33d2d (diff)
Merge branch 'master' into sx-question-mode
Diffstat (limited to 'sx-encoding.el')
-rw-r--r--sx-encoding.el32
1 files changed, 30 insertions, 2 deletions
diff --git a/sx-encoding.el b/sx-encoding.el
index efb333e..0b72365 100644
--- a/sx-encoding.el
+++ b/sx-encoding.el
@@ -2,8 +2,7 @@
;; Copyright (C) 2014 Sean Allred
-;; Author: Sean Allred <sallred@calamity.tcs.com>
-;; Keywords: help
+;; Author: Sean Allred <code@seanallred.com>
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
@@ -75,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