int main(){ char receive_buffer[80] = "12345 shangshandalaohu"; int crc; crc = getcrc(receive_buffer); char* rest; printf("receive_buffer is %s\n",receive_buffer); rest = getrest(receive_buffer); printf("crc is %d\n",crc); printf("rest is %s\n",rest); }
int decodeMPEG(struct AUDIO_HEADER *header) { int cnt, g, snd_eof; /* * decoder loop ********************************** */ snd_eof=0; cnt=0; while (!snd_eof) { while (!snd_eof && ready_audio()) { if ((g=gethdr(header))!=0) { report_header_error(g); snd_eof=1; break; } if (header->protection_bit==0) getcrc(); #if 0 statusDisplay(header,cnt); #endif if (header->layer==1) { if (layer3_frame(header,cnt)) { yell(" error. blip."); return -1; } } else if (header->layer==2) { if (layer2_frame(header,cnt)) { yell(" error. blip."); return -1; } } cnt++; } } return 0; }
int read_bin(char *in, char *outf) { uint16_t crc; FILE *fp, *out; int i,j; fp=fopen(in, "r"); if (!fp) { printf("Error reading file %s\n", in); return -1; } out=fopen(outf, "w"); if (!out) { printf("Error writing file %s\n", outf); return -1; } if (!fread(&header, sizeof(header), 1, fp)) goto err; fprintf(out, "# ---------- Dump generated by eepdump handling format version 0x%02x ----------\n#\n", FORMAT_VERSION); if (FORMAT_VERSION!=header.ver) fprintf(out, "# WARNING: format version mismatch!!!\n"); fprintf(out, "# --Header--\n# signature=0x%08x\n# version=0x%02x\n# reserved=%u\n# numatoms=%u\n# eeplen=%u\n# ----------\n\n\n", header.signature, header.ver, header.res, header.numatoms, header.eeplen); for (i = 0; i<header.numatoms; i++) { if (!fread(&atom, ATOM_SIZE-CRC_SIZE, 1, fp)) goto err; printf("Reading atom %d...\n", i); fprintf(out, "# Start of atom #%u of type 0x%04x and length %u\n", atom.count, atom.type, atom.dlen); if (atom.count != i) { printf("Error: atom count mismatch\n"); fprintf(out, "# Error: atom count mismatch\n"); } long pos = ftell(fp); char *atom_data = (char *) malloc(atom.dlen + ATOM_SIZE-CRC_SIZE); memcpy(atom_data, &atom, ATOM_SIZE-CRC_SIZE); if (!fread(atom_data+ATOM_SIZE-CRC_SIZE, atom.dlen, 1, fp)) goto err; uint16_t calc_crc = getcrc(atom_data, atom.dlen-CRC_SIZE+ATOM_SIZE-CRC_SIZE); fseek(fp, pos, SEEK_SET); if (atom.type==ATOM_VENDOR_TYPE) { //decode vendor info if (!fread(&vinf, VENDOR_SIZE, 1, fp)) goto err; fprintf(out, "# Vendor info\n"); fprintf(out, "product_uuid %08x-%04x-%04x-%04x-%04x%08x\n", vinf.serial_4, vinf.serial_3>>16, vinf.serial_3 & 0xffff, vinf.serial_2>>16, vinf.serial_2 & 0xffff, vinf.serial_1); fprintf(out, "product_id 0x%04x\n", vinf.pid); fprintf(out, "product_ver 0x%04x\n", vinf.pver); vinf.vstr = (char *) malloc(vinf.vslen+1); vinf.pstr = (char *) malloc(vinf.pslen+1); if (!fread(vinf.vstr, vinf.vslen, 1, fp)) goto err; if (!fread(vinf.pstr, vinf.pslen, 1, fp)) goto err; //close strings vinf.vstr[vinf.vslen] = 0; vinf.pstr[vinf.pslen] = 0; fprintf(out, "vendor \"%s\" # length=%u\n", vinf.vstr, vinf.vslen); fprintf(out, "product \"%s\" # length=%u\n", vinf.pstr, vinf.pslen); if (!fread(&crc, CRC_SIZE, 1, fp)) goto err; } else if (atom.type==ATOM_GPIO_TYPE) { //decode GPIO map if (!fread(&gpiomap, GPIO_SIZE, 1, fp)) goto err; fprintf(out, "# GPIO map info\n"); fprintf(out, "gpio_drive %d\n", gpiomap.flags & 15); //1111 fprintf(out, "gpio_slew %d\n", (gpiomap.flags & 48)>>4); //110000 fprintf(out, "gpio_hyteresis %d\n", (gpiomap.flags & 192)>>6); //11000000 fprintf(out, "back_power %d\n", gpiomap.power); fprintf(out, "# GPIO FUNCTION PULL\n# ---- -------- ----\n"); for (j = 0; j<28; j++) { if (gpiomap.pins[j] & (1<<7)) { //board uses this pin char *pull_str = "INVALID"; switch ((gpiomap.pins[j] & 96)>>5) { //1100000 case 0: pull_str = "DEFAULT"; break; case 1: pull_str = "UP"; break; case 2: pull_str = "DOWN"; break; case 3: pull_str = "NONE"; break; } char *func_str = "INVALID"; switch ((gpiomap.pins[j] & 7)) { //111 case 0: func_str = "INPUT"; break; case 1: func_str = "OUTPUT"; break; case 4: func_str = "ALT0"; break; case 5: func_str = "ALT1"; break; case 6: func_str = "ALT2"; break; case 7: func_str = "ALT3"; break; case 3: func_str = "ALT4"; break; case 2: func_str = "ALT5"; break; } fprintf(out, "setgpio %d %s %s\n", j, func_str, pull_str); } } if (!fread(&crc, CRC_SIZE, 1, fp)) goto err; } else if (atom.type==ATOM_DT_TYPE) {
int write_binary(char* out) { FILE *fp; int i, offset; short crc; fp=fopen(out, "wb"); if (!fp) { printf("Error writing file %s\n", out); return -1; } fwrite(&header, sizeof(header), 1, fp); current_atom = (char *) malloc(vinf_atom.dlen+ATOM_SIZE-CRC_SIZE); offset = 0; //vendor information atom first part memcpy(current_atom, &vinf_atom, ATOM_SIZE-CRC_SIZE); offset += ATOM_SIZE-CRC_SIZE; //data first part memcpy(current_atom+offset, vinf_atom.data, VENDOR_SIZE); offset += VENDOR_SIZE; //data strings memcpy(current_atom+offset, vinf->vstr, vinf->vslen); offset += vinf->vslen; memcpy(current_atom+offset, vinf->pstr, vinf->pslen); offset += vinf->pslen; //vinf last part crc = getcrc(current_atom, offset); memcpy(current_atom+offset, &crc, CRC_SIZE); offset += CRC_SIZE; fwrite(current_atom, offset, 1, fp); free(current_atom); current_atom = (char *) malloc(gpio_atom.dlen+ATOM_SIZE-CRC_SIZE); offset = 0; //GPIO map first part memcpy(current_atom, &gpio_atom, ATOM_SIZE-CRC_SIZE); offset += ATOM_SIZE-CRC_SIZE; //GPIO data memcpy(current_atom+offset, gpiomap, GPIO_SIZE); offset += GPIO_SIZE; //GPIO map last part crc = getcrc(current_atom, offset); memcpy(current_atom+offset, &crc, CRC_SIZE); offset += CRC_SIZE; fwrite(current_atom, offset, 1, fp); free(current_atom); if (has_dt) { printf("Writing out DT...\n"); current_atom = (char *) malloc(dt_atom.dlen+ATOM_SIZE-CRC_SIZE); offset = 0; memcpy(current_atom, &dt_atom, ATOM_SIZE-CRC_SIZE); offset += ATOM_SIZE-CRC_SIZE; memcpy(current_atom+offset, dt_atom.data, dt_atom.dlen-CRC_SIZE); offset += dt_atom.dlen-CRC_SIZE; crc = getcrc(current_atom, offset); memcpy(current_atom+offset, &crc, CRC_SIZE); offset += CRC_SIZE; fwrite(current_atom, offset, 1, fp); free(current_atom); } for (i = 0; i<custom_ct; i++) { custom_atom[i].count-=!has_dt; current_atom = (char *) malloc(custom_atom[i].dlen+ATOM_SIZE-CRC_SIZE); offset = 0; memcpy(current_atom, &custom_atom[i], ATOM_SIZE-CRC_SIZE); offset += ATOM_SIZE-CRC_SIZE; memcpy(current_atom+offset, custom_atom[i].data, custom_atom[i].dlen-CRC_SIZE); offset += custom_atom[i].dlen-CRC_SIZE; crc = getcrc(current_atom, offset); memcpy(current_atom+offset, &crc, CRC_SIZE); offset += CRC_SIZE; fwrite(current_atom, offset, 1, fp); free(current_atom); } fflush(fp); fclose(fp); return 0; }
/* * TODO: add some kind of error reporting here */ void play(char *inFileStr) { char *f; long totalframes = 0; long tseconds = 0; struct AUDIO_HEADER header; int bitrate, fs, g, cnt = 0; while ((f = new_next_arg(inFileStr, &inFileStr))) { if (!f || !*f) return; if ((in_file=fopen(f,"r"))==NULL) { if (!do_hook(MODULE_LIST, "AMP ERROR open %s", f)) put_it("Could not open file: %s\n", f); continue; } filesize = file_size(f); initialise_globals(); if ((g=gethdr(&header))!=0) { report_header_error(g); continue; } if (header.protection_bit==0) getcrc(); if (setup_audio(&header)!=0) { yell("Cannot set up audio. Exiting"); continue; } filesize -= sizeof(header); switch (header.layer) { case 1: { if (layer3_frame(&header,cnt)) { yell(" error. blip."); continue; } break; } case 2: { if (layer2_frame(&header,cnt)) { yell(" error. blip."); continue; } break; } default: continue; } bitrate=t_bitrate[header.ID][3-header.layer][header.bitrate_index]; fs=t_sampling_frequency[header.ID][header.sampling_frequency]; if (header.ID) framesize=144000*bitrate/fs; else framesize=72000*bitrate/fs; totalframes = (filesize / (framesize + 1)) - 1; tseconds = (totalframes * 1152/ t_sampling_frequency[header.ID][header.sampling_frequency]); if (A_AUDIO_PLAY) { char *p = strrchr(f, '/'); if (!p) p = f; else p++; if (!do_hook(MODULE_LIST, "AMP PLAY %lu %lu %s", tseconds, filesize, p)) bitchsay("Playing: %s\n", p); } /* * */ if (!(fseek(in_file, 0, SEEK_END))) { char id3_tag[256]; if (!fseek(in_file, -128, SEEK_END) && (fread(id3_tag,128, 1, in_file) == 1)) { if (!strncmp(id3_tag, "TAG", 3)) print_id3_tag(in_file, id3_tag); } fseek(in_file,0,SEEK_SET); } decodeMPEG(&header); do_hook(MODULE_LIST, "AMP CLOSE %s", f); close_audio(); fclose(in_file); } }
int decodeMPEG_2(int inFilefd) { struct AUDIO_HEADER header; int cnt,g,err=0; TControlMsg message; if ((in_file=fdopen(inFilefd,"r"))==NULL) { return(1); } append=data=nch=0; /* initialize globals */ GUI_STOPPED = FALSE; GUI_PLAYING = TRUE; GUI_FD_TO_PLAY = -1; for (cnt=0;;cnt++) { if ((g=gethdr(&header))!=0) { switch (g) { case GETHDR_ERR: die("error reading mpeg bitstream. exiting.\n"); break; case GETHDR_NS : warn("this is a file in MPEG 2.5 format, which is not defined\n"); warn("by ISO/MPEG. It is \"a special Fraunhofer format\".\n"); warn("amp does not support this format. sorry.\n"); break; case GETHDR_FL1: warn("ISO/MPEG layer 1 is not supported by amp (yet).\n"); break; case GETHDR_FF : warn("free format bitstreams are not supported. sorry.\n"); break; case GETHDR_SYN: warn("oops, we're out of sync.\n"); break; default: } break; } if (!(cnt%10)){ GUIstatusDisplay(cnt); } if(get_msg(&message) >= 0) { int pflag = 0; cnt = parse_msg(&message,&header,cnt); if (GUI_PAUSE) { int flags; pflag = 1; #if 0 /* Set stdin to blocking */ if((flags = fcntl(STDIN_FILENO, F_GETFL, 0)) < 0) perror("fcntl"); flags ^= O_NONBLOCK; if(fcntl(STDIN_FILENO, F_SETFL, flags) < 0) perror("fcntl"); #endif } while(GUI_PAUSE){ if(get_msg(&message) >= 0) cnt = parse_msg(&message,&header,cnt); } if (pflag) { int flags; /* Set stdin to non-blocking */ #if 0 if((flags = fcntl(STDIN_FILENO, F_GETFL, 0)) < 0) perror("fcntl"); flags |= O_NONBLOCK; if(fcntl(STDIN_FILENO, F_SETFL, flags) < 0) perror("fcntl"); #endif } if (GUI_STOP || (GUI_FD_TO_PLAY != -1)){ break; } } /* crc is read just to get out of the way. */ if (header.protection_bit==0) getcrc(); if (!cnt && A_AUDIO_PLAY) { /* setup the audio when we have the frame info */ if (AUDIO_BUFFER_SIZE==0) audioOpen(t_sampling_frequency[header.ID][header.sampling_frequency], (header.mode!=3), A_SET_VOLUME); else audioBufferOpen(t_sampling_frequency[header.ID][header.sampling_frequency],(header.mode!=3),A_SET_VOLUME); } if (layer3_frame(&header,cnt)) { warn(" error. blip.\n"); err=1; break; } } fclose(in_file); if (A_AUDIO_PLAY) if (AUDIO_BUFFER_SIZE!=0) audioBufferClose(); else audioClose(); else fclose(out_file); if (!(GUI_STOP) && (GUI_FD_TO_PLAY == -1)) { /* We've reached the end of the track, notify the jukebox... */ TControlMsg rmsg; rmsg.type = MSG_NEXT; rmsg.data = 0; send_msg(&rmsg, TRUE); } GUI_STOPPED = TRUE; GUI_PLAYING = FALSE; return(err); }