~ubuntu-branches/ubuntu/gutsy/vnc4/gutsy

« back to all changes in this revision

Viewing changes to unix/xc/programs/Xserver/XIE/mixie/jpeg/jcxief.c

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2006-05-15 20:35:17 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20060515203517-l4lre1ku942mn26k
Tags: 4.1.1+X4.3.0-10
* Correction of critical security issue. Thanks to Martin Kogler
  <e9925248@student.tuwien.ac.at> that informed me about the issue,
  and provided the patch.
  This flaw was originally found by Steve Wiseman of intelliadmin.com.
* Applied patch from Javier Kohen <jkohen@users.sourceforge.net> that
  inform the user that only 8 first characters of the password will
  actually be used when typing more than 8 characters, closes:
  #355619.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Xorg: jcxief.c,v 1.4 2001/02/09 02:04:28 xorgcvs Exp $ */
 
2
/* Module jcxief.c */
 
3
 
 
4
/****************************************************************************
 
5
 
 
6
Copyright 1993, 1994, 1998  The Open Group
 
7
 
 
8
Permission to use, copy, modify, distribute, and sell this software and its
 
9
documentation for any purpose is hereby granted without fee, provided that
 
10
the above copyright notice appear in all copies and that both that
 
11
copyright notice and this permission notice appear in supporting
 
12
documentation.
 
13
 
 
14
The above copyright notice and this permission notice shall be included in
 
15
all copies or substantial portions of the Software.
 
16
 
 
17
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
18
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
19
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
 
20
OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
 
21
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 
22
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
23
 
 
24
Except as contained in this notice, the name of The Open Group shall not be
 
25
used in advertising or otherwise to promote the sale, use or other dealings
 
26
in this Software without prior written authorization from The Open Group.
 
27
 
 
28
 
 
29
                                NOTICE
 
30
                              
 
31
This software is being provided by AGE Logic, Inc. under the
 
32
following license.  By obtaining, using and/or copying this software,
 
33
you agree that you have read, understood, and will comply with these
 
34
terms and conditions:
 
35
 
 
36
     Permission to use, copy, modify, distribute and sell this
 
37
     software and its documentation for any purpose and without
 
38
     fee or royalty and to grant others any or all rights granted
 
39
     herein is hereby granted, provided that you agree to comply
 
40
     with the following copyright notice and statements, including
 
41
     the disclaimer, and that the same appears on all copies and
 
42
     derivative works of the software and documentation you make.
 
43
     
 
44
     "Copyright 1993, 1994 by AGE Logic, Inc."
 
45
     
 
46
     THIS SOFTWARE IS PROVIDED "AS IS".  AGE LOGIC MAKES NO
 
47
     REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED.  By way of
 
48
     example, but not limitation, AGE LOGIC MAKE NO
 
49
     REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS
 
50
     FOR ANY PARTICULAR PURPOSE OR THAT THE SOFTWARE DOES NOT
 
51
     INFRINGE THIRD-PARTY PROPRIETARY RIGHTS.  AGE LOGIC 
 
52
     SHALL BEAR NO LIABILITY FOR ANY USE OF THIS SOFTWARE.  IN NO
 
53
     EVENT SHALL EITHER PARTY BE LIABLE FOR ANY INDIRECT,
 
54
     INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOSS
 
55
     OF PROFITS, REVENUE, DATA OR USE, INCURRED BY EITHER PARTY OR
 
56
     ANY THIRD PARTY, WHETHER IN AN ACTION IN CONTRACT OR TORT OR
 
57
     BASED ON A WARRANTY, EVEN IF AGE LOGIC LICENSEES
 
58
     HEREUNDER HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
 
59
     DAMAGES.
 
60
    
 
61
     The name of AGE Logic, Inc. may not be used in
 
62
     advertising or publicity pertaining to this software without
 
63
     specific, written prior permission from AGE Logic.
 
64
 
 
65
     Title to this software shall at all times remain with AGE
 
66
     Logic, Inc.
 
67
*****************************************************************************
 
68
 
 
69
        jcxief.c: Xie JPEG Compression Wrapper Routines 
 
70
 
 
71
        Gary Rogers, AGE Logic, Inc., October 1993
 
72
        Gary Rogers, AGE Logic, Inc., January 1994
 
73
 
 
74
****************************************************************************/
 
