summaryrefslogtreecommitdiffstats
path: root/ext/openssl
diff options
context:
space:
mode:
authornobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-04-04 01:17:34 +0000
committernobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2007-04-04 01:17:34 +0000
commit604645535d3b770c88931c813b0239b39dddc5a2 (patch)
treed7c5bb12fb376dfe6b1d5ae65f840297d9b51a95 /ext/openssl
parent1cedc5d47727e4e72ace38dbf843b4676d8d94a8 (diff)
downloadruby-604645535d3b770c88931c813b0239b39dddc5a2.tar.gz
ruby-604645535d3b770c88931c813b0239b39dddc5a2.tar.xz
ruby-604645535d3b770c88931c813b0239b39dddc5a2.zip
* io.c (pipe_open): raise NotImplementedError for command "-" on
platforms where fork(2) is not available. [ruby-dev:30681] git-svn-id: http://svn.ruby-lang.org/repos/ruby/trunk@12144 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'ext/openssl')
0 files changed, 0 insertions, 0 deletions
='n119' href='#n119'>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 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
/*

 Copyright 2009 Red Hat, Inc. and/or its affiliates.

 This program is licensed to you under the GNU General Public License,
 version 2 or (at your option) any later version published by the Free
 Software Foundation.  See the file COPYING for details.

 There is NO WARRANTY for this software, not even the implied
 warranties of MERCHANTABILITY, NONINFRINGEMENT, or FITNESS FOR A
 PARTICULAR PURPOSE.

 This file incorporates work covered by the following copyright and
 permission notice:
   Copyright (C) 2007 Ariya Hidayat (ariya@kde.org)
   Copyright (C) 2006 Ariya Hidayat (ariya@kde.org)
   Copyright (C) 2005 Ariya Hidayat (ariya@kde.org)

   Permission is hereby granted, free of charge, to any person
   obtaining a copy of this software and associated documentation
   files (the "Software"), to deal in the Software without
   restriction, including without limitation the rights to use, copy,
   modify, merge, publish, distribute, sublicense, and/or sell copies
   of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be
   included in all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
   NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
   BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
   ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
   SOFTWARE.

*/

#include "lz.h"

#define DEBUG

#ifdef DEBUG

