int nbt_write_float(nbt_file *nbt, float *val) { float temp = *val; if (get_endianness() == L_ENDIAN) temp = swapf(temp); return gzwrite(nbt->fp, &temp, sizeof(float)); }
int nbt_write_double(nbt_file *nbt, double *val) { double temp = *val; if (get_endianness() == L_ENDIAN) temp = swapd(temp); return gzwrite(nbt->fp, &temp, sizeof(double)); }
int nbt_write_long(nbt_file *nbt, int64_t *val) { int64_t temp = *val; if (get_endianness() == L_ENDIAN) swapl((uint64_t *)&temp); return gzwrite(nbt->fp, &temp, sizeof(int64_t)); }
int nbt_write_short(nbt_file *nbt, int16_t *val) { int16_t temp = *val; /* Needs swapping first? */ if (get_endianness() == L_ENDIAN) swaps((uint16_t *)&temp); return gzwrite(nbt->fp, &temp, sizeof(int16_t)); }
int nbt_read_byte_array(nbt_file *nbt, unsigned char **out) { int32_t len; gzread(nbt->fp, &len, sizeof(len)); if (get_endianness() == L_ENDIAN) swapi((uint32_t *)&len); *out = malloc(len); gzread(nbt->fp, *out, len); return len; }
int nbt_read_double(nbt_file *nbt, double **out) { double t; gzread(nbt->fp, &t, sizeof(t)); if (get_endianness() == L_ENDIAN) t = swapd(t); *out = malloc(sizeof(double)); memcpy(*out, &t, sizeof(double)); return 0; }
int nbt_read_float(nbt_file *nbt, float **out) { float t; gzread(nbt->fp, &t, sizeof(t)); if (get_endianness() == L_ENDIAN) t = swapf(t); *out = malloc(sizeof(float)); memcpy(*out, &t, sizeof(float)); return 0; }
int nbt_read_long(nbt_file *nbt, int64_t **out) { int64_t t; gzread(nbt->fp, &t, sizeof(t)); if (get_endianness() == L_ENDIAN) swapl((uint64_t *)&t); *out = malloc(sizeof(int64_t)); memcpy(*out, &t, sizeof(int64_t)); return 0; }
int nbt_read_string(nbt_file *nbt, char **out) { int16_t len; gzread(nbt->fp, &len, sizeof(len)); if (get_endianness() == L_ENDIAN) swaps((uint16_t *)&len); *out = malloc(len + 1); memset(*out, 0, len + 1); gzread(nbt->fp, *out, len); return len; }
int32_t nbt_read_list(nbt_file *nbt, char *type_out, void ***target) { char type; int32_t len; int i; gzread(nbt->fp, &type, 1); *type_out = type; gzread(nbt->fp, &len, sizeof(len)); if (get_endianness() == L_ENDIAN) swapi((uint32_t *)&len); *target = malloc(len * sizeof(void *)); for (i = 0; i < len; ++i) nbt_read(nbt, type, &((*target)[i])); return len; }
int read_hexdump(FILE *f, endianness_t srcend, uint16_t *mem, size_t memsize) { char buffer[512] = {0}; char *p = buffer; int continue_last = 0; uint16_t last[8] = {0}; int lastoff = -8; #define SKIP(p) while (isspace(*p)) p++; #define SKIPX(p) while (isxdigit(*p)) p++; while (!feof(f)) { memset(buffer, 0, sizeof(buffer)); fgets(buffer, sizeof(buffer), f); p = buffer; if (*p == '*') { /* * Continuation from last line */ continue_last = 1; } else if (isxdigit(*p)) { /* * OOOO: NNNN [NNNN...]{0 .. 7} */ int offset; sscanf(buffer, "%x", &offset); if (!continue_last && ((offset - lastoff) != 8)) { return -1; } if (continue_last) { int i = 0; int off = lastoff + 8; continue_last = 0; while ((off+i) < offset) { mem[off + i] = last[i % 8]; ++i; } } lastoff = offset; SKIPX(p); SKIP(p); if (*p++ == ':') { unsigned int i; for (i = 0; i < 8; ++i) { SKIP(p); if (isxdigit(*p)) { unsigned int n; if ((offset + i) > memsize) { return -1; } sscanf(p, "%04X", &n); if (n > 0xFFFF) { exit(1); } if (srcend != get_endianness()) n = swaps(n); last[i] = n; mem[offset + i] = n; SKIPX(p); } else { break; } } } else { return -1; } } } return 0; }