static inline bool is_vfat(const void *data, size_t size) { return check_magic(data, size, "MSWIN", 5, 0x52) || check_magic(data, size, "FAT32 ", 8, 0x52) || check_magic(data, size, "MSDOS", 5, 0x36) || check_magic(data, size, "FAT16 ", 8, 0x36) || check_magic(data, size, "FAT12 ", 8, 0x36) || check_magic(data, size, "FAT ", 8, 0x36) || check_magic(data, size, "\353", 1, 0) || check_magic(data, size, "\351", 1, 0) || check_magic(data, size, "\125\252", 2, 0x1fe); }
void * ADhash_add (ADhash_t hash, void *key, void *data) { struct hash_t *h; struct hash_element_t *p, *e; unsigned int i; /* Check hash structure validity */ if ((h = check_magic (hash, ADMAGIC_HASH)) == NULL) return (NULL); /* First check for duplicate key */ p = hash_lookup (h, key); if (p != NULL) return (NULL); /* Create new hash element */ NEW_ROF (e, NULL); e->key = key; e->data = data; /* Calculate hash index and add element to bucket */ i = hash_index (h, key); e->bucket = i; if ((p = h->hash[i]) == NULL) { h->hash[i] = e; } else { while (p->next != NULL) p = p->next; p->next = e; } h->size++; /* Return new element */ return (data); }
static void *core_prealloc(void *blk, size_t size) { void *new_blk; palloc_t *m; /* size == 0: free block */ if(size == 0) { if(blk != NULL) pfree(blk); new_blk = NULL; } else { /* allocate a new_blk */ new_blk = palloc(size); /* if allocation was OK, and if old block exists, copy old block to new */ if(new_blk != NULL && blk != NULL) { m = (palloc_t *)((char *)blk - sizeof(palloc_t)); if(check_magic(m) == FALSE) { if(prealloc_error_message(m) == TRUE) return NULL; } /* Copy minimum of old and new block sizes */ if(size > m -> size) size = m -> size; memcpy(new_blk,blk,size); /* free the old block */ pfree(blk); } } return new_blk; }
void debug_ADhash_report (FILE *rf, ADhash_t hash, char *msg) { struct hash_t *h; unsigned int i; struct hash_element_t *p; /* Check hash structure validity */ if ((h = check_magic (hash, ADMAGIC_HASH)) == NULL) return; fprintf (rf, "ADhash (%s) = 0x%8.8x\n", msg, h); fprintf (rf, "buckets = %lu\n", h->buckets); fprintf (rf, "size = %lu\n", h->size); fprintf (rf, "\n"); for (i=0; i<h->buckets; i++) { if (!h->hash[i]) continue; fprintf (rf, "bucket[%3lu]", i); p = h->hash[i]; while (p) { fprintf (rf, " {\"%s\", %lu}", (char *)(p->key), (ULONG)p->data); p = p->next; } fprintf (rf, "\n"); } fprintf (rf, "\n"); }
/* parse a provided line */ int pconf_line(PCONF_CTX *ctx, const char *line) { size_t i, linelen; if (!check_magic(ctx)) return 0; ctx->linenum++; /* start over for the new line */ ctx->numargs = 0; ctx->state = STATE_FINDWORDSTART; linelen = strlen(line); for (i = 0; i < linelen; i++) { ctx->ch = line[i]; parse_char(ctx); if (ctx->state == STATE_PARSEERR) return 1; if (ctx->state == STATE_ENDOFLINE) return 1; } /* deal with any lingering characters */ /* still building a word? */ if (ctx->wordptr != ctx->wordbuf) endofword(ctx); /* tie it off */ return 1; }
int program_load_datas(t_list *programs) { t_list_item *item; t_program *program; int header_len; header_len = MAGIC_LEN + PROG_NAME_LENGTH + SIZE_CONTENT_LENGTH + COMMENT_LENGTH + 4; item = programs->top; while (item != NULL) { program = (t_program *)item->data; if (load_data(program) == RET_FAILURE) return (RET_FAILURE); if (program->data_len < header_len) return (error_int(RET_FAILURE, ERR_INVALID_HEADER)); else if (program->data_len == header_len) return (error_int(RET_FAILURE, ERR_CONTENT_EMPTY)); if (check_magic(program->data) == RET_FAILURE) return (RET_FAILURE); program->code = program->data + header_len; program->code_len = program->data_len - header_len; item = item->next; } return (RET_SUCCESS); }
/* read from a file until a whole line is ready for use */ int pconf_file_next(PCONF_CTX *ctx) { if (!check_magic(ctx)) return 0; ctx->linenum++; /* start over for the new line */ ctx->numargs = 0; ctx->state = STATE_FINDWORDSTART; while ((ctx->ch = fgetc(ctx->f)) != EOF) { parse_char(ctx); if (ctx->state == STATE_PARSEERR) return 1; if (ctx->state == STATE_ENDOFLINE) return 1; } /* deal with files that don't end in a newline */ if (ctx->numargs != 0) { /* still building a word? */ if (ctx->wordptr != ctx->wordbuf) endofword(ctx); return 1; } /* finished with nothing left over */ return 0; }
/******************************************************************************** *函数名称: check_file *函数原型: __s32 check_file( __u32 *mem_base, __u32 size, const char *magic ) *函数功能: 使用“算术和”来校验内存中的一段数据 *入口参数: mem_base 待校验的数据在内存中的起始地址(必须是4字节对齐的) * size 待校验的数据的个数(以字节为单位,必须是4字节对齐的) * magic magic number, 待校验文件的标识码 *返 回 值: CHECK_IS_CORRECT 校验正确 * CHECK_IS_WRONG 校验错误 *备 注: ********************************************************************************/ __s32 check_file( __u32 *mem_base, __u32 size, const char *magic ) { if( check_magic( mem_base, magic ) == CHECK_IS_CORRECT &&check_sum( mem_base, size ) == CHECK_IS_CORRECT ) return CHECK_IS_CORRECT; else return CHECK_IS_WRONG; }
/******************************************************************************** *函数名称: check_file *函数原型: __s32 check_file( __u32 *mem_base, __u32 size, const char *magic ) *函数功能: 使用“算术和”来校验内存中的一段数据 *入口参数: mem_base 待校验的数据在内存中的起始地址(必须是4字节对齐的) * size 待校验的数据的个数(以字节为单位,必须是4字节对齐的) * magic magic number, 待校验文件的标识码 *返 回 值: CHECK_IS_CORRECT 校验正确 * CHECK_IS_WRONG 校验错误 *备 注: ********************************************************************************/ __s32 check_file( __u32 *mem_base, __u32 size, const char *magic ) { if( check_magic( mem_base, magic ) == 0 &&check_sum( mem_base, size ) == 0 ) return 0; else return -1; }
void ext2fs_clear_generic_bitmap(ext2fs_generic_bitmap bitmap) { if (check_magic(bitmap)) return; memset(bitmap->bitmap, 0, (size_t) (((bitmap->real_end - bitmap->start) / 8) + 1)); }
static gint oldmda_detect(const GwyFileDetectInfo *fileinfo, gboolean only_name) { if (only_name) return g_str_has_suffix(fileinfo->name_lowercase, EXTENSION) ? 10 : 0; return check_magic(fileinfo->head) ? 100 : 0; }
AD_size_t ADhash_size (ADhash_t hash) { struct hash_t *h; /* Check hash structure validity */ if ((h = check_magic (hash, ADMAGIC_HASH)) == NULL) return (0); return (h->size); }
/* * Function to determine if a filename is a 'viking' type file */ gboolean check_file_magic_vik ( const gchar *filename ) { gboolean result = FALSE; FILE *ff = xfopen ( filename ); if ( ff ) { result = check_magic ( ff, VIK_MAGIC ); xfclose ( ff ); } return result; }
int16_t RSSIData_rssi(const RSSIData *msg) { check_magic(msg); if (has_field(msg, 2)) { return ntohs(msg->rssi); } else { fprintf(stderr, "Requested field rssi from RSSIData at address %p, but " "message do3s not have the field \n", (void *)msg); return -1; } }
int32_t RSSIData_frameNum(const RSSIData *msg) { check_magic(msg); if (has_field(msg, 5)) { return ntohl(msg->frameNum); } else { fprintf(stderr, "Requested field frameNum from RSSIData at address %p, but " "message do3s not have the field \n", (void *)msg); return -1; } }
/* clean up the ctx space */ void pconf_finish(PCONF_CTX *ctx) { if (!check_magic(ctx)) return; if (ctx->f) fclose(ctx->f); free_storage(ctx); ctx->magic = 0; }
/* return 1 if an error occurred, but only do it once */ int pconf_parse_error(PCONF_CTX *ctx) { if (!check_magic(ctx)) return 0; if (ctx->error == 1) { ctx->error = 0; return 1; } return 0; }
int Read_Modulefile( Tcl_Interp *interp, char *filename) { int result; char *startp, *endp; #if WITH_DEBUGGING_UTIL ErrorLogger( NO_ERR_START, LOC, _proc_Read_Modulefile, NULL); #endif /** ** Parameter check. A valid filename is to be given. **/ if( !filename) { if( OK != ErrorLogger( ERR_PARAM, LOC, "filename", NULL)) return( TCL_ERROR); /** -------- EXIT (FAILURE) -------> **/ } /** ** Check for the module 'magic cookie' ** Trust stdin as a valid module file ... **/ if( !strcmp( filename, _fil_stdin) && !check_magic( filename, MODULES_MAGIC_COOKIE, MODULES_MAGIC_COOKIE_LENGTH)) { if( OK != ErrorLogger( ERR_MAGIC, LOC, filename, NULL)) return( TCL_ERROR); /** -------- EXIT (FAILURE) -------> **/ } /** ** Now do execute that module file and evaluate the result of the ** latest executed command **/ result = Execute_TclFile(interp, filename); #if WITH_DEBUGGING_UTIL if(EM_ERROR == ReturnValue(interp, result)) ErrorLogger( NO_ERR_DEBUG, LOC, "Execution of '", filename, "' failed", NULL); #endif /** ** Return the result as derivered from the module file execution **/ #if WITH_DEBUGGING_UTIL ErrorLogger( NO_ERR_END, LOC, _proc_Read_Modulefile, NULL); #endif return( result); } /** End of 'Read_Modulefile' **/
/* ************************************************************************************************************ * * function * * name : * * parmeters : * * return : * * note : * * ************************************************************************************************************ */ int load_boot1_from_spinor(void) { __u32 length; struct spare_boot_head_t *bfh; if(spinor_init(0)) { printf("spinor init fail\n"); return -1; } /* 载入当前块最前面512字节的数据到SRAM中,目的是获取文件头 */ if(spinor_read(UBOOT_START_SECTOR_IN_SPINOR, 1, (void *)CONFIG_SYS_TEXT_BASE ) ) { printf("the first data is error\n"); goto __load_boot1_from_spinor_fail; } printf("Succeed in reading Boot1 file head.\n"); /* 察看是否是文件头 */ if( check_magic( (__u32 *)CONFIG_SYS_TEXT_BASE, UBOOT_MAGIC ) != 0 ) { printf("ERROR! Add %u doesn't store head of Boot1 copy.\n", UBOOT_START_SECTOR_IN_SPINOR ); goto __load_boot1_from_spinor_fail; } bfh = (struct spare_boot_head_t *)CONFIG_SYS_TEXT_BASE; length = bfh->boot_head.length; printf("The size of uboot is %x.\n", length ); if( ( length & ( 512 - 1 ) ) != 0 ) // length必须是NF_SECTOR_SIZE对齐的 { printf("the boot1 is not aligned by %x\n", bfh->boot_head.align_size); goto __load_boot1_from_spinor_fail; } if(spinor_read(UBOOT_START_SECTOR_IN_SPINOR, length/512, (void *)CONFIG_SYS_TEXT_BASE )) { printf("spinor read data error\n"); goto __load_boot1_from_spinor_fail; } bfh->boot_data.storage_type = 3; return 0; __load_boot1_from_spinor_fail: return -1; }
/* * static int free_mem * * DESCRIPTION: * * Free an address from a memory pool. * * RETURNS: * * Success - MPOOL_ERROR_NONE * * Failure - Mpool error code * * ARGUMENTS: * * mp_p <-> Pointer to the memory pool. If NULL then it will do a * normal free. * * addr <-> Address to free. * * size -> Size of the address being freed. */ static int free_mem(mpool_t *mp_p, void *addr, const unsigned long size) { unsigned long old_size, fence; int ret; mpool_block_t *block_p; /* * If the size is larger than a block then the allocation must be at * the front of the block. */ if (size > MAX_BLOCK_USER_MEMORY(mp_p)) { block_p = (mpool_block_t *)((char *)addr - sizeof(mpool_block_t)); if (block_p->mb_magic != BLOCK_MAGIC || block_p->mb_magic2 != BLOCK_MAGIC) { return MPOOL_ERROR_POOL_OVER; } } /* make sure we have enough bytes */ if (size < MIN_ALLOCATION) { old_size = MIN_ALLOCATION; } else { old_size = size; } /* if we are packing the pool smaller */ if (BIT_IS_SET(mp_p->mp_flags, MPOOL_FLAG_NO_FREE)) { fence = 0; } else { /* find the user's magic numbers if they were written */ ret = check_magic(addr, old_size); if (ret != MPOOL_ERROR_NONE) { return ret; } fence = FENCE_SIZE; } /* now we free the pointer */ ret = free_pointer(mp_p, addr, old_size + fence); if (ret != MPOOL_ERROR_NONE) { return ret; } mp_p->mp_user_alloc -= old_size; /* adjust our stats */ mp_p->mp_alloc_c--; return MPOOL_ERROR_NONE; }
static void core_pfree(void *blk) { palloc_t *m, *n; /* get address of header */ m = (palloc_t *) ((char *)blk - sizeof(palloc_t)); if(check_magic(m) == FALSE) { if(pfree_error_message(m) == TRUE) return; } /* find this block in heap */ n = (palloc_t *) p_heap_bottom; for( ; n != NULL; n = n -> next) { if(check_magic(n) == FALSE) { if(pfree_error_message(n) == TRUE) return; } if(n == m) break; } /* Node not found. Bad pointer or no heap or something else! */ if(n == NULL) { mprintf("pfree: Attempt to free block at 0x%p that is not in the heap.\n", blk); return; } /* free the block */ m -> used = 0; /* do some compaction */ for(m = (palloc_t *)p_heap_bottom; m != NULL; m = m -> next) { while(!(m -> used) && m -> next != NULL && !((m -> next) -> used)) { /* resize this block */ m -> size += sizeof(palloc_t) + (m -> next) -> size; /* merge with next block */ m -> next = (m -> next) -> next; } } }
int pconf_file_begin(PCONF_CTX *ctx, const char *fn) { if (!check_magic(ctx)) return 0; ctx->f = fopen(fn, "r"); if (!ctx->f) { snprintf(ctx->errmsg, PCONF_ERR_LEN, "Can't open %s: %s", fn, strerror(errno)); return 0; } return 1; /* OK */ }
void * ADhash_get (ADhash_t hash, void *key) { struct hash_t *h; struct hash_element_t *p; /* Check hash structure validity */ if ((h = check_magic (hash, ADMAGIC_HASH)) == NULL) return (NULL); /* Look for hash element */ p = hash_lookup (h, key); if (p == NULL) return (NULL); /* Return payload */ return (p->data); }
void ext2fs_free_generic_bitmap(ext2fs_inode_bitmap bitmap) { if (check_magic(bitmap)) return; bitmap->magic = 0; if (bitmap->description) { ext2fs_free_mem(&bitmap->description); bitmap->description = 0; } if (bitmap->bitmap) { ext2fs_free_mem(&bitmap->bitmap); bitmap->bitmap = 0; } ext2fs_free_mem(&bitmap); }
int pconf_file_begin(PCONF_CTX *ctx, const char *fn) { if (!check_magic(ctx)) return 0; errno_t err = fopen_s(&ctx->f, fn, "r"); if (err || !ctx->f) { char errmsg[4096]; strerror_s(errmsg, sizeof(errmsg),err); snprintf(ctx->errmsg, PCONF_ERR_LEN, "Can't open %s: %s", fn, errmsg); return 0; } return 1; /* OK */ }
void ADhash_destroy (ADhash_t hash) { struct hash_t *h; /* Check hash structure validity */ if ((h = check_magic (hash, ADMAGIC_HASH)) == NULL) return; /* Clear hash */ ADhash_clear (hash); /* Invalidate hash structure */ h->magic = 0; /* Free hash buckets and hash structure */ free (h->hash); memset (h, 0, sizeof (struct hash_t)); free (h); }
static int is_emul_openbsd(int fd) { struct exec hdr; int layout, os; if (lseek(fd, 0, SEEK_SET) == -1 || read(fd, &hdr, sizeof(hdr)) != sizeof(hdr)) return 0; os = check_magic(hdr.a_midmag, &layout); if (os == BF_AOUT_386BSD || (os == BF_AOUT_OPENBSD && layout != BF_OMAGIC)) /* XXX - reserved for exos */ return 1; return 0; }
void loadbin(void) { void (*go)(void); struct img_header *head = (struct img_header *)DEFAULT_LOADADDR; struct mtd_info *mtd = get_mtd_info(); #if 1 check_magic(head); mtd->nf_erase(mtd, 0x0, head->img_size); mtd->nf_write(mtd, 0x0, (void *)((u8*)head + head->img_offset), &head->img_size); go = (void *)((u8 *)head + head->img_offset); #else u32 len = 240; mtd->nf_read(mtd, 0, (void *)DEFAULT_LOADADDR, &len); go = (void *)DEFAULT_LOADADDR; #endif // go(); puts("loadbin(): image download complete.\n"); }
AD_size_t ADhash_keys (ADhash_t hash, void **dst, AD_size_t len) { struct hash_t *h; unsigned long left, idx, bucket; struct hash_element_t *p; /* Check hash structure validity */ if ((h = check_magic (hash, ADMAGIC_HASH)) == NULL) return (0); /* Check destination buffer */ if (!dst || !len) return (0); /* Copy keys to destination buffer */ left = h->size; idx = bucket = 0; p = NULL; while ((idx < len) && left) { if (!p && ((p = h->hash[bucket++]) != NULL)) break; dst[idx++] = p; p = p->next; left--; } return (idx); }
/* parse input a character at a time */ int pconf_char(PCONF_CTX *ctx, char ch) { if (!check_magic(ctx)) return -1; /* if the last call finished a line, clean stuff up for another */ if ((ctx->state == STATE_ENDOFLINE) || (ctx->state == STATE_PARSEERR)) { ctx->numargs = 0; ctx->state = STATE_FINDWORDSTART; } ctx->ch = ch; parse_char(ctx); if (ctx->state == STATE_ENDOFLINE) return 1; if (ctx->state == STATE_PARSEERR) return -1; return 0; }