📄 byte_checker.c
字号:
#include <stdio.h>#include <stdlib.h>#include <sys/param.h>#include <strings.h>#include "constants.h"#include "globals.h"#include "output.h"#define HEADER_SIZE 7extern char Header_path[];struct DMC_request *r;void swap_error_msg();char *station_word_order();char *read_station_50_info();int swap_bytes();void byte_swap(); /* ---------------------------------------------------------------------- */int bytes_need_swapping(data_header)struct input_data_logrec *data_header;{ if (data_header->hdr.scan.time.year < 1950 || data_header->hdr.scan.time.year > 2010) return(TRUE); return(FALSE); }/* -------------------------------------------------------------------- */char *read_station_50_info(fptr, buffer)FILE *fptr;char buffer[];{ int num; char wrkstr[20]; if ((num = fread(buffer, HEADER_SIZE, 1, fptr)) != 1) return((char *)NULL); /* */ /* compute block size from header */ sprintf(wrkstr, "%.4s", buffer + 3); num = atoi(wrkstr); /* plus one for \n at end */ if (fread(buffer + HEADER_SIZE, (num - HEADER_SIZE) + 1, 1, fptr) != 1) return((char *)NULL); /* make sure is null terminated */ buffer[num] = 0; return(buffer);}#define MSB_FIRST 0x76543210 /* MC680x0 word order */#define MSB_LAST 0x10325476 /* VAX, 80x86 word order */ /* ------------------------------------------------------------------------ */int swap_bytes(short_str)char *short_str;{ char tmpchar[4]; /* put here because compiler keeps overwriting * the func parameter. Seems to only allocate * one byte on stack for union ! */ union { unsigned char character[4]; unsigned long int integer; } test4; /* Construct a 4-byte word of known contents - 0x76543210 */ test4.character[0] = 0x76; test4.character[1] = 0x54; test4.character[2] = 0x32; test4.character[3] = 0x10; if (test4.integer == MSB_FIRST) { if (strcmp(short_str, "10") == 0) return (FALSE); /* no swapping needed */ return(TRUE); } else if (test4.integer == MSB_LAST) { if (strcmp(short_str, "01") == 0) return (FALSE); /* no swapping needed */ return (TRUE); } else { fprintf (stderr, "ERROR (find_wordorder): "); fprintf (stderr, "machine word order, %d, not treated by byte swappers.\n", test4.integer); fprintf (stderr, "Execution terminating.\n"); return(FALSE); /* assume no byte swapping necessary */ }}/* ------------------------------------------------------------------------ */short swap_2byte(b)short b;{ union { unsigned char character[2]; unsigned short int integer; } swap2byte; /* holds 2-byte word */ char temp0; swap2byte.integer = b; temp0 = swap2byte.character[0]; swap2byte.character[0] = swap2byte.character[1]; swap2byte.character[1] = temp0; return (swap2byte.integer);}/* ---------------------------------------------------------------------- */int swap_4byte(w)int w;{ union { unsigned char character[4]; unsigned long int integer; } swap4byte; /* holds 4-byte word */ char temp0; char temp1; swap4byte.integer = w; temp0 = swap4byte.character[0]; temp1 = swap4byte.character[1]; swap4byte.character[0] = swap4byte.character[3]; swap4byte.character[1] = swap4byte.character[2]; swap4byte.character[2] = temp1; swap4byte.character[3] = temp0; return (swap4byte.integer);}/* ---------------------------------------------------------------------- */void swap_error_msg(msg)char *msg;{ char str[200]; sprintf(str, "[bytes_need_swapping] %s\n", msg); error_handler (WARNING, str); fprintf(stderr, "Assuming no swapping needed!\n"); perror("pod:bytes_need_swapping"); return;}/* ------------------------------------------------------------------------ */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -