comparison spandsp-0.0.6pre17/tests/v42bis_tests.c @ 4:26cd8f1ef0b1

import spandsp-0.0.6pre17
author Peter Meerwald <pmeerw@cosy.sbg.ac.at>
date Fri, 25 Jun 2010 15:50:58 +0200
parents
children
comparison
equal deleted inserted replaced
3:c6c5a16ce2f2 4:26cd8f1ef0b1
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.24 2008/11/15 14:43:08 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 #if defined(HAVE_CONFIG_H)
39 #include <config.h>
40 #endif
41
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <fcntl.h>
45 #include <string.h>
46 #include <unistd.h>
47 #include <errno.h>
48 #include <ctype.h>
49 #include <assert.h>
50
51 #include "spandsp.h"
52
53 #include "spandsp/private/v42bis.h"
54
55 #define COMPRESSED_FILE_NAME "v42bis_tests.v42bis"
56 #define OUTPUT_FILE_NAME "v42bis_tests.out"
57
58 int in_octets_to_date = 0;
59 int out_octets_to_date = 0;
60
61 static void frame_handler(void *user_data, const uint8_t *buf, int len)
62 {
63 int ret;
64
65 if ((ret = write((intptr_t) user_data, buf, len)) != len)
66 fprintf(stderr, "Write error %d/%d\n", ret, errno);
67 out_octets_to_date += len;
68 }
69
70 static void data_handler(void *user_data, const uint8_t *buf, int len)
71 {
72 int ret;
73
74 if ((ret = write((intptr_t) user_data, buf, len)) != len)
75 fprintf(stderr, "Write error %d/%d\n", ret, errno);
76 out_octets_to_date += len;
77 }
78
79 int main(int argc, char *argv[])
80 {
81 int len;
82 v42bis_state_t state_a;
83 v42bis_state_t state_b;
84 uint8_t buf[1024];
85 int in_fd;
86 int v42bis_fd;
87 int out_fd;
88 int do_compression;
89 int do_decompression;
90 time_t now;
91
92 do_compression = TRUE;
93 do_decompression = TRUE;
94 if (argc < 2)
95 {
96 fprintf(stderr, "Usage: %s <file>\n", argv[0]);
97 exit(2);
98 }
99 if (do_compression)
100 {
101 if ((in_fd = open(argv[1], O_RDONLY)) < 0)
102 {
103 fprintf(stderr, "Error opening file '%s'.\n", argv[1]);
104 exit(2);
105 }
106 if ((v42bis_fd = open(COMPRESSED_FILE_NAME, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
107 {
108 fprintf(stderr, "Error opening file '%s'.\n", COMPRESSED_FILE_NAME);
109 exit(2);
110 }
111
112 time(&now);
113 v42bis_init(&state_a, 3, 512, 6, frame_handler, (void *) (intptr_t) v42bis_fd, 512, data_handler, NULL, 512);
114 v42bis_compression_control(&state_a, V42BIS_COMPRESSION_MODE_ALWAYS);
115 in_octets_to_date = 0;
116 out_octets_to_date = 0;
117 while ((len = read(in_fd, buf, 1024)) > 0)
118 {
119 if (v42bis_compress(&state_a, buf, len))
120 {
121 fprintf(stderr, "Bad return code from compression\n");
122 exit(2);
123 }
124 in_octets_to_date += len;
125 }
126 v42bis_compress_flush(&state_a);
127 printf("%d bytes compressed to %d bytes in %lds\n", in_octets_to_date, out_octets_to_date, time(NULL) - now);
128 close(in_fd);
129 close(v42bis_fd);
130 }
131
132 if (do_decompression)
133 {
134 /* Now open the files for the decompression. */
135 if ((v42bis_fd = open(COMPRESSED_FILE_NAME, O_RDONLY)) < 0)
136 {
137 fprintf(stderr, "Error opening file '%s'.\n", COMPRESSED_FILE_NAME);
138 exit(2);
139 }
140 if ((out_fd = open(OUTPUT_FILE_NAME, O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 0)
141 {
142 fprintf(stderr, "Error opening file '%s'.\n", OUTPUT_FILE_NAME);
143 exit(2);
144 }
145
146 time(&now);
147 v42bis_init(&state_b, 3, 512, 6, frame_handler, (void *) (intptr_t) v42bis_fd, 512, data_handler, (void *) (intptr_t) out_fd, 512);
148 in_octets_to_date = 0;
149 out_octets_to_date = 0;
150 while ((len = read(v42bis_fd, buf, 1024)) > 0)
151 {
152 if (v42bis_decompress(&state_b, buf, len))
153 {
154 fprintf(stderr, "Bad return code from decompression\n");
155 exit(2);
156 }
157 in_octets_to_date += len;
158 }
159 v42bis_decompress_flush(&state_b);
160 printf("%d bytes decompressed to %d bytes in %lds\n", in_octets_to_date, out_octets_to_date, time(NULL) - now);
161 close(v42bis_fd);
162 close(out_fd);
163 }
164 return 0;
165 }
166 /*- End of function --------------------------------------------------------*/
167 /*- End of file ------------------------------------------------------------*/

Repositories maintained by Peter Meerwald, pmeerw@pmeerw.net.