forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hpdf_string.c
202 lines (159 loc) · 5.37 KB
/
hpdf_string.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
* << Haru Free PDF Library >> -- hpdf_string.c
*
* URL: http://libharu.org
*
* Copyright (c) 1999-2006 Takeshi Kanno <takeshi_kanno@est.hi-ho.ne.jp>
* Copyright (c) 2007-2009 Antony Dovgal <tony@daylessday.org>
*
* Permission to use, copy, modify, distribute and sell this software
* and its documentation for any purpose is hereby granted without fee,
* provided that the above copyright notice appear in all copies and
* that both that copyright notice and this permission notice appear
* in supporting documentation.
* It is provided "as is" without express or implied warranty.
*
*/
#include <string.h>
#include "hpdf_conf.h"
#include "hpdf_utils.h"
#include "hpdf_objects.h"
static const HPDF_BYTE UNICODE_HEADER[] = {
0xFE, 0xFF
};
HPDF_String
HPDF_String_New (HPDF_MMgr mmgr,
const char *value,
HPDF_Encoder encoder)
{
HPDF_String obj;
HPDF_PTRACE((" HPDF_String_New\n"));
obj = (HPDF_String)HPDF_GetMem (mmgr, sizeof(HPDF_String_Rec));
if (obj) {
HPDF_MemSet (&obj->header, 0, sizeof(HPDF_Obj_Header));
obj->header.obj_class = HPDF_OCLASS_STRING;
obj->mmgr = mmgr;
obj->error = mmgr->error;
obj->encoder = encoder;
obj->value = NULL;
obj->len = 0;
if (HPDF_String_SetValue (obj, value) != HPDF_OK) {
HPDF_FreeMem (obj->mmgr, obj);
return NULL;
}
}
return obj;
}
HPDF_STATUS
HPDF_String_SetValue (HPDF_String obj,
const char *value)
{
HPDF_UINT len;
HPDF_STATUS ret = HPDF_OK;
HPDF_PTRACE((" HPDF_String_SetValue\n"));
if (obj->value) {
HPDF_FreeMem (obj->mmgr, obj->value);
obj->len = 0;
}
len = HPDF_StrLen(value, HPDF_LIMIT_MAX_STRING_LEN + 1);
if (len > HPDF_LIMIT_MAX_STRING_LEN)
return HPDF_SetError (obj->error, HPDF_STRING_OUT_OF_RANGE, 0);
obj->value = HPDF_GetMem (obj->mmgr, len + 1);
if (!obj->value)
return HPDF_Error_GetCode (obj->error);
HPDF_StrCpy ((char *)obj->value, value, (char *)obj->value + len);
obj->len = len;
return ret;
}
void
HPDF_String_Free (HPDF_String obj)
{
if (!obj)
return;
HPDF_PTRACE((" HPDF_String_Free\n"));
HPDF_FreeMem (obj->mmgr, obj->value);
HPDF_FreeMem (obj->mmgr, obj);
}
HPDF_STATUS
HPDF_String_Write (HPDF_String obj,
HPDF_Stream stream,
HPDF_Encrypt e)
{
HPDF_STATUS ret;
/*
* When encoder is not NULL, text is changed to unicode using encoder,
* and it outputs by HPDF_write_binary method.
*/
HPDF_PTRACE((" HPDF_String_Write\n"));
if (e)
HPDF_Encrypt_Reset (e);
if (obj->encoder == NULL) {
if (e) {
if ((ret = HPDF_Stream_WriteChar (stream, '<')) != HPDF_OK)
return ret;
if ((ret = HPDF_Stream_WriteBinary (stream, obj->value,
HPDF_StrLen ((char *)obj->value, -1), e)) != HPDF_OK)
return ret;
return HPDF_Stream_WriteChar (stream, '>');
} else {
return HPDF_Stream_WriteEscapeText (stream, (char *)obj->value);
}
} else {
HPDF_BYTE* src = obj->value;
HPDF_BYTE buf[HPDF_TEXT_DEFAULT_LEN * 2];
HPDF_UINT tmp_len = 0;
HPDF_BYTE* pbuf = buf;
HPDF_INT32 len = obj->len;
HPDF_ParseText_Rec parse_state;
HPDF_UINT i;
if ((ret = HPDF_Stream_WriteChar (stream, '<')) != HPDF_OK)
return ret;
if ((ret = HPDF_Stream_WriteBinary (stream, UNICODE_HEADER, 2, e))
!= HPDF_OK)
return ret;
HPDF_Encoder_SetParseText (obj->encoder, &parse_state, src, len);
for (i = 0; (HPDF_INT32)i < len; i++) {
HPDF_BYTE b = src[i];
HPDF_UNICODE tmp_unicode;
HPDF_ByteType btype = HPDF_Encoder_ByteType (obj->encoder,
&parse_state);
if (tmp_len >= HPDF_TEXT_DEFAULT_LEN - 1) {
if ((ret = HPDF_Stream_WriteBinary (stream, buf,
tmp_len * 2, e)) != HPDF_OK)
return ret;
tmp_len = 0;
pbuf = buf;
}
if (btype != HPDF_BYTE_TYPE_TRIAL) {
if (btype == HPDF_BYTE_TYPE_LEAD) {
HPDF_BYTE b2 = src[i + 1];
HPDF_UINT16 char_code = (HPDF_UINT16)((HPDF_UINT) b * 256 + b2);
tmp_unicode = HPDF_Encoder_ToUnicode (obj->encoder,
char_code);
} else {
tmp_unicode = HPDF_Encoder_ToUnicode (obj->encoder, b);
}
HPDF_UInt16Swap (&tmp_unicode);
HPDF_MemCpy (pbuf, (HPDF_BYTE*)&tmp_unicode, 2);
pbuf += 2;
tmp_len++;
}
}
if (tmp_len > 0) {
if ((ret = HPDF_Stream_WriteBinary (stream, buf, tmp_len * 2, e))
!= HPDF_OK)
return ret;
}
if ((ret = HPDF_Stream_WriteChar (stream, '>')) != HPDF_OK)
return ret;
}
return HPDF_OK;
}
HPDF_INT32
HPDF_String_Cmp (HPDF_String s1,
HPDF_String s2)
{
if (s1->len < s2->len) return -1;
if (s1->len > s2->len) return +1;
return memcmp(s1->value, s2->value, s1->len);
}