75
 
 
76
#include "jinclude.h"
 
77
 
 
78
#ifdef THINK_C
 
79
#include <console.h>            /* command-line reader for Macintosh */
 
80
#endif
 
81
 
 
82
#if NeedFunctionPrototypes
 
83
GLOBAL void
 
84
jselrXIE (compress_info_ptr);
 
85
GLOBAL void     
 
86
process_rgb_ycc_row (compress_info_ptr, JSAMPARRAY);
 
87
GLOBAL int
 
88
jcXIE_get (compress_info_ptr, int, JSAMPARRAY);
 
89
GLOBAL void
 
90
jcXIE_get_buffersize (compress_info_ptr);
 
91
GLOBAL int
 
92
j_add_quant_table (compress_info_ptr, int, const QUANT_VAL *, int, boolean);
 
93
GLOBAL int
 
94
add_huff_table (compress_info_ptr, HUFF_TBL **, const UINT8 *, const UINT8 *);
 
95
#endif  /* NeedFunctionPrototypes */
 
96
 
 
97
/******************************************************************************/
 
98
 
 
99
LOCAL int
 
100
#if NeedFunctionPrototypes
 
101
load_quant_tables (compress_info_ptr cinfo, 
 
102
                        UINT8 * q_table, int nq_table, int scale_factor)
 
103
#else
 
104
load_quant_tables (cinfo, q_table, nq_table, scale_factor)
 
105
        compress_info_ptr cinfo;
 
106
        UINT8 * q_table;
 
107
        int nq_table;
 
108
        int scale_factor;
 
109
#endif  /* NeedFunctionPrototypes */
 
110
/* Read a set of quantization tables pointed at by q_table.
 
111
 * There may be one to NUM_QUANT_TBLS tables, each of 64 values.
 
112
 * The tables are implicitly numbered 0, 1, etc.
 
113
 */
 
114
{
 
115
  /* ZIG[i] is the zigzag-order position of the i'th element of a DCT block */
 
116
  /* read in natural order (left to right, top to bottom). */
 
117
  static const short ZIG[DCTSIZE2] = {
 
118
     0,  1,  5,  6, 14, 15, 27, 28,
 
119
     2,  4,  7, 13, 16, 26, 29, 42,
 
120
     3,  8, 12, 17, 25, 30, 41, 43,
 
121
     9, 11, 18, 24, 31, 40, 44, 53,
 
122
    10, 19, 23, 32, 39, 45, 52, 54,
 
123
    20, 22, 33, 38, 46, 51, 55, 60,
 
124
    21, 34, 37, 47, 50, 56, 59, 61,
 
125
    35, 36, 48, 49, 57, 58, 62, 63
 
126
    };
 
127
  int numtables;    
 
128
  int tblno, i;
 
129
  QUANT_TBL table;
 
130
 
 
131
  numtables = nq_table / 64;
 
132
  if (numtables < 1 || numtables > NUM_QUANT_TBLS)
 
133
        return(XIE_ERR);
 
134
    
 
135
  for (tblno = 0; tblno < numtables; tblno++)
 
136
        {
 
137
    for (i = 0; i < DCTSIZE2; i++)
 
138
      table[ZIG[i]] = *q_table++;
 
139
    j_add_quant_table(cinfo, tblno, table, scale_factor, FALSE);
 
140
  }
 
141
  return(0);
 
142
}
 
143
 
 
144
LOCAL int
 
145
#if NeedFunctionPrototypes
 
146
load_ac_tables (compress_info_ptr cinfo, 
 
147
                        UINT8 * ac_table, int nac_table)
 
