summaryrefslogtreecommitdiff
path: root/modules/autorotate/lib/pel/PelDataWindow.php
blob: b40722c13e510f54e179d5be5233aeba21988b5c (plain)
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
<?php

/**
 *  PEL: PHP Exif Library.  A library with support for reading and
 *  writing all Exif headers in JPEG and TIFF images using PHP.
 *
 *  Copyright (C) 2004, 2005, 2006, 2007  Martin Geisler.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program in the file COPYING; if not, write to the
 *  Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 *  Boston, MA 02110-1301 USA
 */

/* $Id$ */


/**
 * A container for bytes with a limited window of accessible bytes.
 *
 * @author Martin Geisler <mgeisler@users.sourceforge.net>
 * @version $Revision$
 * @date $Date$
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License (GPL)
 * @package PEL
 */

/**#@+ Required class definitions. */
require_once('PelException.php');
require_once('PelConvert.php');
/**#@-*/


/**
 * An exception thrown when an invalid offset is encountered.
 *
 * @package PEL
 * @subpackage Exception
 */
class PelDataWindowOffsetException extends PelException {}

/**
 * An exception thrown when an invalid window is encountered.
 *
 * @package PEL
 * @subpackage Exception
 */
class PelDataWindowWindowException extends PelException {}

/**
 * The window.
 *
 * @package PEL
 */
class PelDataWindow {

    /**
     * The data held by this window.
     *
     * The string can contain any kind of data, including binary data.
     *
     * @var string
     */
    private $data = '';

    /**
     * The byte order currently in use.
     *
     * This will be the byte order used when data is read using the for
     * example the {@link getShort} function.  It must be one of {@link
     * PelConvert::LITTLE_ENDIAN} and {@link PelConvert::BIG_ENDIAN}.
     *
     * @var PelByteOrder
     * @see setByteOrder, getByteOrder
     */
    private $order;

    /**
     * The start of the current window.
     *
     * All offsets used for access into the data will count from this
     * offset, effectively limiting access to a window starting at this
     * byte.
     *
     * @var int
     * @see setWindowStart
     */
    private $start  = 0;

    /**
     * The size of the current window.
     *
     * All offsets used for access into the data will be limited by this
     * variable.  A valid offset must be strictly less than this
     * variable.
     *
     * @var int
     * @see setWindowSize
     */
    private $size   = 0;


    /**
     * Construct a new data window with the data supplied.
     *
     * @param mixed the data that this window will contain. This can
     * either be given as a string (interpreted litteraly as a sequence
     * of bytes) or a PHP image resource handle. The data will be copied
     * into the new data window.
     *
     * @param boolean the initial byte order of the window.  This must
     * be either {@link PelConvert::LITTLE_ENDIAN} or {@link
     * PelConvert::BIG_ENDIAN}.  This will be used when integers are
     * read from the data, and it can be changed later with {@link
     * setByteOrder()}.
     */
    function __construct($data = '', $endianess = PelConvert::LITTLE_ENDIAN) {
        if (is_string($data)) {
            $this->data = $data;
        } elseif (is_resource($data) && get_resource_type($data) == 'gd') {
            /* The ImageJpeg() function insists on printing the bytes
             * instead of returning them in a more civil way as a string, so
             * we have to buffer the output... */
            ob_start();
            ImageJpeg($data, null, Pel::getJPEGQuality());
            $this->data = ob_get_clean();
        } else {
            throw new PelInvalidArgumentException('Bad type for $data: %s',
            gettype($data));
        }

        $this->order = $endianess;
        $this->size  = strlen($this->data);
    }


    /**
     * Get the size of the data window.
     *
     * @return int the number of bytes covered by the window.  The
     * allowed offsets go from 0 up to this number minus one.
     *
     * @see getBytes()
     */
    function getSize() {
        return $this->size;
    }


    /**
     * Change the byte order of the data.
     *
     * @param PelByteOrder the new byte order.  This must be either
     * {@link PelConvert::LITTLE_ENDIAN} or {@link
     * PelConvert::BIG_ENDIAN}.
     */
    function setByteOrder($o) {
        $this->order = $o;
    }


    /**
     * Get the currently used byte order.
     *
     * @return PelByteOrder this will be either {@link
     * PelConvert::LITTLE_ENDIAN} or {@link PelConvert::BIG_ENDIAN}.
     */
    function getByteOrder() {
        return $this->order;
    }


    /* Move the start of the window forward.
     *
     * @param int the new start of the window.  All new offsets will be
     * calculated from this new start offset, and the size of the window
     * will shrink to keep the end of the window in place.
     */
    function setWindowStart($start) {
        if ($start < 0 || $start > $this->size)
        throw new PelDataWindowWindowException('Window [%d, %d] does ' .
                                             'not fit in window [0, %d]',
        $start, $this->size, $this->size);

        $this->start += $start;
        $this->size  -= $start;
    }


