void HPDF_EncryptDict_OnFree (HPDF_Dict obj) { HPDF_Encrypt attr = (HPDF_Encrypt)obj->attr; HPDF_PTRACE((" HPDF_EncryptDict_OnFree\n")); if (attr) HPDF_FreeMem (obj->mmgr, attr); }
void HPDF_CIDFontDef_FreeFunc (HPDF_FontDef fontdef) { HPDF_CIDFontDefAttr attr = (HPDF_CIDFontDefAttr)fontdef->attr; HPDF_PTRACE (" HPDF_CIDFontDef_FreeFunc\n"); HPDF_CIDFontDef_FreeWidth (fontdef); HPDF_FreeMem (fontdef->mmgr, attr); }
static void OnFree_Func (HPDF_Dict obj) { HPDF_FontAttr attr = (HPDF_FontAttr)obj->attr; HPDF_PTRACE (" HPDF_Type0Font_OnFree\n"); if (attr) HPDF_FreeMem (obj->mmgr, attr); }
HPDF_Xref HPDF_Xref_New (HPDF_MMgr mmgr, HPDF_UINT32 offset) { HPDF_Xref xref; HPDF_XrefEntry new_entry; HPDF_PTRACE((" HPDF_Xref_New\n")); xref = (HPDF_Xref)HPDF_GetMem (mmgr, sizeof(HPDF_Xref_Rec)); if (!xref) return NULL; HPDF_MemSet (xref, 0, sizeof(HPDF_Xref_Rec)); xref->mmgr = mmgr; xref->error = mmgr->error; xref->start_offset = offset; xref->entries = HPDF_List_New (mmgr, HPDF_DEFALUT_XREF_ENTRY_NUM); if (!xref->entries) goto Fail; xref->addr = 0; if (xref->start_offset == 0) { new_entry = (HPDF_XrefEntry)HPDF_GetMem (mmgr, sizeof(HPDF_XrefEntry_Rec)); if (!new_entry) goto Fail; if (HPDF_List_Add (xref->entries, new_entry) != HPDF_OK) { HPDF_FreeMem (mmgr, new_entry); goto Fail; } /* add first entry which is free entry and whose generation * number is 0 */ new_entry->entry_typ = HPDF_FREE_ENTRY; new_entry->byte_offset = 0; new_entry->gen_no = HPDF_MAX_GENERATION_NUM; new_entry->obj = NULL; } xref->trailer = HPDF_Dict_New (mmgr); if (!xref->trailer) goto Fail; return xref; Fail: HPDF_PTRACE((" HPDF_Xref_New failed\n")); HPDF_Xref_Free (xref); return NULL; }
static HPDF_STATUS ReadPngData_Interlaced (HPDF_Dict image, png_structp png_ptr, png_infop info_ptr) { png_uint_32 len = png_get_rowbytes(png_ptr, info_ptr); png_uint_32 height = png_get_image_height(png_ptr, info_ptr); png_bytep* row_pointers = HPDF_GetMem (image->mmgr, height * sizeof (png_bytep)); if (row_pointers) { HPDF_UINT i; HPDF_MemSet (row_pointers, 0, height * sizeof (png_bytep)); for (i = 0; i < (HPDF_UINT)height; i++) { row_pointers[i] = HPDF_GetMem (image->mmgr, len); if (image->error->error_no != HPDF_OK) break; } if (image->error->error_no == HPDF_OK) { png_read_image(png_ptr, row_pointers); if (image->error->error_no == HPDF_OK) { /* add this line */ for (i = 0; i < (HPDF_UINT)height; i++) { if (HPDF_Stream_Write (image->stream, row_pointers[i], len) != HPDF_OK) break; } } } /* clean up */ for (i = 0; i < (HPDF_UINT)height; i++) { HPDF_FreeMem (image->mmgr, row_pointers[i]); } HPDF_FreeMem (image->mmgr, row_pointers); } return image->error->error_no; }
void HPDF_List_Free (HPDF_List list) { HPDF_PTRACE((" HPDF_List_Free\n")); if (!list) return ; HPDF_List_Clear (list); HPDF_FreeMem (list->mmgr, list); }
void HPDF_List_Clear (HPDF_List list) { HPDF_PTRACE((" HPDF_List_Clear\n")); if (list->obj) HPDF_FreeMem (list->mmgr, list->obj); list->block_siz = 0; list->count = 0; list->obj = NULL; }
static void OnFree (HPDF_Dict obj) { HPDF_FontAttr attr = (HPDF_FontAttr)obj->attr; HPDF_PTRACE ((" HPDF_TTFont_OnFree\n")); HPDF_Font_FreeConvertersListAll ((HPDF_Font)obj); if (attr) { if (attr->widths) { HPDF_FreeMem (obj->mmgr, attr->widths); } if (attr->used) { HPDF_FreeMem (obj->mmgr, attr->used); } HPDF_FreeMem (obj->mmgr, attr); } }
void HPDF_FontDef_Free (HPDF_FontDef fontdef) { if (!fontdef) return; HPDF_PTRACE (" HPDF_FontDef_Free\n"); if (fontdef->free_fn) fontdef->free_fn (fontdef); HPDF_FreeMem (fontdef->mmgr, fontdef); }
static void FreeWidth (HPDF_FontDef fontdef) { HPDF_Type1FontDefAttr attr = (HPDF_Type1FontDefAttr)fontdef->attr; HPDF_PTRACE (" FreeWidth\n"); HPDF_FreeMem (fontdef->mmgr, attr->widths); attr->widths = NULL; fontdef->valid = HPDF_FALSE; }
static HPDF_STATUS CreatePallet (HPDF_Dict image, png_structp png_ptr, png_infop info_ptr) { HPDF_INT num_pl = 0; png_color *src_pl = NULL; HPDF_BYTE *ppallet; HPDF_BYTE *p; HPDF_UINT i; HPDF_Array array; /* png_get_PLTE does not call PngErrorFunc even if it failed. * so we call HPDF_Set_Error to set error-code. */ if (png_get_PLTE(png_ptr, info_ptr, (png_color**)&src_pl, &num_pl) != PNG_INFO_PLTE) return HPDF_SetError (image->error, HPDF_LIBPNG_ERROR, HPDF_CANNOT_GET_PALLET); /* make a pallet array for indexed image. */ ppallet = HPDF_GetMem (image->mmgr, num_pl * 3); if (!ppallet) return image->error->error_no; p = ppallet; for (i = 0; i < num_pl; i++, src_pl++) { *p++ = src_pl->red; *p++ = src_pl->green; *p++ = src_pl->blue; } array = HPDF_Array_New (image->mmgr); if (array) { HPDF_Binary b; HPDF_Dict_Add (image, "ColorSpace", array); HPDF_Array_AddName (array, "Indexed"); HPDF_Array_AddName (array, "DeviceRGB"); HPDF_Array_AddNumber (array, num_pl - 1); b = HPDF_Binary_New (image->mmgr, ppallet, num_pl * 3); if (b) HPDF_Array_Add (array, b); } HPDF_FreeMem (image->mmgr, ppallet); return image->error->error_no; }
static void Page_OnFree (HPDF_Dict obj) { HPDF_PageAttr attr = (HPDF_PageAttr)obj->attr; HPDF_PTRACE((" HPDF_Page_OnFree\n")); if (attr) { if (attr->gstate) HPDF_GState_Free (obj->mmgr, attr->gstate); HPDF_FreeMem (obj->mmgr, attr); } }
HPDF_Free (HPDF_Doc pdf) { HPDF_PTRACE ((" HPDF_Free\n")); if (pdf) { HPDF_MMgr mmgr = pdf->mmgr; HPDF_FreeDocAll (pdf); pdf->sig_bytes = 0; HPDF_FreeMem (mmgr, pdf); HPDF_MMgr_Free (mmgr); } }
void HPDF_Array_Free (HPDF_Array array) { if (!array) return; HPDF_PTRACE((" HPDF_Array_Free\n")); HPDF_Array_Clear (array); HPDF_List_Free (array->list); array->header.obj_class = 0; HPDF_FreeMem (array->mmgr, array); }
void HPDF_CIDFontDef_FreeWidth (HPDF_FontDef fontdef) { HPDF_CIDFontDefAttr attr = (HPDF_CIDFontDefAttr)fontdef->attr; HPDF_UINT i; HPDF_PTRACE (" HPDF_FontDef_Validate\n"); for (i = 0; i < attr->widths->count; i++) { HPDF_CID_Width *w = (HPDF_CID_Width *)HPDF_List_ItemAt (attr->widths, i); HPDF_FreeMem (fontdef->mmgr, w); } HPDF_List_Free (attr->widths); attr->widths = NULL; fontdef->valid = HPDF_FALSE; }
HPDF_Array HPDF_Array_New (HPDF_MMgr mmgr) { HPDF_Array obj; HPDF_PTRACE((" HPDF_Array_New\n")); obj = HPDF_GetMem (mmgr, sizeof(HPDF_Array_Rec)); if (obj) { HPDF_MemSet (obj, 0, sizeof(HPDF_Array_Rec)); obj->header.obj_class = HPDF_OCLASS_ARRAY; obj->mmgr = mmgr; obj->error = mmgr->error; obj->list = HPDF_List_New (mmgr, HPDF_DEF_ITEMS_PER_BLOCK); if (!obj->list) { HPDF_FreeMem (mmgr, obj); obj = NULL; } } return obj; }
HPDF_STATUS HPDF_Binary_SetValue (HPDF_Binary obj, HPDF_BYTE *value, HPDF_UINT len) { if (len > HPDF_LIMIT_MAX_STRING_LEN) return HPDF_SetError (obj->error, HPDF_BINARY_LENGTH_ERR, 0); if (obj->value) { HPDF_FreeMem (obj->mmgr, obj->value); obj->len = 0; } obj->value = HPDF_GetMem (obj->mmgr, len); if (!obj->value) return HPDF_Error_GetCode (obj->error); HPDF_MemCpy (obj->value, value, len); obj->len = len; return HPDF_OK; }
void HPDF_Obj_ForceFree (HPDF_MMgr mmgr, void *obj) { HPDF_Obj_Header *header; HPDF_PTRACE((" HPDF_Obj_ForceFree\n")); if (!obj) return; header = (HPDF_Obj_Header *)obj; HPDF_PTRACE((" HPDF_Obj_ForceFree obj=0x%08X obj_id=0x%08X " "obj_class=0x%08X\n", (HPDF_UINT)obj, (HPDF_UINT)(header->obj_id), (HPDF_UINT)(header->obj_class))); switch (header->obj_class & HPDF_OCLASS_ANY) { case HPDF_OCLASS_STRING: HPDF_String_Free (obj); break; case HPDF_OCLASS_BINARY: HPDF_Binary_Free (obj); break; case HPDF_OCLASS_ARRAY: HPDF_Array_Free (obj); break; case HPDF_OCLASS_DICT: HPDF_Dict_Free (obj); break; case HPDF_OCLASS_DIRECT: HPDF_Direct_Free (obj); break; default: HPDF_FreeMem (mmgr, obj); } }
static HPDF_STATUS ReadTransparentPngData (HPDF_Dict image, png_structp png_ptr, png_infop info_ptr, png_bytep smask_data) { HPDF_STATUS ret = HPDF_OK; HPDF_INT row_len; HPDF_UINT i, j; png_bytep *row_ptr, row = 0; png_byte color_type; png_uint_32 height = png_get_image_height(png_ptr, info_ptr); png_uint_32 width = png_get_image_width(png_ptr, info_ptr); color_type = png_get_color_type(png_ptr, info_ptr); if (!(color_type & PNG_COLOR_MASK_ALPHA)) { return HPDF_INVALID_PNG_IMAGE; } row_ptr = HPDF_GetMem (image->mmgr, height * sizeof(png_bytep)); if (!row_ptr) { return HPDF_FAILD_TO_ALLOC_MEM; } else { png_uint_32 len = png_get_rowbytes(png_ptr, info_ptr); for (i = 0; i < (HPDF_UINT)height; i++) { row_ptr[i] = HPDF_GetMem(image->mmgr, len); if (!row_ptr[i]) { for (; i > 0; i--) { HPDF_FreeMem (image->mmgr, row_ptr[i]); } HPDF_FreeMem (image->mmgr, row_ptr); return HPDF_FAILD_TO_ALLOC_MEM; } } } png_read_image(png_ptr, row_ptr); if (image->error->error_no != HPDF_OK) { ret = HPDF_INVALID_PNG_IMAGE; goto Error; } switch (color_type) { case PNG_COLOR_TYPE_RGB_ALPHA: row_len = 3 * width * sizeof(png_byte); for (j = 0; j < height; j++) { for (i = 0; i < width; i++) { row = row_ptr[j]; memmove(row + (3 * i), row + (4*i), 3); smask_data[width * j + i] = row[4 * i + 3]; } if (HPDF_Stream_Write (image->stream, row, row_len) != HPDF_OK) { ret = HPDF_FILE_IO_ERROR; goto Error; } } break; case PNG_COLOR_TYPE_GRAY_ALPHA: row_len = width * sizeof(png_byte); for (j = 0; j < height; j++) { for (i = 0; i < width; i++) { row = row_ptr[j]; row[i] = row[2 * i]; smask_data[width * j + i] = row[2 * i + 1]; } if (HPDF_Stream_Write (image->stream, row, row_len) != HPDF_OK) { ret = HPDF_FILE_IO_ERROR; goto Error; } } break; default: ret = HPDF_INVALID_PNG_IMAGE; goto Error; } Error: for (i = 0; i < (HPDF_UINT)height; i++) { HPDF_FreeMem (image->mmgr, row_ptr[i]); } HPDF_FreeMem (image->mmgr, row_ptr); return ret; }
static HPDF_STATUS LoadPngData (HPDF_Dict image, HPDF_Xref xref, HPDF_Stream png_data, HPDF_BOOL delayed_loading) { HPDF_STATUS ret = HPDF_OK; png_uint_32 width, height; int bit_depth, color_type; png_structp png_ptr = NULL; png_infop info_ptr = NULL; HPDF_PTRACE ((" HPDF_Image_LoadPngImage\n")); /* create read_struct. */ png_ptr = png_create_read_struct (PNG_LIBPNG_VER_STRING, image->error, PngErrorFunc, PngErrorFunc); if (png_ptr == NULL) { HPDF_SetError (image->error, HPDF_FAILD_TO_ALLOC_MEM, 0); return HPDF_FAILD_TO_ALLOC_MEM; } /* create info-struct */ info_ptr = png_create_info_struct (png_ptr); if (info_ptr == NULL) { HPDF_SetError (image->error, HPDF_FAILD_TO_ALLOC_MEM, 0); goto Exit; } png_set_sig_bytes (png_ptr, HPDF_PNG_BYTES_TO_CHECK); png_set_read_fn (png_ptr, (void *)png_data, (png_rw_ptr)&PngReadFunc); /* reading info structure. */ png_read_info(png_ptr, info_ptr); if (image->error->error_no != HPDF_OK) { goto Exit; } png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL); /* 16bit images are not supported. */ if (bit_depth == 16) { png_set_strip_16(png_ptr); } png_read_update_info(png_ptr, info_ptr); if (image->error->error_no != HPDF_OK) { goto Exit; } /* check palette-based images for transparent areas and load them immediately if found */ if (xref && PNG_COLOR_TYPE_PALETTE & color_type) { png_bytep trans; int num_trans; HPDF_Dict smask; png_bytep smask_data; if (!png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) || !png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, NULL)) { goto no_transparent_color_in_palette; } smask = HPDF_DictStream_New (image->mmgr, xref); if (!smask) { ret = HPDF_FAILD_TO_ALLOC_MEM; goto Exit; } smask->header.obj_class |= HPDF_OSUBCLASS_XOBJECT; ret = HPDF_Dict_AddName (smask, "Type", "XObject"); ret += HPDF_Dict_AddName (smask, "Subtype", "Image"); ret += HPDF_Dict_AddNumber (smask, "Width", (HPDF_UINT)width); ret += HPDF_Dict_AddNumber (smask, "Height", (HPDF_UINT)height); ret += HPDF_Dict_AddName (smask, "ColorSpace", "DeviceGray"); ret += HPDF_Dict_AddNumber (smask, "BitsPerComponent", (HPDF_UINT)bit_depth); if (ret != HPDF_OK) { HPDF_Dict_Free(smask); ret = HPDF_INVALID_PNG_IMAGE; goto Exit; } smask_data = HPDF_GetMem(image->mmgr, width * height); if (!smask_data) { HPDF_Dict_Free(smask); ret = HPDF_FAILD_TO_ALLOC_MEM; goto Exit; } if (ReadTransparentPaletteData(image, png_ptr, info_ptr, smask_data, trans, num_trans) != HPDF_OK) { HPDF_FreeMem(image->mmgr, smask_data); HPDF_Dict_Free(smask); ret = HPDF_INVALID_PNG_IMAGE; goto Exit; } if (HPDF_Stream_Write(smask->stream, smask_data, width * height) != HPDF_OK) { HPDF_FreeMem(image->mmgr, smask_data); HPDF_Dict_Free(smask); ret = HPDF_FILE_IO_ERROR; goto Exit; } HPDF_FreeMem(image->mmgr, smask_data); ret += CreatePallet(image, png_ptr, info_ptr); ret += HPDF_Dict_AddNumber (image, "Width", (HPDF_UINT)width); ret += HPDF_Dict_AddNumber (image, "Height", (HPDF_UINT)height); ret += HPDF_Dict_AddNumber (image, "BitsPerComponent", (HPDF_UINT)bit_depth); ret += HPDF_Dict_Add (image, "SMask", smask); png_destroy_read_struct(&png_ptr, &info_ptr, NULL); return HPDF_OK; } no_transparent_color_in_palette: /* read images with alpha channel right away we have to do this because image transparent mask must be added to the Xref */ if (xref && PNG_COLOR_MASK_ALPHA & color_type) { HPDF_Dict smask; png_bytep smask_data; smask = HPDF_DictStream_New (image->mmgr, xref); if (!smask) { ret = HPDF_FAILD_TO_ALLOC_MEM; goto Exit; } smask->header.obj_class |= HPDF_OSUBCLASS_XOBJECT; ret = HPDF_Dict_AddName (smask, "Type", "XObject"); ret += HPDF_Dict_AddName (smask, "Subtype", "Image"); ret += HPDF_Dict_AddNumber (smask, "Width", (HPDF_UINT)width); ret += HPDF_Dict_AddNumber (smask, "Height", (HPDF_UINT)height); ret += HPDF_Dict_AddName (smask, "ColorSpace", "DeviceGray"); ret += HPDF_Dict_AddNumber (smask, "BitsPerComponent", (HPDF_UINT)bit_depth); if (ret != HPDF_OK) { HPDF_Dict_Free(smask); ret = HPDF_INVALID_PNG_IMAGE; goto Exit; } smask_data = HPDF_GetMem(image->mmgr, width * height); if (!smask_data) { HPDF_Dict_Free(smask); ret = HPDF_FAILD_TO_ALLOC_MEM; goto Exit; } if (ReadTransparentPngData(image, png_ptr, info_ptr, smask_data) != HPDF_OK) { HPDF_FreeMem(image->mmgr, smask_data); HPDF_Dict_Free(smask); ret = HPDF_INVALID_PNG_IMAGE; goto Exit; } if (HPDF_Stream_Write(smask->stream, smask_data, width * height) != HPDF_OK) { HPDF_FreeMem(image->mmgr, smask_data); HPDF_Dict_Free(smask); ret = HPDF_FILE_IO_ERROR; goto Exit; } HPDF_FreeMem(image->mmgr, smask_data); if (color_type == PNG_COLOR_TYPE_GRAY_ALPHA) { ret += HPDF_Dict_AddName (image, "ColorSpace", "DeviceGray"); } else { ret += HPDF_Dict_AddName (image, "ColorSpace", "DeviceRGB"); } ret += HPDF_Dict_AddNumber (image, "Width", (HPDF_UINT)width); ret += HPDF_Dict_AddNumber (image, "Height", (HPDF_UINT)height); ret += HPDF_Dict_AddNumber (image, "BitsPerComponent", (HPDF_UINT)bit_depth); ret += HPDF_Dict_Add (image, "SMask", smask); png_destroy_read_struct(&png_ptr, &info_ptr, NULL); return HPDF_OK; } /* if the image has color palette, copy the pallet of the image to * create color map. */ if (color_type == PNG_COLOR_TYPE_PALETTE) ret = CreatePallet(image, png_ptr, info_ptr); else if (color_type == PNG_COLOR_TYPE_GRAY) ret = HPDF_Dict_AddName (image, "ColorSpace", "DeviceGray"); else ret = HPDF_Dict_AddName (image, "ColorSpace", "DeviceRGB"); if (ret != HPDF_OK) goto Exit; /* read image-data * if the image is interlaced, read whole image at once. * if delayed_loading is HPDF_TRUE, the data does not load this phase. */ if (delayed_loading) { image->before_write_fn = PngBeforeWrite; image->after_write_fn = PngAfterWrite; } else { if (png_get_interlace_type(png_ptr, info_ptr) != PNG_INTERLACE_NONE) ret = ReadPngData_Interlaced(image, png_ptr, info_ptr); else ret = ReadPngData(image, png_ptr, info_ptr); if (ret != HPDF_OK) goto Exit; } /* setting the info of the image. */ if (HPDF_Dict_AddNumber (image, "Width", (HPDF_UINT)width) != HPDF_OK) goto Exit; if (HPDF_Dict_AddNumber (image, "Height", (HPDF_UINT)height) != HPDF_OK) goto Exit; if (HPDF_Dict_AddNumber (image, "BitsPerComponent", (HPDF_UINT)bit_depth) != HPDF_OK) goto Exit; /* clean up */ png_destroy_read_struct(&png_ptr, &info_ptr, NULL); return HPDF_OK; Exit: png_destroy_read_struct(&png_ptr, &info_ptr, NULL); if (ret != HPDF_OK) { return ret; } return image->error->error_no; }