Playing audio files with libao - c

I'm trying to make a simple C program play an AIFF or WAV file. Based on what I see at http://www.xiph.org/ao/doc/, this should work, but instead it makes a buzzing sound no matter what file I feed it. What's wrong with this?
/* compile with "gcc -o playme playme.c -lao -ldl -lm" */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ao/ao.h>
#include <math.h>
ao_device *device;
ao_sample_format format;
int main(int argc, char *argv[])
{
int default_driver;
char *buffer;
unsigned long count;
FILE *fp;
if (argc != 2) {
printf("usage: %s <filename>\n", argv[0]);
exit(1);
}
ao_initialize();
default_driver = ao_default_driver_id();
memset(&format, 0, sizeof(format));
format.bits = 16;
format.channels = 2;
format.rate = 44100;
format.byte_format = AO_FMT_LITTLE;
device = ao_open_live(default_driver, &format, NULL /* no options */);
if (device == NULL) {
printf("Error opening sound device.\n");
exit(1);
}
fp = fopen(argv[1], "rb");
if (fp == NULL) {
printf("Cannot open %s.\n", argv[1]);
exit(2);
}
fseek(fp, 0, SEEK_END);
count = ftell(fp);
fseek(fp, 0, SEEK_SET);
fread(buffer, sizeof(char), count, fp);
ao_play(device, buffer, count);
ao_close(device);
ao_shutdown();
return 0;
}

A critical something I didn't realize is that libao does absolutely no decoding. It is therefore up to the programmer to extract the sample size, rate, channels, etc and feed those to libao before opening the audio device. libsndfile is available for doing this, but if you just want something quick and dirty, here's the code for playing an AIFF file:
/* compile with "gcc -o playaiff playaiff.c -lao -ldl -lm" */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ao/ao.h>
#include <math.h>
#define MAXCHAN 8
ao_device *device;
ao_sample_format format;
#define gshort( b) (((int)((b)[0]) << 8) + (int)((b)[1]))
#define glong( b) (((int)((b)[0]) << 24) + ((int)((b)[1]) << 16) +\
((int)((b)[2]) << 8) + (int)((b)[3]))
typedef struct {
short channels;
short samplesize;
int samplerate;
unsigned long samplecount;
int valid;
} aiffinfo;
aiffinfo getaiffinfo(FILE *);
static int IeeeExtendedToLong(unsigned char *);
int main(int argc, char *argv[])
{
int default_driver;
char *buffer;
aiffinfo info;
FILE *fp;
if (argc != 2) {
printf("usage: %s <filename>\n", argv[0]);
exit(1);
}
ao_initialize();
default_driver = ao_default_driver_id();
memset(&format, 0, sizeof(format));
fp = fopen(argv[1], "rb");
if (fp == NULL) {
printf("Cannot open %s.\n", argv[1]);
exit(2);
}
info = getaiffinfo(fp);
if (!info.valid) {
printf("Invalid AIFF file.\n");
exit(1);
}
format.bits = info.samplesize;
format.channels = info.channels;
format.rate = info.samplerate;
format.byte_format = AO_FMT_LITTLE;
device = ao_open_live(default_driver, &format, NULL /* no options */);
if (device == NULL) {
printf("Error opening sound device.\n");
exit(1);
}
buffer = malloc(sizeof(char) * info.samplecount);
fread(buffer, sizeof(char), info.samplecount, fp);
ao_play(device, buffer, info.samplecount);
ao_close(device);
ao_shutdown();
return 0;
}
aiffinfo getaiffinfo(FILE *fp)
{
int size;
int len;
int offset;
int blocksize;
int found = 0;
unsigned char chunk[18];
unsigned char fid[4];
aiffinfo info;
info.samplesize = 0;
info.valid = 0;
if (fread(chunk, 1, 4, fp) < 4) return info;
if (memcmp(chunk,"FORM",4)) return info;
if (fread(chunk, 1, 4, fp) < 4) return info;
size = glong(chunk);
if (size & 1) size++;
if (size < 20) return info;
if (fread(chunk, 1, 4, fp) < 4) return info;
if (memcmp(chunk, "AIFF", 4)) return info;
size -= 4;
while (size > 8) {
if (fread(fid, 1, 4, fp) < 4) return info; // chunck id
if (fread(chunk, 1, 4, fp) < 4) return info; // and len
size -= 8;
len = glong(chunk);
if (len < 0) return info;
if (len & 1) len++;
size -= len;
if (size < 0) return info;
if (memcmp(fid, "COMM", 4) == 0) {
if (len != 18) return info;
if (fread(chunk, 1, 18, fp) < 18) return info;
info.channels = gshort(chunk);
if (info.channels < 1) return info;
if (info.channels > MAXCHAN) return info;
info.samplecount = glong(chunk+2);
if (info.samplecount < 1) return info;
info.samplerate = IeeeExtendedToLong(chunk + 8);
if (info.samplerate <= 0) return info;
info.samplesize = gshort(chunk + 6);
if (info.samplesize < 1 || info.samplesize > 16) return info;
} else if (memcmp(fid,"SSND",4)==0){
if (!info.channels) return info;
if (fread(chunk, 1, 4, fp) < 4) return info;
offset = glong(chunk);
if (fread(chunk, 1, 4, fp) < 4) return info;
blocksize = glong(chunk);
if (blocksize) return info;
if (offset) fseek(fp, offset,SEEK_CUR);
found = 1;
break;
} else fseek (fp, len, SEEK_CUR);
}
if (!found) return info;
if (!info.channels) return info;
// printf("Looks good so far.\n");
info.valid = 1;
return info;
}
/****************************************************************
* Extended precision IEEE floating-point conversion routine.
****************************************************************/
#ifndef Uint32
#define Uint32 unsigned int
#endif
#ifndef HUGE_INT32
#define HUGE_INT32 0x7fffffff
#endif /* HUGE_VAL */
static int IeeeExtendedToLong( unsigned char *bytes)
{
int f = 0;
int expon;
Uint32 hiMant;
Uint32 loMant;
expon = ((bytes[0] & 0x7F) << 8) | (bytes[1] & 0xFF);
hiMant = ((Uint32) (bytes[2] & 0xFF) << 24)
| ((Uint32) (bytes[3] & 0xFF) << 16)
| ((Uint32) (bytes[4] & 0xFF) << 8)
| ((Uint32) (bytes[5] & 0xFF));
loMant = ((Uint32) (bytes[6] & 0xFF) << 24)
| ((Uint32) (bytes[7] & 0xFF) << 16)
| ((Uint32) (bytes[8] & 0xFF) << 8)
| ((Uint32) (bytes[9] & 0xFF));
if (expon == 0 && hiMant == 0 && loMant == 0) f = 0;
else if (expon == 0x7FFF) f = HUGE_INT32;
else {
expon -= 16382;
expon = 32-expon;
if (expon < 0) f = HUGE_INT32;
else f = hiMant >> expon;
}
if (bytes[0] & 0x80)
return -f;
else
return f;
}

