1/*
2 * << Haru Free PDF Library >> -- hpdf_info.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_info.h"
21
22static const char * const HPDF_INFO_ATTR_NAMES[] = {
23 "CreationDate",
24 "ModDate",
25 "Author",
26 "Creator",
27 "Producer",
28 "Title",
29 "Subject",
30 "Keywords",
31 "Trapped",
32 "GTS_PDFXVersion",
33 NULL
34};
35
36
37static const char*
38InfoTypeToName (HPDF_InfoType type);
39
40
41/*---------------------------------------------------------------------------*/
42
43static const char*
44InfoTypeToName (HPDF_InfoType type)
45{
46 HPDF_UINT idx = (HPDF_UINT)type;
47
48 return HPDF_INFO_ATTR_NAMES[idx];
49}
50
51
52HPDF_STATUS
53HPDF_Info_SetInfoAttr (HPDF_Dict info,
54 HPDF_InfoType type,
55 const char *value,
56 HPDF_Encoder encoder)
57{
58 const char* name = InfoTypeToName (type);
59
60 HPDF_PTRACE((" HPDF_Info_SetInfoAttr\n"));
61
62 if (type <= HPDF_INFO_MOD_DATE)
63 return HPDF_SetError (info->error, HPDF_INVALID_PARAMETER, 0);
64
65 if (type == HPDF_INFO_TRAPPED)
66 return HPDF_Dict_AddName(info, name, value);
67
68 return HPDF_Dict_Add (info, name, HPDF_String_New (info->mmgr, value,
69 encoder));
70}
71
72
73const char*
74HPDF_Info_GetInfoAttr (HPDF_Dict info,
75 HPDF_InfoType type)
76{
77 const char* name = InfoTypeToName (type);
78 HPDF_String s;
79
80 HPDF_PTRACE((" HPDF_Info_GetInfoAttr\n"));
81
82 if (!info)
83 return NULL;
84
85 s = HPDF_Dict_GetItem (info, name, HPDF_OCLASS_STRING);
86
87 if (!s)
88 return NULL;
89 else
90 return (const char *)(s->value);
91}
92
93
94HPDF_STATUS
95HPDF_Info_SetInfoDateAttr (HPDF_Dict info,
96 HPDF_InfoType type,
97 HPDF_Date value)
98{
99 char tmp[HPDF_DATE_TIME_STR_LEN + 1];
100 char* ptmp;
101 const char* name = InfoTypeToName (type);
102
103 HPDF_PTRACE((" HPDF_Info_SetInfoDateAttr\n"));
104
105 if (type > HPDF_INFO_MOD_DATE)
106 return HPDF_SetError (info->error, HPDF_INVALID_PARAMETER, 0);
107
108 HPDF_MemSet (tmp, 0, HPDF_DATE_TIME_STR_LEN + 1);
109 if (value.month < 1 || 12 < value.month ||
110 value.day < 1 ||
111 23 < value.hour ||
112 59 < value.minutes ||
113 59 < value.seconds ||
114 (value.ind != '+' && value.ind != '-' && value.ind != 'Z' &&
115 value.ind != ' ') ||
116 23 < value.off_hour ||
117 59 < value.off_minutes) {
118 return HPDF_SetError (info->error, HPDF_INVALID_DATE_TIME, 0);
119 }
120
121 switch (value.month) {
122 case 1:
123 case 3:
124 case 5:
125 case 7:
126 case 8:
127 case 10:
128 case 12:
129 if (value.day > 31)
130 return HPDF_SetError (info->error, HPDF_INVALID_DATE_TIME, 0);
131
132 break;
133 case 4:
134 case 6:
135 case 9:
136 case 11:
137 if (value.day > 30)
138 return HPDF_SetError (info->error, HPDF_INVALID_DATE_TIME, 0);
139
140 break;
141 case 2:
142 if (value.day > 29 || (value.day == 29 &&
143 (value.year % 4 != 0 ||
144 (value.year % 100 == 0 && value.year % 400 != 0))))
145 return HPDF_SetError (info->error, HPDF_INVALID_DATE_TIME, 0);
146
147 break;
148 default:
149 return HPDF_SetError (info->error, HPDF_INVALID_DATE_TIME, 0);
150 }
151
152 ptmp = (char *)HPDF_MemCpy ((HPDF_BYTE *)tmp, (HPDF_BYTE *)"D:", 2);
153 ptmp = HPDF_IToA2 (ptmp, value.year, 5);
154 ptmp = HPDF_IToA2 (ptmp, value.month, 3);
155 ptmp = HPDF_IToA2 (ptmp, value.day, 3);
156 ptmp = HPDF_IToA2 (ptmp, value.hour, 3);
157 ptmp = HPDF_IToA2 (ptmp, value.minutes, 3);
158 ptmp = HPDF_IToA2 (ptmp, value.seconds, 3);
159 if (value.ind != ' ') {
160 *ptmp++ = value.ind;
161 ptmp = HPDF_IToA2 (ptmp, value.off_hour, 3);
162 *ptmp++ = '\'';
163 ptmp = HPDF_IToA2 (ptmp, value.off_minutes, 3);
164 *ptmp++ = '\'';
165 }
166 *ptmp = 0;
167
168 return HPDF_Dict_Add (info, name, HPDF_String_New (info->mmgr, tmp,
169 NULL));
170}
171
172
173