148
#else
 
149
load_ac_tables (cinfo, ac_table, nac_table)
 
150
        compress_info_ptr cinfo;
 
151
        UINT8 * ac_table;
 
152
        int nac_table;
 
153
#endif  /* NeedFunctionPrototypes */
 
154
/* Read a set of AC Huffman tables pointed at by ac_table.
 
155
 * There may be one or two tables.
 
156
 */
 
157
{
 
158
  int i;
 
159
  int count;
 
160
  UINT8 bits[17];
 
161
  UINT8 val[256];
 
162
 
 
163
  bits[0] = 0;
 
164
  /* luminance AC coefficients */  
 
165
  count = 0;
 
166
  nac_table -= 16;
 
167
  if (nac_table < 0)    /* bad length */
 
168
        return(XIE_ERR);
 
169
  for(i = 1; i < 17; i++) {
 
170
    bits[i] = *ac_table++;
 
171
    count += bits[i];
 
172
  }
 
173
  nac_table -= count;
 
174
  if (nac_table < 0 || 256 < count)     /* bad length */
 
175
        return(XIE_ERR);
 
176
  for(i = 0; i < count; i++) {
 
177
    val[i] = *ac_table++;
 
178
  }
 
179
  if(XIE_ERR == add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[0], bits, val))
 
180
    return(XIE_ERR);
 
181
    
 
182
  /* chrominance AC coefficients */  
 
183
  count = 0;
 
184
  nac_table -= 16;
 
185
  if (nac_table < 0)    /* bad length */
 
186
        return(XIE_ERR);
 
187
  for(i = 1; i < 17; i++) {
 
188
    bits[i] = *ac_table++;
 
189
    count += bits[i];
 
190
  }
 
191
  nac_table -= count;
 
192
  if (nac_table < 0 || 256 < count)     /* bad length */
 
193
        return(XIE_ERR);
 
194
  for(i = 0; i < count; i++) {
 
195
    val[i] = *ac_table++;
 
196
  }
 
197
  if(XIE_ERR == add_huff_table(cinfo, &cinfo->ac_huff_tbl_ptrs[1], bits, val))
 
198
    return(XIE_ERR);
 
199
    
 
200
  return(0);
 
201
}
 
202
 
 
203
LOCAL int
 
204
#if NeedFunctionPrototypes
 
205
load_dc_tables (compress_info_ptr cinfo, 
 
206
                        UINT8 * dc_table, int ndc_table)
 
207
#else
 
208
load_dc_tables (cinfo, dc_table, ndc_table)
 
209
        compress_info_ptr cinfo;
 
210
        UINT8 * dc_table;
 
211
        int ndc_table;
 
212
#endif  /* NeedFunctionPrototypes */
 
213
/* Read a set of DC Huffman tables pointed at by dc_table.
 
214
 * There may be one or two tables.
 
215
 */
 
216
{
 
217
  int i;
 
218
  int count;
 
219
  UINT8 bits[17];
 
220
  UINT8 val[256];
 
221
 
 
222
  bits[0] = 0;
 
223
  /* luminance DC coefficients */  
 
224
  count = 0;
 
225
  ndc_table -= 16;
 
226
  if (ndc_table < 0)                            /* bad length */
 
227
        return(XIE_ERR);
 
228
  for(i = 1; i < 17; i++) {
 
229
    bits[i] = *dc_table++;
 
230
    count += bits[i];
 
231
  }
 
232
  ndc_table -= count;
 
233
  if (ndc_table < 0 || 256 < count)     /* bad length */
 
234
        return(XIE_ERR);
 
235
  for(i = 0; i < count; i++) {
 
236
    val[i] = *dc_table++;
 
237
  }
 
238
  if(XIE_ERR == add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[0], bits, val))
 
239
    return(XIE_ERR);
 
240
    
 
241
  /* chrominance DC coefficients */  
 