    /**
     * Adjust the size of the window.
     *
     * The size can only be made smaller.
     *
     * @param int the desired size of the window.  If the argument is
     * negative, the window will be shrunk by the argument.
     */
    function setWindowSize($size) {
        if ($size < 0)
        $size += $this->size;

        if ($size < 0 || $size > $this->size)
        throw new PelDataWindowWindowException('Window [0, %d] ' .
                                             'does not fit in window [0, %d]',
        $size, $this->size);
        $this->size = $size;
    }


    /**
     * Make a new data window with the same data as the this window.
     *
     * @param mixed if an integer is supplied, then it will be the start
     * of the window in the clone.  If left unspecified, then the clone
     * will inherit the start from this object.
     *
     * @param mixed if an integer is supplied, then it will be the size
     * of the window in the clone.  If left unspecified, then the clone
     * will inherit the size from this object.
     *
     * @return PelDataWindow a new window that operates on the same data
     * as this window, but (optionally) with a smaller window size.
     */
    function getClone($start = false, $size = false) {
        $c = clone $this;

        if (is_int($start))
        $c->setWindowStart($start);

        if (is_int($size))
        $c->setWindowSize($size);

        return $c;
    }


    /**
     * Validate an offset against the current window.
     *
     * @param int the offset to be validated.  If the offset is negative
     * or if it is greater than or equal to the current window size,
     * then a {@link PelDataWindowOffsetException} is thrown.
     *
     * @return void if the offset is valid nothing is returned, if it is
     * invalid a new {@link PelDataWindowOffsetException} is thrown.
     */
    private function validateOffset($o) {
        if ($o < 0 || $o >= $this->size)
        throw new PelDataWindowOffsetException('Offset %d not within [%d, %d]',
        $o, 0, $this->size-1);
    }


    /**
     * Return some or all bytes visible in the window.
     *
     * This method works just like the standard {@link substr()}
     * function in PHP with the exception that it works within the
     * window of accessible bytes and does strict range checking.
     *
     * @param int the offset to the first byte returned.  If a negative
     * number is given, then the counting will be from the end of the
     * window.  Invalid offsets will result in a {@link
     * PelDataWindowOffsetException} being thrown.
     *
     * @param int the size of the sub-window.  If a negative number is
     * given, then that many bytes will be omitted from the result.
     *
     * @return string a subset of the bytes in the window.  This will
     * always return no more than {@link getSize()} bytes.
     */
    function getBytes($start = false, $size = false) {
        if (is_int($start)) {
            if ($start < 0)
            $start += $this->size;

            $this->validateOffset($start);
        } else {
            $start = 0;
        }

        if (is_int($size)) {
            if ($size <= 0)
            $size += $this->size - $start;

            $this->validateOffset($start+$size);
        } else {
            $size = $this->size - $start;
        }

        return substr($this->data, $this->start + $start, $size);
    }


    /**
     * Return an unsigned byte from the data.
     *
     * @param int the offset into the data.  An offset of zero will
     * return the first byte in the current allowed window.  The last
     * valid offset is equal to {@link getSize()}-1.  Invalid offsets
     * will result in a {@link PelDataWindowOffsetException} being
     * thrown.
     *
     * @return  int  the unsigned byte found at offset.
     */
    function getByte($o = 0) {
        /* Validate the offset --- this throws an exception if offset is
         * out of range. */
        $this->validateOffset($o);

        /* Translate the offset into an offset into the data. */
        $o += $this->start;

        /* Return an unsigned byte. */
        return PelConvert::bytesToByte($this->data, $o);
    }


    /**
     * Return a signed byte from the data.
     *
     * @param int the offset into the data.  An offset of zero will
     * return the first byte in the current allowed window.  The last
     * valid offset is equal to {@link getSize()}-1.  Invalid offsets
     * will result in a {@link PelDataWindowOffsetException} being
     * thrown.
     *
     * @return  int  the signed byte found at offset.
     */
    function getSByte($o = 0) {
        /* Validate the offset --- this throws an exception if offset is
         * out of range. */
        $this->validateOffset($o);

        /* Translate the offset into an offset into the data. */
        $o += $this->start;

        /* Return a signed byte. */
        return PelConvert::bytesToSByte($this->data, $o);
    }


    /**
     * Return an unsigned short read from the data.
     *
     * @param int the offset into the data.  An offset of zero will
     * return the first short available in the current allowed window.
     * The last valid offset is equal to {@link getSize()}-2.  Invalid
     * offsets will result in a {@link PelDataWindowOffsetException}
     * being thrown.
     *
     * @return  int  the unsigned short found at offset.
     */
    function getShort($o = 0) {
        /* Validate the offset+1 to see if we can safely get two bytes ---
         * this throws an exception if offset is out of range. */
        $this->validateOffset($o);
        $this->validateOffset($o+1);

        /* Translate the offset into an offset into the data. */
        $o += $this->start;

        /* Return an unsigned short. */
        return PelConvert::bytesToShort($this->data, $o, $this->order);
    }


