-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathpyevtx_file_object_io_handle.c
1524 lines (1353 loc) · 33 KB
/
pyevtx_file_object_io_handle.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Python file object IO handle functions
*
* Copyright (C) 2011-2021, Joachim Metz <joachim.metz@gmail.com>
*
* Refer to AUTHORS for acknowledgements.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <common.h>
#include <memory.h>
#include <types.h>
#include "pyevtx_error.h"
#include "pyevtx_file_object_io_handle.h"
#include "pyevtx_integer.h"
#include "pyevtx_libbfio.h"
#include "pyevtx_libcerror.h"
#include "pyevtx_python.h"
/* Creates a file object IO handle
* Make sure the value file_object_io_handle is referencing, is set to NULL
* Returns 1 if successful or -1 on error
*/
int pyevtx_file_object_io_handle_initialize(
pyevtx_file_object_io_handle_t **file_object_io_handle,
PyObject *file_object,
libcerror_error_t **error )
{
static char *function = "pyevtx_file_object_io_handle_initialize";
if( file_object_io_handle == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid file object IO handle.",
function );
return( -1 );
}
if( *file_object_io_handle != NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
"%s: invalid file object IO handle value already set.",
function );
return( -1 );
}
if( file_object == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid file object.",
function );
return( -1 );
}
*file_object_io_handle = (pyevtx_file_object_io_handle_t *) PyMem_Malloc(
sizeof( pyevtx_file_object_io_handle_t ) );
if( *file_object_io_handle == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_MEMORY,
LIBCERROR_MEMORY_ERROR_INSUFFICIENT,
"%s: unable to create file object IO handle.",
function );
goto on_error;
}
if( memory_set(
*file_object_io_handle,
0,
sizeof( pyevtx_file_object_io_handle_t ) ) == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_MEMORY,
LIBCERROR_MEMORY_ERROR_SET_FAILED,
"%s: unable to clear file object IO handle.",
function );
goto on_error;
}
( *file_object_io_handle )->file_object = file_object;
Py_IncRef(
( *file_object_io_handle )->file_object );
return( 1 );
on_error:
if( *file_object_io_handle != NULL )
{
PyMem_Free(
*file_object_io_handle );
*file_object_io_handle = NULL;
}
return( -1 );
}
/* Initializes the file object IO handle
* Returns 1 if successful or -1 on error
*/
int pyevtx_file_object_initialize(
libbfio_handle_t **handle,
PyObject *file_object,
libcerror_error_t **error )
{
pyevtx_file_object_io_handle_t *file_object_io_handle = NULL;
static char *function = "pyevtx_file_object_initialize";
if( handle == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid handle.",
function );
return( -1 );
}
if( *handle != NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
"%s: invalid handle value already set.",
function );
return( -1 );
}
if( pyevtx_file_object_io_handle_initialize(
&file_object_io_handle,
file_object,
error ) != 1 )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
"%s: unable to create file object IO handle.",
function );
goto on_error;
}
if( libbfio_handle_initialize(
handle,
(intptr_t *) file_object_io_handle,
(int (*)(intptr_t **, libcerror_error_t **)) pyevtx_file_object_io_handle_free,
(int (*)(intptr_t **, intptr_t *, libcerror_error_t **)) pyevtx_file_object_io_handle_clone,
(int (*)(intptr_t *, int, libcerror_error_t **)) pyevtx_file_object_io_handle_open,
(int (*)(intptr_t *, libcerror_error_t **)) pyevtx_file_object_io_handle_close,
(ssize_t (*)(intptr_t *, uint8_t *, size_t, libcerror_error_t **)) pyevtx_file_object_io_handle_read,
(ssize_t (*)(intptr_t *, const uint8_t *, size_t, libcerror_error_t **)) pyevtx_file_object_io_handle_write,
(off64_t (*)(intptr_t *, off64_t, int, libcerror_error_t **)) pyevtx_file_object_io_handle_seek_offset,
(int (*)(intptr_t *, libcerror_error_t **)) pyevtx_file_object_io_handle_exists,
(int (*)(intptr_t *, libcerror_error_t **)) pyevtx_file_object_io_handle_is_open,
(int (*)(intptr_t *, size64_t *, libcerror_error_t **)) pyevtx_file_object_io_handle_get_size,
LIBBFIO_FLAG_IO_HANDLE_MANAGED | LIBBFIO_FLAG_IO_HANDLE_CLONE_BY_FUNCTION,
error ) != 1 )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
"%s: unable to create handle.",
function );
goto on_error;
}
return( 1 );
on_error:
if( file_object_io_handle != NULL )
{
pyevtx_file_object_io_handle_free(
&file_object_io_handle,
NULL );
}
return( -1 );
}
/* Frees a file object IO handle
* Returns 1 if succesful or -1 on error
*/
int pyevtx_file_object_io_handle_free(
pyevtx_file_object_io_handle_t **file_object_io_handle,
libcerror_error_t **error )
{
static char *function = "pyevtx_file_object_io_handle_free";
PyGILState_STATE gil_state = 0;
if( file_object_io_handle == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid file object IO handle.",
function );
return( -1 );
}
if( *file_object_io_handle != NULL )
{
gil_state = PyGILState_Ensure();
Py_DecRef(
( *file_object_io_handle )->file_object );
PyGILState_Release(
gil_state );
PyMem_Free(
*file_object_io_handle );
*file_object_io_handle = NULL;
}
return( 1 );
}
/* Clones (duplicates) the file object IO handle and its attributes
* Returns 1 if succesful or -1 on error
*/
int pyevtx_file_object_io_handle_clone(
pyevtx_file_object_io_handle_t **destination_file_object_io_handle,
pyevtx_file_object_io_handle_t *source_file_object_io_handle,
libcerror_error_t **error )
{
static char *function = "pyevtx_file_object_io_handle_clone";
if( destination_file_object_io_handle == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid destination file object IO handle.",
function );
return( -1 );
}
if( *destination_file_object_io_handle != NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_VALUE_ALREADY_SET,
"%s: destination file object IO handle already set.",
function );
return( -1 );
}
if( source_file_object_io_handle == NULL )
{
*destination_file_object_io_handle = NULL;
return( 1 );
}
if( pyevtx_file_object_io_handle_initialize(
destination_file_object_io_handle,
source_file_object_io_handle->file_object,
error ) != 1 )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_INITIALIZE_FAILED,
"%s: unable to create file object IO handle.",
function );
return( -1 );
}
if( *destination_file_object_io_handle == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
"%s: missing destination file object IO handle.",
function );
return( -1 );
}
return( 1 );
}
/* Opens the file object IO handle
* Returns 1 if successful or -1 on error
*/
int pyevtx_file_object_io_handle_open(
pyevtx_file_object_io_handle_t *file_object_io_handle,
int access_flags,
libcerror_error_t **error )
{
static char *function = "pyevtx_file_object_io_handle_open";
if( file_object_io_handle == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid file object IO handle.",
function );
return( -1 );
}
if( file_object_io_handle->file_object == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
"%s: invalid file object IO handle - missing file object.",
function );
return( -1 );
}
if( ( ( access_flags & LIBBFIO_ACCESS_FLAG_READ ) != 0 )
&& ( ( access_flags & LIBBFIO_ACCESS_FLAG_WRITE ) != 0 ) )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
"%s: unsupported access flags.",
function );
return( -1 );
}
if( ( access_flags & LIBBFIO_ACCESS_FLAG_WRITE ) != 0 )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
"%s: write access currently not supported.",
function );
return( -1 );
}
/* No need to do anything here, because the file object is already open
*/
file_object_io_handle->access_flags = access_flags;
return( 1 );
}
/* Closes the file object IO handle
* Returns 0 if successful or -1 on error
*/
int pyevtx_file_object_io_handle_close(
pyevtx_file_object_io_handle_t *file_object_io_handle,
libcerror_error_t **error )
{
static char *function = "pyevtx_file_object_io_handle_close";
if( file_object_io_handle == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid file object IO handle.",
function );
return( -1 );
}
if( file_object_io_handle->file_object == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
"%s: invalid file object IO handle - missing file object.",
function );
return( -1 );
}
/* Do not close the file object, have Python deal with it
*/
file_object_io_handle->access_flags = 0;
return( 0 );
}
/* Reads a buffer from the file object
* Make sure to hold the GIL state before calling this function
* Returns the number of bytes read if successful, or -1 on error
*/
ssize_t pyevtx_file_object_read_buffer(
PyObject *file_object,
uint8_t *buffer,
size_t size,
libcerror_error_t **error )
{
PyObject *argument_size = NULL;
PyObject *method_name = NULL;
PyObject *method_result = NULL;
static char *function = "pyevtx_file_object_read_buffer";
char *safe_buffer = NULL;
Py_ssize_t safe_read_count = 0;
ssize_t read_count = 0;
int result = 0;
if( file_object == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid file object.",
function );
return( -1 );
}
if( buffer == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid buffer.",
function );
return( -1 );
}
if( size > (size_t) SSIZE_MAX )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
"%s: invalid size value exceeds maximum.",
function );
return( -1 );
}
if( size > 0 )
{
#if PY_MAJOR_VERSION >= 3
method_name = PyUnicode_FromString(
"read" );
#else
method_name = PyString_FromString(
"read" );
#endif
argument_size = PyLong_FromSize_t(
size );
PyErr_Clear();
method_result = PyObject_CallMethodObjArgs(
file_object,
method_name,
argument_size,
NULL );
if( PyErr_Occurred() )
{
pyevtx_error_fetch(
error,
LIBCERROR_ERROR_DOMAIN_IO,
LIBCERROR_IO_ERROR_READ_FAILED,
"%s: unable to read from file object.",
function );
goto on_error;
}
if( method_result == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
"%s: missing method result.",
function );
goto on_error;
}
#if PY_MAJOR_VERSION >= 3
result = PyObject_IsInstance(
method_result,
(PyObject *) &PyBytes_Type );
#else
result = PyObject_IsInstance(
method_result,
(PyObject *) &PyString_Type );
#endif
if( result == -1 )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_GET_FAILED,
"%s: unable to determine if method result is a binary string object.",
function );
goto on_error;
}
else if( result == 0 )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_UNSUPPORTED_VALUE,
"%s: invalid method result value is not a binary string object.",
function );
goto on_error;
}
#if PY_MAJOR_VERSION >= 3
result = PyBytes_AsStringAndSize(
method_result,
&safe_buffer,
&safe_read_count );
#else
result = PyString_AsStringAndSize(
method_result,
&safe_buffer,
&safe_read_count );
#endif
if( result == -1 )
{
pyevtx_error_fetch(
error,
LIBCERROR_ERROR_DOMAIN_IO,
LIBCERROR_IO_ERROR_READ_FAILED,
"%s: unable to read from file object.",
function );
goto on_error;
}
if( safe_read_count > (Py_ssize_t) SSIZE_MAX )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_VALUE_EXCEEDS_MAXIMUM,
"%s: invalid read count value exceeds maximum.",
function );
goto on_error;
}
read_count = (ssize_t) safe_read_count;
if( memory_copy(
buffer,
safe_buffer,
read_count ) == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_MEMORY,
LIBCERROR_MEMORY_ERROR_COPY_FAILED,
"%s: unable to data to buffer.",
function );
goto on_error;
}
Py_DecRef(
method_result );
Py_DecRef(
argument_size );
Py_DecRef(
method_name );
}
return( read_count );
on_error:
if( method_result != NULL )
{
Py_DecRef(
method_result );
}
if( argument_size != NULL )
{
Py_DecRef(
argument_size );
}
if( method_name != NULL )
{
Py_DecRef(
method_name );
}
return( -1 );
}
/* Reads a buffer from the file object IO handle
* Returns the number of bytes read if successful, or -1 on error
*/
ssize_t pyevtx_file_object_io_handle_read(
pyevtx_file_object_io_handle_t *file_object_io_handle,
uint8_t *buffer,
size_t size,
libcerror_error_t **error )
{
static char *function = "pyevtx_file_object_io_handle_read";
PyGILState_STATE gil_state = 0;
ssize_t read_count = 0;
if( file_object_io_handle == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid file object IO handle.",
function );
return( -1 );
}
gil_state = PyGILState_Ensure();
read_count = pyevtx_file_object_read_buffer(
file_object_io_handle->file_object,
buffer,
size,
error );
if( read_count == -1 )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_IO,
LIBCERROR_IO_ERROR_READ_FAILED,
"%s: unable to read from file object.",
function );
goto on_error;
}
PyGILState_Release(
gil_state );
return( read_count );
on_error:
PyGILState_Release(
gil_state );
return( -1 );
}
/* Writes a buffer to the file object
* Make sure to hold the GIL state before calling this function
* Returns the number of bytes written if successful, or -1 on error
*/
ssize_t pyevtx_file_object_write_buffer(
PyObject *file_object,
const uint8_t *buffer,
size_t size,
libcerror_error_t **error )
{
PyObject *argument_string = NULL;
PyObject *method_name = NULL;
PyObject *method_result = NULL;
static char *function = "pyevtx_file_object_write_buffer";
if( file_object == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid file object.",
function );
return( -1 );
}
if( buffer == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid buffer.",
function );
return( -1 );
}
#if SIZEOF_SIZE_T > SIZEOF_INT
if( size > (size_t) INT_MAX )
#else
if( size > (size_t) SSIZE_MAX )
#endif
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
"%s: invalid size value exceeds maximum.",
function );
return( -1 );
}
if( size > 0 )
{
#if PY_MAJOR_VERSION >= 3
method_name = PyUnicode_FromString(
"write" );
#else
method_name = PyString_FromString(
"write" );
#endif
#if PY_MAJOR_VERSION >= 3
argument_string = PyBytes_FromStringAndSize(
(char *) buffer,
size );
#else
argument_string = PyString_FromStringAndSize(
(char *) buffer,
size );
#endif
PyErr_Clear();
method_result = PyObject_CallMethodObjArgs(
file_object,
method_name,
argument_string,
NULL );
if( PyErr_Occurred() )
{
pyevtx_error_fetch(
error,
LIBCERROR_ERROR_DOMAIN_IO,
LIBCERROR_IO_ERROR_WRITE_FAILED,
"%s: unable to write to file object.",
function );
goto on_error;
}
if( method_result == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
"%s: missing method result.",
function );
goto on_error;
}
Py_DecRef(
method_result );
Py_DecRef(
argument_string );
Py_DecRef(
method_name );
}
return( (ssize_t) size );
on_error:
if( method_result != NULL )
{
Py_DecRef(
method_result );
}
if( argument_string != NULL )
{
Py_DecRef(
argument_string );
}
if( method_name != NULL )
{
Py_DecRef(
method_name );
}
return( -1 );
}
/* Writes a buffer to the file object IO handle
* Returns the number of bytes written if successful, or -1 on error
*/
ssize_t pyevtx_file_object_io_handle_write(
pyevtx_file_object_io_handle_t *file_object_io_handle,
const uint8_t *buffer,
size_t size,
libcerror_error_t **error )
{
static char *function = "pyevtx_file_object_io_handle_write";
PyGILState_STATE gil_state = 0;
ssize_t write_count = 0;
if( file_object_io_handle == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid file object IO handle.",
function );
return( -1 );
}
gil_state = PyGILState_Ensure();
write_count = pyevtx_file_object_write_buffer(
file_object_io_handle->file_object,
buffer,
size,
error );
if( write_count == -1 )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_IO,
LIBCERROR_IO_ERROR_WRITE_FAILED,
"%s: unable to write from file object.",
function );
goto on_error;
}
PyGILState_Release(
gil_state );
return( write_count );
on_error:
PyGILState_Release(
gil_state );
return( -1 );
}
/* Seeks a certain offset within the file object
* Make sure to hold the GIL state before calling this function
* Returns 1 if successful or -1 on error
*/
int pyevtx_file_object_seek_offset(
PyObject *file_object,
off64_t offset,
int whence,
libcerror_error_t **error )
{
PyObject *argument_offset = NULL;
PyObject *argument_whence = NULL;
PyObject *method_name = NULL;
PyObject *method_result = NULL;
static char *function = "pyevtx_file_object_seek_offset";
if( file_object == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_INVALID_VALUE,
"%s: invalid file object.",
function );
return( -1 );
}
#if defined( HAVE_LONG_LONG )
if( offset > (off64_t) INT64_MAX )
#else
if( offset > (off64_t) LONG_MAX )
#endif
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_VALUE_EXCEEDS_MAXIMUM,
"%s: invalid offset value exceeds maximum.",
function );
return( -1 );
}
if( ( whence != SEEK_CUR )
&& ( whence != SEEK_END )
&& ( whence != SEEK_SET ) )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_ARGUMENTS,
LIBCERROR_ARGUMENT_ERROR_UNSUPPORTED_VALUE,
"%s: unsupported whence.",
function );
return( -1 );
}
#if PY_MAJOR_VERSION >= 3
method_name = PyUnicode_FromString(
"seek" );
#else
method_name = PyString_FromString(
"seek" );
#endif
#if defined( HAVE_LONG_LONG )
argument_offset = PyLong_FromLongLong(
(PY_LONG_LONG) offset );
#else
argument_offset = PyLong_FromLongLong(
(long) offset );
#endif
#if PY_MAJOR_VERSION >= 3
argument_whence = PyLong_FromLong(
(long) whence );
#else
argument_whence = PyInt_FromLong(
(long) whence );
#endif
PyErr_Clear();
method_result = PyObject_CallMethodObjArgs(
file_object,
method_name,
argument_offset,
argument_whence,
NULL );
if( PyErr_Occurred() )
{
pyevtx_error_fetch(
error,
LIBCERROR_ERROR_DOMAIN_IO,
LIBCERROR_IO_ERROR_SEEK_FAILED,
"%s: unable to seek in file object.",
function );
goto on_error;
}
if( method_result == NULL )
{
libcerror_error_set(
error,
LIBCERROR_ERROR_DOMAIN_RUNTIME,
LIBCERROR_RUNTIME_ERROR_VALUE_MISSING,
"%s: missing method result.",
function );
goto on_error;
}
Py_DecRef(
method_result );
Py_DecRef(
argument_whence );
Py_DecRef(
argument_offset );
Py_DecRef(
method_name );
return( 1 );
on_error:
if( method_result != NULL )
{
Py_DecRef(
method_result );
}
if( argument_whence != NULL )
{
Py_DecRef(
argument_whence );
}
if( argument_offset != NULL )
{
Py_DecRef(
argument_offset );
}
if( method_name != NULL )
{
Py_DecRef(
method_name );
}
return( -1 );
}