242
  count = 0;
 
243
  ndc_table -= 16;
 
244
  if (ndc_table < 0)                            /* bad length */
 
245
        return(XIE_ERR);
 
246
  for(i = 1; i < 17; i++) {
 
247
    bits[i] = *dc_table++;
 
248
    count += bits[i];
 
249
  }
 
250
  ndc_table -= count;
 
251
  if (ndc_table < 0 || 256 < count)     /* bad length */
 
252
        return(XIE_ERR);
 
253
  for(i = 0; i < count; i++) {
 
254
    val[i] = *dc_table++;
 
255
  }
 
256
  if(XIE_ERR == add_huff_table(cinfo, &cinfo->dc_huff_tbl_ptrs[1], bits, val))
 
257
    return(XIE_ERR);
 
258
    
 
259
  return(0);
 
260
}
 
261
 
 
262
/******************************************************************************/
 
263
 
 
264
GLOBAL int
 
265
#if NeedFunctionPrototypes
 
266
JC_INIT (compress_info_ptr cinfo,
 
267
        compress_methods_ptr cmethods, external_methods_ptr emethods)
 
268
#else
 
269
JC_INIT (cinfo, cmethods, emethods)
 
270
        compress_info_ptr cinfo;
 
271
        compress_methods_ptr cmethods;
 
272
        external_methods_ptr emethods;
 
273
#endif  /* NeedFunctionPrototypes */
 
274
{
 
275
  /* Set up links to method structures. */
 
276
  cinfo->methods = cmethods;
 
277
  cinfo->emethods = emethods;
 
278
 
 
279
  /* Set restart to NULL */
 
280
  cinfo->XIErestart = XIE_RNUL;
 
281
 
 
282
  jselrXIE (cinfo);
 
283
 
 
284
  return (XIE_NRML);
 
285
}
 
286
 
 
287
GLOBAL int
 
288
#if NeedFunctionPrototypes
 
289
JC_BEGINFRAME (compress_info_ptr cinfo,
 
290
        long components, long width, long height,
 
291
        UINT8 * q_table, int nq_table, 
 
292
        UINT8 * ac_table, int nac_table,
 
293
        UINT8 * dc_table, int ndc_table,
 
294
        short * h_sample, short * v_sample)
 
295
#else
 
296
JC_BEGINFRAME (cinfo, components, width, height,
 
297
                q_table, nq_table, ac_table, nac_table, dc_table, ndc_table,
 
298
                h_sample, v_sample)
 
299
        compress_info_ptr cinfo; 
 
300
        long components; 
 
301
        long width; 
 
302
        long height;
 
303
        UINT8 * q_table;
 
304
        int nq_table;
 
305
        UINT8 * ac_table;
 
306
        int nac_table;
 
307
        UINT8 * dc_table;
 
308
        int ndc_table;
 
309
      short * h_sample, * v_sample;
 
310
#endif  /* NeedFunctionPrototypes */
 
