/* * Gets the current working directory * * req: TLV_TYPE_DIRECTORY_PATH - The directory path to change the working * directory to. */ DWORD request_fs_getwd(Remote * remote, Packet * packet) { Packet *response = packet_create_response(packet); char *directory = NULL; DWORD result; result = fs_getwd(&directory); if (directory != NULL) { packet_add_tlv_string(response, TLV_TYPE_DIRECTORY_PATH, directory); free(directory); } return packet_transmit_response(result, remote, response); }
/* * Gets the current working directory * * req: TLV_TYPE_DIRECTORY_PATH - The directory path to change the working * directory to. */ DWORD request_fs_getwd(Remote * remote, Packet * packet) { Packet *response = packet_create_response(packet); char *directory = NULL; DWORD result; result = fs_getwd(&directory); if (directory != NULL) { packet_add_tlv_string(response, TLV_TYPE_DIRECTORY_PATH, directory); free(directory); } packet_add_tlv_uint(response, TLV_TYPE_RESULT, result); return PACKET_TRANSMIT(remote, response, NULL); }
/* Open an MPEG stream and prepare for decode */ static int libmpg123_init(const char *fn) { int err; uint32 fd; /* Open the file */ #ifdef BS_SIZE mp3_fd = fd = fs_open(fn, O_RDONLY); #else fd = fs_open(fn, O_RDONLY); #endif if (fd < 0) { printf("Can't open input file %s\r\n", fn); printf("getwd() returns '%s'\r\n", fs_getwd()); return -1; } #ifndef BS_SIZE fs_close(fd); #endif if (fn != mp3_last_fn) { if (fn[0] != '/') { strcpy(mp3_last_fn, fs_getwd()); strcat(mp3_last_fn, "/"); strcat(mp3_last_fn, fn); } else { strcpy(mp3_last_fn, fn); } } /* Allocate buffers */ if (pcm_buffer == NULL) pcm_buffer = malloc(PCM_SIZE); pcm_ptr = pcm_buffer; pcm_count = pcm_discard = 0; #ifdef BS_SIZE if (bs_buffer == NULL) bs_buffer = malloc(BS_SIZE); bs_ptr = bs_buffer; bs_count = 0; /* Fill bitstream buffer */ if (bs_fill() < 0) { printf("Can't read file header\r\n"); goto errorout; } /* Are we looking at a RIFF file? (stupid Windows encoders) */ if (bs_ptr[0] == 'R' && bs_ptr[1] == 'I' && bs_ptr[2] == 'F' && bs_ptr[3] == 'F') { /* Found a RIFF header, scan through it until we find the data section */ printf("Skipping stupid RIFF header\r\n"); while (bs_ptr[0] != 'd' || bs_ptr[1] != 'a' || bs_ptr[2] != 't' || bs_ptr[3] != 'a') { bs_ptr++; if (bs_ptr >= (bs_buffer + BS_SIZE)) { printf("Indeterminately long RIFF header\r\n"); goto errorout; } } /* Skip 'data' and length */ bs_ptr += 8; bs_count -= (bs_ptr - bs_buffer); printf("Final index is %d\r\n", (bs_ptr - bs_buffer)); } if (((uint8)bs_ptr[0] != 0xff) && (!((uint8)bs_ptr[1] & 0xe0))) { printf("Definitely not an MPEG file\r\n"); goto errorout; } #endif mpg123_init(); mh = mpg123_new(NULL, &err); if(mh == NULL) { printf("Can't init mpg123: %s\n", mpg123_strerror(mh)); goto errorout; } /* Open the MP3 context in open_fd mode */ #ifdef BS_SIZE err = mpg123_open_fd(mh, mp3_fd); #else err = mpg123_open(mh, fn); #endif if(err != MPG123_OK) { printf("Can't open mpg123\n"); mpg123_exit(); goto errorout; } int enc; mpg123_getformat(mh, &rate, &channels, &enc); mpg123_info(mh, &decinfo); printf("Output Sampling rate = %ld\r\n", decinfo.rate); printf("Output Bitrate = %d\r\n", decinfo.bitrate); printf("Output Frame size = %d\r\n", decinfo.framesize); printf("mpg123 initialized successfully\r\n"); return 0; errorout: printf("Exiting on error\r\n"); #ifdef BS_SIZE if (bs_buffer) { free(bs_buffer); bs_buffer = NULL; } #endif if (pcm_buffer) { free(pcm_buffer); pcm_buffer = NULL; } #ifdef BS_SIZE fs_close(fd); mp3_fd = 0; #endif return -1; }
/* Open an MPEG stream and prepare for decode */ static int mpglib_init(const char *fn) { uint32 fd; int size; /* Open the file */ mp3_fd = fd = fs_open(fn, O_RDONLY); if (fd < 0) { printf("Can't open input file %s\r\n", fn); printf("getwd() returns '%s'\r\n", fs_getwd()); return -1; } if (fn != mp3_last_fn) { if (fn[0] != '/') { strcpy(mp3_last_fn, fs_getwd()); strcat(mp3_last_fn, "/"); strcat(mp3_last_fn, fn); } else { strcpy(mp3_last_fn, fn); } } /* Allocate buffers */ if (bs_buffer == NULL) bs_buffer = malloc(BS_SIZE); bs_ptr = bs_buffer; bs_count = 0; if (pcm_buffer == NULL) pcm_buffer = malloc(PCM_SIZE); pcm_ptr = pcm_buffer; pcm_count = pcm_discard = 0; /* Fill bitstream buffer */ if (bs_fill() < 0) { printf("Can't read file header\r\n"); goto errorout; } /* Are we looking at a RIFF file? (stupid Windows encoders) */ if (bs_ptr[0] == 'R' && bs_ptr[1] == 'I' && bs_ptr[2] == 'F' && bs_ptr[3] == 'F') { /* Found a RIFF header, scan through it until we find the data section */ printf("Skipping stupid RIFF header\r\n"); while (bs_ptr[0] != 'd' || bs_ptr[1] != 'a' || bs_ptr[2] != 't' || bs_ptr[3] != 'a') { bs_ptr++; if (bs_ptr >= (bs_buffer + BS_SIZE)) { printf("Indeterminately long RIFF header\r\n"); goto errorout; } } /* Skip 'data' and length */ bs_ptr += 8; bs_count -= (bs_ptr - bs_buffer); printf("Final index is %d\r\n", (bs_ptr - bs_buffer)); } if (((uint8)bs_ptr[0] != 0xff) && (!((uint8)bs_ptr[1] & 0xe0))) { printf("Definitely not an MPEG file\r\n"); goto errorout; } /* Initialize MPEG engines */ InitMP3(&mp); /* Decode the first frame */ printf("decoding first frame:\n"); decodeMP3(&mp, bs_buffer, BS_SIZE, pcm_ptr, PCM_WATER - pcm_count, &size); printf("decoded size was %d\n", size); pcm_ptr += size; pcm_count += size; printf("mpglib initialized successfully\r\n"); return 0; errorout: printf("Exiting on error\r\n"); if (bs_buffer) { free(bs_buffer); bs_buffer = NULL; } if (pcm_buffer) { free(pcm_buffer); pcm_buffer = NULL; } fs_close(fd); mp3_fd = 0; return -1; }
/* Open an MPEG stream and prepare for decode */ static int xing_init(const char *fn) { uint32 fd; /* Open the file */ mp3_fd = fd = fs_open(fn, O_RDONLY); if (fd < 0) { printf("Can't open input file %s\r\n", fn); printf("getwd() returns '%s'\r\n", fs_getwd()); return -1; } if (fn != mp3_last_fn) { if (fn[0] != '/') { strcpy(mp3_last_fn, fs_getwd()); strcat(mp3_last_fn, "/"); strcat(mp3_last_fn, fn); } else { strcpy(mp3_last_fn, fn); } } /* Allocate buffers */ if (bs_buffer == NULL) bs_buffer = malloc(BS_SIZE); bs_ptr = bs_buffer; bs_count = 0; if (pcm_buffer == NULL) pcm_buffer = malloc(PCM_SIZE); pcm_ptr = pcm_buffer; pcm_count = pcm_discard = 0; /* Fill bitstream buffer */ if (bs_fill() < 0) { printf("Can't read file header\r\n"); goto errorout; } /* Are we looking at a RIFF file? (stupid Windows encoders) */ if (bs_ptr[0] == 'R' && bs_ptr[1] == 'I' && bs_ptr[2] == 'F' && bs_ptr[3] == 'F') { /* Found a RIFF header, scan through it until we find the data section */ printf("Skipping stupid RIFF header\r\n"); while (bs_ptr[0] != 'd' || bs_ptr[1] != 'a' || bs_ptr[2] != 't' || bs_ptr[3] != 'a') { bs_ptr++; if (bs_ptr >= (bs_buffer + BS_SIZE)) { printf("Indeterminately long RIFF header\r\n"); goto errorout; } } /* Skip 'data' and length */ bs_ptr += 8; bs_count -= (bs_ptr - bs_buffer); printf("Final index is %d\r\n", (bs_ptr - bs_buffer)); } if (((uint8)bs_ptr[0] != 0xff) && (!((uint8)bs_ptr[1] & 0xe0))) { printf("Definitely not an MPEG file\r\n"); goto errorout; } /* Initialize MPEG engines */ mpeg_init(&mpeg); mpeg_eq_init(&mpeg); /* Parse MPEG header */ frame_bytes = head_info2(bs_ptr, bs_count, &head, &bitrate); if (frame_bytes == 0) { printf("Bad or unsupported MPEG file\r\n"); goto errorout; } /* Print out some info about it */ { static char *layers[] = { "invalid", "3", "2", "1" }; static char *modes[] = { "stereo", "joint-stereo", "dual", "mono" }; static int srs[] = { 22050, 24000, 16000, 1, 44100, 48000, 32000, 1 }; printf("Opened stream %s for decoding:\r\n", fn); printf(" %dKbps Layer %s %s at %dHz\r\n", bitrate/1000, layers[head.option], modes[head.mode], srs[4*head.id + head.sr_index]); } /* Initialize audio decoder */ if (!audio_decode_init(&mpeg, &head, frame_bytes, REDUCT_NORMAL, 0, CONV_NORMAL, 44100)) { printf("Failed to initialize decoder\r\n"); goto errorout; } audio_decode_info(&mpeg, &decinfo); printf("Output Sampling rate = %ld\r\n", decinfo.samprate); printf("Output Channels = %d\r\n", decinfo.channels); printf("Output Bits = %d\r\n", decinfo.bits); printf("Output Type = %d\r\n", decinfo.type); /* if (decinfo.samprate != 44100) { printf("Currently cannot process %ld sampling rate\r\n", decinfo.samprate); goto errorout; } */ printf("XingMP3 initialized successfully\r\n"); return 0; errorout: printf("Exiting on error\r\n"); if (bs_buffer) { free(bs_buffer); bs_buffer = NULL; } if (pcm_buffer) { free(pcm_buffer); pcm_buffer = NULL; } fs_close(fd); mp3_fd = 0; return -1; }
/* * char *realpath(const char *path, char resolved[PATH_MAX]); * * Find the real name of path, by removing all ".", ".." and symlink * components. Returns (resolved) on success, or (NULL) on failure, * in which case the path which caused trouble is left in (resolved). */ char * realpath(const char *path, char resolved[PATH_MAX]) { char *p, *q, *s; size_t left_len, resolved_len; char left[PATH_MAX], next_token[PATH_MAX]; if(path[0] == '/') { resolved[0] = '/'; resolved[1] = '\0'; if(path[1] == '\0') return (resolved); resolved_len = 1; left_len = strlcpy(left, path + 1, sizeof(left)); } else { /* if (getcwd(resolved, PATH_MAX) == NULL) { strlcpy(resolved, ".", PATH_MAX); return (NULL); } */ strcpy(resolved, fs_getwd()); resolved_len = strlen(resolved); left_len = strlcpy(left, path, sizeof(left)); } if(left_len >= sizeof(left) || resolved_len >= PATH_MAX) { errno = ENAMETOOLONG; return (NULL); } /* * Iterate over path components in `left'. */ while(left_len != 0) { /* * Extract the next path component and adjust `left' * and its length. */ p = strchr(left, '/'); s = p ? p : left + left_len; if((unsigned)(s - left) >= sizeof(next_token)) { errno = ENAMETOOLONG; return (NULL); } memcpy(next_token, left, s - left); next_token[s - left] = '\0'; left_len -= s - left; if(p != NULL) memmove(left, s + 1, left_len + 1); if(resolved[resolved_len - 1] != '/') { if(resolved_len + 1 >= PATH_MAX) { errno = ENAMETOOLONG; return (NULL); } resolved[resolved_len++] = '/'; resolved[resolved_len] = '\0'; } if(next_token[0] == '\0') continue; else if(strcmp(next_token, ".") == 0) continue; else if(strcmp(next_token, "..") == 0) { /* * Strip the last path component except when we have * single "/" */ if(resolved_len > 1) { resolved[resolved_len - 1] = '\0'; q = strrchr(resolved, '/') + 1; *q = '\0'; resolved_len = q - resolved; } continue; } /* * Append the next path component and lstat() it. If * lstat() fails we still can return successfully if * there are no more path components left. */ resolved_len = strlcat(resolved, next_token, PATH_MAX); if(resolved_len >= PATH_MAX) { errno = ENAMETOOLONG; return (NULL); } } /* * Remove trailing slash except when the resolved pathname * is a single "/". */ if(resolved_len > 1 && resolved[resolved_len - 1] == '/') resolved[resolved_len - 1] = '\0'; return (resolved); }