Commit 95e7d36b7a9d2c241cfb9e67745c653343bb73c3

Authored by m-holger
Committed by Jay Berkenbilt
1 parent 3c138be0

C-API add two binary UTF8 funtions

add qpdf_oh_new_binary_unicode_string and qpdf_oh_get_binary_utf8_value
include/qpdf/qpdf-c.h
@@ -742,6 +742,9 @@ extern "C" { @@ -742,6 +742,9 @@ extern "C" {
742 QPDF_DLL 742 QPDF_DLL
743 char const* qpdf_oh_get_binary_string_value( 743 char const* qpdf_oh_get_binary_string_value(
744 qpdf_data qpdf, qpdf_oh oh, size_t* length); 744 qpdf_data qpdf, qpdf_oh oh, size_t* length);
  745 + QPDF_DLL
  746 + char const* qpdf_oh_get_binary_utf8_value(
  747 + qpdf_data qpdf, qpdf_oh oh, size_t* length);
745 748
746 QPDF_DLL 749 QPDF_DLL
747 int qpdf_oh_get_array_n_items(qpdf_data qpdf, qpdf_oh oh); 750 int qpdf_oh_get_array_n_items(qpdf_data qpdf, qpdf_oh oh);
@@ -800,6 +803,9 @@ extern "C" { @@ -800,6 +803,9 @@ extern "C" {
800 QPDF_DLL 803 QPDF_DLL
801 qpdf_oh qpdf_oh_new_binary_string( 804 qpdf_oh qpdf_oh_new_binary_string(
802 qpdf_data qpdf, char const* str, size_t length); 805 qpdf_data qpdf, char const* str, size_t length);
  806 + QPDF_DLL
  807 + qpdf_oh qpdf_oh_new_binary_unicode_string(
  808 + qpdf_data qpdf, char const* str, size_t length);
803 QPDF_DLL 809 QPDF_DLL
804 qpdf_oh qpdf_oh_new_array(qpdf_data qpdf); 810 qpdf_oh qpdf_oh_new_array(qpdf_data qpdf);
805 QPDF_DLL 811 QPDF_DLL
libqpdf/qpdf-c.cc
@@ -1327,6 +1327,20 @@ char const* qpdf_oh_get_binary_string_value( @@ -1327,6 +1327,20 @@ char const* qpdf_oh_get_binary_string_value(
1327 }); 1327 });
1328 } 1328 }
1329 1329
  1330 +char const* qpdf_oh_get_binary_utf8_value(
  1331 + qpdf_data qpdf, qpdf_oh oh, size_t* length)
  1332 +{
  1333 + return do_with_oh<char const*>(
  1334 + qpdf, oh,
  1335 + return_T<char const*>(""),
  1336 + [qpdf, length](QPDFObjectHandle& o) {
  1337 + QTC::TC("qpdf", "qpdf-c called qpdf_oh_get_binary_utf8_value");
  1338 + qpdf->tmp_string = o.getUTF8Value();
  1339 + *length = qpdf->tmp_string.length();
  1340 + return qpdf->tmp_string.c_str();
  1341 + });
  1342 +}
  1343 +