#define ASSERT(usr, x) \
    if (!(x)) (usr)->error(usr, "%s: ASSERT %s failed\n", __FUNCTION__, #x);

#else

#define ASSERT(usr, x)

#endif

#define HASH_LOG 13
#define HASH_SIZE (1 << HASH_LOG)
#define HASH_MASK (HASH_SIZE - 1)


typedef struct LzImageSegment LzImageSegment;
struct LzImageSegment {
    uint8_t            *lines;
    uint8_t            *lines_end;
    unsigned int size_delta;    // total size of the previous segments in units of
                                // pixels for rgb and bytes for plt.
    LzImageSegment    *next;
};

//    TODO: pack?
typedef struct HashEntry {
    LzImageSegment    *image_seg;
    uint8_t            *ref;
} HashEntry;

typedef struct Encoder {
    LzUsrContext    *usr;

    LzImageType type;
    const Palette    *palette;    // for decoding images with palettes to rgb
    int stride;                       // stride is in bytes. For rgb must be equal to
                                      // width*bytes_per_pix.
    // For palettes stride can be bigger than width/pixels_per_byte by 1 only if
    // width%pixels_per_byte != 0.
    int height;
    int width;                       // the original width (in pixels)

    LzImageSegment *head_image_segs;
    LzImageSegment *tail_image_segs;
    LzImageSegment *free_image_segs;

    // the dicitionary hash table is composed (1) a pointer to the segment the word was found in
    // (2) a pointer to the first byte in the segment that matches the word
    HashEntry htab[HASH_SIZE];

    uint8_t            *io_start;
    uint8_t            *io_now;
    uint8_t            *io_end;
    size_t io_bytes_count;

    uint8_t            *io_last_copy;  // pointer to the last byte in which copy count was written
} Encoder;

/****************************************************/
/* functions for managing the pool of image segments*/
/****************************************************/
static INLINE LzImageSegment *lz_alloc_image_seg(Encoder *encoder);
static void lz_reset_image_seg(Encoder *encoder);
static int lz_read_image_segments(Encoder *encoder, uint8_t *first_lines,
                                  unsigned int num_first_lines);


// return a free image segement if one exists. Make allocation if needed. adds it to the
// tail of the image segments lists
static INLINE LzImageSegment *lz_alloc_image_seg(Encoder *encoder)
{
    LzImageSegment *ret;

    if (encoder->free_image_segs) {
        ret = encoder->free_image_segs;
        encoder->free_image_segs = ret->next;
    } else {
        if (!(ret = (LzImageSegment *)encoder->usr->malloc(encoder->usr, sizeof(*ret)))) {
            return NULL;
        }
    }

    ret->next = NULL;
    if (encoder->tail_image_segs) {
        encoder->tail_image_segs->next = ret;
    }
    encoder->tail_image_segs = ret;

    if (!encoder->head_image_segs) {
        encoder->head_image_segs = ret;
    }

    return ret;
}

// adding seg to the head of free segments (lz_reset_image_seg removes it from used ones)
static INLINE void __lz_free_image_seg(Encoder *encoder, LzImageSegment *seg)
{
    seg->next = encoder->free_image_segs;
    encoder->free_image_segs = seg;
}

// moves all the used image segments to the free pool
static void lz_reset_image_seg(Encoder *encoder)
{
    while (encoder->head_image_segs) {
        LzImageSegment *seg = encoder->head_image_segs;
        encoder->head_image_segs = seg->next;
        __lz_free_image_seg(encoder, seg);
    }
    encoder->tail_image_segs = NULL;
}

static void lz_dealloc_free_segments(Encoder *encoder)
{
    while (encoder->free_image_segs) {
        LzImageSegment *seg = encoder->free_image_segs;
        encoder->free_image_segs = seg->next;
        encoder->usr->free(encoder->usr, seg);
    }
}

// return FALSE when operation fails (due to failure in allocation)
static int lz_read_image_segments(Encoder *encoder, uint8_t *first_lines,
                                  unsigned int num_first_lines)
{
    LzImageSegment *image_seg;
    uint32_t size_delta = 0;
    unsigned int num_lines = num_first_lines;
    uint8_t* lines = first_lines;
    int row;

    ASSERT(encoder->usr, !encoder->head_image_segs);

    image_seg = lz_alloc_image_seg(encoder);
    if (!image_seg) {
        goto error_1;
    }

    image_seg->lines = lines;
    image_seg->lines_end = lines + num_lines * encoder->stride;
    image_seg->size_delta = size_delta;

    size_delta += num_lines * encoder->stride / RGB_BYTES_PER_PIXEL[encoder->type];

    for (row = num_first_lines; row < encoder->height; row += num_lines) {
        num_lines = encoder->usr->more_lines(encoder->usr, &lines);
        if (num_lines <= 0) {
            encoder->usr->error(encoder->usr, "more lines failed\n");
        }
        image_seg = lz_alloc_image_seg(encoder);

        if (!image_seg) {
            goto error_1;
        }

        image_seg->lines = lines;
        image_seg->lines_end = lines + num_lines * encoder->stride;
        image_seg->size_delta = size_delta;

        size_delta += num_lines * encoder->stride / RGB_BYTES_PER_PIXEL[encoder->type];
    }

    return TRUE;
error_1:
    lz_reset_image_seg(encoder);
    return FALSE;
}

/**************************************************************************
* Handling encoding and decoding of a byte
***************************************************************************/
static INLINE int more_io_bytes(Encoder *encoder)
{
    uint8_t *io_ptr;
    int num_io_bytes = encoder->usr->more_space(encoder->usr, &io_ptr);
    encoder->io_bytes_count += num_io_bytes;
    encoder->io_now = io_ptr;
    encoder->io_end = encoder->io_now + num_io_bytes;
    return num_io_bytes;
}

static INLINE void encode(Encoder *encoder, uint8_t byte)
{
    if (encoder->io_now == encoder->io_end) {
        if (more_io_bytes(encoder) <= 0) {
            encoder->usr->error(encoder->usr, "%s: no more bytes\n", __FUNCTION__);
        }
        ASSERT(encoder->usr, encoder->io_now);
    }

    ASSERT(encoder->usr, encoder->io_now < encoder->io_end);
    *(encoder->io_now++) = byte;
}

static INLINE void encode_32(Encoder *encoder, unsigned int word)
{
    encode(encoder, (uint8_t)(word >> 24));
    encode(encoder, (uint8_t)(word >> 16) & 0x0000ff);
    encode(encoder, (uint8_t)(word >> 8) & 0x0000ff);
    encode(encoder, (uint8_t)(word & 0x0000ff));
}

static INLINE void encode_copy_count(Encoder *encoder, uint8_t copy_count)
{
    encode(encoder, copy_count);
    encoder->io_last_copy = encoder->io_now - 1; // io_now cannot be the first byte of the buffer
}

static INLINE void update_copy_count(Encoder *encoder, uint8_t copy_count)
{
    ASSERT(encoder->usr, encoder->io_last_copy);
    *(encoder->io_last_copy) = copy_count;
}

static INLINE void encode_level(Encoder *encoder, uint8_t level_code)
{
    *(encoder->io_start) |= level_code;
}

// decrease the io ptr by 1
static INLINE void compress_output_prev(Encoder *encoder)
{
    // io_now cannot be the first byte of the buffer
    encoder->io_now--;
    // the function should be called only when copy count is written unnecessarily by lz_compress
    ASSERT(encoder->usr, encoder->io_now == encoder->io_last_copy)
}

static int encoder_reset(Encoder *encoder, uint8_t *io_ptr, uint8_t *io_ptr_end)
{
    ASSERT(encoder->usr, io_ptr <= io_ptr_end);
    encoder->io_bytes_count = io_ptr_end - io_ptr;
    encoder->io_start = io_ptr;
    encoder->io_now = io_ptr;
    encoder->io_end = io_ptr_end;
    encoder->io_last_copy = NULL;

    return TRUE;
}

static INLINE uint8_t decode(Encoder *encoder)
{
    if (encoder->io_now == encoder->io_end) {
        int num_io_bytes = more_io_bytes(encoder);
        if (num_io_bytes <= 0) {
            encoder->usr->error(encoder->usr, "%s: no more bytes\n", __FUNCTION__);
        }
        ASSERT(encoder->usr, encoder->io_now);
    }
    ASSERT(encoder->usr, encoder->io_now < encoder->io_end);
    return *(encoder->io_now++);
}

static INLINE uint32_t decode_32(Encoder *encoder)
{
    uint32_t word = 0;
    word |= decode(encoder);
    word <<= 8;
    word |= decode(encoder);
    word <<= 8;
    word |= decode(encoder);
    word <<= 8;
    word |= decode(encoder);
    return word;
}

static INLINE int is_io_to_decode_end(Encoder *encoder)
{
    if (encoder->io_now != encoder->io_end) {
        return FALSE;
    } else {
        int num_io_bytes = more_io_bytes(encoder); //disable inline optimizations
        return (num_io_bytes <= 0);
    }
}

/*******************************************************************
* intialization and finalization of lz
********************************************************************/
static int init_encoder(Encoder *encoder, LzUsrContext *usr)
{
    encoder->usr = usr;
    encoder->free_image_segs = NULL;
    encoder->head_image_segs = NULL;
    encoder->tail_image_segs = NULL;
    return TRUE;
}

LzContext *lz_create(LzUsrContext *usr)
{
    Encoder *encoder;

    if (!usr || !usr->error || !usr->warn || !usr->info || !usr->malloc ||
        !usr->free || !usr->more_space || !usr->more_lines) {
        return NULL;
    }

    if (!(encoder = (Encoder *)usr->malloc(usr, sizeof(Encoder)))) {
        return NULL;
    }

    if (!init_encoder(encoder, usr)) {
        usr->free(usr, encoder);
        return NULL;
    }
    return (LzContext *)encoder;
}

void lz_destroy(LzContext *lz)
{
    Encoder *encoder = (Encoder *)lz;

    if (!lz) {
        return;
    }

    if (encoder->head_image_segs) {
        encoder->usr->error(encoder->usr, "%s: used_image_segements not empty\n", __FUNCTION__);
        lz_reset_image_seg(encoder);
    }
    lz_dealloc_free_segments(encoder);

    encoder->usr->free(encoder->usr, encoder);
}

/*******************************************************************
*                encoding and decoding the image
********************************************************************/
/*
 * Give hints to the compiler for branch prediction optimization.
 */
#if defined(__GNUC__) && (__GNUC__ > 2)
#define LZ_EXPECT_CONDITIONAL(c) (__builtin_expect((c), 1))
#define LZ_UNEXPECT_CONDITIONAL(c) (__builtin_expect((c), 0))
#else
#define LZ_EXPECT_CONDITIONAL(c) (c)
#define LZ_UNEXPECT_CONDITIONAL(c) (c)
#endif


#ifdef __GNUC__
#define ATTR_PACKED __attribute__ ((__packed__))
#else
#define ATTR_PACKED
#pragma pack(push)
#pragma pack(1)
#endif


/* the palette images will be treated as one byte pixels. Their width should be transformed
   accordingly.
*/
typedef struct ATTR_PACKED one_byte_pixel_t {
    uint8_t a;
} one_byte_pixel_t;

typedef struct ATTR_PACKED rgb32_pixel_t {
    uint8_t b;
    uint8_t g;
    uint8_t r;
    uint8_t pad;
} rgb32_pixel_t;

typedef struct ATTR_PACKED rgb24_pixel_t {
    uint8_t b;
    uint8_t g;
    uint8_t r;
} rgb24_pixel_t;

typedef uint16_t rgb16_pixel_t;

#ifndef __GNUC__
#pragma pack(pop)
#endif

#undef ATTR_PACKED


#define MAX_COPY 32
#define MAX_LEN 264          /* 256 + 8 */
#define BOUND_OFFSET 2
#define LIMIT_OFFSET 6
#define MIN_FILE_SIZE 4
#define COMP_LEVEL_SIZE_LIMIT 65536

// TODO: implemented lz2. should lz1 be an option (no RLE + distance limitation of MAX_DISTANCE)
// TODO: I think MAX_FARDISTANCE can be changed easily to 2^29 
//       (and maybe even more when pixel > byte).
// i.e. we can support 512M Bytes/Pixels distance instead of only ~68K.
#define MAX_DISTANCE 8191                        // 2^13
#define MAX_FARDISTANCE (65535 + MAX_DISTANCE - 1)    // ~2^16+2^13


#define LZ_PLT
#include "lz_compress_tmpl.c"
#define LZ_PLT
#include "lz_decompress_tmpl.c"

#define LZ_PLT
#define PLT8
#define TO_RGB32
#include "lz_decompress_tmpl.c"

#define LZ_PLT
#define PLT4_BE
#define TO_RGB32
#include "lz_decompress_tmpl.c"

#define LZ_PLT
#define PLT4_LE
#define TO_RGB32
#include "lz_decompress_tmpl.c"

#define LZ_PLT
#define PLT1_BE
#define TO_RGB32
#include "lz_decompress_tmpl.c"

#define LZ_PLT
#define PLT1_LE
#define TO_RGB32
#include "lz_decompress_tmpl.c"


#define LZ_RGB16
#include "lz_compress_tmpl.c"
#define LZ_RGB16
#include "lz_decompress_tmpl.c"
#define LZ_RGB16
#define TO_RGB32
#include "lz_decompress_tmpl.c"

#define LZ_RGB24
#include "lz_compress_tmpl.c"
#define LZ_RGB24
#include "lz_decompress_tmpl.c"


#define LZ_RGB32
#include "lz_compress_tmpl.c"
#define LZ_RGB32
#include "lz_decompress_tmpl.c"

#define LZ_RGB_ALPHA
#include "lz_compress_tmpl.c"
#define LZ_RGB_ALPHA
#include "lz_decompress_tmpl.c"

#undef LZ_UNEXPECT_CONDITIONAL
#undef LZ_EXPECT_CONDITIONAL

int lz_encode(LzContext *lz, LzImageType type, int width, int height, int top_down,
              uint8_t *lines, unsigned int num_lines, int stride,
              uint8_t *io_ptr, unsigned int num_io_bytes)
{
    Encoder *encoder = (Encoder *)lz;
    uint8_t *io_ptr_end = io_ptr + num_io_bytes;

    encoder->type = type;
    encoder->width = width;
    encoder->height = height;
    encoder->stride = stride;

    if (IS_IMAGE_TYPE_PLT[encoder->type]) {
        if (encoder->stride > (width / PLT_PIXELS_PER_BYTE[encoder->type])) {
            if (((width % PLT_PIXELS_PER_BYTE[encoder->type]) == 0) || (
                    (encoder->stride - (width / PLT_PIXELS_PER_BYTE[encoder->type])) > 1)) {
                encoder->usr->error(encoder->usr, "sride overflows (plt)\n");
            }
        }
    } else {
        if (encoder->stride != width * RGB_BYTES_PER_PIXEL[encoder->type]) {
            encoder->usr->error(encoder->usr, "sride != width*bytes_per_pixel (rgb)\n");
        }
    }

    // assign the output buffer
    if (!encoder_reset(encoder, io_ptr, io_ptr_end)) {
        encoder->usr->error(encoder->usr, "lz encoder io reset failed\n");
    }

    // first read the list of the image segments
    if (!lz_read_image_segments(encoder, lines, num_lines)) {
        encoder->usr->error(encoder->usr, "lz encoder reading image segments failed\n");
    }

    encode_32(encoder, LZ_MAGIC);
    encode_32(encoder, LZ_VERSION);
    encode_32(encoder, type);
    encode_32(encoder, width);
    encode_32(encoder, height);
    encode_32(encoder, stride);
    encode_32(encoder, top_down); // TODO: maybe compress type and top_down to one byte

    switch (encoder->type) {
    case LZ_IMAGE_TYPE_PLT1_BE:
    case LZ_IMAGE_TYPE_PLT1_LE:
    case LZ_IMAGE_TYPE_PLT4_BE:
    case LZ_IMAGE_TYPE_PLT4_LE:
    case LZ_IMAGE_TYPE_PLT8:
        lz_plt_compress(encoder);
        break;
    case LZ_IMAGE_TYPE_RGB16:
        lz_rgb16_compress(encoder);
        break;
    case LZ_IMAGE_TYPE_RGB24:
        lz_rgb24_compress(encoder);
        break;
    case LZ_IMAGE_TYPE_RGB32:
        lz_rgb32_compress(encoder);
        break;
    case LZ_IMAGE_TYPE_RGBA:
        lz_rgb32_compress(encoder);
        lz_rgb_alpha_compress(encoder);
        break;
    case LZ_IMAGE_TYPE_INVALID:
    default: