5
|
1 /*
|
|
2 * SpanDSP - a series of DSP components for telephony
|
|
3 *
|
|
4 * v42bis_tests.c
|
|
5 *
|
|
6 * Written by Steve Underwood <steveu@coppice.org>
|
|
7 *
|
|
8 * Copyright (C) 2005 Steve Underwood
|
|
9 *
|
|
10 * All rights reserved.
|
|
11 *
|
|
12 * This program is free software; you can redistribute it and/or modify
|
|
13 * it under the terms of the GNU General Public License version 2, as
|
|
14 * published by the Free Software Foundation.
|
|
15 *
|
|
16 * This program is distributed in the hope that it will be useful,
|
|
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
19 * GNU General Public License for more details.
|
|
20 *
|
|
21 * You should have received a copy of the GNU General Public License
|
|
22 * along with this program; if not, write to the Free Software
|
|
23 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
24 *
|
|
25 * $Id: v42bis_tests.c,v 1.18 2006/11/19 14:07:28 steveu Exp $
|
|
26 */
|
|
27
|
|
28 /* THIS IS A WORK IN PROGRESS. IT IS NOT FINISHED. */
|
|
29
|
|
30 /*! \page v42bis_tests_page V.42bis tests
|
|
31 \section v42bis_tests_page_sec_1 What does it do?
|
|
32 These tests compress the contents of a file specified on the command line, writing
|
|
33 the compressed data to v42bis_tests.v42bis. They then read back the contents of the
|
|
34 compressed file, decompress, and write the results to v42bis_tests.out. The contents
|
|
35 of this file should exactly match the original file.
|
|
36 */
|
|
37
|
|
38 #ifdef HAVE_CONFIG_H
|
|
39 #include <config.h>
|
|
40 #endif
|
|
41
|
|
42 #include <stdio.h>
|
|
43 #include <inttypes.h>
|
|
44 #include <stdlib.h>
|
|
45 #include <string.h>
|
|
46 #include <unistd.h>
|
|
47 #include <errno.h>
|
|
48 #include <fcntl.h>
|
|
49 #include <ctype.h>
|
|
50 #if defined(HAVE_TGMATH_H)
|
|
51 #include <tgmath.h>
|
|
52 #endif
|
|
53 #if defined(HAVE_MATH_H)
|
|
54 #include <math.h>
|
|
55 #endif
|
|
56 #include <assert.h>
|
|
57 #include <tiffio.h>
|
|
58
|
|
59 #include "spandsp.h"
|
|
60
|
|
61 #define COMPRESSED_FILE_NAME "v42bis_tests.v42bis"
|
|
62 #define OUTPUT_FILE_NAME "v42bis_tests.out"
|
|
63
|
|
64 int in_octets_to_date = 0;
|
|
65 int out_octets_to_date = 0;
|
|
66
|
|
67 static void frame_handler(void *user_data, const uint8_t *buf, int len)
|
|
68 {
|
|
69 int ret;
|
|
70
|
|
71 if ((ret = write((intptr_t) user_data, buf, len)) != len)
|
|
72 fprintf(stderr, "Write error %d/%d\n", ret, errno);
|
|
73 out_octets_to_date += len;
|
|
74 }
|
|
75
|
|
76 static void data_handler(void *user_data, const uint8_t *buf, int len)
|
|
77 {
|
|
78 int ret;
|
|
79
|
|
80 if ((ret = write((intptr_t) user_data, buf, len)) != len)
|
|
81 fprintf(stderr, "Write error %d/%d\n", ret, errno);
|
|
82 out_octets_to_date += len;
|
|
83 }
|
|
84
|
|
85 int main(int argc, char *argv[])
|
|
86 {
|
|
87 int len;
|
|
88 v42bis_state_t state_a;
|
|
89 v42bis_state_t state_b;
|
|
90 uint8_t buf[1024];
|
|
91 int in_fd;
|
|
92 int v42bis_fd;
|
|
93 int out_fd;
|
|
94 int do_compression;
|
|
95 int do_decompression;
|
|
96 time_t now;
|
|
97
|
|
98 do_compression = TRUE;
|
|
99 do_decompression = TRUE;
|
|
100 if (argc < 2)
|
|
101 {
|
|
102 fprintf(stderr, "Usage: %s <file>\n", argv[0]);
|
|
103 exit(2);
|
|
104 }
|
|
105 if (do_compression)
|
|
106 {
|
|
107 if ((in_fd = open(argv[1], O_RDONLY)) < 0)
|
|
108 {
|
|
109 fprintf(stderr, "Error opening file '%s'.\n", argv[1]);
|
|
110 exit(2);
|
|
111 }
|
|
112 if ((v42bis_fd = open(COMPRESSED_FILE_NAME, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
|
|
113 {
|
|
114 fprintf(stderr, "Error opening file '%s'.\n", COMPRESSED_FILE_NAME);
|
|
115 exit(2);
|
|
116 }
|
|
117
|
|
118 time(&now);
|
|
119 v42bis_init(&state_a, 3, 512, 6, frame_handler, (void *) (intptr_t) v42bis_fd, 512, data_handler, NULL, 512);
|
|
120 v42bis_compression_control(&state_a, V42BIS_COMPRESSION_MODE_ALWAYS);
|
|
121 in_octets_to_date = 0;
|
|
122 out_octets_to_date = 0;
|
|
123 while ((len = read(in_fd, buf, 1024)) > 0)
|
|
124 {
|
|
125 if (v42bis_compress(&state_a, buf, len))
|
|
126 {
|
|
127 fprintf(stderr, "Bad return code from compression\n");
|
|
128 exit(2);
|
|
129 }
|
|
130 in_octets_to_date += len;
|
|
131 }
|
|
132 v42bis_compress_flush(&state_a);
|
|
133 printf("%d bytes compressed to %d bytes in %lds\n", in_octets_to_date, out_octets_to_date, time(NULL) - now);
|
|
134 close(in_fd);
|
|
135 close(v42bis_fd);
|
|
136 }
|
|
137
|
|
138 if (do_decompression)
|
|
139 {
|
|
140 /* Now open the files for the decompression. */
|
|
141 if ((v42bis_fd = open(COMPRESSED_FILE_NAME, O_RDONLY)) < 0)
|
|
142 {
|
|
143 fprintf(stderr, "Error opening file '%s'.\n", COMPRESSED_FILE_NAME);
|
|
144 exit(2);
|
|
145 }
|
|
146 if ((out_fd = open(OUTPUT_FILE_NAME, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
|
|
147 {
|
|
148 fprintf(stderr, "Error opening file '%s'.\n", OUTPUT_FILE_NAME);
|
|
149 exit(2);
|
|
150 }
|
|
151
|
|
152 time(&now);
|
|
153 v42bis_init(&state_b, 3, 512, 6, frame_handler, (void *) (intptr_t) v42bis_fd, 512, data_handler, (void *) (intptr_t) out_fd, 512);
|
|
154 in_octets_to_date = 0;
|
|
155 out_octets_to_date = 0;
|
|
156 while ((len = read(v42bis_fd, buf, 1024)) > 0)
|
|
157 {
|
|
158 if (v42bis_decompress(&state_b, buf, len))
|
|
159 {
|
|
160 fprintf(stderr, "Bad return code from decompression\n");
|
|
161 exit(2);
|
|
162 }
|
|
163 in_octets_to_date += len;
|
|
164 }
|
|
165 v42bis_decompress_flush(&state_b);
|
|
166 printf("%d bytes decompressed to %d bytes in %lds\n", in_octets_to_date, out_octets_to_date, time(NULL) - now);
|
|
167 close(v42bis_fd);
|
|
168 close(out_fd);
|
|
169 }
|
|
170 return 0;
|
|
171 }
|
|
172 /*- End of function --------------------------------------------------------*/
|
|
173 /*- End of file ------------------------------------------------------------*/
|