Related

interrupts not working in KVM x86 16-bit guest

I'm writing a Linux KVM hypervisor for x86 16-bit guests running in real mode. When doing interrupt calls (int ... instruction), I've encountered the KVM_INTERNAL_ERROR_SIMUL_EX error on Linux kernel 3.13.0. The same code is running fine on Linux kernel 3.16.0. Am I missing something? Is there a workaround I can add to my code to make it work with Linux kernel 3.13.0 (and possibly earlier)?
The test guest calls int 0x18 ... int 0x4f, all of which is handled in the hypervisor (C code after KVM_RUN has returned). When it's working correctly, all of the interrupt calls work. On Linux kernel 3.13.0, int 0x21 starts failing (and then int 0x22, int 0x23 and int 0x24 would also fail).
I was trying to write the shortest example C code to demonstrate the problem, here it is:
/* Based on: https://gist.github.com/zserge/d68683f17c68709818f8baab0ded2d15
* Based on: https://gist.githubusercontent.com/zserge/d68683f17c68709818f8baab0ded2d15/raw/b79033254b092ec9121bb891938b27dd128030d7/kvm-host-simple.c
*
* Compile: gcc -ansi -pedantic -s -O2 -W -Wall -o kvm16 kvm16.c && ./kvm16
*
* Expected correct output (e.g. on Linux 3.16.0 compiled for i386 (i686)):
*
* ...
* info: int 0x4f iret to: ...
* info: success, exiting
*
* Failure output (e.g. on Linux 3.13.0 compiled for amd64 (x86_64)):
*
* info: int 0x20 iret to: cs=0x0070 ip=0x0013
* fatal: KVM internal error suberror=2
*
* // Encounter unexpected simultaneous exceptions.
* #define KVM_INTERNAL_ERROR_SIMUL_EX 2
*/
#define _GNU_SOURCE
#include <fcntl.h>
#include <linux/kvm.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <unistd.h>
#define INT_NUM 0x21 /* Also works for INT_NUM == 0x20. */
int main(int argc, char *argv[]) {
int kvm_fd, vm_fd, vcpu_fd;
void *mem;
struct kvm_userspace_memory_region region;
struct kvm_run *run;
struct kvm_regs regs;
struct kvm_sregs sregs;
(void)argc; (void)argv;
if ((kvm_fd = open("/dev/kvm", O_RDWR)) < 0) {
perror("failed to open /dev/kvm");
return 1;
}
if ((vm_fd = ioctl(kvm_fd, KVM_CREATE_VM, 0)) < 0) {
perror("failed to create vm");
return 1;
}
if ((mem = mmap(NULL, 0x1000, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0)) == NULL) {
perror("mmap");
return 1;
}
memset(&region, 0, sizeof(region));
region.slot = 0;
region.guest_phys_addr = 0;
region.memory_size = 0x1000;
region.userspace_addr = (uintptr_t)mem;
if (ioctl(vm_fd, KVM_SET_USER_MEMORY_REGION, &region) < 0) {
perror("KVM_SET_USER_MEMORY_REGION");
return 1;
}
{ /* 8086 real mode machine code. */
char *p = (char*)mem + 0x700;
unsigned int_num;
for (int_num = 0; int_num < 0x100; ++int_num) {
*(unsigned short*)((char*)mem + int_num * 4) = int_num; /* Interrupt vector INT_NUM offset := INT_NUM. */
*(unsigned short*)((char*)mem + int_num * 4 + 2) = 0x54; /* Interrupt vector INT_NUM segment := 0x54. */
}
*p++ = (char)0xf4; /* hlt. */
for (int_num = 0x18; int_num < 0x50; ++int_num) {
*p++ = (char)0xcd; /* int int_num. */
*p++ = (char)int_num;
}
*p++ = (char)0xf4;
}
memset((char*)mem + 0x540, '\xf4', 0x100); /* 256 times hlt. Interrupt vectors point here. */
if ((vcpu_fd = ioctl(vm_fd, KVM_CREATE_VCPU, 0)) < 0) {
perror("KVM_CREATE_VCPU");
return 1;
}
{
int kvm_run_mmap_size = ioctl(kvm_fd, KVM_GET_VCPU_MMAP_SIZE, 0);
if (kvm_run_mmap_size < 0) {
perror("KVM_GET_VCPU_MMAP_SIZE");
return 1;
}
run = (struct kvm_run *)mmap(
NULL, kvm_run_mmap_size, PROT_READ | PROT_WRITE, MAP_SHARED, vcpu_fd, 0);
if (run == NULL) {
perror("mmap kvm_run");
return 1;
}
}
memset(&regs, '\0', sizeof(regs));
if (ioctl(vcpu_fd, KVM_GET_SREGS, &sregs) < 0) {
perror("KVM_GET_SREGS");
return 1;
}
{
int fd = open("kvm16.sregs", O_CREAT | O_WRONLY | O_TRUNC, 0644);
if (fd < 0) {
perror("open");
return 1;
}
if (write(fd, &sregs, sizeof(sregs)) != sizeof(sregs)) {
perror("write");
return 1;
}
if (close(fd) != 0) {
perror("close");
return 1;
}
}
sregs.cs.base = (sregs.cs.selector = 0x70) << 4;
sregs.ds.base = (sregs.ds.selector = sregs.cs.selector) << 4;
sregs.es.base = (sregs.es.selector = sregs.cs.selector) << 4;
sregs.ss.base = (sregs.ss.selector = sregs.cs.selector) << 4;
if (ioctl(vcpu_fd, KVM_GET_REGS, &regs) < 0) {
perror("KVM_GET_REGS");
return 1;
}
regs.rflags = 1 << 1; /* Reserved bit in EFLAGS. Even needed after KVM_GET_REGS. */
regs.rip = 0;
regs.rsp = 0x1000 - 0x700;
if (ioctl(vcpu_fd, KVM_SET_SREGS, &sregs) < 0) {
perror("KVM_SET_SREGS");
return 1;
}
if (ioctl(vcpu_fd, KVM_SET_REGS, &regs) < 0) {
perror("KVM_SET_REGS");
return 1;
}
for (;;) {
int ret = ioctl(vcpu_fd, KVM_RUN, 0);
unsigned short cs, ip;
if (ret < 0) {
perror("KVM_RUN");
return 1;
}
if (ioctl(vcpu_fd, KVM_GET_SREGS, &sregs) < 0) {
perror("KVM_GET_SREGS");
return 1;
}
if (ioctl(vcpu_fd, KVM_GET_REGS, &regs) < 0) {
perror("KVM_GET_REGS");
return 1;
}
cs = sregs.cs.selector;
ip = regs.rip;
if (run->exit_reason == KVM_EXIT_HLT) {
fprintf(stderr, "info: hlt: cs=0x%04x ip=0x%04x\n", cs, ip - 1);
if (cs == 0x70) {
if (ip != 0 + 1) {
fprintf(stderr, "info: success, exiting\n");
return 0; /* EXIT_SUCCESS after the second `hlt' in the code. */
}
} else if (cs == 0x54) { /* Simulate iret. */
const char *csip_ptr = (const char*)mem + ((unsigned short)sregs.ss.selector << 4) + (unsigned short)regs.rsp;
const unsigned short int_ip = ((const unsigned short*)csip_ptr)[0];
const unsigned short int_cs = ((const unsigned short*)csip_ptr)[1];
const unsigned short int_flags = ((const unsigned short*)csip_ptr)[2];
fprintf(stderr, "info: int 0x%02x iret to: cs=0x%04x ip=0x%04x\n", ip - 1, int_cs, int_ip);
sregs.cs.base = (sregs.cs.selector = int_cs) << 4;
regs.rip = int_ip;
if (int_flags & (1 << 9)) regs.rflags |= (1 << 9); /* Set IF back to 1 if it was 1. */
regs.rsp += 6; /* pop ip, pop cs, popfw . */
if (ioctl(vcpu_fd, KVM_SET_SREGS, &sregs) < 0) {
perror("KVM_SET_SREGS");
return 1;
}
if (ioctl(vcpu_fd, KVM_SET_REGS, &regs) < 0) {
perror("KVM_SET_REGS");
return 1;
}
} else {
fprintf(stderr, "fatal: unexpected hlt: cs=0x%04x ip=0x%04x\n", cs, ip - 1);
return 5;
}
} else if (run->exit_reason == KVM_EXIT_INTERNAL_ERROR) {
fprintf(stderr, "fatal: KVM internal error suberror=%d\n", (unsigned)run->internal.suberror);
return 4;
} else {
fprintf(stderr, "fatal: unexpected KVM exit: exit_reason=%d cs=0x%04x ip=0x%04x\n", run->exit_reason, cs, ip);
return 2;
}
}
}