311
{
 
312
  short ci;
 
313
  short hsample, vsample, total;
 
314
  int status;
 
315
  int scale_factor = 100;
 
316
        
 
317
  if (cinfo->XIErestart == XIE_RNUL) {
 
318
    cinfo->input_components = (short)components;
 
319
    cinfo->image_width = width;
 
320
    cinfo->image_height = height;
 
321
    if (components == 1)
 
322
      cinfo->in_color_space = CS_GRAYSCALE;
 
323
    else
 
324
      cinfo->in_color_space = CS_RGB;
 
325
 
 
326
    if (1 < components) {
 
327
      total = 0;
 
328
      for(ci = 0; ci < components; ci++) {
 
329
        hsample = h_sample[ci];
 
330
        if (hsample <= 0 || MAX_SAMP_FACTOR < hsample)
 
331
          return(XIE_ERR);  /* bad sub-sampling factor */
 
332
        vsample = v_sample[ci];
 
333
        if (vsample <= 0 || MAX_SAMP_FACTOR < vsample)
 
334
          return(XIE_ERR);  /* bad sub-sampling factor */
 
335
        total += (hsample*vsample);
 
336
        if (MAX_BLOCKS_IN_MCU < total)
 
337
          return(XIE_ERR);  /* bad sub-sampling factor(s) */
 
338
        cinfo->xie_h_samp_factor[ci] = hsample;            
 
339
        cinfo->xie_v_samp_factor[ci] = vsample;            
 
340
      }
 
341
    }
 
342
 
 
343
    if (((*cinfo->methods->input_init) (cinfo)) == XIE_ERR)     /* jcXIE_init (cinfo); */
 
344
      return(XIE_ERR);
 
345
  }
 
346
  
 
347
  if (0 < nq_table) {
 
348
        if ((load_quant_tables (cinfo, q_table, nq_table, scale_factor)) == XIE_ERR)
 
349
                return(XIE_ERR);
 
350
  }
 
351
        
 
352
  if (0 < nac_table) {
 
353
        if ((load_ac_tables (cinfo, ac_table, nac_table)) == XIE_ERR)
 
354
                return(XIE_ERR);
 
355
  }
 
356
        
 
357
  if (0 < ndc_table) {
 
358
        if ((load_dc_tables (cinfo, dc_table, ndc_table)) == XIE_ERR)
 
359
                return(XIE_ERR);
 
360
  }
 
361
        
 
362
  if ((cinfo->XIErestart == XIE_RNUL) || (cinfo->XIErestart == XIE_RWFH)) {
 
363
    if ((status = ((*cinfo->methods->write_file_header) (cinfo))) < 0) {
 
364
          if (status == XIE_ERR)
 
365
        return(XIE_ERR);
 
366
      cinfo->XIErestart = XIE_RWFH;     /* set restart status */
 
367
      return(XIE_OUT);  
 
368
    }
 
369
  }
 
370
  if ((cinfo->XIErestart == XIE_RNUL) || (cinfo->XIErestart == XIE_RWSH)) {
 
371
        if ((status = ((*cinfo->methods->write_scan_header) (cinfo))) < 0) {
 
372
          if (status == XIE_ERR)
 
373
        return(XIE_ERR);
 
374
      cinfo->XIErestart = XIE_RWSH;     /* set restart status */
 
375
      return(XIE_OUT);  
 
376
    }
 
377
  }
 
378
 
 
379
  return (XIE_NRML);
 
380
}
 
381
 
 
382
GLOBAL int
 
383
#if NeedFunctionPrototypes
 
384
JC_ENDFRAME (compress_info_ptr cinfo)
 
385
#else
 
386
JC_ENDFRAME (cinfo)
 
387
        compress_info_ptr cinfo;
 
388
#endif  /* NeedFunctionPrototypes */
 
389
{
 
390
  int status;
 
391
        
 
392
  /* jcXIE_term (cinfo); */
 
393
  if ((cinfo->XIErestart == XIE_RNUL) || (cinfo->XIErestart == XIE_RMCU)) {
 
394
        if ((status = ((*cinfo->methods->input_term) (cinfo))) < 0) {
 
395
      if (status == XIE_ERR)
 
396
        return(XIE_ERR);
 
397
      cinfo->XIErestart = XIE_RMCU;     /* set restart status */
 
398
      return(XIE_OUT);  
 
399
    }
 
400
  }
 
401
  
 
402
  (*cinfo->methods->write_scan_trailer) (cinfo); /* does nothing */
 
403
  if ((cinfo->XIErestart == XIE_RNUL) || (cinfo->XIErestart == XIE_RWFT)) {
 
404
        if (((*cinfo->methods->write_file_trailer) (cinfo)) == XIE_OUT) {
 
405
          cinfo->XIErestart = XIE_RWFT; /* set restart status */
 
406
      return(XIE_OUT);  
 
407
    }
 
408
  }
 
409
  return (XIE_NRML);
 
410
}
 