    /**
     * Return a signed short read from the data.
     *
     * @param int the offset into the data.  An offset of zero will
     * return the first short available in the current allowed window.
     * The last valid offset is equal to {@link getSize()}-2.  Invalid
     * offsets will result in a {@link PelDataWindowOffsetException}
     * being thrown.
     *
     * @return  int  the signed short found at offset.
     */
    function getSShort($o = 0) {
        /* Validate the offset+1 to see if we can safely get two bytes ---
         * this throws an exception if offset is out of range. */
        $this->validateOffset($o);
        $this->validateOffset($o+1);

        /* Translate the offset into an offset into the data. */
        $o += $this->start;

        /* Return a signed short. */
        return PelConvert::bytesToSShort($this->data, $o, $this->order);
    }


    /**
     * Return an unsigned long read from the data.
     *
     * @param int the offset into the data.  An offset of zero will
     * return the first long available in the current allowed window.
     * The last valid offset is equal to {@link getSize()}-4.  Invalid
     * offsets will result in a {@link PelDataWindowOffsetException}
     * being thrown.
     *
     * @return  int  the unsigned long found at offset.
     */
    function getLong($o = 0) {
        /* Validate the offset+3 to see if we can safely get four bytes
         * --- this throws an exception if offset is out of range. */
        $this->validateOffset($o);
        $this->validateOffset($o+3);

        /* Translate the offset into an offset into the data. */
        $o += $this->start;

        /* Return an unsigned long. */
        return PelConvert::bytesToLong($this->data, $o, $this->order);
    }


    /**
     * Return a signed long read from the data.
     *
     * @param int the offset into the data.  An offset of zero will
     * return the first long available in the current allowed window.
     * The last valid offset is equal to {@link getSize()}-4.  Invalid
     * offsets will result in a {@link PelDataWindowOffsetException}
     * being thrown.
     *
     * @return  int  the signed long found at offset.
     */
    function getSLong($o = 0) {
        /* Validate the offset+3 to see if we can safely get four bytes
         * --- this throws an exception if offset is out of range. */
        $this->validateOffset($o);
        $this->validateOffset($o+3);

        /* Translate the offset into an offset into the data. */
        $o += $this->start;

        /* Return a signed long. */
        return PelConvert::bytesToSLong($this->data, $o, $this->order);
    }


    /**
     * Return an unsigned rational read from the data.
     *
     * @param int the offset into the data.  An offset of zero will
     * return the first rational available in the current allowed
     * window.  The last valid offset is equal to {@link getSize()}-8.
     * Invalid offsets will result in a {@link
     * PelDataWindowOffsetException} being thrown.
     *
     * @return array the unsigned rational found at offset.  A rational
     * number is represented as an array of two numbers: the enumerator
     * and denominator.  Both of these numbers will be unsigned longs.
     */
    function getRational($o = 0) {
        return array($this->getLong($o), $this->getLong($o+4));
    }


    /**
     * Return a signed rational read from the data.
     *
     * @param int the offset into the data.  An offset of zero will
     * return the first rational available in the current allowed
     * window.  The last valid offset is equal to {@link getSize()}-8.
     * Invalid offsets will result in a {@link
     * PelDataWindowOffsetException} being thrown.
     *
     * @return array the signed rational found at offset.  A rational
     * number is represented as an array of two numbers: the enumerator
     * and denominator.  Both of these numbers will be signed longs.
     */
    function getSRational($o = 0) {
        return array($this->getSLong($o), $this->getSLong($o+4));
    }


    /**
     * String comparison on substrings.
     *
     * @param int the offset into the data.  An offset of zero will make
     * the comparison start with the very first byte available in the
     * window.  The last valid offset is equal to {@link getSize()}
     * minus the length of the string.  If the string is too long, then
     * a {@link PelDataWindowOffsetException} will be thrown.
     *
     * @param string the string to compare with.
     *
     * @return boolean true if the string given matches the data in the
     * window, at the specified offset, false otherwise.  The comparison
     * will stop as soon as a mismatch if found.
     */
    function strcmp($o, $str) {
        /* Validate the offset of the final character we might have to
         * check. */
        $s = strlen($str);
        $this->validateOffset($o);
        $this->validateOffset($o + $s - 1);

        /* Translate the offset into an offset into the data. */
        $o += $this->start;

        /* Check each character, return as soon as the answer is known. */
        for ($i = 0; $i < $s; $i++) {
            if ($this->data{$o + $i} != $str{$i})
            return false;
        }

        /* All characters matches each other, return true. */
        return true;
    }


    /**
     * Return a string representation of the data window.
     *
     * @return string a description of the window with information about
     * the number of bytes accessible, the total number of bytes, and
     * the window start and stop.
     */
    function __toString() {
        return Pel::fmt('DataWindow: %d bytes in [%d, %d] of %d bytes',
        $this->size,
        $this->start, $this->start + $this->size,
        strlen($this->data));
    }

}