Exemplo n.º 1
0
void cued_rip_data_track(rip_context_t *rip)
{
    uint8_t buf[CDIO_CD_FRAMESIZE];
    FILE *dataFile;
    lsn_t currSector;
    driver_return_code_t drc;

    if (ripExtract) {

        // does not return on error
        (void) format_get_file_path(rip->cdObj, rip->cddbObj,
            rip->fileNamePattern, ".data", rip->currentTrack,
            rip->fileNameBuffer, rip->bufferSize
            );

        dataFile = fopen2(rip->fileNameBuffer, O_WRONLY | O_CREAT | O_EXCL | O_APPEND, 0666);
        if (!dataFile) {
            cdio2_unix_error("fopen2", rip->fileNameBuffer, 0);
            cdio_error("skipping extraction of data track %02d", rip->currentTrack);
            return;
        }

        for (currSector = rip->firstSector;  currSector <= rip->lastSector;  ++currSector) {
            drc = cdio_read_data_sectors(rip->cdObj, buf, currSector, CDIO_CD_FRAMESIZE, 1);
            if (DRIVER_OP_SUCCESS == drc) {
                if (1 != fwrite(buf, sizeof(buf), 1, dataFile)) {
                    // probably out of disk space, which is bad, because most things rely on it
                    cdio2_unix_error("fwrite", rip->fileNameBuffer, 0);
                    cdio2_abort("failed to write to file \"%s\"", rip->fileNameBuffer);
                }
            } else {
                cdio2_driver_error(drc, "read of data sector");
                cdio_error("error reading sector %d; skipping extraction of data track %02d", currSector, rip->currentTrack);
                //if (unlink(rip->fileNameBuffer)) {
                //    cdio2_unix_error("unlink", rip->fileNameBuffer, 1);
                //}
                break;
            }
        }

        fclose(dataFile);
    }
}
Exemplo n.º 2
0
u8 open_font(unsigned int idx)
{
    char font[20];
    close_font();
    if (! idx) {
        cur_str.font.idx = 0;
        return 1;
    }
    sprintf(font, "media/%s.fon", FontNames[idx-1]);
    finit(&FontFAT, "media");
    cur_str.font.fh = fopen2(&FontFAT, font, "rb");
    if (! cur_str.font.fh) {
        printf("Couldn't open font file: %s\n", font);
        return 0;
    }
    setbuf(cur_str.font.fh, 0);
    if(fread(&cur_str.font.height, 1, 1, cur_str.font.fh) != 1) {
        printf("Failed to read height from font\n");
        fclose(cur_str.font.fh);
        cur_str.font.fh = NULL;
        return 0;
    }
    cur_str.font.idx = idx;
    idx = 0;
    u8 *f = (u8 *)font;
    while(1) {
        if (fread(f, 4, 1, cur_str.font.fh) != 1) {
            printf("Failed to parse font range table\n");
            fclose(cur_str.font.fh);
            cur_str.font.fh = NULL;
            return 0;
        }
        u16 start_c = f[0] | (f[1] << 8);
        u16 end_c = f[2] | (f[3] << 8);
        cur_str.font.range[idx++] = start_c;
        cur_str.font.range[idx++] = end_c;
        if (start_c == 0 && end_c == 0)
            break;
    }
    return 1;
}
Exemplo n.º 3
0
int write_file(const struct byte_array* path, struct byte_array* bytes, uint32_t from, int32_t timestamp) {
    int result = -1;
    const char *path2 = byte_array_to_string(path);

    FILE *file = fopen2(path2);
    if (NULL == file)
        goto done;

    if (fseek(file, from, SEEK_SET)) {
        perror("fseek");
        goto done;
    }
    
    // write bytes
    if (NULL != bytes) {
        int r = (int)fwrite(bytes->data, 1, bytes->length, file);
        int s = fclose(file);
        result = (r<0) || s;
    }
    
done:
    return result;
}
Exemplo n.º 4
0
void PROTOCOL_Load(int no_dlg)
{
    (void)no_dlg;
#ifdef MODULAR
    if(! PROTOCOL_HasModule(Model.protocol)) {
        *loaded_protocol = 0;
        return;
    }
    if(*loaded_protocol == Model.protocol)
        return;
    char file[25];
    strcpy(file, "protocol/");
    #define PROTODEF(proto, module, map, cmd, name) case proto: strcat(file,name); break;
    switch(Model.protocol) {
        #include "protocol.h"
        default: *loaded_protocol = 0; return;
    }
    #undef PROTODEF
    file[17] = '\0'; //truncate filename to 8 characters
    strcat(file, ".mod");
    FILE *fh;
    //We close the current font because on the dveo8 we reuse
    //the font filehandle to read the protocol.
    //Thatis necessary because we need to be able to load the
    //protocol while an ini file is open, and we don't want to
    //waste the RAM for an extra filehandle
    u8 old_font = LCD_SetFont(0);
    finit(&FontFAT, ""); //In case no fonts are loaded yet
    fh = fopen2(&FontFAT, file, "r");
    //printf("Loading %s: %08lx\n", file, fh);
    if(! fh) {
        if(! no_dlg) {
            sprintf(tempstring, "Misisng protocol:\n%s", file);
            PAGE_ShowWarning(NULL, tempstring);
        }
        LCD_SetFont(old_font);
        return;
    }
    setbuf(fh, 0);
    int size = 0;
    unsigned char buf[256];
    int len;
    char *ptr = (char *)loaded_protocol;
    while(size < 4096) {
        len = fread(buf, 1, 256, fh);
        if(len) {
            memcpy(ptr, buf, len);
            ptr += len;
        }
        size += len;
        if (len != 256)
            break;
    }
    fclose(fh);
    LCD_SetFont(old_font);
    if ((unsigned long)&_data_loadaddr != *loaded_protocol) {
        if(! no_dlg) {
            sprintf(tempstring, "Protocol Mismatch:\n%08x\n%08x", (unsigned long)&_data_loadaddr, *loaded_protocol);
            PAGE_ShowWarning(NULL, tempstring);
        }
        *loaded_protocol = 0;
        return;
    }
    //printf("Updated %d (%d) bytes: Data: %08lx %08lx %08lx\n", size, len, *loaded_protocol, *(loaded_protocol+1), *(loaded_protocol+2));
    //We use the same file for multiple protocols, so we need to manually set this here
    *loaded_protocol = Model.protocol;
#else
    if(! PROTOCOL_HasModule(Model.protocol)) {
        PROTO_Cmds = NULL;
        printf("Module is not defined!\n");
        return;
    }
    #define PROTODEF(proto, module, map, cmd, name) case proto: PROTO_Cmds = cmd; break;
    switch(Model.protocol) {
        #include "protocol.h"
        default: PROTO_Cmds = NULL;
    }
    #undef PROTODEF
#endif
    PROTOCOL_SetSwitch(get_module(Model.protocol));
}
Exemplo n.º 5
0
void PROTOCOL_Load(int no_dlg)
{
    (void)no_dlg;
#ifdef ENABLE_MODULAR
    FATFS ModuleFAT;
    FILE *fh;

    if(! PROTOCOL_HasModule(Model.protocol)) {
        *loaded_protocol = 0;
        return;
    }
    if(*loaded_protocol == Model.protocol)
        return;
    char file[25];
    strcpy(file, "protocol/");

    if (Model.protocol > PROTOCOL_COUNT)
    {
        *loaded_protocol = 0;
        return;
    }
    else
    {
        strcat(file, Protocols[Model.protocol].name);
    }
    file[17] = '\0'; //truncate filename to 8 characters
    strcat(file, ".mod");

    memset(&ModuleFAT, 0, sizeof(ModuleFAT));
    finit(&ModuleFAT, "protocol");
    fh = fopen2(&ModuleFAT, file, "r");
    //printf("Loading %s: %08lx\n", file, fh);
    if(! fh) {
        if(! no_dlg) {
            sprintf(tempstring, "Misisng protocol:\n%s", file);
            PAGE_ShowWarning(NULL, tempstring);
        }
        return;
    }
    setbuf(fh, 0);
    fread(loaded_protocol, 1, 4 * 1024, fh);
    fclose(fh);
    if ((unsigned long)&_data_loadaddr != *loaded_protocol) {
        if(! no_dlg) {
            sprintf(tempstring, "Protocol Mismatch:\n%08x\n%08x", (unsigned long)&_data_loadaddr, *loaded_protocol);
            PAGE_ShowWarning(NULL, tempstring);
        }
        *loaded_protocol = 0;
        return;
    }
    //printf("Updated %d (%d) bytes: Data: %08lx %08lx %08lx\n", size, len, *loaded_protocol, *(loaded_protocol+1), *(loaded_protocol+2));
    //We use the same file for multiple protocols, so we need to manually set this here
    *loaded_protocol = Model.protocol;
#else
    if(! PROTOCOL_HasModule(Model.protocol)) {
        PROTO_Cmds = NULL;
        printf("Module is not defined!\n");
        return;
    }
    PROTO_Cmds = Protocols[Model.protocol].cmd;
#endif
    PROTOCOL_SetSwitch(get_module(Model.protocol));
    if (PROTOCOL_GetTelemetryState() != PROTO_TELEM_UNSUPPORTED) {
        memset(&Telemetry, 0, sizeof(Telemetry));
        TELEMETRY_SetType(PROTOCOL_GetTelemetryType());
    }

    CurrentProtocolChannelMap = PROTOCOL_GetChannelMap();
}
Exemplo n.º 6
0
//-------------------------------------------------------------------------------------------------
bool KeyValues::ParseKVFile( const char *file ) {
	char *contents;
	
	FILE *f = fopen2( file, "rb" );
	if( !f ) return false;
	fseek( f, 0, SEEK_END );
	int length = ftell(f);
	fseek( f, 0, SEEK_SET );
	contents = new char[length+2];
	fread( contents, 1, length, f );
	fclose(f);

	contents[length] = ' '; // add extra whitespace
	contents[length+1] = 0;

	// replace whitespace
	for( int i = 0; contents[i]; i++ ) {
		if( contents[i] > 0 && contents[i] < 32 ) contents[i] = ' ';
	}

	char *read = contents;

	int state = 0;
	// 0 = key
	// 1 = value
	

	while( true ) {
		// skip whitespace
		while( (*read) && (*read) == ' ' ) {
			read++;
		}

		char term[256];
		int write = 0;

		bool quoted;
		quoted=false;

		// term types:
		// a {quoted} string
		// the character '{'
		// the character '}'

		int termtype = 0;

		if( (*read) == '{' ) {
			read++;
			termtype = 1;
			goto foundterm;
		}
		if( (*read) == '}' ) {
			read++;
			termtype = 2;
			goto foundterm;
		}
		
		while(true) {
			// first letter
			if( (*read) == 0 ) break;

			if( (*read) == '"' ) {
				if( read[1] == '"' ) {
					term[write++] = '"';
					if( write == 256 ) return false;
					
					read+= 2;
					continue;
				}
				quoted = !quoted;
				read++;
				continue;
			}
			
			if( quoted ) {
				term[write++] = *read++;
				if( write == 256 ) return false;
				continue;
			} else {
				if( (*read) == ' ' ) break;
				if( (*read) == 0 ) break;
				
			}
			if( (*read) == ' ' ) break;
			if( (*read) == '{' ) break;
			if( (*read) == '}' ) break;

			term[write++] = *read++;
			if( write == 256 ) return false;
			
		}

foundterm:

		term[write] = 0;


		if( state == 0 ) {
			if(termtype == 1 ) {
				// not expecting opening brace
				return false;
			} else if( termtype == 2 ) {
				// closing brace: terminate current section
				if( !Exit() ) {
					return false; // parsing error (closing brace at top level)
				}
				continue;
			}
			if( Util::StrEmpty(term) ) {
				return true;
			}
			// add new keyvalue
			KeyValue kv;
			Util::CopyString( kv.name, term );
			current_section->values.push_back( kv );
			current_index = (int)current_section->values.size()-1;

			state = 1;
		} else if( state == 1 ) {
			if( termtype == 1 ) {

				Enter( true );
				state = 0;
				continue;
			} else if( termtype == 2 ) {
				// not expecting closing brace
				return false;
			}

			// normal key value
			Util::CopyString( current_section->values[current_index].value, term );
			state = 0;
		}
	}
	
	if( current_level != 0 || state != 0 ) {
		return false;
	}

	Rewind();
	return true;
}
Exemplo n.º 7
0
static void cued_rip_prologue(rip_context_t *rip)
{
    rip->mmcBuf = NULL;
    rip->allocatedSectors = 0;

#ifdef CUED_HAVE_PARANOIA

    if (ripUseParanoia) {
        char *msg = 0;
        int rc;

        // N.B. this behavior does not match documentation:
        // the 0 here appears to prevent the message "Checking <filename> for cdrom..."
        rip->paranoiaCtlObj = cdio_cddap_identify_cdio(rip->cdObj, 0, &msg);
        if (rip->paranoiaCtlObj) {

            if (msg) {
                cdio_warn("identify returned paranoia message(s) \"%s\"", msg);
            }
            cdio_cddap_verbose_set(rip->paranoiaCtlObj, CDDA_MESSAGE_LOGIT, CDDA_MESSAGE_LOGIT);

            rc = cdio_cddap_open(rip->paranoiaCtlObj);
            cdio2_paranoia_msg(rip->paranoiaCtlObj, "open of device");
            if (!rc) {
                rip->paranoiaRipObj = cdio_paranoia_init(rip->paranoiaCtlObj);
                cdio2_paranoia_msg(rip->paranoiaCtlObj, "initialization of paranoia");
                if (!rip->paranoiaRipObj) {
                    cdio2_abort("out of memory initializing paranoia");
                }

                cdio_paranoia_modeset(rip->paranoiaRipObj, PARANOIA_MODE_FULL ^ PARANOIA_MODE_NEVERSKIP);
                // N.B. not needed at the moment
                cdio2_paranoia_msg(rip->paranoiaCtlObj, "setting of paranoia mode");

                rip->save_read_paranoid = rip->paranoiaCtlObj->read_audio;
                rip->paranoiaCtlObj->read_audio = cued_read_paranoid;
            } else {
                cdio_cddap_close_no_free_cdio(rip->paranoiaCtlObj);

                cdio_error("disabling paranoia");
                CLRF(RIP_F_USE_PARANOIA, rip->flags);
            }
        } else {
            cdio_error("disabling paranoia due to the following message(s):\n%s", msg);
            CLRF(RIP_F_USE_PARANOIA, rip->flags);
        }
    }

#endif // CUED_HAVE_PARANOIA

    if (rip->qSubChannelFileName) {
        if (!strcmp("-", rip->qSubChannelFileName)) {
            rip->qSubChannelFile = stdout;
        } else {
            (void) format_get_file_path(rip->cdObj, rip->cddbObj, rip->qSubChannelFileName, "", 0, rip->fileNameBuffer, rip->bufferSize);

            // replaced O_EXCL with O_TRUNC to allow using /dev/null for testing
            rip->qSubChannelFile = fopen2(rip->fileNameBuffer, O_WRONLY | O_CREAT | O_TRUNC | O_APPEND, 0666);
            if (!rip->qSubChannelFile) {
                cdio2_unix_error("fopen2", rip->fileNameBuffer, 0);
                cdio_error("not creating sub-channel file \"%s\"", rip->fileNameBuffer);

                rip->qSubChannelFileName = 0;
            }
        }
    }

    rip->endOfDiscSector = cdio_get_disc_last_lsn(rip->cdObj);
    if (CDIO_INVALID_LSN == rip->endOfDiscSector) {
        cdio2_abort("failed to get last sector number");
    } else {
        //cdio_debug("end of disc sector is %d", rip->endOfDiscSector);
    }
}