411
 
 
412
GLOBAL int
 
413
#if NeedFunctionPrototypes
 
414
JC_SCANLINE_RGB (compress_info_ptr cinfo, 
 
415
        int * scanline, JSAMPROW ptr0, JSAMPROW ptr1, JSAMPROW ptr2)
 
416
#else
 
417
JC_SCANLINE_RGB (cinfo, scanline, ptr0, ptr1, ptr2)
 
418
        compress_info_ptr cinfo; 
 
419
        int * scanline; 
 
420
        JSAMPROW ptr0; 
 
421
        JSAMPROW ptr1; 
 
422
        JSAMPROW ptr2;
 
423
#endif  /* NeedFunctionPrototypes */
 
424
{
 
425
  JSAMPROW pixel_row[3];
 
426
  int row, rows_this_time;
 
427
  int status;
 
428
 
 
429
  row = *scanline;      
 
430
  if (cinfo->XIErestart == XIE_RNUL) {
 
431
    if (cinfo->cur_pixel_row <= 0)      {
 
432
      rows_this_time = (int) MIN((long) cinfo->rows_in_mem,
 
433
                    cinfo->image_height - row);
 
434
      if (rows_this_time <= 0)
 
435
        return (XIE_NRML);
 
436
      cinfo->rows_this_time = rows_this_time;
 
437
    }
 
438
    pixel_row[0] = ptr0;
 
439
    pixel_row[1] = ptr1;
 
440
    pixel_row[2] = ptr2;
 
441
  }
 
442
  if ((cinfo->XIErestart == XIE_RNUL) || (cinfo->XIErestart == XIE_RMCU)) {
 
443
    if ((status = jcXIE_get (cinfo, row, pixel_row)) < 0) {
 
444
      if (status == XIE_ERR)
 
445
        return(XIE_ERR);
 
446
      cinfo->XIErestart = XIE_RMCU;     /* set restart status */
 
447
      return(XIE_OUT);  
 
448
    }
 
449
  }
 
450
  
 
451
  return (XIE_NRML);
 
452
}
 
453
 
 
454
GLOBAL int
 
455
#if NeedFunctionPrototypes
 
456
JC_SCANLINE_GRAY (compress_info_ptr cinfo, int * scanline, JSAMPROW ptr0)
 
457
#else
 
458
JC_SCANLINE_GRAY (cinfo, scanline, ptr0)
 
459
        compress_info_ptr cinfo; 
 
460
        int * scanline; 
 
461
        JSAMPROW ptr0;
 
462
#endif  /* NeedFunctionPrototypes */
 
463
{
 
464
  JSAMPROW pixel_row[3];
 
465
  int row, rows_this_time;
 
466
  int status;
 
467
 
 
468
  row = *scanline;      
 
469
  if (cinfo->XIErestart == XIE_RNUL) {
 
470
    if (cinfo->cur_pixel_row <= 0)      {
 
471
      rows_this_time = (int) MIN((long) cinfo->rows_in_mem,
 
472
                    cinfo->image_height - row);
 
473
      if (rows_this_time <= 0)
 
474
        return (XIE_NRML);
 
475
      cinfo->rows_this_time = rows_this_time;
 
476
    }
 
477
    pixel_row[0] = ptr0;
 
478
    pixel_row[1] = 0;
 
479
    pixel_row[2] = 0;
 
480
  }
 
481
  if ((cinfo->XIErestart == XIE_RNUL) || (cinfo->XIErestart == XIE_RMCU)) {
 
482
    if ((status = jcXIE_get (cinfo, row, pixel_row)) < 0) {
 
483
      if (status == XIE_ERR)
 
484
        return(XIE_ERR);
 
485
      cinfo->XIErestart = XIE_RMCU;     /* set restart status */
 
486
      return(XIE_OUT);  
 
487
    }
 
488
  }
 
489
  
 
490
  return (XIE_NRML);
 
491
}