1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
/*
* Compression routines for the SNOW steganography program.
* Uses simple Huffman coding.
*
* Copyright (C) 1999 Matthew Kwan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
* implied. See the License for the specific language governing
* permissions and limitations under the License.
*
* For license text, see https://spdx.org/licenses/Apache-2.0>.
*/
#include "snow.h"
#include <string.h>
/*
* The Huffman codes.
*/
static const char *huffcodes[256] = {
#include "huffcode.h"
};
/*
* Local variables used for compression.
*/
static int compress_bit_count;
static int compress_value;
static unsigned long compress_bits_in;
static unsigned long compress_bits_out;
/*
* Initialize the compression routines.
*/
void
compress_init (void)
{
compress_bit_count = 0;
compress_value = 0;
compress_bits_in = 0;
compress_bits_out = 0;
encrypt_init ();
}
/*
* Compress a single bit.
*/
BOOL
compress_bit (
int bit,
FILE *inf,
FILE *outf
) {
if (!compress_flag)
return (encrypt_bit (bit, inf, outf));
compress_bits_in++;
compress_value = (compress_value << 1) | bit;
if (++compress_bit_count == 8) {
const char *s;
for (s = huffcodes[compress_value]; *s != '\0'; s++) {
int bit;
if (*s == '1')
bit = 1;
else if (*s == '0')
bit = 0;
else {
fprintf (stderr, "Illegal Huffman character '%c'\n", *s);
return (FALSE);
}
if (!encrypt_bit (bit, inf, outf))
return (FALSE);
compress_bits_out++;
}
compress_value = 0;
compress_bit_count = 0;
}
return (TRUE);
}
/*
* Flush the contents of the compression routines.
*/
BOOL
compress_flush (
FILE *inf,
FILE *outf
) {
if (compress_bit_count != 0 && !quiet_flag)
fprintf (stderr, "Warning: residual of %d bits not compressed\n",
compress_bit_count);
if (compress_bits_out > 0 && !quiet_flag) {
double cpc = (double) (compress_bits_in - compress_bits_out)
/ (double) compress_bits_in * 100.0;
if (cpc < 0.0)
fprintf (stderr,
"Compression enlarged data by %.2f%% - recommend not using compression\n",
-cpc);
else
fprintf (stderr, "Compressed by %.2f%%\n", cpc);
}
return (encrypt_flush (inf, outf));
}
/*
* Local variables used for output.
*/
static int output_bit_count;
static int output_value;
/*
* Initialize the output variables.
*/
static void
output_init (void)
{
output_bit_count = 0;
output_value = 0;
}
/*
* Output a single bit.
*/
static BOOL
output_bit (
int bit,
FILE *outf
) {
output_value = (output_value << 1) | bit;
if (++output_bit_count == 8) {
if (fputc (output_value, outf) == EOF) {
perror ("Output file");
return (FALSE);
}
output_value = 0;
output_bit_count = 0;
}
return (TRUE);
}
/*
* Flush the contents of the output routines.
*/
static BOOL
output_flush (
FILE *outf
) {
if (output_bit_count > 2 && !quiet_flag)
fprintf (stderr, "Warning: residual of %d bits not output\n",
output_bit_count);
return (TRUE);
}
/*
* Local variables used for uncompression.
*/
static int uncompress_bit_count;
static char uncompress_value[256];
/*
* Initialize the uncompression routines.
*/
void
uncompress_init (void)
{
uncompress_bit_count = 0;
output_init ();
}
/*
* Find the Huffman code string that matches.
*/
static int
huffcode_find (
const char *str
) {
int i;
for (i=0; i<256; i++)
if (strcmp (str, huffcodes[i]) == 0)
return (i);
return (-1);
}
/*
* Uncompress a single bit.
*/
BOOL
uncompress_bit (
int bit,
FILE *outf
) {
int code;
if (!compress_flag)
return (output_bit (bit, outf));
uncompress_value[uncompress_bit_count++] = bit ? '1' : '0';
uncompress_value[uncompress_bit_count] = '\0';
if ((code = huffcode_find (uncompress_value)) >= 0) {
int i;
for (i=0; i<8; i++) {
int b = ((code & (128 >> i)) != 0) ? 1 : 0;
if (!output_bit (b, outf))
return (FALSE);
}
uncompress_bit_count = 0;
}
if (uncompress_bit_count >= 255) {
fprintf (stderr, "Error: Huffman uncompress buffer overflow\n");
return (FALSE);
}
return (TRUE);
}
/*
* Flush the contents of the uncompression routines.
*/
BOOL
uncompress_flush (
FILE *outf
) {
if (uncompress_bit_count > 2 && !quiet_flag)
fprintf (stderr, "Warning: residual of %d bits not uncompressed\n",
uncompress_bit_count);
return (output_flush (outf));
}
|