1330 int qpdf_oh_get_array_n_items(qpdf_data qpdf, qpdf_oh oh) 1344 int qpdf_oh_get_array_n_items(qpdf_data qpdf, qpdf_oh oh)
1331 { 1345 {
1332 return do_with_oh<int>( 1346 return do_with_oh<int>(
@@ -1468,6 +1482,14 @@ qpdf_oh qpdf_oh_new_binary_string( @@ -1468,6 +1482,14 @@ qpdf_oh qpdf_oh_new_binary_string(
1468 qpdf, QPDFObjectHandle::newString(std::string(str, length))); 1482 qpdf, QPDFObjectHandle::newString(std::string(str, length)));
1469 } 1483 }
1470 1484
  1485 +qpdf_oh qpdf_oh_new_binary_unicode_string(
  1486 + qpdf_data qpdf, char const* utf8_str, size_t length)
  1487 +{
  1488 + QTC::TC("qpdf", "qpdf-c called qpdf_oh_new_binary_unicode_string");
  1489 + return new_object(
  1490 + qpdf, QPDFObjectHandle::newUnicodeString(std::string(utf8_str, length)));
  1491 +}
  1492 +
1471 qpdf_oh qpdf_oh_new_array(qpdf_data qpdf) 1493 qpdf_oh qpdf_oh_new_array(qpdf_data qpdf)
1472 { 1494 {
1473 QTC::TC("qpdf", "qpdf-c called qpdf_oh_new_array"); 1495 QTC::TC("qpdf", "qpdf-c called qpdf_oh_new_array");
qpdf/qpdf-ctest.c
@@ -740,10 +740,13 @@ static void test25(char const* infile, @@ -740,10 +740,13 @@ static void test25(char const* infile,
740 qpdf_oh_append_item( 740 qpdf_oh_append_item(
741 qpdf, new_array, 741 qpdf, new_array,
742 qpdf_oh_new_unicode_string(qpdf, "qww\xc3\xb7\xcf\x80")); 742 qpdf_oh_new_unicode_string(qpdf, "qww\xc3\xb7\xcf\x80"));
  743 + qpdf_oh_append_item(
  744 + qpdf, new_array,
  745 + qpdf_oh_new_binary_unicode_string(qpdf, "qw\x00w\xc3\xb7\xcf\x80", 8));
743 qpdf_oh_append_item(qpdf, new_array, qpdf_oh_new_null(qpdf)); /* 2 */ 746 qpdf_oh_append_item(qpdf, new_array, qpdf_oh_new_null(qpdf)); /* 2 */
744 qpdf_oh_append_item(qpdf, new_array, qpdf_oh_new_null(qpdf)); /* 3 */ 747 qpdf_oh_append_item(qpdf, new_array, qpdf_oh_new_null(qpdf)); /* 3 */
745 qpdf_oh_set_array_item( 748 qpdf_oh_set_array_item(
746 - qpdf, new_array, 2, 749 + qpdf, new_array, 3,
747 qpdf_oh_new_name(qpdf, "/Quack")); 750 qpdf_oh_new_name(qpdf, "/Quack"));
748 qpdf_oh_append_item( 751 qpdf_oh_append_item(
749 qpdf, new_array, 752 qpdf, new_array,
@@ -803,6 +806,24 @@ static void test27(char const* infile, @@ -803,6 +806,24 @@ static void test27(char const* infile,
803 "potato\000salad", 13) == 0); 806 "potato\000salad", 13) == 0);
804 assert(qpdf_get_last_string_length(qpdf) == 12); 807 assert(qpdf_get_last_string_length(qpdf) == 12);
805 assert(length == 12); 808 assert(length == 12);
  809 + /* repeat for UTF8 string */
  810 + qpdf_oh p_utf8_string_with_null = qpdf_oh_parse(qpdf,
  811 + "<feff007100770000007700f703c0>");
  812 + assert(qpdf_oh_is_string(qpdf, p_utf8_string_with_null));
  813 + assert(strcmp(qpdf_oh_get_utf8_value(qpdf, p_utf8_string_with_null),
  814 + "qw\x00w\xc3\xb7\xcf\x80") == 0);
  815 + assert(qpdf_get_last_string_length(qpdf) == 8);
  816 + /* memcmp adds a character to verify the trailing null */
  817 + assert(memcmp(qpdf_oh_get_utf8_value(qpdf, p_utf8_string_with_null),
  818 + "qw\x00w\xc3\xb7\xcf\x80", 8) == 0);
  819 + p_utf8_string_with_null = qpdf_oh_new_binary_unicode_string(
  820 + qpdf, "qw\x00w\xc3\xb7\xcf\x80", 8);
  821 + /* memcmp adds a character to verify the trailing null */
  822 + assert(memcmp(qpdf_oh_get_binary_utf8_value(
  823 + qpdf, p_utf8_string_with_null, &length),
  824 + "qw\x00w\xc3\xb7\xcf\x80", 9) == 0);
  825 + assert(qpdf_get_last_string_length(qpdf) == 8);
  826 + assert(length == 8);
806 } 827 }
807 828
808 static void test28(char const* infile, 829 static void test28(char const* infile,
qpdf/qpdf.testcov
@@ -620,7 +620,9 @@ qpdf-c called qpdf_oh_get_page_content_data 0 @@ -620,7 +620,9 @@ qpdf-c called qpdf_oh_get_page_content_data 0
620 qpdf-c called qpdf_oh_replace_stream_data 0 620 qpdf-c called qpdf_oh_replace_stream_data 0
621 qpdf-c silence oh errors 0 621 qpdf-c silence oh errors 0
622 qpdf-c called qpdf_oh_get_binary_string_value 0 622 qpdf-c called qpdf_oh_get_binary_string_value 0
  623 +qpdf-c called qpdf_oh_get_binary_utf8_value 0
623 qpdf-c called qpdf_oh_new_binary_string 0 624 qpdf-c called qpdf_oh_new_binary_string 0
  625 +qpdf-c called qpdf_oh_new_binary_unicode_string 0
624 QPDFJob duplicated pages password 0 626 QPDFJob duplicated pages password 0
625 QPDFJob misplaced pages password 0 627 QPDFJob misplaced pages password 0
626 QPDFJob check encrypted encrypted 0 628 QPDFJob check encrypted encrypted 0
qpdf/qtest/qpdf/c-object-handle-creation-out.pdf
@@ -9,6 +9,7 @@ @@ -9,6 +9,7 @@
9 /B [ 9 /B [
10 (potato) 10 (potato)
11 <feff00710077007700f703c0> 11 <feff00710077007700f703c0>
  12 + <feff007100770000007700f703c0>
12 /Quack 13 /Quack
13 null 14 null
14 4.12 15 4.12
@@ -95,17 +96,17 @@ xref @@ -95,17 +96,17 @@ xref
95 0 8 96 0 8
96 0000000000 65535 f 97 0000000000 65535 f
97 0000000025 00000 n 98 0000000025 00000 n
98 -0000000277 00000 n  
99 -0000000359 00000 n  
100 -0000000574 00000 n  
101 -0000000673 00000 n  
102 -0000000692 00000 n  
103 -0000000810 00000 n 99 +0000000314 00000 n
  100 +0000000396 00000 n
  101 +0000000611 00000 n
  102 +0000000710 00000 n
  103 +0000000729 00000 n
  104 +0000000847 00000 n
104 trailer << 105 trailer <<
105 /Root 1 0 R 106 /Root 1 0 R
106 /Size 8 107 /Size 8
107 /ID [<31415926535897932384626433832795><31415926535897932384626433832795>] 108 /ID [<31415926535897932384626433832795><31415926535897932384626433832795>]
108 >> 109 >>
109 startxref 110 startxref
110 -845 111 +882
111 %%EOF 112 %%EOF