hbmap: fix iterator truncation when size_t < 32bit
[rofl0r-agsutils.git] / File.c
blobab877d90f39a0689c68c8f29c7a70ff8630a5b49
1 #include "File.h"
3 int AF_open(AF *f, const char* fn) {
4 f->b = &f->b_b;
5 ByteArray_ctor(f->b);
6 ByteArray_set_endian(f->b, BAE_LITTLE);
7 return ByteArray_open_file(f->b, fn);
10 void AF_close(AF* f) {
11 ByteArray_close_file(f->b);
14 ssize_t AF_read(AF* f, void* buf, size_t len) {
15 return ByteArray_readMultiByte(f->b, buf, len);
18 long long AF_read_longlong(AF* f) {
19 return ByteArray_readUnsignedLongLong(f->b);
22 int AF_read_int(AF* f) {
23 return ByteArray_readInt(f->b);
26 unsigned AF_read_uint(AF* f) {
27 return ByteArray_readUnsignedInt(f->b);
30 short AF_read_short(AF* f) {
31 return ByteArray_readShort(f->b);
34 unsigned short AF_read_ushort(AF* f) {
35 return ByteArray_readUnsignedShort(f->b);
38 int AF_read_uchar(AF *f) {
39 return ByteArray_readUnsignedByte(f->b);
42 off_t AF_get_pos(AF* f) {
43 return ByteArray_get_position(f->b);
46 int AF_set_pos(AF* f, off_t x) {
47 return ByteArray_set_position(f->b, x);
50 int AF_dump_chunk_stream(AF* a, size_t start, size_t len, FILE* out) {
51 char buf[4096];
52 ByteArray_set_position(a->b, start);
53 while(len) {
54 size_t togo = len > sizeof(buf) ? sizeof(buf) : len;
55 if(togo != (size_t) ByteArray_readMultiByte(a->b, buf, togo)) {
56 return 0;
58 len -= togo;
59 char *p = buf;
60 while (togo) {
61 size_t n = fwrite(p, 1, togo, out);
62 if(!n) return 0;
63 p += n;
64 togo -= n;
67 return 1;
70 int AF_dump_chunk(AF* a, size_t start, size_t len, char* fn) {
71 FILE *out = fopen(fn, "wb");
72 if(!out) return 0;
73 int ret = AF_dump_chunk_stream(a, start, len, out);
74 fclose(out);
75 return ret;
78 int AF_read_junk(AF* a, size_t l) {
79 /*char buf[512];
80 while(l) {
81 size_t togo = l > sizeof(buf) ? sizeof(buf) : l;
82 if(togo != (size_t) ByteArray_readMultiByte(a->b, buf, togo)) return 0;
83 l -= togo;
85 return 1;*/
86 if(!l) return 1;
87 return ByteArray_set_position(a->b, ByteArray_get_position(a->b) + l);
90 int AF_read_string(AF* a, char* buf, size_t max) {
91 size_t l = 0;
92 while(l < max) {
93 if(ByteArray_readMultiByte(a->b, buf + l, 1) != 1)
94 return 0;
95 if(!buf[l]) return 1;
96 l++;
98 return 0;