png.c (3057B)
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 20#include "config.h" 21#include "log.h" 22#include "png.h" 23 24#include <cairo/cairo.h> 25 26#include <stdlib.h> 27#include <string.h> 28 29/** 30 * The current state of the PNG decoder. 31 */ 32typedef struct guacenc_png_read_state { 33 34 /** 35 * The buffer of unread image data. This pointer will be updated to point 36 * to the next unread byte when data is read. 37 */ 38 unsigned char* data; 39 40 /** 41 * The number of bytes remaining to be read within the buffer. 42 */ 43 unsigned int length; 44 45} guacenc_png_read_state; 46 47/** 48 * Attempts to fill the given buffer with read image data. The behavior of 49 * this function is dictated by cairo_read_t. 50 * 51 * @param closure 52 * The current state of the PNG decoding process (an instance of 53 * guacenc_png_read_state). 54 * 55 * @param data 56 * The data buffer to fill. 57 * 58 * @param length 59 * The number of bytes to fill within the data buffer. 60 * 61 * @return 62 * CAIRO_STATUS_SUCCESS if all data was read successfully (the entire 63 * buffer was filled), CAIRO_STATUS_READ_ERROR otherwise. 64 */ 65static cairo_status_t guacenc_png_read(void* closure, unsigned char* data, 66 unsigned int length) { 67 68 guacenc_png_read_state* state = (guacenc_png_read_state*) closure; 69 70 /* If more data is requested than is available in buffer, fail */ 71 if (length > state->length) 72 return CAIRO_STATUS_READ_ERROR; 73 74 /* Read chunk into buffer */ 75 memcpy(data, state->data, length); 76 77 /* Advance to next chunk */ 78 state->length -= length; 79 state->data += length; 80 81 /* Read was successful */ 82 return CAIRO_STATUS_SUCCESS; 83 84} 85 86cairo_surface_t* guacenc_png_decoder(unsigned char* data, int length) { 87 88 guacenc_png_read_state state = { 89 .data = data, 90 .length = length 91 }; 92 93 /* Read PNG from data */ 94 cairo_surface_t* surface = 95 cairo_image_surface_create_from_png_stream(guacenc_png_read, &state); 96 97 /* If surface returned with an error, just return NULL */ 98 if (surface != NULL && 99 cairo_surface_status(surface) != CAIRO_STATUS_SUCCESS) { 100 guacenc_log(GUAC_LOG_WARNING, "Invalid PNG data"); 101 cairo_surface_destroy(surface); 102 return NULL; 103 } 104 105 /* PNG was read successfully */ 106 return surface; 107 108} 109