ffmpeg rtsp on C

I need to write RTSP steram from IP-cam to file. I use FFMPEG to do this. I found code example on C++, but i need to use only C. Can anyone help me?
i have problems in file operations. how can i write stream to file?
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <sstream>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
#include <libswscale/swscale.h>
}
int main(int argc, char** argv) {
// Open the initial context variables that are needed
SwsContext *img_convert_ctx;
AVFormatContext* format_ctx = avformat_alloc_context();
AVCodecContext* codec_ctx = NULL;
int video_stream_index;
// Register everything
av_register_all();
avformat_network_init();
//open RTSP
if (avformat_open_input(&format_ctx, "rtsp://134.169.178.187:8554/h264.3gp",
NULL, NULL) != 0) {
return EXIT_FAILURE;
}
if (avformat_find_stream_info(format_ctx, NULL) < 0) {
return EXIT_FAILURE;
}
//search video stream
for (int i = 0; i < format_ctx->nb_streams; i++) {
if (format_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
video_stream_index = i;
}
AVPacket packet;
av_init_packet(&packet);
//open output file
AVFormatContext* output_ctx = avformat_alloc_context();
AVStream* stream = NULL;
int cnt = 0;
//start reading packets from stream and write them to file
av_read_play(format_ctx); //play RTSP
// Get the codec
AVCodec *codec = NULL;
codec = avcodec_find_decoder(AV_CODEC_ID_H264);
if (!codec) {
exit(1);
}
// Add this to allocate the context by codec
codec_ctx = avcodec_alloc_context3(codec);
avcodec_get_context_defaults3(codec_ctx, codec);
avcodec_copy_context(codec_ctx, format_ctx->streams[video_stream_index]->codec);
std::ofstream output_file;
if (avcodec_open2(codec_ctx, codec, NULL) < 0)
exit(1);
img_convert_ctx = sws_getContext(codec_ctx->width, codec_ctx->height,
codec_ctx->pix_fmt, codec_ctx->width, codec_ctx->height, AV_PIX_FMT_RGB24,
SWS_BICUBIC, NULL, NULL, NULL);
int size = avpicture_get_size(AV_PIX_FMT_YUV420P, codec_ctx->width,
codec_ctx->height);
uint8_t* picture_buffer = (uint8_t*) (av_malloc(size));
AVFrame* picture = av_frame_alloc();
AVFrame* picture_rgb = av_frame_alloc();
int size2 = avpicture_get_size(AV_PIX_FMT_RGB24, codec_ctx->width,
codec_ctx->height);
uint8_t* picture_buffer_2 = (uint8_t*) (av_malloc(size2));
avpicture_fill((AVPicture *) picture, picture_buffer, AV_PIX_FMT_YUV420P,
codec_ctx->width, codec_ctx->height);
avpicture_fill((AVPicture *) picture_rgb, picture_buffer_2, AV_PIX_FMT_RGB24,
codec_ctx->width, codec_ctx->height);
while (av_read_frame(format_ctx, &packet) >= 0 && cnt < 1000) { //read ~ 1000 frames
std::cout << "1 Frame: " << cnt << std::endl;
if (packet.stream_index == video_stream_index) { //packet is video
std::cout << "2 Is Video" << std::endl;
if (stream == NULL) { //create stream in file
std::cout << "3 create stream" << std::endl;
stream = avformat_new_stream(output_ctx,
format_ctx->streams[video_stream_index]->codec->codec);
avcodec_copy_context(stream->codec,
format_ctx->streams[video_stream_index]->codec);
stream->sample_aspect_ratio =
format_ctx->streams[video_stream_index]->codec->sample_aspect_ratio;
}
int check = 0;
packet.stream_index = stream->id;
std::cout << "4 decoding" << std::endl;
int result = avcodec_decode_video2(codec_ctx, picture, &check, &packet);
std::cout << "Bytes decoded " << result << " check " << check
<< std::endl;
if (cnt > 100) //cnt < 0)
{
sws_scale(img_convert_ctx, picture->data, picture->linesize, 0,
codec_ctx->height, picture_rgb->data, picture_rgb->linesize);
std::stringstream file_name;
file_name << "test" << cnt << ".ppm";
output_file.open(file_name.str().c_str());
output_file << "P3 " << codec_ctx->width << " " << codec_ctx->height
<< " 255\n";
for (int y = 0; y < codec_ctx->height; y++) {
for (int x = 0; x < codec_ctx->width * 3; x++)
output_file
<< (int) (picture_rgb->data[0]
+ y * picture_rgb->linesize[0])[x] << " ";
}
output_file.close();
}
cnt++;
}
av_free_packet(&packet);
av_init_packet(&packet);
}
av_free(picture);
av_free(picture_rgb);
av_free(picture_buffer);
av_free(picture_buffer_2);
av_read_pause(format_ctx);
avio_close(output_ctx->pb);
avformat_free_context(output_ctx);
return (EXIT_SUCCESS);
}
please help me to compile this by C-compiler.

socket programming FD_ISSET() method usage

so I'm new to socket programming, and I was asked to write the server side that sends data to a client according to a certain request. I'm trying to server multiple clients at the same time. When a client first connects, the server accepts with no troubles whatsoever, but when a client sends a certain request, I get stuck in an infinity loop and it's not clear at all to me why the server keeps sending the same info to the client over and over and over again, below is my code for the server side:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <math.h>
#include <windows.h>
#include <winsock.h>
#include <string.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <io.h>
#include <fstream>
#include <vector>
#define MAX_CONNECTION 100
#ifndef max
#define max(a,b) ((a) < (b) ? (a) : (b))
#endif
typedef struct connection{
char ipaddr[16];
int port;
int sd;
} connection;
static unsigned short SERVER_PORT = 4118;
int main(int argc, char* argv[])
{
int maxfd =-1;
fd_set rset, allset;
connection client[MAX_CONNECTION];
int passiveSock; /* Main Server Socket */
struct sockaddr_in servSock_in;
#ifdef WIN32
WSADATA wsaData;
WSAStartup(0x0101, &wsaData);
#endif
int port;
if (argc > 1)
port = atoi(argv[1]);
else
port = SERVER_PORT;
for(int i=0; i < MAX_CONNECTION ; i++)
client[i].sd = -1;
memset((char *)&servSock_in, 0, sizeof(servSock_in));
servSock_in.sin_family = PF_INET;
servSock_in.sin_addr.s_addr = htonl(INADDR_ANY);
servSock_in.sin_port = htons((u_short)port);
passiveSock = socket(PF_INET, SOCK_STREAM, 0);
if (passiveSock < 0) {
fprintf(stderr, "I am too tired... I failed to open gate...\n");
return -1;
}
if (bind(passiveSock, (struct sockaddr *)&servSock_in, sizeof(servSock_in)) < 0){
fprintf(stderr, "I couldn't attach gate to port...\n");
return -1;
}
if (listen(passiveSock, 5) < 0) {
fprintf(stderr, "I am not hearing anything...\n");
return -1;
}
FD_SET(passiveSock, &allset);
maxfd = max(maxfd, passiveSock);
struct sockaddr_in cliSock_in;
int cliSockLen;
int connectedSock;
cliSockLen = sizeof(cliSock_in);
printf("\n Waiting for an incoming connection at port number %d", port);
int bytesread = 0;
for (;;)
{
//FD_ZERO(&allset);
rset = allset;
int nready = select(maxfd+1,&rset, NULL, NULL, NULL);
if(FD_ISSET(passiveSock, &rset)){
printf("In the first if");
connectedSock = accept(passiveSock, (struct sockaddr *)&cliSock_in, &cliSockLen);
/* if an error occurs while accepting */
if (connectedSock == -1) {
printf("\n Server: Accept error (errno = %d: %s)\n", errno, strerror(errno));
continue;
}
for (int i=0; i<MAX_CONNECTION; i++)
if (client[i].sd < 0){
client[i].sd=connectedSock;
strcpy(client[i].ipaddr, inet_ntoa(cliSock_in.sin_addr));
client[i].port= ntohs(cliSock_in.sin_port);
printf("\n Server: connection established with %s:%d\n",
client[i].ipaddr, client[i].port);
break;
}
FD_SET(connectedSock, &allset);
maxfd = max(maxfd, connectedSock);
}
else{
for(int j = 0 ; j < MAX_CONNECTION; j++){
connectedSock = client[j].sd;
printf("connectedSock is %d", connectedSock);
if(connectedSock < 0)
continue;
if(FD_ISSET(client[j].sd, &rset)){
unsigned char buffer[66000];
int index = 0;
bytesread = recv(connectedSock, (char *)buffer, 66000, 0);
int type;
type = (buffer[0] & 0xE0) >> 5;
if (type == 0)
{
char fname[100];
int i = 0;
int length = (buffer[i++] & 0x1F);
memcpy(&fname[0], &buffer[i], length);
fname[length] = '\0';
i += length;
int fs = 0;
fs += (buffer[i++] << 8) & 0xff00;
fs += buffer[i++];
char* filedata = (char*)malloc(fs*sizeof(char));
memcpy(&filedata[0], &buffer[i], fs);
filedata[fs] = '\0';
for (int i = 0; i < fs; i++)
printf("%c", filedata[i]);
printf("type=%d,length=%d,data=%s,fs=%d,filedata=%s", type, length, fname, fs, filedata);
std::ofstream of;
of.open(fname, std::ios::binary);
for (int i = 0; i < fs; i++)
of.write(filedata + i, 1);
of.close();
unsigned char rep;
int reptype = 0;
rep = (unsigned char)(reptype & 0x07);
send(connectedSock, (char*)(&rep), 1, 0);
}
else if (type == 1)
{
char fname[100];
int i = 0;
int length = (buffer[i++] & 0x1F);
memcpy(&fname[0], &buffer[i], length);
fname[length] = '\0';
i += length;
std::ifstream t;
int fs;
t.open(fname, std::ios::binary);
std::vector<char> vec((
std::istreambuf_iterator<char>(t)),
(std::istreambuf_iterator<char>()));// open input file
t.close();
fs = vec.size();
char* filedata = (char*)malloc(fs*sizeof(char)); // allocate memory for a buffer of appropriate dimension
filedata = &vec[0];
filedata[fs] = '\0';
i = 0;
unsigned char* repbuffer = (unsigned char*)malloc(3 + length + fs);
repbuffer[i] = (unsigned char)(type & 0x07);
repbuffer[i] = repbuffer[i] << 5;
repbuffer[i] = repbuffer[i] | (length & 0x0000003F);
i++;
memcpy(&repbuffer[i], fname, length);
i = i + length;
printf("sizeof fs=%d", sizeof(fs));
repbuffer[i++] = (unsigned char)((fs & 0xff00) >> 8);
repbuffer[i++] = (unsigned char)(fs & 0xff);
memcpy(&repbuffer[i], filedata, fs);
printf("sizeof buffer=%d", sizeof(repbuffer));
i = i + fs;
// printf("the buffer contains %s\n",&repbuffer[11]);
if (send(connectedSock, (char*)repbuffer, i, 0) == -1)
{
printf("A local error was detected while sending data! (errno = %d: %s)\n", errno, strerror(errno));
return -1;
}
}
else if (type == 2)
{
char fname[100],nfname[100];
int i = 0;
int length = (buffer[i++] & 0x1F);
memcpy(&fname[0], &buffer[i], length);
fname[length] = '\0';
i += length;
int nlength = (buffer[i++] & 0x1F);
memcpy(&nfname[0], &buffer[i], nlength);
nfname[nlength] = '\0';
rename(fname,nfname);
}
else if (type == 3)
{
char rep[32];
strcpy(rep, "bye change get help put");
int length = strlen(rep);
unsigned char* repbuffer = (unsigned char*)malloc(1 + length);
int type = 6;
repbuffer[0] = (unsigned char)(type & 0x07);
repbuffer[0] = repbuffer[0] << 5;
repbuffer[0] = repbuffer[0] | (length & 0x0000003F);
memcpy(&repbuffer[1], rep, length);
if (send(connectedSock, (char*)repbuffer, length+1, 0) == -1)
{
perror("A local error was detected while sending data!!");
return -1;
}
}
else if (type == 4)
{
closesocket(connectedSock);
}
break;
}
}
}
}
closesocket(passiveSock);
WSACleanup();
return 0;
}
I feel there's something wrong with the usage of FD_ISSET() method, I've been trying to figure out the error for 2 hours now, pleeassse help

Printing Partition Table - C program

I am trying to print a partition table using C programming language, everything seems to work fine: Opening and reading, but I don't understand why it is printing garbage values.
Here is the code:
struct partition
{
unsigned char drive;
unsigned char chs_begin[3];
unsigned char sys_type;
unsigned char chs_end[3];
unsigned char start_sector[4];
unsigned char nr_sector[4];
};
int main()
{
int gc = 0, i = 1, nr = 0, pos = -1, nw = 0;
int fd =0;
char buf[512] ;
struct partition *sp;
printf("Ok ");
if ( (fd = open("/dev/sda", O_RDONLY | O_SYNC )) == -1)
{
perror("Open");
exit(1);
}
printf("fd is %d \n", fd);
pos = lseek (fd, 0, SEEK_CUR);
printf("Position of pointer is :%d\n", pos);
if ((nr = read(fd, buf, sizeof(buf))) == -1)
{
perror("Read");
exit(1);
}
close(fd);
printf("Size of buf = %d\n and number of bytes read are %d ", sizeof(buf), nr);
if ((nw = write(1, buf, 64)) == -1)
{
printf("Write: Error");
exit(1);
}
printf("\n\n %d bytes are just been written on stdout\n", nw,"this can also be printed\n");
printf("\n\t\t*************Partition Table****************\n\n");
for (i=0 ; i<4 ; i++)
{
sp = (struct partition *)(buf + 446 + (16 * i));
putchar(sp -> drive);
}
return 0;
}
It is printing garbage instead of partition table.
I might have some basic understanding issues but I searched with Google for a long time but it did not really help. I also saw the source code of fdisk but it is beyond my understanding at this point. Could anyone please guide me? I am not expecting someone to clear my mistake and give me the working code. Just a sentence or two - or any link.
Try this:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
struct partition
{
unsigned char boot_flag; /* 0 = Not active, 0x80 = Active */
unsigned char chs_begin[3];
unsigned char sys_type; /* For example : 82 --> Linux swap, 83 --> Linux native partition, ... */
unsigned char chs_end[3];
unsigned char start_sector[4];
unsigned char nr_sector[4];
};
void string_in_hex(void *in_string, int in_string_size);
void dump_partition(struct partition *part, int partition_number);
void dump_partition(struct partition *part, int partition_number)
{
printf("Partition /dev/sda%d\n", partition_number + 1);
printf("boot_flag = %02X\n", part->boot_flag);
printf("chs_begin = ");
string_in_hex(part->chs_begin, 3);
printf("sys_type = %02X\n", part->sys_type);
printf("chs_end = ");
string_in_hex(part->chs_end, 3);
printf("start_sector = ");
string_in_hex(part->start_sector, 4);
printf("nr_sector = ");
string_in_hex(part->nr_sector, 4);
}
void string_in_hex(void *in_string, int in_string_size)
{
int i;
int k = 0;
for (i = 0; i < in_string_size; i++)
{
printf("%02x ", ((char *)in_string)[i]& 0xFF);
k = k + 1;
if (k == 16)
{
printf("\n");
k = 0;
}
}
printf("\n");
}
int main(int argc, char **argv)
{
int /*gc = 0,*/ i = 1, nr = 0, pos = -1/*, nw = 0*/;
int fd = 0;
char buf[512] ;
struct partition *sp;
int ret = 0;
printf("Ok ");
if ((fd = open("/dev/sda", O_RDONLY | O_SYNC)) == -1)
{
perror("Open");
exit(1);
}
printf("fd is %d\n", fd);
pos = lseek (fd, 0, SEEK_CUR);
printf("Position of pointer is :%d\n", pos);
if ((nr = read(fd, buf, sizeof(buf))) == -1)
{
perror("Read");
exit(1);
}
ret = close(fd);
if (ret == -1)
{
perror("close");
exit(1);
}
/* Dump the MBR buffer, you can compare it on your system with the output of the command:
* hexdump -n 512 -C /dev/sda
*/
string_in_hex(buf, 512);
printf("Size of buf = %d - and number of bytes read are %d\n", sizeof(buf), nr);
/*if ((nw = write(1, buf, 64)) == -1)
{
printf("Write: Error");
exit(1);
}
printf("\n\n%d bytes are just been written on stdout\nthis can also be printed\n", nw);
*/
//printf("\n\t\t*************Partition Table****************\n\n");
printf("\n\t\t*************THE 4 MAIN PARTITIONS****************\n\n");
/* Dump main partitions (4 partitions) */
/* Note : the 4 partitions you are trying to dump are not necessarily existing! */
for (i = 0 ; i < 4 ; i++)
{
sp = (struct partition *)(buf + 446 + (16 * i));
//putchar(sp->boot_flag);
dump_partition(sp, i);
}
return 0;
}

Check the bitness of an application binary on Windows (C/C++)

Is it possible to check the bitness of a binary (EXE) without/before running it? This could be easily done on Linux but I'm not familiar with Windows binary format.
Thanks.
Since you labeled this question C, there's a Win32 API function GetBinaryType. It doesn't work for DLLs, though.
if ( GetBinaryType(argv[i], &bintype) ) {
switch(bintype) {
case SCS_32BIT_BINARY: typename = TEXT("Windows 32 Bit"); break;
case SCS_64BIT_BINARY: typename = TEXT("Windows 64 Bit"); break;
case SCS_DOS_BINARY: typename = TEXT("DOS-Programm"); break;
case SCS_OS216_BINARY: typename = TEXT("OS/2-Programm"); break;
case SCS_PIF_BINARY: typename = TEXT("PIF-Datei"); break;
case SCS_POSIX_BINARY: typename = TEXT("POSIX-Programm"); break;
case SCS_WOW_BINARY: typename = TEXT("Windows 16 Bit"); break;
default: typename = TEXT("unknown"); break;
}
}
else {
typename = TEXT("not executable");
}
look at answers here
It says this information can be gotten by using dumpbin /headers from the Windows Platform SDK
There are many tools that help you to discover the bitness of applications (like WinDbg or PeStudio).
Read header:
IMAGE_FILE_HEADER structure
As a start you could do something like this, (should work for both dll and exe):
(Only tested on a few files – and those gave OK result.)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* Runtime Byteorder detection - Motorola or Intel (Does not catch mixed) */
static int byteorder_mm(void)
{
union {double d; unsigned int i[2];} u;
u.d = 1.0;
return (u.i[0] != 0);
}
/* Char to unsigned int */
static unsigned int chr_to_ui(unsigned char *buf, int mm)
{
if (mm)
return buf[0] << 24 | buf[1] << 16 | buf[2] << 8 | buf[3];
return buf[3] << 24 | buf[2] << 16 | buf[1] << 8 | buf[0];
}
/* Char to unsigned short */
static unsigned short chr_to_us(unsigned char *buf, int mm)
{
if (mm)
return buf[0] << 8 | buf[1];
return buf[1] << 8 | buf[0];
}
int main(int argc, char *argv[])
{
FILE *fh;
unsigned char buf[128] = {0};
char tmpstr[64];
unsigned int tmp_ui;
unsigned short tmp_us;
time_t tt;
int mm = byteorder_mm();
if (argc < 2) {
fprintf(stderr,
"Missing input file.\n");
return 1;
}
if ((fh = fopen(argv[1], "rb")) == NULL) {
fprintf(stderr,
"Unable to open %s.\n",
argv[1]);
perror(0);
return 1;
}
/* Read MS-DOS Segment */
if (!fread(buf, 64, 1, fh)) {
fprintf(stderr,
"Unable to read %d bytes, #%ld.\n",
2, ftell(fh));
perror(0);
fclose(fh);
return 1;
}
/* Check header mark : MZ */
if (buf[0] != 0x4d || buf[1] != 0x5a) {
fprintf(stderr,
"%s is missing Mark Zbikowski header.\n",
argv[1]);
fclose(fh);
return 2;
}
/* Get offset (from 0) to IMAGE_NT_HEADERS */
tmp_ui = chr_to_ui(buf+60, mm);
fseek(fh, tmp_ui - 64, SEEK_CUR);
/* Read IMAGE_NT_HEADER signature */
if (!fread(buf, 4, 1, fh)) {
fprintf(stderr,
"Unable to read %d bytes, #%ld.\n",
4, ftell(fh));
perror(0);
fclose(fh);
return 1;
}
/* Check signature : PE'x0'x0 */
if (buf[0] != 0x50 || buf[1] != 0x45 ||
buf[2] != 0x00 || buf[3] != 0x00) {
fprintf(stderr,
"%s is missing valid Portable Executable signature.\n",
argv[1]);
fclose(fh);
return 2;
}
/* Read IMAGE_FILE_HEADER:
typedef struct _IMAGE_FILE_HEADER {
WORD Machine;
WORD NumberOfSections;
DWORD TimeDateStamp;
DWORD PointerToSymbolTable;
DWORD NumberOfSymbols;
WORD SizeOfOptionalHeader;
WORD Characteristics;
} IMAGE_FILE_HEADER, *PIMAGE_FILE_HEADER;
*/
if (!fread(buf, 20, 1, fh)) {
fprintf(stderr,
"Unable to read %d bytes, #%ld.\n",
4, ftell(fh));
perror(0);
fclose(fh);
return 1;
}
/* Bittype */
tmp_us = chr_to_us(buf, mm);
switch (tmp_us) {
case 0x014c: fprintf(stdout, "Machine: x86 (I386)\n"); break;
case 0x0200: fprintf(stdout, "Machine: IA64 (Intel Itanium)\n"); break;
case 0x8664: fprintf(stdout, "Machine: x64 (AMD64)\n"); break;
default: fprintf(stderr,
"Unable to recognize machine type 0x%04x\n",
tmp_us);
fclose(fh);
return 2;
}
/* Timestamp */
tmp_ui = chr_to_ui(buf+4, mm);
tt = tmp_ui;
strftime(tmpstr, 31, "%a %Y-%m-%d %H:%M:%S", localtime(&tt));
fprintf(stdout,
"Time : %s (%d)\n",
tmpstr, tmp_ui);
/* ... */
fclose(fh);
return 0;
}

Resources