void dmx_device_load_from_disc(void) { char *file_name = "device"; char *base_path = SDL_GetPrefPath("net.exse", "untel"); char *file_path = calloc((strlen(base_path)+strlen(file_name)+1),sizeof(char)); strcat(file_path,base_path); strcat(file_path,file_name); free(base_path); SDL_RWops *file = SDL_RWFromFile(file_path, "r"); free(file_path); if(file == NULL) return; unsigned int devices = SDL_ReadBE32(file); for(unsigned int x = 0; x < devices;x++) { unsigned int type =SDL_ReadBE32(file); unsigned int addr =SDL_ReadBE32(file); unsigned int namelen=SDL_ReadU8(file); char name[DMX_NAME_LENGTH]; if(namelen > DMX_NAME_LENGTH) namelen=DMX_NAME_LENGTH; SDL_RWread(file, name, 1, namelen+1); if(type == DMX_DEVICE_LEDPAR6) { dmx_device_create_ledpar6(addr,name); } } SDL_RWclose(file); }
void process_network_physics_model( void *data) { init_physics_wad_data(); if(data) { // check for M1 physics SDL_RWops *ops = SDL_RWFromConstMem(data, 8); uint32 cookie = SDL_ReadBE32(ops); if(cookie == M1_PHYSICS_MAGIC_COOKIE) { uint32 length= SDL_ReadBE32(ops); SDL_RWclose(ops); uint8 *s= (uint8 *)data; import_m1_physics_data_from_network(&s[8], length); return; } else { SDL_RWclose(ops); } struct wad_header header; struct wad_data *wad; wad= inflate_flat_data(data, &header); if(wad) { import_physics_wad_data(wad); free_wad(wad); /* Note that the flat data points into the wad. */ } } }
int main(int argc, char **argv) { char *data_name = NULL, *rsrc_name = NULL; SDL_RWops *fin; Uint32 id, version; if (argc < 2 || argc > 4) { printf("Usage: %s SOURCE [DATA [RESOURCE]]\nExtract data and resource forks from AppleSingle file.\n", argv[0]); exit(1); } // If the destination file names are not specified, the data fork // will go to <name>.data and the resource fork to <name>.rsrc if (argc > 2) { data_name = argv[2]; if (argc > 3) rsrc_name = argv[3]; } if (data_name == NULL) { data_name = malloc(strlen(argv[1]) + 6); strcpy(data_name, argv[1]); strcat(data_name, ".data"); } if (rsrc_name == NULL) { rsrc_name = malloc(strlen(argv[1]) + 6); strcpy(rsrc_name, argv[1]); strcat(rsrc_name, ".rsrc"); } // Open source file SDL_Init(0); fin = SDL_RWFromFile(argv[1], "rb"); if (fin == NULL) { perror("Can't open source file"); exit(1); } id = SDL_ReadBE32(fin); version = SDL_ReadBE32(fin); if (id != 0x00051600 || version != 0x00020000) { fprintf(stderr, "Source file is not a version 2 AppleSingle file.\n"); exit(1); } // Extract data and resource forks extract_fork(fin, 1, data_name, "data"); extract_fork(fin, 2, rsrc_name, "resource"); return 0; }
bool physics_file_is_m1(void) { bool m1_physics = false; // check for M1 physics OpenedFile PhysicsFile; short SavedType, SavedError = get_game_error(&SavedType); if (PhysicsFileSpec.Open(PhysicsFile)) { uint32 tag = SDL_ReadBE32(PhysicsFile.GetRWops()); switch (tag) { case M1_MONSTER_PHYSICS_TAG: case M1_EFFECTS_PHYSICS_TAG: case M1_PROJECTILE_PHYSICS_TAG: case M1_PHYSICS_PHYSICS_TAG: case M1_WEAPONS_PHYSICS_TAG: m1_physics = true; break; default: break; } PhysicsFile.Close(); } set_game_error(SavedType, SavedError); return m1_physics; }
static int ReadChunkBe(SDL_RWops *src, Chunk *chunk) { chunk->magic = SDL_ReadBE32(src); chunk->length = SDL_ReadBE32(src); chunk->data = (Uint8 *)malloc(chunk->length); if ( chunk->data == NULL ) { SDL_Error(SDL_ENOMEM); return(-1); } if ( SDL_RWread(src, chunk->data, chunk->length, 1) != 1 ) { SDL_Error(SDL_EFREAD); free(chunk->data); return(-1); } return(chunk->length); }
void LoadScores(void) { SDL_RWops *scores_src; int i; memset(&hScores, 0, sizeof(hScores)); scores_src = SDL_RWFromFile(STATEDIR "/" MAELSTROM_SCORES, "rb"); if ( scores_src != NULL ) { for ( i=0; i<NUM_SCORES; ++i ) { SDL_RWread(scores_src, hScores[i].name, sizeof(hScores[i].name), 1); hScores[i].wave = SDL_ReadBE32(scores_src); hScores[i].score = SDL_ReadBE32(scores_src); } SDL_RWclose(scores_src); } }
static void extract_fork(SDL_RWops *fin, int req_id, const char *file_name, const char *fork_name) { long id, fork_start, fork_size; int num_entries, fork_found; SDL_RWops *fout; // Look for fork in source file SDL_RWseek(fin, 0x18, SEEK_SET); num_entries = SDL_ReadBE16(fin); while (num_entries--) { Uint32 id = SDL_ReadBE32(fin); Sint32 ofs = SDL_ReadBE32(fin); Sint32 len = SDL_ReadBE32(fin); if (id == req_id) { fork_found = 1; fork_start = ofs; fork_size = len; } } if (!fork_found) { fprintf(stderr, "Warning: source file doesn't contain a %s fork.\n", fork_name); return; } // Found, open destination file fout = SDL_RWFromFile(file_name, "wb"); if (fout == NULL) { perror("Can't open destination file"); exit(1); } // Copy fork SDL_RWseek(fin, fork_start, SEEK_SET); while (fork_size) { long length = fork_size > BUFFER_SIZE ? BUFFER_SIZE : fork_size; SDL_RWread(fin, buffer, 1, length); SDL_RWwrite(fout, buffer, 1, length); fork_size -= length; } SDL_FreeRW(fout); }
void loadPalette( const char *fn ) { SDL_RWops *rw = SDL_RWFromFile(fn, "r"); if (rw != NULL) { for( int i=0; i<numPalette; i++ ) { for( int j=0; j<numColor; j++ ) { Uint32 c = SDL_ReadBE32(rw); mainPalette[i][j] = sdl_color(c); } } SDL_RWclose(rw); } }
/** * @brief Tests writing and reading from file using endian aware functions. * * \sa * http://wiki.libsdl.org/moin.cgi/SDL_RWFromFile * http://wiki.libsdl.org/moin.cgi/SDL_RWClose * http://wiki.libsdl.org/moin.cgi/SDL_ReadBE16 * http://wiki.libsdl.org/moin.cgi/SDL_WriteBE16 */ int rwops_testFileWriteReadEndian(void) { SDL_RWops *rw; Sint64 result; int mode; size_t objectsWritten; Uint16 BE16value; Uint32 BE32value; Uint64 BE64value; Uint16 LE16value; Uint32 LE32value; Uint64 LE64value; Uint16 BE16test; Uint32 BE32test; Uint64 BE64test; Uint16 LE16test; Uint32 LE32test; Uint64 LE64test; int cresult; for (mode = 0; mode < 3; mode++) { /* Create test data */ switch (mode) { case 0: SDLTest_Log("All 0 values"); BE16value = 0; BE32value = 0; BE64value = 0; LE16value = 0; LE32value = 0; LE64value = 0; break; case 1: SDLTest_Log("All 1 values"); BE16value = 1; BE32value = 1; BE64value = 1; LE16value = 1; LE32value = 1; LE64value = 1; break; case 2: SDLTest_Log("Random values"); BE16value = SDLTest_RandomUint16(); BE32value = SDLTest_RandomUint32(); BE64value = SDLTest_RandomUint64(); LE16value = SDLTest_RandomUint16(); LE32value = SDLTest_RandomUint32(); LE64value = SDLTest_RandomUint64(); break; } /* Write test. */ rw = SDL_RWFromFile(RWopsWriteTestFilename, "w+"); SDLTest_AssertPass("Call to SDL_RWFromFile(..,\"w+\")"); SDLTest_AssertCheck(rw != NULL, "Verify opening file with SDL_RWFromFile in write mode does not return NULL"); /* Bail out if NULL */ if (rw == NULL) return TEST_ABORTED; /* Write test data */ objectsWritten = SDL_WriteBE16(rw, BE16value); SDLTest_AssertPass("Call to SDL_WriteBE16"); SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", objectsWritten); objectsWritten = SDL_WriteBE32(rw, BE32value); SDLTest_AssertPass("Call to SDL_WriteBE32"); SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", objectsWritten); objectsWritten = SDL_WriteBE64(rw, BE64value); SDLTest_AssertPass("Call to SDL_WriteBE64"); SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", objectsWritten); objectsWritten = SDL_WriteLE16(rw, LE16value); SDLTest_AssertPass("Call to SDL_WriteLE16"); SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", objectsWritten); objectsWritten = SDL_WriteLE32(rw, LE32value); SDLTest_AssertPass("Call to SDL_WriteLE32"); SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", objectsWritten); objectsWritten = SDL_WriteLE64(rw, LE64value); SDLTest_AssertPass("Call to SDL_WriteLE64"); SDLTest_AssertCheck(objectsWritten == 1, "Validate number of objects written, expected: 1, got: %i", objectsWritten); /* Test seek to start */ result = SDL_RWseek( rw, 0, RW_SEEK_SET ); SDLTest_AssertPass("Call to SDL_RWseek succeeded"); SDLTest_AssertCheck(result == 0, "Verify result from position 0 with SDL_RWseek, expected 0, got %i", result); /* Read test data */ BE16test = SDL_ReadBE16(rw); SDLTest_AssertPass("Call to SDL_ReadBE16"); SDLTest_AssertCheck(BE16test == BE16value, "Validate return value from SDL_ReadBE16, expected: %hu, got: %hu", BE16value, BE16test); BE32test = SDL_ReadBE32(rw); SDLTest_AssertPass("Call to SDL_ReadBE32"); SDLTest_AssertCheck(BE32test == BE32value, "Validate return value from SDL_ReadBE32, expected: %u, got: %u", BE32value, BE32test); BE64test = SDL_ReadBE64(rw); SDLTest_AssertPass("Call to SDL_ReadBE64"); SDLTest_AssertCheck(BE64test == BE64value, "Validate return value from SDL_ReadBE64, expected: %llu, got: %llu", BE64value, BE64test); LE16test = SDL_ReadLE16(rw); SDLTest_AssertPass("Call to SDL_ReadLE16"); SDLTest_AssertCheck(LE16test == LE16value, "Validate return value from SDL_ReadLE16, expected: %hu, got: %hu", LE16value, LE16test); LE32test = SDL_ReadLE32(rw); SDLTest_AssertPass("Call to SDL_ReadLE32"); SDLTest_AssertCheck(LE32test == LE32value, "Validate return value from SDL_ReadLE32, expected: %u, got: %u", LE32value, LE32test); LE64test = SDL_ReadLE64(rw); SDLTest_AssertPass("Call to SDL_ReadLE64"); SDLTest_AssertCheck(LE64test == LE64value, "Validate return value from SDL_ReadLE64, expected: %llu, got: %llu", LE64value, LE64test); /* Close handle */ cresult = SDL_RWclose(rw); SDLTest_AssertPass("Call to SDL_RWclose() succeeded"); SDLTest_AssertCheck(cresult == 0, "Verify result value is 0; got: %d", cresult); } return TEST_COMPLETED; }
// Determine file type Typecode FileSpecifier::GetType() { // if there's an extension, assume it's correct const char *extension = strrchr(GetPath(), '.'); if (extension) { extension_mapping *mapping = extensions; while (mapping->extension) { if (( mapping->case_sensitive && (strcmp(extension + 1, mapping->extension) == 0)) || (!mapping->case_sensitive && (strcasecmp(extension + 1, mapping->extension) == 0))) { return mapping->typecode; } ++mapping; } } // Open file OpenedFile f; if (!Open(f)) return _typecode_unknown; SDL_RWops *p = f.GetRWops(); int32 file_length = 0; f.GetLength(file_length); // Check for Sounds file { f.SetPosition(0); uint32 version = SDL_ReadBE32(p); uint32 tag = SDL_ReadBE32(p); if ((version == 0 || version == 1) && tag == FOUR_CHARS_TO_INT('s', 'n', 'd', '2')) return _typecode_sounds; } // Check for Map/Physics file { f.SetPosition(0); int version = SDL_ReadBE16(p); int data_version = SDL_ReadBE16(p); if ((version == 0 || version == 1 || version == 2 || version == 4) && (data_version == 0 || data_version == 1 || data_version == 2)) { SDL_RWseek(p, 68, SEEK_CUR); int32 directory_offset = SDL_ReadBE32(p); if (directory_offset >= file_length) goto not_map; f.SetPosition(128); uint32 tag = SDL_ReadBE32(p); // ghs: I do not believe this list is comprehensive // I think it's just what we've seen so far? switch (tag) { case LINE_TAG: case POINT_TAG: case SIDE_TAG: return _typecode_scenario; break; case MONSTER_PHYSICS_TAG: return _typecode_physics; break; } } not_map: ; } // Check for Shapes file { f.SetPosition(0); for (int i=0; i<32; i++) { uint32 status_flags = SDL_ReadBE32(p); int32 offset = SDL_ReadBE32(p); int32 length = SDL_ReadBE32(p); int32 offset16 = SDL_ReadBE32(p); int32 length16 = SDL_ReadBE32(p); if (status_flags != 0 || (offset != NONE && (offset >= file_length || offset + length > file_length)) || (offset16 != NONE && (offset16 >= file_length || offset16 + length16 > file_length))) goto not_shapes; SDL_RWseek(p, 12, SEEK_CUR); } return _typecode_shapes; not_shapes: ; } // Not identified return _typecode_unknown; }
SDL_AudioSpec * SDL_LoadWAV_RW (SDL_RWops *src, int freesrc, SDL_AudioSpec *spec, Uint8 **audio_buf, Uint32 *audio_len) { int was_error; Chunk chunk; int lenread; int MS_ADPCM_encoded, IMA_ADPCM_encoded; int samplesize; /* WAV magic header */ Uint32 RIFFchunk; Uint32 wavelen; Uint32 WAVEmagic; /* FMT chunk */ WaveFMT *format = NULL; /* Make sure we are passed a valid data source */ was_error = 0; if ( src == NULL ) { was_error = 1; goto done; } /* Check the magic header */ RIFFchunk = SDL_ReadLE32(src); wavelen = SDL_ReadLE32(src); if ( wavelen == WAVE ) { /* The RIFFchunk has already been read */ WAVEmagic = wavelen; wavelen = RIFFchunk; //RIFFchunk = RIFF; } if (RIFFchunk != RIFF) { RIFFchunk = SDL_ReadBE32(src); wavelen = SDL_ReadBE32(src); if ( wavelen == WAVE ) { /* The RIFFchunk has already been read */ WAVEmagic = wavelen; wavelen = RIFFchunk; RIFFchunk = RIFX; } } if ( wavelen != WAVE ) { WAVEmagic = SDL_ReadLE32(src); } if (((RIFFchunk != RIFF) && (RIFFchunk != RIFX)) || (WAVEmagic != WAVE)) { SDL_SetError("Unrecognized file type %f (not WAVE)", WAVEmagic); was_error = 1; goto done; } if (RIFFchunk == RIFF) { /* Read the audio data format chunk */ chunk.data = NULL; do { if ( chunk.data != NULL ) { free(chunk.data); } lenread = ReadChunk(src, &chunk); if ( lenread < 0 ) { was_error = 1; goto done; } } while ( (chunk.magic == FACT) || (chunk.magic == LIST) ); /* Decode the audio data format */ format = (WaveFMT *)chunk.data; if ( chunk.magic != FMT ) { SDL_SetError("Complex WAVE files not supported"); was_error = 1; goto done; } MS_ADPCM_encoded = IMA_ADPCM_encoded = 0; switch (SDL_SwapLE16(format->encoding)) { case PCM_CODE: /* We can understand this */ break; case MS_ADPCM_CODE: /* Try to understand this */ if ( InitMS_ADPCM(format) < 0 ) { was_error = 1; goto done; } MS_ADPCM_encoded = 1; break; case IMA_ADPCM_CODE: /* Try to understand this */ if ( InitIMA_ADPCM(format) < 0 ) { was_error = 1; goto done; } IMA_ADPCM_encoded = 1; break; default: SDL_SetError("Unknown WAVE data format: %f", SDL_SwapLE16(format->encoding)); was_error = 1; goto done; } memset(spec, 0, (sizeof *spec)); spec->freq = SDL_SwapLE32(format->frequency); switch (SDL_SwapLE16(format->bitspersample)) { case 4: if ( MS_ADPCM_encoded || IMA_ADPCM_encoded ) { spec->format = AUDIO_S16; } else { was_error = 1; } break; case 8: spec->format = AUDIO_U8; break; case 16: spec->format = AUDIO_S16; break; default: was_error = 1; break; } if ( was_error ) { SDL_SetError("Unknown %d-bit PCM data format", SDL_SwapLE16(format->bitspersample)); goto done; } spec->channels = (Uint8)SDL_SwapLE16(format->channels); spec->samples = 4096; /* Good default buffer size */ /* Read the audio data chunk */ *audio_buf = NULL; do { if ( *audio_buf != NULL ) { free(*audio_buf); } lenread = ReadChunk(src, &chunk); if ( lenread < 0 ) { was_error = 1; goto done; } *audio_len = lenread; *audio_buf = chunk.data; } while ( chunk.magic != DATA ); } else { /* Read the audio data format chunk */ chunk.data = NULL; do { if ( chunk.data != NULL ) { free(chunk.data); } lenread = ReadChunkBe(src, &chunk); if ( lenread < 0 ) { was_error = 1; goto done; } } while ( (chunk.magic == FACT) || (chunk.magic == LIST) ); /* Decode the audio data format */ format = (WaveFMT *)chunk.data; if ( chunk.magic != FMT ) { SDL_SetError("Complex WAVE files not supported"); was_error = 1; goto done; } MS_ADPCM_encoded = IMA_ADPCM_encoded = 0; switch (SDL_SwapBE16(format->encoding)) { case PCM_CODE: /* We can understand this */ break; case MS_ADPCM_CODE: /* Try to understand this */ if ( InitMS_ADPCM(format) < 0 ) { was_error = 1; goto done; } MS_ADPCM_encoded = 1; break; case IMA_ADPCM_CODE: /* Try to understand this */ if ( InitIMA_ADPCM(format) < 0 ) { was_error = 1; goto done; } IMA_ADPCM_encoded = 1; break; default: SDL_SetError("Unknown WAVE data format: 0x%.4x", SDL_SwapBE16(format->encoding)); was_error = 1; goto done; } memset(spec, 0, (sizeof *spec)); spec->freq = SDL_SwapBE32(format->frequency); switch (SDL_SwapBE16(format->bitspersample)) { case 4: if ( MS_ADPCM_encoded || IMA_ADPCM_encoded ) { spec->format = AUDIO_S16; } else { was_error = 1; } break; case 8: spec->format = AUDIO_U8; break; case 16: spec->format = AUDIO_S16; break; default: was_error = 1; break; } if ( was_error ) { SDL_SetError("Unknown %d-bit PCM data format", SDL_SwapBE16(format->bitspersample)); goto done; } spec->channels = (Uint8)SDL_SwapBE16(format->channels); spec->samples = 4096; /* Good default buffer size */ /* Read the audio data chunk */ *audio_buf = NULL; do { if ( *audio_buf != NULL ) { free(*audio_buf); } lenread = ReadChunkBe(src, &chunk); if ( lenread < 0 ) { was_error = 1; goto done; } *audio_len = lenread; *audio_buf = chunk.data; } while ( chunk.magic != DATA ); } if ( MS_ADPCM_encoded ) { if ( MS_ADPCM_decode(audio_buf, audio_len) < 0 ) { was_error = 1; goto done; } } if ( IMA_ADPCM_encoded ) { if ( IMA_ADPCM_decode(audio_buf, audio_len) < 0 ) { was_error = 1; goto done; } } /* Don't return a buffer that isn't a multiple of samplesize */ samplesize = ((spec->format & 0xFF)/8)*spec->channels; *audio_len &= ~(samplesize-1); done: if ( format != NULL ) { free(format); } if ( freesrc && src ) { SDL_RWclose(src); } if ( was_error ) { spec = NULL; } return(spec); }