aboutsummaryrefslogtreecommitdiff
path: root/src/main/extractor_datasource.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/extractor_datasource.c')
-rw-r--r--src/main/extractor_datasource.c163
1 files changed, 127 insertions, 36 deletions
diff --git a/src/main/extractor_datasource.c b/src/main/extractor_datasource.c
index 2338a1e..77702d2 100644
--- a/src/main/extractor_datasource.c
+++ b/src/main/extractor_datasource.c
@@ -23,6 +23,7 @@
23 * @author Christian Grothoff 23 * @author Christian Grothoff
24 */ 24 */
25#include "platform.h" 25#include "platform.h"
26#include "extractor_logging.h"
26#include "extractor_datasource.h" 27#include "extractor_datasource.h"
27 28
28#if HAVE_LIBBZ2 29#if HAVE_LIBBZ2
@@ -218,7 +219,10 @@ bfds_pick_next_buffer_at (struct BufferedFileDataSource *bfds,
218 ssize_t rd; 219 ssize_t rd;
219 220
220 if (pos > bfds->fsize) 221 if (pos > bfds->fsize)
221 return -1; /* invalid */ 222 {
223 LOG ("Invalid seek operation\n");
224 return -1; /* invalid */
225 }
222 if (NULL == bfds->buffer) 226 if (NULL == bfds->buffer)
223 { 227 {
224 bfds->buffer_pos = pos; 228 bfds->buffer_pos = pos;
@@ -231,7 +235,10 @@ bfds_pick_next_buffer_at (struct BufferedFileDataSource *bfds,
231 bfds->buffer_pos = 0; 235 bfds->buffer_pos = 0;
232 rd = read (bfds->fd, bfds->buffer, bfds->buffer_size); 236 rd = read (bfds->fd, bfds->buffer, bfds->buffer_size);
233 if (rd < 0) 237 if (rd < 0)
234 return -1; 238 {
239 LOG_STRERROR ("read");
240 return -1;
241 }
235 bfds->buffer_bytes = rd; 242 bfds->buffer_bytes = rd;
236 return 0; 243 return 0;
237} 244}
@@ -258,13 +265,19 @@ bfds_new (const void *data,
258 else 265 else
259 xtra = (size_t) fsize; 266 xtra = (size_t) fsize;
260 if ( (-1 == fd) && (NULL == data) ) 267 if ( (-1 == fd) && (NULL == data) )
261 return NULL; 268 {
269 LOG ("Invalid arguments\n");
270 return NULL;
271 }
262 if ( (-1 != fd) && (NULL != data) ) 272 if ( (-1 != fd) && (NULL != data) )
263 fd = -1; /* don't need fd */ 273 fd = -1; /* don't need fd */
264 if (NULL != data) 274 if (NULL != data)
265 xtra = 0; 275 xtra = 0;
266 if (NULL == (result = malloc (sizeof (struct BufferedFileDataSource) + xtra))) 276 if (NULL == (result = malloc (sizeof (struct BufferedFileDataSource) + xtra)))
267 return NULL; 277 {
278 LOG_STRERROR ("malloc");
279 return NULL;
280 }
268 memset (result, 0, sizeof (struct BufferedFileDataSource)); 281 memset (result, 0, sizeof (struct BufferedFileDataSource));
269 result->data = (NULL != data) ? data : &result[1]; 282 result->data = (NULL != data) ? data : &result[1];
270 result->buffer = (NULL != data) ? NULL : &result[1]; 283 result->buffer = (NULL != data) ? NULL : &result[1];
@@ -308,9 +321,15 @@ bfds_seek (struct BufferedFileDataSource *bfds,
308 { 321 {
309 case SEEK_CUR: 322 case SEEK_CUR:
310 if (bfds->fpos + bfds->buffer_pos + pos < 0) 323 if (bfds->fpos + bfds->buffer_pos + pos < 0)
311 return -1; 324 {
325 LOG ("Invalid seek operation\n");
326 return -1;
327 }
312 if (bfds->fpos + bfds->buffer_pos + pos > bfds->fsize) 328 if (bfds->fpos + bfds->buffer_pos + pos > bfds->fsize)
313 return -1; 329 {
330 LOG ("Invalid seek operation\n");
331 return -1;
332 }
314 if ( (NULL == bfds->buffer) || 333 if ( (NULL == bfds->buffer) ||
315 ( (bfds->buffer_pos + pos < bfds->buffer_bytes) && 334 ( (bfds->buffer_pos + pos < bfds->buffer_bytes) &&
316 (bfds->buffer_pos + pos >= 0) ) ) 335 (bfds->buffer_pos + pos >= 0) ) )
@@ -320,20 +339,35 @@ bfds_seek (struct BufferedFileDataSource *bfds,
320 } 339 }
321 if (0 != bfds_pick_next_buffer_at (bfds, 340 if (0 != bfds_pick_next_buffer_at (bfds,
322 bfds->fpos + bfds->buffer_pos + pos)) 341 bfds->fpos + bfds->buffer_pos + pos))
323 return -1; 342 {
343 LOG ("seek operation failed\n");
344 return -1;
345 }
324 return bfds->fpos; 346 return bfds->fpos;
325 case SEEK_END: 347 case SEEK_END:
326 if (pos > 0) 348 if (pos > 0)
327 return -1; 349 {
350 LOG ("Invalid seek operation\n");
351 return -1;
352 }
328 if (bfds->fsize < - pos) 353 if (bfds->fsize < - pos)
329 return -1; 354 {
355 LOG ("Invalid seek operation\n");
356 return -1;
357 }
330 pos = bfds->fsize + pos; 358 pos = bfds->fsize + pos;
331 /* fall-through! */ 359 /* fall-through! */
332 case SEEK_SET: 360 case SEEK_SET:
333 if (pos < 0) 361 if (pos < 0)
334 return -1; 362 {
363 LOG ("Invalid seek operation\n");
364 return -1;
365 }
335 if (pos > bfds->fsize) 366 if (pos > bfds->fsize)
336 return -1; 367 {
368 LOG ("Invalid seek operation\n");
369 return -1;
370 }
337 if ( (NULL == bfds->buffer) || 371 if ( (NULL == bfds->buffer) ||
338 ( (bfds->buffer_pos <= pos) && 372 ( (bfds->buffer_pos <= pos) &&
339 (bfds->buffer_pos + bfds->buffer_bytes > pos) ) ) 373 (bfds->buffer_pos + bfds->buffer_bytes > pos) ) )
@@ -342,7 +376,10 @@ bfds_seek (struct BufferedFileDataSource *bfds,
342 return bfds->buffer_pos; 376 return bfds->buffer_pos;
343 } 377 }
344 if (0 != bfds_pick_next_buffer_at (bfds, pos)) 378 if (0 != bfds_pick_next_buffer_at (bfds, pos))
345 return -1; 379 {
380 LOG ("seek operation failed\n");
381 return -1;
382 }
346 return bfds->fpos; 383 return bfds->fpos;
347 } 384 }
348 return -1; 385 return -1;
@@ -383,13 +420,13 @@ bfds_read (struct BufferedFileDataSource *bfds,
383 bfds->fpos = old_off; 420 bfds->fpos = old_off;
384 bfds->buffer_bytes = 0; 421 bfds->buffer_bytes = 0;
385 bfds->buffer_pos = 0; 422 bfds->buffer_pos = 0;
423 LOG ("read operation failed\n");
386 return -1; /* getting more failed */ 424 return -1; /* getting more failed */
387 } 425 }
388 avail = bfds->buffer_bytes - bfds->buffer_pos; 426 avail = bfds->buffer_bytes - bfds->buffer_pos;
389 if (avail > count) 427 if (avail > count)
390 avail = count; 428 avail = count;
391 if (0 == avail) 429 ASSERT (0 != avail);
392 abort (); /* must not happen */
393 memcpy (&cbuf[ret], bfds->data + bfds->buffer_pos, avail); 430 memcpy (&cbuf[ret], bfds->data + bfds->buffer_pos, avail);
394 bfds->buffer_pos += avail; 431 bfds->buffer_pos += avail;
395 count -= avail; 432 count -= avail;
@@ -433,6 +470,7 @@ cfs_reset_stream_zlib (struct CompressedFileSource *cfs)
433#endif 470#endif
434 )) 471 ))
435 { 472 {
473 LOG ("Failed to initialize zlib decompression\n");
436 return -1; 474 return -1;
437 } 475 }
438 cfs->fpos = 0; 476 cfs->fpos = 0;
@@ -450,6 +488,7 @@ static int
450cfs_reset_stream_bz2 (struct CompressedFileSource *cfs) 488cfs_reset_stream_bz2 (struct CompressedFileSource *cfs)
451{ 489{
452 /* not implemented */ 490 /* not implemented */
491 LOG ("bz2 decompression not implemented\n");
453 return -1; 492 return -1;
454} 493}
455 494
@@ -473,6 +512,7 @@ cfs_reset_stream (struct CompressedFileSource *cfs)
473 case COMP_TYPE_BZ2: 512 case COMP_TYPE_BZ2:
474 return cfs_reset_stream_bz2 (cfs); 513 return cfs_reset_stream_bz2 (cfs);
475 default: 514 default:
515 LOG ("invalid compression type selected\n");
476 return -1; 516 return -1;
477 } 517 }
478} 518}
@@ -575,6 +615,7 @@ static int
575cfs_init_decompressor_bz2 (struct CompressedFileSource *cfs, 615cfs_init_decompressor_bz2 (struct CompressedFileSource *cfs,
576 EXTRACTOR_MetaDataProcessor proc, void *proc_cls) 616 EXTRACTOR_MetaDataProcessor proc, void *proc_cls)
577{ 617{
618 LOG ("bz2 decompression not implemented\n");
578 return -1; 619 return -1;
579} 620}
580 621
@@ -599,6 +640,7 @@ cfs_init_decompressor (struct CompressedFileSource *cfs,
599 case COMP_TYPE_BZ2: 640 case COMP_TYPE_BZ2:
600 return cfs_init_decompressor_bz2 (cfs, proc, proc_cls); 641 return cfs_init_decompressor_bz2 (cfs, proc, proc_cls);
601 default: 642 default:
643 LOG ("invalid compression type selected\n");
602 return -1; 644 return -1;
603 } 645 }
604} 646}
@@ -627,6 +669,7 @@ cfs_deinit_decompressor_zlib (struct CompressedFileSource *cfs)
627static int 669static int
628cfs_deinit_decompressor_bz2 (struct CompressedFileSource *cfs) 670cfs_deinit_decompressor_bz2 (struct CompressedFileSource *cfs)
629{ 671{
672 LOG ("bz2 decompression not implemented\n");
630 return -1; 673 return -1;
631} 674}
632 675
@@ -647,6 +690,7 @@ cfs_deinit_decompressor (struct CompressedFileSource *cfs)
647 case COMP_TYPE_BZ2: 690 case COMP_TYPE_BZ2:
648 return cfs_deinit_decompressor_bz2 (cfs); 691 return cfs_deinit_decompressor_bz2 (cfs);
649 default: 692 default:
693 LOG ("invalid compression type selected\n");
650 return -1; 694 return -1;
651 } 695 }
652} 696}
@@ -684,7 +728,10 @@ cfs_new (struct BufferedFileDataSource *bfds,
684 struct CompressedFileSource *cfs; 728 struct CompressedFileSource *cfs;
685 729
686 if (NULL == (cfs = malloc (sizeof (struct CompressedFileSource)))) 730 if (NULL == (cfs = malloc (sizeof (struct CompressedFileSource))))
687 return NULL; 731 {
732 LOG_STRERROR ("malloc");
733 return NULL;
734 }
688 memset (cfs, 0, sizeof (struct CompressedFileSource)); 735 memset (cfs, 0, sizeof (struct CompressedFileSource));
689 cfs->compression_type = compression_type; 736 cfs->compression_type = compression_type;
690 cfs->bfds = bfds; 737 cfs->bfds = bfds;
@@ -784,6 +831,7 @@ cfs_read_bz2 (struct CompressedFileSource *cfs,
784 void *data, 831 void *data,
785 size_t size) 832 size_t size)
786{ 833{
834 LOG ("bz2 decompression not implemented\n");
787 return -1; 835 return -1;
788} 836}
789 837
@@ -810,6 +858,7 @@ cfs_read (struct CompressedFileSource *cfs,
810 case COMP_TYPE_BZ2: 858 case COMP_TYPE_BZ2:
811 return cfs_read_bz2 (cfs, data, size); 859 return cfs_read_bz2 (cfs, data, size);
812 default: 860 default:
861 LOG ("invalid compression type selected\n");
813 return -1; 862 return -1;
814 } 863 }
815} 864}
@@ -837,33 +886,53 @@ cfs_seek (struct CompressedFileSource *cfs,
837 { 886 {
838 case SEEK_CUR: 887 case SEEK_CUR:
839 if (cfs->fpos + position < 0) 888 if (cfs->fpos + position < 0)
840 return -1; 889 {
890 LOG ("Invalid seek operation\n");
891 return -1;
892 }
841 if ( (-1 != cfs->uncompressed_size) && 893 if ( (-1 != cfs->uncompressed_size) &&
842 (cfs->fpos + position > cfs->uncompressed_size) ) 894 (cfs->fpos + position > cfs->uncompressed_size) )
843 return -1; 895 {
896 LOG ("Invalid seek operation\n");
897 return -1;
898 }
844 nposition = cfs->fpos + position; 899 nposition = cfs->fpos + position;
845 break; 900 break;
846 case SEEK_END: 901 case SEEK_END:
847 if (-1 == cfs->uncompressed_size) 902 if (-1 == cfs->uncompressed_size)
848 { 903 {
849 /* yuck, need to first find end of file! */ 904 /* yuck, need to first find end of file! */
905 LOG ("Seeking from end-of-file in compressed files not implemented\n");
850 return -1; // FIXME: not implemented 906 return -1; // FIXME: not implemented
851 } 907 }
852 if (position > 0) 908 if (position > 0)
853 return -1; 909 {
910 LOG ("Invalid seek operation\n");
911 return -1;
912 }
854 if (cfs->uncompressed_size < - position) 913 if (cfs->uncompressed_size < - position)
855 return -1; 914 {
915 LOG ("Invalid seek operation\n");
916 return -1;
917 }
856 nposition = cfs->uncompressed_size + position; 918 nposition = cfs->uncompressed_size + position;
857 break; 919 break;
858 case SEEK_SET: 920 case SEEK_SET:
859 if (position < 0) 921 if (position < 0)
860 return -1; 922 {
923 LOG ("Invalid seek operation\n");
924 return -1;
925 }
861 if ( (-1 != cfs->uncompressed_size) && 926 if ( (-1 != cfs->uncompressed_size) &&
862 (cfs->uncompressed_size < position ) ) 927 (cfs->uncompressed_size < position ) )
863 return -1; 928 {
929 LOG ("Invalid seek operation\n");
930 return -1;
931 }
864 nposition = (uint64_t) position; 932 nposition = (uint64_t) position;
865 break; 933 break;
866 default: 934 default:
935 LOG ("Invalid seek operation\n");
867 return -1; 936 return -1;
868 } 937 }
869 938
@@ -878,7 +947,10 @@ cfs_seek (struct CompressedFileSource *cfs,
878 else 947 else
879 { 948 {
880 if (-1 == cfs_reset_stream (cfs)) 949 if (-1 == cfs_reset_stream (cfs))
881 return -1; 950 {
951 LOG ("Failed to restart compressed stream for seek operation\n");
952 return -1;
953 }
882 delta = nposition; 954 delta = nposition;
883 } 955 }
884 } 956 }
@@ -891,7 +963,10 @@ cfs_seek (struct CompressedFileSource *cfs,
891 max = (sizeof (buf) > delta) ? delta : sizeof (buf); 963 max = (sizeof (buf) > delta) ? delta : sizeof (buf);
892 ret = cfs_read (cfs, buf, max); 964 ret = cfs_read (cfs, buf, max);
893 if (-1 == ret) 965 if (-1 == ret)
894 return -1; 966 {
967 LOG ("Failed to read decompressed stream for seek operation\n");
968 return -1;
969 }
895 delta -= ret; 970 delta -= ret;
896 } 971 }
897 return cfs->fpos; 972 return cfs->fpos;
@@ -979,10 +1054,14 @@ EXTRACTOR_datasource_create_from_file_ (const char *filename,
979 int64_t fsize; 1054 int64_t fsize;
980 1055
981 if (-1 == (fd = open (filename, O_RDONLY | O_LARGEFILE))) 1056 if (-1 == (fd = open (filename, O_RDONLY | O_LARGEFILE)))
982 return NULL; 1057 {
1058 LOG_STRERROR_FILE ("open", filename);
1059 return NULL;
1060 }
983 if ( (0 != fstat (fd, &sb)) || 1061 if ( (0 != fstat (fd, &sb)) ||
984 (S_ISDIR (sb.st_mode)) ) 1062 (S_ISDIR (sb.st_mode)) )
985 { 1063 {
1064 LOG_STRERROR_FILE ("fstat", filename);
986 (void) close (fd); 1065 (void) close (fd);
987 return NULL; 1066 return NULL;
988 } 1067 }
@@ -1000,21 +1079,26 @@ EXTRACTOR_datasource_create_from_file_ (const char *filename,
1000 } 1079 }
1001 if (NULL == (ds = malloc (sizeof (struct EXTRACTOR_Datasource)))) 1080 if (NULL == (ds = malloc (sizeof (struct EXTRACTOR_Datasource))))
1002 { 1081 {
1082 LOG_STRERROR ("malloc");
1003 bfds_delete (bfds); 1083 bfds_delete (bfds);
1004 return NULL; 1084 return NULL;
1005 } 1085 }
1006 ds->bfds = bfds; 1086 ds->bfds = bfds;
1007 ds->fd = fd; 1087 ds->fd = fd;
1088 ds->cfs = NULL;
1008 ct = get_compression_type (bfds); 1089 ct = get_compression_type (bfds);
1009 if ( (COMP_TYPE_ZLIB == ct) || 1090 if ( (COMP_TYPE_ZLIB == ct) ||
1010 (COMP_TYPE_BZ2 == ct) ) 1091 (COMP_TYPE_BZ2 == ct) )
1011 ds->cfs = cfs_new (bfds, fsize, ct, proc, proc_cls);
1012 if (NULL == ds->cfs)
1013 { 1092 {
1014 bfds_delete (bfds); 1093 ds->cfs = cfs_new (bfds, fsize, ct, proc, proc_cls);
1015 free (ds); 1094 if (NULL == ds->cfs)
1016 (void) close (fd); 1095 {
1017 return NULL; 1096 LOG ("Failed to initialize decompressor\n");
1097 bfds_delete (bfds);
1098 free (ds);
1099 (void) close (fd);
1100 return NULL;
1101 }
1018 } 1102 }
1019 return ds; 1103 return ds;
1020} 1104}
@@ -1041,9 +1125,13 @@ EXTRACTOR_datasource_create_from_buffer_ (const char *buf,
1041 if (0 == size) 1125 if (0 == size)
1042 return NULL; 1126 return NULL;
1043 if (NULL == (bfds = bfds_new (buf, -1, size))) 1127 if (NULL == (bfds = bfds_new (buf, -1, size)))
1044 return NULL; 1128 {
1129 LOG ("Failed to initialize buffer data source\n");
1130 return NULL;
1131 }
1045 if (NULL == (ds = malloc (sizeof (struct EXTRACTOR_Datasource)))) 1132 if (NULL == (ds = malloc (sizeof (struct EXTRACTOR_Datasource))))
1046 { 1133 {
1134 LOG_STRERROR ("malloc");
1047 bfds_delete (bfds); 1135 bfds_delete (bfds);
1048 return NULL; 1136 return NULL;
1049 } 1137 }
@@ -1052,12 +1140,15 @@ EXTRACTOR_datasource_create_from_buffer_ (const char *buf,
1052 ct = get_compression_type (bfds); 1140 ct = get_compression_type (bfds);
1053 if ( (COMP_TYPE_ZLIB == ct) || 1141 if ( (COMP_TYPE_ZLIB == ct) ||
1054 (COMP_TYPE_BZ2 == ct) ) 1142 (COMP_TYPE_BZ2 == ct) )
1055 ds->cfs = cfs_new (bfds, size, ct, proc, proc_cls);
1056 if (NULL == ds->cfs)
1057 { 1143 {
1058 bfds_delete (bfds); 1144 ds->cfs = cfs_new (bfds, size, ct, proc, proc_cls);
1059 free (ds); 1145 if (NULL == ds->cfs)
1060 return NULL; 1146 {
1147 LOG ("Failed to initialize decompressor\n");
1148 bfds_delete (bfds);
1149 free (ds);
1150 return NULL;
1151 }
1061 } 1152 }
1062 return ds; 1153 return ds;
1063} 1154}