/* Generate an ID for the trailer dict, PDF/A needs this. TODO: Better algorithm for generate unique ID. */ HPDF_STATUS HPDF_PDFA_GenerateID(HPDF_Doc pdf) { HPDF_Array id; HPDF_BYTE *currentTime; HPDF_BYTE idkey[HPDF_MD5_KEY_LEN]; HPDF_MD5_CTX md5_ctx; time_t ltime; ltime = time(NULL); currentTime = (HPDF_BYTE *)ctime(<ime); id = HPDF_Dict_GetItem(pdf->trailer, "ID", HPDF_OCLASS_ARRAY); if (!id) { id = HPDF_Array_New(pdf->mmgr); if (!id || HPDF_Dict_Add(pdf->trailer, "ID", id) != HPDF_OK) return pdf->error.error_no; HPDF_MD5Init(&md5_ctx); HPDF_MD5Update(&md5_ctx, (HPDF_BYTE *) "libHaru", sizeof("libHaru") - 1); HPDF_MD5Update(&md5_ctx, currentTime, HPDF_StrLen((const char *)currentTime, -1)); HPDF_MD5Final(idkey, &md5_ctx); if (HPDF_Array_Add (id, HPDF_Binary_New (pdf->mmgr, idkey, HPDF_MD5_KEY_LEN)) != HPDF_OK) return pdf->error.error_no; if (HPDF_Array_Add (id, HPDF_Binary_New (pdf->mmgr,idkey,HPDF_MD5_KEY_LEN)) != HPDF_OK) return pdf->error.error_no; return HPDF_OK; } return HPDF_OK; }
HPDF_STATUS HPDF_EncryptDict_Prepare (HPDF_EncryptDict dict, HPDF_Dict info, HPDF_Xref xref) { HPDF_STATUS ret; HPDF_Encrypt attr = (HPDF_Encrypt)dict->attr; HPDF_Binary user_key; HPDF_Binary owner_key; HPDF_PTRACE((" HPDF_EncryptDict_Prepare\n")); HPDF_EncryptDict_CreateID (dict, info, xref); HPDF_Encrypt_CreateOwnerKey (attr); HPDF_Encrypt_CreateEncryptionKey (attr); HPDF_Encrypt_CreateUserKey (attr); owner_key = HPDF_Binary_New (dict->mmgr, attr->owner_key, HPDF_PASSWD_LEN); if (!owner_key) return HPDF_Error_GetCode (dict->error); if ((ret = HPDF_Dict_Add (dict, "O", owner_key)) != HPDF_OK) return ret; user_key = HPDF_Binary_New (dict->mmgr, attr->user_key, HPDF_PASSWD_LEN); if (!user_key) return HPDF_Error_GetCode (dict->error); if ((ret = HPDF_Dict_Add (dict, "U", user_key)) != HPDF_OK) return ret; ret += HPDF_Dict_AddName (dict, "Filter", "Standard"); if (attr->mode == HPDF_ENCRYPT_R2) { ret += HPDF_Dict_AddNumber (dict, "V", 1); ret += HPDF_Dict_AddNumber (dict, "R", 2); } else if (attr->mode == HPDF_ENCRYPT_R3) { ret += HPDF_Dict_AddNumber (dict, "V", 2); ret += HPDF_Dict_AddNumber (dict, "R", 3); ret += HPDF_Dict_AddNumber (dict, "Length", attr->key_len * 8); } ret += HPDF_Dict_AddNumber (dict, "P", attr->permission); if (ret != HPDF_OK) return HPDF_Error_GetCode (dict->error); return HPDF_OK; }
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; }
HPDF_STATUS HPDF_Doc_PrepareEncryption (HPDF_Doc pdf) { HPDF_Encrypt e= HPDF_EncryptDict_GetAttr (pdf->encrypt_dict); HPDF_Dict info = GetInfo (pdf); HPDF_Array id; if (!e) return HPDF_DOC_ENCRYPTDICT_NOT_FOUND; if (!info) return pdf->error.error_no; if (HPDF_EncryptDict_Prepare (pdf->encrypt_dict, info, pdf->xref) != HPDF_OK) return pdf->error.error_no; /* reset 'ID' to trailer-dictionary */ id = HPDF_Dict_GetItem (pdf->trailer, "ID", HPDF_OCLASS_ARRAY); if (!id) { id = HPDF_Array_New (pdf->mmgr); if (!id || HPDF_Dict_Add (pdf->trailer, "ID", id) != HPDF_OK) return pdf->error.error_no; } else HPDF_Array_Clear (id); if (HPDF_Array_Add (id, HPDF_Binary_New (pdf->mmgr, e->encrypt_id, HPDF_ID_LEN)) != HPDF_OK) return pdf->error.error_no; if (HPDF_Array_Add (id, HPDF_Binary_New (pdf->mmgr, e->encrypt_id, HPDF_ID_LEN)) != HPDF_OK) return pdf->error.error_no; return HPDF_OK; }