| 1 | /* |
| 2 | * << Haru Free PDF Library >> -- hpdf_binary.c |
| 3 | * |
| 4 | * URL: http://libharu.org |
| 5 | * |
| 6 | * Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp> |
| 7 | * Copyright (c) 2007-2009 Antony Dovgal <tony@daylessday.org> |
| 8 | * |
| 9 | * Permission to use, copy, modify, distribute and sell this software |
| 10 | * and its documentation for any purpose is hereby granted without fee, |
| 11 | * provided that the above copyright notice appear in all copies and |
| 12 | * that both that copyright notice and this permission notice appear |
| 13 | * in supporting documentation. |
| 14 | * It is provided "as is" without express or implied warranty. |
| 15 | * |
| 16 | */ |
| 17 | |
| 18 | #include "hpdf_conf.h" |
| 19 | #include "hpdf_utils.h" |
| 20 | #include "hpdf_objects.h" |
| 21 | |
| 22 | |
| 23 | HPDF_Binary |
| 24 | HPDF_Binary_New (HPDF_MMgr mmgr, |
| 25 | HPDF_BYTE *value, |
| 26 | HPDF_UINT len) |
| 27 | { |
| 28 | HPDF_Binary obj; |
| 29 | |
| 30 | obj = HPDF_GetMem (mmgr, sizeof(HPDF_Binary_Rec)); |
| 31 | |
| 32 | if (obj) { |
| 33 | HPDF_MemSet(&obj->header, 0, sizeof(HPDF_Obj_Header)); |
| 34 | obj->header.obj_class = HPDF_OCLASS_BINARY; |
| 35 | |
| 36 | obj->mmgr = mmgr; |
| 37 | obj->error = mmgr->error; |
| 38 | obj->value = NULL; |
| 39 | obj->len = 0; |
| 40 | if (HPDF_Binary_SetValue (obj, value, len) != HPDF_OK) { |
| 41 | HPDF_FreeMem (mmgr, obj); |
| 42 | return NULL; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | return obj; |
| 47 | } |
| 48 | |
| 49 | HPDF_STATUS |
| 50 | HPDF_Binary_Write (HPDF_Binary obj, |
| 51 | HPDF_Stream stream, |
| 52 | HPDF_Encrypt e) |
| 53 | { |
| 54 | HPDF_STATUS ret; |
| 55 | |
| 56 | if (obj->len == 0) |
| 57 | return HPDF_Stream_WriteStr (stream, "<>" ); |
| 58 | |
| 59 | if ((ret = HPDF_Stream_WriteChar (stream, '<')) != HPDF_OK) |
| 60 | return ret; |
| 61 | |
| 62 | if (e) |
| 63 | HPDF_Encrypt_Reset (e); |
| 64 | |
| 65 | if ((ret = HPDF_Stream_WriteBinary (stream, obj->value, obj->len, e)) != |
| 66 | HPDF_OK) |
| 67 | return ret; |
| 68 | |
| 69 | return HPDF_Stream_WriteChar (stream, '>'); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | HPDF_STATUS |
| 74 | HPDF_Binary_SetValue (HPDF_Binary obj, |
| 75 | HPDF_BYTE *value, |
| 76 | HPDF_UINT len) |
| 77 | { |
| 78 | if (len > HPDF_LIMIT_MAX_STRING_LEN) |
| 79 | return HPDF_SetError (obj->error, HPDF_BINARY_LENGTH_ERR, 0); |
| 80 | |
| 81 | if (obj->value) { |
| 82 | HPDF_FreeMem (obj->mmgr, obj->value); |
| 83 | obj->len = 0; |
| 84 | } |
| 85 | |
| 86 | obj->value = HPDF_GetMem (obj->mmgr, len); |
| 87 | if (!obj->value) |
| 88 | return HPDF_Error_GetCode (obj->error); |
| 89 | |
| 90 | HPDF_MemCpy (obj->value, value, len); |
| 91 | obj->len = len; |
| 92 | |
| 93 | return HPDF_OK; |
| 94 | } |
| 95 | |
| 96 | |
| 97 | void |
| 98 | HPDF_Binary_Free (HPDF_Binary obj) |
| 99 | { |
| 100 | if (!obj) |
| 101 | return; |
| 102 | |
| 103 | if (obj->value) |
| 104 | HPDF_FreeMem (obj->mmgr, obj->value); |
| 105 | |
| 106 | HPDF_FreeMem (obj->mmgr, obj); |
| 107 | } |
| 108 | |
| 109 | HPDF_UINT |
| 110 | HPDF_Binary_GetLen (HPDF_Binary obj) |
| 111 | { |
| 112 | return obj->len; |
| 113 | } |
| 114 | |
| 115 | HPDF_BYTE* |
| 116 | HPDF_Binary_GetValue (HPDF_Binary obj) |
| 117 | { |
| 118 | return obj->value; |
| 119 | } |
| 120 | |
| 121 | |