123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102 |
- # -*- coding: utf-8 -*-#
-
- # Copyright 2017 Weber Yann <yann.weber@member.fsf.org>
- #
- # This file is part of HtmlSteg.
- #
- # HtmlSteg is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # any later version.
- #
- # HtmlSteg is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with HtmlSteg. If not, see <http://www.gnu.org/licenses/>.
- #
-
- import warnings
-
- R_CHARS = [' ', "\t", "\n"]
-
- def clean_html(html):
- '''Clean html squeezing repeating R_CHARS'''
- res = ''
- prev = None
- for c in html:
- if c in R_CHARS and c == prev:
- continue
- prev = c
- res += c
- return res
-
- def hide(html, payload):
- cur_val = payload[0]
- cur_bit = cur_val & 1
- cur_val >>= 1
- cur_shift = 1
- payload = payload[1:]
-
- end = False
- res = ''
- for c in html:
- if not end and c in R_CHARS:
- if cur_bit == 1:
- res += c
- if cur_shift == 8:
- cur_shift = 0
- if len(payload) == 0:
- end = True
- else:
- cur_val = payload[0]
- payload = payload[1:]
- cur_bit = cur_val & 1
- cur_val >>= 1
- cur_shift += 1
- res += c
- if not end:
- if len(payload) > 0:
- if len(payload) > 100:
- warnings.warn(
- 'Unable to hide all payload. %d bytes not hidden' % (
- len(payload)))
- else:
- warnings.warn(
- 'Unable to hide all payload. %d bytes not hidden : "%s"' % (
- len(payload), repr(payload)))
- if cur_shift != 8:
- warnings.warn(
- 'last byte not complete. %d bits missing' % (8 - cur_shift))
- return res
-
-
- def unhide(html):
- res = bytearray()
- cur_byte = 0
- cur_shift = 0
- prev = None
- pre_add = False
- for c in html:
- bit_added = False
- if c in R_CHARS and prev == c:
- #1
- cur_byte += 1 << cur_shift
- bit_added = True
- c = None
- elif prev in R_CHARS:
- #0
- bit_added = True
- if bit_added:
- if cur_shift == 7:
- #print("unhide byte ", bytearray([cur_byte]))
- res += bytearray([cur_byte])
- cur_byte = 0
- cur_shift = 0
- else:
- cur_shift += 1
- prev = c
- return res
-
|