Debian Codegraph @

libhylafax/FaxParams.c++

raw
1 /*
2 * Copyright (c) 1990-1996 Sam Leffler
3 * Copyright (c) 1991-1996 Silicon Graphics, Inc.
4 * HylaFAX is a trademark of Silicon Graphics
5 *
6 * Permission to use, copy, modify, distribute, and sell this software and
7 * its documentation for any purpose is hereby granted without fee, provided
8 * that (i) the above copyright notices and this permission notice appear in
9 * all copies of the software and related documentation, and (ii) the names of
10 * Sam Leffler and Silicon Graphics may not be used in any advertising or
11 * publicity relating to the software without the specific, prior written
12 * permission of Sam Leffler and Silicon Graphics.
13 *
14 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
15 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
16 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
17 *
18 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
19 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
20 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
21 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
22 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
23 * OF THIS SOFTWARE.
24 */
25
26 #include "FaxParams.h"
27 #include "Class2Params.h"
28 #include "SystemLog.h"
29
30 const int FaxParams::BITNUM_V8_CAPABLE = 6;
31 const int FaxParams::BITNUM_FRAMESIZE_DIS = 7;
32 const int FaxParams::BITNUM_T4XMTR = 9;
33 const int FaxParams::BITNUM_T4RCVR = 10;
34 const int FaxParams::BITNUM_SIGRATE_11 = 11;
35 const int FaxParams::BITNUM_SIGRATE_12 = 12;
36 const int FaxParams::BITNUM_SIGRATE_13 = 13;
37 const int FaxParams::BITNUM_SIGRATE_14 = 14;
38 const int FaxParams::BITNUM_VR_FINE = 15;
39 const int FaxParams::BITNUM_2DMR = 16;
40 const int FaxParams::BITNUM_WIDTH_17 = 17;
41 const int FaxParams::BITNUM_WIDTH_18 = 18;
42 const int FaxParams::BITNUM_LENGTH_19 = 19;
43 const int FaxParams::BITNUM_LENGTH_20 = 20;
44 const int FaxParams::BITNUM_ST_21 = 21;
45 const int FaxParams::BITNUM_ST_22 = 22;
46 const int FaxParams::BITNUM_ST_23 = 23;
47 const int FaxParams::BITNUM_ECM = 27;
48 const int FaxParams::BITNUM_FRAMESIZE_DCS = 28;
49 const int FaxParams::BITNUM_2DMMR = 31;
50 const int FaxParams::BITNUM_JBIG = 36;
51 const int FaxParams::BITNUM_VR_R8 = 41;
52 const int FaxParams::BITNUM_VR_300X300 = 42;
53 const int FaxParams::BITNUM_VR_R16 = 43;
54 const int FaxParams::BITNUM_INCH_RES = 44;
55 const int FaxParams::BITNUM_METRIC_RES = 45;
56 const int FaxParams::BITNUM_SEP = 47;
57 const int FaxParams::BITNUM_SUB = 49;
58 const int FaxParams::BITNUM_PWD = 50;
59 const int FaxParams::BITNUM_JPEG = 68;
60 const int FaxParams::BITNUM_FULLCOLOR = 69;
61 const int FaxParams::BITNUM_LETTER_SIZE = 76;
62 const int FaxParams::BITNUM_LEGAL_SIZE = 77;
63 const int FaxParams::BITNUM_JBIG_BASIC = 78;
64 const int FaxParams::BITNUM_JBIG_L0 = 79;
65
66 FaxParams::FaxParams()
67 {
68 initializeBitString();
69 }
70
71 FaxParams::FaxParams(u_char* pBits, int length)
72 {
73 setupT30(pBits, length);
74 }
75
76 FaxParams::~FaxParams (void)
77 {
78 }
79
80 /*
81 * Convert old dis+xinfo into FaxParams. This should go away eventually.
82 */
83 FaxParams::FaxParams(u_int disDcs, u_int xinfo)
84 {
85 setupT30(disDcs, xinfo);
86 }
87
88 /*
89 * Set all bits to zero. According to Table 2 T.30 NOTE 1, reserved
90 * bits should also be set to zero.
91 */
92 void FaxParams::initializeBitString()
93 {
94 for (int i = 0; i < MAX_BITSTRING_BYTES; i++) m_bits[i] = 0;
95 }
96
97 void FaxParams::setupT30(u_char* pBits, int length)
98 {
99 initializeBitString();
100 bool lastbyte = false;
101
102 for (int byte = 0; byte < MAX_BITSTRING_BYTES && byte < length; byte++) {
103 if (!lastbyte) m_bits[byte] = pBits[byte];
104 else m_bits[byte] = 0; // clear bits when they have no meaning
105
106 if (byte > 2 && !(m_bits[byte] & 0x01)) lastbyte = true;
107 }
108
109 // Don't allow the last byte to have the extend bit set.
110 m_bits[MAX_BITSTRING_BYTES-1] = m_bits[MAX_BITSTRING_BYTES-1] & 0xFE;
111 }
112
113 void FaxParams::setupT30 (u_int disDcs, u_int xinfo)
114 {
115 initializeBitString();
116 m_bits[0] = (disDcs&0xFF0000) >> 16;
117 m_bits[1] = (disDcs&0x00FF00) >> 8;
118 m_bits[2] = (disDcs&0x0000FF) >> 0;
119
120 m_bits[3] = (xinfo&0xFF000000) >> 24;
121 m_bits[4] = (xinfo&0x00FF0000) >> 16;
122 m_bits[5] = (xinfo&0x0000FF00) >> 8;
123 m_bits[6] = (xinfo&0x000000FF) >> 0;
124 }
125
126
127 /*
128 * Table 2 T.30 defines bit numbers 1 ... 127.
129 * Anything else is invalid and should not be used.
130 */
131 bool FaxParams::validBitNumber(int bitNum)
132 {
133 return ((bitNum >= 1) && (bitNum <= 127));
134 }
135
136 bool FaxParams::isBitEnabled(int bitNum)
137 {
138 if (!validBitNumber(bitNum)) return false;
139 return m_bits[calculateByteNumber(bitNum)] & calculateMask(bitNum);
140 }
141
142 void FaxParams::setExtendBits(int byteNum)
143 {
144 if (byteNum >= 3) {
145 for (int byte = byteNum-1; byte >= 2; byte--)
146 m_bits[byte] = m_bits[byte] | 0x01;
147 }
148 }
149
150 void FaxParams::unsetExtendBits()
151 {
152 for (int byte = MAX_BITSTRING_BYTES-1; byte >= 2; byte--) {
153 if (m_bits[byte] & 0xFF) {
154 //extend field is needed in previous byte
155 break;
156 } else {
157 //extend field NOT needed in previous byte
158 //so unset it
159 m_bits[byte-1] = m_bits[byte-1] & 0xFE;
160 }
161 }
162 }
163
164 void FaxParams::setBit(int bitNum, bool val)
165 {
166 if (!validBitNumber(bitNum)) return;
167
168 int byteNum = calculateByteNumber(bitNum);
169 u_char mask = calculateMask(bitNum);
170
171 if (val) {
172 m_bits[byteNum] = m_bits[byteNum] | mask;
173 setExtendBits(byteNum);
174 } else {
175 m_bits[byteNum] = m_bits[byteNum] & ~mask;
176 unsetExtendBits();
177 }
178 }
179
180 int FaxParams::calculateByteNumber(int bitNum)
181 {
182 //Subtract 1 from bitNum because Table 2 T.30 indexes
183 //bit numbers from 1 and C indexes everything from 0.
184
185 return (bitNum-1)/8;
186 }
187
188 u_char FaxParams::calculateMask(int bitNum)
189 {
190 //Subtract 1 from bitNum because Table 2 T.30 indexes
191 //bit numbers from 1 and C indexes everything from 0.
192
193 int shiftLeft = 7-((bitNum-1)%8);
194 u_char mask = 0x01 << shiftLeft;
195 return mask;
196 }
197
198 bool FaxParams::hasNextByte(int byteNum) const
199 {
200 return (byteNum <= 1 || (m_bits[byteNum] & 0x01));
201 }
202
203 u_char FaxParams::getByte(int byteNum) const
204 {
205 return m_bits[byteNum];
206 }
207
208 /*
209 * Convert FaxParams and return a formatted ASCII DCS/DIS equivalent.
210 */
211 void FaxParams::asciiEncode(fxStr& response) const
212 {
213 u_int byte = 0;
214 do {
215 if (byte) response.append(" ");
216 response.append(fxStr::format("%.2X", getByte(byte)));
217 } while (hasNextByte(byte++));
218 }
219
220 /*
221 * Reverse of asciiEncode, take ASCII string and make it into FaxParams
222 */
223 void FaxParams::asciiDecode(const char* dcs)
224 {
225 u_int byte = 0;
226 while (dcs[0] != '\0' && dcs[1] != '\0') {
227 m_bits[byte] = ((dcs[0] - (dcs[0] > 64 ? 55 : 48)) << 4) + (dcs[1] - (dcs[1] > 64 ? 55 : 48));
228 setExtendBits(byte++);
229 dcs += 2;
230 if (dcs[0] == ' ') dcs++;
231 }
232 }
233
234 bool FaxParams::operator==(const FaxParams& operand) const
235 {
236 bool equals = true;
237 u_short byte = 0;
238
239 // We do not compare the next bytes if the extend-bit is not set.
240 while (equals && byte < MAX_BITSTRING_BYTES && (byte < 3 || m_bits[byte] & 0x01)) {
241 if (m_bits[byte] != operand.m_bits[byte]) equals = false;
242 byte++;
243 }
244 return equals;
245 }
246
247 bool FaxParams::operator!=(const FaxParams& operand) const
248 {
249 return !operator==(operand);
250 }
251
252 FaxParams& FaxParams::assign(const FaxParams& operand)
253 {
254 memcpy(m_bits, operand.m_bits, sizeof(m_bits));
255 return *this;
256 }
257
258 void FaxParams::update(bool isDIS)
259 {
260 }